162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/* SCTP kernel implementation
362306a36Sopenharmony_ci * Copyright (c) 1999-2000 Cisco, Inc.
462306a36Sopenharmony_ci * Copyright (c) 1999-2001 Motorola, Inc.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This file is part of the SCTP kernel implementation
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * These functions implement the SCTP primitive functions from Section 10.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Note that the descriptions from the specification are USER level
1162306a36Sopenharmony_ci * functions--this file is the functions which populate the struct proto
1262306a36Sopenharmony_ci * for SCTP which is the BOTTOM of the sockets interface.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * Please send any bug reports or fixes you make to the
1562306a36Sopenharmony_ci * email address(es):
1662306a36Sopenharmony_ci *    lksctp developers <linux-sctp@vger.kernel.org>
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * Written or modified by:
1962306a36Sopenharmony_ci *    La Monte H.P. Yarroll <piggy@acm.org>
2062306a36Sopenharmony_ci *    Narasimha Budihal     <narasimha@refcode.org>
2162306a36Sopenharmony_ci *    Karl Knutson          <karl@athena.chicago.il.us>
2262306a36Sopenharmony_ci *    Ardelle Fan	    <ardelle.fan@intel.com>
2362306a36Sopenharmony_ci *    Kevin Gao             <kevin.gao@intel.com>
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <linux/types.h>
2762306a36Sopenharmony_ci#include <linux/list.h> /* For struct list_head */
2862306a36Sopenharmony_ci#include <linux/socket.h>
2962306a36Sopenharmony_ci#include <linux/ip.h>
3062306a36Sopenharmony_ci#include <linux/time.h> /* For struct timeval */
3162306a36Sopenharmony_ci#include <linux/gfp.h>
3262306a36Sopenharmony_ci#include <net/sock.h>
3362306a36Sopenharmony_ci#include <net/sctp/sctp.h>
3462306a36Sopenharmony_ci#include <net/sctp/sm.h>
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define DECLARE_PRIMITIVE(name) \
3762306a36Sopenharmony_ci/* This is called in the code as sctp_primitive_ ## name.  */ \
3862306a36Sopenharmony_ciint sctp_primitive_ ## name(struct net *net, struct sctp_association *asoc, \
3962306a36Sopenharmony_ci			    void *arg) { \
4062306a36Sopenharmony_ci	int error = 0; \
4162306a36Sopenharmony_ci	enum sctp_event_type event_type; union sctp_subtype subtype; \
4262306a36Sopenharmony_ci	enum sctp_state state; \
4362306a36Sopenharmony_ci	struct sctp_endpoint *ep; \
4462306a36Sopenharmony_ci	\
4562306a36Sopenharmony_ci	event_type = SCTP_EVENT_T_PRIMITIVE; \
4662306a36Sopenharmony_ci	subtype = SCTP_ST_PRIMITIVE(SCTP_PRIMITIVE_ ## name); \
4762306a36Sopenharmony_ci	state = asoc ? asoc->state : SCTP_STATE_CLOSED; \
4862306a36Sopenharmony_ci	ep = asoc ? asoc->ep : NULL; \
4962306a36Sopenharmony_ci	\
5062306a36Sopenharmony_ci	error = sctp_do_sm(net, event_type, subtype, state, ep, asoc,	\
5162306a36Sopenharmony_ci			   arg, GFP_KERNEL); \
5262306a36Sopenharmony_ci	return error; \
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* 10.1 ULP-to-SCTP
5662306a36Sopenharmony_ci * B) Associate
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
5962306a36Sopenharmony_ci *         outbound stream count)
6062306a36Sopenharmony_ci * -> association id [,destination transport addr list] [,outbound stream
6162306a36Sopenharmony_ci *    count]
6262306a36Sopenharmony_ci *
6362306a36Sopenharmony_ci * This primitive allows the upper layer to initiate an association to a
6462306a36Sopenharmony_ci * specific peer endpoint.
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * This version assumes that asoc is fully populated with the initial
6762306a36Sopenharmony_ci * parameters.  We then return a traditional kernel indicator of
6862306a36Sopenharmony_ci * success or failure.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/* This is called in the code as sctp_primitive_ASSOCIATE.  */
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciDECLARE_PRIMITIVE(ASSOCIATE)
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/* 10.1 ULP-to-SCTP
7662306a36Sopenharmony_ci * C) Shutdown
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * Format: SHUTDOWN(association id)
7962306a36Sopenharmony_ci * -> result
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci * Gracefully closes an association. Any locally queued user data
8262306a36Sopenharmony_ci * will be delivered to the peer. The association will be terminated only
8362306a36Sopenharmony_ci * after the peer acknowledges all the SCTP packets sent.  A success code
8462306a36Sopenharmony_ci * will be returned on successful termination of the association. If
8562306a36Sopenharmony_ci * attempting to terminate the association results in a failure, an error
8662306a36Sopenharmony_ci * code shall be returned.
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciDECLARE_PRIMITIVE(SHUTDOWN);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/* 10.1 ULP-to-SCTP
9262306a36Sopenharmony_ci * C) Abort
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * Format: Abort(association id [, cause code])
9562306a36Sopenharmony_ci * -> result
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * Ungracefully closes an association. Any locally queued user data
9862306a36Sopenharmony_ci * will be discarded and an ABORT chunk is sent to the peer. A success
9962306a36Sopenharmony_ci * code will be returned on successful abortion of the association. If
10062306a36Sopenharmony_ci * attempting to abort the association results in a failure, an error
10162306a36Sopenharmony_ci * code shall be returned.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ciDECLARE_PRIMITIVE(ABORT);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci/* 10.1 ULP-to-SCTP
10762306a36Sopenharmony_ci * E) Send
10862306a36Sopenharmony_ci *
10962306a36Sopenharmony_ci * Format: SEND(association id, buffer address, byte count [,context]
11062306a36Sopenharmony_ci *         [,stream id] [,life time] [,destination transport address]
11162306a36Sopenharmony_ci *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
11262306a36Sopenharmony_ci * -> result
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * This is the main method to send user data via SCTP.
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * Mandatory attributes:
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci *  o association id - local handle to the SCTP association
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci *  o buffer address - the location where the user message to be
12162306a36Sopenharmony_ci *    transmitted is stored;
12262306a36Sopenharmony_ci *
12362306a36Sopenharmony_ci *  o byte count - The size of the user data in number of bytes;
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * Optional attributes:
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci *  o context - an optional 32 bit integer that will be carried in the
12862306a36Sopenharmony_ci *    sending failure notification to the ULP if the transportation of
12962306a36Sopenharmony_ci *    this User Message fails.
13062306a36Sopenharmony_ci *
13162306a36Sopenharmony_ci *  o stream id - to indicate which stream to send the data on. If not
13262306a36Sopenharmony_ci *    specified, stream 0 will be used.
13362306a36Sopenharmony_ci *
13462306a36Sopenharmony_ci *  o life time - specifies the life time of the user data. The user data
13562306a36Sopenharmony_ci *    will not be sent by SCTP after the life time expires. This
13662306a36Sopenharmony_ci *    parameter can be used to avoid efforts to transmit stale
13762306a36Sopenharmony_ci *    user messages. SCTP notifies the ULP if the data cannot be
13862306a36Sopenharmony_ci *    initiated to transport (i.e. sent to the destination via SCTP's
13962306a36Sopenharmony_ci *    send primitive) within the life time variable. However, the
14062306a36Sopenharmony_ci *    user data will be transmitted if SCTP has attempted to transmit a
14162306a36Sopenharmony_ci *    chunk before the life time expired.
14262306a36Sopenharmony_ci *
14362306a36Sopenharmony_ci *  o destination transport address - specified as one of the destination
14462306a36Sopenharmony_ci *    transport addresses of the peer endpoint to which this packet
14562306a36Sopenharmony_ci *    should be sent. Whenever possible, SCTP should use this destination
14662306a36Sopenharmony_ci *    transport address for sending the packets, instead of the current
14762306a36Sopenharmony_ci *    primary path.
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci *  o unorder flag - this flag, if present, indicates that the user
15062306a36Sopenharmony_ci *    would like the data delivered in an unordered fashion to the peer
15162306a36Sopenharmony_ci *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
15262306a36Sopenharmony_ci *    message).
15362306a36Sopenharmony_ci *
15462306a36Sopenharmony_ci *  o no-bundle flag - instructs SCTP not to bundle this user data with
15562306a36Sopenharmony_ci *    other outbound DATA chunks. SCTP MAY still bundle even when
15662306a36Sopenharmony_ci *    this flag is present, when faced with network congestion.
15762306a36Sopenharmony_ci *
15862306a36Sopenharmony_ci *  o payload protocol-id - A 32 bit unsigned integer that is to be
15962306a36Sopenharmony_ci *    passed to the peer indicating the type of payload protocol data
16062306a36Sopenharmony_ci *    being transmitted. This value is passed as opaque data by SCTP.
16162306a36Sopenharmony_ci */
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ciDECLARE_PRIMITIVE(SEND);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/* 10.1 ULP-to-SCTP
16662306a36Sopenharmony_ci * J) Request Heartbeat
16762306a36Sopenharmony_ci *
16862306a36Sopenharmony_ci * Format: REQUESTHEARTBEAT(association id, destination transport address)
16962306a36Sopenharmony_ci *
17062306a36Sopenharmony_ci * -> result
17162306a36Sopenharmony_ci *
17262306a36Sopenharmony_ci * Instructs the local endpoint to perform a HeartBeat on the specified
17362306a36Sopenharmony_ci * destination transport address of the given association. The returned
17462306a36Sopenharmony_ci * result should indicate whether the transmission of the HEARTBEAT
17562306a36Sopenharmony_ci * chunk to the destination address is successful.
17662306a36Sopenharmony_ci *
17762306a36Sopenharmony_ci * Mandatory attributes:
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * o association id - local handle to the SCTP association
18062306a36Sopenharmony_ci *
18162306a36Sopenharmony_ci * o destination transport address - the transport address of the
18262306a36Sopenharmony_ci *   association on which a heartbeat should be issued.
18362306a36Sopenharmony_ci */
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciDECLARE_PRIMITIVE(REQUESTHEARTBEAT);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/* ADDIP
18862306a36Sopenharmony_ci* 3.1.1 Address Configuration Change Chunk (ASCONF)
18962306a36Sopenharmony_ci*
19062306a36Sopenharmony_ci* This chunk is used to communicate to the remote endpoint one of the
19162306a36Sopenharmony_ci* configuration change requests that MUST be acknowledged.  The
19262306a36Sopenharmony_ci* information carried in the ASCONF Chunk uses the form of a
19362306a36Sopenharmony_ci* Type-Length-Value (TLV), as described in "3.2.1 Optional/
19462306a36Sopenharmony_ci* Variable-length Parameter Format" in RFC2960 [5], forall variable
19562306a36Sopenharmony_ci* parameters.
19662306a36Sopenharmony_ci*/
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciDECLARE_PRIMITIVE(ASCONF);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/* RE-CONFIG 5.1 */
20162306a36Sopenharmony_ciDECLARE_PRIMITIVE(RECONF);
202