Skip to content

Commit 6c92528

Browse files
Pull up following revision(s) (requested by ozaki-r in ticket #1164):
sys/net/link_proto.c: revision 1.41 sys/netinet6/in6.c: revision 1.293 sys/net/if.h: revision 1.307 sys/netinet/ip_icmp.c: revision 1.180 sys/dev/vmt/vmt_subr.c: revision 1.11 sys/netinet6/in6_var.h: revision 1.105 sys/netinet6/in6_var.h: revision 1.106 sys/net/if.c: revision 1.532 sys/net/if.c: revision 1.533 sys/netinet6/mld6.c: revision 1.102 sys/netinet/in_var.h: revision 1.104 sys/net/if_spppsubr.c: revision 1.270 sys/net/if_spppsubr.c: revision 1.271 sys/netinet6/nd6.c: revision 1.284 if: introduce if_first_addr() (and psref variant) It returns a first address (ifa) of a given family on a given interface. It will replace a bunch of open codes and make their intent clear. Apply if_first_addr() and if_first_addr_psref() in6: introduce in6ifa_first_lladdr() (and psref variant) It returns a first link-local address (ifa) on a given interface. Apply in6ifa_first_lladdr() and in6ifa_first_lladdr_psref()
1 parent ffa0c45 commit 6c92528

11 files changed

Lines changed: 141 additions & 153 deletions

File tree

sys/dev/vmt/vmt_subr.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: vmt_subr.c,v 1.3.16.1 2024/09/11 15:52:17 martin Exp $ */
1+
/* $NetBSD: vmt_subr.c,v 1.3.16.2 2025/10/01 14:58:37 martin Exp $ */
22
/* $OpenBSD: vmt.c,v 1.11 2011/01/27 21:29:25 dtucker Exp $ */
33

44
/*
@@ -711,17 +711,12 @@ vmt_tclo_broadcastip(struct vmt_softc *sc)
711711
continue;
712712
}
713713

714-
IFADDR_READER_FOREACH(iface_addr, iface) {
715-
if (iface_addr->ifa_addr->sa_family != AF_INET) {
716-
continue;
717-
}
718-
714+
iface_addr = if_first_addr_psref(iface, AF_INET, &psref);
715+
if (iface_addr != NULL) {
719716
guest_ip = satosin(iface_addr->ifa_addr);
720-
ifa_acquire(iface_addr, &psref);
721-
goto got;
717+
break;
722718
}
723719
}
724-
got:
725720
pserialize_read_exit(s);
726721

727722
if (guest_ip != NULL) {

sys/net/if.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: if.c,v 1.528.2.1 2025/07/14 18:31:54 martin Exp $ */
1+
/* $NetBSD: if.c,v 1.528.2.2 2025/10/01 14:58:36 martin Exp $ */
22

33
/*-
44
* Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
9090
*/
9191

9292
#include <sys/cdefs.h>
93-
__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.528.2.1 2025/07/14 18:31:54 martin Exp $");
93+
__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.528.2.2 2025/10/01 14:58:36 martin Exp $");
9494

9595
#if defined(_KERNEL_OPT)
9696
#include "opt_inet.h"
@@ -2099,6 +2099,33 @@ ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
20992099
return ifa;
21002100
}
21012101

2102+
struct ifaddr *
2103+
if_first_addr(const struct ifnet *ifp, const int af)
2104+
{
2105+
struct ifaddr *ifa = NULL;
2106+
2107+
IFADDR_READER_FOREACH(ifa, ifp) {
2108+
if (ifa->ifa_addr->sa_family == af)
2109+
break;
2110+
}
2111+
return ifa;
2112+
}
2113+
2114+
struct ifaddr *
2115+
if_first_addr_psref(const struct ifnet *ifp, const int af, struct psref *psref)
2116+
{
2117+
struct ifaddr *ifa;
2118+
int s;
2119+
2120+
s = pserialize_read_enter();
2121+
ifa = if_first_addr(ifp, af);
2122+
if (ifa != NULL)
2123+
ifa_acquire(ifa, psref);
2124+
pserialize_read_exit(s);
2125+
2126+
return ifa;
2127+
}
2128+
21022129
/*
21032130
* Find an interface using a specific address family
21042131
*/
@@ -2113,12 +2140,10 @@ ifa_ifwithaf(int af)
21132140
IFNET_READER_FOREACH(ifp) {
21142141
if (if_is_deactivated(ifp))
21152142
continue;
2116-
IFADDR_READER_FOREACH(ifa, ifp) {
2117-
if (ifa->ifa_addr->sa_family == af)
2118-
goto out;
2119-
}
2143+
ifa = if_first_addr(ifp, af);
2144+
if (ifa != NULL)
2145+
break;
21202146
}
2121-
out:
21222147
pserialize_read_exit(s);
21232148
return ifa;
21242149
}

