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