($INBOX_DIR/description missing)
 help / color / mirror / Atom feed
From: Andrew Zaborowski <andrew.zaborowski at intel.com>
To: ell at lists.01.org
Subject: [PATCH 13/17] netconfig: Write gateway MAC from DHCP to ARP cache
Date: Sat, 14 May 2022 00:47:09 +0200	[thread overview]
Message-ID: <20220513224713.1447773-13-andrew.zaborowski@intel.com> (raw)
In-Reply-To: 20220513224713.1447773-1-andrew.zaborowski@intel.com

[-- Attachment #1: Type: text/plain, Size: 2200 bytes --]

Write the MAC directly to the ARP table to save an ARP query for the
gateway's MAC when it is known from the freshly obtained lease.  For
practical reasons do this outside of l_netconfig_apply_rtnl().
---
 ell/netconfig.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/ell/netconfig.c b/ell/netconfig.c
index bdd67cc..7745d36 100644
--- a/ell/netconfig.c
+++ b/ell/netconfig.c
@@ -25,6 +25,7 @@
 #endif
 
 #include <linux/types.h>
+#include <linux/if_ether.h>
 #include <netinet/ip.h>
 #include <arpa/inet.h>
 #include <netinet/icmp6.h>
@@ -406,6 +407,37 @@ static void netconfig_remove_dhcp_address_routes(struct l_netconfig *nc,
 	}
 }
 
+static void netconfig_set_neighbor_entry_cb(int error,
+						uint16_t type, const void *data,
+						uint32_t len, void *user_data)
+{
+	/* Not critical.  TODO: log warning */
+}
+
+static int netconfig_dhcp_gateway_to_arp(struct l_netconfig *nc)
+{
+	const struct l_dhcp_lease *lease =
+		l_dhcp_client_get_lease(nc->dhcp_client);
+	_auto_(l_free) char *server_id = l_dhcp_lease_get_server_id(lease);
+	_auto_(l_free) char *gw = l_dhcp_lease_get_gateway(lease);
+	const uint8_t *server_mac = l_dhcp_lease_get_server_mac(lease);
+	struct in_addr in_gw;
+
+	if (!gw || strcmp(server_id, gw) || !server_mac)
+		return -ENOENT;
+
+	/* Gateway MAC is known, write it into ARP cache to save ARP traffic */
+	in_gw.s_addr = l_dhcp_lease_get_gateway_u32(lease);
+
+	if (!l_rtnl_neighbor_set_hwaddr(rtnl, nc->ifindex, AF_INET, &in_gw,
+					server_mac, ETH_ALEN,
+					netconfig_set_neighbor_entry_cb, nc,
+					NULL))
+		return -EIO;
+
+	return 0;
+}
+
 static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 						enum l_dhcp_client_event event,
 						void *user_data)
@@ -430,6 +462,7 @@ static void netconfig_dhcp_event_handler(struct l_dhcp_client *client,
 		netconfig_set_dhcp_lifetimes(nc, false);
 		nc->v4_configured = true;
 		netconfig_emit_event(nc, AF_INET, L_NETCONFIG_EVENT_CONFIGURE);
+		netconfig_dhcp_gateway_to_arp(nc);
 		break;
 	case L_DHCP_CLIENT_EVENT_LEASE_RENEWED:
 		if (L_WARN_ON(!nc->v4_configured))
-- 
2.32.0

             reply	other threads:[~2022-05-13 22:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-13 22:47 Andrew Zaborowski [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-05-13 14:55 [PATCH 13/17] netconfig: Write gateway MAC from DHCP to ARP cache Andrew Zaborowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220513224713.1447773-13-andrew.zaborowski@intel.com \
    --to=ell@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).