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