freedombone-mesh-batman 12KB

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