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/7.4/sys/net/if.c 215368 2010-11-16 04:40:03Z sobomax $
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/malloc.h>
43 #include <sys/sbuf.h>
44 #include <sys/bus.h>
45 #include <sys/mbuf.h>
46 #include <sys/systm.h>
47 #include <sys/priv.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_clone.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_var.h>
66 #include <net/radix.h>
67 #include <net/route.h>
68
69 #if defined(INET) || defined(INET6)
70 /*XXX*/
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #ifdef INET6
74 #include <netinet6/in6_var.h>
75 #include <netinet6/in6_ifattach.h>
76 #endif
77 #endif
78 #ifdef INET
79 #include <netinet/if_ether.h>
80 #endif
81 #ifdef DEV_CARP
82 #include <netinet/ip_carp.h>
83 #endif
84
85 #include <security/mac/mac_framework.h>
86
87 static int slowtimo_started;
88
89 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
90 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
91
92 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
93 SYSCTL_UINT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
94 &ifqmaxlen, 0, "max send queue size");
95
96 /* Log link state change events */
97 static int log_link_state_change = 1;
98
99 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
100 &log_link_state_change, 0,
101 "log interface link state change events");
102
103 void (*bstp_linkstate_p)(struct ifnet *ifp, int state);
104 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
105 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
106
107 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
108
109 /*
110 * XXX: Style; these should be sorted alphabetically, and unprototyped
111 * static functions should be prototyped. Currently they are sorted by
112 * declaration order.
113 */
114 static void if_attachdomain(void *);
115 static void if_attachdomain1(struct ifnet *);
116 static void if_purgemaddrs(struct ifnet *);
117 static int ifconf(u_long, caddr_t);
118 static void if_freemulti(struct ifmultiaddr *);
119 static void if_grow(void);
120 static void if_init(void *);
121 static void if_check(void *);
122 static void if_qflush(struct ifaltq *);
123 static void if_route(struct ifnet *, int flag, int fam);
124 static int if_setflag(struct ifnet *, int, int, int *, int);
125 static void if_slowtimo(void *);
126 static void if_unroute(struct ifnet *, int flag, int fam);
127 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
128 static int if_rtdel(struct radix_node *, void *);
129 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
130 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
131 static void if_start_deferred(void *context, int pending);
132 static void do_link_state_change(void *, int);
133 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
134 static int if_getgroupmembers(struct ifgroupreq *);
135 static void if_delgroups(struct ifnet *);
136 #ifdef INET6
137 /*
138 * XXX: declare here to avoid to include many inet6 related files..
139 * should be more generalized?
140 */
141 extern void nd6_setmtu(struct ifnet *);
142 #endif
143
144 int if_index = 0;
145 int ifqmaxlen = IFQ_MAXLEN;
146 struct ifnethead ifnet; /* depend on static init XXX */
147 struct ifgrouphead ifg_head;
148 struct mtx ifnet_lock;
149 static if_com_alloc_t *if_com_alloc[256];
150 static if_com_free_t *if_com_free[256];
151
152 static int if_indexlim = 8;
153 static struct knlist ifklist;
154
155 /*
156 * Table of ifnet/cdev by index. Locked with ifnet_lock.
157 */
158 static struct ifindex_entry *ifindex_table = NULL;
159
160 static void filt_netdetach(struct knote *kn);
161 static int filt_netdev(struct knote *kn, long hint);
162
163 static struct filterops netdev_filtops =
164 { 1, NULL, filt_netdetach, filt_netdev };
165
166 /*
167 * System initialization
168 */
169 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
170 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL);
171
172 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
173 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
174 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
175
176 struct ifnet *
177 ifnet_byindex(u_short idx)
178 {
179 struct ifnet *ifp;
180
181 IFNET_RLOCK();
182 ifp = ifindex_table[idx].ife_ifnet;
183 IFNET_RUNLOCK();
184 return (ifp);
185 }
186
187 static void
188 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
189 {
190
191 IFNET_WLOCK_ASSERT();
192
193 ifindex_table[idx].ife_ifnet = ifp;
194 }
195
196 struct ifaddr *
197 ifaddr_byindex(u_short idx)
198 {
199 struct ifaddr *ifa;
200
201 IFNET_RLOCK();
202 ifa = ifnet_byindex(idx)->if_addr;
203 IFNET_RUNLOCK();
204 return (ifa);
205 }
206
207 struct cdev *
208 ifdev_byindex(u_short idx)
209 {
210 struct cdev *cdev;
211
212 IFNET_RLOCK();
213 cdev = ifindex_table[idx].ife_dev;
214 IFNET_RUNLOCK();
215 return (cdev);
216 }
217
218 static void
219 ifdev_setbyindex(u_short idx, struct cdev *cdev)
220 {
221
222 IFNET_WLOCK();
223 ifindex_table[idx].ife_dev = cdev;
224 IFNET_WUNLOCK();
225 }
226
227 static d_open_t netopen;
228 static d_close_t netclose;
229 static d_ioctl_t netioctl;
230 static d_kqfilter_t netkqfilter;
231
232 static struct cdevsw net_cdevsw = {
233 .d_version = D_VERSION,
234 .d_flags = D_NEEDGIANT,
235 .d_open = netopen,
236 .d_close = netclose,
237 .d_ioctl = netioctl,
238 .d_name = "net",
239 .d_kqfilter = netkqfilter,
240 };
241
242 static int
243 netopen(struct cdev *dev, int flag, int mode, struct thread *td)
244 {
245 return (0);
246 }
247
248 static int
249 netclose(struct cdev *dev, int flags, int fmt, struct thread *td)
250 {
251 return (0);
252 }
253
254 static int
255 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
256 {
257 struct ifnet *ifp;
258 int error, idx;
259
260 /* only support interface specific ioctls */
261 if (IOCGROUP(cmd) != 'i')
262 return (EOPNOTSUPP);
263 idx = minor(dev);
264 if (idx == 0) {
265 /*
266 * special network device, not interface.
267 */
268 if (cmd == SIOCGIFCONF)
269 return (ifconf(cmd, data)); /* XXX remove cmd */
270 #ifdef __amd64__
271 if (cmd == SIOCGIFCONF32)
272 return (ifconf(cmd, data)); /* XXX remove cmd */
273 #endif
274 return (EOPNOTSUPP);
275 }
276
277 ifp = ifnet_byindex(idx);
278 if (ifp == NULL)
279 return (ENXIO);
280
281 error = ifhwioctl(cmd, ifp, data, td);
282 if (error == ENOIOCTL)
283 error = EOPNOTSUPP;
284 return (error);
285 }
286
287 static int
288 netkqfilter(struct cdev *dev, struct knote *kn)
289 {
290 struct knlist *klist;
291 struct ifnet *ifp;
292 int idx;
293
294 switch (kn->kn_filter) {
295 case EVFILT_NETDEV:
296 kn->kn_fop = &netdev_filtops;
297 break;
298 default:
299 return (EINVAL);
300 }
301
302 idx = minor(dev);
303 if (idx == 0) {
304 klist = &ifklist;
305 } else {
306 ifp = ifnet_byindex(idx);
307 if (ifp == NULL)
308 return (1);
309 klist = &ifp->if_klist;
310 }
311
312 kn->kn_hook = (caddr_t)klist;
313
314 knlist_add(klist, kn, 0);
315
316 return (0);
317 }
318
319 static void
320 filt_netdetach(struct knote *kn)
321 {
322 struct knlist *klist = (struct knlist *)kn->kn_hook;
323
324 knlist_remove(klist, kn, 0);
325 }
326
327 static int
328 filt_netdev(struct knote *kn, long hint)
329 {
330 struct knlist *klist = (struct knlist *)kn->kn_hook;
331
332 /*
333 * Currently NOTE_EXIT is abused to indicate device detach.
334 */
335 if (hint == NOTE_EXIT) {
336 kn->kn_data = NOTE_LINKINV;
337 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
338 knlist_remove_inevent(klist, kn);
339 return (1);
340 }
341 if (hint != 0)
342 kn->kn_data = hint; /* current status */
343 if (kn->kn_sfflags & hint)
344 kn->kn_fflags |= hint;
345 return (kn->kn_fflags != 0);
346 }
347
348 /*
349 * Network interface utility routines.
350 *
351 * Routines with ifa_ifwith* names take sockaddr *'s as
352 * parameters.
353 */
354
355 /* ARGSUSED*/
356 static void
357 if_init(void *dummy __unused)
358 {
359
360 IFNET_LOCK_INIT();
361 TAILQ_INIT(&ifnet);
362 TAILQ_INIT(&ifg_head);
363 knlist_init(&ifklist, NULL, NULL, NULL, NULL);
364 if_grow(); /* create initial table */
365 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL,
366 0600, "network"));
367 if_clone_init();
368 }
369
370 static void
371 if_grow(void)
372 {
373 u_int n;
374 struct ifindex_entry *e;
375
376 if_indexlim <<= 1;
377 n = if_indexlim * sizeof(*e);
378 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
379 if (ifindex_table != NULL) {
380 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
381 free((caddr_t)ifindex_table, M_IFNET);
382 }
383 ifindex_table = e;
384 }
385
386 /* ARGSUSED*/
387 static void
388 if_check(void *dummy __unused)
389 {
390 struct ifnet *ifp;
391 int s;
392
393 s = splimp();
394 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */
395 TAILQ_FOREACH(ifp, &ifnet, if_link) {
396 if (ifp->if_snd.ifq_maxlen == 0) {
397 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
398 ifp->if_snd.ifq_maxlen = ifqmaxlen;
399 }
400 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
401 if_printf(ifp,
402 "XXX: driver didn't initialize queue mtx\n");
403 mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
404 MTX_NETWORK_LOCK, MTX_DEF);
405 }
406 }
407 IFNET_RUNLOCK();
408 splx(s);
409
410 /*
411 * If at least one interface added during boot uses
412 * if_watchdog then start the timer.
413 */
414 if (slowtimo_started)
415 if_slowtimo(0);
416 }
417
418 /*
419 * Allocate a struct ifnet and an index for an interface. A layer 2
420 * common structure will also be allocated if an allocation routine is
421 * registered for the passed type.
422 */
423 struct ifnet*
424 if_alloc(u_char type)
425 {
426 struct ifnet *ifp;
427
428 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
429
430 /*
431 * Try to find an empty slot below if_index. If we fail, take
432 * the next slot.
433 *
434 * XXX: should be locked!
435 */
436 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) {
437 if (ifnet_byindex(ifp->if_index) == NULL)
438 break;
439 }
440 /* Catch if_index overflow. */
441 if (ifp->if_index < 1) {
442 free(ifp, M_IFNET);
443 return (NULL);
444 }
445 if (ifp->if_index > if_index)
446 if_index = ifp->if_index;
447 if (if_index >= if_indexlim)
448 if_grow();
449
450 ifp->if_type = type;
451
452 if (if_com_alloc[type] != NULL) {
453 ifp->if_l2com = if_com_alloc[type](type, ifp);
454 if (ifp->if_l2com == NULL) {
455 free(ifp, M_IFNET);
456 return (NULL);
457 }
458 }
459 IFNET_WLOCK();
460 ifnet_setbyindex(ifp->if_index, ifp);
461 IFNET_WUNLOCK();
462 IF_ADDR_LOCK_INIT(ifp);
463
464 return (ifp);
465 }
466
467 /*
468 * Free the struct ifnet, the associated index, and the layer 2 common
469 * structure if needed. All the work is done in if_free_type().
470 *
471 * Do not add code to this function! Add it to if_free_type().
472 */
473 void
474 if_free(struct ifnet *ifp)
475 {
476
477 if_free_type(ifp, ifp->if_type);
478 }
479
480 /*
481 * Do the actual work of freeing a struct ifnet, associated index, and
482 * layer 2 common structure. This version should only be called by
483 * intefaces that switch their type after calling if_alloc().
484 */
485 void
486 if_free_type(struct ifnet *ifp, u_char type)
487 {
488
489 if (ifp != ifnet_byindex(ifp->if_index)) {
490 if_printf(ifp, "%s: value was not if_alloced, skipping\n",
491 __func__);
492 return;
493 }
494
495 IFNET_WLOCK();
496 ifnet_setbyindex(ifp->if_index, NULL);
497
498 /* XXX: should be locked with if_findindex() */
499 while (if_index > 0 && ifnet_byindex(if_index) == NULL)
500 if_index--;
501 IFNET_WUNLOCK();
502
503 if (if_com_free[type] != NULL)
504 if_com_free[type](ifp->if_l2com, type);
505
506 IF_ADDR_LOCK_DESTROY(ifp);
507 free(ifp, M_IFNET);
508 };
509
510 /*
511 * Perform generic interface initalization tasks and attach the interface
512 * to the list of "active" interfaces.
513 *
514 * XXX:
515 * - The decision to return void and thus require this function to
516 * succeed is questionable.
517 * - We do more initialization here then is probably a good idea.
518 * Some of this should probably move to if_alloc().
519 * - We should probably do more sanity checking. For instance we don't
520 * do anything to insure if_xname is unique or non-empty.
521 */
522 void
523 if_attach(struct ifnet *ifp)
524 {
525 unsigned socksize, ifasize;
526 int namelen, masklen;
527 struct sockaddr_dl *sdl;
528 struct ifaddr *ifa;
529
530 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
531 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
532 ifp->if_xname);
533
534 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp);
535 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
536 IF_AFDATA_LOCK_INIT(ifp);
537 ifp->if_afdata_initialized = 0;
538
539 TAILQ_INIT(&ifp->if_addrhead);
540 TAILQ_INIT(&ifp->if_prefixhead);
541 TAILQ_INIT(&ifp->if_multiaddrs);
542 TAILQ_INIT(&ifp->if_groups);
543
544 if_addgroup(ifp, IFG_ALL);
545
546 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL);
547 getmicrotime(&ifp->if_lastchange);
548 ifp->if_data.ifi_epoch = time_uptime;
549 ifp->if_data.ifi_datalen = sizeof(struct if_data);
550
551 #ifdef MAC
552 mac_init_ifnet(ifp);
553 mac_create_ifnet(ifp);
554 #endif
555
556 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw,
557 unit2minor(ifp->if_index), UID_ROOT, GID_WHEEL, 0600, "%s/%s",
558 net_cdevsw.d_name, ifp->if_xname));
559 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
560 net_cdevsw.d_name, ifp->if_index);
561
562 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
563
564 /*
565 * create a Link Level name for this device
566 */
567 namelen = strlen(ifp->if_xname);
568 /*
569 * Always save enough space for any possiable name so we can do
570 * a rename in place later.
571 */
572 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
573 socksize = masklen + ifp->if_addrlen;
574 if (socksize < sizeof(*sdl))
575 socksize = sizeof(*sdl);
576 socksize = roundup2(socksize, sizeof(long));
577 ifasize = sizeof(*ifa) + 2 * socksize;
578 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
579 IFA_LOCK_INIT(ifa);
580 sdl = (struct sockaddr_dl *)(ifa + 1);
581 sdl->sdl_len = socksize;
582 sdl->sdl_family = AF_LINK;
583 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
584 sdl->sdl_nlen = namelen;
585 sdl->sdl_index = ifp->if_index;
586 sdl->sdl_type = ifp->if_type;
587 ifp->if_addr = ifa;
588 ifa->ifa_ifp = ifp;
589 ifa->ifa_rtrequest = link_rtrequest;
590 ifa->ifa_addr = (struct sockaddr *)sdl;
591 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
592 ifa->ifa_netmask = (struct sockaddr *)sdl;
593 sdl->sdl_len = masklen;
594 while (namelen != 0)
595 sdl->sdl_data[--namelen] = 0xff;
596 ifa->ifa_refcnt = 1;
597 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
598 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */
599 ifp->if_snd.altq_type = 0;
600 ifp->if_snd.altq_disc = NULL;
601 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
602 ifp->if_snd.altq_tbr = NULL;
603 ifp->if_snd.altq_ifp = ifp;
604
605 IFNET_WLOCK();
606 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
607 IFNET_WUNLOCK();
608
609 if (domain_init_status >= 2)
610 if_attachdomain1(ifp);
611
612 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
613 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
614
615 /* Announce the interface. */
616 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
617
618 if (ifp->if_watchdog != NULL) {
619 if_printf(ifp,
620 "WARNING: using obsoleted if_watchdog interface\n");
621
622 /*
623 * Note that we need if_slowtimo(). If this happens after
624 * boot, then call if_slowtimo() directly.
625 */
626 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold)
627 if_slowtimo(0);
628 }
629 if (ifp->if_flags & IFF_NEEDSGIANT)
630 if_printf(ifp,
631 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n");
632 }
633
634 static void
635 if_attachdomain(void *dummy)
636 {
637 struct ifnet *ifp;
638 int s;
639
640 s = splnet();
641 TAILQ_FOREACH(ifp, &ifnet, if_link)
642 if_attachdomain1(ifp);
643 splx(s);
644 }
645 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
646 if_attachdomain, NULL);
647
648 static void
649 if_attachdomain1(struct ifnet *ifp)
650 {
651 struct domain *dp;
652 int s;
653
654 s = splnet();
655
656 /*
657 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
658 * cannot lock ifp->if_afdata initialization, entirely.
659 */
660 if (IF_AFDATA_TRYLOCK(ifp) == 0) {
661 splx(s);
662 return;
663 }
664 if (ifp->if_afdata_initialized >= domain_init_status) {
665 IF_AFDATA_UNLOCK(ifp);
666 splx(s);
667 printf("if_attachdomain called more than once on %s\n",
668 ifp->if_xname);
669 return;
670 }
671 ifp->if_afdata_initialized = domain_init_status;
672 IF_AFDATA_UNLOCK(ifp);
673
674 /* address family dependent data region */
675 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
676 for (dp = domains; dp; dp = dp->dom_next) {
677 if (dp->dom_ifattach)
678 ifp->if_afdata[dp->dom_family] =
679 (*dp->dom_ifattach)(ifp);
680 }
681
682 splx(s);
683 }
684
685 /*
686 * Remove any unicast or broadcast network addresses from an interface.
687 */
688 void
689 if_purgeaddrs(struct ifnet *ifp)
690 {
691 struct ifaddr *ifa, *next;
692
693 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
694 if (ifa->ifa_addr->sa_family == AF_LINK)
695 continue;
696 #ifdef INET
697 /* XXX: Ugly!! ad hoc just for INET */
698 if (ifa->ifa_addr->sa_family == AF_INET) {
699 struct ifaliasreq ifr;
700
701 bzero(&ifr, sizeof(ifr));
702 ifr.ifra_addr = *ifa->ifa_addr;
703 if (ifa->ifa_dstaddr)
704 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
705 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
706 NULL) == 0)
707 continue;
708 }
709 #endif /* INET */
710 #ifdef INET6
711 if (ifa->ifa_addr->sa_family == AF_INET6) {
712 in6_purgeaddr(ifa);
713 /* ifp_addrhead is already updated */
714 continue;
715 }
716 #endif /* INET6 */
717 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
718 IFAFREE(ifa);
719 }
720 }
721
722 /*
723 * Remove any multicast network addresses from an interface.
724 */
725 static void
726 if_purgemaddrs(struct ifnet *ifp)
727 {
728 struct ifmultiaddr *ifma;
729 struct ifmultiaddr *next;
730
731 IF_ADDR_LOCK(ifp);
732 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
733 if_delmulti_locked(ifp, ifma, 1);
734 IF_ADDR_UNLOCK(ifp);
735 }
736
737 /*
738 * Detach an interface, removing it from the
739 * list of "active" interfaces.
740 *
741 * XXXRW: There are some significant questions about event ordering, and
742 * how to prevent things from starting to use the interface during detach.
743 */
744 void
745 if_detach(struct ifnet *ifp)
746 {
747 struct ifaddr *ifa;
748 struct radix_node_head *rnh;
749 int s;
750 int i;
751 struct domain *dp;
752 struct ifnet *iter;
753 int found = 0;
754
755 IFNET_WLOCK();
756 TAILQ_FOREACH(iter, &ifnet, if_link)
757 if (iter == ifp) {
758 TAILQ_REMOVE(&ifnet, ifp, if_link);
759 found = 1;
760 break;
761 }
762 IFNET_WUNLOCK();
763 if (!found)
764 return;
765
766 /*
767 * Remove/wait for pending events.
768 */
769 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
770
771 /*
772 * Remove routes and flush queues.
773 */
774 s = splnet();
775 if_down(ifp);
776 #ifdef ALTQ
777 if (ALTQ_IS_ENABLED(&ifp->if_snd))
778 altq_disable(&ifp->if_snd);
779 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
780 altq_detach(&ifp->if_snd);
781 #endif
782
783 if_purgeaddrs(ifp);
784
785 #ifdef INET
786 in_ifdetach(ifp);
787 #endif
788
789 #ifdef INET6
790 /*
791 * Remove all IPv6 kernel structs related to ifp. This should be done
792 * before removing routing entries below, since IPv6 interface direct
793 * routes are expected to be removed by the IPv6-specific kernel API.
794 * Otherwise, the kernel will detect some inconsistency and bark it.
795 */
796 in6_ifdetach(ifp);
797 #endif
798 if_purgemaddrs(ifp);
799
800 /*
801 * Remove link ifaddr pointer and maybe decrement if_index.
802 * Clean up all addresses.
803 */
804 ifp->if_addr = NULL;
805 destroy_dev(ifdev_byindex(ifp->if_index));
806 ifdev_setbyindex(ifp->if_index, NULL);
807
808 /* We can now free link ifaddr. */
809 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
810 ifa = TAILQ_FIRST(&ifp->if_addrhead);
811 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
812 IFAFREE(ifa);
813 }
814
815 /*
816 * Delete all remaining routes using this interface
817 * Unfortuneatly the only way to do this is to slog through
818 * the entire routing table looking for routes which point
819 * to this interface...oh well...
820 */
821 for (i = 1; i <= AF_MAX; i++) {
822 int j;
823 for (j = 0; j < rt_numfibs; j++) {
824 if ((rnh = rt_tables[j][i]) == NULL)
825 continue;
826 RADIX_NODE_HEAD_LOCK(rnh);
827 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
828 RADIX_NODE_HEAD_UNLOCK(rnh);
829 }
830 }
831
832 /* Announce that the interface is gone. */
833 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
834 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
835 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
836 if_delgroups(ifp);
837
838 IF_AFDATA_LOCK(ifp);
839 for (dp = domains; dp; dp = dp->dom_next) {
840 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
841 (*dp->dom_ifdetach)(ifp,
842 ifp->if_afdata[dp->dom_family]);
843 }
844 IF_AFDATA_UNLOCK(ifp);
845
846 #ifdef MAC
847 mac_destroy_ifnet(ifp);
848 #endif /* MAC */
849 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT);
850 knlist_clear(&ifp->if_klist, 0);
851 knlist_destroy(&ifp->if_klist);
852 mtx_destroy(&ifp->if_snd.ifq_mtx);
853 IF_AFDATA_DESTROY(ifp);
854 splx(s);
855 }
856
857 /*
858 * Add a group to an interface
859 */
860 int
861 if_addgroup(struct ifnet *ifp, const char *groupname)
862 {
863 struct ifg_list *ifgl;
864 struct ifg_group *ifg = NULL;
865 struct ifg_member *ifgm;
866
867 if (groupname[0] && groupname[strlen(groupname) - 1] >= '' &&
868 groupname[strlen(groupname) - 1] <= '9')
869 return (EINVAL);
870
871 IFNET_WLOCK();
872 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
873 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
874 IFNET_WUNLOCK();
875 return (EEXIST);
876 }
877
878 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
879 M_NOWAIT)) == NULL) {
880 IFNET_WUNLOCK();
881 return (ENOMEM);
882 }
883
884 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
885 M_TEMP, M_NOWAIT)) == NULL) {
886 free(ifgl, M_TEMP);
887 IFNET_WUNLOCK();
888 return (ENOMEM);
889 }
890
891 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
892 if (!strcmp(ifg->ifg_group, groupname))
893 break;
894
895 if (ifg == NULL) {
896 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
897 M_TEMP, M_NOWAIT)) == NULL) {
898 free(ifgl, M_TEMP);
899 free(ifgm, M_TEMP);
900 IFNET_WUNLOCK();
901 return (ENOMEM);
902 }
903 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
904 ifg->ifg_refcnt = 0;
905 TAILQ_INIT(&ifg->ifg_members);
906 EVENTHANDLER_INVOKE(group_attach_event, ifg);
907 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
908 }
909
910 ifg->ifg_refcnt++;
911 ifgl->ifgl_group = ifg;
912 ifgm->ifgm_ifp = ifp;
913
914 IF_ADDR_LOCK(ifp);
915 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
916 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
917 IF_ADDR_UNLOCK(ifp);
918
919 IFNET_WUNLOCK();
920
921 EVENTHANDLER_INVOKE(group_change_event, groupname);
922
923 return (0);
924 }
925
926 /*
927 * Remove a group from an interface
928 */
929 int
930 if_delgroup(struct ifnet *ifp, const char *groupname)
931 {
932 struct ifg_list *ifgl;
933 struct ifg_member *ifgm;
934
935 IFNET_WLOCK();
936 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
937 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
938 break;
939 if (ifgl == NULL) {
940 IFNET_WUNLOCK();
941 return (ENOENT);
942 }
943
944 IF_ADDR_LOCK(ifp);
945 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
946 IF_ADDR_UNLOCK(ifp);
947
948 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
949 if (ifgm->ifgm_ifp == ifp)
950 break;
951
952 if (ifgm != NULL) {
953 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
954 free(ifgm, M_TEMP);
955 }
956
957 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
958 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
959 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
960 free(ifgl->ifgl_group, M_TEMP);
961 }
962 IFNET_WUNLOCK();
963
964 free(ifgl, M_TEMP);
965
966 EVENTHANDLER_INVOKE(group_change_event, groupname);
967
968 return (0);
969 }
970
971 /*
972 * Remove an interface from all groups
973 */
974 static void
975 if_delgroups(struct ifnet *ifp)
976 {
977 struct ifg_list *ifgl;
978 struct ifg_member *ifgm;
979 char groupname[IFNAMSIZ];
980
981 IFNET_WLOCK();
982 while (!TAILQ_EMPTY(&ifp->if_groups)) {
983 ifgl = TAILQ_FIRST(&ifp->if_groups);
984
985 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
986
987 IF_ADDR_LOCK(ifp);
988 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
989 IF_ADDR_UNLOCK(ifp);
990
991 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
992 if (ifgm->ifgm_ifp == ifp)
993 break;
994
995 if (ifgm != NULL) {
996 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
997 ifgm_next);
998 free(ifgm, M_TEMP);
999 }
1000
1001 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1002 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1003 EVENTHANDLER_INVOKE(group_detach_event,
1004 ifgl->ifgl_group);
1005 free(ifgl->ifgl_group, M_TEMP);
1006 }
1007 IFNET_WUNLOCK();
1008
1009 free(ifgl, M_TEMP);
1010
1011 EVENTHANDLER_INVOKE(group_change_event, groupname);
1012
1013 IFNET_WLOCK();
1014 }
1015 IFNET_WUNLOCK();
1016 }
1017
1018 /*
1019 * Stores all groups from an interface in memory pointed
1020 * to by data
1021 */
1022 static int
1023 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1024 {
1025 int len, error;
1026 struct ifg_list *ifgl;
1027 struct ifg_req ifgrq, *ifgp;
1028 struct ifgroupreq *ifgr = data;
1029
1030 if (ifgr->ifgr_len == 0) {
1031 IF_ADDR_LOCK(ifp);
1032 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1033 ifgr->ifgr_len += sizeof(struct ifg_req);
1034 IF_ADDR_UNLOCK(ifp);
1035 return (0);
1036 }
1037
1038 len = ifgr->ifgr_len;
1039 ifgp = ifgr->ifgr_groups;
1040 /* XXX: wire */
1041 IF_ADDR_LOCK(ifp);
1042 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1043 if (len < sizeof(ifgrq)) {
1044 IF_ADDR_UNLOCK(ifp);
1045 return (EINVAL);
1046 }
1047 bzero(&ifgrq, sizeof ifgrq);
1048 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1049 sizeof(ifgrq.ifgrq_group));
1050 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1051 IF_ADDR_UNLOCK(ifp);
1052 return (error);
1053 }
1054 len -= sizeof(ifgrq);
1055 ifgp++;
1056 }
1057 IF_ADDR_UNLOCK(ifp);
1058
1059 return (0);
1060 }
1061
1062 /*
1063 * Stores all members of a group in memory pointed to by data
1064 */
1065 static int
1066 if_getgroupmembers(struct ifgroupreq *data)
1067 {
1068 struct ifgroupreq *ifgr = data;
1069 struct ifg_group *ifg;
1070 struct ifg_member *ifgm;
1071 struct ifg_req ifgrq, *ifgp;
1072 int len, error;
1073
1074 IFNET_RLOCK();
1075 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1076 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1077 break;
1078 if (ifg == NULL) {
1079 IFNET_RUNLOCK();
1080 return (ENOENT);
1081 }
1082
1083 if (ifgr->ifgr_len == 0) {
1084 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1085 ifgr->ifgr_len += sizeof(ifgrq);
1086 IFNET_RUNLOCK();
1087 return (0);
1088 }
1089
1090 len = ifgr->ifgr_len;
1091 ifgp = ifgr->ifgr_groups;
1092 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1093 if (len < sizeof(ifgrq)) {
1094 IFNET_RUNLOCK();
1095 return (EINVAL);
1096 }
1097 bzero(&ifgrq, sizeof ifgrq);
1098 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1099 sizeof(ifgrq.ifgrq_member));
1100 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1101 IFNET_RUNLOCK();
1102 return (error);
1103 }
1104 len -= sizeof(ifgrq);
1105 ifgp++;
1106 }
1107 IFNET_RUNLOCK();
1108
1109 return (0);
1110 }
1111
1112 /*
1113 * Delete Routes for a Network Interface
1114 *
1115 * Called for each routing entry via the rnh->rnh_walktree() call above
1116 * to delete all route entries referencing a detaching network interface.
1117 *
1118 * Arguments:
1119 * rn pointer to node in the routing table
1120 * arg argument passed to rnh->rnh_walktree() - detaching interface
1121 *
1122 * Returns:
1123 * 0 successful
1124 * errno failed - reason indicated
1125 *
1126 */
1127 static int
1128 if_rtdel(struct radix_node *rn, void *arg)
1129 {
1130 struct rtentry *rt = (struct rtentry *)rn;
1131 struct ifnet *ifp = arg;
1132 int err;
1133
1134 if (rt->rt_ifp == ifp) {
1135
1136 /*
1137 * Protect (sorta) against walktree recursion problems
1138 * with cloned routes
1139 */
1140 if ((rt->rt_flags & RTF_UP) == 0)
1141 return (0);
1142
1143 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1144 rt_mask(rt), rt->rt_flags,
1145 (struct rtentry **) NULL, rt->rt_fibnum);
1146 if (err) {
1147 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1148 }
1149 }
1150
1151 return (0);
1152 }
1153
1154 /*
1155 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1156 * structs used to represent other address families, it is necessary
1157 * to perform a different comparison.
1158 */
1159
1160 #define sa_equal(a1, a2) \
1161 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1162
1163 #define sa_dl_equal(a1, a2) \
1164 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1165 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1166 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1167 LLADDR((struct sockaddr_dl *)(a2)), \
1168 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1169
1170 /*
1171 * Locate an interface based on a complete address.
1172 */
1173 /*ARGSUSED*/
1174 struct ifaddr *
1175 ifa_ifwithaddr(struct sockaddr *addr)
1176 {
1177 struct ifnet *ifp;
1178 struct ifaddr *ifa;
1179
1180 IFNET_RLOCK();
1181 TAILQ_FOREACH(ifp, &ifnet, if_link)
1182 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1183 if (ifa->ifa_addr->sa_family != addr->sa_family)
1184 continue;
1185 if (sa_equal(addr, ifa->ifa_addr))
1186 goto done;
1187 /* IP6 doesn't have broadcast */
1188 if ((ifp->if_flags & IFF_BROADCAST) &&
1189 ifa->ifa_broadaddr &&
1190 ifa->ifa_broadaddr->sa_len != 0 &&
1191 sa_equal(ifa->ifa_broadaddr, addr))
1192 goto done;
1193 }
1194 ifa = NULL;
1195 done:
1196 IFNET_RUNLOCK();
1197 return (ifa);
1198 }
1199
1200 /*
1201 * Locate an interface based on the broadcast address.
1202 */
1203 /* ARGSUSED */
1204 struct ifaddr *
1205 ifa_ifwithbroadaddr(struct sockaddr *addr)
1206 {
1207 struct ifnet *ifp;
1208 struct ifaddr *ifa;
1209
1210 IFNET_RLOCK();
1211 TAILQ_FOREACH(ifp, &ifnet, if_link)
1212 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1213 if (ifa->ifa_addr->sa_family != addr->sa_family)
1214 continue;
1215 if ((ifp->if_flags & IFF_BROADCAST) &&
1216 ifa->ifa_broadaddr &&
1217 ifa->ifa_broadaddr->sa_len != 0 &&
1218 sa_equal(ifa->ifa_broadaddr, addr))
1219 goto done;
1220 }
1221 ifa = NULL;
1222 done:
1223 IFNET_RUNLOCK();
1224 return (ifa);
1225 }
1226
1227 /*
1228 * Locate the point to point interface with a given destination address.
1229 */
1230 /*ARGSUSED*/
1231 struct ifaddr *
1232 ifa_ifwithdstaddr(struct sockaddr *addr)
1233 {
1234 struct ifnet *ifp;
1235 struct ifaddr *ifa;
1236
1237 IFNET_RLOCK();
1238 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1239 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1240 continue;
1241 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1242 if (ifa->ifa_addr->sa_family != addr->sa_family)
1243 continue;
1244 if (ifa->ifa_dstaddr != NULL &&
1245 sa_equal(addr, ifa->ifa_dstaddr))
1246 goto done;
1247 }
1248 }
1249 ifa = NULL;
1250 done:
1251 IFNET_RUNLOCK();
1252 return (ifa);
1253 }
1254
1255 /*
1256 * Find an interface on a specific network. If many, choice
1257 * is most specific found.
1258 */
1259 struct ifaddr *
1260 ifa_ifwithnet(struct sockaddr *addr)
1261 {
1262 struct ifnet *ifp;
1263 struct ifaddr *ifa;
1264 struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
1265 u_int af = addr->sa_family;
1266 char *addr_data = addr->sa_data, *cplim;
1267
1268 /*
1269 * AF_LINK addresses can be looked up directly by their index number,
1270 * so do that if we can.
1271 */
1272 if (af == AF_LINK) {
1273 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1274 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1275 return (ifaddr_byindex(sdl->sdl_index));
1276 }
1277
1278 /*
1279 * Scan though each interface, looking for ones that have
1280 * addresses in this address family.
1281 */
1282 IFNET_RLOCK();
1283 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1284 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1285 char *cp, *cp2, *cp3;
1286
1287 if (ifa->ifa_addr->sa_family != af)
1288 next: continue;
1289 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1290 /*
1291 * This is a bit broken as it doesn't
1292 * take into account that the remote end may
1293 * be a single node in the network we are
1294 * looking for.
1295 * The trouble is that we don't know the
1296 * netmask for the remote end.
1297 */
1298 if (ifa->ifa_dstaddr != NULL &&
1299 sa_equal(addr, ifa->ifa_dstaddr))
1300 goto done;
1301 } else {
1302 /*
1303 * if we have a special address handler,
1304 * then use it instead of the generic one.
1305 */
1306 if (ifa->ifa_claim_addr) {
1307 if ((*ifa->ifa_claim_addr)(ifa, addr))
1308 goto done;
1309 continue;
1310 }
1311
1312 /*
1313 * Scan all the bits in the ifa's address.
1314 * If a bit dissagrees with what we are
1315 * looking for, mask it with the netmask
1316 * to see if it really matters.
1317 * (A byte at a time)
1318 */
1319 if (ifa->ifa_netmask == 0)
1320 continue;
1321 cp = addr_data;
1322 cp2 = ifa->ifa_addr->sa_data;
1323 cp3 = ifa->ifa_netmask->sa_data;
1324 cplim = ifa->ifa_netmask->sa_len
1325 + (char *)ifa->ifa_netmask;
1326 while (cp3 < cplim)
1327 if ((*cp++ ^ *cp2++) & *cp3++)
1328 goto next; /* next address! */
1329 /*
1330 * If the netmask of what we just found
1331 * is more specific than what we had before
1332 * (if we had one) then remember the new one
1333 * before continuing to search
1334 * for an even better one.
1335 */
1336 if (ifa_maybe == 0 ||
1337 rn_refines((caddr_t)ifa->ifa_netmask,
1338 (caddr_t)ifa_maybe->ifa_netmask))
1339 ifa_maybe = ifa;
1340 }
1341 }
1342 }
1343 ifa = ifa_maybe;
1344 done:
1345 IFNET_RUNLOCK();
1346 return (ifa);
1347 }
1348
1349 /*
1350 * Find an interface address specific to an interface best matching
1351 * a given address.
1352 */
1353 struct ifaddr *
1354 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1355 {
1356 struct ifaddr *ifa;
1357 char *cp, *cp2, *cp3;
1358 char *cplim;
1359 struct ifaddr *ifa_maybe = 0;
1360 u_int af = addr->sa_family;
1361
1362 if (af >= AF_MAX)
1363 return (NULL);
1364 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1365 if (ifa->ifa_addr->sa_family != af)
1366 continue;
1367 if (ifa_maybe == 0)
1368 ifa_maybe = ifa;
1369 if (ifa->ifa_netmask == 0) {
1370 if (sa_equal(addr, ifa->ifa_addr) ||
1371 (ifa->ifa_dstaddr &&
1372 sa_equal(addr, ifa->ifa_dstaddr)))
1373 goto done;
1374 continue;
1375 }
1376 if (ifp->if_flags & IFF_POINTOPOINT) {
1377 if (sa_equal(addr, ifa->ifa_dstaddr))
1378 goto done;
1379 } else {
1380 cp = addr->sa_data;
1381 cp2 = ifa->ifa_addr->sa_data;
1382 cp3 = ifa->ifa_netmask->sa_data;
1383 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1384 for (; cp3 < cplim; cp3++)
1385 if ((*cp++ ^ *cp2++) & *cp3)
1386 break;
1387 if (cp3 == cplim)
1388 goto done;
1389 }
1390 }
1391 ifa = ifa_maybe;
1392 done:
1393 return (ifa);
1394 }
1395
1396 #include <net/route.h>
1397
1398 /*
1399 * Default action when installing a route with a Link Level gateway.
1400 * Lookup an appropriate real ifa to point to.
1401 * This should be moved to /sys/net/link.c eventually.
1402 */
1403 static void
1404 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1405 {
1406 struct ifaddr *ifa, *oifa;
1407 struct sockaddr *dst;
1408 struct ifnet *ifp;
1409
1410 RT_LOCK_ASSERT(rt);
1411
1412 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1413 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1414 return;
1415 ifa = ifaof_ifpforaddr(dst, ifp);
1416 if (ifa) {
1417 IFAREF(ifa); /* XXX */
1418 oifa = rt->rt_ifa;
1419 rt->rt_ifa = ifa;
1420 IFAFREE(oifa);
1421 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1422 ifa->ifa_rtrequest(cmd, rt, info);
1423 }
1424 }
1425
1426 /*
1427 * Mark an interface down and notify protocols of
1428 * the transition.
1429 * NOTE: must be called at splnet or eqivalent.
1430 */
1431 static void
1432 if_unroute(struct ifnet *ifp, int flag, int fam)
1433 {
1434 struct ifaddr *ifa;
1435
1436 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1437
1438 ifp->if_flags &= ~flag;
1439 getmicrotime(&ifp->if_lastchange);
1440 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1441 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1442 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1443 if_qflush(&ifp->if_snd);
1444 #ifdef DEV_CARP
1445 if (ifp->if_carp)
1446 carp_carpdev_state(ifp->if_carp);
1447 #endif
1448 rt_ifmsg(ifp);
1449 }
1450
1451 /*
1452 * Mark an interface up and notify protocols of
1453 * the transition.
1454 * NOTE: must be called at splnet or eqivalent.
1455 */
1456 static void
1457 if_route(struct ifnet *ifp, int flag, int fam)
1458 {
1459 struct ifaddr *ifa;
1460
1461 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1462
1463 ifp->if_flags |= flag;
1464 getmicrotime(&ifp->if_lastchange);
1465 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1466 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1467 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1468 #ifdef DEV_CARP
1469 if (ifp->if_carp)
1470 carp_carpdev_state(ifp->if_carp);
1471 #endif
1472 rt_ifmsg(ifp);
1473 #ifdef INET6
1474 in6_if_up(ifp);
1475 #endif
1476 }
1477
1478 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */
1479 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1480
1481 /*
1482 * Handle a change in the interface link state. To avoid LORs
1483 * between driver lock and upper layer locks, as well as possible
1484 * recursions, we post event to taskqueue, and all job
1485 * is done in static do_link_state_change().
1486 */
1487 void
1488 if_link_state_change(struct ifnet *ifp, int link_state)
1489 {
1490 /* Return if state hasn't changed. */
1491 if (ifp->if_link_state == link_state)
1492 return;
1493
1494 ifp->if_link_state = link_state;
1495
1496 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1497 }
1498
1499 static void
1500 do_link_state_change(void *arg, int pending)
1501 {
1502 struct ifnet *ifp = (struct ifnet *)arg;
1503 int link_state = ifp->if_link_state;
1504 int link;
1505
1506 /* Notify that the link state has changed. */
1507 rt_ifmsg(ifp);
1508 if (link_state == LINK_STATE_UP)
1509 link = NOTE_LINKUP;
1510 else if (link_state == LINK_STATE_DOWN)
1511 link = NOTE_LINKDOWN;
1512 else
1513 link = NOTE_LINKINV;
1514 KNOTE_UNLOCKED(&ifp->if_klist, link);
1515 if (ifp->if_vlantrunk != NULL)
1516 (*vlan_link_state_p)(ifp, link);
1517
1518 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1519 IFP2AC(ifp)->ac_netgraph != NULL)
1520 (*ng_ether_link_state_p)(ifp, link_state);
1521 #ifdef DEV_CARP
1522 if (ifp->if_carp)
1523 carp_carpdev_state(ifp->if_carp);
1524 #endif
1525 if (ifp->if_bridge) {
1526 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!"));
1527 (*bstp_linkstate_p)(ifp, link_state);
1528 }
1529 if (ifp->if_lagg) {
1530 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!"));
1531 (*lagg_linkstate_p)(ifp, link_state);
1532 }
1533
1534 devctl_notify("IFNET", ifp->if_xname,
1535 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1536 if (pending > 1)
1537 if_printf(ifp, "%d link states coalesced\n", pending);
1538 if (log_link_state_change)
1539 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1540 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1541 }
1542
1543 /*
1544 * Mark an interface down and notify protocols of
1545 * the transition.
1546 * NOTE: must be called at splnet or eqivalent.
1547 */
1548 void
1549 if_down(struct ifnet *ifp)
1550 {
1551
1552 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1553 }
1554
1555 /*
1556 * Mark an interface up and notify protocols of
1557 * the transition.
1558 * NOTE: must be called at splnet or eqivalent.
1559 */
1560 void
1561 if_up(struct ifnet *ifp)
1562 {
1563
1564 if_route(ifp, IFF_UP, AF_UNSPEC);
1565 }
1566
1567 /*
1568 * Flush an interface queue.
1569 */
1570 static void
1571 if_qflush(struct ifaltq *ifq)
1572 {
1573 struct mbuf *m, *n;
1574
1575 IFQ_LOCK(ifq);
1576 #ifdef ALTQ
1577 if (ALTQ_IS_ENABLED(ifq))
1578 ALTQ_PURGE(ifq);
1579 #endif
1580 n = ifq->ifq_head;
1581 while ((m = n) != 0) {
1582 n = m->m_act;
1583 m_freem(m);
1584 }
1585 ifq->ifq_head = 0;
1586 ifq->ifq_tail = 0;
1587 ifq->ifq_len = 0;
1588 IFQ_UNLOCK(ifq);
1589 }
1590
1591 /*
1592 * Handle interface watchdog timer routines. Called
1593 * from softclock, we decrement timers (if set) and
1594 * call the appropriate interface routine on expiration.
1595 *
1596 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called
1597 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab
1598 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface.
1599 */
1600 static void
1601 if_slowtimo(void *arg)
1602 {
1603 struct ifnet *ifp;
1604 int s = splimp();
1605
1606 IFNET_RLOCK();
1607 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1608 if (ifp->if_timer == 0 || --ifp->if_timer)
1609 continue;
1610 if (ifp->if_watchdog)
1611 (*ifp->if_watchdog)(ifp);
1612 }
1613 IFNET_RUNLOCK();
1614 splx(s);
1615 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1616 }
1617
1618 /*
1619 * Map interface name to
1620 * interface structure pointer.
1621 */
1622 struct ifnet *
1623 ifunit(const char *name)
1624 {
1625 struct ifnet *ifp;
1626
1627 IFNET_RLOCK();
1628 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1629 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1630 break;
1631 }
1632 IFNET_RUNLOCK();
1633 return (ifp);
1634 }
1635
1636 /*
1637 * Hardware specific interface ioctls.
1638 */
1639 static int
1640 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1641 {
1642 struct ifreq *ifr;
1643 struct ifstat *ifs;
1644 int error = 0;
1645 int new_flags, temp_flags;
1646 size_t namelen, onamelen;
1647 char new_name[IFNAMSIZ];
1648 struct ifaddr *ifa;
1649 struct sockaddr_dl *sdl;
1650
1651 ifr = (struct ifreq *)data;
1652 switch (cmd) {
1653 case SIOCGIFINDEX:
1654 ifr->ifr_index = ifp->if_index;
1655 break;
1656
1657 case SIOCGIFFLAGS:
1658 temp_flags = ifp->if_flags | ifp->if_drv_flags;
1659 ifr->ifr_flags = temp_flags & 0xffff;
1660 ifr->ifr_flagshigh = temp_flags >> 16;
1661 break;
1662
1663 case SIOCGIFCAP:
1664 ifr->ifr_reqcap = ifp->if_capabilities;
1665 ifr->ifr_curcap = ifp->if_capenable;
1666 break;
1667
1668 #ifdef MAC
1669 case SIOCGIFMAC:
1670 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1671 break;
1672 #endif
1673
1674 case SIOCGIFMETRIC:
1675 ifr->ifr_metric = ifp->if_metric;
1676 break;
1677
1678 case SIOCGIFMTU:
1679 ifr->ifr_mtu = ifp->if_mtu;
1680 break;
1681
1682 case SIOCGIFPHYS:
1683 ifr->ifr_phys = ifp->if_physical;
1684 break;
1685
1686 case SIOCSIFFLAGS:
1687 error = priv_check(td, PRIV_NET_SETIFFLAGS);
1688 if (error)
1689 return (error);
1690 /*
1691 * Currently, no driver owned flags pass the IFF_CANTCHANGE
1692 * check, so we don't need special handling here yet.
1693 */
1694 new_flags = (ifr->ifr_flags & 0xffff) |
1695 (ifr->ifr_flagshigh << 16);
1696 if (ifp->if_flags & IFF_SMART) {
1697 /* Smart drivers twiddle their own routes */
1698 } else if (ifp->if_flags & IFF_UP &&
1699 (new_flags & IFF_UP) == 0) {
1700 int s = splimp();
1701 if_down(ifp);
1702 splx(s);
1703 } else if (new_flags & IFF_UP &&
1704 (ifp->if_flags & IFF_UP) == 0) {
1705 int s = splimp();
1706 if_up(ifp);
1707 splx(s);
1708 }
1709 /* See if permanently promiscuous mode bit is about to flip */
1710 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
1711 if (new_flags & IFF_PPROMISC)
1712 ifp->if_flags |= IFF_PROMISC;
1713 else if (ifp->if_pcount == 0)
1714 ifp->if_flags &= ~IFF_PROMISC;
1715 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
1716 ifp->if_xname,
1717 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
1718 }
1719 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1720 (new_flags &~ IFF_CANTCHANGE);
1721 if (ifp->if_ioctl) {
1722 IFF_LOCKGIANT(ifp);
1723 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1724 IFF_UNLOCKGIANT(ifp);
1725 }
1726 getmicrotime(&ifp->if_lastchange);
1727 break;
1728
1729 case SIOCSIFCAP:
1730 error = priv_check(td, PRIV_NET_SETIFCAP);
1731 if (error)
1732 return (error);
1733 if (ifp->if_ioctl == NULL)
1734 return (EOPNOTSUPP);
1735 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1736 return (EINVAL);
1737 IFF_LOCKGIANT(ifp);
1738 error = (*ifp->if_ioctl)(ifp, cmd, data);
1739 IFF_UNLOCKGIANT(ifp);
1740 if (error == 0)
1741 getmicrotime(&ifp->if_lastchange);
1742 break;
1743
1744 #ifdef MAC
1745 case SIOCSIFMAC:
1746 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1747 break;
1748 #endif
1749
1750 case SIOCSIFNAME:
1751 error = priv_check(td, PRIV_NET_SETIFNAME);
1752 if (error)
1753 return (error);
1754 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1755 if (error != 0)
1756 return (error);
1757 if (new_name[0] == '\0')
1758 return (EINVAL);
1759 if (ifunit(new_name) != NULL)
1760 return (EEXIST);
1761
1762 /*
1763 * XXX: Locking. Nothing else seems to lock if_flags,
1764 * and there are numerous other races with the
1765 * ifunit() checks not being atomic with namespace
1766 * changes (renames, vmoves, if_attach, etc).
1767 */
1768 ifp->if_flags |= IFF_RENAMING;
1769
1770 /* Announce the departure of the interface. */
1771 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1772 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1773
1774 log(LOG_INFO, "%s: changing name to '%s'\n",
1775 ifp->if_xname, new_name);
1776
1777 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1778 ifa = ifp->if_addr;
1779 IFA_LOCK(ifa);
1780 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1781 namelen = strlen(new_name);
1782 onamelen = sdl->sdl_nlen;
1783 /*
1784 * Move the address if needed. This is safe because we
1785 * allocate space for a name of length IFNAMSIZ when we
1786 * create this in if_attach().
1787 */
1788 if (namelen != onamelen) {
1789 bcopy(sdl->sdl_data + onamelen,
1790 sdl->sdl_data + namelen, sdl->sdl_alen);
1791 }
1792 bcopy(new_name, sdl->sdl_data, namelen);
1793 sdl->sdl_nlen = namelen;
1794 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1795 bzero(sdl->sdl_data, onamelen);
1796 while (namelen != 0)
1797 sdl->sdl_data[--namelen] = 0xff;
1798 IFA_UNLOCK(ifa);
1799
1800 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1801 /* Announce the return of the interface. */
1802 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1803
1804 ifp->if_flags &= ~IFF_RENAMING;
1805 break;
1806
1807 case SIOCSIFMETRIC:
1808 error = priv_check(td, PRIV_NET_SETIFMETRIC);
1809 if (error)
1810 return (error);
1811 ifp->if_metric = ifr->ifr_metric;
1812 getmicrotime(&ifp->if_lastchange);
1813 break;
1814
1815 case SIOCSIFPHYS:
1816 error = priv_check(td, PRIV_NET_SETIFPHYS);
1817 if (error)
1818 return (error);
1819 if (ifp->if_ioctl == NULL)
1820 return (EOPNOTSUPP);
1821 IFF_LOCKGIANT(ifp);
1822 error = (*ifp->if_ioctl)(ifp, cmd, data);
1823 IFF_UNLOCKGIANT(ifp);
1824 if (error == 0)
1825 getmicrotime(&ifp->if_lastchange);
1826 break;
1827
1828 case SIOCSIFMTU:
1829 {
1830 u_long oldmtu = ifp->if_mtu;
1831
1832 error = priv_check(td, PRIV_NET_SETIFMTU);
1833 if (error)
1834 return (error);
1835 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1836 return (EINVAL);
1837 if (ifp->if_ioctl == NULL)
1838 return (EOPNOTSUPP);
1839 IFF_LOCKGIANT(ifp);
1840 error = (*ifp->if_ioctl)(ifp, cmd, data);
1841 IFF_UNLOCKGIANT(ifp);
1842 if (error == 0) {
1843 getmicrotime(&ifp->if_lastchange);
1844 rt_ifmsg(ifp);
1845 }
1846 /*
1847 * If the link MTU changed, do network layer specific procedure.
1848 */
1849 if (ifp->if_mtu != oldmtu) {
1850 #ifdef INET6
1851 nd6_setmtu(ifp);
1852 #endif
1853 }
1854 break;
1855 }
1856
1857 case SIOCADDMULTI:
1858 case SIOCDELMULTI:
1859 if (cmd == SIOCADDMULTI)
1860 error = priv_check(td, PRIV_NET_ADDMULTI);
1861 else
1862 error = priv_check(td, PRIV_NET_DELMULTI);
1863 if (error)
1864 return (error);
1865
1866 /* Don't allow group membership on non-multicast interfaces. */
1867 if ((ifp->if_flags & IFF_MULTICAST) == 0)
1868 return (EOPNOTSUPP);
1869
1870 /* Don't let users screw up protocols' entries. */
1871 if (ifr->ifr_addr.sa_family != AF_LINK)
1872 return (EINVAL);
1873
1874 if (cmd == SIOCADDMULTI) {
1875 struct ifmultiaddr *ifma;
1876
1877 /*
1878 * Userland is only permitted to join groups once
1879 * via the if_addmulti() KPI, because it cannot hold
1880 * struct ifmultiaddr * between calls. It may also
1881 * lose a race while we check if the membership
1882 * already exists.
1883 */
1884 IF_ADDR_LOCK(ifp);
1885 ifma = if_findmulti(ifp, &ifr->ifr_addr);
1886 IF_ADDR_UNLOCK(ifp);
1887 if (ifma != NULL)
1888 error = EADDRINUSE;
1889 else
1890 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1891 } else {
1892 error = if_delmulti(ifp, &ifr->ifr_addr);
1893 }
1894 if (error == 0)
1895 getmicrotime(&ifp->if_lastchange);
1896 break;
1897
1898 case SIOCSIFPHYADDR:
1899 case SIOCDIFPHYADDR:
1900 #ifdef INET6
1901 case SIOCSIFPHYADDR_IN6:
1902 #endif
1903 case SIOCSLIFPHYADDR:
1904 case SIOCSIFMEDIA:
1905 case SIOCSIFGENERIC:
1906 error = priv_check(td, PRIV_NET_HWIOCTL);
1907 if (error)
1908 return (error);
1909 if (ifp->if_ioctl == NULL)
1910 return (EOPNOTSUPP);
1911 IFF_LOCKGIANT(ifp);
1912 error = (*ifp->if_ioctl)(ifp, cmd, data);
1913 IFF_UNLOCKGIANT(ifp);
1914 if (error == 0)
1915 getmicrotime(&ifp->if_lastchange);
1916 break;
1917
1918 case SIOCGIFSTATUS:
1919 ifs = (struct ifstat *)data;
1920 ifs->ascii[0] = '\0';
1921
1922 case SIOCGIFPSRCADDR:
1923 case SIOCGIFPDSTADDR:
1924 case SIOCGLIFPHYADDR:
1925 case SIOCGIFMEDIA:
1926 case SIOCGIFGENERIC:
1927 if (ifp->if_ioctl == NULL)
1928 return (EOPNOTSUPP);
1929 IFF_LOCKGIANT(ifp);
1930 error = (*ifp->if_ioctl)(ifp, cmd, data);
1931 IFF_UNLOCKGIANT(ifp);
1932 break;
1933
1934 case SIOCSIFLLADDR:
1935 error = priv_check(td, PRIV_NET_SETLLADDR);
1936 if (error)
1937 return (error);
1938 error = if_setlladdr(ifp,
1939 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1940 break;
1941
1942 case SIOCAIFGROUP:
1943 {
1944 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1945
1946 error = priv_check(td, PRIV_NET_ADDIFGROUP);
1947 if (error)
1948 return (error);
1949 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
1950 return (error);
1951 break;
1952 }
1953
1954 case SIOCGIFGROUP:
1955 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
1956 return (error);
1957 break;
1958
1959 case SIOCDIFGROUP:
1960 {
1961 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
1962
1963 error = priv_check(td, PRIV_NET_DELIFGROUP);
1964 if (error)
1965 return (error);
1966 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
1967 return (error);
1968 break;
1969 }
1970
1971 default:
1972 error = ENOIOCTL;
1973 break;
1974 }
1975 return (error);
1976 }
1977
1978 /*
1979 * Interface ioctls.
1980 */
1981 int
1982 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1983 {
1984 struct ifnet *ifp;
1985 struct ifreq *ifr;
1986 int error;
1987 int oif_flags;
1988
1989 switch (cmd) {
1990 case SIOCGIFCONF:
1991 case OSIOCGIFCONF:
1992 #ifdef __amd64__
1993 case SIOCGIFCONF32:
1994 #endif
1995 return (ifconf(cmd, data));
1996 }
1997 ifr = (struct ifreq *)data;
1998
1999 switch (cmd) {
2000 case SIOCIFCREATE:
2001 case SIOCIFCREATE2:
2002 error = priv_check(td, PRIV_NET_IFCREATE);
2003 if (error)
2004 return (error);
2005 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
2006 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
2007 case SIOCIFDESTROY:
2008 error = priv_check(td, PRIV_NET_IFDESTROY);
2009 if (error)
2010 return (error);
2011 return if_clone_destroy(ifr->ifr_name);
2012
2013 case SIOCIFGCLONERS:
2014 return (if_clone_list((struct if_clonereq *)data));
2015 case SIOCGIFGMEMB:
2016 return (if_getgroupmembers((struct ifgroupreq *)data));
2017 }
2018
2019 ifp = ifunit(ifr->ifr_name);
2020 if (ifp == 0)
2021 return (ENXIO);
2022
2023 error = ifhwioctl(cmd, ifp, data, td);
2024 if (error != ENOIOCTL)
2025 return (error);
2026
2027 oif_flags = ifp->if_flags;
2028 if (so->so_proto == 0)
2029 return (EOPNOTSUPP);
2030 #ifndef COMPAT_43
2031 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2032 data,
2033 ifp, td));
2034 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL)
2035 error = (*ifp->if_ioctl)(ifp, cmd, data);
2036 #else
2037 {
2038 int ocmd = cmd;
2039
2040 switch (cmd) {
2041
2042 case SIOCSIFDSTADDR:
2043 case SIOCSIFADDR:
2044 case SIOCSIFBRDADDR:
2045 case SIOCSIFNETMASK:
2046 #if BYTE_ORDER != BIG_ENDIAN
2047 if (ifr->ifr_addr.sa_family == 0 &&
2048 ifr->ifr_addr.sa_len < 16) {
2049 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2050 ifr->ifr_addr.sa_len = 16;
2051 }
2052 #else
2053 if (ifr->ifr_addr.sa_len == 0)
2054 ifr->ifr_addr.sa_len = 16;
2055 #endif
2056 break;
2057
2058 case OSIOCGIFADDR:
2059 cmd = SIOCGIFADDR;
2060 break;
2061
2062 case OSIOCGIFDSTADDR:
2063 cmd = SIOCGIFDSTADDR;
2064 break;
2065
2066 case OSIOCGIFBRDADDR:
2067 cmd = SIOCGIFBRDADDR;
2068 break;
2069
2070 case OSIOCGIFNETMASK:
2071 cmd = SIOCGIFNETMASK;
2072 }
2073 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2074 cmd,
2075 data,
2076 ifp, td));
2077 if (error == EOPNOTSUPP && ifp != NULL &&
2078 ifp->if_ioctl != NULL)
2079 error = (*ifp->if_ioctl)(ifp, cmd, data);
2080 switch (ocmd) {
2081
2082 case OSIOCGIFADDR:
2083 case OSIOCGIFDSTADDR:
2084 case OSIOCGIFBRDADDR:
2085 case OSIOCGIFNETMASK:
2086 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2087
2088 }
2089 }
2090 #endif /* COMPAT_43 */
2091
2092 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2093 #ifdef INET6
2094 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2095 if (ifp->if_flags & IFF_UP) {
2096 int s = splimp();
2097 in6_if_up(ifp);
2098 splx(s);
2099 }
2100 #endif
2101 }
2102 return (error);
2103 }
2104
2105 /*
2106 * The code common to handling reference counted flags,
2107 * e.g., in ifpromisc() and if_allmulti().
2108 * The "pflag" argument can specify a permanent mode flag to check,
2109 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2110 *
2111 * Only to be used on stack-owned flags, not driver-owned flags.
2112 */
2113 static int
2114 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2115 {
2116 struct ifreq ifr;
2117 int error;
2118 int oldflags, oldcount;
2119
2120 /* Sanity checks to catch programming errors */
2121 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2122 ("%s: setting driver-owned flag %d", __func__, flag));
2123
2124 if (onswitch)
2125 KASSERT(*refcount >= 0,
2126 ("%s: increment negative refcount %d for flag %d",
2127 __func__, *refcount, flag));
2128 else
2129 KASSERT(*refcount > 0,
2130 ("%s: decrement non-positive refcount %d for flag %d",
2131 __func__, *refcount, flag));
2132
2133 /* In case this mode is permanent, just touch refcount */
2134 if (ifp->if_flags & pflag) {
2135 *refcount += onswitch ? 1 : -1;
2136 return (0);
2137 }
2138
2139 /* Save ifnet parameters for if_ioctl() may fail */
2140 oldcount = *refcount;
2141 oldflags = ifp->if_flags;
2142
2143 /*
2144 * See if we aren't the only and touching refcount is enough.
2145 * Actually toggle interface flag if we are the first or last.
2146 */
2147 if (onswitch) {
2148 if ((*refcount)++)
2149 return (0);
2150 ifp->if_flags |= flag;
2151 } else {
2152 if (--(*refcount))
2153 return (0);
2154 ifp->if_flags &= ~flag;
2155 }
2156
2157 /* Call down the driver since we've changed interface flags */
2158 if (ifp->if_ioctl == NULL) {
2159 error = EOPNOTSUPP;
2160 goto recover;
2161 }
2162 ifr.ifr_flags = ifp->if_flags & 0xffff;
2163 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2164 IFF_LOCKGIANT(ifp);
2165 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2166 IFF_UNLOCKGIANT(ifp);
2167 if (error)
2168 goto recover;
2169 /* Notify userland that interface flags have changed */
2170 rt_ifmsg(ifp);
2171 return (0);
2172
2173 recover:
2174 /* Recover after driver error */
2175 *refcount = oldcount;
2176 ifp->if_flags = oldflags;
2177 return (error);
2178 }
2179
2180 /*
2181 * Set/clear promiscuous mode on interface ifp based on the truth value
2182 * of pswitch. The calls are reference counted so that only the first
2183 * "on" request actually has an effect, as does the final "off" request.
2184 * Results are undefined if the "off" and "on" requests are not matched.
2185 */
2186 int
2187 ifpromisc(struct ifnet *ifp, int pswitch)
2188 {
2189 int error;
2190 int oldflags = ifp->if_flags;
2191
2192 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2193 &ifp->if_pcount, pswitch);
2194 /* If promiscuous mode status has changed, log a message */
2195 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2196 log(LOG_INFO, "%s: promiscuous mode %s\n",
2197 ifp->if_xname,
2198 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2199 return (error);
2200 }
2201
2202 /*
2203 * Return interface configuration
2204 * of system. List may be used
2205 * in later ioctl's (above) to get
2206 * other information.
2207 */
2208 /*ARGSUSED*/
2209 static int
2210 ifconf(u_long cmd, caddr_t data)
2211 {
2212 struct ifconf *ifc = (struct ifconf *)data;
2213 #ifdef __amd64__
2214 struct ifconf32 *ifc32 = (struct ifconf32 *)data;
2215 struct ifconf ifc_swab;
2216 #endif
2217 struct ifnet *ifp;
2218 struct ifaddr *ifa;
2219 struct ifreq ifr;
2220 struct sbuf *sb;
2221 int error, full = 0, valid_len, max_len;
2222
2223 #ifdef __amd64__
2224 if (cmd == SIOCGIFCONF32) {
2225 ifc_swab.ifc_len = ifc32->ifc_len;
2226 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf;
2227 ifc = &ifc_swab;
2228 }
2229 #endif
2230 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2231 max_len = MAXPHYS - 1;
2232
2233 /* Prevent hostile input from being able to crash the system */
2234 if (ifc->ifc_len <= 0)
2235 return (EINVAL);
2236
2237 again:
2238 if (ifc->ifc_len <= max_len) {
2239 max_len = ifc->ifc_len;
2240 full = 1;
2241 }
2242 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2243 max_len = 0;
2244 valid_len = 0;
2245
2246 IFNET_RLOCK(); /* could sleep XXX */
2247 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2248 int addrs;
2249
2250 /*
2251 * Zero the ifr_name buffer to make sure we don't
2252 * disclose the contents of the stack.
2253 */
2254 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2255
2256 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2257 >= sizeof(ifr.ifr_name)) {
2258 sbuf_delete(sb);
2259 IFNET_RUNLOCK();
2260 return (ENAMETOOLONG);
2261 }
2262
2263 addrs = 0;
2264 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2265 struct sockaddr *sa = ifa->ifa_addr;
2266
2267 if (prison_if(curthread->td_ucred, sa) != 0)
2268 continue;
2269 addrs++;
2270 #ifdef COMPAT_43
2271 if (cmd == OSIOCGIFCONF) {
2272 struct osockaddr *osa =
2273 (struct osockaddr *)&ifr.ifr_addr;
2274 ifr.ifr_addr = *sa;
2275 osa->sa_family = sa->sa_family;
2276 sbuf_bcat(sb, &ifr, sizeof(ifr));
2277 max_len += sizeof(ifr);
2278 } else
2279 #endif
2280 if (sa->sa_len <= sizeof(*sa)) {
2281 ifr.ifr_addr = *sa;
2282 sbuf_bcat(sb, &ifr, sizeof(ifr));
2283 max_len += sizeof(ifr);
2284 } else {
2285 sbuf_bcat(sb, &ifr,
2286 offsetof(struct ifreq, ifr_addr));
2287 max_len += offsetof(struct ifreq, ifr_addr);
2288 sbuf_bcat(sb, sa, sa->sa_len);
2289 max_len += sa->sa_len;
2290 }
2291
2292 if (!sbuf_overflowed(sb))
2293 valid_len = sbuf_len(sb);
2294 }
2295 if (addrs == 0) {
2296 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2297 sbuf_bcat(sb, &ifr, sizeof(ifr));
2298 max_len += sizeof(ifr);
2299
2300 if (!sbuf_overflowed(sb))
2301 valid_len = sbuf_len(sb);
2302 }
2303 }
2304 IFNET_RUNLOCK();
2305
2306 /*
2307 * If we didn't allocate enough space (uncommon), try again. If
2308 * we have already allocated as much space as we are allowed,
2309 * return what we've got.
2310 */
2311 if (valid_len != max_len && !full) {
2312 sbuf_delete(sb);
2313 goto again;
2314 }
2315
2316 ifc->ifc_len = valid_len;
2317 #ifdef __amd64__
2318 if (cmd == SIOCGIFCONF32)
2319 ifc32->ifc_len = valid_len;
2320 #endif
2321 sbuf_finish(sb);
2322 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2323 sbuf_delete(sb);
2324 return (error);
2325 }
2326
2327 /*
2328 * Just like ifpromisc(), but for all-multicast-reception mode.
2329 */
2330 int
2331 if_allmulti(struct ifnet *ifp, int onswitch)
2332 {
2333
2334 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2335 }
2336
2337 struct ifmultiaddr *
2338 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2339 {
2340 struct ifmultiaddr *ifma;
2341
2342 IF_ADDR_LOCK_ASSERT(ifp);
2343
2344 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2345 if (sa->sa_family == AF_LINK) {
2346 if (sa_dl_equal(ifma->ifma_addr, sa))
2347 break;
2348 } else {
2349 if (sa_equal(ifma->ifma_addr, sa))
2350 break;
2351 }
2352 }
2353
2354 return ifma;
2355 }
2356
2357 /*
2358 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2359 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2360 * the ifnet multicast address list here, so the caller must do that and
2361 * other setup work (such as notifying the device driver). The reference
2362 * count is initialized to 1.
2363 */
2364 static struct ifmultiaddr *
2365 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2366 int mflags)
2367 {
2368 struct ifmultiaddr *ifma;
2369 struct sockaddr *dupsa;
2370
2371 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags |
2372 M_ZERO);
2373 if (ifma == NULL)
2374 return (NULL);
2375
2376 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags);
2377 if (dupsa == NULL) {
2378 FREE(ifma, M_IFMADDR);
2379 return (NULL);
2380 }
2381 bcopy(sa, dupsa, sa->sa_len);
2382 ifma->ifma_addr = dupsa;
2383
2384 ifma->ifma_ifp = ifp;
2385 ifma->ifma_refcount = 1;
2386 ifma->ifma_protospec = NULL;
2387
2388 if (llsa == NULL) {
2389 ifma->ifma_lladdr = NULL;
2390 return (ifma);
2391 }
2392
2393 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags);
2394 if (dupsa == NULL) {
2395 FREE(ifma->ifma_addr, M_IFMADDR);
2396 FREE(ifma, M_IFMADDR);
2397 return (NULL);
2398 }
2399 bcopy(llsa, dupsa, llsa->sa_len);
2400 ifma->ifma_lladdr = dupsa;
2401
2402 return (ifma);
2403 }
2404
2405 /*
2406 * if_freemulti: free ifmultiaddr structure and possibly attached related
2407 * addresses. The caller is responsible for implementing reference
2408 * counting, notifying the driver, handling routing messages, and releasing
2409 * any dependent link layer state.
2410 */
2411 static void
2412 if_freemulti(struct ifmultiaddr *ifma)
2413 {
2414
2415 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2416 ifma->ifma_refcount));
2417 KASSERT(ifma->ifma_protospec == NULL,
2418 ("if_freemulti: protospec not NULL"));
2419
2420 if (ifma->ifma_lladdr != NULL)
2421 FREE(ifma->ifma_lladdr, M_IFMADDR);
2422 FREE(ifma->ifma_addr, M_IFMADDR);
2423 FREE(ifma, M_IFMADDR);
2424 }
2425
2426 /*
2427 * Register an additional multicast address with a network interface.
2428 *
2429 * - If the address is already present, bump the reference count on the
2430 * address and return.
2431 * - If the address is not link-layer, look up a link layer address.
2432 * - Allocate address structures for one or both addresses, and attach to the
2433 * multicast address list on the interface. If automatically adding a link
2434 * layer address, the protocol address will own a reference to the link
2435 * layer address, to be freed when it is freed.
2436 * - Notify the network device driver of an addition to the multicast address
2437 * list.
2438 *
2439 * 'sa' points to caller-owned memory with the desired multicast address.
2440 *
2441 * 'retifma' will be used to return a pointer to the resulting multicast
2442 * address reference, if desired.
2443 */
2444 int
2445 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2446 struct ifmultiaddr **retifma)
2447 {
2448 struct ifmultiaddr *ifma, *ll_ifma;
2449 struct sockaddr *llsa;
2450 int error;
2451
2452 /*
2453 * If the address is already present, return a new reference to it;
2454 * otherwise, allocate storage and set up a new address.
2455 */
2456 IF_ADDR_LOCK(ifp);
2457 ifma = if_findmulti(ifp, sa);
2458 if (ifma != NULL) {
2459 ifma->ifma_refcount++;
2460 if (retifma != NULL)
2461 *retifma = ifma;
2462 IF_ADDR_UNLOCK(ifp);
2463 return (0);
2464 }
2465
2466 /*
2467 * The address isn't already present; resolve the protocol address
2468 * into a link layer address, and then look that up, bump its
2469 * refcount or allocate an ifma for that also. If 'llsa' was
2470 * returned, we will need to free it later.
2471 */
2472 llsa = NULL;
2473 ll_ifma = NULL;
2474 if (ifp->if_resolvemulti != NULL) {
2475 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2476 if (error)
2477 goto unlock_out;
2478 }
2479
2480 /*
2481 * Allocate the new address. Don't hook it up yet, as we may also
2482 * need to allocate a link layer multicast address.
2483 */
2484 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
2485 if (ifma == NULL) {
2486 error = ENOMEM;
2487 goto free_llsa_out;
2488 }
2489
2490 /*
2491 * If a link layer address is found, we'll need to see if it's
2492 * already present in the address list, or allocate is as well.
2493 * When this block finishes, the link layer address will be on the
2494 * list.
2495 */
2496 if (llsa != NULL) {
2497 ll_ifma = if_findmulti(ifp, llsa);
2498 if (ll_ifma == NULL) {
2499 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
2500 if (ll_ifma == NULL) {
2501 --ifma->ifma_refcount;
2502 if_freemulti(ifma);
2503 error = ENOMEM;
2504 goto free_llsa_out;
2505 }
2506 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
2507 ifma_link);
2508 } else
2509 ll_ifma->ifma_refcount++;
2510 ifma->ifma_llifma = ll_ifma;
2511 }
2512
2513 /*
2514 * We now have a new multicast address, ifma, and possibly a new or
2515 * referenced link layer address. Add the primary address to the
2516 * ifnet address list.
2517 */
2518 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2519
2520 if (retifma != NULL)
2521 *retifma = ifma;
2522
2523 /*
2524 * Must generate the message while holding the lock so that 'ifma'
2525 * pointer is still valid.
2526 */
2527 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2528 IF_ADDR_UNLOCK(ifp);
2529
2530 /*
2531 * We are certain we have added something, so call down to the
2532 * interface to let them know about it.
2533 */
2534 if (ifp->if_ioctl != NULL) {
2535 IFF_LOCKGIANT(ifp);
2536 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
2537 IFF_UNLOCKGIANT(ifp);
2538 }
2539
2540 if (llsa != NULL)
2541 FREE(llsa, M_IFMADDR);
2542
2543 return (0);
2544
2545 free_llsa_out:
2546 if (llsa != NULL)
2547 FREE(llsa, M_IFMADDR);
2548
2549 unlock_out:
2550 IF_ADDR_UNLOCK(ifp);
2551 return (error);
2552 }
2553
2554 /*
2555 * Delete a multicast group membership by network-layer group address.
2556 *
2557 * Returns ENOENT if the entry could not be found. If ifp no longer
2558 * exists, results are undefined. This entry point should only be used
2559 * from subsystems which do appropriate locking to hold ifp for the
2560 * duration of the call.
2561 * Network-layer protocol domains must use if_delmulti_ifma().
2562 */
2563 int
2564 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2565 {
2566 struct ifmultiaddr *ifma;
2567 int lastref;
2568 #ifdef INVARIANTS
2569 struct ifnet *oifp;
2570
2571 IFNET_RLOCK();
2572 TAILQ_FOREACH(oifp, &ifnet, if_link)
2573 if (ifp == oifp)
2574 break;
2575 if (ifp != oifp)
2576 ifp = NULL;
2577 IFNET_RUNLOCK();
2578
2579 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
2580 #endif
2581 if (ifp == NULL)
2582 return (ENOENT);
2583
2584 IF_ADDR_LOCK(ifp);
2585 lastref = 0;
2586 ifma = if_findmulti(ifp, sa);
2587 if (ifma != NULL)
2588 lastref = if_delmulti_locked(ifp, ifma, 0);
2589 IF_ADDR_UNLOCK(ifp);
2590
2591 if (ifma == NULL)
2592 return (ENOENT);
2593
2594 if (lastref && ifp->if_ioctl != NULL) {
2595 IFF_LOCKGIANT(ifp);
2596 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2597 IFF_UNLOCKGIANT(ifp);
2598 }
2599
2600 return (0);
2601 }
2602
2603 /*
2604 * Delete a multicast group membership by group membership pointer.
2605 * Network-layer protocol domains must use this routine.
2606 *
2607 * It is safe to call this routine if the ifp disappeared. Callers should
2608 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be
2609 * reconfigured.
2610 */
2611 void
2612 if_delmulti_ifma(struct ifmultiaddr *ifma)
2613 {
2614 struct ifnet *ifp;
2615 int lastref;
2616
2617 ifp = ifma->ifma_ifp;
2618 #ifdef DIAGNOSTIC
2619 if (ifp == NULL) {
2620 printf("%s: ifma_ifp seems to be detached\n", __func__);
2621 } else {
2622 struct ifnet *oifp;
2623
2624 IFNET_RLOCK();
2625 TAILQ_FOREACH(oifp, &ifnet, if_link)
2626 if (ifp == oifp)
2627 break;
2628 if (ifp != oifp) {
2629 printf("%s: ifnet %p disappeared\n", __func__, ifp);
2630 ifp = NULL;
2631 }
2632 IFNET_RUNLOCK();
2633 }
2634 #endif
2635 /*
2636 * If and only if the ifnet instance exists: Acquire the address lock.
2637 */
2638 if (ifp != NULL)
2639 IF_ADDR_LOCK(ifp);
2640
2641 lastref = if_delmulti_locked(ifp, ifma, 0);
2642
2643 if (ifp != NULL) {
2644 /*
2645 * If and only if the ifnet instance exists:
2646 * Release the address lock.
2647 * If the group was left: update the hardware hash filter.
2648 */
2649 IF_ADDR_UNLOCK(ifp);
2650 if (lastref && ifp->if_ioctl != NULL) {
2651 IFF_LOCKGIANT(ifp);
2652 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
2653 IFF_UNLOCKGIANT(ifp);
2654 }
2655 }
2656 }
2657
2658 /*
2659 * Perform deletion of network-layer and/or link-layer multicast address.
2660 *
2661 * Return 0 if the reference count was decremented.
2662 * Return 1 if the final reference was released, indicating that the
2663 * hardware hash filter should be reprogrammed.
2664 */
2665 static int
2666 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
2667 {
2668 struct ifmultiaddr *ll_ifma;
2669
2670 if (ifp != NULL && ifma->ifma_ifp != NULL) {
2671 KASSERT(ifma->ifma_ifp == ifp,
2672 ("%s: inconsistent ifp %p", __func__, ifp));
2673 IF_ADDR_LOCK_ASSERT(ifp);
2674 }
2675
2676 ifp = ifma->ifma_ifp;
2677
2678 /*
2679 * If the ifnet is detaching, null out references to ifnet,
2680 * so that upper protocol layers will notice, and not attempt
2681 * to obtain locks for an ifnet which no longer exists. The
2682 * routing socket announcement must happen before the ifnet
2683 * instance is detached from the system.
2684 */
2685 if (detaching) {
2686 #ifdef DIAGNOSTIC
2687 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
2688 #endif
2689 /*
2690 * ifp may already be nulled out if we are being reentered
2691 * to delete the ll_ifma.
2692 */
2693 if (ifp != NULL) {
2694 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2695 ifma->ifma_ifp = NULL;
2696 }
2697 }
2698
2699 if (--ifma->ifma_refcount > 0)
2700 return 0;
2701
2702 /*
2703 * If this ifma is a network-layer ifma, a link-layer ifma may
2704 * have been associated with it. Release it first if so.
2705 */
2706 ll_ifma = ifma->ifma_llifma;
2707 if (ll_ifma != NULL) {
2708 KASSERT(ifma->ifma_lladdr != NULL,
2709 ("%s: llifma w/o lladdr", __func__));
2710 if (detaching)
2711 ll_ifma->ifma_ifp = NULL; /* XXX */
2712 if (--ll_ifma->ifma_refcount == 0) {
2713 if (ifp != NULL) {
2714 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
2715 ifma_link);
2716 }
2717 if_freemulti(ll_ifma);
2718 }
2719 }
2720
2721 if (ifp != NULL)
2722 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2723
2724 if_freemulti(ifma);
2725
2726 /*
2727 * The last reference to this instance of struct ifmultiaddr
2728 * was released; the hardware should be notified of this change.
2729 */
2730 return 1;
2731 }
2732
2733 /*
2734 * Set the link layer address on an interface.
2735 *
2736 * At this time we only support certain types of interfaces,
2737 * and we don't allow the length of the address to change.
2738 */
2739 int
2740 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2741 {
2742 struct sockaddr_dl *sdl;
2743 struct ifaddr *ifa;
2744 struct ifreq ifr;
2745
2746 ifa = ifp->if_addr;
2747 if (ifa == NULL)
2748 return (EINVAL);
2749 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2750 if (sdl == NULL)
2751 return (EINVAL);
2752 if (len != sdl->sdl_alen) /* don't allow length to change */
2753 return (EINVAL);
2754 switch (ifp->if_type) {
2755 case IFT_ETHER:
2756 case IFT_FDDI:
2757 case IFT_XETHER:
2758 case IFT_ISO88025:
2759 case IFT_L2VLAN:
2760 case IFT_BRIDGE:
2761 case IFT_ARCNET:
2762 case IFT_IEEE8023ADLAG:
2763 bcopy(lladdr, LLADDR(sdl), len);
2764 break;
2765 default:
2766 return (ENODEV);
2767 }
2768 /*
2769 * If the interface is already up, we need
2770 * to re-init it in order to reprogram its
2771 * address filter.
2772 */
2773 if ((ifp->if_flags & IFF_UP) != 0) {
2774 if (ifp->if_ioctl) {
2775 IFF_LOCKGIANT(ifp);
2776 ifp->if_flags &= ~IFF_UP;
2777 ifr.ifr_flags = ifp->if_flags & 0xffff;
2778 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2779 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2780 ifp->if_flags |= IFF_UP;
2781 ifr.ifr_flags = ifp->if_flags & 0xffff;
2782 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2783 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2784 IFF_UNLOCKGIANT(ifp);
2785 }
2786 #ifdef INET
2787 /*
2788 * Also send gratuitous ARPs to notify other nodes about
2789 * the address change.
2790 */
2791 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2792 if (ifa->ifa_addr->sa_family == AF_INET)
2793 arp_ifinit(ifp, ifa);
2794 }
2795 #endif
2796 }
2797 return (0);
2798 }
2799
2800 /*
2801 * The name argument must be a pointer to storage which will last as
2802 * long as the interface does. For physical devices, the result of
2803 * device_get_name(dev) is a good choice and for pseudo-devices a
2804 * static string works well.
2805 */
2806 void
2807 if_initname(struct ifnet *ifp, const char *name, int unit)
2808 {
2809 ifp->if_dname = name;
2810 ifp->if_dunit = unit;
2811 if (unit != IF_DUNIT_NONE)
2812 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2813 else
2814 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2815 }
2816
2817 int
2818 if_printf(struct ifnet *ifp, const char * fmt, ...)
2819 {
2820 va_list ap;
2821 int retval;
2822
2823 retval = printf("%s: ", ifp->if_xname);
2824 va_start(ap, fmt);
2825 retval += vprintf(fmt, ap);
2826 va_end(ap);
2827 return (retval);
2828 }
2829
2830 /*
2831 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot
2832 * be called without Giant. However, we often can't acquire the Giant lock
2833 * at those points; instead, we run it via a task queue that holds Giant via
2834 * if_start_deferred.
2835 *
2836 * XXXRW: We need to make sure that the ifnet isn't fully detached until any
2837 * outstanding if_start_deferred() tasks that will run after the free. This
2838 * probably means waiting in if_detach().
2839 */
2840 void
2841 if_start(struct ifnet *ifp)
2842 {
2843
2844 if (ifp->if_flags & IFF_NEEDSGIANT) {
2845 if (mtx_owned(&Giant))
2846 (*(ifp)->if_start)(ifp);
2847 else
2848 taskqueue_enqueue(taskqueue_swi_giant,
2849 &ifp->if_starttask);
2850 } else
2851 (*(ifp)->if_start)(ifp);
2852 }
2853
2854 static void
2855 if_start_deferred(void *context, int pending)
2856 {
2857 struct ifnet *ifp;
2858
2859 GIANT_REQUIRED;
2860
2861 ifp = context;
2862 (ifp->if_start)(ifp);
2863 }
2864
2865 int
2866 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
2867 {
2868 int active = 0;
2869
2870 IF_LOCK(ifq);
2871 if (_IF_QFULL(ifq)) {
2872 _IF_DROP(ifq);
2873 IF_UNLOCK(ifq);
2874 m_freem(m);
2875 return (0);
2876 }
2877 if (ifp != NULL) {
2878 ifp->if_obytes += m->m_pkthdr.len + adjust;
2879 if (m->m_flags & (M_BCAST|M_MCAST))
2880 ifp->if_omcasts++;
2881 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
2882 }
2883 _IF_ENQUEUE(ifq, m);
2884 IF_UNLOCK(ifq);
2885 if (ifp != NULL && !active)
2886 if_start(ifp);
2887 return (1);
2888 }
2889
2890 void
2891 if_register_com_alloc(u_char type,
2892 if_com_alloc_t *a, if_com_free_t *f)
2893 {
2894
2895 KASSERT(if_com_alloc[type] == NULL,
2896 ("if_register_com_alloc: %d already registered", type));
2897 KASSERT(if_com_free[type] == NULL,
2898 ("if_register_com_alloc: %d free already registered", type));
2899
2900 if_com_alloc[type] = a;
2901 if_com_free[type] = f;
2902 }
2903
2904 void
2905 if_deregister_com_alloc(u_char type)
2906 {
2907
2908 KASSERT(if_com_alloc[type] != NULL,
2909 ("if_deregister_com_alloc: %d not registered", type));
2910 KASSERT(if_com_free[type] != NULL,
2911 ("if_deregister_com_alloc: %d free not registered", type));
2912 if_com_alloc[type] = NULL;
2913 if_com_free[type] = NULL;
2914 }
Cache object: 939403a718a4665adfe1e8b66c33b3df
|