in_pcb.h 28.7 KB
Newer Older
1
/*-
2
3
 * SPDX-License-Identifier: BSD-3-Clause
 *
Rodney W. Grimes's avatar
Rodney W. Grimes committed
4
 * Copyright (c) 1982, 1986, 1990, 1993
5
 *	The Regents of the University of California.
6
 * Copyright (c) 2010-2011 Juniper Networks, Inc.
7
 * All rights reserved.
Rodney W. Grimes's avatar
Rodney W. Grimes committed
8
 *
9
10
11
 * Portions of this software were developed by Robert N. M. Watson under
 * contract to Juniper Networks, Inc.
 *
Rodney W. Grimes's avatar
Rodney W. Grimes committed
12
13
14
15
16
17
18
19
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
Warner Losh's avatar
Warner Losh committed
20
 * 3. Neither the name of the University nor the names of its contributors
Rodney W. Grimes's avatar
Rodney W. Grimes committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
Peter Wemm's avatar
Peter Wemm committed
37
 * $FreeBSD$
Rodney W. Grimes's avatar
Rodney W. Grimes committed
38
39
 */

Paul Richards's avatar
Paul Richards committed
40
41
42
#ifndef _NETINET_IN_PCB_H_
#define _NETINET_IN_PCB_H_

43
#include <sys/queue.h>
44
#include <sys/epoch.h>
45
46
#include <sys/_lock.h>
#include <sys/_mutex.h>
47
#include <sys/_rwlock.h>
48
#include <net/route.h>
49

50
#ifdef _KERNEL
51
#include <sys/lock.h>
Gleb Smirnoff's avatar
Gleb Smirnoff committed
52
#include <sys/proc.h>
53
#include <sys/rwlock.h>
Gleb Smirnoff's avatar
Gleb Smirnoff committed
54
#include <sys/smr.h>
55
#include <net/vnet.h>
56
#include <vm/uma.h>
57
#endif
58
#include <sys/ck.h>
59

Rodney W. Grimes's avatar
Rodney W. Grimes committed
60
/*
Robert Watson's avatar
Robert Watson committed
61
62
 * struct inpcb is the common protocol control block structure used in most
 * IP transport protocols.
63
64
65
66
 *
 * Pointers to local and foreign host table entries, local and foreign socket
 * numbers, and pointers up (to a socket structure) and down (to a
 * protocol-specific control block) are stored here.
Rodney W. Grimes's avatar
Rodney W. Grimes committed
67
 */
68
69
CK_LIST_HEAD(inpcbhead, inpcb);
CK_LIST_HEAD(inpcbporthead, inpcbport);
70
CK_LIST_HEAD(inpcblbgrouphead, inpcblbgroup);
71
typedef	uint64_t	inp_gen_t;
72

73
74
/*
 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
75
76
 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
 * the following structure.
77
78
79
80
81
82
 */
struct in_addr_4in6 {
	u_int32_t	ia46_pad32[3];
	struct	in_addr	ia46_addr4;
};

83
84
85
86
87
union in_dependaddr {
	struct in_addr_4in6 id46_addr;
	struct in6_addr	id6_addr;
};

88
/*
89
90
 * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
 * some extra padding to accomplish this.
91
92
 * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
 * lport, faddr to generate hash, so these fields shouldn't be moved.
93
94
95
96
97
 */
struct in_endpoints {
	u_int16_t	ie_fport;		/* foreign port */
	u_int16_t	ie_lport;		/* local port */
	/* protocol dependent part, local and foreign addr */
98
99
100
101
102
103
	union in_dependaddr ie_dependfaddr;	/* foreign host table entry */
	union in_dependaddr ie_dependladdr;	/* local host table entry */
#define	ie_faddr	ie_dependfaddr.id46_addr.ia46_addr4
#define	ie_laddr	ie_dependladdr.id46_addr.ia46_addr4
#define	ie6_faddr	ie_dependfaddr.id6_addr
#define	ie6_laddr	ie_dependladdr.id6_addr
104
	u_int32_t	ie6_zoneid;		/* scope zone id */
105
};
106
107

/*
108
109
 * XXX The defines for inc_* are hacks and should be changed to direct
 * references.
110
111
112
113
 */
struct in_conninfo {
	u_int8_t	inc_flags;
	u_int8_t	inc_len;
114
	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
115
	/* protocol dependent part */
116
117
	struct	in_endpoints inc_ie;
};
118
119
120
121
122

/*
 * Flags for inc_flags.
 */
#define	INC_ISIPV6	0x01
123
#define	INC_IPV6MINMTU	0x02
124

