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/11.2/sys/net/if.c 333226 2018-05-03 20:05:57Z brooks $
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/sysent.h>
59 #include <sys/taskqueue.h>
60 #include <sys/domain.h>
61 #include <sys/jail.h>
62 #include <sys/priv.h>
63
64 #include <machine/stdarg.h>
65 #include <vm/uma.h>
66
67 #include <net/bpf.h>
68 #include <net/ethernet.h>
69 #include <net/if.h>
70 #include <net/if_arp.h>
71 #include <net/if_clone.h>
72 #include <net/if_dl.h>
73 #include <net/if_types.h>
74 #include <net/if_var.h>
75 #include <net/if_media.h>
76 #include <net/if_vlan_var.h>
77 #include <net/radix.h>
78 #include <net/route.h>
79 #include <net/vnet.h>
80
81 #if defined(INET) || defined(INET6)
82 #include <net/ethernet.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_carp.h>
87 #ifdef INET
88 #include <netinet/if_ether.h>
89 #endif /* INET */
90 #ifdef INET6
91 #include <netinet6/in6_var.h>
92 #include <netinet6/in6_ifattach.h>
93 #endif /* INET6 */
94 #endif /* INET || INET6 */
95
96 #include <security/mac/mac_framework.h>
97
98 #ifdef COMPAT_FREEBSD32
99 #include <sys/mount.h>
100 #include <compat/freebsd32/freebsd32.h>
101
102 struct ifreq_buffer32 {
103 uint32_t length; /* (size_t) */
104 uint32_t buffer; /* (void *) */
105 };
106
107 /*
108 * Interface request structure used for socket
109 * ioctl's. All interface ioctl's must have parameter
110 * definitions which begin with ifr_name. The
111 * remainder may be interface specific.
112 */
113 struct ifreq32 {
114 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
115 union {
116 struct sockaddr ifru_addr;
117 struct sockaddr ifru_dstaddr;
118 struct sockaddr ifru_broadaddr;
119 struct ifreq_buffer32 ifru_buffer;
120 short ifru_flags[2];
121 short ifru_index;
122 int ifru_jid;
123 int ifru_metric;
124 int ifru_mtu;
125 int ifru_phys;
126 int ifru_media;
127 uint32_t ifru_data;
128 int ifru_cap[2];
129 u_int ifru_fib;
130 u_char ifru_vlan_pcp;
131 } ifr_ifru;
132 };
133 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
134 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
135 __offsetof(struct ifreq32, ifr_ifru));
136
137 struct ifgroupreq32 {
138 char ifgr_name[IFNAMSIZ];
139 u_int ifgr_len;
140 union {
141 char ifgru_group[IFNAMSIZ];
142 uint32_t ifgru_groups;
143 } ifgr_ifgru;
144 };
145
146 struct ifmediareq32 {
147 char ifm_name[IFNAMSIZ];
148 int ifm_current;
149 int ifm_mask;
150 int ifm_status;
151 int ifm_active;
152 int ifm_count;
153 uint32_t ifm_ulist; /* (int *) */
154 };
155 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
156 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
157
158 #define _CASE_IOC_IFGROUPREQ_32(cmd) \
159 case _IOC_NEWTYPE((cmd), struct ifgroupreq32):
160 #else /* !COMPAT_FREEBSD32 */
161 #define _CASE_IOC_IFGROUPREQ_32(cmd)
162 #endif /* !COMPAT_FREEBSD32 */
163
164 #define CASE_IOC_IFGROUPREQ(cmd) \
165 _CASE_IOC_IFGROUPREQ_32(cmd) \
166 case (cmd)
167
168 union ifreq_union {
169 struct ifreq ifr;
170 #ifdef COMPAT_FREEBSD32
171 struct ifreq32 ifr32;
172 #endif
173 };
174
175 union ifgroupreq_union {
176 struct ifgroupreq ifgr;
177 #ifdef COMPAT_FREEBSD32
178 struct ifgroupreq32 ifgr32;
179 #endif
180 };
181
182 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
183 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
184
185 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
186 &ifqmaxlen, 0, "max send queue size");
187
188 /* Log link state change events */
189 static int log_link_state_change = 1;
190
191 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
192 &log_link_state_change, 0,
193 "log interface link state change events");
194
195 /* Log promiscuous mode change events */
196 static int log_promisc_mode_change = 1;
197
198 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
199 &log_promisc_mode_change, 1,
200 "log promiscuous mode change events");
201
202 /* Interface description */
203 static unsigned int ifdescr_maxlen = 1024;
204 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
205 &ifdescr_maxlen, 0,
206 "administrative maximum length for interface description");
207
208 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
209
210 /* global sx for non-critical path ifdescr */
211 static struct sx ifdescr_sx;
212 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
213
214 void (*bridge_linkstate_p)(struct ifnet *ifp);
215 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state);
216 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
217 /* These are external hooks for CARP. */
218 void (*carp_linkstate_p)(struct ifnet *ifp);
219 void (*carp_demote_adj_p)(int, char *);
220 int (*carp_master_p)(struct ifaddr *);
221 #if defined(INET) || defined(INET6)
222 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
223 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
224 const struct sockaddr *sa);
225 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *);
226 int (*carp_attach_p)(struct ifaddr *, int);
227 void (*carp_detach_p)(struct ifaddr *);
228 #endif
229 #ifdef INET
230 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
231 #endif
232 #ifdef INET6
233 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
234 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
235 const struct in6_addr *taddr);
236 #endif
237
238 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
239
240 /*
241 * XXX: Style; these should be sorted alphabetically, and unprototyped
242 * static functions should be prototyped. Currently they are sorted by
243 * declaration order.
244 */
245 static void if_attachdomain(void *);
246 static void if_attachdomain1(struct ifnet *);
247 static int ifconf(u_long, caddr_t);
248 static void if_freemulti(struct ifmultiaddr *);
249 static void if_grow(void);
250 static void if_input_default(struct ifnet *, struct mbuf *);
251 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
252 static void if_route(struct ifnet *, int flag, int fam);
253 static int if_setflag(struct ifnet *, int, int, int *, int);
254 static int if_transmit(struct ifnet *ifp, struct mbuf *m);
255 static void if_unroute(struct ifnet *, int flag, int fam);
256 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
257 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
258 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
259 static void do_link_state_change(void *, int);
260 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
261 static int if_getgroupmembers(struct ifgroupreq *);
262 static void if_delgroups(struct ifnet *);
263 static void if_attach_internal(struct ifnet *, int, struct if_clone *);
264 static int if_detach_internal(struct ifnet *, int, struct if_clone **);
265 #ifdef VIMAGE
266 static void if_vmove(struct ifnet *, struct vnet *);
267 #endif
268
269 #ifdef INET6
270 /*
271 * XXX: declare here to avoid to include many inet6 related files..
272 * should be more generalized?
273 */
274 extern void nd6_setmtu(struct ifnet *);
275 #endif
276
277 /* ipsec helper hooks */
278 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
279 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
280
281 VNET_DEFINE(int, if_index);
282 int ifqmaxlen = IFQ_MAXLEN;
283 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
284 VNET_DEFINE(struct ifgrouphead, ifg_head);
285
286 static VNET_DEFINE(int, if_indexlim) = 8;
287
288 /* Table of ifnet by index. */
289 VNET_DEFINE(struct ifnet **, ifindex_table);
290
291 #define V_if_indexlim VNET(if_indexlim)
292 #define V_ifindex_table VNET(ifindex_table)
293
294 /*
295 * The global network interface list (V_ifnet) and related state (such as
296 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and
297 * an rwlock. Either may be acquired shared to stablize the list, but both
298 * must be acquired writable to modify the list. This model allows us to
299 * both stablize the interface list during interrupt thread processing, but
300 * also to stablize it over long-running ioctls, without introducing priority
301 * inversions and deadlocks.
302 */
303 struct rwlock ifnet_rwlock;
304 RW_SYSINIT_FLAGS(ifnet_rw, &ifnet_rwlock, "ifnet_rw", RW_RECURSE);
305 struct sx ifnet_sxlock;
306 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
307
308 /*
309 * The allocation of network interfaces is a rather non-atomic affair; we
310 * need to select an index before we are ready to expose the interface for
311 * use, so will use this pointer value to indicate reservation.
312 */
313 #define IFNET_HOLD (void *)(uintptr_t)(-1)
314
315 static if_com_alloc_t *if_com_alloc[256];
316 static if_com_free_t *if_com_free[256];
317
318 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
319 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
320 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
321
322 struct ifnet *
323 ifnet_byindex_locked(u_short idx)
324 {
325
326 if (idx > V_if_index)
327 return (NULL);
328 if (V_ifindex_table[idx] == IFNET_HOLD)
329 return (NULL);
330 return (V_ifindex_table[idx]);
331 }
332
333 struct ifnet *
334 ifnet_byindex(u_short idx)
335 {
336 struct ifnet *ifp;
337
338 IFNET_RLOCK_NOSLEEP();
339 ifp = ifnet_byindex_locked(idx);
340 IFNET_RUNLOCK_NOSLEEP();
341 return (ifp);
342 }
343
344 struct ifnet *
345 ifnet_byindex_ref(u_short idx)
346 {
347 struct ifnet *ifp;
348
349 IFNET_RLOCK_NOSLEEP();
350 ifp = ifnet_byindex_locked(idx);
351 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) {
352 IFNET_RUNLOCK_NOSLEEP();
353 return (NULL);
354 }
355 if_ref(ifp);
356 IFNET_RUNLOCK_NOSLEEP();
357 return (ifp);
358 }
359
360 /*
361 * Allocate an ifindex array entry; return 0 on success or an error on
362 * failure.
363 */
364 static u_short
365 ifindex_alloc(void)
366 {
367 u_short idx;
368
369 IFNET_WLOCK_ASSERT();
370 retry:
371 /*
372 * Try to find an empty slot below V_if_index. If we fail, take the
373 * next slot.
374 */
375 for (idx = 1; idx <= V_if_index; idx++) {
376 if (V_ifindex_table[idx] == NULL)
377 break;
378 }
379
380 /* Catch if_index overflow. */
381 if (idx >= V_if_indexlim) {
382 if_grow();
383 goto retry;
384 }
385 if (idx > V_if_index)
386 V_if_index = idx;
387 return (idx);
388 }
389
390 static void
391 ifindex_free_locked(u_short idx)
392 {
393
394 IFNET_WLOCK_ASSERT();
395
396 V_ifindex_table[idx] = NULL;
397 while (V_if_index > 0 &&
398 V_ifindex_table[V_if_index] == NULL)
399 V_if_index--;
400 }
401
402 static void
403 ifindex_free(u_short idx)
404 {
405
406 IFNET_WLOCK();
407 ifindex_free_locked(idx);
408 IFNET_WUNLOCK();
409 }
410
411 static void
412 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp)
413 {
414
415 IFNET_WLOCK_ASSERT();
416
417 V_ifindex_table[idx] = ifp;
418 }
419
420 static void
421 ifnet_setbyindex(u_short idx, struct ifnet *ifp)
422 {
423
424 IFNET_WLOCK();
425 ifnet_setbyindex_locked(idx, ifp);
426 IFNET_WUNLOCK();
427 }
428
429 struct ifaddr *
430 ifaddr_byindex(u_short idx)
431 {
432 struct ifnet *ifp;
433 struct ifaddr *ifa = NULL;
434
435 IFNET_RLOCK_NOSLEEP();
436 ifp = ifnet_byindex_locked(idx);
437 if (ifp != NULL && (ifa = ifp->if_addr) != NULL)
438 ifa_ref(ifa);
439 IFNET_RUNLOCK_NOSLEEP();
440 return (ifa);
441 }
442
443 /*
444 * Network interface utility routines.
445 *
446 * Routines with ifa_ifwith* names take sockaddr *'s as
447 * parameters.
448 */
449
450 static void
451 vnet_if_init(const void *unused __unused)
452 {
453
454 TAILQ_INIT(&V_ifnet);
455 TAILQ_INIT(&V_ifg_head);
456 IFNET_WLOCK();
457 if_grow(); /* create initial table */
458 IFNET_WUNLOCK();
459 vnet_if_clone_init();
460 }
461 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
462 NULL);
463
464 #ifdef VIMAGE
465 static void
466 vnet_if_uninit(const void *unused __unused)
467 {
468
469 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p "
470 "not empty", __func__, __LINE__, &V_ifnet));
471 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p "
472 "not empty", __func__, __LINE__, &V_ifg_head));
473
474 free((caddr_t)V_ifindex_table, M_IFNET);
475 }
476 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST,
477 vnet_if_uninit, NULL);
478
479 static void
480 vnet_if_return(const void *unused __unused)
481 {
482 struct ifnet *ifp, *nifp;
483
484 /* Return all inherited interfaces to their parent vnets. */
485 TAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
486 if (ifp->if_home_vnet != ifp->if_vnet)
487 if_vmove(ifp, ifp->if_home_vnet);
488 }
489 }
490 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
491 vnet_if_return, NULL);
492 #endif
493
494 static void
495 if_grow(void)
496 {
497 int oldlim;
498 u_int n;
499 struct ifnet **e;
500
501 IFNET_WLOCK_ASSERT();
502 oldlim = V_if_indexlim;
503 IFNET_WUNLOCK();
504 n = (oldlim << 1) * sizeof(*e);
505 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO);
506 IFNET_WLOCK();
507 if (V_if_indexlim != oldlim) {
508 free(e, M_IFNET);
509 return;
510 }
511 if (V_ifindex_table != NULL) {
512 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2);
513 free((caddr_t)V_ifindex_table, M_IFNET);
514 }
515 V_if_indexlim <<= 1;
516 V_ifindex_table = e;
517 }
518
519 /*
520 * Allocate a struct ifnet and an index for an interface. A layer 2
521 * common structure will also be allocated if an allocation routine is
522 * registered for the passed type.
523 */
524 struct ifnet *
525 if_alloc(u_char type)
526 {
527 struct ifnet *ifp;
528 u_short idx;
529
530 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO);
531 IFNET_WLOCK();
532 idx = ifindex_alloc();
533 ifnet_setbyindex_locked(idx, IFNET_HOLD);
534 IFNET_WUNLOCK();
535 ifp->if_index = idx;
536 ifp->if_type = type;
537 ifp->if_alloctype = type;
538 #ifdef VIMAGE
539 ifp->if_vnet = curvnet;
540 #endif
541 if (if_com_alloc[type] != NULL) {
542 ifp->if_l2com = if_com_alloc[type](type, ifp);
543 if (ifp->if_l2com == NULL) {
544 free(ifp, M_IFNET);
545 ifindex_free(idx);
546 return (NULL);
547 }
548 }
549
550 IF_ADDR_LOCK_INIT(ifp);
551 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
552 ifp->if_afdata_initialized = 0;
553 IF_AFDATA_LOCK_INIT(ifp);
554 TAILQ_INIT(&ifp->if_addrhead);
555 TAILQ_INIT(&ifp->if_multiaddrs);
556 TAILQ_INIT(&ifp->if_groups);
557 #ifdef MAC
558 mac_ifnet_init(ifp);
559 #endif
560 ifq_init(&ifp->if_snd, ifp);
561
562 refcount_init(&ifp->if_refcount, 1); /* Index reference. */
563 for (int i = 0; i < IFCOUNTERS; i++)
564 ifp->if_counters[i] = counter_u64_alloc(M_WAITOK);
565 ifp->if_get_counter = if_get_counter_default;
566 ifp->if_pcp = IFNET_PCP_NONE;
567 ifnet_setbyindex(ifp->if_index, ifp);
568 return (ifp);
569 }
570
571 /*
572 * Do the actual work of freeing a struct ifnet, and layer 2 common
573 * structure. This call is made when the last reference to an
574 * interface is released.
575 */
576 static void
577 if_free_internal(struct ifnet *ifp)
578 {
579
580 KASSERT((ifp->if_flags & IFF_DYING),
581 ("if_free_internal: interface not dying"));
582
583 if (if_com_free[ifp->if_alloctype] != NULL)
584 if_com_free[ifp->if_alloctype](ifp->if_l2com,
585 ifp->if_alloctype);
586
587 #ifdef MAC
588 mac_ifnet_destroy(ifp);
589 #endif /* MAC */
590 if (ifp->if_description != NULL)
591 free(ifp->if_description, M_IFDESCR);
592 IF_AFDATA_DESTROY(ifp);
593 IF_ADDR_LOCK_DESTROY(ifp);
594 ifq_delete(&ifp->if_snd);
595
596 for (int i = 0; i < IFCOUNTERS; i++)
597 counter_u64_free(ifp->if_counters[i]);
598
599 free(ifp, M_IFNET);
600 }
601
602 /*
603 * Deregister an interface and free the associated storage.
604 */
605 void
606 if_free(struct ifnet *ifp)
607 {
608
609 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
610
611 CURVNET_SET_QUIET(ifp->if_vnet);
612 IFNET_WLOCK();
613 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index),
614 ("%s: freeing unallocated ifnet", ifp->if_xname));
615
616 ifindex_free_locked(ifp->if_index);
617 IFNET_WUNLOCK();
618
619 if (refcount_release(&ifp->if_refcount))
620 if_free_internal(ifp);
621 CURVNET_RESTORE();
622 }
623
624 /*
625 * Interfaces to keep an ifnet type-stable despite the possibility of the
626 * driver calling if_free(). If there are additional references, we defer
627 * freeing the underlying data structure.
628 */
629 void
630 if_ref(struct ifnet *ifp)
631 {
632
633 /* We don't assert the ifnet list lock here, but arguably should. */
634 refcount_acquire(&ifp->if_refcount);
635 }
636
637 void
638 if_rele(struct ifnet *ifp)
639 {
640
641 if (!refcount_release(&ifp->if_refcount))
642 return;
643 if_free_internal(ifp);
644 }
645
646 void
647 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
648 {
649
650 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
651
652 if (ifq->ifq_maxlen == 0)
653 ifq->ifq_maxlen = ifqmaxlen;
654
655 ifq->altq_type = 0;
656 ifq->altq_disc = NULL;
657 ifq->altq_flags &= ALTQF_CANTCHANGE;
658 ifq->altq_tbr = NULL;
659 ifq->altq_ifp = ifp;
660 }
661
662 void
663 ifq_delete(struct ifaltq *ifq)
664 {
665 mtx_destroy(&ifq->ifq_mtx);
666 }
667
668 /*
669 * Perform generic interface initialization tasks and attach the interface
670 * to the list of "active" interfaces. If vmove flag is set on entry
671 * to if_attach_internal(), perform only a limited subset of initialization
672 * tasks, given that we are moving from one vnet to another an ifnet which
673 * has already been fully initialized.
674 *
675 * Note that if_detach_internal() removes group membership unconditionally
676 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
677 * Thus, when if_vmove() is applied to a cloned interface, group membership
678 * is lost while a cloned one always joins a group whose name is
679 * ifc->ifc_name. To recover this after if_detach_internal() and
680 * if_attach_internal(), the cloner should be specified to
681 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
682 * attempts to join a group whose name is ifc->ifc_name.
683 *
684 * XXX:
685 * - The decision to return void and thus require this function to
686 * succeed is questionable.
687 * - We should probably do more sanity checking. For instance we don't
688 * do anything to insure if_xname is unique or non-empty.
689 */
690 void
691 if_attach(struct ifnet *ifp)
692 {
693
694 if_attach_internal(ifp, 0, NULL);
695 }
696
697 /*
698 * Compute the least common TSO limit.
699 */
700 void
701 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
702 {
703 /*
704 * 1) If there is no limit currently, take the limit from
705 * the network adapter.
706 *
707 * 2) If the network adapter has a limit below the current
708 * limit, apply it.
709 */
710 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
711 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
712 pmax->tsomaxbytes = ifp->if_hw_tsomax;
713 }
714 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
715 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
716 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
717 }
718 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
719 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
720 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
721 }
722 }
723
724 /*
725 * Update TSO limit of a network adapter.
726 *
727 * Returns zero if no change. Else non-zero.
728 */
729 int
730 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
731 {
732 int retval = 0;
733 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
734 ifp->if_hw_tsomax = pmax->tsomaxbytes;
735 retval++;
736 }
737 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
738 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
739 retval++;
740 }
741 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
742 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
743 retval++;
744 }
745 return (retval);
746 }
747
748 static void
749 if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc)
750 {
751 unsigned socksize, ifasize;
752 int namelen, masklen;
753 struct sockaddr_dl *sdl;
754 struct ifaddr *ifa;
755
756 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index))
757 panic ("%s: BUG: if_attach called without if_alloc'd input()\n",
758 ifp->if_xname);
759
760 #ifdef VIMAGE
761 ifp->if_vnet = curvnet;
762 if (ifp->if_home_vnet == NULL)
763 ifp->if_home_vnet = curvnet;
764 #endif
765
766 if_addgroup(ifp, IFG_ALL);
767
768 /* Restore group membership for cloned interfaces. */
769 if (vmove && ifc != NULL)
770 if_clone_addgroup(ifp, ifc);
771
772 getmicrotime(&ifp->if_lastchange);
773 ifp->if_epoch = time_uptime;
774
775 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
776 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
777 ("transmit and qflush must both either be set or both be NULL"));
778 if (ifp->if_transmit == NULL) {
779 ifp->if_transmit = if_transmit;
780 ifp->if_qflush = if_qflush;
781 }
782 if (ifp->if_input == NULL)
783 ifp->if_input = if_input_default;
784
785 if (ifp->if_requestencap == NULL)
786 ifp->if_requestencap = if_requestencap_default;
787
788 if (!vmove) {
789 #ifdef MAC
790 mac_ifnet_create(ifp);
791 #endif
792
793 /*
794 * Create a Link Level name for this device.
795 */
796 namelen = strlen(ifp->if_xname);
797 /*
798 * Always save enough space for any possiable name so we
799 * can do a rename in place later.
800 */
801 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
802 socksize = masklen + ifp->if_addrlen;
803 if (socksize < sizeof(*sdl))
804 socksize = sizeof(*sdl);
805 socksize = roundup2(socksize, sizeof(long));
806 ifasize = sizeof(*ifa) + 2 * socksize;
807 ifa = ifa_alloc(ifasize, M_WAITOK);
808 sdl = (struct sockaddr_dl *)(ifa + 1);
809 sdl->sdl_len = socksize;
810 sdl->sdl_family = AF_LINK;
811 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
812 sdl->sdl_nlen = namelen;
813 sdl->sdl_index = ifp->if_index;
814 sdl->sdl_type = ifp->if_type;
815 ifp->if_addr = ifa;
816 ifa->ifa_ifp = ifp;
817 ifa->ifa_rtrequest = link_rtrequest;
818 ifa->ifa_addr = (struct sockaddr *)sdl;
819 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
820 ifa->ifa_netmask = (struct sockaddr *)sdl;
821 sdl->sdl_len = masklen;
822 while (namelen != 0)
823 sdl->sdl_data[--namelen] = 0xff;
824 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
825 /* Reliably crash if used uninitialized. */
826 ifp->if_broadcastaddr = NULL;
827
828 if (ifp->if_type == IFT_ETHER) {
829 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
830 M_WAITOK | M_ZERO);
831 }
832
833 #if defined(INET) || defined(INET6)
834 /* Use defaults for TSO, if nothing is set */
835 if (ifp->if_hw_tsomax == 0 &&
836 ifp->if_hw_tsomaxsegcount == 0 &&
837 ifp->if_hw_tsomaxsegsize == 0) {
838 /*
839 * The TSO defaults needs to be such that an
840 * NFS mbuf list of 35 mbufs totalling just
841 * below 64K works and that a chain of mbufs
842 * can be defragged into at most 32 segments:
843 */
844 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
845 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
846 ifp->if_hw_tsomaxsegcount = 35;
847 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
848
849 /* XXX some drivers set IFCAP_TSO after ethernet attach */
850 if (ifp->if_capabilities & IFCAP_TSO) {
851 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
852 ifp->if_hw_tsomax,
853 ifp->if_hw_tsomaxsegcount,
854 ifp->if_hw_tsomaxsegsize);
855 }
856 }
857 #endif
858 }
859 #ifdef VIMAGE
860 else {
861 /*
862 * Update the interface index in the link layer address
863 * of the interface.
864 */
865 for (ifa = ifp->if_addr; ifa != NULL;
866 ifa = TAILQ_NEXT(ifa, ifa_link)) {
867 if (ifa->ifa_addr->sa_family == AF_LINK) {
868 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
869 sdl->sdl_index = ifp->if_index;
870 }
871 }
872 }
873 #endif
874
875 IFNET_WLOCK();
876 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
877 #ifdef VIMAGE
878 curvnet->vnet_ifcnt++;
879 #endif
880 IFNET_WUNLOCK();
881
882 if (domain_init_status >= 2)
883 if_attachdomain1(ifp);
884
885 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
886 if (IS_DEFAULT_VNET(curvnet))
887 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
888
889 /* Announce the interface. */
890 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
891 }
892
893 static void
894 if_attachdomain(void *dummy)
895 {
896 struct ifnet *ifp;
897
898 TAILQ_FOREACH(ifp, &V_ifnet, if_link)
899 if_attachdomain1(ifp);
900 }
901 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND,
902 if_attachdomain, NULL);
903
904 static void
905 if_attachdomain1(struct ifnet *ifp)
906 {
907 struct domain *dp;
908
909 /*
910 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
911 * cannot lock ifp->if_afdata initialization, entirely.
912 */
913 IF_AFDATA_LOCK(ifp);
914 if (ifp->if_afdata_initialized >= domain_init_status) {
915 IF_AFDATA_UNLOCK(ifp);
916 log(LOG_WARNING, "%s called more than once on %s\n",
917 __func__, ifp->if_xname);
918 return;
919 }
920 ifp->if_afdata_initialized = domain_init_status;
921 IF_AFDATA_UNLOCK(ifp);
922
923 /* address family dependent data region */
924 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
925 for (dp = domains; dp; dp = dp->dom_next) {
926 if (dp->dom_ifattach)
927 ifp->if_afdata[dp->dom_family] =
928 (*dp->dom_ifattach)(ifp);
929 }
930 }
931
932 /*
933 * Remove any unicast or broadcast network addresses from an interface.
934 */
935 void
936 if_purgeaddrs(struct ifnet *ifp)
937 {
938 struct ifaddr *ifa, *next;
939
940 /* XXX cannot hold IF_ADDR_WLOCK over called functions. */
941 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
942 if (ifa->ifa_addr->sa_family == AF_LINK)
943 continue;
944 #ifdef INET
945 /* XXX: Ugly!! ad hoc just for INET */
946 if (ifa->ifa_addr->sa_family == AF_INET) {
947 struct ifaliasreq ifr;
948
949 bzero(&ifr, sizeof(ifr));
950 ifr.ifra_addr = *ifa->ifa_addr;
951 if (ifa->ifa_dstaddr)
952 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
953 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
954 NULL) == 0)
955 continue;
956 }
957 #endif /* INET */
958 #ifdef INET6
959 if (ifa->ifa_addr->sa_family == AF_INET6) {
960 in6_purgeaddr(ifa);
961 /* ifp_addrhead is already updated */
962 continue;
963 }
964 #endif /* INET6 */
965 IF_ADDR_WLOCK(ifp);
966 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
967 IF_ADDR_WUNLOCK(ifp);
968 ifa_free(ifa);
969 }
970 }
971
972 /*
973 * Remove any multicast network addresses from an interface when an ifnet
974 * is going away.
975 */
976 static void
977 if_purgemaddrs(struct ifnet *ifp)
978 {
979 struct ifmultiaddr *ifma;
980 struct ifmultiaddr *next;
981
982 IF_ADDR_WLOCK(ifp);
983 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
984 if_delmulti_locked(ifp, ifma, 1);
985 IF_ADDR_WUNLOCK(ifp);
986 }
987
988 /*
989 * Detach an interface, removing it from the list of "active" interfaces.
990 * If vmove flag is set on entry to if_detach_internal(), perform only a
991 * limited subset of cleanup tasks, given that we are moving an ifnet from
992 * one vnet to another, where it must be fully operational.
993 *
994 * XXXRW: There are some significant questions about event ordering, and
995 * how to prevent things from starting to use the interface during detach.
996 */
997 void
998 if_detach(struct ifnet *ifp)
999 {
1000
1001 CURVNET_SET_QUIET(ifp->if_vnet);
1002 if_detach_internal(ifp, 0, NULL);
1003 CURVNET_RESTORE();
1004 }
1005
1006 /*
1007 * The vmove flag, if set, indicates that we are called from a callpath
1008 * that is moving an interface to a different vnet instance.
1009 *
1010 * The shutdown flag, if set, indicates that we are called in the
1011 * process of shutting down a vnet instance. Currently only the
1012 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1013 * on a vnet instance shutdown without this flag being set, e.g., when
1014 * the cloned interfaces are destoyed as first thing of teardown.
1015 */
1016 static int
1017 if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp)
1018 {
1019 struct ifaddr *ifa;
1020 int i;
1021 struct domain *dp;
1022 struct ifnet *iter;
1023 int found = 0;
1024 #ifdef VIMAGE
1025 int shutdown;
1026
1027 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1028 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1029 #endif
1030 IFNET_WLOCK();
1031 TAILQ_FOREACH(iter, &V_ifnet, if_link)
1032 if (iter == ifp) {
1033 TAILQ_REMOVE(&V_ifnet, ifp, if_link);
1034 found = 1;
1035 break;
1036 }
1037 IFNET_WUNLOCK();
1038 if (!found) {
1039 /*
1040 * While we would want to panic here, we cannot
1041 * guarantee that the interface is indeed still on
1042 * the list given we don't hold locks all the way.
1043 */
1044 return (ENOENT);
1045 #if 0
1046 if (vmove)
1047 panic("%s: ifp=%p not on the ifnet tailq %p",
1048 __func__, ifp, &V_ifnet);
1049 else
1050 return; /* XXX this should panic as well? */
1051 #endif
1052 }
1053
1054 /*
1055 * At this point we know the interface still was on the ifnet list
1056 * and we removed it so we are in a stable state.
1057 */
1058 #ifdef VIMAGE
1059 curvnet->vnet_ifcnt--;
1060 #endif
1061
1062 /*
1063 * In any case (destroy or vmove) detach us from the groups
1064 * and remove/wait for pending events on the taskq.
1065 * XXX-BZ in theory an interface could still enqueue a taskq change?
1066 */
1067 if_delgroups(ifp);
1068
1069 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1070
1071 /*
1072 * Check if this is a cloned interface or not. Must do even if
1073 * shutting down as a if_vmove_reclaim() would move the ifp and
1074 * the if_clone_addgroup() will have a corrupted string overwise
1075 * from a gibberish pointer.
1076 */
1077 if (vmove && ifcp != NULL)
1078 *ifcp = if_clone_findifc(ifp);
1079
1080 if_down(ifp);
1081
1082 #ifdef VIMAGE
1083 /*
1084 * On VNET shutdown abort here as the stack teardown will do all
1085 * the work top-down for us.
1086 */
1087 if (shutdown) {
1088 /*
1089 * In case of a vmove we are done here without error.
1090 * If we would signal an error it would lead to the same
1091 * abort as if we did not find the ifnet anymore.
1092 * if_detach() calls us in void context and does not care
1093 * about an early abort notification, so life is splendid :)
1094 */
1095 goto finish_vnet_shutdown;
1096 }
1097 #endif
1098
1099 /*
1100 * At this point we are not tearing down a VNET and are either
1101 * going to destroy or vmove the interface and have to cleanup
1102 * accordingly.
1103 */
1104
1105 /*
1106 * Remove routes and flush queues.
1107 */
1108 #ifdef ALTQ
1109 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1110 altq_disable(&ifp->if_snd);
1111 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1112 altq_detach(&ifp->if_snd);
1113 #endif
1114
1115 if_purgeaddrs(ifp);
1116
1117 #ifdef INET
1118 in_ifdetach(ifp);
1119 #endif
1120
1121 #ifdef INET6
1122 /*
1123 * Remove all IPv6 kernel structs related to ifp. This should be done
1124 * before removing routing entries below, since IPv6 interface direct
1125 * routes are expected to be removed by the IPv6-specific kernel API.
1126 * Otherwise, the kernel will detect some inconsistency and bark it.
1127 */
1128 in6_ifdetach(ifp);
1129 #endif
1130 if_purgemaddrs(ifp);
1131
1132 /* Announce that the interface is gone. */
1133 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1134 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1135 if (IS_DEFAULT_VNET(curvnet))
1136 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1137
1138 if (!vmove) {
1139 /*
1140 * Prevent further calls into the device driver via ifnet.
1141 */
1142 if_dead(ifp);
1143
1144 /*
1145 * Remove link ifaddr pointer and maybe decrement if_index.
1146 * Clean up all addresses.
1147 */
1148 free(ifp->if_hw_addr, M_IFADDR);
1149 ifp->if_hw_addr = NULL;
1150 ifp->if_addr = NULL;
1151
1152 /* We can now free link ifaddr. */
1153 IF_ADDR_WLOCK(ifp);
1154 if (!TAILQ_EMPTY(&ifp->if_addrhead)) {
1155 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1156 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
1157 IF_ADDR_WUNLOCK(ifp);
1158 ifa_free(ifa);
1159 } else
1160 IF_ADDR_WUNLOCK(ifp);
1161 }
1162
1163 rt_flushifroutes(ifp);
1164
1165 #ifdef VIMAGE
1166 finish_vnet_shutdown:
1167 #endif
1168 /*
1169 * We cannot hold the lock over dom_ifdetach calls as they might
1170 * sleep, for example trying to drain a callout, thus open up the
1171 * theoretical race with re-attaching.
1172 */
1173 IF_AFDATA_LOCK(ifp);
1174 i = ifp->if_afdata_initialized;
1175 ifp->if_afdata_initialized = 0;
1176 IF_AFDATA_UNLOCK(ifp);
1177 for (dp = domains; i > 0 && dp; dp = dp->dom_next) {
1178 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) {
1179 (*dp->dom_ifdetach)(ifp,
1180 ifp->if_afdata[dp->dom_family]);
1181 ifp->if_afdata[dp->dom_family] = NULL;
1182 }
1183 }
1184
1185 return (0);
1186 }
1187
1188 #ifdef VIMAGE
1189 /*
1190 * if_vmove() performs a limited version of if_detach() in current
1191 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1192 * An attempt is made to shrink if_index in current vnet, find an
1193 * unused if_index in target vnet and calls if_grow() if necessary,
1194 * and finally find an unused if_xname for the target vnet.
1195 */
1196 static void
1197 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1198 {
1199 struct if_clone *ifc;
1200 u_int bif_dlt, bif_hdrlen;
1201 int rc;
1202
1203 /*
1204 * if_detach_internal() will call the eventhandler to notify
1205 * interface departure. That will detach if_bpf. We need to
1206 * safe the dlt and hdrlen so we can re-attach it later.
1207 */
1208 bpf_get_bp_params(ifp->if_bpf, &bif_dlt, &bif_hdrlen);
1209
1210 /*
1211 * Detach from current vnet, but preserve LLADDR info, do not
1212 * mark as dead etc. so that the ifnet can be reattached later.
1213 * If we cannot find it, we lost the race to someone else.
1214 */
1215 rc = if_detach_internal(ifp, 1, &ifc);
1216 if (rc != 0)
1217 return;
1218
1219 /*
1220 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink
1221 * the if_index for that vnet if possible.
1222 *
1223 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized,
1224 * or we'd lock on one vnet and unlock on another.
1225 */
1226 IFNET_WLOCK();
1227 ifindex_free_locked(ifp->if_index);
1228 IFNET_WUNLOCK();
1229
1230 /*
1231 * Perform interface-specific reassignment tasks, if provided by
1232 * the driver.
1233 */
1234 if (ifp->if_reassign != NULL)
1235 ifp->if_reassign(ifp, new_vnet, NULL);
1236
1237 /*
1238 * Switch to the context of the target vnet.
1239 */
1240 CURVNET_SET_QUIET(new_vnet);
1241
1242 IFNET_WLOCK();
1243 ifp->if_index = ifindex_alloc();
1244 ifnet_setbyindex_locked(ifp->if_index, ifp);
1245 IFNET_WUNLOCK();
1246
1247 if_attach_internal(ifp, 1, ifc);
1248
1249 if (ifp->if_bpf == NULL)
1250 bpfattach(ifp, bif_dlt, bif_hdrlen);
1251
1252 CURVNET_RESTORE();
1253 }
1254
1255 /*
1256 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1257 */
1258 static int
1259 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1260 {
1261 struct prison *pr;
1262 struct ifnet *difp;
1263 int shutdown;
1264
1265 /* Try to find the prison within our visibility. */
1266 sx_slock(&allprison_lock);
1267 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1268 sx_sunlock(&allprison_lock);
1269 if (pr == NULL)
1270 return (ENXIO);
1271 prison_hold_locked(pr);
1272 mtx_unlock(&pr->pr_mtx);
1273
1274 /* Do not try to move the iface from and to the same prison. */
1275 if (pr->pr_vnet == ifp->if_vnet) {
1276 prison_free(pr);
1277 return (EEXIST);
1278 }
1279
1280 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1281 /* XXX Lock interfaces to avoid races. */
1282 CURVNET_SET_QUIET(pr->pr_vnet);
1283 difp = ifunit(ifname);
1284 if (difp != NULL) {
1285 CURVNET_RESTORE();
1286 prison_free(pr);
1287 return (EEXIST);
1288 }
1289
1290 /* Make sure the VNET is stable. */
1291 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1292 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1293 if (shutdown) {
1294 CURVNET_RESTORE();
1295 prison_free(pr);
1296 return (EBUSY);
1297 }
1298 CURVNET_RESTORE();
1299
1300 /* Move the interface into the child jail/vnet. */
1301 if_vmove(ifp, pr->pr_vnet);
1302
1303 /* Report the new if_xname back to the userland. */
1304 sprintf(ifname, "%s", ifp->if_xname);
1305
1306 prison_free(pr);
1307 return (0);
1308 }
1309
1310 static int
1311 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1312 {
1313 struct prison *pr;
1314 struct vnet *vnet_dst;
1315 struct ifnet *ifp;
1316 int shutdown;
1317
1318 /* Try to find the prison within our visibility. */
1319 sx_slock(&allprison_lock);
1320 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1321 sx_sunlock(&allprison_lock);
1322 if (pr == NULL)
1323 return (ENXIO);
1324 prison_hold_locked(pr);
1325 mtx_unlock(&pr->pr_mtx);
1326
1327 /* Make sure the named iface exists in the source prison/vnet. */
1328 CURVNET_SET(pr->pr_vnet);
1329 ifp = ifunit(ifname); /* XXX Lock to avoid races. */
1330 if (ifp == NULL) {
1331 CURVNET_RESTORE();
1332 prison_free(pr);
1333 return (ENXIO);
1334 }
1335
1336 /* Do not try to move the iface from and to the same prison. */
1337 vnet_dst = TD_TO_VNET(td);
1338 if (vnet_dst == ifp->if_vnet) {
1339 CURVNET_RESTORE();
1340 prison_free(pr);
1341 return (EEXIST);
1342 }
1343
1344 /* Make sure the VNET is stable. */
1345 shutdown = (ifp->if_vnet->vnet_state > SI_SUB_VNET &&
1346 ifp->if_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
1347 if (shutdown) {
1348 CURVNET_RESTORE();
1349 prison_free(pr);
1350 return (EBUSY);
1351 }
1352
1353 /* Get interface back from child jail/vnet. */
1354 if_vmove(ifp, vnet_dst);
1355 CURVNET_RESTORE();
1356
1357 /* Report the new if_xname back to the userland. */
1358 sprintf(ifname, "%s", ifp->if_xname);
1359
1360 prison_free(pr);
1361 return (0);
1362 }
1363 #endif /* VIMAGE */
1364
1365 /*
1366 * Add a group to an interface
1367 */
1368 int
1369 if_addgroup(struct ifnet *ifp, const char *groupname)
1370 {
1371 struct ifg_list *ifgl;
1372 struct ifg_group *ifg = NULL;
1373 struct ifg_member *ifgm;
1374 int new = 0;
1375
1376 if (groupname[0] && groupname[strlen(groupname) - 1] >= '' &&
1377 groupname[strlen(groupname) - 1] <= '9')
1378 return (EINVAL);
1379
1380 IFNET_WLOCK();
1381 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1382 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1383 IFNET_WUNLOCK();
1384 return (EEXIST);
1385 }
1386
1387 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP,
1388 M_NOWAIT)) == NULL) {
1389 IFNET_WUNLOCK();
1390 return (ENOMEM);
1391 }
1392
1393 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member),
1394 M_TEMP, M_NOWAIT)) == NULL) {
1395 free(ifgl, M_TEMP);
1396 IFNET_WUNLOCK();
1397 return (ENOMEM);
1398 }
1399
1400 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1401 if (!strcmp(ifg->ifg_group, groupname))
1402 break;
1403
1404 if (ifg == NULL) {
1405 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group),
1406 M_TEMP, M_NOWAIT)) == NULL) {
1407 free(ifgl, M_TEMP);
1408 free(ifgm, M_TEMP);
1409 IFNET_WUNLOCK();
1410 return (ENOMEM);
1411 }
1412 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1413 ifg->ifg_refcnt = 0;
1414 TAILQ_INIT(&ifg->ifg_members);
1415 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1416 new = 1;
1417 }
1418
1419 ifg->ifg_refcnt++;
1420 ifgl->ifgl_group = ifg;
1421 ifgm->ifgm_ifp = ifp;
1422
1423 IF_ADDR_WLOCK(ifp);
1424 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1425 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1426 IF_ADDR_WUNLOCK(ifp);
1427
1428 IFNET_WUNLOCK();
1429
1430 if (new)
1431 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1432 EVENTHANDLER_INVOKE(group_change_event, groupname);
1433
1434 return (0);
1435 }
1436
1437 /*
1438 * Remove a group from an interface
1439 */
1440 int
1441 if_delgroup(struct ifnet *ifp, const char *groupname)
1442 {
1443 struct ifg_list *ifgl;
1444 struct ifg_member *ifgm;
1445
1446 IFNET_WLOCK();
1447 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1448 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1449 break;
1450 if (ifgl == NULL) {
1451 IFNET_WUNLOCK();
1452 return (ENOENT);
1453 }
1454
1455 IF_ADDR_WLOCK(ifp);
1456 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1457 IF_ADDR_WUNLOCK(ifp);
1458
1459 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1460 if (ifgm->ifgm_ifp == ifp)
1461 break;
1462
1463 if (ifgm != NULL) {
1464 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1465 free(ifgm, M_TEMP);
1466 }
1467
1468 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1469 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1470 IFNET_WUNLOCK();
1471 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1472 free(ifgl->ifgl_group, M_TEMP);
1473 } else
1474 IFNET_WUNLOCK();
1475
1476 free(ifgl, M_TEMP);
1477
1478 EVENTHANDLER_INVOKE(group_change_event, groupname);
1479
1480 return (0);
1481 }
1482
1483 /*
1484 * Remove an interface from all groups
1485 */
1486 static void
1487 if_delgroups(struct ifnet *ifp)
1488 {
1489 struct ifg_list *ifgl;
1490 struct ifg_member *ifgm;
1491 char groupname[IFNAMSIZ];
1492
1493 IFNET_WLOCK();
1494 while (!TAILQ_EMPTY(&ifp->if_groups)) {
1495 ifgl = TAILQ_FIRST(&ifp->if_groups);
1496
1497 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1498
1499 IF_ADDR_WLOCK(ifp);
1500 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1501 IF_ADDR_WUNLOCK(ifp);
1502
1503 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1504 if (ifgm->ifgm_ifp == ifp)
1505 break;
1506
1507 if (ifgm != NULL) {
1508 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1509 ifgm_next);
1510 free(ifgm, M_TEMP);
1511 }
1512
1513 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1514 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next);
1515 IFNET_WUNLOCK();
1516 EVENTHANDLER_INVOKE(group_detach_event,
1517 ifgl->ifgl_group);
1518 free(ifgl->ifgl_group, M_TEMP);
1519 } else
1520 IFNET_WUNLOCK();
1521
1522 free(ifgl, M_TEMP);
1523
1524 EVENTHANDLER_INVOKE(group_change_event, groupname);
1525
1526 IFNET_WLOCK();
1527 }
1528 IFNET_WUNLOCK();
1529 }
1530
1531 static char *
1532 ifgr_group_get(void *ifgrp)
1533 {
1534 union ifgroupreq_union *ifgrup;
1535
1536 ifgrup = ifgrp;
1537 #ifdef COMPAT_FREEBSD32
1538 if (SV_CURPROC_FLAG(SV_ILP32))
1539 return (&ifgrup->ifgr32.ifgr_ifgru.ifgru_group[0]);
1540 #endif
1541 return (&ifgrup->ifgr.ifgr_ifgru.ifgru_group[0]);
1542 }
1543
1544 static struct ifg_req *
1545 ifgr_groups_get(void *ifgrp)
1546 {
1547 union ifgroupreq_union *ifgrup;
1548
1549 ifgrup = ifgrp;
1550 #ifdef COMPAT_FREEBSD32
1551 if (SV_CURPROC_FLAG(SV_ILP32))
1552 return ((struct ifg_req *)(uintptr_t)
1553 ifgrup->ifgr32.ifgr_ifgru.ifgru_groups);
1554 #endif
1555 return (ifgrup->ifgr.ifgr_ifgru.ifgru_groups);
1556 }
1557
1558 /*
1559 * Stores all groups from an interface in memory pointed to by ifgr.
1560 */
1561 static int
1562 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1563 {
1564 int len, error;
1565 struct ifg_list *ifgl;
1566 struct ifg_req ifgrq, *ifgp;
1567
1568 if (ifgr->ifgr_len == 0) {
1569 IF_ADDR_RLOCK(ifp);
1570 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1571 ifgr->ifgr_len += sizeof(struct ifg_req);
1572 IF_ADDR_RUNLOCK(ifp);
1573 return (0);
1574 }
1575
1576 len = ifgr->ifgr_len;
1577 ifgp = ifgr_groups_get(ifgr);
1578 /* XXX: wire */
1579 IF_ADDR_RLOCK(ifp);
1580 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1581 if (len < sizeof(ifgrq)) {
1582 IF_ADDR_RUNLOCK(ifp);
1583 return (EINVAL);
1584 }
1585 bzero(&ifgrq, sizeof ifgrq);
1586 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1587 sizeof(ifgrq.ifgrq_group));
1588 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1589 IF_ADDR_RUNLOCK(ifp);
1590 return (error);
1591 }
1592 len -= sizeof(ifgrq);
1593 ifgp++;
1594 }
1595 IF_ADDR_RUNLOCK(ifp);
1596
1597 return (0);
1598 }
1599
1600 /*
1601 * Stores all members of a group in memory pointed to by igfr
1602 */
1603 static int
1604 if_getgroupmembers(struct ifgroupreq *ifgr)
1605 {
1606 struct ifg_group *ifg;
1607 struct ifg_member *ifgm;
1608 struct ifg_req ifgrq, *ifgp;
1609 int len, error;
1610
1611 IFNET_RLOCK();
1612 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1613 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1614 break;
1615 if (ifg == NULL) {
1616 IFNET_RUNLOCK();
1617 return (ENOENT);
1618 }
1619
1620 if (ifgr->ifgr_len == 0) {
1621 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1622 ifgr->ifgr_len += sizeof(ifgrq);
1623 IFNET_RUNLOCK();
1624 return (0);
1625 }
1626
1627 len = ifgr->ifgr_len;
1628 ifgp = ifgr_groups_get(ifgr);
1629 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1630 if (len < sizeof(ifgrq)) {
1631 IFNET_RUNLOCK();
1632 return (EINVAL);
1633 }
1634 bzero(&ifgrq, sizeof ifgrq);
1635 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1636 sizeof(ifgrq.ifgrq_member));
1637 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1638 IFNET_RUNLOCK();
1639 return (error);
1640 }
1641 len -= sizeof(ifgrq);
1642 ifgp++;
1643 }
1644 IFNET_RUNLOCK();
1645
1646 return (0);
1647 }
1648
1649 /*
1650 * Return counter values from counter(9)s stored in ifnet.
1651 */
1652 uint64_t
1653 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1654 {
1655
1656 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1657
1658 return (counter_u64_fetch(ifp->if_counters[cnt]));
1659 }
1660
1661 /*
1662 * Increase an ifnet counter. Usually used for counters shared
1663 * between the stack and a driver, but function supports them all.
1664 */
1665 void
1666 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1667 {
1668
1669 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1670
1671 counter_u64_add(ifp->if_counters[cnt], inc);
1672 }
1673
1674 /*
1675 * Copy data from ifnet to userland API structure if_data.
1676 */
1677 void
1678 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1679 {
1680
1681 ifd->ifi_type = ifp->if_type;
1682 ifd->ifi_physical = 0;
1683 ifd->ifi_addrlen = ifp->if_addrlen;
1684 ifd->ifi_hdrlen = ifp->if_hdrlen;
1685 ifd->ifi_link_state = ifp->if_link_state;
1686 ifd->ifi_vhid = 0;
1687 ifd->ifi_datalen = sizeof(struct if_data);
1688 ifd->ifi_mtu = ifp->if_mtu;
1689 ifd->ifi_metric = ifp->if_metric;
1690 ifd->ifi_baudrate = ifp->if_baudrate;
1691 ifd->ifi_hwassist = ifp->if_hwassist;
1692 ifd->ifi_epoch = ifp->if_epoch;
1693 ifd->ifi_lastchange = ifp->if_lastchange;
1694
1695 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1696 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1697 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1698 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1699 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1700 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1701 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1702 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1703 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1704 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1705 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1706 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1707 }
1708
1709 /*
1710 * Wrapper functions for struct ifnet address list locking macros. These are
1711 * used by kernel modules to avoid encoding programming interface or binary
1712 * interface assumptions that may be violated when kernel-internal locking
1713 * approaches change.
1714 */
1715 void
1716 if_addr_rlock(struct ifnet *ifp)
1717 {
1718
1719 IF_ADDR_RLOCK(ifp);
1720 }
1721
1722 void
1723 if_addr_runlock(struct ifnet *ifp)
1724 {
1725
1726 IF_ADDR_RUNLOCK(ifp);
1727 }
1728
1729 void
1730 if_maddr_rlock(if_t ifp)
1731 {
1732
1733 IF_ADDR_RLOCK((struct ifnet *)ifp);
1734 }
1735
1736 void
1737 if_maddr_runlock(if_t ifp)
1738 {
1739
1740 IF_ADDR_RUNLOCK((struct ifnet *)ifp);
1741 }
1742
1743 /*
1744 * Initialization, destruction and refcounting functions for ifaddrs.
1745 */
1746 struct ifaddr *
1747 ifa_alloc(size_t size, int flags)
1748 {
1749 struct ifaddr *ifa;
1750
1751 KASSERT(size >= sizeof(struct ifaddr),
1752 ("%s: invalid size %zu", __func__, size));
1753
1754 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1755 if (ifa == NULL)
1756 return (NULL);
1757
1758 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1759 goto fail;
1760 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1761 goto fail;
1762 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1763 goto fail;
1764 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1765 goto fail;
1766
1767 refcount_init(&ifa->ifa_refcnt, 1);
1768
1769 return (ifa);
1770
1771 fail:
1772 /* free(NULL) is okay */
1773 counter_u64_free(ifa->ifa_opackets);
1774 counter_u64_free(ifa->ifa_ipackets);
1775 counter_u64_free(ifa->ifa_obytes);
1776 counter_u64_free(ifa->ifa_ibytes);
1777 free(ifa, M_IFADDR);
1778
1779 return (NULL);
1780 }
1781
1782 void
1783 ifa_ref(struct ifaddr *ifa)
1784 {
1785
1786 refcount_acquire(&ifa->ifa_refcnt);
1787 }
1788
1789 void
1790 ifa_free(struct ifaddr *ifa)
1791 {
1792
1793 if (refcount_release(&ifa->ifa_refcnt)) {
1794 counter_u64_free(ifa->ifa_opackets);
1795 counter_u64_free(ifa->ifa_ipackets);
1796 counter_u64_free(ifa->ifa_obytes);
1797 counter_u64_free(ifa->ifa_ibytes);
1798 free(ifa, M_IFADDR);
1799 }
1800 }
1801
1802 static int
1803 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
1804 struct sockaddr *ia)
1805 {
1806 int error;
1807 struct rt_addrinfo info;
1808 struct sockaddr_dl null_sdl;
1809 struct ifnet *ifp;
1810
1811 ifp = ifa->ifa_ifp;
1812
1813 bzero(&info, sizeof(info));
1814 if (cmd != RTM_DELETE)
1815 info.rti_ifp = V_loif;
1816 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
1817 info.rti_info[RTAX_DST] = ia;
1818 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1819 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
1820
1821 error = rtrequest1_fib(cmd, &info, NULL, ifp->if_fib);
1822
1823 if (error != 0)
1824 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
1825 __func__, otype, if_name(ifp), error);
1826
1827 return (error);
1828 }
1829
1830 int
1831 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1832 {
1833
1834 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
1835 }
1836
1837 int
1838 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1839 {
1840
1841 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
1842 }
1843
1844 int
1845 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
1846 {
1847
1848 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
1849 }
1850
1851 /*
1852 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1853 * structs used to represent other address families, it is necessary
1854 * to perform a different comparison.
1855 */
1856
1857 #define sa_dl_equal(a1, a2) \
1858 ((((const struct sockaddr_dl *)(a1))->sdl_len == \
1859 ((const struct sockaddr_dl *)(a2))->sdl_len) && \
1860 (bcmp(CLLADDR((const struct sockaddr_dl *)(a1)), \
1861 CLLADDR((const struct sockaddr_dl *)(a2)), \
1862 ((const struct sockaddr_dl *)(a1))->sdl_alen) == 0))
1863
1864 /*
1865 * Locate an interface based on a complete address.
1866 */
1867 /*ARGSUSED*/
1868 static struct ifaddr *
1869 ifa_ifwithaddr_internal(const struct sockaddr *addr, int getref)
1870 {
1871 struct ifnet *ifp;
1872 struct ifaddr *ifa;
1873
1874 IFNET_RLOCK_NOSLEEP();
1875 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1876 IF_ADDR_RLOCK(ifp);
1877 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1878 if (ifa->ifa_addr->sa_family != addr->sa_family)
1879 continue;
1880 if (sa_equal(addr, ifa->ifa_addr)) {
1881 if (getref)
1882 ifa_ref(ifa);
1883 IF_ADDR_RUNLOCK(ifp);
1884 goto done;
1885 }
1886 /* IP6 doesn't have broadcast */
1887 if ((ifp->if_flags & IFF_BROADCAST) &&
1888 ifa->ifa_broadaddr &&
1889 ifa->ifa_broadaddr->sa_len != 0 &&
1890 sa_equal(ifa->ifa_broadaddr, addr)) {
1891 if (getref)
1892 ifa_ref(ifa);
1893 IF_ADDR_RUNLOCK(ifp);
1894 goto done;
1895 }
1896 }
1897 IF_ADDR_RUNLOCK(ifp);
1898 }
1899 ifa = NULL;
1900 done:
1901 IFNET_RUNLOCK_NOSLEEP();
1902 return (ifa);
1903 }
1904
1905 struct ifaddr *
1906 ifa_ifwithaddr(const struct sockaddr *addr)
1907 {
1908
1909 return (ifa_ifwithaddr_internal(addr, 1));
1910 }
1911
1912 int
1913 ifa_ifwithaddr_check(const struct sockaddr *addr)
1914 {
1915
1916 return (ifa_ifwithaddr_internal(addr, 0) != NULL);
1917 }
1918
1919 /*
1920 * Locate an interface based on the broadcast address.
1921 */
1922 /* ARGSUSED */
1923 struct ifaddr *
1924 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1925 {
1926 struct ifnet *ifp;
1927 struct ifaddr *ifa;
1928
1929 IFNET_RLOCK_NOSLEEP();
1930 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1931 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1932 continue;
1933 IF_ADDR_RLOCK(ifp);
1934 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1935 if (ifa->ifa_addr->sa_family != addr->sa_family)
1936 continue;
1937 if ((ifp->if_flags & IFF_BROADCAST) &&
1938 ifa->ifa_broadaddr &&
1939 ifa->ifa_broadaddr->sa_len != 0 &&
1940 sa_equal(ifa->ifa_broadaddr, addr)) {
1941 ifa_ref(ifa);
1942 IF_ADDR_RUNLOCK(ifp);
1943 goto done;
1944 }
1945 }
1946 IF_ADDR_RUNLOCK(ifp);
1947 }
1948 ifa = NULL;
1949 done:
1950 IFNET_RUNLOCK_NOSLEEP();
1951 return (ifa);
1952 }
1953
1954 /*
1955 * Locate the point to point interface with a given destination address.
1956 */
1957 /*ARGSUSED*/
1958 struct ifaddr *
1959 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1960 {
1961 struct ifnet *ifp;
1962 struct ifaddr *ifa;
1963
1964 IFNET_RLOCK_NOSLEEP();
1965 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1966 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1967 continue;
1968 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1969 continue;
1970 IF_ADDR_RLOCK(ifp);
1971 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1972 if (ifa->ifa_addr->sa_family != addr->sa_family)
1973 continue;
1974 if (ifa->ifa_dstaddr != NULL &&
1975 sa_equal(addr, ifa->ifa_dstaddr)) {
1976 ifa_ref(ifa);
1977 IF_ADDR_RUNLOCK(ifp);
1978 goto done;
1979 }
1980 }
1981 IF_ADDR_RUNLOCK(ifp);
1982 }
1983 ifa = NULL;
1984 done:
1985 IFNET_RUNLOCK_NOSLEEP();
1986 return (ifa);
1987 }
1988
1989 /*
1990 * Find an interface on a specific network. If many, choice
1991 * is most specific found.
1992 */
1993 struct ifaddr *
1994 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1995 {
1996 struct ifnet *ifp;
1997 struct ifaddr *ifa;
1998 struct ifaddr *ifa_maybe = NULL;
1999 u_int af = addr->sa_family;
2000 const char *addr_data = addr->sa_data, *cplim;
2001
2002 /*
2003 * AF_LINK addresses can be looked up directly by their index number,
2004 * so do that if we can.
2005 */
2006 if (af == AF_LINK) {
2007 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr;
2008 if (sdl->sdl_index && sdl->sdl_index <= V_if_index)
2009 return (ifaddr_byindex(sdl->sdl_index));
2010 }
2011
2012 /*
2013 * Scan though each interface, looking for ones that have addresses
2014 * in this address family and the requested fib. Maintain a reference
2015 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that
2016 * kept it stable when we move onto the next interface.
2017 */
2018 IFNET_RLOCK_NOSLEEP();
2019 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2020 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
2021 continue;
2022 IF_ADDR_RLOCK(ifp);
2023 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2024 const char *cp, *cp2, *cp3;
2025
2026 if (ifa->ifa_addr->sa_family != af)
2027 next: continue;
2028 if (af == AF_INET &&
2029 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
2030 /*
2031 * This is a bit broken as it doesn't
2032 * take into account that the remote end may
2033 * be a single node in the network we are
2034 * looking for.
2035 * The trouble is that we don't know the
2036 * netmask for the remote end.
2037 */
2038 if (ifa->ifa_dstaddr != NULL &&
2039 sa_equal(addr, ifa->ifa_dstaddr)) {
2040 ifa_ref(ifa);
2041 IF_ADDR_RUNLOCK(ifp);
2042 goto done;
2043 }
2044 } else {
2045 /*
2046 * Scan all the bits in the ifa's address.
2047 * If a bit dissagrees with what we are
2048 * looking for, mask it with the netmask
2049 * to see if it really matters.
2050 * (A byte at a time)
2051 */
2052 if (ifa->ifa_netmask == 0)
2053 continue;
2054 cp = addr_data;
2055 cp2 = ifa->ifa_addr->sa_data;
2056 cp3 = ifa->ifa_netmask->sa_data;
2057 cplim = ifa->ifa_netmask->sa_len
2058 + (char *)ifa->ifa_netmask;
2059 while (cp3 < cplim)
2060 if ((*cp++ ^ *cp2++) & *cp3++)
2061 goto next; /* next address! */
2062 /*
2063 * If the netmask of what we just found
2064 * is more specific than what we had before
2065 * (if we had one), or if the virtual status
2066 * of new prefix is better than of the old one,
2067 * then remember the new one before continuing
2068 * to search for an even better one.
2069 */
2070 if (ifa_maybe == NULL ||
2071 ifa_preferred(ifa_maybe, ifa) ||
2072 rn_refines((caddr_t)ifa->ifa_netmask,
2073 (caddr_t)ifa_maybe->ifa_netmask)) {
2074 if (ifa_maybe != NULL)
2075 ifa_free(ifa_maybe);
2076 ifa_maybe = ifa;
2077 ifa_ref(ifa_maybe);
2078 }
2079 }
2080 }
2081 IF_ADDR_RUNLOCK(ifp);
2082 }
2083 ifa = ifa_maybe;
2084 ifa_maybe = NULL;
2085 done:
2086 IFNET_RUNLOCK_NOSLEEP();
2087 if (ifa_maybe != NULL)
2088 ifa_free(ifa_maybe);
2089 return (ifa);
2090 }
2091
2092 /*
2093 * Find an interface address specific to an interface best matching
2094 * a given address.
2095 */
2096 struct ifaddr *
2097 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2098 {
2099 struct ifaddr *ifa;
2100 const char *cp, *cp2, *cp3;
2101 char *cplim;
2102 struct ifaddr *ifa_maybe = NULL;
2103 u_int af = addr->sa_family;
2104
2105 if (af >= AF_MAX)
2106 return (NULL);
2107 IF_ADDR_RLOCK(ifp);
2108 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2109 if (ifa->ifa_addr->sa_family != af)
2110 continue;
2111 if (ifa_maybe == NULL)
2112 ifa_maybe = ifa;
2113 if (ifa->ifa_netmask == 0) {
2114 if (sa_equal(addr, ifa->ifa_addr) ||
2115 (ifa->ifa_dstaddr &&
2116 sa_equal(addr, ifa->ifa_dstaddr)))
2117 goto done;
2118 continue;
2119 }
2120 if (ifp->if_flags & IFF_POINTOPOINT) {
2121 if (sa_equal(addr, ifa->ifa_dstaddr))
2122 goto done;
2123 } else {
2124 cp = addr->sa_data;
2125 cp2 = ifa->ifa_addr->sa_data;
2126 cp3 = ifa->ifa_netmask->sa_data;
2127 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2128 for (; cp3 < cplim; cp3++)
2129 if ((*cp++ ^ *cp2++) & *cp3)
2130 break;
2131 if (cp3 == cplim)
2132 goto done;
2133 }
2134 }
2135 ifa = ifa_maybe;
2136 done:
2137 if (ifa != NULL)
2138 ifa_ref(ifa);
2139 IF_ADDR_RUNLOCK(ifp);
2140 return (ifa);
2141 }
2142
2143 /*
2144 * See whether new ifa is better than current one:
2145 * 1) A non-virtual one is preferred over virtual.
2146 * 2) A virtual in master state preferred over any other state.
2147 *
2148 * Used in several address selecting functions.
2149 */
2150 int
2151 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
2152 {
2153
2154 return (cur->ifa_carp && (!next->ifa_carp ||
2155 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
2156 }
2157
2158 #include <net/if_llatbl.h>
2159
2160 /*
2161 * Default action when installing a route with a Link Level gateway.
2162 * Lookup an appropriate real ifa to point to.
2163 * This should be moved to /sys/net/link.c eventually.
2164 */
2165 static void
2166 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
2167 {
2168 struct ifaddr *ifa, *oifa;
2169 struct sockaddr *dst;
2170 struct ifnet *ifp;
2171
2172 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
2173 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
2174 return;
2175 ifa = ifaof_ifpforaddr(dst, ifp);
2176 if (ifa) {
2177 oifa = rt->rt_ifa;
2178 rt->rt_ifa = ifa;
2179 ifa_free(oifa);
2180 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2181 ifa->ifa_rtrequest(cmd, rt, info);
2182 }
2183 }
2184
2185 struct sockaddr_dl *
2186 link_alloc_sdl(size_t size, int flags)
2187 {
2188
2189 return (malloc(size, M_TEMP, flags));
2190 }
2191
2192 void
2193 link_free_sdl(struct sockaddr *sa)
2194 {
2195 free(sa, M_TEMP);
2196 }
2197
2198 /*
2199 * Fills in given sdl with interface basic info.
2200 * Returns pointer to filled sdl.
2201 */
2202 struct sockaddr_dl *
2203 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
2204 {
2205 struct sockaddr_dl *sdl;
2206
2207 sdl = (struct sockaddr_dl *)paddr;
2208 memset(sdl, 0, sizeof(struct sockaddr_dl));
2209 sdl->sdl_len = sizeof(struct sockaddr_dl);
2210 sdl->sdl_family = AF_LINK;
2211 sdl->sdl_index = ifp->if_index;
2212 sdl->sdl_type = iftype;
2213
2214 return (sdl);
2215 }
2216
2217 /*
2218 * Mark an interface down and notify protocols of
2219 * the transition.
2220 */
2221 static void
2222 if_unroute(struct ifnet *ifp, int flag, int fam)
2223 {
2224 struct ifaddr *ifa;
2225
2226 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP"));
2227
2228 ifp->if_flags &= ~flag;
2229 getmicrotime(&ifp->if_lastchange);
2230 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2231 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2232 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2233 ifp->if_qflush(ifp);
2234
2235 if (ifp->if_carp)
2236 (*carp_linkstate_p)(ifp);
2237 rt_ifmsg(ifp);
2238 }
2239
2240 /*
2241 * Mark an interface up and notify protocols of
2242 * the transition.
2243 */
2244 static void
2245 if_route(struct ifnet *ifp, int flag, int fam)
2246 {
2247 struct ifaddr *ifa;
2248
2249 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP"));
2250
2251 ifp->if_flags |= flag;
2252 getmicrotime(&ifp->if_lastchange);
2253 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
2254 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
2255 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2256 if (ifp->if_carp)
2257 (*carp_linkstate_p)(ifp);
2258 rt_ifmsg(ifp);
2259 #ifdef INET6
2260 in6_if_up(ifp);
2261 #endif
2262 }
2263
2264 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
2265 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
2266 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
2267 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
2268 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
2269 int (*vlan_setcookie_p)(struct ifnet *, void *);
2270 void *(*vlan_cookie_p)(struct ifnet *);
2271
2272 /*
2273 * Handle a change in the interface link state. To avoid LORs
2274 * between driver lock and upper layer locks, as well as possible
2275 * recursions, we post event to taskqueue, and all job
2276 * is done in static do_link_state_change().
2277 */
2278 void
2279 if_link_state_change(struct ifnet *ifp, int link_state)
2280 {
2281 /* Return if state hasn't changed. */
2282 if (ifp->if_link_state == link_state)
2283 return;
2284
2285 ifp->if_link_state = link_state;
2286
2287 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2288 }
2289
2290 static void
2291 do_link_state_change(void *arg, int pending)
2292 {
2293 struct ifnet *ifp = (struct ifnet *)arg;
2294 int link_state = ifp->if_link_state;
2295 CURVNET_SET(ifp->if_vnet);
2296
2297 /* Notify that the link state has changed. */
2298 rt_ifmsg(ifp);
2299 if (ifp->if_vlantrunk != NULL)
2300 (*vlan_link_state_p)(ifp);
2301
2302 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) &&
2303 ifp->if_l2com != NULL)
2304 (*ng_ether_link_state_p)(ifp, link_state);
2305 if (ifp->if_carp)
2306 (*carp_linkstate_p)(ifp);
2307 if (ifp->if_bridge)
2308 (*bridge_linkstate_p)(ifp);
2309 if (ifp->if_lagg)
2310 (*lagg_linkstate_p)(ifp, link_state);
2311
2312 if (IS_DEFAULT_VNET(curvnet))
2313 devctl_notify("IFNET", ifp->if_xname,
2314 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2315 NULL);
2316 if (pending > 1)
2317 if_printf(ifp, "%d link states coalesced\n", pending);
2318 if (log_link_state_change)
2319 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname,
2320 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2321 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2322 CURVNET_RESTORE();
2323 }
2324
2325 /*
2326 * Mark an interface down and notify protocols of
2327 * the transition.
2328 */
2329 void
2330 if_down(struct ifnet *ifp)
2331 {
2332
2333 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2334 if_unroute(ifp, IFF_UP, AF_UNSPEC);
2335 }
2336
2337 /*
2338 * Mark an interface up and notify protocols of
2339 * the transition.
2340 */
2341 void
2342 if_up(struct ifnet *ifp)
2343 {
2344
2345 if_route(ifp, IFF_UP, AF_UNSPEC);
2346 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2347 }
2348
2349 /*
2350 * Flush an interface queue.
2351 */
2352 void
2353 if_qflush(struct ifnet *ifp)
2354 {
2355 struct mbuf *m, *n;
2356 struct ifaltq *ifq;
2357
2358 ifq = &ifp->if_snd;
2359 IFQ_LOCK(ifq);
2360 #ifdef ALTQ
2361 if (ALTQ_IS_ENABLED(ifq))
2362 ALTQ_PURGE(ifq);
2363 #endif
2364 n = ifq->ifq_head;
2365 while ((m = n) != NULL) {
2366 n = m->m_nextpkt;
2367 m_freem(m);
2368 }
2369 ifq->ifq_head = 0;
2370 ifq->ifq_tail = 0;
2371 ifq->ifq_len = 0;
2372 IFQ_UNLOCK(ifq);
2373 }
2374
2375 /*
2376 * Map interface name to interface structure pointer, with or without
2377 * returning a reference.
2378 */
2379 struct ifnet *
2380 ifunit_ref(const char *name)
2381 {
2382 struct ifnet *ifp;
2383
2384 IFNET_RLOCK_NOSLEEP();
2385 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2386 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2387 !(ifp->if_flags & IFF_DYING))
2388 break;
2389 }
2390 if (ifp != NULL)
2391 if_ref(ifp);
2392 IFNET_RUNLOCK_NOSLEEP();
2393 return (ifp);
2394 }
2395
2396 struct ifnet *
2397 ifunit(const char *name)
2398 {
2399 struct ifnet *ifp;
2400
2401 IFNET_RLOCK_NOSLEEP();
2402 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2403 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
2404 break;
2405 }
2406 IFNET_RUNLOCK_NOSLEEP();
2407 return (ifp);
2408 }
2409
2410 static void *
2411 ifr_buffer_get_buffer(void *data)
2412 {
2413 union ifreq_union *ifrup;
2414
2415 ifrup = data;
2416 #ifdef COMPAT_FREEBSD32
2417 if (SV_CURPROC_FLAG(SV_ILP32))
2418 return ((void *)(uintptr_t)
2419 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2420 #endif
2421 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2422 }
2423
2424 static void
2425 ifr_buffer_set_buffer_null(void *data)
2426 {
2427 union ifreq_union *ifrup;
2428
2429 ifrup = data;
2430 #ifdef COMPAT_FREEBSD32
2431 if (SV_CURPROC_FLAG(SV_ILP32))
2432 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2433 else
2434 #endif
2435 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2436 }
2437
2438 static size_t
2439 ifr_buffer_get_length(void *data)
2440 {
2441 union ifreq_union *ifrup;
2442
2443 ifrup = data;
2444 #ifdef COMPAT_FREEBSD32
2445 if (SV_CURPROC_FLAG(SV_ILP32))
2446 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2447 #endif
2448 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2449 }
2450
2451 static void
2452 ifr_buffer_set_length(void *data, size_t len)
2453 {
2454 union ifreq_union *ifrup;
2455
2456 ifrup = data;
2457 #ifdef COMPAT_FREEBSD32
2458 if (SV_CURPROC_FLAG(SV_ILP32))
2459 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2460 else
2461 #endif
2462 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2463 }
2464
2465 void *
2466 ifr_data_get_ptr(void *ifrp)
2467 {
2468 union ifreq_union *ifrup;
2469
2470 ifrup = ifrp;
2471 #ifdef COMPAT_FREEBSD32
2472 if (SV_CURPROC_FLAG(SV_ILP32))
2473 return ((void *)(uintptr_t)
2474 ifrup->ifr32.ifr_ifru.ifru_data);
2475 #endif
2476 return (ifrup->ifr.ifr_ifru.ifru_data);
2477 }
2478
2479 /*
2480 * Hardware specific interface ioctls.
2481 */
2482 static int
2483 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2484 {
2485 struct ifreq *ifr;
2486 int error = 0, do_ifup = 0;
2487 int new_flags, temp_flags;
2488 size_t namelen, onamelen;
2489 size_t descrlen;
2490 char *descrbuf, *odescrbuf;
2491 char new_name[IFNAMSIZ];
2492 struct ifaddr *ifa;
2493 struct sockaddr_dl *sdl;
2494
2495 ifr = (struct ifreq *)data;
2496 switch (cmd) {
2497 case SIOCGIFINDEX:
2498 ifr->ifr_index = ifp->if_index;
2499 break;
2500
2501 case SIOCGIFFLAGS:
2502 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2503 ifr->ifr_flags = temp_flags & 0xffff;
2504 ifr->ifr_flagshigh = temp_flags >> 16;
2505 break;
2506
2507 case SIOCGIFCAP:
2508 ifr->ifr_reqcap = ifp->if_capabilities;
2509 ifr->ifr_curcap = ifp->if_capenable;
2510 break;
2511
2512 #ifdef MAC
2513 case SIOCGIFMAC:
2514 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2515 break;
2516 #endif
2517
2518 case SIOCGIFMETRIC:
2519 ifr->ifr_metric = ifp->if_metric;
2520 break;
2521
2522 case SIOCGIFMTU:
2523 ifr->ifr_mtu = ifp->if_mtu;
2524 break;
2525
2526 case SIOCGIFPHYS:
2527 /* XXXGL: did this ever worked? */
2528 ifr->ifr_phys = 0;
2529 break;
2530
2531 case SIOCGIFDESCR:
2532 error = 0;
2533 sx_slock(&ifdescr_sx);
2534 if (ifp->if_description == NULL)
2535 error = ENOMSG;
2536 else {
2537 /* space for terminating nul */
2538 descrlen = strlen(ifp->if_description) + 1;
2539 if (ifr_buffer_get_length(ifr) < descrlen)
2540 ifr_buffer_set_buffer_null(ifr);
2541 else
2542 error = copyout(ifp->if_description,
2543 ifr_buffer_get_buffer(ifr), descrlen);
2544 ifr_buffer_set_length(ifr, descrlen);
2545 }
2546 sx_sunlock(&ifdescr_sx);
2547 break;
2548
2549 case SIOCSIFDESCR:
2550 error = priv_check(td, PRIV_NET_SETIFDESCR);
2551 if (error)
2552 return (error);
2553
2554 /*
2555 * Copy only (length-1) bytes to make sure that
2556 * if_description is always nul terminated. The
2557 * length parameter is supposed to count the
2558 * terminating nul in.
2559 */
2560 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2561 return (ENAMETOOLONG);
2562 else if (ifr_buffer_get_length(ifr) == 0)
2563 descrbuf = NULL;
2564 else {
2565 descrbuf = malloc(ifr_buffer_get_length(ifr),
2566 M_IFDESCR, M_WAITOK | M_ZERO);
2567 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2568 ifr_buffer_get_length(ifr) - 1);
2569 if (error) {
2570 free(descrbuf, M_IFDESCR);
2571 break;
2572 }
2573 }
2574
2575 sx_xlock(&ifdescr_sx);
2576 odescrbuf = ifp->if_description;
2577 ifp->if_description = descrbuf;
2578 sx_xunlock(&ifdescr_sx);
2579
2580 getmicrotime(&ifp->if_lastchange);
2581 free(odescrbuf, M_IFDESCR);
2582 break;
2583
2584 case SIOCGIFFIB:
2585 ifr->ifr_fib = ifp->if_fib;
2586 break;
2587
2588 case SIOCSIFFIB:
2589 error = priv_check(td, PRIV_NET_SETIFFIB);
2590 if (error)
2591 return (error);
2592 if (ifr->ifr_fib >= rt_numfibs)
2593 return (EINVAL);
2594
2595 ifp->if_fib = ifr->ifr_fib;
2596 break;
2597
2598 case SIOCSIFFLAGS:
2599 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2600 if (error)
2601 return (error);
2602 /*
2603 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2604 * check, so we don't need special handling here yet.
2605 */
2606 new_flags = (ifr->ifr_flags & 0xffff) |
2607 (ifr->ifr_flagshigh << 16);
2608 if (ifp->if_flags & IFF_UP &&
2609 (new_flags & IFF_UP) == 0) {
2610 if_down(ifp);
2611 } else if (new_flags & IFF_UP &&
2612 (ifp->if_flags & IFF_UP) == 0) {
2613 do_ifup = 1;
2614 }
2615 /* See if permanently promiscuous mode bit is about to flip */
2616 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
2617 if (new_flags & IFF_PPROMISC)
2618 ifp->if_flags |= IFF_PROMISC;
2619 else if (ifp->if_pcount == 0)
2620 ifp->if_flags &= ~IFF_PROMISC;
2621 if (log_promisc_mode_change)
2622 log(LOG_INFO, "%s: permanently promiscuous mode %s\n",
2623 ifp->if_xname,
2624 ((new_flags & IFF_PPROMISC) ?
2625 "enabled" : "disabled"));
2626 }
2627 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2628 (new_flags &~ IFF_CANTCHANGE);
2629 if (ifp->if_ioctl) {
2630 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2631 }
2632 if (do_ifup)
2633 if_up(ifp);
2634 getmicrotime(&ifp->if_lastchange);
2635 break;
2636
2637 case SIOCSIFCAP:
2638 error = priv_check(td, PRIV_NET_SETIFCAP);
2639 if (error)
2640 return (error);
2641 if (ifp->if_ioctl == NULL)
2642 return (EOPNOTSUPP);
2643 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2644 return (EINVAL);
2645 error = (*ifp->if_ioctl)(ifp, cmd, data);
2646 if (error == 0)
2647 getmicrotime(&ifp->if_lastchange);
2648 break;
2649
2650 #ifdef MAC
2651 case SIOCSIFMAC:
2652 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2653 break;
2654 #endif
2655
2656 case SIOCSIFNAME:
2657 error = priv_check(td, PRIV_NET_SETIFNAME);
2658 if (error)
2659 return (error);
2660 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2661 NULL);
2662 if (error != 0)
2663 return (error);
2664 if (new_name[0] == '\0')
2665 return (EINVAL);
2666 if (new_name[IFNAMSIZ-1] != '\0') {
2667 new_name[IFNAMSIZ-1] = '\0';
2668 if (strlen(new_name) == IFNAMSIZ-1)
2669 return (EINVAL);
2670 }
2671 if (ifunit(new_name) != NULL)
2672 return (EEXIST);
2673
2674 /*
2675 * XXX: Locking. Nothing else seems to lock if_flags,
2676 * and there are numerous other races with the
2677 * ifunit() checks not being atomic with namespace
2678 * changes (renames, vmoves, if_attach, etc).
2679 */
2680 ifp->if_flags |= IFF_RENAMING;
2681
2682 /* Announce the departure of the interface. */
2683 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2684 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
2685
2686 log(LOG_INFO, "%s: changing name to '%s'\n",
2687 ifp->if_xname, new_name);
2688
2689 IF_ADDR_WLOCK(ifp);
2690 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2691 ifa = ifp->if_addr;
2692 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2693 namelen = strlen(new_name);
2694 onamelen = sdl->sdl_nlen;
2695 /*
2696 * Move the address if needed. This is safe because we
2697 * allocate space for a name of length IFNAMSIZ when we
2698 * create this in if_attach().
2699 */
2700 if (namelen != onamelen) {
2701 bcopy(sdl->sdl_data + onamelen,
2702 sdl->sdl_data + namelen, sdl->sdl_alen);
2703 }
2704 bcopy(new_name, sdl->sdl_data, namelen);
2705 sdl->sdl_nlen = namelen;
2706 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2707 bzero(sdl->sdl_data, onamelen);
2708 while (namelen != 0)
2709 sdl->sdl_data[--namelen] = 0xff;
2710 IF_ADDR_WUNLOCK(ifp);
2711
2712 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
2713 /* Announce the return of the interface. */
2714 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2715
2716 ifp->if_flags &= ~IFF_RENAMING;
2717 break;
2718
2719 #ifdef VIMAGE
2720 case SIOCSIFVNET:
2721 error = priv_check(td, PRIV_NET_SETIFVNET);
2722 if (error)
2723 return (error);
2724 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2725 break;
2726 #endif
2727
2728 case SIOCSIFMETRIC:
2729 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2730 if (error)
2731 return (error);
2732 ifp->if_metric = ifr->ifr_metric;
2733 getmicrotime(&ifp->if_lastchange);
2734 break;
2735
2736 case SIOCSIFPHYS:
2737 error = priv_check(td, PRIV_NET_SETIFPHYS);
2738 if (error)
2739 return (error);
2740 if (ifp->if_ioctl == NULL)
2741 return (EOPNOTSUPP);
2742 error = (*ifp->if_ioctl)(ifp, cmd, data);
2743 if (error == 0)
2744 getmicrotime(&ifp->if_lastchange);
2745 break;
2746
2747 case SIOCSIFMTU:
2748 {
2749 u_long oldmtu = ifp->if_mtu;
2750
2751 error = priv_check(td, PRIV_NET_SETIFMTU);
2752 if (error)
2753 return (error);
2754 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2755 return (EINVAL);
2756 if (ifp->if_ioctl == NULL)
2757 return (EOPNOTSUPP);
2758 error = (*ifp->if_ioctl)(ifp, cmd, data);
2759 if (error == 0) {
2760 getmicrotime(&ifp->if_lastchange);
2761 rt_ifmsg(ifp);
2762 }
2763 /*
2764 * If the link MTU changed, do network layer specific procedure.
2765 */
2766 if (ifp->if_mtu != oldmtu) {
2767 #ifdef INET6
2768 nd6_setmtu(ifp);
2769 #endif
2770 rt_updatemtu(ifp);
2771 }
2772 break;
2773 }
2774
2775 case SIOCADDMULTI:
2776 case SIOCDELMULTI:
2777 if (cmd == SIOCADDMULTI)
2778 error = priv_check(td, PRIV_NET_ADDMULTI);
2779 else
2780 error = priv_check(td, PRIV_NET_DELMULTI);
2781 if (error)
2782 return (error);
2783
2784 /* Don't allow group membership on non-multicast interfaces. */
2785 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2786 return (EOPNOTSUPP);
2787
2788 /* Don't let users screw up protocols' entries. */
2789 if (ifr->ifr_addr.sa_family != AF_LINK)
2790 return (EINVAL);
2791
2792 if (cmd == SIOCADDMULTI) {
2793 struct ifmultiaddr *ifma;
2794
2795 /*
2796 * Userland is only permitted to join groups once
2797 * via the if_addmulti() KPI, because it cannot hold
2798 * struct ifmultiaddr * between calls. It may also
2799 * lose a race while we check if the membership
2800 * already exists.
2801 */
2802 IF_ADDR_RLOCK(ifp);
2803 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2804 IF_ADDR_RUNLOCK(ifp);
2805 if (ifma != NULL)
2806 error = EADDRINUSE;
2807 else
2808 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2809 } else {
2810 error = if_delmulti(ifp, &ifr->ifr_addr);
2811 }
2812 if (error == 0)
2813 getmicrotime(&ifp->if_lastchange);
2814 break;
2815
2816 case SIOCSIFPHYADDR:
2817 case SIOCDIFPHYADDR:
2818 #ifdef INET6
2819 case SIOCSIFPHYADDR_IN6:
2820 #endif
2821 case SIOCSIFMEDIA:
2822 case SIOCSIFGENERIC:
2823 error = priv_check(td, PRIV_NET_HWIOCTL);
2824 if (error)
2825 return (error);
2826 if (ifp->if_ioctl == NULL)
2827 return (EOPNOTSUPP);
2828 error = (*ifp->if_ioctl)(ifp, cmd, data);
2829 if (error == 0)
2830 getmicrotime(&ifp->if_lastchange);
2831 break;
2832
2833 case SIOCGIFSTATUS:
2834 case SIOCGIFPSRCADDR:
2835 case SIOCGIFPDSTADDR:
2836 case SIOCGIFMEDIA:
2837 case SIOCGIFXMEDIA:
2838 case SIOCGIFGENERIC:
2839 case SIOCGIFRSSKEY:
2840 case SIOCGIFRSSHASH:
2841 if (ifp->if_ioctl == NULL)
2842 return (EOPNOTSUPP);
2843 error = (*ifp->if_ioctl)(ifp, cmd, data);
2844 break;
2845
2846 case SIOCSIFLLADDR:
2847 error = priv_check(td, PRIV_NET_SETLLADDR);
2848 if (error)
2849 return (error);
2850 error = if_setlladdr(ifp,
2851 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2852 break;
2853
2854 case SIOCGHWADDR:
2855 error = if_gethwaddr(ifp, ifr);
2856 break;
2857
2858 CASE_IOC_IFGROUPREQ(SIOCAIFGROUP):
2859 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2860 if (error)
2861 return (error);
2862 if ((error = if_addgroup(ifp,
2863 ifgr_group_get((struct ifgroupreq *)data))))
2864 return (error);
2865 break;
2866
2867 CASE_IOC_IFGROUPREQ(SIOCGIFGROUP):
2868 if ((error = if_getgroup((struct ifgroupreq *)data, ifp)))
2869 return (error);
2870 break;
2871
2872 CASE_IOC_IFGROUPREQ(SIOCDIFGROUP):
2873 error = priv_check(td, PRIV_NET_DELIFGROUP);
2874 if (error)
2875 return (error);
2876 if ((error = if_delgroup(ifp,
2877 ifgr_group_get((struct ifgroupreq *)data))))
2878 return (error);
2879 break;
2880
2881 default:
2882 error = ENOIOCTL;
2883 break;
2884 }
2885 return (error);
2886 }
2887
2888 /* COMPAT_SVR4 */
2889 #define OSIOCGIFCONF _IOWR('i', 20, struct ifconf)
2890
2891 #ifdef COMPAT_FREEBSD32
2892 struct ifconf32 {
2893 int32_t ifc_len;
2894 union {
2895 uint32_t ifcu_buf;
2896 uint32_t ifcu_req;
2897 } ifc_ifcu;
2898 };
2899 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
2900 #endif
2901
2902 #ifdef COMPAT_FREEBSD32
2903 static void
2904 ifmr_init(struct ifmediareq *ifmr, caddr_t data)
2905 {
2906 struct ifmediareq32 *ifmr32;
2907
2908 ifmr32 = (struct ifmediareq32 *)data;
2909 memcpy(ifmr->ifm_name, ifmr32->ifm_name,
2910 sizeof(ifmr->ifm_name));
2911 ifmr->ifm_current = ifmr32->ifm_current;
2912 ifmr->ifm_mask = ifmr32->ifm_mask;
2913 ifmr->ifm_status = ifmr32->ifm_status;
2914 ifmr->ifm_active = ifmr32->ifm_active;
2915 ifmr->ifm_count = ifmr32->ifm_count;
2916 ifmr->ifm_ulist = (int *)(uintptr_t)ifmr32->ifm_ulist;
2917 }
2918
2919 static void
2920 ifmr_update(const struct ifmediareq *ifmr, caddr_t data)
2921 {
2922 struct ifmediareq32 *ifmr32;
2923
2924 ifmr32 = (struct ifmediareq32 *)data;
2925 ifmr32->ifm_current = ifmr->ifm_current;
2926 ifmr32->ifm_mask = ifmr->ifm_mask;
2927 ifmr32->ifm_status = ifmr->ifm_status;
2928 ifmr32->ifm_active = ifmr->ifm_active;
2929 ifmr32->ifm_count = ifmr->ifm_count;
2930 }
2931 #endif
2932
2933 /*
2934 * Interface ioctls.
2935 */
2936 int
2937 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2938 {
2939 #ifdef COMPAT_FREEBSD32
2940 caddr_t saved_data;
2941 struct ifmediareq ifmr;
2942 #endif
2943 struct ifmediareq *ifmrp;
2944 struct ifnet *ifp;
2945 struct ifreq *ifr;
2946 int error;
2947 int oif_flags;
2948 #ifdef VIMAGE
2949 int shutdown;
2950 #endif
2951
2952 CURVNET_SET(so->so_vnet);
2953 #ifdef VIMAGE
2954 /* Make sure the VNET is stable. */
2955 shutdown = (so->so_vnet->vnet_state > SI_SUB_VNET &&
2956 so->so_vnet->vnet_state < SI_SUB_VNET_DONE) ? 1 : 0;
2957 if (shutdown) {
2958 CURVNET_RESTORE();
2959 return (EBUSY);
2960 }
2961 #endif
2962
2963
2964 switch (cmd) {
2965 case SIOCGIFCONF:
2966 case OSIOCGIFCONF: /* COMPAT_SVR4 */
2967 error = ifconf(cmd, data);
2968 CURVNET_RESTORE();
2969 return (error);
2970
2971 #ifdef COMPAT_FREEBSD32
2972 case SIOCGIFCONF32:
2973 {
2974 struct ifconf32 *ifc32;
2975 struct ifconf ifc;
2976
2977 ifc32 = (struct ifconf32 *)data;
2978 ifc.ifc_len = ifc32->ifc_len;
2979 ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2980
2981 error = ifconf(SIOCGIFCONF, (void *)&ifc);
2982 CURVNET_RESTORE();
2983 if (error == 0)
2984 ifc32->ifc_len = ifc.ifc_len;
2985 return (error);
2986 }
2987 #endif
2988 }
2989
2990 ifmrp = NULL;
2991 #ifdef COMPAT_FREEBSD32
2992 switch (cmd) {
2993 case SIOCGIFMEDIA32:
2994 case SIOCGIFXMEDIA32:
2995 ifmrp = &ifmr;
2996 ifmr_init(ifmrp, data);
2997 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2998 saved_data = data;
2999 data = (caddr_t)ifmrp;
3000 }
3001 #endif
3002
3003 ifr = (struct ifreq *)data;
3004 switch (cmd) {
3005 #ifdef VIMAGE
3006 case SIOCSIFRVNET:
3007 error = priv_check(td, PRIV_NET_SETIFVNET);
3008 if (error == 0)
3009 error = if_vmove_reclaim(td, ifr->ifr_name,
3010 ifr->ifr_jid);
3011 goto out_noref;
3012 #endif
3013 case SIOCIFCREATE:
3014 case SIOCIFCREATE2:
3015 error = priv_check(td, PRIV_NET_IFCREATE);
3016 if (error == 0)
3017 error = if_clone_create(ifr->ifr_name,
3018 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
3019 ifr_data_get_ptr(ifr) : NULL);
3020 goto out_noref;
3021 case SIOCIFDESTROY:
3022 error = priv_check(td, PRIV_NET_IFDESTROY);
3023 if (error == 0)
3024 error = if_clone_destroy(ifr->ifr_name);
3025 goto out_noref;
3026
3027 case SIOCIFGCLONERS:
3028 error = if_clone_list((struct if_clonereq *)data);
3029 goto out_noref;
3030
3031 CASE_IOC_IFGROUPREQ(SIOCGIFGMEMB):
3032 error = if_getgroupmembers((struct ifgroupreq *)data);
3033 goto out_noref;
3034
3035 #if defined(INET) || defined(INET6)
3036 case SIOCSVH:
3037 case SIOCGVH:
3038 if (carp_ioctl_p == NULL)
3039 error = EPROTONOSUPPORT;
3040 else
3041 error = (*carp_ioctl_p)(ifr, cmd, td);
3042 goto out_noref;
3043 #endif
3044 }
3045
3046 ifp = ifunit_ref(ifr->ifr_name);
3047 if (ifp == NULL) {
3048 error = ENXIO;
3049 goto out_noref;
3050 }
3051
3052 error = ifhwioctl(cmd, ifp, data, td);
3053 if (error != ENOIOCTL)
3054 goto out_ref;
3055
3056 oif_flags = ifp->if_flags;
3057 if (so->so_proto == NULL) {
3058 error = EOPNOTSUPP;
3059 goto out_ref;
3060 }
3061
3062 /*
3063 * Pass the request on to the socket control method, and if the
3064 * latter returns EOPNOTSUPP, directly to the interface.
3065 *
3066 * Make an exception for the legacy SIOCSIF* requests. Drivers
3067 * trust SIOCSIFADDR et al to come from an already privileged
3068 * layer, and do not perform any credentials checks or input
3069 * validation.
3070 */
3071 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data,
3072 ifp, td));
3073 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
3074 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
3075 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
3076 error = (*ifp->if_ioctl)(ifp, cmd, data);
3077
3078 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
3079 #ifdef INET6
3080 if (ifp->if_flags & IFF_UP)
3081 in6_if_up(ifp);
3082 #endif
3083 }
3084
3085 out_ref:
3086 if_rele(ifp);
3087 out_noref:
3088 #ifdef COMPAT_FREEBSD32
3089 if (ifmrp != NULL) {
3090 KASSERT((cmd == SIOCGIFMEDIA || cmd == SIOCGIFXMEDIA),
3091 ("ifmrp non-NULL, but cmd is not an ifmedia req 0x%lx",
3092 cmd));
3093 data = saved_data;
3094 ifmr_update(ifmrp, data);
3095 }
3096 #endif
3097 CURVNET_RESTORE();
3098 return (error);
3099 }
3100
3101 /*
3102 * The code common to handling reference counted flags,
3103 * e.g., in ifpromisc() and if_allmulti().
3104 * The "pflag" argument can specify a permanent mode flag to check,
3105 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3106 *
3107 * Only to be used on stack-owned flags, not driver-owned flags.
3108 */
3109 static int
3110 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3111 {
3112 struct ifreq ifr;
3113 int error;
3114 int oldflags, oldcount;
3115
3116 /* Sanity checks to catch programming errors */
3117 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3118 ("%s: setting driver-owned flag %d", __func__, flag));
3119
3120 if (onswitch)
3121 KASSERT(*refcount >= 0,
3122 ("%s: increment negative refcount %d for flag %d",
3123 __func__, *refcount, flag));
3124 else
3125 KASSERT(*refcount > 0,
3126 ("%s: decrement non-positive refcount %d for flag %d",
3127 __func__, *refcount, flag));
3128
3129 /* In case this mode is permanent, just touch refcount */
3130 if (ifp->if_flags & pflag) {
3131 *refcount += onswitch ? 1 : -1;
3132 return (0);
3133 }
3134
3135 /* Save ifnet parameters for if_ioctl() may fail */
3136 oldcount = *refcount;
3137 oldflags = ifp->if_flags;
3138
3139 /*
3140 * See if we aren't the only and touching refcount is enough.
3141 * Actually toggle interface flag if we are the first or last.
3142 */
3143 if (onswitch) {
3144 if ((*refcount)++)
3145 return (0);
3146 ifp->if_flags |= flag;
3147 } else {
3148 if (--(*refcount))
3149 return (0);
3150 ifp->if_flags &= ~flag;
3151 }
3152
3153 /* Call down the driver since we've changed interface flags */
3154 if (ifp->if_ioctl == NULL) {
3155 error = EOPNOTSUPP;
3156 goto recover;
3157 }
3158 ifr.ifr_flags = ifp->if_flags & 0xffff;
3159 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3160 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3161 if (error)
3162 goto recover;
3163 /* Notify userland that interface flags have changed */
3164 rt_ifmsg(ifp);
3165 return (0);
3166
3167 recover:
3168 /* Recover after driver error */
3169 *refcount = oldcount;
3170 ifp->if_flags = oldflags;
3171 return (error);
3172 }
3173
3174 /*
3175 * Set/clear promiscuous mode on interface ifp based on the truth value
3176 * of pswitch. The calls are reference counted so that only the first
3177 * "on" request actually has an effect, as does the final "off" request.
3178 * Results are undefined if the "off" and "on" requests are not matched.
3179 */
3180 int
3181 ifpromisc(struct ifnet *ifp, int pswitch)
3182 {
3183 int error;
3184 int oldflags = ifp->if_flags;
3185
3186 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3187 &ifp->if_pcount, pswitch);
3188 /* If promiscuous mode status has changed, log a message */
3189 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3190 log_promisc_mode_change)
3191 log(LOG_INFO, "%s: promiscuous mode %s\n",
3192 ifp->if_xname,
3193 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3194 return (error);
3195 }
3196
3197 /*
3198 * Return interface configuration
3199 * of system. List may be used
3200 * in later ioctl's (above) to get
3201 * other information.
3202 */
3203 /*ARGSUSED*/
3204 static int
3205 ifconf(u_long cmd, caddr_t data)
3206 {
3207 struct ifconf *ifc = (struct ifconf *)data;
3208 struct ifnet *ifp;
3209 struct ifaddr *ifa;
3210 struct ifreq ifr;
3211 struct sbuf *sb;
3212 int error, full = 0, valid_len, max_len;
3213
3214 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */
3215 max_len = MAXPHYS - 1;
3216
3217 /* Prevent hostile input from being able to crash the system */
3218 if (ifc->ifc_len <= 0)
3219 return (EINVAL);
3220
3221 again:
3222 if (ifc->ifc_len <= max_len) {
3223 max_len = ifc->ifc_len;
3224 full = 1;
3225 }
3226 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3227 max_len = 0;
3228 valid_len = 0;
3229
3230 IFNET_RLOCK();
3231 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3232 int addrs;
3233
3234 /*
3235 * Zero the ifr to make sure we don't disclose the contents
3236 * of the stack.
3237 */
3238 memset(&ifr, 0, sizeof(ifr));
3239
3240 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3241 >= sizeof(ifr.ifr_name)) {
3242 sbuf_delete(sb);
3243 IFNET_RUNLOCK();
3244 return (ENAMETOOLONG);
3245 }
3246
3247 addrs = 0;
3248 IF_ADDR_RLOCK(ifp);
3249 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3250 struct sockaddr *sa = ifa->ifa_addr;
3251
3252 if (prison_if(curthread->td_ucred, sa) != 0)
3253 continue;
3254 addrs++;
3255 /* COMPAT_SVR4 */
3256 if (cmd == OSIOCGIFCONF) {
3257 struct osockaddr *osa =
3258 (struct osockaddr *)&ifr.ifr_addr;
3259 ifr.ifr_addr = *sa;
3260 osa->sa_family = sa->sa_family;
3261 sbuf_bcat(sb, &ifr, sizeof(ifr));
3262 max_len += sizeof(ifr);
3263 } else
3264 if (sa->sa_len <= sizeof(*sa)) {
3265 if (sa->sa_len < sizeof(*sa)) {
3266 memset(&ifr.ifr_ifru.ifru_addr, 0,
3267 sizeof(ifr.ifr_ifru.ifru_addr));
3268 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3269 sa->sa_len);
3270 } else
3271 ifr.ifr_ifru.ifru_addr = *sa;
3272 sbuf_bcat(sb, &ifr, sizeof(ifr));
3273 max_len += sizeof(ifr);
3274 } else {
3275 sbuf_bcat(sb, &ifr,
3276 offsetof(struct ifreq, ifr_addr));
3277 max_len += offsetof(struct ifreq, ifr_addr);
3278 sbuf_bcat(sb, sa, sa->sa_len);
3279 max_len += sa->sa_len;
3280 }
3281
3282 if (sbuf_error(sb) == 0)
3283 valid_len = sbuf_len(sb);
3284 }
3285 IF_ADDR_RUNLOCK(ifp);
3286 if (addrs == 0) {
3287 sbuf_bcat(sb, &ifr, sizeof(ifr));
3288 max_len += sizeof(ifr);
3289
3290 if (sbuf_error(sb) == 0)
3291 valid_len = sbuf_len(sb);
3292 }
3293 }
3294 IFNET_RUNLOCK();
3295
3296 /*
3297 * If we didn't allocate enough space (uncommon), try again. If
3298 * we have already allocated as much space as we are allowed,
3299 * return what we've got.
3300 */
3301 if (valid_len != max_len && !full) {
3302 sbuf_delete(sb);
3303 goto again;
3304 }
3305
3306 ifc->ifc_len = valid_len;
3307 sbuf_finish(sb);
3308 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3309 sbuf_delete(sb);
3310 return (error);
3311 }
3312
3313 /*
3314 * Just like ifpromisc(), but for all-multicast-reception mode.
3315 */
3316 int
3317 if_allmulti(struct ifnet *ifp, int onswitch)
3318 {
3319
3320 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch));
3321 }
3322
3323 struct ifmultiaddr *
3324 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3325 {
3326 struct ifmultiaddr *ifma;
3327
3328 IF_ADDR_LOCK_ASSERT(ifp);
3329
3330 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3331 if (sa->sa_family == AF_LINK) {
3332 if (sa_dl_equal(ifma->ifma_addr, sa))
3333 break;
3334 } else {
3335 if (sa_equal(ifma->ifma_addr, sa))
3336 break;
3337 }
3338 }
3339
3340 return ifma;
3341 }
3342
3343 /*
3344 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3345 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3346 * the ifnet multicast address list here, so the caller must do that and
3347 * other setup work (such as notifying the device driver). The reference
3348 * count is initialized to 1.
3349 */
3350 static struct ifmultiaddr *
3351 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3352 int mflags)
3353 {
3354 struct ifmultiaddr *ifma;
3355 struct sockaddr *dupsa;
3356
3357 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3358 M_ZERO);
3359 if (ifma == NULL)
3360 return (NULL);
3361
3362 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3363 if (dupsa == NULL) {
3364 free(ifma, M_IFMADDR);
3365 return (NULL);
3366 }
3367 bcopy(sa, dupsa, sa->sa_len);
3368 ifma->ifma_addr = dupsa;
3369
3370 ifma->ifma_ifp = ifp;
3371 ifma->ifma_refcount = 1;
3372 ifma->ifma_protospec = NULL;
3373
3374 if (llsa == NULL) {
3375 ifma->ifma_lladdr = NULL;
3376 return (ifma);
3377 }
3378
3379 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3380 if (dupsa == NULL) {
3381 free(ifma->ifma_addr, M_IFMADDR);
3382 free(ifma, M_IFMADDR);
3383 return (NULL);
3384 }
3385 bcopy(llsa, dupsa, llsa->sa_len);
3386 ifma->ifma_lladdr = dupsa;
3387
3388 return (ifma);
3389 }
3390
3391 /*
3392 * if_freemulti: free ifmultiaddr structure and possibly attached related
3393 * addresses. The caller is responsible for implementing reference
3394 * counting, notifying the driver, handling routing messages, and releasing
3395 * any dependent link layer state.
3396 */
3397 static void
3398 if_freemulti(struct ifmultiaddr *ifma)
3399 {
3400
3401 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3402 ifma->ifma_refcount));
3403
3404 if (ifma->ifma_lladdr != NULL)
3405 free(ifma->ifma_lladdr, M_IFMADDR);
3406 free(ifma->ifma_addr, M_IFMADDR);
3407 free(ifma, M_IFMADDR);
3408 }
3409
3410 /*
3411 * Register an additional multicast address with a network interface.
3412 *
3413 * - If the address is already present, bump the reference count on the
3414 * address and return.
3415 * - If the address is not link-layer, look up a link layer address.
3416 * - Allocate address structures for one or both addresses, and attach to the
3417 * multicast address list on the interface. If automatically adding a link
3418 * layer address, the protocol address will own a reference to the link
3419 * layer address, to be freed when it is freed.
3420 * - Notify the network device driver of an addition to the multicast address
3421 * list.
3422 *
3423 * 'sa' points to caller-owned memory with the desired multicast address.
3424 *
3425 * 'retifma' will be used to return a pointer to the resulting multicast
3426 * address reference, if desired.
3427 */
3428 int
3429 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3430 struct ifmultiaddr **retifma)
3431 {
3432 struct ifmultiaddr *ifma, *ll_ifma;
3433 struct sockaddr *llsa;
3434 struct sockaddr_dl sdl;
3435 int error;
3436
3437 /*
3438 * If the address is already present, return a new reference to it;
3439 * otherwise, allocate storage and set up a new address.
3440 */
3441 IF_ADDR_WLOCK(ifp);
3442 ifma = if_findmulti(ifp, sa);
3443 if (ifma != NULL) {
3444 ifma->ifma_refcount++;
3445 if (retifma != NULL)
3446 *retifma = ifma;
3447 IF_ADDR_WUNLOCK(ifp);
3448 return (0);
3449 }
3450
3451 /*
3452 * The address isn't already present; resolve the protocol address
3453 * into a link layer address, and then look that up, bump its
3454 * refcount or allocate an ifma for that also.
3455 * Most link layer resolving functions returns address data which
3456 * fits inside default sockaddr_dl structure. However callback
3457 * can allocate another sockaddr structure, in that case we need to
3458 * free it later.
3459 */
3460 llsa = NULL;
3461 ll_ifma = NULL;
3462 if (ifp->if_resolvemulti != NULL) {
3463 /* Provide called function with buffer size information */
3464 sdl.sdl_len = sizeof(sdl);
3465 llsa = (struct sockaddr *)&sdl;
3466 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3467 if (error)
3468 goto unlock_out;
3469 }
3470
3471 /*
3472 * Allocate the new address. Don't hook it up yet, as we may also
3473 * need to allocate a link layer multicast address.
3474 */
3475 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3476 if (ifma == NULL) {
3477 error = ENOMEM;
3478 goto free_llsa_out;
3479 }
3480
3481 /*
3482 * If a link layer address is found, we'll need to see if it's
3483 * already present in the address list, or allocate is as well.
3484 * When this block finishes, the link layer address will be on the
3485 * list.
3486 */
3487 if (llsa != NULL) {
3488 ll_ifma = if_findmulti(ifp, llsa);
3489 if (ll_ifma == NULL) {
3490 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3491 if (ll_ifma == NULL) {
3492 --ifma->ifma_refcount;
3493 if_freemulti(ifma);
3494 error = ENOMEM;
3495 goto free_llsa_out;
3496 }
3497 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3498 ifma_link);
3499 } else
3500 ll_ifma->ifma_refcount++;
3501 ifma->ifma_llifma = ll_ifma;
3502 }
3503
3504 /*
3505 * We now have a new multicast address, ifma, and possibly a new or
3506 * referenced link layer address. Add the primary address to the
3507 * ifnet address list.
3508 */
3509 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3510
3511 if (retifma != NULL)
3512 *retifma = ifma;
3513
3514 /*
3515 * Must generate the message while holding the lock so that 'ifma'
3516 * pointer is still valid.
3517 */
3518 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3519 IF_ADDR_WUNLOCK(ifp);
3520
3521 /*
3522 * We are certain we have added something, so call down to the
3523 * interface to let them know about it.
3524 */
3525 if (ifp->if_ioctl != NULL) {
3526 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3527 }
3528
3529 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3530 link_free_sdl(llsa);
3531
3532 return (0);
3533
3534 free_llsa_out:
3535 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3536 link_free_sdl(llsa);
3537
3538 unlock_out:
3539 IF_ADDR_WUNLOCK(ifp);
3540 return (error);
3541 }
3542
3543 /*
3544 * Delete a multicast group membership by network-layer group address.
3545 *
3546 * Returns ENOENT if the entry could not be found. If ifp no longer
3547 * exists, results are undefined. This entry point should only be used
3548 * from subsystems which do appropriate locking to hold ifp for the
3549 * duration of the call.
3550 * Network-layer protocol domains must use if_delmulti_ifma().
3551 */
3552 int
3553 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3554 {
3555 struct ifmultiaddr *ifma;
3556 int lastref;
3557 #ifdef INVARIANTS
3558 struct ifnet *oifp;
3559
3560 IFNET_RLOCK_NOSLEEP();
3561 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3562 if (ifp == oifp)
3563 break;
3564 if (ifp != oifp)
3565 ifp = NULL;
3566 IFNET_RUNLOCK_NOSLEEP();
3567
3568 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__));
3569 #endif
3570 if (ifp == NULL)
3571 return (ENOENT);
3572
3573 IF_ADDR_WLOCK(ifp);
3574 lastref = 0;
3575 ifma = if_findmulti(ifp, sa);
3576 if (ifma != NULL)
3577 lastref = if_delmulti_locked(ifp, ifma, 0);
3578 IF_ADDR_WUNLOCK(ifp);
3579
3580 if (ifma == NULL)
3581 return (ENOENT);
3582
3583 if (lastref && ifp->if_ioctl != NULL) {
3584 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3585 }
3586
3587 return (0);
3588 }
3589
3590 /*
3591 * Delete all multicast group membership for an interface.
3592 * Should be used to quickly flush all multicast filters.
3593 */
3594 void
3595 if_delallmulti(struct ifnet *ifp)
3596 {
3597 struct ifmultiaddr *ifma;
3598 struct ifmultiaddr *next;
3599
3600 IF_ADDR_WLOCK(ifp);
3601 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3602 if_delmulti_locked(ifp, ifma, 0);
3603 IF_ADDR_WUNLOCK(ifp);
3604 }
3605
3606 /*
3607 * Delete a multicast group membership by group membership pointer.
3608 * Network-layer protocol domains must use this routine.
3609 *
3610 * It is safe to call this routine if the ifp disappeared.
3611 */
3612 void
3613 if_delmulti_ifma(struct ifmultiaddr *ifma)
3614 {
3615 struct ifnet *ifp;
3616 int lastref;
3617
3618 ifp = ifma->ifma_ifp;
3619 #ifdef DIAGNOSTIC
3620 if (ifp == NULL) {
3621 printf("%s: ifma_ifp seems to be detached\n", __func__);
3622 } else {
3623 struct ifnet *oifp;
3624
3625 IFNET_RLOCK_NOSLEEP();
3626 TAILQ_FOREACH(oifp, &V_ifnet, if_link)
3627 if (ifp == oifp)
3628 break;
3629 if (ifp != oifp) {
3630 printf("%s: ifnet %p disappeared\n", __func__, ifp);
3631 ifp = NULL;
3632 }
3633 IFNET_RUNLOCK_NOSLEEP();
3634 }
3635 #endif
3636 /*
3637 * If and only if the ifnet instance exists: Acquire the address lock.
3638 */
3639 if (ifp != NULL)
3640 IF_ADDR_WLOCK(ifp);
3641
3642 lastref = if_delmulti_locked(ifp, ifma, 0);
3643
3644 if (ifp != NULL) {
3645 /*
3646 * If and only if the ifnet instance exists:
3647 * Release the address lock.
3648 * If the group was left: update the hardware hash filter.
3649 */
3650 IF_ADDR_WUNLOCK(ifp);
3651 if (lastref && ifp->if_ioctl != NULL) {
3652 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3653 }
3654 }
3655 }
3656
3657 /*
3658 * Perform deletion of network-layer and/or link-layer multicast address.
3659 *
3660 * Return 0 if the reference count was decremented.
3661 * Return 1 if the final reference was released, indicating that the
3662 * hardware hash filter should be reprogrammed.
3663 */
3664 static int
3665 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3666 {
3667 struct ifmultiaddr *ll_ifma;
3668
3669 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3670 KASSERT(ifma->ifma_ifp == ifp,
3671 ("%s: inconsistent ifp %p", __func__, ifp));
3672 IF_ADDR_WLOCK_ASSERT(ifp);
3673 }
3674
3675 ifp = ifma->ifma_ifp;
3676
3677 /*
3678 * If the ifnet is detaching, null out references to ifnet,
3679 * so that upper protocol layers will notice, and not attempt
3680 * to obtain locks for an ifnet which no longer exists. The
3681 * routing socket announcement must happen before the ifnet
3682 * instance is detached from the system.
3683 */
3684 if (detaching) {
3685 #ifdef DIAGNOSTIC
3686 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3687 #endif
3688 /*
3689 * ifp may already be nulled out if we are being reentered
3690 * to delete the ll_ifma.
3691 */
3692 if (ifp != NULL) {
3693 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3694 ifma->ifma_ifp = NULL;
3695 }
3696 }
3697
3698 if (--ifma->ifma_refcount > 0)
3699 return 0;
3700
3701 /*
3702 * If this ifma is a network-layer ifma, a link-layer ifma may
3703 * have been associated with it. Release it first if so.
3704 */
3705 ll_ifma = ifma->ifma_llifma;
3706 if (ll_ifma != NULL) {
3707 KASSERT(ifma->ifma_lladdr != NULL,
3708 ("%s: llifma w/o lladdr", __func__));
3709 if (detaching)
3710 ll_ifma->ifma_ifp = NULL; /* XXX */
3711 if (--ll_ifma->ifma_refcount == 0) {
3712 if (ifp != NULL) {
3713 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma,
3714 ifma_link);
3715 }
3716 if_freemulti(ll_ifma);
3717 }
3718 }
3719
3720 if (ifp != NULL)
3721 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
3722
3723 if_freemulti(ifma);
3724
3725 /*
3726 * The last reference to this instance of struct ifmultiaddr
3727 * was released; the hardware should be notified of this change.
3728 */
3729 return 1;
3730 }
3731
3732 /*
3733 * Set the link layer address on an interface.
3734 *
3735 * At this time we only support certain types of interfaces,
3736 * and we don't allow the length of the address to change.
3737 *
3738 * Set noinline to be dtrace-friendly
3739 */
3740 __noinline int
3741 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3742 {
3743 struct sockaddr_dl *sdl;
3744 struct ifaddr *ifa;
3745 struct ifreq ifr;
3746
3747 IF_ADDR_RLOCK(ifp);
3748 ifa = ifp->if_addr;
3749 if (ifa == NULL) {
3750 IF_ADDR_RUNLOCK(ifp);
3751 return (EINVAL);
3752 }
3753 ifa_ref(ifa);
3754 IF_ADDR_RUNLOCK(ifp);
3755 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3756 if (sdl == NULL) {
3757 ifa_free(ifa);
3758 return (EINVAL);
3759 }
3760 if (len != sdl->sdl_alen) { /* don't allow length to change */
3761 ifa_free(ifa);
3762 return (EINVAL);
3763 }
3764 switch (ifp->if_type) {
3765 case IFT_ETHER:
3766 case IFT_FDDI:
3767 case IFT_XETHER:
3768 case IFT_ISO88025:
3769 case IFT_L2VLAN:
3770 case IFT_BRIDGE:
3771 case IFT_ARCNET:
3772 case IFT_IEEE8023ADLAG:
3773 case IFT_IEEE80211:
3774 bcopy(lladdr, LLADDR(sdl), len);
3775 ifa_free(ifa);
3776 break;
3777 default:
3778 ifa_free(ifa);
3779 return (ENODEV);
3780 }
3781
3782 /*
3783 * If the interface is already up, we need
3784 * to re-init it in order to reprogram its
3785 * address filter.
3786 */
3787 if ((ifp->if_flags & IFF_UP) != 0) {
3788 if (ifp->if_ioctl) {
3789 ifp->if_flags &= ~IFF_UP;
3790 ifr.ifr_flags = ifp->if_flags & 0xffff;
3791 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3792 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3793 ifp->if_flags |= IFF_UP;
3794 ifr.ifr_flags = ifp->if_flags & 0xffff;
3795 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3796 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3797 }
3798 }
3799 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3800 return (0);
3801 }
3802
3803 /*
3804 * Compat function for handling basic encapsulation requests.
3805 * Not converted stacks (FDDI, IB, ..) supports traditional
3806 * output model: ARP (and other similar L2 protocols) are handled
3807 * inside output routine, arpresolve/nd6_resolve() returns MAC
3808 * address instead of full prepend.
3809 *
3810 * This function creates calculated header==MAC for IPv4/IPv6 and
3811 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3812 * address families.
3813 */
3814 static int
3815 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3816 {
3817
3818 if (req->rtype != IFENCAP_LL)
3819 return (EOPNOTSUPP);
3820
3821 if (req->bufsize < req->lladdr_len)
3822 return (ENOMEM);
3823
3824 switch (req->family) {
3825 case AF_INET:
3826 case AF_INET6:
3827 break;
3828 default:
3829 return (EAFNOSUPPORT);
3830 }
3831
3832 /* Copy lladdr to storage as is */
3833 memmove(req->buf, req->lladdr, req->lladdr_len);
3834 req->bufsize = req->lladdr_len;
3835 req->lladdr_off = 0;
3836
3837 return (0);
3838 }
3839
3840 /*
3841 * Get the link layer address that was read from the hardware at attach.
3842 *
3843 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3844 * their component interfaces as IFT_IEEE8023ADLAG.
3845 */
3846 int
3847 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3848 {
3849
3850 if (ifp->if_hw_addr == NULL)
3851 return (ENODEV);
3852
3853 switch (ifp->if_type) {
3854 case IFT_ETHER:
3855 case IFT_IEEE8023ADLAG:
3856 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3857 return (0);
3858 default:
3859 return (ENODEV);
3860 }
3861 }
3862
3863 /*
3864 * The name argument must be a pointer to storage which will last as
3865 * long as the interface does. For physical devices, the result of
3866 * device_get_name(dev) is a good choice and for pseudo-devices a
3867 * static string works well.
3868 */
3869 void
3870 if_initname(struct ifnet *ifp, const char *name, int unit)
3871 {
3872 ifp->if_dname = name;
3873 ifp->if_dunit = unit;
3874 if (unit != IF_DUNIT_NONE)
3875 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3876 else
3877 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3878 }
3879
3880 int
3881 if_printf(struct ifnet *ifp, const char * fmt, ...)
3882 {
3883 va_list ap;
3884 int retval;
3885
3886 retval = printf("%s: ", ifp->if_xname);
3887 va_start(ap, fmt);
3888 retval += vprintf(fmt, ap);
3889 va_end(ap);
3890 return (retval);
3891 }
3892
3893 void
3894 if_start(struct ifnet *ifp)
3895 {
3896
3897 (*(ifp)->if_start)(ifp);
3898 }
3899
3900 /*
3901 * Backwards compatibility interface for drivers
3902 * that have not implemented it
3903 */
3904 static int
3905 if_transmit(struct ifnet *ifp, struct mbuf *m)
3906 {
3907 int error;
3908
3909 IFQ_HANDOFF(ifp, m, error);
3910 return (error);
3911 }
3912
3913 static void
3914 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3915 {
3916
3917 m_freem(m);
3918 }
3919
3920 int
3921 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3922 {
3923 int active = 0;
3924
3925 IF_LOCK(ifq);
3926 if (_IF_QFULL(ifq)) {
3927 IF_UNLOCK(ifq);
3928 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3929 m_freem(m);
3930 return (0);
3931 }
3932 if (ifp != NULL) {
3933 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3934 if (m->m_flags & (M_BCAST|M_MCAST))
3935 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3936 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3937 }
3938 _IF_ENQUEUE(ifq, m);
3939 IF_UNLOCK(ifq);
3940 if (ifp != NULL && !active)
3941 (*(ifp)->if_start)(ifp);
3942 return (1);
3943 }
3944
3945 void
3946 if_register_com_alloc(u_char type,
3947 if_com_alloc_t *a, if_com_free_t *f)
3948 {
3949
3950 KASSERT(if_com_alloc[type] == NULL,
3951 ("if_register_com_alloc: %d already registered", type));
3952 KASSERT(if_com_free[type] == NULL,
3953 ("if_register_com_alloc: %d free already registered", type));
3954
3955 if_com_alloc[type] = a;
3956 if_com_free[type] = f;
3957 }
3958
3959 void
3960 if_deregister_com_alloc(u_char type)
3961 {
3962
3963 KASSERT(if_com_alloc[type] != NULL,
3964 ("if_deregister_com_alloc: %d not registered", type));
3965 KASSERT(if_com_free[type] != NULL,
3966 ("if_deregister_com_alloc: %d free not registered", type));
3967 if_com_alloc[type] = NULL;
3968 if_com_free[type] = NULL;
3969 }
3970
3971 /* API for driver access to network stack owned ifnet.*/
3972 uint64_t
3973 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
3974 {
3975 uint64_t oldbrate;
3976
3977 oldbrate = ifp->if_baudrate;
3978 ifp->if_baudrate = baudrate;
3979 return (oldbrate);
3980 }
3981
3982 uint64_t
3983 if_getbaudrate(if_t ifp)
3984 {
3985
3986 return (((struct ifnet *)ifp)->if_baudrate);
3987 }
3988
3989 int
3990 if_setcapabilities(if_t ifp, int capabilities)
3991 {
3992 ((struct ifnet *)ifp)->if_capabilities = capabilities;
3993 return (0);
3994 }
3995
3996 int
3997 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
3998 {
3999 ((struct ifnet *)ifp)->if_capabilities |= setbit;
4000 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit;
4001
4002 return (0);
4003 }
4004
4005 int
4006 if_getcapabilities(if_t ifp)
4007 {
4008 return ((struct ifnet *)ifp)->if_capabilities;
4009 }
4010
4011 int
4012 if_setcapenable(if_t ifp, int capabilities)
4013 {
4014 ((struct ifnet *)ifp)->if_capenable = capabilities;
4015 return (0);
4016 }
4017
4018 int
4019 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4020 {
4021 if(setcap)
4022 ((struct ifnet *)ifp)->if_capenable |= setcap;
4023 if(clearcap)
4024 ((struct ifnet *)ifp)->if_capenable &= ~clearcap;
4025
4026 return (0);
4027 }
4028
4029 const char *
4030 if_getdname(if_t ifp)
4031 {
4032 return ((struct ifnet *)ifp)->if_dname;
4033 }
4034
4035 int
4036 if_togglecapenable(if_t ifp, int togglecap)
4037 {
4038 ((struct ifnet *)ifp)->if_capenable ^= togglecap;
4039 return (0);
4040 }
4041
4042 int
4043 if_getcapenable(if_t ifp)
4044 {
4045 return ((struct ifnet *)ifp)->if_capenable;
4046 }
4047
4048 /*
4049 * This is largely undesirable because it ties ifnet to a device, but does
4050 * provide flexiblity for an embedded product vendor. Should be used with
4051 * the understanding that it violates the interface boundaries, and should be
4052 * a last resort only.
4053 */
4054 int
4055 if_setdev(if_t ifp, void *dev)
4056 {
4057 return (0);
4058 }
4059
4060 int
4061 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4062 {
4063 ((struct ifnet *)ifp)->if_drv_flags |= set_flags;
4064 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags;
4065
4066 return (0);
4067 }
4068
4069 int
4070 if_getdrvflags(if_t ifp)
4071 {
4072 return ((struct ifnet *)ifp)->if_drv_flags;
4073 }
4074
4075 int
4076 if_setdrvflags(if_t ifp, int flags)
4077 {
4078 ((struct ifnet *)ifp)->if_drv_flags = flags;
4079 return (0);
4080 }
4081
4082
4083 int
4084 if_setflags(if_t ifp, int flags)
4085 {
4086 ((struct ifnet *)ifp)->if_flags = flags;
4087 return (0);
4088 }
4089
4090 int
4091 if_setflagbits(if_t ifp, int set, int clear)
4092 {
4093 ((struct ifnet *)ifp)->if_flags |= set;
4094 ((struct ifnet *)ifp)->if_flags &= ~clear;
4095
4096 return (0);
4097 }
4098
4099 int
4100 if_getflags(if_t ifp)
4101 {
4102 return ((struct ifnet *)ifp)->if_flags;
4103 }
4104
4105 int
4106 if_clearhwassist(if_t ifp)
4107 {
4108 ((struct ifnet *)ifp)->if_hwassist = 0;
4109 return (0);
4110 }
4111
4112 int
4113 if_sethwassistbits(if_t ifp, int toset, int toclear)
4114 {
4115 ((struct ifnet *)ifp)->if_hwassist |= toset;
4116 ((struct ifnet *)ifp)->if_hwassist &= ~toclear;
4117
4118 return (0);
4119 }
4120
4121 int
4122 if_sethwassist(if_t ifp, int hwassist_bit)
4123 {
4124 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit;
4125 return (0);
4126 }
4127
4128 int
4129 if_gethwassist(if_t ifp)
4130 {
4131 return ((struct ifnet *)ifp)->if_hwassist;
4132 }
4133
4134 int
4135 if_setmtu(if_t ifp, int mtu)
4136 {
4137 ((struct ifnet *)ifp)->if_mtu = mtu;
4138 return (0);
4139 }
4140
4141 int
4142 if_getmtu(if_t ifp)
4143 {
4144 return ((struct ifnet *)ifp)->if_mtu;
4145 }
4146
4147 int
4148 if_getmtu_family(if_t ifp, int family)
4149 {
4150 struct domain *dp;
4151
4152 for (dp = domains; dp; dp = dp->dom_next) {
4153 if (dp->dom_family == family && dp->dom_ifmtu != NULL)
4154 return (dp->dom_ifmtu((struct ifnet *)ifp));
4155 }
4156
4157 return (((struct ifnet *)ifp)->if_mtu);
4158 }
4159
4160 int
4161 if_setsoftc(if_t ifp, void *softc)
4162 {
4163 ((struct ifnet *)ifp)->if_softc = softc;
4164 return (0);
4165 }
4166
4167 void *
4168 if_getsoftc(if_t ifp)
4169 {
4170 return ((struct ifnet *)ifp)->if_softc;
4171 }
4172
4173 void
4174 if_setrcvif(struct mbuf *m, if_t ifp)
4175 {
4176 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4177 }
4178
4179 void
4180 if_setvtag(struct mbuf *m, uint16_t tag)
4181 {
4182 m->m_pkthdr.ether_vtag = tag;
4183 }
4184
4185 uint16_t
4186 if_getvtag(struct mbuf *m)
4187 {
4188
4189 return (m->m_pkthdr.ether_vtag);
4190 }
4191
4192 int
4193 if_sendq_empty(if_t ifp)
4194 {
4195 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd);
4196 }
4197
4198 struct ifaddr *
4199 if_getifaddr(if_t ifp)
4200 {
4201 return ((struct ifnet *)ifp)->if_addr;
4202 }
4203
4204 int
4205 if_getamcount(if_t ifp)
4206 {
4207 return ((struct ifnet *)ifp)->if_amcount;
4208 }
4209
4210
4211 int
4212 if_setsendqready(if_t ifp)
4213 {
4214 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd);
4215 return (0);
4216 }
4217
4218 int
4219 if_setsendqlen(if_t ifp, int tx_desc_count)
4220 {
4221 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count);
4222 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count;
4223
4224 return (0);
4225 }
4226
4227 int
4228 if_vlantrunkinuse(if_t ifp)
4229 {
4230 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0;
4231 }
4232
4233 int
4234 if_input(if_t ifp, struct mbuf* sendmp)
4235 {
4236 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp);
4237 return (0);
4238
4239 }
4240
4241 /* XXX */
4242 #ifndef ETH_ADDR_LEN
4243 #define ETH_ADDR_LEN 6
4244 #endif
4245
4246 int
4247 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max)
4248 {
4249 struct ifmultiaddr *ifma;
4250 uint8_t *lmta = (uint8_t *)mta;
4251 int mcnt = 0;
4252
4253 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4254 if (ifma->ifma_addr->sa_family != AF_LINK)
4255 continue;
4256
4257 if (mcnt == max)
4258 break;
4259
4260 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
4261 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN);
4262 mcnt++;
4263 }
4264 *cnt = mcnt;
4265
4266 return (0);
4267 }
4268
4269 int
4270 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max)
4271 {
4272 int error;
4273
4274 if_maddr_rlock(ifp);
4275 error = if_setupmultiaddr(ifp, mta, cnt, max);
4276 if_maddr_runlock(ifp);
4277 return (error);
4278 }
4279
4280 int
4281 if_multiaddr_count(if_t ifp, int max)
4282 {
4283 struct ifmultiaddr *ifma;
4284 int count;
4285
4286 count = 0;
4287 if_maddr_rlock(ifp);
4288 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) {
4289 if (ifma->ifma_addr->sa_family != AF_LINK)
4290 continue;
4291 count++;
4292 if (count == max)
4293 break;
4294 }
4295 if_maddr_runlock(ifp);
4296 return (count);
4297 }
4298
4299 int
4300 if_multi_apply(struct ifnet *ifp, int (*filter)(void *, struct ifmultiaddr *, int), void *arg)
4301 {
4302 struct ifmultiaddr *ifma;
4303 int cnt = 0;
4304
4305 if_maddr_rlock(ifp);
4306 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4307 cnt += filter(arg, ifma, cnt);
4308 if_maddr_runlock(ifp);
4309 return (cnt);
4310 }
4311
4312 struct mbuf *
4313 if_dequeue(if_t ifp)
4314 {
4315 struct mbuf *m;
4316 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m);
4317
4318 return (m);
4319 }
4320
4321 int
4322 if_sendq_prepend(if_t ifp, struct mbuf *m)
4323 {
4324 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m);
4325 return (0);
4326 }
4327
4328 int
4329 if_setifheaderlen(if_t ifp, int len)
4330 {
4331 ((struct ifnet *)ifp)->if_hdrlen = len;
4332 return (0);
4333 }
4334
4335 caddr_t
4336 if_getlladdr(if_t ifp)
4337 {
4338 return (IF_LLADDR((struct ifnet *)ifp));
4339 }
4340
4341 void *
4342 if_gethandle(u_char type)
4343 {
4344 return (if_alloc(type));
4345 }
4346
4347 void
4348 if_bpfmtap(if_t ifh, struct mbuf *m)
4349 {
4350 struct ifnet *ifp = (struct ifnet *)ifh;
4351
4352 BPF_MTAP(ifp, m);
4353 }
4354
4355 void
4356 if_etherbpfmtap(if_t ifh, struct mbuf *m)
4357 {
4358 struct ifnet *ifp = (struct ifnet *)ifh;
4359
4360 ETHER_BPF_MTAP(ifp, m);
4361 }
4362
4363 void
4364 if_vlancap(if_t ifh)
4365 {
4366 struct ifnet *ifp = (struct ifnet *)ifh;
4367 VLAN_CAPABILITIES(ifp);
4368 }
4369
4370 void
4371 if_setinitfn(if_t ifp, void (*init_fn)(void *))
4372 {
4373 ((struct ifnet *)ifp)->if_init = init_fn;
4374 }
4375
4376 void
4377 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t))
4378 {
4379 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn;
4380 }
4381
4382 void
4383 if_setstartfn(if_t ifp, void (*start_fn)(if_t))
4384 {
4385 ((struct ifnet *)ifp)->if_start = (void *)start_fn;
4386 }
4387
4388 void
4389 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4390 {
4391 ((struct ifnet *)ifp)->if_transmit = start_fn;
4392 }
4393
4394 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4395 {
4396 ((struct ifnet *)ifp)->if_qflush = flush_fn;
4397
4398 }
4399
4400 void
4401 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4402 {
4403
4404 ifp->if_get_counter = fn;
4405 }
4406
4407 /* Revisit these - These are inline functions originally. */
4408 int
4409 drbr_inuse_drv(if_t ifh, struct buf_ring *br)
4410 {
4411 return drbr_inuse(ifh, br);
4412 }
4413
4414 struct mbuf*
4415 drbr_dequeue_drv(if_t ifh, struct buf_ring *br)
4416 {
4417 return drbr_dequeue(ifh, br);
4418 }
4419
4420 int
4421 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br)
4422 {
4423 return drbr_needs_enqueue(ifh, br);
4424 }
4425
4426 int
4427 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m)
4428 {
4429 return drbr_enqueue(ifh, br, m);
4430
4431 }
Cache object: bf4117b1f140fdf729b95ad7552b079e
|