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