125
126
127
128
129
130
#define	inc_fport	inc_ie.ie_fport
#define	inc_lport	inc_ie.ie_lport
#define	inc_faddr	inc_ie.ie_faddr
#define	inc_laddr	inc_ie.ie_laddr
#define	inc6_faddr	inc_ie.ie6_faddr
#define	inc6_laddr	inc_ie.ie6_laddr
131
#define	inc6_zoneid	inc_ie.ie6_zoneid
132

133
134
#if defined(_KERNEL) || defined(_WANT_INPCB)
/*
135
136
 * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
 * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
137
 * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
Gleb Smirnoff's avatar
Gleb Smirnoff committed
138
 * are static after creation or protected by a per-inpcb rwlock, inp_lock.
139
 *
Gleb Smirnoff's avatar
Gleb Smirnoff committed
140
141
142
143
 * A inpcb database is indexed by addresses/ports hash as well as list of
 * all pcbs that belong to a certain proto. Database lookups or list traversals
 * are be performed inside SMR section. Once desired PCB is found its own
 * lock is to be obtained and SMR section exited.
144
145
 *
 * Key:
146
 * (b) - Protected by the hpts lock.
147
 * (c) - Constant after initialization
Gleb Smirnoff's avatar
Gleb Smirnoff committed
148
 * (e) - Protected by the SMR section
149
150
 * (i) - Protected by the inpcb lock
 * (p) - Protected by the pcbinfo lock for the inpcb
151
 * (h) - Protected by the pcbhash lock for the inpcb
152
153
 * (s) - Protected by another subsystem's locks
 * (x) - Undefined locking
154
 *
155
 * Notes on the tcp_hpts:
156
 *
157
158
 * First Hpts lock order is
 * 1) INP_WLOCK()
159
 * 2) HPTS_LOCK() i.e. hpts->pmtx
160
 *
161
162
163
 * To insert a TCB on the hpts you *must* be holding the INP_WLOCK().
 * You may check the inp->inp_in_hpts flag without the hpts lock.
 * The hpts is the only one that will clear this flag holding
164
 * only the hpts lock. This means that in your tcp_output()
165
166
167
 * routine when you test for the inp_in_hpts flag to be 1
 * it may be transitioning to 0 (by the hpts).
 * That's ok since that will just mean an extra call to tcp_output
168
 * that most likely will find the call you executed
169
 * (when the mis-match occured) will have put the TCB back
170
171
172
173
174
175
176
177
178
179
 * on the hpts and it will return. If your
 * call did not add the inp back to the hpts then you will either
 * over-send or the cwnd will block you from sending more.
 *
 * Note you should also be holding the INP_WLOCK() when you
 * call the remove from the hpts as well. Though usually
 * you are either doing this from a timer, where you need and have
 * the INP_WLOCK() or from destroying your TCB where again
 * you should already have the INP_WLOCK().
 *
180
 * The inp_hpts_cpu, inp_hpts_cpu_set, inp_input_cpu and
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
 * inp_input_cpu_set fields are controlled completely by
 * the hpts. Do not ever set these. The inp_hpts_cpu_set
 * and inp_input_cpu_set fields indicate if the hpts has
 * setup the respective cpu field. It is advised if this
 * field is 0, to enqueue the packet with the appropriate
 * hpts_immediate() call. If the _set field is 1, then
 * you may compare the inp_*_cpu field to the curcpu and
 * may want to again insert onto the hpts if these fields
 * are not equal (i.e. you are not on the expected CPU).
 *
 * A note on inp_hpts_calls and inp_input_calls, these
 * flags are set when the hpts calls either the output
 * or do_segment routines respectively. If the routine
 * being called wants to use this, then it needs to
 * clear the flag before returning. The hpts will not
 * clear the flag. The flags can be used to tell if
 * the hpts is the function calling the respective
 * routine.
199
200
201
202
203
204
205
206
207
208
209
210
211
 *
 * A few other notes:
 *
 * When a read lock is held, stability of the field is guaranteed; to write
 * to a field, a write lock must generally be held.
 *
 * netinet/netinet6-layer code should not assume that the inp_socket pointer
 * is safe to dereference without inp_lock being held, even for protocols
 * other than TCP (where the inpcb persists during TIMEWAIT even after the
 * socket has been freed), or there may be close(2)-related races.
 *
 * The inp_vflag field is overloaded, and would otherwise ideally be (c).
 */
