if.c revision 1.109 1 /* $NetBSD: if.c,v 1.109 2002/06/08 11:54:24 itojun 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.109 2002/06/08 11:54:24 itojun 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 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 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
472 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
473 if_attachdomain1(ifp);
474 }
475
476 void
477 if_attachdomain1(ifp)
478 struct ifnet *ifp;
479 {
480 struct domain *dp;
481 int s;
482
483 s = splnet();
484
485 /* address family dependent data region */
486 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
487 for (dp = domains; dp; dp = dp->dom_next) {
488 if (dp->dom_ifattach)
489 ifp->if_afdata[dp->dom_family] =
490 (*dp->dom_ifattach)(ifp);
491 }
492
493 splx(s);
494 }
495
496 /*
497 * Deactivate an interface. This points all of the procedure
498 * handles at error stubs. May be called from interrupt context.
499 */
500 void
501 if_deactivate(ifp)
502 struct ifnet *ifp;
503 {
504 int s;
505
506 s = splnet();
507
508 ifp->if_output = if_nulloutput;
509 ifp->if_input = if_nullinput;
510 ifp->if_start = if_nullstart;
511 ifp->if_ioctl = if_nullioctl;
512 ifp->if_init = if_nullinit;
513 ifp->if_stop = if_nullstop;
514 ifp->if_watchdog = if_nullwatchdog;
515 ifp->if_drain = if_nulldrain;
516
517 /* No more packets may be enqueued. */
518 ifp->if_snd.ifq_maxlen = 0;
519
520 splx(s);
521 }
522
523 /*
524 * Detach an interface from the list of "active" interfaces,
525 * freeing any resources as we go along.
526 *
527 * NOTE: This routine must be called with a valid thread context,
528 * as it may block.
529 */
530 void
531 if_detach(ifp)
532 struct ifnet *ifp;
533 {
534 struct socket so;
535 struct ifaddr *ifa;
536 #ifdef IFAREF_DEBUG
537 struct ifaddr *last_ifa = NULL;
538 #endif
539 struct domain *dp;
540 struct protosw *pr;
541 struct radix_node_head *rnh;
542 int s, i, family, purged;
543
544 /*
545 * XXX It's kind of lame that we have to have the
546 * XXX socket structure...
547 */
548 memset(&so, 0, sizeof(so));
549
550 s = splnet();
551
552 /*
553 * Do an if_down() to give protocols a chance to do something.
554 */
555 if_down(ifp);
556
557 #ifdef ALTQ
558 if (ALTQ_IS_ENABLED(&ifp->if_snd))
559 altq_disable(&ifp->if_snd);
560 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
561 altq_detach(&ifp->if_snd);
562 #endif
563
564 #ifdef PFIL_HOOKS
565 (void) pfil_head_unregister(&ifp->if_pfil);
566 #endif
567
568 if_free_sadl(ifp);
569
570 /*
571 * Rip all the addresses off the interface. This should make
572 * all of the routes go away.
573 */
574 while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
575 family = ifa->ifa_addr->sa_family;
576 #ifdef IFAREF_DEBUG
577 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
578 ifa, family, ifa->ifa_refcnt);
579 if (last_ifa != NULL && ifa == last_ifa)
580 panic("if_detach: loop detected");
581 last_ifa = ifa;
582 #endif
583 if (family == AF_LINK) {
584 /*
585 * XXX This case may now be obsolete by
586 * XXX the call to if_free_sadl().
587 */
588 rtinit(ifa, RTM_DELETE, 0);
589 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
590 IFAFREE(ifa);
591 } else {
592 dp = pffinddomain(family);
593 #ifdef DIAGNOSTIC
594 if (dp == NULL)
595 panic("if_detach: no domain for AF %d\n",
596 family);
597 #endif
598 purged = 0;
599 for (pr = dp->dom_protosw;
600 pr < dp->dom_protoswNPROTOSW; pr++) {
601 so.so_proto = pr;
602 if (pr->pr_usrreq != NULL) {
603 (void) (*pr->pr_usrreq)(&so,
604 PRU_PURGEIF, NULL, NULL,
605 (struct mbuf *) ifp, curproc);
606 purged = 1;
607 }
608 }
609 if (purged == 0) {
610 /*
611 * XXX What's really the best thing to do
612 * XXX here? --thorpej (at) netbsd.org
613 */
614 printf("if_detach: WARNING: AF %d not purged\n",
615 family);
616 }
617 }
618 }
619
620 /* Walk the routing table looking for straglers. */
621 for (i = 0; i <= AF_MAX; i++) {
622 if ((rnh = rt_tables[i]) != NULL)
623 (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
624 }
625
626 for (dp = domains; dp; dp = dp->dom_next) {
627 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
628 (*dp->dom_ifdetach)(ifp,
629 ifp->if_afdata[dp->dom_family]);
630 }
631
632 /* Announce that the interface is gone. */
633 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
634
635 ifindex2ifnet[ifp->if_index] = NULL;
636
637 TAILQ_REMOVE(&ifnet, ifp, if_list);
638
639 /*
640 * remove packets came from ifp, from software interrupt queues.
641 * net/netisr_dispatch.h is not usable, as some of them use
642 * strange queue names.
643 */
644 #define IF_DETACH_QUEUES(x) \
645 do { \
646 extern struct ifqueue x; \
647 if_detach_queues(ifp, & x); \
648 } while (0)
649 #ifdef INET
650 #if NARP > 0
651 IF_DETACH_QUEUES(arpintrq);
652 #endif
653 IF_DETACH_QUEUES(ipintrq);
654 #endif
655 #ifdef INET6
656 IF_DETACH_QUEUES(ip6intrq);
657 #endif
658 #ifdef NETATALK
659 IF_DETACH_QUEUES(atintrq1);
660 IF_DETACH_QUEUES(atintrq2);
661 #endif
662 #ifdef NS
663 IF_DETACH_QUEUES(nsintrq);
664 #endif
665 #ifdef ISO
666 IF_DETACH_QUEUES(clnlintrq);
667 #endif
668 #ifdef CCITT
669 IF_DETACH_QUEUES(llcintrq);
670 IF_DETACH_QUEUES(hdintrq);
671 #endif
672 #ifdef NATM
673 IF_DETACH_QUEUES(natmintrq);
674 #endif
675 #undef IF_DETACH_QUEUES
676
677 splx(s);
678 }
679
680 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
681 defined(ISO) || defined(CCITT) || defined(NATM)
682 static void
683 if_detach_queues(ifp, q)
684 struct ifnet *ifp;
685 struct ifqueue *q;
686 {
687 struct mbuf *m, *prev, *next;
688
689 prev = NULL;
690 for (m = q->ifq_head; m; m = next) {
691 next = m->m_nextpkt;
692 #ifdef DIAGNOSTIC
693 if ((m->m_flags & M_PKTHDR) == 0) {
694 prev = m;
695 continue;
696 }
697 #endif
698 if (m->m_pkthdr.rcvif != ifp) {
699 prev = m;
700 continue;
701 }
702
703 if (prev)
704 prev->m_nextpkt = m->m_nextpkt;
705 else
706 q->ifq_head = m->m_nextpkt;
707 if (q->ifq_tail == m)
708 q->ifq_tail = prev;
709 q->ifq_len--;
710
711 m->m_nextpkt = NULL;
712 m_freem(m);
713 IF_DROP(q);
714 }
715 }
716 #endif /* defined(INET) || ... */
717
718 /*
719 * Callback for a radix tree walk to delete all references to an
720 * ifnet.
721 */
722 int
723 if_rt_walktree(rn, v)
724 struct radix_node *rn;
725 void *v;
726 {
727 struct ifnet *ifp = (struct ifnet *)v;
728 struct rtentry *rt = (struct rtentry *)rn;
729 int error;
730
731 if (rt->rt_ifp == ifp) {
732 /* Delete the entry. */
733 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
734 rt_mask(rt), rt->rt_flags, NULL);
735 if (error)
736 printf("%s: warning: unable to delete rtentry @ %p, "
737 "error = %d\n", ifp->if_xname, rt, error);
738 }
739 return (0);
740 }
741
742 /*
743 * Create a clone network interface.
744 */
745 int
746 if_clone_create(name)
747 const char *name;
748 {
749 struct if_clone *ifc;
750 int unit;
751
752 ifc = if_clone_lookup(name, &unit);
753 if (ifc == NULL)
754 return (EINVAL);
755
756 if (ifunit(name) != NULL)
757 return (EEXIST);
758
759 return ((*ifc->ifc_create)(ifc, unit));
760 }
761
762 /*
763 * Destroy a clone network interface.
764 */
765 int
766 if_clone_destroy(name)
767 const char *name;
768 {
769 struct if_clone *ifc;
770 struct ifnet *ifp;
771
772 ifc = if_clone_lookup(name, NULL);
773 if (ifc == NULL)
774 return (EINVAL);
775
776 ifp = ifunit(name);
777 if (ifp == NULL)
778 return (ENXIO);
779
780 if (ifc->ifc_destroy == NULL)
781 return (EOPNOTSUPP);
782
783 (*ifc->ifc_destroy)(ifp);
784 return (0);
785 }
786
787 /*
788 * Look up a network interface cloner.
789 */
790 struct if_clone *
791 if_clone_lookup(name, unitp)
792 const char *name;
793 int *unitp;
794 {
795 struct if_clone *ifc;
796 const char *cp;
797 int i;
798
799 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
800 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
801 if (ifc->ifc_name[i] != *cp)
802 goto next_ifc;
803 }
804 goto found_name;
805 next_ifc:
806 ifc = LIST_NEXT(ifc, ifc_list);
807 }
808
809 /* No match. */
810 return (NULL);
811
812 found_name:
813 for (i = 0; *cp != '\0'; cp++) {
814 if (*cp < '0' || *cp > '9') {
815 /* Bogus unit number. */
816 return (NULL);
817 }
818 i = (i * 10) + (*cp - '0');
819 }
820
821 if (unitp != NULL)
822 *unitp = i;
823 return (ifc);
824 }
825
826 /*
827 * Register a network interface cloner.
828 */
829 void
830 if_clone_attach(ifc)
831 struct if_clone *ifc;
832 {
833
834 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
835 if_cloners_count++;
836 }
837
838 /*
839 * Unregister a network interface cloner.
840 */
841 void
842 if_clone_detach(ifc)
843 struct if_clone *ifc;
844 {
845
846 LIST_REMOVE(ifc, ifc_list);
847 if_cloners_count--;
848 }
849
850 /*
851 * Provide list of interface cloners to userspace.
852 */
853 int
854 if_clone_list(ifcr)
855 struct if_clonereq *ifcr;
856 {
857 char outbuf[IFNAMSIZ], *dst;
858 struct if_clone *ifc;
859 int count, error = 0;
860
861 ifcr->ifcr_total = if_cloners_count;
862 if ((dst = ifcr->ifcr_buffer) == NULL) {
863 /* Just asking how many there are. */
864 return (0);
865 }
866
867 if (ifcr->ifcr_count < 0)
868 return (EINVAL);
869
870 count = (if_cloners_count < ifcr->ifcr_count) ?
871 if_cloners_count : ifcr->ifcr_count;
872
873 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
874 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
875 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
876 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
877 error = copyout(outbuf, dst, IFNAMSIZ);
878 if (error)
879 break;
880 }
881
882 return (error);
883 }
884
885 /*
886 * Locate an interface based on a complete address.
887 */
888 /*ARGSUSED*/
889 struct ifaddr *
890 ifa_ifwithaddr(addr)
891 struct sockaddr *addr;
892 {
893 struct ifnet *ifp;
894 struct ifaddr *ifa;
895
896 #define equal(a1, a2) \
897 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
898
899 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
900 ifp = TAILQ_NEXT(ifp, if_list)) {
901 if (ifp->if_output == if_nulloutput)
902 continue;
903 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
904 ifa = TAILQ_NEXT(ifa, ifa_list)) {
905 if (ifa->ifa_addr->sa_family != addr->sa_family)
906 continue;
907 if (equal(addr, ifa->ifa_addr))
908 return (ifa);
909 if ((ifp->if_flags & IFF_BROADCAST) &&
910 ifa->ifa_broadaddr &&
911 /* IP6 doesn't have broadcast */
912 ifa->ifa_broadaddr->sa_len != 0 &&
913 equal(ifa->ifa_broadaddr, addr))
914 return (ifa);
915 }
916 }
917 return (NULL);
918 }
919
920 /*
921 * Locate the point to point interface with a given destination address.
922 */
923 /*ARGSUSED*/
924 struct ifaddr *
925 ifa_ifwithdstaddr(addr)
926 struct sockaddr *addr;
927 {
928 struct ifnet *ifp;
929 struct ifaddr *ifa;
930
931 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
932 ifp = TAILQ_NEXT(ifp, if_list)) {
933 if (ifp->if_output == if_nulloutput)
934 continue;
935 if (ifp->if_flags & IFF_POINTOPOINT) {
936 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
937 ifa = TAILQ_NEXT(ifa, ifa_list)) {
938 if (ifa->ifa_addr->sa_family !=
939 addr->sa_family ||
940 ifa->ifa_dstaddr == NULL)
941 continue;
942 if (equal(addr, ifa->ifa_dstaddr))
943 return (ifa);
944 }
945 }
946 }
947 return (NULL);
948 }
949
950 /*
951 * Find an interface on a specific network. If many, choice
952 * is most specific found.
953 */
954 struct ifaddr *
955 ifa_ifwithnet(addr)
956 struct sockaddr *addr;
957 {
958 struct ifnet *ifp;
959 struct ifaddr *ifa;
960 struct sockaddr_dl *sdl;
961 struct ifaddr *ifa_maybe = 0;
962 u_int af = addr->sa_family;
963 char *addr_data = addr->sa_data, *cplim;
964
965 if (af == AF_LINK) {
966 sdl = (struct sockaddr_dl *)addr;
967 if (sdl->sdl_index && sdl->sdl_index <= if_index &&
968 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
969 return (ifnet_addrs[sdl->sdl_index]);
970 }
971 #ifdef NETATALK
972 if (af == AF_APPLETALK) {
973 struct sockaddr_at *sat, *sat2;
974 sat = (struct sockaddr_at *)addr;
975 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
976 ifp = TAILQ_NEXT(ifp, if_list)) {
977 if (ifp->if_output == if_nulloutput)
978 continue;
979 ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
980 if (ifa == NULL)
981 continue;
982 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
983 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
984 return (ifa); /* exact match */
985 if (ifa_maybe == NULL) {
986 /* else keep the if with the rigth range */
987 ifa_maybe = ifa;
988 }
989 }
990 return (ifa_maybe);
991 }
992 #endif
993 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
994 ifp = TAILQ_NEXT(ifp, if_list)) {
995 if (ifp->if_output == if_nulloutput)
996 continue;
997 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
998 ifa = TAILQ_NEXT(ifa, ifa_list)) {
999 char *cp, *cp2, *cp3;
1000
1001 if (ifa->ifa_addr->sa_family != af ||
1002 ifa->ifa_netmask == 0)
1003 next: continue;
1004 cp = addr_data;
1005 cp2 = ifa->ifa_addr->sa_data;
1006 cp3 = ifa->ifa_netmask->sa_data;
1007 cplim = (char *)ifa->ifa_netmask +
1008 ifa->ifa_netmask->sa_len;
1009 while (cp3 < cplim) {
1010 if ((*cp++ ^ *cp2++) & *cp3++) {
1011 /* want to continue for() loop */
1012 goto next;
1013 }
1014 }
1015 if (ifa_maybe == 0 ||
1016 rn_refines((caddr_t)ifa->ifa_netmask,
1017 (caddr_t)ifa_maybe->ifa_netmask))
1018 ifa_maybe = ifa;
1019 }
1020 }
1021 return (ifa_maybe);
1022 }
1023
1024 /*
1025 * Find the interface of the addresss.
1026 */
1027 struct ifaddr *
1028 ifa_ifwithladdr(addr)
1029 struct sockaddr *addr;
1030 {
1031 struct ifaddr *ia;
1032
1033 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1034 (ia = ifa_ifwithnet(addr)))
1035 return (ia);
1036 return (NULL);
1037 }
1038
1039 /*
1040 * Find an interface using a specific address family
1041 */
1042 struct ifaddr *
1043 ifa_ifwithaf(af)
1044 int af;
1045 {
1046 struct ifnet *ifp;
1047 struct ifaddr *ifa;
1048
1049 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1050 ifp = TAILQ_NEXT(ifp, if_list)) {
1051 if (ifp->if_output == if_nulloutput)
1052 continue;
1053 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1054 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1055 if (ifa->ifa_addr->sa_family == af)
1056 return (ifa);
1057 }
1058 }
1059 return (NULL);
1060 }
1061
1062 /*
1063 * Find an interface address specific to an interface best matching
1064 * a given address.
1065 */
1066 struct ifaddr *
1067 ifaof_ifpforaddr(addr, ifp)
1068 struct sockaddr *addr;
1069 struct ifnet *ifp;
1070 {
1071 struct ifaddr *ifa;
1072 char *cp, *cp2, *cp3;
1073 char *cplim;
1074 struct ifaddr *ifa_maybe = 0;
1075 u_int af = addr->sa_family;
1076
1077 if (ifp->if_output == if_nulloutput)
1078 return (NULL);
1079
1080 if (af >= AF_MAX)
1081 return (NULL);
1082
1083 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1084 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1085 if (ifa->ifa_addr->sa_family != af)
1086 continue;
1087 ifa_maybe = ifa;
1088 if (ifa->ifa_netmask == 0) {
1089 if (equal(addr, ifa->ifa_addr) ||
1090 (ifa->ifa_dstaddr &&
1091 equal(addr, ifa->ifa_dstaddr)))
1092 return (ifa);
1093 continue;
1094 }
1095 cp = addr->sa_data;
1096 cp2 = ifa->ifa_addr->sa_data;
1097 cp3 = ifa->ifa_netmask->sa_data;
1098 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1099 for (; cp3 < cplim; cp3++) {
1100 if ((*cp++ ^ *cp2++) & *cp3)
1101 break;
1102 }
1103 if (cp3 == cplim)
1104 return (ifa);
1105 }
1106 return (ifa_maybe);
1107 }
1108
1109 /*
1110 * Default action when installing a route with a Link Level gateway.
1111 * Lookup an appropriate real ifa to point to.
1112 * This should be moved to /sys/net/link.c eventually.
1113 */
1114 void
1115 link_rtrequest(cmd, rt, info)
1116 int cmd;
1117 struct rtentry *rt;
1118 struct rt_addrinfo *info;
1119 {
1120 struct ifaddr *ifa;
1121 struct sockaddr *dst;
1122 struct ifnet *ifp;
1123
1124 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1125 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1126 return;
1127 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1128 IFAFREE(rt->rt_ifa);
1129 rt->rt_ifa = ifa;
1130 IFAREF(ifa);
1131 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1132 ifa->ifa_rtrequest(cmd, rt, info);
1133 }
1134 }
1135
1136 /*
1137 * Mark an interface down and notify protocols of
1138 * the transition.
1139 * NOTE: must be called at splsoftnet or equivalent.
1140 */
1141 void
1142 if_down(ifp)
1143 struct ifnet *ifp;
1144 {
1145 struct ifaddr *ifa;
1146
1147 ifp->if_flags &= ~IFF_UP;
1148 microtime(&ifp->if_lastchange);
1149 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1150 ifa = TAILQ_NEXT(ifa, ifa_list))
1151 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1152 IFQ_PURGE(&ifp->if_snd);
1153 rt_ifmsg(ifp);
1154 }
1155
1156 /*
1157 * Mark an interface up and notify protocols of
1158 * the transition.
1159 * NOTE: must be called at splsoftnet or equivalent.
1160 */
1161 void
1162 if_up(ifp)
1163 struct ifnet *ifp;
1164 {
1165 #ifdef notyet
1166 struct ifaddr *ifa;
1167 #endif
1168
1169 ifp->if_flags |= IFF_UP;
1170 microtime(&ifp->if_lastchange);
1171 #ifdef notyet
1172 /* this has no effect on IP, and will kill all ISO connections XXX */
1173 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1174 ifa = TAILQ_NEXT(ifa, ifa_list))
1175 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1176 #endif
1177 rt_ifmsg(ifp);
1178 #ifdef INET6
1179 in6_if_up(ifp);
1180 #endif
1181 }
1182
1183 /*
1184 * Handle interface watchdog timer routines. Called
1185 * from softclock, we decrement timers (if set) and
1186 * call the appropriate interface routine on expiration.
1187 */
1188 void
1189 if_slowtimo(arg)
1190 void *arg;
1191 {
1192 struct ifnet *ifp;
1193 int s = splnet();
1194
1195 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1196 ifp = TAILQ_NEXT(ifp, if_list)) {
1197 if (ifp->if_timer == 0 || --ifp->if_timer)
1198 continue;
1199 if (ifp->if_watchdog)
1200 (*ifp->if_watchdog)(ifp);
1201 }
1202 splx(s);
1203 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
1204 if_slowtimo, NULL);
1205 }
1206
1207 /*
1208 * Set/clear promiscuous mode on interface ifp based on the truth value
1209 * of pswitch. The calls are reference counted so that only the first
1210 * "on" request actually has an effect, as does the final "off" request.
1211 * Results are undefined if the "off" and "on" requests are not matched.
1212 */
1213 int
1214 ifpromisc(ifp, pswitch)
1215 struct ifnet *ifp;
1216 int pswitch;
1217 {
1218 int pcount, ret;
1219 short flags;
1220 struct ifreq ifr;
1221
1222 pcount = ifp->if_pcount;
1223 flags = ifp->if_flags;
1224 if (pswitch) {
1225 /*
1226 * Allow the device to be "placed" into promiscuous
1227 * mode even if it is not configured up. It will
1228 * consult IFF_PROMISC when it is is brought up.
1229 */
1230 if (ifp->if_pcount++ != 0)
1231 return (0);
1232 ifp->if_flags |= IFF_PROMISC;
1233 if ((ifp->if_flags & IFF_UP) == 0)
1234 return (0);
1235 } else {
1236 if (--ifp->if_pcount > 0)
1237 return (0);
1238 ifp->if_flags &= ~IFF_PROMISC;
1239 /*
1240 * If the device is not configured up, we should not need to
1241 * turn off promiscuous mode (device should have turned it
1242 * off when interface went down; and will look at IFF_PROMISC
1243 * again next time interface comes up).
1244 */
1245 if ((ifp->if_flags & IFF_UP) == 0)
1246 return (0);
1247 }
1248 memset(&ifr, 0, sizeof(ifr));
1249 ifr.ifr_flags = ifp->if_flags;
1250 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
1251 /* Restore interface state if not successful. */
1252 if (ret != 0) {
1253 ifp->if_pcount = pcount;
1254 ifp->if_flags = flags;
1255 }
1256 return (ret);
1257 }
1258
1259 /*
1260 * Map interface name to
1261 * interface structure pointer.
1262 */
1263 struct ifnet *
1264 ifunit(name)
1265 const char *name;
1266 {
1267 struct ifnet *ifp;
1268 const char *cp = name;
1269 u_int unit = 0;
1270 u_int i;
1271
1272 /*
1273 * If the entire name is a number, treat it as an ifindex.
1274 */
1275 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1276 unit = unit * 10 + (*cp - '0');
1277 }
1278
1279 /*
1280 * If the number took all of the name, then it's a valid ifindex.
1281 */
1282 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1283 if (unit >= if_index)
1284 return (NULL);
1285 ifp = ifindex2ifnet[unit];
1286 if (ifp == NULL || ifp->if_output == if_nulloutput)
1287 return (NULL);
1288 return (ifp);
1289 }
1290
1291 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1292 ifp = TAILQ_NEXT(ifp, if_list)) {
1293 if (ifp->if_output == if_nulloutput)
1294 continue;
1295 if (strcmp(ifp->if_xname, name) == 0)
1296 return (ifp);
1297 }
1298 return (NULL);
1299 }
1300
1301 /*
1302 * Interface ioctls.
1303 */
1304 int
1305 ifioctl(so, cmd, data, p)
1306 struct socket *so;
1307 u_long cmd;
1308 caddr_t data;
1309 struct proc *p;
1310 {
1311 struct ifnet *ifp;
1312 struct ifreq *ifr;
1313 struct ifcapreq *ifcr;
1314 struct ifdatareq *ifdr;
1315 int s, error = 0;
1316 short oif_flags;
1317
1318 switch (cmd) {
1319
1320 case SIOCGIFCONF:
1321 case OSIOCGIFCONF:
1322 return (ifconf(cmd, data));
1323 }
1324 ifr = (struct ifreq *)data;
1325 ifcr = (struct ifcapreq *)data;
1326 ifdr = (struct ifdatareq *)data;
1327
1328 switch (cmd) {
1329 case SIOCIFCREATE:
1330 case SIOCIFDESTROY:
1331 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1332 return (error);
1333 return ((cmd == SIOCIFCREATE) ?
1334 if_clone_create(ifr->ifr_name) :
1335 if_clone_destroy(ifr->ifr_name));
1336
1337 case SIOCIFGCLONERS:
1338 return (if_clone_list((struct if_clonereq *)data));
1339 }
1340
1341 ifp = ifunit(ifr->ifr_name);
1342 if (ifp == 0)
1343 return (ENXIO);
1344 oif_flags = ifp->if_flags;
1345 switch (cmd) {
1346
1347 case SIOCGIFFLAGS:
1348 ifr->ifr_flags = ifp->if_flags;
1349 break;
1350
1351 case SIOCGIFMETRIC:
1352 ifr->ifr_metric = ifp->if_metric;
1353 break;
1354
1355 case SIOCGIFMTU:
1356 ifr->ifr_mtu = ifp->if_mtu;
1357 break;
1358
1359 case SIOCGIFDLT:
1360 ifr->ifr_dlt = ifp->if_dlt;
1361 break;
1362
1363 case SIOCSIFFLAGS:
1364 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1365 return (error);
1366 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1367 s = splnet();
1368 if_down(ifp);
1369 splx(s);
1370 }
1371 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1372 s = splnet();
1373 if_up(ifp);
1374 splx(s);
1375 }
1376 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1377 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1378 if (ifp->if_ioctl)
1379 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1380 break;
1381
1382 case SIOCGIFCAP:
1383 ifcr->ifcr_capabilities = ifp->if_capabilities;
1384 ifcr->ifcr_capenable = ifp->if_capenable;
1385 break;
1386
1387 case SIOCSIFCAP:
1388 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1389 return (error);
1390 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1391 return (EINVAL);
1392 if (ifp->if_ioctl == NULL)
1393 return (EOPNOTSUPP);
1394
1395 /* Must prevent race with packet reception here. */
1396 s = splnet();
1397 if (ifcr->ifcr_capenable != ifp->if_capenable) {
1398 struct ifreq ifrq;
1399
1400 ifrq.ifr_flags = ifp->if_flags;
1401 ifp->if_capenable = ifcr->ifcr_capenable;
1402
1403 /* Pre-compute the checksum flags mask. */
1404 ifp->if_csum_flags_tx = 0;
1405 ifp->if_csum_flags_rx = 0;
1406 if (ifp->if_capenable & IFCAP_CSUM_IPv4) {
1407 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1408 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1409 }
1410
1411 if (ifp->if_capenable & IFCAP_CSUM_TCPv4) {
1412 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1413 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1414 } else if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
1415 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1416
1417 if (ifp->if_capenable & IFCAP_CSUM_UDPv4) {
1418 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1419 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1420 } else if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
1421 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1422
1423 if (ifp->if_capenable & IFCAP_CSUM_TCPv6) {
1424 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1425 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1426 }
1427
1428 if (ifp->if_capenable & IFCAP_CSUM_UDPv6) {
1429 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1430 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1431 }
1432
1433 /*
1434 * Only kick the interface if it's up. If it's
1435 * not up now, it will notice the cap enables
1436 * when it is brought up later.
1437 */
1438 if (ifp->if_flags & IFF_UP)
1439 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1440 (caddr_t) &ifrq);
1441 }
1442 splx(s);
1443 break;
1444
1445 case SIOCSIFMETRIC:
1446 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1447 return (error);
1448 ifp->if_metric = ifr->ifr_metric;
1449 break;
1450
1451 case SIOCGIFDATA:
1452 ifdr->ifdr_data = ifp->if_data;
1453 break;
1454
1455 case SIOCZIFDATA:
1456 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1457 return (error);
1458 ifdr->ifdr_data = ifp->if_data;
1459 /*
1460 * Assumes that the volatile counters that can be
1461 * zero'ed are at the end of if_data.
1462 */
1463 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1464 offsetof(struct if_data, ifi_ipackets));
1465 break;
1466
1467 case SIOCSIFMTU:
1468 {
1469 u_long oldmtu = ifp->if_mtu;
1470
1471 error = suser(p->p_ucred, &p->p_acflag);
1472 if (error)
1473 return (error);
1474 if (ifp->if_ioctl == NULL)
1475 return (EOPNOTSUPP);
1476 error = (*ifp->if_ioctl)(ifp, cmd, data);
1477
1478 /*
1479 * If the link MTU changed, do network layer specific procedure.
1480 */
1481 if (ifp->if_mtu != oldmtu) {
1482 #ifdef INET6
1483 nd6_setmtu(ifp);
1484 #endif
1485 }
1486 break;
1487 }
1488 case SIOCSIFPHYADDR:
1489 case SIOCDIFPHYADDR:
1490 #ifdef INET6
1491 case SIOCSIFPHYADDR_IN6:
1492 #endif
1493 case SIOCSLIFPHYADDR:
1494 case SIOCADDMULTI:
1495 case SIOCDELMULTI:
1496 case SIOCSIFMEDIA:
1497 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1498 return (error);
1499 /* FALLTHROUGH */
1500 case SIOCGIFPSRCADDR:
1501 case SIOCGIFPDSTADDR:
1502 case SIOCGLIFPHYADDR:
1503 case SIOCGIFMEDIA:
1504 if (ifp->if_ioctl == 0)
1505 return (EOPNOTSUPP);
1506 error = (*ifp->if_ioctl)(ifp, cmd, data);
1507 break;
1508
1509 case SIOCSDRVSPEC:
1510 case SIOCS80211NWID:
1511 case SIOCS80211NWKEY:
1512 case SIOCS80211POWER:
1513 /* XXX: need to pass proc pointer through to driver... */
1514 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1515 return (error);
1516 /* FALLTHROUGH */
1517 default:
1518 if (so->so_proto == 0)
1519 return (EOPNOTSUPP);
1520 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(LKM)
1521 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1522 (struct mbuf *)cmd, (struct mbuf *)data,
1523 (struct mbuf *)ifp, p));
1524 #else
1525 {
1526 int ocmd = cmd;
1527
1528 switch (cmd) {
1529
1530 case SIOCSIFADDR:
1531 case SIOCSIFDSTADDR:
1532 case SIOCSIFBRDADDR:
1533 case SIOCSIFNETMASK:
1534 #if BYTE_ORDER != BIG_ENDIAN
1535 if (ifr->ifr_addr.sa_family == 0 &&
1536 ifr->ifr_addr.sa_len < 16) {
1537 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1538 ifr->ifr_addr.sa_len = 16;
1539 }
1540 #else
1541 if (ifr->ifr_addr.sa_len == 0)
1542 ifr->ifr_addr.sa_len = 16;
1543 #endif
1544 break;
1545
1546 case OSIOCGIFADDR:
1547 cmd = SIOCGIFADDR;
1548 break;
1549
1550 case OSIOCGIFDSTADDR:
1551 cmd = SIOCGIFDSTADDR;
1552 break;
1553
1554 case OSIOCGIFBRDADDR:
1555 cmd = SIOCGIFBRDADDR;
1556 break;
1557
1558 case OSIOCGIFNETMASK:
1559 cmd = SIOCGIFNETMASK;
1560 }
1561
1562 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1563 (struct mbuf *)cmd, (struct mbuf *)data,
1564 (struct mbuf *)ifp, p));
1565
1566 switch (ocmd) {
1567 case OSIOCGIFADDR:
1568 case OSIOCGIFDSTADDR:
1569 case OSIOCGIFBRDADDR:
1570 case OSIOCGIFNETMASK:
1571 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1572 }
1573 }
1574 #endif /* COMPAT_43 */
1575 break;
1576 }
1577
1578 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1579 #ifdef INET6
1580 if ((ifp->if_flags & IFF_UP) != 0) {
1581 s = splnet();
1582 in6_if_up(ifp);
1583 splx(s);
1584 }
1585 #endif
1586 }
1587
1588 return (error);
1589 }
1590
1591 /*
1592 * Return interface configuration
1593 * of system. List may be used
1594 * in later ioctl's (above) to get
1595 * other information.
1596 */
1597 /*ARGSUSED*/
1598 int
1599 ifconf(cmd, data)
1600 u_long cmd;
1601 caddr_t data;
1602 {
1603 struct ifconf *ifc = (struct ifconf *)data;
1604 struct ifnet *ifp;
1605 struct ifaddr *ifa;
1606 struct ifreq ifr, *ifrp;
1607 int space = ifc->ifc_len, error = 0;
1608
1609 ifrp = ifc->ifc_req;
1610 TAILQ_FOREACH(ifp, &ifnet, if_list) {
1611 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1612 if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1613 memset((caddr_t)&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1614 if (space >= (int)sizeof (ifr)) {
1615 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1616 sizeof(ifr));
1617 if (error)
1618 break;
1619 }
1620 space -= sizeof (ifr), ifrp++;
1621 } else
1622 for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1623 struct sockaddr *sa = ifa->ifa_addr;
1624 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
1625 if (cmd == OSIOCGIFCONF) {
1626 struct osockaddr *osa =
1627 (struct osockaddr *)&ifr.ifr_addr;
1628 ifr.ifr_addr = *sa;
1629 osa->sa_family = sa->sa_family;
1630 if (space >= (int)sizeof (ifr)) {
1631 error = copyout((caddr_t)&ifr,
1632 (caddr_t)ifrp,
1633 sizeof (ifr));
1634 ifrp++;
1635 }
1636 } else
1637 #endif
1638 if (sa->sa_len <= sizeof(*sa)) {
1639 ifr.ifr_addr = *sa;
1640 if (space >= (int)sizeof (ifr)) {
1641 error = copyout((caddr_t)&ifr,
1642 (caddr_t)ifrp,
1643 sizeof (ifr));
1644 ifrp++;
1645 }
1646 } else {
1647 space -= sa->sa_len - sizeof(*sa);
1648 if (space >= (int)sizeof (ifr)) {
1649 error = copyout((caddr_t)&ifr,
1650 (caddr_t)ifrp,
1651 sizeof (ifr.ifr_name));
1652 if (error == 0) {
1653 error = copyout((caddr_t)sa,
1654 (caddr_t)&ifrp->ifr_addr,
1655 sa->sa_len);
1656 }
1657 ifrp = (struct ifreq *)
1658 (sa->sa_len +
1659 (caddr_t)&ifrp->ifr_addr);
1660 }
1661 }
1662 if (error)
1663 break;
1664 space -= sizeof (ifr);
1665 }
1666 }
1667 ifc->ifc_len -= space;
1668 return (error);
1669 }
1670