1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifdef __FreeBSD__
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.h 359379 2020-03-27 21:48:52Z tuexen $");
38 #endif
39 
40 #ifndef _NETINET_SCTP_PCB_H_
41 #define _NETINET_SCTP_PCB_H_
42 
43 #include <netinet/sctp_os.h>
44 #include <netinet/sctp.h>
45 #include <netinet/sctp_constants.h>
46 #include <netinet/sctp_sysctl.h>
47 
48 LIST_HEAD(sctppcbhead, sctp_inpcb);
49 LIST_HEAD(sctpasochead, sctp_tcb);
50 LIST_HEAD(sctpladdr, sctp_laddr);
51 LIST_HEAD(sctpvtaghead, sctp_tagblock);
52 LIST_HEAD(sctp_vrflist, sctp_vrf);
53 LIST_HEAD(sctp_ifnlist, sctp_ifn);
54 LIST_HEAD(sctp_ifalist, sctp_ifa);
55 TAILQ_HEAD(sctp_readhead, sctp_queued_to_read);
56 TAILQ_HEAD(sctp_streamhead, sctp_stream_queue_pending);
57 
58 #include <netinet/sctp_structs.h>
59 #include <netinet/sctp_auth.h>
60 
61 #define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask)
62 #define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask)
63 
64 struct sctp_vrf {
65 	LIST_ENTRY (sctp_vrf) next_vrf;
66 	struct sctp_ifalist *vrf_addr_hash;
67 	struct sctp_ifnlist ifnlist;
68 	uint32_t vrf_id;
69 	uint32_t tbl_id_v4;		/* default v4 table id */
70 	uint32_t tbl_id_v6;		/* default v6 table id */
71 	uint32_t total_ifa_count;
72 	u_long   vrf_addr_hashmark;
73 	uint32_t refcount;
74 };
75 
76 struct sctp_ifn {
77 	struct sctp_ifalist ifalist;
78 	struct sctp_vrf *vrf;
79 	LIST_ENTRY(sctp_ifn) next_ifn;
80 	LIST_ENTRY(sctp_ifn) next_bucket;
81 	void     *ifn_p;	/* never access without appropriate lock */
82 	uint32_t ifn_mtu;
83 	uint32_t ifn_type;
84 	uint32_t ifn_index;	/* shorthand way to look at ifn for reference */
85 	uint32_t refcount;	/* number of reference held should be >= ifa_count */
86 	uint32_t ifa_count;	/* IFA's we hold (in our list - ifalist)*/
87 	uint32_t num_v6;	/* number of v6 addresses */
88 	uint32_t num_v4;	/* number of v4 addresses */
89 	uint32_t registered_af;	/* registered address family for i/f events */
90 	char     ifn_name[SCTP_IFNAMSIZ];
91 };
92 
93 /* SCTP local IFA flags */
94 #define SCTP_ADDR_VALID         0x00000001	/* its up and active */
95 #define SCTP_BEING_DELETED      0x00000002	/* being deleted,
96 						 * when refcount = 0. Note
97 						 * that it is pulled from the ifn list
98 						 * and ifa_p is nulled right away but
99 						 * it cannot be freed until the last *net
100 						 * pointing to it is deleted.
101 						 */
102 #define SCTP_ADDR_DEFER_USE     0x00000004	/* Hold off using this one */
103 #define SCTP_ADDR_IFA_UNUSEABLE 0x00000008
104 
105 struct sctp_ifa {
106 	LIST_ENTRY(sctp_ifa) next_ifa;
107 	LIST_ENTRY(sctp_ifa) next_bucket;
108 	struct sctp_ifn *ifn_p;	/* back pointer to parent ifn */
109 	void    *ifa;		/* pointer to ifa, needed for flag
110 				 * update for that we MUST lock
111 				 * appropriate locks. This is for V6.
112 				 */
113 	union sctp_sockstore address;
114 	uint32_t refcount;	/* number of folks referring to this */
115 	uint32_t flags;
116 	uint32_t localifa_flags;
117 	uint32_t vrf_id;	/* vrf_id of this addr (for deleting) */
118 	uint8_t src_is_loop;
119 	uint8_t src_is_priv;
120 	uint8_t src_is_glob;
121 	uint8_t resv;
122 };
123 
124 struct sctp_laddr {
125 	LIST_ENTRY(sctp_laddr) sctp_nxt_addr;	/* next in list */
126 	struct sctp_ifa *ifa;
127 	uint32_t action;		/* Used during asconf and adding
128 					 * if no-zero src-addr selection will
129 					 * not consider this address.
130 					 */
131 	struct timeval start_time;      /* time when this address was created */
132 };
133 
134 struct sctp_block_entry {
135 	int error;
136 };
137 
138 struct sctp_timewait {
139 	uint32_t tv_sec_at_expire;	/* the seconds from boot to expire */
140         uint32_t v_tag;		        /* the vtag that can not be reused */
141         uint16_t lport;                 /* the local port used in vtag */
142         uint16_t rport;                 /* the remote port used in vtag */
143 };
144 
145 struct sctp_tagblock {
146 	LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock;
147 	struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK];
148 };
149 
150 
151 struct sctp_epinfo {
152 #if defined(__FreeBSD__)
153 #ifdef INET
154 	struct socket *udp4_tun_socket;
155 #endif
156 #ifdef INET6
157 	struct socket *udp6_tun_socket;
158 #endif
159 #endif
160 	struct sctpasochead *sctp_asochash;
161 	u_long hashasocmark;
162 
163 	struct sctppcbhead *sctp_ephash;
164 	u_long hashmark;
165 
166 	/*-
167 	 * The TCP model represents a substantial overhead in that we get an
168 	 * additional hash table to keep explicit connections in. The
169 	 * listening TCP endpoint will exist in the usual ephash above and
170 	 * accept only INIT's. It will be incapable of sending off an INIT.
171 	 * When a dg arrives we must look in the normal ephash. If we find a
172 	 * TCP endpoint that will tell us to go to the specific endpoint
173 	 * hash and re-hash to find the right assoc/socket. If we find a UDP
174 	 * model socket we then must complete the lookup. If this fails,
175 	 * i.e. no association can be found then we must continue to see if
176 	 * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
177 	 * acts like a TCP model connected socket).
178 	 */
179 	struct sctppcbhead *sctp_tcpephash;
180 	u_long hashtcpmark;
181 	uint32_t hashtblsize;
182 
183 	struct sctp_vrflist *sctp_vrfhash;
184 	u_long hashvrfmark;
185 
186 	struct sctp_ifnlist *vrf_ifn_hash;
187 	u_long   vrf_ifn_hashmark;
188 
189 	struct sctppcbhead listhead;
190 	struct sctpladdr addr_wq;
191 
192 #if defined(__APPLE__)
193 	struct inpcbhead inplisthead;
194 	struct inpcbinfo sctbinfo;
195 #endif
196 	/* ep zone info */
197 	sctp_zone_t ipi_zone_ep;
198 	sctp_zone_t ipi_zone_asoc;
199 	sctp_zone_t ipi_zone_laddr;
200 	sctp_zone_t ipi_zone_net;
201 	sctp_zone_t ipi_zone_chunk;
202 	sctp_zone_t ipi_zone_readq;
203 	sctp_zone_t ipi_zone_strmoq;
204 	sctp_zone_t ipi_zone_asconf;
205 	sctp_zone_t ipi_zone_asconf_ack;
206 
207 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
208 #if __FreeBSD_version <= 602000
209 	struct mtx ipi_ep_mtx;
210 #else
211 	struct rwlock ipi_ep_mtx;
212 #endif
213 	struct mtx ipi_iterator_wq_mtx;
214 #if __FreeBSD_version <= 602000
215 	struct mtx ipi_addr_mtx;
216 #else
217 	struct rwlock ipi_addr_mtx;
218 #endif
219 	struct mtx ipi_pktlog_mtx;
220 	struct mtx wq_addr_mtx;
221 #elif defined(SCTP_PROCESS_LEVEL_LOCKS)
222 	userland_mutex_t ipi_ep_mtx;
223 	userland_mutex_t ipi_addr_mtx;
224 	userland_mutex_t ipi_count_mtx;
225 	userland_mutex_t ipi_pktlog_mtx;
226 	userland_mutex_t wq_addr_mtx;
227 #elif defined(__APPLE__)
228 #ifdef _KERN_LOCKS_H_
229 	lck_mtx_t *ipi_addr_mtx;
230 	lck_mtx_t *ipi_count_mtx;
231 	lck_mtx_t *ipi_pktlog_mtx;
232 	lck_mtx_t *logging_mtx;
233 	lck_mtx_t *wq_addr_mtx;
234 #else
235 	void *ipi_count_mtx;
236 	void *logging_mtx;
237 #endif /* _KERN_LOCKS_H_ */
238 #elif defined(__Windows__)
239 	struct rwlock ipi_ep_lock;
240 	struct rwlock ipi_addr_lock;
241 	struct spinlock ipi_pktlog_mtx;
242 	struct rwlock wq_addr_mtx;
243 #elif defined(__Userspace__)
244     /* TODO decide on __Userspace__ locks */
245 #endif
246 	uint32_t ipi_count_ep;
247 
248 	/* assoc/tcb zone info */
249 	uint32_t ipi_count_asoc;
250 
251 	/* local addrlist zone info */
252 	uint32_t ipi_count_laddr;
253 
254 	/* remote addrlist zone info */
255 	uint32_t ipi_count_raddr;
256 
257 	/* chunk structure list for output */
258 	uint32_t ipi_count_chunk;
259 
260 	/* socket queue zone info */
261 	uint32_t ipi_count_readq;
262 
263 	/* socket queue zone info */
264 	uint32_t ipi_count_strmoq;
265 
266 	/* Number of vrfs */
267 	uint32_t ipi_count_vrfs;
268 
269         /* Number of ifns */
270 	uint32_t ipi_count_ifns;
271 
272         /* Number of ifas */
273 	uint32_t ipi_count_ifas;
274 
275 	/* system wide number of free chunks hanging around */
276 	uint32_t ipi_free_chunks;
277 	uint32_t ipi_free_strmoq;
278 
279 	struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE];
280 
281 	/* address work queue handling */
282 	struct sctp_timer addr_wq_timer;
283 
284 #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
285 	struct calloutlist callqueue;
286 #endif
287 };
288 
289 
290 struct sctp_base_info {
291 	/* All static structures that
292 	 * anchor the system must be here.
293 	 */
294 	struct sctp_epinfo sctppcbinfo;
295 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
296 	struct sctpstat    *sctpstat;
297 #else
298 	struct sctpstat    sctpstat;
299 #endif
300 	struct sctp_sysctl sctpsysctl;
301 	uint8_t first_time;
302 	char sctp_pcb_initialized;
303 #if defined(SCTP_PACKET_LOGGING)
304 	int packet_log_writers;
305 	int packet_log_end;
306 	uint8_t packet_log_buffer[SCTP_PACKET_LOG_SIZE];
307 #endif
308 #if defined(__FreeBSD__)
309 	eventhandler_tag eh_tag;
310 #endif
311 #if defined(__APPLE__)
312 	int sctp_main_timer_ticks;
313 #endif
314 #if defined(__Userspace__)
315 	userland_mutex_t timer_mtx;
316 	userland_thread_t timer_thread;
317 	int timer_thread_should_exit;
318 	int iterator_thread_started;
319 	int timer_thread_started;
320 #if !defined(__Userspace_os_Windows)
321 	pthread_mutexattr_t mtx_attr;
322 #if defined(INET) || defined(INET6)
323 	int userspace_route;
324 	userland_thread_t recvthreadroute;
325 #endif
326 #endif
327 #ifdef INET
328 #if defined(__Userspace_os_Windows) && !defined(__MINGW32__)
329 	SOCKET userspace_rawsctp;
330 	SOCKET userspace_udpsctp;
331 #else
332 	int userspace_rawsctp;
333 	int userspace_udpsctp;
334 #endif
335 	userland_thread_t recvthreadraw;
336 	userland_thread_t recvthreadudp;
337 #endif
338 #ifdef INET6
339 #if defined(__Userspace_os_Windows) && !defined(__MINGW32__)
340 	SOCKET userspace_rawsctp6;
341 	SOCKET userspace_udpsctp6;
342 #else
343 	int userspace_rawsctp6;
344 	int userspace_udpsctp6;
345 #endif
346 	userland_thread_t recvthreadraw6;
347 	userland_thread_t recvthreadudp6;
348 #endif
349 	int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df);
350 	void (*debug_printf)(const char *format, ...);
351 	int crc32c_offloaded;
352 #endif
353 };
354 
355 /*-
356  * Here we have all the relevant information for each SCTP entity created. We
357  * will need to modify this as approprate. We also need to figure out how to
358  * access /dev/random.
359  */
360 struct sctp_pcb {
361 	unsigned int time_of_secret_change;	/* number of seconds from
362 						 * timeval.tv_sec */
363 	uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
364 	unsigned int size_of_a_cookie;
365 
366 	uint32_t sctp_timeoutticks[SCTP_NUM_TMRS];
367 	uint32_t sctp_minrto;
368 	uint32_t sctp_maxrto;
369 	uint32_t initial_rto;
370 	uint32_t initial_init_rto_max;
371 
372 	unsigned int sctp_sack_freq;
373 	uint32_t sctp_sws_sender;
374 	uint32_t sctp_sws_receiver;
375 
376 	uint32_t sctp_default_cc_module;
377 	uint32_t sctp_default_ss_module;
378 	/* authentication related fields */
379 	struct sctp_keyhead shared_keys;
380 	sctp_auth_chklist_t *local_auth_chunks;
381 	sctp_hmaclist_t *local_hmacs;
382 	uint16_t default_keyid;
383 	uint32_t default_mtu;
384 
385 	/* various thresholds */
386 	/* Max times I will init at a guy */
387 	uint16_t max_init_times;
388 
389 	/* Max times I will send before we consider someone dead */
390 	uint16_t max_send_times;
391 
392 	uint16_t def_net_failure;
393 
394 	uint16_t def_net_pf_threshold;
395 
396 	/* number of streams to pre-open on a association */
397 	uint16_t pre_open_stream_count;
398 	uint16_t max_open_streams_intome;
399 
400 	/* random number generator */
401 	uint32_t random_counter;
402 	uint8_t random_numbers[SCTP_SIGNATURE_ALOC_SIZE];
403 	uint8_t random_store[SCTP_SIGNATURE_ALOC_SIZE];
404 
405 	/*
406 	 * This timer is kept running per endpoint.  When it fires it will
407 	 * change the secret key.  The default is once a hour
408 	 */
409 	struct sctp_timer signature_change;
410 
411 	uint32_t def_cookie_life;
412 	/* defaults to 0 */
413 	uint32_t auto_close_time;
414 	uint32_t initial_sequence_debug;
415 	uint32_t adaptation_layer_indicator;
416 	uint8_t adaptation_layer_indicator_provided;
417 	uint32_t store_at;
418 	uint32_t max_burst;
419 	uint32_t fr_max_burst;
420 #ifdef INET6
421 	uint32_t default_flowlabel;
422 #endif
423 	uint8_t default_dscp;
424 	char current_secret_number;
425 	char last_secret_number;
426 	uint16_t port; /* remote UDP encapsulation port */
427 };
428 
429 #ifndef SCTP_ALIGNMENT
430 #define SCTP_ALIGNMENT 32
431 #endif
432 
433 #ifndef SCTP_ALIGNM1
434 #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
435 #endif
436 
437 #define sctp_lport ip_inp.inp.inp_lport
438 
439 struct sctp_pcbtsn_rlog {
440 	uint32_t vtag;
441 	uint16_t strm;
442 	uint16_t seq;
443 	uint16_t sz;
444 	uint16_t flgs;
445 };
446 #define SCTP_READ_LOG_SIZE 135	/* we choose the number to make a pcb a page */
447 
448 
449 struct sctp_inpcb {
450 	/*-
451 	 * put an inpcb in front of it all, kind of a waste but we need to
452 	 * for compatibility with all the other stuff.
453 	 */
454 	union {
455 		struct inpcb inp;
456 		char align[(sizeof(struct inpcb) + SCTP_ALIGNM1) &
457 		        ~SCTP_ALIGNM1];
458 	}     ip_inp;
459 
460 #if defined(__APPLE__)
461 	/* leave some space in case i386 inpcb is bigger than ppc */
462 	uint8_t		padding[128];
463 #endif
464 
465 	/* Socket buffer lock protects read_queue and of course sb_cc */
466 	struct sctp_readhead read_queue;
467 
468 	LIST_ENTRY(sctp_inpcb) sctp_list;	/* lists all endpoints */
469         /* hash of all endpoints for model */
470 	LIST_ENTRY(sctp_inpcb) sctp_hash;
471 	/* count of local addresses bound, 0 if bound all */
472 	int laddr_count;
473 
474 	/* list of addrs in use by the EP, NULL if bound-all */
475 	struct sctpladdr sctp_addr_list;
476 	/* used for source address selection rotation when we are subset bound */
477 	struct sctp_laddr *next_addr_touse;
478 
479 	/* back pointer to our socket */
480 	struct socket *sctp_socket;
481 	uint64_t sctp_features;	/* Feature flags */
482 	uint32_t sctp_flags;	/* INP state flag set */
483 	uint32_t sctp_mobility_features; /* Mobility  Feature flags */
484 	struct sctp_pcb sctp_ep;/* SCTP ep data */
485 	/* head of the hash of all associations */
486 	struct sctpasochead *sctp_tcbhash;
487 	u_long sctp_hashmark;
488 	/* head of the list of all associations */
489 	struct sctpasochead sctp_asoc_list;
490 #ifdef SCTP_TRACK_FREED_ASOCS
491 	struct sctpasochead sctp_asoc_free_list;
492 #endif
493 	struct sctp_iterator *inp_starting_point_for_iterator;
494 	uint32_t sctp_frag_point;
495 	uint32_t partial_delivery_point;
496 	uint32_t sctp_context;
497 	uint32_t max_cwnd;
498 	uint8_t local_strreset_support;
499 	uint32_t sctp_cmt_on_off;
500 	uint8_t ecn_supported;
501 	uint8_t prsctp_supported;
502 	uint8_t auth_supported;
503 	uint8_t idata_supported;
504 	uint8_t asconf_supported;
505 	uint8_t reconfig_supported;
506 	uint8_t nrsack_supported;
507 	uint8_t pktdrop_supported;
508 	struct sctp_nonpad_sndrcvinfo def_send;
509 	/*-
510 	 * These three are here for the sosend_dgram
511 	 * (pkt, pkt_last and control).
512 	 * routine. However, I don't think anyone in
513 	 * the current FreeBSD kernel calls this. So
514 	 * they are candidates with sctp_sendm for
515 	 * de-supporting.
516 	 */
517 #ifdef __Panda__
518 	pakhandle_type pak_to_read;
519 	pakhandle_type pak_to_read_sendq;
520 #endif
521 	struct mbuf *pkt, *pkt_last;
522 	struct mbuf *control;
523 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__))
524 #ifndef INP_IPV6
525 #define INP_IPV6	0x1
526 #endif
527 #ifndef INP_IPV4
528 #define INP_IPV4	0x2
529 #endif
530 	uint8_t inp_vflag;
531                                /* TODO __Userspace__ where is our inp_vlag going to be? */
532 	uint8_t inp_ip_ttl;
533         uint8_t inp_ip_tos;    /* defined as macro in user_inpcb.h */
534 	uint8_t inp_ip_resv;
535 #endif
536 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
537 	struct mtx inp_mtx;
538 	struct mtx inp_create_mtx;
539 	struct mtx inp_rdata_mtx;
540 	int32_t refcount;
541 #elif defined(SCTP_PROCESS_LEVEL_LOCKS)
542 	userland_mutex_t inp_mtx;
543 	userland_mutex_t inp_create_mtx;
544 	userland_mutex_t inp_rdata_mtx;
545 	int32_t refcount;
546 #elif defined(__APPLE__)
547 #if defined(SCTP_APPLE_RWLOCK)
548 	lck_rw_t *inp_mtx;
549 #else
550 	lck_mtx_t *inp_mtx;
551 #endif
552 	lck_mtx_t *inp_create_mtx;
553 	lck_mtx_t *inp_rdata_mtx;
554 #elif defined(__Windows__)
555 	struct rwlock inp_lock;
556 	struct spinlock inp_create_lock;
557 	struct spinlock inp_rdata_lock;
558 	int32_t refcount;
559 #elif defined(__Userspace__)
560     /* TODO decide on __Userspace__ locks */
561 	int32_t refcount;
562 #endif
563 #if defined(__APPLE__)
564 	int32_t refcount;
565 
566 	uint32_t lock_caller1;
567 	uint32_t lock_caller2;
568 	uint32_t lock_caller3;
569 	uint32_t unlock_caller1;
570 	uint32_t unlock_caller2;
571 	uint32_t unlock_caller3;
572 	uint32_t getlock_caller1;
573 	uint32_t getlock_caller2;
574 	uint32_t getlock_caller3;
575 	uint32_t gen_count;
576 	uint32_t lock_gen_count;
577 	uint32_t unlock_gen_count;
578 	uint32_t getlock_gen_count;
579 
580 	uint32_t i_am_here_file;
581 	uint32_t i_am_here_line;
582 #endif
583 	uint32_t def_vrf_id;
584 	uint16_t fibnum;
585 #ifdef SCTP_MVRF
586 	uint32_t *m_vrf_ids;
587 	uint32_t num_vrfs;
588 	uint32_t vrf_size;
589 #endif
590 	uint32_t total_sends;
591 	uint32_t total_recvs;
592 	uint32_t last_abort_code;
593 	uint32_t total_nospaces;
594 	struct sctpasochead *sctp_asocidhash;
595 	u_long hashasocidmark;
596         uint32_t sctp_associd_counter;
597 
598 #ifdef SCTP_ASOCLOG_OF_TSNS
599 	struct sctp_pcbtsn_rlog readlog[SCTP_READ_LOG_SIZE];
600 	uint32_t readlog_index;
601 #endif
602 #if defined(__Userspace__)
603 	void *ulp_info;
604 	int (*recv_callback)(struct socket *, union sctp_sockstore, void *, size_t,
605                              struct sctp_rcvinfo, int, void *);
606 	uint32_t send_sb_threshold;
607 	int (*send_callback)(struct socket *, uint32_t);
608 #endif
609 };
610 
611 #if defined(__Userspace__)
612 int register_recv_cb (struct socket *,
613                       int (*)(struct socket *, union sctp_sockstore, void *, size_t,
614                               struct sctp_rcvinfo, int, void *));
615 int register_send_cb (struct socket *, uint32_t, int (*)(struct socket *, uint32_t));
616 int register_ulp_info (struct socket *, void *);
617 int retrieve_ulp_info (struct socket *, void **);
618 
619 #endif
620 struct sctp_tcb {
621 	struct socket *sctp_socket;	/* back pointer to socket */
622 	struct sctp_inpcb *sctp_ep;	/* back pointer to ep */
623 	LIST_ENTRY(sctp_tcb) sctp_tcbhash;	/* next link in hash
624 						 * table */
625         LIST_ENTRY(sctp_tcb) sctp_tcblist;	/* list of all of the
626 						 * TCB's */
627         LIST_ENTRY(sctp_tcb) sctp_tcbasocidhash;	/* next link in asocid
628 							 * hash table
629 							 */
630         LIST_ENTRY(sctp_tcb) sctp_asocs;	/* vtag hash list */
631 	struct sctp_block_entry *block_entry;	/* pointer locked by  socket
632 						 * send buffer */
633 	struct sctp_association asoc;
634 	/*-
635 	 * freed_by_sorcv_sincelast is protected by the sockbuf_lock NOT the
636 	 * tcb_lock. Its special in this way to help avoid extra mutex calls
637 	 * in the reading of data.
638 	 */
639 	uint32_t freed_by_sorcv_sincelast;
640 	uint32_t total_sends;
641 	uint32_t total_recvs;
642 	int freed_from_where;
643 	uint16_t rport;		/* remote port in network format */
644 	uint16_t resv;
645 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
646 	struct mtx tcb_mtx;
647 	struct mtx tcb_send_mtx;
648 #elif defined(SCTP_PROCESS_LEVEL_LOCKS)
649 	userland_mutex_t tcb_mtx;
650 	userland_mutex_t tcb_send_mtx;
651 #elif defined(__APPLE__)
652 	lck_mtx_t* tcb_mtx;
653 	lck_mtx_t* tcb_send_mtx;
654 #elif defined(__Windows__)
655 	struct spinlock tcb_lock;
656 	struct spinlock tcb_send_lock;
657 #elif defined(__Userspace__)
658     /* TODO decide on __Userspace__ locks */
659 #endif
660 #if defined(__APPLE__)
661 	uint32_t caller1;
662 	uint32_t caller2;
663 	uint32_t caller3;
664 #endif
665 };
666 
667 
668 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
669 
670 #include <netinet/sctp_lock_bsd.h>
671 
672 #elif defined(__APPLE__)
673 /*
674  * Apple MacOS X 10.4 "Tiger"
675  */
676 
677 #include <netinet/sctp_lock_apple_fg.h>
678 
679 #elif defined(SCTP_PROCESS_LEVEL_LOCKS)
680 
681 #include <netinet/sctp_process_lock.h>
682 
683 #elif defined(__Windows__)
684 
685 #include <netinet/sctp_lock_windows.h>
686 
687 #elif defined(__Userspace__)
688 
689 #include <netinet/sctp_lock_userspace.h>
690 
691 #else
692 /*
693  * Pre-5.x FreeBSD, and others.
694  */
695 #include <netinet/sctp_lock_empty.h>
696 #endif
697 
698 /* TODO where to put non-_KERNEL things for __Userspace__? */
699 #if defined(_KERNEL) || defined(__Userspace__)
700 
701 /* Attention Julian, this is the extern that
702  * goes with the base info. sctp_pcb.c has
703  * the real definition.
704  */
705 #if defined(__FreeBSD__) && __FreeBSD_version >= 801000
706 VNET_DECLARE(struct sctp_base_info, system_base_info) ;
707 #else
708 extern struct sctp_base_info system_base_info;
709 #endif
710 
711 #ifdef INET6
712 int SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b);
713 #endif
714 
715 void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
716 
717 struct sctp_ifn *
718 sctp_find_ifn(void *ifn, uint32_t ifn_index);
719 
720 struct sctp_vrf *sctp_allocate_vrf(int vrfid);
721 struct sctp_vrf *sctp_find_vrf(uint32_t vrfid);
722 void sctp_free_vrf(struct sctp_vrf *vrf);
723 
724 /*-
725  * Change address state, can be used if
726  * O/S supports telling transports about
727  * changes to IFA/IFN's (link layer triggers).
728  * If a ifn goes down, we will do src-addr-selection
729  * and NOT use that, as a source address. This does
730  * not stop the routing system from routing out
731  * that interface, but we won't put it as a source.
732  */
733 void sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, const char *if_name, uint32_t ifn_index);
734 void sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, const char *if_name, uint32_t ifn_index);
735 
736 struct sctp_ifa *
737 sctp_add_addr_to_vrf(uint32_t vrfid,
738 		     void *ifn, uint32_t ifn_index, uint32_t ifn_type,
739 		     const char *if_name,
740 		     void *ifa, struct sockaddr *addr, uint32_t ifa_flags,
741 		     int dynamic_add);
742 
743 void sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu);
744 
745 void sctp_free_ifn(struct sctp_ifn *sctp_ifnp);
746 void sctp_free_ifa(struct sctp_ifa *sctp_ifap);
747 
748 
749 void sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
750 			    uint32_t ifn_index, const char *if_name);
751 
752 
753 
754 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
755 
756 struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int, uint32_t);
757 
758 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
759 int sctp_inpcb_bind(struct socket *, struct sockaddr *,
760 		    struct sctp_ifa *,struct thread *);
761 #elif defined(__Windows__)
762 int sctp_inpcb_bind(struct socket *, struct sockaddr *,
763 		    struct sctp_ifa *,PKTHREAD);
764 #else
765 /* struct proc is a dummy for __Userspace__ */
766 int sctp_inpcb_bind(struct socket *, struct sockaddr *,
767 		    struct sctp_ifa *, struct proc *);
768 #endif
769 
770 struct sctp_tcb *
771 sctp_findassociation_addr(struct mbuf *, int,
772     struct sockaddr *, struct sockaddr *,
773     struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
774     struct sctp_nets **, uint32_t vrf_id);
775 
776 struct sctp_tcb *
777 sctp_findassociation_addr_sa(struct sockaddr *,
778     struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int, uint32_t);
779 
780 void
781 sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
782     struct sctp_tcb *);
783 
784 /*-
785  * For this call ep_addr, the to is the destination endpoint address of the
786  * peer (relative to outbound). The from field is only used if the TCP model
787  * is enabled and helps distingush amongst the subset bound (non-boundall).
788  * The TCP model MAY change the actual ep field, this is why it is passed.
789  */
790 struct sctp_tcb *
791 sctp_findassociation_ep_addr(struct sctp_inpcb **,
792     struct sockaddr *, struct sctp_nets **, struct sockaddr *,
793     struct sctp_tcb *);
794 
795 struct sctp_tcb *
796 sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock);
797 
798 struct sctp_tcb *
799 sctp_findassociation_ep_asocid(struct sctp_inpcb *,
800     sctp_assoc_t, int);
801 
802 struct sctp_tcb *
803 sctp_findassociation_ep_asconf(struct mbuf *, int, struct sockaddr *,
804 			       struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **, uint32_t vrf_id);
805 
806 int sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id);
807 
808 int sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id);
809 
810 void sctp_inpcb_free(struct sctp_inpcb *, int, int);
811 
812 #define SCTP_DONT_INITIALIZE_AUTH_PARAMS	0
813 #define SCTP_INITIALIZE_AUTH_PARAMS		1
814 
815 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
816 struct sctp_tcb *
817 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
818                 int *, uint32_t, uint32_t, uint16_t, uint16_t, struct thread *,
819                 int);
820 #elif defined(__Windows__)
821 struct sctp_tcb *
822 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
823                 int *, uint32_t, uint32_t, uint16_t, uint16_t, PKTHREAD, int);
824 #else
825 /* proc will be NULL for __Userspace__ */
826 struct sctp_tcb *
827 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
828                 int *, uint32_t, uint32_t, uint16_t, uint16_t, struct proc *,
829                 int);
830 #endif
831 
832 int sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *, int, int);
833 
834 
835 void sctp_delete_from_timewait(uint32_t, uint16_t, uint16_t);
836 
837 int sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport);
838 
839 void
840 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport);
841 
842 void sctp_add_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *, uint32_t);
843 
844 void sctp_del_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *);
845 
846 int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, struct sctp_nets **, uint16_t, int, int);
847 
848 void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *);
849 
850 int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
851 
852 #if defined(__Userspace__)
853 void sctp_pcb_init(int);
854 #else
855 void sctp_pcb_init(void);
856 #endif
857 
858 void sctp_pcb_finish(void);
859 
860 void sctp_add_local_addr_restricted(struct sctp_tcb *, struct sctp_ifa *);
861 void sctp_del_local_addr_restricted(struct sctp_tcb *, struct sctp_ifa *);
862 
863 int
864 sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
865     struct sockaddr *, struct sockaddr *, struct sockaddr *, uint16_t);
866 
867 int
868 sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *,
869     struct sctp_nets *);
870 
871 int sctp_is_vtag_good(uint32_t, uint16_t lport, uint16_t rport, struct timeval *);
872 
873 /* void sctp_drain(void); */
874 
875 int sctp_destination_is_reachable(struct sctp_tcb *, struct sockaddr *);
876 
877 int sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp);
878 
879 void sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh);
880 
881 /*-
882  * Null in last arg inpcb indicate run on ALL ep's. Specific inp in last arg
883  * indicates run on ONLY assoc's of the specified endpoint.
884  */
885 int
886 sctp_initiate_iterator(inp_func inpf,
887 		       asoc_func af,
888 		       inp_func inpe,
889 		       uint32_t, uint32_t,
890 		       uint32_t, void *,
891 		       uint32_t,
892 		       end_func ef,
893 		       struct sctp_inpcb *,
894 		       uint8_t co_off);
895 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
896 void
897 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use);
898 
899 #endif
900 
901 #endif				/* _KERNEL */
902 #endif				/* !__sctp_pcb_h__ */
903