212
213
struct icmp6_filter;
struct inpcbpolicy;
214
struct m_snd_tag;
Rodney W. Grimes's avatar
Rodney W. Grimes committed
215
struct inpcb {
216
	/* Cache line #1 (amd64) */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
217
	CK_LIST_ENTRY(inpcb) inp_hash;	/* (w:h/r:e)  hash list */
218
219
	struct rwlock	inp_lock;
	/* Cache line #2 (amd64) */
220
#define	inp_start_zero	inp_hpts
221
222
#define	inp_zero_size	(sizeof(struct inpcb) - \
			    offsetof(struct inpcb, inp_start_zero))
223
224
225
226
227
228
	TAILQ_ENTRY(inpcb) inp_hpts;	/* pacing out queue next lock(b) */

	uint32_t inp_hpts_request;	/* Current hpts request, zero if
					 * fits in the pacing window (i&b). */
	/*
	 * Note the next fields are protected by a
229
	 * different lock (hpts-lock). This means that
230
231
232
233
234
235
	 * they must correspond in size to the smallest
	 * protectable bit field (uint8_t on x86, and
	 * other platfomrs potentially uint32_t?). Also
	 * since CPU switches can occur at different times the two
	 * fields can *not* be collapsed into a signal bit field.
	 */
236
#if defined(__amd64__) || defined(__i386__)
237
238
	uint8_t inp_in_hpts; /* on output hpts (lock b) */
	uint8_t inp_in_dropq; /* on input hpts (lock b) */
239
#else
240
241
	uint32_t inp_in_hpts; /* on output hpts (lock b) */
	uint32_t inp_in_dropq; /* on input hpts (lock b) */
242
243
#endif
	volatile uint16_t  inp_hpts_cpu; /* Lock (i) */
244
	volatile uint16_t  inp_irq_cpu;	/* Set by LRO in behalf of or the driver */
245
246
247
	u_int	inp_refcount;		/* (i) refcount */
	int	inp_flags;		/* (i) generic IP/datagram flags */
	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
248
249
250
	uint16_t  inp_dropq_cpu; /* Lock (i) */
	uint8_t inp_hpts_cpu_set :1,  /* on output hpts (i) */
			 inp_dropq_cpu_set : 1,	/* on input hpts (i) */
251
			 inp_hpts_calls :1,	/* (i) from output hpts */
252
253
			 inp_irq_cpu_set :1,	/* (i) from LRO/Driver */
			 inp_spare_bits2 : 3;
254
	uint8_t inp_numa_domain;	/* numa domain */
255
	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
256
	struct	socket *inp_socket;	/* (i) back pointer to socket */
257
258
	uint32_t 	 inp_hptsslot;	/* Hpts wheel slot this tcb is Lock(i&b) */
	uint32_t         inp_hpts_drop_reas;	/* reason we are dropping the PCB (lock i&b) */
259
260
	uint32_t	inp_dropq_gencnt;
	TAILQ_ENTRY(inpcb) inp_dropq;	/* hpts drop queue next lock(b) */
261
	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
262
	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
263
	u_int32_t inp_flow;		/* (i) IPv6 flow information */
264
265
266
267
	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
	u_char	inp_ip_ttl;		/* (i) time to live proto */
	u_char	inp_ip_p;		/* (c) protocol proto */
	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
268
	uint32_t inp_flowid;		/* (x) flow id / queue id */
269
	struct m_snd_tag *inp_snd_tag;	/* (i) send tag for outgoing mbufs */
Adrian Chadd's avatar
Adrian Chadd committed
270
	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
271
	uint32_t inp_rss_listen_bucket;	/* (x) overridden RSS listen bucket */
272
273

	/* Local and foreign ports, local and foreign addr. */
274
	struct	in_conninfo inp_inc;	/* (i) list for PCB's local port */
275

276
	/* MAC and IPSEC policy information. */
277
278
	struct	label *inp_label;	/* (i) MAC label */
	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
279

280
	/* Protocol-dependent part; options. */
281
	struct {
282
283
284
285
		u_char	inp_ip_tos;		/* (i) type of service proto */
		struct mbuf		*inp_options;	/* (i) IP options */
		struct ip_moptions	*inp_moptions;	/* (i) mcast options */
	};
286
	struct {
287
		/* (i) IP options */
288
		struct mbuf		*in6p_options;
289
		/* (i) IP6 options for outgoing packets */
290
		struct ip6_pktopts	*in6p_outputopts;
291
		/* (i) IP multicast options */
292
		struct ip6_moptions	*in6p_moptions;
293
		/* (i) ICMPv6 code type filter */
294
		struct icmp6_filter	*in6p_icmp6filt;
295
		/* (i) IPV6_CHECKSUM setsockopt */
296
297
298
		int	in6p_cksum;
		short	in6p_hops;
	};
