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

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

    
6
# Router hostname
7
export hostname='node-1'
8

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

    
37
# Set hostname on router and dhcp interfaces, set Brasil timezone
38
uci set system.@system[0].hostname=$hostname
39
uci set system.@system[0].timezone=BRT3BRST,M10.3.0/0,M2.3.0/0
40
uci set system.@system[0].zonename='America/Sao Paulo'
41
uci set network.lan.hostname=$hostname
42
uci set network.wan.hostname=$hostname
43
uci commit system
44
uci commit network
45

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

    
52
# Disable radio0 @ 2.4Ghz
53
uci set wireless.radio0.disabled='0'
54
uci set wireless.radio0.channel=$mesh_channel_radio0
55
uci set wireless.radio0.hwmode='11g'
56
uci set wireless.radio0.htmode='HT20'
57
uci set wireless.radio0.noscan='1'
58
uci set wireless.radio0.txpower='21'
59
uci set wireless.radio0.country='00'
60
uci commit wireless
61

    
62
# enable adhoc mesh, on radio1 @ 5Ghz
63
uci set wireless.radio1.disabled='0'
64
uci set wireless.radio1.channel=$mesh_channel_radio1
65
uci set wireless.radio1.hwmode='11a'
66
uci set wireless.radio1.htmode='HT40'
67
uci set wireless.radio1.noscan='1'
68
uci set wireless.radio1.txpower='19'
69
uci set wireless.radio1.country='00'
70
uci add wireless wifi-iface
71
uci set wireless.@wifi-iface[0].device=radio1
72
uci set wireless.@wifi-iface[0].mode=adhoc
73
uci set wireless.@wifi-iface[0].ssid=$mesh_ssid
74
uci set wireless.@wifi-iface[0].bssid=$mesh_bssid
75
uci set wireless.@wifi-iface[0].mcast_rate=18000
76
uci set wireless.@wifi-iface[0].ifname=$interface_ifname1
77
uci set wireless.@wifi-iface[0].network=$interface_name
78
uci set wireless.@wifi-iface[0].hidden='1'
79
uci commit wireless
80

    
81
# enable AP on radio0 @ 2.4Ghz
82
uci add wireless wifi-iface
83
uci set wireless.@wifi-iface[1].device='radio0'
84
uci set wireless.@wifi-iface[1].mode='ap'
85
uci set wireless.@wifi-iface[1].ssid=$ap_ssid
86
uci set wireless.@wifi-iface[1].encryption='psk2'
87
uci set wireless.@wifi-iface[1].key=$ap_key
88
uci set wireless.@wifi-iface[1].network='lan'
89
uci set wireless.@wifi-iface[1].ieee80211r='1'
90
uci set wireless.@wifi-iface[1].mobility_domain='4f57'
91
uci set wireless.@wifi-iface[1].nasid='meshmobility'
92
uci set wireless.@wifi-iface[1].pmk_r1_push='1'
93
uci commit wireless
94

    
95
# enable AP on radio1 @ 2.4Ghz
96
uci add wireless wifi-iface
97
uci set wireless.@wifi-iface[2].device='radio1'
98
uci set wireless.@wifi-iface[2].mode='ap'
99
uci set wireless.@wifi-iface[2].ssid=$ap_ssid
100
uci set wireless.@wifi-iface[2].encryption='psk2'
101
uci set wireless.@wifi-iface[2].key=$ap_key
102
uci set wireless.@wifi-iface[2].network='lan'
103
uci set wireless.@wifi-iface[2].ieee80211r='1'
104
uci set wireless.@wifi-iface[2].mobility_domain='4f57'
105
uci set wireless.@wifi-iface[2].nasid='meshmobility'
106
uci set wireless.@wifi-iface[2].pmk_r1_push='1'
107
uci commit wireless
108

    
109
# create batman-adv network interface with increased MTU and associate it with mesh
110
uci set network.$interface_name=interface
111
uci set network.$interface_name.mesh=$interface_mesh_name
112
uci set network.$interface_name.proto=batadv
113
uci set network.$interface_name.mtu=1560
114
uci commit network
115

    
116
# Set br-lan to dhcp client, remove remaining static configs
117
uci set network.lan.type='bridge'
118
uci set network.lan.proto='dhcp'
119
uci delete network.lan.ipaddr
120
uci delete network.lan.netmask
121
uci delete network.lan.gateway
122
uci commit network
123

    
124
# Configure WAN interface protocol
125
uci set network.wan.proto='dhcp'
126
uci commit network
127

    
128
# Disable DHCP server on lan interfaces
129
uci set dhcp.lan.dhcpv6='disabled'
130
uci set dhcp.lan.ra='disabled'
131
uci set dhcp.lan.ignore='1'
132
uci commit dhcp
133

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

    
138

    
139
# create batman-adv config file and configure it
140
uci import -m batman-adv </dev/null
141
uci set batman-adv.$interface_mesh_name='mesh'
142
uci set batman-adv.$interface_mesh_name.interfaces=$interface_ifname0
143
uci set batman-adv.$interface_mesh_name.aggregated_ogms='1'
144
uci set batman-adv.$interface_mesh_name.ap_isolation='0'
145
uci set batman-adv.$interface_mesh_name.bonding='0'
146
uci set batman-adv.$interface_mesh_name.fragmentation='0'
147
uci set batman-adv.$interface_mesh_name.gw_bandwidth=
148
uci set batman-adv.$interface_mesh_name.gw_mode='client'
149
uci set batman-adv.$interface_mesh_name.gw_sel_class='20'
150
uci set batman-adv.$interface_mesh_name.log_level=
151
uci set batman-adv.$interface_mesh_name.orig_interval='1000'
152
uci set batman-adv.$interface_mesh_name.vis_mode='1'
153
uci set batman-adv.$interface_mesh_name.bridge_loop_avoidance='1'
154
uci set batman-adv.$interface_mesh_name.distributed_arp_table='1'
155
uci set batman-adv.$interface_mesh_name.multicast_mode='1'
156
uci set batman-adv.$interface_mesh_name.network_coding=
157
uci set batman-adv.$interface_mesh_name.hop_penalty='15'
158
uci set batman-adv.$interface_mesh_name.isolation_mark=
159
uci set batman-adv.$interface_mesh_name.routing_algo='BATMAN_V'
160
uci commit batman-adv
161

    
162
# bridge br-lan with batman mesh interface
163
uci set network.lan._orig_ifname='eth0.1'
164
uci set network.lan._orig_bridge='true'
165
uci set network.lan.ifname='bat0 eth0.1'
166
uci commit network
167

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

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

    
192

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

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

    
204
# Disable Firewall
205
/etc/init.d/firewall stop
206
/etc/init.d/firewall disable
207

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

    
211
reboot
(3-3/5)