]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_pcb.h
Change KPI of how device drivers that provide wireless connectivity interact
[FreeBSD/FreeBSD.git] / sys / netinet / in_pcb.h
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2010-2011 Juniper Networks, Inc.
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Robert N. M. Watson under
8  * contract to Juniper Networks, Inc.
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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)in_pcb.h    8.1 (Berkeley) 6/10/93
35  * $FreeBSD$
36  */
37
38 #ifndef _NETINET_IN_PCB_H_
39 #define _NETINET_IN_PCB_H_
40
41 #include <sys/queue.h>
42 #include <sys/_lock.h>
43 #include <sys/_mutex.h>
44 #include <sys/_rwlock.h>
45
46 #ifdef _KERNEL
47 #include <sys/lock.h>
48 #include <sys/rwlock.h>
49 #include <net/vnet.h>
50 #include <vm/uma.h>
51 #endif
52
53 #define in6pcb          inpcb   /* for KAME src sync over BSD*'s */
54 #define in6p_sp         inp_sp  /* for KAME src sync over BSD*'s */
55 struct inpcbpolicy;
56
57 /*
58  * struct inpcb is the common protocol control block structure used in most
59  * IP transport protocols.
60  *
61  * Pointers to local and foreign host table entries, local and foreign socket
62  * numbers, and pointers up (to a socket structure) and down (to a
63  * protocol-specific control block) are stored here.
64  */
65 LIST_HEAD(inpcbhead, inpcb);
66 LIST_HEAD(inpcbporthead, inpcbport);
67 typedef u_quad_t        inp_gen_t;
68
69 /*
70  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
71  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
72  * the following structure.
73  */
74 struct in_addr_4in6 {
75         u_int32_t       ia46_pad32[3];
76         struct  in_addr ia46_addr4;
77 };
78
79 /*
80  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
81  * some extra padding to accomplish this.
82  */
83 struct in_endpoints {
84         u_int16_t       ie_fport;               /* foreign port */
85         u_int16_t       ie_lport;               /* local port */
86         /* protocol dependent part, local and foreign addr */
87         union {
88                 /* foreign host table entry */
89                 struct  in_addr_4in6 ie46_foreign;
90                 struct  in6_addr ie6_foreign;
91         } ie_dependfaddr;
92         union {
93                 /* local host table entry */
94                 struct  in_addr_4in6 ie46_local;
95                 struct  in6_addr ie6_local;
96         } ie_dependladdr;
97         u_int32_t       ie6_zoneid;             /* scope zone id */
98 };
99 #define ie_faddr        ie_dependfaddr.ie46_foreign.ia46_addr4
100 #define ie_laddr        ie_dependladdr.ie46_local.ia46_addr4
101 #define ie6_faddr       ie_dependfaddr.ie6_foreign
102 #define ie6_laddr       ie_dependladdr.ie6_local
103
104 /*
105  * XXX The defines for inc_* are hacks and should be changed to direct
106  * references.
107  */
108 struct in_conninfo {
109         u_int8_t        inc_flags;
110         u_int8_t        inc_len;
111         u_int16_t       inc_fibnum;     /* XXX was pad, 16 bits is plenty */
112         /* protocol dependent part */
113         struct  in_endpoints inc_ie;
114 };
115
116 /*
117  * Flags for inc_flags.
118  */
119 #define INC_ISIPV6      0x01
120
121 #define inc_isipv6      inc_flags       /* temp compatability */
122 #define inc_fport       inc_ie.ie_fport
123 #define inc_lport       inc_ie.ie_lport
124 #define inc_faddr       inc_ie.ie_faddr
125 #define inc_laddr       inc_ie.ie_laddr
126 #define inc6_faddr      inc_ie.ie6_faddr
127 #define inc6_laddr      inc_ie.ie6_laddr
128 #define inc6_zoneid     inc_ie.ie6_zoneid
129
130 struct  icmp6_filter;
131
132 /*-
133  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
134  * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
135  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
136  * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
137  * few fields are protected by multiple locks as indicated in the locking notes
138  * below.  For these fields, all of the listed locks must be write-locked for
139  * any modifications.  However, these fields can be safely read while any one of
140  * the listed locks are read-locked.  This model can permit greater concurrency
141  * for read operations.  For example, connections can be looked up while only
142  * holding a read lock on the global pcblist lock.  This is important for
143  * performance when attempting to find the connection for a packet given its IP
144  * and port tuple.
145  *
146  * One noteworthy exception is that the global pcbinfo lock follows a different
147  * set of rules in relation to the inp_list field.  Rather than being
148  * write-locked for modifications and read-locked for list iterations, it must
149  * be read-locked during modifications and write-locked during list iterations.
150  * This ensures that the relatively rare global list iterations safely walk a
151  * stable snapshot of connections while allowing more common list modifications
152  * to safely grab the pcblist lock just while adding or removing a connection
153  * from the global list.
154  *
155  * Key:
156  * (c) - Constant after initialization
157  * (g) - Protected by the pcbgroup lock
158  * (i) - Protected by the inpcb lock
159  * (p) - Protected by the pcbinfo lock for the inpcb
160  * (l) - Protected by the pcblist lock for the inpcb
161  * (h) - Protected by the pcbhash lock for the inpcb
162  * (s) - Protected by another subsystem's locks
163  * (x) - Undefined locking
164  *
165  * A few other notes:
166  *
167  * When a read lock is held, stability of the field is guaranteed; to write
168  * to a field, a write lock must generally be held.
169  *
170  * netinet/netinet6-layer code should not assume that the inp_socket pointer
171  * is safe to dereference without inp_lock being held, even for protocols
172  * other than TCP (where the inpcb persists during TIMEWAIT even after the
173  * socket has been freed), or there may be close(2)-related races.
174  *
175  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
176  *
177  * TODO:  Currently only the TCP stack is leveraging the global pcbinfo lock
178  * read-lock usage during modification, this model can be applied to other
179  * protocols (especially SCTP).
180  */
181 struct inpcb {
182         LIST_ENTRY(inpcb) inp_hash;     /* (h/i) hash list */
183         LIST_ENTRY(inpcb) inp_pcbgrouphash;     /* (g/i) hash list */
184         LIST_ENTRY(inpcb) inp_list;     /* (p/l) list for all PCBs for proto */
185                                         /* (p[w]) for list iteration */
186                                         /* (p[r]/l) for addition/removal */
187         void    *inp_ppcb;              /* (i) pointer to per-protocol pcb */
188         struct  inpcbinfo *inp_pcbinfo; /* (c) PCB list info */
189         struct  inpcbgroup *inp_pcbgroup; /* (g/i) PCB group list */
190         LIST_ENTRY(inpcb) inp_pcbgroup_wild; /* (g/i/h) group wildcard entry */
191         struct  socket *inp_socket;     /* (i) back pointer to socket */
192         struct  ucred   *inp_cred;      /* (c) cache of socket cred */
193         u_int32_t inp_flow;             /* (i) IPv6 flow information */
194         int     inp_flags;              /* (i) generic IP/datagram flags */
195         int     inp_flags2;             /* (i) generic IP/datagram flags #2*/
196         u_char  inp_vflag;              /* (i) IP version flag (v4/v6) */
197         u_char  inp_ip_ttl;             /* (i) time to live proto */
198         u_char  inp_ip_p;               /* (c) protocol proto */
199         u_char  inp_ip_minttl;          /* (i) minimum TTL or drop */
200         uint32_t inp_flowid;            /* (x) flow id / queue id */
201         u_int   inp_refcount;           /* (i) refcount */
202         void    *inp_pspare[5];         /* (x) route caching / general use */
203         uint32_t inp_flowtype;          /* (x) M_HASHTYPE value */
204         uint32_t inp_rss_listen_bucket; /* (x) overridden RSS listen bucket */
205         u_int   inp_ispare[4];          /* (x) route caching / user cookie /
206                                          *     general use */
207
208         /* Local and foreign ports, local and foreign addr. */
209         struct  in_conninfo inp_inc;    /* (i) list for PCB's local port */
210
211         /* MAC and IPSEC policy information. */
212         struct  label *inp_label;       /* (i) MAC label */
213         struct  inpcbpolicy *inp_sp;    /* (s) for IPSEC */
214
215         /* Protocol-dependent part; options. */
216         struct {
217                 u_char  inp4_ip_tos;            /* (i) type of service proto */
218                 struct  mbuf *inp4_options;     /* (i) IP options */
219                 struct  ip_moptions *inp4_moptions; /* (i) IP mcast options */
220         } inp_depend4;
221         struct {
222                 /* (i) IP options */
223                 struct  mbuf *inp6_options;
224                 /* (i) IP6 options for outgoing packets */
225                 struct  ip6_pktopts *inp6_outputopts;
226                 /* (i) IP multicast options */
227                 struct  ip6_moptions *inp6_moptions;
228                 /* (i) ICMPv6 code type filter */
229                 struct  icmp6_filter *inp6_icmp6filt;
230                 /* (i) IPV6_CHECKSUM setsockopt */
231                 int     inp6_cksum;
232                 short   inp6_hops;
233         } inp_depend6;
234         LIST_ENTRY(inpcb) inp_portlist; /* (i/h) */
235         struct  inpcbport *inp_phd;     /* (i/h) head of this list */
236 #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
237         inp_gen_t       inp_gencnt;     /* (c) generation count */
238         struct llentry  *inp_lle;       /* cached L2 information */
239         struct rtentry  *inp_rt;        /* cached L3 information */
240         struct rwlock   inp_lock;
241 };
242 #define inp_fport       inp_inc.inc_fport
243 #define inp_lport       inp_inc.inc_lport
244 #define inp_faddr       inp_inc.inc_faddr
245 #define inp_laddr       inp_inc.inc_laddr
246 #define inp_ip_tos      inp_depend4.inp4_ip_tos
247 #define inp_options     inp_depend4.inp4_options
248 #define inp_moptions    inp_depend4.inp4_moptions
249
250 #define in6p_faddr      inp_inc.inc6_faddr
251 #define in6p_laddr      inp_inc.inc6_laddr
252 #define in6p_zoneid     inp_inc.inc6_zoneid
253 #define in6p_hops       inp_depend6.inp6_hops   /* default hop limit */
254 #define in6p_flowinfo   inp_flow
255 #define in6p_options    inp_depend6.inp6_options
256 #define in6p_outputopts inp_depend6.inp6_outputopts
257 #define in6p_moptions   inp_depend6.inp6_moptions
258 #define in6p_icmp6filt  inp_depend6.inp6_icmp6filt
259 #define in6p_cksum      inp_depend6.inp6_cksum
260
261 #define inp_vnet        inp_pcbinfo->ipi_vnet
262
263 /*
264  * The range of the generation count, as used in this implementation, is 9e19.
265  * We would have to create 300 billion connections per second for this number
266  * to roll over in a year.  This seems sufficiently unlikely that we simply
267  * don't concern ourselves with that possibility.
268  */
269
270 /*
271  * Interface exported to userland by various protocols which use inpcbs.  Hack
272  * alert -- only define if struct xsocket is in scope.
273  */
274 #ifdef _SYS_SOCKETVAR_H_
275 struct  xinpcb {
276         size_t  xi_len;         /* length of this structure */
277         struct  inpcb xi_inp;
278         struct  xsocket xi_socket;
279         u_quad_t        xi_alignment_hack;
280 };
281
282 struct  xinpgen {
283         size_t  xig_len;        /* length of this structure */
284         u_int   xig_count;      /* number of PCBs at this time */
285         inp_gen_t xig_gen;      /* generation count at this time */
286         so_gen_t xig_sogen;     /* socket generation count at this time */
287 };
288 #endif /* _SYS_SOCKETVAR_H_ */
289
290 struct inpcbport {
291         LIST_ENTRY(inpcbport) phd_hash;
292         struct inpcbhead phd_pcblist;
293         u_short phd_port;
294 };
295
296 /*-
297  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
298  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
299  *
300  * Each pcbinfo is protected by three locks: ipi_lock, ipi_hash_lock and
301  * ipi_list_lock:
302  *  - ipi_lock covering the global pcb list stability during loop iteration,
303  *  - ipi_hash_lock covering the hashed lookup tables,
304  *  - ipi_list_lock covering mutable global fields (such as the global
305  *    pcb list)
306  *
307  * The lock order is:
308  *
309  *    ipi_lock (before)
310  *        inpcb locks (before)
311  *            ipi_list locks (before)
312  *                {ipi_hash_lock, pcbgroup locks}
313  *
314  * Locking key:
315  *
316  * (c) Constant or nearly constant after initialisation
317  * (g) Locked by ipi_lock
318  * (l) Locked by ipi_list_lock
319  * (h) Read using either ipi_hash_lock or inpcb lock; write requires both
320  * (p) Protected by one or more pcbgroup locks
321  * (x) Synchronisation properties poorly defined
322  */
323 struct inpcbinfo {
324         /*
325          * Global lock protecting full inpcb list traversal
326          */
327         struct rwlock            ipi_lock;
328
329         /*
330          * Global list of inpcbs on the protocol.
331          */
332         struct inpcbhead        *ipi_listhead;          /* (g/l) */
333         u_int                    ipi_count;             /* (l) */
334
335         /*
336          * Generation count -- incremented each time a connection is allocated
337          * or freed.
338          */
339         u_quad_t                 ipi_gencnt;            /* (l) */
340
341         /*
342          * Fields associated with port lookup and allocation.
343          */
344         u_short                  ipi_lastport;          /* (x) */
345         u_short                  ipi_lastlow;           /* (x) */
346         u_short                  ipi_lasthi;            /* (x) */
347
348         /*
349          * UMA zone from which inpcbs are allocated for this protocol.
350          */
351         struct  uma_zone        *ipi_zone;              /* (c) */
352
353         /*
354          * Connection groups associated with this protocol.  These fields are
355          * constant, but pcbgroup structures themselves are protected by
356          * per-pcbgroup locks.
357          */
358         struct inpcbgroup       *ipi_pcbgroups;         /* (c) */
359         u_int                    ipi_npcbgroups;        /* (c) */
360         u_int                    ipi_hashfields;        /* (c) */
361
362         /*
363          * Global lock protecting non-pcbgroup hash lookup tables.
364          */
365         struct rwlock            ipi_hash_lock;
366
367         /*
368          * Global hash of inpcbs, hashed by local and foreign addresses and
369          * port numbers.
370          */
371         struct inpcbhead        *ipi_hashbase;          /* (h) */
372         u_long                   ipi_hashmask;          /* (h) */
373
374         /*
375          * Global hash of inpcbs, hashed by only local port number.
376          */
377         struct inpcbporthead    *ipi_porthashbase;      /* (h) */
378         u_long                   ipi_porthashmask;      /* (h) */
379
380         /*
381          * List of wildcard inpcbs for use with pcbgroups.  In the past, was
382          * per-pcbgroup but is now global.  All pcbgroup locks must be held
383          * to modify the list, so any is sufficient to read it.
384          */
385         struct inpcbhead        *ipi_wildbase;          /* (p) */
386         u_long                   ipi_wildmask;          /* (p) */
387
388         /*
389          * Pointer to network stack instance
390          */
391         struct vnet             *ipi_vnet;              /* (c) */
392
393         /*
394          * general use 2
395          */
396         void                    *ipi_pspare[2];
397
398         /*
399          * Global lock protecting global inpcb list, inpcb count, etc.
400          */
401         struct rwlock            ipi_list_lock;
402 };
403
404 #ifdef _KERNEL
405 /*
406  * Connection groups hold sets of connections that have similar CPU/thread
407  * affinity.  Each connection belongs to exactly one connection group.
408  */
409 struct inpcbgroup {
410         /*
411          * Per-connection group hash of inpcbs, hashed by local and foreign
412          * addresses and port numbers.
413          */
414         struct inpcbhead        *ipg_hashbase;          /* (c) */
415         u_long                   ipg_hashmask;          /* (c) */
416
417         /*
418          * Notional affinity of this pcbgroup.
419          */
420         u_int                    ipg_cpu;               /* (p) */
421
422         /*
423          * Per-connection group lock, not to be confused with ipi_lock.
424          * Protects the hash table hung off the group, but also the global
425          * wildcard list in inpcbinfo.
426          */
427         struct mtx               ipg_lock;
428 } __aligned(CACHE_LINE_SIZE);
429
430 #define INP_LOCK_INIT(inp, d, t) \
431         rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
432 #define INP_LOCK_DESTROY(inp)   rw_destroy(&(inp)->inp_lock)
433 #define INP_RLOCK(inp)          rw_rlock(&(inp)->inp_lock)
434 #define INP_WLOCK(inp)          rw_wlock(&(inp)->inp_lock)
435 #define INP_TRY_RLOCK(inp)      rw_try_rlock(&(inp)->inp_lock)
436 #define INP_TRY_WLOCK(inp)      rw_try_wlock(&(inp)->inp_lock)
437 #define INP_RUNLOCK(inp)        rw_runlock(&(inp)->inp_lock)
438 #define INP_WUNLOCK(inp)        rw_wunlock(&(inp)->inp_lock)
439 #define INP_TRY_UPGRADE(inp)    rw_try_upgrade(&(inp)->inp_lock)
440 #define INP_DOWNGRADE(inp)      rw_downgrade(&(inp)->inp_lock)
441 #define INP_WLOCKED(inp)        rw_wowned(&(inp)->inp_lock)
442 #define INP_LOCK_ASSERT(inp)    rw_assert(&(inp)->inp_lock, RA_LOCKED)
443 #define INP_RLOCK_ASSERT(inp)   rw_assert(&(inp)->inp_lock, RA_RLOCKED)
444 #define INP_WLOCK_ASSERT(inp)   rw_assert(&(inp)->inp_lock, RA_WLOCKED)
445 #define INP_UNLOCK_ASSERT(inp)  rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
446
447 /*
448  * These locking functions are for inpcb consumers outside of sys/netinet,
449  * more specifically, they were added for the benefit of TOE drivers. The
450  * macros are reserved for use by the stack.
451  */
452 void inp_wlock(struct inpcb *);
453 void inp_wunlock(struct inpcb *);
454 void inp_rlock(struct inpcb *);
455 void inp_runlock(struct inpcb *);
456
457 #ifdef INVARIANTS
458 void inp_lock_assert(struct inpcb *);
459 void inp_unlock_assert(struct inpcb *);
460 #else
461 static __inline void
462 inp_lock_assert(struct inpcb *inp __unused)
463 {
464 }
465
466 static __inline void
467 inp_unlock_assert(struct inpcb *inp __unused)
468 {
469 }
470
471 #endif
472
473 void    inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
474 int     inp_ip_tos_get(const struct inpcb *inp);
475 void    inp_ip_tos_set(struct inpcb *inp, int val);
476 struct socket *
477         inp_inpcbtosocket(struct inpcb *inp);
478 struct tcpcb *
479         inp_inpcbtotcpcb(struct inpcb *inp);
480 void    inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
481                 uint32_t *faddr, uint16_t *fp);
482 short   inp_so_options(const struct inpcb *inp);
483
484 #endif /* _KERNEL */
485
486 #define INP_INFO_LOCK_INIT(ipi, d) \
487         rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
488 #define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
489 #define INP_INFO_RLOCK(ipi)     rw_rlock(&(ipi)->ipi_lock)
490 #define INP_INFO_WLOCK(ipi)     rw_wlock(&(ipi)->ipi_lock)
491 #define INP_INFO_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_lock)
492 #define INP_INFO_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_lock)
493 #define INP_INFO_TRY_UPGRADE(ipi)       rw_try_upgrade(&(ipi)->ipi_lock)
494 #define INP_INFO_RUNLOCK(ipi)   rw_runlock(&(ipi)->ipi_lock)
495 #define INP_INFO_WUNLOCK(ipi)   rw_wunlock(&(ipi)->ipi_lock)
496 #define INP_INFO_LOCK_ASSERT(ipi)       rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
497 #define INP_INFO_RLOCK_ASSERT(ipi)      rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
498 #define INP_INFO_WLOCK_ASSERT(ipi)      rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
499 #define INP_INFO_UNLOCK_ASSERT(ipi)     rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
500
501 #define INP_LIST_LOCK_INIT(ipi, d) \
502         rw_init_flags(&(ipi)->ipi_list_lock, (d), 0)
503 #define INP_LIST_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_list_lock)
504 #define INP_LIST_RLOCK(ipi)     rw_rlock(&(ipi)->ipi_list_lock)
505 #define INP_LIST_WLOCK(ipi)     rw_wlock(&(ipi)->ipi_list_lock)
506 #define INP_LIST_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_list_lock)
507 #define INP_LIST_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_list_lock)
508 #define INP_LIST_TRY_UPGRADE(ipi)       rw_try_upgrade(&(ipi)->ipi_list_lock)
509 #define INP_LIST_RUNLOCK(ipi)   rw_runlock(&(ipi)->ipi_list_lock)
510 #define INP_LIST_WUNLOCK(ipi)   rw_wunlock(&(ipi)->ipi_list_lock)
511 #define INP_LIST_LOCK_ASSERT(ipi) \
512         rw_assert(&(ipi)->ipi_list_lock, RA_LOCKED)
513 #define INP_LIST_RLOCK_ASSERT(ipi) \
514         rw_assert(&(ipi)->ipi_list_lock, RA_RLOCKED)
515 #define INP_LIST_WLOCK_ASSERT(ipi) \
516         rw_assert(&(ipi)->ipi_list_lock, RA_WLOCKED)
517 #define INP_LIST_UNLOCK_ASSERT(ipi) \
518         rw_assert(&(ipi)->ipi_list_lock, RA_UNLOCKED)
519
520 #define INP_HASH_LOCK_INIT(ipi, d) \
521         rw_init_flags(&(ipi)->ipi_hash_lock, (d), 0)
522 #define INP_HASH_LOCK_DESTROY(ipi)      rw_destroy(&(ipi)->ipi_hash_lock)
523 #define INP_HASH_RLOCK(ipi)             rw_rlock(&(ipi)->ipi_hash_lock)
524 #define INP_HASH_WLOCK(ipi)             rw_wlock(&(ipi)->ipi_hash_lock)
525 #define INP_HASH_RUNLOCK(ipi)           rw_runlock(&(ipi)->ipi_hash_lock)
526 #define INP_HASH_WUNLOCK(ipi)           rw_wunlock(&(ipi)->ipi_hash_lock)
527 #define INP_HASH_LOCK_ASSERT(ipi)       rw_assert(&(ipi)->ipi_hash_lock, \
528                                             RA_LOCKED)
529 #define INP_HASH_WLOCK_ASSERT(ipi)      rw_assert(&(ipi)->ipi_hash_lock, \
530                                             RA_WLOCKED)
531
532 #define INP_GROUP_LOCK_INIT(ipg, d)     mtx_init(&(ipg)->ipg_lock, (d), NULL, \
533                                             MTX_DEF | MTX_DUPOK)
534 #define INP_GROUP_LOCK_DESTROY(ipg)     mtx_destroy(&(ipg)->ipg_lock)
535
536 #define INP_GROUP_LOCK(ipg)             mtx_lock(&(ipg)->ipg_lock)
537 #define INP_GROUP_LOCK_ASSERT(ipg)      mtx_assert(&(ipg)->ipg_lock, MA_OWNED)
538 #define INP_GROUP_UNLOCK(ipg)           mtx_unlock(&(ipg)->ipg_lock)
539
540 #define INP_PCBHASH(faddr, lport, fport, mask) \
541         (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
542 #define INP_PCBPORTHASH(lport, mask) \
543         (ntohs((lport)) & (mask))
544 #define INP6_PCBHASHKEY(faddr)  ((faddr)->s6_addr32[3])
545
546 /*
547  * Flags for inp_vflags -- historically version flags only
548  */
549 #define INP_IPV4        0x1
550 #define INP_IPV6        0x2
551 #define INP_IPV6PROTO   0x4             /* opened under IPv6 protocol */
552
553 /*
554  * Flags for inp_flags.
555  */
556 #define INP_RECVOPTS            0x00000001 /* receive incoming IP options */
557 #define INP_RECVRETOPTS         0x00000002 /* receive IP options for reply */
558 #define INP_RECVDSTADDR         0x00000004 /* receive IP dst address */
559 #define INP_HDRINCL             0x00000008 /* user supplies entire IP header */
560 #define INP_HIGHPORT            0x00000010 /* user wants "high" port binding */
561 #define INP_LOWPORT             0x00000020 /* user wants "low" port binding */
562 #define INP_ANONPORT            0x00000040 /* port chosen for user */
563 #define INP_RECVIF              0x00000080 /* receive incoming interface */
564 #define INP_MTUDISC             0x00000100 /* user can do MTU discovery */
565                                            /* 0x000200 unused: was INP_FAITH */
566 #define INP_RECVTTL             0x00000400 /* receive incoming IP TTL */
567 #define INP_DONTFRAG            0x00000800 /* don't fragment packet */
568 #define INP_BINDANY             0x00001000 /* allow bind to any address */
569 #define INP_INHASHLIST          0x00002000 /* in_pcbinshash() has been called */
570 #define INP_RECVTOS             0x00004000 /* receive incoming IP TOS */
571 #define IN6P_IPV6_V6ONLY        0x00008000 /* restrict AF_INET6 socket for v6 */
572 #define IN6P_PKTINFO            0x00010000 /* receive IP6 dst and I/F */
573 #define IN6P_HOPLIMIT           0x00020000 /* receive hoplimit */
574 #define IN6P_HOPOPTS            0x00040000 /* receive hop-by-hop options */
575 #define IN6P_DSTOPTS            0x00080000 /* receive dst options after rthdr */
576 #define IN6P_RTHDR              0x00100000 /* receive routing header */
577 #define IN6P_RTHDRDSTOPTS       0x00200000 /* receive dstoptions before rthdr */
578 #define IN6P_TCLASS             0x00400000 /* receive traffic class value */
579 #define IN6P_AUTOFLOWLABEL      0x00800000 /* attach flowlabel automatically */
580 #define INP_TIMEWAIT            0x01000000 /* in TIMEWAIT, ppcb is tcptw */
581 #define INP_ONESBCAST           0x02000000 /* send all-ones broadcast */
582 #define INP_DROPPED             0x04000000 /* protocol drop flag */
583 #define INP_SOCKREF             0x08000000 /* strong socket reference */
584 #define INP_RESERVED_0          0x10000000 /* reserved field */
585 #define INP_RESERVED_1          0x20000000 /* reserved field */
586 #define IN6P_RFC2292            0x40000000 /* used RFC2292 API on the socket */
587 #define IN6P_MTU                0x80000000 /* receive path MTU */
588
589 #define INP_CONTROLOPTS         (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
590                                  INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
591                                  IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
592                                  IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
593                                  IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
594                                  IN6P_MTU)
595
596 /*
597  * Flags for inp_flags2.
598  */
599 #define INP_LLE_VALID           0x00000001 /* cached lle is valid */    
600 #define INP_RT_VALID            0x00000002 /* cached rtentry is valid */
601 #define INP_PCBGROUPWILD        0x00000004 /* in pcbgroup wildcard list */
602 #define INP_REUSEPORT           0x00000008 /* SO_REUSEPORT option is set */
603 #define INP_FREED               0x00000010 /* inp itself is not valid */
604 #define INP_REUSEADDR           0x00000020 /* SO_REUSEADDR option is set */
605 #define INP_BINDMULTI           0x00000040 /* IP_BINDMULTI option is set */
606 #define INP_RSS_BUCKET_SET      0x00000080 /* IP_RSS_LISTEN_BUCKET is set */
607 #define INP_RECVFLOWID          0x00000100 /* populate recv datagram with flow info */
608 #define INP_RECVRSSBUCKETID     0x00000200 /* populate recv datagram with bucket id */
609
610 /*
611  * Flags passed to in_pcblookup*() functions.
612  */
613 #define INPLOOKUP_WILDCARD      0x00000001      /* Allow wildcard sockets. */
614 #define INPLOOKUP_RLOCKPCB      0x00000002      /* Return inpcb read-locked. */
615 #define INPLOOKUP_WLOCKPCB      0x00000004      /* Return inpcb write-locked. */
616
617 #define INPLOOKUP_MASK  (INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
618                             INPLOOKUP_WLOCKPCB)
619
620 #define sotoinpcb(so)   ((struct inpcb *)(so)->so_pcb)
621 #define sotoin6pcb(so)  sotoinpcb(so) /* for KAME src sync over BSD*'s */
622
623 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
624
625 #define INP_CHECK_SOCKAF(so, af)        (INP_SOCKAF(so) == af)
626
627 /*
628  * Constants for pcbinfo.ipi_hashfields.
629  */
630 #define IPI_HASHFIELDS_NONE     0
631 #define IPI_HASHFIELDS_2TUPLE   1
632 #define IPI_HASHFIELDS_4TUPLE   2
633
634 #ifdef _KERNEL
635 VNET_DECLARE(int, ipport_reservedhigh);
636 VNET_DECLARE(int, ipport_reservedlow);
637 VNET_DECLARE(int, ipport_lowfirstauto);
638 VNET_DECLARE(int, ipport_lowlastauto);
639 VNET_DECLARE(int, ipport_firstauto);
640 VNET_DECLARE(int, ipport_lastauto);
641 VNET_DECLARE(int, ipport_hifirstauto);
642 VNET_DECLARE(int, ipport_hilastauto);
643 VNET_DECLARE(int, ipport_randomized);
644 VNET_DECLARE(int, ipport_randomcps);
645 VNET_DECLARE(int, ipport_randomtime);
646 VNET_DECLARE(int, ipport_stoprandom);
647 VNET_DECLARE(int, ipport_tcpallocs);
648
649 #define V_ipport_reservedhigh   VNET(ipport_reservedhigh)
650 #define V_ipport_reservedlow    VNET(ipport_reservedlow)
651 #define V_ipport_lowfirstauto   VNET(ipport_lowfirstauto)
652 #define V_ipport_lowlastauto    VNET(ipport_lowlastauto)
653 #define V_ipport_firstauto      VNET(ipport_firstauto)
654 #define V_ipport_lastauto       VNET(ipport_lastauto)
655 #define V_ipport_hifirstauto    VNET(ipport_hifirstauto)
656 #define V_ipport_hilastauto     VNET(ipport_hilastauto)
657 #define V_ipport_randomized     VNET(ipport_randomized)
658 #define V_ipport_randomcps      VNET(ipport_randomcps)
659 #define V_ipport_randomtime     VNET(ipport_randomtime)
660 #define V_ipport_stoprandom     VNET(ipport_stoprandom)
661 #define V_ipport_tcpallocs      VNET(ipport_tcpallocs)
662
663 void    in_pcbinfo_destroy(struct inpcbinfo *);
664 void    in_pcbinfo_init(struct inpcbinfo *, const char *, struct inpcbhead *,
665             int, int, char *, uma_init, uma_fini, uint32_t, u_int);
666
667 int     in_pcbbind_check_bindmulti(const struct inpcb *ni,
668             const struct inpcb *oi);
669
670 struct inpcbgroup *
671         in_pcbgroup_byhash(struct inpcbinfo *, u_int, uint32_t);
672 struct inpcbgroup *
673         in_pcbgroup_byinpcb(struct inpcb *);
674 struct inpcbgroup *
675         in_pcbgroup_bytuple(struct inpcbinfo *, struct in_addr, u_short,
676             struct in_addr, u_short);
677 void    in_pcbgroup_destroy(struct inpcbinfo *);
678 int     in_pcbgroup_enabled(struct inpcbinfo *);
679 void    in_pcbgroup_init(struct inpcbinfo *, u_int, int);
680 void    in_pcbgroup_remove(struct inpcb *);
681 void    in_pcbgroup_update(struct inpcb *);
682 void    in_pcbgroup_update_mbuf(struct inpcb *, struct mbuf *);
683
684 void    in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
685 int     in_pcballoc(struct socket *, struct inpcbinfo *);
686 int     in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
687 int     in_pcb_lport(struct inpcb *, struct in_addr *, u_short *,
688             struct ucred *, int);
689 int     in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
690             u_short *, struct ucred *);
691 int     in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
692 int     in_pcbconnect_mbuf(struct inpcb *, struct sockaddr *, struct ucred *,
693             struct mbuf *);
694 int     in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
695             u_short *, in_addr_t *, u_short *, struct inpcb **,
696             struct ucred *);
697 void    in_pcbdetach(struct inpcb *);
698 void    in_pcbdisconnect(struct inpcb *);
699 void    in_pcbdrop(struct inpcb *);
700 void    in_pcbfree(struct inpcb *);
701 int     in_pcbinshash(struct inpcb *);
702 int     in_pcbinshash_nopcbgroup(struct inpcb *);
703 int     in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
704             struct ucred *);
705 struct inpcb *
706         in_pcblookup_local(struct inpcbinfo *,
707             struct in_addr, u_short, int, struct ucred *);
708 struct inpcb *
709         in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
710             struct in_addr, u_int, int, struct ifnet *);
711 struct inpcb *
712         in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
713             struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
714 void    in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
715             int, struct inpcb *(*)(struct inpcb *, int));
716 void    in_pcbref(struct inpcb *);
717 void    in_pcbrehash(struct inpcb *);
718 void    in_pcbrehash_mbuf(struct inpcb *, struct mbuf *);
719 int     in_pcbrele(struct inpcb *);
720 int     in_pcbrele_rlocked(struct inpcb *);
721 int     in_pcbrele_wlocked(struct inpcb *);
722 void    in_pcbsetsolabel(struct socket *so);
723 int     in_getpeeraddr(struct socket *so, struct sockaddr **nam);
724 int     in_getsockaddr(struct socket *so, struct sockaddr **nam);
725 struct sockaddr *
726         in_sockaddr(in_port_t port, struct in_addr *addr);
727 void    in_pcbsosetlabel(struct socket *so);
728 #endif /* _KERNEL */
729
730 #endif /* !_NETINET_IN_PCB_H_ */