Gleb Smirnoff's avatar
Gleb Smirnoff committed
299
300
	CK_LIST_ENTRY(inpcb) inp_portlist;	/* (r:e/w:h) port list */
	struct	inpcbport *inp_phd;	/* (r:e/w:h) head of this list */
301
	inp_gen_t	inp_gencnt;	/* (c) generation count */
302
	void		*spare_ptr;	/* Spare pointer. */
303
304
	rt_gen_t	inp_rt_cookie;	/* generation for route entry */
	union {				/* cached L3 information */
305
306
307
		struct route inp_route;
		struct route_in6 inp_route6;
	};
Gleb Smirnoff's avatar
Gleb Smirnoff committed
308
	CK_LIST_ENTRY(inpcb) inp_list;	/* (r:e/w:p) all PCBs for proto */
309
};
310
311
#endif	/* _KERNEL */

312
313
314
315
#define	inp_fport	inp_inc.inc_fport
#define	inp_lport	inp_inc.inc_lport
#define	inp_faddr	inp_inc.inc_faddr
#define	inp_laddr	inp_inc.inc_laddr
Jeffrey Hsu's avatar
Jeffrey Hsu committed
316

317
318
#define	in6p_faddr	inp_inc.inc6_faddr
#define	in6p_laddr	inp_inc.inc6_laddr
319
#define	in6p_zoneid	inp_inc.inc6_zoneid
320

321
322
#define	inp_vnet	inp_pcbinfo->ipi_vnet

323
/*
324
325
326
327
 * The range of the generation count, as used in this implementation, is 9e19.
 * We would have to create 300 billion connections per second for this number
 * to roll over in a year.  This seems sufficiently unlikely that we simply
 * don't concern ourselves with that possibility.
328
 */
329

330
/*
331
332
 * Interface exported to userland by various protocols which use inpcbs.  Hack
 * alert -- only define if struct xsocket is in scope.
333
334
335
336
337
338
339
340
 * Fields prefixed with "xi_" are unique to this structure, and the rest
 * match fields in the struct inpcb, to ease coding and porting.
 *
 * Legend:
 * (s) - used by userland utilities in src
 * (p) - used by utilities in ports
 * (3) - is known to be used by third party software not in ports
 * (n) - no known usage
341
342
 */
#ifdef _SYS_SOCKETVAR_H_
343
struct xinpcb {
344
	ksize_t		xi_len;			/* length of this structure */
345
346
347
	struct xsocket	xi_socket;		/* (s,p) */
	struct in_conninfo inp_inc;		/* (s,p) */
	uint64_t	inp_gencnt;		/* (s,p) */
348
	kvaddr_t	inp_ppcb;		/* (s) netstat(1) */
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
	int64_t		inp_spare64[4];
	uint32_t	inp_flow;		/* (s) */
	uint32_t	inp_flowid;		/* (s) */
	uint32_t	inp_flowtype;		/* (s) */
	int32_t		inp_flags;		/* (s,p) */
	int32_t		inp_flags2;		/* (s) */
	int32_t		inp_rss_listen_bucket;	/* (n) */
	int32_t		in6p_cksum;		/* (n) */
	int32_t		inp_spare32[4];
	uint16_t	in6p_hops;		/* (n) */
	uint8_t		inp_ip_tos;		/* (n) */
	int8_t		pad8;
	uint8_t		inp_vflag;		/* (s,p) */
	uint8_t		inp_ip_ttl;		/* (n) */
	uint8_t		inp_ip_p;		/* (n) */
	uint8_t		inp_ip_minttl;		/* (n) */
	int8_t		inp_spare8[4];
} __aligned(8);
367

368
struct xinpgen {
369
	ksize_t	xig_len;	/* length of this structure */
370
	u_int		xig_count;	/* number of PCBs at this time */
371
	uint32_t	_xig_spare32;
372
373
	inp_gen_t	xig_gen;	/* generation count at this time */
	so_gen_t	xig_sogen;	/* socket generation count this time */
374
	uint64_t	_xig_spare64[4];
375
} __aligned(8);
376
377
378
#ifdef	_KERNEL
void	in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *);
#endif
379
380
#endif /* _SYS_SOCKETVAR_H_ */

