freedombone-mesh-batman 11KB

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