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