FreeBSD/Linux Kernel Cross Reference
sys/net/if.c
1 /*-
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
30 * $FreeBSD: releng/5.4/sys/net/if.c 145094 2005-04-15 01:52:03Z cperciva $
31 */
32
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_mac.h"
37 #include "opt_carp.h"
38
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/conf.h>
42 #include <sys/mac.h>
43 #include <sys/malloc.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/mbuf.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/kernel.h>
53 #include <sys/sockio.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 #include <sys/domain.h>
58 #include <sys/jail.h>
59 #include <machine/stdarg.h>
60
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/if_clone.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_var.h>
67 #include <net/radix.h>
68 #include <net/route.h>
69
70 #if defined(INET) || defined(INET6)
71 /*XXX*/
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #ifdef INET6
75 #include <netinet6/in6_var.h>
76 #include <netinet6/in6_ifattach.h>
77 #endif
78 #endif
79 #ifdef INET
80 #include <netinet/if_ether.h>
81 #endif
82 #ifdef DEV_CARP
83 #include <netinet/ip_carp.h>
84 #endif
85
86 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
87
88 static void if_attachdomain(void *);
89 static void if_attachdomain1(struct ifnet *);
90 static int ifconf(u_long, caddr_t);
91 static void if_grow(void);
92 static void if_init(void *);
93 static void if_check(void *);
94 static int if_findindex(struct ifnet *);
95 static void if_qflush(struct ifaltq *);
96 static void if_route(struct ifnet *, int flag, int fam);
97 static void if_slowtimo(void *);
98 static void if_unroute(struct ifnet *, int flag, int fam);
99 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
100 static int if_rtdel(struct radix_node *, void *);
101 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
102 static void if_start_deferred(void *context, int pending);
103 #ifdef INET6
104 /*
105 * XXX: declare here to avoid to include many inet6 related files..
106 * should be more generalized?
107 */
108 extern void nd6_setmtu(struct ifnet *);
109 #endif
110
111 int if_index = 0;
112 struct ifindex_entry *ifindex_table = NULL;
113 int ifqmaxlen = IFQ_MAXLEN;
114 struct ifnethead ifnet; /* depend on static init XXX */
115 struct mtx ifnet_lock;
116
117 static int if_indexlim = 8;
118 static struct knlist ifklist;
119
120 static void filt_netdetach(struct knote *kn);
121 static int filt_netdev(struct knote *kn, long hint);
122
123 static struct filterops netdev_filtops =
124 { 1, NULL, filt_netdetach, filt_netdev };
125
126 /*
127 * System initialization
128 */
129 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
130 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
131
132 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
133 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
134
135 static d_open_t netopen;
136 static d_close_t netclose;
137 static d_ioctl_t netioctl;
138 static d_kqfilter_t netkqfilter;
139
140 static struct cdevsw net_cdevsw = {
141 .d_version = D_VERSION,
142 .d_flags = D_NEEDGIANT,
143 .d_open = netopen,
144 .d_close = netclose,
145 .d_ioctl = netioctl,
146 .d_name = "net",
147 .d_kqfilter = netkqfilter,
148 };
149
150 static int
151 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
152 {
153 return (0);
154 }
155
156 static int
157 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
158 {
159 return (0);
160 }
161
162 static int
163 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
164 {
165 struct ifnet *ifp;
166 int error, idx;
167
168 /* only support interface specific ioctls */
169 if (IOCGROUP(cmd) != 'i')
170 return (EOPNOTSUPP);
171 idx = minor(dev);
172 if (idx == 0) {
173 /*
174 * special network device, not interface.
175 */
176 if (cmd == SIOCGIFCONF)
177 return (ifconf(cmd, data)); /* XXX remove cmd */
178 return (EOPNOTSUPP);
179 }
180
181 ifp = ifnet_byindex(idx);
182 if (ifp == NULL)
183 return (ENXIO);
184
185 error = ifhwioctl(cmd, ifp, data, td);
186 if (error == ENOIOCTL)
187 error = EOPNOTSUPP;
188 return (error);
189 }
190
191 static int
192 netkqfilter(struct cdev *dev, struct knote *kn)
193 {
194 struct knlist *klist;
195 struct ifnet *ifp;
196 int idx;
197
198 switch (kn->kn_filter) {
199 case EVFILT_NETDEV:
200 kn->kn_fop = &netdev_filtops;
201 break;
202 default:
203 return (1);
204 }
205
206 idx = minor(dev);
207 if (idx == 0) {
208 klist = &ifklist;
209 } else {
210 ifp = ifnet_byindex(idx);
211 if (ifp == NULL)
212 return (1);
213 klist = &ifp->if_klist;
214 }
215
216 kn->kn_hook = (caddr_t)klist;
217
218 knlist_add(klist, kn, 0);
219
220 return (0);
221 }
222
223 static void
224 filt_netdetach(struct knote *kn)
225 {
226 struct knlist *klist = (struct knlist *)kn->kn_hook;
227
228 knlist_remove(klist, kn, 0);
229 }
230
231 static int
232 filt_netdev(struct knote *kn, long hint)
233 {
234 struct knlist *klist = (struct knlist *)kn->kn_hook;
235
236 /*
237 * Currently NOTE_EXIT is abused to indicate device detach.
238 */
239 if (hint == NOTE_EXIT) {
240 kn->kn_data = NOTE_LINKINV;
241 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
242 knlist_remove_inevent(klist, kn);
243 return (1);
244 }
245 if (hint != 0)
246 kn->kn_data = hint; /* current status */
247 if (kn->kn_sfflags & hint)
248 kn->kn_fflags |= hint;
249 return (kn->kn_fflags != 0);
250 }
251
252 /*
253 * Network interface utility routines.
254 *
255 * Routines with ifa_ifwith* names take sockaddr *'s as
256 * parameters.
257 */
258 /* ARGSUSED*/
259 static void
260 if_init(void *dummy __unused)
261 {
262
263 IFNET_LOCK_INIT();
264 TAILQ_INIT(&ifnet);
265 knlist_init(&ifklist, NULL);
266 if_grow(); /* create initial table */
267 ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
268 UID_ROOT, GID_WHEEL, 0600, "network");
269 if_clone_init();
270 }
271
272 static void
273 if_grow(void)
274 {
275 u_int n;
276 struct ifindex_entry *e;
277
278 if_indexlim <<= 1;
279 n = if_indexlim * sizeof(*e);
280 e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
281 if (ifindex_table != NULL) {
282 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
283 free((caddr_t)ifindex_table, M_IFADDR);
284 }
285 ifindex_table = e;
286 }
287
288 /* ARGSUSED*/
289 static void
290 if_check(void *dummy __unused)
291 {
292 struct ifnet *ifp;
293 int s;
294
295 s = splimp();
296 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
297 TAILQ_FOREACH(ifp, &ifnet, if_link) {
298 if (ifp->if_snd.ifq_maxlen == 0) {
299 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
300 ifp->if_snd.ifq_maxlen = ifqmaxlen;
301 }
302 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
303 if_printf(ifp,
304 "XXX: driver didn't initialize queue mtx\n");
305 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
306 MTX_NETWORK_LOCK, MTX_DEF);
307 }
308 }
309 IFNET_RUNLOCK();
310 splx(s);
311 if_slowtimo(0);
312 }
313
314 static int
315 if_findindex(struct ifnet *ifp)
316 {
317 int i, unit;
318 char eaddr[18], devname[32];
319 const char *name, *p;
320
321 switch (ifp->if_type) {
322 case IFT_ETHER: /* these types use struct arpcom */
323 case IFT_FDDI:
324 case IFT_XETHER:
325 case IFT_ISO88025:
326 case IFT_L2VLAN:
327 snprintf(eaddr, 18, "%6D", IFP2AC(ifp)->ac_enaddr, ":");
328 break;
329 default:
330 eaddr[0] = '\0';
331 break;
332 }
333 strlcpy(devname, ifp->if_xname, sizeof(devname));
334 name = net_cdevsw.d_name;
335 i = 0;
336 while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
337 if (resource_string_value(name, unit, "ether", &p) == 0)
338 if (strcmp(p, eaddr) == 0)
339 goto found;
340 if (resource_string_value(name, unit, "dev", &p) == 0)
341 if (strcmp(p, devname) == 0)
342 goto found;
343 }
344 unit = 0;
345 found:
346 if (unit != 0) {
347 if (ifaddr_byindex(unit) == NULL)
348 return (unit);
349 printf("%s%d in use, cannot hardwire it to %s.\n",
350 name, unit, devname);
351 }
352 for (unit = 1; ; unit++) {
353 if (unit <= if_index && ifaddr_byindex(unit) != NULL)
354 continue;
355 if (resource_string_value(name, unit, "ether", &p) == 0 ||
356 resource_string_value(name, unit, "dev", &p) == 0)
357 continue;
358 break;
359 }
360 return (unit);
361 }
362
363 /*
364 * Attach an interface to the
365 * list of "active" interfaces.
366 */
367 void
368 if_attach(struct ifnet *ifp)
369 {
370 unsigned socksize, ifasize;
371 int namelen, masklen;
372 struct sockaddr_dl *sdl;
373 struct ifaddr *ifa;
374
375 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
376 IF_AFDATA_LOCK_INIT(ifp);
377 ifp->if_afdata_initialized = 0;
378 IFNET_WLOCK();
379 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
380 IFNET_WUNLOCK();
381 /*
382 * XXX -
383 * The old code would work if the interface passed a pre-existing
384 * chain of ifaddrs to this code. We don't trust our callers to
385 * properly initialize the tailq, however, so we no longer allow
386 * this unlikely case.
387 */
388 TAILQ_INIT(&ifp->if_addrhead);
389 TAILQ_INIT(&ifp->if_prefixhead);
390 TAILQ_INIT(&ifp->if_multiaddrs);
391 knlist_init(&ifp->if_klist, NULL);
392 getmicrotime(&ifp->if_lastchange);
393 ifp->if_data.ifi_epoch = time_uptime;
394
395 #ifdef MAC
396 mac_init_ifnet(ifp);
397 mac_create_ifnet(ifp);
398 #endif
399
400 ifp->if_index = if_findindex(ifp);
401 if (ifp->if_index > if_index)
402 if_index = ifp->if_index;
403 if (if_index >= if_indexlim)
404 if_grow();
405 ifp->if_data.ifi_datalen = sizeof(struct if_data);
406
407 ifnet_byindex(ifp->if_index) = ifp;
408 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
409 unit2minor(ifp->if_index),
410 UID_ROOT, GID_WHEEL, 0600, "%s/%s",
411 net_cdevsw.d_name, ifp->if_xname);
412 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
413 net_cdevsw.d_name, ifp->if_index);
414
415 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
416
417 /*
418 * create a Link Level name for this device
419 */
420 namelen = strlen(ifp->if_xname);
421 /*
422 * Always save enough space for any possiable name so we can do
423 * a rename in place later.
424 */
425 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
426 socksize = masklen + ifp->if_addrlen;
427 if (socksize < sizeof(*sdl))
428 socksize = sizeof(*sdl);
429 socksize = roundup2(socksize, sizeof(long));
430 ifasize = sizeof(*ifa) + 2 * socksize;
431 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
432 IFA_LOCK_INIT(ifa);
433 sdl = (struct sockaddr_dl *)(ifa + 1);
434 sdl->sdl_len = socksize;
435 sdl->sdl_family = AF_LINK;
436 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
437 sdl->sdl_nlen = namelen;
438 sdl->sdl_index = ifp->if_index;
439 sdl->sdl_type = ifp->if_type;
440 ifaddr_byindex(ifp->if_index) = ifa;
441 ifa->ifa_ifp = ifp;
442 ifa->ifa_rtrequest = link_rtrequest;
443 ifa->ifa_addr = (struct sockaddr *)sdl;
444 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
445 ifa->ifa_netmask = (struct sockaddr *)sdl;
446 sdl->sdl_len = masklen;
447 while (namelen != 0)
448 sdl->sdl_data[--namelen] = 0xff;
449 ifa->ifa_refcnt = 1;
450 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
451 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
452 ifp->if_snd.altq_type = 0;
453 ifp->if_snd.altq_disc = NULL;
454 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
455 ifp->if_snd.altq_tbr = NULL;
456 ifp->if_snd.altq_ifp = ifp;
457
458 if (domains)
459 if_attachdomain1(ifp);
460
461 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
462
463 /* Announce the interface. */
464 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
465 }
466
467 static void
468 if_attachdomain(void *dummy)
469 {
470 struct ifnet *ifp;
471 int s;
472
473 s = splnet();
474 TAILQ_FOREACH(ifp, &ifnet, if_link)
475 if_attachdomain1(ifp);
476 splx(s);
477 }
478 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
479 if_attachdomain, NULL);
480
481 static void
482 if_attachdomain1(struct ifnet *ifp)
483 {
484 struct domain *dp;
485 int s;
486
487 s = splnet();
488
489 /*
490 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
491 * cannot lock ifp->if_afdata initialization, entirely.
492 */
493 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
494 splx(s);
495 return;
496 }
497 if (ifp->if_afdata_initialized) {
498 IF_AFDATA_UNLOCK(ifp);
499 splx(s);
500 return;
501 }
502 ifp->if_afdata_initialized = 1;
503 IF_AFDATA_UNLOCK(ifp);
504
505 /* address family dependent data region */
506 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
507 for (dp = domains; dp; dp = dp->dom_next) {
508 if (dp->dom_ifattach)
509 ifp->if_afdata[dp->dom_family] =
510 (*dp->dom_ifattach)(ifp);
511 }
512
513 splx(s);
514 }
515
516 /*
517 * Detach an interface, removing it from the
518 * list of "active" interfaces.
519 */
520 void
521 if_detach(struct ifnet *ifp)
522 {
523 struct ifaddr *ifa, *next;
524 struct radix_node_head *rnh;
525 int s;
526 int i;
527 struct domain *dp;
528 struct ifnet *iter;
529 int found;
530
531 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
532 #ifdef DEV_CARP
533 /* Maybe hook to the generalized departure handler above?!? */
534 if (ifp->if_carp)
535 carp_ifdetach(ifp);
536 #endif
537
538 /*
539 * Remove routes and flush queues.
540 */
541 s = splnet();
542 if_down(ifp);
543 #ifdef ALTQ
544 if (ALTQ_IS_ENABLED(&ifp->if_snd))
545 altq_disable(&ifp->if_snd);
546 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
547 altq_detach(&ifp->if_snd);
548 #endif
549
550 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; ifa = next) {
551 next = TAILQ_NEXT(ifa, ifa_link);
552
553 if (ifa->ifa_addr->sa_family == AF_LINK)
554 continue;
555 #ifdef INET
556 /* XXX: Ugly!! ad hoc just for INET */
557 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
558 struct ifaliasreq ifr;
559
560 bzero(&ifr, sizeof(ifr));
561 ifr.ifra_addr = *ifa->ifa_addr;
562 if (ifa->ifa_dstaddr)
563 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
564 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
565 NULL) == 0)
566 continue;
567 }
568 #endif /* INET */
569 #ifdef INET6
570 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
571 in6_purgeaddr(ifa);
572 /* ifp_addrhead is already updated */
573 continue;
574 }
575 #endif /* INET6 */
576 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
577 IFAFREE(ifa);
578 }
579
580 #ifdef INET6
581 /*
582 * Remove all IPv6 kernel structs related to ifp. This should be done
583 * before removing routing entries below, since IPv6 interface direct
584 * routes are expected to be removed by the IPv6-specific kernel API.
585 * Otherwise, the kernel will detect some inconsistency and bark it.
586 */
587 in6_ifdetach(ifp);
588 #endif
589 /*
590 * Remove address from ifindex_table[] and maybe decrement if_index.
591 * Clean up all addresses.
592 */
593 ifnet_byindex(ifp->if_index) = NULL;
594 ifaddr_byindex(ifp->if_index) = NULL;
595 destroy_dev(ifdev_byindex(ifp->if_index));
596 ifdev_byindex(ifp->if_index) = NULL;
597
598 while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
599 if_index--;
600
601
602 /* We can now free link ifaddr. */
603 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
604 ifa = TAILQ_FIRST(&ifp->if_addrhead);
605 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
606 IFAFREE(ifa);
607 }
608
609 /*
610 * Delete all remaining routes using this interface
611 * Unfortuneatly the only way to do this is to slog through
612 * the entire routing table looking for routes which point
613 * to this interface...oh well...
614 */
615 for (i = 1; i <= AF_MAX; i++) {
616 if ((rnh = rt_tables[i]) == NULL)
617 continue;
618 RADIX_NODE_HEAD_LOCK(rnh);
619 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
620 RADIX_NODE_HEAD_UNLOCK(rnh);
621 }
622
623 /* Announce that the interface is gone. */
624 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
625
626 IF_AFDATA_LOCK(ifp);
627 for (dp = domains; dp; dp = dp->dom_next) {
628 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
629 (*dp->dom_ifdetach)(ifp,
630 ifp->if_afdata[dp->dom_family]);
631 }
632 IF_AFDATA_UNLOCK(ifp);
633
634 #ifdef MAC
635 mac_destroy_ifnet(ifp);
636 #endif /* MAC */
637 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
638 knlist_clear(&ifp->if_klist, 0);
639 knlist_destroy(&ifp->if_klist);
640 IFNET_WLOCK();
641 found = 0;
642 TAILQ_FOREACH(iter, &ifnet, if_link)
643 if (iter == ifp) {
644 found = 1;
645 break;
646 }
647 if (found)
648 TAILQ_REMOVE(&ifnet, ifp, if_link);
649 IFNET_WUNLOCK();
650 mtx_destroy(&ifp->if_snd.ifq_mtx);
651 IF_AFDATA_DESTROY(ifp);
652 splx(s);
653 }
654
655 /*
656 * Delete Routes for a Network Interface
657 *
658 * Called for each routing entry via the rnh->rnh_walktree() call above
659 * to delete all route entries referencing a detaching network interface.
660 *
661 * Arguments:
662 * rn pointer to node in the routing table
663 * arg argument passed to rnh->rnh_walktree() - detaching interface
664 *
665 * Returns:
666 * 0 successful
667 * errno failed - reason indicated
668 *
669 */
670 static int
671 if_rtdel(struct radix_node *rn, void *arg)
672 {
673 struct rtentry *rt = (struct rtentry *)rn;
674 struct ifnet *ifp = arg;
675 int err;
676
677 if (rt->rt_ifp == ifp) {
678
679 /*
680 * Protect (sorta) against walktree recursion problems
681 * with cloned routes
682 */
683 if ((rt->rt_flags & RTF_UP) == 0)
684 return (0);
685
686 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
687 rt_mask(rt), rt->rt_flags,
688 (struct rtentry **) NULL);
689 if (err) {
690 log(LOG_WARNING, "if_rtdel: error %d\n", err);
691 }
692 }
693
694 return (0);
695 }
696
697 #define equal(a1, a2) (bcmp((a1), (a2), ((a1))->sa_len) == 0)
698
699 /*
700 * Locate an interface based on a complete address.
701 */
702 /*ARGSUSED*/
703 struct ifaddr *
704 ifa_ifwithaddr(struct sockaddr *addr)
705 {
706 struct ifnet *ifp;
707 struct ifaddr *ifa;
708
709 IFNET_RLOCK();
710 TAILQ_FOREACH(ifp, &ifnet, if_link)
711 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
712 if (ifa->ifa_addr->sa_family != addr->sa_family)
713 continue;
714 if (equal(addr, ifa->ifa_addr))
715 goto done;
716 /* IP6 doesn't have broadcast */
717 if ((ifp->if_flags & IFF_BROADCAST) &&
718 ifa->ifa_broadaddr &&
719 ifa->ifa_broadaddr->sa_len != 0 &&
720 equal(ifa->ifa_broadaddr, addr))
721 goto done;
722 }
723 ifa = NULL;
724 done:
725 IFNET_RUNLOCK();
726 return (ifa);
727 }
728
729 /*
730 * Locate the point to point interface with a given destination address.
731 */
732 /*ARGSUSED*/
733 struct ifaddr *
734 ifa_ifwithdstaddr(struct sockaddr *addr)
735 {
736 struct ifnet *ifp;
737 struct ifaddr *ifa;
738
739 IFNET_RLOCK();
740 TAILQ_FOREACH(ifp, &ifnet, if_link) {
741 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
742 continue;
743 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
744 if (ifa->ifa_addr->sa_family != addr->sa_family)
745 continue;
746 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
747 goto done;
748 }
749 }
750 ifa = NULL;
751 done:
752 IFNET_RUNLOCK();
753 return (ifa);
754 }
755
756 /*
757 * Find an interface on a specific network. If many, choice
758 * is most specific found.
759 */
760 struct ifaddr *
761 ifa_ifwithnet(struct sockaddr *addr)
762 {
763 struct ifnet *ifp;
764 struct ifaddr *ifa;
765 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
766 u_int af = addr->sa_family;
767 char *addr_data = addr->sa_data, *cplim;
768
769 /*
770 * AF_LINK addresses can be looked up directly by their index number,
771 * so do that if we can.
772 */
773 if (af == AF_LINK) {
774 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
775 if (sdl->sdl_index && sdl->sdl_index <= if_index)
776 return (ifaddr_byindex(sdl->sdl_index));
777 }
778
779 /*
780 * Scan though each interface, looking for ones that have
781 * addresses in this address family.
782 */
783 IFNET_RLOCK();
784 TAILQ_FOREACH(ifp, &ifnet, if_link) {
785 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
786 char *cp, *cp2, *cp3;
787
788 if (ifa->ifa_addr->sa_family != af)
789 next: continue;
790 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
791 /*
792 * This is a bit broken as it doesn't
793 * take into account that the remote end may
794 * be a single node in the network we are
795 * looking for.
796 * The trouble is that we don't know the
797 * netmask for the remote end.
798 */
799 if (ifa->ifa_dstaddr != 0
800 && equal(addr, ifa->ifa_dstaddr))
801 goto done;
802 } else {
803 /*
804 * if we have a special address handler,
805 * then use it instead of the generic one.
806 */
807 if (ifa->ifa_claim_addr) {
808 if ((*ifa->ifa_claim_addr)(ifa, addr))
809 goto done;
810 continue;
811 }
812
813 /*
814 * Scan all the bits in the ifa's address.
815 * If a bit dissagrees with what we are
816 * looking for, mask it with the netmask
817 * to see if it really matters.
818 * (A byte at a time)
819 */
820 if (ifa->ifa_netmask == 0)
821 continue;
822 cp = addr_data;
823 cp2 = ifa->ifa_addr->sa_data;
824 cp3 = ifa->ifa_netmask->sa_data;
825 cplim = ifa->ifa_netmask->sa_len
826 + (char *)ifa->ifa_netmask;
827 while (cp3 < cplim)
828 if ((*cp++ ^ *cp2++) & *cp3++)
829 goto next; /* next address! */
830 /*
831 * If the netmask of what we just found
832 * is more specific than what we had before
833 * (if we had one) then remember the new one
834 * before continuing to search
835 * for an even better one.
836 */
837 if (ifa_maybe == 0 ||
838 rn_refines((caddr_t)ifa->ifa_netmask,
839 (caddr_t)ifa_maybe->ifa_netmask))
840 ifa_maybe = ifa;
841 }
842 }
843 }
844 ifa = ifa_maybe;
845 done:
846 IFNET_RUNLOCK();
847 return (ifa);
848 }
849
850 /*
851 * Find an interface address specific to an interface best matching
852 * a given address.
853 */
854 struct ifaddr *
855 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
856 {
857 struct ifaddr *ifa;
858 char *cp, *cp2, *cp3;
859 char *cplim;
860 struct ifaddr *ifa_maybe = 0;
861 u_int af = addr->sa_family;
862
863 if (af >= AF_MAX)
864 return (0);
865 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
866 if (ifa->ifa_addr->sa_family != af)
867 continue;
868 if (ifa_maybe == 0)
869 ifa_maybe = ifa;
870 if (ifa->ifa_netmask == 0) {
871 if (equal(addr, ifa->ifa_addr) ||
872 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
873 goto done;
874 continue;
875 }
876 if (ifp->if_flags & IFF_POINTOPOINT) {
877 if (equal(addr, ifa->ifa_dstaddr))
878 goto done;
879 } else {
880 cp = addr->sa_data;
881 cp2 = ifa->ifa_addr->sa_data;
882 cp3 = ifa->ifa_netmask->sa_data;
883 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
884 for (; cp3 < cplim; cp3++)
885 if ((*cp++ ^ *cp2++) & *cp3)
886 break;
887 if (cp3 == cplim)
888 goto done;
889 }
890 }
891 ifa = ifa_maybe;
892 done:
893 return (ifa);
894 }
895
896 #include <net/route.h>
897
898 /*
899 * Default action when installing a route with a Link Level gateway.
900 * Lookup an appropriate real ifa to point to.
901 * This should be moved to /sys/net/link.c eventually.
902 */
903 static void
904 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
905 {
906 struct ifaddr *ifa, *oifa;
907 struct sockaddr *dst;
908 struct ifnet *ifp;
909
910 RT_LOCK_ASSERT(rt);
911
912 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
913 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
914 return;
915 ifa = ifaof_ifpforaddr(dst, ifp);
916 if (ifa) {
917 IFAREF(ifa); /* XXX */
918 oifa = rt->rt_ifa;
919 rt->rt_ifa = ifa;
920 IFAFREE(oifa);
921 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
922 ifa->ifa_rtrequest(cmd, rt, info);
923 }
924 }
925
926 /*
927 * Mark an interface down and notify protocols of
928 * the transition.
929 * NOTE: must be called at splnet or eqivalent.
930 */
931 static void
932 if_unroute(struct ifnet *ifp, int flag, int fam)
933 {
934 struct ifaddr *ifa;
935
936 ifp->if_flags &= ~flag;
937 getmicrotime(&ifp->if_lastchange);
938 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
939 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
940 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
941 if_qflush(&ifp->if_snd);
942 #ifdef DEV_CARP
943 if (ifp->if_carp)
944 carp_carpdev_state(ifp->if_carp);
945 #endif
946 rt_ifmsg(ifp);
947 }
948
949 /*
950 * Mark an interface up and notify protocols of
951 * the transition.
952 * NOTE: must be called at splnet or eqivalent.
953 */
954 static void
955 if_route(struct ifnet *ifp, int flag, int fam)
956 {
957 struct ifaddr *ifa;
958
959 ifp->if_flags |= flag;
960 getmicrotime(&ifp->if_lastchange);
961 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
962 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
963 pfctlinput(PRC_IFUP, ifa->ifa_addr);
964 #ifdef DEV_CARP
965 if (ifp->if_carp)
966 carp_carpdev_state(ifp->if_carp);
967 #endif
968 rt_ifmsg(ifp);
969 #ifdef INET6
970 in6_if_up(ifp);
971 #endif
972 }
973
974 /*
975 * Mark an interface down and notify protocols of
976 * the transition.
977 * NOTE: must be called at splnet or eqivalent.
978 */
979 void
980 if_down(struct ifnet *ifp)
981 {
982
983 if_unroute(ifp, IFF_UP, AF_UNSPEC);
984 }
985
986 /*
987 * Mark an interface up and notify protocols of
988 * the transition.
989 * NOTE: must be called at splnet or eqivalent.
990 */
991 void
992 if_up(struct ifnet *ifp)
993 {
994
995 if_route(ifp, IFF_UP, AF_UNSPEC);
996 }
997
998 /*
999 * Flush an interface queue.
1000 */
1001 static void
1002 if_qflush(struct ifaltq *ifq)
1003 {
1004 struct mbuf *m, *n;
1005
1006 IFQ_LOCK(ifq);
1007 #ifdef ALTQ
1008 if (ALTQ_IS_ENABLED(ifq))
1009 ALTQ_PURGE(ifq);
1010 #endif
1011 n = ifq->ifq_head;
1012 while ((m = n) != 0) {
1013 n = m->m_act;
1014 m_freem(m);
1015 }
1016 ifq->ifq_head = 0;
1017 ifq->ifq_tail = 0;
1018 ifq->ifq_len = 0;
1019 IFQ_UNLOCK(ifq);
1020 }
1021
1022 /*
1023 * Handle interface watchdog timer routines. Called
1024 * from softclock, we decrement timers (if set) and
1025 * call the appropriate interface routine on expiration.
1026 *
1027 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1028 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1029 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1030 */
1031 static void
1032 if_slowtimo(void *arg)
1033 {
1034 struct ifnet *ifp;
1035 int s = splimp();
1036
1037 IFNET_RLOCK();
1038 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1039 if (ifp->if_timer == 0 || --ifp->if_timer)
1040 continue;
1041 if (ifp->if_watchdog)
1042 (*ifp->if_watchdog)(ifp);
1043 }
1044 IFNET_RUNLOCK();
1045 splx(s);
1046 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1047 }
1048
1049 /*
1050 * Map interface name to
1051 * interface structure pointer.
1052 */
1053 struct ifnet *
1054 ifunit(const char *name)
1055 {
1056 struct ifnet *ifp;
1057
1058 IFNET_RLOCK();
1059 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1060 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1061 break;
1062 }
1063 IFNET_RUNLOCK();
1064 return (ifp);
1065 }
1066
1067 /*
1068 * Hardware specific interface ioctls.
1069 */
1070 static int
1071 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1072 {
1073 struct ifreq *ifr;
1074 struct ifstat *ifs;
1075 int error = 0;
1076 int new_flags;
1077 size_t namelen, onamelen;
1078 char new_name[IFNAMSIZ];
1079 struct ifaddr *ifa;
1080 struct sockaddr_dl *sdl;
1081
1082 ifr = (struct ifreq *)data;
1083 switch (cmd) {
1084 case SIOCGIFINDEX:
1085 ifr->ifr_index = ifp->if_index;
1086 break;
1087
1088 case SIOCGIFFLAGS:
1089 ifr->ifr_flags = ifp->if_flags & 0xffff;
1090 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1091 break;
1092
1093 case SIOCGIFCAP:
1094 ifr->ifr_reqcap = ifp->if_capabilities;
1095 ifr->ifr_curcap = ifp->if_capenable;
1096 break;
1097
1098 #ifdef MAC
1099 case SIOCGIFMAC:
1100 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1101 break;
1102 #endif
1103
1104 case SIOCGIFMETRIC:
1105 ifr->ifr_metric = ifp->if_metric;
1106 break;
1107
1108 case SIOCGIFMTU:
1109 ifr->ifr_mtu = ifp->if_mtu;
1110 break;
1111
1112 case SIOCGIFPHYS:
1113 ifr->ifr_phys = ifp->if_physical;
1114 break;
1115
1116 case SIOCSIFFLAGS:
1117 error = suser(td);
1118 if (error)
1119 return (error);
1120 new_flags = (ifr->ifr_flags & 0xffff) |
1121 (ifr->ifr_flagshigh << 16);
1122 if (ifp->if_flags & IFF_SMART) {
1123 /* Smart drivers twiddle their own routes */
1124 } else if (ifp->if_flags & IFF_UP &&
1125 (new_flags & IFF_UP) == 0) {
1126 int s = splimp();
1127 if_down(ifp);
1128 splx(s);
1129 } else if (new_flags & IFF_UP &&
1130 (ifp->if_flags & IFF_UP) == 0) {
1131 int s = splimp();
1132 if_up(ifp);
1133 splx(s);
1134 }
1135 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1136 (new_flags &~ IFF_CANTCHANGE);
1137 if (new_flags & IFF_PPROMISC) {
1138 /* Permanently promiscuous mode requested */
1139 ifp->if_flags |= IFF_PROMISC;
1140 } else if (ifp->if_pcount == 0) {
1141 ifp->if_flags &= ~IFF_PROMISC;
1142 }
1143 if (ifp->if_ioctl) {
1144 IFF_LOCKGIANT(ifp);
1145 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1146 IFF_UNLOCKGIANT(ifp);
1147 }
1148 getmicrotime(&ifp->if_lastchange);
1149 break;
1150
1151 case SIOCSIFCAP:
1152 error = suser(td);
1153 if (error)
1154 return (error);
1155 if (ifp->if_ioctl == NULL)
1156 return (EOPNOTSUPP);
1157 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1158 return (EINVAL);
1159 IFF_LOCKGIANT(ifp);
1160 error = (*ifp->if_ioctl)(ifp, cmd, data);
1161 IFF_UNLOCKGIANT(ifp);
1162 if (error == 0)
1163 getmicrotime(&ifp->if_lastchange);
1164 break;
1165
1166 #ifdef MAC
1167 case SIOCSIFMAC:
1168 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1169 break;
1170 #endif
1171
1172 case SIOCSIFNAME:
1173 error = suser(td);
1174 if (error != 0)
1175 return (error);
1176 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1177 if (error != 0)
1178 return (error);
1179 if (new_name[0] == '\0')
1180 return (EINVAL);
1181 if (ifunit(new_name) != NULL)
1182 return (EEXIST);
1183
1184 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1185 /* Announce the departure of the interface. */
1186 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1187
1188 log(LOG_INFO, "%s: changing name to '%s'\n",
1189 ifp->if_xname, new_name);
1190
1191 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1192 ifa = ifaddr_byindex(ifp->if_index);
1193 IFA_LOCK(ifa);
1194 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1195 namelen = strlen(new_name);
1196 onamelen = sdl->sdl_nlen;
1197 /*
1198 * Move the address if needed. This is safe because we
1199 * allocate space for a name of length IFNAMSIZ when we
1200 * create this in if_attach().
1201 */
1202 if (namelen != onamelen) {
1203 bcopy(sdl->sdl_data + onamelen,
1204 sdl->sdl_data + namelen, sdl->sdl_alen);
1205 }
1206 bcopy(new_name, sdl->sdl_data, namelen);
1207 sdl->sdl_nlen = namelen;
1208 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1209 bzero(sdl->sdl_data, onamelen);
1210 while (namelen != 0)
1211 sdl->sdl_data[--namelen] = 0xff;
1212 IFA_UNLOCK(ifa);
1213
1214 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1215 /* Announce the return of the interface. */
1216 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1217 break;
1218
1219 case SIOCSIFMETRIC:
1220 error = suser(td);
1221 if (error)
1222 return (error);
1223 ifp->if_metric = ifr->ifr_metric;
1224 getmicrotime(&ifp->if_lastchange);
1225 break;
1226
1227 case SIOCSIFPHYS:
1228 error = suser(td);
1229 if (error)
1230 return (error);
1231 if (ifp->if_ioctl == NULL)
1232 return (EOPNOTSUPP);
1233 IFF_LOCKGIANT(ifp);
1234 error = (*ifp->if_ioctl)(ifp, cmd, data);
1235 IFF_UNLOCKGIANT(ifp);
1236 if (error == 0)
1237 getmicrotime(&ifp->if_lastchange);
1238 break;
1239
1240 case SIOCSIFMTU:
1241 {
1242 u_long oldmtu = ifp->if_mtu;
1243
1244 error = suser(td);
1245 if (error)
1246 return (error);
1247 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1248 return (EINVAL);
1249 if (ifp->if_ioctl == NULL)
1250 return (EOPNOTSUPP);
1251 IFF_LOCKGIANT(ifp);
1252 error = (*ifp->if_ioctl)(ifp, cmd, data);
1253 IFF_UNLOCKGIANT(ifp);
1254 if (error == 0) {
1255 getmicrotime(&ifp->if_lastchange);
1256 rt_ifmsg(ifp);
1257 }
1258 /*
1259 * If the link MTU changed, do network layer specific procedure.
1260 */
1261 if (ifp->if_mtu != oldmtu) {
1262 #ifdef INET6
1263 nd6_setmtu(ifp);
1264 #endif
1265 }
1266 break;
1267 }
1268
1269 case SIOCADDMULTI:
1270 case SIOCDELMULTI:
1271 error = suser(td);
1272 if (error)
1273 return (error);
1274
1275 /* Don't allow group membership on non-multicast interfaces. */
1276 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1277 return (EOPNOTSUPP);
1278
1279 /* Don't let users screw up protocols' entries. */
1280 if (ifr->ifr_addr.sa_family != AF_LINK)
1281 return (EINVAL);
1282
1283 if (cmd == SIOCADDMULTI) {
1284 struct ifmultiaddr *ifma;
1285 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1286 } else {
1287 error = if_delmulti(ifp, &ifr->ifr_addr);
1288 }
1289 if (error == 0)
1290 getmicrotime(&ifp->if_lastchange);
1291 break;
1292
1293 case SIOCSIFPHYADDR:
1294 case SIOCDIFPHYADDR:
1295 #ifdef INET6
1296 case SIOCSIFPHYADDR_IN6:
1297 #endif
1298 case SIOCSLIFPHYADDR:
1299 case SIOCSIFMEDIA:
1300 case SIOCSIFGENERIC:
1301 error = suser(td);
1302 if (error)
1303 return (error);
1304 if (ifp->if_ioctl == NULL)
1305 return (EOPNOTSUPP);
1306 IFF_LOCKGIANT(ifp);
1307 error = (*ifp->if_ioctl)(ifp, cmd, data);
1308 IFF_UNLOCKGIANT(ifp);
1309 if (error == 0)
1310 getmicrotime(&ifp->if_lastchange);
1311 break;
1312
1313 case SIOCGIFSTATUS:
1314 ifs = (struct ifstat *)data;
1315 ifs->ascii[0] = '\0';
1316
1317 case SIOCGIFPSRCADDR:
1318 case SIOCGIFPDSTADDR:
1319 case SIOCGLIFPHYADDR:
1320 case SIOCGIFMEDIA:
1321 case SIOCGIFGENERIC:
1322 if (ifp->if_ioctl == NULL)
1323 return (EOPNOTSUPP);
1324 IFF_LOCKGIANT(ifp);
1325 error = (*ifp->if_ioctl)(ifp, cmd, data);
1326 IFF_UNLOCKGIANT(ifp);
1327 break;
1328
1329 case SIOCSIFLLADDR:
1330 error = suser(td);
1331 if (error)
1332 return (error);
1333 error = if_setlladdr(ifp,
1334 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1335 break;
1336
1337 default:
1338 error = ENOIOCTL;
1339 break;
1340 }
1341 return (error);
1342 }
1343
1344 /*
1345 * Interface ioctls.
1346 */
1347 int
1348 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1349 {
1350 struct ifnet *ifp;
1351 struct ifreq *ifr;
1352 int error;
1353 int oif_flags;
1354
1355 switch (cmd) {
1356 case SIOCGIFCONF:
1357 case OSIOCGIFCONF:
1358 return (ifconf(cmd, data));
1359 }
1360 ifr = (struct ifreq *)data;
1361
1362 switch (cmd) {
1363 case SIOCIFCREATE:
1364 case SIOCIFDESTROY:
1365 if ((error = suser(td)) != 0)
1366 return (error);
1367 return ((cmd == SIOCIFCREATE) ?
1368 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1369 if_clone_destroy(ifr->ifr_name));
1370
1371 case SIOCIFGCLONERS:
1372 return (if_clone_list((struct if_clonereq *)data));
1373 }
1374
1375 ifp = ifunit(ifr->ifr_name);
1376 if (ifp == 0)
1377 return (ENXIO);
1378
1379 error = ifhwioctl(cmd, ifp, data, td);
1380 if (error != ENOIOCTL)
1381 return (error);
1382
1383 oif_flags = ifp->if_flags;
1384 if (so->so_proto == 0)
1385 return (EOPNOTSUPP);
1386 #ifndef COMPAT_43
1387 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1388 data,
1389 ifp, td));
1390 #else
1391 {
1392 int ocmd = cmd;
1393
1394 switch (cmd) {
1395
1396 case SIOCSIFDSTADDR:
1397 case SIOCSIFADDR:
1398 case SIOCSIFBRDADDR:
1399 case SIOCSIFNETMASK:
1400 #if BYTE_ORDER != BIG_ENDIAN
1401 if (ifr->ifr_addr.sa_family == 0 &&
1402 ifr->ifr_addr.sa_len < 16) {
1403 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1404 ifr->ifr_addr.sa_len = 16;
1405 }
1406 #else
1407 if (ifr->ifr_addr.sa_len == 0)
1408 ifr->ifr_addr.sa_len = 16;
1409 #endif
1410 break;
1411
1412 case OSIOCGIFADDR:
1413 cmd = SIOCGIFADDR;
1414 break;
1415
1416 case OSIOCGIFDSTADDR:
1417 cmd = SIOCGIFDSTADDR;
1418 break;
1419
1420 case OSIOCGIFBRDADDR:
1421 cmd = SIOCGIFBRDADDR;
1422 break;
1423
1424 case OSIOCGIFNETMASK:
1425 cmd = SIOCGIFNETMASK;
1426 }
1427 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
1428 cmd,
1429 data,
1430 ifp, td));
1431 switch (ocmd) {
1432
1433 case OSIOCGIFADDR:
1434 case OSIOCGIFDSTADDR:
1435 case OSIOCGIFBRDADDR:
1436 case OSIOCGIFNETMASK:
1437 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1438
1439 }
1440 }
1441 #endif /* COMPAT_43 */
1442
1443 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1444 #ifdef INET6
1445 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1446 if (ifp->if_flags & IFF_UP) {
1447 int s = splimp();
1448 in6_if_up(ifp);
1449 splx(s);
1450 }
1451 #endif
1452 }
1453 return (error);
1454 }
1455
1456 /*
1457 * Set/clear promiscuous mode on interface ifp based on the truth value
1458 * of pswitch. The calls are reference counted so that only the first
1459 * "on" request actually has an effect, as does the final "off" request.
1460 * Results are undefined if the "off" and "on" requests are not matched.
1461 */
1462 int
1463 ifpromisc(struct ifnet *ifp, int pswitch)
1464 {
1465 struct ifreq ifr;
1466 int error;
1467 int oldflags, oldpcount;
1468
1469 oldpcount = ifp->if_pcount;
1470 oldflags = ifp->if_flags;
1471 if (ifp->if_flags & IFF_PPROMISC) {
1472 /* Do nothing if device is in permanently promiscuous mode */
1473 ifp->if_pcount += pswitch ? 1 : -1;
1474 return (0);
1475 }
1476 if (pswitch) {
1477 /*
1478 * If the device is not configured up, we cannot put it in
1479 * promiscuous mode.
1480 */
1481 if ((ifp->if_flags & IFF_UP) == 0)
1482 return (ENETDOWN);
1483 if (ifp->if_pcount++ != 0)
1484 return (0);
1485 ifp->if_flags |= IFF_PROMISC;
1486 } else {
1487 if (--ifp->if_pcount > 0)
1488 return (0);
1489 ifp->if_flags &= ~IFF_PROMISC;
1490 }
1491 ifr.ifr_flags = ifp->if_flags & 0xffff;
1492 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1493 IFF_LOCKGIANT(ifp);
1494 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1495 IFF_UNLOCKGIANT(ifp);
1496 if (error == 0) {
1497 log(LOG_INFO, "%s: promiscuous mode %s\n",
1498 ifp->if_xname,
1499 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1500 rt_ifmsg(ifp);
1501 } else {
1502 ifp->if_pcount = oldpcount;
1503 ifp->if_flags = oldflags;
1504 }
1505 return error;
1506 }
1507
1508 /*
1509 * Return interface configuration
1510 * of system. List may be used
1511 * in later ioctl's (above) to get
1512 * other information.
1513 */
1514 /*ARGSUSED*/
1515 static int
1516 ifconf(u_long cmd, caddr_t data)
1517 {
1518 struct ifconf *ifc = (struct ifconf *)data;
1519 struct ifnet *ifp;
1520 struct ifaddr *ifa;
1521 struct ifreq ifr;
1522 struct sbuf *sb;
1523 int error, full = 0, valid_len, max_len;
1524
1525 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
1526 max_len = MAXPHYS - 1;
1527
1528 /* Prevent hostile input from being able to crash the system */
1529 if (ifc->ifc_len <= 0)
1530 return (EINVAL);
1531
1532 again:
1533 if (ifc->ifc_len <= max_len) {
1534 max_len = ifc->ifc_len;
1535 full = 1;
1536 }
1537 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
1538 max_len = 0;
1539 valid_len = 0;
1540
1541 IFNET_RLOCK(); /* could sleep XXX */
1542 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1543 int addrs;
1544
1545 /*
1546 * Zero the ifr_name buffer to make sure we don't
1547 * disclose the contents of the stack.
1548 */
1549 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
1550
1551 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1552 >= sizeof(ifr.ifr_name))
1553 return (ENAMETOOLONG);
1554
1555 addrs = 0;
1556 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1557 struct sockaddr *sa = ifa->ifa_addr;
1558
1559 if (jailed(curthread->td_ucred) &&
1560 prison_if(curthread->td_ucred, sa))
1561 continue;
1562 addrs++;
1563 #ifdef COMPAT_43
1564 if (cmd == OSIOCGIFCONF) {
1565 struct osockaddr *osa =
1566 (struct osockaddr *)&ifr.ifr_addr;
1567 ifr.ifr_addr = *sa;
1568 osa->sa_family = sa->sa_family;
1569 sbuf_bcat(sb, &ifr, sizeof(ifr));
1570 max_len += sizeof(ifr);
1571 } else
1572 #endif
1573 if (sa->sa_len <= sizeof(*sa)) {
1574 ifr.ifr_addr = *sa;
1575 sbuf_bcat(sb, &ifr, sizeof(ifr));
1576 max_len += sizeof(ifr);
1577 } else {
1578 sbuf_bcat(sb, &ifr,
1579 offsetof(struct ifreq, ifr_addr));
1580 max_len += offsetof(struct ifreq, ifr_addr);
1581 sbuf_bcat(sb, sa, sa->sa_len);
1582 max_len += sa->sa_len;
1583 }
1584
1585 if (!sbuf_overflowed(sb))
1586 valid_len = sbuf_len(sb);
1587 }
1588 if (addrs == 0) {
1589 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1590 sbuf_bcat(sb, &ifr, sizeof(ifr));
1591 max_len += sizeof(ifr);
1592
1593 if (!sbuf_overflowed(sb))
1594 valid_len = sbuf_len(sb);
1595 }
1596 }
1597 IFNET_RUNLOCK();
1598
1599 /*
1600 * If we didn't allocate enough space (uncommon), try again. If
1601 * we have already allocated as much space as we are allowed,
1602 * return what we've got.
1603 */
1604 if (valid_len != max_len && !full) {
1605 sbuf_delete(sb);
1606 goto again;
1607 }
1608
1609 ifc->ifc_len = valid_len;
1610 sbuf_finish(sb);
1611 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
1612 sbuf_delete(sb);
1613 return (error);
1614 }
1615
1616 /*
1617 * Just like if_promisc(), but for all-multicast-reception mode.
1618 */
1619 int
1620 if_allmulti(struct ifnet *ifp, int onswitch)
1621 {
1622 int error = 0;
1623 int s = splimp();
1624 struct ifreq ifr;
1625
1626 if (onswitch) {
1627 if (ifp->if_amcount++ == 0) {
1628 ifp->if_flags |= IFF_ALLMULTI;
1629 ifr.ifr_flags = ifp->if_flags & 0xffff;
1630 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1631 IFF_LOCKGIANT(ifp);
1632 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1633 IFF_UNLOCKGIANT(ifp);
1634 }
1635 } else {
1636 if (ifp->if_amcount > 1) {
1637 ifp->if_amcount--;
1638 } else {
1639 ifp->if_amcount = 0;
1640 ifp->if_flags &= ~IFF_ALLMULTI;
1641 ifr.ifr_flags = ifp->if_flags & 0xffff;;
1642 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1643 IFF_LOCKGIANT(ifp);
1644 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1645 IFF_UNLOCKGIANT(ifp);
1646 }
1647 }
1648 splx(s);
1649
1650 if (error == 0)
1651 rt_ifmsg(ifp);
1652 return error;
1653 }
1654
1655 /*
1656 * Add a multicast listenership to the interface in question.
1657 * The link layer provides a routine which converts
1658 */
1659 int
1660 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, struct ifmultiaddr **retifma)
1661 {
1662 struct sockaddr *llsa, *dupsa;
1663 int error, s;
1664 struct ifmultiaddr *ifma;
1665
1666 /*
1667 * If the matching multicast address already exists
1668 * then don't add a new one, just add a reference
1669 */
1670 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1671 if (equal(sa, ifma->ifma_addr)) {
1672 ifma->ifma_refcount++;
1673 if (retifma)
1674 *retifma = ifma;
1675 return 0;
1676 }
1677 }
1678
1679 /*
1680 * Give the link layer a chance to accept/reject it, and also
1681 * find out which AF_LINK address this maps to, if it isn't one
1682 * already.
1683 */
1684 if (ifp->if_resolvemulti) {
1685 error = ifp->if_resolvemulti(ifp, &llsa, sa);
1686 if (error) return error;
1687 } else {
1688 llsa = 0;
1689 }
1690
1691 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1692 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1693 bcopy(sa, dupsa, sa->sa_len);
1694
1695 ifma->ifma_addr = dupsa;
1696 ifma->ifma_lladdr = llsa;
1697 ifma->ifma_ifp = ifp;
1698 ifma->ifma_refcount = 1;
1699 ifma->ifma_protospec = 0;
1700 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1701
1702 /*
1703 * Some network interfaces can scan the address list at
1704 * interrupt time; lock them out.
1705 */
1706 s = splimp();
1707 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1708 splx(s);
1709 if (retifma != NULL)
1710 *retifma = ifma;
1711
1712 if (llsa != 0) {
1713 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1714 if (equal(ifma->ifma_addr, llsa))
1715 break;
1716 }
1717 if (ifma) {
1718 ifma->ifma_refcount++;
1719 } else {
1720 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1721 M_IFMADDR, M_WAITOK);
1722 MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1723 M_IFMADDR, M_WAITOK);
1724 bcopy(llsa, dupsa, llsa->sa_len);
1725 ifma->ifma_addr = dupsa;
1726 ifma->ifma_lladdr = NULL;
1727 ifma->ifma_ifp = ifp;
1728 ifma->ifma_refcount = 1;
1729 ifma->ifma_protospec = 0;
1730 s = splimp();
1731 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1732 splx(s);
1733 }
1734 }
1735 /*
1736 * We are certain we have added something, so call down to the
1737 * interface to let them know about it.
1738 */
1739 s = splimp();
1740 IFF_LOCKGIANT(ifp);
1741 ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1742 IFF_UNLOCKGIANT(ifp);
1743 splx(s);
1744
1745 return 0;
1746 }
1747
1748 /*
1749 * Remove a reference to a multicast address on this interface. Yell
1750 * if the request does not match an existing membership.
1751 */
1752 int
1753 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1754 {
1755 struct ifmultiaddr *ifma;
1756 int s;
1757
1758 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1759 if (equal(sa, ifma->ifma_addr))
1760 break;
1761 if (ifma == 0)
1762 return ENOENT;
1763
1764 if (ifma->ifma_refcount > 1) {
1765 ifma->ifma_refcount--;
1766 return 0;
1767 }
1768
1769 rt_newmaddrmsg(RTM_DELMADDR, ifma);
1770 sa = ifma->ifma_lladdr;
1771 s = splimp();
1772 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1773 /*
1774 * Make sure the interface driver is notified
1775 * in the case of a link layer mcast group being left.
1776 */
1777 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) {
1778 IFF_LOCKGIANT(ifp);
1779 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1780 IFF_UNLOCKGIANT(ifp);
1781 }
1782 splx(s);
1783 free(ifma->ifma_addr, M_IFMADDR);
1784 free(ifma, M_IFMADDR);
1785 if (sa == 0)
1786 return 0;
1787
1788 /*
1789 * Now look for the link-layer address which corresponds to
1790 * this network address. It had been squirreled away in
1791 * ifma->ifma_lladdr for this purpose (so we don't have
1792 * to call ifp->if_resolvemulti() again), and we saved that
1793 * value in sa above. If some nasty deleted the
1794 * link-layer address out from underneath us, we can deal because
1795 * the address we stored was is not the same as the one which was
1796 * in the record for the link-layer address. (So we don't complain
1797 * in that case.)
1798 */
1799 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1800 if (equal(sa, ifma->ifma_addr))
1801 break;
1802 if (ifma == 0)
1803 return 0;
1804
1805 if (ifma->ifma_refcount > 1) {
1806 ifma->ifma_refcount--;
1807 return 0;
1808 }
1809
1810 s = splimp();
1811 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1812 IFF_LOCKGIANT(ifp);
1813 ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1814 IFF_UNLOCKGIANT(ifp);
1815 splx(s);
1816 free(ifma->ifma_addr, M_IFMADDR);
1817 free(sa, M_IFMADDR);
1818 free(ifma, M_IFMADDR);
1819
1820 return 0;
1821 }
1822
1823 /*
1824 * Set the link layer address on an interface.
1825 *
1826 * At this time we only support certain types of interfaces,
1827 * and we don't allow the length of the address to change.
1828 */
1829 int
1830 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1831 {
1832 struct sockaddr_dl *sdl;
1833 struct ifaddr *ifa;
1834 struct ifreq ifr;
1835
1836 ifa = ifaddr_byindex(ifp->if_index);
1837 if (ifa == NULL)
1838 return (EINVAL);
1839 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1840 if (sdl == NULL)
1841 return (EINVAL);
1842 if (len != sdl->sdl_alen) /* don't allow length to change */
1843 return (EINVAL);
1844 switch (ifp->if_type) {
1845 case IFT_ETHER: /* these types use struct arpcom */
1846 case IFT_FDDI:
1847 case IFT_XETHER:
1848 case IFT_ISO88025:
1849 case IFT_L2VLAN:
1850 bcopy(lladdr, IFP2AC(ifp)->ac_enaddr, len);
1851 /*
1852 * XXX We also need to store the lladdr in LLADDR(sdl),
1853 * which is done below. This is a pain because we must
1854 * remember to keep the info in sync.
1855 */
1856 /* FALLTHROUGH */
1857 case IFT_ARCNET:
1858 bcopy(lladdr, LLADDR(sdl), len);
1859 break;
1860 default:
1861 return (ENODEV);
1862 }
1863 /*
1864 * If the interface is already up, we need
1865 * to re-init it in order to reprogram its
1866 * address filter.
1867 */
1868 if ((ifp->if_flags & IFF_UP) != 0) {
1869 IFF_LOCKGIANT(ifp);
1870 ifp->if_flags &= ~IFF_UP;
1871 ifr.ifr_flags = ifp->if_flags & 0xffff;
1872 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1873 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1874 ifp->if_flags |= IFF_UP;
1875 ifr.ifr_flags = ifp->if_flags & 0xffff;
1876 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1877 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1878 IFF_UNLOCKGIANT(ifp);
1879 #ifdef INET
1880 /*
1881 * Also send gratuitous ARPs to notify other nodes about
1882 * the address change.
1883 */
1884 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1885 if (ifa->ifa_addr != NULL &&
1886 ifa->ifa_addr->sa_family == AF_INET)
1887 arp_ifinit(ifp, ifa);
1888 }
1889 #endif
1890 }
1891 return (0);
1892 }
1893
1894 struct ifmultiaddr *
1895 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
1896 {
1897 struct ifmultiaddr *ifma;
1898
1899 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1900 if (equal(ifma->ifma_addr, sa))
1901 break;
1902
1903 return ifma;
1904 }
1905
1906 /*
1907 * The name argument must be a pointer to storage which will last as
1908 * long as the interface does. For physical devices, the result of
1909 * device_get_name(dev) is a good choice and for pseudo-devices a
1910 * static string works well.
1911 */
1912 void
1913 if_initname(struct ifnet *ifp, const char *name, int unit)
1914 {
1915 ifp->if_dname = name;
1916 ifp->if_dunit = unit;
1917 if (unit != IF_DUNIT_NONE)
1918 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
1919 else
1920 strlcpy(ifp->if_xname, name, IFNAMSIZ);
1921 }
1922
1923 int
1924 if_printf(struct ifnet *ifp, const char * fmt, ...)
1925 {
1926 va_list ap;
1927 int retval;
1928
1929 retval = printf("%s: ", ifp->if_xname);
1930 va_start(ap, fmt);
1931 retval += vprintf(fmt, ap);
1932 va_end(ap);
1933 return (retval);
1934 }
1935
1936 /*
1937 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
1938 * be called without Giant. However, we often can't acquire the Giant lock
1939 * at those points; instead, we run it via a task queue that holds Giant via
1940 * if_start_deferred.
1941 *
1942 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
1943 * outstanding if_start_deferred() tasks that will run after the free. This
1944 * probably means waiting in if_detach().
1945 */
1946 void
1947 if_start(struct ifnet *ifp)
1948 {
1949
1950 NET_ASSERT_GIANT();
1951
1952 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) {
1953 if (mtx_owned(&Giant))
1954 (*(ifp)->if_start)(ifp);
1955 else
1956 taskqueue_enqueue(taskqueue_swi_giant,
1957 &ifp->if_starttask);
1958 } else
1959 (*(ifp)->if_start)(ifp);
1960 }
1961
1962 static void
1963 if_start_deferred(void *context, int pending)
1964 {
1965 struct ifnet *ifp;
1966
1967 /*
1968 * This code must be entered with Giant, and should never run if
1969 * we're not running with debug.mpsafenet.
1970 */
1971 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet"));
1972 GIANT_REQUIRED;
1973
1974 ifp = (struct ifnet *)context;
1975 (ifp->if_start)(ifp);
1976 }
1977
1978 int
1979 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
1980 {
1981 int active = 0;
1982
1983 IF_LOCK(ifq);
1984 if (_IF_QFULL(ifq)) {
1985 _IF_DROP(ifq);
1986 IF_UNLOCK(ifq);
1987 m_freem(m);
1988 return (0);
1989 }
1990 if (ifp != NULL) {
1991 ifp->if_obytes += m->m_pkthdr.len + adjust;
1992 if (m->m_flags & (M_BCAST|M_MCAST))
1993 ifp->if_omcasts++;
1994 active = ifp->if_flags & IFF_OACTIVE;
1995 }
1996 _IF_ENQUEUE(ifq, m);
1997 IF_UNLOCK(ifq);
1998 if (ifp != NULL && !active)
1999 if_start(ifp);
2000 return (1);
2001 }
2002
2003 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
2004 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
Cache object: 97a27e5a4c154c98ea9e8a4a944950c1
|