structs.h 61.9 KB
Newer Older
1
/* SCTP kernel implementation
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6
 * (C) Copyright IBM Corp. 2001, 2004
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001 Intel Corp.
 *
7
 * This file is part of the SCTP kernel implementation
Linus Torvalds's avatar
Linus Torvalds committed
8
 *
9
 * This SCTP implementation is free software;
Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
15
 * This SCTP implementation is distributed in the hope that it
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *		   ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
22 23
 * along with GNU CC; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26
 *
 * Please send any bug reports or fixes you make to the
 * email addresses:
27
 *    lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *
 * Written or modified by:
 *    Randall Stewart	    <randall@sctp.chicago.il.us>
 *    Ken Morneau	    <kmorneau@cisco.com>
 *    Qiaobing Xie	    <qxie1@email.mot.com>
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson	    <karl@athena.chicago.il.us>
 *    Jon Grimm		    <jgrimm@us.ibm.com>
 *    Xingang Guo	    <xingang.guo@intel.com>
 *    Hui Huang		    <hui.huang@nokia.com>
 *    Sridhar Samudrala	    <sri@us.ibm.com>
 *    Daisy Chang	    <daisyc@us.ibm.com>
 *    Dajiang Zhang	    <dajiang.zhang@nokia.com>
 *    Ardelle Fan	    <ardelle.fan@intel.com>
 *    Ryan Layer	    <rmlayer@us.ibm.com>
 *    Anup Pemmaiah	    <pemmaiah@cc.usu.edu>
 *    Kevin Gao             <kevin.gao@intel.com>
 */

#ifndef __sctp_structs_h__
#define __sctp_structs_h__

50
#include <linux/ktime.h>
51
#include <linux/rhashtable.h>
Linus Torvalds's avatar
Linus Torvalds committed
52 53 54 55 56
#include <linux/socket.h>	/* linux/in.h needs this!!    */
#include <linux/in.h>		/* We get struct sockaddr_in. */
#include <linux/in6.h>		/* We get struct in6_addr     */
#include <linux/ipv6.h>
#include <asm/param.h>		/* We get MAXHOSTNAMELEN.     */
Arun Sharma's avatar
Arun Sharma committed
57
#include <linux/atomic.h>		/* This gets us atomic counters.  */
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60
#include <linux/skbuff.h>	/* We need sk_buff_head. */
#include <linux/workqueue.h>	/* We need tq_struct.	 */
#include <linux/sctp.h>		/* We need sctp* header structs.  */
61
#include <net/sctp/auth.h>	/* We need auth specific structs */
62
#include <net/ip.h>		/* For inet_skb_parm */
Linus Torvalds's avatar
Linus Torvalds committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

/* A convenience structure for handling sockaddr structures.
 * We should wean ourselves off this.
 */
union sctp_addr {
	struct sockaddr_in v4;
	struct sockaddr_in6 v6;
	struct sockaddr sa;
};

/* Forward declarations for data structures. */
struct sctp_globals;
struct sctp_endpoint;
struct sctp_association;
struct sctp_transport;
struct sctp_packet;
struct sctp_chunk;
struct sctp_inq;
struct sctp_outq;
struct sctp_bind_addr;
struct sctp_ulpq;
struct sctp_ep_common;
struct sctp_ssnmap;
Herbert Xu's avatar
Herbert Xu committed
86
struct crypto_shash;
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90 91 92 93 94 95 96 97


#include <net/sctp/tsnmap.h>
#include <net/sctp/ulpevent.h>
#include <net/sctp/ulpqueue.h>

/* Structures useful for managing bind/connect. */

struct sctp_bind_bucket {
	unsigned short	port;
	unsigned short	fastreuse;
98
	struct hlist_node	node;
Linus Torvalds's avatar
Linus Torvalds committed
99
	struct hlist_head	owner;
100
	struct net	*net;
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104
};

struct sctp_bind_hashbucket {
	spinlock_t	lock;
105
	struct hlist_head	chain;
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109 110
};

/* Used for hashing all associations.  */
struct sctp_hashbucket {
	rwlock_t	lock;
111
	struct hlist_head	chain;
Linus Torvalds's avatar
Linus Torvalds committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
} __attribute__((__aligned__(8)));


/* The SCTP globals structure. */
extern struct sctp_globals {
	/* This is a list of groups of functions for each address
	 * family that we support.
	 */
	struct list_head address_families;

	/* This is the hash of all endpoints. */
	struct sctp_hashbucket *ep_hashtable;
	/* This is the sctp port control hash.	*/
	struct sctp_bind_hashbucket *port_hashtable;
126
	/* This is the hash of all transports. */
127
	struct rhltable transport_hashtable;
128

129 130 131 132 133 134 135 136
	/* Sizes of above hashtables. */
	int ep_hashsize;
	int port_hashsize;

	/* Default initialization values to be applied to new associations. */
	__u16 max_instreams;
	__u16 max_outstreams;

137 138 139
	/* Flag to indicate whether computing and verifying checksum
	 * is disabled. */
        bool checksum_disable;
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145 146 147 148
} sctp_globals;

#define sctp_max_instreams		(sctp_globals.max_instreams)
#define sctp_max_outstreams		(sctp_globals.max_outstreams)
#define sctp_address_families		(sctp_globals.address_families)
#define sctp_ep_hashsize		(sctp_globals.ep_hashsize)
#define sctp_ep_hashtable		(sctp_globals.ep_hashtable)
#define sctp_port_hashsize		(sctp_globals.port_hashsize)
#define sctp_port_hashtable		(sctp_globals.port_hashtable)
149
#define sctp_transport_hashtable	(sctp_globals.transport_hashtable)
150
#define sctp_checksum_disable		(sctp_globals.checksum_disable)
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

/* SCTP Socket type: UDP or TCP style. */
typedef enum {
	SCTP_SOCKET_UDP = 0,
	SCTP_SOCKET_UDP_HIGH_BANDWIDTH,
	SCTP_SOCKET_TCP
} sctp_socket_type_t;

/* Per socket SCTP information. */
struct sctp_sock {
	/* inet_sock has to be the first member of sctp_sock */
	struct inet_sock inet;
	/* What kind of a socket is this? */
	sctp_socket_type_t type;

	/* PF_ family specific functions.  */
	struct sctp_pf *pf;

	/* Access to HMAC transform. */
Herbert Xu's avatar
Herbert Xu committed
170
	struct crypto_shash *hmac;
171
	char *sctp_hmac_alg;
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180 181 182

	/* What is our base endpointer? */
	struct sctp_endpoint *ep;

	struct sctp_bind_bucket *bind_hash;
	/* Various Socket Options.  */
	__u16 default_stream;
	__u32 default_ppid;
	__u16 default_flags;
	__u32 default_context;
	__u32 default_timetolive;
183
	__u32 default_rcv_context;
184
	int max_burst;
Linus Torvalds's avatar
Linus Torvalds committed
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199
	/* Heartbeat interval: The endpoint sends out a Heartbeat chunk to
	 * the destination address every heartbeat interval. This value
	 * will be inherited by all new associations.
	 */
	__u32 hbinterval;

