aab7be32c4d48d2f4863c0e66c5a2b355f8cfb76
[dragonfly.git] / sys / net / if_var.h
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      From: @(#)if.h  8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_var.h,v 1.18.2.16 2003/04/15 18:11:19 fjoe Exp $
35  */
36
37 #ifndef _NET_IF_VAR_H_
38 #define _NET_IF_VAR_H_
39
40 #ifndef _SYS_SERIALIZE_H_
41 #include <sys/serialize.h>
42 #endif
43 #ifndef _NET_IF_H_
44 #include <net/if.h>
45 #endif
46 #ifndef _SYS_MUTEX_H_
47 #include <sys/mutex.h>
48 #endif
49
50 /*
51  * Structures defining a network interface, providing a packet
52  * transport mechanism (ala level 0 of the PUP protocols).
53  *
54  * Each interface accepts output datagrams of a specified maximum
55  * length, and provides higher level routines with input datagrams
56  * received from its medium.
57  *
58  * Output occurs when the routine if_output is called, with four parameters:
59  *
60  *      ifp->if_output(ifp, m, dst, rt)
61  *
62  * Here m is the mbuf chain to be sent and dst is the destination address.
63  * The output routine encapsulates the supplied datagram if necessary,
64  * and then transmits it on its medium.
65  *
66  * On input, each interface unwraps the data received by it, and either
67  * places it on the input queue of a internetwork datagram routine
68  * and posts the associated software interrupt, or passes the datagram to
69  * the routine if_input. It is called with four parameters:
70  *
71  *      ifp->if_input(ifp, m, pi, cpuid)
72  *
73  * Here m is the mbuf chain to be received. The input routine removes the
74  * protocol dependent header if necessary. A driver may also call using
75  * custom struct pktinfo reference pi and a cpuid to take advantage of
76  * hardware supplied information. Otherwise, the defaults for pi and cpuid
77  * are as follows:
78  *
79  *      ifp->if_input(ifp, m, NULL, -1);
80  *
81  * Routines exist for locating interfaces by their addresses
82  * or for locating a interface on a certain network, as well as more general
83  * routing and gateway routines maintaining information used to locate
84  * interfaces.  These routines live in the files if.c and route.c
85  */
86
87 /*
88  * Forward structure declarations for function prototypes [sic].
89  */
90 struct  mbuf;
91 struct  proc;
92 struct  rtentry;
93 struct  rt_addrinfo;
94 struct  socket;
95 struct  ether_header;
96 struct  ucred;
97 struct  lwkt_serialize;
98 struct  ifaddr_container;
99 struct  ifaddr;
100 struct  lwkt_port;
101 struct  lwkt_msg;
102 union   netmsg;
103 struct  pktinfo;
104 struct  ifpoll_info;
105 struct  ifdata_pcpu;
106
107 #include <sys/queue.h>          /* get TAILQ macros */
108
109 #include <net/altq/if_altq.h>
110
111 #ifdef _KERNEL
112 #include <sys/eventhandler.h>
113 #include <sys/mbuf.h>
114 #include <sys/systm.h>          /* XXX */
115 #include <sys/thread2.h>
116 #endif /* _KERNEL */
117
118 #define IF_DUNIT_NONE   -1
119
120 TAILQ_HEAD(ifnethead, ifnet);   /* we use TAILQs so that the order of */
121 TAILQ_HEAD(ifaddrhead, ifaddr_container); /* instantiation is preserved in the list */
122 TAILQ_HEAD(ifmultihead, ifmultiaddr);
123
124 /*
125  * Structure defining a mbuf queue.
126  */
127 struct ifqueue {
128         struct  mbuf *ifq_head;
129         struct  mbuf *ifq_tail;
130         int     ifq_len;
131         int     ifq_maxlen;
132         int     ifq_drops;
133 };
134
135 /*
136  * Note of IFPOLL_ENABLE
137  * 1) Any file(*.c) that depends on IFPOLL_ENABLE supports in this
138  *    file should include opt_ifpoll.h at its beginning.
139  * 2) When struct changes, which are conditioned by IFPOLL_ENABLE,
140  *    are to be introduced, please keep the struct's size and layout
141  *    same, no matter whether IFPOLL_ENABLE is defined or not.
142  *    See ifnet.if_npoll and ifnet.if_npoll_unused for example.
143  */
144
145 /*
146  * Network serialize/deserialize types
147  */
148 enum ifnet_serialize {
149         IFNET_SERIALIZE_ALL     /* all serializers */
150 };
151
152 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
153
154 /*
155  * Structure defining a network interface.
156  *
157  * (Would like to call this struct ``if'', but C isn't PL/1.)
158  */
159
160 /*
161  * NB: For DragonFlyBSD, it is assumed that each NIC driver's softc starts
162  * with one of these structures, typically held within an arpcom structure.
163  *
164  *      struct <foo>_softc {
165  *              struct arpcom {
166  *                      struct  ifnet ac_if;
167  *                      ...
168  *              } <arpcom> ;
169  *              ...   
170  *      };
171  *
172  * The assumption is used in a number of places, including many
173  * files in sys/net, device drivers, and sys/dev/mii.c:miibus_attach().
174  *
175  * Unfortunately devices' softc are opaque, so we depend on this layout
176  * to locate the struct ifnet from the softc in the generic code.
177  *
178  *
179  *
180  * MPSAFE NOTES:
181  *
182  * ifnet is protected by calling if_serialize, if_tryserialize and
183  * if_deserialize serialize functions with the ifnet_serialize parameter.
184  * Callers of if_ioctl, if_watchdog, if_init, if_resolvemulti, and if_npoll
185  * should call the ifnet serialize functions with IFNET_SERIALIZE_ALL.
186  *
187  * if_snd subqueues are protected by its own serializers.  Callers of
188  * if_start should call ifsq_serialiize_hw(), ifsq_deserialize_hw() and
189  * ifsq_tryserialize_hw() to properly serialize hardware for transmission.
190  *
191  * Caller of if_output MUST NOT serialize ifnet or if_snd by calling
192  * the related serialize functions.
193  *
194  * For better tranmission performance, driver should setup if_snd subqueue
195  * owner cpuid properly using ifsq_set_cpuid() (or ifq_set_cpuid(), if not
196  * multiple transmit queue capable).  Normally, the if_snd subqueue owner
197  * cpu is the one that processing the transmission interrupt.  And in driver,
198  * direct call of if_start should be avoided, use ifsq_devstart() or
199  * ifsq_devstart_sched() instead (or if_devstart()/if_devstart_sched(), if
200  * not multiple transmit queue capable).
201  *
202  *
203  *
204  * STATISTICS:
205  *
206  * if_data is no longer used to hold per interface statistics, so DO NOT use
207  * the old style ifp->if_ipackets++ to update statistics; instead IFNET_STAT_
208  * macros should be used.
209  *
210  *
211  *
212  * SINGLE SERIALIZER MODE:
213  *
214  * In this mode, driver MUST NOT setup if_serialize, if_deserialize,
215  * if_tryserialize or if_serialize_assert.  Driver could supply its own
216  * serializer to be used (through the type specific attach function, e.g.
217  * ether_ifattach()) or it could depend on the default serializer.  In this
218  * mode if_serializer will be setup properly.
219  *
220  * If a device driver installs the same serializer for its interrupt
221  * as for ifnet, then the driver only really needs to worry about further
222  * serialization in timeout based entry points and device_method_t entry
223  * points.  All other entry points will already be serialized.
224  *
225  *
226  *
227  * MULTI SERIALIZERS MODE:
228  *
229  * In this mode, driver MUST setup if_serialize, if_deserialize,
230  * if_tryserialize and if_serialize_assert.  Driver MUST NOT supply its own
231  * serializer to be used.  In this mode, if_serializer will be left as NULL.
232  * And driver MUST setup if_snd subqueues' hardware serailizer properly by
233  * calling ifsq_set_hw_serialize().
234  *
235  *
236  *
237  * MULTIPLE TRANSMIT QUEUES:
238  *
239  * This should be implemented in "MULTI SERIALIZERS MODE".  Legacy if_watchdog
240  * method SHOULD NOT be used.
241  *
242  * 1) Attach
243  *
244  * Before the type specific attach, e.g. ether_ifattach(), driver should
245  * setup the transmit queue count and cpuid to subqueue mapping method
246  * properly (assume QCOUNT is power of 2):
247  *
248  *      ifq_set_subq_cnt(&ifp->if_snd, QCOUNT);
249  *      ifp->if_mapsubq = ifq_mapsubq_mask;
250  *      ifq_set_subq_mask(&ifp->if_snd, QCOUNT - 1);
251  *
252  * After the type specific attach, driver should setup the subqueues owner
253  * cpu, serializer and watchdog properly:
254  *
255  *      for (i = 0; i < QCOUNT, ++i) {
256  *              struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
257  *
258  *              ifsq_set_cpuid(ifsq, Q_CPUID);
259  *              ifsq_set_hw_serialize(ifsq, Q_SLIZE);
260  *              ifsq_watchdog_init(Q_WDOG, ifsq, Q_WDOG_FUNC);
261  *      }
262  *
263  * Q_CPUID, the cpu which handles the hardware transmit queue interrupt
264  * Q_SLIZE, the serializer protects the hardware transmit queue
265  * Q_WDOG, per hardware transmit queue watchdog handler, struct ifsubq_watchdog
266  * Q_WDOG_FUNC, watchdog function, probably should reset hardware
267  *
268  * 2) Stop
269  *
270  * Make sure per hardware transmit queue watchdog is stopped and oactive is
271  * cleared:
272  *
273  *      for (i = 0; i < QCOUNT, ++i) {
274  *              ifsq_clr_oactive(ifsq);
275  *              ifsq_watchdog_stop(Q_WDOG);
276  *      }
277  *
278  * 3) Initialize
279  *
280  * Make sure per hardware transmit queue watchdog is started and oactive is
281  * cleared:
282  *
283  *      for (i = 0; i < QCOUNT, ++i) {
284  *              ifsq_clr_oactive(ifsq);
285  *              ifsq_watchdog_start(Q_WDOG);
286  *      }
287  *
288  * 4) if_start
289  *
290  * if_start takes subqueue as parameter, so instead of using ifq_ functions
291  * ifsq_ functions should be used.  If device could not be programmed to
292  * transmit when no media link is not up, MAKE SURE to purge the subqueue:
293  *
294  *      if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
295  *              return;
296  *      if (NO_LINK) {
297  *              ifsq_purge(ifsq);
298  *              return;
299  *      }
300  *      for (;;) {
301  *              if (NO_FREE_DESC) {
302  *                      ifsq_set_oactive(ifsq);
303  *                      break;
304  *              }
305  *              m = ifsq_dequeue(ifsq);
306  *              if (m != NULL)
307  *                      DRIVER_ENCAP(m);
308  *              Q_WDOG.wd_timer = WDOG_TIMEOUT;
309  *      }
310  *
311  * 5) Transmission done, e.g. transmit queue interrupt processing
312  *
313  * Same as if_start, ifsq_ functions should be used:
314  *
315  *      DRIVER_COLLECT_DESC();
316  *      if (HAS_FREE_DESC)
317  *              ifsq_clr_oactive(ifsq);
318  *      if (NO_PENDING_DESC)
319  *              Q_WDOG.wd_timer = 0;
320  *      if (!ifsq_is_empty(ifsq))
321  *              ifsq_devstart(ifsq);
322  */
323 struct ifnet {
324         void    *if_softc;              /* pointer to driver state */
325         void    *if_l2com;              /* pointer to protocol bits */
326         TAILQ_ENTRY(ifnet) if_link;     /* all struct ifnets are chained */
327         char    if_xname[IFNAMSIZ];     /* external name (name + unit) */
328         const char *if_dname;           /* driver name */
329         int     if_dunit;               /* unit or IF_DUNIT_NONE */
330         void    *if_vlantrunks;         /* vlan trunks */
331         struct  ifaddrhead *if_addrheads; /* per-cpu per-if addresses */
332         int     if_pcount;              /* number of promiscuous listeners */
333         void    *if_carp;               /* carp interfaces */
334         struct  bpf_if *if_bpf;         /* packet filter structure */
335         u_short if_index;               /* numeric abbreviation for this if  */
336         short   if_timer;               /* time 'til if_watchdog called */
337         int     if_flags;               /* up/down, broadcast, etc. */
338         int     if_capabilities;        /* interface capabilities */
339         int     if_capenable;           /* enabled features */
340         void    *if_linkmib;            /* link-type-specific MIB data */
341         size_t  if_linkmiblen;          /* length of above data */
342         struct  if_data if_data;        /* NOTE: stats are in if_data_pcpu */
343         struct  ifmultihead if_multiaddrs; /* multicast addresses configured */
344         int     if_amcount;             /* number of all-multicast requests */
345 /* procedure handles */
346         int     (*if_output)            /* output routine (enqueue) */
347                 (struct ifnet *, struct mbuf *, struct sockaddr *,
348                      struct rtentry *);
349         void    (*if_input)             /* input routine from hardware driver */
350                 (struct ifnet *, struct mbuf *,
351                      const struct pktinfo *pi, int cpuid);
352         void    (*if_start)             /* initiate output routine */
353                 (struct ifnet *, struct ifaltq_subque *);
354         int     (*if_ioctl)             /* ioctl routine */
355                 (struct ifnet *, u_long, caddr_t, struct ucred *);
356         void    (*if_watchdog)          /* timer routine */
357                 (struct ifnet *);
358         void    (*if_init)              /* init routine */
359                 (void *);
360         int     (*if_resolvemulti)      /* validate/resolve multicast */
361                 (struct ifnet *, struct sockaddr **, struct sockaddr *);
362         void    *if_unused5;
363         TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
364         int     (*if_mapsubq)           /* cpuid to if_snd subqueue map */
365                 (struct ifaltq *, int);
366         int     if_unused2;
367
368         /*
369          * ifnet serialize functions
370          */
371         void    (*if_serialize)
372                 (struct ifnet *, enum ifnet_serialize);
373         void    (*if_deserialize)
374                 (struct ifnet *, enum ifnet_serialize);
375         int     (*if_tryserialize)
376                 (struct ifnet *, enum ifnet_serialize);
377 #ifdef INVARIANTS
378         void    (*if_serialize_assert)
379                 (struct ifnet *, enum ifnet_serialize, boolean_t);
380 #else
381         /* Place holder */
382         void    (*if_serialize_unused)(void);
383 #endif
384
385 #ifdef IFPOLL_ENABLE
386         void    (*if_npoll)             /* polling config */
387                 (struct ifnet *, struct ifpoll_info *);
388 #else
389         /* Place holder */
390         void    (*if_npoll_unused)(void);
391 #endif
392         int     if_tsolen;              /* max TSO length */
393         struct  ifaltq if_snd;          /* output subqueues */
394         const uint8_t   *if_broadcastaddr;
395         void    *if_bridge;             /* bridge glue */
396         void    *if_lagg;               /* lagg glue */
397         void    *if_afdata[AF_MAX];
398         struct ifaddr   *if_lladdr;
399
400         /* serializer, in single serializer mode */
401         struct lwkt_serialize *if_serializer;
402         /*
403          * default serializer, in single serializer mode,
404          * if driver does not supply one
405          */
406         struct lwkt_serialize if_default_serializer;
407
408         int     if_unused4;
409         struct ifdata_pcpu *if_data_pcpu; /* per-cpu stats */
410         void    *if_pf_kif;             /* pf interface */
411
412         /*
413          * Mbuf clusters/jclusters limits should be increased
414          * by if_nmbclusters/if_nmbjclusters.  Mainly for mbuf
415          * clusters/jclusters that could sit on the device
416          * queues, e.g. reception queues, for quite some time.
417          */
418         int     if_nmbclusters;
419         int     if_nmbjclusters;
420 };
421 typedef void if_init_f_t (void *);
422
423 #define if_mtu          if_data.ifi_mtu
424 #define if_type         if_data.ifi_type
425 #define if_physical     if_data.ifi_physical
426 #define if_addrlen      if_data.ifi_addrlen
427 #define if_hdrlen       if_data.ifi_hdrlen
428 #define if_metric       if_data.ifi_metric
429 #define if_link_state   if_data.ifi_link_state
430 #define if_baudrate     if_data.ifi_baudrate
431 #define if_hwassist     if_data.ifi_hwassist
432 #define if_ipackets     if_data.ifi_ipackets
433 #define if_ierrors      if_data.ifi_ierrors
434 #define if_opackets     if_data.ifi_opackets
435 #define if_oerrors      if_data.ifi_oerrors
436 #define if_collisions   if_data.ifi_collisions
437 #define if_ibytes       if_data.ifi_ibytes
438 #define if_obytes       if_data.ifi_obytes
439 #define if_imcasts      if_data.ifi_imcasts
440 #define if_omcasts      if_data.ifi_omcasts
441 #define if_iqdrops      if_data.ifi_iqdrops
442 #define if_noproto      if_data.ifi_noproto
443 #define if_lastchange   if_data.ifi_lastchange
444 #define if_recvquota    if_data.ifi_recvquota
445 #define if_xmitquota    if_data.ifi_xmitquota
446 #define if_rawoutput(if, m, sa) if_output(if, m, sa, NULL)
447
448 /* for compatibility with other BSDs */
449 #define if_list         if_link
450
451 /*
452  * Per-cpu interface statistics
453  */
454 struct ifdata_pcpu {
455         u_long  ifd_ipackets;           /* packets received on interface */
456         u_long  ifd_ierrors;            /* input errors on interface */
457         u_long  ifd_opackets;           /* packets sent on interface */
458         u_long  ifd_oerrors;            /* output errors on interface */
459         u_long  ifd_collisions;         /* collisions on csma interfaces */
460         u_long  ifd_ibytes;             /* total number of octets received */
461         u_long  ifd_obytes;             /* total number of octets sent */
462         u_long  ifd_imcasts;            /* packets received via multicast */
463         u_long  ifd_omcasts;            /* packets sent via multicast */
464         u_long  ifd_iqdrops;            /* dropped on input, this interface */
465         u_long  ifd_noproto;            /* destined for unsupported protocol */
466 } __cachealign;
467
468 #endif  /* _KERNEL || _KERNEL_STRUCTURES */
469
470 /*
471  * ifqueue operation macros
472  */
473 #define IF_QFULL(ifq)           ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
474 #define IF_DROP(ifq)            ((ifq)->ifq_drops++)
475 #define IF_QLEN(ifq)            ((ifq)->ifq_len)
476 #define IF_QEMPTY(ifq)          (IF_QLEN(ifq) == 0)
477
478 #define IF_ENQUEUE(ifq, m) do {                                         \
479         (m)->m_nextpkt = NULL;                                          \
480         if ((ifq)->ifq_tail == NULL)                                    \
481                 (ifq)->ifq_head = m;                                    \
482         else                                                            \
483                 (ifq)->ifq_tail->m_nextpkt = m;                         \
484         (ifq)->ifq_tail = m;                                            \
485         (ifq)->ifq_len++;                                               \
486 } while (0)
487
488 #define IF_PREPEND(ifq, m) do {                                         \
489         (m)->m_nextpkt = (ifq)->ifq_head;                               \
490         if ((ifq)->ifq_tail == NULL)                                    \
491                 (ifq)->ifq_tail = (m);                                  \
492         (ifq)->ifq_head = (m);                                          \
493         (ifq)->ifq_len++;                                               \
494 } while (0)
495
496 #define IF_DEQUEUE(ifq, m) do {                                         \
497         (m) = (ifq)->ifq_head;                                          \
498         if (m) {                                                        \
499                 if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL)         \
500                         (ifq)->ifq_tail = NULL;                         \
501                 (m)->m_nextpkt = NULL;                                  \
502                 (ifq)->ifq_len--;                                       \
503         }                                                               \
504 } while (0)
505
506 #define IF_POLL(ifq, m)         ((m) = (ifq)->ifq_head)
507
508 #define IF_DRAIN(ifq) do {                                              \
509         struct mbuf *m;                                                 \
510         while (1) {                                                     \
511                 IF_DEQUEUE(ifq, m);                                     \
512                 if (m == NULL)                                          \
513                         break;                                          \
514                 m_freem(m);                                             \
515         }                                                               \
516 } while (0)
517
518 #ifdef _KERNEL
519
520 /* interface link layer address change event */
521 typedef void (*iflladdr_event_handler_t)(void *, struct ifnet *);
522 EVENTHANDLER_DECLARE(iflladdr_event, iflladdr_event_handler_t);
523
524 #ifdef INVARIANTS
525 #define ASSERT_IFNET_SERIALIZED_ALL(ifp) \
526         (ifp)->if_serialize_assert((ifp), IFNET_SERIALIZE_ALL, TRUE)
527 #define ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp) \
528         (ifp)->if_serialize_assert((ifp), IFNET_SERIALIZE_ALL, FALSE)
529 #else
530 #define ASSERT_IFNET_SERIALIZED_ALL(ifp)        ((void)0)
531 #define ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp)    ((void)0)
532 #endif
533
534 static __inline void
535 ifnet_serialize_all(struct ifnet *_ifp)
536 {
537         _ifp->if_serialize(_ifp, IFNET_SERIALIZE_ALL);
538 }
539
540 static __inline void
541 ifnet_deserialize_all(struct ifnet *_ifp)
542 {
543         _ifp->if_deserialize(_ifp, IFNET_SERIALIZE_ALL);
544 }
545
546 static __inline int
547 ifnet_tryserialize_all(struct ifnet *_ifp)
548 {
549         return _ifp->if_tryserialize(_ifp, IFNET_SERIALIZE_ALL);
550 }
551
552 /*
553  * 72 was chosen below because it is the size of a TCP/IP
554  * header (40) + the minimum mss (32).
555  */
556 #define IF_MINMTU       72
557 #define IF_MAXMTU       65535
558
559 #endif /* _KERNEL */
560
561 struct in_ifaddr;
562
563 struct in_ifaddr_container {
564         struct in_ifaddr        *ia;
565         LIST_ENTRY(in_ifaddr_container) ia_hash;
566                                 /* entry in bucket of inet addresses */
567         TAILQ_ENTRY(in_ifaddr_container) ia_link;
568                                 /* list of internet addresses */
569         struct ifaddr_container *ia_ifac; /* parent ifaddr_container */
570 };
571
572 /*
573  * Per-cpu ifaddr container:
574  * - per-cpu ifaddr reference count
575  * - linkage to per-cpu addresses lists
576  * - per-cpu ifaddr statistics
577  */
578 struct ifaddr_container {
579 #define IFA_CONTAINER_MAGIC     0x19810219
580 #define IFA_CONTAINER_DEAD      0xc0dedead
581         uint32_t                ifa_magic;  /* IFA_CONTAINER_MAGIC */
582         struct ifaddr           *ifa;
583         TAILQ_ENTRY(ifaddr_container)   ifa_link;   /* queue macro glue */
584         u_int                   ifa_refcnt; /* references to this structure */
585         uint16_t                ifa_listmask;   /* IFA_LIST_ */
586         uint16_t                ifa_prflags;    /* protocol specific flags */
587
588         u_long                  ifa_ipackets;   /* packets received on addr */
589         u_long                  ifa_ibytes;     /* bytes received on addr */
590         u_long                  ifa_opackets;   /* packets sent on addr */
591         u_long                  ifa_obytes;     /* bytes sent on addr */
592
593         /*
594          * Protocol specific states
595          */
596         union {
597                 struct in_ifaddr_container u_in_ifac;
598         } ifa_proto_u;
599 } __cachealign;
600
601 #define IFA_LIST_IFADDRHEAD     0x01    /* on ifnet.if_addrheads[cpuid] */
602 #define IFA_LIST_IN_IFADDRHEAD  0x02    /* on in_ifaddrheads[cpuid] */
603 #define IFA_LIST_IN_IFADDRHASH  0x04    /* on in_ifaddrhashtbls[cpuid] */
604
605 #define IFA_PRF_FLAG0           0x01
606 #define IFA_PRF_FLAG1           0x02
607 #define IFA_PRF_FLAG2           0x04
608 #define IFA_PRF_FLAG3           0x08
609
610 /*
611  * The ifaddr structure contains information about one address
612  * of an interface.  They are maintained by the different address families,
613  * are allocated and attached when an address is set, and are linked
614  * together so all addresses for an interface can be located.
615  *
616  * NOTE:
617  * Statistics are no longer stored in if_data, instead, they are stored
618  * in the per-cpu ifaddr_container.  So don't use the old style
619  * ifa->if_ipackets++ to update statistics, use IFA_STAT_ macros.
620  */
621 struct ifaddr {
622         struct  sockaddr *ifa_addr;     /* address of interface */
623         struct  sockaddr *ifa_dstaddr;  /* other end of p-to-p link */
624 #define ifa_broadaddr   ifa_dstaddr     /* broadcast address interface */
625         struct  sockaddr *ifa_netmask;  /* used to determine subnet */
626         struct  if_data if_data;        /* not all members are meaningful */
627         struct  ifnet *ifa_ifp;         /* back-pointer to interface */
628         void    *ifa_link_pad;
629         struct ifaddr_container *ifa_containers; /* per-cpu data */
630         void    (*ifa_rtrequest)        /* check or clean routes (+ or -)'d */
631                 (int, struct rtentry *);
632         u_short ifa_flags;              /* mostly rt_flags for cloning */
633         int     ifa_ncnt;               /* # of valid ifaddr_container */
634         int     ifa_metric;             /* cost of going out this interface */
635 #ifdef notdef
636         struct  rtentry *ifa_rt;        /* XXXX for ROUTETOIF ????? */
637 #endif
638         int (*ifa_claim_addr)           /* check if an addr goes to this if */
639                 (struct ifaddr *, struct sockaddr *);
640
641 };
642 #define IFA_ROUTE       RTF_UP          /* route installed */
643
644 /* for compatibility with other BSDs */
645 #define ifa_list        ifa_link
646
647 /*
648  * Multicast address structure.  This is analogous to the ifaddr
649  * structure except that it keeps track of multicast addresses.
650  * Also, the reference count here is a count of requests for this
651  * address, not a count of pointers to this structure.
652  */
653 struct ifmultiaddr {
654         TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
655         struct  sockaddr *ifma_addr;    /* address this membership is for */
656         struct  sockaddr *ifma_lladdr;  /* link-layer translation, if any */
657         struct  ifnet *ifma_ifp;        /* back-pointer to interface */
658         u_int   ifma_refcount;          /* reference count */
659         void    *ifma_protospec;        /* protocol-specific state, if any */
660 };
661
662 #ifdef _KERNEL
663
664 struct ifaddr_marker {
665         struct ifaddr           ifa;
666         struct ifaddr_container ifac;
667         struct sockaddr         addr;
668         struct sockaddr         netmask;
669         struct sockaddr         dstaddr;
670 };
671
672 /*
673  * ifaddr statistics update macro
674  */
675 #define IFA_STAT_INC(ifa, name, v) \
676 do { \
677         (ifa)->ifa_containers[mycpuid].ifa_##name += (v); \
678 } while (0)
679
680 /*
681  * Interface (ifnet) statistics update macros
682  */
683 #define IFNET_STAT_INC(ifp, name, v) \
684 do { \
685         (ifp)->if_data_pcpu[mycpuid].ifd_##name += (v); \
686 } while (0)
687
688 #define IFNET_STAT_SET(ifp, name, v) \
689 do { \
690         int _cpu; \
691         (ifp)->if_data_pcpu[0].ifd_##name = (v); \
692         for (_cpu = 1; _cpu < ncpus; ++_cpu) \
693                 (ifp)->if_data_pcpu[_cpu].ifd_##name = 0; \
694 } while (0)
695
696 #define IFNET_STAT_GET(ifp, name, v) \
697 do { \
698         int _cpu; \
699         (v) = (ifp)->if_data_pcpu[0].ifd_##name; \
700         for (_cpu = 1; _cpu < ncpus; ++_cpu) \
701                 (v) += (ifp)->if_data_pcpu[_cpu].ifd_##name; \
702 } while (0)
703
704 #ifndef _SYS_SERIALIZE2_H_
705 #include <sys/serialize2.h>
706 #endif
707
708 enum ifaddr_event {
709         IFADDR_EVENT_ADD,
710         IFADDR_EVENT_DELETE,
711         IFADDR_EVENT_CHANGE
712 };
713
714 /* interface address change event */
715 typedef void (*ifaddr_event_handler_t)(void *, struct ifnet *,
716         enum ifaddr_event, struct ifaddr *);
717 EVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
718 /* new interface attach event */
719 typedef void (*ifnet_attach_event_handler_t)(void *, struct ifnet *);
720 EVENTHANDLER_DECLARE(ifnet_attach_event, ifnet_attach_event_handler_t);
721 /* interface detach event */
722 typedef void (*ifnet_detach_event_handler_t)(void *, struct ifnet *);
723 EVENTHANDLER_DECLARE(ifnet_detach_event, ifnet_detach_event_handler_t);
724
725 /* Array of all ifnets in the system */
726 struct ifnet_array {
727         int             ifnet_count;    /* # of elem. in ifnet_arr */
728         int             ifnet_pad;      /* explicit */
729         struct ifnet    *ifnet_arr[];
730 };
731
732 /*
733  * interface groups
734  */
735 struct ifg_group {
736         char                             ifg_group[IFNAMSIZ];
737         u_int                            ifg_refcnt;
738         void                            *ifg_pf_kif;
739         int                              ifg_carp_demoted;
740         TAILQ_HEAD(, ifg_member)         ifg_members;
741         TAILQ_ENTRY(ifg_group)           ifg_next;
742 };
743
744 struct ifg_member {
745         TAILQ_ENTRY(ifg_member)  ifgm_next;
746         struct ifnet            *ifgm_ifp;
747 };
748
749 struct ifg_list {
750         struct ifg_group        *ifgl_group;
751         TAILQ_ENTRY(ifg_list)    ifgl_next;
752 };
753
754 /* group attach event */
755 typedef void (*group_attach_event_handler_t)(void *, struct ifg_group *);
756 EVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t);
757 /* group detach event */
758 typedef void (*group_detach_event_handler_t)(void *, struct ifg_group *);
759 EVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t);
760 /* group change event */
761 typedef void (*group_change_event_handler_t)(void *, const char *);
762 EVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t);
763
764
765 #ifdef INVARIANTS
766 #define ASSERT_IFAC_VALID(ifac) do { \
767         KKASSERT((ifac)->ifa_magic == IFA_CONTAINER_MAGIC); \
768         KKASSERT((ifac)->ifa_refcnt > 0); \
769 } while (0)
770 #else
771 #define ASSERT_IFAC_VALID(ifac) ((void)0)
772 #endif
773
774 static __inline void
775 _IFAREF(struct ifaddr *_ifa, int _cpu_id)
776 {
777         struct ifaddr_container *_ifac = &_ifa->ifa_containers[_cpu_id];
778
779         crit_enter();
780         ASSERT_IFAC_VALID(_ifac);
781         ++_ifac->ifa_refcnt;
782         crit_exit();
783 }
784
785 static __inline void
786 IFAREF(struct ifaddr *_ifa)
787 {
788         _IFAREF(_ifa, mycpuid);
789 }
790
791 #include <sys/malloc.h>
792
793 MALLOC_DECLARE(M_IFADDR);
794 MALLOC_DECLARE(M_IFMADDR);
795 MALLOC_DECLARE(M_IFNET);
796
797 void    ifac_free(struct ifaddr_container *, int);
798
799 static __inline void
800 _IFAFREE(struct ifaddr *_ifa, int _cpu_id)
801 {
802         struct ifaddr_container *_ifac = &_ifa->ifa_containers[_cpu_id];
803
804         crit_enter();
805         ASSERT_IFAC_VALID(_ifac);
806         if (--_ifac->ifa_refcnt == 0)
807                 ifac_free(_ifac, _cpu_id);
808         crit_exit();
809 }
810
811 static __inline void
812 IFAFREE(struct ifaddr *_ifa)
813 {
814         _IFAFREE(_ifa, mycpuid);
815 }
816
817 struct lwkt_port *ifnet_portfn(int);
818 int     ifnet_domsg(struct lwkt_msg *, int);
819 void    ifnet_sendmsg(struct lwkt_msg *, int);
820 void    ifnet_forwardmsg(struct lwkt_msg *, int);
821
822 static __inline int
823 ifa_domsg(struct lwkt_msg *_lmsg, int _cpu)
824 {
825         return ifnet_domsg(_lmsg, _cpu);
826 }
827
828 static __inline void
829 ifa_sendmsg(struct lwkt_msg *_lmsg, int _cpu)
830 {
831         ifnet_sendmsg(_lmsg, _cpu);
832 }
833
834 static __inline void
835 ifa_forwardmsg(struct lwkt_msg *_lmsg, int _nextcpu)
836 {
837         ifnet_forwardmsg(_lmsg, _nextcpu);
838 }
839
840 static __inline void
841 ifnet_serialize_array_enter(lwkt_serialize_t *_arr, int _arrcnt,
842     enum ifnet_serialize _slz)
843 {
844         KKASSERT(_slz == IFNET_SERIALIZE_ALL);
845         lwkt_serialize_array_enter(_arr, _arrcnt, 0);
846 }
847
848 static __inline void
849 ifnet_serialize_array_exit(lwkt_serialize_t *_arr, int _arrcnt,
850     enum ifnet_serialize _slz)
851 {
852         KKASSERT(_slz == IFNET_SERIALIZE_ALL);
853         lwkt_serialize_array_exit(_arr, _arrcnt, 0);
854 }
855
856 static __inline int
857 ifnet_serialize_array_try(lwkt_serialize_t *_arr, int _arrcnt,
858     enum ifnet_serialize _slz)
859 {
860         KKASSERT(_slz == IFNET_SERIALIZE_ALL);
861         return lwkt_serialize_array_try(_arr, _arrcnt, 0);
862 }
863
864 #ifdef INVARIANTS
865
866 static __inline void
867 ifnet_serialize_array_assert(lwkt_serialize_t *_arr, int _arrcnt,
868     enum ifnet_serialize _slz, boolean_t _serialized)
869 {
870         int _i;
871
872         KKASSERT(_slz == IFNET_SERIALIZE_ALL);
873         if (_serialized) {
874                 for (_i = 0; _i < _arrcnt; ++_i)
875                         ASSERT_SERIALIZED(_arr[_i]);
876         } else {
877                 for (_i = 0; _i < _arrcnt; ++_i)
878                         ASSERT_NOT_SERIALIZED(_arr[_i]);
879         }
880 }
881
882 #endif  /* INVARIANTS */
883
884 #define REINPUT_KEEPRCVIF       0x0001  /* ether_reinput_oncpu() */
885 #define REINPUT_RUNBPF          0x0002  /* ether_reinput_oncpu() */
886
887 /*
888  * MPSAFE NOTE for ifnet queue (ifnet), ifnet array, ifunit() and
889  * ifindex2ifnet.
890  *
891  * - ifnet queue must only be accessed by non-netisr threads and
892  *   ifnet lock must be held (by ifnet_lock()).
893  * - If accessing ifnet queue is needed in netisrs, ifnet array
894  *   (obtained through ifnet_array_get()) must be used instead.
895  *   There is no need to (must not, actually) hold ifnet lock for
896  *   ifnet array accessing.
897  * - ifindex2ifnet could be accessed by both non-netisr threads and
898  *   netisrs.  Accessing ifindex2ifnet in non-netisr threads must be
899  *   protected by ifnet lock (by ifnet_lock()).  Accessing
900  *   ifindex2ifnet in netisrs is lockless MPSAFE and ifnet lock must
901  *   not be held.  However, ifindex2ifnet should be saved in a stack
902  *   variable to get a consistent view of ifindex2ifnet, if
903  *   ifindex2ifnet is accessed multiple times from a function in
904  *   netisrs.
905  * - ifunit() must only be called in non-netisr threads and ifnet
906  *   lock must be held before calling this function and for the
907  *   accessing of the ifp returned by this function.
908  * - If ifunit() is needed in netisr, ifunit_netisr() must be used
909  *   instead.  There is no need to (must not, actually) hold ifnet
910  *   lock for ifunit_netisr() and the returned ifp.
911  */
912 extern struct ifnethead ifnet;
913 #define ifnetlist       ifnet   /* easily distinguished ifnet alias */
914
915 extern struct ifnet     **ifindex2ifnet;
916 extern int              if_index;
917
918 struct ifnet            *ifunit(const char *);
919 struct ifnet            *ifunit_netisr(const char *);
920 const struct ifnet_array *ifnet_array_get(void);
921 int                     ifnet_array_isempty(void);
922
923 extern  int ifqmaxlen;
924 extern  struct ifnet loif[];
925
926 struct ip;
927 struct tcphdr;
928
929 void    ether_ifattach(struct ifnet *, const uint8_t *,
930             struct lwkt_serialize *);
931 void    ether_ifattach_bpf(struct ifnet *, const uint8_t *, u_int, u_int,
932             struct lwkt_serialize *);
933 void    ether_ifdetach(struct ifnet *);
934 void    ether_demux(struct mbuf *);
935 void    ether_demux_oncpu(struct ifnet *, struct mbuf *);
936 void    ether_reinput_oncpu(struct ifnet *, struct mbuf *, int);
937 void    ether_input(struct ifnet *, struct mbuf *,
938             const struct pktinfo *, int);
939 int     ether_output_frame(struct ifnet *, struct mbuf *);
940 int     ether_ioctl(struct ifnet *, u_long, caddr_t);
941 u_char  *kether_aton(const char *, u_char *);
942 char    *kether_ntoa(const u_char *, char *);
943 struct ifnet *ether_bridge_interface(struct ifnet *ifp);
944 uint32_t        ether_crc32_le(const uint8_t *, size_t);
945 uint32_t        ether_crc32_be(const uint8_t *, size_t);
946
947 int     if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
948 int     if_addmulti_serialized(struct ifnet *, struct sockaddr *,
949             struct ifmultiaddr **);
950 int     if_allmulti(struct ifnet *, int);
951 void    if_attach(struct ifnet *, struct lwkt_serialize *);
952 int     if_delmulti(struct ifnet *, struct sockaddr *);
953 void    if_delallmulti_serialized(struct ifnet *ifp);
954 void    if_purgeaddrs_nolink(struct ifnet *);
955 void    if_detach(struct ifnet *);
956 void    if_down(struct ifnet *);
957 void    if_link_state_change(struct ifnet *);
958 void    if_initname(struct ifnet *, const char *, int);
959 int     if_getanyethermac(uint16_t *, int);
960 int     if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
961 struct ifnet *if_alloc(uint8_t);
962 void    if_free(struct ifnet *);
963 void    if_route(struct ifnet *, int flag, int fam);
964 int     if_setlladdr(struct ifnet *, const u_char *, int);
965 void    if_unroute(struct ifnet *, int flag, int fam);
966 void    if_up(struct ifnet *);
967 /*void  ifinit(void);*/ /* declared in systm.h for main() */
968 int     ifioctl(struct socket *, u_long, caddr_t, struct ucred *);
969 int     ifpromisc(struct ifnet *, int);
970
971 struct  ifg_group *if_creategroup(const char *);
972 int     if_addgroup(struct ifnet *, const char *);
973 int     if_delgroup(struct ifnet *, const char *);
974 int     if_getgroup(caddr_t, struct ifnet *);
975 int     if_getgroupmembers(caddr_t);
976
977 struct  ifaddr *ifa_ifwithaddr(struct sockaddr *);
978 struct  ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
979 struct  ifaddr *ifa_ifwithnet(struct sockaddr *);
980 struct  ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
981 struct  ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
982
983 typedef void *if_com_alloc_t(u_char type, struct ifnet *ifp);
984 typedef void if_com_free_t(void *com, u_char type);
985 void    if_register_com_alloc(u_char, if_com_alloc_t *a, if_com_free_t *);
986 void    if_deregister_com_alloc(u_char);
987
988 void    *ifa_create(int);
989 void    ifa_destroy(struct ifaddr *);
990 void    ifa_iflink(struct ifaddr *, struct ifnet *, int);
991 void    ifa_ifunlink(struct ifaddr *, struct ifnet *);
992 void    ifa_marker_init(struct ifaddr_marker *, struct ifnet *);
993
994 struct  ifmultiaddr *ifmaof_ifpforaddr(struct sockaddr *, struct ifnet *);
995 int     if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
996 void    if_devstart(struct ifnet *ifp); /* COMPAT */
997 void    if_devstart_sched(struct ifnet *ifp); /* COMPAT */
998 int     if_ring_count2(int cnt, int cnt_max);
999
1000 void    ifnet_lock(void);
1001 void    ifnet_unlock(void);
1002
1003 #define IF_LLSOCKADDR(ifp)                                              \
1004     ((struct sockaddr_dl *)(ifp)->if_lladdr->ifa_addr)
1005 #define IF_LLADDR(ifp)  LLADDR(IF_LLSOCKADDR(ifp))
1006
1007 #ifdef IFPOLL_ENABLE
1008 int     ifpoll_register(struct ifnet *);
1009 int     ifpoll_deregister(struct ifnet *);
1010 #endif  /* IFPOLL_ENABLE */
1011
1012 #endif /* _KERNEL */
1013
1014 #endif /* !_NET_IF_VAR_H_ */