freedombone-mesh-batman 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. systemctl restart network-manager
  142. }
  143. function verify {
  144. tempfile="$(mktemp)"
  145. batctl o > $tempfile
  146. if grep -q "disabled" $tempfile; then
  147. echo $'B.A.T.M.A.N. not enabled'
  148. rm $tempfile
  149. stop
  150. exit 726835
  151. fi
  152. echo $'B.A.T.M.A.N. is running'
  153. rm $tempfile
  154. }
  155. function assign_peer_address {
  156. for i in {1..6}; do
  157. number=$RANDOM
  158. let "number %= 255"
  159. octet=$(echo "obase=16;$number" | bc)
  160. if [ ${#octet} -lt 2 ]; then
  161. octet="0${octet}"
  162. fi
  163. if [ $i -gt 1 ]; then
  164. echo -n ":"
  165. fi
  166. echo -n "${octet}"
  167. done
  168. echo ''
  169. }
  170. function start {
  171. if [ -z "$IFACE" ] ; then
  172. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  173. exit 723657
  174. fi
  175. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  176. systemctl stop network-manager
  177. sleep 5
  178. # remove an avahi service which isn't used
  179. if [ -f /etc/avahi/services/udisks.service ]; then
  180. sudo rm /etc/avahi/services/udisks.service
  181. fi
  182. global_rate_limit
  183. # Might have to re-enable wifi
  184. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  185. ifconfig $IFACE down
  186. ifconfig $IFACE mtu 1532
  187. ifconfig $IFACE hw ether $(assign_peer_address)
  188. iwconfig $IFACE enc off
  189. iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
  190. sleep 1
  191. iwconfig $IFACE ap $CELLID
  192. modprobe batman-adv
  193. batctl if add $IFACE
  194. ifconfig $IFACE up
  195. avahi-autoipd --force-bind --daemonize --wait $IFACE
  196. ifconfig bat0 up promisc
  197. #Use persistent HWAddr
  198. ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
  199. if [ ! -f /var/lib/mesh-node/bat0 ]; then
  200. mkdir /var/lib/mesh-node
  201. echo "${ether_new}" > /var/lib/mesh-node/bat0
  202. else
  203. ether=$(cat /var/lib/mesh-node/bat0)
  204. ifconfig bat0 hw ether ${ether}
  205. fi
  206. if [ "$EIFACE" ] ; then
  207. brctl addbr $BRIDGE
  208. brctl addif $BRIDGE bat0
  209. brctl addif $BRIDGE $EIFACE
  210. ifconfig bat0 0.0.0.0
  211. ifconfig $EIFACE 0.0.0.0
  212. ifconfig $EIFACE up promisc
  213. ifconfig $BRIDGE up
  214. avahi-autoipd --force-bind --daemonize --wait $BRIDGE
  215. fi
  216. if [ $IFACE_SECONDARY ]; then
  217. if [[ $IFACE != $IFACE_SECONDARY ]]; then
  218. if [ -d /etc/hostapd ]; then
  219. # bridge between mesh and wifi hotspot for mobile
  220. HOTSPOT_NAME=$"${WIFI_SSID}-hotspot"
  221. ifconfig $IFACE_SECONDARY down
  222. ifconfig $IFACE_SECONDARY mtu 1500
  223. ifconfig $IFACE_SECONDARY hw ether $(assign_peer_address)
  224. iwconfig $IFACE_SECONDARY enc open
  225. iwconfig $IFACE_SECONDARY mode managed essid $HOTSPOT_NAME channel ${HOTSPOT_CHANNEL}
  226. iwconfig $IFACE_SECONDARY ap $CELLID
  227. brctl addbr $BRIDGE_HOTSPOT
  228. brctl addif $BRIDGE_HOTSPOT bat0
  229. brctl addif $BRIDGE_HOTSPOT $IFACE_SECONDARY
  230. ifconfig bat0 0.0.0.0
  231. ifconfig $IFACE_SECONDARY 0.0.0.0
  232. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  233. echo "interface=${IFACE_SECONDARY}" > /etc/hostapd/hostapd.conf
  234. echo "bridge=${BRIDGE_HOTSPOT}" >> /etc/hostapd/hostapd.conf
  235. echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
  236. echo "country_code=UK" >> /etc/hostapd/hostapd.conf
  237. echo "ssid=$HOTSPOT_NAME" >> /etc/hostapd/hostapd.conf
  238. echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
  239. echo "channel=${HOTSPOT_CHANNEL}" >> /etc/hostapd/hostapd.conf
  240. echo 'wpa=2' >> /etc/hostapd/hostapd.conf
  241. echo "wpa_passphrase=$HOTSPOT_PASSPHRASE" >> /etc/hostapd/hostapd.conf
  242. echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
  243. echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
  244. echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
  245. echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
  246. echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
  247. ifconfig $BRIDGE_HOTSPOT up
  248. avahi-autoipd --force-bind --daemonize --wait $BRIDGE_HOTSPOT
  249. ifconfig $IFACE_SECONDARY up promisc
  250. #ifconfig $IFACE_SECONDARY auto-dhcp start
  251. systemctl start hostapd
  252. fi
  253. fi
  254. fi
  255. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  256. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  257. iptables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
  258. iptables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
  259. iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  260. iptables -A INPUT -p udp --dport 80 -j ACCEPT
  261. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  262. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  263. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  264. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  265. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  266. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  267. iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  268. iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  269. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  270. iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT
  271. iptables -A INPUT -p udp --dport $TOX_PORT -j ACCEPT
  272. iptables -A INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
  273. iptables -A INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
  274. iptables -A INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  275. systemctl restart avahi-daemon
  276. verify
  277. }
  278. function monitor {
  279. if [ -z "$IFACE" ] ; then
  280. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  281. exit 723657
  282. fi
  283. stop
  284. echo "info: monitoring mesh network $WIFI_SSID on $IFACE"
  285. systemctl stop network-manager
  286. sleep 5
  287. global_rate_limit
  288. # Might have to re-enable wifi
  289. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  290. ifconfig $IFACE down
  291. ifconfig $IFACE mtu 1532
  292. ifconfig $IFACE hw ether $(assign_peer_address)
  293. iwconfig $IFACE enc off
  294. iwconfig $IFACE mode monitor channel $CHANNEL
  295. sleep 1
  296. iwconfig $IFACE ap $CELLID
  297. modprobe batman-adv
  298. batctl if add $IFACE
  299. ifconfig $IFACE up
  300. horst -i $IFACE
  301. start
  302. }
  303. if ! grep -q "$IFACE" /proc/net/dev; then
  304. echo 'Interface $IFACE was not found'
  305. stop
  306. exit 1
  307. fi
  308. case "$1" in
  309. start|stop|status|monitor)
  310. $1
  311. ;;
  312. restart)
  313. stop
  314. sleep 10
  315. start
  316. ;;
  317. ping)
  318. batctl ping $2
  319. ;;
  320. data)
  321. watch -n1 "batctl s | grep mgmt | grep bytes"
  322. ;;
  323. ls|list)
  324. avahi-browse -atl
  325. ;;
  326. *)
  327. echo "error: invalid parameter $1"
  328. echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
  329. exit 2
  330. ;;
  331. esac
  332. exit 0