1 /* $OpenBSD: in_pcb.h,v 1.120 2020/06/21 05:14:04 dlg Exp $ */ 2 /* $NetBSD: in_pcb.h,v 1.14 1996/02/13 23:42:00 christos Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 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 project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1990, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93 62 */ 63 64 #ifndef _NETINET_IN_PCB_H_ 65 #define _NETINET_IN_PCB_H_ 66 67 #include <sys/queue.h> 68 #include <sys/refcnt.h> 69 #include <netinet/ip6.h> 70 #include <netinet6/ip6_var.h> 71 #include <netinet/icmp6.h> 72 #include <netinet/ip_ipsp.h> 73 74 #include <crypto/siphash.h> 75 76 struct pf_state_key; 77 78 union inpaddru { 79 struct in6_addr iau_addr6; 80 struct { 81 uint8_t pad[12]; 82 struct in_addr inaddr; /* easier transition */ 83 } iau_a4u; 84 }; 85 86 /* 87 * Common structure pcb for internet protocol implementation. 88 * Here are stored pointers to local and foreign host table 89 * entries, local and foreign socket numbers, and pointers 90 * up (to a socket structure) and down (to a protocol-specific) 91 * control block. 92 */ 93 struct inpcb { 94 LIST_ENTRY(inpcb) inp_hash; /* local and foreign hash */ 95 LIST_ENTRY(inpcb) inp_lhash; /* local port hash */ 96 TAILQ_ENTRY(inpcb) inp_queue; /* inet PCB queue */ 97 struct inpcbtable *inp_table; /* inet queue/hash table */ 98 union inpaddru inp_faddru; /* Foreign address. */ 99 union inpaddru inp_laddru; /* Local address. */ 100 #define inp_faddr inp_faddru.iau_a4u.inaddr 101 #define inp_faddr6 inp_faddru.iau_addr6 102 #define inp_laddr inp_laddru.iau_a4u.inaddr 103 #define inp_laddr6 inp_laddru.iau_addr6 104 u_int16_t inp_fport; /* foreign port */ 105 u_int16_t inp_lport; /* local port */ 106 struct socket *inp_socket; /* back pointer to socket */ 107 caddr_t inp_ppcb; /* pointer to per-protocol pcb */ 108 union { /* Route (notice increased size). */ 109 struct route ru_route; 110 struct route_in6 ru_route6; 111 } inp_ru; 112 #define inp_route inp_ru.ru_route 113 #define inp_route6 inp_ru.ru_route6 114 struct refcnt inp_refcnt; /* refcount PCB, delay memory free */ 115 int inp_flags; /* generic IP/datagram flags */ 116 union { /* Header prototype. */ 117 struct ip hu_ip; 118 struct ip6_hdr hu_ipv6; 119 } inp_hu; 120 #define inp_ip inp_hu.hu_ip 121 #define inp_ipv6 inp_hu.hu_ipv6 122 struct mbuf *inp_options; /* IP options */ 123 struct ip6_pktopts *inp_outputopts6; /* IP6 options for outgoing packets */ 124 int inp_hops; 125 union { 126 struct ip_moptions *mou_mo; /* IPv4 multicast options */ 127 struct ip6_moptions *mou_mo6; /* IPv6 multicast options */ 128 } inp_mou; 129 #define inp_moptions inp_mou.mou_mo 130 #define inp_moptions6 inp_mou.mou_mo6 131 u_char inp_seclevel[4]; 132 #define SL_AUTH 0 /* Authentication level */ 133 #define SL_ESP_TRANS 1 /* ESP transport level */ 134 #define SL_ESP_NETWORK 2 /* ESP network (encapsulation) level */ 135 #define SL_IPCOMP 3 /* Compression level */ 136 u_char inp_ip_minttl; /* minimum TTL or drop */ 137 #define inp_ip6_minhlim inp_ip_minttl /* minimum Hop Limit or drop */ 138 #define inp_flowinfo inp_hu.hu_ipv6.ip6_flow 139 140 int inp_cksum6; 141 #ifndef _KERNEL 142 #define inp_csumoffset inp_cksum6 143 #endif 144 struct icmp6_filter *inp_icmp6filt; 145 struct pf_state_key *inp_pf_sk; 146 struct mbuf *(*inp_upcall)(void *, struct mbuf *, 147 struct ip *, struct ip6_hdr *, void *, int); 148 void *inp_upcall_arg; 149 u_int inp_rtableid; 150 int inp_pipex; /* pipex indication */ 151 }; 152 153 LIST_HEAD(inpcbhead, inpcb); 154 155 struct inpcbtable { 156 TAILQ_HEAD(inpthead, inpcb) inpt_queue; /* inet PCB queue */ 157 struct inpcbhead *inpt_hashtbl; /* local and foreign hash */ 158 struct inpcbhead *inpt_lhashtbl; /* local port hash */ 159 SIPHASH_KEY inpt_key, inpt_lkey; /* secrets for hashes */ 160 u_long inpt_mask, inpt_lmask; /* hash masks */ 161 int inpt_count, inpt_size; /* queue count, hash size */ 162 }; 163 164 /* flags in inp_flags: */ 165 #define INP_RECVOPTS 0x001 /* receive incoming IP options */ 166 #define INP_RECVRETOPTS 0x002 /* receive IP options for reply */ 167 #define INP_RECVDSTADDR 0x004 /* receive IP dst address */ 168 169 #define INP_RXDSTOPTS INP_RECVOPTS 170 #define INP_RXHOPOPTS INP_RECVRETOPTS 171 #define INP_RXINFO INP_RECVDSTADDR 172 #define INP_RXSRCRT 0x010 173 #define INP_HOPLIMIT 0x020 174 175 #define INP_HDRINCL 0x008 /* user supplies entire IP header */ 176 #define INP_HIGHPORT 0x010 /* user wants "high" port binding */ 177 #define INP_LOWPORT 0x020 /* user wants "low" port binding */ 178 #define INP_RECVIF 0x080 /* receive incoming interface */ 179 #define INP_RECVTTL 0x040 /* receive incoming IP TTL */ 180 #define INP_RECVDSTPORT 0x200 /* receive IP dst addr before rdr */ 181 #define INP_RECVRTABLE 0x400 /* receive routing table */ 182 #define INP_IPSECFLOWINFO 0x800 /* receive IPsec flow info */ 183 184 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \ 185 INP_RXSRCRT|INP_HOPLIMIT|INP_RECVIF|INP_RECVTTL|INP_RECVDSTPORT| \ 186 INP_RECVRTABLE) 187 188 /* 189 * These flags' values should be determined by either the transport 190 * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*(). 191 */ 192 #define INP_IPV6 0x100 /* sotopf(inp->inp_socket) == PF_INET6 */ 193 194 /* 195 * Flags in inp_flags for IPV6 196 */ 197 #define IN6P_HIGHPORT INP_HIGHPORT /* user wants "high" port */ 198 #define IN6P_LOWPORT INP_LOWPORT /* user wants "low" port */ 199 #define IN6P_RECVDSTPORT INP_RECVDSTPORT /* receive IP dst addr before rdr */ 200 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */ 201 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */ 202 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */ 203 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */ 204 #define IN6P_RTHDR 0x100000 /* receive routing header */ 205 #define IN6P_TCLASS 0x400000 /* receive traffic class value */ 206 #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */ 207 208 #define IN6P_ANONPORT 0x4000000 /* port chosen for user */ 209 #define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */ 210 #define IN6P_MTU 0x80000000 /* receive path MTU */ 211 212 #define IN6P_MINMTU 0x20000000 /* use minimum MTU */ 213 214 #define IN6P_CONTROLOPTS (IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\ 215 IN6P_DSTOPTS|IN6P_RTHDR|\ 216 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\ 217 IN6P_MTU|IN6P_RECVDSTPORT) 218 219 #define INPLOOKUP_WILDCARD 1 220 #define INPLOOKUP_SETLOCAL 2 221 #define INPLOOKUP_IPV6 4 222 223 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb) 224 225 /* macros for handling bitmap of ports not to allocate dynamically */ 226 #define DP_MAPBITS (sizeof(u_int32_t) * NBBY) 227 #define DP_MAPSIZE (howmany(65536, DP_MAPBITS)) 228 #define DP_SET(m, p) ((m)[(p) / DP_MAPBITS] |= (1 << ((p) % DP_MAPBITS))) 229 #define DP_CLR(m, p) ((m)[(p) / DP_MAPBITS] &= ~(1 << ((p) % DP_MAPBITS))) 230 #define DP_ISSET(m, p) ((m)[(p) / DP_MAPBITS] & (1 << ((p) % DP_MAPBITS))) 231 232 /* default values for baddynamicports [see ip_init()] */ 233 #define DEFBADDYNAMICPORTS_TCP { \ 234 587, 749, 750, 751, 853, 871, 2049, \ 235 6000, 6001, 6002, 6003, 6004, 6005, 6006, 6007, 6008, 6009, 6010, \ 236 0 } 237 #define DEFBADDYNAMICPORTS_UDP { 623, 664, 749, 750, 751, 2049, \ 238 3784, 3785, 7784, /* BFD/S-BFD ports */ \ 239 0 } 240 241 #define DEFROOTONLYPORTS_TCP { \ 242 2049, \ 243 0 } 244 #define DEFROOTONLYPORTS_UDP { \ 245 2049, \ 246 0 } 247 248 struct baddynamicports { 249 u_int32_t tcp[DP_MAPSIZE]; 250 u_int32_t udp[DP_MAPSIZE]; 251 }; 252 253 #ifdef _KERNEL 254 255 extern struct inpcbtable rawcbtable, rawin6pcbtable; 256 extern struct baddynamicports baddynamicports; 257 extern struct baddynamicports rootonlyports; 258 extern int in_pcbnotifymiss; 259 260 #define sotopf(so) (so->so_proto->pr_domain->dom_family) 261 262 void in_init(void); 263 void in_losing(struct inpcb *); 264 int in_pcballoc(struct socket *, struct inpcbtable *); 265 int in_pcbbind(struct inpcb *, struct mbuf *, struct proc *); 266 int in_pcbaddrisavail(struct inpcb *, struct sockaddr_in *, int, 267 struct proc *); 268 int in_pcbconnect(struct inpcb *, struct mbuf *); 269 void in_pcbdetach(struct inpcb *); 270 struct inpcb * 271 in_pcbref(struct inpcb *); 272 void in_pcbunref(struct inpcb *); 273 void in_pcbdisconnect(struct inpcb *); 274 struct inpcb * 275 in_pcbhashlookup(struct inpcbtable *, struct in_addr, 276 u_int, struct in_addr, u_int, u_int); 277 struct inpcb * 278 in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int, 279 struct mbuf *, u_int); 280 #ifdef INET6 281 struct inpcbhead * 282 in6_pcbhash(struct inpcbtable *, int, const struct in6_addr *, 283 u_short, const struct in6_addr *, u_short); 284 struct inpcb * 285 in6_pcbhashlookup(struct inpcbtable *, const struct in6_addr *, 286 u_int, const struct in6_addr *, u_int, u_int); 287 struct inpcb * 288 in6_pcblookup_listen(struct inpcbtable *, struct in6_addr *, u_int, 289 struct mbuf *, u_int); 290 int in6_pcbaddrisavail(struct inpcb *, struct sockaddr_in6 *, int, 291 struct proc *); 292 int in6_pcbconnect(struct inpcb *, struct mbuf *); 293 void in6_setsockaddr(struct inpcb *, struct mbuf *); 294 void in6_setpeeraddr(struct inpcb *, struct mbuf *); 295 #endif /* INET6 */ 296 void in_pcbinit(struct inpcbtable *, int); 297 struct inpcb * 298 in_pcblookup_local(struct inpcbtable *, void *, u_int, int, u_int); 299 void in_pcbnotifyall(struct inpcbtable *, struct sockaddr *, 300 u_int, int, void (*)(struct inpcb *, int)); 301 void in_pcbrehash(struct inpcb *); 302 void in_rtchange(struct inpcb *, int); 303 void in_setpeeraddr(struct inpcb *, struct mbuf *); 304 void in_setsockaddr(struct inpcb *, struct mbuf *); 305 int in_baddynamic(u_int16_t, u_int16_t); 306 int in_rootonly(u_int16_t, u_int16_t); 307 int in_pcbselsrc(struct in_addr **, struct sockaddr_in *, struct inpcb *); 308 struct rtentry * 309 in_pcbrtentry(struct inpcb *); 310 311 /* INET6 stuff */ 312 int in6_pcbnotify(struct inpcbtable *, struct sockaddr_in6 *, 313 u_int, const struct sockaddr_in6 *, u_int, u_int, int, void *, 314 void (*)(struct inpcb *, int)); 315 int in6_selecthlim(struct inpcb *); 316 int in_pcbpickport(u_int16_t *, void *, int, struct inpcb *, struct proc *); 317 #endif /* _KERNEL */ 318 #endif /* _NETINET_IN_PCB_H_ */ 319