Gleb Smirnoff's avatar
Gleb Smirnoff committed
381
382
#ifdef _KERNEL
/*
383
384
 * Global data structure for each high-level protocol (UDP, TCP, ...) in both
 * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
385
 *
Gleb Smirnoff's avatar
Gleb Smirnoff committed
386
387
388
389
 * The pcbs are protected with SMR section and thus all lists in inpcbinfo
 * are CK-lists.  Locking is required to insert a pcb into database. Two
 * locks are provided: one for the hash and one for the global list of pcbs,
 * as well as overall count and generation count.
390
391
392
393
 *
 * Locking key:
 *
 * (c) Constant or nearly constant after initialisation
Gleb Smirnoff's avatar
Gleb Smirnoff committed
394
 * (e) Protected by SMR section
395
 * (g) Locked by ipi_lock
Gleb Smirnoff's avatar
Gleb Smirnoff committed
396
 * (h) Locked by ipi_hash_lock
397
398
399
 */
struct inpcbinfo {
	/*
400
	 * Global lock protecting inpcb list modification
401
	 */
402
	struct mtx		 ipi_lock;
Gleb Smirnoff's avatar
Gleb Smirnoff committed
403
404
	struct inpcbhead	 ipi_listhead;		/* (r:e/w:g) */
	u_int			 ipi_count;		/* (g) */
405
406

	/*
407
408
	 * Generation count -- incremented each time a connection is allocated
	 * or freed.
409
	 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
410
	u_quad_t		 ipi_gencnt;		/* (g) */
411
412
413
414

	/*
	 * Fields associated with port lookup and allocation.
	 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
415
416
417
	u_short			 ipi_lastport;		/* (h) */
	u_short			 ipi_lastlow;		/* (h) */
	u_short			 ipi_lasthi;		/* (h) */
418
419
420
421

	/*
	 * UMA zone from which inpcbs are allocated for this protocol.
	 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
422
423
424
	uma_zone_t		 ipi_zone;		/* (c) */
	uma_zone_t		 ipi_portzone;		/* (c) */
	smr_t			 ipi_smr;		/* (c) */
425

426
	/*
427
428
	 * Global hash of inpcbs, hashed by local and foreign addresses and
	 * port numbers.
429
	 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
430
431
432
	struct mtx		 ipi_hash_lock;
	struct inpcbhead 	*ipi_hashbase;		/* (r:e/w:h) */
	u_long			 ipi_hashmask;		/* (c) */
433
434
435
436

	/*
	 * Global hash of inpcbs, hashed by only local port number.
	 */
437
438
	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
	u_long			 ipi_porthashmask;	/* (h) */
439

440
441
442
443
	/*
	 * Load balance groups used for the SO_REUSEPORT_LB option,
	 * hashed by local port.
	 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
444
	struct	inpcblbgrouphead *ipi_lbgrouphashbase;	/* (r:e/w:h) */
445
446
	u_long			 ipi_lbgrouphashmask;	/* (h) */

447
	/*
448
449
	 * Pointer to network stack instance
	 */
450
	struct vnet		*ipi_vnet;		/* (c) */
451
452
};

453
454
455
456
457
458
459
/*
 * Load balance groups used for the SO_REUSEPORT_LB socket option. Each group
 * (or unique address:port combination) can be re-used at most
 * INPCBLBGROUP_SIZMAX (256) times. The inpcbs are stored in il_inp which
 * is dynamically resized as processes bind/unbind to that specific group.
 */
struct inpcblbgroup {
460
461
	CK_LIST_ENTRY(inpcblbgroup) il_list;
	struct epoch_context il_epoch_ctx;
462
463
	uint16_t	il_lport;			/* (c) */
	u_char		il_vflag;			/* (c) */
464
	u_int8_t		il_numa_domain;
465
466
467
468
469
470
471
472
473
	uint32_t	il_pad2;
	union in_dependaddr il_dependladdr;		/* (c) */
#define	il_laddr	il_dependladdr.id46_addr.ia46_addr4
#define	il6_laddr	il_dependladdr.id6_addr
	uint32_t	il_inpsiz; /* max count in il_inp[] (h) */
	uint32_t	il_inpcnt; /* cur count in il_inp[] (h) */
	struct inpcb	*il_inp[];			/* (h) */
};

474
#define INP_LOCK_INIT(inp, d, t) \
Gleb Smirnoff's avatar
Gleb Smirnoff committed
475
	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE | RW_DUPOK)
476
477
478
#define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
#define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
#define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
479
480
#define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
#define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
481
482
#define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
#define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
483
#define INP_UNLOCK(inp)		rw_unlock(&(inp)->inp_lock)
484
485
486
487
#define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
#define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
#define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
#define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
488
489
490
#define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
#define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
#define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
Jeffrey Hsu's avatar
Jeffrey Hsu committed
491

