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