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