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