1 /* $OpenBSD: in_pcb.h,v 1.121 2021/01/25 03:40:46 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 uint16_t inp_flowid; 152 }; 153 154 LIST_HEAD(inpcbhead, inpcb); 155 156 struct inpcbtable { 157 TAILQ_HEAD(inpthead, inpcb) inpt_queue; /* inet PCB queue */ 158 struct inpcbhead *inpt_hashtbl; /* local and foreign hash */ 159 struct inpcbhead *inpt_lhashtbl; /* local port hash */ 160 SIPHASH_KEY inpt_key, inpt_lkey; /* secrets for hashes */ 161 u_long inpt_mask, inpt_lmask; /* hash masks */ 162 int inpt_count, inpt_size; /* queue count, hash size */ 163 }; 164 165 /* flags in inp_flags: */ 166 #define INP_RECVOPTS 0x001 /* receive incoming IP options */ 167 #define INP_RECVRETOPTS 0x002 /* receive IP options for reply */ 168 #define INP_RECVDSTADDR 0x004 /* receive IP dst address */ 169 170 #define INP_RXDSTOPTS INP_RECVOPTS 171 #define INP_RXHOPOPTS INP_RECVRETOPTS 172 #define INP_RXINFO INP_RECVDSTADDR 173 #define INP_RXSRCRT 0x010 174 #define INP_HOPLIMIT 0x020 175 176 #define INP_HDRINCL 0x008 /* user supplies entire IP header */ 177 #define INP_HIGHPORT 0x010 /* user wants "high" port binding */ 178 #define INP_LOWPORT 0x020 /* user wants "low" port binding */ 179 #define INP_RECVIF 0x080 /* receive incoming interface */ 180 #define INP_RECVTTL 0x040 /* receive incoming IP TTL */ 181 #define INP_RECVDSTPORT 0x200 /* receive IP dst addr before rdr */ 182 #define INP_RECVRTABLE 0x400 /* receive routing table */ 183 #define INP_IPSECFLOWINFO 0x800 /* receive IPsec flow info */ 184 185 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \ 186 INP_RXSRCRT|INP_HOPLIMIT|INP_RECVIF|INP_RECVTTL|INP_RECVDSTPORT| \ 187 INP_RECVRTABLE) 188 189 /* 190 * These flags' values should be determined by either the transport 191 * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*(). 192 */ 193 #define INP_IPV6 0x100 /* sotopf(inp->inp_socket) == PF_INET6 */ 194 195 /* 196 * Flags in inp_flags for IPV6 197 */ 198 #define IN6P_HIGHPORT INP_HIGHPORT /* user wants "high" port */ 199 #define IN6P_LOWPORT INP_LOWPORT /* user wants "low" port */ 200 #define IN6P_RECVDSTPORT INP_RECVDSTPORT /* receive IP dst addr before rdr */ 201 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */ 202 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */ 203 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */ 204 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */ 205 #define IN6P_RTHDR 0x100000 /* receive routing header */ 206 #define IN6P_TCLASS 0x400000 /* receive traffic class value */ 207 #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */ 208 209 #define IN6P_ANONPORT 0x4000000 /* port chosen for user */ 210 #define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */ 211 #define IN6P_MTU 0x80000000 /* receive path MTU */ 212 213 #define IN6P_MINMTU 0x20000000 /* use minimum MTU */ 214 215 #define IN6P_CONTROLOPTS (IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\ 216 IN6P_DSTOPTS|IN6P_RTHDR|\ 217 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\ 218 IN6P_MTU|IN6P_RECVDSTPORT) 219 220 #define INPLOOKUP_WILDCARD 1 221 #define INPLOOKUP_SETLOCAL 2 222 #define INPLOOKUP_IPV6 4 223 224 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb) 225 226 /* macros for handling bitmap of ports not to allocate dynamically */ 227 #define DP_MAPBITS (sizeof(u_int32_t) * NBBY) 228 #define DP_MAPSIZE (howmany(65536, DP_MAPBITS)) 229 #define DP_SET(m, p) ((m)[(p) / DP_MAPBITS] |= (1 << ((p) % DP_MAPBITS))) 230 #define DP_CLR(m, p) ((m)[(p) / DP_MAPBITS] &= ~(1 << ((p) % DP_MAPBITS))) 231 #define DP_ISSET(m, p) ((m)[(p) / DP_MAPBITS] & (1 << ((p) % DP_MAPBITS))) 232 233 /* default values for baddynamicports [see ip_init()] */ 234 #define DEFBADDYNAMICPORTS_TCP { \ 235 587, 749, 750, 751, 853, 871, 2049, \ 236 6000, 6001, 6002, 6003, 6004, 6005, 6006, 6007, 6008, 6009, 6010, \ 237 0 } 238 #define DEFBADDYNAMICPORTS_UDP { 623, 664, 749, 750, 751, 2049, \ 239 3784, 3785, 7784, /* BFD/S-BFD ports */ \ 240 0 } 241 242 #define DEFROOTONLYPORTS_TCP { \ 243 2049, \ 244 0 } 245 #define DEFROOTONLYPORTS_UDP { \ 246 2049, \ 247 0 } 248 249 struct baddynamicports { 250 u_int32_t tcp[DP_MAPSIZE]; 251 u_int32_t udp[DP_MAPSIZE]; 252 }; 253 254 #ifdef _KERNEL 255 256 extern struct inpcbtable rawcbtable, rawin6pcbtable; 257 extern struct baddynamicports baddynamicports; 258 extern struct baddynamicports rootonlyports; 259 extern int in_pcbnotifymiss; 260 261 #define sotopf(so) (so->so_proto->pr_domain->dom_family) 262 263 void in_init(void); 264 void in_losing(struct inpcb *); 265 int in_pcballoc(struct socket *, struct inpcbtable *); 266 int in_pcbbind(struct inpcb *, struct mbuf *, struct proc *); 267 int in_pcbaddrisavail(struct inpcb *, struct sockaddr_in *, int, 268 struct proc *); 269 int in_pcbconnect(struct inpcb *, struct mbuf *); 270 void in_pcbdetach(struct inpcb *); 271 struct inpcb * 272 in_pcbref(struct inpcb *); 273 void in_pcbunref(struct inpcb *); 274 void in_pcbdisconnect(struct inpcb *); 275 struct inpcb * 276 in_pcbhashlookup(struct inpcbtable *, struct in_addr, 277 u_int, struct in_addr, u_int, u_int); 278 struct inpcb * 279 in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int, 280 struct mbuf *, u_int); 281 #ifdef INET6 282 struct inpcbhead * 283 in6_pcbhash(struct inpcbtable *, int, const struct in6_addr *, 284 u_short, const struct in6_addr *, u_short); 285 struct inpcb * 286 in6_pcbhashlookup(struct inpcbtable *, const struct in6_addr *, 287 u_int, const struct in6_addr *, u_int, u_int); 288 struct inpcb * 289 in6_pcblookup_listen(struct inpcbtable *, struct in6_addr *, u_int, 290 struct mbuf *, u_int); 291 int in6_pcbaddrisavail(struct inpcb *, struct sockaddr_in6 *, int, 292 struct proc *); 293 int in6_pcbconnect(struct inpcb *, struct mbuf *); 294 void in6_setsockaddr(struct inpcb *, struct mbuf *); 295 void in6_setpeeraddr(struct inpcb *, struct mbuf *); 296 #endif /* INET6 */ 297 void in_pcbinit(struct inpcbtable *, int); 298 struct inpcb * 299 in_pcblookup_local(struct inpcbtable *, void *, u_int, int, u_int); 300 void in_pcbnotifyall(struct inpcbtable *, struct sockaddr *, 301 u_int, int, void (*)(struct inpcb *, int)); 302 void in_pcbrehash(struct inpcb *); 303 void in_rtchange(struct inpcb *, int); 304 void in_setpeeraddr(struct inpcb *, struct mbuf *); 305 void in_setsockaddr(struct inpcb *, struct mbuf *); 306 int in_baddynamic(u_int16_t, u_int16_t); 307 int in_rootonly(u_int16_t, u_int16_t); 308 int in_pcbselsrc(struct in_addr **, struct sockaddr_in *, struct inpcb *); 309 struct rtentry * 310 in_pcbrtentry(struct inpcb *); 311 312 /* INET6 stuff */ 313 int in6_pcbnotify(struct inpcbtable *, struct sockaddr_in6 *, 314 u_int, const struct sockaddr_in6 *, u_int, u_int, int, void *, 315 void (*)(struct inpcb *, int)); 316 int in6_selecthlim(struct inpcb *); 317 int in_pcbpickport(u_int16_t *, void *, int, struct inpcb *, struct proc *); 318 #endif /* _KERNEL */ 319 #endif /* _NETINET_IN_PCB_H_ */ 320