492
/*
Robert Watson's avatar
Robert Watson committed
493
 * These locking functions are for inpcb consumers outside of sys/netinet,
494
495
496
497
498
499
500
501
 * more specifically, they were added for the benefit of TOE drivers. The
 * macros are reserved for use by the stack.
 */
void inp_wlock(struct inpcb *);
void inp_wunlock(struct inpcb *);
void inp_rlock(struct inpcb *);
void inp_runlock(struct inpcb *);

502
#ifdef INVARIANT_SUPPORT
503
504
void inp_lock_assert(struct inpcb *);
void inp_unlock_assert(struct inpcb *);
505
#else
506
507
#define	inp_lock_assert(inp)	do {} while (0)
#define	inp_unlock_assert(inp)	do {} while (0)
508
#endif
509

510
511
512
513
514
515
516
void	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
int 	inp_ip_tos_get(const struct inpcb *inp);
void 	inp_ip_tos_set(struct inpcb *inp, int val);
struct socket *
	inp_inpcbtosocket(struct inpcb *inp);
struct tcpcb *
	inp_inpcbtotcpcb(struct inpcb *inp);
Robert Watson's avatar
Robert Watson committed
517
void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
518
		uint32_t *faddr, uint16_t *fp);
519
int	inp_so_options(const struct inpcb *inp);
520

521
522
#endif /* _KERNEL */

Gleb Smirnoff's avatar
Gleb Smirnoff committed
523
#define INP_INFO_WLOCK(ipi)	mtx_lock(&(ipi)->ipi_lock)
524
525
#define INP_INFO_WLOCKED(ipi)	mtx_owned(&(ipi)->ipi_lock)
#define INP_INFO_WUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_lock)
Gleb Smirnoff's avatar
Gleb Smirnoff committed
526
527
#define	INP_INFO_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
					mtx_owned(&(ipi)->ipi_lock))
528
#define INP_INFO_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_lock, MA_OWNED)
529
#define INP_INFO_WUNLOCK_ASSERT(ipi)	\
Gleb Smirnoff's avatar
Gleb Smirnoff committed
530
531
				mtx_assert(&(ipi)->ipi_lock, MA_NOTOWNED)

532
533
#define	INP_HASH_WLOCK(ipi)		mtx_lock(&(ipi)->ipi_hash_lock)
#define	INP_HASH_WUNLOCK(ipi)		mtx_unlock(&(ipi)->ipi_hash_lock)
Gleb Smirnoff's avatar
Gleb Smirnoff committed
534
535
536
537
#define	INP_HASH_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
					mtx_owned(&(ipi)->ipi_hash_lock))
#define	INP_HASH_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_hash_lock, \
					MA_OWNED)
538

539
#define INP_PCBHASH(faddr, lport, fport, mask) \
540
541
542
	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
#define INP_PCBPORTHASH(lport, mask) \
	(ntohs((lport)) & (mask))
543
544
#define	INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) \
	((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport)))
545
#define	INP6_PCBHASHKEY(faddr)	((faddr)->s6_addr32[3])
546

547
/*
548
 * Flags for inp_vflags -- historically version flags only
549
550
551
552
553
554
 */
#define	INP_IPV4	0x1
#define	INP_IPV6	0x2
#define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */

/*
555
 * Flags for inp_flags.
556
 */
557
558
559
560
561
562
563
564
565
#define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
#define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
#define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
#define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
#define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
#define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
#define	INP_ANONPORT		0x00000040 /* port chosen for user */
#define	INP_RECVIF		0x00000080 /* receive incoming interface */
#define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
566
/*	INP_FREED		0x00000200 private to in_pcb.c */
567
568
#define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
#define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
569
#define	INP_BINDANY		0x00001000 /* allow bind to any address */
570
#define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
571
#define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
572
573
574
575
576
577
578
579
580
581
582
583
584
#define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
#define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
#define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
#define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
#define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
#define	IN6P_RTHDR		0x00100000 /* receive routing header */
#define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
#define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
#define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
#define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
#define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
#define	INP_DROPPED		0x04000000 /* protocol drop flag */
#define	INP_SOCKREF		0x08000000 /* strong socket reference */
585
586
#define	INP_RESERVED_0          0x10000000 /* reserved field */
#define	INP_RESERVED_1          0x20000000 /* reserved field */
Hajimu UMEMOTO's avatar
Hajimu UMEMOTO committed
587
588
#define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
#define	IN6P_MTU		0x80000000 /* receive path MTU */
Hajimu UMEMOTO's avatar
Hajimu UMEMOTO committed
589

