From a8d6ffa091e3e5eb5841b61f7e64b99156c33d1e Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 14 Jan 2016 17:45:02 +0000 Subject: [PATCH] Beginning of mesh setup --- src/freedombone-image | 10 +- src/freedombone-image-customise | 111 +++++++++++++--- src/freedombone-mesh-batman | 222 ++++++++++++++++++++++++++++++++ 3 files changed, 320 insertions(+), 23 deletions(-) create mode 100755 src/freedombone-mesh-batman diff --git a/src/freedombone-image b/src/freedombone-image index 620ec6ca..042e3d3c 100755 --- a/src/freedombone-image +++ b/src/freedombone-image @@ -220,11 +220,11 @@ if [[ $GENERIC_IMAGE == "yes" ]]; then fi # If this is a mesh variant then create an appropriate script -if [[ $VARIANT == "mesh"* ]]; then - CONFIG_FILENAME=/tmp/${PROJECT_NAME}_mesh.cfg - mesh_router_setup_script $CONFIG_FILENAME - DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}') -fi +#if [[ $VARIANT == "mesh"* ]]; then + #CONFIG_FILENAME=/tmp/${PROJECT_NAME}_mesh.cfg + #mesh_router_setup_script $CONFIG_FILENAME + #DEFAULT_DOMAIN_NAME=$(cat $CONFIG_FILENAME | grep 'DEFAULT_DOMAIN_NAME' | awk -F '=' '{print $2}') +#fi if [ ! $PASSWORD ]; then # generate a random password diff --git a/src/freedombone-image-customise b/src/freedombone-image-customise index 62935c46..599e2dc6 100755 --- a/src/freedombone-image-customise +++ b/src/freedombone-image-customise @@ -362,30 +362,105 @@ atheros_wifi() { fi } +mesh_avahi() { + chroot "$rootdir" apt-get -y install avahi-utils avahi-autoipd avahi-dnsconfd + + decarray=( 1 2 3 4 5 6 7 8 9 0 ) + PEER_ID=${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]} + sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" $rootdir/etc/avahi/avahi-daemon.conf + + if [ ! -d $rootdir/etc/avahi/services ]; then + mkdir -p $rootdir/etc/avahi/services + fi + + # remove an avahi service which isn't used + if [ -f $rootdir/etc/avahi/services/udisks.service ]; then + rm $rootdir/etc/avahi/services/udisks.service + fi + + # Add an ssh service + echo '' > $rootdir/etc/avahi/services/ssh.service + echo '' >> $rootdir/etc/avahi/services/ssh.service + echo '' >> $rootdir/etc/avahi/services/ssh.service + echo ' %h SSH' >> $rootdir/etc/avahi/services/ssh.service + echo ' ' >> $rootdir/etc/avahi/services/ssh.service + echo ' _ssh._tcp' >> $rootdir/etc/avahi/services/ssh.service + echo " $SSH_PORT" >> $rootdir/etc/avahi/services/ssh.service + echo ' ' >> $rootdir/etc/avahi/services/ssh.service + echo '' >> $rootdir/etc/avahi/services/ssh.service + + # keep the daemon running + WATCHDOG_SCRIPT_NAME="keepon" + echo '' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo '# keep avahi daemon running' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo 'AVAHI_RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo 'if [ ! $AVAHI_RUNNING ]; then' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo ' systemctl start avahi-daemon' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo ' echo " Avahi daemon restarted" >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + echo 'fi' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME + chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME +} + +function mesh_batman { + chroot "$rootdir" apt-get -y install iproute bridge-utils libnetfilter-conntrack3 batctl + chroot "$rootdir" apt-get -y install python-dev libevent-dev ebtables python-pip git + chroot "$rootdir" apt-get -y install wireless-tools rfkill + + if ! grep -q "batman_adv" $rootdir/etc/modules; then + echo 'batman_adv' >> $rootdir/etc/modules + fi + + if [ -f /usr/local/bin/${PROJECT_NAME}-mesh-batman ]; then + cp /usr/local/bin/${PROJECT_NAME}-mesh-batman $rootdir/var/lib/batman + else + cp /usr/bin/${PROJECT_NAME}-mesh-batman $rootdir/var/lib/batman + fi + + echo '[Unit]' > $rootdir/etc/systemd/system/batman.service + echo 'Description=B.A.T.M.A.N. Advanced' >> $rootdir/etc/systemd/system/batman.service + echo '' >> $rootdir/etc/systemd/system/batman.service + echo '[Service]' >> $rootdir/etc/systemd/system/batman.service + echo 'Type=oneshot' >> $rootdir/etc/systemd/system/batman.service + echo "ExecStart=/var/lib/batman start" >> $rootdir/etc/systemd/system/batman.service + echo "ExecStop=/var/lib/batman stop" >> $rootdir/etc/systemd/system/batman.service + echo 'RemainAfterExit=yes' >> $rootdir/etc/systemd/system/batman.service + echo '' >> $rootdir/etc/systemd/system/batman.service + echo '# Allow time for the server to start/stop' >> $rootdir/etc/systemd/system/batman.service + echo 'TimeoutSec=300' >> $rootdir/etc/systemd/system/batman.service + echo '' >> $rootdir/etc/systemd/system/batman.service + echo '[Install]' >> $rootdir/etc/systemd/system/batman.service + echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/batman.service + chroot "$rootdir" systemctl enable batman +} + initialise_mesh() { if [[ $VARIANT != "mesh" ]]; then return fi - MESH_SERVICE='mesh-setup.service' - MESH_SETUP_DAEMON=$rootdir/etc/systemd/system/$MESH_SERVICE + mesh_avahi + mesh_batman - echo '[Unit]' > $MESH_SETUP_DAEMON - echo 'Description=Initial mesh router configuration' >> $MESH_SETUP_DAEMON - echo 'After=syslog.target' >> $MESH_SETUP_DAEMON - echo 'After=network.target' >> $MESH_SETUP_DAEMON - echo '[Service]' >> $MESH_SETUP_DAEMON - echo 'Type=simple' >> $MESH_SETUP_DAEMON - echo 'User=root' >> $MESH_SETUP_DAEMON - echo 'Group=root' >> $MESH_SETUP_DAEMON - echo 'WorkingDirectory=/root' >> $MESH_SETUP_DAEMON - echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-image-mesh > /var/log/mesh-setup.log" >> $MESH_SETUP_DAEMON - echo '' >> $MESH_SETUP_DAEMON - echo 'TimeoutSec=99999' >> $MESH_SETUP_DAEMON - echo '' >> $MESH_SETUP_DAEMON - echo '[Install]' >> $MESH_SETUP_DAEMON - echo 'WantedBy=multi-user.target' >> $MESH_SETUP_DAEMON + #MESH_SERVICE='mesh-setup.service' + #MESH_SETUP_DAEMON=$rootdir/etc/systemd/system/$MESH_SERVICE - chroot "$rootdir" systemctl enable $MESH_SERVICE + #echo '[Unit]' > $MESH_SETUP_DAEMON + #echo 'Description=Initial mesh router configuration' >> $MESH_SETUP_DAEMON + #echo 'After=syslog.target' >> $MESH_SETUP_DAEMON + #echo 'After=network.target' >> $MESH_SETUP_DAEMON + #echo '[Service]' >> $MESH_SETUP_DAEMON + #echo 'Type=simple' >> $MESH_SETUP_DAEMON + #echo 'User=root' >> $MESH_SETUP_DAEMON + #echo 'Group=root' >> $MESH_SETUP_DAEMON + #echo 'WorkingDirectory=/root' >> $MESH_SETUP_DAEMON + #echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-image-mesh > /var/log/mesh-setup.log" >> $MESH_SETUP_DAEMON + #echo '' >> $MESH_SETUP_DAEMON + #echo 'TimeoutSec=99999' >> $MESH_SETUP_DAEMON + #echo '' >> $MESH_SETUP_DAEMON + #echo '[Install]' >> $MESH_SETUP_DAEMON + #echo 'WantedBy=multi-user.target' >> $MESH_SETUP_DAEMON + + #chroot "$rootdir" systemctl enable $MESH_SERVICE } # Set to true/false to control if eatmydata is used during build diff --git a/src/freedombone-mesh-batman b/src/freedombone-mesh-batman new file mode 100755 index 00000000..8d13593d --- /dev/null +++ b/src/freedombone-mesh-batman @@ -0,0 +1,222 @@ +#!/bin/bash +# +# .---. . . +# | | | +# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. +# | | (.-' (.-' ( | ( )| | | | )( )| | (.-' +# ' ' --' --' -' - -' ' ' -' -' -' ' - --' +# +# Freedom in the Cloud +# +# Used to enable or disable batman mesh protocol on wlanX +# +# License +# ======= +# +# Copyright (C) 2015-2016 Bob Mottram +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +PROJECT_NAME='freedombone' +COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt + +if [[ $1 == "start" ]]; then + # install avahi + sed -i "s|#host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf + sed -i "s|host-name=.*|host-name=$(hostname)|g" /etc/avahi/avahi-daemon.conf + sed -i "s|use-ipv4=.*|use-ipv4=yes|g" /etc/avahi/avahi-daemon.conf + sed -i "s|use-ipv6=.*|use-ipv6=no|g" /etc/avahi/avahi-daemon.conf + sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" /etc/avahi/avahi-daemon.conf + sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf +fi + +# Mesh definition +ESSID= +if ! grep -q "ESSID:" $COMPLETION_FILE; then + ESSID='mesh' +else + ESSID=$(cat $COMPLETION_FILE | grep "ESSID:" | awk -F ':' '{print $2}') +fi +sed -i "s|ESSID:.*|ESSID:${ESSID}|g" $COMPLETION_FILE + +CELLID='any' + +CHANNEL= +if ! grep -q "Wifi channel:" $COMPLETION_FILE; then + CHANNEL=2 +else + CHANNEL=$(cat $COMPLETION_FILE | grep "Wifi channel:" | awk -F ':' '{print $2}') +fi +sed -i "s|Wifi channel:.*|Wifi channel:${CHANNEL}|g" $COMPLETION_FILE + +ZERONET_PORT=15441 +IPFS_PORT=4001 + +# Ethernet bridge definition (bridged to bat0) +BRIDGE=br-mesh +IFACE='wlan0' +EIFACE=eth0 + +if [[ $IFACE == "wlan0" ]]; then + if grep -q "wlan1" /proc/net/dev; then + IFACE=wlan1 + fi +fi +if [[ $IFACE == "wlan0" ]]; then + if grep -q "wlan2" /proc/net/dev; then + IFACE=wlan2 + fi +fi +if [[ $IFACE == "wlan0" ]]; then + if grep -q "wlan3" /proc/net/dev; then + IFACE=wlan3 + fi +fi + +if [ -e /etc/default/batctl ]; then + . /etc/default/batctl +fi + +start() { + if [ -z "$IFACE" ] ; then + echo 'error: unable to find wifi interface, not enabling batman-adv mesh' + return + fi + echo 'info: enabling batman-adv mesh network $ESSID on $IFACE' + + systemctl stop network-manager + sleep 5 + + # remove an avahi service which isn't used + if [ -f /etc/avahi/services/udisks.service ]; then + sudo rm /etc/avahi/services/udisks.service + fi + + # Might have to re-enable wifi + rfkill unblock $(rfkill list|awk -F: "/phy/ {print $1}") || true + + ifconfig $IFACE down + ifconfig $IFACE mtu 1532 + iwconfig $IFACE enc off + iwconfig $IFACE mode ad-hoc essid $ESSID channel $CHANNEL + sleep 1 + iwconfig $IFACE ap $CELLID + + modprobe batman-adv + batctl if add $IFACE + ifconfig $IFACE up + avahi-autoipd --force-bind --daemonize --wait $BRIDGE + avahi-autoipd --force-bind --daemonize --wait $IFACE + ifconfig bat0 up promisc + + #Use persistent HWAddr + ether_new=$(ifconfig eth0 | grep HWaddr | sed -e "s/.*HWaddr //") + if [ ! -f /var/lib/mesh-node/bat0 ]; then + mkdir /var/lib/mesh-node + echo "${ether_new}" > /var/lib/mesh-node/bat0 + else + ether=$(cat /var/lib/mesh-node/bat0) + ifconfig bat0 hw ether ${ether} + fi + + if [ "$EIFACE" ] ; then + brctl addbr $BRIDGE + brctl addif $BRIDGE bat0 + brctl addif $BRIDGE $EIFACE + ifconfig bat0 0.0.0.0 + ifconfig $EIFACE 0.0.0.0 + ifconfig $EIFACE up promisc + ifconfig $BRIDGE up + fi + + iptables -A INPUT -p tcp --dport 548 -j ACCEPT + iptables -A INPUT -p udp --dport 548 -j ACCEPT + iptables -A INPUT -p tcp --dport 5353 -j ACCEPT + iptables -A INPUT -p udp --dport 5353 -j ACCEPT + iptables -A INPUT -p tcp --dport 5354 -j ACCEPT + iptables -A INPUT -p udp --dport 5354 -j ACCEPT + iptables -A INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT + iptables -A INPUT -p udp --dport $ZERONET_PORT -j ACCEPT + iptables -A INPUT -p tcp --dport $IPFS_PORT -j ACCEPT + + systemctl restart avahi-daemon +} + +stop() { + if [ -z "$IFACE" ]; then + echo 'error: unable to find wifi interface, not enabling batman-adv mesh' + return + fi + if [ "$EIFACE" ]; then + brctl delif $BRIDGE bat0 + brctl delif $BRIDGE $EIFACE + ifconfig $BRIDGE down || true + brctl delbr $BRIDGE + ifconfig $EIFACE down -promisc + fi + + avahi-autoipd -k $BRIDGE + avahi-autoipd -k $IFACE + ifconfig bat0 down -promisc + + batctl if del $IFACE + rmmod batman-adv + ifconfig $IFACE mtu 1500 + ifconfig $IFACE down + iwconfig $IFACE mode managed + + iptables -D INPUT -p tcp --dport 548 -j ACCEPT + iptables -D INPUT -p udp --dport 548 -j ACCEPT + iptables -D INPUT -p tcp --dport 5353 -j ACCEPT + iptables -D INPUT -p udp --dport 5353 -j ACCEPT + iptables -D INPUT -p tcp --dport 5354 -j ACCEPT + iptables -D INPUT -p udp --dport 5354 -j ACCEPT + iptables -D INPUT -p tcp --dport $ZERONET_PORT -j ACCEPT + iptables -D INPUT -p udp --dport $ZERONET_PORT -j ACCEPT + iptables -D INPUT -p tcp --dport $IPFS_PORT -j ACCEPT + + systemctl restart network-manager +} + +if ! grep -q "$IFACE" /proc/net/dev; then + echo 'Interface $IFACE was not found' + stop + exit 1 +fi + +case "$1" in + start|stop) + $1 + ;; + restart) + stop + sleep 10 + start + ;; + status) + batctl o + ;; + ping) + batctl ping $2 + ;; + 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