From ef054102feb52ddb99dc2d2672ecec57175f53f5 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Thu, 6 Aug 2026 10:07:59 +0200 Subject: [PATCH 1/2] alfred: add new package alfred is a user space daemon for distributing arbitrary local information (e.g. hostnames or vis data) over a batman-adv mesh network in a decentralized fashion via IPv6 link-local multicast. Moved from the openwrt/routing feed, as discussed in https://github.com/openwrt/routing/issues/184. The one deviation from the feed copy is SUBMENU:=Wireless, which the feed copy does not set at all: without it the package sits ungrouped at the top of the Network menu. Wireless keeps it next to mesh11sd and batctl, which move across with the same value. A dedicated Mesh submenu is the better home for all three and is worth adding once enough of the mesh packages have landed to justify it. Signed-off-by: Josef Schlehofer Co-authored-by: Claude Fable 5 --- net/alfred/Config.in | 22 +++++++ net/alfred/Makefile | 89 ++++++++++++++++++++++++++ net/alfred/files/alfred.config | 8 +++ net/alfred/files/alfred.init | 97 ++++++++++++++++++++++++++++ net/alfred/files/bat-hosts.lua | 112 +++++++++++++++++++++++++++++++++ 5 files changed, 328 insertions(+) create mode 100644 net/alfred/Config.in create mode 100644 net/alfred/Makefile create mode 100644 net/alfred/files/alfred.config create mode 100755 net/alfred/files/alfred.init create mode 100644 net/alfred/files/bat-hosts.lua diff --git a/net/alfred/Config.in b/net/alfred/Config.in new file mode 100644 index 00000000000000..97f9ea21f8b67a --- /dev/null +++ b/net/alfred/Config.in @@ -0,0 +1,22 @@ +config ALFRED_NEEDS_lua + bool + +config ALFRED_NEEDS_libgps + bool + +config PACKAGE_ALFRED_VIS + bool "enable vis server for alfred" + depends on PACKAGE_alfred + default y + +config PACKAGE_ALFRED_BATHOSTS + bool "enable autogeneration of /etc/bat-hosts" + depends on PACKAGE_alfred + select ALFRED_NEEDS_lua + default n + +config PACKAGE_ALFRED_GPSD + bool "enable gpsd service for alfred" + depends on PACKAGE_alfred + select ALFRED_NEEDS_libgps + default n diff --git a/net/alfred/Makefile b/net/alfred/Makefile new file mode 100644 index 00000000000000..c0e7342e966e9c --- /dev/null +++ b/net/alfred/Makefile @@ -0,0 +1,89 @@ +# SPDX-License-Identifier: GPL-2.0-only + +include $(TOPDIR)/rules.mk + +PKG_NAME:=alfred +PKG_VERSION:=2026.2 +PKG_RELEASE:=1 + +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz +PKG_SOURCE_URL:=https://downloads.open-mesh.org/batman/releases/batman-adv-$(PKG_VERSION) +PKG_HASH:=d995748be5f62a42091525ccc102df5ac642186e9b9014698a955a4469b69b96 + +PKG_MAINTAINER:=Simon Wunderlich +PKG_LICENSE:=GPL-2.0-only MIT +PKG_LICENSE_FILES:=LICENSES/preferred/GPL-2.0 LICENSES/preferred/MIT + +PKG_BUILD_PARALLEL:=1 +PKG_BUILD_FLAGS:=gc-sections lto + +PKG_CONFIG_DEPENDS += \ + CONFIG_ALFRED_NEEDS_lua \ + CONFIG_ALFRED_NEEDS_libgps \ + CONFIG_PACKAGE_ALFRED_VIS \ + CONFIG_PACKAGE_ALFRED_BATHOSTS \ + CONFIG_PACKAGE_ALFRED_GPSD + +include $(INCLUDE_DIR)/package.mk + +define Package/alfred + SECTION:=net + CATEGORY:=Network + SUBMENU:=Wireless + TITLE:=A.L.F.R.E.D. - Almighty Lightweight Fact Remote Exchange Daemon + URL:=https://www.open-mesh.org/ + DEPENDS:=@IPV6 +libnl-tiny +librt \ + +ALFRED_NEEDS_lua:lua \ + +ALFRED_NEEDS_libgps:libgps +endef + +define Package/alfred/description + alfred is a user space daemon for distributing arbitrary local information + over the mesh/network in a decentralized fashion. This data can be anything + which appears to be useful - originally designed to replace the batman-adv + visualization (vis), you may distribute hostnames, phone books, administration + information, DNS information, the local weather forecast ... + + alfred runs as daemon in the background of the system. A user may insert + information by using the alfred binary on the command line, or use special + programs to communicate with alfred (done via unix sockets). alfred then takes + care of distributing the local information to other alfred servers on other + nodes. This is done via IPv6 link-local multicast, and does not require any + configuration. A user can request data from alfred, and will receive the + information available from all alfred servers in the network. +endef + +define Package/alfred/conffiles +/etc/config/alfred +/etc/alfred/bat-hosts.lua +endef + +define Package/alfred/config + source "$(SOURCE)/Config.in" +endef + +MAKE_FLAGS += \ + CONFIG_ALFRED_VIS=$(if $(CONFIG_PACKAGE_ALFRED_VIS),y,n) \ + CONFIG_ALFRED_GPSD=$(if $(CONFIG_PACKAGE_ALFRED_GPSD),y,n) \ + CONFIG_ALFRED_CAPABILITIES=n \ + LIBNL_NAME="libnl-tiny" \ + LIBNL_GENL_NAME="libnl-tiny" \ + REVISION="$(PKG_VERSION)-openwrt-$(PKG_RELEASE)" + +define Package/alfred/install + $(INSTALL_DIR) $(1)/usr/sbin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/alfred $(1)/usr/sbin/ + $(if $(CONFIG_PACKAGE_ALFRED_VIS), \ + $(INSTALL_BIN) $(PKG_BUILD_DIR)/vis/batadv-vis $(1)/usr/sbin/) + $(if $(CONFIG_PACKAGE_ALFRED_GPSD), \ + $(INSTALL_BIN) $(PKG_BUILD_DIR)/gpsd/alfred-gpsd $(1)/usr/sbin/) + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_BIN) ./files/alfred.init $(1)/etc/init.d/alfred + $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_CONF) ./files/alfred.config $(1)/etc/config/alfred + $(INSTALL_DIR) $(1)/etc/alfred + $(if $(CONFIG_PACKAGE_ALFRED_BATHOSTS), \ + $(INSTALL_BIN) ./files/bat-hosts.lua $(1)/etc/alfred/bat-hosts.lua) +endef + +$(eval $(call BuildPackage,alfred)) diff --git a/net/alfred/files/alfred.config b/net/alfred/files/alfred.config new file mode 100644 index 00000000000000..704adf13b26418 --- /dev/null +++ b/net/alfred/files/alfred.config @@ -0,0 +1,8 @@ +config 'alfred' 'alfred' + list interface 'br-lan' + option mode 'master' + option batmanif 'bat0' + option start_vis '1' + option run_facters '1' +# REMOVE THIS LINE TO ENABLE ALFRED + option disabled '1' diff --git a/net/alfred/files/alfred.init b/net/alfred/files/alfred.init new file mode 100755 index 00000000000000..f191827fd0af29 --- /dev/null +++ b/net/alfred/files/alfred.init @@ -0,0 +1,97 @@ +#!/bin/sh /etc/rc.common + +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +START=99 +USE_PROCD=1 +alfred_args="" +vis_args="" +facters_dir="/etc/alfred" +enable=0 +vis_enable=0 + +append_interface() +{ + append "interfaces" "$1" "," +} + +alfred_start() { + local args="" + local section="$1" + local disabled interface mode + local interfaces + + # check if section is disabled + config_get_bool disabled "$section" disabled 0 + [ $disabled = 0 ] || return 1 + + args="-f" + + config_list_foreach "$section" "interface" append_interface + if [ -z "$interfaces" ]; then + config_get interface "$section" interface + append_interface "$interface" + fi + append args "-i $interfaces" + + config_get mode "$section" mode + [ "$mode" = "master" ] && append args "-m" + + config_get batmanif "$section" batmanif + append args "-b $batmanif" + + append alfred_args "$args" + enable=1 + + config_get_bool start_vis "$section" start_vis 0 + if [ "$start_vis" = 1 ] && [ -x /usr/sbin/batadv-vis ]; then + vis_enable=1 + append vis_args "-i $batmanif -s" + fi + + config_get_bool run_facters "$section" run_facters 0 + + return 0 +} + +start_service() { + config_load "alfred" + config_foreach alfred_start alfred + + [ "$enable" = "0" ] && return 0 + + procd_open_instance "alfred" + procd_set_param command /usr/sbin/alfred + procd_append_param command ${alfred_args} + procd_close_instance + + [ "$vis_enable" = "1" ] && { + procd_open_instance "batadv-vis" + procd_set_param command /usr/sbin/batadv-vis + procd_append_param command ${vis_args} + procd_close_instance + } + + [ "$run_facters" = "1" ] && { + ( for file in $facters_dir/* ; do [ -x $file ] && $file ; done ) + if ! ( grep -q "for file in $facters_dir/\* ; do " /etc/crontabs/root 2>/dev/null ) ; then + echo "*/5 * * * * ( for file in $facters_dir/* ; do [ -x \$file ] && \$file ; done )" >> /etc/crontabs/root + /etc/init.d/cron enable + /etc/init.d/cron restart + fi + } +} + +service_triggers() { + procd_add_reload_trigger "alfred" +} + +stop_service() { + [ -e /etc/crontabs/root ] && { + sed "\|for file in $facters_dir/\* ; do |d" -i /etc/crontabs/root + /etc/init.d/cron restart + } +} diff --git a/net/alfred/files/bat-hosts.lua b/net/alfred/files/bat-hosts.lua new file mode 100644 index 00000000000000..f9fe586876beff --- /dev/null +++ b/net/alfred/files/bat-hosts.lua @@ -0,0 +1,112 @@ +#!/usr/bin/lua + +local type_id = 64 -- bat-hosts + +function get_hostname() + local hostfile = io.open("/proc/sys/kernel/hostname", "r") + local ret_string = hostfile:read() + hostfile:close() + return ret_string +end + +function get_interfaces_names() + local ret = {} + + for name in io.popen("ls -1 /sys/class/net/"):lines() do + table.insert(ret, name) + end + + return ret +end + +function get_interface_address(name) + local addressfile = io.open("/sys/class/net/"..name.."/address", "r") + local ret_string = addressfile:read() + addressfile:close() + return ret_string +end + + +local function generate_bat_hosts() +-- get hostname and interface macs/names +-- then return a table containing valid bat-hosts lines + local n, i + local ifaces, ret = {}, {} + + local hostname = get_hostname() + + for n, i in ipairs(get_interfaces_names()) do + local address = get_interface_address(i) + if not ifaces[address] then ifaces[address] = i end + end + + for mac, iname in pairs(ifaces) do + if mac:match("^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$") and not mac:match("00:00:00:00:00:00") then + table.insert(ret, mac.." "..hostname.."_"..iname.."\n") + end + end + + return ret +end + +local function publish_bat_hosts() +-- pass a raw chunk of data to alfred + local fd = io.popen("alfred -s " .. type_id, "w") + if fd then + local ret = generate_bat_hosts() + if ret then + fd:write(table.concat(ret)) + end + fd:close() + end +end + +local function write_bat_hosts(rows) + local content = { "### /tmp/bat-hosts generated by alfred-bat-hosts\n", + "### /!\\ This file is overwritten every 5 minutes /!\\\n", + "### (To keep manual changes, replace /etc/bat-hosts symlink with a static file)\n" } + + -- merge the chunks from all nodes, de-escaping newlines + for _, row in ipairs(rows) do + local node, value = unpack(row) + table.insert(content, "# Node ".. node .. "\n") + table.insert(content, value:gsub("\x0a", "\n") .. "\n") + end + + -- write parsed content down to disk + local fd = io.open("/tmp/bat-hosts", "w") + if fd then + fd:write(table.concat(content)) + fd:close() + end + + -- try to make a symlink in /etc pointing to /tmp, + -- if it exists, ln will do nothing. + os.execute("ln -ns /tmp/bat-hosts /etc/bat-hosts 2>/dev/null") +end + +local function receive_bat_hosts() +-- read raw chunks from alfred, convert them to a nested table and call write_bat_hosts +-- "alfred -r" can fail in slave nodes (returns empty stdout), so: +-- check output is not null before writing /tmp/bat-hosts, and retry 3 times before giving up. + for n = 1, 3 do + local fd = io.popen("alfred -r " .. type_id) + --[[ this command returns something like + { "54:e6:fc:b9:cb:37", "00:11:22:33:44:55 ham_wlan0\x0a00:22:33:22:33:22 ham_eth0\x0a" }, + { "90:f6:52:bb:ec:57", "00:22:33:22:33:23 spam\x0a" }, + ]]-- + + if fd then + local output = fd:read("*a") + fd:close() + if output and output ~= "" then + assert(loadstring("rows = {" .. output .. "}"))() + write_bat_hosts(rows) + break + end + end + end +end + +publish_bat_hosts() +receive_bat_hosts() From 3c5db28b04ab347d1f814783a08690e72c5359ed Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Thu, 6 Aug 2026 12:42:19 +0200 Subject: [PATCH 2/2] alfred: handle unreadable files in bat-hosts.lua get_interface_address() calls io.open() and dereferences the result without checking it. /sys/class/net does not contain interfaces only: with the bonding module loaded it also holds the plain file bonding_masters, which the `ls -1` in get_interfaces_names() returns and which has no address below it. io.open() then returns nil and the script dies with "attempt to index a nil value", so no bat-hosts data is published at all on such a node. Return nil when the file cannot be opened and skip those entries in the caller, which would otherwise index the interface table with nil. get_hostname() gets the same guard. Reported-by: openwrt-ai[bot] Signed-off-by: Josef Schlehofer Co-authored-by: Claude Fable 5 --- net/alfred/files/bat-hosts.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/alfred/files/bat-hosts.lua b/net/alfred/files/bat-hosts.lua index f9fe586876beff..c9b5619395d648 100644 --- a/net/alfred/files/bat-hosts.lua +++ b/net/alfred/files/bat-hosts.lua @@ -4,6 +4,7 @@ local type_id = 64 -- bat-hosts function get_hostname() local hostfile = io.open("/proc/sys/kernel/hostname", "r") + if not hostfile then return nil end local ret_string = hostfile:read() hostfile:close() return ret_string @@ -20,7 +21,10 @@ function get_interfaces_names() end function get_interface_address(name) + -- /sys/class/net also contains plain files, e.g. bonding_masters + -- once the bonding module is loaded, which have no address below them local addressfile = io.open("/sys/class/net/"..name.."/address", "r") + if not addressfile then return nil end local ret_string = addressfile:read() addressfile:close() return ret_string @@ -37,7 +41,7 @@ local function generate_bat_hosts() for n, i in ipairs(get_interfaces_names()) do local address = get_interface_address(i) - if not ifaces[address] then ifaces[address] = i end + if address and not ifaces[address] then ifaces[address] = i end end for mac, iname in pairs(ifaces) do