2 * SPDX-License-Identifier: BSD-3-Clause
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
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.
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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #ifndef _NETINET_SCTP_PCB_H_
39 #define _NETINET_SCTP_PCB_H_
41 #include <netinet/sctp_os.h>
42 #include <netinet/sctp.h>
43 #include <netinet/sctp_constants.h>
44 #include <netinet/sctp_sysctl.h>
46 LIST_HEAD(sctppcbhead, sctp_inpcb);
47 LIST_HEAD(sctpasochead, sctp_tcb);
48 LIST_HEAD(sctpladdr, sctp_laddr);
49 LIST_HEAD(sctpvtaghead, sctp_tagblock);
50 LIST_HEAD(sctp_vrflist, sctp_vrf);
51 LIST_HEAD(sctp_ifnlist, sctp_ifn);
52 LIST_HEAD(sctp_ifalist, sctp_ifa);
53 TAILQ_HEAD(sctp_readhead, sctp_queued_to_read);
54 TAILQ_HEAD(sctp_streamhead, sctp_stream_queue_pending);
56 #include <netinet/sctp_structs.h>
57 #include <netinet/sctp_auth.h>
59 #define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask)
60 #define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask)
63 LIST_ENTRY(sctp_vrf) next_vrf;
64 struct sctp_ifalist *vrf_addr_hash;
65 struct sctp_ifnlist ifnlist;
67 uint32_t tbl_id_v4; /* default v4 table id */
68 uint32_t tbl_id_v6; /* default v6 table id */
69 uint32_t total_ifa_count;
70 u_long vrf_addr_hashmark;
75 struct sctp_ifalist ifalist;
77 LIST_ENTRY(sctp_ifn) next_ifn;
78 LIST_ENTRY(sctp_ifn) next_bucket;
79 void *ifn_p; /* never access without appropriate lock */
82 uint32_t ifn_index; /* shorthand way to look at ifn for reference */
83 uint32_t refcount; /* number of reference held should be >=
85 uint32_t ifa_count; /* IFA's we hold (in our list - ifalist) */
86 uint32_t num_v6; /* number of v6 addresses */
87 uint32_t num_v4; /* number of v4 addresses */
88 uint32_t registered_af; /* registered address family for i/f events */
89 char ifn_name[SCTP_IFNAMSIZ];
92 /* SCTP local IFA flags */
93 #define SCTP_ADDR_VALID 0x00000001 /* its up and active */
94 #define SCTP_BEING_DELETED 0x00000002 /* being deleted, when
95 * refcount = 0. Note that it
96 * is pulled from the ifn list
97 * and ifa_p is nulled right
98 * away but it cannot be freed
100 * pointing to it is deleted. */
101 #define SCTP_ADDR_DEFER_USE 0x00000004 /* Hold off using this one */
102 #define SCTP_ADDR_IFA_UNUSEABLE 0x00000008
105 LIST_ENTRY(sctp_ifa) next_ifa;
106 LIST_ENTRY(sctp_ifa) next_bucket;
107 struct sctp_ifn *ifn_p; /* back pointer to parent ifn */
108 void *ifa; /* pointer to ifa, needed for flag update for
109 * that we MUST lock appropriate locks. This
111 union sctp_sockstore address;
112 uint32_t refcount; /* number of folks referring to this */
114 uint32_t localifa_flags;
115 uint32_t vrf_id; /* vrf_id of this addr (for deleting) */
123 LIST_ENTRY(sctp_laddr) sctp_nxt_addr; /* next in list */
124 struct sctp_ifa *ifa;
125 uint32_t action; /* Used during asconf and adding if no-zero
126 * src-addr selection will not consider this
128 struct timeval start_time; /* time when this address was created */
131 struct sctp_block_entry {
135 struct sctp_timewait {
136 uint32_t tv_sec_at_expire; /* the seconds from boot to expire */
137 uint32_t v_tag; /* the vtag that can not be reused */
138 uint16_t lport; /* the local port used in vtag */
139 uint16_t rport; /* the remote port used in vtag */
142 struct sctp_tagblock {
143 LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock;
144 struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK];
150 struct socket *udp4_tun_socket;
153 struct socket *udp6_tun_socket;
155 struct sctpasochead *sctp_asochash;
158 struct sctppcbhead *sctp_ephash;
162 * The TCP model represents a substantial overhead in that we get an
163 * additional hash table to keep explicit connections in. The
164 * listening TCP endpoint will exist in the usual ephash above and
165 * accept only INIT's. It will be incapable of sending off an INIT.
166 * When a dg arrives we must look in the normal ephash. If we find a
167 * TCP endpoint that will tell us to go to the specific endpoint
168 * hash and re-hash to find the right assoc/socket. If we find a UDP
169 * model socket we then must complete the lookup. If this fails,
170 * i.e. no association can be found then we must continue to see if
171 * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
172 * acts like a TCP model connected socket).
174 struct sctppcbhead *sctp_tcpephash;
176 uint32_t hashtblsize;
178 struct sctp_vrflist *sctp_vrfhash;
181 struct sctp_ifnlist *vrf_ifn_hash;
182 u_long vrf_ifn_hashmark;
184 struct sctppcbhead listhead;
185 struct sctpladdr addr_wq;
188 sctp_zone_t ipi_zone_ep;
189 sctp_zone_t ipi_zone_asoc;
190 sctp_zone_t ipi_zone_laddr;
191 sctp_zone_t ipi_zone_net;
192 sctp_zone_t ipi_zone_chunk;
193 sctp_zone_t ipi_zone_readq;
194 sctp_zone_t ipi_zone_strmoq;
195 sctp_zone_t ipi_zone_asconf;
196 sctp_zone_t ipi_zone_asconf_ack;
198 struct rwlock ipi_ep_mtx;
199 struct mtx ipi_iterator_wq_mtx;
200 struct rwlock ipi_addr_mtx;
201 struct mtx ipi_pktlog_mtx;
202 struct mtx wq_addr_mtx;
203 uint32_t ipi_count_ep;
205 /* assoc/tcb zone info */
206 uint32_t ipi_count_asoc;
208 /* local addrlist zone info */
209 uint32_t ipi_count_laddr;
211 /* remote addrlist zone info */
212 uint32_t ipi_count_raddr;
214 /* chunk structure list for output */
215 uint32_t ipi_count_chunk;
217 /* socket queue zone info */
218 uint32_t ipi_count_readq;
220 /* socket queue zone info */
221 uint32_t ipi_count_strmoq;
224 uint32_t ipi_count_vrfs;
227 uint32_t ipi_count_ifns;
230 uint32_t ipi_count_ifas;
232 /* system wide number of free chunks hanging around */
233 uint32_t ipi_free_chunks;
234 uint32_t ipi_free_strmoq;
236 struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE];
238 /* address work queue handling */
239 struct sctp_timer addr_wq_timer;
244 struct sctp_base_info {
246 * All static structures that anchor the system must be here.
248 struct sctp_epinfo sctppcbinfo;
249 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
250 struct sctpstat *sctpstat;
252 struct sctpstat sctpstat;
254 struct sctp_sysctl sctpsysctl;
256 char sctp_pcb_initialized;
257 #if defined(SCTP_PACKET_LOGGING)
258 int packet_log_writers;
260 uint8_t packet_log_buffer[SCTP_PACKET_LOG_SIZE];
265 * Here we have all the relevant information for each SCTP entity created. We
266 * will need to modify this as approprate. We also need to figure out how to
267 * access /dev/random.
270 unsigned int time_of_secret_change; /* number of seconds from
272 uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
273 unsigned int size_of_a_cookie;
275 unsigned int sctp_timeoutticks[SCTP_NUM_TMRS];
276 unsigned int sctp_minrto;
277 unsigned int sctp_maxrto;
278 unsigned int initial_rto;
279 int initial_init_rto_max;
281 unsigned int sctp_sack_freq;
282 uint32_t sctp_sws_sender;
283 uint32_t sctp_sws_receiver;
285 uint32_t sctp_default_cc_module;
286 uint32_t sctp_default_ss_module;
287 /* authentication related fields */
288 struct sctp_keyhead shared_keys;
289 sctp_auth_chklist_t *local_auth_chunks;
290 sctp_hmaclist_t *local_hmacs;
291 uint16_t default_keyid;
292 uint32_t default_mtu;
294 /* various thresholds */
295 /* Max times I will init at a guy */
296 uint16_t max_init_times;
298 /* Max times I will send before we consider someone dead */
299 uint16_t max_send_times;
301 uint16_t def_net_failure;
303 uint16_t def_net_pf_threshold;
305 /* number of streams to pre-open on a association */
306 uint16_t pre_open_stream_count;
307 uint16_t max_open_streams_intome;
309 /* random number generator */
310 uint32_t random_counter;
311 uint8_t random_numbers[SCTP_SIGNATURE_ALOC_SIZE];
312 uint8_t random_store[SCTP_SIGNATURE_ALOC_SIZE];
315 * This timer is kept running per endpoint. When it fires it will
316 * change the secret key. The default is once a hour
318 struct sctp_timer signature_change;
320 uint32_t def_cookie_life;
323 uint32_t initial_sequence_debug;
324 uint32_t adaptation_layer_indicator;
325 uint8_t adaptation_layer_indicator_provided;
328 uint32_t fr_max_burst;
330 uint32_t default_flowlabel;
332 uint8_t default_dscp;
333 char current_secret_number;
334 char last_secret_number;
335 uint16_t port; /* remote UDP encapsulation port */
338 #ifndef SCTP_ALIGNMENT
339 #define SCTP_ALIGNMENT 32
343 #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
346 #define sctp_lport ip_inp.inp.inp_lport
348 struct sctp_pcbtsn_rlog {
355 #define SCTP_READ_LOG_SIZE 135 /* we choose the number to make a pcb a page */
360 * put an inpcb in front of it all, kind of a waste but we need to
361 * for compatibility with all the other stuff.
365 char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
370 /* Socket buffer lock protects read_queue and of course sb_cc */
371 struct sctp_readhead read_queue;
373 LIST_ENTRY(sctp_inpcb) sctp_list; /* lists all endpoints */
374 /* hash of all endpoints for model */
375 LIST_ENTRY(sctp_inpcb) sctp_hash;
376 /* count of local addresses bound, 0 if bound all */
379 /* list of addrs in use by the EP, NULL if bound-all */
380 struct sctpladdr sctp_addr_list;
382 * used for source address selection rotation when we are subset
385 struct sctp_laddr *next_addr_touse;
387 /* back pointer to our socket */
388 struct socket *sctp_socket;
389 uint64_t sctp_features; /* Feature flags */
390 uint32_t sctp_flags; /* INP state flag set */
391 uint32_t sctp_mobility_features; /* Mobility Feature flags */
392 struct sctp_pcb sctp_ep; /* SCTP ep data */
393 /* head of the hash of all associations */
394 struct sctpasochead *sctp_tcbhash;
395 u_long sctp_hashmark;
396 /* head of the list of all associations */
397 struct sctpasochead sctp_asoc_list;
398 #ifdef SCTP_TRACK_FREED_ASOCS
399 struct sctpasochead sctp_asoc_free_list;
401 struct sctp_iterator *inp_starting_point_for_iterator;
402 uint32_t sctp_frag_point;
403 uint32_t partial_delivery_point;
404 uint32_t sctp_context;
406 uint8_t local_strreset_support;
407 uint32_t sctp_cmt_on_off;
408 uint8_t ecn_supported;
409 uint8_t prsctp_supported;
410 uint8_t auth_supported;
411 uint8_t idata_supported;
412 uint8_t asconf_supported;
413 uint8_t reconfig_supported;
414 uint8_t nrsack_supported;
415 uint8_t pktdrop_supported;
416 struct sctp_nonpad_sndrcvinfo def_send;
418 * These three are here for the sosend_dgram
419 * (pkt, pkt_last and control).
420 * routine. However, I don't think anyone in
421 * the current FreeBSD kernel calls this. So
422 * they are candidates with sctp_sendm for
425 struct mbuf *pkt, *pkt_last;
426 struct mbuf *control;
428 struct mtx inp_create_mtx;
429 struct mtx inp_rdata_mtx;
433 uint32_t total_sends;
434 uint32_t total_recvs;
435 uint32_t last_abort_code;
436 uint32_t total_nospaces;
437 struct sctpasochead *sctp_asocidhash;
438 u_long hashasocidmark;
439 uint32_t sctp_associd_counter;
441 #ifdef SCTP_ASOCLOG_OF_TSNS
442 struct sctp_pcbtsn_rlog readlog[SCTP_READ_LOG_SIZE];
443 uint32_t readlog_index;
448 struct socket *sctp_socket; /* back pointer to socket */
449 struct sctp_inpcb *sctp_ep; /* back pointer to ep */
450 LIST_ENTRY(sctp_tcb) sctp_tcbhash; /* next link in hash
452 LIST_ENTRY(sctp_tcb) sctp_tcblist; /* list of all of the
454 LIST_ENTRY(sctp_tcb) sctp_tcbasocidhash; /* next link in asocid
456 LIST_ENTRY(sctp_tcb) sctp_asocs; /* vtag hash list */
457 struct sctp_block_entry *block_entry; /* pointer locked by socket
459 struct sctp_association asoc;
461 * freed_by_sorcv_sincelast is protected by the sockbuf_lock NOT the
462 * tcb_lock. Its special in this way to help avoid extra mutex calls
463 * in the reading of data.
465 uint32_t freed_by_sorcv_sincelast;
466 uint32_t total_sends;
467 uint32_t total_recvs;
468 int freed_from_where;
469 uint16_t rport; /* remote port in network format */
472 struct mtx tcb_send_mtx;
477 #include <netinet/sctp_lock_bsd.h>
480 /* TODO where to put non-_KERNEL things for __Userspace__? */
481 #if defined(_KERNEL) || defined(__Userspace__)
483 /* Attention Julian, this is the extern that
484 * goes with the base info. sctp_pcb.c has
485 * the real definition.
487 VNET_DECLARE(struct sctp_base_info, system_base_info);
490 int SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b);
493 void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
495 struct sctp_ifn *sctp_find_ifn(void *ifn, uint32_t ifn_index);
497 struct sctp_vrf *sctp_allocate_vrf(int vrfid);
498 struct sctp_vrf *sctp_find_vrf(uint32_t vrfid);
499 void sctp_free_vrf(struct sctp_vrf *vrf);
502 * Change address state, can be used if
503 * O/S supports telling transports about
504 * changes to IFA/IFN's (link layer triggers).
505 * If a ifn goes down, we will do src-addr-selection
506 * and NOT use that, as a source address. This does
507 * not stop the routing system from routing out
508 * that interface, but we won't put it as a source.
510 void sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, const char *if_name, uint32_t ifn_index);
511 void sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, const char *if_name, uint32_t ifn_index);
514 sctp_add_addr_to_vrf(uint32_t vrfid,
515 void *ifn, uint32_t ifn_index, uint32_t ifn_type,
517 void *ifa, struct sockaddr *addr, uint32_t ifa_flags,
520 void sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu);
522 void sctp_free_ifn(struct sctp_ifn *sctp_ifnp);
523 void sctp_free_ifa(struct sctp_ifa *sctp_ifap);
527 sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
528 uint32_t ifn_index, const char *if_name);
532 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
534 struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int, uint32_t);
537 sctp_inpcb_bind(struct socket *, struct sockaddr *,
538 struct sctp_ifa *, struct thread *);
541 sctp_findassociation_addr(struct mbuf *, int,
542 struct sockaddr *, struct sockaddr *,
543 struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
544 struct sctp_nets **, uint32_t vrf_id);
547 sctp_findassociation_addr_sa(struct sockaddr *,
548 struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int, uint32_t);
551 sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
555 * For this call ep_addr, the to is the destination endpoint address of the
556 * peer (relative to outbound). The from field is only used if the TCP model
557 * is enabled and helps distingush amongst the subset bound (non-boundall).
558 * The TCP model MAY change the actual ep field, this is why it is passed.
561 sctp_findassociation_ep_addr(struct sctp_inpcb **,
562 struct sockaddr *, struct sctp_nets **, struct sockaddr *,
565 struct sctp_tcb *sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock);
568 sctp_findassociation_ep_asocid(struct sctp_inpcb *,
572 sctp_findassociation_ep_asconf(struct mbuf *, int, struct sockaddr *,
573 struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **, uint32_t vrf_id);
575 int sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id);
577 int sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id);
579 void sctp_inpcb_free(struct sctp_inpcb *, int, int);
582 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
583 int *, uint32_t, uint32_t, uint16_t, uint16_t, struct thread *);
585 int sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *, int, int);
588 void sctp_delete_from_timewait(uint32_t, uint16_t, uint16_t);
590 int sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport);
593 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport);
595 void sctp_add_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *, uint32_t);
597 void sctp_del_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *);
599 int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, struct sctp_nets **, uint16_t, int, int);
601 void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *);
603 int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
605 void sctp_pcb_init(void);
607 void sctp_pcb_finish(void);
609 void sctp_add_local_addr_restricted(struct sctp_tcb *, struct sctp_ifa *);
610 void sctp_del_local_addr_restricted(struct sctp_tcb *, struct sctp_ifa *);
613 sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
614 struct sockaddr *, struct sockaddr *, struct sockaddr *, uint16_t);
617 sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *,
620 int sctp_is_vtag_good(uint32_t, uint16_t lport, uint16_t rport, struct timeval *);
622 /* void sctp_drain(void); */
624 int sctp_destination_is_reachable(struct sctp_tcb *, struct sockaddr *);
626 int sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp);
628 void sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh);
631 * Null in last arg inpcb indicate run on ALL ep's. Specific inp in last arg
632 * indicates run on ONLY assoc's of the specified endpoint.
635 sctp_initiate_iterator(inp_func inpf,
644 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
646 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use);
651 #endif /* !__sctp_pcb_h__ */