xref: /netbsd-src/sys/netinet/sctp_pcb.h (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$KAME: sctp_pcb.h,v 1.21 2005/07/16 01:18:47 suz Exp $	*/
2 /*	$NetBSD: sctp_pcb.h,v 1.3 2020/01/19 20:51:13 riastradh 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 	/*
254 	 * This timer is kept running per endpoint.  When it fires it
255 	 * will change the secret key.  The default is once a hour
256 	 */
257 	struct sctp_timer signature_change;
258 	int def_cookie_life;
259 	/* defaults to 0 */
260 	int auto_close_time;
261 	uint32_t initial_sequence_debug;
262 	uint32_t adaption_layer_indicator;
263 	uint8_t max_burst;
264 	char current_secret_number;
265 	char last_secret_number;
266 };
267 
268 #ifndef SCTP_ALIGNMENT
269 #define SCTP_ALIGNMENT 32
270 #endif
271 
272 #ifndef SCTP_ALIGNM1
273 #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
274 #endif
275 
276 #define sctp_lport ip_inp.inp.inp_lport
277 
278 struct sctp_socket_q_list {
279 	struct sctp_tcb *tcb;
280 	TAILQ_ENTRY(sctp_socket_q_list) next_sq;
281 };
282 
283 struct sctp_inpcb {
284 	/*
285 	 * put an inpcb in front of it all, kind of a waste but we need
286 	 * to for compatability with all the other stuff.
287 	 */
288 	union {
289 		struct inpcb inp;
290 		char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
291 			  ~SCTP_ALIGNM1];
292 	} ip_inp;
293 	LIST_ENTRY(sctp_inpcb) sctp_list;	/* lists all endpoints */
294 	/* hash of all endpoints for model */
295 	LIST_ENTRY(sctp_inpcb) sctp_hash;
296 
297 	/* count of local addresses bound, 0 if bound all */
298 	int laddr_count;
299 	/* list of addrs in use by the EP */
300 	struct sctpladdr sctp_addr_list;
301 	/* used for source address selection rotation */
302 	struct sctp_laddr *next_addr_touse;
303 	struct ifnet *next_ifn_touse;
304 	/* back pointer to our socket */
305 	struct socket *sctp_socket;
306 	uint32_t sctp_flags;			/* flag set */
307 	struct sctp_pcb sctp_ep;		/* SCTP ep data */
308 	/* head of the hash of all associations */
309 	struct sctpasochead *sctp_tcbhash;
310 	u_long sctp_hashmark;
311 	/* head of the list of all associations */
312 	struct sctpasochead sctp_asoc_list;
313 	/* queue of TCB's waiting to stuff data up the socket */
314 	struct sctpsocketq sctp_queue_list;
315 	void *sctp_tcb_at_block;
316 	struct sctp_iterator *inp_starting_point_for_iterator;
317 	int  error_on_block;
318 	uint32_t sctp_frag_point;
319 	uint32_t sctp_vtag_first;
320 	struct mbuf *pkt, *pkt_last, *sb_last_mpkt;
321 	struct mbuf *control;
322 #if !(defined(__FreeBSD__) || defined(__APPLE__))
323 #ifndef INP_IPV6
324 #define INP_IPV6	0x1
325 #endif
326 #ifndef INP_IPV4
327 #define INP_IPV4	0x2
328 #endif
329 	u_char inp_vflag;
330 	u_char inp_ip_ttl;
331 	u_char inp_ip_tos;
332 	u_char inp_ip_resv;
333 #endif
334 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
335 	struct mtx inp_mtx;
336 	struct mtx inp_create_mtx;
337 	u_int32_t refcount;
338 #elif defined(__NetBSD__)
339 	kmutex_t inp_mtx;
340 	kmutex_t inp_create_mtx;
341 	u_int32_t refcount;
342 #endif
343 };
344 
345 struct sctp_tcb {
346 	struct socket *sctp_socket;		/* back pointer to socket */
347 	struct sctp_inpcb *sctp_ep;		/* back pointer to ep */
348 	LIST_ENTRY(sctp_tcb) sctp_tcbhash;	/* next link in hash table */
349 	LIST_ENTRY(sctp_tcb) sctp_tcblist;	/* list of all of the TCB's */
350 	LIST_ENTRY(sctp_tcb) sctp_asocs;
351 	struct sctp_association asoc;
352 	uint16_t rport;			/* remote port in network format */
353 	uint16_t resv;
354 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
355 	struct mtx tcb_mtx;
356 #elif defined(__NetBSD__)
357 	kmutex_t tcb_mtx;
358 #endif
359 };
360 
361 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
362 
363 /* General locking concepts:
364  * The goal of our locking is to of course provide
365  * consistency and yet minimize overhead. We will
366  * attempt to use non-recursive locks which are supposed
367  * to be quite inexpensive. Now in order to do this the goal
368  * is that most functions are not aware of locking. Once we
369  * have a TCB we lock it and unlock when we are through. This
370  * means that the TCB lock is kind-of a "global" lock when
371  * working on an association. Caution must be used when
372  * asserting a TCB_LOCK since if we recurse we deadlock.
373  *
374  * Most other locks (INP and INFO) attempt to localize
375  * the locking i.e. we try to contain the lock and
376  * unlock within the function that needs to lock it. This
377  * sometimes mean we do extra locks and unlocks and loose
378  * a bit of efficency, but if the performance statements about
379  * non-recursive locks are true this should not be a problem.
380  * One issue that arises with this only lock when needed
381  * is that if an implicit association setup is done we
382  * have a problem. If at the time I lookup an association
383  * I have NULL in the tcb return, by the time I call to
384  * create the association some other processor could
385  * have created it. This is what the CREATE lock on
386  * the endpoint. Places where we will be implicitly
387  * creating the association OR just creating an association
388  * (the connect call) will assert the CREATE_INP lock. This
389  * will assure us that during all the lookup of INP and INFO
390  * if another creator is also locking/looking up we can
391  * gate the two to synchronize. So the CREATE_INP lock is
392  * also another one we must use extreme caution in locking
393  * to make sure we don't hit a re-entrancy issue.
394  *
395  * For non FreeBSD 5.x and above we provide a bunch
396  * of EMPTY lock macro's so we can blatantly put locks
397  * everywhere and they reduce to nothing on NetBSD/OpenBSD
398  * and FreeBSD 4.x
399  *
400  */
401 
402 
403 /* When working with the global SCTP lists we lock and unlock
404  * the INP_INFO lock. So when we go to lookup an association
405  * we will want to do a SCTP_INP_INFO_RLOCK() and then when
406  * we want to add a new association to the sctppcbinfo list's
407  * we will do a SCTP_INP_INFO_WLOCK().
408  */
409 
410 /*
411  * FIX ME, all locks right now have a
412  * recursive check/panic to validate that I
413  * don't have any lock recursion going on.
414  */
415 
416 #define SCTP_INP_INFO_LOCK_INIT() \
417         mtx_init(&sctppcbinfo.ipi_ep_mtx, "sctp", "inp_info", MTX_DEF)
418 
419 #ifdef xyzzy
420 #define SCTP_INP_INFO_RLOCK()	do { 					\
421              if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))                     \
422 		panic("INP INFO Recursive Lock-R");                     \
423              mtx_lock(&sctppcbinfo.ipi_ep_mtx);                         \
424 } while (0)
425 
426 #define SCTP_INP_INFO_WLOCK()	do { 					\
427              if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))                     \
428 		panic("INP INFO Recursive Lock-W");                     \
429              mtx_lock(&sctppcbinfo.ipi_ep_mtx);                         \
430 } while (0)
431 
432 #else
433 
434 void SCTP_INP_INFO_RLOCK(void);
435 void SCTP_INP_INFO_WLOCK(void);
436 
437 #endif
438 
439 #define SCTP_INP_INFO_RUNLOCK()		mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
440 #define SCTP_INP_INFO_WUNLOCK()		mtx_unlock(&sctppcbinfo.ipi_ep_mtx)
441 
442 /* The INP locks we will use for locking an SCTP endpoint, so for
443  * example if we want to change something at the endpoint level for
444  * example cookie secrets we lock the INP level.
445  */
446 #define SCTP_INP_LOCK_INIT(_inp) \
447 	mtx_init(&(_inp)->inp_mtx, "sctp", "inp", MTX_DEF | MTX_DUPOK)
448 
449 #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
450 	mtx_init(&(_inp)->inp_create_mtx, "sctp", "inp_create", \
451 		 MTX_DEF | MTX_DUPOK)
452 
453 #define SCTP_INP_LOCK_DESTROY(_inp)	mtx_destroy(&(_inp)->inp_mtx)
454 #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp)	mtx_destroy(&(_inp)->inp_create_mtx)
455 
456 #ifdef xyzzy
457 #define SCTP_INP_RLOCK(_inp)	do { 					\
458         struct sctp_tcb *xx_stcb;					\
459         xx_stcb = LIST_FIRST(&_inp->sctp_asoc_list);                    \
460         if (xx_stcb)                                                     \
461               if (mtx_owned(&(xx_stcb)->tcb_mtx))                        \
462                      panic("I own TCB lock?");                          \
463         if (mtx_owned(&(_inp)->inp_mtx))                                 \
464 		panic("INP Recursive Lock-R");                          \
465         mtx_lock(&(_inp)->inp_mtx);                                     \
466 } while (0)
467 
468 #define SCTP_INP_WLOCK(_inp)	do { 					\
469         struct sctp_tcb *xx_stcb;					\
470         xx_stcb = LIST_FIRST(&_inp->sctp_asoc_list);                    \
471         if (xx_stcb)                                                     \
472               if (mtx_owned(&(xx_stcb)->tcb_mtx))                        \
473                      panic("I own TCB lock?");                          \
474         if (mtx_owned(&(_inp)->inp_mtx))                                 \
475 		panic("INP Recursive Lock-W");                          \
476         mtx_lock(&(_inp)->inp_mtx);                                     \
477 } while (0)
478 
479 #else
480 void SCTP_INP_RLOCK(struct sctp_inpcb *);
481 void SCTP_INP_WLOCK(struct sctp_inpcb *);
482 
483 #endif
484 
485 
486 #define SCTP_INP_INCR_REF(_inp)        _inp->refcount++
487 
488 #define SCTP_INP_DECR_REF(_inp)         do {                                 \
489                                              if (_inp->refcount > 0)          \
490                                                   _inp->refcount--;          \
491                                              else                            \
492                                                   panic("bad inp refcount"); \
493 }while (0)
494 
495 #define SCTP_ASOC_CREATE_LOCK(_inp)  do {				\
496         if (mtx_owned(&(_inp)->inp_create_mtx))                          \
497 		panic("INP Recursive CREATE");                          \
498         mtx_lock(&(_inp)->inp_create_mtx);                              \
499 } while (0)
500 
501 #define SCTP_INP_RUNLOCK(_inp)		mtx_unlock(&(_inp)->inp_mtx)
502 #define SCTP_INP_WUNLOCK(_inp)		mtx_unlock(&(_inp)->inp_mtx)
503 #define SCTP_ASOC_CREATE_UNLOCK(_inp)	mtx_unlock(&(_inp)->inp_create_mtx)
504 
505 /* For the majority of things (once we have found the association) we
506  * will lock the actual association mutex. This will protect all
507  * the assoiciation level queues and streams and such. We will
508  * need to lock the socket layer when we stuff data up into
509  * the receiving sb_mb. I.e. we will need to do an extra
510  * SOCKBUF_LOCK(&so->so_rcv) even though the association is
511  * locked.
512  */
513 
514 #define SCTP_TCB_LOCK_INIT(_tcb) \
515 	mutex_init(&(_tcb)->tcb_mtx, MUTEX_DEFAULT, IPL_NET)
516 #define SCTP_TCB_LOCK_DESTROY(_tcb)	mtx_destroy(&(_tcb)->tcb_mtx)
517 #define SCTP_TCB_LOCK(_tcb)  do {					\
518         if (!mtx_owned(&(_tcb->sctp_ep->inp_mtx)))                       \
519 		panic("TCB locking and no INP lock");                   \
520         if (mtx_owned(&(_tcb)->tcb_mtx))                                 \
521 		panic("TCB Lock-recursive");                            \
522 	mtx_lock(&(_tcb)->tcb_mtx);                                     \
523 } while (0)
524 #define SCTP_TCB_UNLOCK(_tcb)		mtx_unlock(&(_tcb)->tcb_mtx)
525 
526 #define SCTP_ITERATOR_LOCK_INIT() \
527         mtx_init(&sctppcbinfo.it_mtx, "sctp", "iterator", MTX_DEF)
528 #define SCTP_ITERATOR_LOCK()  do {					\
529         if (mtx_owned(&sctppcbinfo.it_mtx))                              \
530 		panic("Iterator Lock");                                 \
531 	mtx_lock(&sctppcbinfo.it_mtx);                                  \
532 } while (0)
533 
534 #define SCTP_ITERATOR_UNLOCK()	        mtx_unlock(&sctppcbinfo.it_mtx)
535 #define SCTP_ITERATOR_LOCK_DESTROY()	mtx_destroy(&sctppcbinfo.it_mtx)
536 #elif 0 /* defined(__NetBSD__) */
537 #define SCTP_INP_INFO_LOCK_INIT() \
538 	rw_init(&sctppcbinfo.ipi_ep_mtx)
539 
540 #define SCTP_INP_INFO_RLOCK()	do { 					\
541 		rw_enter(&sctppcbinfo.ipi_ep_mtx, RW_READER);           \
542 } while (0)
543 
544 #define SCTP_INP_INFO_WLOCK()	do { 					\
545              rw_enter(&sctppcbinfo.ipi_ep_mtx, RW_WRITER);              \
546 } while (0)
547 
548 #define SCTP_INP_INFO_RUNLOCK()		rw_exit(&sctppcbinfo.ipi_ep_mtx)
549 #define SCTP_INP_INFO_WUNLOCK()		rw_exit(&sctppcbinfo.ipi_ep_mtx)
550 
551 /* The INP locks we will use for locking an SCTP endpoint, so for
552  * example if we want to change something at the endpoint level for
553  * example cookie secrets we lock the INP level.
554  */
555 #define SCTP_INP_LOCK_INIT(_inp) \
556 	mutex_init(&(_inp)->inp_mtx, MUTEX_DEFAULT, IPL_NET)
557 
558 #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \
559 	mutex_init(&(_inp)->inp_create_mtx, MUTEX_DEFAULT, IPL_NET)
560 
561 #define SCTP_INP_LOCK_DESTROY(_inp)	mutex_destroy(&(_inp)->inp_mtx)
562 #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp)	mutex_destroy(&(_inp)->inp_create_mtx)
563 
564 #define SCTP_INP_RLOCK(_inp)	do { 					\
565 	mutex_enter(&(_inp)->inp_mtx);                                  \
566 } while (0)
567 
568 #define SCTP_INP_WLOCK(_inp)	do { 					\
569 	mutex_enter(&(_inp)->inp_mtx);                                  \
570 } while (0)
571 
572 
573 #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1)
574 
575 #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1)
576 
577 #define SCTP_ASOC_CREATE_LOCK(_inp)  do {				\
578         mutex_enter(&(_inp)->inp_create_mtx);                              \
579 } while (0)
580 
581 #define SCTP_INP_RUNLOCK(_inp)		mutex_exit(&(_inp)->inp_mtx)
582 #define SCTP_INP_WUNLOCK(_inp)		mutex_exit(&(_inp)->inp_mtx)
583 #define SCTP_ASOC_CREATE_UNLOCK(_inp)	mutex_exit(&(_inp)->inp_create_mtx)
584 
585 /* For the majority of things (once we have found the association) we
586  * will lock the actual association mutex. This will protect all
587  * the assoiciation level queues and streams and such. We will
588  * need to lock the socket layer when we stuff data up into
589  * the receiving sb_mb. I.e. we will need to do an extra
590  * SOCKBUF_LOCK(&so->so_rcv) even though the association is
591  * locked.
592  */
593 
594 #define SCTP_TCB_LOCK_INIT(_tcb) \
595 	mutex_init(&(_tcb)->tcb_mtx, MUTEX_DEFAULT, IPL_NET)
596 #define SCTP_TCB_LOCK_DESTROY(_tcb)	mutex_destroy(&(_tcb)->tcb_mtx)
597 #define SCTP_TCB_LOCK(_tcb)  do {					\
598 	mutex_enter(&(_tcb)->tcb_mtx);                                     \
599 } while (0)
600 #define SCTP_TCB_UNLOCK(_tcb)		mutex_exit(&(_tcb)->tcb_mtx)
601 
602 #define SCTP_ITERATOR_LOCK_INIT() \
603         mutex_init(&sctppcbinfo.it_mtx, MUTEX_DEFAULT, IPL_NET)
604 #define SCTP_ITERATOR_LOCK()  do {					\
605         if (mutex_owned(&sctppcbinfo.it_mtx))                           \
606 		panic("Iterator Lock");                                 \
607 	mutex_enter(&sctppcbinfo.it_mtx);                               \
608 } while (0)
609 
610 #define SCTP_ITERATOR_UNLOCK()	        mutex_exit(&sctppcbinfo.it_mtx)
611 #define SCTP_ITERATOR_LOCK_DESTROY()	mutex_destroy(&sctppcbinfo.it_mtx)
612 #else
613 
614 /* Empty Lock declarations for all other
615  * platforms pre-process away to nothing.
616  */
617 
618 /* Lock for INFO stuff */
619 #define SCTP_INP_INFO_LOCK_INIT()
620 #define SCTP_INP_INFO_RLOCK()
621 #define SCTP_INP_INFO_RLOCK()
622 #define SCTP_INP_INFO_WLOCK()
623 
624 #define SCTP_INP_INFO_RUNLOCK()
625 #define SCTP_INP_INFO_WUNLOCK()
626 /* Lock for INP */
627 #define SCTP_INP_LOCK_INIT(_inp)
628 #define SCTP_INP_LOCK_DESTROY(_inp)
629 #define SCTP_INP_RLOCK(_inp)
630 #define SCTP_INP_RUNLOCK(_inp)
631 #define SCTP_INP_WLOCK(_inp)
632 #define SCTP_INP_INCR_REF(_inp)
633 #define SCTP_INP_DECR_REF(_inp)
634 #define SCTP_INP_WUNLOCK(_inp)
635 #define SCTP_ASOC_CREATE_LOCK_INIT(_inp)
636 #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp)
637 #define SCTP_ASOC_CREATE_LOCK(_inp)
638 #define SCTP_ASOC_CREATE_UNLOCK(_inp)
639 /* Lock for TCB */
640 #define SCTP_TCB_LOCK_INIT(_tcb)
641 #define SCTP_TCB_LOCK_DESTROY(_tcb)
642 #define SCTP_TCB_LOCK(_tcb)
643 #define SCTP_TCB_UNLOCK(_tcb)
644 /* iterator locks */
645 #define SCTP_ITERATOR_LOCK_INIT()
646 #define SCTP_ITERATOR_LOCK()
647 #define SCTP_ITERATOR_UNLOCK()
648 #define SCTP_ITERATOR_LOCK_DESTROY()
649 #endif
650 
651 #if defined(_KERNEL)
652 
653 extern struct sctp_epinfo sctppcbinfo;
654 extern int sctp_auto_asconf;
655 
656 int SCTP6_ARE_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b);
657 
658 void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
659 
660 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
661 
662 struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int);
663 
664 int sctp_inpcb_bind(struct socket *, struct sockaddr *, struct lwp *);
665 
666 struct sctp_tcb *sctp_findassociation_addr(struct mbuf *, int, int,
667     struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
668     struct sctp_nets **);
669 
670 struct sctp_tcb *sctp_findassociation_addr_sa(struct sockaddr *,
671 	struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int);
672 
673 void sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
674 	struct sctp_tcb *);
675 
676 /*
677  * For this call ep_addr, the to is the destination endpoint address
678  * of the peer (relative to outbound). The from field is only used if
679  * the TCP model is enabled and helps distingush amongst the subset
680  * bound (non-boundall). The TCP model MAY change the actual ep field,
681  * this is why it is passed.
682  */
683 struct sctp_tcb *sctp_findassociation_ep_addr(struct sctp_inpcb **,
684 	struct sockaddr *, struct sctp_nets **, struct sockaddr *, struct sctp_tcb *);
685 
686 struct sctp_tcb *sctp_findassociation_ep_asocid(struct sctp_inpcb *, vaddr_t);
687 
688 struct sctp_tcb *sctp_findassociation_ep_asconf(struct mbuf *, int, int,
689     struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **);
690 
691 int sctp_inpcb_alloc(struct socket *);
692 
693 
694 int sctp_is_address_on_local_host(struct sockaddr *addr);
695 
696 void sctp_inpcb_free(struct sctp_inpcb *, int);
697 
698 struct sctp_tcb *sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
699 	int, int *, uint32_t);
700 
701 void sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *);
702 
703 int sctp_add_local_addr_ep(struct sctp_inpcb *, struct ifaddr *);
704 
705 int sctp_insert_laddr(struct sctpladdr *, struct ifaddr *);
706 
707 void sctp_remove_laddr(struct sctp_laddr *);
708 
709 int sctp_del_local_addr_ep(struct sctp_inpcb *, struct ifaddr *);
710 
711 int sctp_del_local_addr_ep_sa(struct sctp_inpcb *, struct sockaddr *);
712 
713 int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, int, int);
714 
715 int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
716 
717 void sctp_pcb_init(void);
718 
719 void sctp_free_remote_addr(struct sctp_nets *);
720 
721 int sctp_add_local_addr_assoc(struct sctp_tcb *, struct ifaddr *);
722 
723 int sctp_del_local_addr_assoc(struct sctp_tcb *, struct ifaddr *);
724 
725 int sctp_del_local_addr_assoc_sa(struct sctp_tcb *, struct sockaddr *);
726 
727 int sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
728     int, struct sctphdr *, struct sockaddr *);
729 
730 int sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *, struct sctp_nets *);
731 
732 int sctp_is_vtag_good(struct sctp_inpcb *, uint32_t, struct timeval *);
733 
734 /*void sctp_drain(void);*/
735 
736 int sctp_destination_is_reachable(struct sctp_tcb *, const struct sockaddr *);
737 
738 int sctp_add_to_socket_q(struct sctp_inpcb *, struct sctp_tcb *);
739 
740 struct sctp_tcb *sctp_remove_from_socket_q(struct sctp_inpcb *);
741 
742 
743 /* Null in last arg inpcb indicate run on ALL ep's. Specific
744  * inp in last arg indicates run on ONLY assoc's of the
745  * specified endpoint.
746  */
747 int
748 sctp_initiate_iterator(asoc_func af, uint32_t, uint32_t, void *, uint32_t,
749 		       end_func ef, struct sctp_inpcb *);
750 
751 void in6_sin6_2_sin (struct sockaddr_in *,
752                             struct sockaddr_in6 *sin6);
753 
754 #endif /* _KERNEL */
755 #endif /* !__SCTP_PCB_H__ */
756