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