freedombone-mesh-batman 6.3KB

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