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