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