if.c revision 1.63 1 /* $NetBSD: if.c,v 1.63 2000/07/02 00:20:48 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 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
110 #include <sys/param.h>
111 #include <sys/mbuf.h>
112 #include <sys/systm.h>
113 #include <sys/callout.h>
114 #include <sys/proc.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/domain.h>
118 #include <sys/protosw.h>
119 #include <sys/kernel.h>
120 #include <sys/ioctl.h>
121
122 #include <net/if.h>
123 #include <net/if_dl.h>
124 #include <net/if_types.h>
125 #include <net/radix.h>
126 #include <net/route.h>
127 #ifdef NETATALK
128 #include <netatalk/at_extern.h>
129 #include <netatalk/at.h>
130 #endif
131
132 #ifdef INET6
133 /*XXX*/
134 #include <netinet/in.h>
135 #endif
136
137 int ifqmaxlen = IFQ_MAXLEN;
138 struct callout if_slowtimo_ch;
139
140 #ifdef INET6
141 /*
142 * XXX: declare here to avoid to include many inet6 related files..
143 * should be more generalized?
144 */
145 extern void nd6_setmtu __P((struct ifnet *));
146 #endif
147
148 int if_rt_walktree __P((struct radix_node *, void *));
149
150 struct if_clone *if_clone_lookup __P((const char *, int *));
151
152 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
153
154 /*
155 * Network interface utility routines.
156 *
157 * Routines with ifa_ifwith* names take sockaddr *'s as
158 * parameters.
159 */
160 void
161 ifinit()
162 {
163
164 callout_init(&if_slowtimo_ch);
165 if_slowtimo(NULL);
166 }
167
168 /*
169 * Null routines used while an interface is going away. These routines
170 * just return an error.
171 */
172
173 int
174 if_nulloutput(ifp, m, so, rt)
175 struct ifnet *ifp;
176 struct mbuf *m;
177 struct sockaddr *so;
178 struct rtentry *rt;
179 {
180
181 return (ENXIO);
182 }
183
184 void
185 if_nullinput(ifp, m)
186 struct ifnet *ifp;
187 struct mbuf *m;
188 {
189
190 /* Nothing. */
191 }
192
193 void
194 if_nullstart(ifp)
195 struct ifnet *ifp;
196 {
197
198 /* Nothing. */
199 }
200
201 int
202 if_nullioctl(ifp, cmd, data)
203 struct ifnet *ifp;
204 u_long cmd;
205 caddr_t data;
206 {
207
208 return (ENXIO);
209 }
210
211 int
212 if_nullreset(ifp)
213 struct ifnet *ifp;
214 {
215
216 return (ENXIO);
217 }
218
219 void
220 if_nullwatchdog(ifp)
221 struct ifnet *ifp;
222 {
223
224 /* Nothing. */
225 }
226
227 void
228 if_nulldrain(ifp)
229 struct ifnet *ifp;
230 {
231
232 /* Nothing. */
233 }
234
235 int if_index = 0;
236 struct ifaddr **ifnet_addrs = NULL;
237 struct ifnet **ifindex2ifnet = NULL;
238
239 /*
240 * Attach an interface to the
241 * list of "active" interfaces.
242 */
243 void
244 if_attach(ifp)
245 struct ifnet *ifp;
246 {
247 unsigned socksize, ifasize;
248 int namelen, masklen;
249 struct sockaddr_dl *sdl;
250 struct ifaddr *ifa;
251 static size_t if_indexlim = 8;
252
253 if (if_index == 0)
254 TAILQ_INIT(&ifnet);
255 TAILQ_INIT(&ifp->if_addrlist);
256 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
257 ifp->if_index = ++if_index;
258
259 /*
260 * We have some arrays that should be indexed by if_index.
261 * since if_index will grow dynamically, they should grow too.
262 * struct ifadd **ifnet_addrs
263 * struct ifnet **ifindex2ifnet
264 */
265 if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
266 ifp->if_index >= if_indexlim) {
267 size_t n;
268 caddr_t q;
269
270 while (ifp->if_index >= if_indexlim)
271 if_indexlim <<= 1;
272
273 /* grow ifnet_addrs */
274 n = if_indexlim * sizeof(ifa);
275 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
276 bzero(q, n);
277 if (ifnet_addrs) {
278 bcopy((caddr_t)ifnet_addrs, q, n/2);
279 free((caddr_t)ifnet_addrs, M_IFADDR);
280 }
281 ifnet_addrs = (struct ifaddr **)q;
282
283 /* grow ifindex2ifnet */
284 n = if_indexlim * sizeof(struct ifnet *);
285 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
286 bzero(q, n);
287 if (ifindex2ifnet) {
288 bcopy((caddr_t)ifindex2ifnet, q, n/2);
289 free((caddr_t)ifindex2ifnet, M_IFADDR);
290 }
291 ifindex2ifnet = (struct ifnet **)q;
292 }
293
294 ifindex2ifnet[ifp->if_index] = ifp;
295
296 /*
297 * create a Link Level name for this device
298 */
299 namelen = strlen(ifp->if_xname);
300 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
301 socksize = masklen + ifp->if_addrlen;
302 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
303 if (socksize < sizeof(*sdl))
304 socksize = sizeof(*sdl);
305 socksize = ROUNDUP(socksize);
306 ifasize = sizeof(*ifa) + 2 * socksize;
307 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
308 bzero((caddr_t)ifa, ifasize);
309 sdl = (struct sockaddr_dl *)(ifa + 1);
310 sdl->sdl_len = socksize;
311 sdl->sdl_family = AF_LINK;
312 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
313 sdl->sdl_nlen = namelen;
314 sdl->sdl_index = ifp->if_index;
315 sdl->sdl_type = ifp->if_type;
316 ifnet_addrs[ifp->if_index] = ifa;
317 IFAREF(ifa);
318 ifa->ifa_ifp = ifp;
319 ifa->ifa_rtrequest = link_rtrequest;
320 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
321 IFAREF(ifa);
322 ifa->ifa_addr = (struct sockaddr *)sdl;
323 ifp->if_sadl = sdl;
324 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
325 ifa->ifa_netmask = (struct sockaddr *)sdl;
326 sdl->sdl_len = masklen;
327 while (namelen != 0)
328 sdl->sdl_data[--namelen] = 0xff;
329 if (ifp->if_snd.ifq_maxlen == 0)
330 ifp->if_snd.ifq_maxlen = ifqmaxlen;
331 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
332
333 ifp->if_link_state = LINK_STATE_UNKNOWN;
334
335 /* Announce the interface. */
336 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
337 }
338
339 /*
340 * Deactivate an interface. This points all of the procedure
341 * handles at error stubs. May be called from interrupt context.
342 */
343 void
344 if_deactivate(ifp)
345 struct ifnet *ifp;
346 {
347 int s;
348
349 s = splimp();
350
351 ifp->if_output = if_nulloutput;
352 ifp->if_input = if_nullinput;
353 ifp->if_start = if_nullstart;
354 ifp->if_ioctl = if_nullioctl;
355 ifp->if_reset = if_nullreset;
356 ifp->if_watchdog = if_nullwatchdog;
357 ifp->if_drain = if_nulldrain;
358
359 /* No more packets may be enqueued. */
360 ifp->if_snd.ifq_maxlen = 0;
361
362 splx(s);
363 }
364
365 /*
366 * Detach an interface from the list of "active" interfaces,
367 * freeing any resources as we go along.
368 *
369 * NOTE: This routine must be called with a valid thread context,
370 * as it may block.
371 */
372 void
373 if_detach(ifp)
374 struct ifnet *ifp;
375 {
376 struct socket so;
377 struct ifaddr *ifa;
378 #ifdef IFAREF_DEBUG
379 struct ifaddr *last_ifa = NULL;
380 #endif
381 struct domain *dp;
382 struct protosw *pr;
383 struct radix_node_head *rnh;
384 int s, i, family, purged;
385
386 /*
387 * XXX It's kind of lame that we have to have the
388 * XXX socket structure...
389 */
390 memset(&so, 0, sizeof(so));
391
392 s = splimp();
393
394 /*
395 * Do an if_down() to give protocols a chance to do something.
396 */
397 if_down(ifp);
398
399 /*
400 * Rip all the addresses off the interface. This should make
401 * all of the routes go away.
402 */
403 while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) {
404 family = ifa->ifa_addr->sa_family;
405 #ifdef IFAREF_DEBUG
406 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
407 ifa, family, ifa->ifa_refcnt);
408 if (last_ifa != NULL && ifa == last_ifa)
409 panic("if_detach: loop detected");
410 last_ifa = ifa;
411 #endif
412 if (family == AF_LINK) {
413 rtinit(ifa, RTM_DELETE, 0);
414 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
415 IFAFREE(ifa);
416 } else {
417 dp = pffinddomain(family);
418 #ifdef DIAGNOSTIC
419 if (dp == NULL)
420 panic("if_detach: no domain for AF %d\n",
421 family);
422 #endif
423 purged = 0;
424 for (pr = dp->dom_protosw;
425 pr < dp->dom_protoswNPROTOSW; pr++) {
426 so.so_proto = pr;
427 if (pr->pr_usrreq != NULL) {
428 (void) (*pr->pr_usrreq)(&so,
429 PRU_PURGEIF, NULL, NULL,
430 (struct mbuf *) ifp, curproc);
431 purged = 1;
432 }
433 }
434 if (purged == 0) {
435 /*
436 * XXX What's really the best thing to do
437 * XXX here? --thorpej (at) netbsd.org
438 */
439 printf("if_detach: WARNING: AF %d not purged\n",
440 family);
441 }
442 }
443 }
444
445 /* Walk the routing table looking for straglers. */
446 for (i = 0; i <= AF_MAX; i++) {
447 if ((rnh = rt_tables[i]) != NULL)
448 (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
449 }
450
451 IFAFREE(ifnet_addrs[ifp->if_index]);
452 ifnet_addrs[ifp->if_index] = NULL;
453
454 /* Announce that the interface is gone. */
455 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
456
457 TAILQ_REMOVE(&ifnet, ifp, if_list);
458
459 splx(s);
460 }
461
462 /*
463 * Callback for a radix tree walk to delete all references to an
464 * ifnet.
465 */
466 int
467 if_rt_walktree(rn, v)
468 struct radix_node *rn;
469 void *v;
470 {
471 struct ifnet *ifp = (struct ifnet *)v;
472 struct rtentry *rt = (struct rtentry *)rn;
473 int error;
474
475 if (rt->rt_ifp == ifp) {
476 /* Delete the entry. */
477 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
478 rt_mask(rt), rt->rt_flags, NULL);
479 if (error)
480 printf("%s: warning: unable to delete rtentry @ %p, "
481 "error = %d\n", ifp->if_xname, rt, error);
482 }
483 return (0);
484 }
485
486 /*
487 * Create a clone network interface.
488 */
489 int
490 if_clone_create(name)
491 const char *name;
492 {
493 struct if_clone *ifc;
494 int unit;
495
496 ifc = if_clone_lookup(name, &unit);
497 if (ifc == NULL)
498 return (EINVAL);
499
500 if (ifunit(name) != NULL)
501 return (EEXIST);
502
503 return ((*ifc->ifc_create)(ifc, unit));
504 }
505
506 /*
507 * Destroy a clone network interface.
508 */
509 int
510 if_clone_destroy(name)
511 const char *name;
512 {
513 struct if_clone *ifc;
514 struct ifnet *ifp;
515
516 ifc = if_clone_lookup(name, NULL);
517 if (ifc == NULL)
518 return (EINVAL);
519
520 ifp = ifunit(name);
521 if (ifp == NULL)
522 return (ENXIO);
523
524 if (ifc->ifc_destroy == NULL)
525 return (EOPNOTSUPP);
526
527 (*ifc->ifc_destroy)(ifp);
528 return (0);
529 }
530
531 /*
532 * Look up a network interface cloner.
533 */
534 struct if_clone *
535 if_clone_lookup(name, unitp)
536 const char *name;
537 int *unitp;
538 {
539 struct if_clone *ifc;
540 const char *cp;
541 int i;
542
543 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
544 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
545 if (ifc->ifc_name[i] != *cp)
546 goto next_ifc;
547 }
548 goto found_name;
549 next_ifc:
550 ifc = LIST_NEXT(ifc, ifc_list);
551 }
552
553 /* No match. */
554 return (NULL);
555
556 found_name:
557 for (i = 0; *cp != '\0'; cp++) {
558 if (*cp < '0' || *cp > '9') {
559 /* Bogus unit number. */
560 return (NULL);
561 }
562 i = (i * 10) + (*cp - '0');
563 }
564
565 if (unitp != NULL)
566 *unitp = i;
567 return (ifc);
568 }
569
570 /*
571 * Register a network interface cloner.
572 */
573 void
574 if_clone_attach(ifc)
575 struct if_clone *ifc;
576 {
577
578 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
579 }
580
581 /*
582 * Unregister a network interface cloner.
583 */
584 void
585 if_clone_detach(ifc)
586 struct if_clone *ifc;
587 {
588
589 LIST_REMOVE(ifc, ifc_list);
590 }
591
592 /*
593 * Locate an interface based on a complete address.
594 */
595 /*ARGSUSED*/
596 struct ifaddr *
597 ifa_ifwithaddr(addr)
598 struct sockaddr *addr;
599 {
600 struct ifnet *ifp;
601 struct ifaddr *ifa;
602
603 #define equal(a1, a2) \
604 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
605
606 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
607 ifp = TAILQ_NEXT(ifp, if_list)) {
608 if (ifp->if_output == if_nulloutput)
609 continue;
610 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
611 ifa = TAILQ_NEXT(ifa, ifa_list)) {
612 if (ifa->ifa_addr->sa_family != addr->sa_family)
613 continue;
614 if (equal(addr, ifa->ifa_addr))
615 return (ifa);
616 if ((ifp->if_flags & IFF_BROADCAST) &&
617 ifa->ifa_broadaddr &&
618 /* IP6 doesn't have broadcast */
619 ifa->ifa_broadaddr->sa_len != 0 &&
620 equal(ifa->ifa_broadaddr, addr))
621 return (ifa);
622 }
623 }
624 return (NULL);
625 }
626
627 /*
628 * Locate the point to point interface with a given destination address.
629 */
630 /*ARGSUSED*/
631 struct ifaddr *
632 ifa_ifwithdstaddr(addr)
633 struct sockaddr *addr;
634 {
635 struct ifnet *ifp;
636 struct ifaddr *ifa;
637
638 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
639 ifp = TAILQ_NEXT(ifp, if_list)) {
640 if (ifp->if_output == if_nulloutput)
641 continue;
642 if (ifp->if_flags & IFF_POINTOPOINT) {
643 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
644 ifa = TAILQ_NEXT(ifa, ifa_list)) {
645 if (ifa->ifa_addr->sa_family !=
646 addr->sa_family ||
647 ifa->ifa_dstaddr == NULL)
648 continue;
649 if (equal(addr, ifa->ifa_dstaddr))
650 return (ifa);
651 }
652 }
653 }
654 return (NULL);
655 }
656
657 /*
658 * Find an interface on a specific network. If many, choice
659 * is most specific found.
660 */
661 struct ifaddr *
662 ifa_ifwithnet(addr)
663 struct sockaddr *addr;
664 {
665 struct ifnet *ifp;
666 struct ifaddr *ifa;
667 struct sockaddr_dl *sdl;
668 struct ifaddr *ifa_maybe = 0;
669 u_int af = addr->sa_family;
670 char *addr_data = addr->sa_data, *cplim;
671
672 if (af == AF_LINK) {
673 sdl = (struct sockaddr_dl *)addr;
674 if (sdl->sdl_index && sdl->sdl_index <= if_index &&
675 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
676 return (ifnet_addrs[sdl->sdl_index]);
677 }
678 #ifdef NETATALK
679 if (af == AF_APPLETALK) {
680 struct sockaddr_at *sat, *sat2;
681 sat = (struct sockaddr_at *)addr;
682 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
683 ifp = TAILQ_NEXT(ifp, if_list)) {
684 if (ifp->if_output == if_nulloutput)
685 continue;
686 ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
687 if (ifa == NULL)
688 continue;
689 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
690 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
691 return (ifa); /* exact match */
692 if (ifa_maybe == NULL) {
693 /* else keep the if with the rigth range */
694 ifa_maybe = ifa;
695 }
696 }
697 return (ifa_maybe);
698 }
699 #endif
700 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
701 ifp = TAILQ_NEXT(ifp, if_list)) {
702 if (ifp->if_output == if_nulloutput)
703 continue;
704 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
705 ifa = TAILQ_NEXT(ifa, ifa_list)) {
706 char *cp, *cp2, *cp3;
707
708 if (ifa->ifa_addr->sa_family != af ||
709 ifa->ifa_netmask == 0)
710 next: continue;
711 cp = addr_data;
712 cp2 = ifa->ifa_addr->sa_data;
713 cp3 = ifa->ifa_netmask->sa_data;
714 cplim = (char *)ifa->ifa_netmask +
715 ifa->ifa_netmask->sa_len;
716 while (cp3 < cplim) {
717 if ((*cp++ ^ *cp2++) & *cp3++) {
718 /* want to continue for() loop */
719 goto next;
720 }
721 }
722 if (ifa_maybe == 0 ||
723 rn_refines((caddr_t)ifa->ifa_netmask,
724 (caddr_t)ifa_maybe->ifa_netmask))
725 ifa_maybe = ifa;
726 }
727 }
728 return (ifa_maybe);
729 }
730
731 /*
732 * Find the interface of the addresss.
733 */
734 struct ifaddr *
735 ifa_ifwithladdr(addr)
736 struct sockaddr *addr;
737 {
738 struct ifaddr *ia;
739
740 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
741 (ia = ifa_ifwithnet(addr)))
742 return (ia);
743 return (NULL);
744 }
745
746 /*
747 * Find an interface using a specific address family
748 */
749 struct ifaddr *
750 ifa_ifwithaf(af)
751 int af;
752 {
753 struct ifnet *ifp;
754 struct ifaddr *ifa;
755
756 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
757 ifp = TAILQ_NEXT(ifp, if_list)) {
758 if (ifp->if_output == if_nulloutput)
759 continue;
760 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
761 ifa = TAILQ_NEXT(ifa, ifa_list)) {
762 if (ifa->ifa_addr->sa_family == af)
763 return (ifa);
764 }
765 }
766 return (NULL);
767 }
768
769 /*
770 * Find an interface address specific to an interface best matching
771 * a given address.
772 */
773 struct ifaddr *
774 ifaof_ifpforaddr(addr, ifp)
775 struct sockaddr *addr;
776 struct ifnet *ifp;
777 {
778 struct ifaddr *ifa;
779 char *cp, *cp2, *cp3;
780 char *cplim;
781 struct ifaddr *ifa_maybe = 0;
782 u_int af = addr->sa_family;
783
784 if (ifp->if_output == if_nulloutput)
785 return (NULL);
786
787 if (af >= AF_MAX)
788 return (NULL);
789
790 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
791 ifa = TAILQ_NEXT(ifa, ifa_list)) {
792 if (ifa->ifa_addr->sa_family != af)
793 continue;
794 ifa_maybe = ifa;
795 if (ifa->ifa_netmask == 0) {
796 if (equal(addr, ifa->ifa_addr) ||
797 (ifa->ifa_dstaddr &&
798 equal(addr, ifa->ifa_dstaddr)))
799 return (ifa);
800 continue;
801 }
802 cp = addr->sa_data;
803 cp2 = ifa->ifa_addr->sa_data;
804 cp3 = ifa->ifa_netmask->sa_data;
805 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
806 for (; cp3 < cplim; cp3++) {
807 if ((*cp++ ^ *cp2++) & *cp3)
808 break;
809 }
810 if (cp3 == cplim)
811 return (ifa);
812 }
813 return (ifa_maybe);
814 }
815
816 /*
817 * Default action when installing a route with a Link Level gateway.
818 * Lookup an appropriate real ifa to point to.
819 * This should be moved to /sys/net/link.c eventually.
820 */
821 void
822 link_rtrequest(cmd, rt, sa)
823 int cmd;
824 struct rtentry *rt;
825 struct sockaddr *sa;
826 {
827 struct ifaddr *ifa;
828 struct sockaddr *dst;
829 struct ifnet *ifp;
830
831 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
832 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
833 return;
834 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
835 IFAFREE(rt->rt_ifa);
836 rt->rt_ifa = ifa;
837 IFAREF(ifa);
838 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
839 ifa->ifa_rtrequest(cmd, rt, sa);
840 }
841 }
842
843 /*
844 * Mark an interface down and notify protocols of
845 * the transition.
846 * NOTE: must be called at splsoftnet or equivalent.
847 */
848 void
849 if_down(ifp)
850 struct ifnet *ifp;
851 {
852 struct ifaddr *ifa;
853
854 ifp->if_flags &= ~IFF_UP;
855 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
856 ifa = TAILQ_NEXT(ifa, ifa_list))
857 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
858 if_qflush(&ifp->if_snd);
859 rt_ifmsg(ifp);
860 }
861
862 /*
863 * Mark an interface up and notify protocols of
864 * the transition.
865 * NOTE: must be called at splsoftnet or equivalent.
866 */
867 void
868 if_up(ifp)
869 struct ifnet *ifp;
870 {
871 #ifdef notyet
872 struct ifaddr *ifa;
873 #endif
874
875 ifp->if_flags |= IFF_UP;
876 #ifdef notyet
877 /* this has no effect on IP, and will kill all ISO connections XXX */
878 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
879 ifa = TAILQ_NEXT(ifa, ifa_list))
880 pfctlinput(PRC_IFUP, ifa->ifa_addr);
881 #endif
882 rt_ifmsg(ifp);
883 #ifdef INET6
884 in6_if_up(ifp);
885 #endif
886 }
887
888 /*
889 * Flush an interface queue.
890 */
891 void
892 if_qflush(ifq)
893 struct ifqueue *ifq;
894 {
895 struct mbuf *m, *n;
896
897 n = ifq->ifq_head;
898 while ((m = n) != NULL) {
899 n = m->m_act;
900 m_freem(m);
901 }
902 ifq->ifq_head = 0;
903 ifq->ifq_tail = 0;
904 ifq->ifq_len = 0;
905 }
906
907 /*
908 * Handle interface watchdog timer routines. Called
909 * from softclock, we decrement timers (if set) and
910 * call the appropriate interface routine on expiration.
911 */
912 void
913 if_slowtimo(arg)
914 void *arg;
915 {
916 struct ifnet *ifp;
917 int s = splimp();
918
919 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
920 ifp = TAILQ_NEXT(ifp, if_list)) {
921 if (ifp->if_timer == 0 || --ifp->if_timer)
922 continue;
923 if (ifp->if_watchdog)
924 (*ifp->if_watchdog)(ifp);
925 }
926 splx(s);
927 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
928 if_slowtimo, NULL);
929 }
930
931 /*
932 * Map interface name to
933 * interface structure pointer.
934 */
935 struct ifnet *
936 ifunit(name)
937 const char *name;
938 {
939 struct ifnet *ifp;
940
941 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
942 ifp = TAILQ_NEXT(ifp, if_list)) {
943 if (ifp->if_output == if_nulloutput)
944 continue;
945 if (strcmp(ifp->if_xname, name) == 0)
946 return (ifp);
947 }
948 return (NULL);
949 }
950
951 /*
952 * Interface ioctls.
953 */
954 int
955 ifioctl(so, cmd, data, p)
956 struct socket *so;
957 u_long cmd;
958 caddr_t data;
959 struct proc *p;
960 {
961 struct ifnet *ifp;
962 struct ifreq *ifr;
963 int error = 0;
964 short oif_flags;
965
966 switch (cmd) {
967
968 case SIOCGIFCONF:
969 case OSIOCGIFCONF:
970 return (ifconf(cmd, data));
971 }
972 ifr = (struct ifreq *)data;
973
974 switch (cmd) {
975 case SIOCIFCREATE:
976 return (if_clone_create(ifr->ifr_name));
977 case SIOCIFDESTROY:
978 return (if_clone_destroy(ifr->ifr_name));
979 }
980
981 ifp = ifunit(ifr->ifr_name);
982 if (ifp == 0)
983 return (ENXIO);
984 oif_flags = ifp->if_flags;
985 switch (cmd) {
986
987 case SIOCGIFFLAGS:
988 ifr->ifr_flags = ifp->if_flags;
989 break;
990
991 case SIOCGIFMETRIC:
992 ifr->ifr_metric = ifp->if_metric;
993 break;
994
995 case SIOCGIFMTU:
996 ifr->ifr_mtu = ifp->if_mtu;
997 break;
998
999 case SIOCSIFFLAGS:
1000 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1001 return (error);
1002 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1003 int s = splimp();
1004 if_down(ifp);
1005 splx(s);
1006 }
1007 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1008 int s = splimp();
1009 if_up(ifp);
1010 splx(s);
1011 }
1012 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1013 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1014 if (ifp->if_ioctl)
1015 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1016 break;
1017
1018 case SIOCSIFMETRIC:
1019 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1020 return (error);
1021 ifp->if_metric = ifr->ifr_metric;
1022 break;
1023
1024 case SIOCSIFMTU:
1025 {
1026 u_long oldmtu = ifp->if_mtu;
1027
1028 error = suser(p->p_ucred, &p->p_acflag);
1029 if (error)
1030 return (error);
1031 if (ifp->if_ioctl == NULL)
1032 return (EOPNOTSUPP);
1033 error = (*ifp->if_ioctl)(ifp, cmd, data);
1034
1035 /*
1036 * If the link MTU changed, do network layer specific procedure.
1037 */
1038 if (ifp->if_mtu != oldmtu) {
1039 #ifdef INET6
1040 nd6_setmtu(ifp);
1041 #endif
1042 }
1043 break;
1044 }
1045 case SIOCADDMULTI:
1046 case SIOCDELMULTI:
1047 case SIOCSIFMEDIA:
1048 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1049 return (error);
1050 /* FALLTHROUGH */
1051 case SIOCGIFMEDIA:
1052 if (ifp->if_ioctl == 0)
1053 return (EOPNOTSUPP);
1054 error = (*ifp->if_ioctl)(ifp, cmd, data);
1055 break;
1056
1057 case SIOCSDRVSPEC:
1058 /* XXX: need to pass proc pointer through to driver... */
1059 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1060 return (error);
1061 /* FALLTHROUGH */
1062 default:
1063 if (so->so_proto == 0)
1064 return (EOPNOTSUPP);
1065 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
1066 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1067 (struct mbuf *)cmd, (struct mbuf *)data,
1068 (struct mbuf *)ifp, p));
1069 #else
1070 {
1071 int ocmd = cmd;
1072
1073 switch (cmd) {
1074
1075 case SIOCSIFADDR:
1076 case SIOCSIFDSTADDR:
1077 case SIOCSIFBRDADDR:
1078 case SIOCSIFNETMASK:
1079 #if BYTE_ORDER != BIG_ENDIAN
1080 if (ifr->ifr_addr.sa_family == 0 &&
1081 ifr->ifr_addr.sa_len < 16) {
1082 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1083 ifr->ifr_addr.sa_len = 16;
1084 }
1085 #else
1086 if (ifr->ifr_addr.sa_len == 0)
1087 ifr->ifr_addr.sa_len = 16;
1088 #endif
1089 break;
1090
1091 case OSIOCGIFADDR:
1092 cmd = SIOCGIFADDR;
1093 break;
1094
1095 case OSIOCGIFDSTADDR:
1096 cmd = SIOCGIFDSTADDR;
1097 break;
1098
1099 case OSIOCGIFBRDADDR:
1100 cmd = SIOCGIFBRDADDR;
1101 break;
1102
1103 case OSIOCGIFNETMASK:
1104 cmd = SIOCGIFNETMASK;
1105 }
1106
1107 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1108 (struct mbuf *)cmd, (struct mbuf *)data,
1109 (struct mbuf *)ifp, p));
1110
1111 switch (ocmd) {
1112 case OSIOCGIFADDR:
1113 case OSIOCGIFDSTADDR:
1114 case OSIOCGIFBRDADDR:
1115 case OSIOCGIFNETMASK:
1116 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1117 }
1118 }
1119 #endif /* COMPAT_43 */
1120 break;
1121 }
1122
1123 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1124 #ifdef INET6
1125 if ((ifp->if_flags & IFF_UP) != 0) {
1126 int s = splimp();
1127 in6_if_up(ifp);
1128 splx(s);
1129 }
1130 #endif
1131 }
1132
1133 return (error);
1134 }
1135
1136 /*
1137 * Return interface configuration
1138 * of system. List may be used
1139 * in later ioctl's (above) to get
1140 * other information.
1141 */
1142 /*ARGSUSED*/
1143 int
1144 ifconf(cmd, data)
1145 u_long cmd;
1146 caddr_t data;
1147 {
1148 struct ifconf *ifc = (struct ifconf *)data;
1149 struct ifnet *ifp;
1150 struct ifaddr *ifa;
1151 struct ifreq ifr, *ifrp;
1152 int space = ifc->ifc_len, error = 0;
1153
1154 ifrp = ifc->ifc_req;
1155 for (ifp = ifnet.tqh_first;
1156 space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
1157 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1158 if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
1159 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1160 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1161 sizeof(ifr));
1162 if (error)
1163 break;
1164 space -= sizeof (ifr), ifrp++;
1165 } else
1166 for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
1167 struct sockaddr *sa = ifa->ifa_addr;
1168 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
1169 if (cmd == OSIOCGIFCONF) {
1170 struct osockaddr *osa =
1171 (struct osockaddr *)&ifr.ifr_addr;
1172 ifr.ifr_addr = *sa;
1173 osa->sa_family = sa->sa_family;
1174 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1175 sizeof (ifr));
1176 ifrp++;
1177 } else
1178 #endif
1179 if (sa->sa_len <= sizeof(*sa)) {
1180 ifr.ifr_addr = *sa;
1181 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1182 sizeof (ifr));
1183 ifrp++;
1184 } else {
1185 space -= sa->sa_len - sizeof(*sa);
1186 if (space < sizeof (ifr))
1187 break;
1188 error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1189 sizeof (ifr.ifr_name));
1190 if (error == 0)
1191 error = copyout((caddr_t)sa,
1192 (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1193 ifrp = (struct ifreq *)
1194 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1195 }
1196 if (error)
1197 break;
1198 space -= sizeof (ifr);
1199 }
1200 }
1201 ifc->ifc_len -= space;
1202 return (error);
1203 }
1204