]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/if.c
1f10ed96f61bf5cfe87b263e32166bf35bc58cb1
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1980, 1986, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)if.c 8.3 (Berkeley) 1/4/94
58 * $FreeBSD: src/sys/net/if.c,v 1.85.2.9 2001/07/24 19:10:17 brooks Exp $
61 #include <sys/param.h>
62 #include <sys/malloc.h>
64 #include <sys/systm.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/protosw.h>
69 #include <sys/kernel.h>
70 #include <sys/sockio.h>
71 #include <sys/syslog.h>
72 #include <sys/sysctl.h>
75 #include <net/if_arp.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_var.h>
79 #include <net/radix.h>
80 #include <net/route.h>
84 #include <sys/domain.h>
87 #if defined(INET) || defined(INET6)
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/in6_ifattach.h>
98 * System initialization
101 static int ifconf
__P((u_long
, caddr_t
));
102 static void if_qflush
__P((struct ifqueue
*));
103 static void link_rtrequest
__P((int, struct rtentry
*, struct sockaddr
*));
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
; /* depend on static init XXX */
113 * XXX: declare here to avoid to include many inet6 related files..
114 * should be more generalized?
116 extern void nd6_setmtu
__P((struct ifnet
*));
117 extern int ip6_auto_on
;
121 * Network interface utility routines.
123 * Routines with ifa_ifwith* names take sockaddr *'s as
128 struct ifaddr
**ifnet_addrs
;
129 struct ifnet
**ifindex2ifnet
= NULL
;
133 * Attach an interface to the
134 * list of "active" interfaces.
140 unsigned socksize
, ifasize
;
141 int namelen
, masklen
;
143 register struct sockaddr_dl
*sdl
;
144 register struct ifaddr
*ifa
;
145 static int if_indexlim
= 8;
148 if (ifp
->if_snd
.ifq_maxlen
== 0)
149 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
156 TAILQ_INSERT_TAIL(&ifnet
, ifp
, if_link
);
157 ifp
->if_index
= ++if_index
;
160 * The old code would work if the interface passed a pre-existing
161 * chain of ifaddrs to this code. We don't trust our callers to
162 * properly initialize the tailq, however, so we no longer allow
163 * this unlikely case.
165 TAILQ_INIT(&ifp
->if_addrhead
);
166 TAILQ_INIT(&ifp
->if_prefixhead
);
167 LIST_INIT(&ifp
->if_multiaddrs
);
168 getmicrotime(&ifp
->if_lastchange
);
169 if (ifnet_addrs
== 0 || if_index
>= if_indexlim
) {
170 unsigned n
= (if_indexlim
<<= 1) * sizeof(ifa
);
171 struct ifaddr
**q
= (struct ifaddr
**)
172 _MALLOC(n
, M_IFADDR
, M_WAITOK
);
173 bzero((caddr_t
)q
, n
);
175 bcopy((caddr_t
)ifnet_addrs
, (caddr_t
)q
, n
/2);
176 FREE((caddr_t
)ifnet_addrs
, M_IFADDR
);
178 ifnet_addrs
= (struct ifaddr
**)q
;
180 /* grow ifindex2ifnet */
181 n
= if_indexlim
* sizeof(struct ifaddr
*);
182 q
= (struct ifaddr
**)_MALLOC(n
, M_IFADDR
, M_WAITOK
);
185 bcopy((caddr_t
)ifindex2ifnet
, q
, n
/2);
186 _FREE((caddr_t
)ifindex2ifnet
, M_IFADDR
);
188 ifindex2ifnet
= (struct ifnet
**)q
;
191 ifindex2ifnet
[if_index
] = ifp
;
194 * create a Link Level name for this device
196 namelen
= snprintf(workbuf
, sizeof(workbuf
),
197 "%s%d", ifp
->if_name
, ifp
->if_unit
);
198 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
199 masklen
= _offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
200 socksize
= masklen
+ ifp
->if_addrlen
;
201 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
202 if (socksize
< sizeof(*sdl
))
203 socksize
= sizeof(*sdl
);
204 socksize
= ROUNDUP(socksize
);
205 ifasize
= sizeof(*ifa
) + 2 * socksize
;
206 ifa
= (struct ifaddr
*) _MALLOC(ifasize
, M_IFADDR
, M_WAITOK
);
208 bzero((caddr_t
)ifa
, ifasize
);
209 sdl
= (struct sockaddr_dl
*)(ifa
+ 1);
210 sdl
->sdl_len
= socksize
;
211 sdl
->sdl_family
= AF_LINK
;
212 bcopy(workbuf
, sdl
->sdl_data
, namelen
);
213 sdl
->sdl_nlen
= namelen
;
214 sdl
->sdl_index
= ifp
->if_index
;
215 sdl
->sdl_type
= ifp
->if_type
;
216 ifnet_addrs
[if_index
- 1] = ifa
;
218 ifa
->ifa_rtrequest
= link_rtrequest
;
219 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
220 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
221 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
222 sdl
->sdl_len
= masklen
;
224 sdl
->sdl_data
[--namelen
] = 0xff;
225 TAILQ_INSERT_HEAD(&ifp
->if_addrhead
, ifa
, ifa_link
);
230 * Locate an interface based on a complete address.
235 register struct sockaddr
*addr
;
237 register struct ifnet
*ifp
;
238 register struct ifaddr
*ifa
;
240 #define equal(a1, a2) \
241 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
242 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
)
243 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
244 ifa
= ifa
->ifa_link
.tqe_next
) {
245 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
247 if (equal(addr
, ifa
->ifa_addr
))
249 if ((ifp
->if_flags
& IFF_BROADCAST
) && ifa
->ifa_broadaddr
&&
250 /* IP6 doesn't have broadcast */
251 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
252 equal(ifa
->ifa_broadaddr
, addr
))
255 return ((struct ifaddr
*)0);
258 * Locate the point to point interface with a given destination address.
262 ifa_ifwithdstaddr(addr
)
263 register struct sockaddr
*addr
;
265 register struct ifnet
*ifp
;
266 register struct ifaddr
*ifa
;
268 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
)
269 if (ifp
->if_flags
& IFF_POINTOPOINT
)
270 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
271 ifa
= ifa
->ifa_link
.tqe_next
) {
272 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
274 if (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
))
277 return ((struct ifaddr
*)0);
281 * Find an interface on a specific network. If many, choice
282 * is most specific found.
286 struct sockaddr
*addr
;
288 register struct ifnet
*ifp
;
289 register struct ifaddr
*ifa
;
290 struct ifaddr
*ifa_maybe
= (struct ifaddr
*) 0;
291 u_int af
= addr
->sa_family
;
292 char *addr_data
= addr
->sa_data
, *cplim
;
295 * AF_LINK addresses can be looked up directly by their index number,
296 * so do that if we can.
299 register struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
300 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
301 return (ifnet_addrs
[sdl
->sdl_index
- 1]);
305 * Scan though each interface, looking for ones that have
306 * addresses in this address family.
308 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
309 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
310 ifa
= ifa
->ifa_link
.tqe_next
) {
311 register char *cp
, *cp2
, *cp3
;
313 if (ifa
->ifa_addr
->sa_family
!= af
)
316 /* This breaks tunneling application trying to install a route with
317 * a specific subnet and the local address as the destination
318 * It's breaks binary compatibility with previous version of MacOS X
322 #if INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
323 addr
->sa_family
!= AF_INET6
&&
325 ifp
->if_flags
& IFF_POINTOPOINT
) {
327 * This is a bit broken as it doesn't
328 * take into account that the remote end may
329 * be a single node in the network we are
331 * The trouble is that we don't know the
332 * netmask for the remote end.
334 if (ifa
->ifa_dstaddr
!= 0
335 && equal(addr
, ifa
->ifa_dstaddr
))
338 #endif /* __APPLE__*/
341 * if we have a special address handler,
342 * then use it instead of the generic one.
344 if (ifa
->ifa_claim_addr
) {
345 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
353 * Scan all the bits in the ifa's address.
354 * If a bit dissagrees with what we are
355 * looking for, mask it with the netmask
356 * to see if it really matters.
359 if (ifa
->ifa_netmask
== 0)
362 cp2
= ifa
->ifa_addr
->sa_data
;
363 cp3
= ifa
->ifa_netmask
->sa_data
;
364 cplim
= ifa
->ifa_netmask
->sa_len
365 + (char *)ifa
->ifa_netmask
;
367 if ((*cp
++ ^ *cp2
++) & *cp3
++)
368 goto next
; /* next address! */
370 * If the netmask of what we just found
371 * is more specific than what we had before
372 * (if we had one) then remember the new one
373 * before continuing to search
374 * for an even better one.
376 if (ifa_maybe
== 0 ||
377 rn_refines((caddr_t
)ifa
->ifa_netmask
,
378 (caddr_t
)ifa_maybe
->ifa_netmask
))
387 * Find an interface address specific to an interface best matching
391 ifaof_ifpforaddr(addr
, ifp
)
392 struct sockaddr
*addr
;
393 register struct ifnet
*ifp
;
395 register struct ifaddr
*ifa
;
396 register char *cp
, *cp2
, *cp3
;
397 register char *cplim
;
398 struct ifaddr
*ifa_maybe
= 0;
399 u_int af
= addr
->sa_family
;
403 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
404 ifa
= ifa
->ifa_link
.tqe_next
) {
405 if (ifa
->ifa_addr
->sa_family
!= af
)
409 if (ifa
->ifa_netmask
== 0) {
410 if (equal(addr
, ifa
->ifa_addr
) ||
411 (ifa
->ifa_dstaddr
&& equal(addr
, ifa
->ifa_dstaddr
)))
415 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
416 if (equal(addr
, ifa
->ifa_dstaddr
))
420 cp2
= ifa
->ifa_addr
->sa_data
;
421 cp3
= ifa
->ifa_netmask
->sa_data
;
422 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
423 for (; cp3
< cplim
; cp3
++)
424 if ((*cp
++ ^ *cp2
++) & *cp3
)
433 #include <net/route.h>
436 * Default action when installing a route with a Link Level gateway.
437 * Lookup an appropriate real ifa to point to.
438 * This should be moved to /sys/net/link.c eventually.
441 link_rtrequest(cmd
, rt
, sa
)
443 register struct rtentry
*rt
;
446 register struct ifaddr
*ifa
;
447 struct sockaddr
*dst
;
450 if (cmd
!= RTM_ADD
|| ((ifa
= rt
->rt_ifa
) == 0) ||
451 ((ifp
= ifa
->ifa_ifp
) == 0) || ((dst
= rt_key(rt
)) == 0))
453 ifa
= ifaof_ifpforaddr(dst
, ifp
);
456 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
457 ifa
->ifa_rtrequest(cmd
, rt
, sa
);
462 * Mark an interface down and notify protocols of
464 * NOTE: must be called at splnet or eqivalent.
467 if_unroute(ifp
, flag
, fam
)
468 register struct ifnet
*ifp
;
471 register struct ifaddr
*ifa
;
473 ifp
->if_flags
&= ~flag
;
474 getmicrotime(&ifp
->if_lastchange
);
475 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
476 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
477 pfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
478 if_qflush(&ifp
->if_snd
);
483 * Mark an interface up and notify protocols of
485 * NOTE: must be called at splnet or eqivalent.
488 if_route(ifp
, flag
, fam
)
489 register struct ifnet
*ifp
;
492 register struct ifaddr
*ifa
;
494 ifp
->if_flags
|= flag
;
495 getmicrotime(&ifp
->if_lastchange
);
496 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
497 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
498 pfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
502 if (ip6_auto_on
) /* Only if IPv6 is on on configured on on all ifs */
508 * Mark an interface down and notify protocols of
510 * NOTE: must be called at splnet or eqivalent.
514 register struct ifnet
*ifp
;
517 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
521 * Mark an interface up and notify protocols of
523 * NOTE: must be called at splnet or eqivalent.
527 register struct ifnet
*ifp
;
530 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
534 * Flush an interface queue.
538 register struct ifqueue
*ifq
;
540 register struct mbuf
*m
, *n
;
543 while ((m
= n
) != 0) {
553 * Map interface name to
554 * interface structure pointer.
557 ifunit(const char *name
)
559 char namebuf
[IFNAMSIZ
+ 1];
567 if (len
< 2 || len
> IFNAMSIZ
)
571 if (c
< '0' || c
> '9')
572 return NULL
; /* trailing garbage */
577 return NULL
; /* no interface name */
578 unit
+= (c
- '0') * m
;
580 return NULL
; /* number is unreasonable */
583 } while (c
>= '0' && c
<= '9');
585 bcopy(name
, namebuf
, len
);
588 * Now search all the interfaces for this name/number
590 for (ifp
= ifnet
.tqh_first
; ifp
; ifp
= ifp
->if_link
.tqe_next
) {
591 if (strcmp(ifp
->if_name
, namebuf
))
593 if (unit
== ifp
->if_unit
)
601 * Map interface name in a sockaddr_dl to
602 * interface structure pointer.
608 char ifname
[IFNAMSIZ
+1];
609 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)sa
;
611 if ( (sa
->sa_family
!= AF_LINK
) || (sdl
->sdl_nlen
== 0) ||
612 (sdl
->sdl_nlen
> IFNAMSIZ
) )
616 * ifunit wants a null-terminated name. It may not be null-terminated
617 * in the sockaddr. We don't want to change the caller's sockaddr,
618 * and there might not be room to put the trailing null anyway, so we
619 * make a local copy that we know we can null terminate safely.
622 bcopy(sdl
->sdl_data
, ifname
, sdl
->sdl_nlen
);
623 ifname
[sdl
->sdl_nlen
] = '\0';
624 return ifunit(ifname
);
632 ifioctl(so
, cmd
, data
, p
)
638 register struct ifnet
*ifp
;
639 register struct ifreq
*ifr
;
643 struct kev_msg ev_msg
;
644 struct net_event_data ev_data
;
650 return (ifconf(cmd
, data
));
652 ifr
= (struct ifreq
*)data
;
653 ifp
= ifunit(ifr
->ifr_name
);
659 ifr
->ifr_flags
= ifp
->if_flags
;
663 ifr
->ifr_metric
= ifp
->if_metric
;
667 ifr
->ifr_mtu
= ifp
->if_mtu
;
671 ifr
->ifr_phys
= ifp
->if_physical
;
675 error
= suser(p
->p_ucred
, &p
->p_acflag
);
679 if (ifp
->if_flags
& IFF_SMART
) {
680 /* Smart drivers twiddle their own routes */
683 if (ifp
->if_flags
& IFF_UP
&&
684 (ifr
->ifr_flags
& IFF_UP
) == 0) {
688 } else if (ifr
->ifr_flags
& IFF_UP
&&
689 (ifp
->if_flags
& IFF_UP
) == 0) {
694 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
695 (ifr
->ifr_flags
&~ IFF_CANTCHANGE
);
697 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
698 ifp
, cmd
, (caddr_t
) data
);
701 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
702 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
703 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
705 ev_msg
.event_code
= KEV_DL_SIFFLAGS
;
706 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
707 ev_data
.if_family
= ifp
->if_family
;
708 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
709 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
710 ev_msg
.dv
[0].data_ptr
= &ev_data
;
711 ev_msg
.dv
[1].data_length
= 0;
712 kev_post_msg(&ev_msg
);
714 getmicrotime(&ifp
->if_lastchange
);
718 error
= suser(p
->p_ucred
, &p
->p_acflag
);
721 ifp
->if_metric
= ifr
->ifr_metric
;
724 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
725 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
726 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
728 ev_msg
.event_code
= KEV_DL_SIFMETRICS
;
729 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
730 ev_data
.if_family
= ifp
->if_family
;
731 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
732 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
733 ev_msg
.dv
[0].data_ptr
= &ev_data
;
735 ev_msg
.dv
[1].data_length
= 0;
736 kev_post_msg(&ev_msg
);
738 getmicrotime(&ifp
->if_lastchange
);
742 error
= suser(p
->p_ucred
, &p
->p_acflag
);
746 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
747 ifp
, cmd
, (caddr_t
) data
);
750 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
751 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
752 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
754 ev_msg
.event_code
= KEV_DL_SIFPHYS
;
755 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
756 ev_data
.if_family
= ifp
->if_family
;
757 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
758 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
759 ev_msg
.dv
[0].data_ptr
= &ev_data
;
760 ev_msg
.dv
[1].data_length
= 0;
761 kev_post_msg(&ev_msg
);
763 getmicrotime(&ifp
->if_lastchange
);
769 u_long oldmtu
= ifp
->if_mtu
;
771 error
= suser(p
->p_ucred
, &p
->p_acflag
);
774 if (ifp
->if_ioctl
== NULL
)
776 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
)
779 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
780 ifp
, cmd
, (caddr_t
) data
);
783 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
784 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
785 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
787 ev_msg
.event_code
= KEV_DL_SIFMTU
;
788 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
789 ev_data
.if_family
= ifp
->if_family
;
790 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
791 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
792 ev_msg
.dv
[0].data_ptr
= &ev_data
;
793 ev_msg
.dv
[1].data_length
= 0;
794 kev_post_msg(&ev_msg
);
796 getmicrotime(&ifp
->if_lastchange
);
800 * If the link MTU changed, do network layer specific procedure.
802 if (ifp
->if_mtu
!= oldmtu
) {
812 error
= suser(p
->p_ucred
, &p
->p_acflag
);
816 /* Don't allow group membership on non-multicast interfaces. */
817 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
821 /* Don't let users screw up protocols' entries. */
822 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
)
826 if (cmd
== SIOCADDMULTI
) {
827 struct ifmultiaddr
*ifma
;
828 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
829 ev_msg
.event_code
= KEV_DL_ADDMULTI
;
831 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
832 ev_msg
.event_code
= KEV_DL_DELMULTI
;
835 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
836 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
837 ev_msg
.kev_subclass
= KEV_DL_SUBCLASS
;
838 strncpy(&ev_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
840 ev_data
.if_family
= ifp
->if_family
;
841 ev_data
.if_unit
= (unsigned long) ifp
->if_unit
;
842 ev_msg
.dv
[0].data_length
= sizeof(struct net_event_data
);
843 ev_msg
.dv
[0].data_ptr
= &ev_data
;
844 ev_msg
.dv
[1].data_length
= 0;
845 kev_post_msg(&ev_msg
);
847 getmicrotime(&ifp
->if_lastchange
);
854 case SIOCSIFPHYADDR_IN6
:
856 case SIOCSLIFPHYADDR
:
860 error
= suser(p
->p_ucred
, &p
->p_acflag
);
864 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
865 ifp
, cmd
, (caddr_t
) data
);
868 getmicrotime(&ifp
->if_lastchange
);
872 ifs
= (struct ifstat
*)data
;
873 ifs
->ascii
[0] = '\0';
875 case SIOCGIFPSRCADDR
:
876 case SIOCGIFPDSTADDR
:
877 case SIOCGLIFPHYADDR
:
881 return dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
882 ifp
, cmd
, (caddr_t
) data
);
885 oif_flags
= ifp
->if_flags
;
886 if (so
->so_proto
== 0)
889 return ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
,
902 #if BYTE_ORDER != BIG_ENDIAN
903 if (ifr
->ifr_addr
.sa_family
== 0 &&
904 ifr
->ifr_addr
.sa_len
< 16) {
905 ifr
->ifr_addr
.sa_family
= ifr
->ifr_addr
.sa_len
;
906 ifr
->ifr_addr
.sa_len
= 16;
909 if (ifr
->ifr_addr
.sa_len
== 0)
910 ifr
->ifr_addr
.sa_len
= 16;
918 case OSIOCGIFDSTADDR
:
919 cmd
= SIOCGIFDSTADDR
;
922 case OSIOCGIFBRDADDR
:
923 cmd
= SIOCGIFBRDADDR
;
926 case OSIOCGIFNETMASK
:
927 cmd
= SIOCGIFNETMASK
;
929 error
= ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
,
936 case OSIOCGIFDSTADDR
:
937 case OSIOCGIFBRDADDR
:
938 case OSIOCGIFNETMASK
:
939 *(u_short
*)&ifr
->ifr_addr
= ifr
->ifr_addr
.sa_family
;
943 #endif /* COMPAT_43 */
945 if (error
== EOPNOTSUPP
)
946 error
= dlil_ioctl(so
->so_proto
->pr_domain
->dom_family
,
947 ifp
, cmd
, (caddr_t
) data
);
955 * Set/clear promiscuous mode on interface ifp based on the truth value
956 * of pswitch. The calls are reference counted so that only the first
957 * "on" request actually has an effect, as does the final "off" request.
958 * Results are undefined if the "off" and "on" requests are not matched.
961 ifpromisc(ifp
, pswitch
)
969 oldflags
= ifp
->if_flags
;
972 * If the device is not configured up, we cannot put it in
975 if ((ifp
->if_flags
& IFF_UP
) == 0)
977 if (ifp
->if_pcount
++ != 0)
979 ifp
->if_flags
|= IFF_PROMISC
;
980 log(LOG_INFO
, "%s%d: promiscuous mode enabled\n",
981 ifp
->if_name
, ifp
->if_unit
);
983 if (--ifp
->if_pcount
> 0)
985 ifp
->if_flags
&= ~IFF_PROMISC
;
986 log(LOG_INFO
, "%s%d: promiscuous mode disabled\n",
987 ifp
->if_name
, ifp
->if_unit
);
989 ifr
.ifr_flags
= ifp
->if_flags
;
990 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
);
994 ifp
->if_flags
= oldflags
;
999 * Return interface configuration
1000 * of system. List may be used
1001 * in later ioctl's (above) to get
1002 * other information.
1010 register struct ifconf
*ifc
= (struct ifconf
*)data
;
1011 register struct ifnet
*ifp
= ifnet
.tqh_first
;
1012 register struct ifaddr
*ifa
;
1013 struct ifreq ifr
, *ifrp
;
1014 int space
= ifc
->ifc_len
, error
= 0;
1016 ifrp
= ifc
->ifc_req
;
1017 for (; space
> sizeof (ifr
) && ifp
; ifp
= ifp
->if_link
.tqe_next
) {
1021 ifnlen
= snprintf(workbuf
, sizeof(workbuf
),
1022 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1023 if(ifnlen
+ 1 > sizeof ifr
.ifr_name
) {
1024 error
= ENAMETOOLONG
;
1027 strcpy(ifr
.ifr_name
, workbuf
);
1031 ifa
= ifp
->if_addrhead
.tqh_first
;
1032 for ( ; space
> sizeof (ifr
) && ifa
;
1033 ifa
= ifa
->ifa_link
.tqe_next
) {
1034 register struct sockaddr
*sa
= ifa
->ifa_addr
;
1036 if (curproc
->p_prison
&& prison_if(curproc
, sa
))
1041 if (cmd
== OSIOCGIFCONF
) {
1042 struct osockaddr
*osa
=
1043 (struct osockaddr
*)&ifr
.ifr_addr
;
1045 osa
->sa_family
= sa
->sa_family
;
1046 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1051 if (sa
->sa_len
<= sizeof(*sa
)) {
1053 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1057 if (space
< sizeof (ifr
) + sa
->sa_len
-
1060 space
-= sa
->sa_len
- sizeof(*sa
);
1061 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1062 sizeof (ifr
.ifr_name
));
1064 error
= copyout((caddr_t
)sa
,
1065 (caddr_t
)&ifrp
->ifr_addr
, sa
->sa_len
);
1066 ifrp
= (struct ifreq
*)
1067 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
1071 space
-= sizeof (ifr
);
1076 bzero((caddr_t
)&ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
1077 error
= copyout((caddr_t
)&ifr
, (caddr_t
)ifrp
,
1081 space
-= sizeof (ifr
);
1085 ifc
->ifc_len
-= space
;
1090 * Just like if_promisc(), but for all-multicast-reception mode.
1093 if_allmulti(ifp
, onswitch
)
1101 if (ifp
->if_amcount
++ == 0) {
1102 ifp
->if_flags
|= IFF_ALLMULTI
;
1103 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1106 if (ifp
->if_amcount
> 1) {
1109 ifp
->if_amcount
= 0;
1110 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1111 error
= dlil_ioctl(0, ifp
, SIOCSIFFLAGS
, (caddr_t
) 0);
1122 * Add a multicast listenership to the interface in question.
1123 * The link layer provides a routine which converts
1126 if_addmulti(ifp
, sa
, retifma
)
1127 struct ifnet
*ifp
; /* interface to manipulate */
1128 struct sockaddr
*sa
; /* address to add */
1129 struct ifmultiaddr
**retifma
;
1131 struct sockaddr
*llsa
= 0;
1132 struct sockaddr
*dupsa
;
1134 struct ifmultiaddr
*ifma
;
1135 struct rslvmulti_req rsreq
;
1138 * If the matching multicast address already exists
1139 * then don't add a new one, just add a reference
1141 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1142 ifma
= ifma
->ifma_link
.le_next
) {
1143 if (equal(sa
, ifma
->ifma_addr
)) {
1144 ifma
->ifma_refcount
++;
1152 * Give the link layer a chance to accept/reject it, and also
1153 * find out which AF_LINK address this maps to, if it isn't one
1159 error
= dlil_ioctl(sa
->sa_family
, ifp
, SIOCRSLVMULTI
, (caddr_t
) &rsreq
);
1161 /* to be similar to FreeBSD */
1162 if (error
== EOPNOTSUPP
)
1168 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
, M_IFMADDR
, M_WAITOK
);
1169 MALLOC(dupsa
, struct sockaddr
*, sa
->sa_len
, M_IFMADDR
, M_WAITOK
);
1170 bcopy(sa
, dupsa
, sa
->sa_len
);
1172 ifma
->ifma_addr
= dupsa
;
1173 ifma
->ifma_lladdr
= llsa
;
1174 ifma
->ifma_ifp
= ifp
;
1175 ifma
->ifma_refcount
= 1;
1176 ifma
->ifma_protospec
= 0;
1177 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
1180 * Some network interfaces can scan the address list at
1181 * interrupt time; lock them out.
1184 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1190 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1191 ifma
= ifma
->ifma_link
.le_next
) {
1192 if (equal(ifma
->ifma_addr
, llsa
))
1196 ifma
->ifma_refcount
++;
1198 MALLOC(ifma
, struct ifmultiaddr
*, sizeof *ifma
,
1199 M_IFMADDR
, M_WAITOK
);
1200 MALLOC(dupsa
, struct sockaddr
*, llsa
->sa_len
,
1201 M_IFMADDR
, M_WAITOK
);
1202 bcopy(llsa
, dupsa
, llsa
->sa_len
);
1203 ifma
->ifma_addr
= dupsa
;
1204 ifma
->ifma_lladdr
= 0;
1205 ifma
->ifma_ifp
= ifp
;
1206 ifma
->ifma_refcount
= 1;
1208 LIST_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
1213 * We are certain we have added something, so call down to the
1214 * interface to let them know about it.
1217 dlil_ioctl(0, ifp
, SIOCADDMULTI
, (caddr_t
) 0);
1224 * Remove a reference to a multicast address on this interface. Yell
1225 * if the request does not match an existing membership.
1228 if_delmulti(ifp
, sa
)
1230 struct sockaddr
*sa
;
1232 struct ifmultiaddr
*ifma
;
1235 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1236 ifma
= ifma
->ifma_link
.le_next
)
1237 if (equal(sa
, ifma
->ifma_addr
))
1242 if (ifma
->ifma_refcount
> 1) {
1243 ifma
->ifma_refcount
--;
1247 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
1248 sa
= ifma
->ifma_lladdr
;
1250 LIST_REMOVE(ifma
, ifma_link
);
1252 * Make sure the interface driver is notified
1253 * in the case of a link layer mcast group being left.
1255 if (ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== 0)
1256 dlil_ioctl(0, ifp
, SIOCDELMULTI
, 0);
1258 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1259 FREE(ifma
, M_IFMADDR
);
1264 * Now look for the link-layer address which corresponds to
1265 * this network address. It had been squirreled away in
1266 * ifma->ifma_lladdr for this purpose (so we don't have
1267 * to call SIOCRSLVMULTI again), and we saved that
1268 * value in sa above. If some nasty deleted the
1269 * link-layer address out from underneath us, we can deal because
1270 * the address we stored was is not the same as the one which was
1271 * in the record for the link-layer address. (So we don't complain
1274 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1275 ifma
= ifma
->ifma_link
.le_next
)
1276 if (equal(sa
, ifma
->ifma_addr
))
1281 if (ifma
->ifma_refcount
> 1) {
1282 ifma
->ifma_refcount
--;
1287 LIST_REMOVE(ifma
, ifma_link
);
1288 dlil_ioctl(0, ifp
, SIOCDELMULTI
, (caddr_t
) 0);
1290 FREE(ifma
->ifma_addr
, M_IFMADDR
);
1291 FREE(sa
, M_IFMADDR
);
1292 FREE(ifma
, M_IFMADDR
);
1299 * We don't use if_setlladdr, our interfaces are responsible for
1300 * handling the SIOCSIFLLADDR ioctl.
1304 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
1310 struct ifmultiaddr
*
1311 ifmaof_ifpforaddr(sa
, ifp
)
1312 struct sockaddr
*sa
;
1315 struct ifmultiaddr
*ifma
;
1317 for (ifma
= ifp
->if_multiaddrs
.lh_first
; ifma
;
1318 ifma
= ifma
->ifma_link
.le_next
)
1319 if (equal(ifma
->ifma_addr
, sa
))
1325 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
1326 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
1330 * Shutdown all network activity. Used boot() when halting
1333 int if_down_all(void)
1339 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
1343 return(0); /* Sheesh */
1347 * Delete Routes for a Network Interface
1349 * Called for each routing entry via the rnh->rnh_walktree() call above
1350 * to delete all route entries referencing a detaching network interface.
1353 * rn pointer to node in the routing table
1354 * arg argument passed to rnh->rnh_walktree() - detaching interface
1358 * errno failed - reason indicated
1363 struct radix_node
*rn
;
1366 struct rtentry
*rt
= (struct rtentry
*)rn
;
1367 struct ifnet
*ifp
= arg
;
1370 if (rt
!= NULL
&& rt
->rt_ifp
== ifp
) {
1373 * Protect (sorta) against walktree recursion problems
1374 * with cloned routes
1376 if ((rt
->rt_flags
& RTF_UP
) == 0)
1379 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1380 rt_mask(rt
), rt
->rt_flags
,
1381 (struct rtentry
**) NULL
);
1383 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1391 * Removes routing table reference to a given interfacei
1392 * for a given protocol family
1395 void if_rtproto_del(struct ifnet
*ifp
, int protocol
)
1398 struct radix_node_head
*rnh
;
1400 if ((protocol
<= AF_MAX
) && ((rnh
= rt_tables
[protocol
]) != NULL
) && (ifp
!= NULL
))
1401 (void) rnh
->rnh_walktree(rnh
, if_rtdel
, ifp
);