	/* This is the max_retrans value for new associations. */
	__u16 pathmaxrxt;

	/* The initial Path MTU to use for new associations. */
	__u32 pathmtu;

	/* The default SACK delay timeout for new associations. */
	__u32 sackdelay;
200
	__u32 sackfreq;
201

202
	/* Flags controlling Heartbeat, SACK delay, and Path MTU Discovery. */
203 204
	__u32 param_flags;

Linus Torvalds's avatar
Linus Torvalds committed
205 206 207 208 209
	struct sctp_initmsg initmsg;
	struct sctp_rtoinfo rtoinfo;
	struct sctp_paddrparams paddrparam;
	struct sctp_event_subscribe subscribe;
	struct sctp_assocparams assocparams;
210

Linus Torvalds's avatar
Linus Torvalds committed
211
	int user_frag;
212

Linus Torvalds's avatar
Linus Torvalds committed
213
	__u32 autoclose;
214
	__u32 adaptation_ind;
215
	__u32 pd_point;
216 217 218 219 220
	__u16	nodelay:1,
		disable_fragments:1,
		v4mapped:1,
		frag_interleave:1,
		recvrcvinfo:1,
221
		recvnxtinfo:1,
222
		data_ready_signalled:1;
Linus Torvalds's avatar
Linus Torvalds committed
223

224
	atomic_t pd_mode;
Linus Torvalds's avatar
Linus Torvalds committed
225 226
	/* Receive to here while partial delivery is in effect. */
	struct sk_buff_head pd_lobby;
227 228 229 230

	/* These must be the last fields, as they will skipped on copies,
	 * like on accept and peeloff operations
	 */
231 232
	struct list_head auto_asconf_list;
	int do_auto_asconf;
Linus Torvalds's avatar
Linus Torvalds committed
233 234 235 236 237 238 239 240 241 242 243 244
};

static inline struct sctp_sock *sctp_sk(const struct sock *sk)
{
       return (struct sctp_sock *)sk;
}

static inline struct sock *sctp_opt2sk(const struct sctp_sock *sp)
{
       return (struct sock *)sp;
}

245
#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
struct sctp6_sock {
       struct sctp_sock  sctp;
       struct ipv6_pinfo inet6;
};
#endif /* CONFIG_IPV6 */


/* This is our APPLICATION-SPECIFIC state cookie.
 * THIS IS NOT DICTATED BY THE SPECIFICATION.
 */
/* These are the parts of an association which we send in the cookie.
 * Most of these are straight out of:
 * RFC2960 12.2 Parameters necessary per association (i.e. the TCB)
 *
 */

struct sctp_cookie {

	/* My	       : Tag expected in every inbound packet and sent
	 * Verification: in the INIT or INIT ACK chunk.
	 * Tag	       :
	 */
	__u32 my_vtag;

	/* Peer's      : Tag expected in every outbound packet except
	 * Verification: in the INIT chunk.
	 * Tag	       :
	 */
	__u32 peer_vtag;

	/* The rest of these are not from the spec, but really need to
	 * be in the cookie.
	 */

	/* My Tie Tag  : Assist in discovering a restarting association. */
	__u32 my_ttag;

	/* Peer's Tie Tag: Assist in discovering a restarting association. */
	__u32 peer_ttag;

	/* When does this cookie expire? */
287
	ktime_t expiration;
Linus Torvalds's avatar
Linus Torvalds committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

	/* Number of inbound/outbound streams which are set
	 * and negotiated during the INIT process.
	 */
	__u16 sinit_num_ostreams;
	__u16 sinit_max_instreams;

	/* This is the first sequence number I used.  */
	__u32 initial_tsn;

	/* This holds the originating address of the INIT packet.  */
	union sctp_addr peer_addr;

	/* IG Section 2.35.3 
	 * Include the source port of the INIT-ACK
	 */
	__u16		my_port;

	__u8 prsctp_capable;

	/* Padding for future use */
	__u8 padding;  		

311
	__u32 adaptation_ind;
Linus Torvalds's avatar
Linus Torvalds committed
312

313
	__u8 auth_random[sizeof(sctp_paramhdr_t) + SCTP_AUTH_RANDOM_LENGTH];
314
	__u8 auth_hmacs[SCTP_AUTH_NUM_HMACS * sizeof(__u16) + 2];
315
	__u8 auth_chunks[sizeof(sctp_paramhdr_t) + SCTP_AUTH_MAX_CHUNKS];
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	/* This is a shim for my peer's INIT packet, followed by
	 * a copy of the raw address list of the association.
	 * The length of the raw address list is saved in the
	 * raw_addr_list_len field, which will be used at the time when
	 * the association TCB is re-constructed from the cookie.
	 */
	__u32 raw_addr_list_len;
	struct sctp_init_chunk peer_init[0];
};


/* The format of our cookie that we send to our peer. */
struct sctp_signed_cookie {
	__u8 signature[SCTP_SECRET_SIZE];
331
	__u32 __pad;		/* force sctp_cookie alignment to 64 bits */
Linus Torvalds's avatar
Linus Torvalds committed
332
	struct sctp_cookie c;
Eric Dumazet's avatar
Eric Dumazet committed
333
} __packed;
Linus Torvalds's avatar
Linus Torvalds committed
334 335 336 337 338 339

/* This is another convenience type to allocate memory for address
 * params for the maximum size and pass such structures around
 * internally.
 */
union sctp_addr_param {
340
	struct sctp_paramhdr p;
Linus Torvalds's avatar
Linus Torvalds committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	struct sctp_ipv4addr_param v4;
	struct sctp_ipv6addr_param v6;
};

/* A convenience type to allow walking through the various
 * parameters and avoid casting all over the place.
 */
union sctp_params {
	void *v;
	struct sctp_paramhdr *p;
	struct sctp_cookie_preserve_param *life;
	struct sctp_hostname_param *dns;
	struct sctp_cookie_param *cookie;
	struct sctp_supported_addrs_param *sat;
	struct sctp_ipv4addr_param *v4;
	struct sctp_ipv6addr_param *v6;
	union sctp_addr_param *addr;
358
	struct sctp_adaptation_ind_param *aind;
359
	struct sctp_supported_ext_param *ext;
360 361 362
	struct sctp_random_param *random;
	struct sctp_chunks_param *chunks;
	struct sctp_hmac_algo_param *hmac_algo;
363
	struct sctp_addip_param *addip;
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367 368 369 370 371 372 373 374 375 376
};

/* RFC 2960.  Section 3.3.5 Heartbeat.
 *    Heartbeat Information: variable length
 *    The Sender-specific Heartbeat Info field should normally include
 *    information about the sender's current time when this HEARTBEAT
 *    chunk is sent and the destination transport address to which this
 *    HEARTBEAT is sent (see Section 8.3).
 */
