1 /*- 2 * Copyright (c) 1982, 1986, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93 30 * $FreeBSD$ 31 */ 32 33 #ifndef _NETINET_IN_PCB_H_ 34 #define _NETINET_IN_PCB_H_ 35 36 #include <sys/queue.h> 37 #include <sys/_lock.h> 38 #include <sys/_mutex.h> 39 40 #include <net/route.h> 41 42 #define in6pcb inpcb /* for KAME src sync over BSD*'s */ 43 #define in6p_sp inp_sp /* for KAME src sync over BSD*'s */ 44 struct inpcbpolicy; 45 46 /* 47 * Struct inpcb is the ommon structure pcb for the Internet Protocol 48 * implementation. 49 * 50 * Pointers to local and foreign host table entries, local and foreign socket 51 * numbers, and pointers up (to a socket structure) and down (to a 52 * protocol-specific control block) are stored here. 53 */ 54 LIST_HEAD(inpcbhead, inpcb); 55 LIST_HEAD(inpcbporthead, inpcbport); 56 typedef u_quad_t inp_gen_t; 57 58 /* 59 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet. 60 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing 61 * the following structure. 62 */ 63 struct in_addr_4in6 { 64 u_int32_t ia46_pad32[3]; 65 struct in_addr ia46_addr4; 66 }; 67 68 /* 69 * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553. in_conninfo has 70 * some extra padding to accomplish this. 71 */ 72 struct in_endpoints { 73 u_int16_t ie_fport; /* foreign port */ 74 u_int16_t ie_lport; /* local port */ 75 /* protocol dependent part, local and foreign addr */ 76 union { 77 /* foreign host table entry */ 78 struct in_addr_4in6 ie46_foreign; 79 struct in6_addr ie6_foreign; 80 } ie_dependfaddr; 81 union { 82 /* local host table entry */ 83 struct in_addr_4in6 ie46_local; 84 struct in6_addr ie6_local; 85 } ie_dependladdr; 86 #define ie_faddr ie_dependfaddr.ie46_foreign.ia46_addr4 87 #define ie_laddr ie_dependladdr.ie46_local.ia46_addr4 88 #define ie6_faddr ie_dependfaddr.ie6_foreign 89 #define ie6_laddr ie_dependladdr.ie6_local 90 }; 91 92 /* 93 * XXX The defines for inc_* are hacks and should be changed to direct 94 * references. 95 */ 96 struct in_conninfo { 97 u_int8_t inc_flags; 98 u_int8_t inc_len; 99 u_int16_t inc_pad; /* XXX alignment for in_endpoints */ 100 /* protocol dependent part */ 101 struct in_endpoints inc_ie; 102 }; 103 #define inc_isipv6 inc_flags /* temp compatability */ 104 #define inc_fport inc_ie.ie_fport 105 #define inc_lport inc_ie.ie_lport 106 #define inc_faddr inc_ie.ie_faddr 107 #define inc_laddr inc_ie.ie_laddr 108 #define inc6_faddr inc_ie.ie6_faddr 109 #define inc6_laddr inc_ie.ie6_laddr 110 111 struct icmp6_filter; 112 113 struct inpcb { 114 LIST_ENTRY(inpcb) inp_hash; /* hash list */ 115 LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */ 116 void *inp_ppcb; /* pointer to per-protocol pcb */ 117 struct inpcbinfo *inp_pcbinfo; /* PCB list info */ 118 struct socket *inp_socket; /* back pointer to socket */ 119 120 u_int32_t inp_flow; 121 int inp_flags; /* generic IP/datagram flags */ 122 123 u_char inp_vflag; /* IP version flag (v4/v6) */ 124 #define INP_IPV4 0x1 125 #define INP_IPV6 0x2 126 #define INP_IPV6PROTO 0x4 /* opened under IPv6 protocol */ 127 #define INP_TIMEWAIT 0x8 /* .. probably doesn't go here */ 128 #define INP_ONESBCAST 0x10 /* send all-ones broadcast */ 129 #define INP_DROPPED 0x20 /* protocol drop flag */ 130 #define INP_SOCKREF 0x40 /* strong socket reference */ 131 u_char inp_ip_ttl; /* time to live proto */ 132 u_char inp_ip_p; /* protocol proto */ 133 u_char inp_ip_minttl; /* minimum TTL or drop */ 134 uint32_t inp_ispare1; /* connection id / queue id */ 135 void *inp_pspare[2]; /* rtentry / general use */ 136 137 /* Local and foreign ports, local and foreign addr. */ 138 struct in_conninfo inp_inc; 139 140 /* list for this PCB's local port */ 141 struct label *inp_label; /* MAC label */ 142 struct inpcbpolicy *inp_sp; /* for IPSEC */ 143 144 /* Protocol-dependent part; options. */ 145 struct { 146 u_char inp4_ip_tos; /* type of service proto */ 147 struct mbuf *inp4_options; /* IP options */ 148 struct ip_moptions *inp4_moptions; /* IP multicast options */ 149 } inp_depend4; 150 #define inp_fport inp_inc.inc_fport 151 #define inp_lport inp_inc.inc_lport 152 #define inp_faddr inp_inc.inc_faddr 153 #define inp_laddr inp_inc.inc_laddr 154 #define inp_ip_tos inp_depend4.inp4_ip_tos 155 #define inp_options inp_depend4.inp4_options 156 #define inp_moptions inp_depend4.inp4_moptions 157 struct { 158 /* IP options */ 159 struct mbuf *inp6_options; 160 /* IP6 options for outgoing packets */ 161 struct ip6_pktopts *inp6_outputopts; 162 /* IP multicast options */ 163 struct ip6_moptions *inp6_moptions; 164 /* ICMPv6 code type filter */ 165 struct icmp6_filter *inp6_icmp6filt; 166 /* IPV6_CHECKSUM setsockopt */ 167 int inp6_cksum; 168 short inp6_hops; 169 } inp_depend6; 170 LIST_ENTRY(inpcb) inp_portlist; 171 struct inpcbport *inp_phd; /* head of this list */ 172 #define inp_zero_size offsetof(struct inpcb, inp_gencnt) 173 inp_gen_t inp_gencnt; /* generation count of this instance */ 174 struct mtx inp_mtx; 175 176 #define in6p_faddr inp_inc.inc6_faddr 177 #define in6p_laddr inp_inc.inc6_laddr 178 #define in6p_hops inp_depend6.inp6_hops /* default hop limit */ 179 #define in6p_ip6_nxt inp_ip_p 180 #define in6p_flowinfo inp_flow 181 #define in6p_vflag inp_vflag 182 #define in6p_options inp_depend6.inp6_options 183 #define in6p_outputopts inp_depend6.inp6_outputopts 184 #define in6p_moptions inp_depend6.inp6_moptions 185 #define in6p_icmp6filt inp_depend6.inp6_icmp6filt 186 #define in6p_cksum inp_depend6.inp6_cksum 187 #define in6p_flags inp_flags /* for KAME src sync over BSD*'s */ 188 #define in6p_socket inp_socket /* for KAME src sync over BSD*'s */ 189 #define in6p_lport inp_lport /* for KAME src sync over BSD*'s */ 190 #define in6p_fport inp_fport /* for KAME src sync over BSD*'s */ 191 #define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */ 192 }; 193 /* 194 * The range of the generation count, as used in this implementation, is 9e19. 195 * We would have to create 300 billion connections per second for this number 196 * to roll over in a year. This seems sufficiently unlikely that we simply 197 * don't concern ourselves with that possibility. 198 */ 199 200 /* 201 * Interface exported to userland by various protocols which use inpcbs. Hack 202 * alert -- only define if struct xsocket is in scope. 203 */ 204 #ifdef _SYS_SOCKETVAR_H_ 205 struct xinpcb { 206 size_t xi_len; /* length of this structure */ 207 struct inpcb xi_inp; 208 struct xsocket xi_socket; 209 u_quad_t xi_alignment_hack; 210 }; 211 212 struct xinpgen { 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 */ 217 }; 218 #endif /* _SYS_SOCKETVAR_H_ */ 219 220 struct inpcbport { 221 LIST_ENTRY(inpcbport) phd_hash; 222 struct inpcbhead phd_pcblist; 223 u_short phd_port; 224 }; 225 226 /* 227 * Global data structure for each high-level protocol (UDP, TCP, ...) in both 228 * IPv4 and IPv6. Holds inpcb lists and information for managing them. 229 */ 230 struct inpcbinfo { 231 /* 232 * Global list of inpcbs on the protocol. 233 */ 234 struct inpcbhead *ipi_listhead; 235 u_int ipi_count; 236 237 /* 238 * Global hash of inpcbs, hashed by local and foreign addresses and 239 * port numbers. 240 */ 241 struct inpcbhead *ipi_hashbase; 242 u_long ipi_hashmask; 243 244 /* 245 * Global hash of inpcbs, hashed by only local port number. 246 */ 247 struct inpcbporthead *ipi_porthashbase; 248 u_long ipi_porthashmask; 249 250 /* 251 * Fields associated with port lookup and allocation. 252 */ 253 u_short ipi_lastport; 254 u_short ipi_lastlow; 255 u_short ipi_lasthi; 256 257 /* 258 * UMA zone from which inpcbs are allocated for this protocol. 259 */ 260 struct uma_zone *ipi_zone; 261 262 /* 263 * Generation count--incremented each time a connection is allocated 264 * or freed. 265 */ 266 u_quad_t ipi_gencnt; 267 struct mtx ipi_mtx; 268 269 /* 270 * vimage 1 271 * general use 1 272 */ 273 void *ipi_pspare[2]; 274 }; 275 276 #define INP_LOCK_INIT(inp, d, t) \ 277 mtx_init(&(inp)->inp_mtx, (d), (t), MTX_DEF | MTX_RECURSE | MTX_DUPOK) 278 #define INP_LOCK_DESTROY(inp) mtx_destroy(&(inp)->inp_mtx) 279 #define INP_LOCK(inp) mtx_lock(&(inp)->inp_mtx) 280 #define INP_UNLOCK(inp) mtx_unlock(&(inp)->inp_mtx) 281 #define INP_LOCK_ASSERT(inp) mtx_assert(&(inp)->inp_mtx, MA_OWNED) 282 #define INP_UNLOCK_ASSERT(inp) mtx_assert(&(inp)->inp_mtx, MA_NOTOWNED) 283 284 #define INP_INFO_LOCK_INIT(ipi, d) \ 285 mtx_init(&(ipi)->ipi_mtx, (d), NULL, MTX_DEF | MTX_RECURSE) 286 #define INP_INFO_LOCK_DESTROY(ipi) mtx_destroy(&(ipi)->ipi_mtx) 287 #define INP_INFO_RLOCK(ipi) mtx_lock(&(ipi)->ipi_mtx) 288 #define INP_INFO_WLOCK(ipi) mtx_lock(&(ipi)->ipi_mtx) 289 #define INP_INFO_RUNLOCK(ipi) mtx_unlock(&(ipi)->ipi_mtx) 290 #define INP_INFO_WUNLOCK(ipi) mtx_unlock(&(ipi)->ipi_mtx) 291 #define INP_INFO_RLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_mtx, MA_OWNED) 292 #define INP_INFO_WLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_mtx, MA_OWNED) 293 #define INP_INFO_UNLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_mtx, MA_NOTOWNED) 294 295 #define INP_PCBHASH(faddr, lport, fport, mask) \ 296 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask)) 297 #define INP_PCBPORTHASH(lport, mask) \ 298 (ntohs((lport)) & (mask)) 299 300 /* flags in inp_flags: */ 301 #define INP_RECVOPTS 0x01 /* receive incoming IP options */ 302 #define INP_RECVRETOPTS 0x02 /* receive IP options for reply */ 303 #define INP_RECVDSTADDR 0x04 /* receive IP dst address */ 304 #define INP_HDRINCL 0x08 /* user supplies entire IP header */ 305 #define INP_HIGHPORT 0x10 /* user wants "high" port binding */ 306 #define INP_LOWPORT 0x20 /* user wants "low" port binding */ 307 #define INP_ANONPORT 0x40 /* port chosen for user */ 308 #define INP_RECVIF 0x80 /* receive incoming interface */ 309 #define INP_MTUDISC 0x100 /* user can do MTU discovery */ 310 #define INP_FAITH 0x200 /* accept FAITH'ed connections */ 311 #define INP_RECVTTL 0x400 /* receive incoming IP TTL */ 312 #define INP_DONTFRAG 0x800 /* don't fragment packet */ 313 314 #define IN6P_IPV6_V6ONLY 0x008000 /* restrict AF_INET6 socket for v6 */ 315 316 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */ 317 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */ 318 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */ 319 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */ 320 #define IN6P_RTHDR 0x100000 /* receive routing header */ 321 #define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */ 322 #define IN6P_TCLASS 0x400000 /* receive traffic class value */ 323 #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */ 324 #define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */ 325 #define IN6P_MTU 0x80000000 /* receive path MTU */ 326 327 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\ 328 INP_RECVIF|INP_RECVTTL|\ 329 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\ 330 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\ 331 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\ 332 IN6P_MTU) 333 #define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\ 334 IN6P_TCLASS|IN6P_AUTOFLOWLABEL) 335 336 /* for KAME src sync over BSD*'s */ 337 #define IN6P_HIGHPORT INP_HIGHPORT 338 #define IN6P_LOWPORT INP_LOWPORT 339 #define IN6P_ANONPORT INP_ANONPORT 340 #define IN6P_RECVIF INP_RECVIF 341 #define IN6P_MTUDISC INP_MTUDISC 342 #define IN6P_FAITH INP_FAITH 343 #define IN6P_CONTROLOPTS INP_CONTROLOPTS 344 /* 345 * socket AF version is {newer than,or include} 346 * actual datagram AF version 347 */ 348 349 #define INPLOOKUP_WILDCARD 1 350 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb) 351 #define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */ 352 353 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family 354 355 #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af) 356 357 #ifdef _KERNEL 358 extern int ipport_reservedhigh; 359 extern int ipport_reservedlow; 360 extern int ipport_lowfirstauto; 361 extern int ipport_lowlastauto; 362 extern int ipport_firstauto; 363 extern int ipport_lastauto; 364 extern int ipport_hifirstauto; 365 extern int ipport_hilastauto; 366 extern struct callout ipport_tick_callout; 367 368 void in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *); 369 int in_pcballoc(struct socket *, struct inpcbinfo *); 370 int in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *); 371 int in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *, 372 u_short *, struct ucred *); 373 int in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *); 374 int in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *, 375 u_short *, in_addr_t *, u_short *, struct inpcb **, 376 struct ucred *); 377 void in_pcbdetach(struct inpcb *); 378 void in_pcbdisconnect(struct inpcb *); 379 void in_pcbdrop(struct inpcb *); 380 void in_pcbfree(struct inpcb *); 381 int in_pcbinshash(struct inpcb *); 382 struct inpcb * 383 in_pcblookup_local(struct inpcbinfo *, 384 struct in_addr, u_int, int); 385 struct inpcb * 386 in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int, 387 struct in_addr, u_int, int, struct ifnet *); 388 void in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr, 389 int, struct inpcb *(*)(struct inpcb *, int)); 390 void in_pcbrehash(struct inpcb *); 391 void in_pcbsetsolabel(struct socket *so); 392 int in_getpeeraddr(struct socket *so, struct sockaddr **nam); 393 int in_getsockaddr(struct socket *so, struct sockaddr **nam); 394 struct sockaddr * 395 in_sockaddr(in_port_t port, struct in_addr *addr); 396 void in_pcbsosetlabel(struct socket *so); 397 void in_pcbremlists(struct inpcb *inp); 398 void ipport_tick(void *xtp); 399 400 /* 401 * Debugging routines compiled in when DDB is present. 402 */ 403 void db_print_inpcb(struct inpcb *inp, const char *name, int indent); 404 405 #endif /* _KERNEL */ 406 407 #endif /* !_NETINET_IN_PCB_H_ */ 408