if.c revision 1.207 1 /* $NetBSD: if.c,v 1.207 2007/12/06 00:23:09 dyoung 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.207 2007/12/06 00:23:09 dyoung Exp $");
101
102 #include "opt_inet.h"
103
104 #include "opt_atalk.h"
105 #include "opt_natm.h"
106 #include "opt_pfil_hooks.h"
107
108 #include <sys/param.h>
109 #include <sys/mbuf.h>
110 #include <sys/systm.h>
111 #include <sys/callout.h>
112 #include <sys/proc.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/domain.h>
116 #include <sys/protosw.h>
117 #include <sys/kernel.h>
118 #include <sys/ioctl.h>
119 #include <sys/sysctl.h>
120 #include <sys/syslog.h>
121 #include <sys/kauth.h>
122
123 #include <net/if.h>
124 #include <net/if_dl.h>
125 #include <net/if_ether.h>
126 #include <net/if_media.h>
127 #include <net80211/ieee80211.h>
128 #include <net80211/ieee80211_ioctl.h>
129 #include <net/if_types.h>
130 #include <net/radix.h>
131 #include <net/route.h>
132 #include <net/netisr.h>
133 #ifdef NETATALK
134 #include <netatalk/at_extern.h>
135 #include <netatalk/at.h>
136 #endif
137 #include <net/pfil.h>
138
139 #ifdef INET6
140 #include <netinet/in.h>
141 #include <netinet6/in6_var.h>
142 #include <netinet6/nd6.h>
143 #endif
144
145 #include "carp.h"
146 #if NCARP > 0
147 #include <netinet/ip_carp.h>
148 #endif
149
150 #include <compat/sys/sockio.h>
151 #include <compat/sys/socket.h>
152
153 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
154 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
155
156 int ifqmaxlen = IFQ_MAXLEN;
157 callout_t if_slowtimo_ch;
158
159 int netisr; /* scheduling bits for network */
160
161 static int if_rt_walktree(struct rtentry *, void *);
162
163 static struct if_clone *if_clone_lookup(const char *, int *);
164 static int if_clone_list(struct if_clonereq *);
165
166 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
167 static int if_cloners_count;
168
169 #ifdef PFIL_HOOKS
170 struct pfil_head if_pfil; /* packet filtering hook for interfaces */
171 #endif
172
173 static void if_detach_queues(struct ifnet *, struct ifqueue *);
174
175 /*
176 * Network interface utility routines.
177 *
178 * Routines with ifa_ifwith* names take sockaddr *'s as
179 * parameters.
180 */
181 void
182 ifinit(void)
183 {
184
185 callout_init(&if_slowtimo_ch, 0);
186 if_slowtimo(NULL);
187 #ifdef PFIL_HOOKS
188 if_pfil.ph_type = PFIL_TYPE_IFNET;
189 if_pfil.ph_ifnet = NULL;
190 if (pfil_head_register(&if_pfil) != 0)
191 printf("WARNING: unable to register pfil hook\n");
192 #endif
193 }
194
195 /*
196 * Null routines used while an interface is going away. These routines
197 * just return an error.
198 */
199
200 int
201 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
202 const struct sockaddr *so, struct rtentry *rt)
203 {
204
205 return ENXIO;
206 }
207
208 void
209 if_nullinput(struct ifnet *ifp, struct mbuf *m)
210 {
211
212 /* Nothing. */
213 }
214
215 void
216 if_nullstart(struct ifnet *ifp)
217 {
218
219 /* Nothing. */
220 }
221
222 int
223 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
224 {
225
226 return ENXIO;
227 }
228
229 int
230 if_nullinit(struct ifnet *ifp)
231 {
232
233 return ENXIO;
234 }
235
236 void
237 if_nullstop(struct ifnet *ifp, int disable)
238 {
239
240 /* Nothing. */
241 }
242
243 void
244 if_nullwatchdog(struct ifnet *ifp)
245 {
246
247 /* Nothing. */
248 }
249
250 void
251 if_nulldrain(struct ifnet *ifp)
252 {
253
254 /* Nothing. */
255 }
256
257 static u_int if_index = 1;
258 struct ifnet_head ifnet;
259 size_t if_indexlim = 0;
260 struct ifaddr **ifnet_addrs = NULL;
261 struct ifnet **ifindex2ifnet = NULL;
262 struct ifnet *lo0ifp;
263
264 /*
265 * Allocate the link level name for the specified interface. This
266 * is an attachment helper. It must be called after ifp->if_addrlen
267 * is initialized, which may not be the case when if_attach() is
268 * called.
269 */
270 void
271 if_alloc_sadl(struct ifnet *ifp)
272 {
273 unsigned socksize, ifasize;
274 int addrlen, namelen;
275 struct sockaddr_dl *mask, *sdl;
276 struct ifaddr *ifa;
277
278 /*
279 * If the interface already has a link name, release it
280 * now. This is useful for interfaces that can change
281 * link types, and thus switch link names often.
282 */
283 if (ifp->if_sadl != NULL)
284 if_free_sadl(ifp);
285
286 namelen = strlen(ifp->if_xname);
287 addrlen = ifp->if_addrlen;
288 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
289 ifasize = sizeof(*ifa) + 2 * socksize;
290 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
291
292 sdl = (struct sockaddr_dl *)(ifa + 1);
293 mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
294
295 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
296 ifp->if_xname, namelen, NULL, addrlen);
297 mask->sdl_len = sockaddr_dl_measure(namelen, 0);
298 memset(&mask->sdl_data[0], 0xff, namelen);
299
300 ifnet_addrs[ifp->if_index] = ifa;
301 IFAREF(ifa);
302 ifa_insert(ifp, ifa);
303 ifa->ifa_rtrequest = link_rtrequest;
304 ifa->ifa_addr = (struct sockaddr *)sdl;
305 ifp->if_sadl = sdl;
306 ifa->ifa_netmask = (struct sockaddr *)mask;
307 }
308
309 /*
310 * Free the link level name for the specified interface. This is
311 * a detach helper. This is called from if_detach() or from
312 * link layer type specific detach functions.
313 */
314 void
315 if_free_sadl(struct ifnet *ifp)
316 {
317 struct ifaddr *ifa;
318 int s;
319
320 ifa = ifnet_addrs[ifp->if_index];
321 if (ifa == NULL) {
322 KASSERT(ifp->if_sadl == NULL);
323 return;
324 }
325
326 KASSERT(ifp->if_sadl != NULL);
327
328 s = splnet();
329 rtinit(ifa, RTM_DELETE, 0);
330 ifa_remove(ifp, ifa);
331
332 ifp->if_sadl = NULL;
333
334 ifnet_addrs[ifp->if_index] = NULL;
335 IFAFREE(ifa);
336 splx(s);
337 }
338
339 /*
340 * Attach an interface to the
341 * list of "active" interfaces.
342 */
343 void
344 if_attach(struct ifnet *ifp)
345 {
346 int indexlim = 0;
347
348 if (if_indexlim == 0) {
349 TAILQ_INIT(&ifnet);
350 if_indexlim = 8;
351 }
352 TAILQ_INIT(&ifp->if_addrlist);
353 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
354 ifp->if_index = if_index;
355 if (ifindex2ifnet == NULL)
356 if_index++;
357 else
358 while (ifp->if_index < if_indexlim &&
359 ifindex2ifnet[ifp->if_index] != NULL) {
360 ++if_index;
361 if (if_index == 0)
362 if_index = 1;
363 /*
364 * If we hit USHRT_MAX, we skip back to 0 since
365 * there are a number of places where the value
366 * of if_index or if_index itself is compared
367 * to or stored in an unsigned short. By
368 * jumping back, we won't botch those assignments
369 * or comparisons.
370 */
371 else if (if_index == USHRT_MAX) {
372 /*
373 * However, if we have to jump back to
374 * zero *twice* without finding an empty
375 * slot in ifindex2ifnet[], then there
376 * there are too many (>65535) interfaces.
377 */
378 if (indexlim++)
379 panic("too many interfaces");
380 else
381 if_index = 1;
382 }
383 ifp->if_index = if_index;
384 }
385
386 /*
387 * We have some arrays that should be indexed by if_index.
388 * since if_index will grow dynamically, they should grow too.
389 * struct ifadd **ifnet_addrs
390 * struct ifnet **ifindex2ifnet
391 */
392 if (ifnet_addrs == NULL || ifindex2ifnet == NULL ||
393 ifp->if_index >= if_indexlim) {
394 size_t m, n, oldlim;
395 void *q;
396
397 oldlim = if_indexlim;
398 while (ifp->if_index >= if_indexlim)
399 if_indexlim <<= 1;
400
401 /* grow ifnet_addrs */
402 m = oldlim * sizeof(struct ifaddr *);
403 n = if_indexlim * sizeof(struct ifaddr *);
404 q = (void *)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
405 if (ifnet_addrs != NULL) {
406 memcpy(q, ifnet_addrs, m);
407 free((void *)ifnet_addrs, M_IFADDR);
408 }
409 ifnet_addrs = (struct ifaddr **)q;
410
411 /* grow ifindex2ifnet */
412 m = oldlim * sizeof(struct ifnet *);
413 n = if_indexlim * sizeof(struct ifnet *);
414 q = (void *)malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
415 if (ifindex2ifnet != NULL) {
416 memcpy(q, (void *)ifindex2ifnet, m);
417 free((void *)ifindex2ifnet, M_IFADDR);
418 }
419 ifindex2ifnet = (struct ifnet **)q;
420 }
421
422 ifindex2ifnet[ifp->if_index] = ifp;
423
424 /*
425 * Link level name is allocated later by a separate call to
426 * if_alloc_sadl().
427 */
428
429 if (ifp->if_snd.ifq_maxlen == 0)
430 ifp->if_snd.ifq_maxlen = ifqmaxlen;
431 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
432
433 ifp->if_link_state = LINK_STATE_UNKNOWN;
434
435 ifp->if_capenable = 0;
436 ifp->if_csum_flags_tx = 0;
437 ifp->if_csum_flags_rx = 0;
438
439 #ifdef ALTQ
440 ifp->if_snd.altq_type = 0;
441 ifp->if_snd.altq_disc = NULL;
442 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
443 ifp->if_snd.altq_tbr = NULL;
444 ifp->if_snd.altq_ifp = ifp;
445 #endif
446
447 #ifdef PFIL_HOOKS
448 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
449 ifp->if_pfil.ph_ifnet = ifp;
450 if (pfil_head_register(&ifp->if_pfil) != 0)
451 printf("%s: WARNING: unable to register pfil hook\n",
452 ifp->if_xname);
453 (void)pfil_run_hooks(&if_pfil,
454 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
455 #endif
456
457 if (!STAILQ_EMPTY(&domains))
458 if_attachdomain1(ifp);
459
460 /* Announce the interface. */
461 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
462 }
463
464 void
465 if_attachdomain(void)
466 {
467 struct ifnet *ifp;
468 int s;
469
470 s = splnet();
471 IFNET_FOREACH(ifp)
472 if_attachdomain1(ifp);
473 splx(s);
474 }
475
476 void
477 if_attachdomain1(struct ifnet *ifp)
478 {
479 struct domain *dp;
480 int s;
481
482 s = splnet();
483
484 /* address family dependent data region */
485 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
486 DOMAIN_FOREACH(dp) {
487 if (dp->dom_ifattach != NULL)
488 ifp->if_afdata[dp->dom_family] =
489 (*dp->dom_ifattach)(ifp);
490 }
491
492 splx(s);
493 }
494
495 /*
496 * Deactivate an interface. This points all of the procedure
497 * handles at error stubs. May be called from interrupt context.
498 */
499 void
500 if_deactivate(struct ifnet *ifp)
501 {
502 int s;
503
504 s = splnet();
505
506 ifp->if_output = if_nulloutput;
507 ifp->if_input = if_nullinput;
508 ifp->if_start = if_nullstart;
509 ifp->if_ioctl = if_nullioctl;
510 ifp->if_init = if_nullinit;
511 ifp->if_stop = if_nullstop;
512 ifp->if_watchdog = if_nullwatchdog;
513 ifp->if_drain = if_nulldrain;
514
515 /* No more packets may be enqueued. */
516 ifp->if_snd.ifq_maxlen = 0;
517
518 splx(s);
519 }
520
521 void
522 if_purgeaddrs(struct ifnet *ifp, int family,
523 void (*purgeaddr)(struct ifaddr *))
524 {
525 struct ifaddr *ifa, *nifa;
526
527 for (ifa = IFADDR_FIRST(ifp); ifa != NULL; ifa = nifa) {
528 nifa = IFADDR_NEXT(ifa);
529 if (ifa->ifa_addr->sa_family != family)
530 continue;
531 (*purgeaddr)(ifa);
532 }
533 }
534
535 /*
536 * Detach an interface from the list of "active" interfaces,
537 * freeing any resources as we go along.
538 *
539 * NOTE: This routine must be called with a valid thread context,
540 * as it may block.
541 */
542 void
543 if_detach(struct ifnet *ifp)
544 {
545 struct socket so;
546 struct ifaddr *ifa;
547 #ifdef IFAREF_DEBUG
548 struct ifaddr *last_ifa = NULL;
549 #endif
550 struct domain *dp;
551 const struct protosw *pr;
552 int s, i, family, purged;
553
554 /*
555 * XXX It's kind of lame that we have to have the
556 * XXX socket structure...
557 */
558 memset(&so, 0, sizeof(so));
559
560 s = splnet();
561
562 /*
563 * Do an if_down() to give protocols a chance to do something.
564 */
565 if_down(ifp);
566
567 #ifdef ALTQ
568 if (ALTQ_IS_ENABLED(&ifp->if_snd))
569 altq_disable(&ifp->if_snd);
570 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
571 altq_detach(&ifp->if_snd);
572 #endif
573
574
575 #if NCARP > 0
576 /* Remove the interface from any carp group it is a part of. */
577 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
578 carp_ifdetach(ifp);
579 #endif
580
581 /*
582 * Rip all the addresses off the interface. This should make
583 * all of the routes go away.
584 *
585 * pr_usrreq calls can remove an arbitrary number of ifaddrs
586 * from the list, including our "cursor", ifa. For safety,
587 * and to honor the TAILQ abstraction, I just restart the
588 * loop after each removal. Note that the loop will exit
589 * when all of the remaining ifaddrs belong to the AF_LINK
590 * family. I am counting on the historical fact that at
591 * least one pr_usrreq in each address domain removes at
592 * least one ifaddr.
593 */
594 again:
595 IFADDR_FOREACH(ifa, ifp) {
596 family = ifa->ifa_addr->sa_family;
597 #ifdef IFAREF_DEBUG
598 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
599 ifa, family, ifa->ifa_refcnt);
600 if (last_ifa != NULL && ifa == last_ifa)
601 panic("if_detach: loop detected");
602 last_ifa = ifa;
603 #endif
604 if (family == AF_LINK)
605 continue;
606 dp = pffinddomain(family);
607 #ifdef DIAGNOSTIC
608 if (dp == NULL)
609 panic("if_detach: no domain for AF %d",
610 family);
611 #endif
612 /*
613 * XXX These PURGEIF calls are redundant with the
614 * purge-all-families calls below, but are left in for
615 * now both to make a smaller change, and to avoid
616 * unplanned interactions with clearing of
617 * ifp->if_addrlist.
618 */
619 purged = 0;
620 for (pr = dp->dom_protosw;
621 pr < dp->dom_protoswNPROTOSW; pr++) {
622 so.so_proto = pr;
623 if (pr->pr_usrreq != NULL) {
624 (void) (*pr->pr_usrreq)(&so,
625 PRU_PURGEIF, NULL, NULL,
626 (struct mbuf *) ifp, curlwp);
627 purged = 1;
628 }
629 }
630 if (purged == 0) {
631 /*
632 * XXX What's really the best thing to do
633 * XXX here? --thorpej (at) NetBSD.org
634 */
635 printf("if_detach: WARNING: AF %d not purged\n",
636 family);
637 ifa_remove(ifp, ifa);
638 }
639 goto again;
640 }
641
642 if_free_sadl(ifp);
643
644 /* Walk the routing table looking for stragglers. */
645 for (i = 0; i <= AF_MAX; i++)
646 (void)rt_walktree(i, if_rt_walktree, ifp);
647
648 DOMAIN_FOREACH(dp) {
649 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
650 (*dp->dom_ifdetach)(ifp,
651 ifp->if_afdata[dp->dom_family]);
652
653 /*
654 * One would expect multicast memberships (INET and
655 * INET6) on UDP sockets to be purged by the PURGEIF
656 * calls above, but if all addresses were removed from
657 * the interface prior to destruction, the calls will
658 * not be made (e.g. ppp, for which pppd(8) generally
659 * removes addresses before destroying the interface).
660 * Because there is no invariant that multicast
661 * memberships only exist for interfaces with IPv4
662 * addresses, we must call PURGEIF regardless of
663 * addresses. (Protocols which might store ifnet
664 * pointers are marked with PR_PURGEIF.)
665 */
666 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
667 so.so_proto = pr;
668 if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
669 (void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
670 NULL, (struct mbuf *)ifp, curlwp);
671 }
672 }
673
674 #ifdef PFIL_HOOKS
675 (void)pfil_run_hooks(&if_pfil,
676 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
677 (void)pfil_head_unregister(&ifp->if_pfil);
678 #endif
679
680 /* Announce that the interface is gone. */
681 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
682
683 ifindex2ifnet[ifp->if_index] = NULL;
684
685 TAILQ_REMOVE(&ifnet, ifp, if_list);
686
687 /*
688 * remove packets that came from ifp, from software interrupt queues.
689 */
690 DOMAIN_FOREACH(dp) {
691 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
692 if (dp->dom_ifqueues[i] == NULL)
693 break;
694 if_detach_queues(ifp, dp->dom_ifqueues[i]);
695 }
696 }
697
698 splx(s);
699 }
700
701 static void
702 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
703 {
704 struct mbuf *m, *prev, *next;
705
706 prev = NULL;
707 for (m = q->ifq_head; m != NULL; m = next) {
708 next = m->m_nextpkt;
709 #ifdef DIAGNOSTIC
710 if ((m->m_flags & M_PKTHDR) == 0) {
711 prev = m;
712 continue;
713 }
714 #endif
715 if (m->m_pkthdr.rcvif != ifp) {
716 prev = m;
717 continue;
718 }
719
720 if (prev != NULL)
721 prev->m_nextpkt = m->m_nextpkt;
722 else
723 q->ifq_head = m->m_nextpkt;
724 if (q->ifq_tail == m)
725 q->ifq_tail = prev;
726 q->ifq_len--;
727
728 m->m_nextpkt = NULL;
729 m_freem(m);
730 IF_DROP(q);
731 }
732 }
733
734 /*
735 * Callback for a radix tree walk to delete all references to an
736 * ifnet.
737 */
738 static int
739 if_rt_walktree(struct rtentry *rt, void *v)
740 {
741 struct ifnet *ifp = (struct ifnet *)v;
742 int error;
743
744 if (rt->rt_ifp != ifp)
745 return 0;
746
747 /* Delete the entry. */
748 ++rt->rt_refcnt;
749 error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
750 rt_mask(rt), rt->rt_flags, NULL);
751 KASSERT((rt->rt_flags & RTF_UP) == 0);
752 rt->rt_ifp = NULL;
753 RTFREE(rt);
754 if (error != 0)
755 printf("%s: warning: unable to delete rtentry @ %p, "
756 "error = %d\n", ifp->if_xname, rt, error);
757 return 0;
758 }
759
760 /*
761 * Create a clone network interface.
762 */
763 int
764 if_clone_create(const char *name)
765 {
766 struct if_clone *ifc;
767 int unit;
768
769 ifc = if_clone_lookup(name, &unit);
770 if (ifc == NULL)
771 return EINVAL;
772
773 if (ifunit(name) != NULL)
774 return EEXIST;
775
776 return (*ifc->ifc_create)(ifc, unit);
777 }
778
779 /*
780 * Destroy a clone network interface.
781 */
782 int
783 if_clone_destroy(const char *name)
784 {
785 struct if_clone *ifc;
786 struct ifnet *ifp;
787
788 ifc = if_clone_lookup(name, NULL);
789 if (ifc == NULL)
790 return EINVAL;
791
792 ifp = ifunit(name);
793 if (ifp == NULL)
794 return ENXIO;
795
796 if (ifc->ifc_destroy == NULL)
797 return EOPNOTSUPP;
798
799 return (*ifc->ifc_destroy)(ifp);
800 }
801
802 /*
803 * Look up a network interface cloner.
804 */
805 static struct if_clone *
806 if_clone_lookup(const char *name, int *unitp)
807 {
808 struct if_clone *ifc;
809 const char *cp;
810 int unit;
811
812 /* separate interface name from unit */
813 for (cp = name;
814 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
815 cp++)
816 continue;
817
818 if (cp == name || cp - name == IFNAMSIZ || !*cp)
819 return NULL; /* No name or unit number */
820
821 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
822 if (strlen(ifc->ifc_name) == cp - name &&
823 strncmp(name, ifc->ifc_name, cp - name) == 0)
824 break;
825 }
826
827 if (ifc == NULL)
828 return NULL;
829
830 unit = 0;
831 while (cp - name < IFNAMSIZ && *cp) {
832 if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
833 /* Bogus unit number. */
834 return NULL;
835 }
836 unit = (unit * 10) + (*cp++ - '0');
837 }
838
839 if (unitp != NULL)
840 *unitp = unit;
841 return ifc;
842 }
843
844 /*
845 * Register a network interface cloner.
846 */
847 void
848 if_clone_attach(struct if_clone *ifc)
849 {
850
851 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
852 if_cloners_count++;
853 }
854
855 /*
856 * Unregister a network interface cloner.
857 */
858 void
859 if_clone_detach(struct if_clone *ifc)
860 {
861
862 LIST_REMOVE(ifc, ifc_list);
863 if_cloners_count--;
864 }
865
866 /*
867 * Provide list of interface cloners to userspace.
868 */
869 static int
870 if_clone_list(struct if_clonereq *ifcr)
871 {
872 char outbuf[IFNAMSIZ], *dst;
873 struct if_clone *ifc;
874 int count, error = 0;
875
876 ifcr->ifcr_total = if_cloners_count;
877 if ((dst = ifcr->ifcr_buffer) == NULL) {
878 /* Just asking how many there are. */
879 return 0;
880 }
881
882 if (ifcr->ifcr_count < 0)
883 return EINVAL;
884
885 count = (if_cloners_count < ifcr->ifcr_count) ?
886 if_cloners_count : ifcr->ifcr_count;
887
888 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
889 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
890 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
891 if (outbuf[sizeof(outbuf) - 1] != '\0')
892 return ENAMETOOLONG;
893 error = copyout(outbuf, dst, sizeof(outbuf));
894 if (error != 0)
895 break;
896 }
897
898 return error;
899 }
900
901 void
902 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
903 {
904 ifa->ifa_ifp = ifp;
905 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
906 IFAREF(ifa);
907 }
908
909 void
910 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
911 {
912 KASSERT(ifa->ifa_ifp == ifp);
913 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
914 IFAFREE(ifa);
915 }
916
917 static inline int
918 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
919 {
920 return sockaddr_cmp(sa1, sa2) == 0;
921 }
922
923 /*
924 * Locate an interface based on a complete address.
925 */
926 /*ARGSUSED*/
927 struct ifaddr *
928 ifa_ifwithaddr(const struct sockaddr *addr)
929 {
930 struct ifnet *ifp;
931 struct ifaddr *ifa;
932
933 IFNET_FOREACH(ifp) {
934 if (ifp->if_output == if_nulloutput)
935 continue;
936 IFADDR_FOREACH(ifa, ifp) {
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 IFADDR_FOREACH(ifa, ifp) {
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 = satocsdl(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 IFADDR_FOREACH(ifa, ifp) {
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 IFADDR_FOREACH(ifa, ifp) {
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 IFADDR_FOREACH(ifa, ifp) {
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 const 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_getkey(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 IFADDR_FOREACH(ifa, ifp)
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 IFADDR_FOREACH(ifa, ifp)
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 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1340 u_long ocmd = cmd;
1341 #endif
1342 short oif_flags;
1343 #ifdef COMPAT_OIFREQ
1344 struct ifreq ifrb;
1345 struct oifreq *oifr = NULL;
1346 #endif
1347
1348 switch (cmd) {
1349 #ifdef COMPAT_OIFREQ
1350 case OSIOCGIFCONF:
1351 case OOSIOCGIFCONF:
1352 return compat_ifconf(cmd, data);
1353 #endif
1354 case SIOCGIFCONF:
1355 return ifconf(cmd, data);
1356 }
1357
1358 #ifdef COMPAT_OIFREQ
1359 cmd = compat_cvtcmd(cmd);
1360 if (cmd != ocmd) {
1361 oifr = data;
1362 data = ifr = &ifrb;
1363 ifreqo2n(oifr, ifr);
1364 } else
1365 #endif
1366 ifr = data;
1367 ifcr = data;
1368 ifdr = data;
1369
1370 ifp = ifunit(ifr->ifr_name);
1371
1372 switch (cmd) {
1373 case SIOCIFCREATE:
1374 case SIOCIFDESTROY:
1375 if (l != NULL) {
1376 error = kauth_authorize_network(l->l_cred,
1377 KAUTH_NETWORK_INTERFACE,
1378 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1379 (void *)cmd, NULL);
1380 if (error != 0)
1381 return error;
1382 }
1383 return (cmd == SIOCIFCREATE) ?
1384 if_clone_create(ifr->ifr_name) :
1385 if_clone_destroy(ifr->ifr_name);
1386
1387 case SIOCIFGCLONERS:
1388 return if_clone_list((struct if_clonereq *)data);
1389 }
1390
1391 if (ifp == NULL)
1392 return ENXIO;
1393
1394 switch (cmd) {
1395 case SIOCSIFFLAGS:
1396 case SIOCSIFCAP:
1397 case SIOCSIFMETRIC:
1398 case SIOCZIFDATA:
1399 case SIOCSIFMTU:
1400 case SIOCSIFPHYADDR:
1401 case SIOCDIFPHYADDR:
1402 #ifdef INET6
1403 case SIOCSIFPHYADDR_IN6:
1404 #endif
1405 case SIOCSLIFPHYADDR:
1406 case SIOCADDMULTI:
1407 case SIOCDELMULTI:
1408 case SIOCSIFMEDIA:
1409 case SIOCSDRVSPEC:
1410 case SIOCG80211:
1411 case SIOCS80211:
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 case SIOCG80211:
1582 case SIOCS80211:
1583 case SIOCS80211NWID:
1584 case SIOCS80211NWKEY:
1585 case SIOCS80211POWER:
1586 case SIOCS80211BSSID:
1587 case SIOCS80211CHANNEL:
1588 if (ifp->if_ioctl == NULL)
1589 return EOPNOTSUPP;
1590 error = (*ifp->if_ioctl)(ifp, cmd, data);
1591 break;
1592
1593 case SIOCSDRVSPEC:
1594 default:
1595 if (so->so_proto == NULL)
1596 return EOPNOTSUPP;
1597 #ifdef COMPAT_OSOCK
1598 error = compat_ifioctl(so, ocmd, cmd, data, l);
1599 #else
1600 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1601 (struct mbuf *)cmd, (struct mbuf *)data,
1602 (struct mbuf *)ifp, l));
1603 #endif
1604 break;
1605 }
1606
1607 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1608 #ifdef INET6
1609 if ((ifp->if_flags & IFF_UP) != 0) {
1610 s = splnet();
1611 in6_if_up(ifp);
1612 splx(s);
1613 }
1614 #endif
1615 }
1616 #ifdef COMPAT_OIFREQ
1617 if (cmd != ocmd)
1618 ifreqn2o(oifr, ifr);
1619 #endif
1620
1621 return error;
1622 }
1623
1624 /*
1625 * Return interface configuration
1626 * of system. List may be used
1627 * in later ioctl's (above) to get
1628 * other information.
1629 *
1630 * Each record is a struct ifreq. Before the addition of
1631 * sockaddr_storage, the API rule was that sockaddr flavors that did
1632 * not fit would extend beyond the struct ifreq, with the next struct
1633 * ifreq starting sa_len beyond the struct sockaddr. Because the
1634 * union in struct ifreq includes struct sockaddr_storage, every kind
1635 * of sockaddr must fit. Thus, there are no longer any overlength
1636 * records.
1637 *
1638 * Records are added to the user buffer if they fit, and ifc_len is
1639 * adjusted to the length that was written. Thus, the user is only
1640 * assured of getting the complete list if ifc_len on return is at
1641 * least sizeof(struct ifreq) less than it was on entry.
1642 *
1643 * If the user buffer pointer is NULL, this routine copies no data and
1644 * returns the amount of space that would be needed.
1645 *
1646 * Invariants:
1647 * ifrp points to the next part of the user's buffer to be used. If
1648 * ifrp != NULL, space holds the number of bytes remaining that we may
1649 * write at ifrp. Otherwise, space holds the number of bytes that
1650 * would have been written had there been adequate space.
1651 */
1652 /*ARGSUSED*/
1653 int
1654 ifconf(u_long cmd, void *data)
1655 {
1656 struct ifconf *ifc = (struct ifconf *)data;
1657 struct ifnet *ifp;
1658 struct ifaddr *ifa;
1659 struct ifreq ifr, *ifrp;
1660 int space, error = 0;
1661 const int sz = (int)sizeof(struct ifreq);
1662
1663 if ((ifrp = ifc->ifc_req) == NULL)
1664 space = 0;
1665 else
1666 space = ifc->ifc_len;
1667 IFNET_FOREACH(ifp) {
1668 (void)strncpy(ifr.ifr_name, ifp->if_xname,
1669 sizeof(ifr.ifr_name));
1670 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1671 return ENAMETOOLONG;
1672 if (IFADDR_EMPTY(ifp)) {
1673 /* Interface with no addresses - send zero sockaddr. */
1674 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1675 if (ifrp != NULL)
1676 {
1677 if (space >= sz) {
1678 error = copyout(&ifr, ifrp, sz);
1679 if (error != 0)
1680 return (error);
1681 ifrp++; space -= sz;
1682 }
1683 }
1684 else
1685 space += sz;
1686 continue;
1687 }
1688
1689 IFADDR_FOREACH(ifa, ifp) {
1690 struct sockaddr *sa = ifa->ifa_addr;
1691 /* all sockaddrs must fit in sockaddr_storage */
1692 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
1693
1694 if (ifrp != NULL)
1695 {
1696 memcpy(&ifr.ifr_space, sa, sa->sa_len);
1697 if (space >= sz) {
1698 error = copyout(&ifr, ifrp, sz);
1699 if (error != 0)
1700 return (error);
1701 ifrp++; space -= sz;
1702 }
1703 }
1704 else
1705 space += sz;
1706 }
1707 }
1708 if (ifrp != NULL)
1709 {
1710 KASSERT(0 <= space && space <= ifc->ifc_len);
1711 ifc->ifc_len -= space;
1712 }
1713 else
1714 {
1715 KASSERT(space >= 0);
1716 ifc->ifc_len = space;
1717 }
1718 return (0);
1719 }
1720
1721 int
1722 ifreq_setaddr(const u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
1723 {
1724 uint8_t len;
1725 u_long ncmd;
1726 const uint8_t osockspace = sizeof(ifr->ifr_addr);
1727 const uint8_t sockspace = sizeof(ifr->ifr_ifru.ifru_space);
1728
1729 #ifdef INET6
1730 if (cmd == SIOCGIFPSRCADDR_IN6 || cmd == SIOCGIFPDSTADDR_IN6)
1731 len = MIN(sizeof(struct sockaddr_in6), sa->sa_len);
1732 else
1733 #endif /* INET6 */
1734 if ((ncmd = compat_cvtcmd(cmd)) != cmd)
1735 len = MIN(osockspace, sa->sa_len);
1736 else
1737 len = MIN(sockspace, sa->sa_len);
1738 if (len < sa->sa_len)
1739 return EFBIG;
1740 sockaddr_copy(&ifr->ifr_addr, len, sa);
1741 return 0;
1742 }
1743
1744 /*
1745 * Queue message on interface, and start output if interface
1746 * not yet active.
1747 */
1748 int
1749 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1750 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1751 {
1752 int len = m->m_pkthdr.len;
1753 int mflags = m->m_flags;
1754 int s = splnet();
1755 int error;
1756
1757 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1758 if (error != 0)
1759 goto out;
1760 ifp->if_obytes += len;
1761 if (mflags & M_MCAST)
1762 ifp->if_omcasts++;
1763 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1764 (*ifp->if_start)(ifp);
1765 out:
1766 splx(s);
1767 return error;
1768 }
1769
1770 /*
1771 * Queue message on interface, possibly using a second fast queue
1772 */
1773 int
1774 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1775 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1776 {
1777 int error = 0;
1778
1779 if (ifq != NULL
1780 #ifdef ALTQ
1781 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1782 #endif
1783 ) {
1784 if (IF_QFULL(ifq)) {
1785 IF_DROP(&ifp->if_snd);
1786 m_freem(m);
1787 if (error == 0)
1788 error = ENOBUFS;
1789 } else
1790 IF_ENQUEUE(ifq, m);
1791 } else
1792 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1793 if (error != 0) {
1794 ++ifp->if_oerrors;
1795 return error;
1796 }
1797 return 0;
1798 }
1799
1800
1801 #if defined(INET) || defined(INET6)
1802 static void
1803 sysctl_net_ifq_setup(struct sysctllog **clog,
1804 int pf, const char *pfname,
1805 int ipn, const char *ipname,
1806 int qid, struct ifqueue *ifq)
1807 {
1808
1809 sysctl_createv(clog, 0, NULL, NULL,
1810 CTLFLAG_PERMANENT,
1811 CTLTYPE_NODE, "net", NULL,
1812 NULL, 0, NULL, 0,
1813 CTL_NET, CTL_EOL);
1814 sysctl_createv(clog, 0, NULL, NULL,
1815 CTLFLAG_PERMANENT,
1816 CTLTYPE_NODE, pfname, NULL,
1817 NULL, 0, NULL, 0,
1818 CTL_NET, pf, CTL_EOL);
1819 sysctl_createv(clog, 0, NULL, NULL,
1820 CTLFLAG_PERMANENT,
1821 CTLTYPE_NODE, ipname, NULL,
1822 NULL, 0, NULL, 0,
1823 CTL_NET, pf, ipn, CTL_EOL);
1824 sysctl_createv(clog, 0, NULL, NULL,
1825 CTLFLAG_PERMANENT,
1826 CTLTYPE_NODE, "ifq",
1827 SYSCTL_DESCR("Protocol input queue controls"),
1828 NULL, 0, NULL, 0,
1829 CTL_NET, pf, ipn, qid, CTL_EOL);
1830
1831 sysctl_createv(clog, 0, NULL, NULL,
1832 CTLFLAG_PERMANENT,
1833 CTLTYPE_INT, "len",
1834 SYSCTL_DESCR("Current input queue length"),
1835 NULL, 0, &ifq->ifq_len, 0,
1836 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1837 sysctl_createv(clog, 0, NULL, NULL,
1838 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1839 CTLTYPE_INT, "maxlen",
1840 SYSCTL_DESCR("Maximum allowed input queue length"),
1841 NULL, 0, &ifq->ifq_maxlen, 0,
1842 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1843 #ifdef notyet
1844 sysctl_createv(clog, 0, NULL, NULL,
1845 CTLFLAG_PERMANENT,
1846 CTLTYPE_INT, "peak",
1847 SYSCTL_DESCR("Highest input queue length"),
1848 NULL, 0, &ifq->ifq_peak, 0,
1849 CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1850 #endif
1851 sysctl_createv(clog, 0, NULL, NULL,
1852 CTLFLAG_PERMANENT,
1853 CTLTYPE_INT, "drops",
1854 SYSCTL_DESCR("Packets dropped due to full input queue"),
1855 NULL, 0, &ifq->ifq_drops, 0,
1856 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1857 }
1858
1859 #ifdef INET
1860 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1861 "sysctl net.inet.ip.ifq subtree setup")
1862 {
1863 extern struct ifqueue ipintrq;
1864
1865 sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1866 IPCTL_IFQ, &ipintrq);
1867 }
1868 #endif /* INET */
1869
1870 #ifdef INET6
1871 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1872 "sysctl net.inet6.ip6.ifq subtree setup")
1873 {
1874 extern struct ifqueue ip6intrq;
1875
1876 sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1877 IPV6CTL_IFQ, &ip6intrq);
1878 }
1879 #endif /* INET6 */
1880 #endif /* INET || INET6 */
1881