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