xref: /freebsd/sys/netinet/sctp_pcb.h (revision 85999a0155e389415cc476110fd5614baf543a55)
1 /*-
2  * Copyright (c) 2001-2007, 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_uio.h>
55 #include <netinet/sctp_auth.h>
56 
57 #define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask)
58 #define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask)
59 
60 struct sctp_vrf {
61 	LIST_ENTRY(sctp_vrf) next_vrf;
62 	struct sctp_ifalist *vrf_addr_hash;
63 	struct sctp_ifnlist ifnlist;
64 	uint32_t vrf_id;
65 	uint32_t total_ifa_count;
66 	u_long vrf_hashmark;
67 };
68 
69 struct sctp_ifn {
70 	struct sctp_ifalist ifalist;
71 	struct sctp_vrf *vrf;
72 	         LIST_ENTRY(sctp_ifn) next_ifn;
73 	void *ifn_p;		/* never access without appropriate lock */
74 	uint32_t ifn_type;
75 	uint32_t ifn_index;	/* shorthand way to look at ifn for reference */
76 	uint32_t refcount;	/* number of reference held should be >=
77 				 * ifa_count */
78 	uint32_t ifa_count;	/* IFA's we hold (in our list - ifalist) */
79 	char ifn_name[SCTP_IFNAMSIZ];
80 };
81 
82 /* SCTP local IFA flags */
83 #define SCTP_ADDR_VALID         0x00000001	/* its up and active */
84 #define SCTP_BEING_DELETED      0x00000002	/* being deleted, when
85 						 * refcount = 0. Note that it
86 						 * is pulled from the ifn list
87 						 * and ifa_p is nulled right
88 						 * away but it cannot be freed
89 						 * until the last *net
90 						 * pointing to it is deleted. */
91 #define SCTP_ADDR_DEFER_USE     0x00000004	/* Hold off using this one */
92 #define SCTP_ADDR_IFA_UNUSEABLE 0x00000008
93 
94 struct sctp_ifa {
95 	LIST_ENTRY(sctp_ifa) next_ifa;
96 	LIST_ENTRY(sctp_ifa) next_bucket;
97 	struct sctp_ifn *ifn_p;	/* back pointer to parent ifn */
98 	void *ifa;		/* pointer to ifa, needed for flag update for
99 				 * that we MUST lock appropriate locks. This
100 				 * is for V6. */
101 	union sctp_sockstore address;
102 	uint32_t refcount;	/* number of folks refering to this */
103 	uint32_t flags;
104 	uint32_t localifa_flags;
105 	uint8_t src_is_loop;
106 	uint8_t src_is_priv;
107 	uint8_t src_is_glob;
108 	uint8_t in_ifa_list;
109 };
110 
111 struct sctp_laddr {
112 	LIST_ENTRY(sctp_laddr) sctp_nxt_addr;	/* next in list */
113 	struct sctp_ifa *ifa;
114 	uint32_t action;	/* Used during asconf and adding if no-zero
115 				 * src-addr selection will not consider this
116 				 * address. */
117 };
118 
119 struct sctp_block_entry {
120 	int error;
121 };
122 
123 struct sctp_timewait {
124 	uint32_t tv_sec_at_expire;	/* the seconds from boot to expire */
125 	uint32_t v_tag;		/* the vtag that can not be reused */
126 };
127 
128 struct sctp_tagblock {
129 	LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock;
130 	struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK];
131 };
132 
133 struct sctp_epinfo {
134 	struct sctpasochead *sctp_asochash;
135 	u_long hashasocmark;
136 
137 	struct sctppcbhead *sctp_ephash;
138 	u_long hashmark;
139 
140 	struct sctpasochead *sctp_restarthash;
141 	u_long hashrestartmark;
142 	/*-
143 	 * The TCP model represents a substantial overhead in that we get an
144 	 * additional hash table to keep explicit connections in. The
145 	 * listening TCP endpoint will exist in the usual ephash above and
146 	 * accept only INIT's. It will be incapable of sending off an INIT.
147 	 * When a dg arrives we must look in the normal ephash. If we find a
148 	 * TCP endpoint that will tell us to go to the specific endpoint
149 	 * hash and re-hash to find the right assoc/socket. If we find a UDP
150 	 * model socket we then must complete the lookup. If this fails,
151 	 * i.e. no association can be found then we must continue to see if
152 	 * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
153 	 * acts like a TCP model connected socket).
154 	 */
155 	struct sctppcbhead *sctp_tcpephash;
156 	u_long hashtcpmark;
157 	uint32_t hashtblsize;
158 
159 	struct sctp_vrflist *sctp_vrfhash;
160 	u_long hashvrfmark;
161 
162 	struct sctppcbhead listhead;
163 	struct sctpladdr addr_wq;
164 
165 	struct sctpiterators iteratorhead;
166 
167 	/* ep zone info */
168 	sctp_zone_t ipi_zone_ep;
169 	sctp_zone_t ipi_zone_asoc;
170 	sctp_zone_t ipi_zone_laddr;
171 	sctp_zone_t ipi_zone_net;
172 	sctp_zone_t ipi_zone_chunk;
173 	sctp_zone_t ipi_zone_readq;
174 	sctp_zone_t ipi_zone_strmoq;
175 
176 	struct mtx ipi_ep_mtx;
177 	struct mtx it_mtx;
178 	struct mtx ipi_iterator_wq_mtx;
179 	struct mtx ipi_addr_mtx;
180 	uint32_t ipi_count_ep;
181 
182 	/* assoc/tcb zone info */
183 	uint32_t ipi_count_asoc;
184 
185 	/* local addrlist zone info */
186 	uint32_t ipi_count_laddr;
187 
188 	/* remote addrlist zone info */
189 	uint32_t ipi_count_raddr;
190 
191 	/* chunk structure list for output */
192 	uint32_t ipi_count_chunk;
193 
194 	/* socket queue zone info */
195 	uint32_t ipi_count_readq;
196 
197 	/* socket queue zone info */
198 	uint32_t ipi_count_strmoq;
199 
200 	/* system wide number of free chunks hanging around */
201 	uint32_t ipi_free_chunks;
202 	uint32_t ipi_free_strmoq;
203 
204 	struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE];
205 
206 	/* address work queue handling */
207 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
208 	uint32_t iterator_running;
209 	SCTP_PROCESS_STRUCT thread_proc;
210 #endif
211 	struct sctp_timer addr_wq_timer;
212 
213 };
214 
215 /*-
216  * Here we have all the relevant information for each SCTP entity created. We
217  * will need to modify this as approprate. We also need to figure out how to
218  * access /dev/random.
219  */
220 struct sctp_pcb {
221 	unsigned int time_of_secret_change;	/* number of seconds from
222 						 * timeval.tv_sec */
223 	uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
224 	unsigned int size_of_a_cookie;
225 
226 	unsigned int sctp_timeoutticks[SCTP_NUM_TMRS];
227 	unsigned int sctp_minrto;
228 	unsigned int sctp_maxrto;
229 	unsigned int initial_rto;
230 	int initial_init_rto_max;
231 
232 	unsigned int sctp_sack_freq;
233 	uint32_t sctp_sws_sender;
234 	uint32_t sctp_sws_receiver;
235 
236 	/* authentication related fields */
237 	struct sctp_keyhead shared_keys;
238 	sctp_auth_chklist_t *local_auth_chunks;
239 	sctp_hmaclist_t *local_hmacs;
240 	uint16_t default_keyid;
241 
242 	/* various thresholds */
243 	/* Max times I will init at a guy */
244 	uint16_t max_init_times;
245 
246 	/* Max times I will send before we consider someone dead */
247 	uint16_t max_send_times;
248 
249 	uint16_t def_net_failure;
250 
251 	/* number of streams to pre-open on a association */
252 	uint16_t pre_open_stream_count;
253 	uint16_t max_open_streams_intome;
254 
255 	/* random number generator */
256 	uint32_t random_counter;
257 	uint8_t random_numbers[SCTP_SIGNATURE_ALOC_SIZE];
258 	uint8_t random_store[SCTP_SIGNATURE_ALOC_SIZE];
259 
260 	/*
261 	 * This timer is kept running per endpoint.  When it fires it will
262 	 * change the secret key.  The default is once a hour
263 	 */
264 	struct sctp_timer signature_change;
265 	int def_cookie_life;
266 	/* defaults to 0 */
267 	int auto_close_time;
268 	uint32_t initial_sequence_debug;
269 	uint32_t adaptation_layer_indicator;
270 	char store_at;
271 	uint8_t max_burst;
272 	char current_secret_number;
273 	char last_secret_number;
274 };
275 
276 #ifndef SCTP_ALIGNMENT
277 #define SCTP_ALIGNMENT 32
278 #endif
279 
280 #ifndef SCTP_ALIGNM1
281 #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
282 #endif
283 
284 #define sctp_lport ip_inp.inp.inp_lport
285 
286 struct sctp_inpcb {
287 	/*-
288 	 * put an inpcb in front of it all, kind of a waste but we need to
289 	 * for compatability with all the other stuff.
290 	 */
291 	union {
292 		struct inpcb inp;
293 		char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
294 		        ~SCTP_ALIGNM1];
295 	}     ip_inp;
296 
297 
298 	/* Socket buffer lock protects read_queue and of course sb_cc */
299 	struct sctp_readhead read_queue;
300 
301 	              LIST_ENTRY(sctp_inpcb) sctp_list;	/* lists all endpoints */
302 	/* hash of all endpoints for model */
303 	              LIST_ENTRY(sctp_inpcb) sctp_hash;
304 	/* count of local addresses bound, 0 if bound all */
305 	int laddr_count;
306 
307 	/* list of addrs in use by the EP, NULL if bound-all */
308 	struct sctpladdr sctp_addr_list;
309 	/*
310 	 * used for source address selection rotation when we are subset
311 	 * bound
312 	 */
313 	struct sctp_laddr *next_addr_touse;
314 
315 	/* back pointer to our socket */
316 	struct socket *sctp_socket;
317 	uint32_t sctp_flags;	/* INP state flag set */
318 	uint32_t sctp_features;	/* Feature flags */
319 	struct sctp_pcb sctp_ep;/* SCTP ep data */
320 	/* head of the hash of all associations */
321 	struct sctpasochead *sctp_tcbhash;
322 	u_long sctp_hashmark;
323 	/* head of the list of all associations */
324 	struct sctpasochead sctp_asoc_list;
325 #ifdef SCTP_TRACK_FREED_ASOCS
326 	struct sctpasochead sctp_asoc_free_list;
327 #endif
328 	struct sctp_iterator *inp_starting_point_for_iterator;
329 	uint32_t sctp_frag_point;
330 	uint32_t partial_delivery_point;
331 	uint32_t sctp_context;
332 	struct sctp_sndrcvinfo def_send;
333 	/*-
334 	 * These three are here for the sosend_dgram
335 	 * (pkt, pkt_last and control).
336 	 * routine. However, I don't think anyone in
337 	 * the current FreeBSD kernel calls this. So
338 	 * they are candidates with sctp_sendm for
339 	 * de-supporting.
340 	 */
341 	struct mbuf *pkt, *pkt_last;
342 	struct mbuf *control;
343 	struct mtx inp_mtx;
344 	struct mtx inp_create_mtx;
345 	struct mtx inp_rdata_mtx;
346 	int32_t refcount;
347 	uint32_t def_vrf_id;
348 	uint32_t total_sends;
349 	uint32_t total_recvs;
350 	uint32_t last_abort_code;
351 	uint32_t total_nospaces;
352 };
353 
354 struct sctp_tcb {
355 	struct socket *sctp_socket;	/* back pointer to socket */
356 	struct sctp_inpcb *sctp_ep;	/* back pointer to ep */
357 	           LIST_ENTRY(sctp_tcb) sctp_tcbhash;	/* next link in hash
358 							 * table */
359 	           LIST_ENTRY(sctp_tcb) sctp_tcblist;	/* list of all of the
360 							 * TCB's */
361 	           LIST_ENTRY(sctp_tcb) sctp_tcbrestarhash;	/* next link in restart
362 								 * hash table */
363 	           LIST_ENTRY(sctp_tcb) sctp_asocs;	/* vtag hash list */
364 	struct sctp_block_entry *block_entry;	/* pointer locked by  socket
365 						 * send buffer */
366 	struct sctp_association asoc;
367 	/*-
368 	 * freed_by_sorcv_sincelast is protected by the sockbuf_lock NOT the
369 	 * tcb_lock. Its special in this way to help avoid extra mutex calls
370 	 * in the reading of data.
371 	 */
372 	uint32_t freed_by_sorcv_sincelast;
373 	uint32_t total_sends;
374 	uint32_t total_recvs;
375 	int freed_from_where;
376 	uint16_t rport;		/* remote port in network format */
377 	uint16_t resv;
378 	struct mtx tcb_mtx;
379 	struct mtx tcb_send_mtx;
380 };
381 
382 
383 
384 #include <netinet/sctp_lock_bsd.h>
385 
386 
387 #if defined(_KERNEL)
388 
389 extern struct sctp_epinfo sctppcbinfo;
390 
391 int SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b);
392 
393 void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
394 
395 struct sctp_ifn *
396          sctp_find_ifn(struct sctp_vrf *vrf, void *ifn, uint32_t ifn_index);
397 
398 struct sctp_vrf *sctp_allocate_vrf(int vrfid);
399 
400 struct sctp_vrf *sctp_find_vrf(uint32_t vrfid);
401 
402 struct sctp_ifa *
403 sctp_add_addr_to_vrf(uint32_t vrfid,
404     void *ifn, uint32_t ifn_index, uint32_t ifn_type,
405     const char *if_name,
406     void *ifa, struct sockaddr *addr, uint32_t ifa_flags);
407 
408 void sctp_free_ifa(struct sctp_ifa *sctp_ifap);
409 
410 struct sctp_ifa *
411 sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
412     uint32_t ifn_index);
413 
414 
415 
416 
417 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
418 
419 struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int, uint32_t);
420 
421 int sctp_inpcb_bind(struct socket *, struct sockaddr *, struct thread *);
422 
423 
424 struct sctp_tcb *
425 sctp_findassociation_addr(struct mbuf *, int, int,
426     struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
427     struct sctp_nets **, uint32_t vrf_id);
428 
429 struct sctp_tcb *
430 sctp_findassociation_addr_sa(struct sockaddr *,
431     struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int, uint32_t);
432 
433 void
434 sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
435     struct sctp_tcb *);
436 
437 /*-
438  * For this call ep_addr, the to is the destination endpoint address of the
439  * peer (relative to outbound). The from field is only used if the TCP model
440  * is enabled and helps distingush amongst the subset bound (non-boundall).
441  * The TCP model MAY change the actual ep field, this is why it is passed.
442  */
443 struct sctp_tcb *
444 sctp_findassociation_ep_addr(struct sctp_inpcb **,
445     struct sockaddr *, struct sctp_nets **, struct sockaddr *,
446     struct sctp_tcb *);
447 
448 struct sctp_tcb *
449 sctp_findassociation_ep_asocid(struct sctp_inpcb *,
450     sctp_assoc_t, int);
451 
452 struct sctp_tcb *
453 sctp_findassociation_ep_asconf(struct mbuf *, int, int,
454     struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **);
455 
456 int sctp_inpcb_alloc(struct socket *);
457 
458 int sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id);
459 
460 void sctp_inpcb_free(struct sctp_inpcb *, int, int);
461 
462 struct sctp_tcb *
463 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
464     int, int *, uint32_t, uint32_t);
465 
466 int sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *, int, int);
467 
468 void
469      sctp_add_vtag_to_timewait(struct sctp_inpcb *, uint32_t, uint32_t);
470 
471 int sctp_add_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *, uint32_t);
472 
473 int sctp_insert_laddr(struct sctpladdr *, struct sctp_ifa *, uint32_t);
474 
475 void sctp_remove_laddr(struct sctp_laddr *);
476 
477 int sctp_del_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *);
478 
479 void sctp_set_initial_cc_param(struct sctp_tcb *, struct sctp_nets *net);
480 
481 
482 int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, int, int);
483 
484 void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *);
485 
486 int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
487 
488 void sctp_pcb_init(void);
489 
490 int sctp_add_local_addr_assoc(struct sctp_tcb *, struct sctp_ifa *, int);
491 
492 int sctp_del_local_addr_assoc(struct sctp_tcb *, struct sctp_ifa *);
493 
494 int
495 sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
496     int, struct sctphdr *, struct sockaddr *);
497 
498 int
499 sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *,
500     struct sctp_nets *);
501 
502 int sctp_is_vtag_good(struct sctp_inpcb *, uint32_t, struct timeval *);
503 
504 /* void sctp_drain(void); */
505 
506 int sctp_destination_is_reachable(struct sctp_tcb *, struct sockaddr *);
507 
508 /*-
509  * Null in last arg inpcb indicate run on ALL ep's. Specific inp in last arg
510  * indicates run on ONLY assoc's of the specified endpoint.
511  */
512 int
513 sctp_initiate_iterator(inp_func inpf,
514     asoc_func af,
515     inp_func inpe,
516     uint32_t, uint32_t,
517     uint32_t, void *,
518     uint32_t,
519     end_func ef,
520     struct sctp_inpcb *,
521     uint8_t co_off);
522 
523 #endif				/* _KERNEL */
524 #endif				/* !__sctp_pcb_h__ */
525