2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * The contents of this file constitute Original Code as defined in and 
   7  * are subject to the Apple Public Source License Version 1.1 (the 
   8  * "License").  You may not use this file except in compliance with the 
   9  * License.  Please obtain a copy of the License at 
  10  * http://www.apple.com/publicsource and read it before using this file. 
  12  * This Original Code and all software distributed under the License are 
  13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the 
  17  * License for the specific language governing rights and limitations 
  20  * @APPLE_LICENSE_HEADER_END@ 
  23  * Copyright (c) 1982, 1986, 1990, 1993 
  24  *      The Regents of the University of California.  All rights reserved. 
  26  * Redistribution and use in source and binary forms, with or without 
  27  * modification, are permitted provided that the following conditions 
  29  * 1. Redistributions of source code must retain the above copyright 
  30  *    notice, this list of conditions and the following disclaimer. 
  31  * 2. Redistributions in binary form must reproduce the above copyright 
  32  *    notice, this list of conditions and the following disclaimer in the 
  33  *    documentation and/or other materials provided with the distribution. 
  34  * 3. All advertising materials mentioning features or use of this software 
  35  *    must display the following acknowledgement: 
  36  *      This product includes software developed by the University of 
  37  *      California, Berkeley and its contributors. 
  38  * 4. Neither the name of the University nor the names of its contributors 
  39  *    may be used to endorse or promote products derived from this software 
  40  *    without specific prior written permission. 
  42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  54  *      @(#)in_pcb.h    8.1 (Berkeley) 6/10/93 
  57 #ifndef _NETINET_IN_PCB_H_ 
  58 #define _NETINET_IN_PCB_H_ 
  60 #include <sys/queue.h> 
  62 #include <netinet6/ipsec.h> 
  65 #define in6pcb          inpcb   /* for KAME src sync over BSD*'s */ 
  66 #define in6p_sp         inp_sp  /* for KAME src sync over BSD*'s */ 
  69  * Common structure pcb for internet protocol implementation. 
  70  * Here are stored pointers to local and foreign host table 
  71  * entries, local and foreign socket numbers, and pointers 
  72  * up (to a socket structure) and down (to a protocol-specific) 
  75 LIST_HEAD(inpcbhead
, inpcb
); 
  76 LIST_HEAD(inpcbporthead
, inpcbport
); 
  77 typedef u_quad_t        inp_gen_t
; 
  80  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet. 
  81  * So, AF_INET6 null laddr is also used as AF_INET null laddr, 
  82  * by utilize following structure. (At last, same as INRIA) 
  85         u_int32_t        ia46_pad32
[3]; 
  86         struct in_addr   ia46_addr4
; 
  90  * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS 
  91  * of the structure.  Therefore, it is important that the members in 
  92  * that position not contain any information which is required to be 
  98         LIST_ENTRY(inpcb
) inp_hash
;     /* hash list */ 
  99         struct  in_addr inp_faddr
;      /* foreign host table entry */ 
 100         struct  in_addr inp_laddr
;      /* local host table entry */ 
 101         u_short inp_fport
;              /* foreign port */ 
 102         u_short inp_lport
;              /* local port */ 
 103         LIST_ENTRY(inpcb
) inp_list
;     /* list for all PCBs of this proto */ 
 104         caddr_t inp_ppcb
;               /* pointer to per-protocol pcb */ 
 105         struct  inpcbinfo 
*inp_pcbinfo
; /* PCB list info */ 
 106         struct  socket 
*inp_socket
;     /* back pointer to socket */ 
 107         u_char  nat_owner
;              /* Used to NAT TCP/UDP traffic */ 
 108         u_long  nat_cookie
;             /* Cookie stored and returned to NAT */ 
 109         LIST_ENTRY(inpcb
) inp_portlist
; /* list for this PCB's local port */ 
 110         struct  inpcbport 
*inp_phd
;     /* head of this list */ 
 111         inp_gen_t inp_gencnt
;           /* generation count of this instance */ 
 112         int     inp_flags
;              /* generic IP/datagram flags */ 
 119         u_char inp_ip_ttl
;              /* time to live proto */ 
 120         u_char inp_ip_p
;                /* protocol proto */ 
 121         /* protocol dependent part */ 
 123                 /* foreign host table entry */ 
 124                 struct in_addr_4in6 inp46_foreign
; 
 125                 struct in6_addr inp6_foreign
; 
 128                 /* local host table entry */ 
 129                 struct in_addr_4in6 inp46_local
; 
 130                 struct in6_addr inp6_local
; 
 133                 /* placeholder for routing entry */ 
 134                 struct route inp4_route
; 
 135                 struct route_in6 inp6_route
; 
 138                 /* type of service proto */ 
 141                 struct mbuf 
*inp4_options
; 
 142                 /* IP multicast options */ 
 143                 struct ip_moptions 
*inp4_moptions
; 
 145 #define inp_faddr       inp_dependfaddr.inp46_foreign.ia46_addr4 
 146 #define inp_laddr       inp_dependladdr.inp46_local.ia46_addr4 
 147 #define inp_route       inp_dependroute.inp4_route 
 148 #define inp_ip_tos      inp_depend4.inp4_ip_tos 
 149 #define inp_options     inp_depend4.inp4_options 
 150 #define inp_moptions    inp_depend4.inp4_moptions 
 153                 struct mbuf 
*inp6_options
; 
 154                 /* IP6 options for incoming packets */ 
 155                 struct ip6_recvpktopts 
*inp6_inputopts
; 
 156                 /* IP6 options for outgoing packets */ 
 157                 struct ip6_pktopts 
*inp6_outputopts
; 
 158                 /* IP multicast options */ 
 159                 struct ip6_moptions 
*inp6_moptions
; 
 160                 /* ICMPv6 code type filter */ 
 161                 struct icmp6_filter 
*inp6_icmp6filt
; 
 162                 /* IPV6_CHECKSUM setsockopt */ 
 164                 u_short inp6_ifindex
; 
 167 #define in6p_faddr      inp_dependfaddr.inp6_foreign 
 168 #define in6p_laddr      inp_dependladdr.inp6_local 
 169 #define in6p_route      inp_dependroute.inp6_route 
 170 #define in6p_hops       inp_depend6.inp6_hops   /* default hop limit */ 
 171 #define in6p_ip6_nxt    inp_ip_p 
 172 #define in6p_flowinfo   inp_flow 
 173 #define in6p_vflag      inp_vflag 
 174 #define in6p_options    inp_depend6.inp6_options 
 175 #define in6p_inputopts  inp_depend6.inp6_inputopts 
 176 #define in6p_outputopts inp_depend6.inp6_outputopts 
 177 #define in6p_moptions   inp_depend6.inp6_moptions 
 178 #define in6p_icmp6filt  inp_depend6.inp6_icmp6filt 
 179 #define in6p_cksum      inp_depend6.inp6_cksum 
 180 #define inp6_ifindex    inp_depend6.inp6_ifindex 
 181 #define in6p_flags      inp_flags  /* for KAME src sync over BSD*'s */ 
 182 #define in6p_socket     inp_socket  /* for KAME src sync over BSD*'s */ 
 183 #define in6p_lport      inp_lport  /* for KAME src sync over BSD*'s */ 
 184 #define in6p_fport      inp_fport  /* for KAME src sync over BSD*'s */ 
 185 #define in6p_ppcb       inp_ppcb  /* for KAME src sync over BSD*'s */ 
 187         struct inpcbpolicy 
*inp_sp
; 
 189         int     hash_element
;           /* Array index of pcb's hash list    */ 
 190         caddr_t inp_saved_ppcb
;         /* place to save pointer while cached */ 
 194  * The range of the generation count, as used in this implementation, 
 195  * is 9e19.  We would have to create 300 billion connections per 
 196  * second for this number to roll over in a year.  This seems sufficiently 
 197  * unlikely that we simply don't concern ourselves with that possibility. 
 201  * Interface exported to userland by various protocols which use 
 202  * inpcbs.  Hack alert -- only define if struct xsocket is in scope. 
 204 #ifdef _SYS_SOCKETVAR_H_ 
 206         size_t  xi_len
;         /* length of this structure */ 
 208         struct  xsocket xi_socket
; 
 209         u_quad_t        xi_alignment_hack
; 
 213         size_t  xig_len
;        /* length of this structure */ 
 214         u_int   xig_count
;      /* number of PCBs at this time */ 
 215         inp_gen_t xig_gen
;      /* generation count at this time */ 
 216         so_gen_t xig_sogen
;     /* socket generation count at this time */ 
 218 #endif /* _SYS_SOCKETVAR_H_ */ 
 221         LIST_ENTRY(inpcbport
) phd_hash
; 
 222         struct inpcbhead phd_pcblist
; 
 226 struct inpcbinfo 
{              /* XXX documentation, prefixes */ 
 227         struct  inpcbhead 
*hashbase
; 
 228         u_long  hashsize
; /* in elements */ 
 230         struct  inpcbporthead 
*porthashbase
; 
 232         struct  inpcbhead 
*listhead
; 
 236         void   *ipi_zone
; /* zone to allocate pcbs from */ 
 237         u_int   ipi_count
;      /* number of pcbs in this list */ 
 238         u_quad_t ipi_gencnt
;    /* current generation count */ 
 240      struct     socket nat_dummy_socket
; 
 241         struct  inpcb 
*last_pcb
; 
 245 #define INP_PCBHASH(faddr, lport, fport, mask) \ 
 246         (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask)) 
 247 #define INP_PCBPORTHASH(lport, mask) \ 
 248         (ntohs((lport)) & (mask)) 
 250 /* flags in inp_flags: */ 
 251 #define INP_RECVOPTS            0x01    /* receive incoming IP options */ 
 252 #define INP_RECVRETOPTS         0x02    /* receive IP options for reply */ 
 253 #define INP_RECVDSTADDR         0x04    /* receive IP dst address */ 
 254 #define INP_HDRINCL             0x08    /* user supplies entire IP header */ 
 255 #define INP_HIGHPORT            0x10    /* user wants "high" port binding */ 
 256 #define INP_LOWPORT             0x20    /* user wants "low" port binding */ 
 257 #define INP_ANONPORT            0x40    /* port chosen for user */ 
 258 #define INP_RECVIF              0x80    /* receive incoming interface */ 
 259 #define INP_MTUDISC             0x100   /* user can do MTU discovery */ 
 260 #define         INP_STRIPHDR            0x200   /* drop receive of raw IP header */ 
 261 #define         INP_FAITH                       0x400   /* accept FAITH'ed connections */ 
 262 #define         IN6P_PKTINFO            0x010000 /* receive IP6 dst and I/F */ 
 263 #define         IN6P_HOPLIMIT           0x020000 /* receive hoplimit */ 
 264 #define         IN6P_HOPOPTS            0x040000 /* receive hop-by-hop options */ 
 265 #define         IN6P_DSTOPTS            0x080000 /* receive dst options after rthdr */ 
 266 #define         IN6P_RTHDR              0x100000 /* receive routing header */ 
 267 #define         IN6P_RTHDRDSTOPTS       0x200000 /* receive dstoptions before rthdr */ 
 268 #define IN6P_BINDV6ONLY 0x10000000 /* do not grab IPv4 traffic */ 
 269 #define IN6P_MINMTU             0x20000000 /* use minimum MTU */ 
 271 #define INP_CONTROLOPTS         (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\ 
 273                                  IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\ 
 274                                  IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS) 
 275 #define INP_UNMAPPABLEOPTS      (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR) 
 277  /* for KAME src sync over BSD*'s */ 
 278 #define IN6P_HIGHPORT           INP_HIGHPORT 
 279 #define IN6P_LOWPORT            INP_LOWPORT 
 280 #define IN6P_ANONPORT           INP_ANONPORT 
 281 #define IN6P_RECVIF             INP_RECVIF 
 282 #define IN6P_MTUDISC            INP_MTUDISC 
 283 #define IN6P_FAITH              INP_FAITH 
 284 #define IN6P_CONTROLOPTS INP_CONTROLOPTS 
 286          * socket AF version is {newer than,or include}  
 287          * actual datagram AF version 
 290 #define INPLOOKUP_WILDCARD      1 
 291 #define INPCB_ALL_OWNERS        0xff 
 292 #define INPCB_NO_OWNER          0x0 
 293 #define INPCB_OWNED_BY_X        0x80 
 294 #define INPCB_MAX_IDS           7 
 296 #define sotoinpcb(so)   ((struct inpcb *)(so)->so_pcb) 
 297 #define sotoin6pcb(so)  sotoinpcb(so) /* for KAME src sync over BSD*'s */ 
 299 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family 
 301 #define INP_CHECK_SOCKAF(so, af) \ 
 302         (INP_SOCKAF(so) == af) 
 305 extern int ipport_lowfirstauto
