freedombone-mesh-batman 12KB

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