freedombone-mesh-batman 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Used to enable or disable batman mesh protocol on a given interface
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. COMPLETION_FILE="/root/${PROJECT_NAME}-completed.txt"
  30. # hotspot passphrase must be 5 characters or longer
  31. HOTSPOT_PASSPHRASE="${PROJECT_NAME}"
  32. # The type of interface which the mesh will run on
  33. MESH_INTERFACE_TYPE='wlan'
  34. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
  35. source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-mesh
  36. function status {
  37. batctl o
  38. if grep -q "bmx6" "$MESH_CURRENT_PROTOCOL"; then
  39. bmx6 -c show=originators
  40. fi
  41. if grep -q "bmx7" "$MESH_CURRENT_PROTOCOL"; then
  42. bmx7 -c show=originators
  43. fi
  44. }
  45. function stop {
  46. if [ ! -f "$MESH_CURRENT_PROTOCOL" ]; then
  47. return
  48. fi
  49. if [ -z "$IFACE" ]; then
  50. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  51. return
  52. fi
  53. systemctl stop bmx6
  54. systemctl stop bmx7
  55. systemctl stop olsr2
  56. systemctl stop babel
  57. systemctl disable bmx6
  58. systemctl disable bmx7
  59. systemctl disable olsr2
  60. systemctl disable babel
  61. systemctl stop dnsmasq
  62. systemctl disable dnsmasq
  63. # shellcheck disable=SC2153
  64. if [ "$EIFACE" ]; then
  65. if [[ "$EIFACE" != "$IFACE" ]] ; then
  66. brctl delif "$BRIDGE" bat0
  67. ifconfig "$BRIDGE" down || true
  68. ethernet_connected=$(cat "/sys/class/net/$EIFACE/carrier")
  69. if [[ "$ethernet_connected" != "0" ]]; then
  70. systemctl stop hostapd
  71. brctl delif "$BRIDGE" "$EIFACE"
  72. ifconfig "$EIFACE" down -promisc
  73. fi
  74. brctl delbr "$BRIDGE"
  75. fi
  76. fi
  77. ifconfig bat0 down -promisc
  78. batctl if del "$IFACE"
  79. ifconfig "$IFACE" mtu 1500
  80. ifconfig "$IFACE" down
  81. if [[ "$MESH_INTERFACE_TYPE" == 'wlan'* ]]; then
  82. iwconfig "$IFACE" mode managed
  83. fi
  84. if [ "$IFACE_SECONDARY" ]; then
  85. systemctl stop hostapd
  86. systemctl disable hostapd
  87. batctl if del "$IFACE_SECONDARY"
  88. ifconfig "$IFACE_SECONDARY" mtu 1500
  89. ifconfig "$IFACE_SECONDARY" down
  90. if [[ "$MESH_INTERFACE_TYPE" == 'wlan'* ]]; then
  91. iwconfig "$IFACE_SECONDARY" mode managed
  92. fi
  93. fi
  94. rmmod batman-adv
  95. disable_mesh_firewall
  96. systemctl restart network-manager
  97. if [ -f "$MESH_CURRENT_PROTOCOL" ]; then
  98. rm "$MESH_CURRENT_PROTOCOL"
  99. fi
  100. }
  101. function verify {
  102. tempfile="$(mktemp)"
  103. batctl o > "$tempfile"
  104. if grep -q "disabled" "$tempfile"; then
  105. echo $'B.A.T.M.A.N. not enabled'
  106. rm "$tempfile"
  107. stop
  108. exit 726835
  109. fi
  110. echo $'B.A.T.M.A.N. is running'
  111. rm "$tempfile"
  112. }
  113. function add_wifi_interface {
  114. ifname=$1
  115. ifssid=$WIFI_SSID
  116. if [ "$2" ]; then
  117. ifssid=$2
  118. fi
  119. ifmode=ad-hoc
  120. if [ "$3" ]; then
  121. ifmode=$3
  122. fi
  123. ifchannel=$CHANNEL
  124. if [ "$4" ]; then
  125. ifchannel=$4
  126. fi
  127. ifconfig "$ifname" down
  128. ifconfig "$ifname" mtu 1532
  129. peermac=$(assign_peer_address)
  130. if [ ! "$peermac" ]; then
  131. echo $"Unable to obtain MAC address for $peermac on $ifname"
  132. return
  133. fi
  134. ifconfig "$ifname" hw ether "$peermac"
  135. echo $"$ifname assigned MAC address $peermac"
  136. if [[ "$MESH_INTERFACE_TYPE" == 'wlan'* ]]; then
  137. iwconfig "$ifname" enc off
  138. iwconfig "$ifname" mode "$ifmode" essid "$ifssid" channel "$ifchannel"
  139. fi
  140. batctl if add "$ifname"
  141. ifconfig "$ifname" up
  142. }
  143. # shellcheck disable=SC2120
  144. function start {
  145. update_wifi_adaptors "${MESH_INTERFACE_TYPE}"
  146. if [ -z "$IFACE" ] ; then
  147. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  148. exit 723657
  149. fi
  150. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  151. stop
  152. systemctl stop network-manager
  153. sleep 5
  154. systemctl stop dnsmasq
  155. systemctl disable dnsmasq
  156. # remove an avahi service which isn't used
  157. if [ -f /etc/avahi/services/udisks.service ]; then
  158. sudo rm /etc/avahi/services/udisks.service
  159. fi
  160. global_rate_limit
  161. # Might have to re-enable wifi
  162. rfkill unblock "$(rfkill list|awk -F: "/phy/ {print $1}")" || true
  163. secondary_wifi_available=
  164. if [[ "$MESH_INTERFACE_TYPE" == 'wlan'* ]]; then
  165. if [ "$IFACE_SECONDARY" ]; then
  166. if [[ "$IFACE" != "$IFACE_SECONDARY" ]]; then
  167. if [ -d /etc/hostapd ]; then
  168. if [ ${#HOTSPOT_PASSPHRASE} -gt 4 ]; then
  169. secondary_wifi_available=1
  170. else
  171. echo $'Hotspot passphrase is too short'
  172. fi
  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. if [[ "$EIFACE" != "$IFACE" ]] ; then
  195. ethernet_connected=$(cat "/sys/class/net/$EIFACE/carrier")
  196. if [[ "$ethernet_connected" != "0" ]]; then
  197. echo $'Trying ethernet bridge to the internet'
  198. brctl addif "$BRIDGE" "$EIFACE"
  199. ifconfig "$EIFACE" 0.0.0.0
  200. ifconfig "$EIFACE" up promisc
  201. echo $'End of internet bridge'
  202. sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx6 dev=${IFACE} dev=${EIFACE}|g" /etc/systemd/system/bmx6.service
  203. sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx7 dev=${IFACE} dev=${EIFACE}|g" /etc/systemd/system/bmx7.service
  204. sed -i "s|ExecStart=.*|ExecStart=/usr/local/sbin/olsrd2_static ${IFACE} ${EIFACE}|g" /etc/systemd/system/olsr2.service
  205. sed -i "s|ExecStart=.*|ExecStart=/usr/local/bin/babeld ${IFACE} ${EIFACE}|g" /etc/systemd/system/babel.service
  206. systemctl daemon-reload
  207. else
  208. echo $"$EIFACE is not connected"
  209. fi
  210. fi
  211. fi
  212. ifconfig "$BRIDGE" up
  213. dhclient "$BRIDGE"
  214. enable_mesh_seconary_wifi
  215. enable_mesh_firewall
  216. enable_mesh_scuttlebot
  217. enable_mesh_tor
  218. sed -i "s|server_name .*|server_name ${HOSTNAME}.local;|g" /etc/nginx/sites-available/git_ssb
  219. systemctl restart nginx
  220. if [ ! -f "$MESH_DEFAULT_PROTOCOL" ]; then
  221. echo 'bmx6' > "$MESH_DEFAULT_PROTOCOL"
  222. fi
  223. if grep -q "bmx6" "$MESH_DEFAULT_PROTOCOL"; then
  224. systemctl enable bmx6
  225. systemctl restart bmx6
  226. sed -i 's|<type>.*|<type>_bmx6._tcp</type>|g' /etc/avahi/services/routing.service
  227. fi
  228. if grep -q "bmx7" "$MESH_DEFAULT_PROTOCOL"; then
  229. systemctl enable bmx7
  230. systemctl restart bmx7
  231. sed -i 's|<type>.*|<type>_bmx7._tcp</type>|g' /etc/avahi/services/routing.service
  232. fi
  233. if grep -q "olsr" "$MESH_DEFAULT_PROTOCOL"; then
  234. IFACE=$(grep ExecStart "/etc/systemd/system/olsr2.service" | awk -F ' ' '{print $2}')
  235. mesh_generate_ipv6_address "$IFACE"
  236. systemctl enable olsr2
  237. systemctl restart olsr2
  238. sed -i 's|<type>.*|<type>_olsr2._tcp</type>|g' /etc/avahi/services/routing.service
  239. fi
  240. if grep -q "babel" "$MESH_DEFAULT_PROTOCOL"; then
  241. IFACE=$(grep ExecStart /etc/systemd/system/babel.service | awk -F ' ' '{print $2}')
  242. mesh_generate_ipv6_address "$IFACE"
  243. systemctl enable babel
  244. systemctl restart babel
  245. sed -i 's|<type>.*|<type>_babel._tcp</type>|g' /etc/avahi/services/routing.service
  246. fi
  247. systemctl restart avahi-daemon
  248. verify
  249. cat "$MESH_DEFAULT_PROTOCOL" > "$MESH_CURRENT_PROTOCOL"
  250. }
  251. function monitor {
  252. if [[ "$MESH_INTERFACE_TYPE" != 'wlan'* ]]; then
  253. return
  254. fi
  255. if [ -z "$IFACE" ] ; then
  256. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  257. exit 723657
  258. fi
  259. clear
  260. echo ''
  261. echo $'*** Stopping network ***'
  262. echo ''
  263. stop
  264. echo "info: monitoring mesh network $WIFI_SSID on $IFACE"
  265. systemctl stop network-manager
  266. sleep 5
  267. clear
  268. echo ''
  269. echo $'*** Setting firewall rate limit ***'
  270. echo ''
  271. global_rate_limit
  272. clear
  273. echo ''
  274. echo $'*** Enabling wifi adaptor in monitor mode ***'
  275. echo ''
  276. # Might have to re-enable wifi
  277. rfkill unblock "$(rfkill list|awk -F: "/phy/ {print $1}")" || true
  278. ifconfig "$IFACE" down
  279. ifconfig "$IFACE" mtu 1532
  280. ifconfig "$IFACE" hw ether "$(assign_peer_address)"
  281. iwconfig "$IFACE" enc off
  282. iwconfig "$IFACE" mode monitor channel "$CHANNEL"
  283. sleep 1
  284. iwconfig "$IFACE" ap "$CELLID"
  285. modprobe batman-adv
  286. batctl if add "$IFACE"
  287. ifconfig "$IFACE" up
  288. horst -i "$IFACE"
  289. clear
  290. echo ''
  291. echo $'*** Restarting the network daemon. This may take a while. ***'
  292. echo ''
  293. # shellcheck disable=SC2119
  294. start
  295. }
  296. # optionally a file can contain the mesh interface type
  297. mesh_interface_type_file=/root/.mesh_interface_type
  298. if [ -f "$mesh_interface_type_file" ]; then
  299. MESH_INTERFACE_TYPE=$(head -n 1 < "$mesh_interface_type_file")
  300. if [[ "$MESH_INTERFACE_TYPE" == 'wlan'* ]]; then
  301. MESH_INTERFACE_TYPE='wlan'
  302. fi
  303. if [[ "$MESH_INTERFACE_TYPE" == 'eth'* ]]; then
  304. MESH_INTERFACE_TYPE='eth'
  305. fi
  306. fi
  307. mesh_protocol_init
  308. if [[ "$MESH_INTERFACE_TYPE" == 'eth'* ]]; then
  309. MESH_INTERFACE_TYPE='eth'
  310. fi
  311. update_wifi_adaptors "${MESH_INTERFACE_TYPE}"
  312. if [ ! "$IFACE" ]; then
  313. echo $'No wlan adaptor'
  314. exit 0
  315. fi
  316. if [ -e /etc/default/batctl ]; then
  317. # shellcheck disable=SC1091
  318. . /etc/default/batctl
  319. fi
  320. if ! grep -q "$IFACE" /proc/net/dev; then
  321. echo "Interface \$IFACE was not found"
  322. stop
  323. exit 1
  324. fi
  325. case "$1" in
  326. start|stop|status|monitor)
  327. $1
  328. ;;
  329. restart)
  330. clear
  331. echo ''
  332. echo $'*** Stopping mesh network connection ***'
  333. echo ''
  334. stop
  335. sleep 10
  336. clear
  337. echo ''
  338. echo $'*** Starting mesh network connection ***'
  339. echo ''
  340. # shellcheck disable=SC2119
  341. start
  342. ;;
  343. ping)
  344. batctl ping "$2"
  345. ;;
  346. data)
  347. watch -n1 "batctl s | grep mgmt | grep bytes"
  348. ;;
  349. ls|list)
  350. avahi-browse -atl
  351. ;;
  352. *)
  353. echo "error: invalid parameter $1"
  354. echo "usage: \$0 {start|stop|restart|status|ping|ls|list}"
  355. exit 2
  356. ;;
  357. esac
  358. exit 0