; 
 306 extern int ipport_lowlastauto
; 
 307 extern int ipport_firstauto
; 
 308 extern int ipport_lastauto
; 
 309 extern int ipport_hifirstauto
; 
 310 extern int ipport_hilastauto
; 
 312 void    in_losing 
__P((struct inpcb 
*)); 
 313 int     in_pcballoc 
__P((struct socket 
*, struct inpcbinfo 
*, struct proc 
*)); 
 314 int     in_pcbbind 
__P((struct inpcb 
*, struct sockaddr 
*, struct proc 
*)); 
 315 int     in_pcbconnect 
__P((struct inpcb 
*, struct sockaddr 
*, struct proc 
*)); 
 316 void    in_pcbdetach 
__P((struct inpcb 
*)); 
 317 void    in_pcbdisconnect 
__P((struct inpcb 
*)); 
 318 int     in_pcbinshash 
__P((struct inpcb 
*)); 
 319 int     in_pcbladdr 
__P((struct inpcb 
*, struct sockaddr 
*, 
 320             struct sockaddr_in 
**)); 
 322         in_pcblookup_local 
__P((struct inpcbinfo 
*, 
 323             struct in_addr
, u_int
, int)); 
 325         in_pcblookup_hash 
__P((struct inpcbinfo 
*, 
 326             struct in_addr
, u_int
, struct in_addr
, u_int
, int, struct ifnet 
*)); 
 327 void    in_pcbnotify 