typedef struct sctp_sender_hb_info {
	struct sctp_paramhdr param_hdr;
	union sctp_addr daddr;
	unsigned long sent_at;
377
	__u64 hb_nonce;
Eric Dumazet's avatar
Eric Dumazet committed
378
} __packed sctp_sender_hb_info_t;
Linus Torvalds's avatar
Linus Torvalds committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

/*
 *  RFC 2960 1.3.2 Sequenced Delivery within Streams
 *
 *  The term "stream" is used in SCTP to refer to a sequence of user
 *  messages that are to be delivered to the upper-layer protocol in
 *  order with respect to other messages within the same stream.  This is
 *  in contrast to its usage in TCP, where it refers to a sequence of
 *  bytes (in this document a byte is assumed to be eight bits).
 *  ...
 *
 *  This is the structure we use to track both our outbound and inbound
 *  SSN, or Stream Sequence Numbers.
 */

struct sctp_stream {
	__u16 *ssn;
	unsigned int len;
};

struct sctp_ssnmap {
	struct sctp_stream in;
	struct sctp_stream out;
};

Alexey Dobriyan's avatar
Alexey Dobriyan committed
404
struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
405
				    gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
void sctp_ssnmap_free(struct sctp_ssnmap *map);
void sctp_ssnmap_clear(struct sctp_ssnmap *map);

/* What is the current SSN number for this stream? */
static inline __u16 sctp_ssn_peek(struct sctp_stream *stream, __u16 id)
{
	return stream->ssn[id];
}

/* Return the next SSN number for this stream.	*/
static inline __u16 sctp_ssn_next(struct sctp_stream *stream, __u16 id)
{
	return stream->ssn[id]++;
}

/* Skip over this ssn and all below. */
static inline void sctp_ssn_skip(struct sctp_stream *stream, __u16 id, 
				 __u16 ssn)
{
	stream->ssn[id] = ssn+1;
}
              
/*
 * Pointers to address related SCTP functions.
 * (i.e. things that depend on the address family.)
 */
struct sctp_af {
	int		(*sctp_xmit)	(struct sk_buff *skb,
434
					 struct sctp_transport *);
Linus Torvalds's avatar
Linus Torvalds committed
435 436 437 438
	int		(*setsockopt)	(struct sock *sk,
					 int level,
					 int optname,
					 char __user *optval,
439
					 unsigned int optlen);
Linus Torvalds's avatar
Linus Torvalds committed
440 441 442 443 444
	int		(*getsockopt)	(struct sock *sk,
					 int level,
					 int optname,
					 char __user *optval,
					 int __user *optlen);
445 446 447 448
	int		(*compat_setsockopt)	(struct sock *sk,
					 int level,
					 int optname,
					 char __user *optval,
449
					 unsigned int optlen);
450 451 452 453 454
	int		(*compat_getsockopt)	(struct sock *sk,
					 int level,
					 int optname,
					 char __user *optval,
					 int __user *optlen);
455
	void		(*get_dst)	(struct sctp_transport *t,
456 457 458
					 union sctp_addr *saddr,
					 struct flowi *fl,
					 struct sock *sk);
459
	void		(*get_saddr)	(struct sctp_sock *sk,
460 461
					 struct sctp_transport *t,
					 struct flowi *fl);
Linus Torvalds's avatar
Linus Torvalds committed
462 463 464 465 466 467 468 469 470 471 472 473 474
	void		(*copy_addrlist) (struct list_head *,
					  struct net_device *);
	int		(*cmp_addr)	(const union sctp_addr *addr1,
					 const union sctp_addr *addr2);
	void		(*addr_copy)	(union sctp_addr *dst,
					 union sctp_addr *src);
	void		(*from_skb)	(union sctp_addr *,
					 struct sk_buff *skb,
					 int saddr);
	void		(*from_sk)	(union sctp_addr *,
					 struct sock *sk);
	void		(*from_addr_param) (union sctp_addr *,
					    union sctp_addr_param *,
475
					    __be16 port, int iif);
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478
	int		(*to_addr_param) (const union sctp_addr *,
					  union sctp_addr_param *); 
	int		(*addr_valid)	(union sctp_addr *,
479 480
					 struct sctp_sock *,
					 const struct sk_buff *);
Linus Torvalds's avatar
Linus Torvalds committed
481
	sctp_scope_t	(*scope) (union sctp_addr *);
Al Viro's avatar
Al Viro committed
482
	void		(*inaddr_any)	(union sctp_addr *, __be16);
Linus Torvalds's avatar
Linus Torvalds committed
483 484 485 486 487 488 489
	int		(*is_any)	(const union sctp_addr *);
	int		(*available)	(union sctp_addr *,
					 struct sctp_sock *);
	int		(*skb_iif)	(const struct sk_buff *sk);
	int		(*is_ce)	(const struct sk_buff *sk);
	void		(*seq_dump_addr)(struct seq_file *seq,
					 union sctp_addr *addr);
490
	void		(*ecn_capable)(struct sock *sk);
Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
	__u16		net_header_len;
	int		sockaddr_len;
	sa_family_t	sa_family;
	struct list_head list;
};

struct sctp_af *sctp_get_af_specific(sa_family_t);
int sctp_register_af(struct sctp_af *);

/* Protocol family functions. */
struct sctp_pf {
	void (*event_msgname)(struct sctp_ulpevent *, char *, int *);
	void (*skb_msgname)  (struct sk_buff *, char *, int *);
	int  (*af_supported) (sa_family_t, struct sctp_sock *);
	int  (*cmp_addr) (const union sctp_addr *,
			  const union sctp_addr *,
			  struct sctp_sock *);
	int  (*bind_verify) (struct sctp_sock *, union sctp_addr *);
	int  (*send_verify) (struct sctp_sock *, union sctp_addr *);
510
	int  (*supported_addrs)(const struct sctp_sock *, __be16 *);
Linus Torvalds's avatar
Linus Torvalds committed
511 512
	struct sock *(*create_accept_sk) (struct sock *sk,
					  struct sctp_association *asoc);
513 514 515
	int (*addr_to_user)(struct sctp_sock *sk, union sctp_addr *addr);
	void (*to_sk_saddr)(union sctp_addr *, struct sock *sk);
	void (*to_sk_daddr)(union sctp_addr *, struct sock *sk);
Linus Torvalds's avatar
Linus Torvalds committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	struct sctp_af *af;
};


/* Structure to track chunk fragments that have been acked, but peer
 * fragments of the same message have not.
 */
struct sctp_datamsg {
	/* Chunks waiting to be submitted to lower layer. */
	struct list_head chunks;
	/* Reference counting. */
	atomic_t refcnt;
	/* When is this message no longer interesting to the peer? */
	unsigned long expires_at;
	/* Did the messenge fail to send? */
	int send_error;
532 533
	u8 send_failed:1,
	   can_delay;	    /* should this message be Nagle delayed */
Linus Torvalds's avatar
Linus Torvalds committed
534 535 536 537
};

struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *,
					    struct sctp_sndrcvinfo *,
538
					    struct iov_iter *);
539
void sctp_datamsg_free(struct sctp_datamsg *);
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544 545 546 547 548 549 550 551 552
void sctp_datamsg_put(struct sctp_datamsg *);
void sctp_chunk_fail(struct sctp_chunk *, int error);
int sctp_chunk_abandoned(struct sctp_chunk *);

/* RFC2960 1.4 Key Terms
 *
 * o Chunk: A unit of information within an SCTP packet, consisting of
 * a chunk header and chunk-specific content.
 *
 * As a matter of convenience, we remember the SCTP common header for
 * each chunk as well as a few other header pointers...
 */
struct sctp_chunk {
553 554
	struct list_head list;

Linus Torvalds's avatar
Linus Torvalds committed
555 556
	atomic_t refcnt;

557 558 559
	/* How many times this chunk have been sent, for prsctp RTX policy */
	int sent_count;

Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565 566 567 568 569 570 571 572
	/* This is our link to the per-transport transmitted list.  */
	struct list_head transmitted_list;

	/* This field is used by chunks that hold fragmented data.
	 * For the first fragment this is the list that holds the rest of
	 * fragments. For the remaining fragments, this is the link to the
	 * frag_list maintained in the first fragment.
	 */
	struct list_head frag_list;

	/* This points to the sk_buff containing the actual data.  */
	struct sk_buff *skb;

573 574 575
	/* In case of GSO packets, this will store the head one */
	struct sk_buff *head_skb;

Linus Torvalds's avatar
Linus Torvalds committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	/* These are the SCTP headers by reverse order in a packet.
	 * Note that some of these may happen more than once.  In that
	 * case, we point at the "current" one, whatever that means
	 * for that level of header.
	 */

	/* We point this at the FIRST TLV parameter to chunk_hdr.  */
	union sctp_params param_hdr;
	union {
		__u8 *v;
		struct sctp_datahdr *data_hdr;
		struct sctp_inithdr *init_hdr;
		struct sctp_sackhdr *sack_hdr;
		struct sctp_heartbeathdr *hb_hdr;
		struct sctp_sender_hb_info *hbs_hdr;
		struct sctp_shutdownhdr *shutdown_hdr;
		struct sctp_signed_cookie *cookie_hdr;
		struct sctp_ecnehdr *ecne_hdr;
		struct sctp_cwrhdr *ecn_cwr_hdr;
		struct sctp_errhdr *err_hdr;
		struct sctp_addiphdr *addip_hdr;
		struct sctp_fwdtsn_hdr *fwdtsn_hdr;
598
		struct sctp_authhdr *auth_hdr;
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	} subh;

	__u8 *chunk_end;

	struct sctp_chunkhdr *chunk_hdr;
	struct sctphdr *sctp_hdr;

	/* This needs to be recoverable for SCTP_SEND_FAILED events. */
	struct sctp_sndrcvinfo sinfo;

	/* Which association does this belong to?  */
	struct sctp_association *asoc;

	/* What endpoint received this chunk? */
	struct sctp_ep_common *rcvr;

	/* We fill this in if we are calculating RTT. */
	unsigned long sent_at;

	/* What is the origin IP address for this chunk?  */
	union sctp_addr source;
	/* Destination address for this chunk. */
	union sctp_addr dest;

	/* For outbound message, track all fragments for SEND_FAILED. */
	struct sctp_datamsg *msg;

	/* For an inbound chunk, this tells us where it came from.
	 * For an outbound chunk, it tells us where we'd like it to
	 * go.	It is NULL if we have no preference.
	 */
	struct sctp_transport *transport;

632 633 634 635 636 637 638
	/* SCTP-AUTH:  For the special case inbound processing of COOKIE-ECHO
	 * we need save a pointer to the AUTH chunk, since the SCTP-AUTH
	 * spec violates the principle premis that all chunks are processed
	 * in order.
	 */
	struct sk_buff *auth_chunk;

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
#define SCTP_CAN_FRTX 0x0
#define SCTP_NEED_FRTX 0x1
#define SCTP_DONT_FRTX 0x2
	__u16	rtt_in_progress:1,	/* This chunk used for RTT calc? */
		has_tsn:1,		/* Does this chunk have a TSN yet? */
		has_ssn:1,		/* Does this chunk have a SSN yet? */
		singleton:1,		/* Only chunk in the packet? */
		end_of_packet:1,	/* Last chunk in the packet? */
		ecn_ce_done:1,		/* Have we processed the ECN CE bit? */
		pdiscard:1,		/* Discard the whole packet now? */
		tsn_gap_acked:1,	/* Is this chunk acked by a GAP ACK? */
		data_accepted:1,	/* At least 1 chunk accepted */
		auth:1,			/* IN: was auth'ed | OUT: needs auth */
		has_asconf:1,		/* IN: have seen an asconf before */
		tsn_missing_report:2,	/* Data chunk missing counter. */
		fast_retransmit:2;	/* Is this chunk fast retransmitted? */
Linus Torvalds's avatar
Linus Torvalds committed
655 656
};

657
#define sctp_chunk_retransmitted(chunk)	(chunk->sent_count > 1)
Linus Torvalds's avatar
Linus Torvalds committed
658 659
void sctp_chunk_hold(struct sctp_chunk *);
void sctp_chunk_put(struct sctp_chunk *);
660 661
int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
			  struct iov_iter *from);