sys/net/if.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: if.h,v 1.304 2022/11/25 08:39:32 knakahara Exp $ */
1+
/* $NetBSD: if.h,v 1.304.2.1 2025/10/01 14:58:36 martin Exp $ */
22

33
/*-
44
* Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
@@ -1230,6 +1230,9 @@ struct ifaddr *ifaof_ifpforaddr_psref(const struct sockaddr *, struct ifnet *,
12301230
void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
12311231
void p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
12321232

1233+
struct ifaddr *if_first_addr(const struct ifnet *ifp, const int af);
1234+
struct ifaddr *if_first_addr_psref(const struct ifnet *ifp, const int af, struct psref *psref);
1235+
12331236
void if_clone_attach(struct if_clone *);
12341237
void if_clone_detach(struct if_clone *);
12351238

sys/net/if_spppsubr.c

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: if_spppsubr.c,v 1.266 2022/09/03 02:47:59 thorpej Exp $ */
1+
/* $NetBSD: if_spppsubr.c,v 1.266.4.1 2025/10/01 14:58:36 martin Exp $ */
22

33
/*
44
* Synchronous PPP/Cisco link level subroutines.
@@ -41,7 +41,7 @@
4141
*/
4242

4343
#include <sys/cdefs.h>
44-
__KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.266 2022/09/03 02:47:59 thorpej Exp $");
44+
__KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.266.4.1 2025/10/01 14:58:36 martin Exp $");
4545

