From patchwork Wed Jan 12 21:05:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Lindner X-Patchwork-Id: 18449 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id D2F8683E4A; Wed, 12 Jan 2022 22:05:25 +0100 (CET) Received: from s2.neomailbox.net (s2.neomailbox.net [5.148.176.60]) by diktynna.open-mesh.org (Postfix) with ESMTPS id F0E7A8038C for ; Wed, 12 Jan 2022 22:05:22 +0100 (CET) From: Marek Lindner To: b.a.t.m.a.n@lists.open-mesh.org Subject: [PATCH 1/3] alfred: move interface check into helper function Date: Wed, 12 Jan 2022 22:05:04 +0100 Message-Id: <20220112210506.3488775-1-mareklindner@neomailbox.ch> In-Reply-To: <10410848.OOsao9LFFs@rousseau> References: <10410848.OOsao9LFFs@rousseau> MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=none; spf=pass (diktynna.open-mesh.org: domain of mareklindner@neomailbox.ch designates 5.148.176.60 as permitted sender) smtp.mailfrom=mareklindner@neomailbox.ch; dmarc=none ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1642021523; a=rsa-sha256; cv=none; b=kxNV2lb+TwZ+G0l4V5tM0ke+ufpkjXIs0IoAofRX/034E8A09pV0UeRXjogtdVWpxhFBnJ n6l6mkiRCTBqSAI6QXP8PMZdK1Hi/0nXLTYGAQAN2Wi2DwGysxBmAmDfr00LC6Idwth5Gr Zp7cfxRD794t9RxRTJXMXzs5chfc3Fo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1642021523; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T7Vfi45bTFi+bODG76o1WqTufK7at7JFpVqaywdEsd0=; b=MpCxBHmscYS1q+G4aLoH0nF7BkanwyUVBbVLq8yqMtCQx8zRjtL1io3XcS7qY/oJPcKvPD uobzONoZ/pvrC2gHFhrpGEGO+zL08IJI41dV5FffGkw2rK6O/pW9sMs+Hh3N/pjCeqwnZ4 jZVKI6AdXJHAj0NN1SpyX90sAlDXT30= Message-ID-Hash: KXUGSTR5WDS5WRM6A37NQKAQ53TCPI5D X-Message-ID-Hash: KXUGSTR5WDS5WRM6A37NQKAQ53TCPI5D X-MailFrom: mareklindner@neomailbox.ch X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Marek Lindner X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Marek Lindner --- alfred.h | 1 + server.c | 4 ++-- util.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/alfred.h b/alfred.h index 0e4dd26..c595b06 100644 --- a/alfred.h +++ b/alfred.h @@ -204,5 +204,6 @@ int time_diff(struct timespec *tv1, struct timespec *tv2, void time_random_seed(void); uint16_t get_random_id(void); bool is_valid_ether_addr(uint8_t *addr); +bool is_iface_disabled(char *iface); int ipv4_arp_request(struct interface *interface, const alfred_addr *addr, struct ether_addr *mac); diff --git a/server.c b/server.c index 85bf453..1efc211 100644 --- a/server.c +++ b/server.c @@ -205,7 +205,7 @@ static void update_server_info(struct globals *globals) if (globals->opmode == OPMODE_PRIMARY) return; - if (strcmp(globals->mesh_iface, "none") != 0) { + if (!is_iface_disabled(globals->mesh_iface)) { tg_hash = tg_hash_new(globals->mesh_iface); if (!tg_hash) { fprintf(stderr, "Failed to create translation hash\n"); @@ -385,7 +385,7 @@ int alfred_server(struct globals *globals) return -1; } - if (strcmp(globals->mesh_iface, "none") != 0 && + if (!is_iface_disabled(globals->mesh_iface) && batadv_interface_check(globals->mesh_iface) < 0 && !globals->force) { fprintf(stderr, "Can't start server: batman-adv interface %s not found\n", diff --git a/util.c b/util.c index 42a625a..eabef57 100644 --- a/util.c +++ b/util.c @@ -67,6 +67,17 @@ bool is_valid_ether_addr(uint8_t addr[ETH_ALEN]) return true; } +bool is_iface_disabled(char *iface) +{ + if (!iface) + return false; + + if (strcmp(iface, "none") != 0) + return false; + + return true; +} + static void ipv4_request_mac_resolve(const alfred_addr *addr) { const struct sockaddr *sockaddr; From patchwork Wed Jan 12 21:05:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Lindner X-Patchwork-Id: 18450 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id BCB5D83E7C; Wed, 12 Jan 2022 22:05:45 +0100 (CET) Received: from s2.neomailbox.net (s2.neomailbox.net [5.148.176.60]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 7F07D80562 for ; Wed, 12 Jan 2022 22:05:42 +0100 (CET) From: Marek Lindner To: b.a.t.m.a.n@lists.open-mesh.org Subject: [PATCH 2/3] alfred: Allow operating without any interface specified Date: Wed, 12 Jan 2022 22:05:05 +0100 Message-Id: <20220112210506.3488775-2-mareklindner@neomailbox.ch> In-Reply-To: <20220112210506.3488775-1-mareklindner@neomailbox.ch> References: <10410848.OOsao9LFFs@rousseau> <20220112210506.3488775-1-mareklindner@neomailbox.ch> MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=none; spf=pass (diktynna.open-mesh.org: domain of mareklindner@neomailbox.ch designates 5.148.176.60 as permitted sender) smtp.mailfrom=mareklindner@neomailbox.ch; dmarc=none ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1642021542; a=rsa-sha256; cv=none; b=j6jbYBd0q2JVxfjqvsAbdUgXc0l+L4dFwIiMGIRxam1VkCTsqnRBTrJiCm/4bCB8NHemT0 ETr1Nt6fPdWjXFYeKWNr6zaoI5GUH/OEVKBY2iFEvc6eNObNTc9nxIcH5lRFd+akmhbShQ dYmt3Uf/hga0wgTgB2S88foypVrWX9E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1642021542; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kax3UjxfXyvuG/4MWxIYWGox7TTgOE2rcU322YpVuIw=; b=1/8r6pNfvg63RGwiN5TWBZp+7z3eVzcmK5y+XlGTlbUh4kbsur2ZyUPNxJRd2cqSHOicD5 Cg7lU3M2PsSstLTQGmss7MS1vJtd2MGqC10XcX0OW4euLcEvFD83OTB4KOj+SaB+F6N+f/ Y4dTO3cpNR5deF6EchufZtlHdVQUSQU= Message-ID-Hash: D2WMJ32B22BTKGD5MHHF2IPJRKSX3KLD X-Message-ID-Hash: D2WMJ32B22BTKGD5MHHF2IPJRKSX3KLD X-MailFrom: mareklindner@neomailbox.ch X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Marek Lindner X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: The '-i' commandline parameter to specify interface names no longer is mandatory. Specifying interface 'none' or sending a 'none' interface string within the ALFRED_CHANGE_INTERFACE unix socket command disables all interfaces operations at runtime. Signed-off-by: Marek Lindner --- README.rst | 6 +++++- alfred.h | 2 +- client.c | 8 ++++---- main.c | 6 +++--- man/alfred.8 | 6 +++++- netsock.c | 4 ++++ server.c | 39 ++++++++++++++++++++++++--------------- 7 files changed, 46 insertions(+), 25 deletions(-) diff --git a/README.rst b/README.rst index 33200e4..7f44db6 100644 --- a/README.rst +++ b/README.rst @@ -82,7 +82,8 @@ documentation how to configure alfred in this case. In any event, you can still run alfred from the command line. The relevant options are (for a full list of options, run alfred -h): - -i, --interface specify the interface to listen on + -i, --interface specify the interface to listen on. use 'none' + to disable interface operations -b specify the batman-adv interface configured on the system (default: bat0). use 'none' to disable the batman-adv based best server selection @@ -90,6 +91,9 @@ list of options, run alfred -h): accepts data from secondaries and syncs it with other primaries +The interface option '-i' is optional. If interface 'none' is specified, the +alfred daemon will not communicate with other alfred instances on the +network unless the interface list is modified at runtime via the unix socket. The -b option is optional, and only needed if you run alfred on a batman-adv interface not called bat0, or if you don't use batman-adv at all (use '-b none'). In this case, alfred will still work but will not be able to diff --git a/alfred.h b/alfred.h index c595b06..9ab92a2 100644 --- a/alfred.h +++ b/alfred.h @@ -112,7 +112,7 @@ struct interface { struct globals { struct list_head interfaces; - char *change_interface; + char *net_iface; struct server *best_server; /* NULL if we are a server ourselves */ char *mesh_iface; enum opmode opmode; diff --git a/client.c b/client.c index d0d19fb..b5d8943 100644 --- a/client.c +++ b/client.c @@ -252,7 +252,7 @@ int alfred_client_change_interface(struct globals *globals) if (unix_sock_open_client(globals)) return -1; - interface_len = strlen(globals->change_interface); + interface_len = strlen(globals->net_iface); if (interface_len > sizeof(change_interface.ifaces)) { fprintf(stderr, "%s: interface name list too long, not changing\n", __func__); @@ -264,15 +264,15 @@ int alfred_client_change_interface(struct globals *globals) change_interface.header.type = ALFRED_CHANGE_INTERFACE; change_interface.header.version = ALFRED_VERSION; change_interface.header.length = FIXED_TLV_LEN(change_interface); - strncpy(change_interface.ifaces, globals->change_interface, + strncpy(change_interface.ifaces, globals->net_iface, sizeof(change_interface.ifaces)); change_interface.ifaces[sizeof(change_interface.ifaces) - 1] = '\0'; /* test it before sending - * globals->change_interface is now saved in change_interface.ifaces + * globals->net_iface is now saved in change_interface.ifaces * and can be modified by strtok_r */ - input = globals->change_interface; + input = globals->net_iface; while ((token = strtok_r(input, ",", &saveptr))) { input = NULL; diff --git a/main.c b/main.c index 2cb6d44..d40a0cc 100644 --- a/main.c +++ b/main.c @@ -179,7 +179,7 @@ static struct globals *alfred_init(int argc, char *argv[]) memset(globals, 0, sizeof(*globals)); INIT_LIST_HEAD(&globals->interfaces); - globals->change_interface = NULL; + globals->net_iface = NULL; globals->opmode = OPMODE_SECONDARY; globals->clientmode = CLIENT_NONE; globals->best_server = NULL; @@ -224,7 +224,7 @@ static struct globals *alfred_init(int argc, char *argv[]) globals->opmode = OPMODE_PRIMARY; break; case 'i': - netsock_set_interfaces(globals, optarg); + globals->net_iface = strdup(optarg); break; case 'b': globals->mesh_iface = strdup(optarg); @@ -252,7 +252,7 @@ static struct globals *alfred_init(int argc, char *argv[]) break; case 'I': globals->clientmode = CLIENT_CHANGE_INTERFACE; - globals->change_interface = strdup(optarg); + globals->net_iface = strdup(optarg); break; case 'B': globals->clientmode = CLIENT_CHANGE_BAT_IFACE; diff --git a/man/alfred.8 b/man/alfred.8 index 4e002f0..74814e0 100644 --- a/man/alfred.8 +++ b/man/alfred.8 @@ -98,12 +98,16 @@ Change the alfred server to use the new \fBbatman-adv interface\fP .SH SERVER OPTIONS .TP \fB\-i\fP, \fB\-\-interface\fP \fIiface\fP -Specify the interface (or comma separated list of interfaces) to listen on +Specify the interface (or comma separated list of interfaces) to listen on. +Use 'none' to disable interface operations. .TP \fB\-b\fP \fIbatmanif\fP Specify the batman-adv interface configured on the system (default: bat0). Use 'none' to disable the batman-adv based best server selection. +The interface option \fB\-i\fP is optional. If interface 'none' is specified, the +alfred daemon will not communicate with other alfred instances on the +network unless the interface list is modified at runtime via the unix socket. The \fB\-b\fP option is optional, and only needed if you run alfred on a batman-adv interface not called bat0, or if you don't use batman-adv at all (use '\fB\-b\fP none'). In this case, alfred will still work but will not be diff --git a/netsock.c b/netsock.c index 84b0ec3..128e768 100644 --- a/netsock.c +++ b/netsock.c @@ -116,6 +116,10 @@ int netsock_set_interfaces(struct globals *globals, char *interfaces) netsock_close_all(globals); + /* interface 'none' disables all interface operations */ + if (is_iface_disabled(interfaces)) + return 0; + input = interfaces; while ((token = strtok_r(input, ",", &saveptr))) { input = NULL; diff --git a/server.c b/server.c index 1efc211..bfc37bc 100644 --- a/server.c +++ b/server.c @@ -380,9 +380,30 @@ int alfred_server(struct globals *globals) if (unix_sock_open_daemon(globals)) return -1; - if (list_empty(&globals->interfaces)) { - fprintf(stderr, "Can't start server: interface missing\n"); - return -1; + if (!is_iface_disabled(globals->net_iface)) { + if (!globals->net_iface) { + fprintf(stderr, "Can't start server: interface missing\n"); + return -1; + } + + netsock_set_interfaces(globals, globals->net_iface); + + if (list_empty(&globals->interfaces) && !globals->force) { + fprintf(stderr, "Can't start server: valid interface missing\n"); + return -1; + } + + num_socks = netsock_open_all(globals); + if (num_socks <= 0 && !globals->force) { + fprintf(stderr, "Failed to open interfaces\n"); + return -1; + } + + num_interfaces = netsocket_count_interfaces(globals); + if (num_interfaces > 1 && globals->opmode == OPMODE_SECONDARY) { + fprintf(stderr, "More than one interface specified in secondary mode\n"); + return -1; + } } if (!is_iface_disabled(globals->mesh_iface) && @@ -393,18 +414,6 @@ int alfred_server(struct globals *globals) return -1; } - num_socks = netsock_open_all(globals); - if (num_socks <= 0 && !globals->force) { - fprintf(stderr, "Failed to open interfaces\n"); - return -1; - } - - num_interfaces = netsocket_count_interfaces(globals); - if (num_interfaces > 1 && globals->opmode == OPMODE_SECONDARY) { - fprintf(stderr, "More than one interface specified in secondary mode\n"); - return -1; - } - clock_gettime(CLOCK_MONOTONIC, &last_check); globals->if_check = last_check; From patchwork Wed Jan 12 21:05:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Lindner X-Patchwork-Id: 18451 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 5E0A883E57; Wed, 12 Jan 2022 22:06:01 +0100 (CET) Received: from s2.neomailbox.net (s2.neomailbox.net [5.148.176.60]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 7143483E46 for ; Wed, 12 Jan 2022 22:05:58 +0100 (CET) From: Marek Lindner To: b.a.t.m.a.n@lists.open-mesh.org Subject: [PATCH 3/3] alfred: properly initialize stack buffer before sending over unix socket Date: Wed, 12 Jan 2022 22:05:06 +0100 Message-Id: <20220112210506.3488775-3-mareklindner@neomailbox.ch> In-Reply-To: <20220112210506.3488775-1-mareklindner@neomailbox.ch> References: <10410848.OOsao9LFFs@rousseau> <20220112210506.3488775-1-mareklindner@neomailbox.ch> MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=none; spf=pass (diktynna.open-mesh.org: domain of mareklindner@neomailbox.ch designates 5.148.176.60 as permitted sender) smtp.mailfrom=mareklindner@neomailbox.ch; dmarc=none ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1642021558; a=rsa-sha256; cv=none; b=qcsDfe7U65/mEbFVwPQzrJeQHFiadyae7GP8dyYK2eygh+6ETNFDOqsR67K8U8h0I9jE4C 2Ou9SpigD/q5KHd0139VYOGbLW7a1HtlHQa/638dbSspPM0mgOlgeWgw7vuaF+4lS8fUaA UwdmHwFC/OlhYjpbzNToYGQi7pOWQAs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1642021558; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NVI4cMzS+FQAMGCh0y9pZ/Cu8V2R+vchBJrn/PpDH5A=; b=YGm1UoJy5A1soOl3zTwkJoKYb+3UFXNrVUopix4D1YKpCXWnZkNK+skharXk1giPfYpDKl JG3TJlguDkaFnHDr2exKv/Wo7NwrIu9ynpqO4zmy7CnEpqYS4mEnm1iUSjNMyaIQSnwxYv vnfoVCUss82rVPs5BYFPnlmCfr0ZnRc= Message-ID-Hash: KAQQACDKPGCW3TNG7PAYE52EAGQJZA2A X-Message-ID-Hash: KAQQACDKPGCW3TNG7PAYE52EAGQJZA2A X-MailFrom: mareklindner@neomailbox.ch X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Marek Lindner X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Without explicitely initializing the buffer with null bytes, the stack variables may contain process information which may be leaked when transmitted via unix socket. Also, the size of the variables sitting on the stack can be reduced. Signed-off-by: Marek Lindner --- client.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client.c b/client.c index b5d8943..cf15ff4 100644 --- a/client.c +++ b/client.c @@ -35,6 +35,7 @@ int alfred_client_request_data(struct globals *globals) return -1; len = sizeof(request); + memset(&request, 0, len); request.header.type = ALFRED_REQUEST; request.header.version = ALFRED_VERSION; @@ -184,6 +185,7 @@ int alfred_client_modeswitch(struct globals *globals) return -1; len = sizeof(modeswitch); + memset(&modeswitch, 0, len); modeswitch.header.type = ALFRED_MODESWITCH; modeswitch.header.version = ALFRED_VERSION; @@ -260,6 +262,7 @@ int alfred_client_change_interface(struct globals *globals) } len = sizeof(change_interface); + memset(&change_interface, 0, len); change_interface.header.type = ALFRED_CHANGE_INTERFACE; change_interface.header.version = ALFRED_VERSION; @@ -308,6 +311,7 @@ int alfred_client_change_bat_iface(struct globals *globals) } len = sizeof(change_bat_iface); + memset(&change_bat_iface, 0, len); change_bat_iface.header.type = ALFRED_CHANGE_BAT_IFACE; change_bat_iface.header.version = ALFRED_VERSION;