SCTP: Fix to encode PROTOCOL VIOLATION error cause correctly
[safe/jmp/linux-2.6] / include / net / sctp / structs.h
1 /* SCTP kernel reference Implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001 Intel Corp.
6  *
7  * This file is part of the SCTP kernel reference Implementation
8  *
9  * The SCTP reference implementation is free software;
10  * you can redistribute it and/or modify it under the terms of
11  * the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * The SCTP reference implementation is distributed in the hope that it
16  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17  *                 ************************
18  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with GNU CC; see the file COPYING.  If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * Please send any bug reports or fixes you make to the
27  * email addresses:
28  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
29  *
30  * Or submit a bug report through the following website:
31  *    http://www.sf.net/projects/lksctp
32  *
33  * Written or modified by:
34  *    Randall Stewart       <randall@sctp.chicago.il.us>
35  *    Ken Morneau           <kmorneau@cisco.com>
36  *    Qiaobing Xie          <qxie1@email.mot.com>
37  *    La Monte H.P. Yarroll <piggy@acm.org>
38  *    Karl Knutson          <karl@athena.chicago.il.us>
39  *    Jon Grimm             <jgrimm@us.ibm.com>
40  *    Xingang Guo           <xingang.guo@intel.com>
41  *    Hui Huang             <hui.huang@nokia.com>
42  *    Sridhar Samudrala     <sri@us.ibm.com>
43  *    Daisy Chang           <daisyc@us.ibm.com>
44  *    Dajiang Zhang         <dajiang.zhang@nokia.com>
45  *    Ardelle Fan           <ardelle.fan@intel.com>
46  *    Ryan Layer            <rmlayer@us.ibm.com>
47  *    Anup Pemmaiah         <pemmaiah@cc.usu.edu>
48  *    Kevin Gao             <kevin.gao@intel.com>
49  *
50  * Any bugs reported given to us we will try to fix... any fixes shared will
51  * be incorporated into the next SCTP release.
52  */
53
54 #ifndef __sctp_structs_h__
55 #define __sctp_structs_h__
56
57 #include <linux/time.h>         /* We get struct timespec.    */
58 #include <linux/socket.h>       /* linux/in.h needs this!!    */
59 #include <linux/in.h>           /* We get struct sockaddr_in. */
60 #include <linux/in6.h>          /* We get struct in6_addr     */
61 #include <linux/ipv6.h>
62 #include <asm/param.h>          /* We get MAXHOSTNAMELEN.     */
63 #include <asm/atomic.h>         /* This gets us atomic counters.  */
64 #include <linux/skbuff.h>       /* We need sk_buff_head. */
65 #include <linux/workqueue.h>    /* We need tq_struct.    */
66 #include <linux/sctp.h>         /* We need sctp* header structs.  */
67
68 /* A convenience structure for handling sockaddr structures.
69  * We should wean ourselves off this.
70  */
71 union sctp_addr {
72         struct sockaddr_in v4;
73         struct sockaddr_in6 v6;
74         struct sockaddr sa;
75 };
76
77 /* Forward declarations for data structures. */
78 struct sctp_globals;
79 struct sctp_endpoint;
80 struct sctp_association;
81 struct sctp_transport;
82 struct sctp_packet;
83 struct sctp_chunk;
84 struct sctp_inq;
85 struct sctp_outq;
86 struct sctp_bind_addr;
87 struct sctp_ulpq;
88 struct sctp_ep_common;
89 struct sctp_ssnmap;
90 struct crypto_hash;
91
92
93 #include <net/sctp/tsnmap.h>
94 #include <net/sctp/ulpevent.h>
95 #include <net/sctp/ulpqueue.h>
96
97 /* Structures useful for managing bind/connect. */
98
99 struct sctp_bind_bucket {
100         unsigned short  port;
101         unsigned short  fastreuse;
102         struct sctp_bind_bucket *next;
103         struct sctp_bind_bucket **pprev;
104         struct hlist_head       owner;
105 };
106
107 struct sctp_bind_hashbucket {
108         spinlock_t      lock;
109         struct sctp_bind_bucket *chain;
110 };
111
112 /* Used for hashing all associations.  */
113 struct sctp_hashbucket {
114         rwlock_t        lock;
115         struct sctp_ep_common  *chain;
116 } __attribute__((__aligned__(8)));
117
118
119 /* The SCTP globals structure. */
120 extern struct sctp_globals {
121         /* RFC2960 Section 14. Suggested SCTP Protocol Parameter Values
122          *
123          * The following protocol parameters are RECOMMENDED:
124          *
125          * RTO.Initial              - 3  seconds
126          * RTO.Min                  - 1  second
127          * RTO.Max                 -  60 seconds
128          * RTO.Alpha                - 1/8  (3 when converted to right shifts.)
129          * RTO.Beta                 - 1/4  (2 when converted to right shifts.)
130          */
131         unsigned int rto_initial;
132         unsigned int rto_min;
133         unsigned int rto_max;
134
135         /* Note: rto_alpha and rto_beta are really defined as inverse
136          * powers of two to facilitate integer operations.
137          */
138         int rto_alpha;
139         int rto_beta;
140
141         /* Max.Burst                - 4 */
142         int max_burst;
143
144         /* Whether Cookie Preservative is enabled(1) or not(0) */
145         int cookie_preserve_enable;
146
147         /* Valid.Cookie.Life        - 60  seconds  */
148         unsigned int valid_cookie_life;
149
150         /* Delayed SACK timeout  200ms default*/
151         unsigned int sack_timeout;
152
153         /* HB.interval              - 30 seconds  */
154         unsigned int hb_interval;
155
156         /* Association.Max.Retrans  - 10 attempts
157          * Path.Max.Retrans         - 5  attempts (per destination address)
158          * Max.Init.Retransmits     - 8  attempts
159          */
160         int max_retrans_association;
161         int max_retrans_path;
162         int max_retrans_init;
163
164         /*
165          * Policy for preforming sctp/socket accounting
166          * 0   - do socket level accounting, all assocs share sk_sndbuf
167          * 1   - do sctp accounting, each asoc may use sk_sndbuf bytes
168          */
169         int sndbuf_policy;
170
171         /*
172          * Policy for preforming sctp/socket accounting
173          * 0   - do socket level accounting, all assocs share sk_rcvbuf
174          * 1   - do sctp accounting, each asoc may use sk_rcvbuf bytes
175          */
176         int rcvbuf_policy;
177
178         /* The following variables are implementation specific.  */
179
180         /* Default initialization values to be applied to new associations. */
181         __u16 max_instreams;
182         __u16 max_outstreams;
183
184         /* This is a list of groups of functions for each address
185          * family that we support.
186          */
187         struct list_head address_families;
188
189         /* This is the hash of all endpoints. */
190         int ep_hashsize;
191         struct sctp_hashbucket *ep_hashtable;
192
193         /* This is the hash of all associations. */
194         int assoc_hashsize;
195         struct sctp_hashbucket *assoc_hashtable;
196
197         /* This is the sctp port control hash.  */
198         int port_hashsize;
199         int port_rover;
200         spinlock_t port_alloc_lock;  /* Protects port_rover. */
201         struct sctp_bind_hashbucket *port_hashtable;
202
203         /* This is the global local address list.
204          * We actively maintain this complete list of addresses on
205          * the system by catching address add/delete events.
206          *
207          * It is a list of sctp_sockaddr_entry.
208          */
209         struct list_head local_addr_list;
210         
211         /* Flag to indicate if addip is enabled. */
212         int addip_enable;
213
214         /* Flag to indicate if PR-SCTP is enabled. */
215         int prsctp_enable;
216 } sctp_globals;
217
218 #define sctp_rto_initial                (sctp_globals.rto_initial)
219 #define sctp_rto_min                    (sctp_globals.rto_min)
220 #define sctp_rto_max                    (sctp_globals.rto_max)
221 #define sctp_rto_alpha                  (sctp_globals.rto_alpha)
222 #define sctp_rto_beta                   (sctp_globals.rto_beta)
223 #define sctp_max_burst                  (sctp_globals.max_burst)
224 #define sctp_valid_cookie_life          (sctp_globals.valid_cookie_life)
225 #define sctp_cookie_preserve_enable     (sctp_globals.cookie_preserve_enable)
226 #define sctp_max_retrans_association    (sctp_globals.max_retrans_association)
227 #define sctp_sndbuf_policy              (sctp_globals.sndbuf_policy)
228 #define sctp_rcvbuf_policy              (sctp_globals.rcvbuf_policy)
229 #define sctp_max_retrans_path           (sctp_globals.max_retrans_path)
230 #define sctp_max_retrans_init           (sctp_globals.max_retrans_init)
231 #define sctp_sack_timeout               (sctp_globals.sack_timeout)
232 #define sctp_hb_interval                (sctp_globals.hb_interval)
233 #define sctp_max_instreams              (sctp_globals.max_instreams)
234 #define sctp_max_outstreams             (sctp_globals.max_outstreams)
235 #define sctp_address_families           (sctp_globals.address_families)
236 #define sctp_ep_hashsize                (sctp_globals.ep_hashsize)
237 #define sctp_ep_hashtable               (sctp_globals.ep_hashtable)
238 #define sctp_assoc_hashsize             (sctp_globals.assoc_hashsize)
239 #define sctp_assoc_hashtable            (sctp_globals.assoc_hashtable)
240 #define sctp_port_hashsize              (sctp_globals.port_hashsize)
241 #define sctp_port_rover                 (sctp_globals.port_rover)
242 #define sctp_port_alloc_lock            (sctp_globals.port_alloc_lock)
243 #define sctp_port_hashtable             (sctp_globals.port_hashtable)
244 #define sctp_local_addr_list            (sctp_globals.local_addr_list)
245 #define sctp_addip_enable               (sctp_globals.addip_enable)
246 #define sctp_prsctp_enable              (sctp_globals.prsctp_enable)
247
248 /* SCTP Socket type: UDP or TCP style. */
249 typedef enum {
250         SCTP_SOCKET_UDP = 0,
251         SCTP_SOCKET_UDP_HIGH_BANDWIDTH,
252         SCTP_SOCKET_TCP
253 } sctp_socket_type_t;
254
255 /* Per socket SCTP information. */
256 struct sctp_sock {
257         /* inet_sock has to be the first member of sctp_sock */
258         struct inet_sock inet;
259         /* What kind of a socket is this? */
260         sctp_socket_type_t type;
261
262         /* PF_ family specific functions.  */
263         struct sctp_pf *pf;
264
265         /* Access to HMAC transform. */
266         struct crypto_hash *hmac;
267
268         /* What is our base endpointer? */
269         struct sctp_endpoint *ep;
270
271         struct sctp_bind_bucket *bind_hash;
272         /* Various Socket Options.  */
273         __u16 default_stream;
274         __u32 default_ppid;
275         __u16 default_flags;
276         __u32 default_context;
277         __u32 default_timetolive;
278         __u32 default_rcv_context;
279         int max_burst;
280
281         /* Heartbeat interval: The endpoint sends out a Heartbeat chunk to
282          * the destination address every heartbeat interval. This value
283          * will be inherited by all new associations.
284          */
285         __u32 hbinterval;
286
287         /* This is the max_retrans value for new associations. */
288         __u16 pathmaxrxt;
289
290         /* The initial Path MTU to use for new associations. */
291         __u32 pathmtu;
292
293         /* The default SACK delay timeout for new associations. */
294         __u32 sackdelay;
295
296         /* Flags controling Heartbeat, SACK delay, and Path MTU Discovery. */
297         __u32 param_flags;
298
299         struct sctp_initmsg initmsg;
300         struct sctp_rtoinfo rtoinfo;
301         struct sctp_paddrparams paddrparam;
302         struct sctp_event_subscribe subscribe;
303         struct sctp_assocparams assocparams;
304         int user_frag;
305         __u32 autoclose;
306         __u8 nodelay;
307         __u8 disable_fragments;
308         __u8 v4mapped;
309         __u8 frag_interleave;
310         __u32 adaptation_ind;
311         __u32 pd_point;
312
313         atomic_t pd_mode;
314         /* Receive to here while partial delivery is in effect. */
315         struct sk_buff_head pd_lobby;
316 };
317
318 static inline struct sctp_sock *sctp_sk(const struct sock *sk)
319 {
320        return (struct sctp_sock *)sk;
321 }
322
323 static inline struct sock *sctp_opt2sk(const struct sctp_sock *sp)
324 {
325        return (struct sock *)sp;
326 }
327
328 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
329 struct sctp6_sock {
330        struct sctp_sock  sctp;
331        struct ipv6_pinfo inet6;
332 };
333 #endif /* CONFIG_IPV6 */
334
335
336 /* This is our APPLICATION-SPECIFIC state cookie.
337  * THIS IS NOT DICTATED BY THE SPECIFICATION.
338  */
339 /* These are the parts of an association which we send in the cookie.
340  * Most of these are straight out of:
341  * RFC2960 12.2 Parameters necessary per association (i.e. the TCB)
342  *
343  */
344
345 struct sctp_cookie {
346
347         /* My          : Tag expected in every inbound packet and sent
348          * Verification: in the INIT or INIT ACK chunk.
349          * Tag         :
350          */
351         __u32 my_vtag;
352
353         /* Peer's      : Tag expected in every outbound packet except
354          * Verification: in the INIT chunk.
355          * Tag         :
356          */
357         __u32 peer_vtag;
358
359         /* The rest of these are not from the spec, but really need to
360          * be in the cookie.
361          */
362
363         /* My Tie Tag  : Assist in discovering a restarting association. */
364         __u32 my_ttag;
365
366         /* Peer's Tie Tag: Assist in discovering a restarting association. */
367         __u32 peer_ttag;
368
369         /* When does this cookie expire? */
370         struct timeval expiration;
371
372         /* Number of inbound/outbound streams which are set
373          * and negotiated during the INIT process.
374          */
375         __u16 sinit_num_ostreams;
376         __u16 sinit_max_instreams;
377
378         /* This is the first sequence number I used.  */
379         __u32 initial_tsn;
380
381         /* This holds the originating address of the INIT packet.  */
382         union sctp_addr peer_addr;
383
384         /* IG Section 2.35.3 
385          * Include the source port of the INIT-ACK
386          */
387         __u16           my_port;
388
389         __u8 prsctp_capable;
390
391         /* Padding for future use */
392         __u8 padding;           
393
394         __u32 adaptation_ind;
395
396
397         /* This is a shim for my peer's INIT packet, followed by
398          * a copy of the raw address list of the association.
399          * The length of the raw address list is saved in the
400          * raw_addr_list_len field, which will be used at the time when
401          * the association TCB is re-constructed from the cookie.
402          */
403         __u32 raw_addr_list_len;
404         struct sctp_init_chunk peer_init[0];
405 };
406
407
408 /* The format of our cookie that we send to our peer. */
409 struct sctp_signed_cookie {
410         __u8 signature[SCTP_SECRET_SIZE];
411         __u32 __pad;            /* force sctp_cookie alignment to 64 bits */
412         struct sctp_cookie c;
413 } __attribute__((packed));
414
415 /* This is another convenience type to allocate memory for address
416  * params for the maximum size and pass such structures around
417  * internally.
418  */
419 union sctp_addr_param {
420         struct sctp_ipv4addr_param v4;
421         struct sctp_ipv6addr_param v6;
422 };
423
424 /* A convenience type to allow walking through the various
425  * parameters and avoid casting all over the place.
426  */
427 union sctp_params {
428         void *v;
429         struct sctp_paramhdr *p;
430         struct sctp_cookie_preserve_param *life;
431         struct sctp_hostname_param *dns;
432         struct sctp_cookie_param *cookie;
433         struct sctp_supported_addrs_param *sat;
434         struct sctp_ipv4addr_param *v4;
435         struct sctp_ipv6addr_param *v6;
436         union sctp_addr_param *addr;
437         struct sctp_adaptation_ind_param *aind;
438 };
439
440 /* RFC 2960.  Section 3.3.5 Heartbeat.
441  *    Heartbeat Information: variable length
442  *    The Sender-specific Heartbeat Info field should normally include
443  *    information about the sender's current time when this HEARTBEAT
444  *    chunk is sent and the destination transport address to which this
445  *    HEARTBEAT is sent (see Section 8.3).
446  */
447 typedef struct sctp_sender_hb_info {
448         struct sctp_paramhdr param_hdr;
449         union sctp_addr daddr;
450         unsigned long sent_at;
451         __u64 hb_nonce;
452 } __attribute__((packed)) sctp_sender_hb_info_t;
453
454 /*
455  *  RFC 2960 1.3.2 Sequenced Delivery within Streams
456  *
457  *  The term "stream" is used in SCTP to refer to a sequence of user
458  *  messages that are to be delivered to the upper-layer protocol in
459  *  order with respect to other messages within the same stream.  This is
460  *  in contrast to its usage in TCP, where it refers to a sequence of
461  *  bytes (in this document a byte is assumed to be eight bits).
462  *  ...
463  *
464  *  This is the structure we use to track both our outbound and inbound
465  *  SSN, or Stream Sequence Numbers.
466  */
467
468 struct sctp_stream {
469         __u16 *ssn;
470         unsigned int len;
471 };
472
473 struct sctp_ssnmap {
474         struct sctp_stream in;
475         struct sctp_stream out;
476         int malloced;
477 };
478
479 struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
480                                     gfp_t gfp);
481 void sctp_ssnmap_free(struct sctp_ssnmap *map);
482 void sctp_ssnmap_clear(struct sctp_ssnmap *map);
483
484 /* What is the current SSN number for this stream? */
485 static inline __u16 sctp_ssn_peek(struct sctp_stream *stream, __u16 id)
486 {
487         return stream->ssn[id];
488 }
489
490 /* Return the next SSN number for this stream.  */
491 static inline __u16 sctp_ssn_next(struct sctp_stream *stream, __u16 id)
492 {
493         return stream->ssn[id]++;
494 }
495
496 /* Skip over this ssn and all below. */
497 static inline void sctp_ssn_skip(struct sctp_stream *stream, __u16 id, 
498                                  __u16 ssn)
499 {
500         stream->ssn[id] = ssn+1;
501 }
502               
503 /*
504  * Pointers to address related SCTP functions.
505  * (i.e. things that depend on the address family.)
506  */
507 struct sctp_af {
508         int             (*sctp_xmit)    (struct sk_buff *skb,
509                                          struct sctp_transport *,
510                                          int ipfragok);
511         int             (*setsockopt)   (struct sock *sk,
512                                          int level,
513                                          int optname,
514                                          char __user *optval,
515                                          int optlen);
516         int             (*getsockopt)   (struct sock *sk,
517                                          int level,
518                                          int optname,
519                                          char __user *optval,
520                                          int __user *optlen);
521         int             (*compat_setsockopt)    (struct sock *sk,
522                                          int level,
523                                          int optname,
524                                          char __user *optval,
525                                          int optlen);
526         int             (*compat_getsockopt)    (struct sock *sk,
527                                          int level,
528                                          int optname,
529                                          char __user *optval,
530                                          int __user *optlen);
531         struct dst_entry *(*get_dst)    (struct sctp_association *asoc,
532                                          union sctp_addr *daddr,
533                                          union sctp_addr *saddr);
534         void            (*get_saddr)    (struct sctp_association *asoc,
535                                          struct dst_entry *dst,
536                                          union sctp_addr *daddr,
537                                          union sctp_addr *saddr);
538         void            (*copy_addrlist) (struct list_head *,
539                                           struct net_device *);
540         void            (*dst_saddr)    (union sctp_addr *saddr,
541                                          struct dst_entry *dst,
542                                          __be16 port);
543         int             (*cmp_addr)     (const union sctp_addr *addr1,
544                                          const union sctp_addr *addr2);
545         void            (*addr_copy)    (union sctp_addr *dst,
546                                          union sctp_addr *src);
547         void            (*from_skb)     (union sctp_addr *,
548                                          struct sk_buff *skb,
549                                          int saddr);
550         void            (*from_sk)      (union sctp_addr *,
551                                          struct sock *sk);
552         void            (*to_sk_saddr)  (union sctp_addr *,
553                                          struct sock *sk);
554         void            (*to_sk_daddr)  (union sctp_addr *,
555                                          struct sock *sk);
556         void            (*from_addr_param) (union sctp_addr *,
557                                             union sctp_addr_param *,
558                                             __be16 port, int iif);
559         int             (*to_addr_param) (const union sctp_addr *,
560                                           union sctp_addr_param *); 
561         int             (*addr_valid)   (union sctp_addr *,
562                                          struct sctp_sock *,
563                                          const struct sk_buff *);
564         sctp_scope_t    (*scope) (union sctp_addr *);
565         void            (*inaddr_any)   (union sctp_addr *, __be16);
566         int             (*is_any)       (const union sctp_addr *);
567         int             (*available)    (union sctp_addr *,
568                                          struct sctp_sock *);
569         int             (*skb_iif)      (const struct sk_buff *sk);
570         int             (*is_ce)        (const struct sk_buff *sk);
571         void            (*seq_dump_addr)(struct seq_file *seq,
572                                          union sctp_addr *addr);
573         __u16           net_header_len;
574         int             sockaddr_len;
575         sa_family_t     sa_family;
576         struct list_head list;
577 };
578
579 struct sctp_af *sctp_get_af_specific(sa_family_t);
580 int sctp_register_af(struct sctp_af *);
581
582 /* Protocol family functions. */
583 struct sctp_pf {
584         void (*event_msgname)(struct sctp_ulpevent *, char *, int *);
585         void (*skb_msgname)  (struct sk_buff *, char *, int *);
586         int  (*af_supported) (sa_family_t, struct sctp_sock *);
587         int  (*cmp_addr) (const union sctp_addr *,
588                           const union sctp_addr *,
589                           struct sctp_sock *);
590         int  (*bind_verify) (struct sctp_sock *, union sctp_addr *);
591         int  (*send_verify) (struct sctp_sock *, union sctp_addr *);
592         int  (*supported_addrs)(const struct sctp_sock *, __be16 *);
593         struct sock *(*create_accept_sk) (struct sock *sk,
594                                           struct sctp_association *asoc);
595         void (*addr_v4map) (struct sctp_sock *, union sctp_addr *);
596         struct sctp_af *af;
597 };
598
599
600 /* Structure to track chunk fragments that have been acked, but peer
601  * fragments of the same message have not.
602  */
603 struct sctp_datamsg {
604         /* Chunks waiting to be submitted to lower layer. */
605         struct list_head chunks;
606         /* Chunks that have been transmitted. */
607         struct list_head track;
608         /* Reference counting. */
609         atomic_t refcnt;
610         /* When is this message no longer interesting to the peer? */
611         unsigned long expires_at;
612         /* Did the messenge fail to send? */
613         int send_error;
614         char send_failed;
615         /* Control whether chunks from this message can be abandoned. */
616         char can_abandon;
617 };
618
619 struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *,
620                                             struct sctp_sndrcvinfo *,
621                                             struct msghdr *, int len);
622 void sctp_datamsg_put(struct sctp_datamsg *);
623 void sctp_datamsg_free(struct sctp_datamsg *);
624 void sctp_datamsg_track(struct sctp_chunk *);
625 void sctp_chunk_fail(struct sctp_chunk *, int error);
626 int sctp_chunk_abandoned(struct sctp_chunk *);
627
628 /* RFC2960 1.4 Key Terms
629  *
630  * o Chunk: A unit of information within an SCTP packet, consisting of
631  * a chunk header and chunk-specific content.
632  *
633  * As a matter of convenience, we remember the SCTP common header for
634  * each chunk as well as a few other header pointers...
635  */
636 struct sctp_chunk {
637         struct list_head list;
638
639         atomic_t refcnt;
640
641         /* This is our link to the per-transport transmitted list.  */
642         struct list_head transmitted_list;
643
644         /* This field is used by chunks that hold fragmented data.
645          * For the first fragment this is the list that holds the rest of
646          * fragments. For the remaining fragments, this is the link to the
647          * frag_list maintained in the first fragment.
648          */
649         struct list_head frag_list;
650
651         /* This points to the sk_buff containing the actual data.  */
652         struct sk_buff *skb;
653
654         /* These are the SCTP headers by reverse order in a packet.
655          * Note that some of these may happen more than once.  In that
656          * case, we point at the "current" one, whatever that means
657          * for that level of header.
658          */
659
660         /* We point this at the FIRST TLV parameter to chunk_hdr.  */
661         union sctp_params param_hdr;
662         union {
663                 __u8 *v;
664                 struct sctp_datahdr *data_hdr;
665                 struct sctp_inithdr *init_hdr;
666                 struct sctp_sackhdr *sack_hdr;
667                 struct sctp_heartbeathdr *hb_hdr;
668                 struct sctp_sender_hb_info *hbs_hdr;
669                 struct sctp_shutdownhdr *shutdown_hdr;
670                 struct sctp_signed_cookie *cookie_hdr;
671                 struct sctp_ecnehdr *ecne_hdr;
672                 struct sctp_cwrhdr *ecn_cwr_hdr;
673                 struct sctp_errhdr *err_hdr;
674                 struct sctp_addiphdr *addip_hdr;
675                 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
676         } subh;
677
678         __u8 *chunk_end;
679
680         struct sctp_chunkhdr *chunk_hdr;
681         struct sctphdr *sctp_hdr;
682
683         /* This needs to be recoverable for SCTP_SEND_FAILED events. */
684         struct sctp_sndrcvinfo sinfo;
685
686         /* Which association does this belong to?  */
687         struct sctp_association *asoc;
688
689         /* What endpoint received this chunk? */
690         struct sctp_ep_common *rcvr;
691
692         /* We fill this in if we are calculating RTT. */
693         unsigned long sent_at;
694
695         /* What is the origin IP address for this chunk?  */
696         union sctp_addr source;
697         /* Destination address for this chunk. */
698         union sctp_addr dest;
699
700         /* For outbound message, track all fragments for SEND_FAILED. */
701         struct sctp_datamsg *msg;
702
703         /* For an inbound chunk, this tells us where it came from.
704          * For an outbound chunk, it tells us where we'd like it to
705          * go.  It is NULL if we have no preference.
706          */
707         struct sctp_transport *transport;
708
709         __u8 rtt_in_progress;   /* Is this chunk used for RTT calculation? */
710         __u8 resent;            /* Has this chunk ever been retransmitted. */
711         __u8 has_tsn;           /* Does this chunk have a TSN yet? */
712         __u8 has_ssn;           /* Does this chunk have a SSN yet? */
713         __u8 singleton;         /* Was this the only chunk in the packet? */
714         __u8 end_of_packet;     /* Was this the last chunk in the packet? */
715         __u8 ecn_ce_done;       /* Have we processed the ECN CE bit? */
716         __u8 pdiscard;          /* Discard the whole packet now? */
717         __u8 tsn_gap_acked;     /* Is this chunk acked by a GAP ACK? */
718         __s8 fast_retransmit;    /* Is this chunk fast retransmitted? */
719         __u8 tsn_missing_report; /* Data chunk missing counter. */
720         __u8 data_accepted;     /* At least 1 chunk in this packet accepted */
721 };
722
723 void sctp_chunk_hold(struct sctp_chunk *);
724 void sctp_chunk_put(struct sctp_chunk *);
725 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
726                           struct iovec *data);
727 void sctp_chunk_free(struct sctp_chunk *);
728 void  *sctp_addto_chunk(struct sctp_chunk *, int len, const void *data);
729 void  *sctp_addto_param(struct sctp_chunk *, int len, const void *data);
730 struct sctp_chunk *sctp_chunkify(struct sk_buff *,
731                                  const struct sctp_association *,
732                                  struct sock *);
733 void sctp_init_addrs(struct sctp_chunk *, union sctp_addr *,
734                      union sctp_addr *);
735 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk);
736
737 /* This is a structure for holding either an IPv6 or an IPv4 address.  */
738 struct sctp_sockaddr_entry {
739         struct list_head list;
740         union sctp_addr a;
741         __u8 use_as_src;
742 };
743
744 typedef struct sctp_chunk *(sctp_packet_phandler_t)(struct sctp_association *);
745
746 /* This structure holds lists of chunks as we are assembling for
747  * transmission.
748  */
749 struct sctp_packet {
750         /* These are the SCTP header values (host order) for the packet. */
751         __u16 source_port;
752         __u16 destination_port;
753         __u32 vtag;
754
755         /* This contains the payload chunks.  */
756         struct list_head chunk_list;
757
758         /* This is the overhead of the sctp and ip headers. */
759         size_t overhead;
760         /* This is the total size of all chunks INCLUDING padding.  */
761         size_t size;
762
763         /* The packet is destined for this transport address.
764          * The function we finally use to pass down to the next lower
765          * layer lives in the transport structure.
766          */
767         struct sctp_transport *transport;
768
769         /* This packet contains a COOKIE-ECHO chunk. */
770         char has_cookie_echo;
771
772         /* This packet containsa SACK chunk. */
773         char has_sack;
774
775         /* SCTP cannot fragment this packet. So let ip fragment it. */
776         char ipfragok;
777
778         int malloced;
779 };
780
781 struct sctp_packet *sctp_packet_init(struct sctp_packet *,
782                                      struct sctp_transport *,
783                                      __u16 sport, __u16 dport);
784 struct sctp_packet *sctp_packet_config(struct sctp_packet *, __u32 vtag, int);
785 sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *,
786                                        struct sctp_chunk *);
787 sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *,
788                                      struct sctp_chunk *);
789 int sctp_packet_transmit(struct sctp_packet *);
790 void sctp_packet_free(struct sctp_packet *);
791
792 static inline int sctp_packet_empty(struct sctp_packet *packet)
793 {
794         return (packet->size == packet->overhead);
795 }
796
797 /* This represents a remote transport address.
798  * For local transport addresses, we just use union sctp_addr.
799  *
800  * RFC2960 Section 1.4 Key Terms
801  *
802  *   o  Transport address:  A Transport Address is traditionally defined
803  *      by Network Layer address, Transport Layer protocol and Transport
804  *      Layer port number.  In the case of SCTP running over IP, a
805  *      transport address is defined by the combination of an IP address
806  *      and an SCTP port number (where SCTP is the Transport protocol).
807  *
808  * RFC2960 Section 7.1 SCTP Differences from TCP Congestion control
809  *
810  *   o  The sender keeps a separate congestion control parameter set for
811  *      each of the destination addresses it can send to (not each
812  *      source-destination pair but for each destination).  The parameters
813  *      should decay if the address is not used for a long enough time
814  *      period.
815  *
816  */
817 struct sctp_transport {
818         /* A list of transports. */
819         struct list_head transports;
820
821         /* Reference counting. */
822         atomic_t refcnt;
823         int      dead;
824
825         /* This is the peer's IP address and port. */
826         union sctp_addr ipaddr;
827
828         /* These are the functions we call to handle LLP stuff.  */
829         struct sctp_af *af_specific;
830
831         /* Which association do we belong to?  */
832         struct sctp_association *asoc;
833
834         /* RFC2960
835          *
836          * 12.3 Per Transport Address Data
837          *
838          * For each destination transport address in the peer's
839          * address list derived from the INIT or INIT ACK chunk, a
840          * number of data elements needs to be maintained including:
841          */
842         __u32 rtt;              /* This is the most recent RTT.  */
843
844         /* RTO         : The current retransmission timeout value.  */
845         unsigned long rto;
846
847         /* RTTVAR      : The current RTT variation.  */
848         __u32 rttvar;
849
850         /* SRTT        : The current smoothed round trip time.  */
851         __u32 srtt;
852
853         /* RTO-Pending : A flag used to track if one of the DATA
854          *              chunks sent to this address is currently being
855          *              used to compute a RTT. If this flag is 0,
856          *              the next DATA chunk sent to this destination
857          *              should be used to compute a RTT and this flag
858          *              should be set. Every time the RTT
859          *              calculation completes (i.e. the DATA chunk
860          *              is SACK'd) clear this flag.
861          */
862         int rto_pending;
863
864         /*
865          * These are the congestion stats.
866          */
867         /* cwnd        : The current congestion window.  */
868         __u32 cwnd;               /* This is the actual cwnd.  */
869
870         /* ssthresh    : The current slow start threshold value.  */
871         __u32 ssthresh;
872
873         /* partial     : The tracking method for increase of cwnd when in
874          * bytes acked : congestion avoidance mode (see Section 6.2.2)
875          */
876         __u32 partial_bytes_acked;
877
878         /* Data that has been sent, but not acknowledged. */
879         __u32 flight_size;
880
881         /* Destination */
882         struct dst_entry *dst;
883         /* Source address. */
884         union sctp_addr saddr;
885
886         /* When was the last time(in jiffies) that a data packet was sent on
887          * this transport?  This is used to adjust the cwnd when the transport
888          * becomes inactive.
889          */
890         unsigned long last_time_used;
891
892         /* Heartbeat interval: The endpoint sends out a Heartbeat chunk to
893          * the destination address every heartbeat interval.
894          */
895         unsigned long hbinterval;
896
897         /* SACK delay timeout */
898         unsigned long sackdelay;
899
900         /* When was the last time (in jiffies) that we heard from this
901          * transport?  We use this to pick new active and retran paths.
902          */
903         unsigned long last_time_heard;
904
905         /* Last time(in jiffies) when cwnd is reduced due to the congestion
906          * indication based on ECNE chunk.
907          */
908         unsigned long last_time_ecne_reduced;
909
910         /* This is the max_retrans value for the transport and will
911          * be initialized from the assocs value.  This can be changed
912          * using SCTP_SET_PEER_ADDR_PARAMS socket option.
913          */
914         __u16 pathmaxrxt;
915
916         /* is the Path MTU update pending on this tranport */
917         __u8 pmtu_pending;
918
919         /* PMTU       : The current known path MTU.  */
920         __u32 pathmtu;
921
922         /* Flags controling Heartbeat, SACK delay, and Path MTU Discovery. */
923         __u32 param_flags;
924
925         /* The number of times INIT has been sent on this transport. */
926         int init_sent_count;
927
928         /* state       : The current state of this destination,
929          *             : i.e. SCTP_ACTIVE, SCTP_INACTIVE, SCTP_UNKOWN.
930          */
931         int state;
932
933         /* These are the error stats for this destination.  */
934
935         /* Error count : The current error count for this destination.  */
936         unsigned short error_count;
937
938         /* Per         : A timer used by each destination.
939          * Destination :
940          * Timer       :
941          *
942          * [Everywhere else in the text this is called T3-rtx. -ed]
943          */
944         struct timer_list T3_rtx_timer;
945
946         /* Heartbeat timer is per destination. */
947         struct timer_list hb_timer;
948
949         /* Since we're using per-destination retransmission timers
950          * (see above), we're also using per-destination "transmitted"
951          * queues.  This probably ought to be a private struct
952          * accessible only within the outqueue, but it's not, yet.
953          */
954         struct list_head transmitted;
955
956         /* We build bundle-able packets for this transport here.  */
957         struct sctp_packet packet;
958
959         /* This is the list of transports that have chunks to send.  */
960         struct list_head send_ready;
961
962         int malloced; /* Is this structure kfree()able? */
963
964         /* State information saved for SFR_CACC algorithm. The key
965          * idea in SFR_CACC is to maintain state at the sender on a
966          * per-destination basis when a changeover happens.
967          *      char changeover_active;
968          *      char cycling_changeover;
969          *      __u32 next_tsn_at_change;
970          *      char cacc_saw_newack;
971          */
972         struct {
973                 /* An unsigned integer, which stores the next TSN to be
974                  * used by the sender, at the moment of changeover.
975                  */
976                 __u32 next_tsn_at_change;
977
978                 /* A flag which indicates the occurrence of a changeover */
979                 char changeover_active;
980
981                 /* A flag which indicates whether the change of primary is
982                  * the first switch to this destination address during an
983                  * active switch.
984                  */
985                 char cycling_changeover;
986
987                 /* A temporary flag, which is used during the processing of
988                  * a SACK to estimate the causative TSN(s)'s group.
989                  */
990                 char cacc_saw_newack;
991         } cacc;
992
993         /* 64-bit random number sent with heartbeat. */
994         __u64 hb_nonce;
995 };
996
997 struct sctp_transport *sctp_transport_new(const union sctp_addr *,
998                                           gfp_t);
999 void sctp_transport_set_owner(struct sctp_transport *,
1000                               struct sctp_association *);
1001 void sctp_transport_route(struct sctp_transport *, union sctp_addr *,
1002                           struct sctp_sock *);
1003 void sctp_transport_pmtu(struct sctp_transport *);
1004 void sctp_transport_free(struct sctp_transport *);
1005 void sctp_transport_reset_timers(struct sctp_transport *);
1006 void sctp_transport_hold(struct sctp_transport *);
1007 void sctp_transport_put(struct sctp_transport *);
1008 void sctp_transport_update_rto(struct sctp_transport *, __u32);
1009 void sctp_transport_raise_cwnd(struct sctp_transport *, __u32, __u32);
1010 void sctp_transport_lower_cwnd(struct sctp_transport *, sctp_lower_cwnd_t);
1011 unsigned long sctp_transport_timeout(struct sctp_transport *);
1012 void sctp_transport_reset(struct sctp_transport *);
1013 void sctp_transport_update_pmtu(struct sctp_transport *, u32);
1014
1015
1016 /* This is the structure we use to queue packets as they come into
1017  * SCTP.  We write packets to it and read chunks from it.
1018  */
1019 struct sctp_inq {
1020         /* This is actually a queue of sctp_chunk each
1021          * containing a partially decoded packet.
1022          */
1023         struct list_head in_chunk_list;
1024         /* This is the packet which is currently off the in queue and is
1025          * being worked on through the inbound chunk processing.
1026          */
1027         struct sctp_chunk *in_progress;
1028
1029         /* This is the delayed task to finish delivering inbound
1030          * messages.
1031          */
1032         struct work_struct immediate;
1033
1034         int malloced;        /* Is this structure kfree()able?  */
1035 };
1036
1037 void sctp_inq_init(struct sctp_inq *);
1038 void sctp_inq_free(struct sctp_inq *);
1039 void sctp_inq_push(struct sctp_inq *, struct sctp_chunk *packet);
1040 struct sctp_chunk *sctp_inq_pop(struct sctp_inq *);
1041 void sctp_inq_set_th_handler(struct sctp_inq *, work_func_t);
1042
1043 /* This is the structure we use to hold outbound chunks.  You push
1044  * chunks in and they automatically pop out the other end as bundled
1045  * packets (it calls (*output_handler)()).
1046  *
1047  * This structure covers sections 6.3, 6.4, 6.7, 6.8, 6.10, 7., 8.1,
1048  * and 8.2 of the v13 draft.
1049  *
1050  * It handles retransmissions.  The connection to the timeout portion
1051  * of the state machine is through sctp_..._timeout() and timeout_handler.
1052  *
1053  * If you feed it SACKs, it will eat them.
1054  *
1055  * If you give it big chunks, it will fragment them.
1056  *
1057  * It assigns TSN's to data chunks.  This happens at the last possible
1058  * instant before transmission.
1059  *
1060  * When free()'d, it empties itself out via output_handler().
1061  */
1062 struct sctp_outq {
1063         struct sctp_association *asoc;
1064
1065         /* Data pending that has never been transmitted.  */
1066         struct list_head out_chunk_list;
1067
1068         unsigned out_qlen;      /* Total length of queued data chunks. */
1069
1070         /* Error of send failed, may used in SCTP_SEND_FAILED event. */
1071         unsigned error;
1072
1073         /* These are control chunks we want to send.  */
1074         struct list_head control_chunk_list;
1075
1076         /* These are chunks that have been sacked but are above the
1077          * CTSN, or cumulative tsn ack point.
1078          */
1079         struct list_head sacked;
1080
1081         /* Put chunks on this list to schedule them for
1082          * retransmission.
1083          */
1084         struct list_head retransmit;
1085
1086         /* Put chunks on this list to save them for FWD TSN processing as
1087          * they were abandoned.
1088          */
1089         struct list_head abandoned;
1090
1091         /* How many unackd bytes do we have in-flight?  */
1092         __u32 outstanding_bytes;
1093
1094         /* Corked? */
1095         char cork;
1096
1097         /* Is this structure empty?  */
1098         char empty;
1099
1100         /* Are we kfree()able? */
1101         char malloced;
1102 };
1103
1104 void sctp_outq_init(struct sctp_association *, struct sctp_outq *);
1105 void sctp_outq_teardown(struct sctp_outq *);
1106 void sctp_outq_free(struct sctp_outq*);
1107 int sctp_outq_tail(struct sctp_outq *, struct sctp_chunk *chunk);
1108 int sctp_outq_flush(struct sctp_outq *, int);
1109 int sctp_outq_sack(struct sctp_outq *, struct sctp_sackhdr *);
1110 int sctp_outq_is_empty(const struct sctp_outq *);
1111 void sctp_outq_restart(struct sctp_outq *);
1112
1113 void sctp_retransmit(struct sctp_outq *, struct sctp_transport *,
1114                      sctp_retransmit_reason_t);
1115 void sctp_retransmit_mark(struct sctp_outq *, struct sctp_transport *, __u8);
1116 int sctp_outq_uncork(struct sctp_outq *);
1117 /* Uncork and flush an outqueue.  */
1118 static inline void sctp_outq_cork(struct sctp_outq *q)
1119 {
1120         q->cork = 1;
1121 }
1122
1123 /* These bind address data fields common between endpoints and associations */
1124 struct sctp_bind_addr {
1125
1126         /* RFC 2960 12.1 Parameters necessary for the SCTP instance
1127          *
1128          * SCTP Port:   The local SCTP port number the endpoint is
1129          *              bound to.
1130          */
1131         __u16 port;
1132
1133         /* RFC 2960 12.1 Parameters necessary for the SCTP instance
1134          *
1135          * Address List: The list of IP addresses that this instance
1136          *      has bound.  This information is passed to one's
1137          *      peer(s) in INIT and INIT ACK chunks.
1138          */
1139         struct list_head address_list;
1140
1141         int malloced;        /* Are we kfree()able?  */
1142 };
1143
1144 void sctp_bind_addr_init(struct sctp_bind_addr *, __u16 port);
1145 void sctp_bind_addr_free(struct sctp_bind_addr *);
1146 int sctp_bind_addr_copy(struct sctp_bind_addr *dest,
1147                         const struct sctp_bind_addr *src,
1148                         sctp_scope_t scope, gfp_t gfp,
1149                         int flags);
1150 int sctp_add_bind_addr(struct sctp_bind_addr *, union sctp_addr *,
1151                        __u8 use_as_src, gfp_t gfp);
1152 int sctp_del_bind_addr(struct sctp_bind_addr *, union sctp_addr *);
1153 int sctp_bind_addr_match(struct sctp_bind_addr *, const union sctp_addr *,
1154                          struct sctp_sock *);
1155 union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr   *bp,
1156                                         const union sctp_addr   *addrs,
1157                                         int                     addrcnt,
1158                                         struct sctp_sock        *opt);
1159 union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
1160                                          int *addrs_len,
1161                                          gfp_t gfp);
1162 int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw, int len,
1163                            __u16 port, gfp_t gfp);
1164
1165 sctp_scope_t sctp_scope(const union sctp_addr *);
1166 int sctp_in_scope(const union sctp_addr *addr, const sctp_scope_t scope);
1167 int sctp_is_any(const union sctp_addr *addr);
1168 int sctp_addr_is_valid(const union sctp_addr *addr);
1169
1170
1171 /* What type of endpoint?  */
1172 typedef enum {
1173         SCTP_EP_TYPE_SOCKET,
1174         SCTP_EP_TYPE_ASSOCIATION,
1175 } sctp_endpoint_type_t;
1176
1177 /*
1178  * A common base class to bridge the implmentation view of a
1179  * socket (usually listening) endpoint versus an association's
1180  * local endpoint.
1181  * This common structure is useful for several purposes:
1182  *   1) Common interface for lookup routines.
1183  *      a) Subfunctions work for either endpoint or association
1184  *      b) Single interface to lookup allows hiding the lookup lock rather
1185  *         than acquiring it externally.
1186  *   2) Common interface for the inbound chunk handling/state machine.
1187  *   3) Common object handling routines for reference counting, etc.
1188  *   4) Disentangle association lookup from endpoint lookup, where we
1189  *      do not have to find our endpoint to find our association.
1190  *
1191  */
1192
1193 struct sctp_ep_common {
1194         /* Fields to help us manage our entries in the hash tables. */
1195         struct sctp_ep_common *next;
1196         struct sctp_ep_common **pprev;
1197         int hashent;
1198
1199         /* Runtime type information.  What kind of endpoint is this? */
1200         sctp_endpoint_type_t type;
1201
1202         /* Some fields to help us manage this object.
1203          *   refcnt   - Reference count access to this object.
1204          *   dead     - Do not attempt to use this object.
1205          *   malloced - Do we need to kfree this object?
1206          */
1207         atomic_t    refcnt;
1208         char        dead;
1209         char        malloced;
1210
1211         /* What socket does this endpoint belong to?  */
1212         struct sock *sk;
1213
1214         /* This is where we receive inbound chunks.  */
1215         struct sctp_inq   inqueue;
1216
1217         /* This substructure includes the defining parameters of the
1218          * endpoint:
1219          * bind_addr.port is our shared port number.
1220          * bind_addr.address_list is our set of local IP addresses.
1221          */
1222         struct sctp_bind_addr bind_addr;
1223
1224         /* Protection during address list comparisons. */
1225         rwlock_t   addr_lock;
1226 };
1227
1228
1229 /* RFC Section 1.4 Key Terms
1230  *
1231  * o SCTP endpoint: The logical sender/receiver of SCTP packets. On a
1232  *   multi-homed host, an SCTP endpoint is represented to its peers as a
1233  *   combination of a set of eligible destination transport addresses to
1234  *   which SCTP packets can be sent and a set of eligible source
1235  *   transport addresses from which SCTP packets can be received.
1236  *   All transport addresses used by an SCTP endpoint must use the
1237  *   same port number, but can use multiple IP addresses. A transport
1238  *   address used by an SCTP endpoint must not be used by another
1239  *   SCTP endpoint. In other words, a transport address is unique
1240  *   to an SCTP endpoint.
1241  *
1242  * From an implementation perspective, each socket has one of these.
1243  * A TCP-style socket will have exactly one association on one of
1244  * these.  An UDP-style socket will have multiple associations hanging
1245  * off one of these.
1246  */
1247
1248 struct sctp_endpoint {
1249         /* Common substructure for endpoint and association. */
1250         struct sctp_ep_common base;
1251
1252         /* Associations: A list of current associations and mappings
1253          *            to the data consumers for each association. This
1254          *            may be in the form of a hash table or other
1255          *            implementation dependent structure. The data
1256          *            consumers may be process identification
1257          *            information such as file descriptors, named pipe
1258          *            pointer, or table pointers dependent on how SCTP
1259          *            is implemented.
1260          */
1261         /* This is really a list of struct sctp_association entries. */
1262         struct list_head asocs;
1263
1264         /* Secret Key: A secret key used by this endpoint to compute
1265          *            the MAC.  This SHOULD be a cryptographic quality
1266          *            random number with a sufficient length.
1267          *            Discussion in [RFC1750] can be helpful in
1268          *            selection of the key.
1269          */
1270         __u8 secret_key[SCTP_HOW_MANY_SECRETS][SCTP_SECRET_SIZE];
1271         int current_key;
1272         int last_key;
1273         int key_changed_at;
1274
1275         /* digest:  This is a digest of the sctp cookie.  This field is
1276          *          only used on the receive path when we try to validate
1277          *          that the cookie has not been tampered with.  We put
1278          *          this here so we pre-allocate this once and can re-use
1279          *          on every receive.
1280          */
1281         __u8 *digest;
1282  
1283         /* sendbuf acct. policy.        */
1284         __u32 sndbuf_policy;
1285
1286         /* rcvbuf acct. policy. */
1287         __u32 rcvbuf_policy;
1288 };
1289
1290 /* Recover the outter endpoint structure. */
1291 static inline struct sctp_endpoint *sctp_ep(struct sctp_ep_common *base)
1292 {
1293         struct sctp_endpoint *ep;
1294
1295         ep = container_of(base, struct sctp_endpoint, base);
1296         return ep;
1297 }
1298
1299 /* These are function signatures for manipulating endpoints.  */
1300 struct sctp_endpoint *sctp_endpoint_new(struct sock *, gfp_t);
1301 void sctp_endpoint_free(struct sctp_endpoint *);
1302 void sctp_endpoint_put(struct sctp_endpoint *);
1303 void sctp_endpoint_hold(struct sctp_endpoint *);
1304 void sctp_endpoint_add_asoc(struct sctp_endpoint *, struct sctp_association *);
1305 struct sctp_association *sctp_endpoint_lookup_assoc(
1306         const struct sctp_endpoint *ep,
1307         const union sctp_addr *paddr,
1308         struct sctp_transport **);
1309 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *,
1310                                 const union sctp_addr *);
1311 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *,
1312                                         const union sctp_addr *);
1313 int sctp_has_association(const union sctp_addr *laddr,
1314                          const union sctp_addr *paddr);
1315
1316 int sctp_verify_init(const struct sctp_association *asoc, sctp_cid_t,
1317                      sctp_init_chunk_t *peer_init, struct sctp_chunk *chunk,
1318                      struct sctp_chunk **err_chunk);
1319 int sctp_process_init(struct sctp_association *, sctp_cid_t cid,
1320                       const union sctp_addr *peer,
1321                       sctp_init_chunk_t *init, gfp_t gfp);
1322 __u32 sctp_generate_tag(const struct sctp_endpoint *);
1323 __u32 sctp_generate_tsn(const struct sctp_endpoint *);
1324
1325 struct sctp_inithdr_host {
1326         __u32 init_tag;
1327         __u32 a_rwnd;
1328         __u16 num_outbound_streams;
1329         __u16 num_inbound_streams;
1330         __u32 initial_tsn;
1331 };
1332
1333 /* RFC2960
1334  *
1335  * 12. Recommended Transmission Control Block (TCB) Parameters
1336  *
1337  * This section details a recommended set of parameters that should
1338  * be contained within the TCB for an implementation. This section is
1339  * for illustrative purposes and should not be deemed as requirements
1340  * on an implementation or as an exhaustive list of all parameters
1341  * inside an SCTP TCB. Each implementation may need its own additional
1342  * parameters for optimization.
1343  */
1344
1345
1346 /* Here we have information about each individual association. */
1347 struct sctp_association {
1348
1349         /* A base structure common to endpoint and association.
1350          * In this context, it represents the associations's view
1351          * of the local endpoint of the association.
1352          */
1353         struct sctp_ep_common base;
1354
1355         /* Associations on the same socket. */
1356         struct list_head asocs;
1357
1358         /* association id. */
1359         sctp_assoc_t assoc_id;
1360
1361         /* This is our parent endpoint.  */
1362         struct sctp_endpoint *ep;
1363
1364         /* These are those association elements needed in the cookie.  */
1365         struct sctp_cookie c;
1366
1367         /* This is all information about our peer.  */
1368         struct {
1369                 /* rwnd
1370                  *
1371                  * Peer Rwnd   : Current calculated value of the peer's rwnd.
1372                  */
1373                 __u32 rwnd;
1374
1375                 /* transport_addr_list
1376                  *
1377                  * Peer        : A list of SCTP transport addresses that the
1378                  * Transport   : peer is bound to. This information is derived
1379                  * Address     : from the INIT or INIT ACK and is used to
1380                  * List        : associate an inbound packet with a given
1381                  *             : association. Normally this information is
1382                  *             : hashed or keyed for quick lookup and access
1383                  *             : of the TCB.
1384                  *             : The list is also initialized with the list
1385                  *             : of addresses passed with the sctp_connectx()
1386                  *             : call.
1387                  *
1388                  * It is a list of SCTP_transport's.
1389                  */
1390                 struct list_head transport_addr_list;
1391
1392                 /* transport_count
1393                  *
1394                  * Peer        : A count of the number of peer addresses
1395                  * Transport   : in the Peer Transport Address List.
1396                  * Address     :
1397                  * Count       :
1398                  */
1399                 __u16 transport_count;
1400
1401                 /* port
1402                  *   The transport layer port number.
1403                  */
1404                 __u16 port;
1405
1406                 /* primary_path
1407                  *
1408                  * Primary     : This is the current primary destination
1409                  * Path        : transport address of the peer endpoint.  It
1410                  *             : may also specify a source transport address
1411                  *             : on this endpoint.
1412                  *
1413                  * All of these paths live on transport_addr_list.
1414                  *
1415                  * At the bakeoffs, we discovered that the intent of
1416                  * primaryPath is that it only changes when the ULP
1417                  * asks to have it changed.  We add the activePath to
1418                  * designate the connection we are currently using to
1419                  * transmit new data and most control chunks.
1420                  */
1421                 struct sctp_transport *primary_path;
1422
1423                 /* Cache the primary path address here, when we
1424                  * need a an address for msg_name.
1425                  */
1426                 union sctp_addr primary_addr;
1427
1428                 /* active_path
1429                  *   The path that we are currently using to
1430                  *   transmit new data and most control chunks.
1431                  */
1432                 struct sctp_transport *active_path;
1433
1434                 /* retran_path
1435                  *
1436                  * RFC2960 6.4 Multi-homed SCTP Endpoints
1437                  * ...
1438                  * Furthermore, when its peer is multi-homed, an
1439                  * endpoint SHOULD try to retransmit a chunk to an
1440                  * active destination transport address that is
1441                  * different from the last destination address to
1442                  * which the DATA chunk was sent.
1443                  */
1444                 struct sctp_transport *retran_path;
1445
1446                 /* Pointer to last transport I have sent on.  */
1447                 struct sctp_transport *last_sent_to;
1448
1449                 /* This is the last transport I have received DATA on.  */
1450                 struct sctp_transport *last_data_from;
1451
1452                 /*
1453                  * Mapping  An array of bits or bytes indicating which out of
1454                  * Array    order TSN's have been received (relative to the
1455                  *          Last Rcvd TSN). If no gaps exist, i.e. no out of
1456                  *          order packets have been received, this array
1457                  *          will be set to all zero. This structure may be
1458                  *          in the form of a circular buffer or bit array.
1459                  *
1460                  * Last Rcvd   : This is the last TSN received in
1461                  * TSN         : sequence. This value is set initially by
1462                  *             : taking the peer's Initial TSN, received in
1463                  *             : the INIT or INIT ACK chunk, and subtracting
1464                  *             : one from it.
1465                  *
1466                  * Throughout most of the specification this is called the
1467                  * "Cumulative TSN ACK Point".  In this case, we
1468                  * ignore the advice in 12.2 in favour of the term
1469                  * used in the bulk of the text.  This value is hidden
1470                  * in tsn_map--we get it by calling sctp_tsnmap_get_ctsn().
1471                  */
1472                 struct sctp_tsnmap tsn_map;
1473                 __u8 _map[sctp_tsnmap_storage_size(SCTP_TSN_MAP_SIZE)];
1474
1475                 /* Ack State   : This flag indicates if the next received
1476                  *             : packet is to be responded to with a
1477                  *             : SACK. This is initializedto 0.  When a packet
1478                  *             : is received it is incremented. If this value
1479                  *             : reaches 2 or more, a SACK is sent and the
1480                  *             : value is reset to 0. Note: This is used only
1481                  *             : when no DATA chunks are received out of
1482                  *             : order.  When DATA chunks are out of order,
1483                  *             : SACK's are not delayed (see Section 6).
1484                  */
1485                 __u8    sack_needed;     /* Do we need to sack the peer? */
1486
1487                 /* These are capabilities which our peer advertised.  */
1488                 __u8    ecn_capable;     /* Can peer do ECN? */
1489                 __u8    ipv4_address;    /* Peer understands IPv4 addresses? */
1490                 __u8    ipv6_address;    /* Peer understands IPv6 addresses? */
1491                 __u8    hostname_address;/* Peer understands DNS addresses? */
1492                 __u8    asconf_capable;  /* Does peer support ADDIP? */
1493                 __u8    prsctp_capable;  /* Can peer do PR-SCTP? */
1494
1495                 __u32   adaptation_ind;  /* Adaptation Code point. */
1496
1497                 /* This mask is used to disable sending the ASCONF chunk
1498                  * with specified parameter to peer.
1499                  */
1500                 __be16 addip_disabled_mask;
1501
1502                 struct sctp_inithdr_host i;
1503                 int cookie_len;
1504                 void *cookie;
1505
1506                 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.
1507                  * C1) ... "Peer-Serial-Number'. This value MUST be initialized to the
1508                  * Initial TSN Value minus 1
1509                  */
1510                 __u32 addip_serial;
1511         } peer;
1512
1513         /* State       : A state variable indicating what state the
1514          *             : association is in, i.e. COOKIE-WAIT,
1515          *             : COOKIE-ECHOED, ESTABLISHED, SHUTDOWN-PENDING,
1516          *             : SHUTDOWN-SENT, SHUTDOWN-RECEIVED, SHUTDOWN-ACK-SENT.
1517          *
1518          *              Note: No "CLOSED" state is illustrated since if a
1519          *              association is "CLOSED" its TCB SHOULD be removed.
1520          *
1521          *              In this implementation we DO have a CLOSED
1522          *              state which is used during initiation and shutdown.
1523          *
1524          *              State takes values from SCTP_STATE_*.
1525          */
1526         sctp_state_t state;
1527
1528         /* The cookie life I award for any cookie.  */
1529         struct timeval cookie_life;
1530
1531         /* Overall     : The overall association error count.
1532          * Error Count : [Clear this any time I get something.]
1533          */
1534         int overall_error_count;
1535
1536         /* These are the association's initial, max, and min RTO values.
1537          * These values will be initialized by system defaults, but can
1538          * be modified via the SCTP_RTOINFO socket option.
1539          */
1540         unsigned long rto_initial;
1541         unsigned long rto_max;
1542         unsigned long rto_min;
1543
1544         /* Maximum number of new data packets that can be sent in a burst.  */
1545         int max_burst;
1546
1547         /* This is the max_retrans value for the association.  This value will
1548          * be initialized initialized from system defaults, but can be
1549          * modified by the SCTP_ASSOCINFO socket option.
1550          */
1551         int max_retrans;
1552
1553         /* Maximum number of times the endpoint will retransmit INIT  */
1554         __u16 max_init_attempts;
1555
1556         /* How many times have we resent an INIT? */
1557         __u16 init_retries;
1558
1559         /* The largest timeout or RTO value to use in attempting an INIT */
1560         unsigned long max_init_timeo;
1561
1562         /* Heartbeat interval: The endpoint sends out a Heartbeat chunk to
1563          * the destination address every heartbeat interval. This value
1564          * will be inherited by all new transports.
1565          */
1566         unsigned long hbinterval;
1567
1568         /* This is the max_retrans value for new transports in the
1569          * association.
1570          */
1571         __u16 pathmaxrxt;
1572
1573         /* Flag that path mtu update is pending */
1574         __u8   pmtu_pending;
1575
1576         /* Association : The smallest PMTU discovered for all of the
1577          * PMTU        : peer's transport addresses.
1578          */
1579         __u32 pathmtu;
1580
1581         /* Flags controling Heartbeat, SACK delay, and Path MTU Discovery. */
1582         __u32 param_flags;
1583
1584         /* SACK delay timeout */
1585         unsigned long sackdelay;
1586
1587
1588         unsigned long timeouts[SCTP_NUM_TIMEOUT_TYPES];
1589         struct timer_list timers[SCTP_NUM_TIMEOUT_TYPES];
1590
1591         /* Transport to which SHUTDOWN chunk was last sent.  */
1592         struct sctp_transport *shutdown_last_sent_to;
1593
1594         /* Transport to which INIT chunk was last sent.  */
1595         struct sctp_transport *init_last_sent_to;
1596
1597         /* Next TSN    : The next TSN number to be assigned to a new
1598          *             : DATA chunk.  This is sent in the INIT or INIT
1599          *             : ACK chunk to the peer and incremented each
1600          *             : time a DATA chunk is assigned a TSN
1601          *             : (normally just prior to transmit or during
1602          *             : fragmentation).
1603          */
1604         __u32 next_tsn;
1605
1606         /*
1607          * Last Rcvd   : This is the last TSN received in sequence.  This value
1608          * TSN         : is set initially by taking the peer's Initial TSN,
1609          *             : received in the INIT or INIT ACK chunk, and
1610          *             : subtracting one from it.
1611          *
1612          * Most of RFC 2960 refers to this as the Cumulative TSN Ack Point.
1613          */
1614
1615         __u32 ctsn_ack_point;
1616
1617         /* PR-SCTP Advanced.Peer.Ack.Point */
1618         __u32 adv_peer_ack_point;
1619
1620         /* Highest TSN that is acknowledged by incoming SACKs. */
1621         __u32 highest_sacked;
1622
1623         /* The number of unacknowledged data chunks.  Reported through
1624          * the SCTP_STATUS sockopt.
1625          */
1626         __u16 unack_data;
1627
1628         /* This is the association's receive buffer space.  This value is used
1629          * to set a_rwnd field in an INIT or a SACK chunk.
1630          */
1631         __u32 rwnd;
1632
1633         /* This is the last advertised value of rwnd over a SACK chunk. */
1634         __u32 a_rwnd;
1635
1636         /* Number of bytes by which the rwnd has slopped.  The rwnd is allowed
1637          * to slop over a maximum of the association's frag_point.
1638          */
1639         __u32 rwnd_over;
1640
1641         /* This is the sndbuf size in use for the association.
1642          * This corresponds to the sndbuf size for the association,
1643          * as specified in the sk->sndbuf.
1644          */
1645         int sndbuf_used;
1646
1647         /* This is the amount of memory that this association has allocated
1648          * in the receive path at any given time.
1649          */
1650         atomic_t rmem_alloc;
1651
1652         /* This is the wait queue head for send requests waiting on
1653          * the association sndbuf space.
1654          */
1655         wait_queue_head_t       wait;
1656
1657         /* The message size at which SCTP fragmentation will occur. */
1658         __u32 frag_point;
1659
1660         /* Counter used to count INIT errors. */
1661         int init_err_counter;
1662
1663         /* Count the number of INIT cycles (for doubling timeout). */
1664         int init_cycle;
1665
1666         /* Default send parameters. */
1667         __u16 default_stream;
1668         __u16 default_flags;
1669         __u32 default_ppid;
1670         __u32 default_context;
1671         __u32 default_timetolive;
1672
1673         /* Default receive parameters */
1674         __u32 default_rcv_context;
1675
1676         /* This tracks outbound ssn for a given stream.  */
1677         struct sctp_ssnmap *ssnmap;
1678
1679         /* All outbound chunks go through this structure.  */
1680         struct sctp_outq outqueue;
1681
1682         /* A smart pipe that will handle reordering and fragmentation,
1683          * as well as handle passing events up to the ULP.
1684          */
1685         struct sctp_ulpq ulpq;
1686
1687         /* Last TSN that caused an ECNE Chunk to be sent.  */
1688         __u32 last_ecne_tsn;
1689
1690         /* Last TSN that caused a CWR Chunk to be sent.  */
1691         __u32 last_cwr_tsn;
1692
1693         /* How many duplicated TSNs have we seen?  */
1694         int numduptsns;
1695
1696         /* Number of seconds of idle time before an association is closed.
1697          * In the association context, this is really used as a boolean
1698          * since the real timeout is stored in the timeouts array
1699          */
1700         __u32 autoclose;
1701
1702         /* These are to support
1703          * "SCTP Extensions for Dynamic Reconfiguration of IP Addresses
1704          *  and Enforcement of Flow and Message Limits"
1705          * <draft-ietf-tsvwg-addip-sctp-02.txt>
1706          * or "ADDIP" for short.
1707          */
1708
1709
1710
1711         /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
1712          *
1713          * R1) One and only one ASCONF Chunk MAY be in transit and
1714          * unacknowledged at any one time.  If a sender, after sending
1715          * an ASCONF chunk, decides it needs to transfer another
1716          * ASCONF Chunk, it MUST wait until the ASCONF-ACK Chunk
1717          * returns from the previous ASCONF Chunk before sending a
1718          * subsequent ASCONF. Note this restriction binds each side,
1719          * so at any time two ASCONF may be in-transit on any given
1720          * association (one sent from each endpoint).
1721          *
1722          * [This is our one-and-only-one ASCONF in flight.  If we do
1723          * not have an ASCONF in flight, this is NULL.]
1724          */
1725         struct sctp_chunk *addip_last_asconf;
1726
1727         /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.
1728          *
1729          * IMPLEMENTATION NOTE: As an optimization a receiver may wish
1730          * to save the last ASCONF-ACK for some predetermined period
1731          * of time and instead of re-processing the ASCONF (with the
1732          * same serial number) it may just re-transmit the
1733          * ASCONF-ACK. It may wish to use the arrival of a new serial
1734          * number to discard the previously saved ASCONF-ACK or any
1735          * other means it may choose to expire the saved ASCONF-ACK.
1736          *
1737          * [This is our saved ASCONF-ACK.  We invalidate it when a new
1738          * ASCONF serial number arrives.]
1739          */
1740         struct sctp_chunk *addip_last_asconf_ack;
1741
1742         /* These ASCONF chunks are waiting to be sent.
1743          *
1744          * These chunaks can't be pushed to outqueue until receiving
1745          * ASCONF_ACK for the previous ASCONF indicated by
1746          * addip_last_asconf, so as to guarantee that only one ASCONF
1747          * is in flight at any time.
1748          *
1749          * ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
1750          *
1751          * In defining the ASCONF Chunk transfer procedures, it is
1752          * essential that these transfers MUST NOT cause congestion
1753          * within the network.  To achieve this, we place these
1754          * restrictions on the transfer of ASCONF Chunks:
1755          *
1756          * R1) One and only one ASCONF Chunk MAY be in transit and
1757          * unacknowledged at any one time.  If a sender, after sending
1758          * an ASCONF chunk, decides it needs to transfer another
1759          * ASCONF Chunk, it MUST wait until the ASCONF-ACK Chunk
1760          * returns from the previous ASCONF Chunk before sending a
1761          * subsequent ASCONF. Note this restriction binds each side,
1762          * so at any time two ASCONF may be in-transit on any given
1763          * association (one sent from each endpoint).
1764          *
1765          *
1766          * [I really think this is EXACTLY the sort of intelligence
1767          *  which already resides in sctp_outq.  Please move this
1768          *  queue and its supporting logic down there.  --piggy]
1769          */
1770         struct list_head addip_chunk_list;
1771
1772         /* ADDIP Section 4.1 ASCONF Chunk Procedures
1773          *
1774          * A2) A serial number should be assigned to the Chunk. The
1775          * serial number SHOULD be a monotonically increasing
1776          * number. The serial number SHOULD be initialized at
1777          * the start of the association to the same value as the
1778          * Initial TSN and every time a new ASCONF chunk is created
1779          * it is incremented by one after assigning the serial number
1780          * to the newly created chunk.
1781          *
1782          * ADDIP
1783          * 3.1.1  Address/Stream Configuration Change Chunk (ASCONF)
1784          *
1785          * Serial Number : 32 bits (unsigned integer)
1786          *
1787          * This value represents a Serial Number for the ASCONF
1788          * Chunk. The valid range of Serial Number is from 0 to
1789          * 4294967295 (2^32 - 1).  Serial Numbers wrap back to 0
1790          * after reaching 4294967295.
1791          */
1792         __u32 addip_serial;
1793
1794         /* Need to send an ECNE Chunk? */
1795         char need_ecne;
1796
1797         /* Is it a temporary association? */
1798         char temp;
1799 };
1800
1801
1802 /* An eyecatcher for determining if we are really looking at an
1803  * association data structure.
1804  */
1805 enum {
1806         SCTP_ASSOC_EYECATCHER = 0xa550c123,
1807 };
1808
1809 /* Recover the outter association structure. */
1810 static inline struct sctp_association *sctp_assoc(struct sctp_ep_common *base)
1811 {
1812         struct sctp_association *asoc;
1813
1814         asoc = container_of(base, struct sctp_association, base);
1815         return asoc;
1816 }
1817
1818 /* These are function signatures for manipulating associations.  */
1819
1820
1821 struct sctp_association *
1822 sctp_association_new(const struct sctp_endpoint *, const struct sock *,
1823                      sctp_scope_t scope, gfp_t gfp);
1824 void sctp_association_free(struct sctp_association *);
1825 void sctp_association_put(struct sctp_association *);
1826 void sctp_association_hold(struct sctp_association *);
1827
1828 struct sctp_transport *sctp_assoc_choose_init_transport(
1829         struct sctp_association *);
1830 struct sctp_transport *sctp_assoc_choose_shutdown_transport(
1831         struct sctp_association *);
1832 void sctp_assoc_update_retran_path(struct sctp_association *);
1833 struct sctp_transport *sctp_assoc_lookup_paddr(const struct sctp_association *,
1834                                           const union sctp_addr *);
1835 int sctp_assoc_lookup_laddr(struct sctp_association *asoc,
1836                             const union sctp_addr *laddr);
1837 struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *,
1838                                      const union sctp_addr *address,
1839                                      const gfp_t gfp,
1840                                      const int peer_state);
1841 void sctp_assoc_del_peer(struct sctp_association *asoc,
1842                          const union sctp_addr *addr);
1843 void sctp_assoc_rm_peer(struct sctp_association *asoc,
1844                          struct sctp_transport *peer);
1845 void sctp_assoc_control_transport(struct sctp_association *,
1846                                   struct sctp_transport *,
1847                                   sctp_transport_cmd_t, sctp_sn_error_t);
1848 struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *, __u32);
1849 struct sctp_transport *sctp_assoc_is_match(struct sctp_association *,
1850                                            const union sctp_addr *,
1851                                            const union sctp_addr *);
1852 void sctp_assoc_migrate(struct sctp_association *, struct sock *);
1853 void sctp_assoc_update(struct sctp_association *old,
1854                        struct sctp_association *new);
1855
1856 __u32 sctp_association_get_next_tsn(struct sctp_association *);
1857
1858 void sctp_assoc_sync_pmtu(struct sctp_association *);
1859 void sctp_assoc_rwnd_increase(struct sctp_association *, unsigned);
1860 void sctp_assoc_rwnd_decrease(struct sctp_association *, unsigned);
1861 void sctp_assoc_set_primary(struct sctp_association *,
1862                             struct sctp_transport *);
1863 int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *,
1864                                      gfp_t);
1865 int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *,
1866                                          struct sctp_cookie*,
1867                                          gfp_t gfp);
1868 int sctp_assoc_set_id(struct sctp_association *, gfp_t);
1869
1870 int sctp_cmp_addr_exact(const union sctp_addr *ss1,
1871                         const union sctp_addr *ss2);
1872 struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc);
1873
1874 /* A convenience structure to parse out SCTP specific CMSGs. */
1875 typedef struct sctp_cmsgs {
1876         struct sctp_initmsg *init;
1877         struct sctp_sndrcvinfo *info;
1878 } sctp_cmsgs_t;
1879
1880 /* Structure for tracking memory objects */
1881 typedef struct {
1882         char *label;
1883         atomic_t *counter;
1884 } sctp_dbg_objcnt_entry_t;
1885
1886 #endif /* __sctp_structs_h__ */