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