freedombone-mesh-batman 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 status {
  82. batctl o
  83. }
  84. function stop {
  85. if [ -z "$IFACE" ]; then
  86. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  87. return
  88. fi
  89. if [ "$EIFACE" ]; then
  90. brctl delif $BRIDGE bat0
  91. brctl delif $BRIDGE $EIFACE
  92. ifconfig $BRIDGE down || true
  93. brctl delbr $BRIDGE
  94. ifconfig $EIFACE down -promisc
  95. fi
  96. avahi-autoipd -k $BRIDGE
  97. avahi-autoipd -k $IFACE
  98. ifconfig bat0 down -promisc
  99. batctl if del $IFACE
  100. rmmod batman-adv
  101. ifconfig $IFACE mtu 1500
  102. ifconfig $IFACE down
  103. iwconfig $IFACE mode managed
  104. iptables -D INPUT -p tcp --dport 548 -j ACCEPT
  105. iptables -D INPUT -p udp --dport 548 -j ACCEPT
  106. iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
  107. iptables -D INPUT -p udp --dport 5353 -j ACCEPT
  108. iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
  109. iptables -D INPUT -p udp --dport 5354 -j ACCEPT
  110. iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  111. iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  112. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  113. systemctl restart network-manager
  114. }
  115. function verify {
  116. tempfile="$(mktemp)"
  117. batctl o > $tempfile
  118. if grep -q "disabled" $tempfile; then
  119. echo $'B.A.T.M.A.N. not enabled'
  120. rm $tempfile
  121. stop
  122. exit 726835
  123. fi
  124. echo $'B.A.T.M.A.N. is running'
  125. rm $tempfile
  126. }
  127. function assign_peer_address {
  128. for i in {1..6}; do
  129. number=$RANDOM
  130. let "number %= 255"
  131. octet=$(echo "obase=16;$number" | bc)
  132. if [ ${#octet} -lt 2 ]; then
  133. octet="0${octet}"
  134. fi
  135. if [ $i -gt 1 ]; then
  136. echo -n ":"
  137. fi
  138. echo -n "${octet}"
  139. done
  140. echo ''
  141. }
  142. function start {
  143. if [ -z "$IFACE" ] ; then
  144. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  145. exit 723657
  146. fi
  147. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  148. systemctl stop network-manager
  149. sleep 5
  150. # remove an avahi service which isn't used
  151. if [ -f /etc/avahi/services/udisks.service ]; then
  152. sudo rm /etc/avahi/services/udisks.service
  153. fi
  154. # Might have to re-enable wifi
  155. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  156. ifconfig $IFACE down
  157. ifconfig $IFACE mtu 1532
  158. ifconfig $IFACE hw ether $(assign_peer_address)
  159. iwconfig $IFACE enc off
  160. iwconfig $IFACE mode ad-hoc essid $WIFI_SSID channel $CHANNEL
  161. sleep 1
  162. iwconfig $IFACE ap $CELLID
  163. modprobe batman-adv
  164. batctl if add $IFACE
  165. ifconfig $IFACE up
  166. avahi-autoipd --force-bind --daemonize --wait $BRIDGE
  167. avahi-autoipd --force-bind --daemonize --wait $IFACE
  168. ifconfig bat0 up promisc
  169. #Use persistent HWAddr
  170. ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //")
  171. if [ ! -f /var/lib/mesh-node/bat0 ]; then
  172. mkdir /var/lib/mesh-node
  173. echo "${ether_new}" > /var/lib/mesh-node/bat0
  174. else
  175. ether=$(cat /var/lib/mesh-node/bat0)
  176. ifconfig bat0 hw ether ${ether}
  177. fi
  178. if [ "$EIFACE" ] ; then
  179. brctl addbr $BRIDGE
  180. brctl addif $BRIDGE bat0
  181. brctl addif $BRIDGE $EIFACE
  182. ifconfig bat0 0.0.0.0
  183. ifconfig $EIFACE 0.0.0.0
  184. ifconfig $EIFACE up promisc
  185. ifconfig $BRIDGE up
  186. fi
  187. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  188. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  189. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  190. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  191. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  192. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  193. iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  194. iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  195. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  196. systemctl restart avahi-daemon
  197. verify
  198. }
  199. if ! grep -q "$IFACE" /proc/net/dev; then
  200. echo 'Interface $IFACE was not found'
  201. stop
  202. exit 1
  203. fi
  204. case "$1" in
  205. start|stop|status)
  206. $1
  207. ;;
  208. restart)
  209. stop
  210. sleep 10
  211. start
  212. ;;
  213. ping)
  214. batctl ping $2
  215. ;;
  216. ls|list)
  217. avahi-browse -atl
  218. ;;
  219. *)
  220. echo "error: invalid parameter $1"
  221. echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
  222. exit 2
  223. ;;
  224. esac
  225. exit 0