#!/bin/bash # # .---. . . # | | | # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-' # ' ' --' --' -' - -' ' ' -' -' -' ' - --' # # Freedom in the Cloud # # Used to enable or disable batman mesh protocol on wlanX # # License # ======= # # Copyright (C) 2015-2017 Bob Mottram # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . PROJECT_NAME='freedombone' COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt # hotspot passphrase must be 5 characters or longer HOTSPOT_PASSPHRASE="${PROJECT_NAME}" source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-mesh mesh_protocol_init update_wifi_adaptors if [ ! $IFACE ]; then echo $'No wlan adaptor' exit 0 fi if [ -e /etc/default/batctl ]; then . /etc/default/batctl fi function status { batctl o } function stop { if [ -z "$IFACE" ]; then echo 'error: unable to find wifi interface, not enabling batman-adv mesh' return fi systemctl stop dnsmasq systemctl disable dnsmasq if [ "$EIFACE" ]; then brctl delif $BRIDGE bat0 ifconfig $BRIDGE down || true ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier) if [[ "$ethernet_connected" != "0" ]]; then systemctl stop hostapd brctl delif $BRIDGE $EIFACE ifconfig $EIFACE down -promisc fi brctl delbr $BRIDGE fi avahi-autoipd -k $BRIDGE avahi-autoipd -k $IFACE ifconfig bat0 down -promisc batctl if del $IFACE ifconfig $IFACE mtu 1500 ifconfig $IFACE down iwconfig $IFACE mode managed if [ $IFACE_SECONDARY ]; then systemctl stop hostapd systemctl disable hostapd batctl if del $IFACE_SECONDARY ifconfig $IFACE_SECONDARY mtu 1500 ifconfig $IFACE_SECONDARY down iwconfig $IFACE_SECONDARY mode managed fi rmmod batman-adv disable_mesh_firewall systemctl restart network-manager if [ -f $MESH_CURRENT_PROTOCOL ]; then rm $MESH_CURRENT_PROTOCOL fi } function verify { tempfile="$(mktemp)" batctl o > $tempfile if grep -q "disabled" $tempfile; then echo $'B.A.T.M.A.N. not enabled' rm $tempfile stop exit 726835 fi echo $'B.A.T.M.A.N. is running' rm $tempfile } function add_wifi_interface { ifname=$1 ifssid=$WIFI_SSID if [ $2 ]; then ifssid=$2 fi ifmode=ad-hoc if [ $3 ]; then ifmode=$3 fi ifchannel=$CHANNEL if [ $4 ]; then ifchannel=$4 fi ifconfig $ifname down ifconfig $ifname mtu 1532 peermac=$(assign_peer_address) if [ ! $peermac ]; then echo $"Unable to obtain MAC address for $peermac on $ifname" return fi ifconfig $ifname hw ether $peermac echo $"$ifname assigned MAC address $peermac" iwconfig $ifname enc off iwconfig $ifname mode $ifmode essid $ifssid channel $ifchannel batctl if add $ifname ifconfig $ifname up } function start { update_wifi_adaptors if [ -z "$IFACE" ] ; then echo 'error: unable to find wifi interface, not enabling batman-adv mesh' exit 723657 fi echo "info: enabling batman-adv mesh network $WIFI_SSID on $IFACE" mesh_protocol_stop systemctl stop network-manager sleep 5 systemctl stop bmx6 systemctl disable bmx6 systemctl stop dnsmasq systemctl disable dnsmasq # remove an avahi service which isn't used if [ -f /etc/avahi/services/udisks.service ]; then sudo rm /etc/avahi/services/udisks.service fi global_rate_limit # Might have to re-enable wifi rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true secondary_wifi_available= if [ $IFACE_SECONDARY ]; then if [[ $IFACE != $IFACE_SECONDARY ]]; then if [ -d /etc/hostapd ]; then if [ ${#HOTSPOT_PASSPHRASE} -gt 4 ]; then secondary_wifi_available=1 else echo $'Hotspot passphrase is too short' fi fi fi fi modprobe batman-adv add_wifi_interface $IFACE $WIFI_SSID ad-hoc $CHANNEL avahi-autoipd --force-bind --daemonize --wait $IFACE # NOTE: Don't connect the secondary wifi device. hostapd will handle that by itself ifconfig bat0 up promisc brctl addbr $BRIDGE brctl addif $BRIDGE bat0 ifconfig bat0 0.0.0.0 ethernet_connected='0' if [ "$EIFACE" ] ; then ethernet_connected=$(cat /sys/class/net/$EIFACE/carrier) if [[ "$ethernet_connected" != "0" ]]; then echo $'Trying ethernet bridge to the internet' brctl addif $BRIDGE $EIFACE ifconfig $EIFACE 0.0.0.0 ifconfig $EIFACE up promisc echo $'End of ethernet bridge' else echo $"$EIFACE is not connected" fi fi ifconfig $BRIDGE up dhclient $BRIDGE enable_mesh_seconary_wifi enable_mesh_firewall systemctl restart avahi-daemon enable_mesh_scuttlebot enable_mesh_tor sed -i "s|server_name .*|server_name ${HOSTNAME}.local;|g" /etc/nginx/sites-available/git_ssb systemctl restart nginx verify echo "batman-adv" > $MESH_CURRENT_PROTOCOL } function monitor { if [ -z "$IFACE" ] ; then echo 'error: unable to find wifi interface, not enabling batman-adv mesh' exit 723657 fi clear echo '' echo $'*** Stopping network ***' echo '' stop echo "info: monitoring mesh network $WIFI_SSID on $IFACE" systemctl stop network-manager sleep 5 clear echo '' echo $'*** Setting firewall rate limit ***' echo '' global_rate_limit clear echo '' echo $'*** Enabling wifi adaptor in monitor mode ***' echo '' # Might have to re-enable wifi rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true ifconfig $IFACE down ifconfig $IFACE mtu 1532 ifconfig $IFACE hw ether $(assign_peer_address) iwconfig $IFACE enc off iwconfig $IFACE mode monitor channel $CHANNEL sleep 1 iwconfig $IFACE ap $CELLID modprobe batman-adv batctl if add $IFACE ifconfig $IFACE up horst -i $IFACE clear echo '' echo $'*** Restarting the network daemon. This may take a while. ***' echo '' start } if ! grep -q "$IFACE" /proc/net/dev; then echo 'Interface $IFACE was not found' stop exit 1 fi case "$1" in start|stop|status|monitor) $1 ;; restart) clear echo '' echo $'*** Stopping mesh network connection ***' echo '' stop sleep 10 clear echo '' echo $'*** Starting mesh network connection ***' echo '' start ;; ping) batctl ping $2 ;; data) watch -n1 "batctl s | grep mgmt | grep bytes" ;; ls|list) avahi-browse -atl ;; *) echo "error: invalid parameter $1" echo 'usage: $0 {start|stop|restart|status|ping|ls|list}' exit 2 ;; esac exit 0