1/* $OpenBSD: in_pcb.h,v 1.174 2026/02/05 03:26:00 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/mutex.h>
69#include <sys/rwlock.h>
70#include <sys/refcnt.h>
71#include <netinet/ip6.h>
72#include <netinet/icmp6.h>
73#include <netinet/ip_ipsp.h>
74
75#include <crypto/siphash.h>
76
77/*
78 * Locks used to protect struct members in this file:
79 * I immutable after creation
80 * N net lock
81 * t inpt_mtx pcb table mutex
82 * L pf_inp_mtx link pf to inp mutex
83 * s so_lock socket rwlock
84 */
85
86/*
87 * The pcb table mutex guarantees that all inpcb are consistent and
88 * that bind(2) and connect(2) create unique combinations of
89 * laddr/faddr/lport/fport/rtableid. This mutex is used to protect
90 * both address consistency and inpcb lookup during protocol input.
91 * All writes to inp_[lf]addr take table mutex. A per socket lock is
92 * needed, so that socket layer input have a consistent view at these
93 * values.
94 *
95 * In soconnect() and sosend() a per pcb mutex cannot be used. They
96 * eventually call IP output which takes pf lock which is a sleeping lock.
97 * Also connect(2) does a route lookup for source selection. There
98 * route resolve happens, which creates a route, which sends a route
99 * message, which needs route lock, which is a rw-lock.
100 *
101 * On the other hand a mutex should be used in protocol input. It
102 * does not make sense to do a process switch per packet. Better spin
103 * until the packet can be processed.
104 *
105 * So there are three locks. Table mutex is for writing inp_[lf]addr/port
106 * and lookup, socket rw-lock to separate sockets in system calls, and
107 * socket buffer mutex to protect socket receive buffer. Changing
108 * inp_[lf]addr/port takes both per socket rw-lock and global table mutex.
109 * Protocol input only reads inp_[lf]addr/port during lookup and is safe.
110 */
111
112struct pf_state_key;
113
114union inpaddru {
115 struct in_addr iau_addr;
116 struct in6_addr iau_addr6;
117};
118
119/*
120 * Common structure pcb for internet protocol implementation.
121 * Here are stored pointers to local and foreign host table
122 * entries, local and foreign socket numbers, and pointers
123 * up (to a socket structure) and down (to a protocol-specific)
124 * control block.
125 */
126struct inpcb {
127 struct inpcbtable *inp_table; /* [I] inet queue/hash table */
128 TAILQ_ENTRY(inpcb) inp_queue; /* [t] inet PCB queue */
129 /* keep fields above in sync with struct inpcb_iterator */
130 LIST_ENTRY(inpcb) inp_hash; /* [t] local and foreign hash */
131 LIST_ENTRY(inpcb) inp_lhash; /* [t] local port hash */
132 union inpaddru inp_faddru; /* [t] Foreign address. */
133 union inpaddru inp_laddru; /* [t] Local address. */
134#define inp_faddr inp_faddru.iau_addr
135#define inp_faddr6 inp_faddru.iau_addr6
136#define inp_laddr inp_laddru.iau_addr
137#define inp_laddr6 inp_laddru.iau_addr6
138 u_int16_t inp_fport; /* [t] foreign port */
139 u_int16_t inp_lport; /* [t] local port */
140 struct socket *inp_socket; /* [I] back pointer to socket */
141 caddr_t inp_ppcb; /* [s] pointer to per-protocol pcb */
142 struct route inp_route; /* [s] cached route */
143 struct refcnt inp_refcnt; /* refcount PCB, delay memory free */
144 int inp_flags; /* generic IP/datagram flags */
145 union { /* Header prototype. */
146 struct ip hu_ip;
147 struct ip6_hdr hu_ipv6;
148 } inp_hu;
149#define inp_ip inp_hu.hu_ip
150#define inp_ipv6 inp_hu.hu_ipv6
151 union {
152 struct mbuf *inp_options; /* IPv4 options */
153 struct ip6_pktopts *inp_outputopts6; /* IPv6 options */
154 };
155 int inp_hops;
156 union {
157 struct ip_moptions *mou_mo;
158 struct ip6_moptions *mou_mo6;
159 } inp_mou;
160#define inp_moptions inp_mou.mou_mo /* [N] IPv4 multicast options */
161#define inp_moptions6 inp_mou.mou_mo6 /* [N] IPv6 multicast options */
162 struct ipsec_level inp_seclevel; /* [N] IPsec level of socket */
163 u_char inp_ip_minttl; /* minimum TTL or drop */
164#define inp_ip6_minhlim inp_ip_minttl /* minimum Hop Limit or drop */
165#define inp_flowinfo inp_hu.hu_ipv6.ip6_flow
166
167 int inp_cksum6;
168 struct icmp6_filter *inp_icmp6filt;
169 struct pf_state_key *inp_pf_sk; /* [L] */
170 struct mbuf *(*inp_upcall)(void *, struct mbuf *,
171 struct ip *, struct ip6_hdr *, void *, int, struct netstack *);
172 void *inp_upcall_arg;
173 u_int inp_rtableid; /* [t] */
174 int inp_pipex; /* pipex indication */
175 uint16_t inp_flowid; /* [s] */
176};
177
178LIST_HEAD(inpcbhead, inpcb);
179
180struct inpcb_iterator {
181 struct inpcbtable *inp_table; /* [I] always NULL */
182 TAILQ_ENTRY(inpcb) inp_queue; /* [t] inet PCB queue */
183 /* keep fields above in sync with struct inpcb */
184};
185
186static inline int
187in_pcb_is_iterator(struct inpcb *inp)
188{
189 return (inp->inp_table == NULL ? 1 : 0);
190}
191
192struct inpcbtable {
193 struct mutex inpt_mtx; /* protect queue and hash */
194 TAILQ_HEAD(inpthead, inpcb) inpt_queue; /* [t] inet PCB queue */
195 struct inpcbhead *inpt_hashtbl; /* [t] local and foreign hash */
196 struct inpcbhead *inpt_lhashtbl; /* [t] local port hash */
197 SIPHASH_KEY inpt_key, inpt_lkey; /* [I] secrets for hashes */
198 u_long inpt_mask, inpt_lmask; /* [t] hash masks */
199 int inpt_count, inpt_size; /* [t] queue count, hash size */
200};
201
202/* flags in inp_flags: */
203#define INP_RECVOPTS 0x001 /* receive incoming IP options */
204#define INP_RECVRETOPTS 0x002 /* receive IP options for reply */
205#define INP_RECVDSTADDR 0x004 /* receive IP dst address */
206
207#define INP_RXDSTOPTS INP_RECVOPTS
208#define INP_RXHOPOPTS INP_RECVRETOPTS
209#define INP_RXINFO INP_RECVDSTADDR
210#define INP_RXSRCRT 0x010
211#define INP_HOPLIMIT 0x020
212
213#define INP_HDRINCL 0x008 /* user supplies entire IP header */
214#define INP_HIGHPORT 0x010 /* user wants "high" port binding */
215#define INP_LOWPORT 0x020 /* user wants "low" port binding */
216#define INP_RECVIF 0x080 /* receive incoming interface */
217#define INP_RECVTTL 0x040 /* receive incoming IP TTL */
218#define INP_RECVDSTPORT 0x200 /* receive IP dst addr before rdr */
219#define INP_RECVRTABLE 0x400 /* receive routing table */
220#define INP_IPSECFLOWINFO 0x800 /* receive IPsec flow info */
221
222#define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \
223 INP_RXSRCRT|INP_HOPLIMIT|INP_RECVIF|INP_RECVTTL|INP_RECVDSTPORT| \
224 INP_RECVRTABLE)
225
226/*
227 * These flags' values should be determined by either the transport
228 * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*().
229 */
230#define INP_IPV6 0x100 /* socket, proto, domain, family is PF_INET6 */
231
232/*
233 * Flags in inp_flags for IPV6
234 */
235#define IN6P_HIGHPORT INP_HIGHPORT /* user wants "high" port */
236#define IN6P_LOWPORT INP_LOWPORT /* user wants "low" port */
237#define IN6P_RECVDSTPORT INP_RECVDSTPORT /* receive IP dst addr before rdr */
238#define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
239#define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
240#define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
241#define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
242#define IN6P_RTHDR 0x100000 /* receive routing header */
243#define IN6P_TCLASS 0x400000 /* receive traffic class value */
244#define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */
245
246#define IN6P_ANONPORT 0x4000000 /* port chosen for user */
247#define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */
248#define IN6P_MTU 0x80000000 /* receive path MTU */
249
250#define IN6P_MINMTU 0x20000000 /* use minimum MTU */
251
252#define IN6P_CONTROLOPTS (IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
253 IN6P_DSTOPTS|IN6P_RTHDR|\
254 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
255 IN6P_MTU|IN6P_RECVDSTPORT)
256
257#define INPLOOKUP_WILDCARD 1
258#define INPLOOKUP_SETLOCAL 2
259#define INPLOOKUP_IPV6 4
260
261#define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
262
263/* macros for handling bitmap of ports not to allocate dynamically */
264#define DP_MAPBITS (sizeof(u_int32_t) * NBBY)
265#define DP_MAPSIZE (howmany(65536, DP_MAPBITS))
266#define DP_SET(m, p) ((m)[(p) / DP_MAPBITS] |= (1U << ((p) % DP_MAPBITS)))
267#define DP_CLR(m, p) ((m)[(p) / DP_MAPBITS] &= ~(1U << ((p) % DP_MAPBITS)))
268#define DP_ISSET(m, p) ((m)[(p) / DP_MAPBITS] & (1U << ((p) % DP_MAPBITS)))
269
270/* default values for baddynamicports [see ip_init()] */
271#define DEFBADDYNAMICPORTS_TCP { \
272 587, 749, 750, 751, 853, 871, 2049, \
273 6000, 6001, 6002, 6003, 6004, 6005, 6006, 6007, 6008, 6009, 6010, \
274 0 }
275#define DEFBADDYNAMICPORTS_UDP { 623, 664, 749, 750, 751, 2049, \
276 3784, 3785, 7784, /* BFD/S-BFD ports */ \
277 0 }
278
279#define DEFROOTONLYPORTS_TCP { \
280 2049, \
281 0 }
282#define DEFROOTONLYPORTS_UDP { \
283 2049, \
284 0 }
285
286struct baddynamicports {
287 u_int32_t tcp[DP_MAPSIZE];
288 u_int32_t udp[DP_MAPSIZE];
289};
290
291#ifdef _KERNEL
292
293#define IN_PCBLOCK_HOLD 1
294#define IN_PCBLOCK_GRAB 2
295
296extern struct inpcbtable rawcbtable, rawin6pcbtable;
297extern struct baddynamicports baddynamicports;
298extern struct baddynamicports rootonlyports;
299extern int in_pcbnotifymiss;
300
301void in_init(void);
302void in_losing(struct inpcb *);
303int in_pcballoc(struct socket *, struct inpcbtable *, int);
304int in_pcbbind_locked(struct inpcb *, struct mbuf *, const void *,
305 struct proc *);
306int in_pcbbind(struct inpcb *, struct mbuf *, struct proc *);
307int in_pcbaddrisavail(const struct inpcb *, struct sockaddr_in *, int,
308 struct proc *);
309int in_pcbconnect(struct inpcb *, struct mbuf *);
310void in_pcbdetach(struct inpcb *);
311struct socket *
312 in_pcbsolock(struct inpcb *);
313void in_pcbsounlock(struct inpcb *, struct socket *);
314struct inpcb *
315 in_pcbref(struct inpcb *);
316void in_pcbunref(struct inpcb *);
317void in_pcbdisconnect(struct inpcb *);
318struct inpcb *
319 in_pcb_iterator(struct inpcbtable *, struct inpcb *,
320 struct inpcb_iterator *);
321void in_pcb_iterator_abort(struct inpcbtable *, struct inpcb *,
322 struct inpcb_iterator *);
323struct inpcb *
324 in_pcblookup(struct inpcbtable *, struct in_addr, u_int,
325 struct in_addr, u_int, u_int);
326struct inpcb *
327 in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int,
328 struct mbuf *, u_int);
329#ifdef INET6
330uint64_t in6_pcbhash(struct inpcbtable *, u_int, const struct in6_addr *,
331 u_short, const struct in6_addr *, u_short);
332struct inpcb *
333 in6_pcblookup(struct inpcbtable *, const struct in6_addr *,
334 u_int, const struct in6_addr *, u_int, u_int);
335struct inpcb *
336 in6_pcblookup_listen(struct inpcbtable *, struct in6_addr *, u_int,
337 struct mbuf *, u_int);
338int in6_pcbaddrisavail_lock(const struct inpcb *, struct sockaddr_in6 *,
339 int, struct proc *, int);
340int in6_pcbaddrisavail(const struct inpcb *, struct sockaddr_in6 *, int,
341 struct proc *);
342int in6_pcbconnect(struct inpcb *, struct mbuf *);
343void in6_setsockaddr(struct inpcb *, struct mbuf *);
344void in6_setpeeraddr(struct inpcb *, struct mbuf *);
345int in6_sockaddr(struct socket *, struct mbuf *);
346int in6_peeraddr(struct socket *, struct mbuf *);
347#endif /* INET6 */
348void in_pcbinit(struct inpcbtable *, int);
349struct inpcb *
350 in_pcblookup_local_lock(struct inpcbtable *, const void *, u_int, int,
351 u_int, int);
352void in_pcbnotifyall(struct inpcbtable *, const struct sockaddr_in *,
353 u_int, int, void (*)(struct inpcb *, int));
354void in_pcbrehash(struct inpcb *);
355void in_pcbrtchange(struct inpcb *, int);
356void in_setpeeraddr(struct inpcb *, struct mbuf *);
357void in_setsockaddr(struct inpcb *, struct mbuf *);
358int in_sockaddr(struct socket *, struct mbuf *);
359int in_peeraddr(struct socket *, struct mbuf *);
360int in_flowid(struct socket *);
361int in_baddynamic(u_int16_t, u_int16_t);
362int in_rootonly(u_int16_t, u_int16_t);
363int in_pcbselsrc(struct in_addr *, const struct sockaddr_in *,
364 struct inpcb *);
365struct rtentry *
366 in_pcbrtentry(struct inpcb *);
367
368/* INET6 stuff */
369struct rtentry *
370 in6_pcbrtentry(struct inpcb *);
371void in6_pcbnotify(struct inpcbtable *, const struct sockaddr_in6 *,
372 u_int, const struct sockaddr_in6 *, u_int, u_int, int, void *,
373 void (*)(struct inpcb *, int));
374int in6_selecthlim(const struct inpcb *);
375int in_pcbset_rtableid(struct inpcb *, u_int);
376int in_pcbset_addr(struct inpcb *, const struct sockaddr *,
377 const struct sockaddr *, u_int);
378int in6_pcbset_addr(struct inpcb *, const struct sockaddr_in6 *,
379 const struct sockaddr_in6 *, u_int);
380void in_pcbunset_faddr(struct inpcb *);
381void in_pcbunset_laddr(struct inpcb *);
382
383#endif /* _KERNEL */
384#endif /* _NETINET_IN_PCB_H_ */