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