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