From 0fb8b6a8abf3fef5f04214951e4f209244e373ef Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Sat, 12 Jun 2021 19:28:59 +0530 Subject: [PATCH 1/5] stopped messing with resolv.conf Completely removed the part that symlinks resolv.conf to systemd-resolved's conf. Instead, added an optional dependency of resolved in README.md --- README.md | 3 ++- naxalnet | 35 +---------------------------------- naxalnet.service | 1 - 3 files changed, 3 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 2b2dbe6..15b904b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ advocating the constitutional rights). ## Requirements -- systemd v248 or more (for batman support) +- systemd{,-networkd} v248 or more (for batman support) - Linux kernel with batman-adv module (if `modinfo batman-adv` shows no error then you already have it) - iwd (for starting ad-hoc network) @@ -36,6 +36,7 @@ advocating the constitutional rights). - [python-dasbus][] - wifi adapter with ad-hoc support - two or more computers with wifi adapter +- systemd-resolved (optional, for DNS) ## Installing diff --git a/naxalnet b/naxalnet index 124a1bc..042c327 100755 --- a/naxalnet +++ b/naxalnet @@ -22,7 +22,6 @@ with systemd-networkd and iwd import sys -import time from pathlib import Path from shutil import copy from dasbus.connection import SystemMessageBus @@ -30,8 +29,6 @@ from dasbus.error import DBusError 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" ADHOC_SSID = "HelloWorld" # Copy networkd configs to volatile dir. @@ -51,36 +48,6 @@ except PermissionError as error: print(error) sys.exit("Make sure you are root") - -# 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.is_symlink(): - # Naxalnet will be started before resolved, so the destination - # is unlikely to exist at the time - # If r is linked to resolved's resolv.conf, dont change it - if r.samefile(RESOLVED_STUB_RESOLVE): - print(r, "is already linked to stub-resolv.conf. Not changing") - # Else if the destination exists - elif r.exists(): - print(r, "is a symlink that exists. Not removing") - else: - print(r, "is a symlink to a destination that doesn't exist. Removing") - r.unlink() - elif r.exists(): - print(r, "is not a symlink") - x = r.rename(RESOLV_CONF + ".naxalnet-bkp") - print(r, "was moved to", x) - print("Linking resolv.conf") - r.symlink_to(RESOLVED_STUB_RESOLVE) -except PermissionError as error: - print(error) - sys.exit("An error occured while linking resolv.conf") - - # Now, the iwd part try: # connect to the System bus @@ -108,7 +75,7 @@ try: print("Device is in", dev1.Mode) print("Switching to ad-hoc") dev1.Mode = "ad-hoc" - # Changing Mode requires connecting to the proxy again + # Changing Mode needs connecting to the proxy again dev1 = bus.get_proxy("net.connman.iwd", devpath) print("Starting ad-hoc network") dev1.StartOpen(ADHOC_SSID) diff --git a/naxalnet.service b/naxalnet.service index 19a6182..98082bf 100644 --- a/naxalnet.service +++ b/naxalnet.service @@ -15,7 +15,6 @@ After=wpa_supplicant.service [Service] Type=oneshot RemainAfterExit=yes -# Temporary (maybe permanent) fix to aborting after changing to ad-hoc Restart=on-failure RestartSec=5sec ExecStart=/usr/bin/naxalnet From 006cb1095cafbe31ff7bd6bf626f10909057a655 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Mon, 14 Jun 2021 11:02:27 +0530 Subject: [PATCH 2/5] checks if device supports ad-hoc before starting ad-hoc Partly added AP support --- naxalnet | 70 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/naxalnet b/naxalnet index 042c327..85a0ba8 100755 --- a/naxalnet +++ b/naxalnet @@ -57,30 +57,58 @@ try: # Get list of all devices print("Finding connected devices") objects = iwd.GetManagedObjects() - devices = [] - for name, obj in objects.items(): + # devices that support ad-hoc + adhoc_devices = [] + # devices that support ap + ap_devices = [] + for path, 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) + name = obj["net.connman.iwd.Device"]["Name"] + print("Found device:", name) + adapter_path = obj["net.connman.iwd.Device"]["Adapter"].get_string() + adapter = objects[adapter_path]["net.connman.iwd.Adapter"] + if "ad-hoc" in adapter["SupportedModes"]: + print(name, "supports ad-hoc") + adhoc_devices.append(path) + if "ap" in adapter["SupportedModes"]: + print(name, "supports ap") + ap_devices.append(path) + print(objects) + print(adhoc_devices) + print(ap_devices) - # Start ad-hoc on first device - devpath = devices.pop() - print("Working on first device", devpath) - dev1 = bus.get_proxy("net.connman.iwd", devpath) - if not dev1.Powered: - print("Device is off. Turning on") - dev1.Powered = True - if dev1.Mode != "ad-hoc": - print("Device is in", dev1.Mode) - print("Switching to ad-hoc") - dev1.Mode = "ad-hoc" - # Changing Mode needs connecting to the proxy again - dev1 = bus.get_proxy("net.connman.iwd", devpath) - print("Starting ad-hoc network") - dev1.StartOpen(ADHOC_SSID) - # TODO: If there is a second device, start AP - # in it + if len(adhoc_devices) != 0: + # Start ad-hoc on first device + dev1path = adhoc_devices.pop() + # Remove device from ap_devices if it exists there + if dev1path in ap_devices: + ap_devices.remove(dev1path) + print("Working on AP") + dev1 = bus.get_proxy("net.connman.iwd", dev1path) + print("Starting ad-hoc on", dev1.Name) + if not dev1.Powered: + print("Device is off. Turning on") + dev1.Powered = True + if dev1.Mode != "ad-hoc": + print("Device is in", dev1.Mode) + print("Switching to ad-hoc") + dev1.Mode = "ad-hoc" + # Changing Mode needs connecting to the proxy again + dev1 = bus.get_proxy("net.connman.iwd", dev1path) + print("Starting ad-hoc network") + dev1.StartOpen(ADHOC_SSID) + + # Start Access point + if len(ap_devices) != 0: + print("Working on AP") + dev2path = ap_devices.pop() + dev2 = bus.get_proxy("net.connman.iwd", dev2path) + print("Starting AP on", dev2.Name) + if not dev1.Powered: + print("Device is off. Turning on") + dev1.Powered = True + # TODO: Start AP on dev2 except DBusError: sys.exit("An error occured while communicating with iwd") From 5e8ff6b65e45e966c009250de38193c48764ce62 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Mon, 14 Jun 2021 11:11:36 +0530 Subject: [PATCH 3/5] updated README.md Added link to Arch Wiki article on systemd-resolved --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 15b904b..2da951d 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,15 @@ advocating the constitutional rights). ### Arch Linux -Install [naxalnet-git][] from the AUR: +Install [naxalnet-git][] from the AUR with your favourite helper: ```sh yay -S naxalnet-git ``` +Optionally, [setup systemd-resolved][arch-resolved] if any of the +computers have internet access. + ### Manually Clone the repo and cd into it. @@ -126,3 +129,4 @@ This project is in alpha stage. Documentation is incomplete. [ssb]: https://scuttlebutt.nz [python-dasbus]: https://github.com/rhinstaller/dasbus [naxalnet-git]: https://aur.archlinux.org/packages/naxalnet-git +[arch-resolved]: https://wiki.archlinux.org/title/Systemd-resolved#DNS From 79b9a161f154b74d29baa7629c81df7c55233233 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Tue, 15 Jun 2021 14:00:48 +0530 Subject: [PATCH 4/5] Now supports AP! Fixed error when already connected to ad-hoc or ap. Removed some print() lines I had forgot to remove after debugging. But we can't close #1 yet (need to update the README) --- naxalnet | 59 +++++++++++++++++++++++++----------------------- naxalnet.service | 4 ++++ 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/naxalnet b/naxalnet index 85a0ba8..57d6f49 100755 --- a/naxalnet +++ b/naxalnet @@ -30,9 +30,12 @@ from dasbus.error import DBusError NETWORKD_CONFIGS = "/usr/share/naxalnet/networkd" NETWORKD_VOLATILE_DIR = "/run/systemd/network" ADHOC_SSID = "HelloWorld" - +AP_SSID = "NaxalNet" +AP_PASSWD = "naxalnet256" # Copy networkd configs to volatile dir. -# See man:systemd.networkm(5) +# The D-Bus API does not support creating new interfaces +# or linking to bridges. So we use config files. +# See man:systemd.network(5) try: print("Copying network config files") dest = Path(NETWORKD_VOLATILE_DIR) @@ -65,7 +68,7 @@ try: if "net.connman.iwd.Device" in obj: # add all devices to the list name = obj["net.connman.iwd.Device"]["Name"] - print("Found device:", name) + print("Found device", name) adapter_path = obj["net.connman.iwd.Device"]["Adapter"].get_string() adapter = objects[adapter_path]["net.connman.iwd.Adapter"] if "ad-hoc" in adapter["SupportedModes"]: @@ -74,17 +77,16 @@ try: if "ap" in adapter["SupportedModes"]: print(name, "supports ap") ap_devices.append(path) - print(objects) - print(adhoc_devices) - print(ap_devices) if len(adhoc_devices) != 0: - # Start ad-hoc on first device + # Start ad-hoc on first device supporting ad-hoc dev1path = adhoc_devices.pop() - # Remove device from ap_devices if it exists there + # The same device is likely to have ap support too. + # But we can't start ad-hoc and ap on the same interface. + # Remove dev1 from ap_devices if it exists there if dev1path in ap_devices: ap_devices.remove(dev1path) - print("Working on AP") + print("Working on ad-hoc") dev1 = bus.get_proxy("net.connman.iwd", dev1path) print("Starting ad-hoc on", dev1.Name) if not dev1.Powered: @@ -96,36 +98,37 @@ try: dev1.Mode = "ad-hoc" # Changing Mode needs connecting to the proxy again dev1 = bus.get_proxy("net.connman.iwd", dev1path) + # If already connected to ad-hoc, stop it + if dev1.Started is True: + print("Already connected to ad-hoc. Stopping") + dev1.Stop() + # Reconnect to proxy or StartOpen won't work + dev1 = bus.get_proxy("net.connman.iwd", dev1path) print("Starting ad-hoc network") dev1.StartOpen(ADHOC_SSID) - # Start Access point + # Start Access point if ap_device is not empty, + # ie, we have more devices if len(ap_devices) != 0: print("Working on AP") dev2path = ap_devices.pop() dev2 = bus.get_proxy("net.connman.iwd", dev2path) - print("Starting AP on", dev2.Name) if not dev1.Powered: print("Device is off. Turning on") dev1.Powered = True - # TODO: Start AP on dev2 + if dev2.Mode != "ap": + print(dev2.Name, "is in", dev2.Mode) + print("Switching to ap") + dev2.Mode = "ap" + dev2 = bus.get_proxy("net.connman.iwd", dev2path) + if dev2.Started is True: + print("An AP is already started on", dev2.Name) + print("Stopping") + dev2.Stop() + dev2 = bus.get_proxy("net.connman.iwd", dev2path) + print("Starting AP on", dev2.Name) + dev2.Start(AP_SSID, AP_PASSWD) except DBusError: sys.exit("An error occured while communicating with iwd") -# Sleep my little baby-oh -# Sleep until you waken -# When you wake you'll see the world -# If I'm not mistaken... -# -# Kiss a lover -# Dance a measure, -# Find your name -# And buried treasure... -# -# Face your life -# Its pain, -# Its pleasure, -# Leave no path untaken. -# -# -- Neil Gaiman, The Graveyard Book print("Bye") diff --git a/naxalnet.service b/naxalnet.service index 98082bf..1f7880f 100644 --- a/naxalnet.service +++ b/naxalnet.service @@ -17,6 +17,10 @@ Type=oneshot RemainAfterExit=yes Restart=on-failure RestartSec=5sec +# IWD takes some time to find devices. +# Without the sleep 5, naxalnet could not detect the second +# device while testing. +ExecStartPre=/usr/bin/sleep 5 ExecStart=/usr/bin/naxalnet [Install] From b8d14e1d65510f0291f222b7c528b21bfd4b6220 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Tue, 15 Jun 2021 14:24:03 +0530 Subject: [PATCH 5/5] Updated README Now the issue #1 is ready to be closed. --- README.md | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2da951d..f592d57 100644 --- a/README.md +++ b/README.md @@ -15,14 +15,14 @@ West Bengal. @@ -37,6 +37,7 @@ advocating the constitutional rights). - wifi adapter with ad-hoc support - two or more computers with wifi adapter - systemd-resolved (optional, for DNS) +- batctl (optional, for debugging) ## Installing @@ -65,15 +66,41 @@ without rebooting: sudo systemctl daemon-reload ``` -## Running the program +## How to use You need more than one computer running for the connection to work. -Start the naxalnet service: + +### Communicate between peers + +Connect a wifi adapter to all the computers you intend to run +naxalnet. +Start the naxalnet service on all of them: ```sh sudo systemctl start naxalnet.service ``` +To test if it works, run `ip addr` to find out your address. +Note the `inet` or `inet6` address of `bridge0`. Ping the address +from another computer (example: `ping 169.254.62.90`) to find out +if it is online. Press Ctrl-C to stop. + +### Getting internet access + +Connect an ethernet to any of the peers and start naxalnet. +Now all the peers should be able to connect after renewing +their DHCP connection (`sudo networkctl renew bridge0`). + +### Tethering via WiFi AP + +Connect two wifi adapters on a device and start naxalnet. +Now an ap will be created on one of the devices with +SSID `NaxalNet` and password `naxalnet256`. +If you had set up internet access on one of the peers, internet +can be accessed from the AP. + +### Running at boot + Starting the service will stop `NetworkManager.service` and `wpa_supplicant.service` if it is running. If you start either of these services after naxalnet is started, systemd will stop naxalnet.