Linus Torvalds's avatar
Linus Torvalds committed
662 663 664 665
void sctp_chunk_free(struct sctp_chunk *);
void  *sctp_addto_chunk(struct sctp_chunk *, int len, const void *data);
struct sctp_chunk *sctp_chunkify(struct sk_buff *,
				 const struct sctp_association *,
666
				 struct sock *, gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
667 668 669 670
void sctp_init_addrs(struct sctp_chunk *, union sctp_addr *,
		     union sctp_addr *);
const union sctp_addr *sctp_source(const struct sctp_chunk *chunk);

671 672 673 674 675 676
enum {
	SCTP_ADDR_NEW,		/* new address added to assoc/ep */
	SCTP_ADDR_SRC,		/* address can be used as source */
	SCTP_ADDR_DEL,		/* address about to be deleted */
};

Linus Torvalds's avatar
Linus Torvalds committed
677 678 679
/* This is a structure for holding either an IPv6 or an IPv4 address.  */
struct sctp_sockaddr_entry {
	struct list_head list;
680
	struct rcu_head	rcu;
Linus Torvalds's avatar
Linus Torvalds committed
681
	union sctp_addr a;
682
	__u8 state;
683
	__u8 valid;
Linus Torvalds's avatar
Linus Torvalds committed
684 685
};

686 687
#define SCTP_ADDRESS_TICK_DELAY	500

Linus Torvalds's avatar
Linus Torvalds committed
688 689 690 691 692 693 694 695 696 697 698 699
typedef struct sctp_chunk *(sctp_packet_phandler_t)(struct sctp_association *);

/* This structure holds lists of chunks as we are assembling for
 * transmission.
 */
struct sctp_packet {
	/* These are the SCTP header values (host order) for the packet. */
	__u16 source_port;
	__u16 destination_port;
	__u32 vtag;

	/* This contains the payload chunks.  */
700
	struct list_head chunk_list;
Linus Torvalds's avatar
Linus Torvalds committed
701 702 703 704 705

	/* This is the overhead of the sctp and ip headers. */
	size_t overhead;
	/* This is the total size of all chunks INCLUDING padding.  */
	size_t size;
706 707
	/* This is the maximum size this packet may have */
	size_t max_size;
Linus Torvalds's avatar
Linus Torvalds committed
708 709 710 711 712 713 714

	/* The packet is destined for this transport address.
	 * The function we finally use to pass down to the next lower
	 * layer lives in the transport structure.
	 */
	struct sctp_transport *transport;

715 716 717
	/* pointer to the auth chunk for this packet */
	struct sctp_chunk *auth;

718 719 720 721
	u8  has_cookie_echo:1,	/* This packet contains a COOKIE-ECHO chunk. */
	    has_sack:1,		/* This packet contains a SACK chunk. */
	    has_auth:1,		/* This packet contains an AUTH chunk */
	    has_data:1,		/* This packet contains at least 1 DATA chunk */
722
	    ipfragok:1;		/* So let ip fragment this packet */
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726 727 728 729
};

struct sctp_packet *sctp_packet_init(struct sctp_packet *,
				     struct sctp_transport *,
				     __u16 sport, __u16 dport);
struct sctp_packet *sctp_packet_config(struct sctp_packet *, __u32 vtag, int);
sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *,
730
				       struct sctp_chunk *, int, gfp_t);
Linus Torvalds's avatar
Linus Torvalds committed
731 732
sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *,
                                     struct sctp_chunk *);
733
int sctp_packet_transmit(struct sctp_packet *, gfp_t);
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736 737
void sctp_packet_free(struct sctp_packet *);

static inline int sctp_packet_empty(struct sctp_packet *packet)
{
Eric Dumazet's avatar
Eric Dumazet committed
738
	return packet->size == packet->overhead;
Linus Torvalds's avatar
Linus Torvalds committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
}

/* This represents a remote transport address.
 * For local transport addresses, we just use union sctp_addr.
 *
 * RFC2960 Section 1.4 Key Terms
 *
 *   o	Transport address:  A Transport Address is traditionally defined
 *	by Network Layer address, Transport Layer protocol and Transport
 *	Layer port number.  In the case of SCTP running over IP, a
 *	transport address is defined by the combination of an IP address
 *	and an SCTP port number (where SCTP is the Transport protocol).
 *
 * RFC2960 Section 7.1 SCTP Differences from TCP Congestion control
 *
 *   o	The sender keeps a separate congestion control parameter set for
 *	each of the destination addresses it can send to (not each
 *	source-destination pair but for each destination).  The parameters
 *	should decay if the address is not used for a long enough time
 *	period.
 *
 */
struct sctp_transport {
	/* A list of transports. */
	struct list_head transports;
764
	struct rhlist_head node;
Linus Torvalds's avatar
Linus Torvalds committed
765 766 767

	/* Reference counting. */
	atomic_t refcnt;
768 769 770 771 772 773 774 775 776
		/* RTO-Pending : A flag used to track if one of the DATA
		 *		chunks sent to this address is currently being
		 *		used to compute a RTT. If this flag is 0,
		 *		the next DATA chunk sent to this destination
		 *		should be used to compute a RTT and this flag
		 *		should be set. Every time the RTT
		 *		calculation completes (i.e. the DATA chunk
		 *		is SACK'd) clear this flag.
		 */
777
	__u32	rto_pending:1,
778 779 780 781 782 783 784 785

		/*
		 * hb_sent : a flag that signals that we have a pending
		 * heartbeat.
		 */
		hb_sent:1,

		/* Is the Path MTU update pending on this tranport */
786
		pmtu_pending:1,
787

788 789
		/* Has this transport moved the ctsn since we last sacked */
		sack_generation:1;
790
	u32 dst_cookie;
791

792
	struct flowi fl;
Linus Torvalds's avatar
Linus Torvalds committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

	/* This is the peer's IP address and port. */
	union sctp_addr ipaddr;

	/* These are the functions we call to handle LLP stuff.	 */
	struct sctp_af *af_specific;

	/* Which association do we belong to?  */
	struct sctp_association *asoc;

	/* RFC2960
	 *
	 * 12.3 Per Transport Address Data
	 *
	 * For each destination transport address in the peer's
	 * address list derived from the INIT or INIT ACK chunk, a
	 * number of data elements needs to be maintained including:
	 */
	/* RTO	       : The current retransmission timeout value.  */
812
	unsigned long rto;
813 814

	__u32 rtt;		/* This is the most recent RTT.	 */
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

	/* RTTVAR      : The current RTT variation.  */
	__u32 rttvar;

	/* SRTT	       : The current smoothed round trip time.	*/
	__u32 srtt;

	/*
	 * These are the congestion stats.
	 */
	/* cwnd	       : The current congestion window.	 */
	__u32 cwnd;		  /* This is the actual cwnd.  */

	/* ssthresh    : The current slow start threshold value.  */
	__u32 ssthresh;

	/* partial     : The tracking method for increase of cwnd when in
	 * bytes acked : congestion avoidance mode (see Section 6.2.2)
	 */
	__u32 partial_bytes_acked;

	/* Data that has been sent, but not acknowledged. */
	__u32 flight_size;

839 840
	__u32 burst_limited;	/* Holds old cwnd when max.burst is applied */

Linus Torvalds's avatar
Linus Torvalds committed
841 842 843 844 845 846 847 848
	/* Destination */
	struct dst_entry *dst;
	/* Source address. */
	union sctp_addr saddr;

	/* Heartbeat interval: The endpoint sends out a Heartbeat chunk to
	 * the destination address every heartbeat interval.
	 */
849
	unsigned long hbinterval;
850 851

