if.c revision 1.177 1 /* $NetBSD: if.c,v 1.177 2006/11/16 01:33:40 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the project nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 /*
69 * Copyright (c) 1980, 1986, 1993
70 * The Regents of the University of California. All rights reserved.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the University nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
83 *
84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * SUCH DAMAGE.
95 *
96 * @(#)if.c 8.5 (Berkeley) 1/9/95
97 */
98
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.177 2006/11/16 01:33:40 christos Exp $");
101
102 #include "opt_inet.h"
103
104 #include "opt_compat_linux.h"
105 #include "opt_compat_svr4.h"
106 #include "opt_compat_ultrix.h"
107 #include "opt_compat_43.h"
108 #include "opt_atalk.h"
109 #include "opt_natm.h"
110 #include "opt_pfil_hooks.h"
111
112 #include <sys/param.h>
113 #include <sys/mbuf.h>
114 #include <sys/systm.h>
115 #include <sys/callout.h>
116 #include <sys/proc.h>
117 #include <sys/socket.h>
118 #include <sys/socketvar.h>
119 #include <sys/domain.h>
120 #include <sys/protosw.h>
121 #include <sys/kernel.h>
122 #include <sys/ioctl.h>
123 #include <sys/sysctl.h>
124 #include <sys/syslog.h>
125 #include <sys/kauth.h>
126
127 #include <net/if.h>
128 #include <net/if_dl.h>
129 #include <net/if_ether.h>
130 #include <net/if_media.h>
131 #include <net80211/ieee80211.h>
132 #include <net80211/ieee80211_ioctl.h>
133 #include <net/if_types.h>
134 #include <net/radix.h>
135 #include <net/route.h>
136 #include <net/netisr.h>
137 #ifdef NETATALK
138 #include <netatalk/at_extern.h>
139 #include <netatalk/at.h>
140 #endif
141 #include <net/pfil.h>
142
143 #ifdef INET6
144 #include <netinet/in.h>
145 #include <netinet6/in6_var.h>
146 #include <netinet6/nd6.h>
147 #endif
148
149 #include "carp.h"
150 #if NCARP > 0
151 #include <netinet/ip_carp.h>
152 #endif
153
154 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX) || defined(LKM)
155 #define COMPAT_OSOCK
156 #include <compat/sys/socket.h>
157 #endif
158
159 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
160 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
161
162 int ifqmaxlen = IFQ_MAXLEN;
163 struct callout if_slowtimo_ch;
164
165 int netisr; /* scheduling bits for network */
166
167 static int if_rt_walktree(struct radix_node *, void *);
168
169 static struct if_clone *if_clone_lookup(const char *, int *);
170 static int if_clone_list(struct if_clonereq *);
171
172 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
173 static int if_cloners_count;
174
175 #ifdef PFIL_HOOKS
176 struct pfil_head if_pfil; /* packet filtering hook for interfaces */
177 #endif
178
179 static void if_detach_queues(struct ifnet *, struct ifqueue *);
180
181 /*
182 * Network interface utility routines.
183 *
184 * Routines with ifa_ifwith* names take sockaddr *'s as
185 * parameters.
186 */
187 void
188 ifinit(void)
189 {
190
191 callout_init(&if_slowtimo_ch);
192 if_slowtimo(NULL);
193 #ifdef PFIL_HOOKS
194 if_pfil.ph_type = PFIL_TYPE_IFNET;
195 if_pfil.ph_ifnet = NULL;
196 if (pfil_head_register(&if_pfil) != 0)
197 printf("WARNING: unable to register pfil hook\n");
198 #endif
199 }
200
201 /*
202 * Null routines used while an interface is going away. These routines
203 * just return an error.
204 */
205
206 int
207 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
208 struct sockaddr *so, struct rtentry *rt)
209 {
210
211 return (ENXIO);
212 }
213
214 void
215 if_nullinput(struct ifnet *ifp, struct mbuf *m)
216 {
217
218 /* Nothing. */
219 }
220
221 void
222 if_nullstart(struct ifnet *ifp)
223 {
224
225 /* Nothing. */
226 }
227
228 int
229 if_nullioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
230 {
231
232 return (ENXIO);
233 }
234
235 int
236 if_nullinit(struct ifnet *ifp)
237 {
238
239 return (ENXIO);
240 }
241
242 void
243 if_nullstop(struct ifnet *ifp, int disable)
244 {
245
246 /* Nothing. */
247 }
248
249 void
250 if_nullwatchdog(struct ifnet *ifp)
251 {
252
253 /* Nothing. */
254 }
255
256 void
257 if_nulldrain(struct ifnet *ifp)
258 {
259
260 /* Nothing. */
261 }
262
263 static u_int if_index = 1;
264 struct ifnet_head ifnet;
265 size_t if_indexlim = 0;
266 struct ifaddr **ifnet_addrs = NULL;
267 struct ifnet **ifindex2ifnet = NULL;
268 struct ifnet *lo0ifp;
269
270 /*
271 * Allocate the link level name for the specified interface. This
272 * is an attachment helper. It must be called after ifp->if_addrlen
273 * is initialized, which may not be the case when if_attach() is
274 * called.
275 */
276 void
277 if_alloc_sadl(struct ifnet *ifp)
278 {
279 unsigned socksize, ifasize;
280 int namelen, masklen;
281 struct sockaddr_dl *sdl;
282 struct ifaddr *ifa;
283
284 /*
285 * If the interface already has a link name, release it
286 * now. This is useful for interfaces that can change
287 * link types, and thus switch link names often.
288 */
289 if (ifp->if_sadl != NULL)
290 if_free_sadl(ifp);
291
292 namelen = strlen(ifp->if_xname);
293 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
294 socksize = masklen + ifp->if_addrlen;
295 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
296 if (socksize < sizeof(*sdl))
297 socksize = sizeof(*sdl);
298 socksize = ROUNDUP(socksize);
299 ifasize = sizeof(*ifa) + 2 * socksize;
300 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
301 memset((caddr_t)ifa, 0, ifasize);
302 sdl = (struct sockaddr_dl *)(ifa + 1);
303 sdl->sdl_len = socksize;
304 sdl->sdl_family = AF_LINK;
305 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
306 sdl->sdl_nlen = namelen;
307 sdl->sdl_alen = ifp->if_addrlen;
308 sdl->sdl_index = ifp->if_index;
309 sdl->sdl_type = ifp->if_type;
310 ifnet_addrs[ifp->if_index] = ifa;
311 IFAREF(ifa);
312 ifa->ifa_ifp = ifp;
313 ifa->ifa_rtrequest = link_rtrequest;
314 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
315 IFAREF(ifa);
316 ifa->ifa_addr = (struct sockaddr *)sdl;
317 ifp->if_sadl = sdl;
318 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
319 ifa->ifa_netmask = (struct sockaddr *)sdl;
320 sdl->sdl_len = masklen;
321 while (namelen != 0)
322 sdl->sdl_data[--namelen] = 0xff;
323 }
324
325 /*
326 * Free the link level name for the specified interface. This is
327 * a detach helper. This is called from if_detach() or from
328 * link layer type specific detach functions.
329 */
330 void
331 if_free_sadl(struct ifnet *ifp)
332 {
333 struct ifaddr *ifa;
334 int s;
335
336 ifa = ifnet_addrs[ifp->if_index];
337 if (ifa == NULL) {
338 KASSERT(ifp->if_sadl == NULL);
339 return;
340 }
341
342 KASSERT(ifp->if_sadl != NULL);
343
344 s = splnet();
345 rtinit(ifa, RTM_DELETE, 0);
346 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
347 IFAFREE(ifa);
348
349 ifp->if_sadl = NULL;
350
351 ifnet_addrs[ifp->if_index] = NULL;
352 IFAFREE(ifa);
353 splx(s);
354 }
355
356 /*
357 * Attach an interface to the
358 * list of "active" interfaces.
359 */
360 void
361 if_attach(struct ifnet *ifp)
362 {
363 int indexlim = 0;
364
365 if (if_indexlim == 0) {
366 TAILQ_INIT(&ifnet);
367 if_indexlim = 8;
368 }
369 TAILQ_INIT(&ifp->if_addrlist);
370 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
371 ifp->if_index = if_index;
372 if (ifindex2ifnet == 0)
373 if_index++;
374 else
375 while (ifp->if_index < if_indexlim &&
376 ifindex2ifnet[ifp->if_index] != NULL) {
377 ++if_index;
378 if (if_index == 0)
379 if_index = 1;
380 /*
381 * If we hit USHRT_MAX, we skip back to 0 since
382 * there are a number of places where the value
383 * of if_index or if_index itself is compared
384 * to or stored in an unsigned short. By
385 * jumping back, we won't botch those assignments
386 * or comparisons.
387 */
388 else if (if_index == USHRT_MAX) {
389 /*
390 * However, if we have to jump back to
391 * zero *twice* without finding an empty
392 * slot in ifindex2ifnet[], then there
393 * there are too many (>65535) interfaces.
394 */
395 if (indexlim++)
396 panic("too many interfaces");
397 else
398 if_index = 1;
399 }
400 ifp->if_index = if_index;
401 }
402
403 /*
404 * We have some arrays that should be indexed by if_index.
405 * since if_index will grow dynamically, they should grow too.
406 * struct ifadd **ifnet_addrs
407 * struct ifnet **ifindex2ifnet
408 */
409 if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
410 ifp->if_index >= if_indexlim) {
411 size_t m, n, oldlim;
412 caddr_t q;
413
414 oldlim = if_indexlim;
415 while (ifp->if_index >= if_indexlim)
416 if_indexlim <<= 1;
417
418 /* grow ifnet_addrs */
419 m = oldlim * sizeof(struct ifaddr *);
420 n = if_indexlim * sizeof(struct ifaddr *);
421 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
422 memset(q, 0, n);
423 if (ifnet_addrs) {
424 bcopy((caddr_t)ifnet_addrs, q, m);
425 free((caddr_t)ifnet_addrs, M_IFADDR);
426 }
427 ifnet_addrs = (struct ifaddr **)q;
428
429 /* grow ifindex2ifnet */
430 m = oldlim * sizeof(struct ifnet *);
431 n = if_indexlim * sizeof(struct ifnet *);
432 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
433 memset(q, 0, n);
434 if (ifindex2ifnet) {
435 bcopy((caddr_t)ifindex2ifnet, q, m);
436 free((caddr_t)ifindex2ifnet, M_IFADDR);
437 }
438 ifindex2ifnet = (struct ifnet **)q;
439 }
440
441 ifindex2ifnet[ifp->if_index] = ifp;
442
443 /*
444 * Link level name is allocated later by a separate call to
445 * if_alloc_sadl().
446 */
447
448 if (ifp->if_snd.ifq_maxlen == 0)
449 ifp->if_snd.ifq_maxlen = ifqmaxlen;
450 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
451
452 ifp->if_link_state = LINK_STATE_UNKNOWN;
453
454 ifp->if_capenable = 0;
455 ifp->if_csum_flags_tx = 0;
456 ifp->if_csum_flags_rx = 0;
457
458 #ifdef ALTQ
459 ifp->if_snd.altq_type = 0;
460 ifp->if_snd.altq_disc = NULL;
461 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
462 ifp->if_snd.altq_tbr = NULL;
463 ifp->if_snd.altq_ifp = ifp;
464 #endif
465
466 #ifdef PFIL_HOOKS
467 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
468 ifp->if_pfil.ph_ifnet = ifp;
469 if (pfil_head_register(&ifp->if_pfil) != 0)
470 printf("%s: WARNING: unable to register pfil hook\n",
471 ifp->if_xname);
472 (void)pfil_run_hooks(&if_pfil,
473 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
474 #endif
475
476 if (!STAILQ_EMPTY(&domains))
477 if_attachdomain1(ifp);
478
479 /* Announce the interface. */
480 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
481 }
482
483 void
484 if_attachdomain(void)
485 {
486 struct ifnet *ifp;
487 int s;
488
489 s = splnet();
490 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
491 if_attachdomain1(ifp);
492 splx(s);
493 }
494
495 void
496 if_attachdomain1(struct ifnet *ifp)
497 {
498 struct domain *dp;
499 int s;
500
501 s = splnet();
502
503 /* address family dependent data region */
504 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
505 DOMAIN_FOREACH(dp) {
506 if (dp->dom_ifattach)
507 ifp->if_afdata[dp->dom_family] =
508 (*dp->dom_ifattach)(ifp);
509 }
510
511 splx(s);
512 }
513
514 /*
515 * Deactivate an interface. This points all of the procedure
516 * handles at error stubs. May be called from interrupt context.
517 */
518 void
519 if_deactivate(struct ifnet *ifp)
520 {
521 int s;
522
523 s = splnet();
524
525 ifp->if_output = if_nulloutput;
526 ifp->if_input = if_nullinput;
527 ifp->if_start = if_nullstart;
528 ifp->if_ioctl = if_nullioctl;
529 ifp->if_init = if_nullinit;
530 ifp->if_stop = if_nullstop;
531 ifp->if_watchdog = if_nullwatchdog;
532 ifp->if_drain = if_nulldrain;
533
534 /* No more packets may be enqueued. */
535 ifp->if_snd.ifq_maxlen = 0;
536
537 splx(s);
538 }
539
540 /*
541 * Detach an interface from the list of "active" interfaces,
542 * freeing any resources as we go along.
543 *
544 * NOTE: This routine must be called with a valid thread context,
545 * as it may block.
546 */
547 void
548 if_detach(struct ifnet *ifp)
549 {
550 struct socket so;
551 struct ifaddr *ifa, **ifap;
552 #ifdef IFAREF_DEBUG
553 struct ifaddr *last_ifa = NULL;
554 #endif
555 struct domain *dp;
556 const struct protosw *pr;
557 struct radix_node_head *rnh;
558 int s, i, family, purged;
559
560 /*
561 * XXX It's kind of lame that we have to have the
562 * XXX socket structure...
563 */
564 memset(&so, 0, sizeof(so));
565
566 s = splnet();
567
568 /*
569 * Do an if_down() to give protocols a chance to do something.
570 */
571 if_down(ifp);
572
573 #ifdef ALTQ
574 if (ALTQ_IS_ENABLED(&ifp->if_snd))
575 altq_disable(&ifp->if_snd);
576 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
577 altq_detach(&ifp->if_snd);
578 #endif
579
580
581 #if NCARP > 0
582 /* Remove the interface from any carp group it is a part of. */
583 if (ifp->if_carp && ifp->if_type != IFT_CARP)
584 carp_ifdetach(ifp);
585 #endif
586
587 #ifdef PFIL_HOOKS
588 (void)pfil_run_hooks(&if_pfil,
589 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
590 (void)pfil_head_unregister(&ifp->if_pfil);
591 #endif
592
593 /*
594 * Rip all the addresses off the interface. This should make
595 * all of the routes go away.
596 */
597 ifap = &TAILQ_FIRST(&ifp->if_addrlist); /* XXX abstraction violation */
598 while ((ifa = *ifap)) {
599 family = ifa->ifa_addr->sa_family;
600 #ifdef IFAREF_DEBUG
601 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
602 ifa, family, ifa->ifa_refcnt);
603 if (last_ifa != NULL && ifa == last_ifa)
604 panic("if_detach: loop detected");
605 last_ifa = ifa;
606 #endif
607 if (family == AF_LINK) {
608 ifap = &TAILQ_NEXT(ifa, ifa_list);
609 continue;
610 }
611 dp = pffinddomain(family);
612 #ifdef DIAGNOSTIC
613 if (dp == NULL)
614 panic("if_detach: no domain for AF %d",
615 family);
616 #endif
617 /*
618 * XXX These PURGEIF calls are redundant with the
619 * purge-all-families calls below, but are left in for
620 * now both to make a smaller change, and to avoid
621 * unplanned interactions with clearing of
622 * ifp->if_addrlist.
623 */
624 purged = 0;
625 for (pr = dp->dom_protosw;
626 pr < dp->dom_protoswNPROTOSW; pr++) {
627 so.so_proto = pr;
628 if (pr->pr_usrreq != NULL) {
629 (void) (*pr->pr_usrreq)(&so,
630 PRU_PURGEIF, NULL, NULL,
631 (struct mbuf *) ifp, curlwp);
632 purged = 1;
633 }
634 }
635 if (purged == 0) {
636 /*
637 * XXX What's really the best thing to do
638 * XXX here? --thorpej (at) NetBSD.org
639 */
640 printf("if_detach: WARNING: AF %d not purged\n",
641 family);
642 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
643 }
644 }
645
646 if_free_sadl(ifp);
647
648 /* Walk the routing table looking for straglers. */
649 for (i = 0; i <= AF_MAX; i++) {
650 if ((rnh = rt_tables[i]) != NULL)
651 (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
652 }
653
654 DOMAIN_FOREACH(dp) {
655 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
656 (*dp->dom_ifdetach)(ifp,
657 ifp->if_afdata[dp->dom_family]);
658
659 /*
660 * One would expect multicast memberships (INET and
661 * INET6) on UDP sockets to be purged by the PURGEIF
662 * calls above, but if all addresses were removed from
663 * the interface prior to destruction, the calls will
664 * not be made (e.g. ppp, for which pppd(8) generally
665 * removes addresses before destroying the interface).
666 * Because there is no invariant that multicast
667 * memberships only exist for interfaces with IPv4
668 * addresses, we must call PURGEIF regardless of
669 * addresses. (Protocols which might store ifnet
670 * pointers are marked with PR_PURGEIF.)
671 */
672 for (pr = dp->dom_protosw;
673 pr < dp->dom_protoswNPROTOSW; pr++) {
674 so.so_proto = pr;
675 if (pr->pr_usrreq != NULL &&
676 pr->pr_flags & PR_PURGEIF)
677 (void) (*pr->pr_usrreq)(&so,
678 PRU_PURGEIF, NULL, NULL,
679 (struct mbuf *) ifp, curlwp);
680 }
681 }
682
683 /* Announce that the interface is gone. */
684 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
685
686 ifindex2ifnet[ifp->if_index] = NULL;
687
688 TAILQ_REMOVE(&ifnet, ifp, if_list);
689
690 /*
691 * remove packets that came from ifp, from software interrupt queues.
692 */
693 DOMAIN_FOREACH(dp) {
694 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
695 if (dp->dom_ifqueues[i] == NULL)
696 break;
697 if_detach_queues(ifp, dp->dom_ifqueues[i]);
698 }
699 }
700
701 splx(s);
702 }
703
704 static void
705 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
706 {
707 struct mbuf *m, *prev, *next;
708
709 prev = NULL;
710 for (m = q->ifq_head; m; m = next) {
711 next = m->m_nextpkt;
712 #ifdef DIAGNOSTIC
713 if ((m->m_flags & M_PKTHDR) == 0) {
714 prev = m;
715 continue;
716 }
717 #endif
718 if (m->m_pkthdr.rcvif != ifp) {
719 prev = m;
720 continue;
721 }
722
723 if (prev)
724 prev->m_nextpkt = m->m_nextpkt;
725 else
726 q->ifq_head = m->m_nextpkt;
727 if (q->ifq_tail == m)
728 q->ifq_tail = prev;
729 q->ifq_len--;
730
731 m->m_nextpkt = NULL;
732 m_freem(m);
733 IF_DROP(q);
734 }
735 }
736
737 /*
738 * Callback for a radix tree walk to delete all references to an
739 * ifnet.
740 */
741 static int
742 if_rt_walktree(struct radix_node *rn, void *v)
743 {
744 struct ifnet *ifp = (struct ifnet *)v;
745 struct rtentry *rt = (struct rtentry *)rn;
746 int error;
747
748 if (rt->rt_ifp == ifp) {
749 /* Delete the entry. */
750 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
751 rt_mask(rt), rt->rt_flags, NULL);
752 if (error)
753 printf("%s: warning: unable to delete rtentry @ %p, "
754 "error = %d\n", ifp->if_xname, rt, error);
755 }
756 return (0);
757 }
758
759 /*
760 * Create a clone network interface.
761 */
762 int
763 if_clone_create(const char *name)
764 {
765 struct if_clone *ifc;
766 int unit;
767
768 ifc = if_clone_lookup(name, &unit);
769 if (ifc == NULL)
770 return (EINVAL);
771
772 if (ifunit(name) != NULL)
773 return (EEXIST);
774
775 return ((*ifc->ifc_create)(ifc, unit));
776 }
777
778 /*
779 * Destroy a clone network interface.
780 */
781 int
782 if_clone_destroy(const char *name)
783 {
784 struct if_clone *ifc;
785 struct ifnet *ifp;
786
787 ifc = if_clone_lookup(name, NULL);
788 if (ifc == NULL)
789 return (EINVAL);
790
791 ifp = ifunit(name);
792 if (ifp == NULL)
793 return (ENXIO);
794
795 if (ifc->ifc_destroy == NULL)
796 return (EOPNOTSUPP);
797
798 return ((*ifc->ifc_destroy)(ifp));
799 }
800
801 /*
802 * Look up a network interface cloner.
803 */
804 static struct if_clone *
805 if_clone_lookup(const char *name, int *unitp)
806 {
807 struct if_clone *ifc;
808 const char *cp;
809 int unit;
810
811 /* separate interface name from unit */
812 for (cp = name;
813 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
814 cp++)
815 continue;
816
817 if (cp == name || cp - name == IFNAMSIZ || !*cp)
818 return (NULL); /* No name or unit number */
819
820 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
821 if (strlen(ifc->ifc_name) == cp - name &&
822 !strncmp(name, ifc->ifc_name, cp - name))
823 break;
824 }
825
826 if (ifc == NULL)
827 return (NULL);
828
829 unit = 0;
830 while (cp - name < IFNAMSIZ && *cp) {
831 if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
832 /* Bogus unit number. */
833 return (NULL);
834 }
835 unit = (unit * 10) + (*cp++ - '0');
836 }
837
838 if (unitp != NULL)
839 *unitp = unit;
840 return (ifc);
841 }
842
843 /*
844 * Register a network interface cloner.
845 */
846 void
847 if_clone_attach(struct if_clone *ifc)
848 {
849
850 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
851 if_cloners_count++;
852 }
853
854 /*
855 * Unregister a network interface cloner.
856 */
857 void
858 if_clone_detach(struct if_clone *ifc)
859 {
860
861 LIST_REMOVE(ifc, ifc_list);
862 if_cloners_count--;
863 }
864
865 /*
866 * Provide list of interface cloners to userspace.
867 */
868 static int
869 if_clone_list(struct if_clonereq *ifcr)
870 {
871 char outbuf[IFNAMSIZ], *dst;
872 struct if_clone *ifc;
873 int count, error = 0;
874
875 ifcr->ifcr_total = if_cloners_count;
876 if ((dst = ifcr->ifcr_buffer) == NULL) {
877 /* Just asking how many there are. */
878 return (0);
879 }
880
881 if (ifcr->ifcr_count < 0)
882 return (EINVAL);
883
884 count = (if_cloners_count < ifcr->ifcr_count) ?
885 if_cloners_count : ifcr->ifcr_count;
886
887 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
888 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
889 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
890 if (outbuf[sizeof(outbuf) - 1] != '\0')
891 return ENAMETOOLONG;
892 error = copyout(outbuf, dst, sizeof(outbuf));
893 if (error)
894 break;
895 }
896
897 return (error);
898 }
899
900 /*
901 * Locate an interface based on a complete address.
902 */
903 /*ARGSUSED*/
904 struct ifaddr *
905 ifa_ifwithaddr(const struct sockaddr *addr)
906 {
907 struct ifnet *ifp;
908 struct ifaddr *ifa;
909
910 #define equal(a1, a2) \
911 (bcmp((a1), (a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
912
913 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
914 ifp = TAILQ_NEXT(ifp, if_list)) {
915 if (ifp->if_output == if_nulloutput)
916 continue;
917 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
918 ifa = TAILQ_NEXT(ifa, ifa_list)) {
919 if (ifa->ifa_addr->sa_family != addr->sa_family)
920 continue;
921 if (equal(addr, ifa->ifa_addr))
922 return (ifa);
923 if ((ifp->if_flags & IFF_BROADCAST) &&
924 ifa->ifa_broadaddr &&
925 /* IP6 doesn't have broadcast */
926 ifa->ifa_broadaddr->sa_len != 0 &&
927 equal(ifa->ifa_broadaddr, addr))
928 return (ifa);
929 }
930 }
931 return (NULL);
932 }
933
934 /*
935 * Locate the point to point interface with a given destination address.
936 */
937 /*ARGSUSED*/
938 struct ifaddr *
939 ifa_ifwithdstaddr(const struct sockaddr *addr)
940 {
941 struct ifnet *ifp;
942 struct ifaddr *ifa;
943
944 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
945 ifp = TAILQ_NEXT(ifp, if_list)) {
946 if (ifp->if_output == if_nulloutput)
947 continue;
948 if (ifp->if_flags & IFF_POINTOPOINT) {
949 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
950 ifa = TAILQ_NEXT(ifa, ifa_list)) {
951 if (ifa->ifa_addr->sa_family !=
952 addr->sa_family ||
953 ifa->ifa_dstaddr == NULL)
954 continue;
955 if (equal(addr, ifa->ifa_dstaddr))
956 return (ifa);
957 }
958 }
959 }
960 return (NULL);
961 }
962
963 /*
964 * Find an interface on a specific network. If many, choice
965 * is most specific found.
966 */
967 struct ifaddr *
968 ifa_ifwithnet(const struct sockaddr *addr)
969 {
970 struct ifnet *ifp;
971 struct ifaddr *ifa;
972 const struct sockaddr_dl *sdl;
973 struct ifaddr *ifa_maybe = 0;
974 u_int af = addr->sa_family;
975 const char *addr_data = addr->sa_data, *cplim;
976
977 if (af == AF_LINK) {
978 sdl = (const struct sockaddr_dl *)addr;
979 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
980 ifindex2ifnet[sdl->sdl_index] &&
981 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
982 return (ifnet_addrs[sdl->sdl_index]);
983 }
984 #ifdef NETATALK
985 if (af == AF_APPLETALK) {
986 const struct sockaddr_at *sat, *sat2;
987 sat = (const struct sockaddr_at *)addr;
988 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
989 ifp = TAILQ_NEXT(ifp, if_list)) {
990 if (ifp->if_output == if_nulloutput)
991 continue;
992 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
993 if (ifa == NULL)
994 continue;
995 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
996 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
997 return (ifa); /* exact match */
998 if (ifa_maybe == NULL) {
999 /* else keep the if with the right range */
1000 ifa_maybe = ifa;
1001 }
1002 }
1003 return (ifa_maybe);
1004 }
1005 #endif
1006 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1007 ifp = TAILQ_NEXT(ifp, if_list)) {
1008 if (ifp->if_output == if_nulloutput)
1009 continue;
1010 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1011 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1012 const char *cp, *cp2, *cp3;
1013
1014 if (ifa->ifa_addr->sa_family != af ||
1015 ifa->ifa_netmask == 0)
1016 next: continue;
1017 cp = addr_data;
1018 cp2 = ifa->ifa_addr->sa_data;
1019 cp3 = ifa->ifa_netmask->sa_data;
1020 cplim = (const char *)ifa->ifa_netmask +
1021 ifa->ifa_netmask->sa_len;
1022 while (cp3 < cplim) {
1023 if ((*cp++ ^ *cp2++) & *cp3++) {
1024 /* want to continue for() loop */
1025 goto next;
1026 }
1027 }
1028 if (ifa_maybe == 0 ||
1029 rn_refines((caddr_t)ifa->ifa_netmask,
1030 (caddr_t)ifa_maybe->ifa_netmask))
1031 ifa_maybe = ifa;
1032 }
1033 }
1034 return (ifa_maybe);
1035 }
1036
1037 /*
1038 * Find the interface of the addresss.
1039 */
1040 struct ifaddr *
1041 ifa_ifwithladdr(const struct sockaddr *addr)
1042 {
1043 struct ifaddr *ia;
1044
1045 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1046 (ia = ifa_ifwithnet(addr)))
1047 return (ia);
1048 return (NULL);
1049 }
1050
1051 /*
1052 * Find an interface using a specific address family
1053 */
1054 struct ifaddr *
1055 ifa_ifwithaf(int af)
1056 {
1057 struct ifnet *ifp;
1058 struct ifaddr *ifa;
1059
1060 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1061 ifp = TAILQ_NEXT(ifp, if_list)) {
1062 if (ifp->if_output == if_nulloutput)
1063 continue;
1064 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1065 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1066 if (ifa->ifa_addr->sa_family == af)
1067 return (ifa);
1068 }
1069 }
1070 return (NULL);
1071 }
1072
1073 /*
1074 * Find an interface address specific to an interface best matching
1075 * a given address.
1076 */
1077 struct ifaddr *
1078 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1079 {
1080 struct ifaddr *ifa;
1081 const char *cp, *cp2, *cp3;
1082 const char *cplim;
1083 struct ifaddr *ifa_maybe = 0;
1084 u_int af = addr->sa_family;
1085
1086 if (ifp->if_output == if_nulloutput)
1087 return (NULL);
1088
1089 if (af >= AF_MAX)
1090 return (NULL);
1091
1092 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1093 if (ifa->ifa_addr->sa_family != af)
1094 continue;
1095 ifa_maybe = ifa;
1096 if (ifa->ifa_netmask == 0) {
1097 if (equal(addr, ifa->ifa_addr) ||
1098 (ifa->ifa_dstaddr &&
1099 equal(addr, ifa->ifa_dstaddr)))
1100 return (ifa);
1101 continue;
1102 }
1103 cp = addr->sa_data;
1104 cp2 = ifa->ifa_addr->sa_data;
1105 cp3 = ifa->ifa_netmask->sa_data;
1106 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1107 for (; cp3 < cplim; cp3++) {
1108 if ((*cp++ ^ *cp2++) & *cp3)
1109 break;
1110 }
1111 if (cp3 == cplim)
1112 return (ifa);
1113 }
1114 return (ifa_maybe);
1115 }
1116
1117 /*
1118 * Default action when installing a route with a Link Level gateway.
1119 * Lookup an appropriate real ifa to point to.
1120 * This should be moved to /sys/net/link.c eventually.
1121 */
1122 void
1123 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1124 {
1125 struct ifaddr *ifa;
1126 struct sockaddr *dst;
1127 struct ifnet *ifp;
1128
1129 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1130 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1131 return;
1132 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1133 rt_replace_ifa(rt, ifa);
1134 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1135 ifa->ifa_rtrequest(cmd, rt, info);
1136 }
1137 }
1138
1139 /*
1140 * Handle a change in the interface link state.
1141 */
1142 void
1143 if_link_state_change(struct ifnet *ifp, int link_state)
1144 {
1145 /* Notify that the link state has changed. */
1146 if (ifp->if_link_state != link_state) {
1147 ifp->if_link_state = link_state;
1148 rt_ifmsg(ifp);
1149 #if NCARP > 0
1150 if (ifp->if_carp)
1151 carp_carpdev_state(ifp);
1152 #endif
1153 }
1154 }
1155
1156 /*
1157 * Mark an interface down and notify protocols of
1158 * the transition.
1159 * NOTE: must be called at splsoftnet or equivalent.
1160 */
1161 void
1162 if_down(struct ifnet *ifp)
1163 {
1164 struct ifaddr *ifa;
1165
1166 ifp->if_flags &= ~IFF_UP;
1167 microtime(&ifp->if_lastchange);
1168 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1169 ifa = TAILQ_NEXT(ifa, ifa_list))
1170 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1171 IFQ_PURGE(&ifp->if_snd);
1172 #if NCARP > 0
1173 if (ifp->if_carp)
1174 carp_carpdev_state(ifp);
1175 #endif
1176 rt_ifmsg(ifp);
1177 }
1178
1179 /*
1180 * Mark an interface up and notify protocols of
1181 * the transition.
1182 * NOTE: must be called at splsoftnet or equivalent.
1183 */
1184 void
1185 if_up(struct ifnet *ifp)
1186 {
1187 #ifdef notyet
1188 struct ifaddr *ifa;
1189 #endif
1190
1191 ifp->if_flags |= IFF_UP;
1192 microtime(&ifp->if_lastchange);
1193 #ifdef notyet
1194 /* this has no effect on IP, and will kill all ISO connections XXX */
1195 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1196 ifa = TAILQ_NEXT(ifa, ifa_list))
1197 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1198 #endif
1199 #if NCARP > 0
1200 if (ifp->if_carp)
1201 carp_carpdev_state(ifp);
1202 #endif
1203 rt_ifmsg(ifp);
1204 #ifdef INET6
1205 in6_if_up(ifp);
1206 #endif
1207 }
1208
1209 /*
1210 * Handle interface watchdog timer routines. Called
1211 * from softclock, we decrement timers (if set) and
1212 * call the appropriate interface routine on expiration.
1213 */
1214 void
1215 if_slowtimo(void *arg)
1216 {
1217 struct ifnet *ifp;
1218 int s = splnet();
1219
1220 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1221 ifp = TAILQ_NEXT(ifp, if_list)) {
1222 if (ifp->if_timer == 0 || --ifp->if_timer)
1223 continue;
1224 if (ifp->if_watchdog)
1225 (*ifp->if_watchdog)(ifp);
1226 }
1227 splx(s);
1228 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
1229 if_slowtimo, NULL);
1230 }
1231
1232 /*
1233 * Set/clear promiscuous mode on interface ifp based on the truth value
1234 * of pswitch. The calls are reference counted so that only the first
1235 * "on" request actually has an effect, as does the final "off" request.
1236 * Results are undefined if the "off" and "on" requests are not matched.
1237 */
1238 int
1239 ifpromisc(struct ifnet *ifp, int pswitch)
1240 {
1241 int pcount, ret;
1242 short flags;
1243 struct ifreq ifr;
1244
1245 pcount = ifp->if_pcount;
1246 flags = ifp->if_flags;
1247 if (pswitch) {
1248 /*
1249 * Allow the device to be "placed" into promiscuous
1250 * mode even if it is not configured up. It will
1251 * consult IFF_PROMISC when it is is brought up.
1252 */
1253 if (ifp->if_pcount++ != 0)
1254 return (0);
1255 ifp->if_flags |= IFF_PROMISC;
1256 if ((ifp->if_flags & IFF_UP) == 0)
1257 return (0);
1258 } else {
1259 if (--ifp->if_pcount > 0)
1260 return (0);
1261 ifp->if_flags &= ~IFF_PROMISC;
1262 /*
1263 * If the device is not configured up, we should not need to
1264 * turn off promiscuous mode (device should have turned it
1265 * off when interface went down; and will look at IFF_PROMISC
1266 * again next time interface comes up).
1267 */
1268 if ((ifp->if_flags & IFF_UP) == 0)
1269 return (0);
1270 }
1271 memset(&ifr, 0, sizeof(ifr));
1272 ifr.ifr_flags = ifp->if_flags;
1273 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
1274 /* Restore interface state if not successful. */
1275 if (ret != 0) {
1276 ifp->if_pcount = pcount;
1277 ifp->if_flags = flags;
1278 }
1279 return (ret);
1280 }
1281
1282 /*
1283 * Map interface name to
1284 * interface structure pointer.
1285 */
1286 struct ifnet *
1287 ifunit(const char *name)
1288 {
1289 struct ifnet *ifp;
1290 const char *cp = name;
1291 u_int unit = 0;
1292 u_int i;
1293
1294 /*
1295 * If the entire name is a number, treat it as an ifindex.
1296 */
1297 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1298 unit = unit * 10 + (*cp - '0');
1299 }
1300
1301 /*
1302 * If the number took all of the name, then it's a valid ifindex.
1303 */
1304 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1305 if (unit >= if_indexlim)
1306 return (NULL);
1307 ifp = ifindex2ifnet[unit];
1308 if (ifp == NULL || ifp->if_output == if_nulloutput)
1309 return (NULL);
1310 return (ifp);
1311 }
1312
1313 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1314 ifp = TAILQ_NEXT(ifp, if_list)) {
1315 if (ifp->if_output == if_nulloutput)
1316 continue;
1317 if (strcmp(ifp->if_xname, name) == 0)
1318 return (ifp);
1319 }
1320 return (NULL);
1321 }
1322
1323 /*
1324 * Interface ioctls.
1325 */
1326 int
1327 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct lwp *l)
1328 {
1329 struct ifnet *ifp;
1330 struct ifreq *ifr;
1331 struct ifcapreq *ifcr;
1332 struct ifdatareq *ifdr;
1333 int s, error = 0;
1334 short oif_flags;
1335
1336 switch (cmd) {
1337
1338 case SIOCGIFCONF:
1339 case OSIOCGIFCONF:
1340 return (ifconf(cmd, data));
1341 }
1342 ifr = (struct ifreq *)data;
1343 ifcr = (struct ifcapreq *)data;
1344 ifdr = (struct ifdatareq *)data;
1345
1346 ifp = ifunit(ifr->ifr_name);
1347
1348 switch (cmd) {
1349 case SIOCIFCREATE:
1350 case SIOCIFDESTROY:
1351 if (l) {
1352 error = kauth_authorize_network(l->l_cred,
1353 KAUTH_NETWORK_INTERFACE,
1354 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1355 (void *)cmd, NULL);
1356 if (error)
1357 return error;
1358 }
1359 return ((cmd == SIOCIFCREATE) ?
1360 if_clone_create(ifr->ifr_name) :
1361 if_clone_destroy(ifr->ifr_name));
1362
1363 case SIOCIFGCLONERS:
1364 return (if_clone_list((struct if_clonereq *)data));
1365 }
1366
1367 if (ifp == 0)
1368 return (ENXIO);
1369
1370 switch (cmd) {
1371 case SIOCSIFFLAGS:
1372 case SIOCSIFCAP:
1373 case SIOCSIFMETRIC:
1374 case SIOCZIFDATA:
1375 case SIOCSIFMTU:
1376 case SIOCSIFPHYADDR:
1377 case SIOCDIFPHYADDR:
1378 #ifdef INET6
1379 case SIOCSIFPHYADDR_IN6:
1380 #endif
1381 case SIOCSLIFPHYADDR:
1382 case SIOCADDMULTI:
1383 case SIOCDELMULTI:
1384 case SIOCSIFMEDIA:
1385 case SIOCSDRVSPEC:
1386 case SIOCS80211NWID:
1387 case SIOCS80211NWKEY:
1388 case SIOCS80211POWER:
1389 case SIOCS80211BSSID:
1390 case SIOCS80211CHANNEL:
1391 if (l) {
1392 error = kauth_authorize_network(l->l_cred,
1393 KAUTH_NETWORK_INTERFACE,
1394 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1395 (void *)cmd, NULL);
1396 if (error)
1397 return error;
1398 }
1399 }
1400
1401 oif_flags = ifp->if_flags;
1402 switch (cmd) {
1403
1404 case SIOCGIFFLAGS:
1405 ifr->ifr_flags = ifp->if_flags;
1406 break;
1407
1408 case SIOCGIFMETRIC:
1409 ifr->ifr_metric = ifp->if_metric;
1410 break;
1411
1412 case SIOCGIFMTU:
1413 ifr->ifr_mtu = ifp->if_mtu;
1414 break;
1415
1416 case SIOCGIFDLT:
1417 ifr->ifr_dlt = ifp->if_dlt;
1418 break;
1419
1420 case SIOCSIFFLAGS:
1421 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1422 s = splnet();
1423 if_down(ifp);
1424 splx(s);
1425 }
1426 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1427 s = splnet();
1428 if_up(ifp);
1429 splx(s);
1430 }
1431 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1432 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1433 if (ifp->if_ioctl)
1434 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1435 break;
1436
1437 case SIOCGIFCAP:
1438 ifcr->ifcr_capabilities = ifp->if_capabilities;
1439 ifcr->ifcr_capenable = ifp->if_capenable;
1440 break;
1441
1442 case SIOCSIFCAP:
1443 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1444 return (EINVAL);
1445 if (ifp->if_ioctl == NULL)
1446 return (EOPNOTSUPP);
1447
1448 /* Must prevent race with packet reception here. */
1449 s = splnet();
1450 if (ifcr->ifcr_capenable != ifp->if_capenable) {
1451 struct ifreq ifrq;
1452
1453 ifrq.ifr_flags = ifp->if_flags;
1454 ifp->if_capenable = ifcr->ifcr_capenable;
1455
1456 /* Pre-compute the checksum flags mask. */
1457 ifp->if_csum_flags_tx = 0;
1458 ifp->if_csum_flags_rx = 0;
1459 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1460 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1461 }
1462 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1463 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1464 }
1465
1466 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1467 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1468 }
1469 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1470 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1471 }
1472
1473 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1474 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1475 }
1476 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1477 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1478 }
1479
1480 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1481 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1482 }
1483 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1484 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1485 }
1486
1487 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1488 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1489 }
1490 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1491 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1492 }
1493
1494 /*
1495 * Only kick the interface if it's up. If it's
1496 * not up now, it will notice the cap enables
1497 * when it is brought up later.
1498 */
1499 if (ifp->if_flags & IFF_UP)
1500 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1501 (caddr_t) &ifrq);
1502 }
1503 splx(s);
1504 break;
1505
1506 case SIOCSIFMETRIC:
1507 ifp->if_metric = ifr->ifr_metric;
1508 break;
1509
1510 case SIOCGIFDATA:
1511 ifdr->ifdr_data = ifp->if_data;
1512 break;
1513
1514 case SIOCZIFDATA:
1515 ifdr->ifdr_data = ifp->if_data;
1516 /*
1517 * Assumes that the volatile counters that can be
1518 * zero'ed are at the end of if_data.
1519 */
1520 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1521 offsetof(struct if_data, ifi_ipackets));
1522 break;
1523
1524 case SIOCSIFMTU:
1525 {
1526 u_long oldmtu = ifp->if_mtu;
1527
1528 if (ifp->if_ioctl == NULL)
1529 return (EOPNOTSUPP);
1530 error = (*ifp->if_ioctl)(ifp, cmd, data);
1531
1532 /*
1533 * If the link MTU changed, do network layer specific procedure.
1534 */
1535 if (ifp->if_mtu != oldmtu) {
1536 #ifdef INET6
1537 nd6_setmtu(ifp);
1538 #endif
1539 }
1540 break;
1541 }
1542 case SIOCSIFPHYADDR:
1543 case SIOCDIFPHYADDR:
1544 #ifdef INET6
1545 case SIOCSIFPHYADDR_IN6:
1546 #endif
1547 case SIOCSLIFPHYADDR:
1548 case SIOCADDMULTI:
1549 case SIOCDELMULTI:
1550 case SIOCSIFMEDIA:
1551 case SIOCGIFPSRCADDR:
1552 case SIOCGIFPDSTADDR:
1553 case SIOCGLIFPHYADDR:
1554 case SIOCGIFMEDIA:
1555 if (ifp->if_ioctl == 0)
1556 return (EOPNOTSUPP);
1557 error = (*ifp->if_ioctl)(ifp, cmd, data);
1558 break;
1559
1560 case SIOCSDRVSPEC:
1561 case SIOCS80211NWID:
1562 case SIOCS80211NWKEY:
1563 case SIOCS80211POWER:
1564 case SIOCS80211BSSID:
1565 case SIOCS80211CHANNEL:
1566 default:
1567 if (so->so_proto == 0)
1568 return (EOPNOTSUPP);
1569 #ifdef COMPAT_OSOCK
1570 error = compat_ifioctl(so, cmd, data, l);
1571 #else
1572 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1573 (struct mbuf *)cmd, (struct mbuf *)data,
1574 (struct mbuf *)ifp, l));
1575 #endif
1576 break;
1577 }
1578
1579 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1580 #ifdef INET6
1581 if ((ifp->if_flags & IFF_UP) != 0) {
1582 s = splnet();
1583 in6_if_up(ifp);
1584 splx(s);
1585 }
1586 #endif
1587 }
1588
1589 return (error);
1590 }
1591
1592 /*
1593 * Return interface configuration
1594 * of system. List may be used
1595 * in later ioctl's (above) to get
1596 * other information.
1597 */
1598 /*ARGSUSED*/
1599 int
1600 ifconf(u_long cmd, caddr_t data)
1601 {
1602 struct ifconf *ifc = (struct ifconf *)data;
1603 struct ifnet *ifp;
1604 struct ifaddr *ifa;
1605 struct ifreq ifr, *ifrp;
1606 int space = ifc->ifc_len, error = 0;
1607 const int sz = (int)sizeof(ifr);
1608 int sign;
1609
1610 if ((ifrp = ifc->ifc_req) == NULL) {
1611 space = 0;
1612 sign = -1;
1613 } else {
1614 sign = 1;
1615 }
1616 IFNET_FOREACH(ifp) {
1617 (void)strncpy(ifr.ifr_name, ifp->if_xname,
1618 sizeof(ifr.ifr_name));
1619 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1620 return ENAMETOOLONG;
1621 if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1622 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1623 if (ifrp != NULL && space >= sz) {
1624 error = copyout(&ifr, ifrp, sz);
1625 if (error)
1626 break;
1627 ifrp++;
1628 }
1629 space -= sizeof(ifr) * sign;
1630 continue;
1631 }
1632
1633 for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1634 struct sockaddr *sa = ifa->ifa_addr;
1635 #ifdef COMPAT_OSOCK
1636 if (cmd == OSIOCGIFCONF) {
1637 struct osockaddr *osa =
1638 (struct osockaddr *)&ifr.ifr_addr;
1639 /*
1640 * If it does not fit, we don't bother with it
1641 */
1642 if (sa->sa_len > sizeof(*osa))
1643 continue;
1644 ifr.ifr_addr = *sa;
1645 osa->sa_family = sa->sa_family;
1646 if (ifrp != NULL && space >= sz) {
1647 error = copyout(&ifr, ifrp, sz);
1648 ifrp++;
1649 }
1650 } else
1651 #endif
1652 if (sa->sa_len <= sizeof(*sa)) {
1653 ifr.ifr_addr = *sa;
1654 if (ifrp != NULL && space >= sz) {
1655 error = copyout(&ifr, ifrp, sz);
1656 ifrp++;
1657 }
1658 } else {
1659 space -= (sa->sa_len - sizeof(*sa)) * sign;
1660 if (ifrp != NULL && space >= sz) {
1661 error = copyout(&ifr, ifrp,
1662 sizeof(ifr.ifr_name));
1663 if (error == 0) {
1664 error = copyout(sa,
1665 &ifrp->ifr_addr,
1666 sa->sa_len);
1667 }
1668 ifrp = (struct ifreq *)
1669 (sa->sa_len +
1670 (caddr_t)&ifrp->ifr_addr);
1671 }
1672 }
1673 if (error)
1674 break;
1675 space -= sz * sign;
1676 }
1677 }
1678 if (ifrp != NULL)
1679 ifc->ifc_len -= space;
1680 else
1681 ifc->ifc_len = space;
1682 return (error);
1683 }
1684
1685 /*
1686 * Queue message on interface, and start output if interface
1687 * not yet active.
1688 */
1689 int
1690 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1691 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1692 {
1693 int len = m->m_pkthdr.len;
1694 int mflags = m->m_flags;
1695 int s = splnet();
1696 int error;
1697
1698 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1699 if (error) {
1700 splx(s);
1701 return error;
1702 }
1703 ifp->if_obytes += len;
1704 if (mflags & M_MCAST)
1705 ifp->if_omcasts++;
1706 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1707 (*ifp->if_start)(ifp);
1708 splx(s);
1709 return error;
1710 }
1711
1712 /*
1713 * Queue message on interface, possibly using a second fast queue
1714 */
1715 int
1716 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1717 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1718 {
1719 int error = 0;
1720
1721 if (ifq != NULL
1722 #ifdef ALTQ
1723 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1724 #endif
1725 ) {
1726 if (IF_QFULL(ifq)) {
1727 IF_DROP(&ifp->if_snd);
1728 m_freem(m);
1729 if (error == 0)
1730 error = ENOBUFS;
1731 }
1732 else
1733 IF_ENQUEUE(ifq, m);
1734 } else
1735 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1736 if (error != 0) {
1737 ++ifp->if_oerrors;
1738 return error;
1739 }
1740
1741 return 0;
1742 }
1743
1744
1745 #if defined(INET) || defined(INET6)
1746 static void
1747 sysctl_net_ifq_setup(struct sysctllog **clog,
1748 int pf, const char *pfname,
1749 int ipn, const char *ipname,
1750 int qid, struct ifqueue *ifq)
1751 {
1752
1753 sysctl_createv(clog, 0, NULL, NULL,
1754 CTLFLAG_PERMANENT,
1755 CTLTYPE_NODE, "net", NULL,
1756 NULL, 0, NULL, 0,
1757 CTL_NET, CTL_EOL);
1758 sysctl_createv(clog, 0, NULL, NULL,
1759 CTLFLAG_PERMANENT,
1760 CTLTYPE_NODE, pfname, NULL,
1761 NULL, 0, NULL, 0,
1762 CTL_NET, pf, CTL_EOL);
1763 sysctl_createv(clog, 0, NULL, NULL,
1764 CTLFLAG_PERMANENT,
1765 CTLTYPE_NODE, ipname, NULL,
1766 NULL, 0, NULL, 0,
1767 CTL_NET, pf, ipn, CTL_EOL);
1768 sysctl_createv(clog, 0, NULL, NULL,
1769 CTLFLAG_PERMANENT,
1770 CTLTYPE_NODE, "ifq",
1771 SYSCTL_DESCR("Protocol input queue controls"),
1772 NULL, 0, NULL, 0,
1773 CTL_NET, pf, ipn, qid, CTL_EOL);
1774
1775 sysctl_createv(clog, 0, NULL, NULL,
1776 CTLFLAG_PERMANENT,
1777 CTLTYPE_INT, "len",
1778 SYSCTL_DESCR("Current input queue length"),
1779 NULL, 0, &ifq->ifq_len, 0,
1780 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1781 sysctl_createv(clog, 0, NULL, NULL,
1782 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1783 CTLTYPE_INT, "maxlen",
1784 SYSCTL_DESCR("Maximum allowed input queue length"),
1785 NULL, 0, &ifq->ifq_maxlen, 0,
1786 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1787 #ifdef notyet
1788 sysctl_createv(clog, 0, NULL, NULL,
1789 CTLFLAG_PERMANENT,
1790 CTLTYPE_INT, "peak",
1791 SYSCTL_DESCR("Highest input queue length"),
1792 NULL, 0, &ifq->ifq_peak, 0,
1793 CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1794 #endif
1795 sysctl_createv(clog, 0, NULL, NULL,
1796 CTLFLAG_PERMANENT,
1797 CTLTYPE_INT, "drops",
1798 SYSCTL_DESCR("Packets dropped due to full input queue"),
1799 NULL, 0, &ifq->ifq_drops, 0,
1800 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1801 }
1802
1803 #ifdef INET
1804 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1805 "sysctl net.inet.ip.ifq subtree setup")
1806 {
1807 extern struct ifqueue ipintrq;
1808
1809 sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1810 IPCTL_IFQ, &ipintrq);
1811 }
1812 #endif /* INET */
1813
1814 #ifdef INET6
1815 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1816 "sysctl net.inet6.ip6.ifq subtree setup")
1817 {
1818 extern struct ifqueue ip6intrq;
1819
1820 sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1821 IPV6CTL_IFQ, &ip6intrq);
1822 }
1823 #endif /* INET6 */
1824 #endif /* INET || INET6 */
1825