sctp_pcb.h revision 1.3 1 1.1 rjs /* $KAME: sctp_pcb.h,v 1.21 2005/07/16 01:18:47 suz Exp $ */
2 1.3 riastrad /* $NetBSD: sctp_pcb.h,v 1.3 2020/01/19 20:51:13 riastradh Exp $ */
3 1.1 rjs
4 1.1 rjs #ifndef __SCTP_PCB_H__
5 1.1 rjs #define __SCTP_PCB_H__
6 1.1 rjs
7 1.1 rjs /*
8 1.1 rjs * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
9 1.1 rjs * All rights reserved.
10 1.1 rjs *
11 1.1 rjs * Redistribution and use in source and binary forms, with or without
12 1.1 rjs * modification, are permitted provided that the following conditions
13 1.1 rjs * are met:
14 1.1 rjs * 1. Redistributions of source code must retain the above copyright
15 1.1 rjs * notice, this list of conditions and the following disclaimer.
16 1.1 rjs * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 rjs * notice, this list of conditions and the following disclaimer in the
18 1.1 rjs * documentation and/or other materials provided with the distribution.
19 1.1 rjs * 3. All advertising materials mentioning features or use of this software
20 1.1 rjs * must display the following acknowledgement:
21 1.1 rjs * This product includes software developed by Cisco Systems, Inc.
22 1.1 rjs * 4. Neither the name of the project nor the names of its contributors
23 1.1 rjs * may be used to endorse or promote products derived from this software
24 1.1 rjs * without specific prior written permission.
25 1.1 rjs *
26 1.1 rjs * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 rjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 rjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 rjs * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
30 1.1 rjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 rjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 rjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 rjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 rjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 rjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 rjs * SUCH DAMAGE.
37 1.1 rjs */
38 1.1 rjs
39 1.1 rjs /*
40 1.1 rjs * We must have V6 so the size of the proto can be calculated. Otherwise
41 1.1 rjs * we would not allocate enough for Net/Open BSD :-<
42 1.1 rjs */
43 1.1 rjs #include <net/if.h>
44 1.1 rjs #include <netinet/ip6.h>
45 1.1 rjs #include <netinet6/ip6_var.h>
46 1.1 rjs #include <netinet6/ip6protosw.h>
47 1.1 rjs #include <netinet6/in6_var.h>
48 1.1 rjs #include <netinet6/in6_pcb.h>
49 1.1 rjs
50 1.1 rjs #include <netinet/sctp.h>
51 1.1 rjs #include <netinet/sctp_constants.h>
52 1.1 rjs
53 1.1 rjs LIST_HEAD(sctppcbhead, sctp_inpcb);
54 1.1 rjs LIST_HEAD(sctpasochead, sctp_tcb);
55 1.1 rjs TAILQ_HEAD(sctpsocketq, sctp_socket_q_list);
56 1.1 rjs LIST_HEAD(sctpladdr, sctp_laddr);
57 1.1 rjs LIST_HEAD(sctpvtaghead, sctp_tagblock);
58 1.1 rjs
59 1.1 rjs #include <netinet/sctp_structs.h>
60 1.1 rjs #include <netinet/sctp_uio.h>
61 1.1 rjs
62 1.1 rjs /*
63 1.1 rjs * PCB flags
64 1.1 rjs */
65 1.1 rjs #define SCTP_PCB_FLAGS_UDPTYPE 0x00000001
66 1.1 rjs #define SCTP_PCB_FLAGS_TCPTYPE 0x00000002
67 1.1 rjs #define SCTP_PCB_FLAGS_BOUNDALL 0x00000004
68 1.1 rjs #define SCTP_PCB_FLAGS_ACCEPTING 0x00000008
69 1.1 rjs #define SCTP_PCB_FLAGS_UNBOUND 0x00000010
70 1.1 rjs #define SCTP_PCB_FLAGS_DO_ASCONF 0x00000020
71 1.1 rjs #define SCTP_PCB_FLAGS_AUTO_ASCONF 0x00000040
72 1.1 rjs /* socket options */
73 1.1 rjs #define SCTP_PCB_FLAGS_NODELAY 0x00000100
74 1.1 rjs #define SCTP_PCB_FLAGS_AUTOCLOSE 0x00000200
75 1.1 rjs #define SCTP_PCB_FLAGS_RECVDATAIOEVNT 0x00000400
76 1.1 rjs #define SCTP_PCB_FLAGS_RECVASSOCEVNT 0x00000800
77 1.1 rjs #define SCTP_PCB_FLAGS_RECVPADDREVNT 0x00001000
78 1.1 rjs #define SCTP_PCB_FLAGS_RECVPEERERR 0x00002000
79 1.1 rjs #define SCTP_PCB_FLAGS_RECVSENDFAILEVNT 0x00004000
80 1.1 rjs #define SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT 0x00008000
81 1.1 rjs #define SCTP_PCB_FLAGS_ADAPTIONEVNT 0x00010000
82 1.1 rjs #define SCTP_PCB_FLAGS_PDAPIEVNT 0x00020000
83 1.1 rjs #define SCTP_PCB_FLAGS_STREAM_RESETEVNT 0x00040000
84 1.1 rjs #define SCTP_PCB_FLAGS_NO_FRAGMENT 0x00080000
85 1.1 rjs /* TCP model support */
86 1.1 rjs #define SCTP_PCB_FLAGS_CONNECTED 0x00100000
87 1.1 rjs #define SCTP_PCB_FLAGS_IN_TCPPOOL 0x00200000
88 1.1 rjs #define SCTP_PCB_FLAGS_DONT_WAKE 0x00400000
89 1.1 rjs #define SCTP_PCB_FLAGS_WAKEOUTPUT 0x00800000
90 1.1 rjs #define SCTP_PCB_FLAGS_WAKEINPUT 0x01000000
91 1.1 rjs #define SCTP_PCB_FLAGS_BOUND_V6 0x02000000
92 1.1 rjs #define SCTP_PCB_FLAGS_NEEDS_MAPPED_V4 0x04000000
93 1.1 rjs #define SCTP_PCB_FLAGS_BLOCKING_IO 0x08000000
94 1.1 rjs #define SCTP_PCB_FLAGS_SOCKET_GONE 0x10000000
95 1.1 rjs #define SCTP_PCB_FLAGS_SOCKET_ALLGONE 0x20000000
96 1.1 rjs
97 1.1 rjs /* flags to copy to new PCB */
98 1.1 rjs #define SCTP_PCB_COPY_FLAGS 0x0707ff64
99 1.1 rjs
100 1.1 rjs #define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask)
101 1.1 rjs #define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask)
102 1.1 rjs
103 1.1 rjs struct sctp_laddr {
104 1.1 rjs LIST_ENTRY(sctp_laddr) sctp_nxt_addr; /* next in list */
105 1.1 rjs struct ifaddr *ifa;
106 1.1 rjs };
107 1.1 rjs
108 1.1 rjs struct sctp_timewait {
109 1.1 rjs uint32_t tv_sec_at_expire; /* the seconds from boot to expire */
110 1.1 rjs uint32_t v_tag; /* the vtag that can not be reused */
111 1.1 rjs };
112 1.1 rjs
113 1.1 rjs struct sctp_tagblock {
114 1.1 rjs LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock;
115 1.1 rjs struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK];
116 1.1 rjs };
117 1.1 rjs
118 1.1 rjs struct sctp_epinfo {
119 1.1 rjs struct sctpasochead *sctp_asochash;
120 1.1 rjs u_long hashasocmark;
121 1.1 rjs
122 1.1 rjs struct sctppcbhead *sctp_ephash;
123 1.1 rjs u_long hashmark;
124 1.1 rjs
125 1.1 rjs /*
126 1.1 rjs * The TCP model represents a substantial overhead in that we get
127 1.1 rjs * an additional hash table to keep explicit connections in. The
128 1.1 rjs * listening TCP endpoint will exist in the usual ephash above and
129 1.1 rjs * accept only INIT's. It will be incapable of sending off an INIT.
130 1.1 rjs * When a dg arrives we must look in the normal ephash. If we find
131 1.1 rjs * a TCP endpoint that will tell us to go to the specific endpoint
132 1.1 rjs * hash and re-hash to find the right assoc/socket. If we find a
133 1.1 rjs * UDP model socket we then must complete the lookup. If this fails,
134 1.1 rjs * i.e. no association can be found then we must continue to see if
135 1.1 rjs * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
136 1.1 rjs * acts like a TCP model connected socket).
137 1.1 rjs */
138 1.1 rjs struct sctppcbhead *sctp_tcpephash;
139 1.1 rjs u_long hashtcpmark;
140 1.1 rjs uint32_t hashtblsize;
141 1.1 rjs
142 1.1 rjs struct sctppcbhead listhead;
143 1.1 rjs
144 1.1 rjs struct sctpiterators iteratorhead;
145 1.1 rjs
146 1.1 rjs /* ep zone info */
147 1.1 rjs #if defined(__FreeBSD__) || defined(__APPLE__)
148 1.1 rjs #if __FreeBSD_version >= 500000
149 1.1 rjs struct uma_zone *ipi_zone_ep;
150 1.1 rjs struct uma_zone *ipi_zone_asoc;
151 1.1 rjs struct uma_zone *ipi_zone_laddr;
152 1.1 rjs struct uma_zone *ipi_zone_net;
153 1.1 rjs struct uma_zone *ipi_zone_chunk;
154 1.1 rjs struct uma_zone *ipi_zone_sockq;
155 1.1 rjs #else
156 1.1 rjs struct vm_zone *ipi_zone_ep;
157 1.1 rjs struct vm_zone *ipi_zone_asoc;
158 1.1 rjs struct vm_zone *ipi_zone_laddr;
159 1.1 rjs struct vm_zone *ipi_zone_net;
160 1.1 rjs struct vm_zone *ipi_zone_chunk;
161 1.1 rjs struct vm_zone *ipi_zone_sockq;
162 1.1 rjs #endif
163 1.1 rjs #endif
164 1.1 rjs #if defined(__NetBSD__) || defined(__OpenBSD__)
165 1.1 rjs struct pool ipi_zone_ep;
166 1.1 rjs struct pool ipi_zone_asoc;
167 1.1 rjs struct pool ipi_zone_laddr;
168 1.1 rjs struct pool ipi_zone_net;
169 1.1 rjs struct pool ipi_zone_chunk;
170 1.1 rjs struct pool ipi_zone_sockq;
171 1.1 rjs struct pool ipi_zone_hash;
172 1.1 rjs #endif
173 1.1 rjs
174 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
175 1.1 rjs struct mtx ipi_ep_mtx;
176 1.1 rjs struct mtx it_mtx;
177 1.1 rjs #elif 0 /* defined(__NetBSD__) */
178 1.1 rjs krwlock_t ipi_ep_mtx;
179 1.1 rjs kmutex_t it_mtx;
180 1.1 rjs #endif
181 1.1 rjs u_int ipi_count_ep;
182 1.1 rjs u_quad_t ipi_gencnt_ep;
183 1.1 rjs
184 1.1 rjs /* assoc/tcb zone info */
185 1.1 rjs u_int ipi_count_asoc;
186 1.1 rjs u_quad_t ipi_gencnt_asoc;
187 1.1 rjs
188 1.1 rjs /* local addrlist zone info */
189 1.1 rjs u_int ipi_count_laddr;
190 1.1 rjs u_quad_t ipi_gencnt_laddr;
191 1.1 rjs
192 1.1 rjs /* remote addrlist zone info */
193 1.1 rjs u_int ipi_count_raddr;
194 1.1 rjs u_quad_t ipi_gencnt_raddr;
195 1.1 rjs
196 1.1 rjs /* chunk structure list for output */
197 1.1 rjs u_int ipi_count_chunk;
198 1.1 rjs u_quad_t ipi_gencnt_chunk;
199 1.1 rjs
200 1.1 rjs /* socket queue zone info */
201 1.1 rjs u_int ipi_count_sockq;
202 1.1 rjs u_quad_t ipi_gencnt_sockq;
203 1.1 rjs
204 1.1 rjs struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE];
205 1.1 rjs
206 1.1 rjs #ifdef _SCTP_NEEDS_CALLOUT_
207 1.1 rjs struct calloutlist callqueue;
208 1.1 rjs #endif /* _SCTP_NEEDS_CALLOUT_ */
209 1.1 rjs
210 1.1 rjs uint32_t mbuf_track;
211 1.1 rjs
212 1.1 rjs /* for port allocations */
213 1.1 rjs uint16_t lastport;
214 1.1 rjs uint16_t lastlow;
215 1.1 rjs uint16_t lasthi;
216 1.1 rjs
217 1.1 rjs };
218 1.1 rjs
219 1.1 rjs extern uint32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
220 1.1 rjs /*
221 1.1 rjs * Here we have all the relevant information for each SCTP entity created.
222 1.1 rjs * We will need to modify this as approprate. We also need to figure out
223 1.1 rjs * how to access /dev/random.
224 1.1 rjs */
225 1.1 rjs struct sctp_pcb {
226 1.1 rjs unsigned int time_of_secret_change; /* number of seconds from timeval.tv_sec */
227 1.1 rjs uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
228 1.1 rjs unsigned int size_of_a_cookie;
229 1.1 rjs
230 1.1 rjs unsigned int sctp_timeoutticks[SCTP_NUM_TMRS];
231 1.1 rjs unsigned int sctp_minrto;
232 1.1 rjs unsigned int sctp_maxrto;
233 1.1 rjs unsigned int initial_rto;
234 1.1 rjs
235 1.1 rjs int initial_init_rto_max;
236 1.1 rjs
237 1.1 rjs uint32_t sctp_sws_sender;
238 1.1 rjs uint32_t sctp_sws_receiver;
239 1.1 rjs
240 1.1 rjs /* various thresholds */
241 1.1 rjs /* Max times I will init at a guy */
242 1.1 rjs uint16_t max_init_times;
243 1.1 rjs
244 1.1 rjs /* Max times I will send before we consider someone dead */
245 1.1 rjs uint16_t max_send_times;
246 1.1 rjs
247 1.1 rjs uint16_t def_net_failure;
248 1.1 rjs
249 1.1 rjs /* number of streams to pre-open on a association */
250 1.1 rjs uint16_t pre_open_stream_count;
251 1.1 rjs uint16_t max_open_streams_intome;
252 1.1 rjs
253 1.1 rjs /*
254 1.1 rjs * This timer is kept running per endpoint. When it fires it
255 1.1 rjs * will change the secret key. The default is once a hour
256 1.1 rjs */
257 1.1 rjs struct sctp_timer signature_change;
258 1.1 rjs int def_cookie_life;
259 1.1 rjs /* defaults to 0 */
260 1.1 rjs int auto_close_time;
261 1.1 rjs uint32_t initial_sequence_debug;
262 1.1 rjs uint32_t adaption_layer_indicator;
263 1.1 rjs uint8_t max_burst;
264 1.1 rjs char current_secret_number;
265 1.1 rjs char last_secret_number;
266 1.1 rjs };
267 1.1 rjs
268 1.1 rjs #ifndef SCTP_ALIGNMENT
269 1.1 rjs #define SCTP_ALIGNMENT 32
270 1.1 rjs #endif
271 1.1 rjs
272 1.1 rjs #ifndef SCTP_ALIGNM1
273 1.1 rjs #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
274 1.1 rjs #endif
275 1.1 rjs
276 1.1 rjs #define sctp_lport ip_inp.inp.inp_lport
277 1.1 rjs
278 1.1 rjs struct sctp_socket_q_list {
279 1.1 rjs struct sctp_tcb *tcb;
280 1.1 rjs TAILQ_ENTRY(sctp_socket_q_list) next_sq;
281 1.1 rjs };
282 1.1 rjs
283 1.1 rjs struct sctp_inpcb {
284 1.1 rjs /*
285 1.1 rjs * put an inpcb in front of it all, kind of a waste but we need
286 1.1 rjs * to for compatability with all the other stuff.
287 1.1 rjs */
288 1.1 rjs union {
289 1.1 rjs struct inpcb inp;
290 1.1 rjs char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
291 1.1 rjs ~SCTP_ALIGNM1];
292 1.1 rjs } ip_inp;
293 1.1 rjs LIST_ENTRY(sctp_inpcb) sctp_list; /* lists all endpoints */
294 1.1 rjs /* hash of all endpoints for model */
295 1.1 rjs LIST_ENTRY(sctp_inpcb) sctp_hash;
296 1.1 rjs
297 1.1 rjs /* count of local addresses bound, 0 if bound all */
298 1.1 rjs int laddr_count;
299 1.1 rjs /* list of addrs in use by the EP */
300 1.1 rjs struct sctpladdr sctp_addr_list;
301 1.1 rjs /* used for source address selection rotation */
302 1.1 rjs struct sctp_laddr *next_addr_touse;
303 1.1 rjs struct ifnet *next_ifn_touse;
304 1.1 rjs /* back pointer to our socket */
305 1.1 rjs struct socket *sctp_socket;
306 1.1 rjs uint32_t sctp_flags; /* flag set */
307 1.1 rjs struct sctp_pcb sctp_ep; /* SCTP ep data */
308 1.1 rjs /* head of the hash of all associations */
309 1.1 rjs struct sctpasochead *sctp_tcbhash;
310 1.1 rjs u_long sctp_hashmark;
311 1.1 rjs /* head of the list of all associations */
312 1.1 rjs struct sctpasochead sctp_asoc_list;
313 1.1 rjs /* queue of TCB's waiting to stuff data up the socket */
314 1.1 rjs struct sctpsocketq sctp_queue_list;
315 1.1 rjs void *sctp_tcb_at_block;
316 1.1 rjs struct sctp_iterator *inp_starting_point_for_iterator;
317 1.1 rjs int error_on_block;
318 1.1 rjs uint32_t sctp_frag_point;
319 1.1 rjs uint32_t sctp_vtag_first;
320 1.1 rjs struct mbuf *pkt, *pkt_last, *sb_last_mpkt;
321 1.1 rjs struct mbuf *control;
322 1.1 rjs #if !(defined(__FreeBSD__) || defined(__APPLE__))
323 1.1 rjs #ifndef INP_IPV6
324 1.1 rjs #define INP_IPV6 0x1
325 1.1 rjs #endif
326 1.1 rjs #ifndef INP_IPV4
327 1.1 rjs #define INP_IPV4 0x2
328 1.1 rjs #endif
329 1.1 rjs u_char inp_vflag;
330 1.1 rjs u_char inp_ip_ttl;
331 1.1 rjs u_char inp_ip_tos;
332 1.1 rjs u_char inp_ip_resv;
333 1.1 rjs #endif
334 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
335 1.1 rjs struct mtx inp_mtx;
336 1.1 rjs struct mtx inp_create_mtx;
337 1.1 rjs u_int32_t refcount;
338 1.1 rjs #elif defined(__NetBSD__)
339 1.1 rjs kmutex_t inp_mtx;
340 1.1 rjs kmutex_t inp_create_mtx;
341 1.1 rjs u_int32_t refcount;
342 1.1 rjs #endif
343 1.1 rjs };
344 1.1 rjs
345 1.1 rjs struct sctp_tcb {
346 1.1 rjs struct socket *sctp_socket; /* back pointer to socket */
347 1.1 rjs struct sctp_inpcb *sctp_ep; /* back pointer to ep */
348 1.1 rjs LIST_ENTRY(sctp_tcb) sctp_tcbhash; /* next link in hash table */
349 1.1 rjs LIST_ENTRY(sctp_tcb) sctp_tcblist; /* list of all of the TCB's */
350 1.1 rjs LIST_ENTRY(sctp_tcb) sctp_asocs;
351 1.1 rjs struct sctp_association asoc;
352 1.1 rjs uint16_t rport; /* remote port in network format */
353 1.1 rjs uint16_t resv;
354 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
355 1.1 rjs struct mtx tcb_mtx;
356 1.1 rjs #elif defined(__NetBSD__)
357 1.1 rjs kmutex_t tcb_mtx;
358 1.1 rjs #endif
359 1.1 rjs };
360 1.1 rjs
361 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
362 1.1 rjs
363 1.1 rjs /* General locking concepts:
364 1.1 rjs * The goal of our locking is to of course provide
365 1.1 rjs * consistency and yet minimize overhead. We will
366 1.1 rjs * attempt to use non-recursive locks which are supposed
367 1.1 rjs * to be quite inexpensive. Now in order to do this the goal
368 1.1 rjs * is that most functions are not aware of locking. Once we
369 1.1 rjs * have a TCB we lock it and unlock when we are through. This
370 1.1 rjs * means that the TCB lock is kind-of a "global" lock when
371 1.1 rjs * working on an association. Caution must be used when
372 1.1 rjs * asserting a TCB_LOCK since if we recurse we deadlock.
373 1.1 rjs *
374 1.1 rjs * Most other locks (INP and INFO) attempt to localize
375 1.1 rjs * the locking i.e. we try to contain the lock and
376 1.1 rjs * unlock within the function that needs to lock it. This
377 1.1 rjs * sometimes mean we do extra locks and unlocks and loose
378 1.1 rjs * a bit of efficency, but if the performance statements about
379 1.1 rjs * non-recursive locks are true this should not be a problem.
380 1.1 rjs * One issue that arises with this only lock when needed
381 1.1 rjs * is that if an implicit association setup is done we
382 1.1 rjs * have a problem. If at the time I lookup an association
383 1.1 rjs * I have NULL in the tcb return, by the time I call to
384 1.1 rjs * create the association some other processor could
385 1.1 rjs * have created it. This is what the CREATE lock on
386 1.1 rjs * the endpoint. Places where we will be implicitly
387 1.1 rjs * creating the association OR just creating an association
388 1.1 rjs * (the connect call) will assert the CREATE_INP lock. This
389 1.1 rjs * will assure us that during all the lookup of INP and INFO
390 1.1 rjs * if another creator is also locking/looking up we can
391 1.1 rjs * gate the two to synchronize. So the CREATE_INP lock is
392 1.1 rjs * also another one we must use extreme caution in locking
393 1.1 rjs * to make sure we don't hit a re-entrancy issue.
394 1.1 rjs *
395 1.1 rjs * For non FreeBSD 5.x and above we provide a bunch
396 1.1 rjs * of EMPTY lock macro's so we can blatantly put locks
397 1.1 rjs * everywhere and they reduce to nothing on NetBSD/OpenBSD
398 1.1 rjs * and FreeBSD 4.x
399 1.1 rjs *
400 1.1 rjs */
401 1.1 rjs
402 1.1 rjs
403 1.1 rjs /* When working with the global SCTP lists we lock and unlock
404 1.1 rjs * the INP_INFO lock. So when we go to lookup an association
405 1.1 rjs * we will want to do a SCTP_INP_INFO_RLOCK() and then when
406 1.1 rjs * we want to add a new association to the sctppcbinfo list's
407 1.1 rjs * we will do a SCTP_INP_INFO_WLOCK().
408 1.1 rjs */
409 1.1 rjs
410 1.1 rjs /*
411 1.1 rjs * FIX ME, all locks right now have a
412 1.1 rjs * recursive check/panic to validate that I
413 1.1 rjs * don't have any lock recursion going on.
414 1.1 rjs */
415 1.1 rjs
416 1.1 rjs #define SCTP_INP_INFO_LOCK_INIT() \
417 1.1 rjs mtx_init(&sctppcbinfo.ipi_ep_mtx, "sctp", "inp_info", MTX_DEF)
418 1.1 rjs
419 1.1 rjs #ifdef xyzzy
420 1.1 rjs #define SCTP_INP_INFO_RLOCK() do { \
421 1.1 rjs if (mtx_owned(&sctppcbinfo.ipi_ep_mtx)) \
422 1.1 rjs panic("INP INFO Recursive Lock-R"); \
423 1.1 rjs mtx_lock(&sctppcbinfo.ipi_ep_mtx); \
424 1.1 rjs } while (0)
425 1.1 rjs
426 1.1 rjs #define SCTP_INP_INFO_WLOCK() do { \
427 1.1 rjs if (mtx_owned(&sctppcbinfo.ipi_ep_mtx)) \
428 1.1 rjs panic("INP INFO Recursive Lock-W"); \
429 1.1 rjs mtx_lock(&sctppcbinfo.ipi_ep_mtx); \
430 1.1 rjs } while (0)
431 1.1 rjs
432 1.1 rjs #else
433 1.1 rjs
434 1.1 rjs void SCTP_INP_INFO_RLOCK(void);
435 1.1 rjs void SCTP_INP_INFO_WLOCK(void);
436 1.1 rjs
437 1.1 rjs #endif
438 1.1 rjs
439 1.1 rjs #define SCTP_INP_INFO_RUNLOCK() mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
440 1.1 rjs #define SCTP_INP_INFO_WUNLOCK() mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
441 1.1 rjs
442 1.1 rjs /* The INP locks we will use for locking an SCTP endpoint, so for
443 1.1 rjs * example if we want to change something at the endpoint level for
444 1.3 riastrad * example cookie secrets we lock the INP level.
445 1.1 rjs */
446 1.1 rjs #define SCTP_INP_LOCK_INIT(_inp) \
447 1.1 rjs mtx_init(&(_inp)->inp_mtx, "sctp", "inp", MTX_DEF | MTX_DUPOK)
448 1.1 rjs
449 1.1 rjs #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
450 1.1 rjs mtx_init(&(_inp)->inp_create_mtx, "sctp", "inp_create", \
451 1.1 rjs MTX_DEF | MTX_DUPOK)
452 1.1 rjs
453 1.1 rjs #define SCTP_INP_LOCK_DESTROY(_inp) mtx_destroy(&(_inp)->inp_mtx)
454 1.1 rjs #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) mtx_destroy(&(_inp)->inp_create_mtx)
455 1.1 rjs
456 1.1 rjs #ifdef xyzzy
457 1.1 rjs #define SCTP_INP_RLOCK(_inp) do { \
458 1.1 rjs struct sctp_tcb *xx_stcb; \
459 1.1 rjs xx_stcb = LIST_FIRST(&_inp->sctp_asoc_list); \
460 1.1 rjs if (xx_stcb) \
461 1.1 rjs if (mtx_owned(&(xx_stcb)->tcb_mtx)) \
462 1.1 rjs panic("I own TCB lock?"); \
463 1.1 rjs if (mtx_owned(&(_inp)->inp_mtx)) \
464 1.1 rjs panic("INP Recursive Lock-R"); \
465 1.1 rjs mtx_lock(&(_inp)->inp_mtx); \
466 1.1 rjs } while (0)
467 1.1 rjs
468 1.1 rjs #define SCTP_INP_WLOCK(_inp) do { \
469 1.1 rjs struct sctp_tcb *xx_stcb; \
470 1.1 rjs xx_stcb = LIST_FIRST(&_inp->sctp_asoc_list); \
471 1.1 rjs if (xx_stcb) \
472 1.1 rjs if (mtx_owned(&(xx_stcb)->tcb_mtx)) \
473 1.1 rjs panic("I own TCB lock?"); \
474 1.1 rjs if (mtx_owned(&(_inp)->inp_mtx)) \
475 1.1 rjs panic("INP Recursive Lock-W"); \
476 1.1 rjs mtx_lock(&(_inp)->inp_mtx); \
477 1.1 rjs } while (0)
478 1.1 rjs
479 1.1 rjs #else
480 1.1 rjs void SCTP_INP_RLOCK(struct sctp_inpcb *);
481 1.1 rjs void SCTP_INP_WLOCK(struct sctp_inpcb *);
482 1.1 rjs
483 1.1 rjs #endif
484 1.1 rjs
485 1.1 rjs
486 1.1 rjs #define SCTP_INP_INCR_REF(_inp) _inp->refcount++
487 1.1 rjs
488 1.1 rjs #define SCTP_INP_DECR_REF(_inp) do { \
489 1.1 rjs if (_inp->refcount > 0) \
490 1.1 rjs _inp->refcount--; \
491 1.1 rjs else \
492 1.1 rjs panic("bad inp refcount"); \
493 1.1 rjs }while (0)
494 1.1 rjs
495 1.1 rjs #define SCTP_ASOC_CREATE_LOCK(_inp) do { \
496 1.1 rjs if (mtx_owned(&(_inp)->inp_create_mtx)) \
497 1.1 rjs panic("INP Recursive CREATE"); \
498 1.1 rjs mtx_lock(&(_inp)->inp_create_mtx); \
499 1.1 rjs } while (0)
500 1.1 rjs
501 1.1 rjs #define SCTP_INP_RUNLOCK(_inp) mtx_unlock(&(_inp)->inp_mtx)
502 1.1 rjs #define SCTP_INP_WUNLOCK(_inp) mtx_unlock(&(_inp)->inp_mtx)
503 1.1 rjs #define SCTP_ASOC_CREATE_UNLOCK(_inp) mtx_unlock(&(_inp)->inp_create_mtx)
504 1.1 rjs
505 1.1 rjs /* For the majority of things (once we have found the association) we
506 1.1 rjs * will lock the actual association mutex. This will protect all
507 1.1 rjs * the assoiciation level queues and streams and such. We will
508 1.1 rjs * need to lock the socket layer when we stuff data up into
509 1.1 rjs * the receiving sb_mb. I.e. we will need to do an extra
510 1.1 rjs * SOCKBUF_LOCK(&so->so_rcv) even though the association is
511 1.1 rjs * locked.
512 1.1 rjs */
513 1.1 rjs
514 1.1 rjs #define SCTP_TCB_LOCK_INIT(_tcb) \
515 1.1 rjs mutex_init(&(_tcb)->tcb_mtx, MUTEX_DEFAULT, IPL_NET)
516 1.1 rjs #define SCTP_TCB_LOCK_DESTROY(_tcb) mtx_destroy(&(_tcb)->tcb_mtx)
517 1.1 rjs #define SCTP_TCB_LOCK(_tcb) do { \
518 1.1 rjs if (!mtx_owned(&(_tcb->sctp_ep->inp_mtx))) \
519 1.1 rjs panic("TCB locking and no INP lock"); \
520 1.1 rjs if (mtx_owned(&(_tcb)->tcb_mtx)) \
521 1.1 rjs panic("TCB Lock-recursive"); \
522 1.1 rjs mtx_lock(&(_tcb)->tcb_mtx); \
523 1.1 rjs } while (0)
524 1.1 rjs #define SCTP_TCB_UNLOCK(_tcb) mtx_unlock(&(_tcb)->tcb_mtx)
525 1.1 rjs
526 1.1 rjs #define SCTP_ITERATOR_LOCK_INIT() \
527 1.1 rjs mtx_init(&sctppcbinfo.it_mtx, "sctp", "iterator", MTX_DEF)
528 1.1 rjs #define SCTP_ITERATOR_LOCK() do { \
529 1.1 rjs if (mtx_owned(&sctppcbinfo.it_mtx)) \
530 1.1 rjs panic("Iterator Lock"); \
531 1.1 rjs mtx_lock(&sctppcbinfo.it_mtx); \
532 1.1 rjs } while (0)
533 1.1 rjs
534 1.1 rjs #define SCTP_ITERATOR_UNLOCK() mtx_unlock(&sctppcbinfo.it_mtx)
535 1.1 rjs #define SCTP_ITERATOR_LOCK_DESTROY() mtx_destroy(&sctppcbinfo.it_mtx)
536 1.1 rjs #elif 0 /* defined(__NetBSD__) */
537 1.1 rjs #define SCTP_INP_INFO_LOCK_INIT() \
538 1.1 rjs rw_init(&sctppcbinfo.ipi_ep_mtx)
539 1.1 rjs
540 1.1 rjs #define SCTP_INP_INFO_RLOCK() do { \
541 1.1 rjs rw_enter(&sctppcbinfo.ipi_ep_mtx, RW_READER); \
542 1.1 rjs } while (0)
543 1.1 rjs
544 1.1 rjs #define SCTP_INP_INFO_WLOCK() do { \
545 1.1 rjs rw_enter(&sctppcbinfo.ipi_ep_mtx, RW_WRITER); \
546 1.1 rjs } while (0)
547 1.1 rjs
548 1.1 rjs #define SCTP_INP_INFO_RUNLOCK() rw_exit(&sctppcbinfo.ipi_ep_mtx)
549 1.1 rjs #define SCTP_INP_INFO_WUNLOCK() rw_exit(&sctppcbinfo.ipi_ep_mtx)
550 1.1 rjs
551 1.1 rjs /* The INP locks we will use for locking an SCTP endpoint, so for
552 1.1 rjs * example if we want to change something at the endpoint level for
553 1.3 riastrad * example cookie secrets we lock the INP level.
554 1.1 rjs */
555 1.1 rjs #define SCTP_INP_LOCK_INIT(_inp) \
556 1.1 rjs mutex_init(&(_inp)->inp_mtx, MUTEX_DEFAULT, IPL_NET)
557 1.1 rjs
558 1.1 rjs #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
559 1.1 rjs mutex_init(&(_inp)->inp_create_mtx, MUTEX_DEFAULT, IPL_NET)
560 1.1 rjs
561 1.1 rjs #define SCTP_INP_LOCK_DESTROY(_inp) mutex_destroy(&(_inp)->inp_mtx)
562 1.1 rjs #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) mutex_destroy(&(_inp)->inp_create_mtx)
563 1.1 rjs
564 1.1 rjs #define SCTP_INP_RLOCK(_inp) do { \
565 1.1 rjs mutex_enter(&(_inp)->inp_mtx); \
566 1.1 rjs } while (0)
567 1.1 rjs
568 1.1 rjs #define SCTP_INP_WLOCK(_inp) do { \
569 1.1 rjs mutex_enter(&(_inp)->inp_mtx); \
570 1.1 rjs } while (0)
571 1.1 rjs
572 1.1 rjs
573 1.1 rjs #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
574 1.1 rjs
575 1.1 rjs #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
576 1.1 rjs
577 1.1 rjs #define SCTP_ASOC_CREATE_LOCK(_inp) do { \
578 1.1 rjs mutex_enter(&(_inp)->inp_create_mtx); \
579 1.1 rjs } while (0)
580 1.1 rjs
581 1.1 rjs #define SCTP_INP_RUNLOCK(_inp) mutex_exit(&(_inp)->inp_mtx)
582 1.1 rjs #define SCTP_INP_WUNLOCK(_inp) mutex_exit(&(_inp)->inp_mtx)
583 1.1 rjs #define SCTP_ASOC_CREATE_UNLOCK(_inp) mutex_exit(&(_inp)->inp_create_mtx)
584 1.1 rjs
585 1.1 rjs /* For the majority of things (once we have found the association) we
586 1.1 rjs * will lock the actual association mutex. This will protect all
587 1.1 rjs * the assoiciation level queues and streams and such. We will
588 1.1 rjs * need to lock the socket layer when we stuff data up into
589 1.1 rjs * the receiving sb_mb. I.e. we will need to do an extra
590 1.1 rjs * SOCKBUF_LOCK(&so->so_rcv) even though the association is
591 1.1 rjs * locked.
592 1.1 rjs */
593 1.1 rjs
594 1.1 rjs #define SCTP_TCB_LOCK_INIT(_tcb) \
595 1.1 rjs mutex_init(&(_tcb)->tcb_mtx, MUTEX_DEFAULT, IPL_NET)
596 1.1 rjs #define SCTP_TCB_LOCK_DESTROY(_tcb) mutex_destroy(&(_tcb)->tcb_mtx)
597 1.1 rjs #define SCTP_TCB_LOCK(_tcb) do { \
598 1.1 rjs mutex_enter(&(_tcb)->tcb_mtx); \
599 1.1 rjs } while (0)
600 1.1 rjs #define SCTP_TCB_UNLOCK(_tcb) mutex_exit(&(_tcb)->tcb_mtx)
601 1.1 rjs
602 1.1 rjs #define SCTP_ITERATOR_LOCK_INIT() \
603 1.1 rjs mutex_init(&sctppcbinfo.it_mtx, MUTEX_DEFAULT, IPL_NET)
604 1.1 rjs #define SCTP_ITERATOR_LOCK() do { \
605 1.1 rjs if (mutex_owned(&sctppcbinfo.it_mtx)) \
606 1.1 rjs panic("Iterator Lock"); \
607 1.1 rjs mutex_enter(&sctppcbinfo.it_mtx); \
608 1.1 rjs } while (0)
609 1.1 rjs
610 1.1 rjs #define SCTP_ITERATOR_UNLOCK() mutex_exit(&sctppcbinfo.it_mtx)
611 1.1 rjs #define SCTP_ITERATOR_LOCK_DESTROY() mutex_destroy(&sctppcbinfo.it_mtx)
612 1.1 rjs #else
613 1.1 rjs
614 1.1 rjs /* Empty Lock declarations for all other
615 1.1 rjs * platforms pre-process away to nothing.
616 1.1 rjs */
617 1.1 rjs
618 1.1 rjs /* Lock for INFO stuff */
619 1.1 rjs #define SCTP_INP_INFO_LOCK_INIT()
620 1.1 rjs #define SCTP_INP_INFO_RLOCK()
621 1.1 rjs #define SCTP_INP_INFO_RLOCK()
622 1.1 rjs #define SCTP_INP_INFO_WLOCK()
623 1.1 rjs
624 1.1 rjs #define SCTP_INP_INFO_RUNLOCK()
625 1.1 rjs #define SCTP_INP_INFO_WUNLOCK()
626 1.1 rjs /* Lock for INP */
627 1.1 rjs #define SCTP_INP_LOCK_INIT(_inp)
628 1.1 rjs #define SCTP_INP_LOCK_DESTROY(_inp)
629 1.1 rjs #define SCTP_INP_RLOCK(_inp)
630 1.1 rjs #define SCTP_INP_RUNLOCK(_inp)
631 1.1 rjs #define SCTP_INP_WLOCK(_inp)
632 1.1 rjs #define SCTP_INP_INCR_REF(_inp)
633 1.1 rjs #define SCTP_INP_DECR_REF(_inp)
634 1.1 rjs #define SCTP_INP_WUNLOCK(_inp)
635 1.1 rjs #define SCTP_ASOC_CREATE_LOCK_INIT(_inp)
636 1.1 rjs #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp)
637 1.1 rjs #define SCTP_ASOC_CREATE_LOCK(_inp)
638 1.1 rjs #define SCTP_ASOC_CREATE_UNLOCK(_inp)
639 1.1 rjs /* Lock for TCB */
640 1.1 rjs #define SCTP_TCB_LOCK_INIT(_tcb)
641 1.1 rjs #define SCTP_TCB_LOCK_DESTROY(_tcb)
642 1.1 rjs #define SCTP_TCB_LOCK(_tcb)
643 1.1 rjs #define SCTP_TCB_UNLOCK(_tcb)
644 1.1 rjs /* iterator locks */
645 1.1 rjs #define SCTP_ITERATOR_LOCK_INIT()
646 1.1 rjs #define SCTP_ITERATOR_LOCK()
647 1.1 rjs #define SCTP_ITERATOR_UNLOCK()
648 1.1 rjs #define SCTP_ITERATOR_LOCK_DESTROY()
649 1.1 rjs #endif
650 1.1 rjs
651 1.1 rjs #if defined(_KERNEL)
652 1.1 rjs
653 1.1 rjs extern struct sctp_epinfo sctppcbinfo;
654 1.1 rjs extern int sctp_auto_asconf;
655 1.1 rjs
656 1.1 rjs int SCTP6_ARE_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b);
657 1.1 rjs
658 1.1 rjs void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
659 1.1 rjs
660 1.1 rjs struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
661 1.1 rjs
662 1.1 rjs struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int);
663 1.1 rjs
664 1.1 rjs int sctp_inpcb_bind(struct socket *, struct sockaddr *, struct lwp *);
665 1.1 rjs
666 1.1 rjs struct sctp_tcb *sctp_findassociation_addr(struct mbuf *, int, int,
667 1.1 rjs struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
668 1.1 rjs struct sctp_nets **);
669 1.1 rjs
670 1.1 rjs struct sctp_tcb *sctp_findassociation_addr_sa(struct sockaddr *,
671 1.1 rjs struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int);
672 1.1 rjs
673 1.1 rjs void sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
674 1.1 rjs struct sctp_tcb *);
675 1.1 rjs
676 1.1 rjs /*
677 1.1 rjs * For this call ep_addr, the to is the destination endpoint address
678 1.1 rjs * of the peer (relative to outbound). The from field is only used if
679 1.1 rjs * the TCP model is enabled and helps distingush amongst the subset
680 1.1 rjs * bound (non-boundall). The TCP model MAY change the actual ep field,
681 1.1 rjs * this is why it is passed.
682 1.1 rjs */
683 1.1 rjs struct sctp_tcb *sctp_findassociation_ep_addr(struct sctp_inpcb **,
684 1.1 rjs struct sockaddr *, struct sctp_nets **, struct sockaddr *, struct sctp_tcb *);
685 1.1 rjs
686 1.1 rjs struct sctp_tcb *sctp_findassociation_ep_asocid(struct sctp_inpcb *, vaddr_t);
687 1.1 rjs
688 1.1 rjs struct sctp_tcb *sctp_findassociation_ep_asconf(struct mbuf *, int, int,
689 1.1 rjs struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **);
690 1.1 rjs
691 1.1 rjs int sctp_inpcb_alloc(struct socket *);
692 1.1 rjs
693 1.1 rjs
694 1.1 rjs int sctp_is_address_on_local_host(struct sockaddr *addr);
695 1.1 rjs
696 1.1 rjs void sctp_inpcb_free(struct sctp_inpcb *, int);
697 1.1 rjs
698 1.1 rjs struct sctp_tcb *sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
699 1.1 rjs int, int *, uint32_t);
700 1.1 rjs
701 1.1 rjs void sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *);
702 1.1 rjs
703 1.1 rjs int sctp_add_local_addr_ep(struct sctp_inpcb *, struct ifaddr *);
704 1.1 rjs
705 1.1 rjs int sctp_insert_laddr(struct sctpladdr *, struct ifaddr *);
706 1.1 rjs
707 1.1 rjs void sctp_remove_laddr(struct sctp_laddr *);
708 1.1 rjs
709 1.1 rjs int sctp_del_local_addr_ep(struct sctp_inpcb *, struct ifaddr *);
710 1.1 rjs
711 1.1 rjs int sctp_del_local_addr_ep_sa(struct sctp_inpcb *, struct sockaddr *);
712 1.1 rjs
713 1.1 rjs int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, int, int);
714 1.1 rjs
715 1.1 rjs int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
716 1.1 rjs
717 1.1 rjs void sctp_pcb_init(void);
718 1.1 rjs
719 1.1 rjs void sctp_free_remote_addr(struct sctp_nets *);
720 1.1 rjs
721 1.1 rjs int sctp_add_local_addr_assoc(struct sctp_tcb *, struct ifaddr *);
722 1.1 rjs
723 1.1 rjs int sctp_del_local_addr_assoc(struct sctp_tcb *, struct ifaddr *);
724 1.1 rjs
725 1.1 rjs int sctp_del_local_addr_assoc_sa(struct sctp_tcb *, struct sockaddr *);
726 1.1 rjs
727 1.1 rjs int sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
728 1.1 rjs int, struct sctphdr *, struct sockaddr *);
729 1.1 rjs
730 1.1 rjs int sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *, struct sctp_nets *);
731 1.1 rjs
732 1.1 rjs int sctp_is_vtag_good(struct sctp_inpcb *, uint32_t, struct timeval *);
733 1.1 rjs
734 1.1 rjs /*void sctp_drain(void);*/
735 1.1 rjs
736 1.1 rjs int sctp_destination_is_reachable(struct sctp_tcb *, const struct sockaddr *);
737 1.1 rjs
738 1.1 rjs int sctp_add_to_socket_q(struct sctp_inpcb *, struct sctp_tcb *);
739 1.1 rjs
740 1.1 rjs struct sctp_tcb *sctp_remove_from_socket_q(struct sctp_inpcb *);
741 1.1 rjs
742 1.1 rjs
743 1.1 rjs /* Null in last arg inpcb indicate run on ALL ep's. Specific
744 1.1 rjs * inp in last arg indicates run on ONLY assoc's of the
745 1.1 rjs * specified endpoint.
746 1.1 rjs */
747 1.1 rjs int
748 1.1 rjs sctp_initiate_iterator(asoc_func af, uint32_t, uint32_t, void *, uint32_t,
749 1.1 rjs end_func ef, struct sctp_inpcb *);
750 1.1 rjs
751 1.2 rjs void in6_sin6_2_sin (struct sockaddr_in *,
752 1.1 rjs struct sockaddr_in6 *sin6);
753 1.1 rjs
754 1.1 rjs #endif /* _KERNEL */
755 1.1 rjs #endif /* !__SCTP_PCB_H__ */
756