sctp_pcb.h revision 169352
1239313Sdim/*-
2239313Sdim * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
3239313Sdim *
4239313Sdim * Redistribution and use in source and binary forms, with or without
5239313Sdim * modification, are permitted provided that the following conditions are met:
6239313Sdim *
7239313Sdim * a) Redistributions of source code must retain the above copyright notice,
8239313Sdim *   this list of conditions and the following disclaimer.
9239313Sdim *
10239313Sdim * b) Redistributions in binary form must reproduce the above copyright
11243830Sdim *    notice, this list of conditions and the following disclaimer in
12239313Sdim *   the documentation and/or other materials provided with the distribution.
13239313Sdim *
14239313Sdim * c) Neither the name of Cisco Systems, Inc. nor the names of its
15239313Sdim *    contributors may be used to endorse or promote products derived
16243830Sdim *    from this software without specific prior written permission.
17239313Sdim *
18249423Sdim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19249423Sdim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20249423Sdim * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21249423Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22249423Sdim * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23239313Sdim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24249423Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25249423Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26249423Sdim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27249423Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28249423Sdim * THE POSSIBILITY OF SUCH DAMAGE.
29249423Sdim */
30249423Sdim
31249423Sdim/* $KAME: sctp_pcb.h,v 1.21 2005/07/16 01:18:47 suz Exp $	 */
32249423Sdim
33249423Sdim#include <sys/cdefs.h>
34243830Sdim__FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.h 169352 2007-05-08 00:21:05Z rrs $");
35243830Sdim
36243830Sdim#ifndef __sctp_pcb_h__
37243830Sdim#define __sctp_pcb_h__
38243830Sdim
39239313Sdim#include <netinet/sctp_os.h>
40243830Sdim#include <netinet/sctp.h>
41243830Sdim#include <netinet/sctp_constants.h>
42243830Sdim
43243830SdimLIST_HEAD(sctppcbhead, sctp_inpcb);
44239313SdimLIST_HEAD(sctpasochead, sctp_tcb);
45239313SdimLIST_HEAD(sctpladdr, sctp_laddr);
46263508SdimLIST_HEAD(sctpvtaghead, sctp_tagblock);
47263508SdimLIST_HEAD(sctp_vrflist, sctp_vrf);
48263508SdimLIST_HEAD(sctp_ifnlist, sctp_ifn);
49263508SdimLIST_HEAD(sctp_ifalist, sctp_ifa);
50263508SdimTAILQ_HEAD(sctp_readhead, sctp_queued_to_read);
51263508SdimTAILQ_HEAD(sctp_streamhead, sctp_stream_queue_pending);
52263508Sdim
53263508Sdim#include <netinet/sctp_structs.h>
54263508Sdim#include <netinet/sctp_auth.h>
55263508Sdim
56263508Sdim#define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask)
57263508Sdim#define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask)
58263508Sdim
59263508Sdimstruct sctp_vrf {
60263508Sdim	LIST_ENTRY(sctp_vrf) next_vrf;
61263508Sdim	struct sctp_ifalist *vrf_addr_hash;
62263508Sdim	struct sctp_ifnlist *vrf_ifn_hash;
63263508Sdim	struct sctp_ifnlist ifnlist;
64263508Sdim	uint32_t vrf_id;
65263508Sdim	uint32_t total_ifa_count;
66263508Sdim	u_long vrf_addr_hashmark;
67263508Sdim	u_long vrf_ifn_hashmark;
68263508Sdim};
69263508Sdim
70263508Sdimstruct sctp_ifn {
71263508Sdim	struct sctp_ifalist ifalist;
72263508Sdim	struct sctp_vrf *vrf;
73263508Sdim	         LIST_ENTRY(sctp_ifn) next_ifn;
74263508Sdim	         LIST_ENTRY(sctp_ifn) next_bucket;
75263508Sdim	void *ifn_p;		/* never access without appropriate lock */
76263508Sdim	uint32_t ifn_mtu;
77263508Sdim	uint32_t ifn_type;
78263508Sdim	uint32_t ifn_index;	/* shorthand way to look at ifn for reference */
79263508Sdim	uint32_t refcount;	/* number of reference held should be >=
80263508Sdim				 * ifa_count */
81263508Sdim	uint32_t ifa_count;	/* IFA's we hold (in our list - ifalist) */
82263508Sdim	char ifn_name[SCTP_IFNAMSIZ];
83263508Sdim};
84263508Sdim
85263508Sdim/* SCTP local IFA flags */
86263508Sdim#define SCTP_ADDR_VALID         0x00000001	/* its up and active */
87263508Sdim#define SCTP_BEING_DELETED      0x00000002	/* being deleted, when
88263508Sdim						 * refcount = 0. Note that it
89249423Sdim						 * is pulled from the ifn list
90243830Sdim						 * and ifa_p is nulled right
91243830Sdim						 * away but it cannot be freed
92243830Sdim						 * until the last *net
93239313Sdim						 * pointing to it is deleted. */
94243830Sdim#define SCTP_ADDR_DEFER_USE     0x00000004	/* Hold off using this one */
95243830Sdim#define SCTP_ADDR_IFA_UNUSEABLE 0x00000008
96243830Sdim
97243830Sdimstruct sctp_ifa {
98239313Sdim	LIST_ENTRY(sctp_ifa) next_ifa;
99243830Sdim	LIST_ENTRY(sctp_ifa) next_bucket;
100239313Sdim	struct sctp_ifn *ifn_p;	/* back pointer to parent ifn */
101243830Sdim	void *ifa;		/* pointer to ifa, needed for flag update for
102239313Sdim				 * that we MUST lock appropriate locks. This
103239313Sdim				 * is for V6. */
104249423Sdim	union sctp_sockstore address;
105249423Sdim	uint32_t refcount;	/* number of folks refering to this */
106249423Sdim	uint32_t flags;
107249423Sdim	uint32_t localifa_flags;
108249423Sdim	uint8_t src_is_loop;
109249423Sdim	uint8_t src_is_priv;
110249423Sdim	uint8_t src_is_glob;
111249423Sdim	uint8_t resv;
112249423Sdim
113249423Sdim};
114249423Sdim
115249423Sdimstruct sctp_laddr {
116249423Sdim	LIST_ENTRY(sctp_laddr) sctp_nxt_addr;	/* next in list */
117243830Sdim	struct sctp_ifa *ifa;
118243830Sdim	uint32_t action;	/* Used during asconf and adding if no-zero
119243830Sdim				 * src-addr selection will not consider this
120243830Sdim				 * address. */
121243830Sdim};
122239313Sdim
123239313Sdimstruct sctp_block_entry {
124243830Sdim	int error;
125243830Sdim};
126243830Sdim
127243830Sdimstruct sctp_timewait {
128243830Sdim	uint32_t tv_sec_at_expire;	/* the seconds from boot to expire */
129243830Sdim	uint32_t v_tag;		/* the vtag that can not be reused */
130243830Sdim};
131239313Sdim
132239313Sdimstruct sctp_tagblock {
133243830Sdim	LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock;
134243830Sdim	struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK];
135243830Sdim};
136239313Sdim
137239313Sdimstruct sctp_epinfo {
138239313Sdim	struct sctpasochead *sctp_asochash;
139239313Sdim	u_long hashasocmark;
140239313Sdim
141	struct sctppcbhead *sctp_ephash;
142	u_long hashmark;
143
144	struct sctpasochead *sctp_restarthash;
145	u_long hashrestartmark;
146	/*-
147	 * The TCP model represents a substantial overhead in that we get an
148	 * additional hash table to keep explicit connections in. The
149	 * listening TCP endpoint will exist in the usual ephash above and
150	 * accept only INIT's. It will be incapable of sending off an INIT.
151	 * When a dg arrives we must look in the normal ephash. If we find a
152	 * TCP endpoint that will tell us to go to the specific endpoint
153	 * hash and re-hash to find the right assoc/socket. If we find a UDP
154	 * model socket we then must complete the lookup. If this fails,
155	 * i.e. no association can be found then we must continue to see if
156	 * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket
157	 * acts like a TCP model connected socket).
158	 */
159	struct sctppcbhead *sctp_tcpephash;
160	u_long hashtcpmark;
161	uint32_t hashtblsize;
162
163	struct sctp_vrflist *sctp_vrfhash;
164	u_long hashvrfmark;
165
166	struct sctppcbhead listhead;
167	struct sctpladdr addr_wq;
168
169	struct sctpiterators iteratorhead;
170
171	/* ep zone info */
172	sctp_zone_t ipi_zone_ep;
173	sctp_zone_t ipi_zone_asoc;
174	sctp_zone_t ipi_zone_laddr;
175	sctp_zone_t ipi_zone_net;
176	sctp_zone_t ipi_zone_chunk;
177	sctp_zone_t ipi_zone_readq;
178	sctp_zone_t ipi_zone_strmoq;
179
180	struct mtx ipi_ep_mtx;
181	struct mtx it_mtx;
182	struct mtx ipi_iterator_wq_mtx;
183	struct mtx ipi_addr_mtx;
184	uint32_t ipi_count_ep;
185
186	/* assoc/tcb zone info */
187	uint32_t ipi_count_asoc;
188
189	/* local addrlist zone info */
190	uint32_t ipi_count_laddr;
191
192	/* remote addrlist zone info */
193	uint32_t ipi_count_raddr;
194
195	/* chunk structure list for output */
196	uint32_t ipi_count_chunk;
197
198	/* socket queue zone info */
199	uint32_t ipi_count_readq;
200
201	/* socket queue zone info */
202	uint32_t ipi_count_strmoq;
203
204	/* Number of vrfs */
205	uint32_t ipi_count_vrfs;
206
207	/* Number of ifns */
208	uint32_t ipi_count_ifns;
209
210	/* Number of ifas */
211	uint32_t ipi_count_ifas;
212
213	/* system wide number of free chunks hanging around */
214	uint32_t ipi_free_chunks;
215	uint32_t ipi_free_strmoq;
216
217
218	struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE];
219
220	/* address work queue handling */
221#if defined(SCTP_USE_THREAD_BASED_ITERATOR)
222	uint32_t iterator_running;
223	SCTP_PROCESS_STRUCT thread_proc;
224#endif
225	struct sctp_timer addr_wq_timer;
226
227};
228
229/*-
230 * Here we have all the relevant information for each SCTP entity created. We
231 * will need to modify this as approprate. We also need to figure out how to
232 * access /dev/random.
233 */
234struct sctp_pcb {
235	unsigned int time_of_secret_change;	/* number of seconds from
236						 * timeval.tv_sec */
237	uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS];
238	unsigned int size_of_a_cookie;
239
240	unsigned int sctp_timeoutticks[SCTP_NUM_TMRS];
241	unsigned int sctp_minrto;
242	unsigned int sctp_maxrto;
243	unsigned int initial_rto;
244	int initial_init_rto_max;
245
246	unsigned int sctp_sack_freq;
247	uint32_t sctp_sws_sender;
248	uint32_t sctp_sws_receiver;
249
250	/* authentication related fields */
251	struct sctp_keyhead shared_keys;
252	sctp_auth_chklist_t *local_auth_chunks;
253	sctp_hmaclist_t *local_hmacs;
254	uint16_t default_keyid;
255
256	/* various thresholds */
257	/* Max times I will init at a guy */
258	uint16_t max_init_times;
259
260	/* Max times I will send before we consider someone dead */
261	uint16_t max_send_times;
262
263	uint16_t def_net_failure;
264
265	/* number of streams to pre-open on a association */
266	uint16_t pre_open_stream_count;
267	uint16_t max_open_streams_intome;
268
269	/* random number generator */
270	uint32_t random_counter;
271	uint8_t random_numbers[SCTP_SIGNATURE_ALOC_SIZE];
272	uint8_t random_store[SCTP_SIGNATURE_ALOC_SIZE];
273
274	/*
275	 * This timer is kept running per endpoint.  When it fires it will
276	 * change the secret key.  The default is once a hour
277	 */
278	struct sctp_timer signature_change;
279	int def_cookie_life;
280	/* defaults to 0 */
281	int auto_close_time;
282	uint32_t initial_sequence_debug;
283	uint32_t adaptation_layer_indicator;
284	char store_at;
285	uint8_t max_burst;
286	char current_secret_number;
287	char last_secret_number;
288};
289
290#ifndef SCTP_ALIGNMENT
291#define SCTP_ALIGNMENT 32
292#endif
293
294#ifndef SCTP_ALIGNM1
295#define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1)
296#endif
297
298#define sctp_lport ip_inp.inp.inp_lport
299
300struct sctp_inpcb {
301	/*-
302	 * put an inpcb in front of it all, kind of a waste but we need to
303	 * for compatability with all the other stuff.
304	 */
305	union {
306		struct inpcb inp;
307		char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) &
308		        ~SCTP_ALIGNM1];
309	}     ip_inp;
310
311
312	/* Socket buffer lock protects read_queue and of course sb_cc */
313	struct sctp_readhead read_queue;
314
315	              LIST_ENTRY(sctp_inpcb) sctp_list;	/* lists all endpoints */
316	/* hash of all endpoints for model */
317	              LIST_ENTRY(sctp_inpcb) sctp_hash;
318	/* count of local addresses bound, 0 if bound all */
319	int laddr_count;
320
321	/* list of addrs in use by the EP, NULL if bound-all */
322	struct sctpladdr sctp_addr_list;
323	/*
324	 * used for source address selection rotation when we are subset
325	 * bound
326	 */
327	struct sctp_laddr *next_addr_touse;
328
329	/* back pointer to our socket */
330	struct socket *sctp_socket;
331	uint32_t sctp_flags;	/* INP state flag set */
332	uint32_t sctp_features;	/* Feature flags */
333	struct sctp_pcb sctp_ep;/* SCTP ep data */
334	/* head of the hash of all associations */
335	struct sctpasochead *sctp_tcbhash;
336	u_long sctp_hashmark;
337	/* head of the list of all associations */
338	struct sctpasochead sctp_asoc_list;
339#ifdef SCTP_TRACK_FREED_ASOCS
340	struct sctpasochead sctp_asoc_free_list;
341#endif
342	struct sctp_iterator *inp_starting_point_for_iterator;
343	uint32_t sctp_frag_point;
344	uint32_t partial_delivery_point;
345	uint32_t sctp_context;
346	struct sctp_sndrcvinfo def_send;
347	/*-
348	 * These three are here for the sosend_dgram
349	 * (pkt, pkt_last and control).
350	 * routine. However, I don't think anyone in
351	 * the current FreeBSD kernel calls this. So
352	 * they are candidates with sctp_sendm for
353	 * de-supporting.
354	 */
355	struct mbuf *pkt, *pkt_last;
356	struct mbuf *control;
357	struct mtx inp_mtx;
358	struct mtx inp_create_mtx;
359	struct mtx inp_rdata_mtx;
360	int32_t refcount;
361	uint32_t def_vrf_id;
362	uint32_t def_table_id;
363	uint32_t total_sends;
364	uint32_t total_recvs;
365	uint32_t last_abort_code;
366	uint32_t total_nospaces;
367};
368
369struct sctp_tcb {
370	struct socket *sctp_socket;	/* back pointer to socket */
371	struct sctp_inpcb *sctp_ep;	/* back pointer to ep */
372	           LIST_ENTRY(sctp_tcb) sctp_tcbhash;	/* next link in hash
373							 * table */
374	           LIST_ENTRY(sctp_tcb) sctp_tcblist;	/* list of all of the
375							 * TCB's */
376	           LIST_ENTRY(sctp_tcb) sctp_tcbrestarhash;	/* next link in restart
377								 * hash table */
378	           LIST_ENTRY(sctp_tcb) sctp_asocs;	/* vtag hash list */
379	struct sctp_block_entry *block_entry;	/* pointer locked by  socket
380						 * send buffer */
381	struct sctp_association asoc;
382	/*-
383	 * freed_by_sorcv_sincelast is protected by the sockbuf_lock NOT the
384	 * tcb_lock. Its special in this way to help avoid extra mutex calls
385	 * in the reading of data.
386	 */
387	uint32_t freed_by_sorcv_sincelast;
388	uint32_t total_sends;
389	uint32_t total_recvs;
390	int freed_from_where;
391	uint16_t rport;		/* remote port in network format */
392	uint16_t resv;
393	struct mtx tcb_mtx;
394	struct mtx tcb_send_mtx;
395};
396
397
398
399#include <netinet/sctp_lock_bsd.h>
400
401
402#if defined(_KERNEL)
403
404extern struct sctp_epinfo sctppcbinfo;
405
406int SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b);
407
408void sctp_fill_pcbinfo(struct sctp_pcbinfo *);
409
410struct sctp_ifn *
411         sctp_find_ifn(struct sctp_vrf *vrf, void *ifn, uint32_t ifn_index);
412
413struct sctp_vrf *sctp_allocate_vrf(int vrfid);
414
415struct sctp_vrf *sctp_find_vrf(uint32_t vrfid);
416
417struct sctp_ifa *
418sctp_add_addr_to_vrf(uint32_t vrfid,
419    void *ifn, uint32_t ifn_index, uint32_t ifn_type,
420    const char *if_name,
421    void *ifa, struct sockaddr *addr, uint32_t ifa_flags, int dynamic_add);
422
423void sctp_update_ifn_mtu(uint32_t vrf_id, uint32_t ifn_index, uint32_t mtu);
424
425void sctp_free_ifn(struct sctp_ifn *sctp_ifnp);
426void sctp_free_ifa(struct sctp_ifa *sctp_ifap);
427
428
429void
430sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
431    uint32_t ifn_index);
432
433
434
435struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *);
436
437struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int, uint32_t);
438
439int sctp_inpcb_bind(struct socket *, struct sockaddr *, struct thread *);
440
441
442struct sctp_tcb *
443sctp_findassociation_addr(struct mbuf *, int, int,
444    struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **,
445    struct sctp_nets **, uint32_t vrf_id);
446
447struct sctp_tcb *
448sctp_findassociation_addr_sa(struct sockaddr *,
449    struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int, uint32_t);
450
451void
452sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *,
453    struct sctp_tcb *);
454
455/*-
456 * For this call ep_addr, the to is the destination endpoint address of the
457 * peer (relative to outbound). The from field is only used if the TCP model
458 * is enabled and helps distingush amongst the subset bound (non-boundall).
459 * The TCP model MAY change the actual ep field, this is why it is passed.
460 */
461struct sctp_tcb *
462sctp_findassociation_ep_addr(struct sctp_inpcb **,
463    struct sockaddr *, struct sctp_nets **, struct sockaddr *,
464    struct sctp_tcb *);
465
466struct sctp_tcb *
467sctp_findassociation_ep_asocid(struct sctp_inpcb *,
468    sctp_assoc_t, int);
469
470struct sctp_tcb *
471sctp_findassociation_ep_asconf(struct mbuf *, int, int,
472    struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **);
473
474int sctp_inpcb_alloc(struct socket *);
475
476int sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id);
477
478void sctp_inpcb_free(struct sctp_inpcb *, int, int);
479
480struct sctp_tcb *
481sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *,
482    int, int *, uint32_t, uint32_t);
483
484int sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *, int, int);
485
486void
487     sctp_add_vtag_to_timewait(struct sctp_inpcb *, uint32_t, uint32_t);
488
489int sctp_add_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *, uint32_t);
490
491int sctp_insert_laddr(struct sctpladdr *, struct sctp_ifa *, uint32_t);
492
493void sctp_remove_laddr(struct sctp_laddr *);
494
495int sctp_del_local_addr_ep(struct sctp_inpcb *, struct sctp_ifa *);
496
497void sctp_set_initial_cc_param(struct sctp_tcb *, struct sctp_nets *net);
498
499
500int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, int, int);
501
502void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *);
503
504int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *);
505
506void sctp_pcb_init(void);
507
508int sctp_add_local_addr_assoc(struct sctp_tcb *, struct sctp_ifa *, int);
509
510int sctp_del_local_addr_assoc(struct sctp_tcb *, struct sctp_ifa *);
511
512int
513sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int,
514    int, struct sctphdr *, struct sockaddr *);
515
516int
517sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *,
518    struct sctp_nets *);
519
520int sctp_is_vtag_good(struct sctp_inpcb *, uint32_t, struct timeval *);
521
522/* void sctp_drain(void); */
523
524int sctp_destination_is_reachable(struct sctp_tcb *, struct sockaddr *);
525
526/*-
527 * Null in last arg inpcb indicate run on ALL ep's. Specific inp in last arg
528 * indicates run on ONLY assoc's of the specified endpoint.
529 */
530int
531sctp_initiate_iterator(inp_func inpf,
532    asoc_func af,
533    inp_func inpe,
534    uint32_t, uint32_t,
535    uint32_t, void *,
536    uint32_t,
537    end_func ef,
538    struct sctp_inpcb *,
539    uint8_t co_off);
540
541#endif				/* _KERNEL */
542#endif				/* !__sctp_pcb_h__ */
543