if.c revision 1.91 1 /* $NetBSD: if.c,v 1.91 2001/06/14 05:44:23 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 #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 microtime(&ifp->if_lastchange);
996 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
997 ifa = TAILQ_NEXT(ifa, ifa_list))
998 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
999 IFQ_PURGE(&ifp->if_snd);
1000 rt_ifmsg(ifp);
1001 }
1002
1003 /*
1004 * Mark an interface up and notify protocols of
1005 * the transition.
1006 * NOTE: must be called at splsoftnet or equivalent.
1007 */
1008 void
1009 if_up(ifp)
1010 struct ifnet *ifp;
1011 {
1012 #ifdef notyet
1013 struct ifaddr *ifa;
1014 #endif
1015
1016 ifp->if_flags |= IFF_UP;
1017 microtime(&ifp->if_lastchange);
1018 #ifdef notyet
1019 /* this has no effect on IP, and will kill all ISO connections XXX */
1020 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1021 ifa = TAILQ_NEXT(ifa, ifa_list))
1022 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1023 #endif
1024 rt_ifmsg(ifp);
1025 #ifdef INET6
1026 in6_if_up(ifp);
1027 #endif
1028 }
1029
1030 /*
1031 * Handle interface watchdog timer routines. Called
1032 * from softclock, we decrement timers (if set) and
1033 * call the appropriate interface routine on expiration.
1034 */
1035 void
1036 if_slowtimo(arg)
1037 void *arg;
1038 {
1039 struct ifnet *ifp;
1040 int s = splnet();
1041
1042 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1043 ifp = TAILQ_NEXT(ifp, if_list)) {
1044 if (ifp->if_timer == 0 || --ifp->if_timer)
1045 continue;
1046 if (ifp->if_watchdog)
1047 (*ifp->if_watchdog)(ifp);
1048 }
1049 splx(s);
1050 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
1051 if_slowtimo, NULL);
1052 }
1053
1054 /*
1055 * Set/clear promiscuous mode on interface ifp based on the truth value
1056 * of pswitch. The calls are reference counted so that only the first
1057 * "on" request actually has an effect, as does the final "off" request.
1058 * Results are undefined if the "off" and "on" requests are not matched.
1059 */
1060 int
1061 ifpromisc(ifp, pswitch)
1062 struct ifnet *ifp;
1063 int pswitch;
1064 {
1065 int pcount, ret;
1066 short flags;
1067 struct ifreq ifr;
1068
1069 pcount = ifp->if_pcount;
1070 flags = ifp->if_flags;
1071 if (pswitch) {
1072 /*
1073 * Allow the device to be "placed" into promiscuous
1074 * mode even if it is not configured up. It will
1075 * consult IFF_PROMISC when it is is brought up.
1076 */
1077 if (ifp->if_pcount++ != 0)
1078 return (0);
1079 ifp->if_flags |= IFF_PROMISC;
1080 if ((ifp->if_flags & IFF_UP) == 0)
1081 return (0);
1082 } else {
1083 if (--ifp->if_pcount > 0)
1084 return (0);
1085 ifp->if_flags &= ~IFF_PROMISC;
1086 /*
1087 * If the device is not configured up, we should not need to
1088 * turn off promiscuous mode (device should have turned it
1089 * off when interface went down; and will look at IFF_PROMISC
1090 * again next time interface comes up).
1091 */
1092 if ((ifp->if_flags & IFF_UP) == 0)
1093 return (0);
1094 }
1095 memset(&ifr, 0, sizeof(ifr));
1096 ifr.ifr_flags = ifp->if_flags;
1097 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
1098 /* Restore interface state if not successful. */
1099 if (ret != 0) {
1100 ifp->if_pcount = pcount;
1101 ifp->if_flags = flags;
1102 }
1103 return (ret);
1104 }
1105
1106 /*
1107 * Map interface name to
1108 * interface structure pointer.
1109 */
1110 struct ifnet *
1111 ifunit(name)
1112 const char *name;
1113 {
1114 struct ifnet *ifp;
1115
1116 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1117 ifp = TAILQ_NEXT(ifp, if_list)) {
1118 if (ifp->if_output == if_nulloutput)
1119 continue;
1120 if (strcmp(ifp->if_xname, name) == 0)
1121 return (ifp);
1122 }
1123 return (NULL);
1124 }
1125
1126 /*
1127 * Interface ioctls.
1128 */
1129 int
1130 ifioctl(so, cmd, data, p)
1131 struct socket *so;
1132 u_long cmd;
1133 caddr_t data;
1134 struct proc *p;
1135 {
1136 struct ifnet *ifp;
1137 struct ifreq *ifr;
1138 struct ifcapreq *ifcr;
1139 int s, error = 0;
1140 short oif_flags;
1141
1142 switch (cmd) {
1143
1144 case SIOCGIFCONF:
1145 case OSIOCGIFCONF:
1146 return (ifconf(cmd, data));
1147 }
1148 ifr = (struct ifreq *)data;
1149 ifcr = (struct ifcapreq *)data;
1150
1151 switch (cmd) {
1152 case SIOCIFCREATE:
1153 case SIOCIFDESTROY:
1154 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1155 return (error);
1156 return ((cmd == SIOCIFCREATE) ?
1157 if_clone_create(ifr->ifr_name) :
1158 if_clone_destroy(ifr->ifr_name));
1159
1160 case SIOCIFGCLONERS:
1161 return (if_clone_list((struct if_clonereq *)data));
1162 }
1163
1164 ifp = ifunit(ifr->ifr_name);
1165 if (ifp == 0)
1166 return (ENXIO);
1167 oif_flags = ifp->if_flags;
1168 switch (cmd) {
1169
1170 case SIOCGIFFLAGS:
1171 ifr->ifr_flags = ifp->if_flags;
1172 break;
1173
1174 case SIOCGIFMETRIC:
1175 ifr->ifr_metric = ifp->if_metric;
1176 break;
1177
1178 case SIOCGIFMTU:
1179 ifr->ifr_mtu = ifp->if_mtu;
1180 break;
1181
1182 case SIOCGIFDLT:
1183 ifr->ifr_dlt = ifp->if_dlt;
1184 break;
1185
1186 case SIOCSIFFLAGS:
1187 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1188 return (error);
1189 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1190 s = splnet();
1191 if_down(ifp);
1192 splx(s);
1193 }
1194 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1195 s = splnet();
1196 if_up(ifp);
1197 splx(s);
1198 }
1199 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1200 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1201 if (ifp->if_ioctl)
1202 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1203 break;
1204
1205 case SIOCGIFCAP:
1206 ifcr->ifcr_capabilities = ifp->if_capabilities;
1207 ifcr->ifcr_capenable = ifp->if_capenable;
1208 break;
1209
1210 case SIOCSIFCAP:
1211 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1212 return (error);
1213 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1214 return (EINVAL);
1215 if (ifp->if_ioctl == NULL)
1216 return (EOPNOTSUPP);
1217
1218 /* Must prevent race with packet reception here. */
1219 s = splnet();
1220 if (ifcr->ifcr_capenable != ifp->if_capenable) {
1221 struct ifreq ifrq;
1222
1223 ifrq.ifr_flags = ifp->if_flags;
1224 ifp->if_capenable = ifcr->ifcr_capenable;
1225
1226 /* Pre-compute the checksum flags mask. */
1227 ifp->if_csum_flags = 0;
1228 if (ifp->if_capenable & IFCAP_CSUM_IPv4)
1229 ifp->if_csum_flags |= M_CSUM_IPv4;
1230 if (ifp->if_capenable & IFCAP_CSUM_TCPv4)
1231 ifp->if_csum_flags |= M_CSUM_TCPv4;
1232 if (ifp->if_capenable & IFCAP_CSUM_UDPv4)
1233 ifp->if_csum_flags |= M_CSUM_UDPv4;
1234 if (ifp->if_capenable & IFCAP_CSUM_TCPv6)
1235 ifp->if_csum_flags |= M_CSUM_TCPv6;
1236 if (ifp->if_capenable & IFCAP_CSUM_UDPv6)
1237 ifp->if_csum_flags |= M_CSUM_UDPv6;
1238
1239 /*
1240 * Only kick the interface if it's up. If it's
1241 * not up now, it will notice the cap enables
1242 * when it is brought up later.
1243 */
1244 if (ifp->if_flags & IFF_UP)
1245 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1246 (caddr_t) &ifrq);
1247 }
1248 splx(s);
1249 break;
1250
1251 case SIOCSIFMETRIC:
1252 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1253 return (error);
1254 ifp->if_metric = ifr->ifr_metric;
1255 break;
1256
1257 case SIOCSIFMTU:
1258 {
1259 u_long oldmtu = ifp->if_mtu;
1260
1261 error = suser(p->p_ucred, &p->p_acflag);
1262 if (error)
1263 return (error);
1264 if (ifp->if_ioctl == NULL)
1265 return (EOPNOTSUPP);
1266 error = (*ifp->if_ioctl)(ifp, cmd, data);
1267
1268 /*
1269 * If the link MTU changed, do network layer specific procedure.
1270 */
1271 if (ifp->if_mtu != oldmtu) {
1272 #ifdef INET6
1273 nd6_setmtu(ifp);
1274 #endif
1275 }
1276 break;
1277 }
1278 case SIOCSIFPHYADDR:
1279 case SIOCDIFPHYADDR:
1280 #ifdef INET6
1281 case SIOCSIFPHYADDR_IN6:
1282 #endif
1283 case SIOCSLIFPHYADDR:
1284 case SIOCADDMULTI:
1285 case SIOCDELMULTI:
1286 case SIOCSIFMEDIA:
1287 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1288 return (error);
1289 /* FALLTHROUGH */
1290 case SIOCGIFPSRCADDR:
1291 case SIOCGIFPDSTADDR:
1292 case SIOCGLIFPHYADDR:
1293 case SIOCGIFMEDIA:
1294 if (ifp->if_ioctl == 0)
1295 return (EOPNOTSUPP);
1296 error = (*ifp->if_ioctl)(ifp, cmd, data);
1297 break;
1298
1299 case SIOCSDRVSPEC:
1300 case SIOCS80211NWID:
1301 case SIOCS80211NWKEY:
1302 case SIOCS80211POWER:
1303 /* XXX: need to pass proc pointer through to driver... */
1304 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1305 return (error);
1306 /* FALLTHROUGH */
1307 default:
1308 if (so->so_proto == 0)
1309 return (EOPNOTSUPP);
1310 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(LKM)
1311 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1312 (struct mbuf *)cmd, (struct mbuf *)data,
1313 (struct mbuf *)ifp, p));
1314 #else
1315 {
1316 int ocmd = cmd;
1317
1318 switch (cmd) {
1319
1320 case SIOCSIFADDR:
1321 case SIOCSIFDSTADDR:
1322 case SIOCSIFBRDADDR:
1323 case SIOCSIFNETMASK:
1324 #if BYTE_ORDER != BIG_ENDIAN
1325 if (ifr->ifr_addr.sa_family == 0 &&
1326 ifr->ifr_addr.sa_len < 16) {
1327 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1328 ifr->ifr_addr.sa_len = 16;
1329 }
1330 #else
1331 if (ifr->ifr_addr.sa_len == 0)
1332 ifr->ifr_addr.sa_len = 16;
1333 #endif
1334 break;
1335
1336 case OSIOCGIFADDR:
1337 cmd = SIOCGIFADDR;
1338 break;
1339
1340 case OSIOCGIFDSTADDR:
1341 cmd = SIOCGIFDSTADDR;
1342 break;
1343
1344 case OSIOCGIFBRDADDR:
1345 cmd = SIOCGIFBRDADDR;
1346 break;
1347
1348 case OSIOCGIFNETMASK:
1349 cmd = SIOCGIFNETMASK;
1350 }
1351
1352 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1353 (struct mbuf *)cmd, (struct mbuf *)data,
1354 (struct mbuf *)ifp, p));
1355
1356 switch (ocmd) {
1357 case OSIOCGIFADDR:
1358 case OSIOCGIFDSTADDR:
1359 case OSIOCGIFBRDADDR:
1360 case OSIOCGIFNETMASK:
1361 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1362 }
1363 }
1364 #endif /* COMPAT_43 */
1365 break;
1366 }
1367
1368 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1369 #ifdef INET6
1370 if ((ifp->if_flags & IFF_UP) != 0) {
1371 s = splnet();
1372 in6_if_up(ifp);
1373 splx(s);
1374 }
1375 #endif
1376 }
1377
1378 return (error);
1379 }
1380
1381 /*
1382 * Return interface configuration
1383 * of system. List may be used
1384 * in later ioctl's (above) to get
1385 * other information.
1386 */
1387 /*ARGSUSED*/
1388 int
1389 ifconf(cmd, data)
1390 u_long cmd;
1391 caddr_t data;
1392 {
1393 struct ifconf *ifc = (struct ifconf *)data;
1394 struct ifnet *ifp;
1395 struct ifaddr *ifa;
1396 struct ifreq ifr, *ifrp;
1397 int space = ifc->ifc_len, error = 0;
1398
1399 ifrp = ifc->ifc_req;
1400 for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
1401 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1402 if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
1403 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1404 if (space >= (int)sizeof (ifr)) {
1405 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1406 sizeof(ifr));
1407 if (error)
1408 break;
1409 }
1410 space -= sizeof (ifr), ifrp++;
1411 } else
1412 for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
1413 struct sockaddr *sa = ifa->ifa_addr;
1414 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
1415 if (cmd == OSIOCGIFCONF) {
1416 struct osockaddr *osa =
1417 (struct osockaddr *)&ifr.ifr_addr;
1418 ifr.ifr_addr = *sa;
1419 osa->sa_family = sa->sa_family;
1420 if (space >= (int)sizeof (ifr)) {
1421 error = copyout((caddr_t)&ifr,
1422 (caddr_t)ifrp,
1423 sizeof (ifr));
1424 ifrp++;
1425 }
1426 } else
1427 #endif
1428 if (sa->sa_len <= sizeof(*sa)) {
1429 ifr.ifr_addr = *sa;
1430 if (space >= (int)sizeof (ifr)) {
1431 error = copyout((caddr_t)&ifr,
1432 (caddr_t)ifrp,
1433 sizeof (ifr));
1434 ifrp++;
1435 }
1436 } else {
1437 space -= sa->sa_len - sizeof(*sa);
1438 if (space >= (int)sizeof (ifr)) {
1439 error = copyout((caddr_t)&ifr,
1440 (caddr_t)ifrp,
1441 sizeof (ifr.ifr_name));
1442 if (error == 0) {
1443 error = copyout((caddr_t)sa,
1444 (caddr_t)&ifrp->ifr_addr,
1445 sa->sa_len);
1446 }
1447 ifrp = (struct ifreq *)
1448 (sa->sa_len +
1449 (caddr_t)&ifrp->ifr_addr);
1450 }
1451 }
1452 if (error)
1453 break;
1454 space -= sizeof (ifr);
1455 }
1456 }
1457 ifc->ifc_len -= space;
1458 return (error);
1459 }
1460