kernel - Replace zalloc zones with kmalloc for PCBs
[dragonfly.git] / sys / netinet / in_pcb.h
CommitLineData
984263bc 1/*
66d6c637
JH
2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
f23061d4 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
f23061d4 7 *
66d6c637
JH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
f23061d4 19 *
66d6c637
JH
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
66d6c637 34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93
67 * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.7 2003/01/24 05:11:34 sam Exp $
5aa41e7c 68 * $DragonFly: src/sys/netinet/in_pcb.h,v 1.24 2008/09/04 09:08:22 hasso Exp $
984263bc
MD
69 */
70
71#ifndef _NETINET_IN_PCB_H_
72#define _NETINET_IN_PCB_H_
73
1bd40720 74#ifndef _SYS_QUEUE_H_
984263bc 75#include <sys/queue.h>
1bd40720
MD
76#endif
77#ifndef _SYS_SYSCTL_H_
48a2763f 78#include <sys/sysctl.h>
1bd40720
MD
79#endif
80#ifndef _NETINET_IN_H_
81#include <netinet/in.h>
82#endif
83#ifndef _NET_ROUTE_H_
984263bc 84#include <net/route.h>
1bd40720 85#endif
984263bc
MD
86
87#define in6pcb inpcb /* for KAME src sync over BSD*'s */
88#define in6p_sp inp_sp /* for KAME src sync over BSD*'s */
89struct inpcbpolicy;
90
91/*
92 * Common structure pcb for internet protocol implementation.
93 * Here are stored pointers to local and foreign host table
94 * entries, local and foreign socket numbers, and pointers
95 * up (to a socket structure) and down (to a protocol-specific)
96 * control block.
97 */
98LIST_HEAD(inpcbhead, inpcb);
99LIST_HEAD(inpcbporthead, inpcbport);
100typedef u_quad_t inp_gen_t;
101
ef962575
JH
102struct inpcontainer {
103 struct inpcb *ic_inp;
104 LIST_ENTRY(inpcontainer) ic_list;
105};
106LIST_HEAD(inpcontainerhead, inpcontainer);
107
984263bc
MD
108/*
109 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
110 * So, AF_INET6 null laddr is also used as AF_INET null laddr,
111 * by utilize following structure. (At last, same as INRIA)
112 */
113struct in_addr_4in6 {
114 u_int32_t ia46_pad32[3];
115 struct in_addr ia46_addr4;
116};
117
118/*
119 * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.
120 * in_conninfo has some extra padding to accomplish this.
121 */
122struct in_endpoints {
123 u_int16_t ie_fport; /* foreign port */
124 u_int16_t ie_lport; /* local port */
125 /* protocol dependent part, local and foreign addr */
126 union {
127 /* foreign host table entry */
128 struct in_addr_4in6 ie46_foreign;
129 struct in6_addr ie6_foreign;
130 } ie_dependfaddr;
131 union {
132 /* local host table entry */
133 struct in_addr_4in6 ie46_local;
134 struct in6_addr ie6_local;
135 } ie_dependladdr;
136#define ie_faddr ie_dependfaddr.ie46_foreign.ia46_addr4
137#define ie_laddr ie_dependladdr.ie46_local.ia46_addr4
138#define ie6_faddr ie_dependfaddr.ie6_foreign
139#define ie6_laddr ie_dependladdr.ie6_local
140};
141
142/*
143 * XXX
144 * At some point struct route should possibly change to:
145 * struct rtentry *rt
f23061d4 146 * struct in_endpoints *ie;
984263bc
MD
147 */
148struct in_conninfo {
149 u_int8_t inc_flags;
150 u_int8_t inc_len;
151 u_int16_t inc_pad; /* XXX alignment for in_endpoints */
152 /* protocol dependent part; cached route */
153 struct in_endpoints inc_ie;
154 union {
155 /* placeholder for routing entry */
156 struct route inc4_route;
157 struct route_in6 inc6_route;
158 } inc_dependroute;
159};
9a7c6212 160#define inc_isipv6 inc_flags /* temp compatibility */
984263bc
MD
161#define inc_fport inc_ie.ie_fport
162#define inc_lport inc_ie.ie_lport
163#define inc_faddr inc_ie.ie_faddr
164#define inc_laddr inc_ie.ie_laddr
165#define inc_route inc_dependroute.inc4_route
166#define inc6_faddr inc_ie.ie6_faddr
167#define inc6_laddr inc_ie.ie6_laddr
168#define inc6_route inc_dependroute.inc6_route
169
170/*
171 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
172 * of the structure. Therefore, it is important that the members in
173 * that position not contain any information which is required to be
174 * stable.
175 */
176struct icmp6_filter;
177
178struct inpcb {
179 LIST_ENTRY(inpcb) inp_hash; /* hash list */
180 LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
181 u_int32_t inp_flow;
182
183 /* local and foreign ports, local and foreign addr */
184 struct in_conninfo inp_inc;
185
f23061d4 186 void *inp_ppcb; /* pointer to per-protocol pcb */
984263bc 187 struct inpcbinfo *inp_pcbinfo; /* PCB list info */
7fe56515 188 struct inpcbinfo *inp_cpcbinfo;/* back pointer for connection table */
984263bc
MD
189 struct socket *inp_socket; /* back pointer to socket */
190 /* list for this PCB's local port */
191 int inp_flags; /* generic IP/datagram flags */
192
193 struct inpcbpolicy *inp_sp; /* for IPSEC */
194 u_char inp_vflag;
195#define INP_IPV4 0x1
196#define INP_IPV6 0x2
197 u_char inp_ip_ttl; /* time to live proto */
198 u_char inp_ip_p; /* protocol proto */
95926362 199 u_char inp_ip_minttl; /* minimum TTL or drop */
984263bc
MD
200
201 /* protocol dependent part; options */
202 struct {
203 u_char inp4_ip_tos; /* type of service proto */
204 struct mbuf *inp4_options; /* IP options */
205 struct ip_moptions *inp4_moptions; /* IP multicast options */
206 } inp_depend4;
207#define inp_fport inp_inc.inc_fport
208#define inp_lport inp_inc.inc_lport
209#define inp_faddr inp_inc.inc_faddr
210#define inp_laddr inp_inc.inc_laddr
211#define inp_route inp_inc.inc_route
212#define inp_ip_tos inp_depend4.inp4_ip_tos
213#define inp_options inp_depend4.inp4_options
214#define inp_moptions inp_depend4.inp4_moptions
215 struct {
216 /* IP options */
217 struct mbuf *inp6_options;
218 /* IP6 options for outgoing packets */
219 struct ip6_pktopts *inp6_outputopts;
220 /* IP multicast options */
221 struct ip6_moptions *inp6_moptions;
222 /* ICMPv6 code type filter */
223 struct icmp6_filter *inp6_icmp6filt;
224 /* IPV6_CHECKSUM setsockopt */
225 int inp6_cksum;
226 u_short inp6_ifindex;
227 short inp6_hops;
228 u_int8_t inp6_hlim;
229 } inp_depend6;
230 LIST_ENTRY(inpcb) inp_portlist;
231 struct inpcbport *inp_phd; /* head of this list */
232 inp_gen_t inp_gencnt; /* generation count of this instance */
233#define in6p_faddr inp_inc.inc6_faddr
234#define in6p_laddr inp_inc.inc6_laddr
235#define in6p_route inp_inc.inc6_route
236#define in6p_ip6_hlim inp_depend6.inp6_hlim
237#define in6p_hops inp_depend6.inp6_hops /* default hop limit */
238#define in6p_ip6_nxt inp_ip_p
239#define in6p_flowinfo inp_flow
240#define in6p_vflag inp_vflag
241#define in6p_options inp_depend6.inp6_options
242#define in6p_outputopts inp_depend6.inp6_outputopts
243#define in6p_moptions inp_depend6.inp6_moptions
244#define in6p_icmp6filt inp_depend6.inp6_icmp6filt
245#define in6p_cksum inp_depend6.inp6_cksum
246#define inp6_ifindex inp_depend6.inp6_ifindex
247#define in6p_flags inp_flags /* for KAME src sync over BSD*'s */
248#define in6p_socket inp_socket /* for KAME src sync over BSD*'s */
249#define in6p_lport inp_lport /* for KAME src sync over BSD*'s */
250#define in6p_fport inp_fport /* for KAME src sync over BSD*'s */
251#define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */
252};
253/*
254 * The range of the generation count, as used in this implementation,
255 * is 9e19. We would have to create 300 billion connections per
256 * second for this number to roll over in a year. This seems sufficiently
257 * unlikely that we simply don't concern ourselves with that possibility.
258 */
259
260/*
261 * Interface exported to userland by various protocols which use
262 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
263 */
264#ifdef _SYS_SOCKETVAR_H_
265struct xinpcb {
266 size_t xi_len; /* length of this structure */
267 struct inpcb xi_inp;
268 struct xsocket xi_socket;
269 u_quad_t xi_alignment_hack;
270};
984263bc
MD
271#endif /* _SYS_SOCKETVAR_H_ */
272
273struct inpcbport {
274 LIST_ENTRY(inpcbport) phd_hash;
275 struct inpcbhead phd_pcblist;
276 u_short phd_port;
277};
278
279struct inpcbinfo { /* XXX documentation, prefixes */
280 struct inpcbhead *hashbase;
281 u_long hashmask;
282 struct inpcbporthead *porthashbase;
283 u_long porthashmask;
ef962575 284 struct inpcontainerhead *wildcardhashbase;
2322f6ca 285 u_long wildcardhashmask;
d2e9e54c 286 struct inpcbhead pcblisthead; /* head of queue of active pcb's */
984263bc
MD
287 u_short lastport;
288 u_short lastlow;
289 u_short lasthi;
9f42c129 290 size_t ipi_size; /* allocation size for pcbs */
984263bc
MD
291 u_int ipi_count; /* number of pcbs in this list */
292 u_quad_t ipi_gencnt; /* current generation count */
eb594563 293 int cpu; /* related protocol thread cpu or -1 */
984263bc
MD
294};
295
13d8907a 296
2322f6ca 297#define INP_PCBCONNHASH(faddr, fport, laddr, lport, mask) \
13d8907a
JH
298 (((faddr) ^ ((faddr) >> 16) ^ (laddr) ^ ntohs((lport) ^ (fport))) & (mask))
299
2322f6ca 300#define INP_PCBPORTHASH(lport, mask) (ntohs(lport) & (mask))
13d8907a 301
2322f6ca 302#define INP_PCBWILDCARDHASH(lport, mask) (ntohs(lport) & (mask))
984263bc
MD
303
304/* flags in inp_flags: */
305#define INP_RECVOPTS 0x01 /* receive incoming IP options */
306#define INP_RECVRETOPTS 0x02 /* receive IP options for reply */
307#define INP_RECVDSTADDR 0x04 /* receive IP dst address */
308#define INP_HDRINCL 0x08 /* user supplies entire IP header */
309#define INP_HIGHPORT 0x10 /* user wants "high" port binding */
310#define INP_LOWPORT 0x20 /* user wants "low" port binding */
311#define INP_ANONPORT 0x40 /* port chosen for user */
312#define INP_RECVIF 0x80 /* receive incoming interface */
313#define INP_MTUDISC 0x100 /* user can do MTU discovery */
314#define INP_FAITH 0x200 /* accept FAITH'ed connections */
2322f6ca 315#define INP_WILDCARD 0x400 /* wildcard match */
1e019813
JH
316#define INP_WILDCARD_MP 0x800 /* distributed wildcard match */
317#define INP_CONNECTED 0x1000 /* exact match */
318#define INP_WASBOUND_NOTANY 0x2000 /* was bound to non-null laddr */
d2e9e54c 319#define INP_PLACEMARKER 0x4000 /* skip this pcb, its a placemarker */
984263bc
MD
320
321#define IN6P_IPV6_V6ONLY 0x008000 /* restrict AF_INET6 socket for v6 */
322
323#define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
324#define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
325#define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
326#define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
327#define IN6P_RTHDR 0x100000 /* receive routing header */
328#define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */
329#define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */
5aa41e7c
HT
330/*
331 * RFC3542 Definition
332 */
333#define IN6P_TCLASS 0x400000 /* receive traffic class value */
334#define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */
335#define IN6P_MTU 0x80000000 /* receive path MTU */
984263bc 336
95926362
MD
337#define INP_RECVTTL 0x80000000 /* receive incoming IP TTL */
338
984263bc 339#define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
5aa41e7c 340 INP_RECVIF|INP_RECVTTL|\
984263bc
MD
341 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
342 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
5aa41e7c
HT
343 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
344 IN6P_MTU)
345
984263bc 346#define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
5aa41e7c 347 IN6P_TCLASS|IN6P_AUTOFLOWLABEL)
984263bc
MD
348
349 /* for KAME src sync over BSD*'s */
350#define IN6P_HIGHPORT INP_HIGHPORT
351#define IN6P_LOWPORT INP_LOWPORT
352#define IN6P_ANONPORT INP_ANONPORT
353#define IN6P_RECVIF INP_RECVIF
354#define IN6P_MTUDISC INP_MTUDISC
355#define IN6P_FAITH INP_FAITH
356#define IN6P_CONTROLOPTS INP_CONTROLOPTS
357 /*
358 * socket AF version is {newer than,or include}
359 * actual datagram AF version
360 */
361
362#define INPLOOKUP_WILDCARD 1
363#define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
364#define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */
365
366#define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
367
f23061d4 368#define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af)
984263bc
MD
369
370#ifdef _KERNEL
371extern int ipport_lowfirstauto;
372extern int ipport_lowlastauto;
373extern int ipport_firstauto;
374extern int ipport_lastauto;
375extern int ipport_hifirstauto;
376extern int ipport_hilastauto;
377
42a7fc75
RG
378void in_pcbpurgeif0 (struct inpcb *, struct ifnet *);
379void in_losing (struct inpcb *);
380void in_rtchange (struct inpcb *, int);
d2e9e54c 381void in_pcbinfo_init (struct inpcbinfo *);
e4700d00 382int in_pcballoc (struct socket *, struct inpcbinfo *);
42a7fc75
RG
383int in_pcbbind (struct inpcb *, struct sockaddr *, struct thread *);
384int in_pcbconnect (struct inpcb *, struct sockaddr *, struct thread *);
385void in_pcbdetach (struct inpcb *);
386void in_pcbdisconnect (struct inpcb *);
2322f6ca 387void in_pcbinswildcardhash(struct inpcb *inp);
8affadf8 388void in_pcbinswildcardhash_oncpu(struct inpcb *, struct inpcbinfo *);
13d8907a
JH
389void in_pcbinsconnhash(struct inpcb *inp);
390int in_pcbinsporthash (struct inpcb *);
42a7fc75 391int in_pcbladdr (struct inpcb *, struct sockaddr *,
3e4150ef 392 struct sockaddr_in **, struct thread *);
984263bc 393struct inpcb *
3e4150ef
VBD
394 in_pcblookup_local (struct inpcbinfo *, struct in_addr, u_int, int,
395 struct ucred *);
984263bc 396struct inpcb *
42a7fc75 397 in_pcblookup_hash (struct inpcbinfo *,
984263bc 398 struct in_addr, u_int, struct in_addr, u_int,
42a7fc75
RG
399 int, struct ifnet *);
400void in_pcbnotifyall (struct inpcbhead *, struct in_addr,
401 int, void (*)(struct inpcb *, int));
42a7fc75
RG
402int in_setpeeraddr (struct socket *so, struct sockaddr **nam);
403int in_setsockaddr (struct socket *so, struct sockaddr **nam);
2322f6ca 404void in_pcbremwildcardhash(struct inpcb *inp);
8affadf8 405void in_pcbremwildcardhash_oncpu(struct inpcb *, struct inpcbinfo *);
13d8907a 406void in_pcbremconnhash(struct inpcb *inp);
42a7fc75
RG
407void in_pcbremlists (struct inpcb *inp);
408int prison_xinpcb (struct thread *p, struct inpcb *inp);
48a2763f
JS
409
410int in_pcblist_global(SYSCTL_HANDLER_ARGS);
984263bc
MD
411#endif /* _KERNEL */
412
413#endif /* !_NETINET_IN_PCB_H_ */