freedombone/src/freedombone-mesh-batman

395 lines
10 KiB
Plaintext
Raw Normal View History

2016-01-14 18:45:02 +01:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# Used to enable or disable batman mesh protocol on wlanX
#
# License
# =======
#
2018-01-25 19:35:39 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2016-01-14 18:45:02 +01:00
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
2016-01-14 18:45:02 +01:00
# 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
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
2016-01-14 18:45:02 +01:00
#
2016-02-13 23:09:27 +01:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-01-14 18:45:02 +01:00
PROJECT_NAME='freedombone'
COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
# hotspot passphrase must be 5 characters or longer
HOTSPOT_PASSPHRASE="${PROJECT_NAME}"
2016-10-06 20:09:13 +02:00
2016-10-21 11:24:07 +02:00
source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
2018-01-12 23:18:23 +01:00
source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-mesh
2016-10-06 20:09:13 +02:00
2018-01-13 11:11:10 +01:00
mesh_protocol_init
2016-10-06 20:09:13 +02:00
update_wifi_adaptors
if [ ! $IFACE ]; then
echo $'No wlan adaptor'
exit 0
fi
2016-01-14 18:45:02 +01:00
if [ -e /etc/default/batctl ]; then
2016-07-21 21:44:20 +02:00
. /etc/default/batctl
2016-01-14 18:45:02 +01:00
fi
function status {
2016-07-21 21:44:20 +02:00
batctl o
if grep -q "bmx6" $MESH_CURRENT_PROTOCOL; then
bmx6 -c show=originators
2018-01-16 14:11:10 +01:00
fi
if grep -q "bmx7" $MESH_CURRENT_PROTOCOL; then
bmx7 -c show=originators
fi
}
2016-06-26 11:34:54 +02:00
function stop {
if [ ! -f $MESH_CURRENT_PROTOCOL ]; then
return
fi
2016-07-21 21:44:20 +02:00
if [ -z "$IFACE" ]; then
echo 'error: unable to find wifi interface, not enabling batman-adv mesh'
return
fi
2017-11-22 23:21:01 +01:00
systemctl stop bmx6
systemctl stop bmx7
2018-01-16 13:35:18 +01:00
systemctl stop olsr2
2018-01-17 14:42:40 +01:00
systemctl stop babel
2018-01-15 15:31:07 +01:00
systemctl disable bmx6
systemctl disable bmx7
2018-01-16 13:35:18 +01:00
systemctl disable olsr2
2018-01-17 14:42:40 +01:00
systemctl disable babel
2017-11-22 23:21:01 +01:00
systemctl stop dnsmasq
systemctl disable dnsmasq
2016-07-21 21:44:20 +02:00
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
2016-07-21 21:44:20 +02:00
brctl delbr $BRIDGE
2016-10-06 20:09:13 +02:00
fi
2016-07-21 21:44:20 +02:00
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
2018-01-12 23:18:23 +01:00
disable_mesh_firewall
2017-09-29 18:32:51 +02:00
2016-07-21 21:44:20 +02:00
systemctl restart network-manager
if [ -f $MESH_CURRENT_PROTOCOL ]; then
rm $MESH_CURRENT_PROTOCOL
fi
2016-06-26 11:34:54 +02:00
}
function verify {
2016-07-21 21:44:20 +02:00
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 {
2017-11-22 23:21:01 +01:00
update_wifi_adaptors
2016-07-21 21:44:20 +02:00
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"
stop
2016-07-21 21:44:20 +02:00
systemctl stop network-manager
sleep 5
2017-11-22 23:21:01 +01:00
systemctl stop dnsmasq
systemctl disable dnsmasq
2016-07-21 21:44:20 +02:00
# remove an avahi service which isn't used
if [ -f /etc/avahi/services/udisks.service ]; then
sudo rm /etc/avahi/services/udisks.service
fi
2016-08-10 23:16:34 +02:00
global_rate_limit
2016-07-21 21:44:20 +02:00
# 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
2017-09-29 23:23:03 +02:00
fi
2016-07-21 21:44:20 +02:00
modprobe batman-adv
2018-01-15 15:11:23 +01:00
# avahi on ipv6
sed -i 's|use-ipv4=.*|use-ipv4=no|g' /etc/avahi/avahi-daemon.conf
sed -i 's|use-ipv6=.*|use-ipv6=yes|g' /etc/avahi/avahi-daemon.conf
2018-01-13 22:02:45 +01:00
2018-01-15 15:11:23 +01:00
sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx6 dev=${IFACE}|g" /etc/systemd/system/bmx6.service
sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx7 dev=${IFACE}|g" /etc/systemd/system/bmx7.service
2018-01-16 13:35:18 +01:00
sed -i "s|ExecStart=.*|ExecStart=/usr/local/sbin/olsrd2_static ${IFACE}|g" /etc/systemd/system/olsr2.service
2018-01-17 14:42:40 +01:00
sed -i "s|ExecStart=.*|ExecStart=/usr/local/bin/babeld ${IFACE}|g" /etc/systemd/system/babel.service
2018-01-15 15:11:23 +01:00
systemctl daemon-reload
add_wifi_interface $IFACE $WIFI_SSID ad-hoc $CHANNEL
# NOTE: Don't connect the secondary wifi device. hostapd will handle that by itself
2016-07-21 21:44:20 +02:00
ifconfig bat0 up promisc
brctl addbr $BRIDGE
brctl addif $BRIDGE bat0
ifconfig bat0 0.0.0.0
ethernet_connected='0'
2016-07-21 21:44:20 +02:00
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'
2018-01-15 15:11:23 +01:00
sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx6 dev=${IFACE} dev=${EIFACE}|g" /etc/systemd/system/bmx6.service
sed -i "s|ExecStart=.*|ExecStart=/usr/sbin/bmx7 dev=${IFACE} dev=${EIFACE}|g" /etc/systemd/system/bmx7.service
2018-01-16 13:35:18 +01:00
sed -i "s|ExecStart=.*|ExecStart=/usr/local/sbin/olsrd2_static ${IFACE} ${EIFACE}|g" /etc/systemd/system/olsr2.service
2018-01-17 14:42:40 +01:00
sed -i "s|ExecStart=.*|ExecStart=/usr/local/bin/babeld ${IFACE} ${EIFACE}|g" /etc/systemd/system/babel.service
2018-01-15 15:11:23 +01:00
systemctl daemon-reload
else
echo $"$EIFACE is not connected"
2016-10-06 20:09:13 +02:00
fi
2016-07-21 21:44:20 +02:00
fi
ifconfig $BRIDGE up
dhclient $BRIDGE
2018-01-12 23:18:23 +01:00
enable_mesh_seconary_wifi
2016-07-21 21:44:20 +02:00
2018-01-12 23:18:23 +01:00
enable_mesh_firewall
2017-09-29 18:32:51 +02:00
2018-01-12 23:18:23 +01:00
enable_mesh_scuttlebot
enable_mesh_tor
2017-12-16 19:57:41 +01:00
sed -i "s|server_name .*|server_name ${HOSTNAME}.local;|g" /etc/nginx/sites-available/git_ssb
2018-01-12 23:18:23 +01:00
2017-12-16 19:57:41 +01:00
systemctl restart nginx
if [ ! -f $MESH_DEFAULT_PROTOCOL ]; then
echo 'bmx6' > $MESH_DEFAULT_PROTOCOL
fi
2018-01-15 14:35:10 +01:00
if grep -q "bmx6" $MESH_DEFAULT_PROTOCOL; then
2018-01-15 15:11:23 +01:00
systemctl enable bmx6
systemctl restart bmx6
sed -i 's|<type>.*|<type>_bmx6._tcp</type>|g' /etc/avahi/services/routing.service
2018-01-16 13:35:18 +01:00
fi
if grep -q "bmx7" $MESH_DEFAULT_PROTOCOL; then
2018-01-15 15:11:23 +01:00
systemctl enable bmx7
systemctl restart bmx7
sed -i 's|<type>.*|<type>_bmx7._tcp</type>|g' /etc/avahi/services/routing.service
fi
2018-01-16 13:35:18 +01:00
if grep -q "olsr" $MESH_DEFAULT_PROTOCOL; then
2018-01-17 14:42:40 +01:00
IFACE=$(cat /etc/systemd/system/olsr2.service | grep ExecStart | awk -F ' ' '{print $2}')
mesh_generate_ipv6_address $IFACE
2018-01-16 13:35:18 +01:00
systemctl enable olsr2
systemctl restart olsr2
sed -i 's|<type>.*|<type>_olsr2._tcp</type>|g' /etc/avahi/services/routing.service
2018-01-17 14:42:40 +01:00
fi
if grep -q "babel" $MESH_DEFAULT_PROTOCOL; then
IFACE=$(cat /etc/systemd/system/babel.service | grep ExecStart | awk -F ' ' '{print $2}')
mesh_generate_ipv6_address $IFACE
systemctl enable babel
systemctl restart babel
sed -i 's|<type>.*|<type>_babel._tcp</type>|g' /etc/avahi/services/routing.service
2018-01-16 13:35:18 +01:00
fi
systemctl restart avahi-daemon
2016-07-21 21:44:20 +02:00
verify
2018-01-15 18:48:19 +01:00
echo "$(cat $MESH_DEFAULT_PROTOCOL)" > $MESH_CURRENT_PROTOCOL
2016-01-14 18:45:02 +01:00
}
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 ''
2016-09-10 16:20:14 +02:00
start
}
2016-01-14 18:45:02 +01:00
if ! grep -q "$IFACE" /proc/net/dev; then
2016-07-21 21:44:20 +02:00
echo 'Interface $IFACE was not found'
stop
exit 1
2016-01-14 18:45:02 +01:00
fi
case "$1" in
start|stop|status|monitor)
2016-07-21 21:44:20 +02:00
$1
;;
restart)
2017-10-29 12:49:02 +01:00
clear
echo ''
echo $'*** Stopping mesh network connection ***'
echo ''
2016-07-21 21:44:20 +02:00
stop
sleep 10
2017-10-29 12:49:02 +01:00
clear
echo ''
echo $'*** Starting mesh network connection ***'
echo ''
2016-07-21 21:44:20 +02:00
start
;;
ping)
batctl ping $2
;;
2016-09-10 14:09:33 +02:00
data)
watch -n1 "batctl s | grep mgmt | grep bytes"
;;
2016-07-21 21:44:20 +02:00
ls|list)
avahi-browse -atl
;;
*)
echo "error: invalid parameter $1"
echo 'usage: $0 {start|stop|restart|status|ping|ls|list}'
exit 2
;;
2016-01-14 18:45:02 +01:00
esac
exit 0