Project

General

Profile

Bug #249 » mesh_node_with_batman_V.sh

Script to configure the mesh node - Alvaro Antelo, 03/25/2016 12:18 AM

 
#!/bin/ash
# Alvaro Lopez Antelo
# OpenWRT script to configure a BATMAN-ADV mesh node
# Tailored to TP-LINK WDR-3600 on Chaos Calmer Release

# Router hostname
export hostname='node-1'

# Global variables
# Default mesh interface names and radio channels. Customize them at will.
export interface_name='mesh'
export interface_mesh_name='bat0'
export interface_ifname0='adhoc0'
export interface_ifname1='adhoc1'
export mesh_channel_radio0='9'
export mesh_channel_radio1='149'
# Mesh SSID and BSSID. Must be the same for this particular mesh deployment
export mesh_ssid='02_CA_FE_CA_00_10'
export mesh_bssid='02:CA:FE:CA:00:10'
# WPA-2 key hash derived with the following tool
# https://www.wireshark.org/tools/wpa-psk.html - WPA PSK Calculator
export ap_ssid='mesh'
export ap_key='92c013cfff68a59a38a2a77da861a8cbc676e916c0446ff7b3e8d1649f3a51fe'
# Management SNMP station IPv4 address
export management_station_ip='10.61.33.1'
# Only the gateway node has a DHCP server and local IPv4 address on LAN interface
export br_lan_ip='10.61.33.254'
export br_lan_netmask='255.255.255.0'
# Mesh internal DHCP range, excluding gateway and monitoring addresses
export start_ip_dhcp='2'
export end_ip_dhcp='251'
# internal domain name, for mesh nodes
export local_domain='mesh.lan'
# External backhaul link supplied DNS server to be passed along to mesh nodes and clients
export external_dns_ip='8.8.8.8'

# Set hostname on router and dhcp interfaces, set Brasil timezone
uci set system.@system[0].hostname=$hostname
uci set system.@system[0].timezone=BRT3BRST,M10.3.0/0,M2.3.0/0
uci set system.@system[0].zonename='America/Sao Paulo'
uci set network.lan.hostname=$hostname
uci set network.wan.hostname=$hostname
uci commit system
uci commit network

# Start with a clean wireless config - generate brand new one and delete preexisting interfaces
rm /etc/config/wireless
wifi detect >>/etc/config/wireless
( for i in `seq 0 9` ; do echo "delete wireless.@wifi-iface[]" ; done ) | uci batch -q
uci commit wireless

# Disable radio0 @ 2.4Ghz
uci set wireless.radio0.disabled='0'
uci set wireless.radio0.channel=$mesh_channel_radio0
uci set wireless.radio0.hwmode='11g'
uci set wireless.radio0.htmode='HT20'
uci set wireless.radio0.noscan='1'
uci set wireless.radio0.txpower='21'
uci set wireless.radio0.country='00'
uci commit wireless

# enable adhoc mesh, on radio1 @ 5Ghz
uci set wireless.radio1.disabled='0'
uci set wireless.radio1.channel=$mesh_channel_radio1
uci set wireless.radio1.hwmode='11a'
uci set wireless.radio1.htmode='HT40'
uci set wireless.radio1.noscan='1'
uci set wireless.radio1.txpower='19'
uci set wireless.radio1.country='00'
uci add wireless wifi-iface
uci set wireless.@wifi-iface[0].device=radio1
uci set wireless.@wifi-iface[0].mode=adhoc
uci set wireless.@wifi-iface[0].ssid=$mesh_ssid
uci set wireless.@wifi-iface[0].bssid=$mesh_bssid
uci set wireless.@wifi-iface[0].mcast_rate=18000
uci set wireless.@wifi-iface[0].ifname=$interface_ifname1
uci set wireless.@wifi-iface[0].network=$interface_name
uci set wireless.@wifi-iface[0].hidden='1'
uci commit wireless

# enable AP on radio0 @ 2.4Ghz
uci add wireless wifi-iface
uci set wireless.@wifi-iface[1].device='radio0'
uci set wireless.@wifi-iface[1].mode='ap'
uci set wireless.@wifi-iface[1].ssid=$ap_ssid
uci set wireless.@wifi-iface[1].encryption='psk2'
uci set wireless.@wifi-iface[1].key=$ap_key
uci set wireless.@wifi-iface[1].network='lan'
uci set wireless.@wifi-iface[1].ieee80211r='1'
uci set wireless.@wifi-iface[1].mobility_domain='4f57'
uci set wireless.@wifi-iface[1].nasid='meshmobility'
uci set wireless.@wifi-iface[1].pmk_r1_push='1'
uci commit wireless

