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