All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] netconfig: Write gateway MAC from DHCP to ARP cache
@ 2022-05-11 22:43 Andrew Zaborowski
  0 siblings, 0 replies; only message in thread
From: Andrew Zaborowski @ 2022-05-11 22:43 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 2257 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().
---
This applies on top of the earlier netconfig patches.

 ell/netconfig.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/ell/netconfig.c b/ell/netconfig.c
index ad41c9a..d495da2 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>
@@ -360,6 +361,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)
@@ -384,6 +416,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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-11 22:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-11 22:43 [PATCH 1/2] netconfig: Write gateway MAC from DHCP to ARP cache Andrew Zaborowski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.