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