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