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