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