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