	/* SACK delay timeout */
852
	unsigned long sackdelay;
853
	__u32 sackfreq;
Linus Torvalds's avatar
Linus Torvalds committed
854

855 856
	/* When was the last time that we heard from this transport? We use
	 * this to pick new active and retran paths.
Linus Torvalds's avatar
Linus Torvalds committed
857
	 */
858
	ktime_t last_time_heard;
Linus Torvalds's avatar
Linus Torvalds committed
859

860 861 862 863 864
	/* When was the last time that we sent a chunk using this
	 * transport? We use this to check for idle transports
	 */
	unsigned long last_time_sent;

Linus Torvalds's avatar
Linus Torvalds committed
865 866 867 868 869
	/* Last time(in jiffies) when cwnd is reduced due to the congestion
	 * indication based on ECNE chunk.
	 */
	unsigned long last_time_ecne_reduced;

870 871
	/* This is the max_retrans value for the transport and will
	 * be initialized from the assocs value.  This can be changed
872
	 * using the SCTP_SET_PEER_ADDR_PARAMS socket option.
873 874 875
	 */
	__u16 pathmaxrxt;

876 877 878 879 880
	/* This is the partially failed retrans value for the transport
	 * and will be initialized from the assocs value.  This can be changed
	 * using the SCTP_PEER_ADDR_THLDS socket option
	 */
	int pf_retrans;
881 882 883
	/* PMTU	      : The current known path MTU.  */
	__u32 pathmtu;

884
	/* Flags controlling Heartbeat, SACK delay, and Path MTU Discovery. */
885 886
	__u32 param_flags;

887 888 889 890
	/* The number of times INIT has been sent on this transport. */
	int init_sent_count;

	/* state       : The current state of this destination,
891
	 *             : i.e. SCTP_ACTIVE, SCTP_INACTIVE, SCTP_UNKNOWN.
Linus Torvalds's avatar
Linus Torvalds committed
892
	 */
893
	int state;
Linus Torvalds's avatar
Linus Torvalds committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

	/* These are the error stats for this destination.  */

	/* Error count : The current error count for this destination.	*/
	unsigned short error_count;

	/* Per	       : A timer used by each destination.
	 * Destination :
	 * Timer       :
	 *
	 * [Everywhere else in the text this is called T3-rtx. -ed]
	 */
	struct timer_list T3_rtx_timer;

	/* Heartbeat timer is per destination. */
	struct timer_list hb_timer;

911 912 913
	/* Timer to handle ICMP proto unreachable envets */
	struct timer_list proto_unreach_timer;

Linus Torvalds's avatar
Linus Torvalds committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	/* Since we're using per-destination retransmission timers
	 * (see above), we're also using per-destination "transmitted"
	 * queues.  This probably ought to be a private struct
	 * accessible only within the outqueue, but it's not, yet.
	 */
	struct list_head transmitted;

	/* We build bundle-able packets for this transport here.  */
	struct sctp_packet packet;

	/* This is the list of transports that have chunks to send.  */
	struct list_head send_ready;

	/* State information saved for SFR_CACC algorithm. The key
	 * idea in SFR_CACC is to maintain state at the sender on a
	 * per-destination basis when a changeover happens.
	 *	char changeover_active;
	 *	char cycling_changeover;
	 *	__u32 next_tsn_at_change;
	 *	char cacc_saw_newack;
	 */
	struct {
		/* An unsigned integer, which stores the next TSN to be
		 * used by the sender, at the moment of changeover.
		 */
		__u32 next_tsn_at_change;

		/* A flag which indicates the occurrence of a changeover */
		char changeover_active;

		/* A flag which indicates whether the change of primary is
		 * the first switch to this destination address during an
		 * active switch.
		 */
		char cycling_changeover;

		/* A temporary flag, which is used during the processing of
		 * a SACK to estimate the causative TSN(s)'s group.
		 */
		char cacc_saw_newack;
	} cacc;
955 956 957

	/* 64-bit random number sent with heartbeat. */
	__u64 hb_nonce;
958 959

	struct rcu_head rcu;
Linus Torvalds's avatar
Linus Torvalds committed
960 961
};

962
struct sctp_transport *sctp_transport_new(struct net *, const union sctp_addr *,
963
					  gfp_t);
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966 967
void sctp_transport_set_owner(struct sctp_transport *,
			      struct sctp_association *);
void sctp_transport_route(struct sctp_transport *, union sctp_addr *,
			  struct sctp_sock *);
968
void sctp_transport_pmtu(struct sctp_transport *, struct sock *sk);
Linus Torvalds's avatar
Linus Torvalds committed
969
void sctp_transport_free(struct sctp_transport *);
970 971
void sctp_transport_reset_t3_rtx(struct sctp_transport *);
void sctp_transport_reset_hb_timer(struct sctp_transport *);
972
int sctp_transport_hold(struct sctp_transport *);
Linus Torvalds's avatar
Linus Torvalds committed
973 974 975 976
void sctp_transport_put(struct sctp_transport *);
void sctp_transport_update_rto(struct sctp_transport *, __u32);
void sctp_transport_raise_cwnd(struct sctp_transport *, __u32, __u32);
void sctp_transport_lower_cwnd(struct sctp_transport *, sctp_lower_cwnd_t);
977 978
void sctp_transport_burst_limited(struct sctp_transport *);
void sctp_transport_burst_reset(struct sctp_transport *);
Linus Torvalds's avatar
Linus Torvalds committed
979
unsigned long sctp_transport_timeout(struct sctp_transport *);
980
void sctp_transport_reset(struct sctp_transport *);
981
void sctp_transport_update_pmtu(struct sock *, struct sctp_transport *, u32);
982
void sctp_transport_immediate_rtx(struct sctp_transport *);
Linus Torvalds's avatar
Linus Torvalds committed
983 984 985 986 987 988 989 990 991


/* This is the structure we use to queue packets as they come into
 * SCTP.  We write packets to it and read chunks from it.
 */
struct sctp_inq {
	/* This is actually a queue of sctp_chunk each
	 * containing a partially decoded packet.
	 */
992
	struct list_head in_chunk_list;
Linus Torvalds's avatar
Linus Torvalds committed
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	/* This is the packet which is currently off the in queue and is
	 * being worked on through the inbound chunk processing.
	 */
	struct sctp_chunk *in_progress;

	/* This is the delayed task to finish delivering inbound
	 * messages.
	 */
	struct work_struct immediate;
};

void sctp_inq_init(struct sctp_inq *);
void sctp_inq_free(struct sctp_inq *);
void sctp_inq_push(struct sctp_inq *, struct sctp_chunk *packet);
struct sctp_chunk *sctp_inq_pop(struct sctp_inq *);
1008
struct sctp_chunkhdr *sctp_inq_peek(struct sctp_inq *);
David Howells's avatar
David Howells committed
1009
void sctp_inq_set_th_handler(struct sctp_inq *, work_func_t);
Linus Torvalds's avatar
Linus Torvalds committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

/* This is the structure we use to hold outbound chunks.  You push
 * chunks in and they automatically pop out the other end as bundled
 * packets (it calls (*output_handler)()).
 *
 * This structure covers sections 6.3, 6.4, 6.7, 6.8, 6.10, 7., 8.1,
 * and 8.2 of the v13 draft.
 *
 * It handles retransmissions.	The connection to the timeout portion
 * of the state machine is through sctp_..._timeout() and timeout_handler.
 *
 * If you feed it SACKs, it will eat them.
 *
 * If you give it big chunks, it will fragment them.
 *
 * It assigns TSN's to data chunks.  This happens at the last possible
 * instant before transmission.
 *
 * When free()'d, it empties itself out via output_handler().
 */
