2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1980, 1986, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)if.c 8.3 (Berkeley) 1/4/94
55 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
58 #include <sys/param.h>
59 #include <sys/malloc.h>
61 #include <sys/systm.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/protosw.h>
66 #include <sys/kernel.h>
67 #include <sys/sockio.h>
68 #include <sys/syslog.h>
69 #include <sys/sysctl.h>
72 #include <net/if_arp.h>
73 #include <net/if_dl.h>
74 #include <net/if_types.h>
75 #include <net/if_var.h>
76 #include <net/radix.h>
77 #include <net/route.h>
81 #include <sys/domain.h>
84 #if defined(INET) || defined(INET6)
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet6/in6_ifattach.h>
95 * System initialization
98 static int ifconf
__P((u_long
, caddr_t
));
99 static void if_qflush
__P((struct ifqueue
*));
100 static void link_rtrequest
__P((int, struct rtentry
*, struct sockaddr
*));
102 static struct if_clone
*if_clone_lookup(const char *, int *);
103 static int if_clone_list(struct if_clonereq
*);
105 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
106 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
108 int ifqmaxlen
= IFQ_MAXLEN
;
109 struct ifnethead ifnet
= TAILQ_HEAD_INITIALIZER(ifnet
);
110 struct ifmultihead ifma_lostlist
= LIST_HEAD_INITIALIZER(ifma_lostlist
);
112 static int if_cloners_count
;
113 LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
117 * XXX: declare here to avoid to include many inet6 related files..
118 * should be more generalized?
120 extern void nd6_setmtu
__P((struct ifnet
*));
123 #define M_CLONE M_IFADDR
126 * Network interface utility routines.
128 * Routines with ifa_ifwith* names take sockaddr *'s as
133 struct ifaddr
**ifnet_addrs
;
134 struct ifnet
**ifindex2ifnet
;
136 #define INITIAL_IF_INDEXLIM 8
139 * Function: if_next_index
141 * Return the next available interface index.
142 * Grow the ifnet_addrs[] and ifindex2ifnet[] arrays to accomodate the
143 * added entry when necessary.
146 * ifnet_addrs[] is indexed by (if_index - 1), whereas
147 * ifindex2ifnet[] is indexed by ifp->if_index. That requires us to
148 * always allocate one extra element to hold ifindex2ifnet[0], which
154 static int if_indexlim
= 0;
155 static int if_list_growing
= 0;
158 while (if_list_growing
) {
159 /* wait until list is done growing */
160 (void)tsleep((caddr_t
)&ifnet_addrs
, PZERO
, "if_next_index", 0);
162 new_index
= ++if_index
;
163 if (if_index
> if_indexlim
) {
166 caddr_t new_ifnet_addrs
;
167 caddr_t new_ifindex2ifnet
;
168 caddr_t old_ifnet_addrs
;
170 /* mark list as growing */
173 old_ifnet_addrs
= (caddr_t
)ifnet_addrs
;
174 if (ifnet_addrs
== NULL
) {
175 new_if_indexlim
= INITIAL_IF_INDEXLIM
;
177 new_if_indexlim
= if_indexlim
<< 1;
180 /* allocate space for the larger arrays */
181 n
= (2 * new_if_indexlim
+ 1) * sizeof(caddr_t
);
182 new_ifnet_addrs
= _MALLOC(n
, M_IFADDR
, M_WAITOK
);
183 new_ifindex2ifnet
= new_ifnet_addrs
184 + new_if_indexlim
* sizeof(caddr_t
);
185 bzero(new_ifnet_addrs
, n
);
186 if (ifnet_addrs
!= NULL
) {
187 /* copy the existing data */
188 bcopy((caddr_t
)ifnet_addrs
, new_ifnet_addrs
,
189 if_indexlim
* sizeof(caddr_t
));
190 bcopy((caddr_t
)ifindex2ifnet
,
192 (if_indexlim
+ 1) * sizeof(caddr_t
));
195 /* switch to the new tables and size */
196 ifnet_addrs
= (struct ifaddr
**)new_ifnet_addrs
;
197 ifindex2ifnet
= (struct ifnet
**)new_ifindex2ifnet
;
198 if_indexlim
= new_if_indexlim
;
200 /* release the old data */
201 if (old_ifnet_addrs
!= NULL
) {
202 _FREE((caddr_t
)old_ifnet_addrs
, M_IFADDR
);
205 /* wake up others that might be blocked */
207 wakeup((caddr_t
)&ifnet_addrs
);
214 * Attach an interface to the
215 * list of "active" interfaces.
221 unsigned socksize
, ifasize
;
222 int namelen
, masklen
;
224 register struct sockaddr_dl
*sdl
;
225 register struct ifaddr
*ifa
;
227 if (ifp
->if_snd
.ifq_maxlen
== 0)
228 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
232 * The old code would work if the interface passed a pre-existing
233 * chain of ifaddrs to this code. We don't trust our callers to
234 * properly initialize the tailq, however, so we no longer allow
235 * this unlikely case.
237 TAILQ_INIT(&ifp
->if_addrhead
);
238 TAILQ_INIT(&ifp
->if_prefixhead
);
239 LIST_INIT(&ifp
->if_multiaddrs
);
240 getmicrotime(&ifp
->if_lastchange
);
242 if ((ifp
->if_eflags
& IFEF_REUSE
) == 0 || ifp
->if_index
== 0) {
243 /* allocate a new entry */
244 ifp
->if_index
= if_next_index();
245 ifindex2ifnet
[ifp
->if_index
] = ifp
;
248 * create a Link Level name for this device
250 namelen
= snprintf(workbuf
, sizeof(workbuf
),
251 "%s%d", ifp
->if_name
, ifp
->if_unit
);
252 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
253 masklen
= _offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
254 socksize
= masklen
+ ifp
->if_addrlen
;
255 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
256 if (socksize
< sizeof(*sdl
))
257 socksize
= sizeof(*sdl
);
258 socksize
= ROUNDUP(socksize
);
259 ifasize
= sizeof(*ifa
) + 2 * socksize
;
260 ifa
= (struct ifaddr
*) _MALLOC(ifasize
, M_IFADDR
, M_WAITOK
);
262 bzero((caddr_t
)ifa
, ifasize
);
263 sdl
= (struct sockaddr_dl
*)(ifa
+ 1);
264 sdl
->sdl_len
= socksize
;
265 sdl
->sdl_family
= AF_LINK
;
266 bcopy(workbuf
, sdl
->sdl_data
, namelen
);
267 sdl
->sdl_nlen
= namelen
;
268 sdl
->sdl_index
= ifp
->if_index
;
269 sdl
->sdl_type
= ifp
->if_type
;
270 ifnet_addrs
[ifp
->if_index
- 1] = ifa
;
272 ifa
->ifa_rtrequest
= link_rtrequest
;
273 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
274 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
275 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
276 sdl
->sdl_len
= masklen
;
278 sdl
->sdl_data
[--namelen
] = 0xff;
281 ifa
= ifnet_addrs
[ifp
->if_index
- 1];
284 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
286 TAILQ_INSERT_TAIL(&ifnet
, ifp
, if_link
);
290 * Create a clone network interface.
293 if_clone_create(char *name
, int len
)
295 struct if_clone
*ifc
;
297 int wildcard
, bytoff
, bitoff
;
301 ifc
= if_clone_lookup(name
, &unit
);
305 if (ifunit(name
) != NULL
)
309 wildcard
= (unit
< 0);
311 * Find a free unit if none was given.
314 while ((bytoff
< ifc
->ifc_bmlen
)
315 && (ifc
->ifc_units
[bytoff
] == 0xff))
317 if (bytoff
>= ifc
->ifc_bmlen
)
319 while ((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0)
321 unit
= (bytoff
<< 3) + bitoff
;
324 if (unit
> ifc
->ifc_maxunit
)
327 err
= (*ifc
->ifc_create
)(ifc
, unit
);
333 bitoff
= unit
- (bytoff
<< 3);
337 * Allocate the unit in the bitmap.
339 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) == 0,
340 ("%s: bit is already set", __func__
));
341 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
343 /* In the wildcard case, we need to update the name. */
345 for (dp
= name
; *dp
!= '\0'; dp
++);
346 if (snprintf(dp
, len
- (dp
-name
), "%d", unit
) >
347 len
- (dp
-name
) - 1) {
349 * This can only be a programmer error and
350 * there's no straightforward way to recover if
353 panic("if_clone_create(): interface name too long");
362 * Destroy a clone network interface.
365 if_clone_destroy(const char *name
)
367 struct if_clone
*ifc
;
372 ifc
= if_clone_lookup(name
, &unit
);
376 if (unit
< ifc
->ifc_minifs
)
383 if (ifc
->ifc_destroy
== NULL
)
386 (*ifc
->ifc_destroy
)(ifp
);
389 * Compute offset in the bitmap and deallocate the unit.
392 bitoff
= unit
- (bytoff
<< 3);
393 KASSERT((ifc
->ifc_units
[bytoff
] & (1 << bitoff
)) != 0,
394 ("%s: bit is already cleared", __func__
));
395 ifc
->ifc_units
[bytoff
] &= ~(1 << bitoff
);
400 * Look up a network interface cloner.
403 static struct if_clone
*
404 if_clone_lookup(const char *name
, int *unitp
)
406 struct if_clone
*ifc
;
410 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
;) {
411 for (cp
= name
, i
= 0; i
< ifc
->ifc_namelen
; i
++, cp
++) {
412 if (ifc
->ifc_name
[i
] != *cp
)
417 ifc
= LIST_NEXT(ifc
, ifc_list
);
421 return ((struct if_clone
*)NULL
);
427 for (i
= 0; *cp
!= '\0'; cp
++) {
428 if (*cp
< '0' || *cp
> '9') {
429 /* Bogus unit number. */
432 i
= (i
* 10) + (*cp
- '0');
442 * Register a network interface cloner.
445 if_clone_attach(struct if_clone
*ifc
)
452 KASSERT(ifc
->ifc_minifs
- 1 <= ifc
->ifc_maxunit
,
453 ("%s: %s requested more units then allowed (%d > %d)",
454 __func__
, ifc
->ifc_name
, ifc
->ifc_minifs
,
455 ifc
->ifc_maxunit
+ 1));
457 * Compute bitmap size and allocate it.
459 maxclone
= ifc
->ifc_maxunit
+ 1;
461 if ((len
<< 3) < maxclone
)
463 ifc
->ifc_units
= _MALLOC(len
, M_CLONE
, M_WAITOK
| M_ZERO
);
464 bzero(ifc
->ifc_units
, len
);
465 ifc
->ifc_bmlen
= len
;
467 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
470 for (unit
= 0; unit
< ifc
->ifc_minifs
; unit
++) {
471 err
= (*ifc
->ifc_create
)(ifc
, unit
);
473 ("%s: failed to create required interface %s%d",
474 __func__
, ifc
->ifc_name
, unit
));
476 /* Allocate the unit in the bitmap. */
478 bitoff
= unit
- (bytoff
<< 3);
479 ifc
->ifc_units
[bytoff
] |= (1 << bitoff
);
484 * Unregister a network interface cloner.
487 if_clone_detach(struct if_clone
*ifc
)
490 LIST_REMOVE(ifc
, ifc_list
);
491 FREE(ifc
->ifc_units
, M_CLONE
);
496 * Provide list of interface cloners to userspace.
499 if_clone_list(struct if_clonereq
*ifcr
)
501 char outbuf
[IFNAMSIZ
], *dst
;
502 struct if_clone
*ifc
;
503 int count
, error
= 0;
505 ifcr
->ifcr_total
= if_cloners_count
;
506 if ((dst
= ifcr
->ifcr_buffer
) == NULL
) {
507 /* Just asking how many there are. */
511 if (ifcr
->ifcr_count
< 0)
514 count
= (if_cloners_count
< ifcr
->ifcr_count
) ?
515 if_cloners_count
: ifcr
->ifcr_count
;
517 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
518 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
519 strncpy(outbuf
, ifc
->ifc_name
, IFNAMSIZ
- 1);
520 error
= copyout(outbuf
, dst
, IFNAMSIZ
);
528 __private_extern__
int
532 register struct ifnet
*ifp
;
533 register struct ifaddr
*ifa
;
534 register unsigned int addr2
;
537 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
)
538 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
539 ifa
= ifa
->ifa_link
.tqe_next
) {
540 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
542 addr2
= IA_SIN(ifa
)->sin_addr
.s_addr
;
551 * Locate an interface based on a complete address.
556 register struct sockaddr
*addr
;
558 register struct ifnet
*ifp
;
559 register struct ifaddr
*ifa
;
561 #define equal(a1, a2) \
562 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
563 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
)
564 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
565 ifa
= ifa
->ifa_link
.tqe_next
) {
566 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
568 if (equal(addr
, ifa
->ifa_addr
))
570 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
571 /* IP6 doesn't have broadcast */
572 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
573 equal(ifa
->ifa_broadaddr
, addr
))
576 return ((struct ifaddr
*)0);
579 * Locate the point to point interface with a given destination address.
583 ifa_ifwithdstaddr(addr
)
584 register struct sockaddr
*addr
;
586 register struct ifnet
*ifp
;
587 register struct ifaddr
*ifa
;
589 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
)
590 if (ifp
->if_flags
& IFF_POINTOPOINT
)
591 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
592 ifa
= ifa
->ifa_link
.tqe_next
) {
593 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
595 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
))
598 return ((struct ifaddr
*)0);
602 * Find an interface on a specific network. If many, choice
603 * is most specific found.
607 struct sockaddr
*addr
;
609 register struct ifnet
*ifp
;
610 register struct ifaddr
*ifa
;
611 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
612 u_int af
= addr
->sa_family
;
613 char *addr_data
= addr
->sa_data
, *cplim
;
616 * AF_LINK addresses can be looked up directly by their index number,
617 * so do that if we can.
620 register struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
621 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
622 return (ifnet_addrs
[sdl
->sdl_index
- 1]);
626 * Scan though each interface, looking for ones that have
627 * addresses in this address family.
629 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
630 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
631 ifa
= ifa
->ifa_link
.tqe_next
) {
632 register char *cp
, *cp2
, *cp3
;
634 if (ifa
->ifa_addr
->sa_family
!= af
)
637 /* This breaks tunneling application trying to install a route with
638 * a specific subnet and the local address as the destination
639 * It's breaks binary compatibility with previous version of MacOS X
643 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
644 addr
->sa_family
!= AF_INET6
&&
646 ifp
->if_flags
& IFF_POINTOPOINT
) {
648 * This is a bit broken as it doesn't
649 * take into account that the remote end may
650 * be a single node in the network we are
652 * The trouble is that we don't know the
653 * netmask for the remote end.
655 if (ifa
->ifa_dstaddr
!= 0
656 && equal(addr
, ifa
->ifa_dstaddr
))
659 #endif /* __APPLE__*/
662 * if we have a special address handler,
663 * then use it instead of the generic one.
665 if (ifa
->ifa_claim_addr
) {
666 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
674 * Scan all the bits in the ifa's address.
675 * If a bit dissagrees with what we are
676 * looking for, mask it with the netmask
677 * to see if it really matters.
680 if (ifa
->ifa_netmask
== 0)
683 cp2
= ifa
->ifa_addr
->sa_data
;
684 cp3
= ifa
->ifa_netmask
->sa_data
;
685 cplim
= ifa
->ifa_netmask
->sa_len
686 + (char *)ifa
->ifa_netmask
;
688 if ((*cp
++ ^ *cp2
++) & *cp3
++)
689 goto next
; /* next address! */
691 * If the netmask of what we just found
692 * is more specific than what we had before
693 * (if we had one) then remember the new one
694 * before continuing to search
695 * for an even better one.
697 if (ifa_maybe
== 0 ||
698 rn_refines((caddr_t
)ifa
->ifa_netmask
,
699 (caddr_t
)ifa_maybe
->ifa_netmask
))
708 * Find an interface address specific to an interface best matching
712 ifaof_ifpforaddr(addr
, ifp
)
713 struct sockaddr
*addr
;
714 register struct ifnet
*ifp
;
716 register struct ifaddr
*ifa
;
717 register char *cp
, *cp2
, *cp3
;
718 register char *cplim
;
719 struct ifaddr
*ifa_maybe
= 0;
720 u_int af
= addr
->sa_family
;
724 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
725 ifa
= ifa
->ifa_link
.tqe_next
) {
726 if (ifa
->ifa_addr
->sa_family
!= af
)
730 if (ifa
->ifa_netmask
== 0) {
731 if (equal(addr
, ifa
->ifa_addr
) ||
732 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
736 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
737 if (equal(addr
, ifa
->ifa_dstaddr
))
741 cp2
= ifa
->ifa_addr
->sa_data
;
742 cp3
= ifa
->ifa_netmask
->sa_data
;
743 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
744 for (; cp3
< cplim
; cp3
++)
745 if ((*cp
++ ^ *cp2
++) & *cp3
)
754 #include <net/route.h>
757 * Default action when installing a route with a Link Level gateway.
758 * Lookup an appropriate real ifa to point to.
759 * This should be moved to /sys/net/link.c eventually.
762 link_rtrequest(cmd
, rt
, sa
)
764 register struct rtentry
*rt
;
767 register struct ifaddr
*ifa
;
768 struct sockaddr
*dst
;
771 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
772 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
774 ifa
= ifaof_ifpforaddr(dst
, ifp
);
777 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
778 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
783 * Mark an interface down and notify protocols of
785 * NOTE: must be called at splnet or eqivalent.
788 if_unroute(ifp
, flag
, fam
)
789 register struct ifnet
*ifp
;
792 register struct ifaddr
*ifa
;
794 ifp
->if_flags
&= ~flag
;
795 getmicrotime(&ifp
->if_lastchange
);
796 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
797 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
798 pfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
799 if_qflush(&ifp
->if_snd
);
804 * Mark an interface up and notify protocols of
806 * NOTE: must be called at splnet or eqivalent.
809 if_route(ifp
, flag
, fam
)
810 register struct ifnet
*ifp
;
813 register struct ifaddr
*ifa
;
815 ifp
->if_flags
|= flag
;
816 getmicrotime(&ifp
->if_lastchange
);
817 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
818 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
819 pfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
825 * Mark an interface down and notify protocols of
827 * NOTE: must be called at splnet or eqivalent.
831 register struct ifnet
*ifp
;
834 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
838 * Mark an interface up and notify protocols of
840 * NOTE: must be called at splnet or eqivalent.
844 register struct ifnet
*ifp
;
847 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
851 * Flush an interface queue.
855 register struct ifqueue
*ifq
;
857 register struct mbuf
*m
, *n
;
860 while ((m
= n
) != 0) {
870 * Map interface name to
871 * interface structure pointer.
874 ifunit(const char *name
)
876 char namebuf
[IFNAMSIZ
+ 1];
884 if (len
< 2 || len
> IFNAMSIZ
)
888 if (c
< '0' || c
> '9')
889 return NULL
; /* trailing garbage */
894 return NULL
; /* no interface name */
895 unit
+= (c
- '0') * m
;
897 return NULL
; /* number is unreasonable */
900 } while (c
>= '0' && c
<= '9');
902 bcopy(name
, namebuf
, len
);
905 * Now search all the interfaces for this name/number
907 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
908 if (strcmp(ifp
->if_name
, namebuf
))
910 if (unit
== ifp
->if_unit
)
918 * Map interface name in a sockaddr_dl to
919 * interface structure pointer.
925 char ifname
[IFNAMSIZ
+1];
926 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
928 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
929 (sdl
->sdl_nlen
> IFNAMSIZ
) )
933 * ifunit wants a null-terminated name. It may not be null-terminated
934 * in the sockaddr. We don't want to change the caller's sockaddr,
935 * and there might not be room to put the trailing null anyway, so we
936 * make a local copy that we know we can null terminate safely.
939 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
940 ifname
[sdl
->sdl_nlen
] = '\0';
941 return ifunit(ifname
);
949 ifioctl(so
, cmd
, data
, p
)
955 register struct ifnet
*ifp
;
956 register struct ifreq
*ifr
;
960 struct kev_msg ev_msg
;
961 struct net_event_data ev_data
;
967 return (ifconf(cmd
, data
));
969 ifr
= (struct ifreq
*)data
;
974 error
= suser(p
->p_ucred
, &p
->p_acflag
);
977 return ((cmd
== SIOCIFCREATE
) ?
978 if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
)) :
979 if_clone_destroy(ifr
->ifr_name
));
982 return (if_clone_list((struct if_clonereq
*)data
));
986 ifp
= ifunit(ifr
->ifr_name
);
992 ifr
->ifr_flags
= ifp
->if_flags
;
996 ifr
->ifr_metric
= ifp
->if_metric
;
1000 ifr
->ifr_mtu
= ifp
->if_mtu
;
1004 ifr
->ifr_phys
= ifp
->if_physical
;
1008 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1012 if (ifp
->if_flags
& IFF_SMART
) {
1013 /* Smart drivers twiddle their own routes */
1016 if (ifp
->if_flags
& IFF_UP
&&
1017 (ifr
->ifr_flags
& IFF_UP
) == 0) {
1021 } else if (ifr
->ifr_flags
& IFF_UP
&&
1022 (ifp
->if_flags
& IFF_UP
) == 0) {
1027 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1028 (ifr
->ifr_flags
&~ IFF_CANTCHANGE
);
1030 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1031 ifp
, cmd
, (caddr_t
) data
);
1034 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1035 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1036 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1038 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
1039 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1040 ev_data
.if_family
= ifp
->if_family
;
1041 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1042 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1043 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1044 ev_msg
.dv
[1].data_length
= 0;
1045 kev_post_msg(&ev_msg
);
1047 getmicrotime(&ifp
->if_lastchange
);
1051 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1054 ifp
->if_metric
= ifr
->ifr_metric
;
1057 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1058 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1059 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1061 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
1062 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1063 ev_data
.if_family
= ifp
->if_family
;
1064 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1065 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1066 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1068 ev_msg
.dv
[1].data_length
= 0;
1069 kev_post_msg(&ev_msg
);
1071 getmicrotime(&ifp
->if_lastchange
);
1075 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1079 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1080 ifp
, cmd
, (caddr_t
) data
);
1083 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1084 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1085 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1087 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
1088 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1089 ev_data
.if_family
= ifp
->if_family
;
1090 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1091 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1092 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1093 ev_msg
.dv
[1].data_length
= 0;
1094 kev_post_msg(&ev_msg
);
1096 getmicrotime(&ifp
->if_lastchange
);
1102 u_long oldmtu
= ifp
->if_mtu
;
1104 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1107 if (ifp
->if_ioctl
== NULL
)
1108 return (EOPNOTSUPP
);
1109 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
1112 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1113 ifp
, cmd
, (caddr_t
) data
);
1116 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1117 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1118 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1120 ev_msg
.event_code
= KEV_DL_SIFMTU
;
1121 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1122 ev_data
.if_family
= ifp
->if_family
;
1123 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1124 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1125 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1126 ev_msg
.dv
[1].data_length
= 0;
1127 kev_post_msg(&ev_msg
);
1129 getmicrotime(&ifp
->if_lastchange
);
1133 * If the link MTU changed, do network layer specific procedure.
1135 if (ifp
->if_mtu
!= oldmtu
) {
1145 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1149 /* Don't allow group membership on non-multicast interfaces. */
1150 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
1154 /* Don't let users screw up protocols' entries. */
1155 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
1159 if (cmd
== SIOCADDMULTI
) {
1160 struct ifmultiaddr
*ifma
;
1161 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
1162 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
1164 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
1165 ev_msg
.event_code
= KEV_DL_DELMULTI
;
1168 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
1169 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
1170 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
1171 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
1173 ev_data
.if_family
= ifp
->if_family
;
1174 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
1175 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
1176 ev_msg
.dv
[0].data_ptr
= &ev_data
;
1177 ev_msg
.dv
[1].data_length
= 0;
1178 kev_post_msg(&ev_msg
);
1180 getmicrotime(&ifp
->if_lastchange
);
1185 if (ifp
->if_type
!= IFT_L2VLAN
) {
1186 return (EOPNOTSUPP
);
1188 case SIOCSIFPHYADDR
:
1189 case SIOCDIFPHYADDR
:
1191 case SIOCSIFPHYADDR_IN6
:
1193 case SIOCSLIFPHYADDR
:
1195 case SIOCSIFGENERIC
:
1197 error
= suser(p
->p_ucred
, &p
->p_acflag
);
1201 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1202 ifp
, cmd
, (caddr_t
) data
);
1205 getmicrotime(&ifp
->if_lastchange
);
1209 ifs
= (struct ifstat
*)data
;
1210 ifs
->ascii
[0] = '\0';
1212 case SIOCGIFPSRCADDR
:
1213 case SIOCGIFPDSTADDR
:
1214 case SIOCGLIFPHYADDR
:
1216 case SIOCGIFGENERIC
:
1218 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1219 ifp
, cmd
, (caddr_t
) data
);
1221 if (ifp
->if_type
!= IFT_L2VLAN
) {
1222 return (EOPNOTSUPP
);
1224 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1225 ifp
, cmd
, (caddr_t
) data
);
1228 oif_flags
= ifp
->if_flags
;
1229 if (so
->so_proto
== 0)
1230 return (EOPNOTSUPP
);
1232 return ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
1241 case SIOCSIFDSTADDR
:
1243 case SIOCSIFBRDADDR
:
1244 case SIOCSIFNETMASK
:
1245 #if BYTE_ORDER != BIG_ENDIAN
1246 if (ifr
->ifr_addr
.sa_family
== 0 &&
1247 ifr
->ifr_addr
.sa_len
< 16) {
1248 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
1249 ifr
->ifr_addr
.sa_len
= 16;
1252 if (ifr
->ifr_addr
.sa_len
== 0)
1253 ifr
->ifr_addr
.sa_len
= 16;
1261 case OSIOCGIFDSTADDR
:
1262 cmd
= SIOCGIFDSTADDR
;
1265 case OSIOCGIFBRDADDR
:
1266 cmd
= SIOCGIFBRDADDR
;
1269 case OSIOCGIFNETMASK
:
1270 cmd
= SIOCGIFNETMASK
;
1272 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
,
1279 case OSIOCGIFDSTADDR
:
1280 case OSIOCGIFBRDADDR
:
1281 case OSIOCGIFNETMASK
:
1282 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
1286 #endif /* COMPAT_43 */
1288 if (error
== EOPNOTSUPP
)
1289 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
1290 ifp
, cmd
, (caddr_t
) data
);
1298 * Set/clear promiscuous mode on interface ifp based on the truth value
1299 * of pswitch. The calls are reference counted so that only the first
1300 * "on" request actually has an effect, as does the final "off" request.
1301 * Results are undefined if the "off" and "on" requests are not matched.
1304 ifpromisc(ifp
, pswitch
)
1312 oldflags
= ifp
->if_flags
;
1315 * If the device is not configured up, we cannot put it in
1318 if ((ifp
->if_flags
& IFF_UP
) == 0)
1320 if (ifp
->if_pcount
++ != 0)
1322 ifp
->if_flags
|= IFF_PROMISC
;
1323 log(LOG_INFO
, "%s%d: promiscuous mode enabled\n",
1324 ifp
->if_name
, ifp
->if_unit
);
1326 if (--ifp
->if_pcount
> 0)
1328 ifp
->if_flags
&= ~IFF_PROMISC
;
1329 log(LOG_INFO
, "%s%d: promiscuous mode disabled\n",
1330 ifp
->if_name
, ifp
->if_unit
);
1332 ifr
.ifr_flags
= ifp
->if_flags
;
1333 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
);
1337 ifp
->if_flags
= oldflags
;
1342 * Return interface configuration
1343 * of system. List may be used
1344 * in later ioctl's (above) to get
1345 * other information.
1353 register struct ifconf
*ifc
= (struct ifconf
*)data
;
1354 register struct ifnet
*ifp
= ifnet
.tqh_first
;
1355 register struct ifaddr
*ifa
;
1356 struct ifreq ifr
, *ifrp
;
1357 int space
= ifc
->ifc_len
, error
= 0;
1359 ifrp
= ifc
->ifc_req
;
1360 for (; space
> sizeof (ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1364 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1365 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1366 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1367 error
= ENAMETOOLONG
;
1370 strcpy(ifr
.ifr_name
, workbuf
);
1374 ifa
= ifp
->if_addrhead
.tqh_first
;
1375 for ( ; space
> sizeof (ifr
) && ifa
;
1376 ifa
= ifa
->ifa_link
.tqe_next
) {
1377 register struct sockaddr
*sa
= ifa
->ifa_addr
;
1379 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1384 if (cmd
== OSIOCGIFCONF
) {
1385 struct osockaddr
*osa
=
1386 (struct osockaddr
*)&ifr
.ifr_addr
;
1388 osa
->sa_family
= sa
->sa_family
;
1389 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1394 if (sa
->sa_len
<= sizeof(*sa
)) {
1396 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1400 if (space
< sizeof (ifr
) + sa
->sa_len
-
1403 space
-= sa
->sa_len
- sizeof(*sa
);
1404 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1405 sizeof (ifr
.ifr_name
));
1407 error
= copyout((caddr_t
)sa
,
1408 (caddr_t
)&ifrp
->ifr_addr
, sa
->sa_len
);
1409 ifrp
= (struct ifreq
*)
1410 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
1414 space
-= sizeof (ifr
);
1419 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1420 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1424 space
-= sizeof (ifr
);
1428 ifc
->ifc_len
-= space
;
1433 * Just like if_promisc(), but for all-multicast-reception mode.
1436 if_allmulti(ifp
, onswitch
)
1444 if (ifp
->if_amcount
++ == 0) {
1445 ifp
->if_flags
|= IFF_ALLMULTI
;
1446 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1449 if (ifp
->if_amcount
> 1) {
1452 ifp
->if_amcount
= 0;
1453 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1454 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1465 * Add a multicast listenership to the interface in question.
1466 * The link layer provides a routine which converts
1469 if_addmulti(ifp
, sa
, retifma
)
1470 struct ifnet
*ifp
; /* interface to manipulate */
1471 struct sockaddr
*sa
; /* address to add */
1472 struct ifmultiaddr
**retifma
;
1474 struct sockaddr
*llsa
= 0;
1475 struct sockaddr
*dupsa
;
1477 struct ifmultiaddr
*ifma
;
1478 struct rslvmulti_req rsreq
;
1481 * If the matching multicast address already exists
1482 * then don't add a new one, just add a reference
1484 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1485 ifma
= ifma
->ifma_link
.le_next
) {
1486 if (equal(sa
, ifma
->ifma_addr
)) {
1487 ifma
->ifma_refcount
++;
1495 * Give the link layer a chance to accept/reject it, and also
1496 * find out which AF_LINK address this maps to, if it isn't one
1502 error
= dlil_ioctl(sa
->sa_family
, ifp
, SIOCRSLVMULTI
, (caddr_t
) &rsreq
);
1504 /* to be similar to FreeBSD */
1505 if (error
== EOPNOTSUPP
)
1511 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1512 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1513 bcopy(sa
, dupsa
, sa
->sa_len
);
1515 ifma
->ifma_addr
= dupsa
;
1516 ifma
->ifma_lladdr
= llsa
;
1517 ifma
->ifma_ifp
= ifp
;
1518 ifma
->ifma_refcount
= 1;
1519 ifma
->ifma_protospec
= 0;
1520 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1523 * Some network interfaces can scan the address list at
1524 * interrupt time; lock them out.
1527 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1533 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1534 ifma
= ifma
->ifma_link
.le_next
) {
1535 if (equal(ifma
->ifma_addr
, llsa
))
1539 ifma
->ifma_refcount
++;
1541 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
,
1542 M_IFMADDR
, M_WAITOK
);
1543 MALLOC(dupsa
, struct sockaddr
*, llsa
->sa_len
,
1544 M_IFMADDR
, M_WAITOK
);
1545 bcopy(llsa
, dupsa
, llsa
->sa_len
);
1546 ifma
->ifma_addr
= dupsa
;
1547 ifma
->ifma_lladdr
= 0;
1548 ifma
->ifma_ifp
= ifp
;
1549 ifma
->ifma_refcount
= 1;
1551 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1556 * We are certain we have added something, so call down to the
1557 * interface to let them know about it.
1560 dlil_ioctl(0, ifp
, SIOCADDMULTI
, (caddr_t
) 0);
1567 if_delmultiaddr(struct ifmultiaddr
*ifma
)
1569 struct sockaddr
*sa
;
1572 /* Verify ifma is valid */
1574 struct ifmultiaddr
*match
= NULL
;
1575 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1576 for (match
= ifp
->if_multiaddrs
.lh_first
; match
; match
= match
->ifma_link
.le_next
) {
1577 if (match
->ifma_ifp
!= ifp
) {
1578 printf("if_delmultiaddr: ifma (%x) on ifp i(%s) is stale\n",
1579 match
, if_name(ifp
));
1580 return (0) ; /* swallow error ? */
1588 if (match
!= ifma
) {
1589 for (match
= ifma_lostlist
.lh_first
; match
; match
= match
->ifma_link
.le_next
) {
1590 if (match
->ifma_ifp
!= NULL
) {
1591 printf("if_delmultiaddr: item on lost list (%x) contains non-null ifp=%s\n",
1592 match
, if_name(match
->ifma_ifp
));
1593 return (0) ; /* swallow error ? */
1600 if (match
!= ifma
) {
1601 printf("if_delmultiaddr: ifma 0x%X is invalid\n", ifma
);
1606 if (ifma
->ifma_refcount
> 1) {
1607 ifma
->ifma_refcount
--;
1611 sa
= ifma
->ifma_lladdr
;
1613 if (sa
) /* send a routing msg for network addresses only */
1614 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1616 ifp
= ifma
->ifma_ifp
;
1618 LIST_REMOVE(ifma
, ifma_link
);
1620 * Make sure the interface driver is notified
1621 * in the case of a link layer mcast group being left.
1623 if (ifp
&& ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== 0)
1624 dlil_ioctl(0, ifp
, SIOCDELMULTI
, 0);
1625 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1626 FREE(ifma
, M_IFMADDR
);
1631 * Now look for the link-layer address which corresponds to
1632 * this network address. It had been squirreled away in
1633 * ifma->ifma_lladdr for this purpose (so we don't have
1634 * to call SIOCRSLVMULTI again), and we saved that
1635 * value in sa above. If some nasty deleted the
1636 * link-layer address out from underneath us, we can deal because
1637 * the address we stored was is not the same as the one which was
1638 * in the record for the link-layer address. (So we don't complain
1642 ifma
= ifp
->if_multiaddrs
.lh_first
;
1644 ifma
= ifma_lostlist
.lh_first
;
1645 for (; ifma
; ifma
= ifma
->ifma_link
.le_next
)
1646 if (equal(sa
, ifma
->ifma_addr
))
1649 FREE(sa
, M_IFMADDR
);
1654 return if_delmultiaddr(ifma
);
1658 * Remove a reference to a multicast address on this interface. Yell
1659 * if the request does not match an existing membership.
1662 if_delmulti(ifp
, sa
)
1664 struct sockaddr
*sa
;
1666 struct ifmultiaddr
*ifma
;
1668 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1669 ifma
= ifma
->ifma_link
.le_next
)
1670 if (equal(sa
, ifma
->ifma_addr
))
1675 return if_delmultiaddr(ifma
);
1680 * We don't use if_setlladdr, our interfaces are responsible for
1681 * handling the SIOCSIFLLADDR ioctl.
1685 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1691 struct ifmultiaddr
*
1692 ifmaof_ifpforaddr(sa
, ifp
)
1693 struct sockaddr
*sa
;
1696 struct ifmultiaddr
*ifma
;
1698 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1699 ifma
= ifma
->ifma_link
.le_next
)
1700 if (equal(ifma
->ifma_addr
, sa
))
1706 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1707 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
1711 * Shutdown all network activity. Used boot() when halting
1714 int if_down_all(void)
1720 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1724 return(0); /* Sheesh */
1728 * Delete Routes for a Network Interface
1730 * Called for each routing entry via the rnh->rnh_walktree() call above
1731 * to delete all route entries referencing a detaching network interface.
1734 * rn pointer to node in the routing table
1735 * arg argument passed to rnh->rnh_walktree() - detaching interface
1739 * errno failed - reason indicated
1744 struct radix_node
*rn
;
1747 struct rtentry
*rt
= (struct rtentry
*)rn
;
1748 struct ifnet
*ifp
= arg
;
1751 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
1754 * Protect (sorta) against walktree recursion problems
1755 * with cloned routes
1757 if ((rt
->rt_flags
& RTF_UP
) == 0)
1760 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1761 rt_mask(rt
), rt
->rt_flags
,
1762 (struct rtentry
**) NULL
);
1764 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1772 * Removes routing table reference to a given interfacei
1773 * for a given protocol family
1776 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
1779 struct radix_node_head
*rnh
;
1781 if ((protocol
<= AF_MAX
) && ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
))
1782 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);