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