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