From c08668058e890bcbb48eafdd4d2ed8573f02cafa Mon Sep 17 00:00:00 2001 From: Koda YeenBean Date: Sat, 17 Oct 2026 12:37:51 +0000 Subject: [PATCH] added settings to disable all broadcasting from openui --- README.md | 23 +++++++++- files/openuf.conf | 10 ++++- src/announce.c | 106 +++++++++++++++++++++++++++++++++------------- src/announce.h | 11 +++-- src/config.c | 9 ++++ src/config.h | 3 ++ src/main.c | 15 +++++-- 7 files changed, 138 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index fb663f0..4f61f56 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,10 @@ controller_ip = # Empty: use IPv4 default gateway lan_if = br-lan # Default Lan interface Name ufmodel = u6-inwall # emulated model inform_interval = 10 # inform interval -enable_announce = 1 +enable_announce = 1 # Master switch for UDP discovery +enable_announce_broadcast = 1 # Broadcast discovery on lan_if +enable_announce_multicast = 1 # Multicast discovery on lan_if +enable_lldp_tx = 1 # LLDP transmission on model Ethernet ports enable_inform = 1 # Enable adoption and telemetry requests enable_logging = 1 # Write diagnostics to /var/log/openuf.log protocol_debug_level = 0 # 0=off, 1=safe response details, @@ -166,6 +169,24 @@ Set `controller_ip` to a controller IPv4 address or hostname to override autodetection. The automatic mode uses the IPv4 default-route gateway for initial adoption; after adoption, the controller-provided inform URL is kept. +To isolate discovery traffic when troubleshooting switch outages, set +`enable_announce_broadcast`, `enable_announce_multicast`, or `enable_lldp_tx` +to `0` independently and run `/etc/init.d/openuf restart`. All three default to `1` +when omitted from older configurations. `enable_announce=0` disables both UDP +channels; it does not disable LLDP or inform. Setting both UDP channel controls +to `0` also disables UDP discovery. + +Both UDP discovery sockets are bound to `lan_if`, including explicit multicast +interface selection. If a requested socket cannot be configured on that +interface, UDP discovery is disabled and an error is reported; it does not fall +back to a different interface. LLDP continues to use the emulated model's +Ethernet port list through interface-specific raw sockets. + +`enable_lldp_tx=0` stops only openUF's LLDP transmission. LLDP neighbor telemetry +and transmissions from a separately running `lldpd` remain independent. These +controls support diagnosis; they do not establish or fix the cause of switch +outages. + Daemon settings live in `/etc/openuf/openuf.conf`. Adoption and controller state are persisted separately in `/etc/openuf/state.json`; do not copy that file between devices because it contains the device authentication key. diff --git a/files/openuf.conf b/files/openuf.conf index 8930262..326e511 100644 --- a/files/openuf.conf +++ b/files/openuf.conf @@ -5,7 +5,12 @@ # lan_if: primary LAN interface (used for the AP MAC and IP) # ufmodel: emulated model (u6-inwall recommended) # inform_interval: seconds between inform requests (minimum 5) -# enable_announce: 1=enable UDP discovery (port 10001) +# enable_announce: master switch for UDP discovery (port 10001) +# enable_announce_broadcast: 1=send discovery to 255.255.255.255 +# enable_announce_multicast: 1=send discovery to 233.89.188.1 +# Both UDP channels are bound to lan_if. +# enable_lldp_tx: 1=send LLDP on model Ethernet ports; independent of announce +# Does not disable neighbor reading or a separate lldpd. # enable_inform: 1=enable HTTP inform requests (adoption and telemetry) # enable_logging: 1=enable logging to /var/log/openuf.log (default: off) # protocol_debug_level: 0=off, 1=safe response structure, @@ -16,6 +21,9 @@ lan_if = br-lan ufmodel = uapg2-ac-lr inform_interval = 10 enable_announce = 1 +enable_announce_broadcast = 1 +enable_announce_multicast = 1 +enable_lldp_tx = 1 enable_inform = 1 enable_logging = 0 protocol_debug_level = 0 diff --git a/src/announce.c b/src/announce.c index 6a5789d..2f048c6 100644 --- a/src/announce.c +++ b/src/announce.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "announce.h" #include "config.h" @@ -107,16 +108,76 @@ static void parse_ip(const char *s, unsigned char out[4]) for (int i = 0; i < 4; i++) out[i] = (unsigned char)b[i]; } +/* Bind discovery to the selected LAN; never fall back to another route. */ +static int announce_socket(const char *ifname, unsigned int ifindex, + int multicast) +{ + int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (fd < 0) { + LOGF(stderr, "announce socket on %s (%s): %s", ifname, + multicast ? "multicast" : "broadcast", strerror(errno)); + return -1; + } + + if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, + ifname, strlen(ifname) + 1) < 0) + goto fail; + + if (multicast) { + struct ip_mreqn iface = { .imr_ifindex = (int)ifindex }; + unsigned char ttl = 1, loop = 0; + if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, + &iface, sizeof(iface)) < 0 || + setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, + &ttl, sizeof(ttl)) < 0 || + setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, + &loop, sizeof(loop)) < 0) + goto fail; + } else { + int on = 1; + if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) + goto fail; + } + + /* An ephemeral source port avoids conflicting with other discovery agents. */ + struct sockaddr_in local = { + .sin_family = AF_INET, + .sin_addr.s_addr = INADDR_ANY, + .sin_port = 0, + }; + if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) + goto fail; + return fd; + +fail: + LOGF(stderr, "announce socket setup on %s (%s): %s", ifname, + multicast ? "multicast" : "broadcast", strerror(errno)); + close(fd); + return -1; +} + /* ─── announce_init ─────────────────────────────────────────────── */ int announce_init(announce_ctx_t *ctx, const uf_model_t *m, const char *mac_str, - const char *ip_str) + const char *ip_str, + const char *ifname, + int enable_broadcast, + int enable_multicast) { memset(ctx, 0, sizeof(*ctx)); ctx->sockfd = -1; ctx->sockfd_mcast = -1; + unsigned int ifindex = 0; + if (enable_broadcast || enable_multicast) { + if (!ifname || !ifname[0] || strlen(ifname) >= IFNAMSIZ || + !(ifindex = if_nametoindex(ifname))) { + LOGF(stderr, "announce: invalid or missing LAN interface"); + return -1; + } + } + unsigned char mac[6], ip[4]; parse_mac(mac_str, mac); parse_ip(ip_str, ip); @@ -197,37 +258,20 @@ int announce_init(announce_ctx_t *ctx, ctx->counter = 0; ctx->uptime = 10; - /* ── Socket for broadcast 255.255.255.255 ─────────────────── */ - ctx->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (ctx->sockfd < 0) { - LOGF(stderr, "announce socket: %s", strerror(errno)); - return -1; + if (enable_broadcast) { + ctx->sockfd = announce_socket(ifname, ifindex, 0); + if (ctx->sockfd < 0) goto fail; } - int on = 1; - setsockopt(ctx->sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); - /* Bind an ephemeral port; OpenWrt does not allow broadcast setpeername(). */ - struct sockaddr_in bind_addr = { - .sin_family = AF_INET, - .sin_addr.s_addr = INADDR_ANY, - .sin_port = 0, - }; - bind(ctx->sockfd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); - - /* ── Multicast Socket (233.89.188.1) ────────────────────────── */ - /* The UniFi Controller also listens on this multicast group, - * allowing discovery even when broadcast traffic is filtered. */ - ctx->sockfd_mcast = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (ctx->sockfd_mcast >= 0) { - int ttl = 1; /* TTL=1: no cruzar router */ - setsockopt(ctx->sockfd_mcast, IPPROTO_IP, IP_MULTICAST_TTL, - &ttl, sizeof(ttl)); - int loop = 0; - setsockopt(ctx->sockfd_mcast, IPPROTO_IP, IP_MULTICAST_LOOP, - &loop, sizeof(loop)); - bind(ctx->sockfd_mcast, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); + if (enable_multicast) { + ctx->sockfd_mcast = announce_socket(ifname, ifindex, 1); + if (ctx->sockfd_mcast < 0) goto fail; } return 0; + +fail: + announce_close(ctx); + return -1; } /* ─── announce_send ─────────────────────────────────────────────── */ @@ -248,7 +292,8 @@ int announce_send(announce_ctx_t *ctx) .sin_port = htons(ANNOUNCE_PORT), .sin_addr.s_addr = INADDR_BROADCAST, }; - if (sendto(ctx->sockfd, ctx->pkt, ctx->pkt_len, 0, + if (ctx->sockfd >= 0 && + sendto(ctx->sockfd, ctx->pkt, ctx->pkt_len, 0, (struct sockaddr *)&dest_bcast, sizeof(dest_bcast)) < 0) { LOGF(stderr, "announce sendto broadcast: %s", strerror(errno)); ret = -1; @@ -263,7 +308,8 @@ int announce_send(announce_ctx_t *ctx) inet_pton(AF_INET, "233.89.188.1", &dest_mcast.sin_addr); if (sendto(ctx->sockfd_mcast, ctx->pkt, ctx->pkt_len, 0, (struct sockaddr *)&dest_mcast, sizeof(dest_mcast)) < 0) { - /* This is nonfatal; some kernels have no multicast route. */ + LOGF(stderr, "announce sendto multicast: %s", strerror(errno)); + ret = -1; } } diff --git a/src/announce.h b/src/announce.h index 46bb04d..4d436f2 100644 --- a/src/announce.h +++ b/src/announce.h @@ -1,6 +1,7 @@ #ifndef OPENUF_ANNOUNCE_H #define OPENUF_ANNOUNCE_H +#include #include "ufmodel.h" /* Announce context – keeps mutable state between sends */ @@ -15,16 +16,20 @@ typedef struct { uint32_t uptime; } announce_ctx_t; -/* Build the static part of the announce packet and open the UDP socket. +/* Build the packet and open only enabled UDP sockets, bound to ifname. + * Both channels may be disabled. Any setup failure closes all sockets. * mac_str : "aa:bb:cc:dd:ee:ff" * ip_str : "192.168.1.x" * Returns 0 on success. */ int announce_init(announce_ctx_t *ctx, const uf_model_t *model, const char *mac_str, - const char *ip_str); + const char *ip_str, + const char *ifname, + int enable_broadcast, + int enable_multicast); -/* Send one announce burst to 255.255.255.255:10001. +/* Send enabled discovery channels to broadcast and/or multicast port 10001. * Increments counter and uptime. Returns 0 on success. */ int announce_send(announce_ctx_t *ctx); diff --git a/src/config.c b/src/config.c index 0a7d9b3..9370896 100644 --- a/src/config.c +++ b/src/config.c @@ -13,6 +13,9 @@ void config_load(openuf_config_t *cfg) strncpy(cfg->ufmodel, DEFAULT_UFMODEL, sizeof(cfg->ufmodel) - 1); cfg->inform_interval = DEFAULT_INFORM_INTERVAL; cfg->enable_announce = 1; + cfg->enable_announce_broadcast = 1; + cfg->enable_announce_multicast = 1; + cfg->enable_lldp_tx = 1; cfg->enable_inform = 1; /* File logging is opt-in to avoid flash wear and unbounded log growth. */ cfg->enable_logging = 0; @@ -50,6 +53,12 @@ void config_load(openuf_config_t *cfg) else if (!strcmp(key, "ufmodel")) strncpy(cfg->ufmodel, val, sizeof(cfg->ufmodel) - 1); else if (!strcmp(key, "inform_interval")) cfg->inform_interval = atoi(val); else if (!strcmp(key, "enable_announce")) cfg->enable_announce = atoi(val); + else if (!strcmp(key, "enable_announce_broadcast")) + cfg->enable_announce_broadcast = atoi(val); + else if (!strcmp(key, "enable_announce_multicast")) + cfg->enable_announce_multicast = atoi(val); + else if (!strcmp(key, "enable_lldp_tx")) + cfg->enable_lldp_tx = atoi(val); else if (!strcmp(key, "enable_inform")) cfg->enable_inform = atoi(val); else if (!strcmp(key, "enable_logging")) cfg->enable_logging = atoi(val); else if (!strcmp(key, "protocol_debug_level")) diff --git a/src/config.h b/src/config.h index d3c9d3e..3cc7088 100644 --- a/src/config.h +++ b/src/config.h @@ -60,6 +60,9 @@ typedef struct { char ufmodel[32]; /* "u6-inwall" | "u6-lite" */ int inform_interval; int enable_announce; + int enable_announce_broadcast; + int enable_announce_multicast; + int enable_lldp_tx; int enable_inform; int enable_logging; int protocol_debug_level; diff --git a/src/main.c b/src/main.c index 1256d60..670d800 100644 --- a/src/main.c +++ b/src/main.c @@ -191,10 +191,17 @@ int main(int argc, char *argv[]) LOG("Daemon started"); /* ── Announce socket ────────────────────────────────────────── */ - announce_ctx_t ann; + announce_ctx_t ann = { .sockfd = -1, .sockfd_mcast = -1 }; + cfg.enable_announce = cfg.enable_announce && + (cfg.enable_announce_broadcast || cfg.enable_announce_multicast); + LOGF(stdout, "Discovery interface=%s broadcast=%d multicast=%d; LLDP transmit=%d", + cfg.lan_if, cfg.enable_announce && cfg.enable_announce_broadcast, + cfg.enable_announce && cfg.enable_announce_multicast, !!cfg.enable_lldp_tx); if (cfg.enable_announce) { - if (announce_init(&ann, model, mac_str, ip_str) != 0) { - LOG("Failed to initialize announce service"); + if (announce_init(&ann, model, mac_str, ip_str, cfg.lan_if, + cfg.enable_announce_broadcast, + cfg.enable_announce_multicast) != 0) { + LOGF(stderr, "UDP discovery disabled: failed to initialize on %s", cfg.lan_if); cfg.enable_announce = 0; } } @@ -226,7 +233,7 @@ int main(int argc, char *argv[]) } /* LLDP frames for each Ethernet interface */ - if ((now - last_lldp) >= LLDP_INTERVAL) { + if (cfg.enable_lldp_tx && (now - last_lldp) >= LLDP_INTERVAL) { LOG("Sending LLDP frames"); for (int i = 0; i < model->port_table_len; i++) { const char *iface = model->port_table[i].ifname;