freedombone-mesh-batman 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Used to enable or disable batman mesh protocol on wlanX
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. PROJECT_NAME='freedombone'
  31. COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
  32. HOTSPOT_PASSPHRASE='mesh'
  33. function count_wlan {
  34. # counts the number of wlan devices
  35. ctr=0
  36. for i in $(seq 0 1 10); do
  37. if grep -q "wlan${i}" /proc/net/dev; then
  38. ctr=$((ctr + 1))
  39. fi
  40. done
  41. echo $ctr
  42. }
  43. function update_wifi_adaptors {
  44. IFACE=
  45. IFACE_SECONDARY=
  46. for i in $(seq 10 -1 0); do
  47. if grep -q "wlan${i}" /proc/net/dev; then
  48. if [ ! $IFACE ]; then
  49. IFACE="wlan${i}"
  50. else
  51. IFACE_SECONDARY="wlan${i}"
  52. return
  53. fi
  54. fi
  55. done
  56. }
  57. if [[ $1 == "start" ]]; then
  58. # install avahi
  59. sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
  60. sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
  61. sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf
  62. sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf
  63. sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf
  64. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  65. fi
  66. # Mesh definition
  67. WIFI_SSID='mesh'
  68. if [ -f $COMPLETION_FILE ]; then
  69. if grep -q "WIFI_SSID:" $COMPLETION_FILE; then
  70. WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}')
  71. fi
  72. sed -i "s|WIFI_SSID:.*|WIFI_SSID:${WIFI_SSID}|g" $COMPLETION_FILE
  73. fi
  74. CELLID='any'
  75. CHANNEL=2
  76. if [ -f $COMPLETION_FILE ]; then
  77. if grep -q "Wifi channel:" $COMPLETION_FILE; then
  78. CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}')
  79. fi
  80. sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE
  81. fi
  82. ZERONET_PORT=15441
  83. IPFS_PORT=4001
  84. TOX_PORT=33445
  85. TRACKER_PORT=6969
  86. LIBREVAULT_PORT=42345
  87. TAHOELAFS_PORT=50213
  88. # Ethernet bridge definition (bridged to bat0)
  89. BRIDGE=br-mesh
  90. BRIDGE_HOTSPOT=br-hotspot
  91. IFACE=
  92. IFACE_SECONDARY=
  93. EIFACE=eth0
  94. WLAN_ADAPTORS=$(count_wlan)
  95. if [ $WLAN_ADAPTORS -eq 0 ]; then
  96. echo $'No wlan adaptors found'
  97. exit 0
  98. fi
  99. update_wifi_adaptors
  100. if [ ! $IFACE ]; then
  101. echo $'No wlan adaptor'
  102. exit 0
  103. fi
  104. if [ -e /etc/default/batctl ]; then
  105. . /etc/default/batctl
  106. fi
  107. function global_rate_limit {
  108. if ! grep -q "tcp_challenge_ack_limit" /etc/sysctl.conf; then
  109. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> /etc/sysctl.conf
  110. else
  111. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' /etc/sysctl.conf
  112. fi
  113. sysctl -p -q
  114. }
  115. function status {
  116. batctl o
  117. }
  118. function stop {
  119. if [ -z "$IFACE" ]; then
  120. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  121. return
  122. fi
  123. if [ "$EIFACE" ]; then
  124. brctl delif $BRIDGE bat0
  125. brctl delif $BRIDGE $EIFACE
  126. ifconfig $BRIDGE down || true
  127. brctl delbr $BRIDGE
  128. ifconfig $EIFACE down -promisc
  129. fi
  130. if [ $IFACE_SECONDARY ]; then
  131. systemctl stop hostapd
  132. brctl delif $BRIDGE_HOTSPOT bat0
  133. ifconfig $BRIDGE_HOTSPOT down || true
  134. brctl delbr $BRIDGE_HOTSPOT
  135. fi
  136. avahi-autoipd -k $BRIDGE
  137. avahi-autoipd -k $IFACE
  138. ifconfig bat0 down -promisc
  139. batctl if del $IFACE
  140. rmmod batman-adv
  141. ifconfig $IFACE mtu 1500
  142. ifconfig $IFACE down
  143. iwconfig $IFACE mode managed
  144. iptables -D INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
  145. iptables -D INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
  146. iptables -D INPUT -p tcp --dport 80 -j ACCEPT
  147. iptables -D INPUT -p udp --dport 80 -j ACCEPT
  148. iptables -D INPUT -p tcp --dport 548 -j ACCEPT
  149. iptables -D INPUT -p udp --dport 548 -j ACCEPT
  150. iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
  151. iptables -D INPUT -p udp --dport 5353 -j ACCEPT
  152. iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
  153. iptables -D INPUT -p udp --dport 5354 -j ACCEPT
  154. iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  155. iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  156. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  157. iptables -D INPUT -p udp --dport $IPFS_PORT -j ACCEPT
  158. iptables -D INPUT -p tcp --dport $TOX_PORT -j ACCEPT
  159. iptables -D INPUT -p udp --dport $TOX_PORT -j ACCEPT
  160. iptables -D INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
  161. iptables -D INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
  162. iptables -D INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  163. systemctl restart network-manager
  164. }
  165. function verify {
  166. tempfile="$(mktemp)"
  167. batctl o > $tempfile
  168. if grep -q "disabled" $tempfile; then
  169. echo $'B.A.T.M.A.N. not enabled'
  170. rm $tempfile
  171. stop
  172. exit 726835
  173. fi
  174. echo $'B.A.T.M.A.N. is running'
  175. rm $tempfile
  176. }
  177. function assign_peer_address {
  178. for i in {1..6}; do
  179. number=$RANDOM
  180. let "number %= 255"
  181. octet=$(echo "obase=16;$number" | bc)
  182. if [ ${#octet} -lt 2 ]; then
  183. octet="0${octet}"
  184. fi
  185. if [ $i -gt 1 ]; then
  186. echo -n ":"
  187. fi
  188. echo -n "${octet}"
  189. done
  190. echo ''
  191. }
  192. function start {
  193. if [ -z "$IFACE" ] ; then
  194. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  195. exit 723657
  196. fi
  197. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  198. systemctl stop network-manager
  199. sleep 5
  200. # remove an avahi service which isn't used
  201. if [ -f /etc/avahi/services/udisks.service ]; then
  202. sudo rm /etc/avahi/services/udisks.service
  203. fi
  204. global_rate_limit
  205. # Might have to re-enable wifi
  206. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  207. ifconfig $IFACE down
  208. ifconfig $IFACE mtu 1532
  209. ifconfig $IFACE hw ether $(assign_peer_address)
  210. iwconfig $IFACE enc off
  211. iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
  212. sleep 1
  213. iwconfig $IFACE ap $CELLID
  214. modprobe batman-adv
  215. batctl if add $IFACE
  216. ifconfig $IFACE up
  217. avahi-autoipd --force-bind --daemonize --wait $IFACE
  218. ifconfig bat0 up promisc
  219. #Use persistent HWAddr
  220. ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
  221. if [ ! -f /var/lib/mesh-node/bat0 ]; then
  222. mkdir /var/lib/mesh-node
  223. echo "${ether_new}" > /var/lib/mesh-node/bat0
  224. else
  225. ether=$(cat /var/lib/mesh-node/bat0)
  226. ifconfig bat0 hw ether ${ether}
  227. fi
  228. if [ "$EIFACE" ] ; then
  229. brctl addbr $BRIDGE
  230. brctl addif $BRIDGE bat0
  231. brctl addif $BRIDGE $EIFACE
  232. ifconfig bat0 0.0.0.0
  233. ifconfig $EIFACE 0.0.0.0
  234. ifconfig $EIFACE up promisc
  235. ifconfig $BRIDGE up
  236. avahi-autoipd --force-bind --daemonize --wait $BRIDGE
  237. fi
  238. if [ $IFACE_SECONDARY ]; then
  239. if [[ $IFACE != $IFACE_SECONDARY ]]; then
  240. if [ -d /etc/hostapd ]; then
  241. # bridge between mesh and wifi hotspot for mobile
  242. HOTSPOT_NAME=$"${WIFI_SSID}-hotspot"
  243. ifconfig $IFACE_SECONDARY down
  244. ifconfig $IFACE_SECONDARY mtu 1500
  245. ifconfig $IFACE_SECONDARY hw ether $(assign_peer_address)
  246. iwconfig $IFACE_SECONDARY enc open
  247. iwconfig $IFACE_SECONDARY mode managed essid $HOTSPOT_NAME channel ${CHANNEL}
  248. iwconfig $IFACE_SECONDARY ap $CELLID
  249. brctl addbr $BRIDGE_HOTSPOT
  250. brctl addif $BRIDGE_HOTSPOT bat0
  251. brctl addif $BRIDGE_HOTSPOT $IFACE_SECONDARY
  252. ifconfig bat0 0.0.0.0
  253. ifconfig $IFACE_SECONDARY 0.0.0.0
  254. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  255. echo "interface=${IFACE_SECONDARY}" > /etc/hostapd/hostapd.conf
  256. echo "bridge=${BRIDGE_HOTSPOT}" >> /etc/hostapd/hostapd.conf
  257. echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
  258. echo "country_code=UK" >> /etc/hostapd/hostapd.conf
  259. echo "ssid=$HOTSPOT_NAME" >> /etc/hostapd/hostapd.conf
  260. echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
  261. echo "channel=${CHANNEL}" >> /etc/hostapd/hostapd.conf
  262. echo 'wpa=2' >> /etc/hostapd/hostapd.conf
  263. echo "wpa_passphrase=$HOTSPOT_PASSPHRASE" >> /etc/hostapd/hostapd.conf
  264. echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
  265. echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
  266. echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
  267. echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
  268. echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
  269. ifconfig $BRIDGE_HOTSPOT up
  270. avahi-autoipd --force-bind --daemonize --wait $BRIDGE_HOTSPOT
  271. ifconfig $IFACE_SECONDARY up promisc
  272. #ifconfig $IFACE_SECONDARY auto-dhcp start
  273. systemctl start hostapd
  274. fi
  275. fi
  276. fi
  277. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  278. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  279. iptables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
  280. iptables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
  281. iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  282. iptables -A INPUT -p udp --dport 80 -j ACCEPT
  283. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  284. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  285. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  286. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  287. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  288. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  289. iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  290. iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  291. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  292. iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT
  293. iptables -A INPUT -p udp --dport $TOX_PORT -j ACCEPT
  294. iptables -A INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
  295. iptables -A INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
  296. iptables -A INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  297. systemctl restart avahi-daemon
  298. verify
  299. }
  300. function monitor {
  301. if [ -z "$IFACE" ] ; then
  302. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  303. exit 723657
  304. fi
  305. stop
  306. echo "info: monitoring mesh network $WIFI_SSID on $IFACE"
  307. systemctl stop network-manager
  308. sleep 5
  309. global_rate_limit
  310. # Might have to re-enable wifi
  311. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  312. ifconfig $IFACE down
  313. ifconfig $IFACE mtu 1532
  314. ifconfig $IFACE hw ether $(assign_peer_address)
  315. iwconfig $IFACE enc off
  316. iwconfig $IFACE mode monitor channel $CHANNEL
  317. sleep 1
  318. iwconfig $IFACE ap $CELLID
  319. modprobe batman-adv
  320. batctl if add $IFACE
  321. ifconfig $IFACE up
  322. horst -i $IFACE
  323. start
  324. }
  325. if ! grep -q "$IFACE" /proc/net/dev; then
  326. echo 'Interface $IFACE was not found'
  327. stop
  328. exit 1
  329. fi
  330. case "$1" in
  331. start|stop|status|monitor)
  332. $1
  333. ;;
  334. restart)
  335. stop
  336. sleep 10
  337. start
  338. ;;
  339. ping)
  340. batctl ping $2
  341. ;;
  342. data)
  343. watch -n1 "batctl s | grep mgmt | grep bytes"
  344. ;;
  345. ls|list)
  346. avahi-browse -atl
  347. ;;
  348. *)
  349. echo "error: invalid parameter $1"
  350. echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
  351. exit 2
  352. ;;
  353. esac
  354. exit 0