1 /* $OpenBSD: if_var.h,v 1.103 2019/11/08 07:16:29 dlg Exp $ */ 2 /* $NetBSD: if.h,v 1.23 1996/05/07 02:40:27 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 2012-2013 Henning Brauer <henning@openbsd.org> 6 * Copyright (c) 1982, 1986, 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if.h 8.1 (Berkeley) 6/10/93 34 */ 35 36 #ifndef _NET_IF_VAR_H_ 37 #define _NET_IF_VAR_H_ 38 39 #ifdef _KERNEL 40 41 #include <sys/queue.h> 42 #include <sys/mbuf.h> 43 #include <sys/srp.h> 44 #include <sys/refcnt.h> 45 #include <sys/task.h> 46 #include <sys/time.h> 47 #include <sys/timeout.h> 48 49 #include <net/ifq.h> 50 51 /* 52 * Structures defining a network interface, providing a packet 53 * transport mechanism (ala level 0 of the PUP protocols). 54 * 55 * Each interface accepts output datagrams of a specified maximum 56 * length, and provides higher level routines with input datagrams 57 * received from its medium. 58 * 59 * Output occurs when the routine if_output is called, with four parameters: 60 * (*ifp->if_output)(ifp, m, dst, rt) 61 * Here m is the mbuf chain to be sent and dst is the destination address. 62 * The output routine encapsulates the supplied datagram if necessary, 63 * and then transmits it on its medium. 64 * 65 * On input, each interface unwraps the data received by it, and either 66 * places it on the input queue of an internetwork datagram routine 67 * and posts the associated software interrupt, or passes the datagram to a raw 68 * packet input routine. 69 * 70 * Routines exist for locating interfaces by their addresses 71 * or for locating an interface on a certain network, as well as more general 72 * routing and gateway routines maintaining information used to locate 73 * interfaces. These routines live in the files if.c and route.c 74 */ 75 76 struct rtentry; 77 struct ifnet; 78 struct task; 79 struct cpumem; 80 81 /* 82 * Structure describing a `cloning' interface. 83 */ 84 struct if_clone { 85 LIST_ENTRY(if_clone) ifc_list; /* on list of cloners */ 86 const char *ifc_name; /* name of device, e.g. `gif' */ 87 size_t ifc_namelen; /* length of name */ 88 89 int (*ifc_create)(struct if_clone *, int); 90 int (*ifc_destroy)(struct ifnet *); 91 }; 92 93 #define IF_CLONE_INITIALIZER(name, create, destroy) \ 94 { \ 95 .ifc_list = { NULL, NULL }, \ 96 .ifc_name = name, \ 97 .ifc_namelen = sizeof(name) - 1, \ 98 .ifc_create = create, \ 99 .ifc_destroy = destroy, \ 100 } 101 102 /* 103 * Locks used to protect struct members in this file: 104 * I immutable after creation 105 * d protection left do the driver 106 * c only used in ioctl or routing socket contexts (kernel lock) 107 * k kernel lock 108 * N net lock 109 * 110 * For SRP related structures that allow lock-free reads, the write lock 111 * is indicated below. 112 */ 113 /* 114 * Structure defining a queue for a network interface. 115 * 116 * (Would like to call this struct ``if'', but C isn't PL/1.) 117 */ 118 TAILQ_HEAD(ifnet_head, ifnet); /* the actual queue head */ 119 120 struct ifnet { /* and the entries */ 121 void *if_softc; /* [I] lower-level data for this if */ 122 struct refcnt if_refcnt; 123 TAILQ_ENTRY(ifnet) if_list; /* [k] all struct ifnets are chained */ 124 TAILQ_HEAD(, ifaddr) if_addrlist; /* [N] list of addresses per if */ 125 TAILQ_HEAD(, ifmaddr) if_maddrlist; /* [N] list of multicast records */ 126 TAILQ_HEAD(, ifg_list) if_groups; /* [N] list of groups per if */ 127 struct task_list if_addrhooks; /* [I] address change callbacks */ 128 struct task_list if_linkstatehooks; /* [I] link change callbacks*/ 129 struct task_list if_detachhooks; /* [I] detach callbacks */ 130 /* [I] check or clean routes (+ or -)'d */ 131 void (*if_rtrequest)(struct ifnet *, int, struct rtentry *); 132 char if_xname[IFNAMSIZ]; /* [I] external name (name + unit) */ 133 int if_pcount; /* [k] # of promiscuous listeners */ 134 unsigned int if_bridgeidx; /* [k] used by bridge ports */ 135 caddr_t if_bpf; /* packet filter structure */ 136 caddr_t if_switchport; /* used by switch ports */ 137 caddr_t if_mcast; /* used by multicast code */ 138 caddr_t if_mcast6; /* used by IPv6 multicast code */ 139 caddr_t if_pf_kif; /* pf interface abstraction */ 140 union { 141 struct srpl carp_s; /* carp if list (used by !carp ifs) */ 142 struct ifnet *carp_d; /* ptr to carpdev (used by carp ifs) */ 143 } if_carp_ptr; 144 #define if_carp if_carp_ptr.carp_s 145 #define if_carpdev if_carp_ptr.carp_d 146 unsigned int if_index; /* [I] unique index for this if */ 147 short if_timer; /* time 'til if_watchdog called */ 148 unsigned short if_flags; /* [N] up/down, broadcast, etc. */ 149 int if_xflags; /* [N] extra softnet flags */ 150 struct if_data if_data; /* stats and other data about if */ 151 struct cpumem *if_counters; /* per cpu stats */ 152 uint32_t if_hardmtu; /* [d] maximum MTU device supports */ 153 char if_description[IFDESCRSIZE]; /* [c] interface description */ 154 u_short if_rtlabelid; /* [c] next route label */ 155 uint8_t if_priority; /* [c] route priority offset */ 156 uint8_t if_llprio; /* [N] link layer priority */ 157 struct timeout if_slowtimo; /* [I] watchdog timeout */ 158 struct task if_watchdogtask; /* [I] watchdog task */ 159 struct task if_linkstatetask; /* [I] task to do route updates */ 160 161 /* procedure handles */ 162 SRPL_HEAD(, ifih) if_inputs; /* [k] input routines (dequeue) */ 163 int (*if_output)(struct ifnet *, struct mbuf *, struct sockaddr *, 164 struct rtentry *); /* output routine (enqueue) */ 165 /* link level output function */ 166 int (*if_ll_output)(struct ifnet *, struct mbuf *, 167 struct sockaddr *, struct rtentry *); 168 int (*if_enqueue)(struct ifnet *, struct mbuf *); 169 void (*if_start)(struct ifnet *); /* initiate output */ 170 int (*if_ioctl)(struct ifnet *, u_long, caddr_t); /* ioctl hook */ 171 void (*if_watchdog)(struct ifnet *); /* timer routine */ 172 int (*if_wol)(struct ifnet *, int); /* WoL routine **/ 173 174 /* queues */ 175 struct ifqueue if_snd; /* transmit queue */ 176 struct ifqueue **if_ifqs; /* [I] pointer to an array of sndqs */ 177 void (*if_qstart)(struct ifqueue *); 178 unsigned int if_nifqs; /* [I] number of output queues */ 179 unsigned int if_txmit; /* [c] txmitigation amount */ 180 181 struct ifiqueue if_rcv; /* rx/input queue */ 182 struct ifiqueue **if_iqs; /* [I] pointer to the array of iqs */ 183 unsigned int if_niqs; /* [I] number of input queues */ 184 185 struct sockaddr_dl *if_sadl; /* [N] pointer to our sockaddr_dl */ 186 187 void *if_afdata[AF_MAX]; 188 }; 189 #define if_mtu if_data.ifi_mtu 190 #define if_type if_data.ifi_type 191 #define if_addrlen if_data.ifi_addrlen 192 #define if_hdrlen if_data.ifi_hdrlen 193 #define if_metric if_data.ifi_metric 194 #define if_link_state if_data.ifi_link_state 195 #define if_baudrate if_data.ifi_baudrate 196 #define if_ipackets if_data.ifi_ipackets 197 #define if_ierrors if_data.ifi_ierrors 198 #define if_opackets if_data.ifi_opackets 199 #define if_oerrors if_data.ifi_oerrors 200 #define if_collisions if_data.ifi_collisions 201 #define if_ibytes if_data.ifi_ibytes 202 #define if_obytes if_data.ifi_obytes 203 #define if_imcasts if_data.ifi_imcasts 204 #define if_omcasts if_data.ifi_omcasts 205 #define if_iqdrops if_data.ifi_iqdrops 206 #define if_oqdrops if_data.ifi_oqdrops 207 #define if_noproto if_data.ifi_noproto 208 #define if_lastchange if_data.ifi_lastchange /* [c] last op. state change */ 209 #define if_capabilities if_data.ifi_capabilities 210 #define if_rdomain if_data.ifi_rdomain 211 212 enum if_counters { 213 ifc_ipackets, /* packets received on interface */ 214 ifc_ierrors, /* input errors on interface */ 215 ifc_opackets, /* packets sent on interface */ 216 ifc_oerrors, /* output errors on interface */ 217 ifc_collisions, /* collisions on csma interfaces */ 218 ifc_ibytes, /* total number of octets received */ 219 ifc_obytes, /* total number of octets sent */ 220 ifc_imcasts, /* packets received via multicast */ 221 ifc_omcasts, /* packets sent via multicast */ 222 ifc_iqdrops, /* dropped on input, this interface */ 223 ifc_oqdrops, /* dropped on output, this interface */ 224 ifc_noproto, /* destined for unsupported protocol */ 225 226 ifc_ncounters 227 }; 228 229 /* 230 * The ifaddr structure contains information about one address 231 * of an interface. They are maintained by the different address families, 232 * are allocated and attached when an address is set, and are linked 233 * together so all addresses for an interface can be located. 234 */ 235 struct ifaddr { 236 struct sockaddr *ifa_addr; /* address of interface */ 237 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 238 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 239 struct sockaddr *ifa_netmask; /* used to determine subnet */ 240 struct ifnet *ifa_ifp; /* back-pointer to interface */ 241 TAILQ_ENTRY(ifaddr) ifa_list; /* list of addresses for interface */ 242 u_int ifa_flags; /* interface flags, see below */ 243 u_int ifa_refcnt; /* number of `rt_ifa` references */ 244 int ifa_metric; /* cost of going out this interface */ 245 }; 246 247 #define IFA_ROUTE 0x01 /* Auto-magically installed route */ 248 249 /* 250 * Interface multicast address. 251 */ 252 struct ifmaddr { 253 struct sockaddr *ifma_addr; /* Protocol address */ 254 unsigned int ifma_ifidx; /* Index of the interface */ 255 unsigned int ifma_refcnt; /* Count of references */ 256 TAILQ_ENTRY(ifmaddr) ifma_list; /* Per-interface list */ 257 }; 258 259 /* 260 * interface groups 261 */ 262 263 struct ifg_group { 264 char ifg_group[IFNAMSIZ]; 265 u_int ifg_refcnt; 266 caddr_t ifg_pf_kif; 267 int ifg_carp_demoted; 268 TAILQ_HEAD(, ifg_member) ifg_members; 269 TAILQ_ENTRY(ifg_group) ifg_next; 270 }; 271 272 struct ifg_member { 273 TAILQ_ENTRY(ifg_member) ifgm_next; 274 struct ifnet *ifgm_ifp; 275 }; 276 277 struct ifg_list { 278 struct ifg_group *ifgl_group; 279 TAILQ_ENTRY(ifg_list) ifgl_next; 280 }; 281 282 #define IFNET_SLOWTIMO 1 /* granularity is 1 second */ 283 284 /* 285 * IFQ compat on ifq API 286 */ 287 288 #define IFQ_ENQUEUE(ifq, m, err) \ 289 do { \ 290 (err) = ifq_enqueue((ifq), (m)); \ 291 } while (/* CONSTCOND */0) 292 293 #define IFQ_DEQUEUE(ifq, m) \ 294 do { \ 295 (m) = ifq_dequeue(ifq); \ 296 } while (/* CONSTCOND */0) 297 298 #define IFQ_PURGE(ifq) \ 299 do { \ 300 (void)ifq_purge(ifq); \ 301 } while (/* CONSTCOND */0) 302 303 #define IFQ_LEN(ifq) ifq_len(ifq) 304 #define IFQ_IS_EMPTY(ifq) ifq_empty(ifq) 305 #define IFQ_SET_MAXLEN(ifq, len) ifq_set_maxlen(ifq, len) 306 307 #define IF_TXMIT_MIN 1 308 #define IF_TXMIT_DEFAULT 16 309 310 /* default interface priorities */ 311 #define IF_WIRED_DEFAULT_PRIORITY 0 312 #define IF_WIRELESS_DEFAULT_PRIORITY 4 313 #define IF_WWAN_DEFAULT_PRIORITY 6 314 #define IF_CARP_DEFAULT_PRIORITY 15 315 316 /* 317 * Network stack input queues. 318 */ 319 struct niqueue { 320 struct mbuf_queue ni_q; 321 u_int ni_isr; 322 }; 323 324 #define NIQUEUE_INITIALIZER(_len, _isr) \ 325 { MBUF_QUEUE_INITIALIZER((_len), IPL_NET), (_isr) } 326 327 void niq_init(struct niqueue *, u_int, u_int); 328 int niq_enqueue(struct niqueue *, struct mbuf *); 329 int niq_enlist(struct niqueue *, struct mbuf_list *); 330 331 #define niq_dequeue(_q) mq_dequeue(&(_q)->ni_q) 332 #define niq_dechain(_q) mq_dechain(&(_q)->ni_q) 333 #define niq_delist(_q, _ml) mq_delist(&(_q)->ni_q, (_ml)) 334 #define niq_len(_q) mq_len(&(_q)->ni_q) 335 #define niq_drops(_q) mq_drops(&(_q)->ni_q) 336 #define sysctl_niq(_n, _l, _op, _olp, _np, _nl, _niq) \ 337 sysctl_mq((_n), (_l), (_op), (_olp), (_np), (_nl), &(_niq)->ni_q) 338 339 extern struct ifnet_head ifnet; 340 341 void if_start(struct ifnet *); 342 int if_enqueue(struct ifnet *, struct mbuf *); 343 int if_enqueue_ifq(struct ifnet *, struct mbuf *); 344 void if_input(struct ifnet *, struct mbuf_list *); 345 void if_vinput(struct ifnet *, struct mbuf *); 346 void if_input_process(struct ifnet *, struct mbuf_list *); 347 int if_input_local(struct ifnet *, struct mbuf *, sa_family_t); 348 int if_output_local(struct ifnet *, struct mbuf *, sa_family_t); 349 void if_rtrequest_dummy(struct ifnet *, int, struct rtentry *); 350 void p2p_rtrequest(struct ifnet *, int, struct rtentry *); 351 352 struct ifaddr *ifa_ifwithaddr(struct sockaddr *, u_int); 353 struct ifaddr *ifa_ifwithdstaddr(struct sockaddr *, u_int); 354 struct ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *); 355 void ifafree(struct ifaddr *); 356 357 int if_isconnected(const struct ifnet *, unsigned int); 358 359 void if_clone_attach(struct if_clone *); 360 361 int if_clone_create(const char *, int); 362 int if_clone_destroy(const char *); 363 364 struct if_clone * 365 if_clone_lookup(const char *, int *); 366 367 void ifa_add(struct ifnet *, struct ifaddr *); 368 void ifa_del(struct ifnet *, struct ifaddr *); 369 void ifa_update_broadaddr(struct ifnet *, struct ifaddr *, 370 struct sockaddr *); 371 372 void if_ih_insert(struct ifnet *, int (*)(struct ifnet *, struct mbuf *, 373 void *), void *); 374 void if_ih_remove(struct ifnet *, int (*)(struct ifnet *, struct mbuf *, 375 void *), void *); 376 377 void if_addrhook_add(struct ifnet *, struct task *); 378 void if_addrhook_del(struct ifnet *, struct task *); 379 void if_addrhooks_run(struct ifnet *); 380 void if_linkstatehook_add(struct ifnet *, struct task *); 381 void if_linkstatehook_del(struct ifnet *, struct task *); 382 void if_detachhook_add(struct ifnet *, struct task *); 383 void if_detachhook_del(struct ifnet *, struct task *); 384 385 void if_rxr_livelocked(struct if_rxring *); 386 void if_rxr_init(struct if_rxring *, u_int, u_int); 387 u_int if_rxr_get(struct if_rxring *, u_int); 388 389 #define if_rxr_put(_r, _c) do { (_r)->rxr_alive -= (_c); } while (0) 390 #define if_rxr_inuse(_r) ((_r)->rxr_alive) 391 #define if_rxr_cwm(_r) ((_r)->rxr_cwm) 392 393 int if_rxr_info_ioctl(struct if_rxrinfo *, u_int, struct if_rxring_info *); 394 int if_rxr_ioctl(struct if_rxrinfo *, const char *, u_int, 395 struct if_rxring *); 396 397 void if_counters_alloc(struct ifnet *); 398 void if_counters_free(struct ifnet *); 399 400 int if_txhprio_l2_check(int); 401 int if_txhprio_l3_check(int); 402 int if_rxhprio_l2_check(int); 403 int if_rxhprio_l3_check(int); 404 405 #endif /* _KERNEL */ 406 407 #endif /* _NET_IF_VAR_H_ */ 408