# enable AP on radio1 @ 2.4Ghz
uci add wireless wifi-iface
uci set wireless.@wifi-iface[2].device='radio1'
uci set wireless.@wifi-iface[2].mode='ap'
uci set wireless.@wifi-iface[2].ssid=$ap_ssid
uci set wireless.@wifi-iface[2].encryption='psk2'
uci set wireless.@wifi-iface[2].key=$ap_key
uci set wireless.@wifi-iface[2].network='lan'
uci set wireless.@wifi-iface[2].ieee80211r='1'
uci set wireless.@wifi-iface[2].mobility_domain='4f57'
uci set wireless.@wifi-iface[2].nasid='meshmobility'
uci set wireless.@wifi-iface[2].pmk_r1_push='1'
uci commit wireless

# create batman-adv network interface with increased MTU and associate it with mesh
uci set network.$interface_name=interface
uci set network.$interface_name.mesh=$interface_mesh_name
uci set network.$interface_name.proto=batadv
uci set network.$interface_name.mtu=1560
uci commit network

# Set br-lan to dhcp client, remove remaining static configs
uci set network.lan.type='bridge'
uci set network.lan.proto='dhcp'
uci delete network.lan.ipaddr
uci delete network.lan.netmask
uci delete network.lan.gateway
uci commit network

# Configure WAN interface protocol
uci set network.wan.proto='dhcp'
uci commit network

# Disable DHCP server on lan interfaces
uci set dhcp.lan.dhcpv6='disabled'
uci set dhcp.lan.ra='disabled'
uci set dhcp.lan.ignore='1'
uci commit dhcp

# Adjust DNS settings to use DHCP offered server
rm /etc/resolv.conf
ln -nsf /tmp/resolv.conf.auto /etc/resolv.conf


# create batman-adv config file and configure it
uci import -m batman-adv </dev/null
uci set batman-adv.$interface_mesh_name='mesh'
uci set batman-adv.$interface_mesh_name.interfaces=$interface_ifname0
uci set batman-adv.$interface_mesh_name.aggregated_ogms='1'
uci set batman-adv.$interface_mesh_name.ap_isolation='0'
uci set batman-adv.$interface_mesh_name.bonding='0'
uci set batman-adv.$interface_mesh_name.fragmentation='0'
uci set batman-adv.$interface_mesh_name.gw_bandwidth=
uci set batman-adv.$interface_mesh_name.gw_mode='client'
uci set batman-adv.$interface_mesh_name.gw_sel_class='20'
uci set batman-adv.$interface_mesh_name.log_level=
uci set batman-adv.$interface_mesh_name.orig_interval='1000'
uci set batman-adv.$interface_mesh_name.vis_mode='1'
uci set batman-adv.$interface_mesh_name.bridge_loop_avoidance='1'
uci set batman-adv.$interface_mesh_name.distributed_arp_table='1'
uci set batman-adv.$interface_mesh_name.multicast_mode='1'
uci set batman-adv.$interface_mesh_name.network_coding=
uci set batman-adv.$interface_mesh_name.hop_penalty='15'
uci set batman-adv.$interface_mesh_name.isolation_mark=
uci set batman-adv.$interface_mesh_name.routing_algo='BATMAN_V'
uci commit batman-adv

# bridge br-lan with batman mesh interface
uci set network.lan._orig_ifname='eth0.1'
uci set network.lan._orig_bridge='true'
uci set network.lan.ifname='bat0 eth0.1'
uci commit network

# Configure SNMP daemon local coordinates and contacts
uci set snmpd.@system[-1].sysLocation='[-22.9785166,-43.1908759]'
uci set snmpd.@system[-1].sysContact='alvaro.antelo@gmail.com'
uci set snmpd.@system[-1].sysName=$(uci get system.@system[0].hostname)
uci commit

# Script for MTU adjustment and adhoc interfaces association to batman
# Insert on rc.local for execution at the final boot phase
# allow 20 seconds for adhoc interfaces to stabilize
export TAB="$(printf '\t')"
cat <<EOF > /etc/rc.local
/bin/sleep 20
# Set MTU 1560 on wireless adhoc interfaces as a requirement for batman-adv
ip link set mtu 1560 dev adhoc1
/usr/bin/logger 'ADHOC1 MTU increased'
/bin/echo 'BATMAN_V' > /sys/module/batman_adv/parameters/routing_algo
/usr/sbin/batctl if del adhoc1
/bin/sleep 1
/usr/sbin/batctl if add adhoc1
/bin/sleep 1
/sbin/ifconfig bat0 up
exit 0
EOF


# Send syslog to management station
uci set system.@system[0].log_ip=$management_station_ip
uci set system.@system[0].conloglevel=7
uci commit

# Configure alfred as a slave node, the master will be the monitoring station (raspberry pi)
uci set alfred.alfred.mode='slave'
uci set alfred.alfred.start_vis='1'
uci set alfred.alfred.disabled='0'
uci commit alfred

# Disable Firewall
/etc/init.d/firewall stop
/etc/init.d/firewall disable

# generate ssh password on router, disabling telnet and enabling dropbear ssh server
passwd

reboot
(3-3/5)