1 /* $NetBSD: if.h,v 1.221 2016/07/11 02:14:27 ozaki-r 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 #if defined(_NETBSD_SOURCE) 79 80 #include <sys/socket.h> 81 #include <sys/queue.h> 82 #include <sys/mutex.h> 83 84 #include <net/dlt.h> 85 #include <net/pfil.h> 86 #ifdef _KERNEL 87 #include <net/pktqueue.h> 88 #include <sys/pslist.h> 89 #include <sys/pserialize.h> 90 #include <sys/psref.h> 91 #endif 92 93 /* 94 * Always include ALTQ glue here -- we use the ALTQ interface queue 95 * structure even when ALTQ is not configured into the kernel so that 96 * the size of struct ifnet does not changed based on the option. The 97 * ALTQ queue structure is API-compatible with the legacy ifqueue. 98 */ 99 #include <altq/if_altq.h> 100 101 /* 102 * Structures defining a network interface, providing a packet 103 * transport mechanism (ala level 0 of the PUP protocols). 104 * 105 * Each interface accepts output datagrams of a specified maximum 106 * length, and provides higher level routines with input datagrams 107 * received from its medium. 108 * 109 * Output occurs when the routine if_output is called, with four parameters: 110 * (*ifp->if_output)(ifp, m, dst, rt) 111 * Here m is the mbuf chain to be sent and dst is the destination address. 112 * The output routine encapsulates the supplied datagram if necessary, 113 * and then transmits it on its medium. 114 * 115 * On input, each interface unwraps the data received by it, and either 116 * places it on the input queue of a internetwork datagram routine 117 * and posts the associated software interrupt, or passes the datagram to a raw 118 * packet input routine. 119 * 120 * Routines exist for locating interfaces by their addresses 121 * or for locating a interface on a certain network, as well as more general 122 * routing and gateway routines maintaining information used to locate 123 * interfaces. These routines live in the files if.c and route.c 124 */ 125 #include <sys/time.h> 126 127 #if defined(_KERNEL_OPT) 128 #include "opt_compat_netbsd.h" 129 #include "opt_gateway.h" 130 #endif 131 132 struct mbuf; 133 struct proc; 134 struct rtentry; 135 struct socket; 136 struct ether_header; 137 struct ifaddr; 138 struct ifnet; 139 struct rt_addrinfo; 140 141 #define IFNAMSIZ IF_NAMESIZE 142 143 /* 144 * Structure describing a `cloning' interface. 145 */ 146 struct if_clone { 147 LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */ 148 const char *ifc_name; /* name of device, e.g. `gif' */ 149 size_t ifc_namelen; /* length of name */ 150 151 int (*ifc_create)(struct if_clone *, int); 152 int (*ifc_destroy)(struct ifnet *); 153 }; 154 155 #define IF_CLONE_INITIALIZER(name, create, destroy) \ 156 { { NULL, NULL }, name, sizeof(name) - 1, create, destroy } 157 158 /* 159 * Structure used to query names of interface cloners. 160 */ 161 struct if_clonereq { 162 int ifcr_total; /* total cloners (out) */ 163 int ifcr_count; /* room for this many in user buffer */ 164 char *ifcr_buffer; /* buffer for cloner names */ 165 }; 166 167 /* 168 * Structure defining statistics and other data kept regarding a network 169 * interface. 170 */ 171 struct if_data { 172 /* generic interface information */ 173 u_char ifi_type; /* ethernet, tokenring, etc. */ 174 u_char ifi_addrlen; /* media address length */ 175 u_char ifi_hdrlen; /* media header length */ 176 int ifi_link_state; /* current link state */ 177 uint64_t ifi_mtu; /* maximum transmission unit */ 178 uint64_t ifi_metric; /* routing metric (external only) */ 179 uint64_t ifi_baudrate; /* linespeed */ 180 /* volatile statistics */ 181 uint64_t ifi_ipackets; /* packets received on interface */ 182 uint64_t ifi_ierrors; /* input errors on interface */ 183 uint64_t ifi_opackets; /* packets sent on interface */ 184 uint64_t ifi_oerrors; /* output errors on interface */ 185 uint64_t ifi_collisions; /* collisions on csma interfaces */ 186 uint64_t ifi_ibytes; /* total number of octets received */ 187 uint64_t ifi_obytes; /* total number of octets sent */ 188 uint64_t ifi_imcasts; /* packets received via multicast */ 189 uint64_t ifi_omcasts; /* packets sent via multicast */ 190 uint64_t ifi_iqdrops; /* dropped on input, this interface */ 191 uint64_t ifi_noproto; /* destined for unsupported protocol */ 192 struct timespec ifi_lastchange;/* last operational state change */ 193 }; 194 195 /* 196 * Values for if_link_state. 197 */ 198 #define LINK_STATE_UNKNOWN 0 /* link invalid/unknown */ 199 #define LINK_STATE_DOWN 1 /* link is down */ 200 #define LINK_STATE_UP 2 /* link is up */ 201 202 /* 203 * Structure defining a queue for a network interface. 204 */ 205 struct ifqueue { 206 struct mbuf *ifq_head; 207 struct mbuf *ifq_tail; 208 int ifq_len; 209 int ifq_maxlen; 210 int ifq_drops; 211 kmutex_t *ifq_lock; 212 }; 213 214 #ifdef _KERNEL 215 #include <sys/percpu.h> 216 #include <sys/callout.h> 217 #include <sys/rwlock.h> 218 219 #endif /* _KERNEL */ 220 221 /* 222 * Structure defining a queue for a network interface. 223 * 224 * (Would like to call this struct ``if'', but C isn't PL/1.) 225 */ 226 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */ 227 228 struct bridge_softc; 229 struct bridge_iflist; 230 struct callout; 231 struct krwlock; 232 struct if_percpuq; 233 234 typedef unsigned short if_index_t; 235 236 typedef struct ifnet { 237 void *if_softc; /* lower-level data for this if */ 238 /* DEPRECATED. Keep it to avoid breaking kvm(3) users */ 239 TAILQ_ENTRY(ifnet) if_list; /* all struct ifnets are chained */ 240 TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */ 241 char if_xname[IFNAMSIZ]; /* external name (name + unit) */ 242 int if_pcount; /* number of promiscuous listeners */ 243 struct bpf_if *if_bpf; /* packet filter structure */ 244 if_index_t if_index; /* numeric abbreviation for this if */ 245 short if_timer; /* time 'til if_slowtimo called */ 246 short if_flags; /* up/down, broadcast, etc. */ 247 short if_extflags; /* if_output MP-safe, etc. */ 248 struct if_data if_data; /* statistics and other data about if */ 249 /* 250 * Procedure handles. If you add more of these, don't forget the 251 * corresponding NULL stub in if.c. 252 */ 253 int (*if_output) /* output routine (enqueue) */ 254 (struct ifnet *, struct mbuf *, const struct sockaddr *, 255 const struct rtentry *); 256 void (*_if_input) /* input routine (from h/w driver) */ 257 (struct ifnet *, struct mbuf *); 258 void (*if_start) /* initiate output routine */ 259 (struct ifnet *); 260 int (*if_transmit) /* output routine, must be MP-safe */ 261 (struct ifnet *, struct mbuf *); 262 int (*if_ioctl) /* ioctl routine */ 263 (struct ifnet *, u_long, void *); 264 int (*if_init) /* init routine */ 265 (struct ifnet *); 266 void (*if_stop) /* stop routine */ 267 (struct ifnet *, int); 268 void (*if_slowtimo) /* timer routine */ 269 (struct ifnet *); 270 #define if_watchdog if_slowtimo 271 void (*if_drain) /* routine to release resources */ 272 (struct ifnet *); 273 struct ifaltq if_snd; /* output queue (includes altq) */ 274 struct ifaddr *if_dl; /* identity of this interface. */ 275 const struct sockaddr_dl *if_sadl; /* pointer to sockaddr_dl 276 * of if_dl 277 */ 278 /* if_hwdl: h/w identity 279 * 280 * May be NULL. If not NULL, it is the address assigned 281 * to the interface by the manufacturer, so it very likely 282 * to be unique. It MUST NOT be deleted. It is highly 283 * suitable for deriving the EUI64 for the interface. 284 */ 285 struct ifaddr *if_hwdl; 286 const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */ 287 struct bridge_softc *if_bridge; /* bridge glue */ 288 struct bridge_iflist *if_bridgeif; /* shortcut to interface list entry */ 289 int if_dlt; /* data link type (<net/dlt.h>) */ 290 pfil_head_t * if_pfil; /* filtering point */ 291 uint64_t if_capabilities; /* interface capabilities */ 292 uint64_t if_capenable; /* capabilities enabled */ 293 union { 294 void * carp_s; /* carp structure (used by !carp ifs) */ 295 struct ifnet *carp_d;/* ptr to carpdev (used by carp ifs) */ 296 } if_carp_ptr; 297 #define if_carp if_carp_ptr.carp_s 298 #define if_carpdev if_carp_ptr.carp_d 299 /* 300 * These are pre-computed based on an interfaces enabled 301 * capabilities, for speed elsewhere. 302 */ 303 int if_csum_flags_tx; /* M_CSUM_* flags for Tx */ 304 int if_csum_flags_rx; /* M_CSUM_* flags for Rx */ 305 306 void *if_afdata[AF_MAX]; 307 struct mowner *if_mowner; /* who owns mbufs for this interface */ 308 309 void *if_agrprivate; /* used only when #if NAGR > 0 */ 310 311 /* 312 * pf specific data, used only when #if NPF > 0. 313 */ 314 void *if_pf_kif; /* pf interface abstraction */ 315 void *if_pf_groups; /* pf interface groups */ 316 /* 317 * During an ifnet's lifetime, it has only one if_index, but 318 * and if_index is not sufficient to identify an ifnet 319 * because during the lifetime of the system, many ifnets may occupy a 320 * given if_index. Let us tell different ifnets at the same 321 * if_index apart by their if_index_gen, a unique number that each ifnet 322 * is assigned when it if_attach()s. Now, the kernel can use the 323 * pair (if_index, if_index_gen) as a weak reference to an ifnet. 324 */ 325 uint64_t if_index_gen; /* generation number for the ifnet 326 * at if_index: if two ifnets' index 327 * and generation number are both the 328 * same, they are the same ifnet. 329 */ 330 struct sysctllog *if_sysctl_log; 331 int (*if_initaddr)(struct ifnet *, struct ifaddr *, bool); 332 int (*if_mcastop)(struct ifnet *, const unsigned long, 333 const struct sockaddr *); 334 int (*if_setflags)(struct ifnet *, const short); 335 kmutex_t *if_ioctl_lock; 336 #ifdef _KERNEL /* XXX kvm(3) */ 337 struct callout *if_slowtimo_ch; 338 struct krwlock *if_afdata_lock; 339 struct if_percpuq *if_percpuq; /* We should remove it in the future */ 340 void *if_link_si; /* softint to handle link state changes */ 341 uint16_t if_link_queue; /* masked link state change queue */ 342 struct pslist_entry if_pslist_entry; 343 struct psref_target if_psref; 344 struct pslist_head if_addr_pslist; 345 #endif 346 } ifnet_t; 347 348 #define if_mtu if_data.ifi_mtu 349 #define if_type if_data.ifi_type 350 #define if_addrlen if_data.ifi_addrlen 351 #define if_hdrlen if_data.ifi_hdrlen 352 #define if_metric if_data.ifi_metric 353 #define if_link_state if_data.ifi_link_state 354 #define if_baudrate if_data.ifi_baudrate 355 #define if_ipackets if_data.ifi_ipackets 356 #define if_ierrors if_data.ifi_ierrors 357 #define if_opackets if_data.ifi_opackets 358 #define if_oerrors if_data.ifi_oerrors 359 #define if_collisions if_data.ifi_collisions 360 #define if_ibytes if_data.ifi_ibytes 361 #define if_obytes if_data.ifi_obytes 362 #define if_imcasts if_data.ifi_imcasts 363 #define if_omcasts if_data.ifi_omcasts 364 #define if_iqdrops if_data.ifi_iqdrops 365 #define if_noproto if_data.ifi_noproto 366 #define if_lastchange if_data.ifi_lastchange 367 368 #define IFF_UP 0x0001 /* interface is up */ 369 #define IFF_BROADCAST 0x0002 /* broadcast address valid */ 370 #define IFF_DEBUG 0x0004 /* turn on debugging */ 371 #define IFF_LOOPBACK 0x0008 /* is a loopback net */ 372 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */ 373 #define IFF_NOTRAILERS 0x0020 /* avoid use of trailers */ 374 #define IFF_RUNNING 0x0040 /* resources allocated */ 375 #define IFF_NOARP 0x0080 /* no address resolution protocol */ 376 #define IFF_PROMISC 0x0100 /* receive all packets */ 377 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */ 378 #define IFF_OACTIVE 0x0400 /* transmission in progress */ 379 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */ 380 #define IFF_LINK0 0x1000 /* per link layer defined bit */ 381 #define IFF_LINK1 0x2000 /* per link layer defined bit */ 382 #define IFF_LINK2 0x4000 /* per link layer defined bit */ 383 #define IFF_MULTICAST 0x8000 /* supports multicast */ 384 385 #define IFEF_OUTPUT_MPSAFE __BIT(0) /* if_output() can run parallel */ 386 #define IFEF_START_MPSAFE __BIT(1) /* if_start() can run parallel */ 387 #define IFEF_NO_LINK_STATE_CHANGE __BIT(2) /* doesn't use link state interrupts */ 388 389 #ifdef _KERNEL 390 static inline bool 391 if_output_is_mpsafe(struct ifnet *ifp) 392 { 393 394 return ((ifp->if_extflags & IFEF_OUTPUT_MPSAFE) != 0); 395 } 396 397 static inline int 398 if_output_lock(struct ifnet *cifp, struct ifnet *ifp, struct mbuf *m, 399 const struct sockaddr *dst, const struct rtentry *rt) 400 { 401 402 if (if_output_is_mpsafe(cifp)) { 403 return (*cifp->if_output)(ifp, m, dst, rt); 404 } else { 405 int ret; 406 407 KERNEL_LOCK(1, NULL); 408 ret = (*cifp->if_output)(ifp, m, dst, rt); 409 KERNEL_UNLOCK_ONE(NULL); 410 return ret; 411 } 412 } 413 414 static inline bool 415 if_start_is_mpsafe(struct ifnet *ifp) 416 { 417 418 return ((ifp->if_extflags & IFEF_START_MPSAFE) != 0); 419 } 420 421 static inline void 422 if_start_lock(struct ifnet *ifp) 423 { 424 425 if (if_start_is_mpsafe(ifp)) { 426 (*ifp->if_start)(ifp); 427 } else { 428 KERNEL_LOCK(1, NULL); 429 (*ifp->if_start)(ifp); 430 KERNEL_UNLOCK_ONE(NULL); 431 } 432 } 433 434 static inline bool 435 if_is_link_state_changeable(struct ifnet *ifp) 436 { 437 438 return ((ifp->if_extflags & IFEF_NO_LINK_STATE_CHANGE) == 0); 439 } 440 #endif /* _KERNEL */ 441 442 #define IFFBITS \ 443 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \ 444 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \ 445 "\15LINK0\16LINK1\17LINK2\20MULTICAST" 446 447 /* flags set internally only: */ 448 #define IFF_CANTCHANGE \ 449 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\ 450 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC) 451 452 /* 453 * Some convenience macros used for setting ifi_baudrate. 454 */ 455 #define IF_Kbps(x) ((x) * 1000ULL) /* kilobits/sec. */ 456 #define IF_Mbps(x) (IF_Kbps((x) * 1000ULL)) /* megabits/sec. */ 457 #define IF_Gbps(x) (IF_Mbps((x) * 1000ULL)) /* gigabits/sec. */ 458 459 /* Capabilities that interfaces can advertise. */ 460 /* 0x01 .. 0x40 were previously used */ 461 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */ 462 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */ 463 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */ 464 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */ 465 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */ 466 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */ 467 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */ 468 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */ 469 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */ 470 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */ 471 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */ 472 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */ 473 #define IFCAP_LRO 0x80000 /* can do Large Receive Offload */ 474 #define IFCAP_MASK 0xfff80 /* currently valid capabilities */ 475 476 #define IFCAPBITS \ 477 "\020" \ 478 "\10TSO4" \ 479 "\11IP4CSUM_Rx" \ 480 "\12IP4CSUM_Tx" \ 481 "\13TCP4CSUM_Rx" \ 482 "\14TCP4CSUM_Tx" \ 483 "\15UDP4CSUM_Rx" \ 484 "\16UDP4CSUM_Tx" \ 485 "\17TCP6CSUM_Rx" \ 486 "\20TCP6CSUM_Tx" \ 487 "\21UDP6CSUM_Rx" \ 488 "\22UDP6CSUM_Tx" \ 489 "\23TSO6" \ 490 "\24LRO" \ 491 492 #define IF_AFDATA_LOCK_INIT(ifp) \ 493 do {(ifp)->if_afdata_lock = rw_obj_alloc();} while (0) 494 495 #define IF_AFDATA_LOCK_DESTROY(ifp) rw_obj_free((ifp)->if_afdata_lock) 496 497 #define IF_AFDATA_WLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_WRITER) 498 #define IF_AFDATA_RLOCK(ifp) rw_enter((ifp)->if_afdata_lock, RW_READER) 499 #define IF_AFDATA_WUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock) 500 #define IF_AFDATA_RUNLOCK(ifp) rw_exit((ifp)->if_afdata_lock) 501 #define IF_AFDATA_LOCK(ifp) IF_AFDATA_WLOCK(ifp) 502 #define IF_AFDATA_UNLOCK(ifp) IF_AFDATA_WUNLOCK(ifp) 503 #define IF_AFDATA_TRYLOCK(ifp) rw_tryenter((ifp)->if_afdata_lock, RW_WRITER) 504 505 #define IF_AFDATA_LOCK_ASSERT(ifp) \ 506 KASSERT(rw_lock_held((ifp)->if_afdata_lock)) 507 #define IF_AFDATA_RLOCK_ASSERT(ifp) \ 508 KASSERT(rw_read_held((ifp)->if_afdata_lock)) 509 #define IF_AFDATA_WLOCK_ASSERT(ifp) \ 510 KASSERT(rw_write_held((ifp)->if_afdata_lock)) 511 #define IF_AFDATA_UNLOCK_ASSERT(ifp) \ 512 KASSERT(!rw_lock_held((ifp)->if_afdata_lock)) 513 514 #define IFQ_LOCK(_ifq) if ((_ifq)->ifq_lock) mutex_enter((_ifq)->ifq_lock) 515 #define IFQ_UNLOCK(_ifq) if ((_ifq)->ifq_lock) mutex_exit((_ifq)->ifq_lock) 516 517 /* 518 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 519 * input routines have queues of messages stored on ifqueue structures 520 * (defined above). Entries are added to and deleted from these structures 521 * by these macros, which should be called with ipl raised to splnet(). 522 */ 523 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 524 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 525 #define IF_ENQUEUE(ifq, m) do { \ 526 (m)->m_nextpkt = 0; \ 527 if ((ifq)->ifq_tail == 0) \ 528 (ifq)->ifq_head = m; \ 529 else \ 530 (ifq)->ifq_tail->m_nextpkt = m; \ 531 (ifq)->ifq_tail = m; \ 532 (ifq)->ifq_len++; \ 533 } while (/*CONSTCOND*/0) 534 #define IF_PREPEND(ifq, m) do { \ 535 (m)->m_nextpkt = (ifq)->ifq_head; \ 536 if ((ifq)->ifq_tail == 0) \ 537 (ifq)->ifq_tail = (m); \ 538 (ifq)->ifq_head = (m); \ 539 (ifq)->ifq_len++; \ 540 } while (/*CONSTCOND*/0) 541 #define IF_DEQUEUE(ifq, m) do { \ 542 (m) = (ifq)->ifq_head; \ 543 if (m) { \ 544 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \ 545 (ifq)->ifq_tail = 0; \ 546 (m)->m_nextpkt = 0; \ 547 (ifq)->ifq_len--; \ 548 } \ 549 } while (/*CONSTCOND*/0) 550 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head) 551 #define IF_PURGE(ifq) \ 552 do { \ 553 struct mbuf *__m0; \ 554 \ 555 for (;;) { \ 556 IF_DEQUEUE((ifq), __m0); \ 557 if (__m0 == NULL) \ 558 break; \ 559 else \ 560 m_freem(__m0); \ 561 } \ 562 } while (/*CONSTCOND*/ 0) 563 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0) 564 565 #ifndef IFQ_MAXLEN 566 #define IFQ_MAXLEN 256 567 #endif 568 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 569 570 /* 571 * Structure defining statistics and other data kept regarding an address 572 * on a network interface. 573 */ 574 struct ifaddr_data { 575 int64_t ifad_inbytes; 576 int64_t ifad_outbytes; 577 }; 578 579 /* 580 * The ifaddr structure contains information about one address 581 * of an interface. They are maintained by the different address families, 582 * are allocated and attached when an address is set, and are linked 583 * together so all addresses for an interface can be located. 584 */ 585 struct ifaddr { 586 struct sockaddr *ifa_addr; /* address of interface */ 587 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 588 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 589 struct sockaddr *ifa_netmask; /* used to determine subnet */ 590 struct ifnet *ifa_ifp; /* back-pointer to interface */ 591 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */ 592 struct ifaddr_data ifa_data; /* statistics on the address */ 593 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */ 594 (int, struct rtentry *, const struct rt_addrinfo *); 595 u_int ifa_flags; /* mostly rt_flags for cloning */ 596 int ifa_refcnt; /* count of references */ 597 int ifa_metric; /* cost of going out this interface */ 598 struct ifaddr *(*ifa_getifa)(struct ifaddr *, 599 const struct sockaddr *); 600 uint32_t *ifa_seqno; 601 int16_t ifa_preference; /* preference level for this address */ 602 /* XXX adding variables here breaks kvm(3) users of struct *_ifaddr */ 603 }; 604 #define IFA_ROUTE RTF_UP /* (0x01) route installed */ 605 606 /* 607 * Message format for use in obtaining information about interfaces from 608 * sysctl and the routing socket. We need to force 64-bit alignment if we 609 * aren't using compatiblity definitons. 610 */ 611 #if !defined(_KERNEL) || !defined(COMPAT_RTSOCK) 612 #define __align64 __aligned(sizeof(uint64_t)) 613 #else 614 #define __align64 615 #endif 616 struct if_msghdr { 617 u_short ifm_msglen __align64; 618 /* to skip over non-understood messages */ 619 u_char ifm_version; /* future binary compatibility */ 620 u_char ifm_type; /* message type */ 621 int ifm_addrs; /* like rtm_addrs */ 622 int ifm_flags; /* value of if_flags */ 623 u_short ifm_index; /* index for associated ifp */ 624 struct if_data ifm_data __align64; 625 /* statistics and other data about if */ 626 }; 627 628 /* 629 * Message format for use in obtaining information about interface addresses 630 * from sysctl and the routing socket. 631 */ 632 struct ifa_msghdr { 633 u_short ifam_msglen __align64; 634 /* to skip over non-understood messages */ 635 u_char ifam_version; /* future binary compatibility */ 636 u_char ifam_type; /* message type */ 637 int ifam_addrs; /* like rtm_addrs */ 638 int ifam_flags; /* value of ifa_flags */ 639 int ifam_metric; /* value of ifa_metric */ 640 u_short ifam_index; /* index for associated ifp */ 641 }; 642 643 /* 644 * Message format announcing the arrival or departure of a network interface. 645 */ 646 struct if_announcemsghdr { 647 u_short ifan_msglen __align64; 648 /* to skip over non-understood messages */ 649 u_char ifan_version; /* future binary compatibility */ 650 u_char ifan_type; /* message type */ 651 u_short ifan_index; /* index for associated ifp */ 652 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 653 u_short ifan_what; /* what type of announcement */ 654 }; 655 656 #define IFAN_ARRIVAL 0 /* interface arrival */ 657 #define IFAN_DEPARTURE 1 /* interface departure */ 658 659 #undef __align64 660 661 /* 662 * Interface request structure used for socket 663 * ioctl's. All interface ioctl's must have parameter 664 * definitions which begin with ifr_name. The 665 * remainder may be interface specific. 666 */ 667 struct ifreq { 668 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 669 union { 670 struct sockaddr ifru_addr; 671 struct sockaddr ifru_dstaddr; 672 struct sockaddr ifru_broadaddr; 673 struct sockaddr_storage ifru_space; 674 short ifru_flags; 675 int ifru_addrflags; 676 int ifru_metric; 677 int ifru_mtu; 678 int ifru_dlt; 679 u_int ifru_value; 680 void * ifru_data; 681 struct { 682 uint32_t b_buflen; 683 void *b_buf; 684 } ifru_b; 685 } ifr_ifru; 686 #define ifr_addr ifr_ifru.ifru_addr /* address */ 687 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 688 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 689 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */ 690 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 691 #define ifr_addrflags ifr_ifru.ifru_addrflags /* addr flags */ 692 #define ifr_metric ifr_ifru.ifru_metric /* metric */ 693 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */ 694 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */ 695 #define ifr_value ifr_ifru.ifru_value /* generic value */ 696 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */ 697 #define ifr_data ifr_ifru.ifru_data /* for use by interface 698 * XXX deprecated 699 */ 700 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */ 701 #define ifr_buflen ifr_ifru.ifru_b.b_buflen 702 #define ifr_index ifr_ifru.ifru_value /* interface index, BSD */ 703 #define ifr_ifindex ifr_index /* interface index, linux */ 704 }; 705 706 #ifdef _KERNEL 707 #define ifreq_setdstaddr ifreq_setaddr 708 #define ifreq_setbroadaddr ifreq_setaddr 709 #define ifreq_getdstaddr ifreq_getaddr 710 #define ifreq_getbroadaddr ifreq_getaddr 711 712 static inline const struct sockaddr * 713 /*ARGSUSED*/ 714 ifreq_getaddr(u_long cmd, const struct ifreq *ifr) 715 { 716 return &ifr->ifr_addr; 717 } 718 #endif /* _KERNEL */ 719 720 struct ifcapreq { 721 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 722 uint64_t ifcr_capabilities; /* supported capabiliites */ 723 uint64_t ifcr_capenable; /* capabilities enabled */ 724 }; 725 726 struct ifaliasreq { 727 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 728 struct sockaddr ifra_addr; 729 struct sockaddr ifra_dstaddr; 730 #define ifra_broadaddr ifra_dstaddr 731 struct sockaddr ifra_mask; 732 }; 733 734 struct ifdatareq { 735 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 736 struct if_data ifdr_data; 737 }; 738 739 struct ifmediareq { 740 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 741 int ifm_current; /* current media options */ 742 int ifm_mask; /* don't care mask */ 743 int ifm_status; /* media status */ 744 int ifm_active; /* active options */ 745 int ifm_count; /* # entries in ifm_ulist 746 array */ 747 int *ifm_ulist; /* media words */ 748 }; 749 750 751 struct ifdrv { 752 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 753 unsigned long ifd_cmd; 754 size_t ifd_len; 755 void *ifd_data; 756 }; 757 #define IFLINKSTR_QUERYLEN 0x01 758 #define IFLINKSTR_UNSET 0x02 759 760 /* 761 * Structure used in SIOCGIFCONF request. 762 * Used to retrieve interface configuration 763 * for machine (useful for programs which 764 * must know all networks accessible). 765 */ 766 struct ifconf { 767 int ifc_len; /* size of associated buffer */ 768 union { 769 void * ifcu_buf; 770 struct ifreq *ifcu_req; 771 } ifc_ifcu; 772 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 773 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 774 }; 775 776 /* 777 * Structure for SIOC[AGD]LIFADDR 778 */ 779 struct if_laddrreq { 780 char iflr_name[IFNAMSIZ]; 781 unsigned int flags; 782 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */ 783 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */ 784 #define IFLR_FACTORY 0x2000 /* in/out: factory link-layer address */ 785 unsigned int prefixlen; /* in/out */ 786 struct sockaddr_storage addr; /* in/out */ 787 struct sockaddr_storage dstaddr; /* out */ 788 }; 789 790 /* 791 * Structure for SIOC[SG]IFADDRPREF 792 */ 793 struct if_addrprefreq { 794 char ifap_name[IFNAMSIZ]; 795 int16_t ifap_preference; /* in/out */ 796 struct sockaddr_storage ifap_addr; /* in/out */ 797 }; 798 799 #include <net/if_arp.h> 800 801 #endif /* _NETBSD_SOURCE */ 802 803 #ifdef _KERNEL 804 #ifdef ALTQ 805 #define IFQ_ENQUEUE(ifq, m, err) \ 806 do { \ 807 IFQ_LOCK((ifq)); \ 808 if (ALTQ_IS_ENABLED((ifq))) \ 809 ALTQ_ENQUEUE((ifq), (m), (err)); \ 810 else { \ 811 if (IF_QFULL((ifq))) { \ 812 m_freem((m)); \ 813 (err) = ENOBUFS; \ 814 } else { \ 815 IF_ENQUEUE((ifq), (m)); \ 816 (err) = 0; \ 817 } \ 818 } \ 819 if ((err)) \ 820 (ifq)->ifq_drops++; \ 821 IFQ_UNLOCK((ifq)); \ 822 } while (/*CONSTCOND*/ 0) 823 824 #define IFQ_DEQUEUE(ifq, m) \ 825 do { \ 826 IFQ_LOCK((ifq)); \ 827 if (TBR_IS_ENABLED((ifq))) \ 828 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \ 829 else if (ALTQ_IS_ENABLED((ifq))) \ 830 ALTQ_DEQUEUE((ifq), (m)); \ 831 else \ 832 IF_DEQUEUE((ifq), (m)); \ 833 IFQ_UNLOCK((ifq)); \ 834 } while (/*CONSTCOND*/ 0) 835 836 #define IFQ_POLL(ifq, m) \ 837 do { \ 838 IFQ_LOCK((ifq)); \ 839 if (TBR_IS_ENABLED((ifq))) \ 840 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \ 841 else if (ALTQ_IS_ENABLED((ifq))) \ 842 ALTQ_POLL((ifq), (m)); \ 843 else \ 844 IF_POLL((ifq), (m)); \ 845 IFQ_UNLOCK((ifq)); \ 846 } while (/*CONSTCOND*/ 0) 847 848 #define IFQ_PURGE(ifq) \ 849 do { \ 850 IFQ_LOCK((ifq)); \ 851 if (ALTQ_IS_ENABLED((ifq))) \ 852 ALTQ_PURGE((ifq)); \ 853 else \ 854 IF_PURGE((ifq)); \ 855 IFQ_UNLOCK((ifq)); \ 856 } while (/*CONSTCOND*/ 0) 857 858 #define IFQ_SET_READY(ifq) \ 859 do { \ 860 (ifq)->altq_flags |= ALTQF_READY; \ 861 } while (/*CONSTCOND*/ 0) 862 863 #define IFQ_CLASSIFY(ifq, m, af) \ 864 do { \ 865 IFQ_LOCK((ifq)); \ 866 if (ALTQ_IS_ENABLED((ifq))) { \ 867 if (ALTQ_NEEDS_CLASSIFY((ifq))) \ 868 m->m_pkthdr.pattr_class = (*(ifq)->altq_classify) \ 869 ((ifq)->altq_clfier, (m), (af)); \ 870 m->m_pkthdr.pattr_af = (af); \ 871 m->m_pkthdr.pattr_hdr = mtod((m), void *); \ 872 } \ 873 IFQ_UNLOCK((ifq)); \ 874 } while (/*CONSTCOND*/ 0) 875 #else /* ! ALTQ */ 876 #define IFQ_ENQUEUE(ifq, m, err) \ 877 do { \ 878 IFQ_LOCK((ifq)); \ 879 if (IF_QFULL((ifq))) { \ 880 m_freem((m)); \ 881 (err) = ENOBUFS; \ 882 } else { \ 883 IF_ENQUEUE((ifq), (m)); \ 884 (err) = 0; \ 885 } \ 886 if ((err)) \ 887 (ifq)->ifq_drops++; \ 888 IFQ_UNLOCK((ifq)); \ 889 } while (/*CONSTCOND*/ 0) 890 891 #define IFQ_DEQUEUE(ifq, m) \ 892 do { \ 893 IFQ_LOCK((ifq)); \ 894 IF_DEQUEUE((ifq), (m)); \ 895 IFQ_UNLOCK((ifq)); \ 896 } while (/*CONSTCOND*/ 0) 897 898 #define IFQ_POLL(ifq, m) \ 899 do { \ 900 IFQ_LOCK((ifq)); \ 901 IF_POLL((ifq), (m)); \ 902 IFQ_UNLOCK((ifq)); \ 903 } while (/*CONSTCOND*/ 0) 904 905 #define IFQ_PURGE(ifq) \ 906 do { \ 907 IFQ_LOCK((ifq)); \ 908 IF_PURGE((ifq)); \ 909 IFQ_UNLOCK((ifq)); \ 910 } while (/*CONSTCOND*/ 0) 911 912 #define IFQ_SET_READY(ifq) /* nothing */ 913 914 #define IFQ_CLASSIFY(ifq, m, af) /* nothing */ 915 916 #endif /* ALTQ */ 917 918 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY((ifq)) 919 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++) 920 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len) 921 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++) 922 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len)) 923 924 #include <sys/mallocvar.h> 925 MALLOC_DECLARE(M_IFADDR); 926 MALLOC_DECLARE(M_IFMADDR); 927 928 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *); 929 930 struct ifnet *if_alloc(u_char); 931 void if_free(struct ifnet *); 932 void if_initname(struct ifnet *, const char *, int); 933 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **); 934 void if_activate_sadl(struct ifnet *, struct ifaddr *, 935 const struct sockaddr_dl *); 936 void if_set_sadl(struct ifnet *, const void *, u_char, bool); 937 void if_alloc_sadl(struct ifnet *); 938 void if_initialize(struct ifnet *); 939 void if_register(struct ifnet *); 940 void if_attach(struct ifnet *); /* Deprecated. Use if_initialize and if_register */ 941 void if_attachdomain(void); 942 void if_deactivate(struct ifnet *); 943 bool if_is_deactivated(struct ifnet *); 944 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *)); 945 void if_detach(struct ifnet *); 946 void if_down(struct ifnet *); 947 void if_link_state_change(struct ifnet *, int); 948 void if_up(struct ifnet *); 949 void ifinit(void); 950 void ifinit1(void); 951 int ifaddrpref_ioctl(struct socket *, u_long, void *, struct ifnet *); 952 extern int (*ifioctl)(struct socket *, u_long, void *, struct lwp *); 953 int ifioctl_common(struct ifnet *, u_long, void *); 954 int ifpromisc(struct ifnet *, int); 955 int if_addr_init(ifnet_t *, struct ifaddr *, bool); 956 int if_do_dad(struct ifnet *); 957 int if_mcast_op(ifnet_t *, const unsigned long, const struct sockaddr *); 958 int if_flags_set(struct ifnet *, const short); 959 int if_clone_list(int, char *, int *); 960 961 struct ifnet *ifunit(const char *); 962 struct ifnet *if_get(const char *, struct psref *); 963 ifnet_t *if_byindex(u_int); 964 ifnet_t *if_get_byindex(u_int, struct psref *); 965 void if_put(const struct ifnet *, struct psref *); 966 void if_acquire_NOMPSAFE(struct ifnet *, struct psref *); 967 968 static inline if_index_t 969 if_get_index(const struct ifnet *ifp) 970 { 971 972 return ifp != NULL ? ifp->if_index : 0; 973 } 974 975 bool if_held(struct ifnet *); 976 977 void if_input(struct ifnet *, struct mbuf *); 978 979 struct if_percpuq * 980 if_percpuq_create(struct ifnet *); 981 void if_percpuq_destroy(struct if_percpuq *); 982 void 983 if_percpuq_enqueue(struct if_percpuq *, struct mbuf *); 984 985 void ifa_insert(struct ifnet *, struct ifaddr *); 986 void ifa_remove(struct ifnet *, struct ifaddr *); 987 988 void ifaref(struct ifaddr *); 989 void ifafree(struct ifaddr *); 990 991 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *); 992 struct ifaddr *ifa_ifwithaf(int); 993 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *); 994 struct ifaddr *ifa_ifwithnet(const struct sockaddr *); 995 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *); 996 struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *, 997 const struct sockaddr *); 998 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *); 999 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *); 1000 void p2p_rtrequest(int, struct rtentry *, const struct rt_addrinfo *); 1001 1002 void if_clone_attach(struct if_clone *); 1003 void if_clone_detach(struct if_clone *); 1004 1005 int if_transmit_lock(struct ifnet *, struct mbuf *); 1006 1007 int ifq_enqueue(struct ifnet *, struct mbuf *); 1008 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf *); 1009 1010 int loioctl(struct ifnet *, u_long, void *); 1011 void loopattach(int); 1012 int looutput(struct ifnet *, 1013 struct mbuf *, const struct sockaddr *, const struct rtentry *); 1014 void lortrequest(int, struct rtentry *, const struct rt_addrinfo *); 1015 1016 /* 1017 * These are exported because they're an easy way to tell if 1018 * an interface is going away without having to burn a flag. 1019 */ 1020 int if_nulloutput(struct ifnet *, struct mbuf *, 1021 const struct sockaddr *, const struct rtentry *); 1022 void if_nullinput(struct ifnet *, struct mbuf *); 1023 void if_nullstart(struct ifnet *); 1024 int if_nulltransmit(struct ifnet *, struct mbuf *); 1025 int if_nullioctl(struct ifnet *, u_long, void *); 1026 int if_nullinit(struct ifnet *); 1027 void if_nullstop(struct ifnet *, int); 1028 void if_nullslowtimo(struct ifnet *); 1029 #define if_nullwatchdog if_nullslowtimo 1030 void if_nulldrain(struct ifnet *); 1031 #else 1032 struct if_nameindex { 1033 unsigned int if_index; /* 1, 2, ... */ 1034 char *if_name; /* null terminated name: "le0", ... */ 1035 }; 1036 1037 #include <sys/cdefs.h> 1038 __BEGIN_DECLS 1039 unsigned int if_nametoindex(const char *); 1040 char * if_indextoname(unsigned int, char *); 1041 struct if_nameindex * if_nameindex(void); 1042 void if_freenameindex(struct if_nameindex *); 1043 __END_DECLS 1044 #endif /* _KERNEL */ /* XXX really ALTQ? */ 1045 1046 #ifdef _KERNEL 1047 1048 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist) 1049 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list) 1050 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \ 1051 &(__ifp)->if_addrlist, ifa_list) 1052 #define IFADDR_FOREACH_SAFE(__ifa, __ifp, __nifa) \ 1053 TAILQ_FOREACH_SAFE(__ifa, \ 1054 &(__ifp)->if_addrlist, ifa_list, __nifa) 1055 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist) 1056 1057 #ifdef notyet 1058 #define IFADDR_ENTRY_INIT(__ifa) \ 1059 PSLIST_ENTRY_INIT((__ifa), ifa_pslist_entry) 1060 #define IFADDR_ENTRY_DESTROY(__ifa) \ 1061 PSLIST_ENTRY_DESTROY((__ifa), ifa_pslist_entry) 1062 #define IFADDR_READER_EMPTY(__ifp) \ 1063 (PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \ 1064 ifa_pslist_entry) == NULL) 1065 #define IFADDR_READER_FIRST(__ifp) \ 1066 PSLIST_READER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \ 1067 ifa_pslist_entry) 1068 #define IFADDR_READER_NEXT(__ifa) \ 1069 PSLIST_READER_NEXT((__ifa), struct ifaddr, ifa_pslist_entry) 1070 #define IFADDR_READER_FOREACH(__ifa, __ifp) \ 1071 PSLIST_READER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\ 1072 ifa_pslist_entry) 1073 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa) \ 1074 PSLIST_WRITER_INSERT_HEAD(&(__ifp)->if_addr_pslist, (__ifa), \ 1075 ifa_pslist_entry) 1076 #define IFADDR_WRITER_REMOVE(__ifa) \ 1077 PSLIST_WRITER_REMOVE((__ifa), ifa_pslist_entry) 1078 #define IFADDR_WRITER_FOREACH(__ifa, __ifp) \ 1079 PSLIST_WRITER_FOREACH((__ifa), &(__ifp)->if_addr_pslist, struct ifaddr,\ 1080 ifa_pslist_entry) 1081 #define IFADDR_WRITER_NEXT(__ifp) \ 1082 PSLIST_WRITER_NEXT((__ifp), struct ifaddr, ifa_pslist_entry) 1083 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new) \ 1084 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), ifa_pslist_entry) 1085 #define IFADDR_WRITER_EMPTY(__ifp) \ 1086 (PSLIST_WRITER_FIRST(&(__ifp)->if_addr_pslist, struct ifaddr, \ 1087 ifa_pslist_entry) == NULL) 1088 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new) \ 1089 do { \ 1090 if (IFADDR_WRITER_EMPTY((__ifp))) { \ 1091 IFADDR_WRITER_INSERT_HEAD((__ifp), (__new)); \ 1092 } else { \ 1093 struct ifaddr *__ifa; \ 1094 IFADDR_WRITER_FOREACH(__ifa, (__ifp)) { \ 1095 if (IFADDR_WRITER_NEXT(__ifa) == NULL) {\ 1096 IFADDR_WRITER_INSERT_AFTER(__ifa,\ 1097 (__new)); \ 1098 break; \ 1099 } \ 1100 } \ 1101 } \ 1102 } while (0) 1103 #else 1104 #define IFADDR_ENTRY_INIT(__ifa) \ 1105 do {} while (0) 1106 #define IFADDR_ENTRY_DESTROY(__ifa) \ 1107 do {} while (0) 1108 #define IFADDR_READER_EMPTY(__ifp) \ 1109 IFADDR_EMPTY(__ifp) 1110 #define IFADDR_READER_FIRST(__ifp) \ 1111 IFADDR_FIRST(__ifp) 1112 #define IFADDR_READER_NEXT(__ifa) \ 1113 IFADDR_NEXT(__ifa) 1114 #define IFADDR_READER_FOREACH(__ifa, __ifp) \ 1115 IFADDR_FOREACH(__ifa, __ifp) 1116 #define IFADDR_WRITER_INSERT_HEAD(__ifp, __ifa) \ 1117 do {} while (0) 1118 #define IFADDR_WRITER_REMOVE(__ifa) \ 1119 do {} while (0) 1120 #define IFADDR_WRITER_FOREACH(__ifa, __ifp) \ 1121 IFADDR_FOREACH(__ifa, __ifp) 1122 #define IFADDR_WRITER_NEXT(__ifp) \ 1123 IFADDR_NEXT(__ifa) 1124 #define IFADDR_WRITER_INSERT_AFTER(__ifp, __new) \ 1125 do {} while (0) 1126 #define IFADDR_WRITER_EMPTY(__ifp) \ 1127 IFADDR_EMPTY(__ifp) 1128 #define IFADDR_WRITER_INSERT_TAIL(__ifp, __new) \ 1129 do {} while (0) 1130 #endif /* notyet */ 1131 1132 #define IFNET_LOCK() mutex_enter(&ifnet_mtx) 1133 #define IFNET_UNLOCK() mutex_exit(&ifnet_mtx) 1134 #define IFNET_LOCKED() mutex_owned(&ifnet_mtx) 1135 1136 #define IFNET_READER_EMPTY() \ 1137 (PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL) 1138 #define IFNET_READER_FIRST() \ 1139 PSLIST_READER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) 1140 #define IFNET_READER_NEXT(__ifp) \ 1141 PSLIST_READER_NEXT((__ifp), struct ifnet, if_pslist_entry) 1142 #define IFNET_READER_FOREACH(__ifp) \ 1143 PSLIST_READER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \ 1144 if_pslist_entry) 1145 #define IFNET_WRITER_INSERT_HEAD(__ifp) \ 1146 PSLIST_WRITER_INSERT_HEAD(&ifnet_pslist, (__ifp), if_pslist_entry) 1147 #define IFNET_WRITER_REMOVE(__ifp) \ 1148 PSLIST_WRITER_REMOVE((__ifp), if_pslist_entry) 1149 #define IFNET_WRITER_FOREACH(__ifp) \ 1150 PSLIST_WRITER_FOREACH((__ifp), &ifnet_pslist, struct ifnet, \ 1151 if_pslist_entry) 1152 #define IFNET_WRITER_NEXT(__ifp) \ 1153 PSLIST_WRITER_NEXT((__ifp), struct ifnet, if_pslist_entry) 1154 #define IFNET_WRITER_INSERT_AFTER(__ifp, __new) \ 1155 PSLIST_WRITER_INSERT_AFTER((__ifp), (__new), if_pslist_entry) 1156 #define IFNET_WRITER_EMPTY() \ 1157 (PSLIST_WRITER_FIRST(&ifnet_pslist, struct ifnet, if_pslist_entry) == NULL) 1158 #define IFNET_WRITER_INSERT_TAIL(__new) \ 1159 do { \ 1160 if (IFNET_WRITER_EMPTY()) { \ 1161 IFNET_WRITER_INSERT_HEAD((__new)); \ 1162 } else { \ 1163 struct ifnet *__ifp; \ 1164 IFNET_WRITER_FOREACH(__ifp) { \ 1165 if (IFNET_WRITER_NEXT(__ifp) == NULL) { \ 1166 IFNET_WRITER_INSERT_AFTER(__ifp,\ 1167 (__new)); \ 1168 break; \ 1169 } \ 1170 } \ 1171 } \ 1172 } while (0) 1173 1174 extern struct pslist_head ifnet_pslist; 1175 extern struct psref_class *ifnet_psref_class; 1176 extern kmutex_t ifnet_mtx; 1177 1178 extern struct ifnet *lo0ifp; 1179 1180 /* 1181 * ifq sysctl support 1182 */ 1183 int sysctl_ifq(int *name, u_int namelen, void *oldp, 1184 size_t *oldlenp, void *newp, size_t newlen, 1185 struct ifqueue *ifq); 1186 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */ 1187 #define IFQCTL_LEN 1 1188 #define IFQCTL_MAXLEN 2 1189 #define IFQCTL_PEAK 3 1190 #define IFQCTL_DROPS 4 1191 #define IFQCTL_MAXID 5 1192 1193 #endif /* _KERNEL */ 1194 1195 #ifdef _NETBSD_SOURCE 1196 /* 1197 * sysctl for ifq (per-protocol packet input queue variant of ifqueue) 1198 */ 1199 #define CTL_IFQ_NAMES { \ 1200 { 0, 0 }, \ 1201 { "len", CTLTYPE_INT }, \ 1202 { "maxlen", CTLTYPE_INT }, \ 1203 { "peak", CTLTYPE_INT }, \ 1204 { "drops", CTLTYPE_INT }, \ 1205 } 1206 #endif /* _NETBSD_SOURCE */ 1207 #endif /* !_NET_IF_H_ */ 1208