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