xref: /freebsd/sys/netinet/sctp_pcb.h (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_pcb.h,v 1.21 2005/07/16 01:18:47 suz Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #ifndef __sctp_pcb_h__
37 #define __sctp_pcb_h__
38 
39 #include <netinet/sctp_os.h>
40 #include <netinet/sctp.h>
41 #include <netinet/sctp_constants.h>
42 
43 LIST_HEAD(sctppcbhead, sctp_inpcb);
44 LIST_HEAD(sctpasochead, sctp_tcb);
45 LIST_HEAD(sctpladdr, sctp_laddr);
46 LIST_HEAD(sctpvtaghead, sctp_tagblock);
47 LIST_HEAD(sctp_vrflist, sctp_vrf);
48 LIST_HEAD(sctp_ifnlist, sctp_ifn);
49 LIST_HEAD(sctp_ifalist, sctp_ifa);
50 TAILQ_HEAD(sctp_readhead, sctp_queued_to_read);
51 TAILQ_HEAD(sctp_streamhead, sctp_stream_queue_pending);
52 
53 #include <netinet/sctp_structs.h>
54 #include <netinet/sctp_auth.h>
55 
56 #define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask)
57 #define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask)
58 
59 struct sctp_vrf {
60 	LIST_ENTRY(sctp_vrf) next_vrf;
61 	struct sctp_ifalist *vrf_addr_hash;
62 	struct sctp_ifnlist ifnlist;
63 	uint32_t vrf_id;
64 	uint32_t tbl_id_v4;	/* default v4 table id */
65 	uint32_t tbl_id_v6;	/* default v6 table id */
66 	uint32_t total_ifa_count;
67 	u_long vrf_addr_hashmark;
68 	uint32_t refcount;
69 };
70 
71 struct sctp_ifn {
72 	struct sctp_ifalist ifalist;
73 	struct sctp_vrf *vrf;
74 	         LIST_ENTRY(sctp_ifn) next_ifn;
75 	         LIST_ENTRY(sctp_ifn) next_bucket;
76 	void *ifn_p;		/* never access without appropriate lock */
77 	uint32_t ifn_mtu;
78 	uint32_t ifn_type;
79 	uint32_t ifn_index;	/* shorthand way to look at ifn for reference */
80 	uint32_t refcount;	/* number of reference held should be >=
81 				 * ifa_count */
82 	uint32_t ifa_count;	/* IFA's we hold (in our list - ifalist) */
83 	uint32_t num_v6;	/* number of v6 addresses */
84 	uint32_t num_v4;	/* number of v4 addresses */
85 	uint32_t registered_af;	/* registered address family for i/f events */
86 	char ifn_name[SCTP_IFNAMSIZ];
87 };
88 
89 /* SCTP local IFA flags */
90 #define SCTP_ADDR_VALID         0x00000001	/* its up and active */
91 #define SCTP_BEING_DELETED      0x00000002	/* being deleted, when
92 						 * refcount = 0. Note that it
93 						 * is pulled from the ifn list
94 						 * and ifa_p is nulled right
95 						 * away but it cannot be freed
96 						 * until the last *net
97 						 * pointing to it is deleted. */
98 #define SCTP_ADDR_DEFER_USE     0x00000004	/* Hold off using this one */
99 #define SCTP_ADDR_IFA_UNUSEABLE 0x00000008
100 
101 struct sctp_ifa {
102 	LIST_ENTRY(sctp_ifa) next_ifa;
103 	LIST_ENTRY(sctp_ifa) next_bucket;
104 	struct sctp_ifn *ifn_p;	/* back pointer to parent ifn */
105 	void *ifa;		/* pointer to ifa, needed for flag update for
106 				 * that we MUST lock appropriate locks. This
107 				 * is for V6. */
108 	union sctp_sockstore address;
109 	uint32_t refcount;	/* number of folks refering to this */
110 	uint32_t flags;
111 	uint32_t localifa_flags;
112 	uint32_t vrf_id;	/* vrf_id of this addr (for deleting) */
113 	uint8_t src_is_loop;
114 	uint8_t src_is_priv;
115 	uint8_t src_is_glob;
116 	uint8_t resv;
117 };
118 
119 struct sctp_laddr {
120 	LIST_ENTRY(sctp_laddr) sctp_nxt_addr;	/* next in list */
121 	struct sctp_ifa *ifa;
122 	uint32_t action;	/* Used during asconf and adding if no-zero
123 				 * src-addr selection will not consider this
124 				 * address. */
125 	struct timeval start_time;	/* time when this address was created */
126 };
127 
128 struct sctp_block_entry {
129 	int error;
130 };
131 
132 struct sctp_timewait {
133 	uint32_t tv_sec_at_expire;	/* the seconds from boot to expire */
134 	uint32_t v_tag;		/* the vtag that can not be reused */
135 };
136 
137 struct sctp_tagblock {
138 	LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock;
139 	struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK];
140 };
141 
142 struct sctp_epinfo {
143 	struct sctpasochead *sctp_asochash;
144 	u_long hashasocmark;
145 
146 	struct sctppcbhead *sctp_ephash;
147 	u_long hashmark;
148 
149 	struct sctpasochead *sctp_restarthash;
150 	u_long hashrestartmark;
151 	/*-
152 	 * The TCP model represents a substantial overhead in that we get an
153 	 * additional hash table to keep explicit connections in. The
154 	 * listening TCP endpoint will exist in the usual ephash above and
155 	 * accept only INIT's. It will be incapable of sending off an INIT.
156 	 * When a dg arrives we must look in the normal ephash. If we find a
157 	 * TCP endpoint that will tell us to go to the specific endpoint
158 	 * hash and re-hash to find the right assoc/socket. If we find a UDP
159 	 * model socket we then must complete the lookup. If this fails,
160 	 * i.e. no association can be found then we must continue to see if
161 	 * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
162 	 * acts like a TCP model connected socket).
163 	 */
164 	struct sctppcbhead *sctp_tcpephash;
165 	u_long hashtcpmark;
166 	uint32_t hashtblsize;
167 
168 	struct sctp_vrflist *sctp_vrfhash;
169 	u_long hashvrfmark;
170 
171 	struct sctp_ifnlist *vrf_ifn_hash;
172 	u_long vrf_ifn_hashmark;
173 
174 	struct sctppcbhead listhead;
175 	struct sctpladdr addr_wq;
176 
177 	struct sctpiterators iteratorhead;
178 
179 	/* ep zone info */
180 	sctp_zone_t ipi_zone_ep;
181 	sctp_zone_t ipi_zone_asoc;
182 	sctp_zone_t ipi_zone_laddr;
183 	sctp_zone_t ipi_zone_net;
184 	sctp_zone_t ipi_zone_chunk;
185 	sctp_zone_t ipi_zone_readq;
186 	sctp_zone_t ipi_zone_strmoq;
187 
188 	struct mtx ipi_ep_mtx;
189 	struct mtx it_mtx;
190 	struct mtx ipi_iterator_wq_mtx;
191 	struct mtx ipi_addr_mtx;
192 	struct mtx ipi_pktlog_mtx;
193 	uint32_t ipi_count_ep;
194 
195 	/* assoc/tcb zone info */
196 	uint32_t ipi_count_asoc;
197 
198 	/* local addrlist zone info */
199 	uint32_t ipi_count_laddr;
200 
201 	/* remote addrlist zone info */
202 	uint32_t ipi_count_raddr;
203 
204 	/* chunk structure list for output */
205 	uint32_t ipi_count_chunk;
206 
207 	/* socket queue zone info */
208 	uint32_t ipi_count_readq;
209 
210 	/* socket queue zone info */
211 	uint32_t ipi_count_strmoq;
212 
213 	/* Number of vrfs */
214 	uint32_t ipi_count_vrfs;
215 
216 	/* Number of ifns */
217 	uint32_t ipi_count_ifns;
218 
219 	/* Number of ifas */
220 	uint32_t ipi_count_ifas;
221 
222 	/* system wide number of free chunks hanging around */
223 	uint32_t ipi_free_chunks;
224 	uint32_t ipi_free_strmoq;
225 
226 
227 	struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE_A];
228 
229 	/* address work queue handling */
230 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
231 	uint32_t iterator_running;
232 	SCTP_PROCESS_STRUCT thread_proc;
233 #endif
234 	struct sctp_timer addr_wq_timer;
235 
236 };
237 
238 /*-
239  * Here we have all the relevant information for each SCTP entity created. We
240  * will need to modify this as approprate. We also need to figure out how to
241  * access /dev/random.
242  */
243 struct sctp_pcb {
244 	unsigned int time_of_secret_change;	/* number of seconds from
245 						 * timeval.tv_sec */
246 	uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
247 	unsigned int size_of_a_cookie;
248 
249 	unsigned int sctp_timeoutticks[SCTP_NUM_TMRS];
250 	unsigned int sctp_minrto;
251 	unsigned int sctp_maxrto;
252 	unsigned int initial_rto;
253 	int initial_init_rto_max;
254 
255 	unsigned int sctp_sack_freq;
256 	uint32_t sctp_sws_sender;
257 	uint32_t sctp_sws_receiver;
258 
259 	uint32_t sctp_default_cc_module;
260 	/* authentication related fields */
261 	struct sctp_keyhead shared_keys;
262 	sctp_auth_chklist_t *local_auth_chunks;
263 	sctp_hmaclist_t *local_hmacs;
264 	uint16_t default_keyid;
265 
266 	/* various thresholds */
267 	/* Max times I will init at a guy */
268 	uint16_t max_init_times;
269 
270 	/* Max times I will send before we consider someone dead */
271 	uint16_t max_send_times;
272 
273 	uint16_t def_net_failure;
274 
275 	/* number of streams to pre-open on a association */
276 	uint16_t pre_open_stream_count;
277 	uint16_t max_open_streams_intome;
278 
279 	/* random number generator */
280 	uint32_t random_counter;
281 	uint8_t random_numbers[SCTP_SIGNATURE_ALOC_SIZE];
282 	uint8_t random_store[SCTP_SIGNATURE_ALOC_SIZE];
283 
284 	/*
285 	 * This timer is kept running per endpoint.  When it fires it will
286 	 * change the secret key.  The default is once a hour
287 	 */
288 	struct sctp_timer signature_change;
289 
290 	/* Zero copy full buffer timer */
291 	struct sctp_timer zero_copy_timer;
292 	/* Zero copy app to transport (sendq) read repulse timer */
293 	struct sctp_timer zero_copy_sendq_timer;
294 	uint32_t def_cookie_life;
295 	/* defaults to 0 */
296 	int auto_close_time;
297 	uint32_t initial_sequence_debug;
298 	uint32_t adaptation_layer_indicator;
299 	char store_at;
300 	uint8_t max_burst;
301 	char current_secret_number;
302 	char last_secret_number;
303 };
304 
305 #ifndef SCTP_ALIGNMENT
306 #define SCTP_ALIGNMENT 32
307 #endif
308 
309 #ifndef SCTP_ALIGNM1
310 #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
311 #endif
312 
313 #define sctp_lport ip_inp.inp.inp_lport
314 
315 struct sctp_pcbtsn_rlog {
316 	uint32_t vtag;
317 	uint16_t strm;
318 	uint16_t seq;
319 	uint16_t sz;
320 	uint16_t flgs;
321 };
322 
323 #define SCTP_READ_LOG_SIZE 135	/* we choose the number to make a pcb a page */
324 
325 
326 struct sctp_inpcb {
327 	/*-
328 	 * put an inpcb in front of it all, kind of a waste but we need to
329 	 * for compatability with all the other stuff.
330 	 */
331 	union {
332 		struct inpcb inp;
333 		char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
334 		        ~SCTP_ALIGNM1];
335 	}     ip_inp;
336 
337 
338 	/* Socket buffer lock protects read_queue and of course sb_cc */
339 	struct sctp_readhead read_queue;
340 
341 	              LIST_ENTRY(sctp_inpcb) sctp_list;	/* lists all endpoints */
342 	/* hash of all endpoints for model */
343 	              LIST_ENTRY(sctp_inpcb) sctp_hash;
344 	/* count of local addresses bound, 0 if bound all */
345 	int laddr_count;
346 
347 	/* list of addrs in use by the EP, NULL if bound-all */
348 	struct sctpladdr sctp_addr_list;
349 	/*
350 	 * used for source address selection rotation when we are subset
351 	 * bound
352 	 */
353 	struct sctp_laddr *next_addr_touse;
354 
355 	/* back pointer to our socket */
356 	struct socket *sctp_socket;
357 	uint32_t sctp_flags;	/* INP state flag set */
358 	uint32_t sctp_features;	/* Feature flags */
359 	struct sctp_pcb sctp_ep;/* SCTP ep data */
360 	/* head of the hash of all associations */
361 	struct sctpasochead *sctp_tcbhash;
362 	u_long sctp_hashmark;
363 	/* head of the list of all associations */
364 	struct sctpasochead sctp_asoc_list;
365 #ifdef SCTP_TRACK_FREED_ASOCS
366 	struct sctpasochead sctp_asoc_free_list;
367 #endif
368 	struct sctp_iterator *inp_starting_point_for_iterator;
369 	uint32_t sctp_frag_point;
370 	uint32_t partial_delivery_point;
371 	uint32_t sctp_context;
372 	struct sctp_nonpad_sndrcvinfo def_send;
373 	/*-
374 	 * These three are here for the sosend_dgram
375 	 * (pkt, pkt_last and control).
376 	 * routine. However, I don't think anyone in
377 	 * the current FreeBSD kernel calls this. So
378 	 * they are candidates with sctp_sendm for
379 	 * de-supporting.
380 	 */
381 	struct mbuf *pkt, *pkt_last;
382 	struct mbuf *control;
383 	struct mtx inp_mtx;
384 	struct mtx inp_create_mtx;
385 	struct mtx inp_rdata_mtx;
386 	int32_t refcount;
387 	uint32_t def_vrf_id;
388 	uint32_t total_sends;
389 	uint32_t total_recvs;
390 	uint32_t last_abort_code;
391 	uint32_t total_nospaces;
392 #ifdef SCTP_ASOCLOG_OF_TSNS
393 	struct sctp_pcbtsn_rlog readlog[SCTP_READ_LOG_SIZE];
394 	uint32_t readlog_index;
395 #endif
396 };
397 
398 struct sctp_tcb {
399 	struct socket *sctp_socket;	/* back pointer to socket */
400 	struct sctp_inpcb *sctp_ep;	/* back pointer to ep */
401 	           LIST_ENTRY(sctp_tcb) sctp_tcbhash;	/* next link in hash
402 							 * table */
403 	           LIST_ENTRY(sctp_tcb) sctp_tcblist;	/* list of all of the
404 							 * TCB's */
405 	           LIST_ENTRY(sctp_tcb) sctp_tcbrestarhash;	/* next link in restart
406 								 * hash table */
407 	           LIST_ENTRY(sctp_tcb) sctp_asocs;	/* vtag hash list */
408 	struct sctp_block_entry *block_entry;	/* pointer locked by  socket
409 						 * send buffer */
410 	struct sctp_association asoc;
411 	/*-
412 	 * freed_by_sorcv_sincelast is protected by the sockbuf_lock NOT the
413 	 * tcb_lock. Its special in this way to help avoid extra mutex calls
414 	 * in the reading of data.
415 	 */
416 	uint32_t freed_by_sorcv_sincelast;
417 	uint32_t total_sends;
418 	uint32_t total_recvs;
419 	int freed_from_where;
420 	uint16_t rport;		/* remote port in network format */
421 	uint16_t resv;
422 	struct mtx tcb_mtx;
423 	struct mtx tcb_send_mtx;
424 };
425 
426 
427 
428 #include <netinet/sctp_lock_bsd.h>
429 
430 
431 #if defined(_KERNEL)
432 
433 extern struct sctp_epinfo sctppcbinfo;
434 
435 int SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b);
436 
437 void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
438 
439 struct sctp_ifn *
440          sctp_find_ifn(void *ifn, uint32_t ifn_index);
441 
442 struct sctp_vrf *sctp_allocate_vrf(int vrfid);
443 struct sctp_vrf *sctp_find_vrf(uint32_t vrfid);
444 void sctp_free_vrf(struct sctp_vrf *vrf);
445 
446 struct sctp_ifa *
447 sctp_add_addr_to_vrf(uint32_t vrfid,
448     void *ifn, uint32_t ifn_index, uint32_t ifn_type,
449     const char *if_name,
450     void *ifa, struct sockaddr *addr, uint32_t ifa_flags,
451     int dynamic_add);
452 
453 void sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu);
454 
455 void sctp_free_ifn(struct sctp_ifn *sctp_ifnp);
456 void sctp_free_ifa(struct sctp_ifa *sctp_ifap);
457 
458 
459 void
460 sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
461     uint32_t ifn_index);
462 
463 
464 
465 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
466 
467 struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int, uint32_t);
468 
469 int
470 sctp_inpcb_bind(struct socket *, struct sockaddr *,
471     struct thread *);
472 
473 struct sctp_tcb *
474 sctp_findassociation_addr(struct mbuf *, int, int,
475     struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
476     struct sctp_nets **, uint32_t vrf_id);
477 
478 struct sctp_tcb *
479 sctp_findassociation_addr_sa(struct sockaddr *,
480     struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int, uint32_t);
481 
482 void
483 sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
484     struct sctp_tcb *);
485 
486 /*-
487  * For this call ep_addr, the to is the destination endpoint address of the
488  * peer (relative to outbound). The from field is only used if the TCP model
489  * is enabled and helps distingush amongst the subset bound (non-boundall).
490  * The TCP model MAY change the actual ep field, this is why it is passed.
491  */
492 struct sctp_tcb *
493 sctp_findassociation_ep_addr(struct sctp_inpcb **,
494     struct sockaddr *, struct sctp_nets **, struct sockaddr *,
495     struct sctp_tcb *);
496 
497 struct sctp_tcb *
498 sctp_findassociation_ep_asocid(struct sctp_inpcb *,
499     sctp_assoc_t, int);
500 
501 struct sctp_tcb *
502 sctp_findassociation_ep_asconf(struct mbuf *, int, int,
503     struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **);
504 
505 int sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id);
506 
507 int sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id);
508 
509 void sctp_inpcb_free(struct sctp_inpcb *, int, int);
510 
511 struct sctp_tcb *
512 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
513     int, int *, uint32_t, uint32_t, struct thread *);
514 
515 int sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *, int, int);
516 
517 void
518      sctp_add_vtag_to_timewait(struct sctp_inpcb *, uint32_t, uint32_t);
519 
520 void sctp_add_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *, uint32_t);
521 
522 int sctp_insert_laddr(struct sctpladdr *, struct sctp_ifa *, uint32_t);
523 
524 void sctp_remove_laddr(struct sctp_laddr *);
525 
526 void sctp_del_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *);
527 
528 int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, int, int);
529 
530 void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *);
531 
532 int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
533 
534 void sctp_pcb_init(void);
535 
536 void sctp_add_local_addr_assoc(struct sctp_tcb *, struct sctp_ifa *, int);
537 
538 void sctp_del_local_addr_assoc(struct sctp_tcb *, struct sctp_ifa *);
539 
540 int
541 sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
542     int, struct sctphdr *, struct sockaddr *);
543 
544 int
545 sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *,
546     struct sctp_nets *);
547 
548 int sctp_is_vtag_good(struct sctp_inpcb *, uint32_t, struct timeval *);
549 
550 /* void sctp_drain(void); */
551 
552 int sctp_destination_is_reachable(struct sctp_tcb *, struct sockaddr *);
553 
554 /*-
555  * Null in last arg inpcb indicate run on ALL ep's. Specific inp in last arg
556  * indicates run on ONLY assoc's of the specified endpoint.
557  */
558 int
559 sctp_initiate_iterator(inp_func inpf,
560     asoc_func af,
561     inp_func inpe,
562     uint32_t, uint32_t,
563     uint32_t, void *,
564     uint32_t,
565     end_func ef,
566     struct sctp_inpcb *,
567     uint8_t co_off);
568 
569 #endif				/* _KERNEL */
570 #endif				/* !__sctp_pcb_h__ */
571