1 /* $NetBSD: if.h,v 1.289 2020/10/15 10:20:44 roy Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by William Studenmund and Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1989, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)if.h 8.3 (Berkeley) 2/9/95 61 */ 62 63 #ifndef _NET_IF_H_ 64 #define _NET_IF_H_ 65 66 #if !defined(_KERNEL) && !defined(_STANDALONE) 67 #include <stdbool.h> 68 #endif 69 70 #include <sys/featuretest.h> 71 72 /* 73 * Length of interface external name, including terminating '\0'. 74 * Note: this is the same size as a generic device's external name. 75 */ 76 #define IF_NAMESIZE 16 77 78 /* 79 * Length of interface description, including terminating '\0'. 80 */ 81 #define IFDESCRSIZE 64 82 83 #if defined(_NETBSD_SOURCE) 84 85 #include <sys/socket.h> 86 #include <sys/queue.h> 87 #include <sys/mutex.h> 88 89 #include <net/dlt.h> 90 #include <net/pfil.h> 91 #ifdef _KERNEL 92 #include <net/pktqueue.h> 93 #include <sys/pslist.h> 94 #include <sys/pserialize.h> 95 #include <sys/psref.h> 96 #include <sys/module_hook.h> 97 #endif 98 99 /* 100 * Always include ALTQ glue here -- we use the ALTQ interface queue 101 * structure even when ALTQ is not configured into the kernel so that 102 * the size of struct ifnet does not changed based on the option. The 103 * ALTQ queue structure is API-compatible with the legacy ifqueue. 104 */ 105 #include <altq/if_altq.h> 106 107 /* 108 * Structures defining a network interface, providing a packet 109 * transport mechanism (ala level 0 of the PUP protocols). 110 * 111 * Each interface accepts output datagrams of a specified maximum 112 * length, and provides higher level routines with input datagrams 113 * received from its medium. 114 * 115 * Output occurs when the routine if_output is called, with four parameters: 116 * (*ifp->if_output)(ifp, m, dst, rt) 117 * Here m is the mbuf chain to be sent and dst is the destination address. 118 * The output routine encapsulates the supplied datagram if necessary, 119 * and then transmits it on its medium. 120 * 121 * On input, each interface unwraps the data received by it, and either 122 * places it on the input queue of a internetwork datagram routine 123 * and posts the associated software interrupt, or passes the datagram to a raw 124 * packet input routine. 125 * 126 * Routines exist for locating interfaces by their addresses 127 * or for locating a interface on a certain network, as well as more general 128 * routing and gateway routines maintaining information used to locate 129 * interfaces. These routines live in the files if.c and route.c 130 */ 131 #include <sys/time.h> 132 133 #if defined(_KERNEL_OPT) 134 #include "opt_compat_netbsd.h" 135 #include "opt_gateway.h" 136 #endif 137 138 struct mbuf; 139 struct proc; 140 struct rtentry; 141 struct socket; 142 struct ether_header; 143 struct ifaddr; 144 struct ifnet; 145 struct rt_addrinfo; 146 147 #define IFNAMSIZ IF_NAMESIZE 148 149 /* 150 * Structure describing a `cloning' interface. 151 */ 152 struct if_clone { 153 LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */ 154 const char *ifc_name; /* name of device, e.g. `gif' */ 155 size_t ifc_namelen; /* length of name */ 156 157 int (*ifc_create)(struct if_clone *, int); 158 int (*ifc_destroy)(struct ifnet *); 159 }; 160 161 #define IF_CLONE_INITIALIZER(name, create, destroy) \ 162 { { NULL, NULL }, name, sizeof(name) - 1, create, destroy } 163 164 /* 165 * Structure used to query names of interface cloners. 166 */ 167 struct if_clonereq { 168 int ifcr_total; /* total cloners (out) */ 169 int ifcr_count; /* room for this many in user buffer */ 170 char *ifcr_buffer; /* buffer for cloner names */ 171 }; 172 173 /* 174 * Structure defining statistics and other data kept regarding a network 175 * interface. 176 * 177 * Only used for exporting data from the interface. 178 */ 179 struct if_data { 180 /* generic interface information */ 181 u_char ifi_type; /* ethernet, tokenring, etc. */ 182 u_char ifi_addrlen; /* media address length */ 183 u_char ifi_hdrlen; /* media header length */ 184 int ifi_link_state; /* current link state */ 185 uint64_t ifi_mtu; /* maximum transmission unit */ 186 uint64_t ifi_metric; /* routing metric (external only) */ 187 uint64_t ifi_baudrate; /* linespeed */ 188 /* volatile statistics */ 189 uint64_t ifi_ipackets; /* packets received on interface */ 190 uint64_t ifi_ierrors; /* input errors on interface */ 191 uint64_t ifi_opackets; /* packets sent on interface */ 192 uint64_t ifi_oerrors; /* output errors on interface */ 193 uint64_t ifi_collisions; /* collisions on csma interfaces */ 194 uint64_t ifi_ibytes; /* total number of octets received */ 195 uint64_t ifi_obytes; /* total number of octets sent */ 196 uint64_t ifi_imcasts; /* packets received via multicast */ 197 uint64_t ifi_omcasts; /* packets sent via multicast */ 198 uint64_t ifi_iqdrops; /* dropped on input, this interface */ 199 uint64_t ifi_noproto; /* destined for unsupported protocol */ 200 struct timespec ifi_lastchange;/* last operational state change */ 201 }; 202 203 /* 204 * Values for if_link_state. 205 */ 206 #define LINK_STATE_UNKNOWN 0 /* link invalid/unknown */ 207 #define LINK_STATE_DOWN 1 /* link is down */ 208 #define LINK_STATE_UP 2 /* link is up */ 209 210 /* 211 * Status bit descriptions for the various interface types. 212 */ 213 struct if_status_description { 214 unsigned char ifs_type; 215 unsigned char ifs_state; 216 const char *ifs_string; 217 }; 218 219 #define LINK_STATE_DESC_MATCH(_ifs, _t, _s) \ 220 (((_ifs)->ifs_type == (_t) || (_ifs)->ifs_type == 0) && \ 221 (_ifs)->ifs_state == (_s)) 222 223 #define LINK_STATE_DESCRIPTIONS { \ 224 { IFT_ETHER, LINK_STATE_DOWN, "no carrier" }, \ 225 { IFT_IEEE80211, LINK_STATE_DOWN, "no network" }, \ 226 { IFT_PPP, LINK_STATE_DOWN, "no carrier" }, \ 227 { IFT_CARP, LINK_STATE_DOWN, "backup" }, \ 228 { IFT_CARP, LINK_STATE_UP, "master" }, \ 229 { 0, LINK_STATE_UP, "active" }, \ 230 { 0, LINK_STATE_UNKNOWN, "unknown" }, \ 231 { 0, LINK_STATE_DOWN, "down" }, \ 232 { 0, 0, NULL } \ 233 } 234 235 /* 236 * Structure defining a queue for a network interface. 237 */ 238 struct ifqueue { 239 struct mbuf *ifq_head; 240 struct mbuf *ifq_tail; 241 int ifq_len; 242 int ifq_maxlen; 243 int ifq_drops; 244 kmutex_t *ifq_lock; 245 }; 246 247 #ifdef _KERNEL 248 #include <sys/percpu.h> 249 #include <sys/callout.h> 250 #include <sys/rwlock.h> 251 #include <sys/workqueue.h> 252 253 #endif /* _KERNEL */ 254 255 /* 256 * Structure defining a queue for a network interface. 257 * 258 * (Would like to call this struct ``if'', but C isn't PL/1.) 259 */ 260 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */ 261 262 struct bridge_softc; 263 struct bridge_iflist; 264 struct callout; 265 struct krwlock; 266 struct if_percpuq; 267 struct if_deferred_start; 268 struct in6_multi; 269 270 typedef unsigned short if_index_t; 271 272 /* 273 * Interface. Field markings and the corresponding locks: 274 * 275 * i: IFNET_LOCK (a.k.a., if_ioctl_lock) 276 * q: ifq_lock (struct ifaltq) 277 * a: if_afdata_lock 278 * 6: in6_multilock (global lock) 279 * :: unlocked, stable 280 * ?: unknown, maybe unsafe 281 * 282 * Lock order: IFNET_LOCK => in6_multilock => if_afdata_lock => ifq_lock 283 * Note that currently if_afdata_lock and ifq_lock aren't held 284 * at the same time, but define the order anyway. 285 * 286 * Lock order of IFNET_LOCK with other locks: 287 * softnet_lock => solock => IFNET_LOCK => ND6_LOCK, in_multilock 288 */ 289 typedef struct ifnet { 290 void *if_softc; /* :: lower-level data for this if */ 291 /* DEPRECATED. Keep it to avoid breaking kvm(3) users */ 292 TAILQ_ENTRY(ifnet) 293 if_list; /* i: all struct ifnets are chained */ 294 TAILQ_HEAD(, ifaddr) 295 if_addrlist; /* i: linked list of addresses per if */ 296 char if_xname[IFNAMSIZ]; 297 /* :: external name (name + unit) */ 298 int if_pcount; /* i: number of promiscuous listeners */ 299 struct bpf_if *if_bpf; /* :: packet filter structure */ 300 if_index_t if_index; /* :: numeric abbreviation for this if */ 301 short if_timer; /* ?: time 'til if_slowtimo called */ 302 unsigned short if_flags; /* i: up/down, broadcast, etc. */ 303 short if_extflags; /* :: if_output MP-safe, etc. */ 304 u_char if_type; /* :: ethernet, tokenring, etc. */ 305 u_char if_addrlen; /* :: media address length */ 306 u_char if_hdrlen; /* :: media header length */ 307 /* XXX audit :? fields here. */ 308 int if_link_state; /* :? current link state */ 309 uint64_t if_mtu; /* :? maximum transmission unit */ 310 uint64_t if_metric; /* :? routing metric (external only) */ 311 uint64_t if_baudrate; /* :? linespeed */ 312 struct timespec if_lastchange; /* :? last operational state change */ 313 #ifdef _KERNEL 314 percpu_t *if_stats; /* :: statistics */ 315 #else 316 void *if_stats; /* opaque to user-space */ 317 #endif /* _KERNEL */ 318 /* 319 * Procedure handles. If you add more of these, don't forget the 320 * corresponding NULL stub in if.c. 321 */ 322 int (*if_output) /* :: output routine (enqueue) */ 323 (struct ifnet *, struct mbuf *, const struct sockaddr *, 324 const struct rtentry *); 325 void (*_if_input) /* :: input routine (from h/w driver) */ 326 (struct ifnet *, struct mbuf *); 327 void (*if_start) /* :: initiate output routine */ 328 (struct ifnet *); 329 int (*if_transmit) /* :: output routine, must be MP-safe */ 330 (struct ifnet *, struct mbuf *); 331 int (*if_ioctl) /* :: ioctl routine */ 332 (struct ifnet *, u_long, void *); 333 int (*if_init) /* :: init routine */ 334 (struct ifnet *); 335 void (*if_stop) /* :: stop routine */ 336 (struct ifnet *, int); 337 void (*if_slowtimo) /* :: timer routine */ 338 (struct ifnet *); 339 #define if_watchdog if_slowtimo 340 void (*if_drain) /* :: routine to release resources */ 341 (struct ifnet *); 342 struct ifaltq if_snd; /* q: output queue (includes altq) */ 343 struct ifaddr *if_dl; /* i: identity of this interface. */ 344 const struct sockaddr_dl 345 *if_sadl; /* i: pointer to sockaddr_dl of if_dl */ 346 /* 347 * May be NULL. If not NULL, it is the address assigned 348 * to the interface by the manufacturer, so it very likely 349 * to be unique. It MUST NOT be deleted. It is highly 350 * suitable for deriving the EUI64 for the interface. 351 */ 352 struct ifaddr *if_hwdl; /* i: h/w identity */ 353 const uint8_t *if_broadcastaddr; 354 /* :: linklevel broadcast bytestring */ 355 struct bridge_softc 356 *if_bridge; /* i: bridge glue */ 357 struct bridge_iflist 358 *if_bridgeif; /* i: shortcut to interface list entry */ 359 int if_dlt; /* :: data link type (<net/dlt.h>) */ 360 pfil_head_t * if_pfil; /* :: filtering point */ 361 uint64_t if_capabilities; 362 /* i: interface capabilities */ 363 uint64_t if_capenable; /* i: capabilities enabled */ 364 union { 365 void * carp_s; /* carp structure (used by !carp ifs) */ 366 struct ifnet *carp_d;/* ptr to carpdev (used by carp ifs) */ 367 } if_carp_ptr; /* ?: */ 368 #define if_carp if_carp_ptr.carp_s 369 #define if_carpdev if_carp_ptr.carp_d 370 /* 371 * These are pre-computed based on an interfaces enabled 372 * capabilities, for speed elsewhere. 373 */ 374 int if_csum_flags_tx; 375 /* i: M_CSUM_* flags for Tx */ 376 int if_csum_flags_rx; 377 /* i: M_CSUM_* flags for Rx */ 378 379 void *if_afdata[AF_MAX]; 380 /* a: */ 381 struct mowner *if_mowner; /* ?: who owns mbufs for this interface */ 382 383 void *if_agrprivate; /* ?: used only when #if NAGR > 0 */ 384 void *if_npf_private;/* ?: associated NPF context */ 385 386 /* 387 * pf specific data, used only when #if NPF > 0. 388 */ 389 void *if_pf_kif; /* ?: pf interface abstraction */ 390 void *if_pf_groups; /* ?: pf interface groups */ 391 /* 392 * During an ifnet's lifetime, it has only one if_index, but 393 * and if_index is not sufficient to identify an ifnet 394 * because during the lifetime of the system, many ifnets may occupy a 395 * given if_index. Let us tell different ifnets at the same 396 * if_index apart by their if_index_gen, a unique number that each ifnet 397 * is assigned when it if_attach()s. Now, the kernel can use the 398 * pair (if_index, if_index_gen) as a weak reference to an ifnet. 399 */ 400 uint64_t if_index_gen; /* :: generation number for the ifnet 401 * at if_index: if two ifnets' index 402 * and generation number are both the 403 * same, they are the same ifnet. 404 */ 405 struct sysctllog 406 *if_sysctl_log; /* :: */ 407 int (*if_initaddr) /* :: */ 408 (struct ifnet *, struct ifaddr *, bool); 409 int (*if_setflags) /* :: */ 410 (struct ifnet *, const u_short); 411 kmutex_t *if_ioctl_lock; /* :: */ 412 char *if_description; /* i: interface description */ 413 #ifdef _KERNEL /* XXX kvm(3) */ 414 struct callout *if_slowtimo_ch;/* :: */ 415 struct krwlock *if_afdata_lock;/* :: */ 416 struct if_percpuq 417 *if_percpuq; /* :: we should remove it in the future */ 418 struct work if_link_work; /* q: linkage on link state work queue */ 419 uint16_t if_link_queue; /* q: masked link state change queue */ 420 /* q: is link state work scheduled? */ 421 bool if_link_scheduled; 422 void (*if_link_state_changed)(struct ifnet *, int); 423 struct pslist_entry 424 if_pslist_entry;/* i: */ 425 struct psref_target 426 if_psref; /* :: */ 427 struct pslist_head 428 if_addr_pslist; /* i: */ 429 struct if_deferred_start 430 *if_deferred_start; 431 /* :: */ 432 /* XXX should be protocol independent */ 433 LIST_HEAD(, in6_multi) 434 if_multiaddrs; /* 6: */ 435 #endif 436 } ifnet_t; 437 438 #include <net/if_stats.h> 439 440 #define if_name(ifp) ((ifp)->if_xname) 441 442 #define IFF_UP 0x0001 /* interface is up */ 443 #define IFF_BROADCAST 0x0002 /* broadcast address valid */ 444 #define IFF_DEBUG 0x0004 /* turn on debugging */ 445 #define IFF_LOOPBACK 0x0008 /* is a loopback net */ 446 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */ 447 /* 0x0020 was IFF_NOTRAILERS */ 448 #define IFF_RUNNING 0x0040 /* resources allocated */ 449 #define IFF_NOARP 0x0080 /* no address resolution protocol */ 450 #define IFF_PROMISC 0x0100 /* receive all packets */ 451 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */ 452 #define IFF_OACTIVE 0x0400 /* transmission in progress */ 453 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */ 454 #define IFF_LINK0 0x1000 /* per link layer defined bit */ 455 #define IFF_LINK1 0x2000 /* per link layer defined bit */ 456 #define IFF_LINK2 0x4000 /* per link layer defined bit */ 457 #define IFF_MULTICAST 0x8000 /* supports multicast */ 458 459 #define IFEF_MPSAFE __BIT(0) /* handlers can run in parallel (see below) */ 460 461 /* 462 * The guidelines for converting an interface to IFEF_MPSAFE are as follows 463 * 464 * Enabling IFEF_MPSAFE on an interface suppresses taking KERNEL_LOCK when 465 * calling the following handlers: 466 * - if_start 467 * - Note that if_transmit is always called without KERNEL_LOCK 468 * - if_output 469 * - if_ioctl 470 * - if_init 471 * - if_stop 472 * 473 * This means that an interface with IFEF_MPSAFE must make the above handlers 474 * MP-safe or take KERNEL_LOCK by itself inside handlers that aren't MP-safe 475 * yet. 476 * 477 * There are some additional restrictions to access member variables of struct 478 * ifnet: 479 * - if_flags 480 * - Must be updated with holding IFNET_LOCK 481 * - You cannot use the flag in Tx/Rx paths anymore because there is no 482 * synchronization on the flag except for IFNET_LOCK 483 * - Note that IFNET_LOCK can't be taken in softint because it's known 484 * that it causes a deadlock 485 * - Some synchronization mechanisms such as pserialize_perform are called 486 * with IFNET_LOCK and also require context switches on every CPUs 487 * that mean softints finish so trying to take IFNET_LOCK in softint 488 * might block on IFNET_LOCK and prevent such synchronization mechanisms 489 * from being completed 490 * - Currently the deadlock occurs only if NET_MPSAFE is enabled, however, 491 * we should deal with the restriction because NET_MPSAFE will be enabled 492 * by default in the future 493 * - if_watchdog and if_timer 494 * - The watchdog framework works only for non-IFEF_MPSAFE interfaces 495 * that rely on KERNEL_LOCK 496 * - Interfaces with IFEF_MPSAFE have to provide its own watchdog mechanism 497 * if needed 498 * - Keep if_watchdog NULL when calling if_attach 499 */ 500 501 #ifdef _KERNEL 502 static __inline bool 503 if_is_mpsafe(struct ifnet *ifp) 504 { 505 506 return ((ifp->if_extflags & IFEF_MPSAFE) != 0); 507 } 508 509 static __inline int 510 if_output_lock(struct ifnet *cifp, struct ifnet *ifp, struct mbuf *m, 511 const struct sockaddr *dst, const struct rtentry *rt) 512 { 513 514 if (if_is_mpsafe(cifp)) { 515 return (*cifp->if_output)(ifp, m, dst, rt); 516 } else { 517 int ret; 518 519 KERNEL_LOCK(1, NULL); 520 ret = (*cifp->if_output)(ifp, m, dst, rt); 521 KERNEL_UNLOCK_ONE(NULL); 522 return ret; 523 } 524 } 525 526 static __inline void 527 if_start_lock(struct ifnet *ifp) 528 { 529 530 if (if_is_mpsafe(ifp)) { 531 (*ifp->if_start)(ifp); 532 } else { 533 KERNEL_LOCK(1, NULL); 534 (*ifp->if_start)(ifp); 535 KERNEL_UNLOCK_ONE(NULL); 536 } 537 } 538 539 #define KERNEL_LOCK_IF_IFP_MPSAFE(ifp) \ 540 do { if (if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0) 541 #define KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp) \ 542 do { if (if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0) 543 544 #define KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp) \ 545 do { if (!if_is_mpsafe(ifp)) { KERNEL_LOCK(1, NULL); } } while (0) 546 #define KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp) \ 547 do { if (!if_is_mpsafe(ifp)) { KERNEL_UNLOCK_ONE(NULL); } } while (0) 548 549 #ifdef _KERNEL_OPT 550 #include "opt_net_mpsafe.h" 551 #endif 552 553 /* XXX explore a better place to define */ 554 #ifdef NET_MPSAFE 555 556 #define KERNEL_LOCK_UNLESS_NET_MPSAFE() do { } while (0) 557 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE() do { } while (0) 558 559 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE() do { } while (0) 560 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE() do { } while (0) 561 562 #define SOFTNET_LOCK_IF_NET_MPSAFE() \ 563 do { mutex_enter(softnet_lock); } while (0) 564 #define SOFTNET_UNLOCK_IF_NET_MPSAFE() \ 565 do { mutex_exit(softnet_lock); } while (0) 566 567 #else /* NET_MPSAFE */ 568 569 #define KERNEL_LOCK_UNLESS_NET_MPSAFE() \ 570 do { KERNEL_LOCK(1, NULL); } while (0) 571 #define KERNEL_UNLOCK_UNLESS_NET_MPSAFE() \ 572 do { KERNEL_UNLOCK_ONE(NULL); } while (0) 573 574 #define SOFTNET_LOCK_UNLESS_NET_MPSAFE() \ 575 do { mutex_enter(softnet_lock); } while (0) 576 #define SOFTNET_UNLOCK_UNLESS_NET_MPSAFE() \ 577 do { mutex_exit(softnet_lock); } while (0) 578 579 #define SOFTNET_LOCK_IF_NET_MPSAFE() do { } while (0) 580 #define SOFTNET_UNLOCK_IF_NET_MPSAFE() do { } while (0) 581 582 #endif /* NET_MPSAFE */ 583 584 #define SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE() \ 585 do { \ 586 SOFTNET_LOCK_UNLESS_NET_MPSAFE(); \ 587 KERNEL_LOCK_UNLESS_NET_MPSAFE(); \ 588 } while (0) 589 590 #define SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE() \ 591 do { \ 592 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); \ 593 SOFTNET_UNLOCK_UNLESS_NET_MPSAFE(); \ 594 } while (0) 595 596 #endif /* _KERNEL */ 597 598 #define IFFBITS \ 599 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT" \ 600 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \ 601 "\15LINK0\16LINK1\17LINK2\20MULTICAST" 602 603 /* flags set internally only: */ 604 #define IFF_CANTCHANGE \ 605 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\ 606 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC) 607 608 /* 609 * Some convenience macros used for setting ifi_baudrate. 610 */ 611 #define IF_Kbps(x) ((x) * 1000ULL) /* kilobits/sec. */ 612 #define IF_Mbps(x) (IF_Kbps((x) * 1000ULL)) /* megabits/sec. */ 613 #define IF_Gbps(x) (IF_Mbps((x) * 1000ULL)) /* gigabits/sec. */ 614 615 /* Capabilities that interfaces can advertise. */ 616 /* 0x01 .. 0x40 were previously used */ 617 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */ 618 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */ 619 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */ 620 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */ 621 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */ 622 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */ 623 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */ 624 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */ 625 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */ 626 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */ 627 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */ 628 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */ 629 #define IFCAP_LRO 0x80000 /* can do Large Receive Offload */ 630 #define IFCAP_MASK 0xfff80 /* currently valid capabilities */ 631 632 #define IFCAPBITS \ 633 "\020" \ 634 "\10TSO4" \ 635 "\11IP4CSUM_Rx" \ 636 "\12IP4CSUM_Tx" \ 637 "\13TCP4CSUM_Rx" \ 638 "\14TCP4CSUM_Tx" \ 639 "\15UDP4CSUM_Rx" \ 640 "\16UDP4CSUM_Tx" \ 641 "\17TCP6CSUM_Rx" \ 642 "\20TCP6CSUM_Tx" \ 643 "\21UDP6CSUM_Rx" \ 644 "\22UDP6CSUM_Tx" \ 645 "\23TSO6" \ 646 "\24LRO" \ 647 648 #define IF_AFDATA_LOCK_INIT(ifp) \ 649 do {(ifp)->if_afdata_lock = rw_obj_alloc();} while (0) 650 651 #define IF_AFDATA_LOCK_DESTROY(ifp) rw_obj_free((ifp)->if_afdata_lock) 652 653 #define IF_AFDATA_WLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_WRITER) 654 #define IF_AFDATA_RLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_READER) 655 #define IF_AFDATA_WUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock) 656 #define IF_AFDATA_RUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock) 657 #define IF_AFDATA_LOCK(ifp) IF_AFDATA_WLOCK(ifp) 658 #define IF_AFDATA_UNLOCK(ifp) IF_AFDATA_WUNLOCK(ifp) 659 #define IF_AFDATA_TRYLOCK(ifp) rw_tryenter((ifp)->if_afdata_lock, RW_WRITER) 660 661 #define IF_AFDATA_LOCK_ASSERT(ifp) \ 662 KASSERT(rw_lock_held((ifp)->if_afdata_lock)) 663 #define IF_AFDATA_RLOCK_ASSERT(ifp) \ 664 KASSERT(rw_read_held((ifp)->if_afdata_lock)) 665 #define IF_AFDATA_WLOCK_ASSERT(ifp) \ 666 KASSERT(rw_write_held((ifp)->if_afdata_lock)) 667 668 /* 669 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 670 * input routines have queues of messages stored on ifqueue structures 671 * (defined above). Entries are added to and deleted from these structures 672 * by these macros, which should be called with ipl raised to splnet(). 673 */ 674 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 675 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 676 #define IF_ENQUEUE(ifq, m) do { \ 677 (m)->m_nextpkt = 0; \ 678 if ((ifq)->ifq_tail == 0) \ 679 (ifq)->ifq_head = m; \ 680 else \ 681 (ifq)->ifq_tail->m_nextpkt = m; \ 682 (ifq)->ifq_tail = m; \ 683 (ifq)->ifq_len++; \ 684 } while (/*CONSTCOND*/0) 685 #define IF_PREPEND(ifq, m) do { \ 686 (m)->m_nextpkt = (ifq)->ifq_head; \ 687 if ((ifq)->ifq_tail == 0) \ 688 (ifq)->ifq_tail = (m); \ 689 (ifq)->ifq_head = (m); \ 690 (ifq)->ifq_len++; \ 691 } while (/*CONSTCOND*/0) 692 #define IF_DEQUEUE(ifq, m) do { \ 693 (m) = (ifq)->ifq_head; \ 694 if (m) { \ 695 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \ 696 (ifq)->ifq_tail = 0; \ 697 (m)->m_nextpkt = 0; \ 698 (ifq)->ifq_len--; \ 699 } \ 700 } while (/*CONSTCOND*/0) 701 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head) 702 #define IF_PURGE(ifq) \ 703 do { \ 704 struct mbuf *__m0; \ 705 \ 706 for (;;) { \ 707 IF_DEQUEUE((ifq), __m0); \ 708 if (__m0 == NULL) \ 709 break; \ 710 else \ 711 m_freem(__m0); \ 712 } \ 713 } while (/*CONSTCOND*/ 0) 714 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0) 715 716 #ifndef IFQ_MAXLEN 717 #define IFQ_MAXLEN 256 718 #endif 719 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 720 721 /* 722 * Structure defining statistics and other data kept regarding an address 723 * on a network interface. 724 */ 725 struct ifaddr_data { 726 int64_t ifad_inbytes; 727 int64_t ifad_outbytes; 728 }; 729 730 /* 731 * The ifaddr structure contains information about one address 732 * of an interface. They are maintained by the different address families, 733 * are allocated and attached when an address is set, and are linked 734 * together so all addresses for an interface can be located. 735 */ 736 struct ifaddr { 737 struct sockaddr *ifa_addr; /* address of interface */ 738 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 739 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 740 struct sockaddr *ifa_netmask; /* used to determine subnet */ 741 struct ifnet *ifa_ifp; /* back-pointer to interface */ 742 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */ 743 struct ifaddr_data ifa_data; /* statistics on the address */ 744 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */ 745 (int, struct rtentry *, const struct rt_addrinfo *); 746 u_int ifa_flags; /* mostly rt_flags for cloning */ 747 int ifa_refcnt; /* count of references */ 748 int ifa_metric; /* cost of going out this interface */ 749 struct ifaddr *(*ifa_getifa)(struct ifaddr *, 750 const struct sockaddr *); 751 uint32_t *ifa_seqno; 752 int16_t ifa_preference; /* preference level for this address */ 753 #ifdef _KERNEL 754 struct pslist_entry ifa_pslist_entry; 755 struct psref_target ifa_psref; 756 #endif 757 }; 758 #define IFA_ROUTE RTF_UP /* (0x01) route installed */ 759 #define IFA_DESTROYING 0x2 760 761 /* 762 * Message format for use in obtaining information about interfaces from 763 * sysctl and the routing socket. We need to force 64-bit alignment if we 764 * aren't using compatiblity definitons. 765 */ 766 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK) 767 #define __align64 __aligned(sizeof(uint64_t)) 768 #else 769 #define __align64 770 #endif 771 struct if_msghdr { 772 u_short ifm_msglen __align64; 773 /* to skip over non-understood messages */ 774 u_char ifm_version; /* future binary compatibility */ 775 u_char ifm_type; /* message type */ 776 int ifm_addrs; /* like rtm_addrs */ 777 int ifm_flags; /* value of if_flags */ 778 u_short ifm_index; /* index for associated ifp */ 779 struct if_data ifm_data __align64; 780 /* statistics and other data about if */ 781 }; 782 783 /* 784 * Message format for use in obtaining information about interface addresses 785 * from sysctl and the routing socket. 786 */ 787 struct ifa_msghdr { 788 u_short ifam_msglen __align64; 789 /* to skip over non-understood messages */ 790 u_char ifam_version; /* future binary compatibility */ 791 u_char ifam_type; /* message type */ 792 u_short ifam_index; /* index for associated ifp */ 793 int ifam_flags; /* value of ifa_flags */ 794 int ifam_addrs; /* like rtm_addrs */ 795 pid_t ifam_pid; /* identify sender */ 796 int ifam_addrflags; /* family specific address flags */ 797 int ifam_metric; /* value of ifa_metric */ 798 }; 799 800 /* 801 * Message format announcing the arrival or departure of a network interface. 802 */ 803 struct if_announcemsghdr { 804 u_short ifan_msglen __align64; 805 /* to skip over non-understood messages */ 806 u_char ifan_version; /* future binary compatibility */ 807 u_char ifan_type; /* message type */ 808 u_short ifan_index; /* index for associated ifp */ 809 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 810 u_short ifan_what; /* what type of announcement */ 811 }; 812 813 #define IFAN_ARRIVAL 0 /* interface arrival */ 814 #define IFAN_DEPARTURE 1 /* interface departure */ 815 816 #undef __align64 817 818 /* 819 * Interface request structure used for socket 820 * ioctl's. All interface ioctl's must have parameter 821 * definitions which begin with ifr_name. The 822 * remainder may be interface specific. 823 */ 824 struct ifreq { 825 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 826 union { 827 struct sockaddr ifru_addr; 828 struct sockaddr ifru_dstaddr; 829 struct sockaddr ifru_broadaddr; 830 struct sockaddr_storage ifru_space; 831 short ifru_flags; 832 int ifru_addrflags; 833 int ifru_metric; 834 int ifru_mtu; 835 int ifru_dlt; 836 u_int ifru_value; 837 void * ifru_data; 838 struct { 839 uint32_t b_buflen; 840 void *b_buf; 841 } ifru_b; 842 } ifr_ifru; 843 #define ifr_addr ifr_ifru.ifru_addr /* address */ 844 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 845 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 846 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */ 847 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 848 #define ifr_addrflags ifr_ifru.ifru_addrflags /* addr flags */ 849 #define ifr_metric ifr_ifru.ifru_metric /* metric */ 850 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */ 851 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */ 852 #define ifr_value ifr_ifru.ifru_value /* generic value */ 853 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */ 854 #define ifr_data ifr_ifru.ifru_data /* for use by interface 855 * XXX deprecated 856 */ 857 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */ 858 #define ifr_buflen ifr_ifru.ifru_b.b_buflen 859 #define ifr_index ifr_ifru.ifru_value /* interface index, BSD */ 860 #define ifr_ifindex ifr_index /* interface index, linux */ 861 }; 862 863 #ifdef _KERNEL 864 #define ifreq_setdstaddr ifreq_setaddr 865 #define ifreq_setbroadaddr ifreq_setaddr 866 #define ifreq_getdstaddr ifreq_getaddr 867 #define ifreq_getbroadaddr ifreq_getaddr 868 869 static __inline const struct sockaddr * 870 /*ARGSUSED*/ 871 ifreq_getaddr(u_long cmd, const struct ifreq *ifr) 872 { 873 return &ifr->ifr_addr; 874 } 875 #endif /* _KERNEL */ 876 877 struct ifcapreq { 878 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 879 uint64_t ifcr_capabilities; /* supported capabiliites */ 880 uint64_t ifcr_capenable; /* capabilities enabled */ 881 }; 882 883 struct ifaliasreq { 884 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 885 struct sockaddr ifra_addr; 886 struct sockaddr ifra_dstaddr; 887 #define ifra_broadaddr ifra_dstaddr 888 struct sockaddr ifra_mask; 889 }; 890 891 struct ifdatareq { 892 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 893 struct if_data ifdr_data; 894 }; 895 896 struct ifmediareq { 897 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 898 int ifm_current; /* IFMWD: current media options */ 899 int ifm_mask; /* IFMWD: don't care mask */ 900 int ifm_status; /* media status */ 901 int ifm_active; /* IFMWD: active options */ 902 int ifm_count; /* # entries in ifm_ulist 903 array */ 904 int *ifm_ulist; /* array of ifmedia word */ 905 }; 906 907 908 struct ifdrv { 909 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 910 unsigned long ifd_cmd; 911 size_t ifd_len; 912 void *ifd_data; 913 }; 914 #define IFLINKSTR_QUERYLEN 0x01 915 #define IFLINKSTR_UNSET 0x02 916 917 /* 918 * Structure used in SIOCGIFCONF request. 919 * Used to retrieve interface configuration 920 * for machine (useful for programs which 921 * must know all networks accessible). 922 */ 923 struct ifconf { 924 int ifc_len; /* size of associated buffer */ 925 union { 926 void * ifcu_buf; 927 struct ifreq *ifcu_req; 928 } ifc_ifcu; 929 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 930 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 931 }; 932 933 /* 934 * Structure for SIOC[AGD]LIFADDR 935 */ 936 struct if_laddrreq { 937 char iflr_name[IFNAMSIZ]; 938 unsigned int flags; 939 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */ 940 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */ 941 #define IFLR_FACTORY 0x2000 /* in/out: factory link-layer address */ 942 unsigned int prefixlen; /* in/out */ 943 struct sockaddr_storage addr; /* in/out */ 944 struct sockaddr_storage dstaddr; /* out */ 945 }; 946 947 /* 948 * Structure for SIOC[SG]IFADDRPREF 949 */ 950 struct if_addrprefreq { 951 char ifap_name[IFNAMSIZ]; 952 int16_t ifap_preference; /* in/out */ 953 struct sockaddr_storage ifap_addr; /* in/out */ 954 }; 955 956 #include <net/if_arp.h> 957 958 #endif /* _NETBSD_SOURCE */ 959 960 #ifdef _KERNEL 961 #ifdef ALTQ 962 #define IFQ_ENQUEUE(ifq, m, err) \ 963 do { \ 964 mutex_enter((ifq)->ifq_lock); \ 965 if (ALTQ_IS_ENABLED(ifq)) \ 966 ALTQ_ENQUEUE((ifq), (m), (err)); \ 967 else { \ 968 if (IF_QFULL(ifq)) { \ 969 m_freem(m); \ 970 (err) = ENOBUFS; \ 971 } else { \ 972 IF_ENQUEUE((ifq), (m)); \ 973 (err) = 0; \ 974 } \ 975 } \ 976 if ((err)) \ 977 (ifq)->ifq_drops++; \ 978 mutex_exit((ifq)->ifq_lock); \ 979 } while (/*CONSTCOND*/ 0) 980 981 #define IFQ_DEQUEUE(ifq, m) \ 982 do { \ 983 mutex_enter((ifq)->ifq_lock); \ 984 if (TBR_IS_ENABLED(ifq)) \ 985 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \ 986 else if (ALTQ_IS_ENABLED(ifq)) \ 987 ALTQ_DEQUEUE((ifq), (m)); \ 988 else \ 989 IF_DEQUEUE((ifq), (m)); \ 990 mutex_exit((ifq)->ifq_lock); \ 991 } while (/*CONSTCOND*/ 0) 992 993 #define IFQ_POLL(ifq, m) \ 994 do { \ 995 mutex_enter((ifq)->ifq_lock); \ 996 if (TBR_IS_ENABLED(ifq)) \ 997 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \ 998 else if (ALTQ_IS_ENABLED(ifq)) \ 999 ALTQ_POLL((ifq), (m)); \ 1000 else \ 1001 IF_POLL((ifq), (m)); \ 1002 mutex_exit((ifq)->ifq_lock); \ 1003 } while (/*CONSTCOND*/ 0) 1004 1005 #define IFQ_PURGE(ifq) \ 1006 do { \ 1007 mutex_enter((ifq)->ifq_lock); \ 1008 if (ALTQ_IS_ENABLED(ifq)) \ 1009 ALTQ_PURGE(ifq); \ 1010 else \ 1011 IF_PURGE(ifq); \ 1012 mutex_exit((ifq)->ifq_lock); \ 1013 } while (/*CONSTCOND*/ 0) 1014 1015 #define IFQ_SET_READY(ifq) \ 1016 do { \ 1017 (ifq)->altq_flags |= ALTQF_READY; \ 1018 } while (/*CONSTCOND*/ 0) 1019 1020 #define IFQ_CLASSIFY(ifq, m, af) \ 1021 do { \ 1022 KASSERT(((m)->m_flags & M_PKTHDR) != 0); \ 1023 mutex_enter((ifq)->ifq_lock); \ 1024 if (ALTQ_IS_ENABLED(ifq)) { \ 1025 if (ALTQ_NEEDS_CLASSIFY(ifq)) \ 1026 (m)->m_pkthdr.pattr_class = (*(ifq)->altq_classify) \ 1027 ((ifq)->altq_clfier, (m), (af)); \ 1028 (m)->m_pkthdr.pattr_af = (af); \ 1029 (m)->m_pkthdr.pattr_hdr = mtod((m), void *); \ 1030 } \ 1031 mutex_exit((ifq)->ifq_lock); \ 1032 } while (/*CONSTCOND*/ 0) 1033 #else /* ! ALTQ */ 1034 #define IFQ_ENQUEUE(ifq, m, err) \ 1035 do { \ 1036 mutex_enter((ifq)->ifq_lock); \ 1037 if (IF_QFULL(ifq)) { \ 1038 m_freem(m); \ 1039 (err) = ENOBUFS; \ 1040 } else { \ 1041 IF_ENQUEUE((ifq), (m)); \ 1042 (err) = 0; \ 1043 } \ 1044 if (err) \ 1045 (ifq)->ifq_drops++; \ 1046 mutex_exit((ifq)->ifq_lock); \ 1047 } while (/*CONSTCOND*/ 0) 1048 1049 #define IFQ_DEQUEUE(ifq, m) \ 1050 do { \ 1051 mutex_enter((ifq)->ifq_lock); \ 1052 IF_DEQUEUE((ifq), (m)); \ 1053 mutex_exit((ifq)->ifq_lock); \ 1054 } while (/*CONSTCOND*/ 0) 1055 1056 #define IFQ_POLL(ifq, m) \ 1057 do { \ 1058 mutex_enter((ifq)->ifq_lock); \ 1059 IF_POLL((ifq), (m)); \ 1060 mutex_exit((ifq)->ifq_lock); \ 1061 } while (/*CONSTCOND*/ 0) 1062 1063 #define IFQ_PURGE(ifq) \ 1064 do { \ 1065 mutex_enter((ifq)->ifq_lock); \ 1066 IF_PURGE(ifq); \ 1067 mutex_exit((ifq)->ifq_lock); \ 1068 } while (/*CONSTCOND*/ 0) 1069 1070 #define IFQ_SET_READY(ifq) /* nothing */ 1071 1072 #define IFQ_CLASSIFY(ifq, m, af) /* nothing */ 1073 1074 #endif /* ALTQ */ 1075 1076 #define IFQ_LOCK_INIT(ifq) (ifq)->ifq_lock = \ 1077 mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET) 1078 #define IFQ_LOCK_DESTROY(ifq) mutex_obj_free((ifq)->ifq_lock) 1079 #define IFQ_LOCK(ifq) mutex_enter((ifq)->ifq_lock) 1080 #define IFQ_UNLOCK(ifq) mutex_exit((ifq)->ifq_lock) 1081 1082 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY(ifq) 1083 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++) 1084 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len) 1085 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++) 1086 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len)) 1087 1088 #define IFQ_ENQUEUE_ISR(ifq, m, isr) \ 1089 do { \ 1090 IFQ_LOCK(inq); \ 1091 if (IF_QFULL(inq)) { \ 1092 IF_DROP(inq); \ 1093 IFQ_UNLOCK(inq); \ 1094 m_freem(m); \ 1095 } else { \ 1096 IF_ENQUEUE(inq, m); \ 1097 IFQ_UNLOCK(inq); \ 1098 schednetisr(isr); \ 1099 } \ 1100 } while (/*CONSTCOND*/ 0) 1101 1102 #include <sys/mallocvar.h> 1103 MALLOC_DECLARE(M_IFADDR); 1104 MALLOC_DECLARE(M_IFMADDR); 1105 1106 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *); 1107 1108 struct ifnet *if_alloc(u_char); 1109 void if_free(struct ifnet *); 1110 void if_initname(struct ifnet *, const char *, int); 1111 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **); 1112 void if_activate_sadl(struct ifnet *, struct ifaddr *, 1113 const struct sockaddr_dl *); 1114 void if_set_sadl(struct ifnet *, const void *, u_char, bool); 1115 void if_alloc_sadl(struct ifnet *); 1116 void if_free_sadl(struct ifnet *, int); 1117 int if_initialize(struct ifnet *); 1118 void if_register(struct ifnet *); 1119 int if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */ 1120 void if_attachdomain(void); 1121 void if_deactivate(struct ifnet *); 1122 bool if_is_deactivated(const struct ifnet *); 1123 void if_export_if_data(struct ifnet *, struct if_data *, bool); 1124 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *)); 1125 void if_detach(struct ifnet *); 1126 void if_down(struct ifnet *); 1127 void if_down_locked(struct ifnet *); 1128 void if_link_state_change(struct ifnet *, int); 1129 void if_domain_link_state_change(struct ifnet *, int); 1130 void if_up(struct ifnet *); 1131 void ifinit(void); 1132 void ifinit1(void); 1133 void ifinit_post(void); 1134 int ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *); 1135 extern int (*ifioctl)(struct socket *, u_long, void *, struct lwp *); 1136 int ifioctl_common(struct ifnet *, u_long, void *); 1137 int ifpromisc(struct ifnet *, int); 1138 int ifpromisc_locked(struct ifnet *, int); 1139 int if_addr_init(ifnet_t *, struct ifaddr *, bool); 1140 int if_do_dad(struct ifnet *); 1141 int if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *); 1142 int if_flags_set(struct ifnet *, const u_short); 1143 int if_clone_list(int, char *, int *); 1144 1145 struct ifnet *ifunit(const char *); 1146 struct ifnet *if_get(const char *, struct psref *); 1147 ifnet_t *if_byindex(u_int); 1148 ifnet_t *_if_byindex(u_int); 1149 ifnet_t *if_get_byindex(u_int, struct psref *); 1150 ifnet_t *if_get_bylla(const void *, unsigned char, struct psref *); 1151 void if_put(const struct ifnet *, struct psref *); 1152 void if_acquire(struct ifnet *, struct psref *); 1153 #define if_release if_put 1154 1155 int if_tunnel_check_nesting(struct ifnet *, struct mbuf *, int); 1156 percpu_t *if_tunnel_alloc_ro_percpu(void); 1157 void if_tunnel_free_ro_percpu(percpu_t *); 1158 void if_tunnel_ro_percpu_rtcache_free(percpu_t *); 1159 1160 struct tunnel_ro { 1161 struct route *tr_ro; 1162 kmutex_t *tr_lock; 1163 }; 1164 1165 static inline void 1166 if_tunnel_get_ro(percpu_t *ro_percpu, struct route **ro, kmutex_t **lock) 1167 { 1168 struct tunnel_ro *tro; 1169 1170 tro = percpu_getref(ro_percpu); 1171 *ro = tro->tr_ro; 1172 *lock = tro->tr_lock; 1173 mutex_enter(*lock); 1174 } 1175 1176 static inline void 1177 if_tunnel_put_ro(percpu_t *ro_percpu, kmutex_t *lock) 1178 { 1179 1180 mutex_exit(lock); 1181 percpu_putref(ro_percpu); 1182 } 1183 1184 static __inline if_index_t 1185 if_get_index(const struct ifnet *ifp) 1186 { 1187 1188 return ifp != NULL ? ifp->if_index : 0; 1189 } 1190 1191 bool if_held(struct ifnet *); 1192 1193 void if_input(struct ifnet *, struct mbuf *); 1194 1195 struct if_percpuq * 1196 if_percpuq_create(struct ifnet *); 1197 void if_percpuq_destroy(struct if_percpuq *); 1198 void 1199 if_percpuq_enqueue(struct if_percpuq *, struct mbuf *); 1200 1201 void if_deferred_start_init(struct ifnet *, void (*)(struct ifnet *)); 1202 void if_schedule_deferred_start(struct ifnet *); 1203 1204 void ifa_insert(struct ifnet *, struct ifaddr *); 1205 void ifa_remove(struct ifnet *, struct ifaddr *); 1206 1207 void ifa_psref_init(struct ifaddr *); 1208 void ifa_acquire(struct ifaddr *, struct psref *); 1209 void ifa_release(struct ifaddr *, struct psref *); 1210 bool ifa_held(struct ifaddr *); 1211 bool ifa_is_destroying(struct ifaddr *); 1212 1213 void ifaref(struct ifaddr *); 1214 void ifafree(struct ifaddr *); 1215 1216 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *); 1217 struct ifaddr *ifa_ifwithaddr_psref(const struct sockaddr *, struct psref *); 1218 struct ifaddr *ifa_ifwithaf(int); 1219 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *); 1220 struct ifaddr *ifa_ifwithdstaddr_psref(const struct sockaddr *, 1221 struct psref *); 1222 struct ifaddr *ifa_ifwithnet(const struct sockaddr *); 1223 struct ifaddr *ifa_ifwithnet_psref(const struct sockaddr *, struct psref *); 1224 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *); 1225 struct ifaddr *ifa_ifwithladdr_psref(const struct sockaddr *, struct psref *); 1226 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *); 1227 struct ifaddr *ifaof_ifpforaddr_psref(const struct sockaddr *, struct ifnet *, 1228 struct psref *); 1229 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *); 1230 void p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *); 1231 1232 void if_clone_attach(struct if_clone *); 1233 void if_clone_detach(struct if_clone *); 1234 1235 int if_transmit_lock(struct ifnet *, struct mbuf *); 1236 1237 int ifq_enqueue(struct ifnet *, struct mbuf *); 1238 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf *); 1239 1240 int loioctl(struct ifnet *, u_long, void *); 1241 void loopattach(int); 1242 void loopinit(void); 1243 int looutput(struct ifnet *, 1244 struct mbuf *, const struct sockaddr *, const struct rtentry *); 1245 1246 /* 1247 * These are exported because they're an easy way to tell if 1248 * an interface is going away without having to burn a flag. 1249 */ 1250 int if_nulloutput(struct ifnet *, struct mbuf *, 1251 const struct sockaddr *, const struct rtentry *); 1252 void if_nullinput(struct ifnet *, struct mbuf *); 1253 void if_nullstart(struct ifnet *); 1254 int if_nulltransmit(struct ifnet *, struct mbuf *); 1255 int if_nullioctl(struct ifnet *, u_long, void *); 1256 int if_nullinit(struct ifnet *); 1257 void if_nullstop(struct ifnet *, int); 1258 void if_nullslowtimo(struct ifnet *); 1259 #define if_nullwatchdog if_nullslowtimo 1260 void if_nulldrain(struct ifnet *); 1261 #else 1262 struct if_nameindex { 1263 unsigned int if_index; /* 1, 2, ... */ 1264 char *if_name; /* null terminated name: "le0", ... */ 1265 }; 1266 1267 #include <sys/cdefs.h> 1268 __BEGIN_DECLS 1269 unsigned int if_nametoindex(const char *); 1270 char * if_indextoname(unsigned int, char *); 1271 struct if_nameindex * if_nameindex(void); 1272 void if_freenameindex(struct if_nameindex *); 1273 __END_DECLS 1274 #endif /* _KERNEL */ /* XXX really ALTQ? */ 1275 1276 #ifdef _KERNEL 1277 1278 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist) 1279 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list) 1280 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \ 1281 &(__ifp)->if_addrlist, ifa_list) 1282 #define IFADDR_FOREACH_SAFE(__ifa, __ifp, __nifa) \ 1283 TAILQ_FOREACH_SAFE(__ifa, \ 1284 &(__ifp)->if_addrlist, ifa_list, __nifa) 1285 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist) 1286 1287 #define IFADDR_ENTRY_INIT(__ifa) \ 1288 PSLIST_ENTRY_INIT((__ifa), ifa_pslist_entry) 1289 #define IFADDR_ENTRY_DESTROY(__ifa) \ 1290 PSLIST_ENTRY_DESTROY((__ifa), ifa_pslist_entry) 1291 #define IFADDR_READER_EMPTY(__ifp) \ 1292 (PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \ 1293 ifa_pslist_entry) == NULL) 1294 #define IFADDR_READER_FIRST(__ifp) \ 1295 PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \ 1296 ifa_pslist_entry) 1297 #define IFADDR_READER_NEXT(__ifa) \ 1298 PSLIST_READER_NEXT((__ifa), struct ifaddr, ifa_pslist_entry) 1299 #define IFADDR_READER_FOREACH(__ifa, __ifp) \ 1300 PSLIST_READER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\ 1301 ifa_pslist_entry) 1302 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa) \ 1303 PSLIST_WRITER_INSERT_HEAD(&(__ifp)->if_addr_pslist, (__ifa), \ 1304 ifa_pslist_entry) 1305 #define IFADDR_WRITER_REMOVE(__ifa) \ 1306 PSLIST_WRITER_REMOVE((__ifa), ifa_pslist_entry) 1307 #define IFADDR_WRITER_FOREACH(__ifa, __ifp) \ 1308 PSLIST_WRITER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\ 1309 ifa_pslist_entry) 1310 #define IFADDR_WRITER_NEXT(__ifp) \ 1311 PSLIST_WRITER_NEXT((__ifp), struct ifaddr, ifa_pslist_entry) 1312 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new) \ 1313 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), ifa_pslist_entry) 1314 #define IFADDR_WRITER_EMPTY(__ifp) \ 1315 (PSLIST_WRITER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \ 1316 ifa_pslist_entry) == NULL) 1317 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new) \ 1318 do { \ 1319 if (IFADDR_WRITER_EMPTY(__ifp)) { \ 1320 IFADDR_WRITER_INSERT_HEAD((__ifp), (__new)); \ 1321 } else { \ 1322 struct ifaddr *__ifa; \ 1323 IFADDR_WRITER_FOREACH(__ifa, (__ifp)) { \ 1324 if (IFADDR_WRITER_NEXT(__ifa) == NULL) {\ 1325 IFADDR_WRITER_INSERT_AFTER(__ifa,\ 1326 (__new)); \ 1327 break; \ 1328 } \ 1329 } \ 1330 } \ 1331 } while (0) 1332 1333 #define IFNET_GLOBAL_LOCK() mutex_enter(&ifnet_mtx) 1334 #define IFNET_GLOBAL_UNLOCK() mutex_exit(&ifnet_mtx) 1335 #define IFNET_GLOBAL_LOCKED() mutex_owned(&ifnet_mtx) 1336 1337 #define IFNET_READER_EMPTY() \ 1338 (PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL) 1339 #define IFNET_READER_FIRST() \ 1340 PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) 1341 #define IFNET_READER_NEXT(__ifp) \ 1342 PSLIST_READER_NEXT((__ifp), struct ifnet, if_pslist_entry) 1343 #define IFNET_READER_FOREACH(__ifp) \ 1344 PSLIST_READER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \ 1345 if_pslist_entry) 1346 #define IFNET_WRITER_INSERT_HEAD(__ifp) \ 1347 PSLIST_WRITER_INSERT_HEAD(&ifnet_pslist, (__ifp), if_pslist_entry) 1348 #define IFNET_WRITER_REMOVE(__ifp) \ 1349 PSLIST_WRITER_REMOVE((__ifp), if_pslist_entry) 1350 #define IFNET_WRITER_FOREACH(__ifp) \ 1351 PSLIST_WRITER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \ 1352 if_pslist_entry) 1353 #define IFNET_WRITER_NEXT(__ifp) \ 1354 PSLIST_WRITER_NEXT((__ifp), struct ifnet, if_pslist_entry) 1355 #define IFNET_WRITER_INSERT_AFTER(__ifp, __new) \ 1356 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), if_pslist_entry) 1357 #define IFNET_WRITER_EMPTY() \ 1358 (PSLIST_WRITER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL) 1359 #define IFNET_WRITER_INSERT_TAIL(__new) \ 1360 do { \ 1361 if (IFNET_WRITER_EMPTY()) { \ 1362 IFNET_WRITER_INSERT_HEAD(__new); \ 1363 } else { \ 1364 struct ifnet *__ifp; \ 1365 IFNET_WRITER_FOREACH(__ifp) { \ 1366 if (IFNET_WRITER_NEXT(__ifp) == NULL) { \ 1367 IFNET_WRITER_INSERT_AFTER(__ifp,\ 1368 (__new)); \ 1369 break; \ 1370 } \ 1371 } \ 1372 } \ 1373 } while (0) 1374 1375 #define IFNET_LOCK(ifp) mutex_enter((ifp)->if_ioctl_lock) 1376 #define IFNET_UNLOCK(ifp) mutex_exit((ifp)->if_ioctl_lock) 1377 #define IFNET_LOCKED(ifp) mutex_owned((ifp)->if_ioctl_lock) 1378 1379 #define IFNET_ASSERT_UNLOCKED(ifp) \ 1380 KDASSERT(mutex_ownable((ifp)->if_ioctl_lock)) 1381 1382 extern struct pslist_head ifnet_pslist; 1383 extern kmutex_t ifnet_mtx; 1384 1385 extern struct ifnet *lo0ifp; 1386 1387 /* 1388 * ifq sysctl support 1389 */ 1390 int sysctl_ifq(int *name, u_int namelen, void *oldp, 1391 size_t *oldlenp, void *newp, size_t newlen, 1392 struct ifqueue *ifq); 1393 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */ 1394 #define IFQCTL_LEN 1 1395 #define IFQCTL_MAXLEN 2 1396 #define IFQCTL_PEAK 3 1397 #define IFQCTL_DROPS 4 1398 1399 /* 1400 * Hook for if_vlan - needed by if_agr 1401 */ 1402 MODULE_HOOK(if_vlan_vlan_input_hook, void, (struct ifnet *, struct mbuf *)); 1403 1404 #endif /* _KERNEL */ 1405 1406 #endif /* !_NET_IF_H_ */ 1407