freedombone-mesh-batman 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 must be 5 characters or longer
  33. HOTSPOT_PASSPHRASE="${PROJECT_NAME}"
  34. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
  35. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-mesh
  36. mesh_protocol_init
  37. update_wifi_adaptors
  38. if [ ! $IFACE ]; then
  39. echo $'No wlan adaptor'
  40. exit 0
  41. fi
  42. if [ -e /etc/default/batctl ]; then
  43. . /etc/default/batctl
  44. fi
  45. function status {
  46. batctl o
  47. if grep -q "bmx6" $MESH_CURRENT_PROTOCOL; then
  48. bmx6 -c show=originators
  49. fi
  50. if grep -q "bmx7" $MESH_CURRENT_PROTOCOL; then
  51. bmx7 -c show=originators
  52. fi
  53. }
  54. function stop {
  55. if [ ! -f $MESH_CURRENT_PROTOCOL ]; then
  56. return
  57. fi
  58. if [ -z "$IFACE" ]; then
  59. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  60. return
  61. fi
  62. systemctl stop bmx6
  63. systemctl stop bmx7
  64. systemctl stop olsr2
  65. systemctl stop babel
  66. systemctl disable bmx6
  67. systemctl disable bmx7
  68. systemctl disable olsr2
  69. systemctl disable babel
  70. systemctl stop dnsmasq
  71. systemctl disable dnsmasq
  72. if [ "$EIFACE" ]; then
  73. brctl delif $BRIDGE bat0
  74. ifconfig $BRIDGE down || true
  75. ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier)
  76. if [[ "$ethernet_connected" != "0" ]]; then
  77. systemctl stop hostapd
  78. brctl delif $BRIDGE $EIFACE
  79. ifconfig $EIFACE down -promisc
  80. fi
  81. brctl delbr $BRIDGE
  82. fi
  83. ifconfig bat0 down -promisc
  84. batctl if del $IFACE
  85. ifconfig $IFACE mtu 1500
  86. ifconfig $IFACE down
  87. iwconfig $IFACE mode managed
  88. if [ $IFACE_SECONDARY ]; then
  89. systemctl stop hostapd
  90. systemctl disable hostapd
  91. batctl if del $IFACE_SECONDARY
  92. ifconfig $IFACE_SECONDARY mtu 1500
  93. ifconfig $IFACE_SECONDARY down
  94. iwconfig $IFACE_SECONDARY mode managed
  95. fi
  96. rmmod batman-adv
  97. disable_mesh_firewall
  98. systemctl restart network-manager
  99. if [ -f $MESH_CURRENT_PROTOCOL ]; then
  100. rm $MESH_CURRENT_PROTOCOL
  101. fi
  102. }
  103. function verify {
  104. tempfile="$(mktemp)"
  105. batctl o > $tempfile
  106. if grep -q "disabled" $tempfile; then
  107. echo $'B.A.T.M.A.N. not enabled'
  108. rm $tempfile
  109. stop
  110. exit 726835
  111. fi
  112. echo $'B.A.T.M.A.N. is running'
  113. rm $tempfile
  114. }
  115. function add_wifi_interface {
  116. ifname=$1
  117. ifssid=$WIFI_SSID
  118. if [ $2 ]; then
  119. ifssid=$2
  120. fi
  121. ifmode=ad-hoc
  122. if [ $3 ]; then
  123. ifmode=$3
  124. fi
  125. ifchannel=$CHANNEL
  126. if [ $4 ]; then
  127. ifchannel=$4
  128. fi
  129. ifconfig $ifname down
  130. ifconfig $ifname mtu 1532
  131. peermac=$(assign_peer_address)
  132. if [ ! $peermac ]; then
  133. echo $"Unable to obtain MAC address for $peermac on $ifname"
  134. return
  135. fi
  136. ifconfig $ifname hw ether $peermac
  137. echo $"$ifname assigned MAC address $peermac"
  138. iwconfig $ifname enc off
  139. iwconfig $ifname mode $ifmode essid $ifssid channel $ifchannel
  140. batctl if add $ifname
  141. ifconfig $ifname up
  142. }
  143. function start {
  144. update_wifi_adaptors
  145. if [ -z "$IFACE" ] ; then
  146. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  147. exit 723657
  148. fi
  149. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  150. stop
  151. systemctl stop network-manager
  152. sleep 5
  153. systemctl stop dnsmasq
  154. systemctl disable dnsmasq
  155. # remove an avahi service which isn't used
  156. if [ -f /etc/avahi/services/udisks.service ]; then
  157. sudo rm /etc/avahi/services/udisks.service
  158. fi
  159. global_rate_limit
  160. # Might have to re-enable wifi
  161. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  162. secondary_wifi_available=
  163. if [ $IFACE_SECONDARY ]; then
  164. if [[ $IFACE != $IFACE_SECONDARY ]]; then
  165. if [ -d /etc/hostapd ]; then
  166. if [ ${#HOTSPOT_PASSPHRASE} -gt 4 ]; then
  167. secondary_wifi_available=1
  168. else
  169. echo $'Hotspot passphrase is too short'
  170. fi
  171. fi
  172. fi
  173. fi
  174. modprobe batman-adv
  175. # avahi on ipv6
  176. sed -i 's|use-ipv4=.*|use-ipv4=no|g' /etc/avahi/avahi-daemon.conf
  177. sed -i 's|use-ipv6=.*|use-ipv6=yes|g' /etc/avahi/avahi-daemon.conf
  178. sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx6 dev=${IFACE}|g" /etc/systemd/system/bmx6.service
  179. sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx7 dev=${IFACE}|g" /etc/systemd/system/bmx7.service
  180. sed -i "s|ExecStart=.*|ExecStart=/usr/local/sbin/olsrd2_static ${IFACE}|g" /etc/systemd/system/olsr2.service
  181. sed -i "s|ExecStart=.*|ExecStart=/usr/local/bin/babeld ${IFACE}|g" /etc/systemd/system/babel.service
  182. systemctl daemon-reload
  183. add_wifi_interface $IFACE $WIFI_SSID ad-hoc $CHANNEL
  184. # NOTE: Don't connect the secondary wifi device. hostapd will handle that by itself
  185. ifconfig bat0 up promisc
  186. brctl addbr $BRIDGE
  187. brctl addif $BRIDGE bat0
  188. ifconfig bat0 0.0.0.0
  189. ethernet_connected='0'
  190. if [ "$EIFACE" ] ; then
  191. ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier)
  192. if [[ "$ethernet_connected" != "0" ]]; then
  193. echo $'Trying ethernet bridge to the internet'
  194. brctl addif $BRIDGE $EIFACE
  195. ifconfig $EIFACE 0.0.0.0
  196. ifconfig $EIFACE up promisc
  197. echo $'End of ethernet bridge'
  198. sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx6 dev=${IFACE} dev=${EIFACE}|g" /etc/systemd/system/bmx6.service
  199. sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx7 dev=${IFACE} dev=${EIFACE}|g" /etc/systemd/system/bmx7.service
  200. sed -i "s|ExecStart=.*|ExecStart=/usr/local/sbin/olsrd2_static ${IFACE} ${EIFACE}|g" /etc/systemd/system/olsr2.service
  201. sed -i "s|ExecStart=.*|ExecStart=/usr/local/bin/babeld ${IFACE} ${EIFACE}|g" /etc/systemd/system/babel.service
  202. systemctl daemon-reload
  203. else
  204. echo $"$EIFACE is not connected"
  205. fi
  206. fi
  207. ifconfig $BRIDGE up
  208. dhclient $BRIDGE
  209. enable_mesh_seconary_wifi
  210. enable_mesh_firewall
  211. enable_mesh_scuttlebot
  212. enable_mesh_tor
  213. sed -i "s|server_name .*|server_name ${HOSTNAME}.local;|g" /etc/nginx/sites-available/git_ssb
  214. systemctl restart nginx
  215. if [ ! -f $MESH_DEFAULT_PROTOCOL ]; then
  216. echo 'bmx6' > $MESH_DEFAULT_PROTOCOL
  217. fi
  218. if grep -q "bmx6" $MESH_DEFAULT_PROTOCOL; then
  219. systemctl enable bmx6
  220. systemctl restart bmx6
  221. sed -i 's|<type>.*|<type>_bmx6._tcp</type>|g' /etc/avahi/services/routing.service
  222. fi
  223. if grep -q "bmx7" $MESH_DEFAULT_PROTOCOL; then
  224. systemctl enable bmx7
  225. systemctl restart bmx7
  226. sed -i 's|<type>.*|<type>_bmx7._tcp</type>|g' /etc/avahi/services/routing.service
  227. fi
  228. if grep -q "olsr" $MESH_DEFAULT_PROTOCOL; then
  229. IFACE=$(cat /etc/systemd/system/olsr2.service | grep ExecStart | awk -F ' ' '{print $2}')
  230. mesh_generate_ipv6_address $IFACE
  231. systemctl enable olsr2
  232. systemctl restart olsr2
  233. sed -i 's|<type>.*|<type>_olsr2._tcp</type>|g' /etc/avahi/services/routing.service
  234. fi
  235. if grep -q "babel" $MESH_DEFAULT_PROTOCOL; then
  236. IFACE=$(cat /etc/systemd/system/babel.service | grep ExecStart | awk -F ' ' '{print $2}')
  237. mesh_generate_ipv6_address $IFACE
  238. systemctl enable babel
  239. systemctl restart babel
  240. sed -i 's|<type>.*|<type>_babel._tcp</type>|g' /etc/avahi/services/routing.service
  241. fi
  242. systemctl restart avahi-daemon
  243. verify
  244. echo "$(cat $MESH_DEFAULT_PROTOCOL)" > $MESH_CURRENT_PROTOCOL
  245. }
  246. function monitor {
  247. if [ -z "$IFACE" ] ; then
  248. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  249. exit 723657
  250. fi
  251. clear
  252. echo ''
  253. echo $'*** Stopping network ***'
  254. echo ''
  255. stop
  256. echo "info: monitoring mesh network $WIFI_SSID on $IFACE"
  257. systemctl stop network-manager
  258. sleep 5
  259. clear
  260. echo ''
  261. echo $'*** Setting firewall rate limit ***'
  262. echo ''
  263. global_rate_limit
  264. clear
  265. echo ''
  266. echo $'*** Enabling wifi adaptor in monitor mode ***'
  267. echo ''
  268. # Might have to re-enable wifi
  269. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  270. ifconfig $IFACE down
  271. ifconfig $IFACE mtu 1532
  272. ifconfig $IFACE hw ether $(assign_peer_address)
  273. iwconfig $IFACE enc off
  274. iwconfig $IFACE mode monitor channel $CHANNEL
  275. sleep 1
  276. iwconfig $IFACE ap $CELLID
  277. modprobe batman-adv
  278. batctl if add $IFACE
  279. ifconfig $IFACE up
  280. horst -i $IFACE
  281. clear
  282. echo ''
  283. echo $'*** Restarting the network daemon. This may take a while. ***'
  284. echo ''
  285. start
  286. }
  287. if ! grep -q "$IFACE" /proc/net/dev; then
  288. echo 'Interface $IFACE was not found'
  289. stop
  290. exit 1
  291. fi
  292. case "$1" in
  293. start|stop|status|monitor)
  294. $1
  295. ;;
  296. restart)
  297. clear
  298. echo ''
  299. echo $'*** Stopping mesh network connection ***'
  300. echo ''
  301. stop
  302. sleep 10
  303. clear
  304. echo ''
  305. echo $'*** Starting mesh network connection ***'
  306. echo ''
  307. start
  308. ;;
  309. ping)
  310. batctl ping $2
  311. ;;
  312. data)
  313. watch -n1 "batctl s | grep mgmt | grep bytes"
  314. ;;
  315. ls|list)
  316. avahi-browse -atl
  317. ;;
  318. *)
  319. echo "error: invalid parameter $1"
  320. echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
  321. exit 2
  322. ;;
  323. esac
  324. exit 0