590
#define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
591
				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
Hajimu UMEMOTO's avatar
Hajimu UMEMOTO committed
592
593
				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
594
595
				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
				 IN6P_MTU)
596

597
598
599
/*
 * Flags for inp_flags2.
 */
600
601
#define	INP_MBUF_L_ACKS		0x00000001 /* We need large mbufs for ack compression */
#define	INP_MBUF_ACKCMP		0x00000002 /* TCP mbuf ack compression ok */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
602
/*				0x00000004 */
603
#define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
604
/*				0x00000010 */
605
#define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
606
607
#define	INP_BINDMULTI		0x00000040 /* IP_BINDMULTI option is set */
#define	INP_RSS_BUCKET_SET	0x00000080 /* IP_RSS_LISTEN_BUCKET is set */
608
609
#define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
#define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
610
#define	INP_RATE_LIMIT_CHANGED	0x00000400 /* rate limit needs attention */
611
#define	INP_ORIGDSTADDR		0x00000800 /* receive IP dst address/port */
612
#define INP_CANNOT_DO_ECN	0x00001000 /* The stack does not do ECN */
613
#define	INP_REUSEPORT_LB	0x00002000 /* SO_REUSEPORT_LB option is set */
614
615
616
#define INP_SUPPORTS_MBUFQ	0x00004000 /* Supports the mbuf queue method of LRO */
#define INP_MBUF_QUEUE_READY	0x00008000 /* The transport is pacing, inputs can be queued */
#define INP_DONT_SACK_QUEUE	0x00010000 /* If a sack arrives do not wake me */
617
618
619
620
621
622
623
#define INP_2PCP_SET		0x00020000 /* If the Eth PCP should be set explicitly */
#define INP_2PCP_BIT0		0x00040000 /* Eth PCP Bit 0 */
#define INP_2PCP_BIT1		0x00080000 /* Eth PCP Bit 1 */
#define INP_2PCP_BIT2		0x00100000 /* Eth PCP Bit 2 */
#define INP_2PCP_BASE	INP_2PCP_BIT0
#define INP_2PCP_MASK	(INP_2PCP_BIT0 | INP_2PCP_BIT1 | INP_2PCP_BIT2)
#define INP_2PCP_SHIFT		18         /* shift PCP field in/out of inp_flags2 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
624

625
/*
Gleb Smirnoff's avatar
Gleb Smirnoff committed
626
 * Flags passed to in_pcblookup*(), inp_smr_lock() and inp_next().
627
 */
Gleb Smirnoff's avatar
Gleb Smirnoff committed
628
629
630
631
632
typedef	enum {
	INPLOOKUP_WILDCARD = 0x00000001,	/* Allow wildcard sockets. */
	INPLOOKUP_RLOCKPCB = 0x00000002,	/* Return inpcb read-locked. */
	INPLOOKUP_WLOCKPCB = 0x00000004,	/* Return inpcb write-locked. */
} inp_lookup_t;
633
634

#define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
635
	    INPLOOKUP_WLOCKPCB)
Gleb Smirnoff's avatar
Gleb Smirnoff committed
636
#define	INPLOOKUP_LOCKMASK	(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)
637

Rodney W. Grimes's avatar
Rodney W. Grimes committed
638
#define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
639
640
641

#define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family

642
#define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
Rodney W. Grimes's avatar
Rodney W. Grimes committed
643

644
#ifdef _KERNEL
645
646
647
648
649
650
651
652
653
654
655
656
657
658
VNET_DECLARE(int, ipport_reservedhigh);
VNET_DECLARE(int, ipport_reservedlow);
VNET_DECLARE(int, ipport_lowfirstauto);
VNET_DECLARE(int, ipport_lowlastauto);
VNET_DECLARE(int, ipport_firstauto);
VNET_DECLARE(int, ipport_lastauto);
VNET_DECLARE(int, ipport_hifirstauto);
VNET_DECLARE(int, ipport_hilastauto);
VNET_DECLARE(int, ipport_randomized);
VNET_DECLARE(int, ipport_randomcps);
VNET_DECLARE(int, ipport_randomtime);
VNET_DECLARE(int, ipport_stoprandom);
VNET_DECLARE(int, ipport_tcpallocs);

