1 /* $NetBSD: if.h,v 1.142 2009/01/11 02:45:54 christos 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 #include <sys/featuretest.h> 67 68 /* 69 * Length of interface external name, including terminating '\0'. 70 * Note: this is the same size as a generic device's external name. 71 */ 72 #define IF_NAMESIZE 16 73 74 #if defined(_NETBSD_SOURCE) 75 76 #include <sys/mutex.h> 77 #include <sys/condvar.h> 78 #include <sys/socket.h> 79 #include <sys/queue.h> 80 #include <net/dlt.h> 81 #include <net/pfil.h> 82 83 /* 84 * Always include ALTQ glue here -- we use the ALTQ interface queue 85 * structure even when ALTQ is not configured into the kernel so that 86 * the size of struct ifnet does not changed based on the option. The 87 * ALTQ queue structure is API-compatible with the legacy ifqueue. 88 */ 89 #include <altq/if_altq.h> 90 91 /* 92 * Structures defining a network interface, providing a packet 93 * transport mechanism (ala level 0 of the PUP protocols). 94 * 95 * Each interface accepts output datagrams of a specified maximum 96 * length, and provides higher level routines with input datagrams 97 * received from its medium. 98 * 99 * Output occurs when the routine if_output is called, with four parameters: 100 * (*ifp->if_output)(ifp, m, dst, rt) 101 * Here m is the mbuf chain to be sent and dst is the destination address. 102 * The output routine encapsulates the supplied datagram if necessary, 103 * and then transmits it on its medium. 104 * 105 * On input, each interface unwraps the data received by it, and either 106 * places it on the input queue of a internetwork datagram routine 107 * and posts the associated software interrupt, or passes the datagram to a raw 108 * packet input routine. 109 * 110 * Routines exist for locating interfaces by their addresses 111 * or for locating a interface on a certain network, as well as more general 112 * routing and gateway routines maintaining information used to locate 113 * interfaces. These routines live in the files if.c and route.c 114 */ 115 /* XXX fast fix for SNMP, going away soon */ 116 #include <sys/time.h> 117 118 #if defined(_KERNEL_OPT) 119 #include "opt_compat_netbsd.h" 120 #endif 121 122 struct mbuf; 123 struct proc; 124 struct rtentry; 125 struct socket; 126 struct ether_header; 127 struct ifaddr; 128 struct ifnet; 129 struct rt_addrinfo; 130 131 #define IFNAMSIZ IF_NAMESIZE 132 133 /* 134 * Structure describing a `cloning' interface. 135 */ 136 struct if_clone { 137 LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */ 138 const char *ifc_name; /* name of device, e.g. `gif' */ 139 size_t ifc_namelen; /* length of name */ 140 141 int (*ifc_create)(struct if_clone *, int); 142 int (*ifc_destroy)(struct ifnet *); 143 }; 144 145 #define IF_CLONE_INITIALIZER(name, create, destroy) \ 146 { { NULL, NULL }, name, sizeof(name) - 1, create, destroy } 147 148 /* 149 * Structure used to query names of interface cloners. 150 */ 151 struct if_clonereq { 152 int ifcr_total; /* total cloners (out) */ 153 int ifcr_count; /* room for this many in user buffer */ 154 char *ifcr_buffer; /* buffer for cloner names */ 155 }; 156 157 /* 158 * Structure defining statistics and other data kept regarding a network 159 * interface. 160 */ 161 struct if_data { 162 /* generic interface information */ 163 u_char ifi_type; /* ethernet, tokenring, etc. */ 164 u_char ifi_addrlen; /* media address length */ 165 u_char ifi_hdrlen; /* media header length */ 166 int ifi_link_state; /* current link state */ 167 u_quad_t ifi_mtu; /* maximum transmission unit */ 168 u_quad_t ifi_metric; /* routing metric (external only) */ 169 u_quad_t ifi_baudrate; /* linespeed */ 170 /* volatile statistics */ 171 u_quad_t ifi_ipackets; /* packets received on interface */ 172 u_quad_t ifi_ierrors; /* input errors on interface */ 173 u_quad_t ifi_opackets; /* packets sent on interface */ 174 u_quad_t ifi_oerrors; /* output errors on interface */ 175 u_quad_t ifi_collisions; /* collisions on csma interfaces */ 176 u_quad_t ifi_ibytes; /* total number of octets received */ 177 u_quad_t ifi_obytes; /* total number of octets sent */ 178 u_quad_t ifi_imcasts; /* packets received via multicast */ 179 u_quad_t ifi_omcasts; /* packets sent via multicast */ 180 u_quad_t ifi_iqdrops; /* dropped on input, this interface */ 181 u_quad_t ifi_noproto; /* destined for unsupported protocol */ 182 struct timespec ifi_lastchange;/* last operational state change */ 183 }; 184 185 /* 186 * Values for if_link_state. 187 */ 188 #define LINK_STATE_UNKNOWN 0 /* link invalid/unknown */ 189 #define LINK_STATE_DOWN 1 /* link is down */ 190 #define LINK_STATE_UP 2 /* link is up */ 191 192 /* 193 * Structure defining a queue for a network interface. 194 */ 195 struct ifqueue { 196 struct mbuf *ifq_head; 197 struct mbuf *ifq_tail; 198 int ifq_len; 199 int ifq_maxlen; 200 int ifq_drops; 201 }; 202 203 /* 204 * Structure defining a queue for a network interface. 205 * 206 * (Would like to call this struct ``if'', but C isn't PL/1.) 207 */ 208 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */ 209 210 struct ifnet { /* and the entries */ 211 void *if_softc; /* lower-level data for this if */ 212 TAILQ_ENTRY(ifnet) if_list; /* all struct ifnets are chained */ 213 TAILQ_HEAD(, ifaddr) if_addrlist; /* linked list of addresses per if */ 214 char if_xname[IFNAMSIZ]; /* external name (name + unit) */ 215 int if_pcount; /* number of promiscuous listeners */ 216 void * if_bpf; /* packet filter structure */ 217 u_short if_index; /* numeric abbreviation for this if */ 218 short if_timer; /* time 'til if_watchdog called */ 219 short if_flags; /* up/down, broadcast, etc. */ 220 short if__pad1; /* be nice to m68k ports */ 221 struct if_data if_data; /* statistics and other data about if */ 222 /* 223 * Procedure handles. If you add more of these, don't forget the 224 * corresponding NULL stub in if.c. 225 */ 226 int (*if_output) /* output routine (enqueue) */ 227 (struct ifnet *, struct mbuf *, const struct sockaddr *, 228 struct rtentry *); 229 void (*if_input) /* input routine (from h/w driver) */ 230 (struct ifnet *, struct mbuf *); 231 void (*if_start) /* initiate output routine */ 232 (struct ifnet *); 233 int (*if_ioctl) /* ioctl routine */ 234 (struct ifnet *, u_long, void *); 235 int (*if_init) /* init routine */ 236 (struct ifnet *); 237 void (*if_stop) /* stop routine */ 238 (struct ifnet *, int); 239 void (*if_watchdog) /* timer routine */ 240 (struct ifnet *); 241 void (*if_drain) /* routine to release resources */ 242 (struct ifnet *); 243 struct ifaltq if_snd; /* output queue (includes altq) */ 244 struct ifaddr *if_dl; /* identity of this interface. */ 245 const struct sockaddr_dl *if_sadl; /* pointer to sockaddr_dl 246 * of if_dl 247 */ 248 /* if_hwdl: h/w identity 249 * 250 * May be NULL. If not NULL, it is the address assigned 251 * to the interface by the manufacturer, so it very likely 252 * to be unique. It MUST NOT be deleted. It is highly 253 * suitable for deriving the EUI64 for the interface. 254 */ 255 struct ifaddr *if_hwdl; 256 const uint8_t *if_broadcastaddr;/* linklevel broadcast bytestring */ 257 void *if_bridge; /* bridge glue */ 258 int if_dlt; /* data link type (<net/dlt.h>) */ 259 struct pfil_head if_pfil; /* filtering point */ 260 uint64_t if_capabilities; /* interface capabilities */ 261 uint64_t if_capenable; /* capabilities enabled */ 262 union { 263 void * carp_s; /* carp structure (used by !carp ifs) */ 264 struct ifnet *carp_d;/* ptr to carpdev (used by carp ifs) */ 265 } if_carp_ptr; 266 #define if_carp if_carp_ptr.carp_s 267 #define if_carpdev if_carp_ptr.carp_d 268 /* 269 * These are pre-computed based on an interfaces enabled 270 * capabilities, for speed elsewhere. 271 */ 272 int if_csum_flags_tx; /* M_CSUM_* flags for Tx */ 273 int if_csum_flags_rx; /* M_CSUM_* flags for Rx */ 274 275 void *if_afdata[AF_MAX]; 276 struct mowner *if_mowner; /* who owns mbufs for this interface */ 277 278 void *if_agrprivate; /* used only when #if NAGR > 0 */ 279 280 /* 281 * pf specific data, used only when #if NPF > 0. 282 */ 283 void *if_pf_kif; /* pf interface abstraction */ 284 void *if_pf_groups; /* pf interface groups */ 285 }; 286 #define if_mtu if_data.ifi_mtu 287 #define if_type if_data.ifi_type 288 #define if_addrlen if_data.ifi_addrlen 289 #define if_hdrlen if_data.ifi_hdrlen 290 #define if_metric if_data.ifi_metric 291 #define if_link_state if_data.ifi_link_state 292 #define if_baudrate if_data.ifi_baudrate 293 #define if_ipackets if_data.ifi_ipackets 294 #define if_ierrors if_data.ifi_ierrors 295 #define if_opackets if_data.ifi_opackets 296 #define if_oerrors if_data.ifi_oerrors 297 #define if_collisions if_data.ifi_collisions 298 #define if_ibytes if_data.ifi_ibytes 299 #define if_obytes if_data.ifi_obytes 300 #define if_imcasts if_data.ifi_imcasts 301 #define if_omcasts if_data.ifi_omcasts 302 #define if_iqdrops if_data.ifi_iqdrops 303 #define if_noproto if_data.ifi_noproto 304 #define if_lastchange if_data.ifi_lastchange 305 306 #define IFF_UP 0x0001 /* interface is up */ 307 #define IFF_BROADCAST 0x0002 /* broadcast address valid */ 308 #define IFF_DEBUG 0x0004 /* turn on debugging */ 309 #define IFF_LOOPBACK 0x0008 /* is a loopback net */ 310 #define IFF_POINTOPOINT 0x0010 /* interface is point-to-point link */ 311 #define IFF_NOTRAILERS 0x0020 /* avoid use of trailers */ 312 #define IFF_RUNNING 0x0040 /* resources allocated */ 313 #define IFF_NOARP 0x0080 /* no address resolution protocol */ 314 #define IFF_PROMISC 0x0100 /* receive all packets */ 315 #define IFF_ALLMULTI 0x0200 /* receive all multicast packets */ 316 #define IFF_OACTIVE 0x0400 /* transmission in progress */ 317 #define IFF_SIMPLEX 0x0800 /* can't hear own transmissions */ 318 #define IFF_LINK0 0x1000 /* per link layer defined bit */ 319 #define IFF_LINK1 0x2000 /* per link layer defined bit */ 320 #define IFF_LINK2 0x4000 /* per link layer defined bit */ 321 #define IFF_MULTICAST 0x8000 /* supports multicast */ 322 323 #define IFFBITS \ 324 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS" \ 325 "\7RUNNING\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX" \ 326 "\15LINK0\16LINK1\17LINK2\20MULTICAST" 327 328 /* flags set internally only: */ 329 #define IFF_CANTCHANGE \ 330 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|\ 331 IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI|IFF_PROMISC) 332 333 /* 334 * Some convenience macros used for setting ifi_baudrate. 335 * XXX 1000 vs. 1024? --thorpej@NetBSD.org 336 */ 337 #define IF_Kbps(x) ((x) * 1000) /* kilobits/sec. */ 338 #define IF_Mbps(x) (IF_Kbps((x) * 1000)) /* megabits/sec. */ 339 #define IF_Gbps(x) (IF_Mbps((x) * 1000)) /* gigabits/sec. */ 340 341 /* Capabilities that interfaces can advertise. */ 342 #define IFCAP_TSOv4 0x00080 /* can do TCPv4 segmentation offload */ 343 #define IFCAP_CSUM_IPv4_Rx 0x00100 /* can do IPv4 header checksums (Rx) */ 344 #define IFCAP_CSUM_IPv4_Tx 0x00200 /* can do IPv4 header checksums (Tx) */ 345 #define IFCAP_CSUM_TCPv4_Rx 0x00400 /* can do IPv4/TCP checksums (Rx) */ 346 #define IFCAP_CSUM_TCPv4_Tx 0x00800 /* can do IPv4/TCP checksums (Tx) */ 347 #define IFCAP_CSUM_UDPv4_Rx 0x01000 /* can do IPv4/UDP checksums (Rx) */ 348 #define IFCAP_CSUM_UDPv4_Tx 0x02000 /* can do IPv4/UDP checksums (Tx) */ 349 #define IFCAP_CSUM_TCPv6_Rx 0x04000 /* can do IPv6/TCP checksums (Rx) */ 350 #define IFCAP_CSUM_TCPv6_Tx 0x08000 /* can do IPv6/TCP checksums (Tx) */ 351 #define IFCAP_CSUM_UDPv6_Rx 0x10000 /* can do IPv6/UDP checksums (Rx) */ 352 #define IFCAP_CSUM_UDPv6_Tx 0x20000 /* can do IPv6/UDP checksums (Tx) */ 353 #define IFCAP_TSOv6 0x40000 /* can do TCPv6 segmentation offload */ 354 355 #define IFCAPBITS \ 356 "\020" \ 357 "\10TSO4" \ 358 "\11IP4CSUM_Rx" \ 359 "\12IP4CSUM_Tx" \ 360 "\13TCP4CSUM_Rx" \ 361 "\14TCP4CSUM_Tx" \ 362 "\15UDP4CSUM_Rx" \ 363 "\16UDP4CSUM_Tx" \ 364 "\17TCP6CSUM_Rx" \ 365 "\20TCP6CSUM_Tx" \ 366 "\21UDP6CSUM_Rx" \ 367 "\22UDP6CSUM_Tx" \ 368 "\23TSO6" 369 370 /* 371 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 372 * input routines have queues of messages stored on ifqueue structures 373 * (defined above). Entries are added to and deleted from these structures 374 * by these macros, which should be called with ipl raised to splnet(). 375 */ 376 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 377 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 378 #define IF_ENQUEUE(ifq, m) do { \ 379 (m)->m_nextpkt = 0; \ 380 if ((ifq)->ifq_tail == 0) \ 381 (ifq)->ifq_head = m; \ 382 else \ 383 (ifq)->ifq_tail->m_nextpkt = m; \ 384 (ifq)->ifq_tail = m; \ 385 (ifq)->ifq_len++; \ 386 } while (/*CONSTCOND*/0) 387 #define IF_PREPEND(ifq, m) do { \ 388 (m)->m_nextpkt = (ifq)->ifq_head; \ 389 if ((ifq)->ifq_tail == 0) \ 390 (ifq)->ifq_tail = (m); \ 391 (ifq)->ifq_head = (m); \ 392 (ifq)->ifq_len++; \ 393 } while (/*CONSTCOND*/0) 394 #define IF_DEQUEUE(ifq, m) do { \ 395 (m) = (ifq)->ifq_head; \ 396 if (m) { \ 397 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \ 398 (ifq)->ifq_tail = 0; \ 399 (m)->m_nextpkt = 0; \ 400 (ifq)->ifq_len--; \ 401 } \ 402 } while (/*CONSTCOND*/0) 403 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head) 404 #define IF_PURGE(ifq) \ 405 do { \ 406 struct mbuf *__m0; \ 407 \ 408 for (;;) { \ 409 IF_DEQUEUE((ifq), __m0); \ 410 if (__m0 == NULL) \ 411 break; \ 412 else \ 413 m_freem(__m0); \ 414 } \ 415 } while (/*CONSTCOND*/ 0) 416 #define IF_IS_EMPTY(ifq) ((ifq)->ifq_len == 0) 417 418 #ifndef IFQ_MAXLEN 419 #define IFQ_MAXLEN 256 420 #endif 421 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 422 423 /* 424 * Structure defining statistics and other data kept regarding an address 425 * on a network interface. 426 */ 427 struct ifaddr_data { 428 int64_t ifad_inbytes; 429 int64_t ifad_outbytes; 430 }; 431 432 /* 433 * The ifaddr structure contains information about one address 434 * of an interface. They are maintained by the different address families, 435 * are allocated and attached when an address is set, and are linked 436 * together so all addresses for an interface can be located. 437 */ 438 struct ifaddr { 439 struct sockaddr *ifa_addr; /* address of interface */ 440 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 441 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 442 struct sockaddr *ifa_netmask; /* used to determine subnet */ 443 struct ifnet *ifa_ifp; /* back-pointer to interface */ 444 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */ 445 struct ifaddr_data ifa_data; /* statistics on the address */ 446 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */ 447 (int, struct rtentry *, const struct rt_addrinfo *); 448 u_int ifa_flags; /* mostly rt_flags for cloning */ 449 int ifa_refcnt; /* count of references */ 450 int ifa_metric; /* cost of going out this interface */ 451 struct ifaddr *(*ifa_getifa)(struct ifaddr *, 452 const struct sockaddr *); 453 uint32_t *ifa_seqno; 454 int16_t ifa_preference; /* preference level for this address */ 455 }; 456 #define IFA_ROUTE RTF_UP /* (0x01) route installed */ 457 458 /* 459 * Message format for use in obtaining information about interfaces 460 * from sysctl and the routing socket. 461 */ 462 struct if_msghdr { 463 u_short ifm_msglen; /* to skip over non-understood messages */ 464 u_char ifm_version; /* future binary compatibility */ 465 u_char ifm_type; /* message type */ 466 int ifm_addrs; /* like rtm_addrs */ 467 int ifm_flags; /* value of if_flags */ 468 u_short ifm_index; /* index for associated ifp */ 469 struct if_data ifm_data;/* statistics and other data about if */ 470 }; 471 472 /* 473 * Message format for use in obtaining information about interface addresses 474 * from sysctl and the routing socket. 475 */ 476 struct ifa_msghdr { 477 u_short ifam_msglen; /* to skip over non-understood messages */ 478 u_char ifam_version; /* future binary compatibility */ 479 u_char ifam_type; /* message type */ 480 int ifam_addrs; /* like rtm_addrs */ 481 int ifam_flags; /* value of ifa_flags */ 482 u_short ifam_index; /* index for associated ifp */ 483 int ifam_metric; /* value of ifa_metric */ 484 }; 485 486 /* 487 * Message format announcing the arrival or departure of a network interface. 488 */ 489 struct if_announcemsghdr { 490 u_short ifan_msglen; /* to skip over non-understood messages */ 491 u_char ifan_version; /* future binary compatibility */ 492 u_char ifan_type; /* message type */ 493 u_short ifan_index; /* index for associated ifp */ 494 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 495 u_short ifan_what; /* what type of announcement */ 496 }; 497 498 #define IFAN_ARRIVAL 0 /* interface arrival */ 499 #define IFAN_DEPARTURE 1 /* interface departure */ 500 501 /* 502 * Interface request structure used for socket 503 * ioctl's. All interface ioctl's must have parameter 504 * definitions which begin with ifr_name. The 505 * remainder may be interface specific. 506 */ 507 struct ifreq { 508 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 509 union { 510 struct sockaddr ifru_addr; 511 struct sockaddr ifru_dstaddr; 512 struct sockaddr ifru_broadaddr; 513 struct sockaddr_storage ifru_space; 514 short ifru_flags; 515 int ifru_metric; 516 int ifru_mtu; 517 int ifru_dlt; 518 u_int ifru_value; 519 void * ifru_data; 520 struct { 521 uint32_t b_buflen; 522 void *b_buf; 523 } ifru_b; 524 } ifr_ifru; 525 #define ifr_addr ifr_ifru.ifru_addr /* address */ 526 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 527 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 528 #define ifr_space ifr_ifru.ifru_space /* sockaddr_storage */ 529 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 530 #define ifr_metric ifr_ifru.ifru_metric /* metric */ 531 #define ifr_mtu ifr_ifru.ifru_mtu /* mtu */ 532 #define ifr_dlt ifr_ifru.ifru_dlt /* data link type (DLT_*) */ 533 #define ifr_value ifr_ifru.ifru_value /* generic value */ 534 #define ifr_media ifr_ifru.ifru_metric /* media options (overload) */ 535 #define ifr_data ifr_ifru.ifru_data /* for use by interface 536 * XXX deprecated 537 */ 538 #define ifr_buf ifr_ifru.ifru_b.b_buf /* new interface ioctls */ 539 #define ifr_buflen ifr_ifru.ifru_b.b_buflen 540 }; 541 542 #ifdef _KERNEL 543 #define ifreq_setdstaddr ifreq_setaddr 544 #define ifreq_setbroadaddr ifreq_setaddr 545 #define ifreq_getdstaddr ifreq_getaddr 546 #define ifreq_getbroadaddr ifreq_getaddr 547 548 static inline const struct sockaddr * 549 ifreq_getaddr(u_long cmd, const struct ifreq *ifr) 550 { 551 return &ifr->ifr_addr; 552 } 553 #endif /* _KERNEL */ 554 555 struct ifcapreq { 556 char ifcr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 557 uint64_t ifcr_capabilities; /* supported capabiliites */ 558 uint64_t ifcr_capenable; /* capabilities enabled */ 559 }; 560 561 struct ifaliasreq { 562 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 563 struct sockaddr ifra_addr; 564 struct sockaddr ifra_dstaddr; 565 #define ifra_broadaddr ifra_dstaddr 566 struct sockaddr ifra_mask; 567 }; 568 569 struct ifdatareq { 570 char ifdr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 571 struct if_data ifdr_data; 572 }; 573 574 struct ifmediareq { 575 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 576 int ifm_current; /* current media options */ 577 int ifm_mask; /* don't care mask */ 578 int ifm_status; /* media status */ 579 int ifm_active; /* active options */ 580 int ifm_count; /* # entries in ifm_ulist 581 array */ 582 int *ifm_ulist; /* media words */ 583 }; 584 585 586 struct ifdrv { 587 char ifd_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 588 unsigned long ifd_cmd; 589 size_t ifd_len; 590 void *ifd_data; 591 }; 592 593 /* 594 * Structure used in SIOCGIFCONF request. 595 * Used to retrieve interface configuration 596 * for machine (useful for programs which 597 * must know all networks accessible). 598 */ 599 struct ifconf { 600 int ifc_len; /* size of associated buffer */ 601 union { 602 void * ifcu_buf; 603 struct ifreq *ifcu_req; 604 } ifc_ifcu; 605 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 606 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 607 }; 608 609 /* 610 * Structure for SIOC[AGD]LIFADDR 611 */ 612 struct if_laddrreq { 613 char iflr_name[IFNAMSIZ]; 614 unsigned int flags; 615 #define IFLR_PREFIX 0x8000 /* in: prefix given out: kernel fills id */ 616 #define IFLR_ACTIVE 0x4000 /* in/out: link-layer address activation */ 617 #define IFLR_FACTORY 0x2000 /* in/out: factory link-layer address */ 618 unsigned int prefixlen; /* in/out */ 619 struct sockaddr_storage addr; /* in/out */ 620 struct sockaddr_storage dstaddr; /* out */ 621 }; 622 623 /* 624 * Structure for SIOC[SG]IFADDRPREF 625 */ 626 struct if_addrprefreq { 627 char ifap_name[IFNAMSIZ]; 628 int16_t ifap_preference; /* in/out */ 629 struct sockaddr_storage ifap_addr; /* in/out */ 630 }; 631 632 #include <net/if_arp.h> 633 634 #endif /* _NETBSD_SOURCE */ 635 636 #ifdef _KERNEL 637 #ifdef IFAREF_DEBUG 638 #define IFAREF(ifa) \ 639 do { \ 640 printf("IFAREF: %s:%d %p -> %d\n", __FILE__, __LINE__, \ 641 (ifa), ++(ifa)->ifa_refcnt); \ 642 } while (/*CONSTCOND*/ 0) 643 644 #define IFAFREE(ifa) \ 645 do { \ 646 if ((ifa)->ifa_refcnt <= 0) \ 647 panic("%s:%d: %p ifa_refcnt <= 0", __FILE__, \ 648 __LINE__, (ifa)); \ 649 printf("IFAFREE: %s:%d %p -> %d\n", __FILE__, __LINE__, \ 650 (ifa), --(ifa)->ifa_refcnt); \ 651 if ((ifa)->ifa_refcnt == 0) \ 652 ifafree(ifa); \ 653 } while (/*CONSTCOND*/ 0) 654 #else 655 #define IFAREF(ifa) (ifa)->ifa_refcnt++ 656 657 #ifdef DIAGNOSTIC 658 #define IFAFREE(ifa) \ 659 do { \ 660 if ((ifa)->ifa_refcnt <= 0) \ 661 panic("%s:%d: %p ifa_refcnt <= 0", __FILE__, \ 662 __LINE__, (ifa)); \ 663 if (--(ifa)->ifa_refcnt == 0) \ 664 ifafree(ifa); \ 665 } while (/*CONSTCOND*/ 0) 666 #else 667 #define IFAFREE(ifa) \ 668 do { \ 669 if (--(ifa)->ifa_refcnt == 0) \ 670 ifafree(ifa); \ 671 } while (/*CONSTCOND*/ 0) 672 #endif /* DIAGNOSTIC */ 673 #endif /* IFAREF_DEBUG */ 674 675 #ifdef ALTQ 676 #define ALTQ_DECL(x) x 677 #define ALTQ_COMMA , 678 679 #define IFQ_ENQUEUE(ifq, m, pattr, err) \ 680 do { \ 681 if (ALTQ_IS_ENABLED((ifq))) \ 682 ALTQ_ENQUEUE((ifq), (m), (pattr), (err)); \ 683 else { \ 684 if (IF_QFULL((ifq))) { \ 685 m_freem((m)); \ 686 (err) = ENOBUFS; \ 687 } else { \ 688 IF_ENQUEUE((ifq), (m)); \ 689 (err) = 0; \ 690 } \ 691 } \ 692 if ((err)) \ 693 (ifq)->ifq_drops++; \ 694 } while (/*CONSTCOND*/ 0) 695 696 #define IFQ_DEQUEUE(ifq, m) \ 697 do { \ 698 if (TBR_IS_ENABLED((ifq))) \ 699 (m) = tbr_dequeue((ifq), ALTDQ_REMOVE); \ 700 else if (ALTQ_IS_ENABLED((ifq))) \ 701 ALTQ_DEQUEUE((ifq), (m)); \ 702 else \ 703 IF_DEQUEUE((ifq), (m)); \ 704 } while (/*CONSTCOND*/ 0) 705 706 #define IFQ_POLL(ifq, m) \ 707 do { \ 708 if (TBR_IS_ENABLED((ifq))) \ 709 (m) = tbr_dequeue((ifq), ALTDQ_POLL); \ 710 else if (ALTQ_IS_ENABLED((ifq))) \ 711 ALTQ_POLL((ifq), (m)); \ 712 else \ 713 IF_POLL((ifq), (m)); \ 714 } while (/*CONSTCOND*/ 0) 715 716 #define IFQ_PURGE(ifq) \ 717 do { \ 718 if (ALTQ_IS_ENABLED((ifq))) \ 719 ALTQ_PURGE((ifq)); \ 720 else \ 721 IF_PURGE((ifq)); \ 722 } while (/*CONSTCOND*/ 0) 723 724 #define IFQ_SET_READY(ifq) \ 725 do { \ 726 (ifq)->altq_flags |= ALTQF_READY; \ 727 } while (/*CONSTCOND*/ 0) 728 729 #define IFQ_CLASSIFY(ifq, m, af, pattr) \ 730 do { \ 731 if (ALTQ_IS_ENABLED((ifq))) { \ 732 if (ALTQ_NEEDS_CLASSIFY((ifq))) \ 733 (pattr)->pattr_class = (*(ifq)->altq_classify) \ 734 ((ifq)->altq_clfier, (m), (af)); \ 735 (pattr)->pattr_af = (af); \ 736 (pattr)->pattr_hdr = mtod((m), void *); \ 737 } \ 738 } while (/*CONSTCOND*/ 0) 739 #else /* ! ALTQ */ 740 #define ALTQ_DECL(x) /* nothing */ 741 #define ALTQ_COMMA 742 743 #define IFQ_ENQUEUE(ifq, m, pattr, err) \ 744 do { \ 745 if (IF_QFULL((ifq))) { \ 746 m_freem((m)); \ 747 (err) = ENOBUFS; \ 748 } else { \ 749 IF_ENQUEUE((ifq), (m)); \ 750 (err) = 0; \ 751 } \ 752 if ((err)) \ 753 (ifq)->ifq_drops++; \ 754 } while (/*CONSTCOND*/ 0) 755 756 #define IFQ_DEQUEUE(ifq, m) IF_DEQUEUE((ifq), (m)) 757 758 #define IFQ_POLL(ifq, m) IF_POLL((ifq), (m)) 759 760 #define IFQ_PURGE(ifq) IF_PURGE((ifq)) 761 762 #define IFQ_SET_READY(ifq) /* nothing */ 763 764 #define IFQ_CLASSIFY(ifq, m, af, pattr) /* nothing */ 765 766 #endif /* ALTQ */ 767 768 #define IFQ_IS_EMPTY(ifq) IF_IS_EMPTY((ifq)) 769 #define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++) 770 #define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len) 771 #define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++) 772 #define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len)) 773 774 #include <sys/mallocvar.h> 775 MALLOC_DECLARE(M_IFADDR); 776 MALLOC_DECLARE(M_IFMADDR); 777 778 #define IFNET_FIRST() TAILQ_FIRST(&ifnet) 779 #define IFNET_NEXT(__ifp) TAILQ_NEXT((__ifp), if_list) 780 #define IFNET_FOREACH(__ifp) TAILQ_FOREACH(__ifp, &ifnet, if_list) 781 #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist) 782 #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list) 783 #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \ 784 &(__ifp)->if_addrlist, ifa_list) 785 #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist) 786 787 extern struct ifnet_head ifnet; 788 extern struct ifnet **ifindex2ifnet; 789 extern struct ifnet *lo0ifp; 790 extern size_t if_indexlim; 791 792 void ether_input(struct ifnet *, struct mbuf *); 793 794 int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *); 795 796 struct ifnet *if_alloc(u_char); 797 void if_initname(struct ifnet *, const char *, int); 798 struct ifaddr *if_dl_create(const struct ifnet *, const struct sockaddr_dl **); 799 void if_activate_sadl(struct ifnet *, struct ifaddr *, 800 const struct sockaddr_dl *); 801 void if_set_sadl(struct ifnet *, const void *, u_char, bool); 802 void if_alloc_sadl(struct ifnet *); 803 void if_free_sadl(struct ifnet *); 804 void if_attach(struct ifnet *); 805 void if_attachdomain(void); 806 void if_attachdomain1(struct ifnet *); 807 void if_deactivate(struct ifnet *); 808 void if_purgeaddrs(struct ifnet *, int, void (*)(struct ifaddr *)); 809 void if_detach(struct ifnet *); 810 void if_down(struct ifnet *); 811 void if_link_state_change(struct ifnet *, int); 812 void if_slowtimo(void *); 813 void if_up(struct ifnet *); 814 int ifconf(u_long, void *); 815 void ifinit(void); 816 void ifinit1(void); 817 int ifioctl(struct socket *, u_long, void *, struct lwp *); 818 int ifioctl_common(struct ifnet *, u_long, void *); 819 int ifpromisc(struct ifnet *, int); 820 struct ifnet *ifunit(const char *); 821 822 void ifa_insert(struct ifnet *, struct ifaddr *); 823 void ifa_remove(struct ifnet *, struct ifaddr *); 824 825 struct ifaddr *ifa_ifwithaddr(const struct sockaddr *); 826 struct ifaddr *ifa_ifwithaf(int); 827 struct ifaddr *ifa_ifwithdstaddr(const struct sockaddr *); 828 struct ifaddr *ifa_ifwithnet(const struct sockaddr *); 829 struct ifaddr *ifa_ifwithladdr(const struct sockaddr *); 830 struct ifaddr *ifa_ifwithroute(int, const struct sockaddr *, 831 const struct sockaddr *); 832 struct ifaddr *ifaof_ifpforaddr(const struct sockaddr *, struct ifnet *); 833 void ifafree(struct ifaddr *); 834 void link_rtrequest(int, struct rtentry *, const struct rt_addrinfo *); 835 836 void if_clone_attach(struct if_clone *); 837 void if_clone_detach(struct if_clone *); 838 839 int if_clone_create(const char *); 840 int if_clone_destroy(const char *); 841 842 int ifq_enqueue(struct ifnet *, struct mbuf * ALTQ_COMMA 843 ALTQ_DECL(struct altq_pktattr *)); 844 int ifq_enqueue2(struct ifnet *, struct ifqueue *, struct mbuf * ALTQ_COMMA 845 ALTQ_DECL(struct altq_pktattr *)); 846 847 int loioctl(struct ifnet *, u_long, void *); 848 void loopattach(int); 849 int looutput(struct ifnet *, 850 struct mbuf *, const struct sockaddr *, struct rtentry *); 851 void lortrequest(int, struct rtentry *, const struct rt_addrinfo *); 852 853 /* 854 * These are exported because they're an easy way to tell if 855 * an interface is going away without having to burn a flag. 856 */ 857 int if_nulloutput(struct ifnet *, struct mbuf *, 858 const struct sockaddr *, struct rtentry *); 859 void if_nullinput(struct ifnet *, struct mbuf *); 860 void if_nullstart(struct ifnet *); 861 int if_nullioctl(struct ifnet *, u_long, void *); 862 int if_nullinit(struct ifnet *); 863 void if_nullstop(struct ifnet *, int); 864 void if_nullwatchdog(struct ifnet *); 865 void if_nulldrain(struct ifnet *); 866 #else 867 struct if_nameindex { 868 unsigned int if_index; /* 1, 2, ... */ 869 char *if_name; /* null terminated name: "le0", ... */ 870 }; 871 872 #include <sys/cdefs.h> 873 __BEGIN_DECLS 874 unsigned int if_nametoindex(const char *); 875 char * if_indextoname(unsigned int, char *); 876 struct if_nameindex * if_nameindex(void); 877 void if_freenameindex(struct if_nameindex *); 878 __END_DECLS 879 #endif /* _KERNEL */ /* XXX really ALTQ? */ 880 881 #ifdef _KERNEL 882 /* 883 * ifq sysctl support 884 */ 885 int sysctl_ifq(int *name, u_int namelen, void *oldp, 886 size_t *oldlenp, void *newp, size_t newlen, 887 struct ifqueue *ifq); 888 /* symbolic names for terminal (per-protocol) CTL_IFQ_ nodes */ 889 #define IFQCTL_LEN 1 890 #define IFQCTL_MAXLEN 2 891 #define IFQCTL_PEAK 3 892 #define IFQCTL_DROPS 4 893 #define IFQCTL_MAXID 5 894 895 #endif /* _KERNEL */ 896 897 #ifdef _NETBSD_SOURCE 898 /* 899 * sysctl for ifq (per-protocol packet input queue variant of ifqueue) 900 */ 901 #define CTL_IFQ_NAMES { \ 902 { 0, 0 }, \ 903 { "len", CTLTYPE_INT }, \ 904 { "maxlen", CTLTYPE_INT }, \ 905 { "peak", CTLTYPE_INT }, \ 906 { "drops", CTLTYPE_INT }, \ 907 } 908 #endif /* _NETBSD_SOURCE */ 909 #endif /* !_NET_IF_H_ */ 910