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