4646
#if defined(_KERNEL_OPT)
4747
#include "opt_inet.h"
@@ -5567,39 +5567,27 @@ sppp_get_ip_addrs(struct sppp *sp, uint32_t *src, uint32_t *dst, uint32_t *srcma
55675567
{
55685568
struct ifnet *ifp = &sp->pp_if;
55695569
struct ifaddr *ifa;
5570-
struct sockaddr_in *si, *sm;
55715570
uint32_t ssrc, ddst;
5572-
int bound, s;
5571+
int bound;
55735572
struct psref psref;
55745573

5575-
sm = NULL;
55765574
ssrc = ddst = 0;
55775575
/*
55785576
* Pick the first AF_INET address from the list,
55795577
* aliases don't make any sense on a p2p link anyway.
55805578
*/
5581-
si = 0;
55825579
bound = curlwp_bind();
5583-
s = pserialize_read_enter();
5584-
IFADDR_READER_FOREACH(ifa, ifp) {
5585-
if (ifa->ifa_addr->sa_family == AF_INET) {
5586-
si = (struct sockaddr_in *)ifa->ifa_addr;
5587-
sm = (struct sockaddr_in *)ifa->ifa_netmask;
5588-
if (si) {
5589-
ifa_acquire(ifa, &psref);
5590-
break;
5591-
}
5592-
}
5593-
}
5594-
pserialize_read_exit(s);
5595-
if (ifa) {
5596-
if (si && si->sin_addr.s_addr) {
5580+
ifa = if_first_addr_psref(ifp, AF_INET, &psref);
5581+
if (ifa != NULL) {
5582+
struct sockaddr_in *si = satosin(ifa->ifa_addr);
5583+
struct sockaddr_in *sm = satosin(ifa->ifa_netmask);
5584+
if (si->sin_addr.s_addr) {
55975585
ssrc = si->sin_addr.s_addr;
55985586
if (srcmask)
55995587
*srcmask = ntohl(sm->sin_addr.s_addr);
56005588
}
56015589

5602-
si = (struct sockaddr_in *)ifa->ifa_dstaddr;
5590+
si = satosin(ifa->ifa_dstaddr);
56035591
if (si && si->sin_addr.s_addr)
56045592
ddst = si->sin_addr.s_addr;
56055593
ifa_release(ifa, &psref);
@@ -5621,7 +5609,7 @@ sppp_set_ip_addrs(struct sppp *sp)
56215609
struct ifaddr *ifa;
56225610
struct sockaddr_in *si, *dest;
56235611
uint32_t myaddr = 0, hisaddr = 0;
5624-
int s;
5612+
struct psref psref;
56255613

56265614
KASSERT(SPPP_WLOCKED(sp));
56275615

@@ -5636,15 +5624,11 @@ sppp_set_ip_addrs(struct sppp *sp)
56365624
* aliases don't make any sense on a p2p link anyway.
56375625
*/
56385626
si = dest = NULL;
5639-
s = pserialize_read_enter();
5640-
IFADDR_READER_FOREACH(ifa, ifp) {
5641-
if (ifa->ifa_addr->sa_family == AF_INET) {
5642-
si = (struct sockaddr_in *)ifa->ifa_addr;
5643-
dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
5644-
break;
5645-
}
5627+
ifa = if_first_addr_psref(ifp, AF_INET, &psref);
5628+
if (ifa != NULL) {
5629+
si = satosin(ifa->ifa_addr);
5630+
dest = satosin(ifa->ifa_dstaddr);
56465631
}
5647-
pserialize_read_exit(s);
56485632

56495633
if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
56505634
myaddr = sp->ipcp.req_myaddr;
@@ -5682,6 +5666,8 @@ sppp_set_ip_addrs(struct sppp *sp)
56825666
pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
56835667
}
56845668
}
5669+
if (ifa != NULL)
5670+
ifa_release(ifa, &psref);
56855671

56865672
IFNET_UNLOCK(ifp);
56875673

@@ -5697,7 +5683,7 @@ sppp_clear_ip_addrs(struct sppp *sp)
56975683
struct ifnet *ifp;
56985684
struct ifaddr *ifa;
56995685
struct sockaddr_in *si, *dest;
5700-
int s;
5686+
struct psref psref;
57015687

57025688
KASSERT(SPPP_WLOCKED(sp));
57035689

@@ -5712,15 +5698,16 @@ sppp_clear_ip_addrs(struct sppp *sp)
57125698
* aliases don't make any sense on a p2p link anyway.
57135699
*/
57145700
si = dest = NULL;
5715-
s = pserialize_read_enter();
5716-
IFADDR_READER_FOREACH(ifa, ifp) {
5717-
if (ifa->ifa_addr->sa_family == AF_INET) {
5718-
si = (struct sockaddr_in *)ifa->ifa_addr;
5719-
dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
5720-
break;
5721-
}
5701+
ifa = if_first_addr_psref(ifp, AF_INET, &psref);
5702+
if (ifa != NULL) {
5703+
si = satosin(ifa->ifa_addr);
5704+
dest = satosin(ifa->ifa_dstaddr);
5705+
/*
5706+
* ignore "0.0.0.0" which means ppp is not opened yet.
5707+
*/
5708+
if (si->sin_addr.s_addr == INADDR_ANY)
5709+
si = NULL;
57225710
}
5723-
pserialize_read_exit(s);
57245711

57255712
if (si != NULL) {
57265713
struct sockaddr_in new_sin = *si;
@@ -5746,6 +5733,8 @@ sppp_clear_ip_addrs(struct sppp *sp)
57465733
pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
57475734
}
57485735
}
5736+
if (ifa != NULL)
5737+
ifa_release(ifa, &psref);
57495738

57505739
IFNET_UNLOCK(ifp);
57515740
}
@@ -5761,35 +5750,22 @@ sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
57615750
{
57625751
struct ifnet *ifp = &sp->pp_if;
57635752
struct ifaddr *ifa;
5764-
struct sockaddr_in6 *si, *sm;
57655753
struct in6_addr ssrc, ddst;
5766-
int bound, s;
5754+
int bound;
57675755
struct psref psref;
57685756

5769-
sm = NULL;
57705757
memset(&ssrc, 0, sizeof(ssrc));
57715758
memset(&ddst, 0, sizeof(ddst));
57725759
/*
57735760
* Pick the first link-local AF_INET6 address from the list,
57745761
* aliases don't make any sense on a p2p link anyway.
57755762
*/
5776-
si = 0;
57775763
bound = curlwp_bind();
5778-
s = pserialize_read_enter();
5779-
IFADDR_READER_FOREACH(ifa, ifp) {
5780-
if (ifa->ifa_addr->sa_family == AF_INET6) {
5781-
si = (struct sockaddr_in6 *)ifa->ifa_addr;
5782-
sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5783-
if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr)) {
5784-
ifa_acquire(ifa, &psref);
5785-
break;
5786-
}
5787-
}
5788-
}
5789-
pserialize_read_exit(s);
5790-
5791-
if (ifa) {
5792-
if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5764+
ifa = in6ifa_first_lladdr_psref(ifp, &psref);
5765+
if (ifa != NULL) {
5766+
struct sockaddr_in6 *si = satosin6(ifa->ifa_addr);
5767+
struct sockaddr_in6 *sm = satosin6(ifa->ifa_netmask);
5768+
if (!IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
57935769
memcpy(&ssrc, &si->sin6_addr, sizeof(ssrc));
57945770
if (srcmask) {
57955771
memcpy(srcmask, &sm->sin6_addr,
@@ -5828,7 +5804,6 @@ sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
58285804
{
58295805
struct ifnet *ifp;
58305806
struct ifaddr *ifa;
5831-
struct sockaddr_in6 *sin6;
58325807
int s;
58335808
struct psref psref;
58345809

@@ -5845,25 +5820,11 @@ sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
58455820
* aliases don't make any sense on a p2p link anyway.
58465821
*/
58475822

5848-
sin6 = NULL;
5849-
s = pserialize_read_enter();
5850-
IFADDR_READER_FOREACH(ifa, ifp)
5851-
{
5852-
if (ifa->ifa_addr->sa_family == AF_INET6)
5853-
{
5854-
sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5855-
if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5856-
ifa_acquire(ifa, &psref);
5857-
break;
5858-
}
5859-
}
5860-
}
5861-
pserialize_read_exit(s);
5862-
5863-
if (ifa && sin6)
5864-
{
5865-
int error;
5823+
ifa = in6ifa_first_lladdr_psref(ifp, &psref);
5824+
if (ifa != NULL) {
5825+
struct sockaddr_in6 *sin6 = satosin6(ifa->ifa_addr);
58665826
struct sockaddr_in6 new_sin6 = *sin6;
5827+
int error;
58675828

58685829
memcpy(&new_sin6.sin6_addr, src, sizeof(new_sin6.sin6_addr));
58695830
error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);

sys/net/link_proto.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: link_proto.c,v 1.40 2021/12/31 14:25:24 riastradh Exp $ */
1+
/* $NetBSD: link_proto.c,v 1.40.4.1 2025/10/01 14:58:36 martin Exp $ */
22

33
/*-
44
* Copyright (c) 1982, 1986, 1993
@@ -32,7 +32,7 @@
3232
*/
3333

3434
#include <sys/cdefs.h>
35-
__KERNEL_RCSID(0, "$NetBSD: link_proto.c,v 1.40 2021/12/31 14:25:24 riastradh Exp $");
35+
__KERNEL_RCSID(0, "$NetBSD: link_proto.c,v 1.40.4.1 2025/10/01 14:58:36 martin Exp $");
3636

3737
#include <sys/param.h>
3838
#include <sys/socket.h>
@@ -182,12 +182,8 @@ link_control(struct socket *so, unsigned long cmd, void *data,
182182
case SIOCGLIFADDR:
183183
ifa_release(ifa, &psref);
184184
s = pserialize_read_enter();
185-
if ((iflr->flags & IFLR_PREFIX) == 0) {
186-
IFADDR_READER_FOREACH(ifa, ifp) {
187-
if (ifa->ifa_addr->sa_family == AF_LINK)
188-
break;
189-
}
190-
}
185+
if ((iflr->flags & IFLR_PREFIX) == 0)
186+
ifa = if_first_addr(ifp, AF_LINK);
191187
if (ifa == NULL) {
192188
pserialize_read_exit(s);
193189
error = EADDRNOTAVAIL;

sys/netinet/in_var.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: in_var.h,v 1.103.2.1 2025/07/29 09:35:28 martin Exp $ */
1+
/* $NetBSD: in_var.h,v 1.103.2.2 2025/10/01 14:58:37 martin Exp $ */
22

33
/*-
44
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -327,11 +327,7 @@ in_get_ia_from_ifp(struct ifnet *ifp)
327327
{
328328
struct ifaddr *ifa;
329329

330-
IFADDR_READER_FOREACH(ifa, ifp) {
331-
if (ifa->ifa_addr->sa_family == AF_INET)
332-
break;
333-
}
334-
330+
ifa = if_first_addr(ifp, AF_INET);
335331
return ifatoia(ifa);
336332
}
337333

0 commit comments

Comments
 (0)