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