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