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