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