From 803dcfbe91b76bd40e2f7674b9ba4081e46d3de6 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Fri, 21 May 2021 20:26:00 +0530 Subject: [PATCH 1/8] added WIP python version --- naxalnet | 67 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/naxalnet b/naxalnet index c333bdd..80977d8 100755 --- a/naxalnet +++ b/naxalnet @@ -1,35 +1,48 @@ -#!/usr/bin/env bash +#!/usr/bin/env python3 -DATADIR="$(dirname "$0")/../share/naxalnet" -NETWORKD_DIR=/run/systemd/network -SSID="Hello World" +""" +Setup a working BATMAN Advanced network +with systemd-networkd and iwd +""" -if [[ $# != 1 ]] -then - echo "Usage: $0 {wireless device}" - echo "Example: $0 wlan0" - exit 1 -fi +# for linking resolv.conf +from pathlib import Path +from dasbus.connection import SystemMessageBus -if [[ ! -d "$NETWORKD_DIR" ]] -then - mkdir "$NETWORKD_DIR" -fi +SYSTEMD_RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf" +RESOLV_CONF = "/etc/resolv.conf" -echo "Copying systemd-networkd config files" -# Copies all files in DATADIR to NETWORKD_DIR -find "$DATADIR" -type f -execdir cp -t "$NETWORKD_DIR" {} + -echo "Linking resolv.conf to systemd-resolved's stub-resolved.conf" -ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf +def get_all_devices(proxy): + """ + Returns d-bus object path of all devices as a list of strings. + proxy: usually what is returned by bus.get_proxy('net.connman.iwd','/') + """ + # Gets all D-Bus objects in the proxy. + objects = proxy.GetManagedObjects() -echo "Starting services" -systemctl reload-or-restart iwd systemd-resolved systemd-networkd + # Now find out which of them are devices + devices = [] + for name, obj in objects.items(): + if "net.connman.iwd.Device" in obj: + # add all devices to the list + devices.append(name) -# wait five seconds for iwd to start -#sleep 5 -echo "Configuring iwd" -iwctl device "$1" set-property Mode ad-hoc -iwctl ad-hoc "$1" start_open "$SSID" + return devices -echo "Done" + +# Copy networkd configs + + +# syslink resolvd +try: + Path(RESOLV_CONF).symlink_to(SYSTEMD_RESOLVED_STUB_RESOLVE) +except FileExistsError: + print("Resolv.conf already exists") + +# connect to the System bus +bus = SystemMessageBus() +# iwd proxy +proxy = bus.get_proxy("net.connman.iwd", "/") + +print("Bye") From 45e4326e3184cf31c7cd08daf0b9ed5b4df5e9e2 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Sat, 22 May 2021 15:14:16 +0530 Subject: [PATCH 2/8] Changed systemd service --- naxalnet@.service => naxalnet.service | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename naxalnet@.service => naxalnet.service (60%) diff --git a/naxalnet@.service b/naxalnet.service similarity index 60% rename from naxalnet@.service rename to naxalnet.service index 3380768..9749225 100644 --- a/naxalnet@.service +++ b/naxalnet.service @@ -1,10 +1,10 @@ [Unit] -Description=Naxalnet on %i +Description=Naxalnet [Service] Type=oneshot RemainAfterExit=yes -ExecStart=/usr/bin/naxalnet %i +ExecStart=/usr/bin/naxalnet [Install] WantedBy=multi-user.target From 715f4b730844e43d1753153f8e423c40078a0c3e Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Fri, 4 Jun 2021 22:27:54 +0530 Subject: [PATCH 3/8] Updated rewrite and README.md The rewrite is partly over, with only the iwd part remaining. The README was updated and a commented-out section which had no comment closing tag was fixed. --- Makefile | 13 ++++++--- README.md | 45 ++++++++++++++++--------------- naxalnet | 79 +++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 85 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index 04482a8..62373c1 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,14 @@ PREFIX := /usr install: naxalnet install -d $(DESTDIR)$(PREFIX)/bin install -d $(DESTDIR)$(PREFIX)/lib/systemd/system/ - install naxalnet@.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ + install naxalnet.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ install naxalnet $(DESTDIR)$(PREFIX)/bin/ - install -d $(DESTDIR)$(PREFIX)/share/naxalnet - install systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/ + install -d $(DESTDIR)$(PREFIX)/share/naxalnet/networkd + install systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/networkd + +testdeps: + @for i in networkctl systemctl python3; do \ + echo "Checking for $$i"; \ + which $$i > /dev/null && echo " $$i found" || \ + (echo " $$i not found"; exit 1); \ + done diff --git a/README.md b/README.md index c2b5ae4..5c95037 100644 --- a/README.md +++ b/README.md @@ -31,17 +31,18 @@ into networkd's runtime configuration directory. It uses iwctl to start an ad-hoc network named "Hello World". The wireless interface is linked to `bat0`, the batman interface. A bridge `bridge0` is created so that other devices such as wired connections and wireless ap can be bridged. `bat0` is -added to the bridge, and the bridge gets an ip address (link-local, or DHCP if -any of the computers have a DHCP server configured). If these were successful, -an intranet is created. You can now use services like [IPFS](https://ipfs.io), -[Jami](https://jami.net), [Secure Scuttlebutt](https://scuttlebutt.nz) -and others which can work without internet access. +added to the bridge, and the bridge gets an ip address (link-local, or +DHCP if any of the computers have a DHCP server configured). If these +were successful, an intranet is created. You can now use services +like [IPFS](https://ipfs.io), [Jami](https://jami.net), +[Secure Scuttlebutt](https://scuttlebutt.nz) and others which can work +without internet access. ## Requirements - systemd v248 or more (for batman support) -- Linux kernel with batman-adv module (if `modinfo batman-adv` shows no error - then you already have it) +- Linux kernel with batman-adv module (if `modinfo batman-adv` shows + no error then you already have it) - iwd (for starting ad-hoc network) - wifi adapter with ad-hoc support - two or more computers with wifi adapter @@ -55,12 +56,10 @@ Clone the repo and cd into it. Run `sudo make install` to install naxalnet. This will install naxalnet in `/usr/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@.service}`. - ## TODO @@ -101,4 +104,4 @@ sudo rm -r /usr/local/{bin/naxalnet,share/naxalnet/,lib/systemd/system/naxalnet@ Add list of things here. -This project is in pre-alpha stage. Documentation is incomplete. \ No newline at end of file +This project is in alpha stage. Documentation is incomplete. diff --git a/naxalnet b/naxalnet index 80977d8..52c6f21 100755 --- a/naxalnet +++ b/naxalnet @@ -5,44 +5,67 @@ Setup a working BATMAN Advanced network with systemd-networkd and iwd """ -# for linking resolv.conf +import sys from pathlib import Path +from shutil import copy from dasbus.connection import SystemMessageBus -SYSTEMD_RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf" +NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd" +NETWORKD_VOLATILE_DIR = "/run/systemd/network" +RESOLVED_STUB_RESOLVE = "/run/systemd/resolve/stub-resolv.conf" RESOLV_CONF = "/etc/resolv.conf" -def get_all_devices(proxy): - """ - Returns d-bus object path of all devices as a list of strings. - proxy: usually what is returned by bus.get_proxy('net.connman.iwd','/') - """ - # Gets all D-Bus objects in the proxy. - objects = proxy.GetManagedObjects() - - # Now find out which of them are devices - devices = [] - for name, obj in objects.items(): - if "net.connman.iwd.Device" in obj: - # add all devices to the list - devices.append(name) - - return devices - - -# Copy networkd configs - - -# syslink resolvd +# Copy networkd configs to volatile dir. +# See man:systemd.networkm(5) try: - Path(RESOLV_CONF).symlink_to(SYSTEMD_RESOLVED_STUB_RESOLVE) -except FileExistsError: - print("Resolv.conf already exists") + print("Copying network config files") + dest = Path(NETWORKD_VOLATILE_DIR) + src = Path(NETWORKD_CONFIGS) + + # Create the volatile directory if it doesn't exist + dest.mkdir(parents=True, exist_ok=True) + + # Copy all files in src to dest + for i in src.iterdir(): + copy(i, dest) +except: + sys.exit("An error occured") + + +# Symlink resolvd.conf to systemd's stub-resolvd.conf +# This is needed for DNS resolution to work. +# see https://wiki.archlinux.org/title/Systemd-resolved#DNS +try: + print("Checking resolv.conf") + r = Path(RESOLV_CONF) + if r.exists(): + print(r, "already exists. Removing it") + r.unlink() + print("Linking resolv.conf") + r.symlink_to(RESOLVED_STUB_RESOLVE) +except: + sys.exit("An error occured while linking resolv.conf") + # connect to the System bus bus = SystemMessageBus() # iwd proxy -proxy = bus.get_proxy("net.connman.iwd", "/") +iwd = bus.get_proxy("net.connman.iwd", "/") + +# Get list of all devices +print("Finding connected devices") +objects = iwd.GetManagedObjects() +device_paths = [] +for name, obj in objects.items(): + if "net.connman.iwd.Device" in obj: + # add all devices to the list + print("Found device:", obj["net.connman.iwd.Device"]["Name"]) + device_paths.append(name) + + +# TODO: On first devices, start ad-hoc +# If there is a second device, start AP +# in it print("Bye") From e28a0f75832695293a023a17c1f98ed29853741c Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Sun, 6 Jun 2021 11:25:37 +0530 Subject: [PATCH 4/8] Changed systemd service Fixed permissions in install lines in Makefile. The systemd service now stops NetworkManager and wpa_supplicant before starting naxalnet. It also starts systemd-{networkd,resolved} and iwd alongside naxalnet.service. --- Makefile | 4 ++-- README.md | 10 +++++++++ naxalnet | 56 ++++++++++++++++++++++++++++++++---------------- naxalnet.service | 11 ++++++++++ 4 files changed, 60 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 62373c1..b8dfece 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ PREFIX := /usr install: naxalnet install -d $(DESTDIR)$(PREFIX)/bin install -d $(DESTDIR)$(PREFIX)/lib/systemd/system/ - install naxalnet.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ + install -m644 naxalnet.service $(DESTDIR)$(PREFIX)/lib/systemd/system/ install naxalnet $(DESTDIR)$(PREFIX)/bin/ install -d $(DESTDIR)$(PREFIX)/share/naxalnet/networkd - install systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/networkd + install -m644 systemd-networkd/* $(DESTDIR)$(PREFIX)/share/naxalnet/networkd testdeps: @for i in networkctl systemctl python3; do \ diff --git a/README.md b/README.md index 5c95037..b40d69a 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,16 @@ If you were using NetworkManager before, enable it: sudo systemctl enable --now NetworkManager.service ``` +## License + +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. + +See [LICENSE.md](LICENSE.md) for the complete version of the +license. + ## TODO - add support for wireless ap (WiFi hotspot) diff --git a/naxalnet b/naxalnet index 52c6f21..a87cb81 100755 --- a/naxalnet +++ b/naxalnet @@ -5,6 +5,22 @@ Setup a working BATMAN Advanced network with systemd-networkd and iwd """ +# Copyright (C) 2021 The Authors + +# 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 . + + import sys from pathlib import Path from shutil import copy @@ -30,7 +46,7 @@ try: for i in src.iterdir(): copy(i, dest) except: - sys.exit("An error occured") + sys.exit("An error occured while copying network files") # Symlink resolvd.conf to systemd's stub-resolvd.conf @@ -48,24 +64,26 @@ except: sys.exit("An error occured while linking resolv.conf") -# connect to the System bus -bus = SystemMessageBus() -# iwd proxy -iwd = bus.get_proxy("net.connman.iwd", "/") +# Now, the iwd part +try: + # connect to the System bus + bus = SystemMessageBus() + # iwd proxy + iwd = bus.get_proxy("net.connman.iwd", "/") + # Get list of all devices + print("Finding connected devices") + objects = iwd.GetManagedObjects() + devices = [] + for name, obj in objects.items(): + if "net.connman.iwd.Device" in obj: + # add all devices to the list + print("Found device:", obj["net.connman.iwd.Device"]["Name"]) + devices.append(name) + # TODO: On first devices, start ad-hoc + # If there is a second device, start AP + # in it +except: + sys.exit("An error occured while communicating with iwd") -# Get list of all devices -print("Finding connected devices") -objects = iwd.GetManagedObjects() -device_paths = [] -for name, obj in objects.items(): - if "net.connman.iwd.Device" in obj: - # add all devices to the list - print("Found device:", obj["net.connman.iwd.Device"]["Name"]) - device_paths.append(name) - - -# TODO: On first devices, start ad-hoc -# If there is a second device, start AP -# in it print("Bye") diff --git a/naxalnet.service b/naxalnet.service index 9749225..339c3ed 100644 --- a/naxalnet.service +++ b/naxalnet.service @@ -1,5 +1,16 @@ [Unit] Description=Naxalnet +Requires=systemd-networkd.service +Requires=iwd.service +Wants=systemd-resolved.service +Before=systemd-networkd.service +After=iwd.service + +# Stops NetworkManager and wpa_supplicant if already running +Conflicts=NetworkManager.service +Conflicts=wpa_supplicant.service +After=NetworkManager.service +After=wpa_supplicant.service [Service] Type=oneshot From adc56015890eccbc1630805c0eaf179946cc4af0 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Sun, 6 Jun 2021 17:49:56 +0530 Subject: [PATCH 5/8] updated README.md Updated the inroduction and added a bit more on how it works. --- README.md | 44 +++++++++++++++++++++++--------------------- naxalnet | 17 ++++++++++++++++- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b40d69a..e51331e 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,27 @@ # naxalnet -Naxalnet is an experiment to create an intranet and use it for -communicating with each other during an internet shutdown. It tries to use +**naxalnet** is an experiment to create a wireless mesh network for +communicating with each other during an internet shutdown, using existing software and tech as much as possible. Currently -you can only communicate with other peers running the same software. +you can only communicate with other peers running naxalnet or +using the same network as naxalnet. It uses +[B.A.T.M.A.N. Advanced](https://www.open-mesh.org/projects/batman-adv/wiki), +an implementation of the B.A.T.M.A.N. routing protocol to +communicate with peers. The name naxal comes from Naxalbari, a village in Darjeeling, West Bengal. -A rewrite in python is in progress in the branch python-rewrite. +This is the ongoing python rewrite branch, which uses +[dasbus](https://github.com/rhinstaller/dasbus) to communicate +with iwd.