struct sctp_outq {
	struct sctp_association *asoc;

	/* Data pending that has never been transmitted.  */
1034
	struct list_head out_chunk_list;
Linus Torvalds's avatar
Linus Torvalds committed
1035

1036
	unsigned int out_qlen;	/* Total length of queued data chunks. */
Linus Torvalds's avatar
Linus Torvalds committed
1037 1038

	/* Error of send failed, may used in SCTP_SEND_FAILED event. */
1039
	unsigned int error;
Linus Torvalds's avatar
Linus Torvalds committed
1040 1041

	/* These are control chunks we want to send.  */
1042
	struct list_head control_chunk_list;
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	/* These are chunks that have been sacked but are above the
	 * CTSN, or cumulative tsn ack point.
	 */
	struct list_head sacked;

	/* Put chunks on this list to schedule them for
	 * retransmission.
	 */
	struct list_head retransmit;

	/* Put chunks on this list to save them for FWD TSN processing as
	 * they were abandoned.
	 */
	struct list_head abandoned;

	/* How many unackd bytes do we have in-flight?	*/
	__u32 outstanding_bytes;

1062 1063 1064
	/* Are we doing fast-rtx on this queue */
	char fast_rtx;

Linus Torvalds's avatar
Linus Torvalds committed
1065 1066 1067 1068 1069 1070 1071
	/* Corked? */
	char cork;
};

void sctp_outq_init(struct sctp_association *, struct sctp_outq *);
void sctp_outq_teardown(struct sctp_outq *);
void sctp_outq_free(struct sctp_outq*);
1072
void sctp_outq_tail(struct sctp_outq *, struct sctp_chunk *chunk, gfp_t);
1073
int sctp_outq_sack(struct sctp_outq *, struct sctp_chunk *);
Linus Torvalds's avatar
Linus Torvalds committed
1074 1075 1076 1077 1078 1079
int sctp_outq_is_empty(const struct sctp_outq *);
void sctp_outq_restart(struct sctp_outq *);

void sctp_retransmit(struct sctp_outq *, struct sctp_transport *,
		     sctp_retransmit_reason_t);
void sctp_retransmit_mark(struct sctp_outq *, struct sctp_transport *, __u8);
1080
void sctp_outq_uncork(struct sctp_outq *, gfp_t gfp);
Xin Long's avatar
Xin Long committed
1081 1082
void sctp_prsctp_prune(struct sctp_association *asoc,
		       struct sctp_sndrcvinfo *sinfo, int msg_len);
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084 1085 1086 1087 1088
/* Uncork and flush an outqueue.  */
static inline void sctp_outq_cork(struct sctp_outq *q)
{
	q->cork = 1;
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
/* SCTP skb control block.
 * sctp_input_cb is currently used on rx and sock rx queue
 */
struct sctp_input_cb {
	union {
		struct inet_skb_parm	h4;
#if IS_ENABLED(CONFIG_IPV6)
		struct inet6_skb_parm	h6;
#endif
	} header;
	struct sctp_chunk *chunk;
1100
	struct sctp_af *af;
1101 1102 1103
};
#define SCTP_INPUT_CB(__skb)	((struct sctp_input_cb *)&((__skb)->cb[0]))

1104 1105 1106 1107 1108 1109 1110
static inline const struct sk_buff *sctp_gso_headskb(const struct sk_buff *skb)
{
	const struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;

	return chunk->head_skb ? : skb;
}

Linus Torvalds's avatar
Linus Torvalds committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
/* These bind address data fields common between endpoints and associations */
struct sctp_bind_addr {

	/* RFC 2960 12.1 Parameters necessary for the SCTP instance
	 *
	 * SCTP Port:	The local SCTP port number the endpoint is
	 *		bound to.
	 */
	__u16 port;

	/* RFC 2960 12.1 Parameters necessary for the SCTP instance
	 *
	 * Address List: The list of IP addresses that this instance
	 *	has bound.  This information is passed to one's
	 *	peer(s) in INIT and INIT ACK chunks.
	 */
	struct list_head address_list;
};

void sctp_bind_addr_init(struct sctp_bind_addr *, __u16 port);
void sctp_bind_addr_free(struct sctp_bind_addr *);
1132
int sctp_bind_addr_copy(struct net *net, struct sctp_bind_addr *dest,
Linus Torvalds's avatar
Linus Torvalds committed
1133
			const struct sctp_bind_addr *src,
1134
			sctp_scope_t scope, gfp_t gfp,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
1135
			int flags);
1136 1137 1138
int sctp_bind_addr_dup(struct sctp_bind_addr *dest,
			const struct sctp_bind_addr *src,
			gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1139
int sctp_add_bind_addr(struct sctp_bind_addr *, union sctp_addr *,
1140
		       int new_size, __u8 addr_state, gfp_t gfp);
1141
int sctp_del_bind_addr(struct sctp_bind_addr *, union sctp_addr *);
Linus Torvalds's avatar
Linus Torvalds committed
1142 1143
int sctp_bind_addr_match(struct sctp_bind_addr *, const union sctp_addr *,
			 struct sctp_sock *);
1144 1145
int sctp_bind_addr_conflict(struct sctp_bind_addr *, const union sctp_addr *,
			 struct sctp_sock *, struct sctp_sock *);
1146 1147
int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
			 const union sctp_addr *addr);
Linus Torvalds's avatar
Linus Torvalds committed
1148 1149 1150 1151 1152
union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr	*bp,
					const union sctp_addr	*addrs,
					int			addrcnt,
					struct sctp_sock	*opt);
union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
Alexey Dobriyan's avatar
Alexey Dobriyan committed
1153
					 int *addrs_len,
1154
					 gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1155
int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw, int len,
1156
			   __u16 port, gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158

sctp_scope_t sctp_scope(const union sctp_addr *);
1159
int sctp_in_scope(struct net *net, const union sctp_addr *addr, const sctp_scope_t scope);
1160
int sctp_is_any(struct sock *sk, const union sctp_addr *addr);
1161
int sctp_is_ep_boundall(struct sock *sk);
Linus Torvalds's avatar
Linus Torvalds committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187


/* What type of endpoint?  */
typedef enum {
	SCTP_EP_TYPE_SOCKET,
	SCTP_EP_TYPE_ASSOCIATION,
} sctp_endpoint_type_t;

