From patchwork Fri Jul 1 13:32:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 12903301 Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2728F1871 for ; Fri, 1 Jul 2022 13:32:46 +0000 (UTC) Received: by mail-lj1-f181.google.com with SMTP id l7so1912630ljj.4 for ; Fri, 01 Jul 2022 06:32:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=4yhvNPxQbStgkKWfMv2II9x6lR9oJSRsLjXfA0v/NJM=; b=bTaKFNJMCJd6YnFKgV1cUis9ZCHo1c0RtrD7qWY9LTkcHAcoNDQog5OloJNyex831g EuYIFjzM1CMwYy9FOLyxOyOk/8nmhDwu6coOWQH7wIDIoGjT4DAW4z7Vqe8eZI/KBmo7 x0IY4/nuMHCImhnmDkLalsbrfosnXeE9IPoP3KuPfO6xKSM12M+0k5/ten9tTnMQJpHD H7ZkQ6MnTongYs4wpowaJ1Cdo/8l24JBFvoU34aFNNAbXQhnCLpXCo1ul0tVVt30a+ZR vY6K6n0CRP1I0GXUC9DbZf3FiuszYpB56Bdh1OwDSoQsV1QyjWfWg3QuWDMPW+rb3H5D 7K6A== X-Gm-Message-State: AJIora8cEijavX678VctejOHtcVQtScFVsT/cX9FbJWLzJT5RYoOzp/w tiVM2kuIIB/hqwR05XfWY67OC6yRoUn2rYmp X-Google-Smtp-Source: AGRyM1u02kA5FIwKEzrQsaHwGByh7MPcFYmn+to+1oTtW7iY51t7QieMbB6HnPgczT7P02LEoSID/g== X-Received: by 2002:a2e:a54a:0:b0:25a:6609:835a with SMTP id e10-20020a2ea54a000000b0025a6609835amr8225527ljn.408.1656682363797; Fri, 01 Jul 2022 06:32:43 -0700 (PDT) Received: from iss.home (79.184.238.213.ipv4.supernova.orange.pl. [79.184.238.213]) by smtp.gmail.com with ESMTPSA id 11-20020a05651c128b00b0025a725af81csm3185815ljc.39.2022.07.01.06.32.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 06:32:43 -0700 (PDT) From: Andrew Zaborowski To: ell@lists.linux.dev Subject: [PATCH 1/4] dhcp: Set lease->bound_time before emitting event Date: Fri, 1 Jul 2022 15:32:34 +0200 Message-Id: <20220701133237.2887854-1-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The event handler for L_DHCP_CLIENT_EVENT_LEASE_OBTAINED/RENEWED will want to use the new lease->bound_time value for expiry time calculation to we need to set it before we emit the events in the DHCP ACK handler. --- ell/dhcp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ell/dhcp.c b/ell/dhcp.c index f9c0826..afd5c34 100644 --- a/ell/dhcp.c +++ b/ell/dhcp.c @@ -898,6 +898,7 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata, CLIENT_ENTER_STATE(DHCP_STATE_BOUND); l_timeout_remove(client->timeout_resend); client->timeout_resend = NULL; + client->lease->bound_time = timestamp; if (client->transport->bind) { e = client->transport->bind(client->transport, @@ -910,8 +911,6 @@ static void dhcp_client_rx_message(const void *data, size_t len, void *userdata, dhcp_client_event_notify(client, r); - client->lease->bound_time = timestamp; - /* * Start T1, once it expires we will start the T2 timer. If * we renew the lease, we will end up back here. From patchwork Fri Jul 1 13:32:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 12903302 Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E78BE1C33 for ; Fri, 1 Jul 2022 13:32:46 +0000 (UTC) Received: by mail-lf1-f48.google.com with SMTP id y16so3879505lfb.9 for ; Fri, 01 Jul 2022 06:32:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FrdumpKgXPp2r6swPM7a1qQnXusW8LjchYX/eWC+yf8=; b=7BsOt+gTV/N54lTOZFOvHLkhjHx+/h7BG3RMQ8hUkaURGcdBWpvfgzkVpdzst9Jb0O sHNyRG1XvIpMXxl655fX7oECDcW3Fg4WCFF7WaWFs5Ii1MuHpMcRJfbJEfvWsE3gDWCy pKPqsRRWzWDyK4b0+aXBwpQqHGJMy/0iB582iHE+630MapwG7tw2kZwtI+wnX94VUhTz vixPJHwKmkivytoEOKsg+peCmaEjN5Ot32wRrpvVpntd8iXByP6geW1Hl8JAGmD5OXJY l8yW5mLz0bYygB0LCGebQa/mmvmsVZcOioTh3Bb3NCzCWO2JYAgsHlTalEFMEX5o9+rn hg7w== X-Gm-Message-State: AJIora+XutMFhl7a/Mzy9qK7dqUee33UNKa49UML93aev2fq3BZsQYlU h1Ngwk+RZ/Qm4BBu+tp7wIYce3382mp1wVS2 X-Google-Smtp-Source: AGRyM1to2GmVQXDryzjRa0jVxX8uwtxLI7cImQonYOSEnkF6BCyBxcawpuK9DWPDzSVr5MrNvnRAWQ== X-Received: by 2002:a05:6512:3e05:b0:481:2bf2:db06 with SMTP id i5-20020a0565123e0500b004812bf2db06mr8916238lfv.408.1656682364696; Fri, 01 Jul 2022 06:32:44 -0700 (PDT) Received: from iss.home (79.184.238.213.ipv4.supernova.orange.pl. [79.184.238.213]) by smtp.gmail.com with ESMTPSA id 11-20020a05651c128b00b0025a725af81csm3185815ljc.39.2022.07.01.06.32.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 06:32:44 -0700 (PDT) From: Andrew Zaborowski To: ell@lists.linux.dev Subject: [PATCH 2/4] useful: Add a cleanup handler for fd variables Date: Fri, 1 Jul 2022 15:32:35 +0200 Message-Id: <20220701133237.2887854-2-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220701133237.2887854-1-andrew.zaborowski@intel.com> References: <20220701133237.2887854-1-andrew.zaborowski@intel.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Allow declaring an fd variable with a simple _auto_(close) int fd = -1; to be able to skip the close() call. This is useful for very simple file accesses where it's not worth using l_io with l_io_set_close_on_destroy. As an example, update netconfig_proc_write_ipv6_setting to use _auto_(close) where it helps in returning the errno from the read() call (on error) which would otherwise be clobbered by the close() call. --- ell/netconfig.c | 5 ++--- ell/useful.h | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ell/netconfig.c b/ell/netconfig.c index d4487bd..19aaf56 100644 --- a/ell/netconfig.c +++ b/ell/netconfig.c @@ -968,7 +968,7 @@ static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc, { char ifname[IF_NAMESIZE]; _auto_(l_free) char *filename = NULL; - int fd; + _auto_(close) int fd = -1; int r; if (unlikely(!if_indextoname(nc->ifindex, ifname))) @@ -982,8 +982,7 @@ static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc, return -errno; r = L_TFR(write(fd, value, strlen(value))); - L_TFR(close(fd)); - return r; + return r > 0 ? 0 : -errno; } LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) diff --git a/ell/useful.h b/ell/useful.h index 4c8b23e..791fa20 100644 --- a/ell/useful.h +++ b/ell/useful.h @@ -20,6 +20,11 @@ * */ +#include +#include + +#include + #define align_len(len, boundary) (((len)+(boundary)-1) & ~((boundary)-1)) #define likely(x) __builtin_expect(!!(x), 1) @@ -65,6 +70,15 @@ static inline unsigned char bit_field(const unsigned char oct, #define _auto_(func) \ __AUTODESTRUCT(func) +/* Enables declaring _auto_(close) int fd = <-1 or L_TFR(open(...))>; */ +inline __attribute__((always_inline)) void close_cleanup(void *p) +{ + int fd = *(int *) p; + + if (fd >= 0) + L_TFR(close(fd)); +} + /* * Trick the compiler into thinking that var might be changed somehow by * the asm From patchwork Fri Jul 1 13:32:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 12903303 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E60D11871 for ; Fri, 1 Jul 2022 13:32:47 +0000 (UTC) Received: by mail-lf1-f44.google.com with SMTP id e12so3903781lfr.6 for ; Fri, 01 Jul 2022 06:32:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/hLvYs7sEwJvI3VTTxUUHhC+s9Axkwyt9+JhIeFsxbo=; b=qaHsuIbE9S2tYPLw2GgIDnbhEvhADPGUr/9cbskat8xNLbKB6VtmwxYFhXKaWutKl9 Zn82Wn9dloPvguRI8zXmAlDzIsOCAfZQWDx7JT0CqSFAXMc3Jjq07xmXTTy4jkc3aTW+ O7WT+6YiAvlr0rk82ZX8Di+DV2VNv/4UFHMn7S6AOSbIM6IYyagNNGuDUQGqdRrOXHt2 eBUnFJeKij0cdawwjTB3/6wk/mzJYiDXUCHgyGlMWoY0ZO5/6wQpbTxXUrValBTP3xYC yXyfX44wcfRFQ0B55yqypwdp8WJbAww9D0kJmHmAkSsrcmD8+i7p4jNR0c3E7mupyh+y Ti2A== X-Gm-Message-State: AJIora8QHHrfjaw7U5qWkjG0WtbcFT8Zy7I+yL1dI4Z8DuJcC/Xd5fJU waD2kAx56pBU5QFl2KxyHkNkdCEc+Vsb7C7x X-Google-Smtp-Source: AGRyM1vvAZPufe61tyqAmkaQkrQ0kxnUJuMDg6Tc2J0syo88MJubaXkVgeFeYoDSncz22exiXNfFVA== X-Received: by 2002:a05:6512:31cc:b0:47f:63ca:bfc8 with SMTP id j12-20020a05651231cc00b0047f63cabfc8mr8792578lfe.242.1656682365554; Fri, 01 Jul 2022 06:32:45 -0700 (PDT) Received: from iss.home (79.184.238.213.ipv4.supernova.orange.pl. [79.184.238.213]) by smtp.gmail.com with ESMTPSA id 11-20020a05651c128b00b0025a725af81csm3185815ljc.39.2022.07.01.06.32.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 06:32:44 -0700 (PDT) From: Andrew Zaborowski To: ell@lists.linux.dev Subject: [PATCH 3/4] netconfig: Restore net.ipv6.conf...disable_ipv6 on stop Date: Fri, 1 Jul 2022 15:32:36 +0200 Message-Id: <20220701133237.2887854-3-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220701133237.2887854-1-andrew.zaborowski@intel.com> References: <20220701133237.2887854-1-andrew.zaborowski@intel.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since we write the net.ipv6.conf..disable_ipv6 option to 0 when enabling DHCPv6, reset it to its original value when stopping netconfig. --- ell/netconfig.c | 69 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/ell/netconfig.c b/ell/netconfig.c index 19aaf56..f48018b 100644 --- a/ell/netconfig.c +++ b/ell/netconfig.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include "private.h" #include "useful.h" @@ -83,6 +85,7 @@ struct l_netconfig { unsigned int ifaddr6_dump_cmd_id; struct l_queue *icmp_route_data; struct l_acd *acd; + unsigned int orig_disable_ipv6; /* These objects, if not NULL, are owned by @addresses and @routes */ struct l_rtnl_address *v4_address; @@ -962,14 +965,15 @@ static void netconfig_icmp6_event_handler(struct l_icmp6_client *client, netconfig_emit_event(nc, AF_INET6, L_NETCONFIG_EVENT_UPDATE); } -static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc, - const char *setting, - const char *value) +static int netconfig_proc_write_ipv6_uint_setting(struct l_netconfig *nc, + const char *setting, + unsigned int value) { char ifname[IF_NAMESIZE]; _auto_(l_free) char *filename = NULL; _auto_(close) int fd = -1; int r; + char valuestr[20]; if (unlikely(!if_indextoname(nc->ifindex, ifname))) return -errno; @@ -981,10 +985,46 @@ static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc, if (unlikely(fd < 0)) return -errno; - r = L_TFR(write(fd, value, strlen(value))); + snprintf(valuestr, sizeof(valuestr), "%u", value); + r = L_TFR(write(fd, valuestr, strlen(valuestr))); return r > 0 ? 0 : -errno; } +static long netconfig_proc_read_ipv6_uint_setting(struct l_netconfig *nc, + const char *setting) +{ + char ifname[IF_NAMESIZE]; + _auto_(l_free) char *filename = NULL; + _auto_(close) int fd = -1; + int r; + char valuestr[20]; + long value; + char *endp; + + if (unlikely(!if_indextoname(nc->ifindex, ifname))) + return -errno; + + filename = l_strdup_printf("/proc/sys/net/ipv6/conf/%s/%s", + ifname, setting); + + fd = L_TFR(open(filename, O_RDONLY)); + if (unlikely(fd < 0)) + return -errno; + + r = L_TFR(read(fd, valuestr, sizeof(valuestr) - 1)); + if (unlikely(r < 1)) + return r == 0 ? -EINVAL : -errno; + + valuestr[r - 1] = '\0'; + errno = 0; + value = strtoul(valuestr, &endp, 10); + + if (unlikely(errno || !L_IN_SET(*endp, '\n', '\0'))) + return -EINVAL; + + return value; +} + LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) { struct l_netconfig *nc; @@ -1019,7 +1059,7 @@ LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex) nc, NULL); /* Disable in-kernel autoconfiguration for the interface */ - netconfig_proc_write_ipv6_setting(nc, "accept_ra", "0"); + netconfig_proc_write_ipv6_uint_setting(nc, "accept_ra", 0); return nc; } @@ -1554,13 +1594,15 @@ static void netconfig_ifaddr_ipv6_dump_done_cb(void *user_data) nc->ifaddr6_dump_cmd_id = 0; /* "do not generate a link-local address" */ - netconfig_proc_write_ipv6_setting(nc, "addr_gen_mode", "1"); + netconfig_proc_write_ipv6_uint_setting(nc, "addr_gen_mode", 1); /* "generate address based on EUI64 (default)" */ - netconfig_proc_write_ipv6_setting(nc, "addr_gen_mode", "0"); - /* "enable IPv6 operation" */ - netconfig_proc_write_ipv6_setting(nc, "disable_ipv6", "0"); + netconfig_proc_write_ipv6_uint_setting(nc, "addr_gen_mode", 0); - /* TODO: save original values and reset in l_netconfig_stop() */ + /* "enable IPv6 operation" */ + nc->orig_disable_ipv6 = + netconfig_proc_read_ipv6_uint_setting(nc, "disable_ipv6"); + if (nc->orig_disable_ipv6) + netconfig_proc_write_ipv6_uint_setting(nc, "disable_ipv6", 0); } LIB_EXPORT bool l_netconfig_start(struct l_netconfig *netconfig) @@ -1680,6 +1722,13 @@ LIB_EXPORT void l_netconfig_stop(struct l_netconfig *netconfig) l_dhcp6_client_stop(netconfig->dhcp6_client); l_acd_destroy(l_steal_ptr(netconfig->acd)); + + if (netconfig->orig_disable_ipv6) { + netconfig_proc_write_ipv6_uint_setting(netconfig, + "disable_ipv6", + netconfig->orig_disable_ipv6); + netconfig->orig_disable_ipv6 = 0; + } } LIB_EXPORT struct l_dhcp_client *l_netconfig_get_dhcp_client( From patchwork Fri Jul 1 13:32:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Zaborowski X-Patchwork-Id: 12903304 Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC6341C33 for ; Fri, 1 Jul 2022 13:32:48 +0000 (UTC) Received: by mail-lf1-f51.google.com with SMTP id t19so3274201lfl.5 for ; Fri, 01 Jul 2022 06:32:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5WaiZ6WdenVYaLa/HU1wDrnAQDLNZons6UkKeVPzK3I=; b=KLtaC442PhdcciQfzi2r66LAK+s35wHYCyBh3YPXpjwN0R6Hc7L+B0DEjl4TOYAi62 5U/DBDOcnU+IlWflcu4RwZDCoqyCvyMxc1OmcUL80GX3bsIX6Al/ufavIV2x+sPNJexA B3z5cHxPXKqon7x4P9ei7JcV8t9A9bU1wXjUvoSdMHDIp1cUCS7bd+59QpgIvSVvdft3 stGZRGSb6CJSuYv7bK8TJ7AZQWH6nMCrla5Y0ShxGcvPxJKsx+/UO+dvuk6yiG1QG1Hi lOSZFB4gR4I8bOhkQQCxywYkAvw+oofT36Rb0yFB6ivlevZp/Zse+sK2hGqbPjTsRQ8S /TDA== X-Gm-Message-State: AJIora/gWnX8oMfGh5G0nWkKsCdLTe77vD53CXZZBGlCtjH5IJOifw28 IrpwYhxi9hnCtQXFWqBlMPmTRm1fYO2Hblrd X-Google-Smtp-Source: AGRyM1t8RCd0YNWqm2e6Rc1hstRFJ9SxLDgRtZdqXX0SMPtoteLkUL4UVIgYnH5AO2US7005y6cPnA== X-Received: by 2002:a05:6512:3e19:b0:47f:77d8:236c with SMTP id i25-20020a0565123e1900b0047f77d8236cmr8990258lfv.560.1656682366526; Fri, 01 Jul 2022 06:32:46 -0700 (PDT) Received: from iss.home (79.184.238.213.ipv4.supernova.orange.pl. [79.184.238.213]) by smtp.gmail.com with ESMTPSA id 11-20020a05651c128b00b0025a725af81csm3185815ljc.39.2022.07.01.06.32.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jul 2022 06:32:45 -0700 (PDT) From: Andrew Zaborowski To: ell@lists.linux.dev Subject: [PATCH 4/4] netconfig: Fix leaking domain name string Date: Fri, 1 Jul 2022 15:32:37 +0200 Message-Id: <20220701133237.2887854-4-andrew.zaborowski@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220701133237.2887854-1-andrew.zaborowski@intel.com> References: <20220701133237.2887854-1-andrew.zaborowski@intel.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In l_netconfig_get_domain_names we had two l_dhcp_lease_get_domain_name() calls and only saved one of the values. l_dhcp_lease_get_domain_name() internally does an strdup so we own the returned string. Reported by Coverity Scan as CID 379209. --- ell/netconfig.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ell/netconfig.c b/ell/netconfig.c index f48018b..28afa9b 100644 --- a/ell/netconfig.c +++ b/ell/netconfig.c @@ -1908,15 +1908,16 @@ LIB_EXPORT char **l_netconfig_get_domain_names(struct l_netconfig *netconfig) char **ret = NULL; const struct l_dhcp_lease *v4_lease; const struct l_dhcp6_lease *v6_lease; + char *dn; if (netconfig->v4_domain_names_override) netconfig_strv_cat(&ret, netconfig->v4_domain_names_override, false); else if ((v4_lease = l_dhcp_client_get_lease(netconfig->dhcp_client)) && - l_dhcp_lease_get_domain_name(v4_lease)) { + (dn = l_dhcp_lease_get_domain_name(v4_lease))) { ret = l_new(char *, 2); - ret[0] = l_dhcp_lease_get_domain_name(v4_lease); + ret[0] = dn; } if (netconfig->v6_dns_override)