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