/*
 * A common base class to bridge the implmentation view of a
 * socket (usually listening) endpoint versus an association's
 * local endpoint.
 * This common structure is useful for several purposes:
 *   1) Common interface for lookup routines.
 *	a) Subfunctions work for either endpoint or association
 *	b) Single interface to lookup allows hiding the lookup lock rather
 *	   than acquiring it externally.
 *   2) Common interface for the inbound chunk handling/state machine.
 *   3) Common object handling routines for reference counting, etc.
 *   4) Disentangle association lookup from endpoint lookup, where we
 *	do not have to find our endpoint to find our association.
 *
 */

struct sctp_ep_common {
	/* Fields to help us manage our entries in the hash tables. */
1188
	struct hlist_node node;
Linus Torvalds's avatar
Linus Torvalds committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	int hashent;

	/* Runtime type information.  What kind of endpoint is this? */
	sctp_endpoint_type_t type;

	/* Some fields to help us manage this object.
	 *   refcnt   - Reference count access to this object.
	 *   dead     - Do not attempt to use this object.
	 */
	atomic_t    refcnt;
1199
	bool	    dead;
Linus Torvalds's avatar
Linus Torvalds committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

	/* What socket does this endpoint belong to?  */
	struct sock *sk;

	/* This is where we receive inbound chunks.  */
	struct sctp_inq	  inqueue;

	/* This substructure includes the defining parameters of the
	 * endpoint:
	 * bind_addr.port is our shared port number.
	 * bind_addr.address_list is our set of local IP addresses.
	 */
	struct sctp_bind_addr bind_addr;
};


/* RFC Section 1.4 Key Terms
 *
 * o SCTP endpoint: The logical sender/receiver of SCTP packets. On a
 *   multi-homed host, an SCTP endpoint is represented to its peers as a
 *   combination of a set of eligible destination transport addresses to
 *   which SCTP packets can be sent and a set of eligible source
 *   transport addresses from which SCTP packets can be received.
 *   All transport addresses used by an SCTP endpoint must use the
 *   same port number, but can use multiple IP addresses. A transport
 *   address used by an SCTP endpoint must not be used by another
 *   SCTP endpoint. In other words, a transport address is unique
 *   to an SCTP endpoint.
 *
 * From an implementation perspective, each socket has one of these.
 * A TCP-style socket will have exactly one association on one of
 * these.  An UDP-style socket will have multiple associations hanging
 * off one of these.
 */

struct sctp_endpoint {
	/* Common substructure for endpoint and association. */
	struct sctp_ep_common base;

	/* Associations: A list of current associations and mappings
	 *	      to the data consumers for each association. This
	 *	      may be in the form of a hash table or other
	 *	      implementation dependent structure. The data
	 *	      consumers may be process identification
	 *	      information such as file descriptors, named pipe
	 *	      pointer, or table pointers dependent on how SCTP
	 *	      is implemented.
	 */
	/* This is really a list of struct sctp_association entries. */
	struct list_head asocs;

	/* Secret Key: A secret key used by this endpoint to compute
	 *	      the MAC.	This SHOULD be a cryptographic quality
	 *	      random number with a sufficient length.
	 *	      Discussion in [RFC1750] can be helpful in
	 *	      selection of the key.
	 */
1257
	__u8 secret_key[SCTP_SECRET_SIZE];
Linus Torvalds's avatar
Linus Torvalds committed
1258

1259 1260 1261 1262 1263 1264
 	/* digest:  This is a digest of the sctp cookie.  This field is
 	 * 	    only used on the receive path when we try to validate
 	 * 	    that the cookie has not been tampered with.  We put
 	 * 	    this here so we pre-allocate this once and can re-use
 	 * 	    on every receive.
 	 */
Vlad Yasevich's avatar
Vlad Yasevich committed
1265
 	__u8 *digest;
1266
 
1267 1268
	/* sendbuf acct. policy.	*/
	__u32 sndbuf_policy;
1269 1270 1271

	/* rcvbuf acct. policy.	*/
	__u32 rcvbuf_policy;
1272 1273 1274 1275

	/* SCTP AUTH: array of the HMACs that will be allocated
	 * we need this per association so that we don't serialize
	 */
Herbert Xu's avatar
Herbert Xu committed
1276
	struct crypto_shash **auth_hmacs;
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

	/* SCTP-AUTH: hmacs for the endpoint encoded into parameter */
	 struct sctp_hmac_algo_param *auth_hmacs_list;

	/* SCTP-AUTH: chunks to authenticate encoded into parameter */
	struct sctp_chunks_param *auth_chunk_list;

	/* SCTP-AUTH: endpoint shared keys */
	struct list_head endpoint_shared_keys;
	__u16 active_key_id;
1287 1288
	__u8  auth_enable:1,
	      prsctp_enable:1;
Linus Torvalds's avatar
Linus Torvalds committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
};

/* Recover the outter endpoint structure. */
static inline struct sctp_endpoint *sctp_ep(struct sctp_ep_common *base)
{
	struct sctp_endpoint *ep;

	ep = container_of(base, struct sctp_endpoint, base);
	return ep;
}

/* These are function signatures for manipulating endpoints.  */
1301
struct sctp_endpoint *sctp_endpoint_new(struct sock *, gfp_t);
Linus Torvalds's avatar
Linus Torvalds committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
void sctp_endpoint_free(struct sctp_endpoint *);
void sctp_endpoint_put(struct sctp_endpoint *);
void sctp_endpoint_hold(struct sctp_endpoint *);
void sctp_endpoint_add_asoc(struct sctp_endpoint *, struct sctp_association *);
struct sctp_association *sctp_endpoint_lookup_assoc(
	const struct sctp_endpoint *ep,
	const union sctp_addr *paddr,
	struct sctp_transport **);
int sctp_endpoint_is_peeled_off(struct sctp_endpoint *,
				const union sctp_addr *);
struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *,
1313
					struct net *, const union sctp_addr *);
1314
int sctp_has_association(struct net *net, const union sctp_addr *laddr,
Linus Torvalds's avatar
Linus Torvalds committed
1315 1316
			 const union sctp_addr *paddr);

1317 1318
int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
		     const struct sctp_association *asoc,
1319 1320
		     sctp_cid_t, sctp_init_chunk_t *peer_init,
		     struct sctp_chunk *chunk, struct sctp_chunk **err_chunk);
1321
int sctp_process_init(struct sctp_association *, struct sctp_chunk *chunk,
Linus Torvalds's avatar
Linus Torvalds committed
1322
		      const union sctp_addr *peer,
1323
		      sctp_init_chunk_t *init, gfp_t gfp);
Linus Torvalds's avatar
Linus Torvalds committed
1324 1325 1326
__u32 sctp_generate_tag(const struct sctp_endpoint *);
__u32 sctp_generate_tsn(const struct sctp_endpoint *);

1327 1328 1329 1330 1331 1332 1333
struct sctp_inithdr_host {
	__u32 init_tag;
	__u32 a_rwnd;
	__u16 num_outbound_streams;
	__u16 num_inbound_streams;
	__u32 initial_tsn;
};
Linus Torvalds's avatar
Linus Torvalds committed
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352