1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include "opt_bpf.h"
34 #include "opt_inet6.h"
35 #include "opt_inet.h"
36 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/conf.h>
41 #include <sys/eventhandler.h>
42 #include <sys/malloc.h>
43 #include <sys/domainset.h>
44 #include <sys/sbuf.h>
45 #include <sys/bus.h>
46 #include <sys/epoch.h>
47 #include <sys/mbuf.h>
48 #include <sys/systm.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/refcount.h>
57 #include <sys/module.h>
58 #include <sys/nv.h>
59 #include <sys/rwlock.h>
60 #include <sys/sockio.h>
61 #include <sys/stdarg.h>
62 #include <sys/syslog.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/taskqueue.h>
66 #include <sys/domain.h>
67 #include <sys/jail.h>
68 #include <sys/priv.h>
69
70 #ifdef DDB
71 #include <ddb/ddb.h>
72 #endif
73
74 #include <vm/uma.h>
75
76 #include <net/bpf.h>
77 #include <net/if.h>
78 #include <net/if_arp.h>
79 #include <net/if_clone.h>
80 #include <net/if_dl.h>
81 #include <net/if_strings.h>
82 #include <net/if_types.h>
83 #include <net/if_var.h>
84 #include <net/if_media.h>
85 #include <net/if_mib.h>
86 #include <net/if_private.h>
87 #include <net/if_vlan_var.h>
88 #include <net/radix.h>
89 #include <net/route.h>
90 #include <net/route/route_ctl.h>
91 #include <net/vnet.h>
92
93 #if defined(INET) || defined(INET6)
94 #include <net/ethernet.h>
95 #include <netinet/in.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
98 #include <netinet/ip_carp.h>
99 #ifdef INET
100 #include <net/debugnet.h>
101 #include <netinet/if_ether.h>
102 #endif /* INET */
103 #ifdef INET6
104 #include <netinet6/in6_var.h>
105 #endif /* INET6 */
106 #endif /* INET || INET6 */
107
108 #include <security/mac/mac_framework.h>
109
110 /*
111 * Consumers of struct ifreq such as tcpdump assume no pad between ifr_name
112 * and ifr_ifru when it is used in SIOCGIFCONF.
113 */
114 _Static_assert(sizeof(((struct ifreq *)0)->ifr_name) ==
115 offsetof(struct ifreq, ifr_ifru), "gap between ifr_name and ifr_ifru");
116
117 __read_mostly epoch_t net_epoch_preempt;
118 #ifdef COMPAT_FREEBSD32
119 #include <sys/mount.h>
120 #include <compat/freebsd32/freebsd32.h>
121
122 struct ifreq_buffer32 {
123 uint32_t length; /* (size_t) */
124 uint32_t buffer; /* (void *) */
125 };
126
127 /*
128 * Interface request structure used for socket
129 * ioctl's. All interface ioctl's must have parameter
130 * definitions which begin with ifr_name. The
131 * remainder may be interface specific.
132 */
133 struct ifreq32 {
134 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */
135 union {
136 struct sockaddr ifru_addr;
137 struct sockaddr ifru_dstaddr;
138 struct sockaddr ifru_broadaddr;
139 struct ifreq_buffer32 ifru_buffer;
140 short ifru_flags[2];
141 short ifru_index;
142 int ifru_jid;
143 int ifru_metric;
144 int ifru_mtu;
145 int ifru_phys;
146 int ifru_media;
147 uint32_t ifru_data;
148 int ifru_cap[2];
149 u_int ifru_fib;
150 u_char ifru_vlan_pcp;
151 } ifr_ifru;
152 };
153 CTASSERT(sizeof(struct ifreq) == sizeof(struct ifreq32));
154 CTASSERT(__offsetof(struct ifreq, ifr_ifru) ==
155 __offsetof(struct ifreq32, ifr_ifru));
156
157 struct ifconf32 {
158 int32_t ifc_len;
159 union {
160 uint32_t ifcu_buf;
161 uint32_t ifcu_req;
162 } ifc_ifcu;
163 };
164 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32)
165
166 struct ifdrv32 {
167 char ifd_name[IFNAMSIZ];
168 uint32_t ifd_cmd;
169 uint32_t ifd_len;
170 uint32_t ifd_data;
171 };
172 #define SIOCSDRVSPEC32 _IOC_NEWTYPE(SIOCSDRVSPEC, struct ifdrv32)
173 #define SIOCGDRVSPEC32 _IOC_NEWTYPE(SIOCGDRVSPEC, struct ifdrv32)
174
175 struct ifgroupreq32 {
176 char ifgr_name[IFNAMSIZ];
177 u_int ifgr_len;
178 union {
179 char ifgru_group[IFNAMSIZ];
180 uint32_t ifgru_groups;
181 } ifgr_ifgru;
182 };
183 #define SIOCAIFGROUP32 _IOC_NEWTYPE(SIOCAIFGROUP, struct ifgroupreq32)
184 #define SIOCGIFGROUP32 _IOC_NEWTYPE(SIOCGIFGROUP, struct ifgroupreq32)
185 #define SIOCDIFGROUP32 _IOC_NEWTYPE(SIOCDIFGROUP, struct ifgroupreq32)
186 #define SIOCGIFGMEMB32 _IOC_NEWTYPE(SIOCGIFGMEMB, struct ifgroupreq32)
187
188 struct ifmediareq32 {
189 char ifm_name[IFNAMSIZ];
190 int ifm_current;
191 int ifm_mask;
192 int ifm_status;
193 int ifm_active;
194 int ifm_count;
195 uint32_t ifm_ulist; /* (int *) */
196 };
197 #define SIOCGIFMEDIA32 _IOC_NEWTYPE(SIOCGIFMEDIA, struct ifmediareq32)
198 #define SIOCGIFXMEDIA32 _IOC_NEWTYPE(SIOCGIFXMEDIA, struct ifmediareq32)
199 #endif /* COMPAT_FREEBSD32 */
200
201 union ifreq_union {
202 struct ifreq ifr;
203 #ifdef COMPAT_FREEBSD32
204 struct ifreq32 ifr32;
205 #endif
206 };
207
208 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
209 "Link layers");
210 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
211 "Generic link-management");
212
213 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN,
214 &ifqmaxlen, 0, "max send queue size");
215
216 /* Log link state change events */
217 static int log_link_state_change = 1;
218
219 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW,
220 &log_link_state_change, 0,
221 "log interface link state change events");
222
223 /* Log promiscuous mode change events */
224 static int log_promisc_mode_change = 1;
225
226 SYSCTL_INT(_net_link, OID_AUTO, log_promisc_mode_change, CTLFLAG_RDTUN,
227 &log_promisc_mode_change, 1,
228 "log promiscuous mode change events");
229
230 /* Interface description */
231 static unsigned int ifdescr_maxlen = 1024;
232 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW,
233 &ifdescr_maxlen, 0,
234 "administrative maximum length for interface description");
235
236 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions");
237
238 /* global sx for non-critical path ifdescr */
239 static struct sx ifdescr_sx;
240 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr");
241
242 void (*lagg_linkstate_p)(struct ifnet *ifp, int state);
243 /* These are external hooks for CARP. */
244 void (*carp_linkstate_p)(struct ifnet *ifp);
245 void (*carp_demote_adj_p)(int, char *);
246 int (*carp_master_p)(struct ifaddr *);
247 #if defined(INET) || defined(INET6)
248 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost);
249 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m,
250 const struct sockaddr *sa);
251 int (*carp_attach_p)(struct ifaddr *, int);
252 void (*carp_detach_p)(struct ifaddr *, bool);
253 #endif
254 #ifdef INET
255 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **);
256 #endif
257 #ifdef INET6
258 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6);
259 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m,
260 const struct in6_addr *taddr);
261 #endif
262
263 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL;
264
265 /*
266 * XXX: Style; these should be sorted alphabetically, and unprototyped
267 * static functions should be prototyped. Currently they are sorted by
268 * declaration order.
269 */
270 static int ifconf(u_long, caddr_t);
271 static void if_input_default(struct ifnet *, struct mbuf *);
272 static int if_requestencap_default(struct ifnet *, struct if_encap_req *);
273 static int if_setflag(struct ifnet *, int, int, int *, int);
274 static int if_transmit_default(struct ifnet *ifp, struct mbuf *m);
275 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int);
276 static void do_link_state_change(void *, int);
277 static int if_getgroup(struct ifgroupreq *, struct ifnet *);
278 static int if_getgroupmembers(struct ifgroupreq *);
279 static void if_delgroups(struct ifnet *);
280 static void if_attach_internal(struct ifnet *, bool);
281 static void if_detach_internal(struct ifnet *, bool);
282 static void if_siocaddmulti(void *, int);
283 static void if_link_ifnet(struct ifnet *);
284 static bool if_unlink_ifnet(struct ifnet *, bool);
285 #ifdef VIMAGE
286 static void if_vmove(struct ifnet *, struct vnet *);
287 #endif
288
289 #ifdef INET6
290 /*
291 * XXX: declare here to avoid to include many inet6 related files..
292 * should be more generalized?
293 */
294 extern void nd6_setmtu(struct ifnet *);
295 #endif
296
297 /* ipsec helper hooks */
298 VNET_DEFINE(struct hhook_head *, ipsec_hhh_in[HHOOK_IPSEC_COUNT]);
299 VNET_DEFINE(struct hhook_head *, ipsec_hhh_out[HHOOK_IPSEC_COUNT]);
300
301 int ifqmaxlen = IFQ_MAXLEN;
302 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */
303 VNET_DEFINE(struct ifgrouphead, ifg_head);
304
305 /* Table of ifnet by index. */
306 static int if_index;
307 static int if_indexlim = 8;
308 static struct ifindex_entry {
309 struct ifnet *ife_ifnet;
310 uint16_t ife_gencnt;
311 } *ifindex_table;
312
313 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
314 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
315 "Variables global to all interfaces");
316 static int
sysctl_ifcount(SYSCTL_HANDLER_ARGS)317 sysctl_ifcount(SYSCTL_HANDLER_ARGS)
318 {
319 int rv = 0;
320
321 IFNET_RLOCK();
322 for (int i = 1; i <= if_index; i++)
323 if (ifindex_table[i].ife_ifnet != NULL &&
324 ifindex_table[i].ife_ifnet->if_vnet == curvnet)
325 rv = i;
326 IFNET_RUNLOCK();
327
328 return (sysctl_handle_int(oidp, &rv, 0, req));
329 }
330 SYSCTL_PROC(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
331 CTLTYPE_INT | CTLFLAG_VNET | CTLFLAG_RD, NULL, 0, sysctl_ifcount, "I",
332 "Maximum known interface index");
333
334 /*
335 * The global network interface list (V_ifnet) and related state (such as
336 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock.
337 * This may be acquired to stabilise the list, or we may rely on NET_EPOCH.
338 */
339 struct sx ifnet_sxlock;
340 SX_SYSINIT_FLAGS(ifnet_sx, &ifnet_sxlock, "ifnet_sx", SX_RECURSE);
341
342 struct sx ifnet_detach_sxlock;
343 SX_SYSINIT_FLAGS(ifnet_detach, &ifnet_detach_sxlock, "ifnet_detach_sx",
344 SX_RECURSE);
345
346 static if_com_alloc_t *if_com_alloc[256];
347 static if_com_free_t *if_com_free[256];
348
349 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals");
350 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
351 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
352
353 struct ifnet *
ifnet_byindex(u_int idx)354 ifnet_byindex(u_int idx)
355 {
356 struct ifnet *ifp;
357
358 NET_EPOCH_ASSERT();
359
360 if (__predict_false(idx > if_index))
361 return (NULL);
362
363 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
364
365 if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet)
366 ifp = NULL;
367
368 return (ifp);
369 }
370
371 struct ifnet *
ifnet_byindex_ref(u_int idx)372 ifnet_byindex_ref(u_int idx)
373 {
374 struct ifnet *ifp;
375
376 ifp = ifnet_byindex(idx);
377 if (ifp == NULL || (ifp->if_flags & IFF_DYING))
378 return (NULL);
379 if (!if_try_ref(ifp))
380 return (NULL);
381 return (ifp);
382 }
383
384 struct ifnet *
ifnet_byindexgen(uint16_t idx,uint16_t gen)385 ifnet_byindexgen(uint16_t idx, uint16_t gen)
386 {
387 struct ifnet *ifp;
388
389 NET_EPOCH_ASSERT();
390
391 if (__predict_false(idx > if_index))
392 return (NULL);
393
394 ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet);
395
396 if (ifindex_table[idx].ife_gencnt == gen)
397 return (ifp);
398 else
399 return (NULL);
400 }
401
402 /*
403 * Network interface utility routines.
404 *
405 * Routines with ifa_ifwith* names take sockaddr *'s as
406 * parameters.
407 */
408
409 static void
if_init_idxtable(void * arg __unused)410 if_init_idxtable(void *arg __unused)
411 {
412
413 ifindex_table = malloc(if_indexlim * sizeof(*ifindex_table),
414 M_IFNET, M_WAITOK | M_ZERO);
415 }
416 SYSINIT(if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init_idxtable, NULL);
417
418 static void
vnet_if_init(const void * unused __unused)419 vnet_if_init(const void *unused __unused)
420 {
421
422 CK_STAILQ_INIT(&V_ifnet);
423 CK_STAILQ_INIT(&V_ifg_head);
424 }
425 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init,
426 NULL);
427
428 static void
if_link_ifnet(struct ifnet * ifp)429 if_link_ifnet(struct ifnet *ifp)
430 {
431 IFNET_WLOCK();
432
433 MPASS(refcount_load(&ifp->if_refcount) > 0);
434 MPASS(ifp->if_vnet == curvnet);
435 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
436
437 CK_STAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link);
438 #ifdef VIMAGE
439 curvnet->vnet_ifcnt++;
440 #endif
441 IFNET_WUNLOCK();
442 }
443
444 static bool
if_unlink_ifnet(struct ifnet * ifp,bool vmove)445 if_unlink_ifnet(struct ifnet *ifp, bool vmove)
446 {
447 struct ifnet *iter;
448 int found = 0;
449
450 IFNET_WLOCK();
451 CK_STAILQ_FOREACH(iter, &V_ifnet, if_link)
452 if (iter == ifp) {
453 MPASS(refcount_load(&ifp->if_refcount) > 0);
454 MPASS(ifp->if_vnet == curvnet);
455 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
456
457 CK_STAILQ_REMOVE(&V_ifnet, ifp, ifnet, if_link);
458 #ifdef VIMAGE
459 curvnet->vnet_ifcnt--;
460 #endif
461 if (!vmove)
462 ifp->if_flags |= IFF_DYING;
463 found = 1;
464 break;
465 }
466 IFNET_WUNLOCK();
467
468 return (found);
469 }
470
471 #ifdef VIMAGE
472 static void
vnet_if_return(const void * unused __unused)473 vnet_if_return(const void *unused __unused)
474 {
475 struct ifnet *ifp, *nifp;
476 struct ifnet **pending;
477 int found __diagused;
478 int i;
479
480 i = 0;
481
482 /*
483 * We need to protect our access to the V_ifnet tailq. Ordinarily we'd
484 * enter NET_EPOCH, but that's not possible, because if_vmove() calls
485 * if_detach_internal(), which waits for NET_EPOCH callbacks to
486 * complete. We can't do that from within NET_EPOCH.
487 *
488 * However, we can also use the IFNET_xLOCK, which is the V_ifnet
489 * read/write lock. We cannot hold the lock as we call if_vmove()
490 * though, as that presents LOR w.r.t ifnet_sx, in_multi_sx and iflib
491 * ctx lock.
492 */
493 IFNET_WLOCK();
494
495 pending = malloc(sizeof(struct ifnet *) * curvnet->vnet_ifcnt,
496 M_IFNET, M_WAITOK | M_ZERO);
497
498 /* Return all inherited interfaces to their parent vnets. */
499 CK_STAILQ_FOREACH_SAFE(ifp, &V_ifnet, if_link, nifp) {
500 if (ifp->if_home_vnet != ifp->if_vnet) {
501 found = if_unlink_ifnet(ifp, true);
502 MPASS(found);
503
504 pending[i++] = ifp;
505 }
506 }
507 IFNET_WUNLOCK();
508
509 for (int j = 0; j < i; j++) {
510 sx_xlock(&ifnet_detach_sxlock);
511 if_vmove(pending[j], pending[j]->if_home_vnet);
512 sx_xunlock(&ifnet_detach_sxlock);
513 }
514
515 free(pending, M_IFNET);
516 }
517 VNET_SYSUNINIT(vnet_if_return, SI_SUB_VNET_DONE, SI_ORDER_ANY,
518 vnet_if_return, NULL);
519 #endif
520
521 /*
522 * Allocate a struct ifnet and an index for an interface. A layer 2
523 * common structure will also be allocated if an allocation routine is
524 * registered for the passed type.
525 */
526 static struct ifnet *
if_alloc_domain(u_char type,int numa_domain)527 if_alloc_domain(u_char type, int numa_domain)
528 {
529 struct ifnet *ifp;
530 u_short idx;
531
532 KASSERT(numa_domain <= IF_NODOM, ("numa_domain too large"));
533 if (numa_domain == IF_NODOM)
534 ifp = malloc(sizeof(struct ifnet), M_IFNET,
535 M_WAITOK | M_ZERO);
536 else
537 ifp = malloc_domainset(sizeof(struct ifnet), M_IFNET,
538 DOMAINSET_PREF(numa_domain), M_WAITOK | M_ZERO);
539 ifp->if_type = type;
540 ifp->if_alloctype = type;
541 ifp->if_numa_domain = numa_domain;
542 #ifdef VIMAGE
543 ifp->if_vnet = curvnet;
544 #endif
545 if (if_com_alloc[type] != NULL) {
546 ifp->if_l2com = if_com_alloc[type](type, ifp);
547 KASSERT(ifp->if_l2com, ("%s: if_com_alloc[%u] failed", __func__,
548 type));
549 }
550
551 IF_ADDR_LOCK_INIT(ifp);
552 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp);
553 TASK_INIT(&ifp->if_addmultitask, 0, if_siocaddmulti, ifp);
554 CK_STAILQ_INIT(&ifp->if_addrhead);
555 CK_STAILQ_INIT(&ifp->if_multiaddrs);
556 CK_STAILQ_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
568 /* Allocate an ifindex array entry. */
569 IFNET_WLOCK();
570 /*
571 * Try to find an empty slot below if_index. If we fail, take the
572 * next slot.
573 */
574 for (idx = 1; idx <= if_index; idx++) {
575 if (ifindex_table[idx].ife_ifnet == NULL)
576 break;
577 }
578
579 /* Catch if_index overflow. */
580 if (idx >= if_indexlim) {
581 struct ifindex_entry *new, *old;
582 int newlim;
583
584 newlim = if_indexlim * 2;
585 new = malloc(newlim * sizeof(*new), M_IFNET, M_WAITOK | M_ZERO);
586 memcpy(new, ifindex_table, if_indexlim * sizeof(*new));
587 old = ifindex_table;
588 ck_pr_store_ptr(&ifindex_table, new);
589 if_indexlim = newlim;
590 NET_EPOCH_WAIT();
591 free(old, M_IFNET);
592 }
593 if (idx > if_index)
594 if_index = idx;
595
596 ifp->if_index = idx;
597 ifp->if_idxgen = ifindex_table[idx].ife_gencnt;
598 ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp);
599 IFNET_WUNLOCK();
600
601 return (ifp);
602 }
603
604 struct ifnet *
if_alloc_dev(u_char type,device_t dev)605 if_alloc_dev(u_char type, device_t dev)
606 {
607 int numa_domain;
608
609 if (dev == NULL || bus_get_domain(dev, &numa_domain) != 0)
610 return (if_alloc_domain(type, IF_NODOM));
611 return (if_alloc_domain(type, numa_domain));
612 }
613
614 struct ifnet *
if_alloc(u_char type)615 if_alloc(u_char type)
616 {
617
618 return (if_alloc_domain(type, IF_NODOM));
619 }
620 /*
621 * Do the actual work of freeing a struct ifnet, and layer 2 common
622 * structure. This call is made when the network epoch guarantees
623 * us that nobody holds a pointer to the interface.
624 */
625 static void
if_free_deferred(epoch_context_t ctx)626 if_free_deferred(epoch_context_t ctx)
627 {
628 struct ifnet *ifp = __containerof(ctx, struct ifnet, if_epoch_ctx);
629
630 KASSERT((ifp->if_flags & IFF_DYING),
631 ("%s: interface not dying", __func__));
632
633 if (if_com_free[ifp->if_alloctype] != NULL)
634 if_com_free[ifp->if_alloctype](ifp->if_l2com,
635 ifp->if_alloctype);
636
637 #ifdef MAC
638 mac_ifnet_destroy(ifp);
639 #endif /* MAC */
640 IF_ADDR_LOCK_DESTROY(ifp);
641 ifq_delete(&ifp->if_snd);
642
643 for (int i = 0; i < IFCOUNTERS; i++)
644 counter_u64_free(ifp->if_counters[i]);
645
646 if_freedescr(ifp->if_description);
647 free(ifp->if_hw_addr, M_IFADDR);
648 free(ifp, M_IFNET);
649 }
650
651 /*
652 * Deregister an interface and free the associated storage.
653 */
654 void
if_free(struct ifnet * ifp)655 if_free(struct ifnet *ifp)
656 {
657
658 ifp->if_flags |= IFF_DYING; /* XXX: Locking */
659
660 /*
661 * XXXGL: An interface index is really an alias to ifp pointer.
662 * Why would we clear the alias now, and not in the deferred
663 * context? Indeed there is nothing wrong with some network
664 * thread obtaining ifp via ifnet_byindex() inside the network
665 * epoch and then dereferencing ifp while we perform if_free(),
666 * and after if_free() finished, too.
667 *
668 * This early index freeing was important back when ifindex was
669 * virtualized and interface would outlive the vnet.
670 */
671 IFNET_WLOCK();
672 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
673 ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL);
674 ifindex_table[ifp->if_index].ife_gencnt++;
675 while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL)
676 if_index--;
677 IFNET_WUNLOCK();
678
679 if (refcount_release(&ifp->if_refcount))
680 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
681 }
682
683 /*
684 * Interfaces to keep an ifnet type-stable despite the possibility of the
685 * driver calling if_free(). If there are additional references, we defer
686 * freeing the underlying data structure.
687 */
688 void
if_ref(struct ifnet * ifp)689 if_ref(struct ifnet *ifp)
690 {
691 u_int old __diagused;
692
693 /* We don't assert the ifnet list lock here, but arguably should. */
694 old = refcount_acquire(&ifp->if_refcount);
695 KASSERT(old > 0, ("%s: ifp %p has 0 refs", __func__, ifp));
696 }
697
698 bool
if_try_ref(struct ifnet * ifp)699 if_try_ref(struct ifnet *ifp)
700 {
701 NET_EPOCH_ASSERT();
702 return (refcount_acquire_if_not_zero(&ifp->if_refcount));
703 }
704
705 void
if_rele(struct ifnet * ifp)706 if_rele(struct ifnet *ifp)
707 {
708
709 if (!refcount_release(&ifp->if_refcount))
710 return;
711 NET_EPOCH_CALL(if_free_deferred, &ifp->if_epoch_ctx);
712 }
713
714 void
ifq_init(struct ifaltq * ifq,struct ifnet * ifp)715 ifq_init(struct ifaltq *ifq, struct ifnet *ifp)
716 {
717
718 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
719
720 if (ifq->ifq_maxlen == 0)
721 ifq->ifq_maxlen = ifqmaxlen;
722
723 ifq->altq_type = 0;
724 ifq->altq_disc = NULL;
725 ifq->altq_flags &= ALTQF_CANTCHANGE;
726 ifq->altq_tbr = NULL;
727 ifq->altq_ifp = ifp;
728 }
729
730 void
ifq_delete(struct ifaltq * ifq)731 ifq_delete(struct ifaltq *ifq)
732 {
733 mtx_destroy(&ifq->ifq_mtx);
734 }
735
736 /*
737 * Perform generic interface initialization tasks and attach the interface
738 * to the list of "active" interfaces. If vmove flag is set on entry
739 * to if_attach_internal(), perform only a limited subset of initialization
740 * tasks, given that we are moving from one vnet to another an ifnet which
741 * has already been fully initialized.
742 *
743 * Note that if_detach_internal() removes group membership unconditionally
744 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL.
745 * Thus, when if_vmove() is applied to a cloned interface, group membership
746 * is lost while a cloned one always joins a group whose name is
747 * ifc->ifc_name. To recover this after if_detach_internal() and
748 * if_attach_internal(), the cloner should be specified to
749 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal()
750 * attempts to join a group whose name is ifc->ifc_name.
751 *
752 * XXX:
753 * - The decision to return void and thus require this function to
754 * succeed is questionable.
755 * - We should probably do more sanity checking. For instance we don't
756 * do anything to insure if_xname is unique or non-empty.
757 */
758 void
if_attach(struct ifnet * ifp)759 if_attach(struct ifnet *ifp)
760 {
761
762 if_attach_internal(ifp, false);
763 }
764
765 /*
766 * Compute the least common TSO limit.
767 */
768 void
if_hw_tsomax_common(if_t ifp,struct ifnet_hw_tsomax * pmax)769 if_hw_tsomax_common(if_t ifp, struct ifnet_hw_tsomax *pmax)
770 {
771 /*
772 * 1) If there is no limit currently, take the limit from
773 * the network adapter.
774 *
775 * 2) If the network adapter has a limit below the current
776 * limit, apply it.
777 */
778 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 &&
779 ifp->if_hw_tsomax < pmax->tsomaxbytes)) {
780 pmax->tsomaxbytes = ifp->if_hw_tsomax;
781 }
782 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 &&
783 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) {
784 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount;
785 }
786 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 &&
787 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) {
788 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize;
789 }
790 }
791
792 /*
793 * Update TSO limit of a network adapter.
794 *
795 * Returns zero if no change. Else non-zero.
796 */
797 int
if_hw_tsomax_update(if_t ifp,struct ifnet_hw_tsomax * pmax)798 if_hw_tsomax_update(if_t ifp, struct ifnet_hw_tsomax *pmax)
799 {
800 int retval = 0;
801 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) {
802 ifp->if_hw_tsomax = pmax->tsomaxbytes;
803 retval++;
804 }
805 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) {
806 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize;
807 retval++;
808 }
809 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) {
810 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount;
811 retval++;
812 }
813 return (retval);
814 }
815
816 static void
if_attach_internal(struct ifnet * ifp,bool vmove)817 if_attach_internal(struct ifnet *ifp, bool vmove)
818 {
819 unsigned socksize, ifasize;
820 int namelen, masklen;
821 struct sockaddr_dl *sdl;
822 struct ifaddr *ifa;
823
824 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
825
826 #ifdef VIMAGE
827 CURVNET_ASSERT_SET();
828 ifp->if_vnet = curvnet;
829 if (ifp->if_home_vnet == NULL)
830 ifp->if_home_vnet = curvnet;
831 #endif
832
833 if_addgroup(ifp, IFG_ALL);
834
835 #ifdef VIMAGE
836 /* Restore group membership for cloned interface. */
837 if (vmove)
838 if_clone_restoregroup(ifp);
839 #endif
840
841 getmicrotime(&ifp->if_lastchange);
842 ifp->if_epoch = time_uptime;
843
844 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) ||
845 (ifp->if_transmit != NULL && ifp->if_qflush != NULL),
846 ("transmit and qflush must both either be set or both be NULL"));
847 if (ifp->if_transmit == NULL) {
848 ifp->if_transmit = if_transmit_default;
849 ifp->if_qflush = if_qflush;
850 }
851 if (ifp->if_input == NULL)
852 ifp->if_input = if_input_default;
853
854 if (ifp->if_requestencap == NULL)
855 ifp->if_requestencap = if_requestencap_default;
856
857 if (!vmove) {
858 #ifdef MAC
859 mac_ifnet_create(ifp);
860 #endif
861
862 /*
863 * Create a Link Level name for this device.
864 */
865 namelen = strlen(ifp->if_xname);
866 /*
867 * Always save enough space for any possible name so we
868 * can do a rename in place later.
869 */
870 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
871 socksize = masklen + ifp->if_addrlen;
872 if (socksize < sizeof(*sdl))
873 socksize = sizeof(*sdl);
874 socksize = roundup2(socksize, sizeof(long));
875 ifasize = sizeof(*ifa) + 2 * socksize;
876 ifa = ifa_alloc(ifasize, M_WAITOK);
877 sdl = (struct sockaddr_dl *)(ifa + 1);
878 sdl->sdl_len = socksize;
879 sdl->sdl_family = AF_LINK;
880 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
881 sdl->sdl_nlen = namelen;
882 sdl->sdl_index = ifp->if_index;
883 sdl->sdl_type = ifp->if_type;
884 ifp->if_addr = ifa;
885 ifa->ifa_ifp = ifp;
886 ifa->ifa_addr = (struct sockaddr *)sdl;
887 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
888 ifa->ifa_netmask = (struct sockaddr *)sdl;
889 sdl->sdl_len = masklen;
890 while (namelen != 0)
891 sdl->sdl_data[--namelen] = 0xff;
892 CK_STAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
893 /* Reliably crash if used uninitialized. */
894 ifp->if_broadcastaddr = NULL;
895
896 if (ifp->if_type == IFT_ETHER) {
897 ifp->if_hw_addr = malloc(ifp->if_addrlen, M_IFADDR,
898 M_WAITOK | M_ZERO);
899 }
900
901 #if defined(INET) || defined(INET6)
902 /* Use defaults for TSO, if nothing is set */
903 if (ifp->if_hw_tsomax == 0 &&
904 ifp->if_hw_tsomaxsegcount == 0 &&
905 ifp->if_hw_tsomaxsegsize == 0) {
906 /*
907 * The TSO defaults needs to be such that an
908 * NFS mbuf list of 35 mbufs totalling just
909 * below 64K works and that a chain of mbufs
910 * can be defragged into at most 32 segments:
911 */
912 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) -
913 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
914 ifp->if_hw_tsomaxsegcount = 35;
915 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */
916
917 /* XXX some drivers set IFCAP_TSO after ethernet attach */
918 if (ifp->if_capabilities & IFCAP_TSO) {
919 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n",
920 ifp->if_hw_tsomax,
921 ifp->if_hw_tsomaxsegcount,
922 ifp->if_hw_tsomaxsegsize);
923 }
924 }
925 #endif
926 }
927
928 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
929 if_link_ifnet(ifp);
930 EVENTHANDLER_INVOKE(ifnet_attached_event, ifp);
931 if (IS_DEFAULT_VNET(curvnet))
932 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
933 }
934
935 static void
if_epochalloc(void * dummy __unused)936 if_epochalloc(void *dummy __unused)
937 {
938
939 net_epoch_preempt = epoch_alloc("Net preemptible", EPOCH_PREEMPT);
940 }
941 SYSINIT(ifepochalloc, SI_SUB_EPOCH, SI_ORDER_ANY, if_epochalloc, NULL);
942
943 /*
944 * Remove any unicast or broadcast network addresses from an interface.
945 */
946 void
if_purgeaddrs(struct ifnet * ifp)947 if_purgeaddrs(struct ifnet *ifp)
948 {
949 struct ifaddr *ifa;
950
951 #ifdef INET6
952 /*
953 * Need to leave multicast addresses of proxy NDP llentries
954 * before in6_purgeifaddr() because the llentries are keys
955 * for in6_multi objects of proxy NDP entries.
956 * in6_purgeifaddr()s clean up llentries including proxy NDPs
957 * then we would lose the keys if they are called earlier.
958 */
959 in6_purge_proxy_ndp(ifp);
960 #endif
961 while (1) {
962 struct epoch_tracker et;
963
964 NET_EPOCH_ENTER(et);
965 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
966 if (ifa->ifa_addr->sa_family != AF_LINK)
967 break;
968 }
969 NET_EPOCH_EXIT(et);
970
971 if (ifa == NULL)
972 break;
973 #ifdef INET
974 /* XXX: Ugly!! ad hoc just for INET */
975 if (ifa->ifa_addr->sa_family == AF_INET) {
976 struct ifreq ifr;
977
978 bzero(&ifr, sizeof(ifr));
979 ifr.ifr_addr = *ifa->ifa_addr;
980 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
981 NULL) == 0)
982 continue;
983 }
984 #endif /* INET */
985 #ifdef INET6
986 if (ifa->ifa_addr->sa_family == AF_INET6) {
987 in6_purgeifaddr((struct in6_ifaddr *)ifa);
988 /* ifp_addrhead is already updated */
989 continue;
990 }
991 #endif /* INET6 */
992 IF_ADDR_WLOCK(ifp);
993 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
994 IF_ADDR_WUNLOCK(ifp);
995 ifa_free(ifa);
996 }
997 }
998
999 /*
1000 * Remove any multicast network addresses from an interface when an ifnet
1001 * is going away.
1002 */
1003 static void
if_purgemaddrs(struct ifnet * ifp)1004 if_purgemaddrs(struct ifnet *ifp)
1005 {
1006 struct ifmultiaddr *ifma;
1007
1008 IF_ADDR_WLOCK(ifp);
1009 while (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
1010 ifma = CK_STAILQ_FIRST(&ifp->if_multiaddrs);
1011 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
1012 if_delmulti_locked(ifp, ifma, 1);
1013 }
1014 IF_ADDR_WUNLOCK(ifp);
1015 }
1016
1017 /*
1018 * Detach an interface, removing it from the list of "active" interfaces.
1019 * If vmove flag is set on entry to if_detach_internal(), perform only a
1020 * limited subset of cleanup tasks, given that we are moving an ifnet from
1021 * one vnet to another, where it must be fully operational.
1022 *
1023 * XXXRW: There are some significant questions about event ordering, and
1024 * how to prevent things from starting to use the interface during detach.
1025 */
1026 void
if_detach(struct ifnet * ifp)1027 if_detach(struct ifnet *ifp)
1028 {
1029 bool found;
1030
1031 CURVNET_SET_QUIET(ifp->if_vnet);
1032 found = if_unlink_ifnet(ifp, false);
1033 if (found) {
1034 sx_xlock(&ifnet_detach_sxlock);
1035 if_detach_internal(ifp, false);
1036 sx_xunlock(&ifnet_detach_sxlock);
1037 }
1038 CURVNET_RESTORE();
1039 }
1040
1041 /*
1042 * The vmove flag, if set, indicates that we are called from a callpath
1043 * that is moving an interface to a different vnet instance.
1044 *
1045 * The shutdown flag, if set, indicates that we are called in the
1046 * process of shutting down a vnet instance. Currently only the
1047 * vnet_if_return SYSUNINIT function sets it. Note: we can be called
1048 * on a vnet instance shutdown without this flag being set, e.g., when
1049 * the cloned interfaces are destoyed as first thing of teardown.
1050 */
1051 static void
if_detach_internal(struct ifnet * ifp,bool vmove)1052 if_detach_internal(struct ifnet *ifp, bool vmove)
1053 {
1054 struct ifaddr *ifa;
1055 #ifdef VIMAGE
1056 bool shutdown;
1057
1058 shutdown = VNET_IS_SHUTTING_DOWN(ifp->if_vnet);
1059 #endif
1060
1061 sx_assert(&ifnet_detach_sxlock, SX_XLOCKED);
1062
1063 /*
1064 * At this point we know the interface still was on the ifnet list
1065 * and we removed it so we are in a stable state.
1066 */
1067 NET_EPOCH_WAIT();
1068
1069 /*
1070 * Ensure all pending EPOCH(9) callbacks have been executed. This
1071 * fixes issues about late destruction of multicast options
1072 * which lead to leave group calls, which in turn access the
1073 * belonging ifnet structure:
1074 */
1075 NET_EPOCH_DRAIN_CALLBACKS();
1076
1077 /*
1078 * In any case (destroy or vmove) detach us from the groups
1079 * and remove/wait for pending events on the taskq.
1080 * XXX-BZ in theory an interface could still enqueue a taskq change?
1081 */
1082 if_delgroups(ifp);
1083
1084 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
1085 taskqueue_drain(taskqueue_swi, &ifp->if_addmultitask);
1086
1087 if_down(ifp);
1088
1089 #ifdef VIMAGE
1090 /*
1091 * On VNET shutdown abort here as the stack teardown will do all
1092 * the work top-down for us.
1093 */
1094 if (shutdown) {
1095 /* Give interface users the chance to clean up. */
1096 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1097
1098 /*
1099 * In case of a vmove we are done here without error.
1100 * If we would signal an error it would lead to the same
1101 * abort as if we did not find the ifnet anymore.
1102 * if_detach() calls us in void context and does not care
1103 * about an early abort notification, so life is splendid :)
1104 */
1105 return;
1106 }
1107 #endif
1108
1109 /*
1110 * At this point we are not tearing down a VNET and are either
1111 * going to destroy or vmove the interface and have to cleanup
1112 * accordingly.
1113 */
1114
1115 /*
1116 * Remove routes and flush queues.
1117 */
1118 #ifdef ALTQ
1119 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1120 altq_disable(&ifp->if_snd);
1121 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1122 altq_detach(&ifp->if_snd);
1123 #endif
1124
1125 rt_flushifroutes(ifp);
1126
1127 if_purgeaddrs(ifp);
1128 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1129 if_purgemaddrs(ifp);
1130 if (IS_DEFAULT_VNET(curvnet))
1131 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
1132
1133 if (!vmove) {
1134 /*
1135 * Prevent further calls into the device driver via ifnet.
1136 */
1137 if_dead(ifp);
1138
1139 /*
1140 * Clean up all addresses.
1141 */
1142 IF_ADDR_WLOCK(ifp);
1143 if (!CK_STAILQ_EMPTY(&ifp->if_addrhead)) {
1144 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
1145 CK_STAILQ_REMOVE(&ifp->if_addrhead, ifa, ifaddr, ifa_link);
1146 IF_ADDR_WUNLOCK(ifp);
1147 ifa_free(ifa);
1148 } else
1149 IF_ADDR_WUNLOCK(ifp);
1150 }
1151 }
1152
1153 #ifdef VIMAGE
1154 /*
1155 * if_vmove() performs a limited version of if_detach() in current
1156 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg.
1157 */
1158 static void
if_vmove(struct ifnet * ifp,struct vnet * new_vnet)1159 if_vmove(struct ifnet *ifp, struct vnet *new_vnet)
1160 {
1161 #ifdef DEV_BPF
1162 /*
1163 * Detach BPF file descriptors from its interface.
1164 */
1165 bpf_ifdetach(ifp);
1166 #endif
1167
1168 /*
1169 * Detach from current vnet, but preserve LLADDR info, do not
1170 * mark as dead etc. so that the ifnet can be reattached later.
1171 */
1172 if_detach_internal(ifp, true);
1173
1174 /*
1175 * Perform interface-specific reassignment tasks, if provided by
1176 * the driver.
1177 */
1178 if (ifp->if_reassign != NULL)
1179 ifp->if_reassign(ifp, new_vnet, NULL);
1180
1181 /*
1182 * Switch to the context of the target vnet.
1183 */
1184 CURVNET_SET_QUIET(new_vnet);
1185 if_attach_internal(ifp, true);
1186 bpf_vmove(ifp->if_bpf);
1187 CURVNET_RESTORE();
1188 }
1189
1190 /*
1191 * Move an ifnet to or from another child prison/vnet, specified by the jail id.
1192 */
1193 static int
if_vmove_loan(struct thread * td,struct ifnet * ifp,char * ifname,int jid)1194 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid)
1195 {
1196 struct prison *pr;
1197 struct ifnet *difp;
1198 bool found;
1199
1200 /* Try to find the prison within our visibility. */
1201 sx_slock(&allprison_lock);
1202 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1203 sx_sunlock(&allprison_lock);
1204 if (pr == NULL)
1205 return (ENXIO);
1206 prison_hold_locked(pr);
1207 mtx_unlock(&pr->pr_mtx);
1208
1209 /* Do not try to move the iface from and to the same prison. */
1210 if (pr->pr_vnet == ifp->if_vnet) {
1211 prison_free(pr);
1212 return (EEXIST);
1213 }
1214
1215 /* Make sure the named iface does not exists in the dst. prison/vnet. */
1216 /* XXX Lock interfaces to avoid races. */
1217 CURVNET_SET_QUIET(pr->pr_vnet);
1218 difp = ifunit(ifname);
1219 CURVNET_RESTORE();
1220 if (difp != NULL) {
1221 prison_free(pr);
1222 return (EEXIST);
1223 }
1224 sx_xlock(&ifnet_detach_sxlock);
1225
1226 found = if_unlink_ifnet(ifp, true);
1227 if (! found) {
1228 sx_xunlock(&ifnet_detach_sxlock);
1229 prison_free(pr);
1230 return (ENODEV);
1231 }
1232
1233 /* Move the interface into the child jail/vnet. */
1234 if_vmove(ifp, pr->pr_vnet);
1235
1236 /* Report the new if_xname back to the userland. */
1237 sprintf(ifname, "%s", ifp->if_xname);
1238
1239 sx_xunlock(&ifnet_detach_sxlock);
1240
1241 prison_free(pr);
1242 return (0);
1243 }
1244
1245 static int
if_vmove_reclaim(struct thread * td,char * ifname,int jid)1246 if_vmove_reclaim(struct thread *td, char *ifname, int jid)
1247 {
1248 struct prison *pr;
1249 struct vnet *vnet_dst;
1250 struct ifnet *ifp;
1251 int found;
1252
1253 /* Try to find the prison within our visibility. */
1254 sx_slock(&allprison_lock);
1255 pr = prison_find_child(td->td_ucred->cr_prison, jid);
1256 sx_sunlock(&allprison_lock);
1257 if (pr == NULL)
1258 return (ENXIO);
1259 prison_hold_locked(pr);
1260 mtx_unlock(&pr->pr_mtx);
1261
1262 /* Make sure the named iface exists in the source prison/vnet. */
1263 CURVNET_SET(pr->pr_vnet);
1264 ifp = ifunit(ifname);
1265 if (ifp == NULL) {
1266 CURVNET_RESTORE();
1267 prison_free(pr);
1268 return (ENXIO);
1269 }
1270
1271 /* Do not try to move the iface from and to the same vnet. */
1272 vnet_dst = TD_TO_VNET(td);
1273 if (vnet_dst == pr->pr_vnet) {
1274 CURVNET_RESTORE();
1275 prison_free(pr);
1276 return (EEXIST);
1277 }
1278
1279 /* Get interface back from child jail/vnet. */
1280 found = if_unlink_ifnet(ifp, true);
1281 if (! found) {
1282 CURVNET_RESTORE();
1283 prison_free(pr);
1284 return (ENODEV);
1285 }
1286 sx_xlock(&ifnet_detach_sxlock);
1287 if_vmove(ifp, vnet_dst);
1288 sx_xunlock(&ifnet_detach_sxlock);
1289 CURVNET_RESTORE();
1290
1291 /* Report the new if_xname back to the userland. */
1292 sprintf(ifname, "%s", ifp->if_xname);
1293
1294 prison_free(pr);
1295 return (0);
1296 }
1297 #endif /* VIMAGE */
1298
1299 /*
1300 * Add a group to an interface
1301 */
1302 int
if_addgroup(struct ifnet * ifp,const char * groupname)1303 if_addgroup(struct ifnet *ifp, const char *groupname)
1304 {
1305 struct ifg_list *ifgl;
1306 struct ifg_group *ifg = NULL;
1307 struct ifg_member *ifgm;
1308 int new = 0;
1309
1310 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1311 groupname[strlen(groupname) - 1] <= '9')
1312 return (EINVAL);
1313
1314 IFNET_WLOCK();
1315 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1316 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) {
1317 IFNET_WUNLOCK();
1318 return (EEXIST);
1319 }
1320
1321 if ((ifgl = malloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL) {
1322 IFNET_WUNLOCK();
1323 return (ENOMEM);
1324 }
1325
1326 if ((ifgm = malloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1327 free(ifgl, M_TEMP);
1328 IFNET_WUNLOCK();
1329 return (ENOMEM);
1330 }
1331
1332 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1333 if (!strcmp(ifg->ifg_group, groupname))
1334 break;
1335
1336 if (ifg == NULL) {
1337 if ((ifg = malloc(sizeof(*ifg), M_TEMP, M_NOWAIT)) == NULL) {
1338 free(ifgl, M_TEMP);
1339 free(ifgm, M_TEMP);
1340 IFNET_WUNLOCK();
1341 return (ENOMEM);
1342 }
1343 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1344 ifg->ifg_refcnt = 0;
1345 CK_STAILQ_INIT(&ifg->ifg_members);
1346 CK_STAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next);
1347 new = 1;
1348 }
1349
1350 ifg->ifg_refcnt++;
1351 ifgl->ifgl_group = ifg;
1352 ifgm->ifgm_ifp = ifp;
1353
1354 IF_ADDR_WLOCK(ifp);
1355 CK_STAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1356 CK_STAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1357 IF_ADDR_WUNLOCK(ifp);
1358
1359 IFNET_WUNLOCK();
1360
1361 if (new)
1362 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1363 EVENTHANDLER_INVOKE(group_change_event, groupname);
1364
1365 return (0);
1366 }
1367
1368 /*
1369 * Helper function to remove a group out of an interface. Expects the global
1370 * ifnet lock to be write-locked, and drops it before returning.
1371 */
1372 static void
_if_delgroup_locked(struct ifnet * ifp,struct ifg_list * ifgl,const char * groupname)1373 _if_delgroup_locked(struct ifnet *ifp, struct ifg_list *ifgl,
1374 const char *groupname)
1375 {
1376 struct ifg_member *ifgm;
1377 bool freeifgl;
1378
1379 IFNET_WLOCK_ASSERT();
1380
1381 IF_ADDR_WLOCK(ifp);
1382 CK_STAILQ_REMOVE(&ifp->if_groups, ifgl, ifg_list, ifgl_next);
1383 IF_ADDR_WUNLOCK(ifp);
1384
1385 CK_STAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1386 if (ifgm->ifgm_ifp == ifp) {
1387 CK_STAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm,
1388 ifg_member, ifgm_next);
1389 break;
1390 }
1391 }
1392
1393 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1394 CK_STAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_group,
1395 ifg_next);
1396 freeifgl = true;
1397 } else {
1398 freeifgl = false;
1399 }
1400 IFNET_WUNLOCK();
1401
1402 NET_EPOCH_WAIT();
1403 EVENTHANDLER_INVOKE(group_change_event, groupname);
1404 if (freeifgl) {
1405 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group);
1406 free(ifgl->ifgl_group, M_TEMP);
1407 }
1408 free(ifgm, M_TEMP);
1409 free(ifgl, M_TEMP);
1410 }
1411
1412 /*
1413 * Remove a group from an interface
1414 */
1415 int
if_delgroup(struct ifnet * ifp,const char * groupname)1416 if_delgroup(struct ifnet *ifp, const char *groupname)
1417 {
1418 struct ifg_list *ifgl;
1419
1420 IFNET_WLOCK();
1421 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1422 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1423 break;
1424 if (ifgl == NULL) {
1425 IFNET_WUNLOCK();
1426 return (ENOENT);
1427 }
1428
1429 _if_delgroup_locked(ifp, ifgl, groupname);
1430
1431 return (0);
1432 }
1433
1434 /*
1435 * Remove an interface from all groups
1436 */
1437 static void
if_delgroups(struct ifnet * ifp)1438 if_delgroups(struct ifnet *ifp)
1439 {
1440 struct ifg_list *ifgl;
1441 char groupname[IFNAMSIZ];
1442
1443 IFNET_WLOCK();
1444 while ((ifgl = CK_STAILQ_FIRST(&ifp->if_groups)) != NULL) {
1445 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ);
1446 _if_delgroup_locked(ifp, ifgl, groupname);
1447 IFNET_WLOCK();
1448 }
1449 IFNET_WUNLOCK();
1450 }
1451
1452 /*
1453 * Stores all groups from an interface in memory pointed to by ifgr.
1454 */
1455 static int
if_getgroup(struct ifgroupreq * ifgr,struct ifnet * ifp)1456 if_getgroup(struct ifgroupreq *ifgr, struct ifnet *ifp)
1457 {
1458 int len, error;
1459 struct ifg_list *ifgl;
1460 struct ifg_req ifgrq, *ifgp;
1461
1462 NET_EPOCH_ASSERT();
1463
1464 if (ifgr->ifgr_len == 0) {
1465 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1466 ifgr->ifgr_len += sizeof(struct ifg_req);
1467 return (0);
1468 }
1469
1470 len = ifgr->ifgr_len;
1471 ifgp = ifgr->ifgr_groups;
1472 /* XXX: wire */
1473 CK_STAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1474 if (len < sizeof(ifgrq))
1475 return (EINVAL);
1476 bzero(&ifgrq, sizeof ifgrq);
1477 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1478 sizeof(ifgrq.ifgrq_group));
1479 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req))))
1480 return (error);
1481 len -= sizeof(ifgrq);
1482 ifgp++;
1483 }
1484
1485 return (0);
1486 }
1487
1488 /*
1489 * Stores all members of a group in memory pointed to by igfr
1490 */
1491 static int
if_getgroupmembers(struct ifgroupreq * ifgr)1492 if_getgroupmembers(struct ifgroupreq *ifgr)
1493 {
1494 struct ifg_group *ifg;
1495 struct ifg_member *ifgm;
1496 struct ifg_req ifgrq, *ifgp;
1497 int len, error;
1498
1499 IFNET_RLOCK();
1500 CK_STAILQ_FOREACH(ifg, &V_ifg_head, ifg_next)
1501 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1502 break;
1503 if (ifg == NULL) {
1504 IFNET_RUNLOCK();
1505 return (ENOENT);
1506 }
1507
1508 if (ifgr->ifgr_len == 0) {
1509 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1510 ifgr->ifgr_len += sizeof(ifgrq);
1511 IFNET_RUNLOCK();
1512 return (0);
1513 }
1514
1515 len = ifgr->ifgr_len;
1516 ifgp = ifgr->ifgr_groups;
1517 CK_STAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1518 if (len < sizeof(ifgrq)) {
1519 IFNET_RUNLOCK();
1520 return (EINVAL);
1521 }
1522 bzero(&ifgrq, sizeof ifgrq);
1523 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1524 sizeof(ifgrq.ifgrq_member));
1525 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) {
1526 IFNET_RUNLOCK();
1527 return (error);
1528 }
1529 len -= sizeof(ifgrq);
1530 ifgp++;
1531 }
1532 IFNET_RUNLOCK();
1533
1534 return (0);
1535 }
1536
1537 /*
1538 * Return counter values from counter(9)s stored in ifnet.
1539 */
1540 uint64_t
if_get_counter_default(struct ifnet * ifp,ift_counter cnt)1541 if_get_counter_default(struct ifnet *ifp, ift_counter cnt)
1542 {
1543
1544 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1545
1546 return (counter_u64_fetch(ifp->if_counters[cnt]));
1547 }
1548
1549 /*
1550 * Increase an ifnet counter. Usually used for counters shared
1551 * between the stack and a driver, but function supports them all.
1552 */
1553 void
if_inc_counter(struct ifnet * ifp,ift_counter cnt,int64_t inc)1554 if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc)
1555 {
1556
1557 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt));
1558
1559 counter_u64_add(ifp->if_counters[cnt], inc);
1560 }
1561
1562 /*
1563 * Copy data from ifnet to userland API structure if_data.
1564 */
1565 void
if_data_copy(struct ifnet * ifp,struct if_data * ifd)1566 if_data_copy(struct ifnet *ifp, struct if_data *ifd)
1567 {
1568
1569 ifd->ifi_type = ifp->if_type;
1570 ifd->ifi_physical = 0;
1571 ifd->ifi_addrlen = ifp->if_addrlen;
1572 ifd->ifi_hdrlen = ifp->if_hdrlen;
1573 ifd->ifi_link_state = ifp->if_link_state;
1574 ifd->ifi_vhid = 0;
1575 ifd->ifi_datalen = sizeof(struct if_data);
1576 ifd->ifi_mtu = ifp->if_mtu;
1577 ifd->ifi_metric = ifp->if_metric;
1578 ifd->ifi_baudrate = ifp->if_baudrate;
1579 ifd->ifi_hwassist = ifp->if_hwassist;
1580 ifd->ifi_epoch = ifp->if_epoch;
1581 ifd->ifi_lastchange = ifp->if_lastchange;
1582
1583 ifd->ifi_ipackets = ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS);
1584 ifd->ifi_ierrors = ifp->if_get_counter(ifp, IFCOUNTER_IERRORS);
1585 ifd->ifi_opackets = ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS);
1586 ifd->ifi_oerrors = ifp->if_get_counter(ifp, IFCOUNTER_OERRORS);
1587 ifd->ifi_collisions = ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS);
1588 ifd->ifi_ibytes = ifp->if_get_counter(ifp, IFCOUNTER_IBYTES);
1589 ifd->ifi_obytes = ifp->if_get_counter(ifp, IFCOUNTER_OBYTES);
1590 ifd->ifi_imcasts = ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS);
1591 ifd->ifi_omcasts = ifp->if_get_counter(ifp, IFCOUNTER_OMCASTS);
1592 ifd->ifi_iqdrops = ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS);
1593 ifd->ifi_oqdrops = ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
1594 ifd->ifi_noproto = ifp->if_get_counter(ifp, IFCOUNTER_NOPROTO);
1595 }
1596
1597 /*
1598 * Initialization, destruction and refcounting functions for ifaddrs.
1599 */
1600 struct ifaddr *
ifa_alloc(size_t size,int flags)1601 ifa_alloc(size_t size, int flags)
1602 {
1603 struct ifaddr *ifa;
1604
1605 KASSERT(size >= sizeof(struct ifaddr),
1606 ("%s: invalid size %zu", __func__, size));
1607
1608 ifa = malloc(size, M_IFADDR, M_ZERO | flags);
1609 if (ifa == NULL)
1610 return (NULL);
1611
1612 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL)
1613 goto fail;
1614 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL)
1615 goto fail;
1616 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL)
1617 goto fail;
1618 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL)
1619 goto fail;
1620
1621 refcount_init(&ifa->ifa_refcnt, 1);
1622
1623 return (ifa);
1624
1625 fail:
1626 /* free(NULL) is okay */
1627 counter_u64_free(ifa->ifa_opackets);
1628 counter_u64_free(ifa->ifa_ipackets);
1629 counter_u64_free(ifa->ifa_obytes);
1630 counter_u64_free(ifa->ifa_ibytes);
1631 free(ifa, M_IFADDR);
1632
1633 return (NULL);
1634 }
1635
1636 void
ifa_ref(struct ifaddr * ifa)1637 ifa_ref(struct ifaddr *ifa)
1638 {
1639 u_int old __diagused;
1640
1641 old = refcount_acquire(&ifa->ifa_refcnt);
1642 KASSERT(old > 0, ("%s: ifa %p has 0 refs", __func__, ifa));
1643 }
1644
1645 int
ifa_try_ref(struct ifaddr * ifa)1646 ifa_try_ref(struct ifaddr *ifa)
1647 {
1648
1649 NET_EPOCH_ASSERT();
1650 return (refcount_acquire_if_not_zero(&ifa->ifa_refcnt));
1651 }
1652
1653 static void
ifa_destroy(epoch_context_t ctx)1654 ifa_destroy(epoch_context_t ctx)
1655 {
1656 struct ifaddr *ifa;
1657
1658 ifa = __containerof(ctx, struct ifaddr, ifa_epoch_ctx);
1659 counter_u64_free(ifa->ifa_opackets);
1660 counter_u64_free(ifa->ifa_ipackets);
1661 counter_u64_free(ifa->ifa_obytes);
1662 counter_u64_free(ifa->ifa_ibytes);
1663 free(ifa, M_IFADDR);
1664 }
1665
1666 void
ifa_free(struct ifaddr * ifa)1667 ifa_free(struct ifaddr *ifa)
1668 {
1669
1670 if (refcount_release(&ifa->ifa_refcnt))
1671 NET_EPOCH_CALL(ifa_destroy, &ifa->ifa_epoch_ctx);
1672 }
1673
1674 /*
1675 * XXX: Because sockaddr_dl has deeper structure than the sockaddr
1676 * structs used to represent other address families, it is necessary
1677 * to perform a different comparison.
1678 */
1679 static bool
sa_dl_equal(const struct sockaddr * a,const struct sockaddr * b)1680 sa_dl_equal(const struct sockaddr *a, const struct sockaddr *b)
1681 {
1682 const struct sockaddr_dl *sdl1 = (const struct sockaddr_dl *)a;
1683 const struct sockaddr_dl *sdl2 = (const struct sockaddr_dl *)b;
1684
1685 return (sdl1->sdl_len == sdl2->sdl_len &&
1686 bcmp(sdl1->sdl_data + sdl1->sdl_nlen,
1687 sdl2->sdl_data + sdl2->sdl_nlen, sdl1->sdl_alen) == 0);
1688 }
1689
1690 /*
1691 * Locate an interface based on a complete address.
1692 */
1693 /*ARGSUSED*/
1694 struct ifaddr *
ifa_ifwithaddr(const struct sockaddr * addr)1695 ifa_ifwithaddr(const struct sockaddr *addr)
1696 {
1697 struct ifnet *ifp;
1698 struct ifaddr *ifa;
1699
1700 NET_EPOCH_ASSERT();
1701
1702 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1703 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1704 if (ifa->ifa_addr->sa_family != addr->sa_family)
1705 continue;
1706 if (sa_equal(addr, ifa->ifa_addr)) {
1707 goto done;
1708 }
1709 /* IP6 doesn't have broadcast */
1710 if ((ifp->if_flags & IFF_BROADCAST) &&
1711 ifa->ifa_broadaddr &&
1712 ifa->ifa_broadaddr->sa_len != 0 &&
1713 sa_equal(ifa->ifa_broadaddr, addr)) {
1714 goto done;
1715 }
1716 }
1717 }
1718 ifa = NULL;
1719 done:
1720 return (ifa);
1721 }
1722
1723 int
ifa_ifwithaddr_check(const struct sockaddr * addr)1724 ifa_ifwithaddr_check(const struct sockaddr *addr)
1725 {
1726 struct epoch_tracker et;
1727 int rc;
1728
1729 NET_EPOCH_ENTER(et);
1730 rc = (ifa_ifwithaddr(addr) != NULL);
1731 NET_EPOCH_EXIT(et);
1732 return (rc);
1733 }
1734
1735 /*
1736 * Locate an interface based on the broadcast address.
1737 */
1738 /* ARGSUSED */
1739 struct ifaddr *
ifa_ifwithbroadaddr(const struct sockaddr * addr,int fibnum)1740 ifa_ifwithbroadaddr(const struct sockaddr *addr, int fibnum)
1741 {
1742 struct ifnet *ifp;
1743 struct ifaddr *ifa;
1744
1745 NET_EPOCH_ASSERT();
1746 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1747 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1748 continue;
1749 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1750 if (ifa->ifa_addr->sa_family != addr->sa_family)
1751 continue;
1752 if ((ifp->if_flags & IFF_BROADCAST) &&
1753 ifa->ifa_broadaddr &&
1754 ifa->ifa_broadaddr->sa_len != 0 &&
1755 sa_equal(ifa->ifa_broadaddr, addr)) {
1756 goto done;
1757 }
1758 }
1759 }
1760 ifa = NULL;
1761 done:
1762 return (ifa);
1763 }
1764
1765 /*
1766 * Locate the point to point interface with a given destination address.
1767 */
1768 /*ARGSUSED*/
1769 struct ifaddr *
ifa_ifwithdstaddr(const struct sockaddr * addr,int fibnum)1770 ifa_ifwithdstaddr(const struct sockaddr *addr, int fibnum)
1771 {
1772 struct ifnet *ifp;
1773 struct ifaddr *ifa;
1774
1775 NET_EPOCH_ASSERT();
1776 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1777 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1778 continue;
1779 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1780 continue;
1781 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1782 if (ifa->ifa_addr->sa_family != addr->sa_family)
1783 continue;
1784 if (ifa->ifa_dstaddr != NULL &&
1785 sa_equal(addr, ifa->ifa_dstaddr)) {
1786 goto done;
1787 }
1788 }
1789 }
1790 ifa = NULL;
1791 done:
1792 return (ifa);
1793 }
1794
1795 /*
1796 * Find an interface on a specific network. If many, choice
1797 * is most specific found.
1798 */
1799 struct ifaddr *
ifa_ifwithnet(const struct sockaddr * addr,int ignore_ptp,int fibnum)1800 ifa_ifwithnet(const struct sockaddr *addr, int ignore_ptp, int fibnum)
1801 {
1802 struct ifnet *ifp;
1803 struct ifaddr *ifa;
1804 struct ifaddr *ifa_maybe = NULL;
1805 u_int af = addr->sa_family;
1806 const char *addr_data = addr->sa_data, *cplim;
1807
1808 NET_EPOCH_ASSERT();
1809 /*
1810 * AF_LINK addresses can be looked up directly by their index number,
1811 * so do that if we can.
1812 */
1813 if (af == AF_LINK) {
1814 ifp = ifnet_byindex(
1815 ((const struct sockaddr_dl *)addr)->sdl_index);
1816 return (ifp ? ifp->if_addr : NULL);
1817 }
1818
1819 /*
1820 * Scan though each interface, looking for ones that have addresses
1821 * in this address family and the requested fib.
1822 */
1823 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1824 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum))
1825 continue;
1826 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1827 const char *cp, *cp2, *cp3;
1828
1829 if (ifa->ifa_addr->sa_family != af)
1830 next: continue;
1831 if (af == AF_INET &&
1832 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) {
1833 /*
1834 * This is a bit broken as it doesn't
1835 * take into account that the remote end may
1836 * be a single node in the network we are
1837 * looking for.
1838 * The trouble is that we don't know the
1839 * netmask for the remote end.
1840 */
1841 if (ifa->ifa_dstaddr != NULL &&
1842 sa_equal(addr, ifa->ifa_dstaddr)) {
1843 goto done;
1844 }
1845 } else {
1846 /*
1847 * Scan all the bits in the ifa's address.
1848 * If a bit dissagrees with what we are
1849 * looking for, mask it with the netmask
1850 * to see if it really matters.
1851 * (A byte at a time)
1852 */
1853 if (ifa->ifa_netmask == 0)
1854 continue;
1855 cp = addr_data;
1856 cp2 = ifa->ifa_addr->sa_data;
1857 cp3 = ifa->ifa_netmask->sa_data;
1858 cplim = ifa->ifa_netmask->sa_len
1859 + (char *)ifa->ifa_netmask;
1860 while (cp3 < cplim)
1861 if ((*cp++ ^ *cp2++) & *cp3++)
1862 goto next; /* next address! */
1863 /*
1864 * If the netmask of what we just found
1865 * is more specific than what we had before
1866 * (if we had one), or if the virtual status
1867 * of new prefix is better than of the old one,
1868 * then remember the new one before continuing
1869 * to search for an even better one.
1870 */
1871 if (ifa_maybe == NULL ||
1872 ifa_preferred(ifa_maybe, ifa) ||
1873 rn_refines((caddr_t)ifa->ifa_netmask,
1874 (caddr_t)ifa_maybe->ifa_netmask)) {
1875 ifa_maybe = ifa;
1876 }
1877 }
1878 }
1879 }
1880 ifa = ifa_maybe;
1881 ifa_maybe = NULL;
1882 done:
1883 return (ifa);
1884 }
1885
1886 /*
1887 * Find an interface address specific to an interface best matching
1888 * a given address.
1889 */
1890 struct ifaddr *
ifaof_ifpforaddr(const struct sockaddr * addr,struct ifnet * ifp)1891 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1892 {
1893 struct ifaddr *ifa;
1894 const char *cp, *cp2, *cp3;
1895 char *cplim;
1896 struct ifaddr *ifa_maybe = NULL;
1897 u_int af = addr->sa_family;
1898
1899 if (af >= AF_MAX)
1900 return (NULL);
1901
1902 NET_EPOCH_ASSERT();
1903 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1904 if (ifa->ifa_addr->sa_family != af)
1905 continue;
1906 if (ifa_maybe == NULL)
1907 ifa_maybe = ifa;
1908 if (ifa->ifa_netmask == 0) {
1909 if (sa_equal(addr, ifa->ifa_addr) ||
1910 (ifa->ifa_dstaddr &&
1911 sa_equal(addr, ifa->ifa_dstaddr)))
1912 goto done;
1913 continue;
1914 }
1915 if (ifp->if_flags & IFF_POINTOPOINT) {
1916 if (ifa->ifa_dstaddr && sa_equal(addr, ifa->ifa_dstaddr))
1917 goto done;
1918 } else {
1919 cp = addr->sa_data;
1920 cp2 = ifa->ifa_addr->sa_data;
1921 cp3 = ifa->ifa_netmask->sa_data;
1922 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1923 for (; cp3 < cplim; cp3++)
1924 if ((*cp++ ^ *cp2++) & *cp3)
1925 break;
1926 if (cp3 == cplim)
1927 goto done;
1928 }
1929 }
1930 ifa = ifa_maybe;
1931 done:
1932 return (ifa);
1933 }
1934
1935 /*
1936 * See whether new ifa is better than current one:
1937 * 1) A non-virtual one is preferred over virtual.
1938 * 2) A virtual in master state preferred over any other state.
1939 *
1940 * Used in several address selecting functions.
1941 */
1942 int
ifa_preferred(struct ifaddr * cur,struct ifaddr * next)1943 ifa_preferred(struct ifaddr *cur, struct ifaddr *next)
1944 {
1945
1946 return (cur->ifa_carp && (!next->ifa_carp ||
1947 ((*carp_master_p)(next) && !(*carp_master_p)(cur))));
1948 }
1949
1950 struct sockaddr_dl *
link_alloc_sdl(size_t size,int flags)1951 link_alloc_sdl(size_t size, int flags)
1952 {
1953
1954 return (malloc(size, M_TEMP, flags));
1955 }
1956
1957 void
link_free_sdl(struct sockaddr * sa)1958 link_free_sdl(struct sockaddr *sa)
1959 {
1960 free(sa, M_TEMP);
1961 }
1962
1963 /*
1964 * Fills in given sdl with interface basic info.
1965 * Returns pointer to filled sdl.
1966 */
1967 struct sockaddr_dl *
link_init_sdl(struct ifnet * ifp,struct sockaddr * paddr,u_char iftype)1968 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype)
1969 {
1970 struct sockaddr_dl *sdl;
1971
1972 sdl = (struct sockaddr_dl *)paddr;
1973 memset(sdl, 0, sizeof(struct sockaddr_dl));
1974 sdl->sdl_len = sizeof(struct sockaddr_dl);
1975 sdl->sdl_family = AF_LINK;
1976 sdl->sdl_index = ifp->if_index;
1977 sdl->sdl_type = iftype;
1978
1979 return (sdl);
1980 }
1981
1982 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */
1983 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */
1984 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *);
1985 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t);
1986 int (*vlan_tag_p)(struct ifnet *, uint16_t *);
1987 int (*vlan_pcp_p)(struct ifnet *, uint16_t *);
1988 int (*vlan_setcookie_p)(struct ifnet *, void *);
1989 void *(*vlan_cookie_p)(struct ifnet *);
1990 void (*vlan_input_p)(struct ifnet *, struct mbuf *);
1991
1992 /*
1993 * Handle a change in the interface link state. To avoid LORs
1994 * between driver lock and upper layer locks, as well as possible
1995 * recursions, we post event to taskqueue, and all job
1996 * is done in static do_link_state_change().
1997 */
1998 void
if_link_state_change(struct ifnet * ifp,int link_state)1999 if_link_state_change(struct ifnet *ifp, int link_state)
2000 {
2001 /* Return if state hasn't changed. */
2002 if (ifp->if_link_state == link_state)
2003 return;
2004
2005 ifp->if_link_state = link_state;
2006
2007 /* XXXGL: reference ifp? */
2008 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask);
2009 }
2010
2011 static void
do_link_state_change(void * arg,int pending)2012 do_link_state_change(void *arg, int pending)
2013 {
2014 struct ifnet *ifp;
2015 int link_state;
2016
2017 ifp = arg;
2018 link_state = ifp->if_link_state;
2019
2020 CURVNET_SET(ifp->if_vnet);
2021 rt_ifmsg(ifp, 0);
2022 if (ifp->if_vlantrunk != NULL)
2023 (*vlan_link_state_p)(ifp);
2024 if (ifp->if_carp)
2025 (*carp_linkstate_p)(ifp);
2026 if (ifp->if_bridge)
2027 ifp->if_bridge_linkstate(ifp);
2028 if (ifp->if_lagg)
2029 (*lagg_linkstate_p)(ifp, link_state);
2030
2031 if (IS_DEFAULT_VNET(curvnet))
2032 devctl_notify("IFNET", ifp->if_xname,
2033 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN",
2034 NULL);
2035 if (pending > 1)
2036 if_printf(ifp, "%d link states coalesced\n", pending);
2037 if (log_link_state_change)
2038 if_printf(ifp, "link state changed to %s\n",
2039 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" );
2040 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
2041 CURVNET_RESTORE();
2042 }
2043
2044 /*
2045 * Mark an interface down and notify protocols of
2046 * the transition.
2047 */
2048 void
if_down(struct ifnet * ifp)2049 if_down(struct ifnet *ifp)
2050 {
2051
2052 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
2053
2054 ifp->if_flags &= ~IFF_UP;
2055 getmicrotime(&ifp->if_lastchange);
2056 ifp->if_qflush(ifp);
2057
2058 if (ifp->if_carp)
2059 (*carp_linkstate_p)(ifp);
2060 rt_ifmsg(ifp, IFF_UP);
2061 }
2062
2063 /*
2064 * Mark an interface up and notify protocols of
2065 * the transition.
2066 */
2067 void
if_up(struct ifnet * ifp)2068 if_up(struct ifnet *ifp)
2069 {
2070
2071 ifp->if_flags |= IFF_UP;
2072 getmicrotime(&ifp->if_lastchange);
2073 if (ifp->if_carp)
2074 (*carp_linkstate_p)(ifp);
2075 rt_ifmsg(ifp, IFF_UP);
2076 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
2077 }
2078
2079 /*
2080 * Flush an interface queue.
2081 */
2082 void
if_qflush(struct ifnet * ifp)2083 if_qflush(struct ifnet *ifp)
2084 {
2085 struct mbuf *m, *n;
2086 struct ifaltq *ifq;
2087
2088 ifq = &ifp->if_snd;
2089 IFQ_LOCK(ifq);
2090 #ifdef ALTQ
2091 if (ALTQ_IS_ENABLED(ifq))
2092 ALTQ_PURGE(ifq);
2093 #endif
2094 n = ifq->ifq_head;
2095 while ((m = n) != NULL) {
2096 n = m->m_nextpkt;
2097 m_freem(m);
2098 }
2099 ifq->ifq_head = 0;
2100 ifq->ifq_tail = 0;
2101 ifq->ifq_len = 0;
2102 IFQ_UNLOCK(ifq);
2103 }
2104
2105 /*
2106 * Map interface name to interface structure pointer, with or without
2107 * returning a reference.
2108 */
2109 struct ifnet *
ifunit_ref(const char * name)2110 ifunit_ref(const char *name)
2111 {
2112 struct epoch_tracker et;
2113 struct ifnet *ifp;
2114
2115 NET_EPOCH_ENTER(et);
2116 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2117 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 &&
2118 !(ifp->if_flags & IFF_DYING)) {
2119 MPASS(ifp->if_vnet == curvnet);
2120 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2121 if_ref(ifp);
2122 break;
2123 }
2124 }
2125 NET_EPOCH_EXIT(et);
2126 return (ifp);
2127 }
2128
2129 struct ifnet *
ifunit(const char * name)2130 ifunit(const char *name)
2131 {
2132 struct epoch_tracker et;
2133 struct ifnet *ifp;
2134
2135 NET_EPOCH_ENTER(et);
2136 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2137 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) {
2138 MPASS(refcount_load(&ifp->if_refcount) > 0);
2139 MPASS(ifp->if_vnet == curvnet);
2140 MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp);
2141 break;
2142 }
2143 }
2144 NET_EPOCH_EXIT(et);
2145 return (ifp);
2146 }
2147
2148 void *
ifr_buffer_get_buffer(void * data)2149 ifr_buffer_get_buffer(void *data)
2150 {
2151 union ifreq_union *ifrup;
2152
2153 ifrup = data;
2154 #ifdef COMPAT_FREEBSD32
2155 if (SV_CURPROC_FLAG(SV_ILP32))
2156 return ((void *)(uintptr_t)
2157 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer);
2158 #endif
2159 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer);
2160 }
2161
2162 static void
ifr_buffer_set_buffer_null(void * data)2163 ifr_buffer_set_buffer_null(void *data)
2164 {
2165 union ifreq_union *ifrup;
2166
2167 ifrup = data;
2168 #ifdef COMPAT_FREEBSD32
2169 if (SV_CURPROC_FLAG(SV_ILP32))
2170 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0;
2171 else
2172 #endif
2173 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL;
2174 }
2175
2176 size_t
ifr_buffer_get_length(void * data)2177 ifr_buffer_get_length(void *data)
2178 {
2179 union ifreq_union *ifrup;
2180
2181 ifrup = data;
2182 #ifdef COMPAT_FREEBSD32
2183 if (SV_CURPROC_FLAG(SV_ILP32))
2184 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length);
2185 #endif
2186 return (ifrup->ifr.ifr_ifru.ifru_buffer.length);
2187 }
2188
2189 static void
ifr_buffer_set_length(void * data,size_t len)2190 ifr_buffer_set_length(void *data, size_t len)
2191 {
2192 union ifreq_union *ifrup;
2193
2194 ifrup = data;
2195 #ifdef COMPAT_FREEBSD32
2196 if (SV_CURPROC_FLAG(SV_ILP32))
2197 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len;
2198 else
2199 #endif
2200 ifrup->ifr.ifr_ifru.ifru_buffer.length = len;
2201 }
2202
2203 void *
ifr_data_get_ptr(void * ifrp)2204 ifr_data_get_ptr(void *ifrp)
2205 {
2206 union ifreq_union *ifrup;
2207
2208 ifrup = ifrp;
2209 #ifdef COMPAT_FREEBSD32
2210 if (SV_CURPROC_FLAG(SV_ILP32))
2211 return ((void *)(uintptr_t)
2212 ifrup->ifr32.ifr_ifru.ifru_data);
2213 #endif
2214 return (ifrup->ifr.ifr_ifru.ifru_data);
2215 }
2216
2217 struct ifcap_nv_bit_name {
2218 uint64_t cap_bit;
2219 const char *cap_name;
2220 };
2221 #define CAPNV(x) {.cap_bit = IFCAP_##x, \
2222 .cap_name = __CONCAT(IFCAP_, __CONCAT(x, _NAME)) }
2223 const struct ifcap_nv_bit_name ifcap_nv_bit_names[] = {
2224 CAPNV(RXCSUM),
2225 CAPNV(TXCSUM),
2226 CAPNV(NETCONS),
2227 CAPNV(VLAN_MTU),
2228 CAPNV(VLAN_HWTAGGING),
2229 CAPNV(JUMBO_MTU),
2230 CAPNV(POLLING),
2231 CAPNV(VLAN_HWCSUM),
2232 CAPNV(TSO4),
2233 CAPNV(TSO6),
2234 CAPNV(LRO),
2235 CAPNV(WOL_UCAST),
2236 CAPNV(WOL_MCAST),
2237 CAPNV(WOL_MAGIC),
2238 CAPNV(TOE4),
2239 CAPNV(TOE6),
2240 CAPNV(VLAN_HWFILTER),
2241 CAPNV(VLAN_HWTSO),
2242 CAPNV(LINKSTATE),
2243 CAPNV(NETMAP),
2244 CAPNV(RXCSUM_IPV6),
2245 CAPNV(TXCSUM_IPV6),
2246 CAPNV(HWSTATS),
2247 CAPNV(TXRTLMT),
2248 CAPNV(HWRXTSTMP),
2249 CAPNV(MEXTPG),
2250 CAPNV(TXTLS4),
2251 CAPNV(TXTLS6),
2252 CAPNV(VXLAN_HWCSUM),
2253 CAPNV(VXLAN_HWTSO),
2254 CAPNV(TXTLS_RTLMT),
2255 {0, NULL}
2256 };
2257 #define CAP2NV(x) {.cap_bit = IFCAP2_BIT(IFCAP2_##x), \
2258 .cap_name = __CONCAT(IFCAP2_, __CONCAT(x, _NAME)) }
2259 const struct ifcap_nv_bit_name ifcap2_nv_bit_names[] = {
2260 CAP2NV(RXTLS4),
2261 CAP2NV(RXTLS6),
2262 CAP2NV(IPSEC_OFFLOAD),
2263 {0, NULL}
2264 };
2265 #undef CAPNV
2266 #undef CAP2NV
2267
2268 int
if_capnv_to_capint(const nvlist_t * nv,int * old_cap,const struct ifcap_nv_bit_name * nn,bool all)2269 if_capnv_to_capint(const nvlist_t *nv, int *old_cap,
2270 const struct ifcap_nv_bit_name *nn, bool all)
2271 {
2272 int i, res;
2273
2274 res = 0;
2275 for (i = 0; nn[i].cap_name != NULL; i++) {
2276 if (nvlist_exists_bool(nv, nn[i].cap_name)) {
2277 if (all || nvlist_get_bool(nv, nn[i].cap_name))
2278 res |= nn[i].cap_bit;
2279 } else {
2280 res |= *old_cap & nn[i].cap_bit;
2281 }
2282 }
2283 return (res);
2284 }
2285
2286 void
if_capint_to_capnv(nvlist_t * nv,const struct ifcap_nv_bit_name * nn,int ifr_cap,int ifr_req)2287 if_capint_to_capnv(nvlist_t *nv, const struct ifcap_nv_bit_name *nn,
2288 int ifr_cap, int ifr_req)
2289 {
2290 int i;
2291
2292 for (i = 0; nn[i].cap_name != NULL; i++) {
2293 if ((nn[i].cap_bit & ifr_cap) != 0) {
2294 nvlist_add_bool(nv, nn[i].cap_name,
2295 (nn[i].cap_bit & ifr_req) != 0);
2296 }
2297 }
2298 }
2299
2300 /*
2301 * Hardware specific interface ioctls.
2302 */
2303 int
ifhwioctl(u_long cmd,struct ifnet * ifp,caddr_t data,struct thread * td)2304 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
2305 {
2306 struct ifreq *ifr;
2307 int error = 0, do_ifup = 0;
2308 int new_flags, temp_flags;
2309 size_t descrlen, nvbuflen;
2310 char *descrbuf;
2311 char new_name[IFNAMSIZ];
2312 void *buf;
2313 nvlist_t *nvcap;
2314 struct siocsifcapnv_driver_data drv_ioctl_data;
2315
2316 ifr = (struct ifreq *)data;
2317 switch (cmd) {
2318 case SIOCGIFINDEX:
2319 ifr->ifr_index = ifp->if_index;
2320 break;
2321
2322 case SIOCGIFFLAGS:
2323 temp_flags = ifp->if_flags | ifp->if_drv_flags;
2324 ifr->ifr_flags = temp_flags & 0xffff;
2325 ifr->ifr_flagshigh = temp_flags >> 16;
2326 break;
2327
2328 case SIOCGIFCAP:
2329 ifr->ifr_reqcap = ifp->if_capabilities;
2330 ifr->ifr_curcap = ifp->if_capenable;
2331 break;
2332
2333 case SIOCGIFCAPNV:
2334 if ((ifp->if_capabilities & IFCAP_NV) == 0) {
2335 error = EINVAL;
2336 break;
2337 }
2338 buf = NULL;
2339 nvcap = nvlist_create(0);
2340 for (;;) {
2341 if_capint_to_capnv(nvcap, ifcap_nv_bit_names,
2342 ifp->if_capabilities, ifp->if_capenable);
2343 if_capint_to_capnv(nvcap, ifcap2_nv_bit_names,
2344 ifp->if_capabilities2, ifp->if_capenable2);
2345 error = (*ifp->if_ioctl)(ifp, SIOCGIFCAPNV,
2346 __DECONST(caddr_t, nvcap));
2347 if (error != 0) {
2348 if_printf(ifp,
2349 "SIOCGIFCAPNV driver mistake: nvlist error %d\n",
2350 error);
2351 break;
2352 }
2353 buf = nvlist_pack(nvcap, &nvbuflen);
2354 if (buf == NULL) {
2355 error = nvlist_error(nvcap);
2356 if (error == 0)
2357 error = EDOOFUS;
2358 break;
2359 }
2360 if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
2361 ifr->ifr_cap_nv.length = nvbuflen;
2362 ifr->ifr_cap_nv.buffer = NULL;
2363 error = EFBIG;
2364 break;
2365 }
2366 ifr->ifr_cap_nv.length = nvbuflen;
2367 error = copyout(buf, ifr->ifr_cap_nv.buffer, nvbuflen);
2368 break;
2369 }
2370 free(buf, M_NVLIST);
2371 nvlist_destroy(nvcap);
2372 break;
2373
2374 case SIOCGIFDATA:
2375 {
2376 struct if_data ifd;
2377
2378 /* Ensure uninitialised padding is not leaked. */
2379 memset(&ifd, 0, sizeof(ifd));
2380
2381 if_data_copy(ifp, &ifd);
2382 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd));
2383 break;
2384 }
2385
2386 #ifdef MAC
2387 case SIOCGIFMAC:
2388 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp);
2389 break;
2390 #endif
2391
2392 case SIOCGIFMETRIC:
2393 ifr->ifr_metric = ifp->if_metric;
2394 break;
2395
2396 case SIOCGIFMTU:
2397 ifr->ifr_mtu = ifp->if_mtu;
2398 break;
2399
2400 case SIOCGIFPHYS:
2401 /* XXXGL: did this ever worked? */
2402 ifr->ifr_phys = 0;
2403 break;
2404
2405 case SIOCGIFDESCR:
2406 error = 0;
2407 sx_slock(&ifdescr_sx);
2408 if (ifp->if_description == NULL)
2409 error = ENOMSG;
2410 else {
2411 /* space for terminating nul */
2412 descrlen = strlen(ifp->if_description) + 1;
2413 if (ifr_buffer_get_length(ifr) < descrlen)
2414 ifr_buffer_set_buffer_null(ifr);
2415 else
2416 error = copyout(ifp->if_description,
2417 ifr_buffer_get_buffer(ifr), descrlen);
2418 ifr_buffer_set_length(ifr, descrlen);
2419 }
2420 sx_sunlock(&ifdescr_sx);
2421 break;
2422
2423 case SIOCSIFDESCR:
2424 error = priv_check(td, PRIV_NET_SETIFDESCR);
2425 if (error)
2426 return (error);
2427
2428 /*
2429 * Copy only (length-1) bytes to make sure that
2430 * if_description is always nul terminated. The
2431 * length parameter is supposed to count the
2432 * terminating nul in.
2433 */
2434 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen)
2435 return (ENAMETOOLONG);
2436 else if (ifr_buffer_get_length(ifr) == 0)
2437 descrbuf = NULL;
2438 else {
2439 descrbuf = if_allocdescr(ifr_buffer_get_length(ifr), M_WAITOK);
2440 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf,
2441 ifr_buffer_get_length(ifr) - 1);
2442 if (error) {
2443 if_freedescr(descrbuf);
2444 break;
2445 }
2446 }
2447
2448 if_setdescr(ifp, descrbuf);
2449 getmicrotime(&ifp->if_lastchange);
2450 break;
2451
2452 case SIOCGIFFIB:
2453 ifr->ifr_fib = ifp->if_fib;
2454 break;
2455
2456 case SIOCSIFFIB:
2457 error = priv_check(td, PRIV_NET_SETIFFIB);
2458 if (error)
2459 return (error);
2460 if (ifr->ifr_fib >= rt_numfibs)
2461 return (EINVAL);
2462
2463 ifp->if_fib = ifr->ifr_fib;
2464 break;
2465
2466 case SIOCSIFFLAGS:
2467 error = priv_check(td, PRIV_NET_SETIFFLAGS);
2468 if (error)
2469 return (error);
2470 /*
2471 * Currently, no driver owned flags pass the IFF_CANTCHANGE
2472 * check, so we don't need special handling here yet.
2473 */
2474 new_flags = (ifr->ifr_flags & 0xffff) |
2475 (ifr->ifr_flagshigh << 16);
2476 if (ifp->if_flags & IFF_UP &&
2477 (new_flags & IFF_UP) == 0) {
2478 if_down(ifp);
2479 } else if (new_flags & IFF_UP &&
2480 (ifp->if_flags & IFF_UP) == 0) {
2481 do_ifup = 1;
2482 }
2483
2484 /*
2485 * See if the promiscuous mode or allmulti bits are about to
2486 * flip. They require special handling because in-kernel
2487 * consumers may indepdently toggle them.
2488 */
2489 if_setppromisc(ifp, new_flags & IFF_PPROMISC);
2490 if ((ifp->if_flags ^ new_flags) & IFF_PALLMULTI) {
2491 if (new_flags & IFF_PALLMULTI)
2492 ifp->if_flags |= IFF_ALLMULTI;
2493 else if (ifp->if_amcount == 0)
2494 ifp->if_flags &= ~IFF_ALLMULTI;
2495 }
2496 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2497 (new_flags &~ IFF_CANTCHANGE);
2498 if (ifp->if_ioctl) {
2499 (void) (*ifp->if_ioctl)(ifp, cmd, data);
2500 }
2501 if (do_ifup)
2502 if_up(ifp);
2503 getmicrotime(&ifp->if_lastchange);
2504 break;
2505
2506 case SIOCSIFCAP:
2507 error = priv_check(td, PRIV_NET_SETIFCAP);
2508 if (error != 0)
2509 return (error);
2510 if (ifp->if_ioctl == NULL)
2511 return (EOPNOTSUPP);
2512 if (ifr->ifr_reqcap & ~ifp->if_capabilities)
2513 return (EINVAL);
2514 error = (*ifp->if_ioctl)(ifp, cmd, data);
2515 if (error == 0)
2516 getmicrotime(&ifp->if_lastchange);
2517 break;
2518
2519 case SIOCSIFCAPNV:
2520 error = priv_check(td, PRIV_NET_SETIFCAP);
2521 if (error != 0)
2522 return (error);
2523 if (ifp->if_ioctl == NULL)
2524 return (EOPNOTSUPP);
2525 if ((ifp->if_capabilities & IFCAP_NV) == 0)
2526 return (EINVAL);
2527 if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
2528 return (EINVAL);
2529 nvcap = NULL;
2530 buf = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
2531 for (;;) {
2532 error = copyin(ifr->ifr_cap_nv.buffer, buf,
2533 ifr->ifr_cap_nv.length);
2534 if (error != 0)
2535 break;
2536 nvcap = nvlist_unpack(buf, ifr->ifr_cap_nv.length, 0);
2537 if (nvcap == NULL) {
2538 error = EINVAL;
2539 break;
2540 }
2541 drv_ioctl_data.reqcap = if_capnv_to_capint(nvcap,
2542 &ifp->if_capenable, ifcap_nv_bit_names, false);
2543 if ((drv_ioctl_data.reqcap &
2544 ~ifp->if_capabilities) != 0) {
2545 error = EINVAL;
2546 break;
2547 }
2548 drv_ioctl_data.reqcap2 = if_capnv_to_capint(nvcap,
2549 &ifp->if_capenable2, ifcap2_nv_bit_names, false);
2550 if ((drv_ioctl_data.reqcap2 &
2551 ~ifp->if_capabilities2) != 0) {
2552 error = EINVAL;
2553 break;
2554 }
2555 drv_ioctl_data.nvcap = nvcap;
2556 error = (*ifp->if_ioctl)(ifp, SIOCSIFCAPNV,
2557 (caddr_t)&drv_ioctl_data);
2558 break;
2559 }
2560 nvlist_destroy(nvcap);
2561 free(buf, M_TEMP);
2562 if (error == 0)
2563 getmicrotime(&ifp->if_lastchange);
2564 break;
2565
2566 #ifdef MAC
2567 case SIOCSIFMAC:
2568 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp);
2569 break;
2570 #endif
2571
2572 case SIOCSIFNAME:
2573 error = priv_check(td, PRIV_NET_SETIFNAME);
2574 if (error)
2575 return (error);
2576 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ,
2577 NULL);
2578 if (error != 0)
2579 return (error);
2580 error = if_rename(ifp, new_name);
2581 break;
2582
2583 #ifdef VIMAGE
2584 case SIOCSIFVNET:
2585 error = priv_check(td, PRIV_NET_SETIFVNET);
2586 if (error)
2587 return (error);
2588 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid);
2589 break;
2590 #endif
2591
2592 case SIOCSIFMETRIC:
2593 error = priv_check(td, PRIV_NET_SETIFMETRIC);
2594 if (error)
2595 return (error);
2596 ifp->if_metric = ifr->ifr_metric;
2597 getmicrotime(&ifp->if_lastchange);
2598 break;
2599
2600 case SIOCSIFPHYS:
2601 error = priv_check(td, PRIV_NET_SETIFPHYS);
2602 if (error)
2603 return (error);
2604 if (ifp->if_ioctl == NULL)
2605 return (EOPNOTSUPP);
2606 error = (*ifp->if_ioctl)(ifp, cmd, data);
2607 if (error == 0)
2608 getmicrotime(&ifp->if_lastchange);
2609 break;
2610
2611 case SIOCSIFMTU:
2612 {
2613 u_long oldmtu = ifp->if_mtu;
2614
2615 error = priv_check(td, PRIV_NET_SETIFMTU);
2616 if (error)
2617 return (error);
2618 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
2619 return (EINVAL);
2620 if (ifp->if_ioctl == NULL)
2621 return (EOPNOTSUPP);
2622 /* Disallow MTU changes on bridge member interfaces. */
2623 if (ifp->if_bridge)
2624 return (EOPNOTSUPP);
2625 error = (*ifp->if_ioctl)(ifp, cmd, data);
2626 if (error == 0) {
2627 getmicrotime(&ifp->if_lastchange);
2628 rt_ifmsg(ifp, 0);
2629 #ifdef INET
2630 DEBUGNET_NOTIFY_MTU(ifp);
2631 #endif
2632 }
2633 /*
2634 * If the link MTU changed, do network layer specific procedure.
2635 */
2636 if (ifp->if_mtu != oldmtu)
2637 if_notifymtu(ifp);
2638 break;
2639 }
2640
2641 case SIOCADDMULTI:
2642 case SIOCDELMULTI:
2643 if (cmd == SIOCADDMULTI)
2644 error = priv_check(td, PRIV_NET_ADDMULTI);
2645 else
2646 error = priv_check(td, PRIV_NET_DELMULTI);
2647 if (error)
2648 return (error);
2649
2650 /* Don't allow group membership on non-multicast interfaces. */
2651 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2652 return (EOPNOTSUPP);
2653
2654 /* Don't let users screw up protocols' entries. */
2655 if (ifr->ifr_addr.sa_family != AF_LINK)
2656 return (EINVAL);
2657
2658 if (cmd == SIOCADDMULTI) {
2659 struct epoch_tracker et;
2660 struct ifmultiaddr *ifma;
2661
2662 /*
2663 * Userland is only permitted to join groups once
2664 * via the if_addmulti() KPI, because it cannot hold
2665 * struct ifmultiaddr * between calls. It may also
2666 * lose a race while we check if the membership
2667 * already exists.
2668 */
2669 NET_EPOCH_ENTER(et);
2670 ifma = if_findmulti(ifp, &ifr->ifr_addr);
2671 NET_EPOCH_EXIT(et);
2672 if (ifma != NULL)
2673 error = EADDRINUSE;
2674 else
2675 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2676 } else {
2677 error = if_delmulti(ifp, &ifr->ifr_addr);
2678 }
2679 if (error == 0)
2680 getmicrotime(&ifp->if_lastchange);
2681 break;
2682
2683 case SIOCSIFPHYADDR:
2684 case SIOCDIFPHYADDR:
2685 #ifdef INET6
2686 case SIOCSIFPHYADDR_IN6:
2687 #endif
2688 case SIOCSIFMEDIA:
2689 case SIOCSIFGENERIC:
2690 error = priv_check(td, PRIV_NET_HWIOCTL);
2691 if (error)
2692 return (error);
2693 if (ifp->if_ioctl == NULL)
2694 return (EOPNOTSUPP);
2695 error = (*ifp->if_ioctl)(ifp, cmd, data);
2696 if (error == 0)
2697 getmicrotime(&ifp->if_lastchange);
2698 break;
2699
2700 case SIOCGIFSTATUS:
2701 case SIOCGIFPSRCADDR:
2702 case SIOCGIFPDSTADDR:
2703 case SIOCGIFMEDIA:
2704 case SIOCGIFXMEDIA:
2705 case SIOCGIFGENERIC:
2706 case SIOCGIFRSSKEY:
2707 case SIOCGIFRSSHASH:
2708 case SIOCGIFDOWNREASON:
2709 if (ifp->if_ioctl == NULL)
2710 return (EOPNOTSUPP);
2711 error = (*ifp->if_ioctl)(ifp, cmd, data);
2712 break;
2713
2714 case SIOCSIFLLADDR:
2715 error = priv_check(td, PRIV_NET_SETLLADDR);
2716 if (error)
2717 return (error);
2718 error = if_setlladdr(ifp,
2719 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
2720 break;
2721
2722 case SIOCGHWADDR:
2723 error = if_gethwaddr(ifp, ifr);
2724 break;
2725
2726 case SIOCAIFGROUP:
2727 {
2728 const char *groupname;
2729
2730 error = priv_check(td, PRIV_NET_ADDIFGROUP);
2731 if (error)
2732 return (error);
2733 groupname = ((struct ifgroupreq *)data)->ifgr_group;
2734 if (strnlen(groupname, IFNAMSIZ) == IFNAMSIZ)
2735 return (EINVAL);
2736 error = if_addgroup(ifp, groupname);
2737 if (error != 0)
2738 return (error);
2739 break;
2740 }
2741 case SIOCGIFGROUP:
2742 {
2743 struct epoch_tracker et;
2744
2745 NET_EPOCH_ENTER(et);
2746 error = if_getgroup((struct ifgroupreq *)data, ifp);
2747 NET_EPOCH_EXIT(et);
2748 break;
2749 }
2750
2751 case SIOCDIFGROUP:
2752 {
2753 const char *groupname;
2754
2755 error = priv_check(td, PRIV_NET_DELIFGROUP);
2756 if (error)
2757 return (error);
2758 groupname = ((struct ifgroupreq *)data)->ifgr_group;
2759 if (strnlen(groupname, IFNAMSIZ) == IFNAMSIZ)
2760 return (EINVAL);
2761 error = if_delgroup(ifp, groupname);
2762 if (error != 0)
2763 return (error);
2764 break;
2765 }
2766 default:
2767 error = ENOIOCTL;
2768 break;
2769 }
2770 return (error);
2771 }
2772
2773 /*
2774 * Interface ioctls.
2775 */
2776 int
ifioctl(struct socket * so,u_long cmd,caddr_t data,struct thread * td)2777 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
2778 {
2779 #ifdef COMPAT_FREEBSD32
2780 union {
2781 struct ifconf ifc;
2782 struct ifdrv ifd;
2783 struct ifgroupreq ifgr;
2784 struct ifmediareq ifmr;
2785 } thunk;
2786 u_long saved_cmd;
2787 struct ifconf32 *ifc32;
2788 struct ifdrv32 *ifd32;
2789 struct ifgroupreq32 *ifgr32;
2790 struct ifmediareq32 *ifmr32;
2791 #endif
2792 struct ifnet *ifp;
2793 struct ifreq *ifr;
2794 int error;
2795 int oif_flags;
2796 #ifdef VIMAGE
2797 bool shutdown;
2798 #endif
2799
2800 CURVNET_SET(so->so_vnet);
2801 #ifdef VIMAGE
2802 /* Make sure the VNET is stable. */
2803 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet);
2804 if (shutdown) {
2805 CURVNET_RESTORE();
2806 return (EBUSY);
2807 }
2808 #endif
2809
2810 #ifdef COMPAT_FREEBSD32
2811 saved_cmd = cmd;
2812 switch (cmd) {
2813 case SIOCGIFCONF32:
2814 ifc32 = (struct ifconf32 *)data;
2815 thunk.ifc.ifc_len = ifc32->ifc_len;
2816 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf);
2817 data = (caddr_t)&thunk.ifc;
2818 cmd = SIOCGIFCONF;
2819 break;
2820 case SIOCGDRVSPEC32:
2821 case SIOCSDRVSPEC32:
2822 ifd32 = (struct ifdrv32 *)data;
2823 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name,
2824 sizeof(thunk.ifd.ifd_name));
2825 thunk.ifd.ifd_cmd = ifd32->ifd_cmd;
2826 thunk.ifd.ifd_len = ifd32->ifd_len;
2827 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data);
2828 data = (caddr_t)&thunk.ifd;
2829 cmd = _IOC_NEWTYPE(cmd, struct ifdrv);
2830 break;
2831 case SIOCAIFGROUP32:
2832 case SIOCGIFGROUP32:
2833 case SIOCDIFGROUP32:
2834 case SIOCGIFGMEMB32:
2835 ifgr32 = (struct ifgroupreq32 *)data;
2836 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name,
2837 sizeof(thunk.ifgr.ifgr_name));
2838 thunk.ifgr.ifgr_len = ifgr32->ifgr_len;
2839 switch (cmd) {
2840 case SIOCAIFGROUP32:
2841 case SIOCDIFGROUP32:
2842 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group,
2843 sizeof(thunk.ifgr.ifgr_group));
2844 break;
2845 case SIOCGIFGROUP32:
2846 case SIOCGIFGMEMB32:
2847 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups);
2848 break;
2849 }
2850 data = (caddr_t)&thunk.ifgr;
2851 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq);
2852 break;
2853 case SIOCGIFMEDIA32:
2854 case SIOCGIFXMEDIA32:
2855 ifmr32 = (struct ifmediareq32 *)data;
2856 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name,
2857 sizeof(thunk.ifmr.ifm_name));
2858 thunk.ifmr.ifm_current = ifmr32->ifm_current;
2859 thunk.ifmr.ifm_mask = ifmr32->ifm_mask;
2860 thunk.ifmr.ifm_status = ifmr32->ifm_status;
2861 thunk.ifmr.ifm_active = ifmr32->ifm_active;
2862 thunk.ifmr.ifm_count = ifmr32->ifm_count;
2863 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist);
2864 data = (caddr_t)&thunk.ifmr;
2865 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq);
2866 break;
2867 }
2868 #endif
2869
2870 switch (cmd) {
2871 case SIOCGIFCONF:
2872 error = ifconf(cmd, data);
2873 goto out_noref;
2874 }
2875
2876 ifr = (struct ifreq *)data;
2877 switch (cmd) {
2878 #ifdef VIMAGE
2879 case SIOCSIFRVNET:
2880 error = priv_check(td, PRIV_NET_SETIFVNET);
2881 if (error == 0)
2882 error = if_vmove_reclaim(td, ifr->ifr_name,
2883 ifr->ifr_jid);
2884 goto out_noref;
2885 #endif
2886 case SIOCIFCREATE:
2887 case SIOCIFCREATE2:
2888 error = priv_check(td, PRIV_NET_IFCREATE);
2889 if (error == 0)
2890 error = if_clone_create(ifr->ifr_name,
2891 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ?
2892 ifr_data_get_ptr(ifr) : NULL);
2893 goto out_noref;
2894 case SIOCIFDESTROY:
2895 error = priv_check(td, PRIV_NET_IFDESTROY);
2896
2897 if (error == 0) {
2898 sx_xlock(&ifnet_detach_sxlock);
2899 error = if_clone_destroy(ifr->ifr_name);
2900 sx_xunlock(&ifnet_detach_sxlock);
2901 }
2902 goto out_noref;
2903
2904 case SIOCIFGCLONERS:
2905 error = if_clone_list((struct if_clonereq *)data);
2906 goto out_noref;
2907
2908 case SIOCGIFGMEMB:
2909 {
2910 struct ifgroupreq *req;
2911
2912 req = (struct ifgroupreq *)data;
2913 if (strnlen(req->ifgr_name, IFNAMSIZ) == IFNAMSIZ) {
2914 error = EINVAL;
2915 goto out_noref;
2916 }
2917 error = if_getgroupmembers(req);
2918 goto out_noref;
2919 }
2920 }
2921
2922 ifp = ifunit_ref(ifr->ifr_name);
2923 if (ifp == NULL) {
2924 error = ENXIO;
2925 goto out_noref;
2926 }
2927
2928 error = ifhwioctl(cmd, ifp, data, td);
2929 if (error != ENOIOCTL)
2930 goto out_ref;
2931
2932 oif_flags = ifp->if_flags;
2933 if (so->so_proto == NULL) {
2934 error = EOPNOTSUPP;
2935 goto out_ref;
2936 }
2937
2938 /*
2939 * Pass the request on to the socket control method, and if the
2940 * latter returns EOPNOTSUPP, directly to the interface.
2941 *
2942 * Make an exception for the legacy SIOCSIF* requests. Drivers
2943 * trust SIOCSIFADDR et al to come from an already privileged
2944 * layer, and do not perform any credentials checks or input
2945 * validation.
2946 */
2947 error = so->so_proto->pr_control(so, cmd, data, ifp, td);
2948 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL &&
2949 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR &&
2950 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK)
2951 error = (*ifp->if_ioctl)(ifp, cmd, data);
2952
2953 if (!(oif_flags & IFF_UP) && (ifp->if_flags & IFF_UP))
2954 if_up(ifp);
2955 out_ref:
2956 if_rele(ifp);
2957 out_noref:
2958 CURVNET_RESTORE();
2959 #ifdef COMPAT_FREEBSD32
2960 if (error != 0)
2961 return (error);
2962 switch (saved_cmd) {
2963 case SIOCGIFCONF32:
2964 ifc32->ifc_len = thunk.ifc.ifc_len;
2965 break;
2966 case SIOCGDRVSPEC32:
2967 /*
2968 * SIOCGDRVSPEC is IOWR, but nothing actually touches
2969 * the struct so just assert that ifd_len (the only
2970 * field it might make sense to update) hasn't
2971 * changed.
2972 */
2973 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len,
2974 ("ifd_len was updated %u -> %zu", ifd32->ifd_len,
2975 thunk.ifd.ifd_len));
2976 break;
2977 case SIOCGIFGROUP32:
2978 case SIOCGIFGMEMB32:
2979 ifgr32->ifgr_len = thunk.ifgr.ifgr_len;
2980 break;
2981 case SIOCGIFMEDIA32:
2982 case SIOCGIFXMEDIA32:
2983 ifmr32->ifm_current = thunk.ifmr.ifm_current;
2984 ifmr32->ifm_mask = thunk.ifmr.ifm_mask;
2985 ifmr32->ifm_status = thunk.ifmr.ifm_status;
2986 ifmr32->ifm_active = thunk.ifmr.ifm_active;
2987 ifmr32->ifm_count = thunk.ifmr.ifm_count;
2988 break;
2989 }
2990 #endif
2991 return (error);
2992 }
2993
2994 int
if_rename(struct ifnet * ifp,char * new_name)2995 if_rename(struct ifnet *ifp, char *new_name)
2996 {
2997 struct ifaddr *ifa;
2998 struct sockaddr_dl *sdl;
2999 size_t namelen, onamelen;
3000 char old_name[IFNAMSIZ];
3001 char strbuf[IFNAMSIZ + 8];
3002
3003 if (new_name[0] == '\0')
3004 return (EINVAL);
3005 if (strcmp(new_name, ifp->if_xname) == 0)
3006 return (0);
3007 if (ifunit(new_name) != NULL)
3008 return (EEXIST);
3009
3010 if_printf(ifp, "changing name to '%s'\n", new_name);
3011
3012 IF_ADDR_WLOCK(ifp);
3013 strlcpy(old_name, ifp->if_xname, sizeof(old_name));
3014 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
3015 ifa = ifp->if_addr;
3016 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3017 namelen = strlen(new_name);
3018 onamelen = sdl->sdl_nlen;
3019 /*
3020 * Move the address if needed. This is safe because we
3021 * allocate space for a name of length IFNAMSIZ when we
3022 * create this in if_attach().
3023 */
3024 if (namelen != onamelen) {
3025 bcopy(sdl->sdl_data + onamelen,
3026 sdl->sdl_data + namelen, sdl->sdl_alen);
3027 }
3028 bcopy(new_name, sdl->sdl_data, namelen);
3029 sdl->sdl_nlen = namelen;
3030 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
3031 bzero(sdl->sdl_data, onamelen);
3032 while (namelen != 0)
3033 sdl->sdl_data[--namelen] = 0xff;
3034 IF_ADDR_WUNLOCK(ifp);
3035
3036 EVENTHANDLER_INVOKE(ifnet_rename_event, ifp, old_name);
3037
3038 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name);
3039 devctl_notify("IFNET", old_name, "RENAME", strbuf);
3040
3041 return (0);
3042 }
3043
3044 /*
3045 * The code common to handling reference counted flags,
3046 * e.g., in ifpromisc() and if_allmulti().
3047 * The "pflag" argument can specify a permanent mode flag to check,
3048 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none.
3049 *
3050 * Only to be used on stack-owned flags, not driver-owned flags.
3051 */
3052 static int
if_setflag(struct ifnet * ifp,int flag,int pflag,int * refcount,int onswitch)3053 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch)
3054 {
3055 struct ifreq ifr;
3056 int error;
3057 int oldflags, oldcount;
3058
3059 /* Sanity checks to catch programming errors */
3060 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0,
3061 ("%s: setting driver-owned flag %d", __func__, flag));
3062
3063 if (onswitch)
3064 KASSERT(*refcount >= 0,
3065 ("%s: increment negative refcount %d for flag %d",
3066 __func__, *refcount, flag));
3067 else
3068 KASSERT(*refcount > 0,
3069 ("%s: decrement non-positive refcount %d for flag %d",
3070 __func__, *refcount, flag));
3071
3072 /* In case this mode is permanent, just touch refcount */
3073 if (ifp->if_flags & pflag) {
3074 *refcount += onswitch ? 1 : -1;
3075 return (0);
3076 }
3077
3078 /* Save ifnet parameters for if_ioctl() may fail */
3079 oldcount = *refcount;
3080 oldflags = ifp->if_flags;
3081
3082 /*
3083 * See if we aren't the only and touching refcount is enough.
3084 * Actually toggle interface flag if we are the first or last.
3085 */
3086 if (onswitch) {
3087 if ((*refcount)++)
3088 return (0);
3089 ifp->if_flags |= flag;
3090 } else {
3091 if (--(*refcount))
3092 return (0);
3093 ifp->if_flags &= ~flag;
3094 }
3095
3096 /* Call down the driver since we've changed interface flags */
3097 if (ifp->if_ioctl == NULL) {
3098 error = EOPNOTSUPP;
3099 goto recover;
3100 }
3101 ifr.ifr_flags = ifp->if_flags & 0xffff;
3102 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3103 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3104 if (error)
3105 goto recover;
3106 /* Notify userland that interface flags have changed */
3107 rt_ifmsg(ifp, flag);
3108 return (0);
3109
3110 recover:
3111 /* Recover after driver error */
3112 *refcount = oldcount;
3113 ifp->if_flags = oldflags;
3114 return (error);
3115 }
3116
3117 /*
3118 * Set/clear promiscuous mode on interface ifp based on the truth value
3119 * of pswitch. The calls are reference counted so that only the first
3120 * "on" request actually has an effect, as does the final "off" request.
3121 * Results are undefined if the "off" and "on" requests are not matched.
3122 */
3123 int
ifpromisc(struct ifnet * ifp,int pswitch)3124 ifpromisc(struct ifnet *ifp, int pswitch)
3125 {
3126 int error;
3127 int oldflags = ifp->if_flags;
3128
3129 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC,
3130 &ifp->if_pcount, pswitch);
3131 /* If promiscuous mode status has changed, log a message */
3132 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) &&
3133 log_promisc_mode_change)
3134 if_printf(ifp, "promiscuous mode %s\n",
3135 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
3136 return (error);
3137 }
3138
3139 /*
3140 * Return interface configuration
3141 * of system. List may be used
3142 * in later ioctl's (above) to get
3143 * other information.
3144 */
3145 /*ARGSUSED*/
3146 static int
ifconf(u_long cmd,caddr_t data)3147 ifconf(u_long cmd, caddr_t data)
3148 {
3149 struct ifconf *ifc = (struct ifconf *)data;
3150 struct ifnet *ifp;
3151 struct ifaddr *ifa;
3152 struct ifreq ifr;
3153 struct sbuf *sb;
3154 int error, full = 0, valid_len, max_len;
3155
3156 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */
3157 max_len = maxphys - 1;
3158
3159 /* Prevent hostile input from being able to crash the system */
3160 if (ifc->ifc_len <= 0)
3161 return (EINVAL);
3162
3163 again:
3164 if (ifc->ifc_len <= max_len) {
3165 max_len = ifc->ifc_len;
3166 full = 1;
3167 }
3168 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN);
3169 max_len = 0;
3170 valid_len = 0;
3171
3172 IFNET_RLOCK();
3173 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
3174 struct epoch_tracker et;
3175 int addrs;
3176
3177 /*
3178 * Zero the ifr to make sure we don't disclose the contents
3179 * of the stack.
3180 */
3181 memset(&ifr, 0, sizeof(ifr));
3182
3183 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
3184 >= sizeof(ifr.ifr_name)) {
3185 sbuf_delete(sb);
3186 IFNET_RUNLOCK();
3187 return (ENAMETOOLONG);
3188 }
3189
3190 addrs = 0;
3191 NET_EPOCH_ENTER(et);
3192 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3193 struct sockaddr *sa = ifa->ifa_addr;
3194
3195 if (prison_if(curthread->td_ucred, sa) != 0)
3196 continue;
3197 addrs++;
3198 if (sa->sa_len <= sizeof(*sa)) {
3199 if (sa->sa_len < sizeof(*sa)) {
3200 memset(&ifr.ifr_ifru.ifru_addr, 0,
3201 sizeof(ifr.ifr_ifru.ifru_addr));
3202 memcpy(&ifr.ifr_ifru.ifru_addr, sa,
3203 sa->sa_len);
3204 } else
3205 ifr.ifr_ifru.ifru_addr = *sa;
3206 sbuf_bcat(sb, &ifr, sizeof(ifr));
3207 max_len += sizeof(ifr);
3208 } else {
3209 sbuf_bcat(sb, &ifr,
3210 offsetof(struct ifreq, ifr_addr));
3211 max_len += offsetof(struct ifreq, ifr_addr);
3212 sbuf_bcat(sb, sa, sa->sa_len);
3213 max_len += sa->sa_len;
3214 }
3215
3216 if (sbuf_error(sb) == 0)
3217 valid_len = sbuf_len(sb);
3218 }
3219 NET_EPOCH_EXIT(et);
3220 if (addrs == 0) {
3221 sbuf_bcat(sb, &ifr, sizeof(ifr));
3222 max_len += sizeof(ifr);
3223
3224 if (sbuf_error(sb) == 0)
3225 valid_len = sbuf_len(sb);
3226 }
3227 }
3228 IFNET_RUNLOCK();
3229
3230 /*
3231 * If we didn't allocate enough space (uncommon), try again. If
3232 * we have already allocated as much space as we are allowed,
3233 * return what we've got.
3234 */
3235 if (valid_len != max_len && !full) {
3236 sbuf_delete(sb);
3237 goto again;
3238 }
3239
3240 ifc->ifc_len = valid_len;
3241 sbuf_finish(sb);
3242 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len);
3243 sbuf_delete(sb);
3244 return (error);
3245 }
3246
3247 /*
3248 * Just like ifpromisc(), but for all-multicast-reception mode.
3249 */
3250 int
if_allmulti(struct ifnet * ifp,int onswitch)3251 if_allmulti(struct ifnet *ifp, int onswitch)
3252 {
3253
3254 return (if_setflag(ifp, IFF_ALLMULTI, IFF_PALLMULTI, &ifp->if_amcount,
3255 onswitch));
3256 }
3257
3258 struct ifmultiaddr *
if_findmulti(struct ifnet * ifp,const struct sockaddr * sa)3259 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa)
3260 {
3261 struct ifmultiaddr *ifma;
3262
3263 IF_ADDR_LOCK_ASSERT(ifp);
3264
3265 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3266 if (sa->sa_family == AF_LINK) {
3267 if (sa_dl_equal(ifma->ifma_addr, sa))
3268 break;
3269 } else {
3270 if (sa_equal(ifma->ifma_addr, sa))
3271 break;
3272 }
3273 }
3274
3275 return ifma;
3276 }
3277
3278 /*
3279 * Allocate a new ifmultiaddr and initialize based on passed arguments. We
3280 * make copies of passed sockaddrs. The ifmultiaddr will not be added to
3281 * the ifnet multicast address list here, so the caller must do that and
3282 * other setup work (such as notifying the device driver). The reference
3283 * count is initialized to 1.
3284 */
3285 static struct ifmultiaddr *
if_allocmulti(struct ifnet * ifp,struct sockaddr * sa,struct sockaddr * llsa,int mflags)3286 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa,
3287 int mflags)
3288 {
3289 struct ifmultiaddr *ifma;
3290 struct sockaddr *dupsa;
3291
3292 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags |
3293 M_ZERO);
3294 if (ifma == NULL)
3295 return (NULL);
3296
3297 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags);
3298 if (dupsa == NULL) {
3299 free(ifma, M_IFMADDR);
3300 return (NULL);
3301 }
3302 bcopy(sa, dupsa, sa->sa_len);
3303 ifma->ifma_addr = dupsa;
3304
3305 ifma->ifma_ifp = ifp;
3306 ifma->ifma_refcount = 1;
3307 ifma->ifma_protospec = NULL;
3308
3309 if (llsa == NULL) {
3310 ifma->ifma_lladdr = NULL;
3311 return (ifma);
3312 }
3313
3314 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags);
3315 if (dupsa == NULL) {
3316 free(ifma->ifma_addr, M_IFMADDR);
3317 free(ifma, M_IFMADDR);
3318 return (NULL);
3319 }
3320 bcopy(llsa, dupsa, llsa->sa_len);
3321 ifma->ifma_lladdr = dupsa;
3322
3323 return (ifma);
3324 }
3325
3326 /*
3327 * if_freemulti: free ifmultiaddr structure and possibly attached related
3328 * addresses. The caller is responsible for implementing reference
3329 * counting, notifying the driver, handling routing messages, and releasing
3330 * any dependent link layer state.
3331 */
3332 #ifdef MCAST_VERBOSE
3333 extern void kdb_backtrace(void);
3334 #endif
3335 static void
if_freemulti_internal(struct ifmultiaddr * ifma)3336 if_freemulti_internal(struct ifmultiaddr *ifma)
3337 {
3338
3339 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d",
3340 ifma->ifma_refcount));
3341
3342 if (ifma->ifma_lladdr != NULL)
3343 free(ifma->ifma_lladdr, M_IFMADDR);
3344 #ifdef MCAST_VERBOSE
3345 kdb_backtrace();
3346 printf("%s freeing ifma: %p\n", __func__, ifma);
3347 #endif
3348 free(ifma->ifma_addr, M_IFMADDR);
3349 free(ifma, M_IFMADDR);
3350 }
3351
3352 static void
if_destroymulti(epoch_context_t ctx)3353 if_destroymulti(epoch_context_t ctx)
3354 {
3355 struct ifmultiaddr *ifma;
3356
3357 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx);
3358 if_freemulti_internal(ifma);
3359 }
3360
3361 void
if_freemulti(struct ifmultiaddr * ifma)3362 if_freemulti(struct ifmultiaddr *ifma)
3363 {
3364 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d",
3365 ifma->ifma_refcount));
3366
3367 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx);
3368 }
3369
3370 /*
3371 * Register an additional multicast address with a network interface.
3372 *
3373 * - If the address is already present, bump the reference count on the
3374 * address and return.
3375 * - If the address is not link-layer, look up a link layer address.
3376 * - Allocate address structures for one or both addresses, and attach to the
3377 * multicast address list on the interface. If automatically adding a link
3378 * layer address, the protocol address will own a reference to the link
3379 * layer address, to be freed when it is freed.
3380 * - Notify the network device driver of an addition to the multicast address
3381 * list.
3382 *
3383 * 'sa' points to caller-owned memory with the desired multicast address.
3384 *
3385 * 'retifma' will be used to return a pointer to the resulting multicast
3386 * address reference, if desired.
3387 */
3388 int
if_addmulti(struct ifnet * ifp,struct sockaddr * sa,struct ifmultiaddr ** retifma)3389 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
3390 struct ifmultiaddr **retifma)
3391 {
3392 struct ifmultiaddr *ifma, *ll_ifma;
3393 struct sockaddr *llsa;
3394 struct sockaddr_dl sdl;
3395 int error;
3396
3397 #ifdef INET
3398 IN_MULTI_LIST_UNLOCK_ASSERT();
3399 #endif
3400 #ifdef INET6
3401 IN6_MULTI_LIST_UNLOCK_ASSERT();
3402 #endif
3403 /*
3404 * If the address is already present, return a new reference to it;
3405 * otherwise, allocate storage and set up a new address.
3406 */
3407 IF_ADDR_WLOCK(ifp);
3408 ifma = if_findmulti(ifp, sa);
3409 if (ifma != NULL) {
3410 ifma->ifma_refcount++;
3411 if (retifma != NULL)
3412 *retifma = ifma;
3413 IF_ADDR_WUNLOCK(ifp);
3414 return (0);
3415 }
3416
3417 /*
3418 * The address isn't already present; resolve the protocol address
3419 * into a link layer address, and then look that up, bump its
3420 * refcount or allocate an ifma for that also.
3421 * Most link layer resolving functions returns address data which
3422 * fits inside default sockaddr_dl structure. However callback
3423 * can allocate another sockaddr structure, in that case we need to
3424 * free it later.
3425 */
3426 llsa = NULL;
3427 ll_ifma = NULL;
3428 if (ifp->if_resolvemulti != NULL) {
3429 /* Provide called function with buffer size information */
3430 sdl.sdl_len = sizeof(sdl);
3431 llsa = (struct sockaddr *)&sdl;
3432 error = ifp->if_resolvemulti(ifp, &llsa, sa);
3433 if (error)
3434 goto unlock_out;
3435 }
3436
3437 /*
3438 * Allocate the new address. Don't hook it up yet, as we may also
3439 * need to allocate a link layer multicast address.
3440 */
3441 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT);
3442 if (ifma == NULL) {
3443 error = ENOMEM;
3444 goto free_llsa_out;
3445 }
3446
3447 /*
3448 * If a link layer address is found, we'll need to see if it's
3449 * already present in the address list, or allocate is as well.
3450 * When this block finishes, the link layer address will be on the
3451 * list.
3452 */
3453 if (llsa != NULL) {
3454 ll_ifma = if_findmulti(ifp, llsa);
3455 if (ll_ifma == NULL) {
3456 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT);
3457 if (ll_ifma == NULL) {
3458 --ifma->ifma_refcount;
3459 if_freemulti(ifma);
3460 error = ENOMEM;
3461 goto free_llsa_out;
3462 }
3463 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED;
3464 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma,
3465 ifma_link);
3466 } else
3467 ll_ifma->ifma_refcount++;
3468 ifma->ifma_llifma = ll_ifma;
3469 }
3470
3471 /*
3472 * We now have a new multicast address, ifma, and possibly a new or
3473 * referenced link layer address. Add the primary address to the
3474 * ifnet address list.
3475 */
3476 ifma->ifma_flags |= IFMA_F_ENQUEUED;
3477 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
3478
3479 if (retifma != NULL)
3480 *retifma = ifma;
3481
3482 /*
3483 * Must generate the message while holding the lock so that 'ifma'
3484 * pointer is still valid.
3485 */
3486 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
3487 IF_ADDR_WUNLOCK(ifp);
3488
3489 /*
3490 * We are certain we have added something, so call down to the
3491 * interface to let them know about it.
3492 */
3493 if (ifp->if_ioctl != NULL) {
3494 if (THREAD_CAN_SLEEP())
3495 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3496 else
3497 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask);
3498 }
3499
3500 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3501 link_free_sdl(llsa);
3502
3503 return (0);
3504
3505 free_llsa_out:
3506 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl))
3507 link_free_sdl(llsa);
3508
3509 unlock_out:
3510 IF_ADDR_WUNLOCK(ifp);
3511 return (error);
3512 }
3513
3514 static void
if_siocaddmulti(void * arg,int pending)3515 if_siocaddmulti(void *arg, int pending)
3516 {
3517 struct ifnet *ifp;
3518
3519 ifp = arg;
3520 #ifdef DIAGNOSTIC
3521 if (pending > 1)
3522 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending);
3523 #endif
3524 CURVNET_SET(ifp->if_vnet);
3525 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0);
3526 CURVNET_RESTORE();
3527 }
3528
3529 /*
3530 * Delete a multicast group membership by network-layer group address.
3531 *
3532 * Returns ENOENT if the entry could not be found. If ifp no longer
3533 * exists, results are undefined. This entry point should only be used
3534 * from subsystems which do appropriate locking to hold ifp for the
3535 * duration of the call.
3536 * Network-layer protocol domains must use if_delmulti_ifma().
3537 */
3538 int
if_delmulti(struct ifnet * ifp,struct sockaddr * sa)3539 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
3540 {
3541 struct ifmultiaddr *ifma;
3542 int lastref;
3543
3544 KASSERT(ifp, ("%s: NULL ifp", __func__));
3545
3546 IF_ADDR_WLOCK(ifp);
3547 lastref = 0;
3548 ifma = if_findmulti(ifp, sa);
3549 if (ifma != NULL)
3550 lastref = if_delmulti_locked(ifp, ifma, 0);
3551 IF_ADDR_WUNLOCK(ifp);
3552
3553 if (ifma == NULL)
3554 return (ENOENT);
3555
3556 if (lastref && ifp->if_ioctl != NULL) {
3557 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3558 }
3559
3560 return (0);
3561 }
3562
3563 /*
3564 * Delete all multicast group membership for an interface.
3565 * Should be used to quickly flush all multicast filters.
3566 */
3567 void
if_delallmulti(struct ifnet * ifp)3568 if_delallmulti(struct ifnet *ifp)
3569 {
3570 struct ifmultiaddr *ifma;
3571 struct ifmultiaddr *next;
3572
3573 IF_ADDR_WLOCK(ifp);
3574 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next)
3575 if_delmulti_locked(ifp, ifma, 0);
3576 IF_ADDR_WUNLOCK(ifp);
3577 }
3578
3579 void
if_delmulti_ifma(struct ifmultiaddr * ifma)3580 if_delmulti_ifma(struct ifmultiaddr *ifma)
3581 {
3582 if_delmulti_ifma_flags(ifma, 0);
3583 }
3584
3585 /*
3586 * Delete a multicast group membership by group membership pointer.
3587 * Network-layer protocol domains must use this routine.
3588 *
3589 * It is safe to call this routine if the ifp disappeared.
3590 */
3591 void
if_delmulti_ifma_flags(struct ifmultiaddr * ifma,int flags)3592 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags)
3593 {
3594 struct ifnet *ifp;
3595 int lastref;
3596 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma);
3597 #ifdef INET
3598 IN_MULTI_LIST_UNLOCK_ASSERT();
3599 #endif
3600 ifp = ifma->ifma_ifp;
3601 #ifdef DIAGNOSTIC
3602 if (ifp == NULL) {
3603 printf("%s: ifma_ifp seems to be detached\n", __func__);
3604 } else {
3605 struct epoch_tracker et;
3606 struct ifnet *oifp;
3607
3608 NET_EPOCH_ENTER(et);
3609 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link)
3610 if (ifp == oifp)
3611 break;
3612 NET_EPOCH_EXIT(et);
3613 if (ifp != oifp)
3614 ifp = NULL;
3615 }
3616 #endif
3617 /*
3618 * If and only if the ifnet instance exists: Acquire the address lock.
3619 */
3620 if (ifp != NULL)
3621 IF_ADDR_WLOCK(ifp);
3622
3623 lastref = if_delmulti_locked(ifp, ifma, flags);
3624
3625 if (ifp != NULL) {
3626 /*
3627 * If and only if the ifnet instance exists:
3628 * Release the address lock.
3629 * If the group was left: update the hardware hash filter.
3630 */
3631 IF_ADDR_WUNLOCK(ifp);
3632 if (lastref && ifp->if_ioctl != NULL) {
3633 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0);
3634 }
3635 }
3636 }
3637
3638 /*
3639 * Perform deletion of network-layer and/or link-layer multicast address.
3640 *
3641 * Return 0 if the reference count was decremented.
3642 * Return 1 if the final reference was released, indicating that the
3643 * hardware hash filter should be reprogrammed.
3644 */
3645 static int
if_delmulti_locked(struct ifnet * ifp,struct ifmultiaddr * ifma,int detaching)3646 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching)
3647 {
3648 struct ifmultiaddr *ll_ifma;
3649
3650 if (ifp != NULL && ifma->ifma_ifp != NULL) {
3651 KASSERT(ifma->ifma_ifp == ifp,
3652 ("%s: inconsistent ifp %p", __func__, ifp));
3653 IF_ADDR_WLOCK_ASSERT(ifp);
3654 }
3655
3656 ifp = ifma->ifma_ifp;
3657 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : "");
3658
3659 /*
3660 * If the ifnet is detaching, null out references to ifnet,
3661 * so that upper protocol layers will notice, and not attempt
3662 * to obtain locks for an ifnet which no longer exists. The
3663 * routing socket announcement must happen before the ifnet
3664 * instance is detached from the system.
3665 */
3666 if (detaching) {
3667 #ifdef DIAGNOSTIC
3668 printf("%s: detaching ifnet instance %p\n", __func__, ifp);
3669 #endif
3670 /*
3671 * ifp may already be nulled out if we are being reentered
3672 * to delete the ll_ifma.
3673 */
3674 if (ifp != NULL) {
3675 rt_newmaddrmsg(RTM_DELMADDR, ifma);
3676 ifma->ifma_ifp = NULL;
3677 }
3678 }
3679
3680 if (--ifma->ifma_refcount > 0)
3681 return 0;
3682
3683 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) {
3684 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
3685 ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3686 }
3687 /*
3688 * If this ifma is a network-layer ifma, a link-layer ifma may
3689 * have been associated with it. Release it first if so.
3690 */
3691 ll_ifma = ifma->ifma_llifma;
3692 if (ll_ifma != NULL) {
3693 KASSERT(ifma->ifma_lladdr != NULL,
3694 ("%s: llifma w/o lladdr", __func__));
3695 if (detaching)
3696 ll_ifma->ifma_ifp = NULL; /* XXX */
3697 if (--ll_ifma->ifma_refcount == 0) {
3698 if (ifp != NULL) {
3699 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) {
3700 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr,
3701 ifma_link);
3702 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED;
3703 }
3704 }
3705 if_freemulti(ll_ifma);
3706 }
3707 }
3708 #ifdef INVARIANTS
3709 if (ifp) {
3710 struct ifmultiaddr *ifmatmp;
3711
3712 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link)
3713 MPASS(ifma != ifmatmp);
3714 }
3715 #endif
3716 if_freemulti(ifma);
3717 /*
3718 * The last reference to this instance of struct ifmultiaddr
3719 * was released; the hardware should be notified of this change.
3720 */
3721 return 1;
3722 }
3723
3724 /*
3725 * Set the link layer address on an interface.
3726 *
3727 * At this time we only support certain types of interfaces,
3728 * and we don't allow the length of the address to change.
3729 *
3730 * Set noinline to be dtrace-friendly
3731 */
3732 __noinline int
if_setlladdr(struct ifnet * ifp,const u_char * lladdr,int len)3733 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
3734 {
3735 struct sockaddr_dl *sdl;
3736 struct ifaddr *ifa;
3737 struct ifreq ifr;
3738
3739 ifa = ifp->if_addr;
3740 if (ifa == NULL)
3741 return (EINVAL);
3742
3743 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3744 if (sdl == NULL)
3745 return (EINVAL);
3746
3747 if (len != sdl->sdl_alen) /* don't allow length to change */
3748 return (EINVAL);
3749
3750 switch (ifp->if_type) {
3751 case IFT_ETHER:
3752 case IFT_XETHER:
3753 case IFT_L2VLAN:
3754 case IFT_BRIDGE:
3755 case IFT_IEEE8023ADLAG:
3756 bcopy(lladdr, LLADDR(sdl), len);
3757 break;
3758 default:
3759 return (ENODEV);
3760 }
3761
3762 /*
3763 * If the interface is already up, we need
3764 * to re-init it in order to reprogram its
3765 * address filter.
3766 */
3767 if ((ifp->if_flags & IFF_UP) != 0) {
3768 if (ifp->if_ioctl) {
3769 ifp->if_flags &= ~IFF_UP;
3770 ifr.ifr_flags = ifp->if_flags & 0xffff;
3771 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3772 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3773 ifp->if_flags |= IFF_UP;
3774 ifr.ifr_flags = ifp->if_flags & 0xffff;
3775 ifr.ifr_flagshigh = ifp->if_flags >> 16;
3776 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
3777 }
3778 }
3779 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3780
3781 return (0);
3782 }
3783
3784 /*
3785 * Compat function for handling basic encapsulation requests.
3786 * Not converted stacks (FDDI, IB, ..) supports traditional
3787 * output model: ARP (and other similar L2 protocols) are handled
3788 * inside output routine, arpresolve/nd6_resolve() returns MAC
3789 * address instead of full prepend.
3790 *
3791 * This function creates calculated header==MAC for IPv4/IPv6 and
3792 * returns EAFNOSUPPORT (which is then handled in ARP code) for other
3793 * address families.
3794 */
3795 static int
if_requestencap_default(struct ifnet * ifp,struct if_encap_req * req)3796 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req)
3797 {
3798 if (req->rtype != IFENCAP_LL)
3799 return (EOPNOTSUPP);
3800
3801 if (req->bufsize < req->lladdr_len)
3802 return (ENOMEM);
3803
3804 switch (req->family) {
3805 case AF_INET:
3806 case AF_INET6:
3807 break;
3808 default:
3809 return (EAFNOSUPPORT);
3810 }
3811
3812 /* Copy lladdr to storage as is */
3813 memmove(req->buf, req->lladdr, req->lladdr_len);
3814 req->bufsize = req->lladdr_len;
3815 req->lladdr_off = 0;
3816
3817 return (0);
3818 }
3819
3820 /*
3821 * Tunnel interfaces can nest, also they may cause infinite recursion
3822 * calls when misconfigured. We'll prevent this by detecting loops.
3823 * High nesting level may cause stack exhaustion. We'll prevent this
3824 * by introducing upper limit.
3825 *
3826 * Return 0, if tunnel nesting count is equal or less than limit.
3827 */
3828 int
if_tunnel_check_nesting(struct ifnet * ifp,struct mbuf * m,uint32_t cookie,int limit)3829 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie,
3830 int limit)
3831 {
3832 struct m_tag *mtag;
3833 int count;
3834
3835 count = 1;
3836 mtag = NULL;
3837 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) {
3838 if (*(struct ifnet **)(mtag + 1) == ifp) {
3839 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
3840 return (EIO);
3841 }
3842 count++;
3843 }
3844 if (count > limit) {
3845 log(LOG_NOTICE,
3846 "%s: if_output recursively called too many times(%d)\n",
3847 if_name(ifp), count);
3848 return (EIO);
3849 }
3850 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT);
3851 if (mtag == NULL)
3852 return (ENOMEM);
3853 *(struct ifnet **)(mtag + 1) = ifp;
3854 m_tag_prepend(m, mtag);
3855 return (0);
3856 }
3857
3858 /*
3859 * Get the link layer address that was read from the hardware at attach.
3860 *
3861 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type
3862 * their component interfaces as IFT_IEEE8023ADLAG.
3863 */
3864 int
if_gethwaddr(struct ifnet * ifp,struct ifreq * ifr)3865 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr)
3866 {
3867 if (ifp->if_hw_addr == NULL)
3868 return (ENODEV);
3869
3870 switch (ifp->if_type) {
3871 case IFT_ETHER:
3872 case IFT_IEEE8023ADLAG:
3873 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen);
3874 return (0);
3875 default:
3876 return (ENODEV);
3877 }
3878 }
3879
3880 /*
3881 * The name argument must be a pointer to storage which will last as
3882 * long as the interface does. For physical devices, the result of
3883 * device_get_name(dev) is a good choice and for pseudo-devices a
3884 * static string works well.
3885 */
3886 void
if_initname(struct ifnet * ifp,const char * name,int unit)3887 if_initname(struct ifnet *ifp, const char *name, int unit)
3888 {
3889 ifp->if_dname = name;
3890 ifp->if_dunit = unit;
3891 if (unit != IF_DUNIT_NONE)
3892 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
3893 else
3894 strlcpy(ifp->if_xname, name, IFNAMSIZ);
3895 }
3896
3897 static int
if_vlog(struct ifnet * ifp,int pri,const char * fmt,va_list ap)3898 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
3899 {
3900 char if_fmt[256];
3901
3902 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
3903 vlog(pri, if_fmt, ap);
3904 return (0);
3905 }
3906
3907
3908 int
if_printf(struct ifnet * ifp,const char * fmt,...)3909 if_printf(struct ifnet *ifp, const char *fmt, ...)
3910 {
3911 va_list ap;
3912
3913 va_start(ap, fmt);
3914 if_vlog(ifp, LOG_INFO, fmt, ap);
3915 va_end(ap);
3916 return (0);
3917 }
3918
3919 int
if_log(struct ifnet * ifp,int pri,const char * fmt,...)3920 if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
3921 {
3922 va_list ap;
3923
3924 va_start(ap, fmt);
3925 if_vlog(ifp, pri, fmt, ap);
3926 va_end(ap);
3927 return (0);
3928 }
3929
3930 void
if_start(struct ifnet * ifp)3931 if_start(struct ifnet *ifp)
3932 {
3933
3934 (*(ifp)->if_start)(ifp);
3935 }
3936
3937 /*
3938 * Backwards compatibility interface for drivers
3939 * that have not implemented it
3940 */
3941 static int
if_transmit_default(struct ifnet * ifp,struct mbuf * m)3942 if_transmit_default(struct ifnet *ifp, struct mbuf *m)
3943 {
3944 int error;
3945
3946 IFQ_HANDOFF(ifp, m, error);
3947 return (error);
3948 }
3949
3950 static void
if_input_default(struct ifnet * ifp __unused,struct mbuf * m)3951 if_input_default(struct ifnet *ifp __unused, struct mbuf *m)
3952 {
3953 m_freem(m);
3954 }
3955
3956 int
if_handoff(struct ifqueue * ifq,struct mbuf * m,struct ifnet * ifp,int adjust)3957 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
3958 {
3959 int active = 0;
3960
3961 IF_LOCK(ifq);
3962 if (_IF_QFULL(ifq)) {
3963 IF_UNLOCK(ifq);
3964 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
3965 m_freem(m);
3966 return (0);
3967 }
3968 if (ifp != NULL) {
3969 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust);
3970 if (m->m_flags & (M_BCAST|M_MCAST))
3971 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3972 active = ifp->if_drv_flags & IFF_DRV_OACTIVE;
3973 }
3974 _IF_ENQUEUE(ifq, m);
3975 IF_UNLOCK(ifq);
3976 if (ifp != NULL && !active)
3977 (*(ifp)->if_start)(ifp);
3978 return (1);
3979 }
3980
3981 void
if_register_com_alloc(u_char type,if_com_alloc_t * a,if_com_free_t * f)3982 if_register_com_alloc(u_char type,
3983 if_com_alloc_t *a, if_com_free_t *f)
3984 {
3985
3986 KASSERT(if_com_alloc[type] == NULL,
3987 ("if_register_com_alloc: %d already registered", type));
3988 KASSERT(if_com_free[type] == NULL,
3989 ("if_register_com_alloc: %d free already registered", type));
3990
3991 if_com_alloc[type] = a;
3992 if_com_free[type] = f;
3993 }
3994
3995 void
if_deregister_com_alloc(u_char type)3996 if_deregister_com_alloc(u_char type)
3997 {
3998
3999 KASSERT(if_com_alloc[type] != NULL,
4000 ("if_deregister_com_alloc: %d not registered", type));
4001 KASSERT(if_com_free[type] != NULL,
4002 ("if_deregister_com_alloc: %d free not registered", type));
4003
4004 /*
4005 * Ensure all pending EPOCH(9) callbacks have been executed. This
4006 * fixes issues about late invocation of if_destroy(), which leads
4007 * to memory leak from if_com_alloc[type] allocated if_l2com.
4008 */
4009 NET_EPOCH_DRAIN_CALLBACKS();
4010
4011 if_com_alloc[type] = NULL;
4012 if_com_free[type] = NULL;
4013 }
4014
4015 /* API for driver access to network stack owned ifnet.*/
4016 uint64_t
if_setbaudrate(struct ifnet * ifp,uint64_t baudrate)4017 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate)
4018 {
4019 uint64_t oldbrate;
4020
4021 oldbrate = ifp->if_baudrate;
4022 ifp->if_baudrate = baudrate;
4023 return (oldbrate);
4024 }
4025
4026 uint64_t
if_getbaudrate(const if_t ifp)4027 if_getbaudrate(const if_t ifp)
4028 {
4029 return (ifp->if_baudrate);
4030 }
4031
4032 int
if_setcapabilities(if_t ifp,int capabilities)4033 if_setcapabilities(if_t ifp, int capabilities)
4034 {
4035 ifp->if_capabilities = capabilities;
4036 return (0);
4037 }
4038
4039 int
if_setcapabilitiesbit(if_t ifp,int setbit,int clearbit)4040 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit)
4041 {
4042 ifp->if_capabilities &= ~clearbit;
4043 ifp->if_capabilities |= setbit;
4044 return (0);
4045 }
4046
4047 int
if_getcapabilities(const if_t ifp)4048 if_getcapabilities(const if_t ifp)
4049 {
4050 return (ifp->if_capabilities);
4051 }
4052
4053 int
if_setcapenable(if_t ifp,int capabilities)4054 if_setcapenable(if_t ifp, int capabilities)
4055 {
4056 ifp->if_capenable = capabilities;
4057 return (0);
4058 }
4059
4060 int
if_setcapenablebit(if_t ifp,int setcap,int clearcap)4061 if_setcapenablebit(if_t ifp, int setcap, int clearcap)
4062 {
4063 ifp->if_capenable &= ~clearcap;
4064 ifp->if_capenable |= setcap;
4065 return (0);
4066 }
4067
4068 int
if_setcapabilities2(if_t ifp,int capabilities)4069 if_setcapabilities2(if_t ifp, int capabilities)
4070 {
4071 ifp->if_capabilities2 = capabilities;
4072 return (0);
4073 }
4074
4075 int
if_setcapabilities2bit(if_t ifp,int setbit,int clearbit)4076 if_setcapabilities2bit(if_t ifp, int setbit, int clearbit)
4077 {
4078 ifp->if_capabilities2 &= ~clearbit;
4079 ifp->if_capabilities2 |= setbit;
4080 return (0);
4081 }
4082
4083 int
if_getcapabilities2(const if_t ifp)4084 if_getcapabilities2(const if_t ifp)
4085 {
4086 return (ifp->if_capabilities2);
4087 }
4088
4089 int
if_setcapenable2(if_t ifp,int capabilities2)4090 if_setcapenable2(if_t ifp, int capabilities2)
4091 {
4092 ifp->if_capenable2 = capabilities2;
4093 return (0);
4094 }
4095
4096 int
if_setcapenable2bit(if_t ifp,int setcap,int clearcap)4097 if_setcapenable2bit(if_t ifp, int setcap, int clearcap)
4098 {
4099 ifp->if_capenable2 &= ~clearcap;
4100 ifp->if_capenable2 |= setcap;
4101 return (0);
4102 }
4103
4104 const char *
if_getdname(const if_t ifp)4105 if_getdname(const if_t ifp)
4106 {
4107 return (ifp->if_dname);
4108 }
4109
4110 void
if_setdname(if_t ifp,const char * dname)4111 if_setdname(if_t ifp, const char *dname)
4112 {
4113 ifp->if_dname = dname;
4114 }
4115
4116 const char *
if_name(if_t ifp)4117 if_name(if_t ifp)
4118 {
4119 return (ifp->if_xname);
4120 }
4121
4122 int
if_setname(if_t ifp,const char * name)4123 if_setname(if_t ifp, const char *name)
4124 {
4125 if (strlen(name) > sizeof(ifp->if_xname) - 1)
4126 return (ENAMETOOLONG);
4127 strcpy(ifp->if_xname, name);
4128
4129 return (0);
4130 }
4131
4132 int
if_togglecapenable(if_t ifp,int togglecap)4133 if_togglecapenable(if_t ifp, int togglecap)
4134 {
4135 ifp->if_capenable ^= togglecap;
4136 return (0);
4137 }
4138
4139 int
if_getcapenable(const if_t ifp)4140 if_getcapenable(const if_t ifp)
4141 {
4142 return (ifp->if_capenable);
4143 }
4144
4145 int
if_togglecapenable2(if_t ifp,int togglecap)4146 if_togglecapenable2(if_t ifp, int togglecap)
4147 {
4148 ifp->if_capenable2 ^= togglecap;
4149 return (0);
4150 }
4151
4152 int
if_getcapenable2(const if_t ifp)4153 if_getcapenable2(const if_t ifp)
4154 {
4155 return (ifp->if_capenable2);
4156 }
4157
4158 int
if_getdunit(const if_t ifp)4159 if_getdunit(const if_t ifp)
4160 {
4161 return (ifp->if_dunit);
4162 }
4163
4164 int
if_getindex(const if_t ifp)4165 if_getindex(const if_t ifp)
4166 {
4167 return (ifp->if_index);
4168 }
4169
4170 int
if_getidxgen(const if_t ifp)4171 if_getidxgen(const if_t ifp)
4172 {
4173 return (ifp->if_idxgen);
4174 }
4175
4176 const char *
if_getdescr(if_t ifp)4177 if_getdescr(if_t ifp)
4178 {
4179 return (ifp->if_description);
4180 }
4181
4182 void
if_setdescr(if_t ifp,char * descrbuf)4183 if_setdescr(if_t ifp, char *descrbuf)
4184 {
4185 sx_xlock(&ifdescr_sx);
4186 char *odescrbuf = ifp->if_description;
4187 ifp->if_description = descrbuf;
4188 sx_xunlock(&ifdescr_sx);
4189
4190 if_freedescr(odescrbuf);
4191 }
4192
4193 char *
if_allocdescr(size_t sz,int malloc_flag)4194 if_allocdescr(size_t sz, int malloc_flag)
4195 {
4196 malloc_flag &= (M_WAITOK | M_NOWAIT);
4197 return (malloc(sz, M_IFDESCR, M_ZERO | malloc_flag));
4198 }
4199
4200 void
if_freedescr(char * descrbuf)4201 if_freedescr(char *descrbuf)
4202 {
4203 free(descrbuf, M_IFDESCR);
4204 }
4205
4206 int
if_getalloctype(const if_t ifp)4207 if_getalloctype(const if_t ifp)
4208 {
4209 return (ifp->if_alloctype);
4210 }
4211
4212 void
if_setlastchange(if_t ifp)4213 if_setlastchange(if_t ifp)
4214 {
4215 getmicrotime(&ifp->if_lastchange);
4216 }
4217
4218 /*
4219 * This is largely undesirable because it ties ifnet to a device, but does
4220 * provide flexiblity for an embedded product vendor. Should be used with
4221 * the understanding that it violates the interface boundaries, and should be
4222 * a last resort only.
4223 */
4224 int
if_setdev(if_t ifp,void * dev)4225 if_setdev(if_t ifp, void *dev)
4226 {
4227 return (0);
4228 }
4229
4230 int
if_setdrvflagbits(if_t ifp,int set_flags,int clear_flags)4231 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags)
4232 {
4233 ifp->if_drv_flags &= ~clear_flags;
4234 ifp->if_drv_flags |= set_flags;
4235
4236 return (0);
4237 }
4238
4239 int
if_getdrvflags(const if_t ifp)4240 if_getdrvflags(const if_t ifp)
4241 {
4242 return (ifp->if_drv_flags);
4243 }
4244
4245 int
if_setdrvflags(if_t ifp,int flags)4246 if_setdrvflags(if_t ifp, int flags)
4247 {
4248 ifp->if_drv_flags = flags;
4249 return (0);
4250 }
4251
4252 int
if_setflags(if_t ifp,int flags)4253 if_setflags(if_t ifp, int flags)
4254 {
4255 ifp->if_flags = flags;
4256 return (0);
4257 }
4258
4259 int
if_setflagbits(if_t ifp,int set,int clear)4260 if_setflagbits(if_t ifp, int set, int clear)
4261 {
4262 ifp->if_flags &= ~clear;
4263 ifp->if_flags |= set;
4264 return (0);
4265 }
4266
4267 int
if_getflags(const if_t ifp)4268 if_getflags(const if_t ifp)
4269 {
4270 return (ifp->if_flags);
4271 }
4272
4273 int
if_clearhwassist(if_t ifp)4274 if_clearhwassist(if_t ifp)
4275 {
4276 ifp->if_hwassist = 0;
4277 return (0);
4278 }
4279
4280 int
if_sethwassistbits(if_t ifp,int toset,int toclear)4281 if_sethwassistbits(if_t ifp, int toset, int toclear)
4282 {
4283 ifp->if_hwassist &= ~toclear;
4284 ifp->if_hwassist |= toset;
4285
4286 return (0);
4287 }
4288
4289 int
if_sethwassist(if_t ifp,int hwassist_bit)4290 if_sethwassist(if_t ifp, int hwassist_bit)
4291 {
4292 ifp->if_hwassist = hwassist_bit;
4293 return (0);
4294 }
4295
4296 int
if_gethwassist(const if_t ifp)4297 if_gethwassist(const if_t ifp)
4298 {
4299 return (ifp->if_hwassist);
4300 }
4301
4302 int
if_togglehwassist(if_t ifp,int toggle_bits)4303 if_togglehwassist(if_t ifp, int toggle_bits)
4304 {
4305 ifp->if_hwassist ^= toggle_bits;
4306 return (0);
4307 }
4308
4309 int
if_setmtu(if_t ifp,int mtu)4310 if_setmtu(if_t ifp, int mtu)
4311 {
4312 ifp->if_mtu = mtu;
4313 return (0);
4314 }
4315
4316 void
if_notifymtu(if_t ifp)4317 if_notifymtu(if_t ifp)
4318 {
4319 #ifdef INET6
4320 nd6_setmtu(ifp);
4321 #endif
4322 rt_updatemtu(ifp);
4323 }
4324
4325 int
if_getmtu(const if_t ifp)4326 if_getmtu(const if_t ifp)
4327 {
4328 return (ifp->if_mtu);
4329 }
4330
4331 void
if_setppromisc(if_t ifp,bool ppromisc)4332 if_setppromisc(if_t ifp, bool ppromisc)
4333 {
4334 int new_flags;
4335
4336 if (ppromisc)
4337 new_flags = ifp->if_flags | IFF_PPROMISC;
4338 else
4339 new_flags = ifp->if_flags & ~IFF_PPROMISC;
4340 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) {
4341 if (new_flags & IFF_PPROMISC)
4342 new_flags |= IFF_PROMISC;
4343 /*
4344 * Only unset IFF_PROMISC if there are no more consumers of
4345 * promiscuity, i.e. the ifp->if_pcount refcount is 0.
4346 */
4347 else if (ifp->if_pcount == 0)
4348 new_flags &= ~IFF_PROMISC;
4349 if (log_promisc_mode_change)
4350 if_printf(ifp, "permanently promiscuous mode %s\n",
4351 ((new_flags & IFF_PPROMISC) ?
4352 "enabled" : "disabled"));
4353 }
4354 ifp->if_flags = new_flags;
4355 }
4356
4357 /*
4358 * Methods for drivers to access interface unicast and multicast
4359 * link level addresses. Driver shall not know 'struct ifaddr' neither
4360 * 'struct ifmultiaddr'.
4361 */
4362 u_int
if_lladdr_count(if_t ifp)4363 if_lladdr_count(if_t ifp)
4364 {
4365 struct epoch_tracker et;
4366 struct ifaddr *ifa;
4367 u_int count;
4368
4369 count = 0;
4370 NET_EPOCH_ENTER(et);
4371 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4372 if (ifa->ifa_addr->sa_family == AF_LINK)
4373 count++;
4374 NET_EPOCH_EXIT(et);
4375
4376 return (count);
4377 }
4378
4379 int
if_foreach(if_foreach_cb_t cb,void * cb_arg)4380 if_foreach(if_foreach_cb_t cb, void *cb_arg)
4381 {
4382 if_t ifp;
4383 int error;
4384
4385 NET_EPOCH_ASSERT();
4386 MPASS(cb);
4387
4388 error = 0;
4389 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4390 error = cb(ifp, cb_arg);
4391 if (error != 0)
4392 break;
4393 }
4394
4395 return (error);
4396 }
4397
4398 /*
4399 * Iterates over the list of interfaces, permitting callback function @cb to sleep.
4400 * Stops iteration if @cb returns non-zero error code.
4401 * Returns the last error code from @cb.
4402 * @match_cb: optional match callback limiting the iteration to only matched interfaces
4403 * @match_arg: argument to pass to @match_cb
4404 * @cb: iteration callback
4405 * @cb_arg: argument to pass to @cb
4406 */
4407 int
if_foreach_sleep(if_foreach_match_t match_cb,void * match_arg,if_foreach_cb_t cb,void * cb_arg)4408 if_foreach_sleep(if_foreach_match_t match_cb, void *match_arg, if_foreach_cb_t cb,
4409 void *cb_arg)
4410 {
4411 int match_count = 0, array_size = 16; /* 128 bytes for malloc */
4412 struct ifnet **match_array = NULL;
4413 int error = 0;
4414
4415 MPASS(cb);
4416
4417 while (true) {
4418 struct ifnet **new_array;
4419 int new_size = array_size;
4420 struct epoch_tracker et;
4421 struct ifnet *ifp;
4422
4423 while (new_size < match_count)
4424 new_size *= 2;
4425 new_array = malloc(new_size * sizeof(void *), M_TEMP, M_WAITOK);
4426 if (match_array != NULL)
4427 memcpy(new_array, match_array, array_size * sizeof(void *));
4428 free(match_array, M_TEMP);
4429 match_array = new_array;
4430 array_size = new_size;
4431
4432 match_count = 0;
4433 NET_EPOCH_ENTER(et);
4434 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
4435 if (match_cb != NULL && !match_cb(ifp, match_arg))
4436 continue;
4437 if (match_count < array_size) {
4438 if (if_try_ref(ifp))
4439 match_array[match_count++] = ifp;
4440 } else
4441 match_count++;
4442 }
4443 NET_EPOCH_EXIT(et);
4444
4445 if (match_count > array_size) {
4446 for (int i = 0; i < array_size; i++)
4447 if_rele(match_array[i]);
4448 continue;
4449 } else {
4450 for (int i = 0; i < match_count; i++) {
4451 if (error == 0)
4452 error = cb(match_array[i], cb_arg);
4453 if_rele(match_array[i]);
4454 }
4455 free(match_array, M_TEMP);
4456 break;
4457 }
4458 }
4459
4460 return (error);
4461 }
4462
4463
4464 /*
4465 * Uses just 1 pointer of the 4 available in the public struct.
4466 */
4467 if_t
if_iter_start(struct if_iter * iter)4468 if_iter_start(struct if_iter *iter)
4469 {
4470 if_t ifp;
4471
4472 NET_EPOCH_ASSERT();
4473
4474 bzero(iter, sizeof(*iter));
4475 ifp = CK_STAILQ_FIRST(&V_ifnet);
4476 if (ifp != NULL)
4477 iter->context[0] = CK_STAILQ_NEXT(ifp, if_link);
4478 else
4479 iter->context[0] = NULL;
4480 return (ifp);
4481 }
4482
4483 if_t
if_iter_next(struct if_iter * iter)4484 if_iter_next(struct if_iter *iter)
4485 {
4486 if_t cur_ifp = iter->context[0];
4487
4488 if (cur_ifp != NULL)
4489 iter->context[0] = CK_STAILQ_NEXT(cur_ifp, if_link);
4490 return (cur_ifp);
4491 }
4492
4493 void
if_iter_finish(struct if_iter * iter)4494 if_iter_finish(struct if_iter *iter)
4495 {
4496 /* Nothing to do here for now. */
4497 }
4498
4499 u_int
if_foreach_lladdr(if_t ifp,iflladdr_cb_t cb,void * cb_arg)4500 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4501 {
4502 struct epoch_tracker et;
4503 struct ifaddr *ifa;
4504 u_int count;
4505
4506 MPASS(cb);
4507
4508 count = 0;
4509 NET_EPOCH_ENTER(et);
4510 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4511 if (ifa->ifa_addr->sa_family != AF_LINK)
4512 continue;
4513 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr,
4514 count);
4515 }
4516 NET_EPOCH_EXIT(et);
4517
4518 return (count);
4519 }
4520
4521 u_int
if_llmaddr_count(if_t ifp)4522 if_llmaddr_count(if_t ifp)
4523 {
4524 struct epoch_tracker et;
4525 struct ifmultiaddr *ifma;
4526 int count;
4527
4528 count = 0;
4529 NET_EPOCH_ENTER(et);
4530 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
4531 if (ifma->ifma_addr->sa_family == AF_LINK)
4532 count++;
4533 NET_EPOCH_EXIT(et);
4534
4535 return (count);
4536 }
4537
4538 bool
if_maddr_empty(if_t ifp)4539 if_maddr_empty(if_t ifp)
4540 {
4541
4542 return (CK_STAILQ_EMPTY(&ifp->if_multiaddrs));
4543 }
4544
4545 u_int
if_foreach_llmaddr(if_t ifp,iflladdr_cb_t cb,void * cb_arg)4546 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg)
4547 {
4548 struct epoch_tracker et;
4549 struct ifmultiaddr *ifma;
4550 u_int count;
4551
4552 MPASS(cb);
4553
4554 count = 0;
4555 NET_EPOCH_ENTER(et);
4556 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
4557 if (ifma->ifma_addr->sa_family != AF_LINK)
4558 continue;
4559 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr,
4560 count);
4561 }
4562 NET_EPOCH_EXIT(et);
4563
4564 return (count);
4565 }
4566
4567 u_int
if_foreach_addr_type(if_t ifp,int type,if_addr_cb_t cb,void * cb_arg)4568 if_foreach_addr_type(if_t ifp, int type, if_addr_cb_t cb, void *cb_arg)
4569 {
4570 struct epoch_tracker et;
4571 struct ifaddr *ifa;
4572 u_int count;
4573
4574 MPASS(cb);
4575
4576 count = 0;
4577 NET_EPOCH_ENTER(et);
4578 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4579 if (ifa->ifa_addr->sa_family != type)
4580 continue;
4581 count += (*cb)(cb_arg, ifa, count);
4582 }
4583 NET_EPOCH_EXIT(et);
4584
4585 return (count);
4586 }
4587
4588 struct ifaddr *
ifa_iter_start(if_t ifp,struct ifa_iter * iter)4589 ifa_iter_start(if_t ifp, struct ifa_iter *iter)
4590 {
4591 struct ifaddr *ifa;
4592
4593 NET_EPOCH_ASSERT();
4594
4595 bzero(iter, sizeof(*iter));
4596 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
4597 if (ifa != NULL)
4598 iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4599 else
4600 iter->context[0] = NULL;
4601 return (ifa);
4602 }
4603
4604 struct ifaddr *
ifa_iter_next(struct ifa_iter * iter)4605 ifa_iter_next(struct ifa_iter *iter)
4606 {
4607 struct ifaddr *ifa = iter->context[0];
4608
4609 if (ifa != NULL)
4610 iter->context[0] = CK_STAILQ_NEXT(ifa, ifa_link);
4611 return (ifa);
4612 }
4613
4614 void
ifa_iter_finish(struct ifa_iter * iter)4615 ifa_iter_finish(struct ifa_iter *iter)
4616 {
4617 /* Nothing to do here for now. */
4618 }
4619
4620 int
if_setsoftc(if_t ifp,void * softc)4621 if_setsoftc(if_t ifp, void *softc)
4622 {
4623 ifp->if_softc = softc;
4624 return (0);
4625 }
4626
4627 void *
if_getsoftc(const if_t ifp)4628 if_getsoftc(const if_t ifp)
4629 {
4630 return (ifp->if_softc);
4631 }
4632
4633 void
if_setrcvif(struct mbuf * m,if_t ifp)4634 if_setrcvif(struct mbuf *m, if_t ifp)
4635 {
4636
4637 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
4638 m->m_pkthdr.rcvif = (struct ifnet *)ifp;
4639 }
4640
4641 void
if_setvtag(struct mbuf * m,uint16_t tag)4642 if_setvtag(struct mbuf *m, uint16_t tag)
4643 {
4644 m->m_pkthdr.ether_vtag = tag;
4645 }
4646
4647 uint16_t
if_getvtag(struct mbuf * m)4648 if_getvtag(struct mbuf *m)
4649 {
4650 return (m->m_pkthdr.ether_vtag);
4651 }
4652
4653 int
if_sendq_empty(if_t ifp)4654 if_sendq_empty(if_t ifp)
4655 {
4656 return (IFQ_DRV_IS_EMPTY(&ifp->if_snd));
4657 }
4658
4659 struct ifaddr *
if_getifaddr(const if_t ifp)4660 if_getifaddr(const if_t ifp)
4661 {
4662 return (ifp->if_addr);
4663 }
4664
4665 int
if_setsendqready(if_t ifp)4666 if_setsendqready(if_t ifp)
4667 {
4668 IFQ_SET_READY(&ifp->if_snd);
4669 return (0);
4670 }
4671
4672 int
if_setsendqlen(if_t ifp,int tx_desc_count)4673 if_setsendqlen(if_t ifp, int tx_desc_count)
4674 {
4675 IFQ_SET_MAXLEN(&ifp->if_snd, tx_desc_count);
4676 ifp->if_snd.ifq_drv_maxlen = tx_desc_count;
4677 return (0);
4678 }
4679
4680 void
if_setnetmapadapter(if_t ifp,struct netmap_adapter * na)4681 if_setnetmapadapter(if_t ifp, struct netmap_adapter *na)
4682 {
4683 ifp->if_netmap = na;
4684 }
4685
4686 struct netmap_adapter *
if_getnetmapadapter(if_t ifp)4687 if_getnetmapadapter(if_t ifp)
4688 {
4689 return (ifp->if_netmap);
4690 }
4691
4692 int
if_vlantrunkinuse(if_t ifp)4693 if_vlantrunkinuse(if_t ifp)
4694 {
4695 return (ifp->if_vlantrunk != NULL);
4696 }
4697
4698 void
if_init(if_t ifp,void * ctx)4699 if_init(if_t ifp, void *ctx)
4700 {
4701 (*ifp->if_init)(ctx);
4702 }
4703
4704 void
if_input(if_t ifp,struct mbuf * sendmp)4705 if_input(if_t ifp, struct mbuf* sendmp)
4706 {
4707 (*ifp->if_input)(ifp, sendmp);
4708 }
4709
4710 int
if_transmit(if_t ifp,struct mbuf * m)4711 if_transmit(if_t ifp, struct mbuf *m)
4712 {
4713 return ((*ifp->if_transmit)(ifp, m));
4714 }
4715
4716 int
if_resolvemulti(if_t ifp,struct sockaddr ** srcs,struct sockaddr * dst)4717 if_resolvemulti(if_t ifp, struct sockaddr **srcs, struct sockaddr *dst)
4718 {
4719 if (ifp->if_resolvemulti == NULL)
4720 return (EOPNOTSUPP);
4721
4722 return (ifp->if_resolvemulti(ifp, srcs, dst));
4723 }
4724
4725 int
if_ioctl(if_t ifp,u_long cmd,void * data)4726 if_ioctl(if_t ifp, u_long cmd, void *data)
4727 {
4728 if (ifp->if_ioctl == NULL)
4729 return (EOPNOTSUPP);
4730
4731 return (ifp->if_ioctl(ifp, cmd, data));
4732 }
4733
4734 struct mbuf *
if_dequeue(if_t ifp)4735 if_dequeue(if_t ifp)
4736 {
4737 struct mbuf *m;
4738
4739 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
4740 return (m);
4741 }
4742
4743 int
if_sendq_prepend(if_t ifp,struct mbuf * m)4744 if_sendq_prepend(if_t ifp, struct mbuf *m)
4745 {
4746 IFQ_DRV_PREPEND(&ifp->if_snd, m);
4747 return (0);
4748 }
4749
4750 int
if_setifheaderlen(if_t ifp,int len)4751 if_setifheaderlen(if_t ifp, int len)
4752 {
4753 ifp->if_hdrlen = len;
4754 return (0);
4755 }
4756
4757 char *
if_getlladdr(const if_t ifp)4758 if_getlladdr(const if_t ifp)
4759 {
4760 return (IF_LLADDR(ifp));
4761 }
4762
4763 void *
if_gethandle(u_char type)4764 if_gethandle(u_char type)
4765 {
4766 return (if_alloc(type));
4767 }
4768
4769 void
if_vlancap(if_t ifp)4770 if_vlancap(if_t ifp)
4771 {
4772 VLAN_CAPABILITIES(ifp);
4773 }
4774
4775 int
if_sethwtsomax(if_t ifp,u_int if_hw_tsomax)4776 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax)
4777 {
4778 ifp->if_hw_tsomax = if_hw_tsomax;
4779 return (0);
4780 }
4781
4782 int
if_sethwtsomaxsegcount(if_t ifp,u_int if_hw_tsomaxsegcount)4783 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount)
4784 {
4785 ifp->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount;
4786 return (0);
4787 }
4788
4789 int
if_sethwtsomaxsegsize(if_t ifp,u_int if_hw_tsomaxsegsize)4790 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize)
4791 {
4792 ifp->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize;
4793 return (0);
4794 }
4795
4796 u_int
if_gethwtsomax(const if_t ifp)4797 if_gethwtsomax(const if_t ifp)
4798 {
4799 return (ifp->if_hw_tsomax);
4800 }
4801
4802 u_int
if_gethwtsomaxsegcount(const if_t ifp)4803 if_gethwtsomaxsegcount(const if_t ifp)
4804 {
4805 return (ifp->if_hw_tsomaxsegcount);
4806 }
4807
4808 u_int
if_gethwtsomaxsegsize(const if_t ifp)4809 if_gethwtsomaxsegsize(const if_t ifp)
4810 {
4811 return (ifp->if_hw_tsomaxsegsize);
4812 }
4813
4814 void
if_setinitfn(if_t ifp,if_init_fn_t init_fn)4815 if_setinitfn(if_t ifp, if_init_fn_t init_fn)
4816 {
4817 ifp->if_init = init_fn;
4818 }
4819
4820 void
if_setinputfn(if_t ifp,if_input_fn_t input_fn)4821 if_setinputfn(if_t ifp, if_input_fn_t input_fn)
4822 {
4823 ifp->if_input = input_fn;
4824 }
4825
4826 if_input_fn_t
if_getinputfn(if_t ifp)4827 if_getinputfn(if_t ifp)
4828 {
4829 return (ifp->if_input);
4830 }
4831
4832 void
if_setioctlfn(if_t ifp,if_ioctl_fn_t ioctl_fn)4833 if_setioctlfn(if_t ifp, if_ioctl_fn_t ioctl_fn)
4834 {
4835 ifp->if_ioctl = ioctl_fn;
4836 }
4837
4838 void
if_setoutputfn(if_t ifp,if_output_fn_t output_fn)4839 if_setoutputfn(if_t ifp, if_output_fn_t output_fn)
4840 {
4841 ifp->if_output = output_fn;
4842 }
4843
4844 void
if_setstartfn(if_t ifp,if_start_fn_t start_fn)4845 if_setstartfn(if_t ifp, if_start_fn_t start_fn)
4846 {
4847 ifp->if_start = start_fn;
4848 }
4849
4850 if_start_fn_t
if_getstartfn(if_t ifp)4851 if_getstartfn(if_t ifp)
4852 {
4853 return (ifp->if_start);
4854 }
4855
4856 void
if_settransmitfn(if_t ifp,if_transmit_fn_t start_fn)4857 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn)
4858 {
4859 ifp->if_transmit = start_fn;
4860 }
4861
4862 if_transmit_fn_t
if_gettransmitfn(if_t ifp)4863 if_gettransmitfn(if_t ifp)
4864 {
4865 return (ifp->if_transmit);
4866 }
4867
4868 void
if_setqflushfn(if_t ifp,if_qflush_fn_t flush_fn)4869 if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn)
4870 {
4871 ifp->if_qflush = flush_fn;
4872 }
4873
4874 void
if_setsndtagallocfn(if_t ifp,if_snd_tag_alloc_t alloc_fn)4875 if_setsndtagallocfn(if_t ifp, if_snd_tag_alloc_t alloc_fn)
4876 {
4877 ifp->if_snd_tag_alloc = alloc_fn;
4878 }
4879
4880 int
if_snd_tag_alloc(if_t ifp,union if_snd_tag_alloc_params * params,struct m_snd_tag ** mstp)4881 if_snd_tag_alloc(if_t ifp, union if_snd_tag_alloc_params *params,
4882 struct m_snd_tag **mstp)
4883 {
4884 if (ifp->if_snd_tag_alloc == NULL)
4885 return (EOPNOTSUPP);
4886 return (ifp->if_snd_tag_alloc(ifp, params, mstp));
4887 }
4888
4889 void
if_setgetcounterfn(if_t ifp,if_get_counter_t fn)4890 if_setgetcounterfn(if_t ifp, if_get_counter_t fn)
4891 {
4892 ifp->if_get_counter = fn;
4893 }
4894
4895 void
if_setreassignfn(if_t ifp,if_reassign_fn_t fn)4896 if_setreassignfn(if_t ifp, if_reassign_fn_t fn)
4897 {
4898 ifp->if_reassign = fn;
4899 }
4900
4901 void
if_setratelimitqueryfn(if_t ifp,if_ratelimit_query_t fn)4902 if_setratelimitqueryfn(if_t ifp, if_ratelimit_query_t fn)
4903 {
4904 ifp->if_ratelimit_query = fn;
4905 }
4906
4907 void
if_setdebugnet_methods(if_t ifp,struct debugnet_methods * m)4908 if_setdebugnet_methods(if_t ifp, struct debugnet_methods *m)
4909 {
4910 ifp->if_debugnet_methods = m;
4911 }
4912
4913 struct label *
if_getmaclabel(if_t ifp)4914 if_getmaclabel(if_t ifp)
4915 {
4916 return (ifp->if_label);
4917 }
4918
4919 void
if_setmaclabel(if_t ifp,struct label * label)4920 if_setmaclabel(if_t ifp, struct label *label)
4921 {
4922 ifp->if_label = label;
4923 }
4924
4925 int
if_gettype(if_t ifp)4926 if_gettype(if_t ifp)
4927 {
4928 return (ifp->if_type);
4929 }
4930
4931 void *
if_getllsoftc(if_t ifp)4932 if_getllsoftc(if_t ifp)
4933 {
4934 return (ifp->if_llsoftc);
4935 }
4936
4937 void
if_setllsoftc(if_t ifp,void * llsoftc)4938 if_setllsoftc(if_t ifp, void *llsoftc)
4939 {
4940 ifp->if_llsoftc = llsoftc;
4941 };
4942
4943 int
if_getlinkstate(if_t ifp)4944 if_getlinkstate(if_t ifp)
4945 {
4946 return (ifp->if_link_state);
4947 }
4948
4949 const uint8_t *
if_getbroadcastaddr(if_t ifp)4950 if_getbroadcastaddr(if_t ifp)
4951 {
4952 return (ifp->if_broadcastaddr);
4953 }
4954
4955 void
if_setbroadcastaddr(if_t ifp,const uint8_t * addr)4956 if_setbroadcastaddr(if_t ifp, const uint8_t *addr)
4957 {
4958 ifp->if_broadcastaddr = addr;
4959 }
4960
4961 int
if_getnumadomain(if_t ifp)4962 if_getnumadomain(if_t ifp)
4963 {
4964 return (ifp->if_numa_domain);
4965 }
4966
4967 uint64_t
if_getcounter(if_t ifp,ift_counter counter)4968 if_getcounter(if_t ifp, ift_counter counter)
4969 {
4970 return (ifp->if_get_counter(ifp, counter));
4971 }
4972
4973 bool
if_altq_is_enabled(if_t ifp)4974 if_altq_is_enabled(if_t ifp)
4975 {
4976 return (ALTQ_IS_ENABLED(&ifp->if_snd));
4977 }
4978
4979 struct vnet *
if_getvnet(if_t ifp)4980 if_getvnet(if_t ifp)
4981 {
4982 return (ifp->if_vnet);
4983 }
4984
4985 struct in_ifinfo *
if_getinet(if_t ifp)4986 if_getinet(if_t ifp)
4987 {
4988 return (ifp->if_inet);
4989 }
4990
4991 struct in6_ifextra *
if_getinet6(if_t ifp)4992 if_getinet6(if_t ifp)
4993 {
4994 return (ifp->if_inet6);
4995 }
4996
4997 u_int
if_getfib(if_t ifp)4998 if_getfib(if_t ifp)
4999 {
5000 return (ifp->if_fib);
5001 }
5002
5003 uint8_t
if_getaddrlen(if_t ifp)5004 if_getaddrlen(if_t ifp)
5005 {
5006 return (ifp->if_addrlen);
5007 }
5008
5009 struct bpf_if *
if_getbpf(if_t ifp)5010 if_getbpf(if_t ifp)
5011 {
5012 return (ifp->if_bpf);
5013 }
5014
5015 struct ifvlantrunk *
if_getvlantrunk(if_t ifp)5016 if_getvlantrunk(if_t ifp)
5017 {
5018 return (ifp->if_vlantrunk);
5019 }
5020
5021 uint8_t
if_getpcp(if_t ifp)5022 if_getpcp(if_t ifp)
5023 {
5024 return (ifp->if_pcp);
5025 }
5026
5027 void *
if_getl2com(if_t ifp)5028 if_getl2com(if_t ifp)
5029 {
5030 return (ifp->if_l2com);
5031 }
5032
5033 void
if_setipsec_accel_methods(if_t ifp,const struct if_ipsec_accel_methods * m)5034 if_setipsec_accel_methods(if_t ifp, const struct if_ipsec_accel_methods *m)
5035 {
5036 ifp->if_ipsec_accel_m = m;
5037 }
5038
5039 #ifdef DDB
5040 static void
if_show_ifnet(struct ifnet * ifp)5041 if_show_ifnet(struct ifnet *ifp)
5042 {
5043 if (ifp == NULL)
5044 return;
5045 db_printf("%s:\n", ifp->if_xname);
5046 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e);
5047 IF_DB_PRINTF("%s", if_dname);
5048 IF_DB_PRINTF("%d", if_dunit);
5049 IF_DB_PRINTF("%s", if_description);
5050 IF_DB_PRINTF("%u", if_index);
5051 IF_DB_PRINTF("%d", if_idxgen);
5052 IF_DB_PRINTF("%u", if_refcount);
5053 IF_DB_PRINTF("%p", if_softc);
5054 IF_DB_PRINTF("%p", if_l2com);
5055 IF_DB_PRINTF("%p", if_llsoftc);
5056 IF_DB_PRINTF("%d", if_amcount);
5057 IF_DB_PRINTF("%p", if_addr);
5058 IF_DB_PRINTF("%p", if_broadcastaddr);
5059 IF_DB_PRINTF("%u", if_fib);
5060 IF_DB_PRINTF("%p", if_vnet);
5061 IF_DB_PRINTF("%p", if_home_vnet);
5062 IF_DB_PRINTF("%p", if_vlantrunk);
5063 IF_DB_PRINTF("%p", if_bpf);
5064 IF_DB_PRINTF("%u", if_pcount);
5065 IF_DB_PRINTF("%p", if_bridge);
5066 IF_DB_PRINTF("%p", if_lagg);
5067 IF_DB_PRINTF("%p", if_pf_kif);
5068 IF_DB_PRINTF("%p", if_carp);
5069 IF_DB_PRINTF("%p", if_label);
5070 IF_DB_PRINTF("%p", if_netmap);
5071 IF_DB_PRINTF("0x%08x", if_flags);
5072 IF_DB_PRINTF("0x%08x", if_drv_flags);
5073 IF_DB_PRINTF("0x%08x", if_capabilities);
5074 IF_DB_PRINTF("0x%08x", if_capenable);
5075 IF_DB_PRINTF("%p", if_snd.ifq_head);
5076 IF_DB_PRINTF("%p", if_snd.ifq_tail);
5077 IF_DB_PRINTF("%d", if_snd.ifq_len);
5078 IF_DB_PRINTF("%d", if_snd.ifq_maxlen);
5079 IF_DB_PRINTF("%p", if_snd.ifq_drv_head);
5080 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail);
5081 IF_DB_PRINTF("%d", if_snd.ifq_drv_len);
5082 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen);
5083 IF_DB_PRINTF("%d", if_snd.altq_type);
5084 IF_DB_PRINTF("%x", if_snd.altq_flags);
5085 #undef IF_DB_PRINTF
5086 }
5087
DB_SHOW_COMMAND(ifnet,db_show_ifnet)5088 DB_SHOW_COMMAND(ifnet, db_show_ifnet)
5089 {
5090 if (!have_addr) {
5091 db_printf("usage: show ifnet <struct ifnet *>\n");
5092 return;
5093 }
5094
5095 if_show_ifnet((struct ifnet *)addr);
5096 }
5097
DB_SHOW_ALL_COMMAND(ifnets,db_show_all_ifnets)5098 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets)
5099 {
5100 struct ifnet *ifp;
5101 u_short idx;
5102
5103 for (idx = 1; idx <= if_index; idx++) {
5104 ifp = ifindex_table[idx].ife_ifnet;
5105 if (ifp == NULL)
5106 continue;
5107 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp);
5108 if (db_pager_quit)
5109 break;
5110 }
5111 }
5112 #endif /* DDB */
5113