freedombone-mesh-batman 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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@robotics.uk.to>
  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. if [[ $1 == "start" ]]; then
  33. # install avahi
  34. sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
  35. sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
  36. sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf
  37. sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf
  38. sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf
  39. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  40. fi
  41. # Mesh definition
  42. WIFI_SSID=
  43. if ! grep -q "WIFI_SSID:" $COMPLETION_FILE; then
  44. WIFI_SSID='mesh'
  45. else
  46. WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}')
  47. fi
  48. sed -i "s|WIFI_SSID:.*|WIFI_SSID:${WIFI_SSID}|g" $COMPLETION_FILE
  49. CELLID='any'
  50. CHANNEL=
  51. if ! grep -q "Wifi channel:" $COMPLETION_FILE; then
  52. CHANNEL=2
  53. else
  54. CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}')
  55. fi
  56. sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE
  57. ZERONET_PORT=15441
  58. IPFS_PORT=4001
  59. # Ethernet bridge definition (bridged to bat0)
  60. BRIDGE=br-mesh
  61. IFACE='wlan0'
  62. EIFACE=eth0
  63. if [[ $IFACE == "wlan0" ]]; then
  64. if grep -q "wlan1" /proc/net/dev; then
  65. IFACE=wlan1
  66. fi
  67. fi
  68. if [[ $IFACE == "wlan0" ]]; then
  69. if grep -q "wlan2" /proc/net/dev; then
  70. IFACE=wlan2
  71. fi
  72. fi
  73. if [[ $IFACE == "wlan0" ]]; then
  74. if grep -q "wlan3" /proc/net/dev; then
  75. IFACE=wlan3
  76. fi
  77. fi
  78. if [ -e /etc/default/batctl ]; then
  79. . /etc/default/batctl
  80. fi
  81. function global_rate_limit {
  82. if ! grep -q "tcp_challenge_ack_limit" /etc/sysctl.conf; then
  83. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> /etc/sysctl.conf
  84. else
  85. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' /etc/sysctl.conf
  86. fi
  87. sysctl -p
  88. }
  89. function status {
  90. batctl o
  91. }
  92. function stop {
  93. if [ -z "$IFACE" ]; then
  94. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  95. return
  96. fi
  97. if [ "$EIFACE" ]; then
  98. brctl delif $BRIDGE bat0
  99. brctl delif $BRIDGE $EIFACE
  100. ifconfig $BRIDGE down || true
  101. brctl delbr $BRIDGE
  102. ifconfig $EIFACE down -promisc
  103. fi
  104. avahi-autoipd -k $BRIDGE
  105. avahi-autoipd -k $IFACE
  106. ifconfig bat0 down -promisc
  107. batctl if del $IFACE
  108. rmmod batman-adv
  109. ifconfig $IFACE mtu 1500
  110. ifconfig $IFACE down
  111. iwconfig $IFACE mode managed
  112. iptables -D INPUT -p tcp --dport 548 -j ACCEPT
  113. iptables -D INPUT -p udp --dport 548 -j ACCEPT
  114. iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
  115. iptables -D INPUT -p udp --dport 5353 -j ACCEPT
  116. iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
  117. iptables -D INPUT -p udp --dport 5354 -j ACCEPT
  118. iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  119. iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  120. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  121. iptables -D INPUT -p udp --dport $IPFS_PORT -j ACCEPT
  122. systemctl restart network-manager
  123. }
  124. function verify {
  125. tempfile="$(mktemp)"
  126. batctl o > $tempfile
  127. if grep -q "disabled" $tempfile; then
  128. echo $'B.A.T.M.A.N. not enabled'
  129. rm $tempfile
  130. stop
  131. exit 726835
  132. fi
  133. echo $'B.A.T.M.A.N. is running'
  134. rm $tempfile
  135. }
  136. function assign_peer_address {
  137. for i in {1..6}; do
  138. number=$RANDOM
  139. let "number %= 255"
  140. octet=$(echo "obase=16;$number" | bc)
  141. if [ ${#octet} -lt 2 ]; then
  142. octet="0${octet}"
  143. fi
  144. if [ $i -gt 1 ]; then
  145. echo -n ":"
  146. fi
  147. echo -n "${octet}"
  148. done
  149. echo ''
  150. }
  151. function start {
  152. if [ -z "$IFACE" ] ; then
  153. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  154. exit 723657
  155. fi
  156. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  157. systemctl stop network-manager
  158. sleep 5
  159. # remove an avahi service which isn't used
  160. if [ -f /etc/avahi/services/udisks.service ]; then
  161. sudo rm /etc/avahi/services/udisks.service
  162. fi
  163. global_rate_limit
  164. # Might have to re-enable wifi
  165. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  166. ifconfig $IFACE down
  167. ifconfig $IFACE mtu 1532
  168. ifconfig $IFACE hw ether $(assign_peer_address)
  169. iwconfig $IFACE enc off
  170. iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
  171. sleep 1
  172. iwconfig $IFACE ap $CELLID
  173. modprobe batman-adv
  174. batctl if add $IFACE
  175. ifconfig $IFACE up
  176. avahi-autoipd --force-bind --daemonize --wait $BRIDGE
  177. avahi-autoipd --force-bind --daemonize --wait $IFACE
  178. ifconfig bat0 up promisc
  179. #Use persistent HWAddr
  180. ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
  181. if [ ! -f /var/lib/mesh-node/bat0 ]; then
  182. mkdir /var/lib/mesh-node
  183. echo "${ether_new}" > /var/lib/mesh-node/bat0
  184. else
  185. ether=$(cat /var/lib/mesh-node/bat0)
  186. ifconfig bat0 hw ether ${ether}
  187. fi
  188. if [ "$EIFACE" ] ; then
  189. brctl addbr $BRIDGE
  190. brctl addif $BRIDGE bat0
  191. brctl addif $BRIDGE $EIFACE
  192. ifconfig bat0 0.0.0.0
  193. ifconfig $EIFACE 0.0.0.0
  194. ifconfig $EIFACE up promisc
  195. ifconfig $BRIDGE up
  196. fi
  197. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  198. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  199. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  200. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  201. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  202. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  203. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  204. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  205. iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  206. iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  207. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  208. systemctl restart avahi-daemon
  209. verify
  210. }
  211. if ! grep -q "$IFACE" /proc/net/dev; then
  212. echo 'Interface $IFACE was not found'
  213. stop
  214. exit 1
  215. fi
  216. case "$1" in
  217. start|stop|status)
  218. $1
  219. ;;
  220. restart)
  221. stop
  222. sleep 10
  223. start
  224. ;;
  225. ping)
  226. batctl ping $2
  227. ;;
  228. ls|list)
  229. avahi-browse -atl
  230. ;;
  231. *)
  232. echo "error: invalid parameter $1"
  233. echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
  234. exit 2
  235. ;;
  236. esac
  237. exit 0