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