__P((struct inpcbhead 
*, struct sockaddr 
*, 
 328             u_int
, struct in_addr
, u_int
, int, void (*)(struct inpcb 
*, int))); 
 329 void    in_pcbrehash 
__P((struct inpcb 
*)); 
 330 int     in_setpeeraddr 
__P((struct socket 
*so
, struct sockaddr 
**nam
)); 
 331 int     in_setsockaddr 
__P((struct socket 
*so
, struct sockaddr 
**nam
)); 
 334 in_pcb_grab_port  
__P((struct inpcbinfo 
*pcbinfo
, 
 336                        struct in_addr   laddr
,  
 338                        struct in_addr   faddr
, 
 344 in_pcb_letgo_port 
__P((struct inpcbinfo 
*pcbinfo
,  
 345                        struct in_addr laddr
,  
 347                        struct in_addr faddr
, 
 348                        u_short fport
, u_char owner_id
)); 
 351 in_pcb_get_owner 
__P((struct inpcbinfo 
*pcbinfo
,  
 352                       struct in_addr laddr
,  
 354                       struct in_addr faddr
, 
 358 void in_pcb_nat_init(struct inpcbinfo 
*pcbinfo
, int afamily
, int pfamily
, 
 362 in_pcb_new_share_client(struct inpcbinfo 
*pcbinfo
, u_char 
*owner_id
); 
 365 in_pcb_rem_share_client(struct inpcbinfo 
*pcbinfo
, u_char owner_id
); 
 367 void    in_pcbremlists 
__P((struct inpcb 
*inp
)); 
 369 int     in6_selecthlim 
__P((struct inpcb 
*, struct ifnet 
*)); 
 374 #endif /* !_NETINET_IN_PCB_H_ */