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