Project

General

Profile

Bug #249 » openwrt_gateway_with_batman_V.sh

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

 
1
#!/bin/ash
2
# Alvaro Lopez Antelo
3
# OpenWRT script to configure a BATMAN-ADV gateway node on TP-LINK WDR-3600
4
# Firmware compiled with all necessary packages and Batman-ADV Version 5 on Chaos Calmer Release
5

    
6
# Router hostname
7
export hostname='gateway'
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='1'
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
# create batman-adv network interface with increased MTU and associate it with mesh
82
uci set network.$interface_name=interface
83
uci set network.$interface_name.mesh=$interface_mesh_name
84
uci set network.$interface_name.proto=batadv
85
uci set network.$interface_name.mtu=1560
86
uci commit network
87

    
88
# create batman-adv network interface with increased MTU and associate it with ETH0.3
89
# This wired interface can extend the mesh network to a monitoring station
90
uci set network.wired_mesh=interface
91
uci set network.wired_mesh.ifname='eth0.3'
92
uci set network.wired_mesh.mtu=1560
93
uci set network.wired_mesh.proto='none'
94
uci commit network
95

    
96
# In order to increase MTU of eth0.3 we need to bump main interface ETH0 to 1560 bytes
97
uci set network.eth0=interface
98
uci set network.eth0.ifname='eth0'
99
uci set network.eth0.mtu=1560
100
uci commit network
101

    
102
# Add VLAN 3 to switch0 and assign it as an untagged interface to port 3
103
# Port 3 need to be removed from VLAN 1 (LAN)
104
# This will be naturally connected subinterface eth0.3 created above
105
# And will be a batman-adv speaking interface to extend this mesh via cable
106
uci set network.@switch_vlan[0].vlan='1'
107
uci set network.@switch_vlan[0].ports='0t 2 4 5'
108
uci set network.@switch_vlan[1].vlan='2'
109
uci set network.@switch_vlan[1].ports='0t 1'
110
uci add network switch_vlan
111
uci set network.@switch_vlan[2]=switch_vlan
112
uci set network.@switch_vlan[2].device='switch0'
113
uci set network.@switch_vlan[2].vlan='3'
114
uci set network.@switch_vlan[2].ports='0t 3'
115
uci commit network
116

    
117
# Set WAN interface protocol and IP
118
uci set network.wan.proto='dhcp'
119
uci set network.wan.mtu=1500
120
#uci set network.wan.proto='static'
121
#uci set network.wan.ipaddr='10.7.162.130'
122
#uci set network.wan.netmask='255.255.255.248'
123
#uci set network.wan.gateway='10.7.162.129'
124
uci commit network
125

    
126
# Disable NAT on WAN but mantain firewall
127
#uci set firewall.@zone[1].conntrack='1'
128
#uci set firewall.@zone[1].masq='0'
129
#uci commit firewall
130

    
131
# Set IPv4 static address on br-lan bridge
132
uci set network.lan.proto='static'
133
uci set network.lan.ipaddr=$br_lan_ip
134
uci set network.lan.netmask=$br_lan_netmask
135
uci set network.lan.mtu='1500'
136
uci commit network
137

    
138
# Enable DHCP server on LAN
139
uci add_list dhcp.lan.dhcp_option='6,'$br_lan_ip,$external_dns_ip
140
uci set dhcp.lan.start=$start_ip_dhcp
141
uci set dhcp.lan.limit=$end_ip_dhcp
142
uci set dhcp.lan.leasetime='1h'
143
uci set dhcp.@dnsmasq[0].domain=$local_domain
144
uci set dhcp.@dnsmasq[0].localservice='0'
145
uci set dhcp.@dnsmasq[0].resolvfile='/etc/resolv.conf'
146
uci set dhcp.lan.ignore='0'
147
uci commit dhcp
148

    
149
# Use external DNS server
150
rm /etc/resolv.conf
151
echo "nameserver 127.0.0.1" > /etc/resolv.conf
152
echo "nameserver " $external_dns_ip >> /etc/resolv.conf
153
echo "search "$local_domain >> /etc/resolv.conf
154

    
155
# create batman-adv config file and configure it
156
uci import -m batman-adv </dev/null
157
uci set batman-adv.$interface_mesh_name='mesh'
158
uci set batman-adv.$interface_mesh_name.interfaces=$interface_ifname0
159
uci set batman-adv.$interface_mesh_name.aggregated_ogms='1'
160
uci set batman-adv.$interface_mesh_name.ap_isolation='0'
161
uci set batman-adv.$interface_mesh_name.bonding='0'
162
uci set batman-adv.$interface_mesh_name.fragmentation='0'
163
uci set batman-adv.$interface_mesh_name.gw_bandwidth=
164
uci set batman-adv.$interface_mesh_name.gw_mode='server'
165
uci set batman-adv.$interface_mesh_name.gw_sel_class='20'
166
uci set batman-adv.$interface_mesh_name.log_level=
167
uci set batman-adv.$interface_mesh_name.orig_interval='1000'
168
uci set batman-adv.$interface_mesh_name.vis_mode='1'
169
uci set batman-adv.$interface_mesh_name.bridge_loop_avoidance='1'
170
uci set batman-adv.$interface_mesh_name.distributed_arp_table='1'
171
uci set batman-adv.$interface_mesh_name.multicast_mode='1'
172
uci set batman-adv.$interface_mesh_name.network_coding=
173
uci set batman-adv.$interface_mesh_name.hop_penalty='15'
174
uci set batman-adv.$interface_mesh_name.isolation_mark=
175
uci set batman-adv.$interface_mesh_name.routing_algo='BATMAN_V'
176
uci commit batman-adv
177

    
178
# bridge br-lan with AP and batman mesh interface
179
uci set network.lan._orig_ifname='eth0.1'
180
uci set network.lan._orig_bridge='true'
181
uci set network.lan.ifname='bat0 eth0.1'
182
uci commit network
183

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

    
190
# Script for MTU adjustment and adhoc interfaces association to batman
191
# Insert on rc.local for execution at the final boot phase
192
# allow 20 seconds for adhoc interfaces to stabilize
193
export TAB="$(printf '\t')"
194
cat <<EOF > /etc/rc.local
195
/bin/sleep 20
196
# Set MTU 1560 on wireless adhoc interfaces as a requirement for batman-adv
197
ip link set mtu 1560 dev adhoc1
198
/usr/bin/logger 'ADHOC1 MTU increased'
199
/bin/echo 'BATMAN_V' > /sys/module/batman_adv/parameters/routing_algo
200
/usr/bin/logger 'Selecting BATMAN_V as routing algorithm'
201
/usr/sbin/batctl if del adhoc1
202
/bin/sleep 1
203
/usr/sbin/batctl if del eth0.3
204
/bin/sleep 1
205
/usr/sbin/batctl if add adhoc1
206
/bin/sleep 1
207
/usr/sbin/batctl if add eth0.3
208
/sbin/ifconfig bat0 up
209
exit 0
210
EOF
211

    
212
# Redirect syslog to management station
213
uci set system.@system[0].log_ip=$management_station_ip
214
uci set system.@system[0].conloglevel=7
215
uci commit
216

    
217
# Configure alfred as a slave node, master will be the monitoring station
218
uci set alfred.alfred.mode='slave'
219
uci set alfred.alfred.start_vis='1'
220
uci set alfred.alfred.disabled='0'
221
uci commit alfred
222

    
223
# Enable SSH from wan
224
uci set firewall.openssh_server=rule
225
uci set firewall.openssh_server.name='ssh from wan to lan'
226
uci set firewall.openssh_server.src='wan'
227
uci set firewall.openssh_server.target='ACCEPT'
228
uci set firewall.openssh_server.proto='tcp'
229
uci set firewall.openssh_server.dest_port='22'
230
uci commit firewall
231
/etc/init.d/firewall restart
232

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

    
236
reboot
(4-4/5)