From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202410 header.b=JP5gzfWl; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 229E15A0621 for ; Wed, 13 Nov 2024 04:37:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1731469038; bh=oQMwxWHDuojHWe6v68X0brLuADs7tlCB0AarGtPYwHg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JP5gzfWluvQjl07uM2uBG+D0pbDrU0uUGOYiHOWVZFZ5m94P06uV+9aE+qscXg8QS RX99QhkTwGCSAfdY4tAyY0Lrh2OV3AubwcaOqTWRdvZJ0HfaxObso8FOcH2M9SfjW4 JXLCia2BkgFlDXPmqntQ8CekbnTLSnzA1UFn+2p+WHM1rcv0sbk2rSYpQJ3vXTDt3H GS2TRApK4sVqHMhVxHij8ipaQMg27JGjQMtFObVC3qRS191tuV8IOr3VD8I5/jOIOv xcwEAkRet0eZ5V6TS4P1bAxR9i8ZCUE1bG/j0dFQEDwk6ZS6G0qM0HiGH4cvCukVWZ OGMHIKuWT3DKA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Xp89V0BR8z4x8C; Wed, 13 Nov 2024 14:37:18 +1100 (AEDT) Date: Wed, 13 Nov 2024 13:07:04 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 3/6] ndp: Split out helpers for sending specific NDP message types Message-ID: References: <20241112040618.1804081-1-david@gibson.dropbear.id.au> <20241112040618.1804081-4-david@gibson.dropbear.id.au> <20241113021412.2e678d26@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="WslfWYhBs89upAeU" Content-Disposition: inline In-Reply-To: <20241113021412.2e678d26@elisabeth> Message-ID-Hash: GKBAPCWBHVF3OFOZKB3K46D5AOSYHKF4 X-Message-ID-Hash: GKBAPCWBHVF3OFOZKB3K46D5AOSYHKF4 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --WslfWYhBs89upAeU Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Nov 13, 2024 at 02:14:12AM +0100, Stefano Brivio wrote: > Nits only: >=20 > On Tue, 12 Nov 2024 15:06:15 +1100 > David Gibson wrote: >=20 > > Currently the large ndp() function responds to all NDP messages we hand= le, > > both parsing the message as necessary and sending the response. Split = out > > the code to construct and send specific message types into ndp_na() (to > > send NA messages) and ndp_ra() (to send RA messages). > >=20 > > As well as breaking up an excessively large function, this is a first s= tep > > to being able to send unsolicited NDP messages. > >=20 > > While we're there, remove a slighty ugly goto. > >=20 > > Signed-off-by: David Gibson > > --- > > ndp.c | 136 +++++++++++++++++++++++++++++++++------------------------- > > 1 file changed, 78 insertions(+), 58 deletions(-) > >=20 > > diff --git a/ndp.c b/ndp.c > > index fa1b67a..e876c34 100644 > > --- a/ndp.c > > +++ b/ndp.c > > @@ -186,16 +186,13 @@ static void ndp_send(const struct ctx *c, const s= truct in6_addr *dst, > > } > > =20 > > /** > > - * ndp() - Check for NDP solicitations, reply as needed > > + * ndp_na() - Send an NDP Neighbour Advertisement (NA) message > > * @c: Execution context > > - * @ih: ICMPv6 header > > - * @saddr: Source IPv6 address > > - * @p: Packet pool > > - * > > - * Return: 0 if not handled here, 1 if handled, -1 on failure > > + * @dst: IPv6 address to send the NA to > > + * @addr: IPv6 address to advertise > > */ > > -int ndp(const struct ctx *c, const struct icmp6hdr *ih, > > - const struct in6_addr *saddr, const struct pool *p) > > +static void ndp_na(const struct ctx *c, const struct in6_addr *dst, > > + const void *addr) > > { > > struct ndp_na na =3D { > > .ih =3D { > > @@ -212,6 +209,20 @@ int ndp(const struct ctx *c, const struct icmp6hdr= *ih, > > }, > > } > > }; > > + > > + memcpy(&na.target_addr, addr, sizeof(na.target_addr)); > > + memcpy(na.target_l2_addr.mac, c->our_tap_mac, ETH_ALEN); > > + > > + ndp_send(c, dst, &na, sizeof(na)); > > +} > > + > > +/** > > + * ndp_ra() - Send an NDP Router Advertisement (RA) message > > + * @c: Execution context > > + * @dst: IPv6 address to send the RA to > > + */ > > +static void ndp_ra(const struct ctx *c, const struct in6_addr *dst) > > +{ > > struct ndp_ra ra =3D { > > .ih =3D { > > .icmp6_type =3D RA, > > @@ -238,58 +249,28 @@ int ndp(const struct ctx *c, const struct icmp6hd= r *ih, > > }, > > }, > > }; > > + unsigned char *ptr =3D NULL; > > =20 > > - if (ih->icmp6_type < RS || ih->icmp6_type > NA) > > - return 0; > > - > > - if (c->no_ndp) > > - return 1; > > - > > - if (ih->icmp6_type =3D=3D NS) { > > - const struct ndp_ns *ns =3D > > - packet_get(p, 0, 0, sizeof(struct ndp_ns), NULL); > > + memcpy(&ra.prefix, &c->ip6.addr, sizeof(ra.prefix)); > > =20 > > - if (!ns) > > - return -1; > > + ptr =3D &ra.var[0]; > > =20 > > - if (IN6_IS_ADDR_UNSPECIFIED(saddr)) > > - return 1; > > - > > - info("NDP: received NS, sending NA"); > > - > > - memcpy(&na.target_addr, &ns->target_addr, > > - sizeof(na.target_addr)); > > - memcpy(na.target_l2_addr.mac, c->our_tap_mac, ETH_ALEN); > > + if (c->mtu !=3D -1) { > > + struct opt_mtu *mtu =3D (struct opt_mtu *)ptr; > > + *mtu =3D (struct opt_mtu) { > > + .header =3D { > > + .type =3D OPT_MTU, > > + .len =3D 1, > > + }, > > + .value =3D htonl(c->mtu), > > + }; > > + ptr +=3D sizeof(struct opt_mtu); > > + } > > =20 > > - ndp_send(c, saddr, &na, sizeof(struct ndp_na)); > > - } else if (ih->icmp6_type =3D=3D RS) { > > - unsigned char *ptr =3D NULL; > > + if (!c->no_dhcp_dns) { > > size_t dns_s_len =3D 0; > > int i, n; > > =20 > > - if (c->no_ra) > > - return 1; > > - > > - info("NDP: received RS, sending RA"); > > - memcpy(&ra.prefix, &c->ip6.addr, sizeof(ra.prefix)); > > - > > - ptr =3D &ra.var[0]; > > - > > - if (c->mtu !=3D -1) { > > - struct opt_mtu *mtu =3D (struct opt_mtu *)ptr; > > - *mtu =3D (struct opt_mtu) { > > - .header =3D { > > - .type =3D OPT_MTU, > > - .len =3D 1, > > - }, > > - .value =3D htonl(c->mtu), > > - }; > > - ptr +=3D sizeof(struct opt_mtu); > > - } > > - > > - if (c->no_dhcp_dns) > > - goto dns_done; > > - > > for (n =3D 0; !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns[n]); n++); > > if (n) { > > struct opt_rdnss *rdnss =3D (struct opt_rdnss *)ptr; > > @@ -305,7 +286,7 @@ int ndp(const struct ctx *c, const struct icmp6hdr = *ih, > > sizeof(rdnss->dns[i])); > > } > > ptr +=3D offsetof(struct opt_rdnss, dns) + > > - i * sizeof(rdnss->dns[0]); > > + i * sizeof(rdnss->dns[0]); >=20 > Unrelated change, less readable, probably from your new editor / clangd? Curiously, no, I wasn't using Zed at the time. That just happened because I reindented the block a couple of times, and apparently my emacs indent settings aren't *quite* the same as passt standard. Anyway, fixed. > > =20 > > for (n =3D 0; *c->dns_search[n].n; n++) > > dns_s_len +=3D strlen(c->dns_search[n].n) + 2; > > @@ -329,7 +310,7 @@ int ndp(const struct ctx *c, const struct icmp6hdr = *ih, > > *(ptr++) =3D '.'; > > =20 > > len =3D sizeof(dnssl->domains) - > > - (ptr - dnssl->domains); > > + (ptr - dnssl->domains); >=20 > Same here. >=20 > > =20 > > strncpy((char *)ptr, c->dns_search[i].n, len); > > for (dot =3D (char *)ptr - 1; *dot; dot++) { > > @@ -343,11 +324,50 @@ int ndp(const struct ctx *c, const struct icmp6hd= r *ih, > > memset(ptr, 0, 8 - dns_s_len % 8); /* padding */ > > ptr +=3D 8 - dns_s_len % 8; > > } > > + } > > =20 > > -dns_done: > > - memcpy(&ra.source_ll.mac, c->our_tap_mac, ETH_ALEN); > > + memcpy(&ra.source_ll.mac, c->our_tap_mac, ETH_ALEN); > > =20 > > - ndp_send(c, saddr, &ra, ptr - (unsigned char *)&ra); > > + ndp_send(c, dst, &ra, ptr - (unsigned char *)&ra); > > +} > > + > > +/** > > + * ndp() - Check for NDP solicitations, reply as needed > > + * @c: Execution context > > + * @ih: ICMPv6 header > > + * @saddr: Source IPv6 address > > + * @p: Packet pool > > + * > > + * Return: 0 if not handled here, 1 if handled, -1 on failure > > + */ > > +int ndp(const struct ctx *c, const struct icmp6hdr *ih, > > + const struct in6_addr *saddr, const struct pool *p) > > +{ > > + if (ih->icmp6_type < RS || ih->icmp6_type > NA) > > + return 0; > > + > > + if (c->no_ndp) > > + return 1; > > + > > + if (ih->icmp6_type =3D=3D NS) { > > + const struct ndp_ns *ns =3D > > + packet_get(p, 0, 0, sizeof(struct ndp_ns), NULL); >=20 > The assignment could go on its line here? We don't save any line this > way. Good point, done. > > + > > + if (!ns) > > + return -1; > > + > > + if (IN6_IS_ADDR_UNSPECIFIED(saddr)) > > + return 1; > > + > > + info("NDP: received NS, sending NA"); > > + > > + ndp_na(c, saddr, &ns->target_addr); > > + } else if (ih->icmp6_type =3D=3D RS) { > > + if (c->no_ra) > > + return 1; > > + > > + info("NDP: received RS, sending RA"); > > + ndp_ra(c, saddr); > > } > > =20 > > return 1; >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --WslfWYhBs89upAeU Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmc0CcQACgkQzQJF27ox 2GeB4w/+IDW0AH1oVcbZuH3kxRaZGDmf78A1LTNQAJQV+9An1OXgoJIpO9Epev3r VP1nktxGRsl8m8ef9vZITpj5gqFbSJ+ajmOBzKyiUJr7IE8v+8rb463+NJGEygcy M1u/b0pqO+D7RjT3fjyI63nJs4NM9a9GbqxWSvNIy6+3rrLR7WHQ7I6AUBff1aOM +zU4PXL+3SmcOWNKV3j2t3ekSJPLQu5j5tw8v2AGBLuvghxX5Y2UmHOGvhoIuB2t 4ztlE74lTvw9+eUTYUkPGoKT/sWoix76CvTgzV/B+5NIgPaR7LQHMU9bqe0Xiq6i h5BRP2GiWy+rOEldJbx5dRgkJa7OO5UHtazSf18cfGU2mlRR3uKwxW72eqWHtxm9 ldVGjRhW1PUJ/EyTfNYn9iBB7grhvPiPzHt8lWmNipgBVHh9/25IYTQmKj7+O1Cj oEDsPTuCLCUNP4ySCH9fzWdwKgElfJYcuTCTqLWQPWYWp7KvP3UHXduHlUOJBqdr LysiuerqDcV8rq4M3JUPJ4lLLKSA5ryC6/Yu8llmz2vn+6Pd+sVK119tTiLt++k3 kc4QX3GYPe6YHAW4r9bFqeg0KYFnsL9nupY4pTMC/sLXfT+Qdd5BZfzM+BBrK4sG klhiOiX8xBrVW0YV/b4CIHRPfr1qjdLI3Of6xC/3hYpXrwGH0ZY= =fUna -----END PGP SIGNATURE----- --WslfWYhBs89upAeU--