if.c revision 1.124 1 /* $NetBSD: if.c,v 1.124 2003/07/06 07:54:43 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William 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.124 2003/07/06 07:54:43 dyoung Exp $");
105
106 #include "opt_inet.h"
107
108 #include "opt_compat_linux.h"
109 #include "opt_compat_svr4.h"
110 #include "opt_compat_ultrix.h"
111 #include "opt_compat_43.h"
112 #include "opt_atalk.h"
113 #include "opt_ccitt.h"
114 #include "opt_natm.h"
115 #include "opt_pfil_hooks.h"
116
117 #include <sys/param.h>
118 #include <sys/mbuf.h>
119 #include <sys/systm.h>
120 #include <sys/callout.h>
121 #include <sys/proc.h>
122 #include <sys/socket.h>
123 #include <sys/socketvar.h>
124 #include <sys/domain.h>
125 #include <sys/protosw.h>
126 #include <sys/kernel.h>
127 #include <sys/ioctl.h>
128
129 #include <net/if.h>
130 #include <net/if_dl.h>
131 #include <net/if_ether.h>
132 #include <net/if_media.h>
133 #include <net/if_ieee80211.h>
134 #include <net/if_types.h>
135 #include <net/radix.h>
136 #include <net/route.h>
137 #include <net/netisr.h>
138 #ifdef NETATALK
139 #include <netatalk/at_extern.h>
140 #include <netatalk/at.h>
141 #endif
142
143 #ifdef INET6
144 #include <netinet/in.h>
145 #include <netinet6/in6_var.h>
146 #include <netinet6/nd6.h>
147 #endif
148
149 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
150 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
151
152 int ifqmaxlen = IFQ_MAXLEN;
153 struct callout if_slowtimo_ch;
154
155 int netisr; /* scheduling bits for network */
156
157 int if_rt_walktree __P((struct radix_node *, void *));
158
159 struct if_clone *if_clone_lookup __P((const char *, int *));
160 int if_clone_list __P((struct if_clonereq *));
161
162 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
163 int if_cloners_count;
164
165 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
166 defined(ISO) || defined(CCITT) || defined(NATM)
167 static void if_detach_queues __P((struct ifnet *, struct ifqueue *));
168 #endif
169
170 /*
171 * Network interface utility routines.
172 *
173 * Routines with ifa_ifwith* names take sockaddr *'s as
174 * parameters.
175 */
176 void
177 ifinit()
178 {
179
180 callout_init(&if_slowtimo_ch);
181 if_slowtimo(NULL);
182 }
183
184 /*
185 * Null routines used while an interface is going away. These routines
186 * just return an error.
187 */
188
189 int
190 if_nulloutput(ifp, m, so, rt)
191 struct ifnet *ifp;
192 struct mbuf *m;
193 struct sockaddr *so;
194 struct rtentry *rt;
195 {
196
197 return (ENXIO);
198 }
199
200 void
201 if_nullinput(ifp, m)
202 struct ifnet *ifp;
203 struct mbuf *m;
204 {
205
206 /* Nothing. */
207 }
208
209 void
210 if_nullstart(ifp)
211 struct ifnet *ifp;
212 {
213
214 /* Nothing. */
215 }
216
217 int
218 if_nullioctl(ifp, cmd, data)
219 struct ifnet *ifp;
220 u_long cmd;
221 caddr_t data;
222 {
223
224 return (ENXIO);
225 }
226
227 int
228 if_nullinit(ifp)
229 struct ifnet *ifp;
230 {
231
232 return (ENXIO);
233 }
234
235 void
236 if_nullstop(ifp, disable)
237 struct ifnet *ifp;
238 int disable;
239 {
240
241 /* Nothing. */
242 }
243
244 void
245 if_nullwatchdog(ifp)
246 struct ifnet *ifp;
247 {
248
249 /* Nothing. */
250 }
251
252 void
253 if_nulldrain(ifp)
254 struct ifnet *ifp;
255 {
256
257 /* Nothing. */
258 }
259
260 u_int if_index = 1;
261 struct ifnet_head ifnet;
262 struct ifaddr **ifnet_addrs = NULL;
263 struct ifnet **ifindex2ifnet = NULL;
264
265 /*
266 * Allocate the link level name for the specified interface. This
267 * is an attachment helper. It must be called after ifp->if_addrlen
268 * is initialized, which may not be the case when if_attach() is
269 * called.
270 */
271 void
272 if_alloc_sadl(struct ifnet *ifp)
273 {
274 unsigned socksize, ifasize;
275 int namelen, masklen;
276 struct sockaddr_dl *sdl;
277 struct ifaddr *ifa;
278
279 /*
280 * If the interface already has a link name, release it
281 * now. This is useful for interfaces that can change
282 * link types, and thus switch link names often.
283 */
284 if (ifp->if_sadl != NULL)
285 if_free_sadl(ifp);
286
287 namelen = strlen(ifp->if_xname);
288 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
289 socksize = masklen + ifp->if_addrlen;
290 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
291 if (socksize < sizeof(*sdl))
292 socksize = sizeof(*sdl);
293 socksize = ROUNDUP(socksize);
294 ifasize = sizeof(*ifa) + 2 * socksize;
295 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
296 memset((caddr_t)ifa, 0, ifasize);
297 sdl = (struct sockaddr_dl *)(ifa + 1);
298 sdl->sdl_len = socksize;
299 sdl->sdl_family = AF_LINK;
300 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
301 sdl->sdl_nlen = namelen;
302 sdl->sdl_alen = ifp->if_addrlen;
303 sdl->sdl_index = ifp->if_index;
304 sdl->sdl_type = ifp->if_type;
305 ifnet_addrs[ifp->if_index] = ifa;
306 IFAREF(ifa);
307 ifa->ifa_ifp = ifp;
308 ifa->ifa_rtrequest = link_rtrequest;
309 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
310 IFAREF(ifa);
311 ifa->ifa_addr = (struct sockaddr *)sdl;
312 ifp->if_sadl = sdl;
313 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
314 ifa->ifa_netmask = (struct sockaddr *)sdl;
315 sdl->sdl_len = masklen;
316 while (namelen != 0)
317 sdl->sdl_data[--namelen] = 0xff;
318 }
319
320 /*
321 * Free the link level name for the specified interface. This is
322 * a detach helper. This is called from if_detach() or from
323 * link layer type specific detach functions.
324 */
325 void
326 if_free_sadl(struct ifnet *ifp)
327 {
328 struct ifaddr *ifa;
329 int s;
330
331 ifa = ifnet_addrs[ifp->if_index];
332 if (ifa == NULL) {
333 KASSERT(ifp->if_sadl == NULL);
334 return;
335 }
336
337 KASSERT(ifp->if_sadl != NULL);
338
339 s = splnet();
340 rtinit(ifa, RTM_DELETE, 0);
341 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
342 IFAFREE(ifa);
343
344 ifp->if_sadl = NULL;
345
346 ifnet_addrs[ifp->if_index] = NULL;
347 IFAFREE(ifa);
348 splx(s);
349 }
350
351 /*
352 * Attach an interface to the
353 * list of "active" interfaces.
354 */
355 void
356 if_attach(ifp)
357 struct ifnet *ifp;
358 {
359 static size_t if_indexlim = 0;
360 int indexlim = 0;
361
362 if (if_indexlim == 0) {
363 TAILQ_INIT(&ifnet);
364 if_indexlim = 8;
365 }
366 TAILQ_INIT(&ifp->if_addrlist);
367 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
368 ifp->if_index = if_index;
369 if (ifindex2ifnet == 0)
370 if_index++;
371 else
372 while (ifindex2ifnet[ifp->if_index] != NULL) {
373 ++if_index;
374 if (if_index == 0)
375 if_index = 1;
376 /*
377 * If we hit USHRT_MAX, we skip back to 0 since
378 * there are a number of places where the value
379 * of if_index or if_index itself is compared
380 * to or stored in an unsigned short. By
381 * jumping back, we won't botch those assignments
382 * or comparisons.
383 */
384 else if (if_index == USHRT_MAX) {
385 /*
386 * However, if we have to jump back to
387 * zero *twice* without finding an empty
388 * slot in ifindex2ifnet[], then there
389 * there are too many (>65535) interfaces.
390 */
391 if (indexlim++)
392 panic("too many interfaces");
393 else
394 if_index = 1;
395 }
396 ifp->if_index = if_index;
397 }
398
399 /*
400 * We have some arrays that should be indexed by if_index.
401 * since if_index will grow dynamically, they should grow too.
402 * struct ifadd **ifnet_addrs
403 * struct ifnet **ifindex2ifnet
404 */
405 if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
406 ifp->if_index >= if_indexlim) {
407 size_t n;
408 caddr_t q;
409
410 while (ifp->if_index >= if_indexlim)
411 if_indexlim <<= 1;
412
413 /* grow ifnet_addrs */
414 n = if_indexlim * sizeof(struct ifaddr *);
415 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
416 memset(q, 0, n);
417 if (ifnet_addrs) {
418 bcopy((caddr_t)ifnet_addrs, q, n/2);
419 free((caddr_t)ifnet_addrs, M_IFADDR);
420 }
421 ifnet_addrs = (struct ifaddr **)q;
422
423 /* grow ifindex2ifnet */
424 n = if_indexlim * sizeof(struct ifnet *);
425 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
426 memset(q, 0, n);
427 if (ifindex2ifnet) {
428 bcopy((caddr_t)ifindex2ifnet, q, n/2);
429 free((caddr_t)ifindex2ifnet, M_IFADDR);
430 }
431 ifindex2ifnet = (struct ifnet **)q;
432 }
433
434 ifindex2ifnet[ifp->if_index] = ifp;
435
436 /*
437 * Link level name is allocated later by a separate call to
438 * if_alloc_sadl().
439 */
440
441 if (ifp->if_snd.ifq_maxlen == 0)
442 ifp->if_snd.ifq_maxlen = ifqmaxlen;
443 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
444
445 ifp->if_link_state = LINK_STATE_UNKNOWN;
446
447 ifp->if_capenable = 0;
448 ifp->if_csum_flags_tx = 0;
449 ifp->if_csum_flags_rx = 0;
450
451 #ifdef ALTQ
452 ifp->if_snd.altq_type = 0;
453 ifp->if_snd.altq_disc = NULL;
454 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
455 ifp->if_snd.altq_tbr = NULL;
456 ifp->if_snd.altq_ifp = ifp;
457 #endif
458
459 #ifdef PFIL_HOOKS
460 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
461 ifp->if_pfil.ph_ifnet = ifp;
462 if (pfil_head_register(&ifp->if_pfil) != 0)
463 printf("%s: WARNING: unable to register pfil hook\n",
464 ifp->if_xname);
465 #endif
466
467 if (domains)
468 if_attachdomain1(ifp);
469
470 /* Announce the interface. */
471 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
472 }
473
474 void
475 if_attachdomain()
476 {
477 struct ifnet *ifp;
478 int s;
479
480 s = splnet();
481 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
482 if_attachdomain1(ifp);
483 splx(s);
484 }
485
486 void
487 if_attachdomain1(ifp)
488 struct ifnet *ifp;
489 {
490 struct domain *dp;
491 int s;
492
493 s = splnet();
494
495 /* address family dependent data region */
496 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
497 for (dp = domains; dp; dp = dp->dom_next) {
498 if (dp->dom_ifattach)
499 ifp->if_afdata[dp->dom_family] =
500 (*dp->dom_ifattach)(ifp);
501 }
502
503 splx(s);
504 }
505
506 /*
507 * Deactivate an interface. This points all of the procedure
508 * handles at error stubs. May be called from interrupt context.
509 */
510 void
511 if_deactivate(ifp)
512 struct ifnet *ifp;
513 {
514 int s;
515
516 s = splnet();
517
518 ifp->if_output = if_nulloutput;
519 ifp->if_input = if_nullinput;
520 ifp->if_start = if_nullstart;
521 ifp->if_ioctl = if_nullioctl;
522 ifp->if_init = if_nullinit;
523 ifp->if_stop = if_nullstop;
524 ifp->if_watchdog = if_nullwatchdog;
525 ifp->if_drain = if_nulldrain;
526
527 /* No more packets may be enqueued. */
528 ifp->if_snd.ifq_maxlen = 0;
529
530 splx(s);
531 }
532
533 /*
534 * Detach an interface from the list of "active" interfaces,
535 * freeing any resources as we go along.
536 *
537 * NOTE: This routine must be called with a valid thread context,
538 * as it may block.
539 */
540 void
541 if_detach(ifp)
542 struct ifnet *ifp;
543 {
544 struct socket so;
545 struct ifaddr *ifa, *next;
546 #ifdef IFAREF_DEBUG
547 struct ifaddr *last_ifa = NULL;
548 #endif
549 struct domain *dp;
550 struct protosw *pr;
551 struct radix_node_head *rnh;
552 int s, i, family, purged;
553
554 /*
555 * XXX It's kind of lame that we have to have the
556 * XXX socket structure...
557 */
558 memset(&so, 0, sizeof(so));
559
560 s = splnet();
561
562 /*
563 * Do an if_down() to give protocols a chance to do something.
564 */
565 if_down(ifp);
566
567 #ifdef ALTQ
568 if (ALTQ_IS_ENABLED(&ifp->if_snd))
569 altq_disable(&ifp->if_snd);
570 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
571 altq_detach(&ifp->if_snd);
572 #endif
573
574 #ifdef PFIL_HOOKS
575 (void) pfil_head_unregister(&ifp->if_pfil);
576 #endif
577
578 /*
579 * Rip all the addresses off the interface. This should make
580 * all of the routes go away.
581 */
582 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa; ifa = next) {
583 next = TAILQ_NEXT(ifa, ifa_list);
584 family = ifa->ifa_addr->sa_family;
585 #ifdef IFAREF_DEBUG
586 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
587 ifa, family, ifa->ifa_refcnt);
588 if (last_ifa != NULL && ifa == last_ifa)
589 panic("if_detach: loop detected");
590 last_ifa = ifa;
591 #endif
592 if (family == AF_LINK)
593 continue;
594 dp = pffinddomain(family);
595 #ifdef DIAGNOSTIC
596 if (dp == NULL)
597 panic("if_detach: no domain for AF %d",
598 family);
599 #endif
600 purged = 0;
601 for (pr = dp->dom_protosw;
602 pr < dp->dom_protoswNPROTOSW; pr++) {
603 so.so_proto = pr;
604 if (pr->pr_usrreq != NULL) {
605 (void) (*pr->pr_usrreq)(&so,
606 PRU_PURGEIF, NULL, NULL,
607 (struct mbuf *) ifp, curproc);
608 purged = 1;
609 }
610 }
611 if (purged == 0) {
612 /*
613 * XXX What's really the best thing to do
614 * XXX here? --thorpej (at) netbsd.org
615 */
616 printf("if_detach: WARNING: AF %d not purged\n",
617 family);
618 }
619 }
620
621 if_free_sadl(ifp);
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 (/*CONSTCOND*/ 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 case SIOCS80211BSSID:
1517 case SIOCS80211CHANNEL:
1518 /* XXX: need to pass proc pointer through to driver... */
1519 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1520 return (error);
1521 /* FALLTHROUGH */
1522 default:
1523 if (so->so_proto == 0)
1524 return (EOPNOTSUPP);
1525 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(COMPAT_ULTRIX) && !defined(LKM)
1526 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1527 (struct mbuf *)cmd, (struct mbuf *)data,
1528 (struct mbuf *)ifp, p));
1529 #else
1530 {
1531 int ocmd = cmd;
1532
1533 switch (cmd) {
1534
1535 case SIOCSIFADDR:
1536 case SIOCSIFDSTADDR:
1537 case SIOCSIFBRDADDR:
1538 case SIOCSIFNETMASK:
1539 #if BYTE_ORDER != BIG_ENDIAN
1540 if (ifr->ifr_addr.sa_family == 0 &&
1541 ifr->ifr_addr.sa_len < 16) {
1542 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1543 ifr->ifr_addr.sa_len = 16;
1544 }
1545 #else
1546 if (ifr->ifr_addr.sa_len == 0)
1547 ifr->ifr_addr.sa_len = 16;
1548 #endif
1549 break;
1550
1551 case OSIOCGIFADDR:
1552 cmd = SIOCGIFADDR;
1553 break;
1554
1555 case OSIOCGIFDSTADDR:
1556 cmd = SIOCGIFDSTADDR;
1557 break;
1558
1559 case OSIOCGIFBRDADDR:
1560 cmd = SIOCGIFBRDADDR;
1561 break;
1562
1563 case OSIOCGIFNETMASK:
1564 cmd = SIOCGIFNETMASK;
1565 }
1566
1567 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1568 (struct mbuf *)cmd, (struct mbuf *)data,
1569 (struct mbuf *)ifp, p));
1570
1571 switch (ocmd) {
1572 case OSIOCGIFADDR:
1573 case OSIOCGIFDSTADDR:
1574 case OSIOCGIFBRDADDR:
1575 case OSIOCGIFNETMASK:
1576 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1577 }
1578 }
1579 #endif /* COMPAT_43 */
1580 break;
1581 }
1582
1583 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1584 #ifdef INET6
1585 if ((ifp->if_flags & IFF_UP) != 0) {
1586 s = splnet();
1587 in6_if_up(ifp);
1588 splx(s);
1589 }
1590 #endif
1591 }
1592
1593 return (error);
1594 }
1595
1596 /*
1597 * Return interface configuration
1598 * of system. List may be used
1599 * in later ioctl's (above) to get
1600 * other information.
1601 */
1602 /*ARGSUSED*/
1603 int
1604 ifconf(cmd, data)
1605 u_long cmd;
1606 caddr_t data;
1607 {
1608 struct ifconf *ifc = (struct ifconf *)data;
1609 struct ifnet *ifp;
1610 struct ifaddr *ifa;
1611 struct ifreq ifr, *ifrp;
1612 int space = ifc->ifc_len, error = 0;
1613
1614 ifrp = ifc->ifc_req;
1615 TAILQ_FOREACH(ifp, &ifnet, if_list) {
1616 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1617 if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1618 memset((caddr_t)&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1619 if (space >= (int)sizeof (ifr)) {
1620 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1621 sizeof(ifr));
1622 if (error)
1623 break;
1624 }
1625 space -= sizeof (ifr), ifrp++;
1626 } else
1627 for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1628 struct sockaddr *sa = ifa->ifa_addr;
1629 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX)
1630 if (cmd == OSIOCGIFCONF) {
1631 struct osockaddr *osa =
1632 (struct osockaddr *)&ifr.ifr_addr;
1633 ifr.ifr_addr = *sa;
1634 osa->sa_family = sa->sa_family;
1635 if (space >= (int)sizeof (ifr)) {
1636 error = copyout((caddr_t)&ifr,
1637 (caddr_t)ifrp,
1638 sizeof (ifr));
1639 ifrp++;
1640 }
1641 } else
1642 #endif
1643 if (sa->sa_len <= sizeof(*sa)) {
1644 ifr.ifr_addr = *sa;
1645 if (space >= (int)sizeof (ifr)) {
1646 error = copyout((caddr_t)&ifr,
1647 (caddr_t)ifrp,
1648 sizeof (ifr));
1649 ifrp++;
1650 }
1651 } else {
1652 space -= sa->sa_len - sizeof(*sa);
1653 if (space >= (int)sizeof (ifr)) {
1654 error = copyout((caddr_t)&ifr,
1655 (caddr_t)ifrp,
1656 sizeof (ifr.ifr_name));
1657 if (error == 0) {
1658 error = copyout((caddr_t)sa,
1659 (caddr_t)&ifrp->ifr_addr,
1660 sa->sa_len);
1661 }
1662 ifrp = (struct ifreq *)
1663 (sa->sa_len +
1664 (caddr_t)&ifrp->ifr_addr);
1665 }
1666 }
1667 if (error)
1668 break;
1669 space -= sizeof (ifr);
1670 }
1671 }
1672 ifc->ifc_len -= space;
1673 return (error);
1674 }
1675