freedombone-mesh-batman 14KB

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