659
660
661
662
663
664
665
666
667
668
669
670
671
#define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
#define	V_ipport_reservedlow	VNET(ipport_reservedlow)
#define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
#define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
#define	V_ipport_firstauto	VNET(ipport_firstauto)
#define	V_ipport_lastauto	VNET(ipport_lastauto)
#define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
#define	V_ipport_hilastauto	VNET(ipport_hilastauto)
#define	V_ipport_randomized	VNET(ipport_randomized)
#define	V_ipport_randomcps	VNET(ipport_randomcps)
#define	V_ipport_randomtime	VNET(ipport_randomtime)
#define	V_ipport_stoprandom	VNET(ipport_stoprandom)
#define	V_ipport_tcpallocs	VNET(ipport_tcpallocs)
672

673
void	in_pcbinfo_destroy(struct inpcbinfo *);
Gleb Smirnoff's avatar
Gleb Smirnoff committed
674
675
void	in_pcbinfo_init(struct inpcbinfo *, const char *, u_int, int, char *,
	    uma_init);
676

677
678
679
int	in_pcbbind_check_bindmulti(const struct inpcb *ni,
	    const struct inpcb *oi);

Jeffrey Hsu's avatar
Jeffrey Hsu committed
680
void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
681
int	in_pcballoc(struct socket *, struct inpcbinfo *);
682
int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
683
int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
684
	    u_short *, struct ucred *);
685
int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *, bool);
686
687
int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
	    u_short *, in_addr_t *, u_short *, struct inpcb **,
688
	    struct ucred *);
Alfred Perlstein's avatar
Alfred Perlstein committed
689
690
void	in_pcbdetach(struct inpcb *);
void	in_pcbdisconnect(struct inpcb *);
691
void	in_pcbdrop(struct inpcb *);
692
void	in_pcbfree(struct inpcb *);
Alfred Perlstein's avatar
Alfred Perlstein committed
693
int	in_pcbinshash(struct inpcb *);
694
695
int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
	    struct ucred *);
696
int	in_pcblbgroup_numa(struct inpcb *, int arg);
697
struct inpcb *
698
	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
699
	    struct in_addr, u_int, int, struct ifnet *);
700
701
702
struct inpcb *
	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
Jeffrey Hsu's avatar
Jeffrey Hsu committed
703
void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
704
	    int, struct inpcb *(*)(struct inpcb *, int));
705
void	in_pcbref(struct inpcb *);
Alfred Perlstein's avatar
Alfred Perlstein committed
706
void	in_pcbrehash(struct inpcb *);
Gleb Smirnoff's avatar
Gleb Smirnoff committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
bool	in_pcbrele_rlocked(struct inpcb *);
bool	in_pcbrele_wlocked(struct inpcb *);

typedef bool inp_match_t(const struct inpcb *, void *);
struct inpcb_iterator {
	const struct inpcbinfo	*ipi;
	struct inpcb		*inp;
	inp_match_t		*match;
	void			*ctx;
	int			hash;
#define	INP_ALL_LIST		-1
	const inp_lookup_t	lock;
};

/* Note: sparse initializers guarantee .inp = NULL. */
#define	INP_ITERATOR(_ipi, _lock, _match, _ctx)		\
	{						\
		.ipi = (_ipi),				\
		.lock = (_lock),			\
		.hash = INP_ALL_LIST,			\
		.match = (_match),			\
		.ctx = (_ctx),				\
	}
#define	INP_ALL_ITERATOR(_ipi, _lock)			\
	{						\
		.ipi = (_ipi),				\
		.lock = (_lock),			\
		.hash = INP_ALL_LIST,			\
	}

struct inpcb *inp_next(struct inpcb_iterator *);
738
void	in_losing(struct inpcb *);
739
void	in_pcbsetsolabel(struct socket *so);
740
741
int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
742
743
struct sockaddr *
	in_sockaddr(in_port_t port, struct in_addr *addr);
744
void	in_pcbsosetlabel(struct socket *so);
745
#ifdef RATELIMIT
746
747
748
749
750
int
in_pcboutput_txrtlmt_locked(struct inpcb *, struct ifnet *,
	    struct mbuf *, uint32_t);
int	in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t,
	    uint32_t, struct m_snd_tag **);
751
void	in_pcbdetach_txrtlmt(struct inpcb *);
752
void    in_pcbdetach_tag(struct m_snd_tag *);
753
754
int	in_pcbmodify_txrtlmt(struct inpcb *, uint32_t);
int	in_pcbquery_txrtlmt(struct inpcb *, uint32_t *);
755
int	in_pcbquery_txrlevel(struct inpcb *, uint32_t *);
756
757
758
void	in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *);
void	in_pcboutput_eagain(struct inpcb *);
#endif
759
#endif /* _KERNEL */
760

761
#endif /* !_NETINET_IN_PCB_H_ */