freedombone-mesh-batman 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. if [[ $1 == "start" ]]; then
  36. # install avahi
  37. sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
  38. sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf
  39. sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf
  40. sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf
  41. sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf
  42. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  43. fi
  44. # Mesh definition
  45. WIFI_SSID='mesh'
  46. if [ -f $COMPLETION_FILE ]; then
  47. if grep -q "WIFI_SSID:" $COMPLETION_FILE; then
  48. WIFI_SSID=$(cat $COMPLETION_FILE | grep "WIFI_SSID:" | awk -F ':' '{print $2}')
  49. fi
  50. sed -i "s|WIFI_SSID:.*|WIFI_SSID:${WIFI_SSID}|g" $COMPLETION_FILE
  51. fi
  52. CELLID='any'
  53. CHANNEL=2
  54. HOTSPOT_CHANNEL=6
  55. if [ -f $COMPLETION_FILE ]; then
  56. if grep -q "Wifi channel:" $COMPLETION_FILE; then
  57. CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}')
  58. fi
  59. sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE
  60. fi
  61. ZERONET_PORT=15441
  62. IPFS_PORT=4001
  63. TOX_PORT=33445
  64. TRACKER_PORT=6969
  65. LIBREVAULT_PORT=42345
  66. TAHOELAFS_PORT=50213
  67. # Ethernet bridge definition (bridged to bat0)
  68. BRIDGE=br-mesh
  69. BRIDGE_HOTSPOT=br-hotspot
  70. IFACE=
  71. IFACE_SECONDARY=
  72. EIFACE=eth0
  73. WLAN_ADAPTORS=$(count_wlan)
  74. if [ $WLAN_ADAPTORS -eq 0 ]; then
  75. echo $'No wlan adaptors found'
  76. exit 0
  77. fi
  78. update_wifi_adaptors
  79. if [ ! $IFACE ]; then
  80. echo $'No wlan adaptor'
  81. exit 0
  82. fi
  83. if [ -e /etc/default/batctl ]; then
  84. . /etc/default/batctl
  85. fi
  86. function get_ipv4_wlan {
  87. echo $(ip -o -f inet addr show dev "$IFACE" | awk '{print $4}' | awk 'END {print}' | awk -F '/' '{print $1}')
  88. }
  89. function mesh_hotspot_ip_address {
  90. echo $(ip -o -f inet addr show dev "${BRIDGE}" | awk '{print $4}' | awk 'END {print}' | awk -F '/' '{print $1}')
  91. }
  92. function global_rate_limit {
  93. if ! grep -q "tcp_challenge_ack_limit" /etc/sysctl.conf; then
  94. echo 'net.ipv4.tcp_challenge_ack_limit = 999999999' >> /etc/sysctl.conf
  95. else
  96. sed -i 's|net.ipv4.tcp_challenge_ack_limit.*|net.ipv4.tcp_challenge_ack_limit = 999999999|g' /etc/sysctl.conf
  97. fi
  98. sysctl -p -q
  99. }
  100. function status {
  101. batctl o
  102. }
  103. function stop {
  104. if [ -z "$IFACE" ]; then
  105. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  106. return
  107. fi
  108. systemctl stop dnsmasq
  109. systemctl disable dnsmasq
  110. if [ "$EIFACE" ]; then
  111. brctl delif $BRIDGE bat0
  112. ifconfig $BRIDGE down || true
  113. ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier)
  114. if [[ "$ethernet_connected" != "0" ]]; then
  115. systemctl stop hostapd
  116. brctl delif $BRIDGE $EIFACE
  117. ifconfig $EIFACE down -promisc
  118. fi
  119. brctl delbr $BRIDGE
  120. fi
  121. avahi-autoipd -k $BRIDGE
  122. avahi-autoipd -k $IFACE
  123. ifconfig bat0 down -promisc
  124. batctl if del $IFACE
  125. ifconfig $IFACE mtu 1500
  126. ifconfig $IFACE down
  127. iwconfig $IFACE mode managed
  128. if [ $IFACE_SECONDARY ]; then
  129. systemctl stop hostapd
  130. systemctl disable hostapd
  131. batctl if del $IFACE_SECONDARY
  132. ifconfig $IFACE_SECONDARY mtu 1500
  133. ifconfig $IFACE_SECONDARY down
  134. iwconfig $IFACE_SECONDARY mode managed
  135. fi
  136. rmmod batman-adv
  137. iptables -D INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
  138. iptables -D INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
  139. iptables -D INPUT -p tcp --dport 80 -j ACCEPT
  140. iptables -D INPUT -p udp --dport 80 -j ACCEPT
  141. iptables -D INPUT -p tcp --dport 548 -j ACCEPT
  142. iptables -D INPUT -p udp --dport 548 -j ACCEPT
  143. iptables -D INPUT -p tcp --dport 5353 -j ACCEPT
  144. iptables -D INPUT -p udp --dport 5353 -j ACCEPT
  145. iptables -D INPUT -p tcp --dport 5354 -j ACCEPT
  146. iptables -D INPUT -p udp --dport 5354 -j ACCEPT
  147. iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  148. iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  149. iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  150. iptables -D INPUT -p udp --dport $IPFS_PORT -j ACCEPT
  151. iptables -D INPUT -p tcp --dport $TOX_PORT -j ACCEPT
  152. iptables -D INPUT -p udp --dport $TOX_PORT -j ACCEPT
  153. iptables -D INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
  154. iptables -D INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
  155. iptables -D INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  156. # SSB/Scuttlebot/Patchwork
  157. iptables -D INPUT -p udp --dport 8008 -j ACCEPT
  158. iptables -D INPUT -p tcp --dport 8008 -j ACCEPT
  159. iptables -D INPUT -p udp --dport 8010 -j ACCEPT
  160. iptables -D INPUT -p tcp --dport 8010 -j ACCEPT
  161. # vpn over the internet
  162. iptables -D INPUT -p tcp --dport 653 -j ACCEPT
  163. iptables -D INPUT -p udp --dport 653 -j ACCEPT
  164. iptables -D INPUT -i ${EIFACE} -m state --state NEW -p tcp --dport 1194 -j ACCEPT
  165. iptables -D INPUT -i tun+ -j ACCEPT
  166. iptables -D FORWARD -i tun+ -j ACCEPT
  167. iptables -D FORWARD -i tun+ -o ${EIFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT
  168. iptables -D FORWARD -i ${EIFACE} -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  169. iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o ${EIFACE} -j MASQUERADE
  170. iptables -D OUTPUT -o tun+ -j ACCEPT
  171. echo 0 > /proc/sys/net/ipv4/ip_forward
  172. sed -i 's|net.ipv4.ip_forward=.*|net.ipv4.ip_forward=0|g' /etc/sysctl.conf
  173. systemctl restart network-manager
  174. }
  175. function verify {
  176. tempfile="$(mktemp)"
  177. batctl o > $tempfile
  178. if grep -q "disabled" $tempfile; then
  179. echo $'B.A.T.M.A.N. not enabled'
  180. rm $tempfile
  181. stop
  182. exit 726835
  183. fi
  184. echo $'B.A.T.M.A.N. is running'
  185. rm $tempfile
  186. }
  187. function assign_peer_address {
  188. for i in {1..6}; do
  189. number=$RANDOM
  190. let "number %= 255"
  191. octet=$(echo "obase=16;$number" | bc)
  192. if [ ${#octet} -lt 2 ]; then
  193. octet="0${octet}"
  194. fi
  195. if [ $i -gt 1 ]; then
  196. echo -n ":"
  197. fi
  198. echo -n "${octet}"
  199. done
  200. }
  201. function add_wifi_interface {
  202. ifname=$1
  203. ifssid=$WIFI_SSID
  204. if [ $2 ]; then
  205. ifssid=$2
  206. fi
  207. ifmode=ad-hoc
  208. if [ $3 ]; then
  209. ifmode=$3
  210. fi
  211. ifchannel=$CHANNEL
  212. if [ $4 ]; then
  213. ifchannel=$4
  214. fi
  215. ifconfig $ifname down
  216. ifconfig $ifname mtu 1532
  217. peermac=$(assign_peer_address)
  218. if [ ! $peermac ]; then
  219. echo $"Unable to obtain MAC address for $peermac on $ifname"
  220. return
  221. fi
  222. ifconfig $ifname hw ether $peermac
  223. echo $"$ifname assigned MAC address $peermac"
  224. iwconfig $ifname enc off
  225. iwconfig $ifname mode $ifmode essid $ifssid channel $ifchannel
  226. batctl if add $ifname
  227. ifconfig $ifname up
  228. }
  229. function mesh_create_app_downloads_page {
  230. if [ ! -d /root/$PROJECT_NAME/image_build/mesh_apps ]; then
  231. return
  232. fi
  233. if [ ! -d /var/www/html ]; then
  234. return
  235. fi
  236. # Don't go straight to cryptpad when navigating to the peer's IP address
  237. if [ -L /etc/nginx/sites-enabled/cryptpad ]; then
  238. rm /etc/nginx/sites-enabled/cryptpad
  239. ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
  240. if [ -d /etc/cryptpad ]; then
  241. systemctl stop cryptpad
  242. systemctl disable cryptpad
  243. fi
  244. systemctl restart nginx
  245. fi
  246. # Don't show the cryptpad icon on the desktop
  247. if [ -f /home/fbone/Desktop/cryptpad.desktop ]; then
  248. mv /home/fbone/Desktop/cryptpad.desktop /home/fbone/.cryptpad.desktop
  249. fi
  250. cp /root/$PROJECT_NAME/website/EN/meshindex.html /var/www/html/index.html
  251. if [ ! -f /var/www/html/ssb.apk ]; then
  252. cp /root/$PROJECT_NAME/image_build/mesh_apps/ssb.apk /var/www/html/ssb.apk
  253. fi
  254. if [ ! -f /var/www/html/trifa.apk ]; then
  255. cp /root/$PROJECT_NAME/image_build/mesh_apps/trifa.apk /var/www/html/trifa.apk
  256. fi
  257. if [ ! -d /var/www/html/images ]; then
  258. mkdir /var/www/html/images
  259. fi
  260. if [ ! -f /var/www/html/images/logo.png ]; then
  261. cp /root/$PROJECT_NAME/img/logo.png /var/www/html/images/logo.png
  262. fi
  263. if [ ! -f /var/www/html/images/ssb.png ]; then
  264. cp /root/$PROJECT_NAME/img/icon_patchwork.png /var/www/html/images/ssb.png
  265. fi
  266. if [ ! -f /var/www/html/images/trifa.png ]; then
  267. cp /root/$PROJECT_NAME/img/trifa.png /var/www/html/images/trifa.png
  268. fi
  269. if [ ! -f /var/www/html/freedombone.css ]; then
  270. cp /root/$PROJECT_NAME/website/freedombone.css /var/www/html/freedombone.css
  271. fi
  272. chown -R www-data:www-data /var/www/html/*
  273. }
  274. function start {
  275. update_wifi_adaptors
  276. if [ -z "$IFACE" ] ; then
  277. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  278. exit 723657
  279. fi
  280. echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE"
  281. systemctl stop network-manager
  282. sleep 5
  283. systemctl stop dnsmasq
  284. systemctl disable dnsmasq
  285. # remove an avahi service which isn't used
  286. if [ -f /etc/avahi/services/udisks.service ]; then
  287. sudo rm /etc/avahi/services/udisks.service
  288. fi
  289. global_rate_limit
  290. # Might have to re-enable wifi
  291. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  292. secondary_wifi_available=
  293. if [ $IFACE_SECONDARY ]; then
  294. if [[ $IFACE != $IFACE_SECONDARY ]]; then
  295. if [ -d /etc/hostapd ]; then
  296. if [ ${#HOTSPOT_PASSPHRASE} -gt 4 ]; then
  297. secondary_wifi_available=1
  298. else
  299. echo $'Hotspot passphrase is too short'
  300. fi
  301. fi
  302. fi
  303. fi
  304. modprobe batman-adv
  305. add_wifi_interface $IFACE $WIFI_SSID ad-hoc $CHANNEL
  306. avahi-autoipd --force-bind --daemonize --wait $IFACE
  307. # NOTE: Don't connect the secondary wifi device. hostapd will handle that by itself
  308. ifconfig bat0 up promisc
  309. brctl addbr $BRIDGE
  310. brctl addif $BRIDGE bat0
  311. ifconfig bat0 0.0.0.0
  312. ethernet_connected='0'
  313. if [ "$EIFACE" ] ; then
  314. ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier)
  315. if [[ "$ethernet_connected" != "0" ]]; then
  316. echo $'Trying ethernet bridge to the internet'
  317. brctl addif $BRIDGE $EIFACE
  318. ifconfig $EIFACE 0.0.0.0
  319. ifconfig $EIFACE up promisc
  320. echo $'End of ethernet bridge'
  321. else
  322. echo $"$EIFACE is not connected"
  323. fi
  324. fi
  325. ifconfig $BRIDGE up
  326. dhclient $BRIDGE
  327. if [ $secondary_wifi_available ]; then
  328. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  329. mesh_hotspot_address=$(mesh_hotspot_ip_address)
  330. if [[ "$mesh_hotspot_address" == *'.'* ]]; then
  331. echo "interface=${IFACE_SECONDARY}" > /etc/hostapd/hostapd.conf
  332. echo "bridge=${BRIDGE}" >> /etc/hostapd/hostapd.conf
  333. echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
  334. echo "country_code=UK" >> /etc/hostapd/hostapd.conf
  335. echo "ssid=${WIFI_SSID}-${mesh_hotspot_address}" >> /etc/hostapd/hostapd.conf
  336. echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
  337. echo "channel=${HOTSPOT_CHANNEL}" >> /etc/hostapd/hostapd.conf
  338. echo 'wpa=2' >> /etc/hostapd/hostapd.conf
  339. echo "wpa_passphrase=$HOTSPOT_PASSPHRASE" >> /etc/hostapd/hostapd.conf
  340. echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
  341. echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
  342. echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
  343. echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
  344. echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
  345. sed -i "s|#interface=.*|interface=${IFACE_SECONDARY}|g" /etc/dnsmasq.conf
  346. sed -i "s|interface=.*|interface=${IFACE_SECONDARY}|g" /etc/dnsmasq.conf
  347. sed -i "s|listen-address=.*|listen-address=127.0.0.1,$mesh_hotspot_address|g" /etc/dnsmasq.conf
  348. sed -i 's|#listen-address|listen-address|g' /etc/dnsmasq.conf
  349. systemctl enable dnsmasq
  350. systemctl restart dnsmasq
  351. systemctl enable hostapd
  352. systemctl restart hostapd
  353. mesh_create_app_downloads_page
  354. else
  355. secondary_wifi_available=
  356. echo $'WARNING: No IP address could be obtained for the hotspot'
  357. fi
  358. fi
  359. if [ ! $secondary_wifi_available ]; then
  360. systemctl stop hostapd
  361. systemctl disable hostapd
  362. # Recreate the cryptpad symlink
  363. if [ -f /etc/nginx/sites-available/cryptpad ]; then
  364. if [ -L /etc/nginx/sites-enabled/cryptpad ]; then
  365. rm /etc/nginx/sites-enabled/default
  366. fi
  367. systemctl enable cryptpad
  368. systemctl start cryptpad
  369. if [ ! -L /etc/nginx/sites-enabled/cryptpad ]; then
  370. ln -s /etc/nginx/sites-available/cryptpad /etc/nginx/sites-enabled/cryptpad
  371. systemctl restart nginx
  372. fi
  373. fi
  374. if [ -f /home/fbone/.cryptpad.desktop ]; then
  375. mv /home/fbone/.cryptpad.desktop /home/fbone/Desktop/cryptpad.desktop
  376. fi
  377. fi
  378. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  379. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  380. iptables -A INPUT -p tcp --dport $TRACKER_PORT -j ACCEPT
  381. iptables -A INPUT -p udp --dport $TRACKER_PORT -j ACCEPT
  382. iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  383. iptables -A INPUT -p udp --dport 80 -j ACCEPT
  384. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  385. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  386. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  387. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  388. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  389. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  390. iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT
  391. iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT
  392. iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT
  393. iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT
  394. iptables -A INPUT -p udp --dport $TOX_PORT -j ACCEPT
  395. iptables -A INPUT -p tcp --dport $LIBREVAULT_PORT -j ACCEPT
  396. iptables -A INPUT -p udp --dport $LIBREVAULT_PORT -j ACCEPT
  397. iptables -A INPUT -p tcp --dport $TAHOELAFS_PORT -j ACCEPT
  398. # SSB/Scuttlebot/Patchwork
  399. iptables -A INPUT -p udp --dport 8008 -j ACCEPT
  400. iptables -A INPUT -p tcp --dport 8008 -j ACCEPT
  401. iptables -A INPUT -p udp --dport 8010 -j ACCEPT
  402. iptables -A INPUT -p tcp --dport 8010 -j ACCEPT
  403. if [[ "$ethernet_connected" != "0" ]]; then
  404. # vpn over the internet
  405. iptables -A INPUT -p tcp --dport 653 -j ACCEPT
  406. iptables -A INPUT -p udp --dport 653 -j ACCEPT
  407. iptables -A INPUT -i ${EIFACE} -m state --state NEW -p tcp --dport 1194 -j ACCEPT
  408. iptables -A INPUT -i tun+ -j ACCEPT
  409. iptables -A FORWARD -i tun+ -j ACCEPT
  410. iptables -A FORWARD -i tun+ -o ${EIFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT
  411. iptables -A FORWARD -i ${EIFACE} -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  412. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o ${EIFACE} -j MASQUERADE
  413. iptables -A OUTPUT -o tun+ -j ACCEPT
  414. echo 1 > /proc/sys/net/ipv4/ip_forward
  415. sed -i 's|# net.ipv4.ip_forward|net.ipv4.ip_forward|g' /etc/sysctl.conf
  416. sed -i 's|#net.ipv4.ip_forward|net.ipv4.ip_forward|g' /etc/sysctl.conf
  417. sed -i 's|net.ipv4.ip_forward.*|net.ipv4.ip_forward=1|g' /etc/sysctl.conf
  418. fi
  419. systemctl restart avahi-daemon
  420. if [ -f /etc/scuttlebot/.ssb/config ]; then
  421. ethernet_connected=$(cat /sys/class/net/eth0/carrier)
  422. if [[ "$ethernet_connected" != "0" ]]; then
  423. sed -i "s|\"host\": .*|\"host\": \"$(get_ipv4_wlan)\",|g" /etc/scuttlebot/.ssb/config
  424. systemctl restart scuttlebot
  425. else
  426. systemctl stop scuttlebot
  427. fi
  428. fi
  429. # if we have an ethernet connection to an internet router then create
  430. # an onion address for this peer
  431. if [[ "$ethernet_connected" != "0" ]]; then
  432. systemctl enable tor
  433. systemctl start tor
  434. HIDDEN_SERVICE_PATH=/var/lib/tor/hidden_service_
  435. if [ ! -f ${HIDDEN_SERVICE_PATH}mesh/hostname ]; then
  436. echo "HiddenServiceDir ${HIDDEN_SERVICE_PATH}mesh/" >> /etc/tor/torrc
  437. echo "HiddenServicePort 653 127.0.0.1:653" >> /etc/tor/torrc
  438. systemctl restart tor
  439. fi
  440. else
  441. systemctl stop tor
  442. systemctl disable tor
  443. fi
  444. verify
  445. }
  446. function monitor {
  447. if [ -z "$IFACE" ] ; then
  448. echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
  449. exit 723657
  450. fi
  451. clear
  452. echo ''
  453. echo $'*** Stopping network ***'
  454. echo ''
  455. stop
  456. echo "info: monitoring mesh network $WIFI_SSID on $IFACE"
  457. systemctl stop network-manager
  458. sleep 5
  459. clear
  460. echo ''
  461. echo $'*** Setting firewall rate limit ***'
  462. echo ''
  463. global_rate_limit
  464. clear
  465. echo ''
  466. echo $'*** Enabling wifi adaptor in monitor mode ***'
  467. echo ''
  468. # Might have to re-enable wifi
  469. rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true
  470. ifconfig $IFACE down
  471. ifconfig $IFACE mtu 1532
  472. ifconfig $IFACE hw ether $(assign_peer_address)
  473. iwconfig $IFACE enc off
  474. iwconfig $IFACE mode monitor channel $CHANNEL
  475. sleep 1
  476. iwconfig $IFACE ap $CELLID
  477. modprobe batman-adv
  478. batctl if add $IFACE
  479. ifconfig $IFACE up
  480. horst -i $IFACE
  481. clear
  482. echo ''
  483. echo $'*** Restarting the network daemon. This may take a while. ***'
  484. echo ''
  485. start
  486. }
  487. if ! grep -q "$IFACE" /proc/net/dev; then
  488. echo 'Interface $IFACE was not found'
  489. stop
  490. exit 1
  491. fi
  492. case "$1" in
  493. start|stop|status|monitor)
  494. $1
  495. ;;
  496. restart)
  497. clear
  498. echo ''
  499. echo $'*** Stopping mesh network connection ***'
  500. echo ''
  501. stop
  502. sleep 10
  503. clear
  504. echo ''
  505. echo $'*** Starting mesh network connection ***'
  506. echo ''
  507. start
  508. ;;
  509. ping)
  510. batctl ping $2
  511. ;;
  512. data)
  513. watch -n1 "batctl s | grep mgmt | grep bytes"
  514. ;;
  515. ls|list)
  516. avahi-browse -atl
  517. ;;
  518. *)
  519. echo "error: invalid parameter $1"
  520. echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
  521. exit 2
  522. ;;
  523. esac
  524. exit 0