freedombone-mesh-batman 13KB

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