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/10.1/sys/net/if.c 269046 2014-07-24 06:02:03Z kevlo $
31 */
32
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/conf.h>
40 #include <sys/malloc.h>
41 #include <sys/sbuf.h>
42 #include <sys/bus.h>
43 #include <sys/mbuf.h>
44 #include <sys/systm.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/refcount.h>
53 #include <sys/module.h>
54 #include <sys/rwlock.h>
55 #include <sys/sockio.h>
56 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/domain.h>
60 #include <sys/jail.h>
61 #include <sys/priv.h>
62
63 #include <machine/stdarg.h>
64 #include <vm/uma.h>
65
66 #include <net/if.h>
67 #include <net/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_var.h>
72 #include <net/radix.h>
73 #include <net/route.h>
74 #include <net/vnet.h>
75
76 #if defined(INET) || defined(INET6)
77 #include <net/ethernet.h>
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_carp.h>
82 #ifdef INET
83 #include <netinet/if_ether.h>
84 #endif /* INET */
85 #ifdef INET6
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
88 #endif /* INET6 */
89 #endif /* INET || INET6 */
90
91 #include <security/mac/mac_framework.h>
92
93 #ifdef COMPAT_FREEBSD32
94 #include <sys/mount.h>
95 #include <compat/freebsd32/freebsd32.h>
96 #endif
97
98 struct ifindex_entry {
99 struct ifnet *ife_ifnet;
100 };
101
102 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
103 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
104
105 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen);
106 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
107 &ifqmaxlen, 0, "max send queue size");
108
109 /* Log link state change events */
110 static int log_link_state_change = 1;
111
112 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
113 &log_link_state_change, 0,
114 "log interface link state change events");
115
116 /* Interface description */
117 static unsigned int ifdescr_maxlen = 1024;
118 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
119 &ifdescr_maxlen, 0,
120 "administrative maximum length for interface description");
121
122 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
123
124 /* global sx for non-critical path ifdescr */
125 static struct sx ifdescr_sx;
126 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
127
128 void (*bridge_linkstate_p)(struct ifnet *ifp);
129 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
130 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
131 /* These are external hooks for CARP. */
132 void (*carp_linkstate_p)(struct ifnet *ifp);
133 void (*carp_demote_adj_p)(int, char *);
134 int (*carp_master_p)(struct ifaddr *);
135 #if defined(INET) || defined(INET6)
136 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
137 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
138 const struct sockaddr *sa);
139 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
140 int (*carp_attach_p)(struct ifaddr *, int);
141 void (*carp_detach_p)(struct ifaddr *);
142 #endif
143 #ifdef INET
144 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
145 #endif
146 #ifdef INET6
147 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
148 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
149 const struct in6_addr *taddr);
150 #endif
151
152 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
153
154 /*
155 * XXX: Style; these should be sorted alphabetically, and unprototyped
156 * static functions should be prototyped. Currently they are sorted by
157 * declaration order.
158 */
159 static void if_attachdomain(void *);
160 static void if_attachdomain1(struct ifnet *);
161 static int ifconf(u_long, caddr_t);
162 static void if_freemulti(struct ifmultiaddr *);
163 static void if_init(void *);
164 static void if_grow(void);
165 static void if_route(struct ifnet *, int flag, int fam);
166 static int if_setflag(struct ifnet *, int, int, int *, int);
167 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
168 static void if_unroute(struct ifnet *, int flag, int fam);
169 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
170 static int if_rtdel(struct radix_node *, void *);
171 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
172 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
173 static void do_link_state_change(void *, int);
174 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
175 static int if_getgroupmembers(struct ifgroupreq *);
176 static void if_delgroups(struct ifnet *);
177 static void if_attach_internal(struct ifnet *, int);
178 static void if_detach_internal(struct ifnet *, int);
179
180 #ifdef INET6
181 /*
182 * XXX: declare here to avoid to include many inet6 related files..
183 * should be more generalized?
184 */
185 extern void nd6_setmtu(struct ifnet *);
186 #endif
187
188 VNET_DEFINE(int, if_index);
189 int ifqmaxlen = IFQ_MAXLEN;
190 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
191 VNET_DEFINE(struct ifgrouphead, ifg_head);
192
193 static VNET_DEFINE(int, if_indexlim) = 8;
194
195 /* Table of ifnet by index. */
196 VNET_DEFINE(struct ifindex_entry *, ifindex_table);
197
198 #define V_if_indexlim VNET(if_indexlim)
199 #define V_ifindex_table VNET(ifindex_table)
200
201 /*
202 * The global network interface list (V_ifnet) and related state (such as
203 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
204 * an rwlock. Either may be acquired shared to stablize the list, but both
205 * must be acquired writable to modify the list. This model allows us to
206 * both stablize the interface list during interrupt thread processing, but
207 * also to stablize it over long-running ioctls, without introducing priority
208 * inversions and deadlocks.
209 */
210 struct rwlock ifnet_rwlock;
211 struct sx ifnet_sxlock;
212
213 /*
214 * The allocation of network interfaces is a rather non-atomic affair; we
215 * need to select an index before we are ready to expose the interface for
216 * use, so will use this pointer value to indicate reservation.
217 */
218 #define IFNET_HOLD (void *)(uintptr_t)(-1)
219
220 static if_com_alloc_t *if_com_alloc[256];
221 static if_com_free_t *if_com_free[256];
222
223 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
224 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
225 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
226
227 struct ifnet *
228 ifnet_byindex_locked(u_short idx)
229 {
230
231 if (idx > V_if_index)
232 return (NULL);
233 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD)
234 return (NULL);
235 return (V_ifindex_table[idx].ife_ifnet);
236 }
237
238 struct ifnet *
239 ifnet_byindex(u_short idx)
240 {
241 struct ifnet *ifp;
242
243 IFNET_RLOCK_NOSLEEP();
244 ifp = ifnet_byindex_locked(idx);
245 IFNET_RUNLOCK_NOSLEEP();
246 return (ifp);
247 }
248
249 struct ifnet *
250 ifnet_byindex_ref(u_short idx)
251 {
252 struct ifnet *ifp;
253
254 IFNET_RLOCK_NOSLEEP();
255 ifp = ifnet_byindex_locked(idx);
256 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
257 IFNET_RUNLOCK_NOSLEEP();
258 return (NULL);
259 }
260 if_ref(ifp);
261 IFNET_RUNLOCK_NOSLEEP();
262 return (ifp);
263 }
264
265 /*
266 * Allocate an ifindex array entry; return 0 on success or an error on
267 * failure.
268 */
269 static int
270 ifindex_alloc_locked(u_short *idxp)
271 {
272 u_short idx;
273
274 IFNET_WLOCK_ASSERT();
275
276 retry:
277 /*
278 * Try to find an empty slot below V_if_index. If we fail, take the
279 * next slot.
280 */
281 for (idx = 1; idx <= V_if_index; idx++) {
282 if (V_ifindex_table[idx].ife_ifnet == NULL)
283 break;
284 }
285
286 /* Catch if_index overflow. */
287 if (idx < 1)
288 return (ENOSPC);
289 if (idx >= V_if_indexlim) {
290 if_grow();
291 goto retry;
292 }
293 if (idx > V_if_index)
294 V_if_index = idx;
295 *idxp = idx;
296 return (0);
297 }
298
299 static void
300 ifindex_free_locked(u_short idx)
301 {
302
303 IFNET_WLOCK_ASSERT();
304
305 V_ifindex_table[idx].ife_ifnet = NULL;
306 while (V_if_index > 0 &&
307 V_ifindex_table[V_if_index].ife_ifnet == NULL)
308 V_if_index--;
309 }
310
311 static void
312 ifindex_free(u_short idx)
313 {
314
315 IFNET_WLOCK();
316 ifindex_free_locked(idx);
317 IFNET_WUNLOCK();
318 }
319
320 static void
321 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
322 {
323
324 IFNET_WLOCK_ASSERT();
325
326 V_ifindex_table[idx].ife_ifnet = ifp;
327 }
328
329 static void
330 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
331 {
332
333 IFNET_WLOCK();
334 ifnet_setbyindex_locked(idx, ifp);
335 IFNET_WUNLOCK();
336 }
337
338 struct ifaddr *
339 ifaddr_byindex(u_short idx)
340 {
341 struct ifaddr *ifa;
342
343 IFNET_RLOCK_NOSLEEP();
344 ifa = ifnet_byindex_locked(idx)->if_addr;
345 if (ifa != NULL)
346 ifa_ref(ifa);
347 IFNET_RUNLOCK_NOSLEEP();
348 return (ifa);
349 }
350
351 /*
352 * Network interface utility routines.
353 *
354 * Routines with ifa_ifwith* names take sockaddr *'s as
355 * parameters.
356 */
357
358 static void
359 vnet_if_init(const void *unused __unused)
360 {
361
362 TAILQ_INIT(&V_ifnet);
363 TAILQ_INIT(&V_ifg_head);
364 IFNET_WLOCK();
365 if_grow(); /* create initial table */
366 IFNET_WUNLOCK();
367 vnet_if_clone_init();
368 }
369 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
370 NULL);
371
372 /* ARGSUSED*/
373 static void
374 if_init(void *dummy __unused)
375 {
376
377 IFNET_LOCK_INIT();
378 if_clone_init();
379 }
380 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL);
381
382
383 #ifdef VIMAGE
384 static void
385 vnet_if_uninit(const void *unused __unused)
386 {
387
388 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
389 "not empty", __func__, __LINE__, &V_ifnet));
390 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
391 "not empty", __func__, __LINE__, &V_ifg_head));
392
393 free((caddr_t)V_ifindex_table, M_IFNET);
394 }
395 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
396 vnet_if_uninit, NULL);
397 #endif
398
399 static void
400 if_grow(void)
401 {
402 int oldlim;
403 u_int n;
404 struct ifindex_entry *e;
405
406 IFNET_WLOCK_ASSERT();
407 oldlim = V_if_indexlim;
408 IFNET_WUNLOCK();
409 n = (oldlim << 1) * sizeof(*e);
410 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
411 IFNET_WLOCK();
412 if (V_if_indexlim != oldlim) {
413 free(e, M_IFNET);
414 return;
415 }
416 if (V_ifindex_table != NULL) {
417 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
418 free((caddr_t)V_ifindex_table, M_IFNET);
419 }
420 V_if_indexlim <<= 1;
421 V_ifindex_table = e;
422 }
423
424 /*
425 * Allocate a struct ifnet and an index for an interface. A layer 2
426 * common structure will also be allocated if an allocation routine is
427 * registered for the passed type.
428 */
429 struct ifnet *
430 if_alloc(u_char type)
431 {
432 struct ifnet *ifp;
433 u_short idx;
434
435 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
436 IFNET_WLOCK();
437 if (ifindex_alloc_locked(&idx) != 0) {
438 IFNET_WUNLOCK();
439 free(ifp, M_IFNET);
440 return (NULL);
441 }
442 ifnet_setbyindex_locked(idx, IFNET_HOLD);
443 IFNET_WUNLOCK();
444 ifp->if_index = idx;
445 ifp->if_type = type;
446 ifp->if_alloctype = type;
447 if (if_com_alloc[type] != NULL) {
448 ifp->if_l2com = if_com_alloc[type](type, ifp);
449 if (ifp->if_l2com == NULL) {
450 free(ifp, M_IFNET);
451 ifindex_free(idx);
452 return (NULL);
453 }
454 }
455
456 IF_ADDR_LOCK_INIT(ifp);
457 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
458 ifp->if_afdata_initialized = 0;
459 IF_AFDATA_LOCK_INIT(ifp);
460 TAILQ_INIT(&ifp->if_addrhead);
461 TAILQ_INIT(&ifp->if_multiaddrs);
462 TAILQ_INIT(&ifp->if_groups);
463 #ifdef MAC
464 mac_ifnet_init(ifp);
465 #endif
466 ifq_init(&ifp->if_snd, ifp);
467
468 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
469 ifnet_setbyindex(ifp->if_index, ifp);
470 return (ifp);
471 }
472
473 /*
474 * Do the actual work of freeing a struct ifnet, and layer 2 common
475 * structure. This call is made when the last reference to an
476 * interface is released.
477 */
478 static void
479 if_free_internal(struct ifnet *ifp)
480 {
481
482 KASSERT((ifp->if_flags & IFF_DYING),
483 ("if_free_internal: interface not dying"));
484
485 if (if_com_free[ifp->if_alloctype] != NULL)
486 if_com_free[ifp->if_alloctype](ifp->if_l2com,
487 ifp->if_alloctype);
488
489 #ifdef MAC
490 mac_ifnet_destroy(ifp);
491 #endif /* MAC */
492 if (ifp->if_description != NULL)
493 free(ifp->if_description, M_IFDESCR);
494 IF_AFDATA_DESTROY(ifp);
495 IF_ADDR_LOCK_DESTROY(ifp);
496 ifq_delete(&ifp->if_snd);
497 free(ifp, M_IFNET);
498 }
499
500 /*
501 * Deregister an interface and free the associated storage.
502 */
503 void
504 if_free(struct ifnet *ifp)
505 {
506
507 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
508
509 CURVNET_SET_QUIET(ifp->if_vnet);
510 IFNET_WLOCK();
511 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
512 ("%s: freeing unallocated ifnet", ifp->if_xname));
513
514 ifindex_free_locked(ifp->if_index);
515 IFNET_WUNLOCK();
516
517 if (refcount_release(&ifp->if_refcount))
518 if_free_internal(ifp);
519 CURVNET_RESTORE();
520 }
521
522 /*
523 * Interfaces to keep an ifnet type-stable despite the possibility of the
524 * driver calling if_free(). If there are additional references, we defer
525 * freeing the underlying data structure.
526 */
527 void
528 if_ref(struct ifnet *ifp)
529 {
530
531 /* We don't assert the ifnet list lock here, but arguably should. */
532 refcount_acquire(&ifp->if_refcount);
533 }
534
535 void
536 if_rele(struct ifnet *ifp)
537 {
538
539 if (!refcount_release(&ifp->if_refcount))
540 return;
541 if_free_internal(ifp);
542 }
543
544 void
545 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
546 {
547
548 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
549
550 if (ifq->ifq_maxlen == 0)
551 ifq->ifq_maxlen = ifqmaxlen;
552
553 ifq->altq_type = 0;
554 ifq->altq_disc = NULL;
555 ifq->altq_flags &= ALTQF_CANTCHANGE;
556 ifq->altq_tbr = NULL;
557 ifq->altq_ifp = ifp;
558 }
559
560 void
561 ifq_delete(struct ifaltq *ifq)
562 {
563 mtx_destroy(&ifq->ifq_mtx);
564 }
565
566 /*
567 * Perform generic interface initalization tasks and attach the interface
568 * to the list of "active" interfaces. If vmove flag is set on entry
569 * to if_attach_internal(), perform only a limited subset of initialization
570 * tasks, given that we are moving from one vnet to another an ifnet which
571 * has already been fully initialized.
572 *
573 * XXX:
574 * - The decision to return void and thus require this function to
575 * succeed is questionable.
576 * - We should probably do more sanity checking. For instance we don't
577 * do anything to insure if_xname is unique or non-empty.
578 */
579 void
580 if_attach(struct ifnet *ifp)
581 {
582
583 if_attach_internal(ifp, 0);
584 }
585
586 static void
587 if_attach_internal(struct ifnet *ifp, int vmove)
588 {
589 unsigned socksize, ifasize;
590 int namelen, masklen;
591 struct sockaddr_dl *sdl;
592 struct ifaddr *ifa;
593
594 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
595 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
596 ifp->if_xname);
597
598 #ifdef VIMAGE
599 ifp->if_vnet = curvnet;
600 if (ifp->if_home_vnet == NULL)
601 ifp->if_home_vnet = curvnet;
602 #endif
603
604 if_addgroup(ifp, IFG_ALL);
605
606 getmicrotime(&ifp->if_lastchange);
607 ifp->if_data.ifi_epoch = time_uptime;
608 ifp->if_data.ifi_datalen = sizeof(struct if_data);
609
610 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
611 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
612 ("transmit and qflush must both either be set or both be NULL"));
613 if (ifp->if_transmit == NULL) {
614 ifp->if_transmit = if_transmit;
615 ifp->if_qflush = if_qflush;
616 }
617
618 if (!vmove) {
619 #ifdef MAC
620 mac_ifnet_create(ifp);
621 #endif
622
623 /*
624 * Create a Link Level name for this device.
625 */
626 namelen = strlen(ifp->if_xname);
627 /*
628 * Always save enough space for any possiable name so we
629 * can do a rename in place later.
630 */
631 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
632 socksize = masklen + ifp->if_addrlen;
633 if (socksize < sizeof(*sdl))
634 socksize = sizeof(*sdl);
635 socksize = roundup2(socksize, sizeof(long));
636 ifasize = sizeof(*ifa) + 2 * socksize;
637 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
638 ifa_init(ifa);
639 sdl = (struct sockaddr_dl *)(ifa + 1);
640 sdl->sdl_len = socksize;
641 sdl->sdl_family = AF_LINK;
642 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
643 sdl->sdl_nlen = namelen;
644 sdl->sdl_index = ifp->if_index;
645 sdl->sdl_type = ifp->if_type;
646 ifp->if_addr = ifa;
647 ifa->ifa_ifp = ifp;
648 ifa->ifa_rtrequest = link_rtrequest;
649 ifa->ifa_addr = (struct sockaddr *)sdl;
650 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
651 ifa->ifa_netmask = (struct sockaddr *)sdl;
652 sdl->sdl_len = masklen;
653 while (namelen != 0)
654 sdl->sdl_data[--namelen] = 0xff;
655 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
656 /* Reliably crash if used uninitialized. */
657 ifp->if_broadcastaddr = NULL;
658
659 #if defined(INET) || defined(INET6)
660 /* Initialize to max value. */
661 if (ifp->if_hw_tsomax == 0)
662 ifp->if_hw_tsomax = min(IP_MAXPACKET, 32 * MCLBYTES -
663 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
664 KASSERT(ifp->if_hw_tsomax <= IP_MAXPACKET &&
665 ifp->if_hw_tsomax >= IP_MAXPACKET / 8,
666 ("%s: tsomax outside of range", __func__));
667 #endif
668 }
669 #ifdef VIMAGE
670 else {
671 /*
672 * Update the interface index in the link layer address
673 * of the interface.
674 */
675 for (ifa = ifp->if_addr; ifa != NULL;
676 ifa = TAILQ_NEXT(ifa, ifa_link)) {
677 if (ifa->ifa_addr->sa_family == AF_LINK) {
678 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
679 sdl->sdl_index = ifp->if_index;
680 }
681 }
682 }
683 #endif
684
685 IFNET_WLOCK();
686 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
687 #ifdef VIMAGE
688 curvnet->vnet_ifcnt++;
689 #endif
690 IFNET_WUNLOCK();
691
692 if (domain_init_status >= 2)
693 if_attachdomain1(ifp);
694
695 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
696 if (IS_DEFAULT_VNET(curvnet))
697 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
698
699 /* Announce the interface. */
700 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
701 }
702
703 static void
704 if_attachdomain(void *dummy)
705 {
706 struct ifnet *ifp;
707
708 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
709 if_attachdomain1(ifp);
710 }
711 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
712 if_attachdomain, NULL);
713
714 static void
715 if_attachdomain1(struct ifnet *ifp)
716 {
717 struct domain *dp;
718
719 /*
720 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
721 * cannot lock ifp->if_afdata initialization, entirely.
722 */
723 if (IF_AFDATA_TRYLOCK(ifp) == 0)
724 return;
725 if (ifp->if_afdata_initialized >= domain_init_status) {
726 IF_AFDATA_UNLOCK(ifp);
727 log(LOG_WARNING, "%s called more than once on %s\n",
728 __func__, ifp->if_xname);
729 return;
730 }
731 ifp->if_afdata_initialized = domain_init_status;
732 IF_AFDATA_UNLOCK(ifp);
733
734 /* address family dependent data region */
735 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
736 for (dp = domains; dp; dp = dp->dom_next) {
737 if (dp->dom_ifattach)
738 ifp->if_afdata[dp->dom_family] =
739 (*dp->dom_ifattach)(ifp);
740 }
741 }
742
743 /*
744 * Remove any unicast or broadcast network addresses from an interface.
745 */
746 void
747 if_purgeaddrs(struct ifnet *ifp)
748 {
749 struct ifaddr *ifa, *next;
750
751 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
752 if (ifa->ifa_addr->sa_family == AF_LINK)
753 continue;
754 #ifdef INET
755 /* XXX: Ugly!! ad hoc just for INET */
756 if (ifa->ifa_addr->sa_family == AF_INET) {
757 struct ifaliasreq ifr;
758
759 bzero(&ifr, sizeof(ifr));
760 ifr.ifra_addr = *ifa->ifa_addr;
761 if (ifa->ifa_dstaddr)
762 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
763 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
764 NULL) == 0)
765 continue;
766 }
767 #endif /* INET */
768 #ifdef INET6
769 if (ifa->ifa_addr->sa_family == AF_INET6) {
770 in6_purgeaddr(ifa);
771 /* ifp_addrhead is already updated */
772 continue;
773 }
774 #endif /* INET6 */
775 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
776 ifa_free(ifa);
777 }
778 }
779
780 /*
781 * Remove any multicast network addresses from an interface when an ifnet
782 * is going away.
783 */
784 static void
785 if_purgemaddrs(struct ifnet *ifp)
786 {
787 struct ifmultiaddr *ifma;
788 struct ifmultiaddr *next;
789
790 IF_ADDR_WLOCK(ifp);
791 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
792 if_delmulti_locked(ifp, ifma, 1);
793 IF_ADDR_WUNLOCK(ifp);
794 }
795
796 /*
797 * Detach an interface, removing it from the list of "active" interfaces.
798 * If vmove flag is set on entry to if_detach_internal(), perform only a
799 * limited subset of cleanup tasks, given that we are moving an ifnet from
800 * one vnet to another, where it must be fully operational.
801 *
802 * XXXRW: There are some significant questions about event ordering, and
803 * how to prevent things from starting to use the interface during detach.
804 */
805 void
806 if_detach(struct ifnet *ifp)
807 {
808
809 CURVNET_SET_QUIET(ifp->if_vnet);
810 if_detach_internal(ifp, 0);
811 CURVNET_RESTORE();
812 }
813
814 static void
815 if_detach_internal(struct ifnet *ifp, int vmove)
816 {
817 struct ifaddr *ifa;
818 struct radix_node_head *rnh;
819 int i, j;
820 struct domain *dp;
821 struct ifnet *iter;
822 int found = 0;
823
824 IFNET_WLOCK();
825 TAILQ_FOREACH(iter, &V_ifnet, if_link)
826 if (iter == ifp) {
827 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
828 found = 1;
829 break;
830 }
831 #ifdef VIMAGE
832 if (found)
833 curvnet->vnet_ifcnt--;
834 #endif
835 IFNET_WUNLOCK();
836 if (!found) {
837 if (vmove)
838 panic("%s: ifp=%p not on the ifnet tailq %p",
839 __func__, ifp, &V_ifnet);
840 else
841 return; /* XXX this should panic as well? */
842 }
843
844 /*
845 * Remove/wait for pending events.
846 */
847 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
848
849 /*
850 * Remove routes and flush queues.
851 */
852 if_down(ifp);
853 #ifdef ALTQ
854 if (ALTQ_IS_ENABLED(&ifp->if_snd))
855 altq_disable(&ifp->if_snd);
856 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
857 altq_detach(&ifp->if_snd);
858 #endif
859
860 if_purgeaddrs(ifp);
861
862 #ifdef INET
863 in_ifdetach(ifp);
864 #endif
865
866 #ifdef INET6
867 /*
868 * Remove all IPv6 kernel structs related to ifp. This should be done
869 * before removing routing entries below, since IPv6 interface direct
870 * routes are expected to be removed by the IPv6-specific kernel API.
871 * Otherwise, the kernel will detect some inconsistency and bark it.
872 */
873 in6_ifdetach(ifp);
874 #endif
875 if_purgemaddrs(ifp);
876
877 if (!vmove) {
878 /*
879 * Prevent further calls into the device driver via ifnet.
880 */
881 if_dead(ifp);
882
883 /*
884 * Remove link ifaddr pointer and maybe decrement if_index.
885 * Clean up all addresses.
886 */
887 ifp->if_addr = NULL;
888
889 /* We can now free link ifaddr. */
890 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
891 ifa = TAILQ_FIRST(&ifp->if_addrhead);
892 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
893 ifa_free(ifa);
894 }
895 }
896
897 /*
898 * Delete all remaining routes using this interface
899 * Unfortuneatly the only way to do this is to slog through
900 * the entire routing table looking for routes which point
901 * to this interface...oh well...
902 */
903 for (i = 1; i <= AF_MAX; i++) {
904 for (j = 0; j < rt_numfibs; j++) {
905 rnh = rt_tables_get_rnh(j, i);
906 if (rnh == NULL)
907 continue;
908 RADIX_NODE_HEAD_LOCK(rnh);
909 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
910 RADIX_NODE_HEAD_UNLOCK(rnh);
911 }
912 }
913
914 /* Announce that the interface is gone. */
915 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
916 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
917 if (IS_DEFAULT_VNET(curvnet))
918 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
919 if_delgroups(ifp);
920
921 /*
922 * We cannot hold the lock over dom_ifdetach calls as they might
923 * sleep, for example trying to drain a callout, thus open up the
924 * theoretical race with re-attaching.
925 */
926 IF_AFDATA_LOCK(ifp);
927 i = ifp->if_afdata_initialized;
928 ifp->if_afdata_initialized = 0;
929 IF_AFDATA_UNLOCK(ifp);
930 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
931 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
932 (*dp->dom_ifdetach)(ifp,
933 ifp->if_afdata[dp->dom_family]);
934 }
935 }
936
937 #ifdef VIMAGE
938 /*
939 * if_vmove() performs a limited version of if_detach() in current
940 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
941 * An attempt is made to shrink if_index in current vnet, find an
942 * unused if_index in target vnet and calls if_grow() if necessary,
943 * and finally find an unused if_xname for the target vnet.
944 */
945 void
946 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
947 {
948 u_short idx;
949
950 /*
951 * Detach from current vnet, but preserve LLADDR info, do not
952 * mark as dead etc. so that the ifnet can be reattached later.
953 */
954 if_detach_internal(ifp, 1);
955
956 /*
957 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
958 * the if_index for that vnet if possible.
959 *
960 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
961 * or we'd lock on one vnet and unlock on another.
962 */
963 IFNET_WLOCK();
964 ifindex_free_locked(ifp->if_index);
965 IFNET_WUNLOCK();
966
967 /*
968 * Perform interface-specific reassignment tasks, if provided by
969 * the driver.
970 */
971 if (ifp->if_reassign != NULL)
972 ifp->if_reassign(ifp, new_vnet, NULL);
973
974 /*
975 * Switch to the context of the target vnet.
976 */
977 CURVNET_SET_QUIET(new_vnet);
978
979 IFNET_WLOCK();
980 if (ifindex_alloc_locked(&idx) != 0) {
981 IFNET_WUNLOCK();
982 panic("if_index overflow");
983 }
984 ifp->if_index = idx;
985 ifnet_setbyindex_locked(ifp->if_index, ifp);
986 IFNET_WUNLOCK();
987
988 if_attach_internal(ifp, 1);
989
990 CURVNET_RESTORE();
991 }
992
993 /*
994 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
995 */
996 static int
997 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
998 {
999 struct prison *pr;
1000 struct ifnet *difp;
1001
1002 /* Try to find the prison within our visibility. */
1003 sx_slock(&allprison_lock);
1004 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1005 sx_sunlock(&allprison_lock);
1006 if (pr == NULL)
1007 return (ENXIO);
1008 prison_hold_locked(pr);
1009 mtx_unlock(&pr->pr_mtx);
1010
1011 /* Do not try to move the iface from and to the same prison. */
1012 if (pr->pr_vnet == ifp->if_vnet) {
1013 prison_free(pr);
1014 return (EEXIST);
1015 }
1016
1017 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1018 /* XXX Lock interfaces to avoid races. */
1019 CURVNET_SET_QUIET(pr->pr_vnet);
1020 difp = ifunit(ifname);
1021 CURVNET_RESTORE();
1022 if (difp != NULL) {
1023 prison_free(pr);
1024 return (EEXIST);
1025 }
1026
1027 /* Move the interface into the child jail/vnet. */
1028 if_vmove(ifp, pr->pr_vnet);
1029
1030 /* Report the new if_xname back to the userland. */
1031 sprintf(ifname, "%s", ifp->if_xname);
1032
1033 prison_free(pr);
1034 return (0);
1035 }
1036
1037 static int
1038 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1039 {
1040 struct prison *pr;
1041 struct vnet *vnet_dst;
1042 struct ifnet *ifp;
1043
1044 /* Try to find the prison within our visibility. */
1045 sx_slock(&allprison_lock);
1046 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1047 sx_sunlock(&allprison_lock);
1048 if (pr == NULL)
1049 return (ENXIO);
1050 prison_hold_locked(pr);
1051 mtx_unlock(&pr->pr_mtx);
1052
1053 /* Make sure the named iface exists in the source prison/vnet. */
1054 CURVNET_SET(pr->pr_vnet);
1055 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1056 if (ifp == NULL) {
1057 CURVNET_RESTORE();
1058 prison_free(pr);
1059 return (ENXIO);
1060 }
1061
1062 /* Do not try to move the iface from and to the same prison. */
1063 vnet_dst = TD_TO_VNET(td);
1064 if (vnet_dst == ifp->if_vnet) {
1065 CURVNET_RESTORE();
1066 prison_free(pr);
1067 return (EEXIST);
1068 }
1069
1070 /* Get interface back from child jail/vnet. */
1071 if_vmove(ifp, vnet_dst);
1072 CURVNET_RESTORE();
1073
1074 /* Report the new if_xname back to the userland. */
1075 sprintf(ifname, "%s", ifp->if_xname);
1076
1077 prison_free(pr);
1078 return (0);
1079 }
1080 #endif /* VIMAGE */
1081
1082 /*
1083 * Add a group to an interface
1084 */
1085 int
1086 if_addgroup(struct ifnet *ifp, const char *groupname)
1087 {
1088 struct ifg_list *ifgl;
1089 struct ifg_group *ifg = NULL;
1090 struct ifg_member *ifgm;
1091 int new = 0;
1092
1093 if (groupname[0] && groupname[strlen(groupname) - 1] >= '' &&
1094 groupname[strlen(groupname) - 1] <= '9')
1095 return (EINVAL);
1096
1097 IFNET_WLOCK();
1098 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1099 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1100 IFNET_WUNLOCK();
1101 return (EEXIST);
1102 }
1103
1104 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1105 M_NOWAIT)) == NULL) {
1106 IFNET_WUNLOCK();
1107 return (ENOMEM);
1108 }
1109
1110 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1111 M_TEMP, M_NOWAIT)) == NULL) {
1112 free(ifgl, M_TEMP);
1113 IFNET_WUNLOCK();
1114 return (ENOMEM);
1115 }
1116
1117 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1118 if (!strcmp(ifg->ifg_group, groupname))
1119 break;
1120
1121 if (ifg == NULL) {
1122 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1123 M_TEMP, M_NOWAIT)) == NULL) {
1124 free(ifgl, M_TEMP);
1125 free(ifgm, M_TEMP);
1126 IFNET_WUNLOCK();
1127 return (ENOMEM);
1128 }
1129 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1130 ifg->ifg_refcnt = 0;
1131 TAILQ_INIT(&ifg->ifg_members);
1132 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1133 new = 1;
1134 }
1135
1136 ifg->ifg_refcnt++;
1137 ifgl->ifgl_group = ifg;
1138 ifgm->ifgm_ifp = ifp;
1139
1140 IF_ADDR_WLOCK(ifp);
1141 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1142 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1143 IF_ADDR_WUNLOCK(ifp);
1144
1145 IFNET_WUNLOCK();
1146
1147 if (new)
1148 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1149 EVENTHANDLER_INVOKE(group_change_event, groupname);
1150
1151 return (0);
1152 }
1153
1154 /*
1155 * Remove a group from an interface
1156 */
1157 int
1158 if_delgroup(struct ifnet *ifp, const char *groupname)
1159 {
1160 struct ifg_list *ifgl;
1161 struct ifg_member *ifgm;
1162
1163 IFNET_WLOCK();
1164 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1165 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1166 break;
1167 if (ifgl == NULL) {
1168 IFNET_WUNLOCK();
1169 return (ENOENT);
1170 }
1171
1172 IF_ADDR_WLOCK(ifp);
1173 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1174 IF_ADDR_WUNLOCK(ifp);
1175
1176 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1177 if (ifgm->ifgm_ifp == ifp)
1178 break;
1179
1180 if (ifgm != NULL) {
1181 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1182 free(ifgm, M_TEMP);
1183 }
1184
1185 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1186 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1187 IFNET_WUNLOCK();
1188 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1189 free(ifgl->ifgl_group, M_TEMP);
1190 } else
1191 IFNET_WUNLOCK();
1192
1193 free(ifgl, M_TEMP);
1194
1195 EVENTHANDLER_INVOKE(group_change_event, groupname);
1196
1197 return (0);
1198 }
1199
1200 /*
1201 * Remove an interface from all groups
1202 */
1203 static void
1204 if_delgroups(struct ifnet *ifp)
1205 {
1206 struct ifg_list *ifgl;
1207 struct ifg_member *ifgm;
1208 char groupname[IFNAMSIZ];
1209
1210 IFNET_WLOCK();
1211 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1212 ifgl = TAILQ_FIRST(&ifp->if_groups);
1213
1214 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1215
1216 IF_ADDR_WLOCK(ifp);
1217 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1218 IF_ADDR_WUNLOCK(ifp);
1219
1220 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1221 if (ifgm->ifgm_ifp == ifp)
1222 break;
1223
1224 if (ifgm != NULL) {
1225 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1226 ifgm_next);
1227 free(ifgm, M_TEMP);
1228 }
1229
1230 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1231 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1232 IFNET_WUNLOCK();
1233 EVENTHANDLER_INVOKE(group_detach_event,
1234 ifgl->ifgl_group);
1235 free(ifgl->ifgl_group, M_TEMP);
1236 } else
1237 IFNET_WUNLOCK();
1238
1239 free(ifgl, M_TEMP);
1240
1241 EVENTHANDLER_INVOKE(group_change_event, groupname);
1242
1243 IFNET_WLOCK();
1244 }
1245 IFNET_WUNLOCK();
1246 }
1247
1248 /*
1249 * Stores all groups from an interface in memory pointed
1250 * to by data
1251 */
1252 static int
1253 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp)
1254 {
1255 int len, error;
1256 struct ifg_list *ifgl;
1257 struct ifg_req ifgrq, *ifgp;
1258 struct ifgroupreq *ifgr = data;
1259
1260 if (ifgr->ifgr_len == 0) {
1261 IF_ADDR_RLOCK(ifp);
1262 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1263 ifgr->ifgr_len += sizeof(struct ifg_req);
1264 IF_ADDR_RUNLOCK(ifp);
1265 return (0);
1266 }
1267
1268 len = ifgr->ifgr_len;
1269 ifgp = ifgr->ifgr_groups;
1270 /* XXX: wire */
1271 IF_ADDR_RLOCK(ifp);
1272 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1273 if (len < sizeof(ifgrq)) {
1274 IF_ADDR_RUNLOCK(ifp);
1275 return (EINVAL);
1276 }
1277 bzero(&ifgrq, sizeof ifgrq);
1278 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1279 sizeof(ifgrq.ifgrq_group));
1280 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1281 IF_ADDR_RUNLOCK(ifp);
1282 return (error);
1283 }
1284 len -= sizeof(ifgrq);
1285 ifgp++;
1286 }
1287 IF_ADDR_RUNLOCK(ifp);
1288
1289 return (0);
1290 }
1291
1292 /*
1293 * Stores all members of a group in memory pointed to by data
1294 */
1295 static int
1296 if_getgroupmembers(struct ifgroupreq *data)
1297 {
1298 struct ifgroupreq *ifgr = data;
1299 struct ifg_group *ifg;
1300 struct ifg_member *ifgm;
1301 struct ifg_req ifgrq, *ifgp;
1302 int len, error;
1303
1304 IFNET_RLOCK();
1305 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1306 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1307 break;
1308 if (ifg == NULL) {
1309 IFNET_RUNLOCK();
1310 return (ENOENT);
1311 }
1312
1313 if (ifgr->ifgr_len == 0) {
1314 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1315 ifgr->ifgr_len += sizeof(ifgrq);
1316 IFNET_RUNLOCK();
1317 return (0);
1318 }
1319
1320 len = ifgr->ifgr_len;
1321 ifgp = ifgr->ifgr_groups;
1322 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1323 if (len < sizeof(ifgrq)) {
1324 IFNET_RUNLOCK();
1325 return (EINVAL);
1326 }
1327 bzero(&ifgrq, sizeof ifgrq);
1328 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1329 sizeof(ifgrq.ifgrq_member));
1330 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1331 IFNET_RUNLOCK();
1332 return (error);
1333 }
1334 len -= sizeof(ifgrq);
1335 ifgp++;
1336 }
1337 IFNET_RUNLOCK();
1338
1339 return (0);
1340 }
1341
1342 /*
1343 * Delete Routes for a Network Interface
1344 *
1345 * Called for each routing entry via the rnh->rnh_walktree() call above
1346 * to delete all route entries referencing a detaching network interface.
1347 *
1348 * Arguments:
1349 * rn pointer to node in the routing table
1350 * arg argument passed to rnh->rnh_walktree() - detaching interface
1351 *
1352 * Returns:
1353 * 0 successful
1354 * errno failed - reason indicated
1355 *
1356 */
1357 static int
1358 if_rtdel(struct radix_node *rn, void *arg)
1359 {
1360 struct rtentry *rt = (struct rtentry *)rn;
1361 struct ifnet *ifp = arg;
1362 int err;
1363
1364 if (rt->rt_ifp == ifp) {
1365
1366 /*
1367 * Protect (sorta) against walktree recursion problems
1368 * with cloned routes
1369 */
1370 if ((rt->rt_flags & RTF_UP) == 0)
1371 return (0);
1372
1373 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1374 rt_mask(rt),
1375 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED,
1376 (struct rtentry **) NULL, rt->rt_fibnum);
1377 if (err) {
1378 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1379 }
1380 }
1381
1382 return (0);
1383 }
1384
1385 /*
1386 * Wrapper functions for struct ifnet address list locking macros. These are
1387 * used by kernel modules to avoid encoding programming interface or binary
1388 * interface assumptions that may be violated when kernel-internal locking
1389 * approaches change.
1390 */
1391 void
1392 if_addr_rlock(struct ifnet *ifp)
1393 {
1394
1395 IF_ADDR_RLOCK(ifp);
1396 }
1397
1398 void
1399 if_addr_runlock(struct ifnet *ifp)
1400 {
1401
1402 IF_ADDR_RUNLOCK(ifp);
1403 }
1404
1405 void
1406 if_maddr_rlock(struct ifnet *ifp)
1407 {
1408
1409 IF_ADDR_RLOCK(ifp);
1410 }
1411
1412 void
1413 if_maddr_runlock(struct ifnet *ifp)
1414 {
1415
1416 IF_ADDR_RUNLOCK(ifp);
1417 }
1418
1419 /*
1420 * Initialization, destruction and refcounting functions for ifaddrs.
1421 */
1422 void
1423 ifa_init(struct ifaddr *ifa)
1424 {
1425
1426 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF);
1427 refcount_init(&ifa->ifa_refcnt, 1);
1428 ifa->if_data.ifi_datalen = sizeof(ifa->if_data);
1429 }
1430
1431 void
1432 ifa_ref(struct ifaddr *ifa)
1433 {
1434
1435 refcount_acquire(&ifa->ifa_refcnt);
1436 }
1437
1438 void
1439 ifa_free(struct ifaddr *ifa)
1440 {
1441
1442 if (refcount_release(&ifa->ifa_refcnt)) {
1443 mtx_destroy(&ifa->ifa_mtx);
1444 free(ifa, M_IFADDR);
1445 }
1446 }
1447
1448 int
1449 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1450 {
1451 int error = 0;
1452 struct rtentry *rt = NULL;
1453 struct rt_addrinfo info;
1454 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1455
1456 bzero(&info, sizeof(info));
1457 info.rti_ifp = V_loif;
1458 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1459 info.rti_info[RTAX_DST] = ia;
1460 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1461 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib);
1462
1463 if (error == 0 && rt != NULL) {
1464 RT_LOCK(rt);
1465 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1466 ifa->ifa_ifp->if_type;
1467 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1468 ifa->ifa_ifp->if_index;
1469 RT_REMREF(rt);
1470 RT_UNLOCK(rt);
1471 } else if (error != 0)
1472 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error);
1473
1474 return (error);
1475 }
1476
1477 int
1478 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1479 {
1480 int error = 0;
1481 struct rt_addrinfo info;
1482 struct sockaddr_dl null_sdl;
1483
1484 bzero(&null_sdl, sizeof(null_sdl));
1485 null_sdl.sdl_len = sizeof(null_sdl);
1486 null_sdl.sdl_family = AF_LINK;
1487 null_sdl.sdl_type = ifa->ifa_ifp->if_type;
1488 null_sdl.sdl_index = ifa->ifa_ifp->if_index;
1489 bzero(&info, sizeof(info));
1490 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC;
1491 info.rti_info[RTAX_DST] = ia;
1492 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1493 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib);
1494
1495 if (error != 0)
1496 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error);
1497
1498 return (error);
1499 }
1500
1501 /*
1502 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1503 * structs used to represent other address families, it is necessary
1504 * to perform a different comparison.
1505 */
1506
1507 #define sa_equal(a1, a2) \
1508 (bcmp((a1), (a2), ((a1))->sa_len) == 0)
1509
1510 #define sa_dl_equal(a1, a2) \
1511 ((((struct sockaddr_dl *)(a1))->sdl_len == \
1512 ((struct sockaddr_dl *)(a2))->sdl_len) && \
1513 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \
1514 LLADDR((struct sockaddr_dl *)(a2)), \
1515 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1516
1517 /*
1518 * Locate an interface based on a complete address.
1519 */
1520 /*ARGSUSED*/
1521 static struct ifaddr *
1522 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref)
1523 {
1524 struct ifnet *ifp;
1525 struct ifaddr *ifa;
1526
1527 IFNET_RLOCK_NOSLEEP();
1528 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1529 IF_ADDR_RLOCK(ifp);
1530 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1531 if (ifa->ifa_addr->sa_family != addr->sa_family)
1532 continue;
1533 if (sa_equal(addr, ifa->ifa_addr)) {
1534 if (getref)
1535 ifa_ref(ifa);
1536 IF_ADDR_RUNLOCK(ifp);
1537 goto done;
1538 }
1539 /* IP6 doesn't have broadcast */
1540 if ((ifp->if_flags & IFF_BROADCAST) &&
1541 ifa->ifa_broadaddr &&
1542 ifa->ifa_broadaddr->sa_len != 0 &&
1543 sa_equal(ifa->ifa_broadaddr, addr)) {
1544 if (getref)
1545 ifa_ref(ifa);
1546 IF_ADDR_RUNLOCK(ifp);
1547 goto done;
1548 }
1549 }
1550 IF_ADDR_RUNLOCK(ifp);
1551 }
1552 ifa = NULL;
1553 done:
1554 IFNET_RUNLOCK_NOSLEEP();
1555 return (ifa);
1556 }
1557
1558 struct ifaddr *
1559 ifa_ifwithaddr(struct sockaddr *addr)
1560 {
1561
1562 return (ifa_ifwithaddr_internal(addr, 1));
1563 }
1564
1565 int
1566 ifa_ifwithaddr_check(struct sockaddr *addr)
1567 {
1568
1569 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1570 }
1571
1572 /*
1573 * Locate an interface based on the broadcast address.
1574 */
1575 /* ARGSUSED */
1576 struct ifaddr *
1577 ifa_ifwithbroadaddr(struct sockaddr *addr)
1578 {
1579 struct ifnet *ifp;
1580 struct ifaddr *ifa;
1581
1582 IFNET_RLOCK_NOSLEEP();
1583 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1584 IF_ADDR_RLOCK(ifp);
1585 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1586 if (ifa->ifa_addr->sa_family != addr->sa_family)
1587 continue;
1588 if ((ifp->if_flags & IFF_BROADCAST) &&
1589 ifa->ifa_broadaddr &&
1590 ifa->ifa_broadaddr->sa_len != 0 &&
1591 sa_equal(ifa->ifa_broadaddr, addr)) {
1592 ifa_ref(ifa);
1593 IF_ADDR_RUNLOCK(ifp);
1594 goto done;
1595 }
1596 }
1597 IF_ADDR_RUNLOCK(ifp);
1598 }
1599 ifa = NULL;
1600 done:
1601 IFNET_RUNLOCK_NOSLEEP();
1602 return (ifa);
1603 }
1604
1605 /*
1606 * Locate the point to point interface with a given destination address.
1607 */
1608 /*ARGSUSED*/
1609 struct ifaddr *
1610 ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum)
1611 {
1612 struct ifnet *ifp;
1613 struct ifaddr *ifa;
1614
1615 IFNET_RLOCK_NOSLEEP();
1616 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1617 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1618 continue;
1619 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1620 continue;
1621 IF_ADDR_RLOCK(ifp);
1622 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1623 if (ifa->ifa_addr->sa_family != addr->sa_family)
1624 continue;
1625 if (ifa->ifa_dstaddr != NULL &&
1626 sa_equal(addr, ifa->ifa_dstaddr)) {
1627 ifa_ref(ifa);
1628 IF_ADDR_RUNLOCK(ifp);
1629 goto done;
1630 }
1631 }
1632 IF_ADDR_RUNLOCK(ifp);
1633 }
1634 ifa = NULL;
1635 done:
1636 IFNET_RUNLOCK_NOSLEEP();
1637 return (ifa);
1638 }
1639
1640 struct ifaddr *
1641 ifa_ifwithdstaddr(struct sockaddr *addr)
1642 {
1643
1644 return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS));
1645 }
1646
1647 /*
1648 * Find an interface on a specific network. If many, choice
1649 * is most specific found.
1650 */
1651 struct ifaddr *
1652 ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum)
1653 {
1654 struct ifnet *ifp;
1655 struct ifaddr *ifa;
1656 struct ifaddr *ifa_maybe = NULL;
1657 u_int af = addr->sa_family;
1658 char *addr_data = addr->sa_data, *cplim;
1659
1660 /*
1661 * AF_LINK addresses can be looked up directly by their index number,
1662 * so do that if we can.
1663 */
1664 if (af == AF_LINK) {
1665 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1666 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
1667 return (ifaddr_byindex(sdl->sdl_index));
1668 }
1669
1670 /*
1671 * Scan though each interface, looking for ones that have addresses
1672 * in this address family and the requested fib. Maintain a reference
1673 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
1674 * kept it stable when we move onto the next interface.
1675 */
1676 IFNET_RLOCK_NOSLEEP();
1677 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1678 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1679 continue;
1680 IF_ADDR_RLOCK(ifp);
1681 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1682 char *cp, *cp2, *cp3;
1683
1684 if (ifa->ifa_addr->sa_family != af)
1685 next: continue;
1686 if (af == AF_INET &&
1687 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1688 /*
1689 * This is a bit broken as it doesn't
1690 * take into account that the remote end may
1691 * be a single node in the network we are
1692 * looking for.
1693 * The trouble is that we don't know the
1694 * netmask for the remote end.
1695 */
1696 if (ifa->ifa_dstaddr != NULL &&
1697 sa_equal(addr, ifa->ifa_dstaddr)) {
1698 ifa_ref(ifa);
1699 IF_ADDR_RUNLOCK(ifp);
1700 goto done;
1701 }
1702 } else {
1703 /*
1704 * if we have a special address handler,
1705 * then use it instead of the generic one.
1706 */
1707 if (ifa->ifa_claim_addr) {
1708 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1709 ifa_ref(ifa);
1710 IF_ADDR_RUNLOCK(ifp);
1711 goto done;
1712 }
1713 continue;
1714 }
1715
1716 /*
1717 * Scan all the bits in the ifa's address.
1718 * If a bit dissagrees with what we are
1719 * looking for, mask it with the netmask
1720 * to see if it really matters.
1721 * (A byte at a time)
1722 */
1723 if (ifa->ifa_netmask == 0)
1724 continue;
1725 cp = addr_data;
1726 cp2 = ifa->ifa_addr->sa_data;
1727 cp3 = ifa->ifa_netmask->sa_data;
1728 cplim = ifa->ifa_netmask->sa_len
1729 + (char *)ifa->ifa_netmask;
1730 while (cp3 < cplim)
1731 if ((*cp++ ^ *cp2++) & *cp3++)
1732 goto next; /* next address! */
1733 /*
1734 * If the netmask of what we just found
1735 * is more specific than what we had before
1736 * (if we had one), or if the virtual status
1737 * of new prefix is better than of the old one,
1738 * then remember the new one before continuing
1739 * to search for an even better one.
1740 */
1741 if (ifa_maybe == NULL ||
1742 ifa_preferred(ifa_maybe, ifa) ||
1743 rn_refines((caddr_t)ifa->ifa_netmask,
1744 (caddr_t)ifa_maybe->ifa_netmask)) {
1745 if (ifa_maybe != NULL)
1746 ifa_free(ifa_maybe);
1747 ifa_maybe = ifa;
1748 ifa_ref(ifa_maybe);
1749 }
1750 }
1751 }
1752 IF_ADDR_RUNLOCK(ifp);
1753 }
1754 ifa = ifa_maybe;
1755 ifa_maybe = NULL;
1756 done:
1757 IFNET_RUNLOCK_NOSLEEP();
1758 if (ifa_maybe != NULL)
1759 ifa_free(ifa_maybe);
1760 return (ifa);
1761 }
1762
1763 struct ifaddr *
1764 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp)
1765 {
1766
1767 return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS));
1768 }
1769
1770 /*
1771 * Find an interface address specific to an interface best matching
1772 * a given address.
1773 */
1774 struct ifaddr *
1775 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1776 {
1777 struct ifaddr *ifa;
1778 char *cp, *cp2, *cp3;
1779 char *cplim;
1780 struct ifaddr *ifa_maybe = NULL;
1781 u_int af = addr->sa_family;
1782
1783 if (af >= AF_MAX)
1784 return (NULL);
1785 IF_ADDR_RLOCK(ifp);
1786 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1787 if (ifa->ifa_addr->sa_family != af)
1788 continue;
1789 if (ifa_maybe == NULL)
1790 ifa_maybe = ifa;
1791 if (ifa->ifa_netmask == 0) {
1792 if (sa_equal(addr, ifa->ifa_addr) ||
1793 (ifa->ifa_dstaddr &&
1794 sa_equal(addr, ifa->ifa_dstaddr)))
1795 goto done;
1796 continue;
1797 }
1798 if (ifp->if_flags & IFF_POINTOPOINT) {
1799 if (sa_equal(addr, ifa->ifa_dstaddr))
1800 goto done;
1801 } else {
1802 cp = addr->sa_data;
1803 cp2 = ifa->ifa_addr->sa_data;
1804 cp3 = ifa->ifa_netmask->sa_data;
1805 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1806 for (; cp3 < cplim; cp3++)
1807 if ((*cp++ ^ *cp2++) & *cp3)
1808 break;
1809 if (cp3 == cplim)
1810 goto done;
1811 }
1812 }
1813 ifa = ifa_maybe;
1814 done:
1815 if (ifa != NULL)
1816 ifa_ref(ifa);
1817 IF_ADDR_RUNLOCK(ifp);
1818 return (ifa);
1819 }
1820
1821 /*
1822 * See whether new ifa is better than current one:
1823 * 1) A non-virtual one is preferred over virtual.
1824 * 2) A virtual in master state preferred over any other state.
1825 *
1826 * Used in several address selecting functions.
1827 */
1828 int
1829 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1830 {
1831
1832 return (cur->ifa_carp && (!next->ifa_carp ||
1833 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1834 }
1835
1836 #include <net/if_llatbl.h>
1837
1838 /*
1839 * Default action when installing a route with a Link Level gateway.
1840 * Lookup an appropriate real ifa to point to.
1841 * This should be moved to /sys/net/link.c eventually.
1842 */
1843 static void
1844 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1845 {
1846 struct ifaddr *ifa, *oifa;
1847 struct sockaddr *dst;
1848 struct ifnet *ifp;
1849
1850 RT_LOCK_ASSERT(rt);
1851
1852 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1853 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1854 return;
1855 ifa = ifaof_ifpforaddr(dst, ifp);
1856 if (ifa) {
1857 oifa = rt->rt_ifa;
1858 rt->rt_ifa = ifa;
1859 ifa_free(oifa);
1860 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1861 ifa->ifa_rtrequest(cmd, rt, info);
1862 }
1863 }
1864
1865 /*
1866 * Mark an interface down and notify protocols of
1867 * the transition.
1868 */
1869 static void
1870 if_unroute(struct ifnet *ifp, int flag, int fam)
1871 {
1872 struct ifaddr *ifa;
1873
1874 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
1875
1876 ifp->if_flags &= ~flag;
1877 getmicrotime(&ifp->if_lastchange);
1878 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1879 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1880 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1881 ifp->if_qflush(ifp);
1882
1883 if (ifp->if_carp)
1884 (*carp_linkstate_p)(ifp);
1885 rt_ifmsg(ifp);
1886 }
1887
1888 /*
1889 * Mark an interface up and notify protocols of
1890 * the transition.
1891 */
1892 static void
1893 if_route(struct ifnet *ifp, int flag, int fam)
1894 {
1895 struct ifaddr *ifa;
1896
1897 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
1898
1899 ifp->if_flags |= flag;
1900 getmicrotime(&ifp->if_lastchange);
1901 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1902 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1903 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1904 if (ifp->if_carp)
1905 (*carp_linkstate_p)(ifp);
1906 rt_ifmsg(ifp);
1907 #ifdef INET6
1908 in6_if_up(ifp);
1909 #endif
1910 }
1911
1912 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1913 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1914 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1915 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1916 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1917 int (*vlan_setcookie_p)(struct ifnet *, void *);
1918 void *(*vlan_cookie_p)(struct ifnet *);
1919
1920 /*
1921 * Handle a change in the interface link state. To avoid LORs
1922 * between driver lock and upper layer locks, as well as possible
1923 * recursions, we post event to taskqueue, and all job
1924 * is done in static do_link_state_change().
1925 */
1926 void
1927 if_link_state_change(struct ifnet *ifp, int link_state)
1928 {
1929 /* Return if state hasn't changed. */
1930 if (ifp->if_link_state == link_state)
1931 return;
1932
1933 ifp->if_link_state = link_state;
1934
1935 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
1936 }
1937
1938 static void
1939 do_link_state_change(void *arg, int pending)
1940 {
1941 struct ifnet *ifp = (struct ifnet *)arg;
1942 int link_state = ifp->if_link_state;
1943 CURVNET_SET(ifp->if_vnet);
1944
1945 /* Notify that the link state has changed. */
1946 rt_ifmsg(ifp);
1947 if (ifp->if_vlantrunk != NULL)
1948 (*vlan_link_state_p)(ifp);
1949
1950 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
1951 IFP2AC(ifp)->ac_netgraph != NULL)
1952 (*ng_ether_link_state_p)(ifp, link_state);
1953 if (ifp->if_carp)
1954 (*carp_linkstate_p)(ifp);
1955 if (ifp->if_bridge)
1956 (*bridge_linkstate_p)(ifp);
1957 if (ifp->if_lagg)
1958 (*lagg_linkstate_p)(ifp, link_state);
1959
1960 if (IS_DEFAULT_VNET(curvnet))
1961 devctl_notify("IFNET", ifp->if_xname,
1962 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
1963 NULL);
1964 if (pending > 1)
1965 if_printf(ifp, "%d link states coalesced\n", pending);
1966 if (log_link_state_change)
1967 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
1968 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
1969 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state);
1970 CURVNET_RESTORE();
1971 }
1972
1973 /*
1974 * Mark an interface down and notify protocols of
1975 * the transition.
1976 */
1977 void
1978 if_down(struct ifnet *ifp)
1979 {
1980
1981 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1982 }
1983
1984 /*
1985 * Mark an interface up and notify protocols of
1986 * the transition.
1987 */
1988 void
1989 if_up(struct ifnet *ifp)
1990 {
1991
1992 if_route(ifp, IFF_UP, AF_UNSPEC);
1993 }
1994
1995 /*
1996 * Flush an interface queue.
1997 */
1998 void
1999 if_qflush(struct ifnet *ifp)
2000 {
2001 struct mbuf *m, *n;
2002 struct ifaltq *ifq;
2003
2004 ifq = &ifp->if_snd;
2005 IFQ_LOCK(ifq);
2006 #ifdef ALTQ
2007 if (ALTQ_IS_ENABLED(ifq))
2008 ALTQ_PURGE(ifq);
2009 #endif
2010 n = ifq->ifq_head;
2011 while ((m = n) != 0) {
2012 n = m->m_nextpkt;
2013 m_freem(m);
2014 }
2015 ifq->ifq_head = 0;
2016 ifq->ifq_tail = 0;
2017 ifq->ifq_len = 0;
2018 IFQ_UNLOCK(ifq);
2019 }
2020
2021 /*
2022 * Map interface name to interface structure pointer, with or without
2023 * returning a reference.
2024 */
2025 struct ifnet *
2026 ifunit_ref(const char *name)
2027 {
2028 struct ifnet *ifp;
2029
2030 IFNET_RLOCK_NOSLEEP();
2031 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2032 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2033 !(ifp->if_flags & IFF_DYING))
2034 break;
2035 }
2036 if (ifp != NULL)
2037 if_ref(ifp);
2038 IFNET_RUNLOCK_NOSLEEP();
2039 return (ifp);
2040 }
2041
2042 struct ifnet *
2043 ifunit(const char *name)
2044 {
2045 struct ifnet *ifp;
2046
2047 IFNET_RLOCK_NOSLEEP();
2048 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2049 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2050 break;
2051 }
2052 IFNET_RUNLOCK_NOSLEEP();
2053 return (ifp);
2054 }
2055
2056 /*
2057 * Hardware specific interface ioctls.
2058 */
2059 static int
2060 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2061 {
2062 struct ifreq *ifr;
2063 struct ifstat *ifs;
2064 int error = 0;
2065 int new_flags, temp_flags;
2066 size_t namelen, onamelen;
2067 size_t descrlen;
2068 char *descrbuf, *odescrbuf;
2069 char new_name[IFNAMSIZ];
2070 struct ifaddr *ifa;
2071 struct sockaddr_dl *sdl;
2072
2073 ifr = (struct ifreq *)data;
2074 switch (cmd) {
2075 case SIOCGIFINDEX:
2076 ifr->ifr_index = ifp->if_index;
2077 break;
2078
2079 case SIOCGIFFLAGS:
2080 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2081 ifr->ifr_flags = temp_flags & 0xffff;
2082 ifr->ifr_flagshigh = temp_flags >> 16;
2083 break;
2084
2085 case SIOCGIFCAP:
2086 ifr->ifr_reqcap = ifp->if_capabilities;
2087 ifr->ifr_curcap = ifp->if_capenable;
2088 break;
2089
2090 #ifdef MAC
2091 case SIOCGIFMAC:
2092 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2093 break;
2094 #endif
2095
2096 case SIOCGIFMETRIC:
2097 ifr->ifr_metric = ifp->if_metric;
2098 break;
2099
2100 case SIOCGIFMTU:
2101 ifr->ifr_mtu = ifp->if_mtu;
2102 break;
2103
2104 case SIOCGIFPHYS:
2105 ifr->ifr_phys = ifp->if_physical;
2106 break;
2107
2108 case SIOCGIFDESCR:
2109 error = 0;
2110 sx_slock(&ifdescr_sx);
2111 if (ifp->if_description == NULL)
2112 error = ENOMSG;
2113 else {
2114 /* space for terminating nul */
2115 descrlen = strlen(ifp->if_description) + 1;
2116 if (ifr->ifr_buffer.length < descrlen)
2117 ifr->ifr_buffer.buffer = NULL;
2118 else
2119 error = copyout(ifp->if_description,
2120 ifr->ifr_buffer.buffer, descrlen);
2121 ifr->ifr_buffer.length = descrlen;
2122 }
2123 sx_sunlock(&ifdescr_sx);
2124 break;
2125
2126 case SIOCSIFDESCR:
2127 error = priv_check(td, PRIV_NET_SETIFDESCR);
2128 if (error)
2129 return (error);
2130
2131 /*
2132 * Copy only (length-1) bytes to make sure that
2133 * if_description is always nul terminated. The
2134 * length parameter is supposed to count the
2135 * terminating nul in.
2136 */
2137 if (ifr->ifr_buffer.length > ifdescr_maxlen)
2138 return (ENAMETOOLONG);
2139 else if (ifr->ifr_buffer.length == 0)
2140 descrbuf = NULL;
2141 else {
2142 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR,
2143 M_WAITOK | M_ZERO);
2144 error = copyin(ifr->ifr_buffer.buffer, descrbuf,
2145 ifr->ifr_buffer.length - 1);
2146 if (error) {
2147 free(descrbuf, M_IFDESCR);
2148 break;
2149 }
2150 }
2151
2152 sx_xlock(&ifdescr_sx);
2153 odescrbuf = ifp->if_description;
2154 ifp->if_description = descrbuf;
2155 sx_xunlock(&ifdescr_sx);
2156
2157 getmicrotime(&ifp->if_lastchange);
2158 free(odescrbuf, M_IFDESCR);
2159 break;
2160
2161 case SIOCGIFFIB:
2162 ifr->ifr_fib = ifp->if_fib;
2163 break;
2164
2165 case SIOCSIFFIB:
2166 error = priv_check(td, PRIV_NET_SETIFFIB);
2167 if (error)
2168 return (error);
2169 if (ifr->ifr_fib >= rt_numfibs)
2170 return (EINVAL);
2171
2172 ifp->if_fib = ifr->ifr_fib;
2173 break;
2174
2175 case SIOCSIFFLAGS:
2176 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2177 if (error)
2178 return (error);
2179 /*
2180 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2181 * check, so we don't need special handling here yet.
2182 */
2183 new_flags = (ifr->ifr_flags & 0xffff) |
2184 (ifr->ifr_flagshigh << 16);
2185 if (ifp->if_flags & IFF_SMART) {
2186 /* Smart drivers twiddle their own routes */
2187 } else if (ifp->if_flags & IFF_UP &&
2188 (new_flags & IFF_UP) == 0) {
2189 if_down(ifp);
2190 } else if (new_flags & IFF_UP &&
2191 (ifp->if_flags & IFF_UP) == 0) {
2192 if_up(ifp);
2193 }
2194 /* See if permanently promiscuous mode bit is about to flip */
2195 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2196 if (new_flags & IFF_PPROMISC)
2197 ifp->if_flags |= IFF_PROMISC;
2198 else if (ifp->if_pcount == 0)
2199 ifp->if_flags &= ~IFF_PROMISC;
2200 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2201 ifp->if_xname,
2202 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled");
2203 }
2204 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2205 (new_flags &~ IFF_CANTCHANGE);
2206 if (ifp->if_ioctl) {
2207 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2208 }
2209 getmicrotime(&ifp->if_lastchange);
2210 break;
2211
2212 case SIOCSIFCAP:
2213 error = priv_check(td, PRIV_NET_SETIFCAP);
2214 if (error)
2215 return (error);
2216 if (ifp->if_ioctl == NULL)
2217 return (EOPNOTSUPP);
2218 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2219 return (EINVAL);
2220 error = (*ifp->if_ioctl)(ifp, cmd, data);
2221 if (error == 0)
2222 getmicrotime(&ifp->if_lastchange);
2223 break;
2224
2225 #ifdef MAC
2226 case SIOCSIFMAC:
2227 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2228 break;
2229 #endif
2230
2231 case SIOCSIFNAME:
2232 error = priv_check(td, PRIV_NET_SETIFNAME);
2233 if (error)
2234 return (error);
2235 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2236 if (error != 0)
2237 return (error);
2238 if (new_name[0] == '\0')
2239 return (EINVAL);
2240 if (ifunit(new_name) != NULL)
2241 return (EEXIST);
2242
2243 /*
2244 * XXX: Locking. Nothing else seems to lock if_flags,
2245 * and there are numerous other races with the
2246 * ifunit() checks not being atomic with namespace
2247 * changes (renames, vmoves, if_attach, etc).
2248 */
2249 ifp->if_flags |= IFF_RENAMING;
2250
2251 /* Announce the departure of the interface. */
2252 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2253 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2254
2255 log(LOG_INFO, "%s: changing name to '%s'\n",
2256 ifp->if_xname, new_name);
2257
2258 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2259 ifa = ifp->if_addr;
2260 IFA_LOCK(ifa);
2261 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2262 namelen = strlen(new_name);
2263 onamelen = sdl->sdl_nlen;
2264 /*
2265 * Move the address if needed. This is safe because we
2266 * allocate space for a name of length IFNAMSIZ when we
2267 * create this in if_attach().
2268 */
2269 if (namelen != onamelen) {
2270 bcopy(sdl->sdl_data + onamelen,
2271 sdl->sdl_data + namelen, sdl->sdl_alen);
2272 }
2273 bcopy(new_name, sdl->sdl_data, namelen);
2274 sdl->sdl_nlen = namelen;
2275 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2276 bzero(sdl->sdl_data, onamelen);
2277 while (namelen != 0)
2278 sdl->sdl_data[--namelen] = 0xff;
2279 IFA_UNLOCK(ifa);
2280
2281 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2282 /* Announce the return of the interface. */
2283 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2284
2285 ifp->if_flags &= ~IFF_RENAMING;
2286 break;
2287
2288 #ifdef VIMAGE
2289 case SIOCSIFVNET:
2290 error = priv_check(td, PRIV_NET_SETIFVNET);
2291 if (error)
2292 return (error);
2293 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2294 break;
2295 #endif
2296
2297 case SIOCSIFMETRIC:
2298 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2299 if (error)
2300 return (error);
2301 ifp->if_metric = ifr->ifr_metric;
2302 getmicrotime(&ifp->if_lastchange);
2303 break;
2304
2305 case SIOCSIFPHYS:
2306 error = priv_check(td, PRIV_NET_SETIFPHYS);
2307 if (error)
2308 return (error);
2309 if (ifp->if_ioctl == NULL)
2310 return (EOPNOTSUPP);
2311 error = (*ifp->if_ioctl)(ifp, cmd, data);
2312 if (error == 0)
2313 getmicrotime(&ifp->if_lastchange);
2314 break;
2315
2316 case SIOCSIFMTU:
2317 {
2318 u_long oldmtu = ifp->if_mtu;
2319
2320 error = priv_check(td, PRIV_NET_SETIFMTU);
2321 if (error)
2322 return (error);
2323 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2324 return (EINVAL);
2325 if (ifp->if_ioctl == NULL)
2326 return (EOPNOTSUPP);
2327 error = (*ifp->if_ioctl)(ifp, cmd, data);
2328 if (error == 0) {
2329 getmicrotime(&ifp->if_lastchange);
2330 rt_ifmsg(ifp);
2331 }
2332 /*
2333 * If the link MTU changed, do network layer specific procedure.
2334 */
2335 if (ifp->if_mtu != oldmtu) {
2336 #ifdef INET6
2337 nd6_setmtu(ifp);
2338 #endif
2339 }
2340 break;
2341 }
2342
2343 case SIOCADDMULTI:
2344 case SIOCDELMULTI:
2345 if (cmd == SIOCADDMULTI)
2346 error = priv_check(td, PRIV_NET_ADDMULTI);
2347 else
2348 error = priv_check(td, PRIV_NET_DELMULTI);
2349 if (error)
2350 return (error);
2351
2352 /* Don't allow group membership on non-multicast interfaces. */
2353 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2354 return (EOPNOTSUPP);
2355
2356 /* Don't let users screw up protocols' entries. */
2357 if (ifr->ifr_addr.sa_family != AF_LINK)
2358 return (EINVAL);
2359
2360 if (cmd == SIOCADDMULTI) {
2361 struct ifmultiaddr *ifma;
2362
2363 /*
2364 * Userland is only permitted to join groups once
2365 * via the if_addmulti() KPI, because it cannot hold
2366 * struct ifmultiaddr * between calls. It may also
2367 * lose a race while we check if the membership
2368 * already exists.
2369 */
2370 IF_ADDR_RLOCK(ifp);
2371 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2372 IF_ADDR_RUNLOCK(ifp);
2373 if (ifma != NULL)
2374 error = EADDRINUSE;
2375 else
2376 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2377 } else {
2378 error = if_delmulti(ifp, &ifr->ifr_addr);
2379 }
2380 if (error == 0)
2381 getmicrotime(&ifp->if_lastchange);
2382 break;
2383
2384 case SIOCSIFPHYADDR:
2385 case SIOCDIFPHYADDR:
2386 #ifdef INET6
2387 case SIOCSIFPHYADDR_IN6:
2388 #endif
2389 case SIOCSLIFPHYADDR:
2390 case SIOCSIFMEDIA:
2391 case SIOCSIFGENERIC:
2392 error = priv_check(td, PRIV_NET_HWIOCTL);
2393 if (error)
2394 return (error);
2395 if (ifp->if_ioctl == NULL)
2396 return (EOPNOTSUPP);
2397 error = (*ifp->if_ioctl)(ifp, cmd, data);
2398 if (error == 0)
2399 getmicrotime(&ifp->if_lastchange);
2400 break;
2401
2402 case SIOCGIFSTATUS:
2403 ifs = (struct ifstat *)data;
2404 ifs->ascii[0] = '\0';
2405
2406 case SIOCGIFPSRCADDR:
2407 case SIOCGIFPDSTADDR:
2408 case SIOCGLIFPHYADDR:
2409 case SIOCGIFMEDIA:
2410 case SIOCGIFGENERIC:
2411 if (ifp->if_ioctl == NULL)
2412 return (EOPNOTSUPP);
2413 error = (*ifp->if_ioctl)(ifp, cmd, data);
2414 break;
2415
2416 case SIOCSIFLLADDR:
2417 error = priv_check(td, PRIV_NET_SETLLADDR);
2418 if (error)
2419 return (error);
2420 error = if_setlladdr(ifp,
2421 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2422 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2423 break;
2424
2425 case SIOCAIFGROUP:
2426 {
2427 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2428
2429 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2430 if (error)
2431 return (error);
2432 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2433 return (error);
2434 break;
2435 }
2436
2437 case SIOCGIFGROUP:
2438 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp)))
2439 return (error);
2440 break;
2441
2442 case SIOCDIFGROUP:
2443 {
2444 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr;
2445
2446 error = priv_check(td, PRIV_NET_DELIFGROUP);
2447 if (error)
2448 return (error);
2449 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2450 return (error);
2451 break;
2452 }
2453
2454 default:
2455 error = ENOIOCTL;
2456 break;
2457 }
2458 return (error);
2459 }
2460
2461 #ifdef COMPAT_FREEBSD32
2462 struct ifconf32 {
2463 int32_t ifc_len;
2464 union {
2465 uint32_t ifcu_buf;
2466 uint32_t ifcu_req;
2467 } ifc_ifcu;
2468 };
2469 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2470 #endif
2471
2472 /*
2473 * Interface ioctls.
2474 */
2475 int
2476 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2477 {
2478 struct ifnet *ifp;
2479 struct ifreq *ifr;
2480 int error;
2481 int oif_flags;
2482
2483 CURVNET_SET(so->so_vnet);
2484 switch (cmd) {
2485 case SIOCGIFCONF:
2486 case OSIOCGIFCONF:
2487 error = ifconf(cmd, data);
2488 CURVNET_RESTORE();
2489 return (error);
2490
2491 #ifdef COMPAT_FREEBSD32
2492 case SIOCGIFCONF32:
2493 {
2494 struct ifconf32 *ifc32;
2495 struct ifconf ifc;
2496
2497 ifc32 = (struct ifconf32 *)data;
2498 ifc.ifc_len = ifc32->ifc_len;
2499 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2500
2501 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2502 CURVNET_RESTORE();
2503 if (error == 0)
2504 ifc32->ifc_len = ifc.ifc_len;
2505 return (error);
2506 }
2507 #endif
2508 }
2509 ifr = (struct ifreq *)data;
2510
2511 switch (cmd) {
2512 #ifdef VIMAGE
2513 case SIOCSIFRVNET:
2514 error = priv_check(td, PRIV_NET_SETIFVNET);
2515 if (error == 0)
2516 error = if_vmove_reclaim(td, ifr->ifr_name,
2517 ifr->ifr_jid);
2518 CURVNET_RESTORE();
2519 return (error);
2520 #endif
2521 case SIOCIFCREATE:
2522 case SIOCIFCREATE2:
2523 error = priv_check(td, PRIV_NET_IFCREATE);
2524 if (error == 0)
2525 error = if_clone_create(ifr->ifr_name,
2526 sizeof(ifr->ifr_name),
2527 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL);
2528 CURVNET_RESTORE();
2529 return (error);
2530 case SIOCIFDESTROY:
2531 error = priv_check(td, PRIV_NET_IFDESTROY);
2532 if (error == 0)
2533 error = if_clone_destroy(ifr->ifr_name);
2534 CURVNET_RESTORE();
2535 return (error);
2536
2537 case SIOCIFGCLONERS:
2538 error = if_clone_list((struct if_clonereq *)data);
2539 CURVNET_RESTORE();
2540 return (error);
2541 case SIOCGIFGMEMB:
2542 error = if_getgroupmembers((struct ifgroupreq *)data);
2543 CURVNET_RESTORE();
2544 return (error);
2545 #if defined(INET) || defined(INET6)
2546 case SIOCSVH:
2547 case SIOCGVH:
2548 if (carp_ioctl_p == NULL)
2549 error = EPROTONOSUPPORT;
2550 else
2551 error = (*carp_ioctl_p)(ifr, cmd, td);
2552 CURVNET_RESTORE();
2553 return (error);
2554 #endif
2555 }
2556
2557 ifp = ifunit_ref(ifr->ifr_name);
2558 if (ifp == NULL) {
2559 CURVNET_RESTORE();
2560 return (ENXIO);
2561 }
2562
2563 error = ifhwioctl(cmd, ifp, data, td);
2564 if (error != ENOIOCTL) {
2565 if_rele(ifp);
2566 CURVNET_RESTORE();
2567 return (error);
2568 }
2569
2570 oif_flags = ifp->if_flags;
2571 if (so->so_proto == NULL) {
2572 if_rele(ifp);
2573 CURVNET_RESTORE();
2574 return (EOPNOTSUPP);
2575 }
2576
2577 /*
2578 * Pass the request on to the socket control method, and if the
2579 * latter returns EOPNOTSUPP, directly to the interface.
2580 *
2581 * Make an exception for the legacy SIOCSIF* requests. Drivers
2582 * trust SIOCSIFADDR et al to come from an already privileged
2583 * layer, and do not perform any credentials checks or input
2584 * validation.
2585 */
2586 #ifndef COMPAT_43
2587 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
2588 data,
2589 ifp, td));
2590 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2591 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2592 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2593 error = (*ifp->if_ioctl)(ifp, cmd, data);
2594 #else
2595 {
2596 u_long ocmd = cmd;
2597
2598 switch (cmd) {
2599
2600 case SIOCSIFDSTADDR:
2601 case SIOCSIFADDR:
2602 case SIOCSIFBRDADDR:
2603 case SIOCSIFNETMASK:
2604 #if BYTE_ORDER != BIG_ENDIAN
2605 if (ifr->ifr_addr.sa_family == 0 &&
2606 ifr->ifr_addr.sa_len < 16) {
2607 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
2608 ifr->ifr_addr.sa_len = 16;
2609 }
2610 #else
2611 if (ifr->ifr_addr.sa_len == 0)
2612 ifr->ifr_addr.sa_len = 16;
2613 #endif
2614 break;
2615
2616 case OSIOCGIFADDR:
2617 cmd = SIOCGIFADDR;
2618 break;
2619
2620 case OSIOCGIFDSTADDR:
2621 cmd = SIOCGIFDSTADDR;
2622 break;
2623
2624 case OSIOCGIFBRDADDR:
2625 cmd = SIOCGIFBRDADDR;
2626 break;
2627
2628 case OSIOCGIFNETMASK:
2629 cmd = SIOCGIFNETMASK;
2630 }
2631 error = ((*so->so_proto->pr_usrreqs->pru_control)(so,
2632 cmd,
2633 data,
2634 ifp, td));
2635 if (error == EOPNOTSUPP && ifp != NULL &&
2636 ifp->if_ioctl != NULL &&
2637 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2638 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2639 error = (*ifp->if_ioctl)(ifp, cmd, data);
2640 switch (ocmd) {
2641
2642 case OSIOCGIFADDR:
2643 case OSIOCGIFDSTADDR:
2644 case OSIOCGIFBRDADDR:
2645 case OSIOCGIFNETMASK:
2646 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
2647
2648 }
2649 }
2650 #endif /* COMPAT_43 */
2651
2652 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2653 #ifdef INET6
2654 if (ifp->if_flags & IFF_UP)
2655 in6_if_up(ifp);
2656 #endif
2657 }
2658 if_rele(ifp);
2659 CURVNET_RESTORE();
2660 return (error);
2661 }
2662
2663 /*
2664 * The code common to handling reference counted flags,
2665 * e.g., in ifpromisc() and if_allmulti().
2666 * The "pflag" argument can specify a permanent mode flag to check,
2667 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
2668 *
2669 * Only to be used on stack-owned flags, not driver-owned flags.
2670 */
2671 static int
2672 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
2673 {
2674 struct ifreq ifr;
2675 int error;
2676 int oldflags, oldcount;
2677
2678 /* Sanity checks to catch programming errors */
2679 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
2680 ("%s: setting driver-owned flag %d", __func__, flag));
2681
2682 if (onswitch)
2683 KASSERT(*refcount >= 0,
2684 ("%s: increment negative refcount %d for flag %d",
2685 __func__, *refcount, flag));
2686 else
2687 KASSERT(*refcount > 0,
2688 ("%s: decrement non-positive refcount %d for flag %d",
2689 __func__, *refcount, flag));
2690
2691 /* In case this mode is permanent, just touch refcount */
2692 if (ifp->if_flags & pflag) {
2693 *refcount += onswitch ? 1 : -1;
2694 return (0);
2695 }
2696
2697 /* Save ifnet parameters for if_ioctl() may fail */
2698 oldcount = *refcount;
2699 oldflags = ifp->if_flags;
2700
2701 /*
2702 * See if we aren't the only and touching refcount is enough.
2703 * Actually toggle interface flag if we are the first or last.
2704 */
2705 if (onswitch) {
2706 if ((*refcount)++)
2707 return (0);
2708 ifp->if_flags |= flag;
2709 } else {
2710 if (--(*refcount))
2711 return (0);
2712 ifp->if_flags &= ~flag;
2713 }
2714
2715 /* Call down the driver since we've changed interface flags */
2716 if (ifp->if_ioctl == NULL) {
2717 error = EOPNOTSUPP;
2718 goto recover;
2719 }
2720 ifr.ifr_flags = ifp->if_flags & 0xffff;
2721 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2722 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2723 if (error)
2724 goto recover;
2725 /* Notify userland that interface flags have changed */
2726 rt_ifmsg(ifp);
2727 return (0);
2728
2729 recover:
2730 /* Recover after driver error */
2731 *refcount = oldcount;
2732 ifp->if_flags = oldflags;
2733 return (error);
2734 }
2735
2736 /*
2737 * Set/clear promiscuous mode on interface ifp based on the truth value
2738 * of pswitch. The calls are reference counted so that only the first
2739 * "on" request actually has an effect, as does the final "off" request.
2740 * Results are undefined if the "off" and "on" requests are not matched.
2741 */
2742 int
2743 ifpromisc(struct ifnet *ifp, int pswitch)
2744 {
2745 int error;
2746 int oldflags = ifp->if_flags;
2747
2748 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
2749 &ifp->if_pcount, pswitch);
2750 /* If promiscuous mode status has changed, log a message */
2751 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC))
2752 log(LOG_INFO, "%s: promiscuous mode %s\n",
2753 ifp->if_xname,
2754 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
2755 return (error);
2756 }
2757
2758 /*
2759 * Return interface configuration
2760 * of system. List may be used
2761 * in later ioctl's (above) to get
2762 * other information.
2763 */
2764 /*ARGSUSED*/
2765 static int
2766 ifconf(u_long cmd, caddr_t data)
2767 {
2768 struct ifconf *ifc = (struct ifconf *)data;
2769 struct ifnet *ifp;
2770 struct ifaddr *ifa;
2771 struct ifreq ifr;
2772 struct sbuf *sb;
2773 int error, full = 0, valid_len, max_len;
2774
2775 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
2776 max_len = MAXPHYS - 1;
2777
2778 /* Prevent hostile input from being able to crash the system */
2779 if (ifc->ifc_len <= 0)
2780 return (EINVAL);
2781
2782 again:
2783 if (ifc->ifc_len <= max_len) {
2784 max_len = ifc->ifc_len;
2785 full = 1;
2786 }
2787 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
2788 max_len = 0;
2789 valid_len = 0;
2790
2791 IFNET_RLOCK();
2792 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2793 int addrs;
2794
2795 /*
2796 * Zero the ifr_name buffer to make sure we don't
2797 * disclose the contents of the stack.
2798 */
2799 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name));
2800
2801 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2802 >= sizeof(ifr.ifr_name)) {
2803 sbuf_delete(sb);
2804 IFNET_RUNLOCK();
2805 return (ENAMETOOLONG);
2806 }
2807
2808 addrs = 0;
2809 IF_ADDR_RLOCK(ifp);
2810 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2811 struct sockaddr *sa = ifa->ifa_addr;
2812
2813 if (prison_if(curthread->td_ucred, sa) != 0)
2814 continue;
2815 addrs++;
2816 #ifdef COMPAT_43
2817 if (cmd == OSIOCGIFCONF) {
2818 struct osockaddr *osa =
2819 (struct osockaddr *)&ifr.ifr_addr;
2820 ifr.ifr_addr = *sa;
2821 osa->sa_family = sa->sa_family;
2822 sbuf_bcat(sb, &ifr, sizeof(ifr));
2823 max_len += sizeof(ifr);
2824 } else
2825 #endif
2826 if (sa->sa_len <= sizeof(*sa)) {
2827 ifr.ifr_addr = *sa;
2828 sbuf_bcat(sb, &ifr, sizeof(ifr));
2829 max_len += sizeof(ifr);
2830 } else {
2831 sbuf_bcat(sb, &ifr,
2832 offsetof(struct ifreq, ifr_addr));
2833 max_len += offsetof(struct ifreq, ifr_addr);
2834 sbuf_bcat(sb, sa, sa->sa_len);
2835 max_len += sa->sa_len;
2836 }
2837
2838 if (sbuf_error(sb) == 0)
2839 valid_len = sbuf_len(sb);
2840 }
2841 IF_ADDR_RUNLOCK(ifp);
2842 if (addrs == 0) {
2843 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
2844 sbuf_bcat(sb, &ifr, sizeof(ifr));
2845 max_len += sizeof(ifr);
2846
2847 if (sbuf_error(sb) == 0)
2848 valid_len = sbuf_len(sb);
2849 }
2850 }
2851 IFNET_RUNLOCK();
2852
2853 /*
2854 * If we didn't allocate enough space (uncommon), try again. If
2855 * we have already allocated as much space as we are allowed,
2856 * return what we've got.
2857 */
2858 if (valid_len != max_len && !full) {
2859 sbuf_delete(sb);
2860 goto again;
2861 }
2862
2863 ifc->ifc_len = valid_len;
2864 sbuf_finish(sb);
2865 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
2866 sbuf_delete(sb);
2867 return (error);
2868 }
2869
2870 /*
2871 * Just like ifpromisc(), but for all-multicast-reception mode.
2872 */
2873 int
2874 if_allmulti(struct ifnet *ifp, int onswitch)
2875 {
2876
2877 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
2878 }
2879
2880 struct ifmultiaddr *
2881 if_findmulti(struct ifnet *ifp, struct sockaddr *sa)
2882 {
2883 struct ifmultiaddr *ifma;
2884
2885 IF_ADDR_LOCK_ASSERT(ifp);
2886
2887 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2888 if (sa->sa_family == AF_LINK) {
2889 if (sa_dl_equal(ifma->ifma_addr, sa))
2890 break;
2891 } else {
2892 if (sa_equal(ifma->ifma_addr, sa))
2893 break;
2894 }
2895 }
2896
2897 return ifma;
2898 }
2899
2900 /*
2901 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
2902 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
2903 * the ifnet multicast address list here, so the caller must do that and
2904 * other setup work (such as notifying the device driver). The reference
2905 * count is initialized to 1.
2906 */
2907 static struct ifmultiaddr *
2908 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
2909 int mflags)
2910 {
2911 struct ifmultiaddr *ifma;
2912 struct sockaddr *dupsa;
2913
2914 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
2915 M_ZERO);
2916 if (ifma == NULL)
2917 return (NULL);
2918
2919 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
2920 if (dupsa == NULL) {
2921 free(ifma, M_IFMADDR);
2922 return (NULL);
2923 }
2924 bcopy(sa, dupsa, sa->sa_len);
2925 ifma->ifma_addr = dupsa;
2926
2927 ifma->ifma_ifp = ifp;
2928 ifma->ifma_refcount = 1;
2929 ifma->ifma_protospec = NULL;
2930
2931 if (llsa == NULL) {
2932 ifma->ifma_lladdr = NULL;
2933 return (ifma);
2934 }
2935
2936 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
2937 if (dupsa == NULL) {
2938 free(ifma->ifma_addr, M_IFMADDR);
2939 free(ifma, M_IFMADDR);
2940 return (NULL);
2941 }
2942 bcopy(llsa, dupsa, llsa->sa_len);
2943 ifma->ifma_lladdr = dupsa;
2944
2945 return (ifma);
2946 }
2947
2948 /*
2949 * if_freemulti: free ifmultiaddr structure and possibly attached related
2950 * addresses. The caller is responsible for implementing reference
2951 * counting, notifying the driver, handling routing messages, and releasing
2952 * any dependent link layer state.
2953 */
2954 static void
2955 if_freemulti(struct ifmultiaddr *ifma)
2956 {
2957
2958 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
2959 ifma->ifma_refcount));
2960 KASSERT(ifma->ifma_protospec == NULL,
2961 ("if_freemulti: protospec not NULL"));
2962
2963 if (ifma->ifma_lladdr != NULL)
2964 free(ifma->ifma_lladdr, M_IFMADDR);
2965 free(ifma->ifma_addr, M_IFMADDR);
2966 free(ifma, M_IFMADDR);
2967 }
2968
2969 /*
2970 * Register an additional multicast address with a network interface.
2971 *
2972 * - If the address is already present, bump the reference count on the
2973 * address and return.
2974 * - If the address is not link-layer, look up a link layer address.
2975 * - Allocate address structures for one or both addresses, and attach to the
2976 * multicast address list on the interface. If automatically adding a link
2977 * layer address, the protocol address will own a reference to the link
2978 * layer address, to be freed when it is freed.
2979 * - Notify the network device driver of an addition to the multicast address
2980 * list.
2981 *
2982 * 'sa' points to caller-owned memory with the desired multicast address.
2983 *
2984 * 'retifma' will be used to return a pointer to the resulting multicast
2985 * address reference, if desired.
2986 */
2987 int
2988 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2989 struct ifmultiaddr **retifma)
2990 {
2991 struct ifmultiaddr *ifma, *ll_ifma;
2992 struct sockaddr *llsa;
2993 int error;
2994
2995 /*
2996 * If the address is already present, return a new reference to it;
2997 * otherwise, allocate storage and set up a new address.
2998 */
2999 IF_ADDR_WLOCK(ifp);
3000 ifma = if_findmulti(ifp, sa);
3001 if (ifma != NULL) {
3002 ifma->ifma_refcount++;
3003 if (retifma != NULL)
3004 *retifma = ifma;
3005 IF_ADDR_WUNLOCK(ifp);
3006 return (0);
3007 }
3008
3009 /*
3010 * The address isn't already present; resolve the protocol address
3011 * into a link layer address, and then look that up, bump its
3012 * refcount or allocate an ifma for that also. If 'llsa' was
3013 * returned, we will need to free it later.
3014 */
3015 llsa = NULL;
3016 ll_ifma = NULL;
3017 if (ifp->if_resolvemulti != NULL) {
3018 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3019 if (error)
3020 goto unlock_out;
3021 }
3022
3023 /*
3024 * Allocate the new address. Don't hook it up yet, as we may also
3025 * need to allocate a link layer multicast address.
3026 */
3027 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3028 if (ifma == NULL) {
3029 error = ENOMEM;
3030 goto free_llsa_out;
3031 }
3032
3033 /*
3034 * If a link layer address is found, we'll need to see if it's
3035 * already present in the address list, or allocate is as well.
3036 * When this block finishes, the link layer address will be on the
3037 * list.
3038 */
3039 if (llsa != NULL) {
3040 ll_ifma = if_findmulti(ifp, llsa);
3041 if (ll_ifma == NULL) {
3042 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3043 if (ll_ifma == NULL) {
3044 --ifma->ifma_refcount;
3045 if_freemulti(ifma);
3046 error = ENOMEM;
3047 goto free_llsa_out;
3048 }
3049 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3050 ifma_link);
3051 } else
3052 ll_ifma->ifma_refcount++;
3053 ifma->ifma_llifma = ll_ifma;
3054 }
3055
3056 /*
3057 * We now have a new multicast address, ifma, and possibly a new or
3058 * referenced link layer address. Add the primary address to the
3059 * ifnet address list.
3060 */
3061 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3062
3063 if (retifma != NULL)
3064 *retifma = ifma;
3065
3066 /*
3067 * Must generate the message while holding the lock so that 'ifma'
3068 * pointer is still valid.
3069 */
3070 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3071 IF_ADDR_WUNLOCK(ifp);
3072
3073 /*
3074 * We are certain we have added something, so call down to the
3075 * interface to let them know about it.
3076 */
3077 if (ifp->if_ioctl != NULL) {
3078 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3079 }
3080
3081 if (llsa != NULL)
3082 free(llsa, M_IFMADDR);
3083
3084 return (0);
3085
3086 free_llsa_out:
3087 if (llsa != NULL)
3088 free(llsa, M_IFMADDR);
3089
3090 unlock_out:
3091 IF_ADDR_WUNLOCK(ifp);
3092 return (error);
3093 }
3094
3095 /*
3096 * Delete a multicast group membership by network-layer group address.
3097 *
3098 * Returns ENOENT if the entry could not be found. If ifp no longer
3099 * exists, results are undefined. This entry point should only be used
3100 * from subsystems which do appropriate locking to hold ifp for the
3101 * duration of the call.
3102 * Network-layer protocol domains must use if_delmulti_ifma().
3103 */
3104 int
3105 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3106 {
3107 struct ifmultiaddr *ifma;
3108 int lastref;
3109 #ifdef INVARIANTS
3110 struct ifnet *oifp;
3111
3112 IFNET_RLOCK_NOSLEEP();
3113 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3114 if (ifp == oifp)
3115 break;
3116 if (ifp != oifp)
3117 ifp = NULL;
3118 IFNET_RUNLOCK_NOSLEEP();
3119
3120 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3121 #endif
3122 if (ifp == NULL)
3123 return (ENOENT);
3124
3125 IF_ADDR_WLOCK(ifp);
3126 lastref = 0;
3127 ifma = if_findmulti(ifp, sa);
3128 if (ifma != NULL)
3129 lastref = if_delmulti_locked(ifp, ifma, 0);
3130 IF_ADDR_WUNLOCK(ifp);
3131
3132 if (ifma == NULL)
3133 return (ENOENT);
3134
3135 if (lastref && ifp->if_ioctl != NULL) {
3136 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3137 }
3138
3139 return (0);
3140 }
3141
3142 /*
3143 * Delete all multicast group membership for an interface.
3144 * Should be used to quickly flush all multicast filters.
3145 */
3146 void
3147 if_delallmulti(struct ifnet *ifp)
3148 {
3149 struct ifmultiaddr *ifma;
3150 struct ifmultiaddr *next;
3151
3152 IF_ADDR_WLOCK(ifp);
3153 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3154 if_delmulti_locked(ifp, ifma, 0);
3155 IF_ADDR_WUNLOCK(ifp);
3156 }
3157
3158 /*
3159 * Delete a multicast group membership by group membership pointer.
3160 * Network-layer protocol domains must use this routine.
3161 *
3162 * It is safe to call this routine if the ifp disappeared.
3163 */
3164 void
3165 if_delmulti_ifma(struct ifmultiaddr *ifma)
3166 {
3167 struct ifnet *ifp;
3168 int lastref;
3169
3170 ifp = ifma->ifma_ifp;
3171 #ifdef DIAGNOSTIC
3172 if (ifp == NULL) {
3173 printf("%s: ifma_ifp seems to be detached\n", __func__);
3174 } else {
3175 struct ifnet *oifp;
3176
3177 IFNET_RLOCK_NOSLEEP();
3178 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3179 if (ifp == oifp)
3180 break;
3181 if (ifp != oifp) {
3182 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3183 ifp = NULL;
3184 }
3185 IFNET_RUNLOCK_NOSLEEP();
3186 }
3187 #endif
3188 /*
3189 * If and only if the ifnet instance exists: Acquire the address lock.
3190 */
3191 if (ifp != NULL)
3192 IF_ADDR_WLOCK(ifp);
3193
3194 lastref = if_delmulti_locked(ifp, ifma, 0);
3195
3196 if (ifp != NULL) {
3197 /*
3198 * If and only if the ifnet instance exists:
3199 * Release the address lock.
3200 * If the group was left: update the hardware hash filter.
3201 */
3202 IF_ADDR_WUNLOCK(ifp);
3203 if (lastref && ifp->if_ioctl != NULL) {
3204 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3205 }
3206 }
3207 }
3208
3209 /*
3210 * Perform deletion of network-layer and/or link-layer multicast address.
3211 *
3212 * Return 0 if the reference count was decremented.
3213 * Return 1 if the final reference was released, indicating that the
3214 * hardware hash filter should be reprogrammed.
3215 */
3216 static int
3217 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3218 {
3219 struct ifmultiaddr *ll_ifma;
3220
3221 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3222 KASSERT(ifma->ifma_ifp == ifp,
3223 ("%s: inconsistent ifp %p", __func__, ifp));
3224 IF_ADDR_WLOCK_ASSERT(ifp);
3225 }
3226
3227 ifp = ifma->ifma_ifp;
3228
3229 /*
3230 * If the ifnet is detaching, null out references to ifnet,
3231 * so that upper protocol layers will notice, and not attempt
3232 * to obtain locks for an ifnet which no longer exists. The
3233 * routing socket announcement must happen before the ifnet
3234 * instance is detached from the system.
3235 */
3236 if (detaching) {
3237 #ifdef DIAGNOSTIC
3238 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3239 #endif
3240 /*
3241 * ifp may already be nulled out if we are being reentered
3242 * to delete the ll_ifma.
3243 */
3244 if (ifp != NULL) {
3245 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3246 ifma->ifma_ifp = NULL;
3247 }
3248 }
3249
3250 if (--ifma->ifma_refcount > 0)
3251 return 0;
3252
3253 /*
3254 * If this ifma is a network-layer ifma, a link-layer ifma may
3255 * have been associated with it. Release it first if so.
3256 */
3257 ll_ifma = ifma->ifma_llifma;
3258 if (ll_ifma != NULL) {
3259 KASSERT(ifma->ifma_lladdr != NULL,
3260 ("%s: llifma w/o lladdr", __func__));
3261 if (detaching)
3262 ll_ifma->ifma_ifp = NULL; /* XXX */
3263 if (--ll_ifma->ifma_refcount == 0) {
3264 if (ifp != NULL) {
3265 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3266 ifma_link);
3267 }
3268 if_freemulti(ll_ifma);
3269 }
3270 }
3271
3272 if (ifp != NULL)
3273 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3274
3275 if_freemulti(ifma);
3276
3277 /*
3278 * The last reference to this instance of struct ifmultiaddr
3279 * was released; the hardware should be notified of this change.
3280 */
3281 return 1;
3282 }
3283
3284 /*
3285 * Set the link layer address on an interface.
3286 *
3287 * At this time we only support certain types of interfaces,
3288 * and we don't allow the length of the address to change.
3289 */
3290 int
3291 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3292 {
3293 struct sockaddr_dl *sdl;
3294 struct ifaddr *ifa;
3295 struct ifreq ifr;
3296
3297 IF_ADDR_RLOCK(ifp);
3298 ifa = ifp->if_addr;
3299 if (ifa == NULL) {
3300 IF_ADDR_RUNLOCK(ifp);
3301 return (EINVAL);
3302 }
3303 ifa_ref(ifa);
3304 IF_ADDR_RUNLOCK(ifp);
3305 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3306 if (sdl == NULL) {
3307 ifa_free(ifa);
3308 return (EINVAL);
3309 }
3310 if (len != sdl->sdl_alen) { /* don't allow length to change */
3311 ifa_free(ifa);
3312 return (EINVAL);
3313 }
3314 switch (ifp->if_type) {
3315 case IFT_ETHER:
3316 case IFT_FDDI:
3317 case IFT_XETHER:
3318 case IFT_ISO88025:
3319 case IFT_L2VLAN:
3320 case IFT_BRIDGE:
3321 case IFT_ARCNET:
3322 case IFT_IEEE8023ADLAG:
3323 case IFT_IEEE80211:
3324 bcopy(lladdr, LLADDR(sdl), len);
3325 ifa_free(ifa);
3326 break;
3327 default:
3328 ifa_free(ifa);
3329 return (ENODEV);
3330 }
3331
3332 /*
3333 * If the interface is already up, we need
3334 * to re-init it in order to reprogram its
3335 * address filter.
3336 */
3337 if ((ifp->if_flags & IFF_UP) != 0) {
3338 if (ifp->if_ioctl) {
3339 ifp->if_flags &= ~IFF_UP;
3340 ifr.ifr_flags = ifp->if_flags & 0xffff;
3341 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3342 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3343 ifp->if_flags |= IFF_UP;
3344 ifr.ifr_flags = ifp->if_flags & 0xffff;
3345 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3346 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3347 }
3348 #ifdef INET
3349 /*
3350 * Also send gratuitous ARPs to notify other nodes about
3351 * the address change.
3352 */
3353 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3354 if (ifa->ifa_addr->sa_family == AF_INET)
3355 arp_ifinit(ifp, ifa);
3356 }
3357 #endif
3358 }
3359 return (0);
3360 }
3361
3362 /*
3363 * The name argument must be a pointer to storage which will last as
3364 * long as the interface does. For physical devices, the result of
3365 * device_get_name(dev) is a good choice and for pseudo-devices a
3366 * static string works well.
3367 */
3368 void
3369 if_initname(struct ifnet *ifp, const char *name, int unit)
3370 {
3371 ifp->if_dname = name;
3372 ifp->if_dunit = unit;
3373 if (unit != IF_DUNIT_NONE)
3374 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3375 else
3376 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3377 }
3378
3379 int
3380 if_printf(struct ifnet *ifp, const char * fmt, ...)
3381 {
3382 va_list ap;
3383 int retval;
3384
3385 retval = printf("%s: ", ifp->if_xname);
3386 va_start(ap, fmt);
3387 retval += vprintf(fmt, ap);
3388 va_end(ap);
3389 return (retval);
3390 }
3391
3392 void
3393 if_start(struct ifnet *ifp)
3394 {
3395
3396 (*(ifp)->if_start)(ifp);
3397 }
3398
3399 /*
3400 * Backwards compatibility interface for drivers
3401 * that have not implemented it
3402 */
3403 static int
3404 if_transmit(struct ifnet *ifp, struct mbuf *m)
3405 {
3406 int error;
3407
3408 IFQ_HANDOFF(ifp, m, error);
3409 return (error);
3410 }
3411
3412 int
3413 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3414 {
3415 int active = 0;
3416
3417 IF_LOCK(ifq);
3418 if (_IF_QFULL(ifq)) {
3419 _IF_DROP(ifq);
3420 IF_UNLOCK(ifq);
3421 m_freem(m);
3422 return (0);
3423 }
3424 if (ifp != NULL) {
3425 ifp->if_obytes += m->m_pkthdr.len + adjust;
3426 if (m->m_flags & (M_BCAST|M_MCAST))
3427 ifp->if_omcasts++;
3428 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3429 }
3430 _IF_ENQUEUE(ifq, m);
3431 IF_UNLOCK(ifq);
3432 if (ifp != NULL && !active)
3433 (*(ifp)->if_start)(ifp);
3434 return (1);
3435 }
3436
3437 void
3438 if_register_com_alloc(u_char type,
3439 if_com_alloc_t *a, if_com_free_t *f)
3440 {
3441
3442 KASSERT(if_com_alloc[type] == NULL,
3443 ("if_register_com_alloc: %d already registered", type));
3444 KASSERT(if_com_free[type] == NULL,
3445 ("if_register_com_alloc: %d free already registered", type));
3446
3447 if_com_alloc[type] = a;
3448 if_com_free[type] = f;
3449 }
3450
3451 void
3452 if_deregister_com_alloc(u_char type)
3453 {
3454
3455 KASSERT(if_com_alloc[type] != NULL,
3456 ("if_deregister_com_alloc: %d not registered", type));
3457 KASSERT(if_com_free[type] != NULL,
3458 ("if_deregister_com_alloc: %d free not registered", type));
3459 if_com_alloc[type] = NULL;
3460 if_com_free[type] = NULL;
3461 }
Cache object: acea6e55ab6054bb0907902c0f6de6da
|