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