18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* SCTP kernel implementation
38c2ecf20Sopenharmony_ci * Copyright (c) 1999-2000 Cisco, Inc.
48c2ecf20Sopenharmony_ci * Copyright (c) 1999-2001 Motorola, Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is part of the SCTP kernel implementation
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * These functions implement the SCTP primitive functions from Section 10.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Note that the descriptions from the specification are USER level
118c2ecf20Sopenharmony_ci * functions--this file is the functions which populate the struct proto
128c2ecf20Sopenharmony_ci * for SCTP which is the BOTTOM of the sockets interface.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Please send any bug reports or fixes you make to the
158c2ecf20Sopenharmony_ci * email address(es):
168c2ecf20Sopenharmony_ci *    lksctp developers <linux-sctp@vger.kernel.org>
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Written or modified by:
198c2ecf20Sopenharmony_ci *    La Monte H.P. Yarroll <piggy@acm.org>
208c2ecf20Sopenharmony_ci *    Narasimha Budihal     <narasimha@refcode.org>
218c2ecf20Sopenharmony_ci *    Karl Knutson          <karl@athena.chicago.il.us>
228c2ecf20Sopenharmony_ci *    Ardelle Fan	    <ardelle.fan@intel.com>
238c2ecf20Sopenharmony_ci *    Kevin Gao             <kevin.gao@intel.com>
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/types.h>
278c2ecf20Sopenharmony_ci#include <linux/list.h> /* For struct list_head */
288c2ecf20Sopenharmony_ci#include <linux/socket.h>
298c2ecf20Sopenharmony_ci#include <linux/ip.h>
308c2ecf20Sopenharmony_ci#include <linux/time.h> /* For struct timeval */
318c2ecf20Sopenharmony_ci#include <linux/gfp.h>
328c2ecf20Sopenharmony_ci#include <net/sock.h>
338c2ecf20Sopenharmony_ci#include <net/sctp/sctp.h>
348c2ecf20Sopenharmony_ci#include <net/sctp/sm.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define DECLARE_PRIMITIVE(name) \
378c2ecf20Sopenharmony_ci/* This is called in the code as sctp_primitive_ ## name.  */ \
388c2ecf20Sopenharmony_ciint sctp_primitive_ ## name(struct net *net, struct sctp_association *asoc, \
398c2ecf20Sopenharmony_ci			    void *arg) { \
408c2ecf20Sopenharmony_ci	int error = 0; \
418c2ecf20Sopenharmony_ci	enum sctp_event_type event_type; union sctp_subtype subtype; \
428c2ecf20Sopenharmony_ci	enum sctp_state state; \
438c2ecf20Sopenharmony_ci	struct sctp_endpoint *ep; \
448c2ecf20Sopenharmony_ci	\
458c2ecf20Sopenharmony_ci	event_type = SCTP_EVENT_T_PRIMITIVE; \
468c2ecf20Sopenharmony_ci	subtype = SCTP_ST_PRIMITIVE(SCTP_PRIMITIVE_ ## name); \
478c2ecf20Sopenharmony_ci	state = asoc ? asoc->state : SCTP_STATE_CLOSED; \
488c2ecf20Sopenharmony_ci	ep = asoc ? asoc->ep : NULL; \
498c2ecf20Sopenharmony_ci	\
508c2ecf20Sopenharmony_ci	error = sctp_do_sm(net, event_type, subtype, state, ep, asoc,	\
518c2ecf20Sopenharmony_ci			   arg, GFP_KERNEL); \
528c2ecf20Sopenharmony_ci	return error; \
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* 10.1 ULP-to-SCTP
568c2ecf20Sopenharmony_ci * B) Associate
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
598c2ecf20Sopenharmony_ci *         outbound stream count)
608c2ecf20Sopenharmony_ci * -> association id [,destination transport addr list] [,outbound stream
618c2ecf20Sopenharmony_ci *    count]
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * This primitive allows the upper layer to initiate an association to a
648c2ecf20Sopenharmony_ci * specific peer endpoint.
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * This version assumes that asoc is fully populated with the initial
678c2ecf20Sopenharmony_ci * parameters.  We then return a traditional kernel indicator of
688c2ecf20Sopenharmony_ci * success or failure.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* This is called in the code as sctp_primitive_ASSOCIATE.  */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(ASSOCIATE)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* 10.1 ULP-to-SCTP
768c2ecf20Sopenharmony_ci * C) Shutdown
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * Format: SHUTDOWN(association id)
798c2ecf20Sopenharmony_ci * -> result
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * Gracefully closes an association. Any locally queued user data
828c2ecf20Sopenharmony_ci * will be delivered to the peer. The association will be terminated only
838c2ecf20Sopenharmony_ci * after the peer acknowledges all the SCTP packets sent.  A success code
848c2ecf20Sopenharmony_ci * will be returned on successful termination of the association. If
858c2ecf20Sopenharmony_ci * attempting to terminate the association results in a failure, an error
868c2ecf20Sopenharmony_ci * code shall be returned.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(SHUTDOWN);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* 10.1 ULP-to-SCTP
928c2ecf20Sopenharmony_ci * C) Abort
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Format: Abort(association id [, cause code])
958c2ecf20Sopenharmony_ci * -> result
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * Ungracefully closes an association. Any locally queued user data
988c2ecf20Sopenharmony_ci * will be discarded and an ABORT chunk is sent to the peer. A success
998c2ecf20Sopenharmony_ci * code will be returned on successful abortion of the association. If
1008c2ecf20Sopenharmony_ci * attempting to abort the association results in a failure, an error
1018c2ecf20Sopenharmony_ci * code shall be returned.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(ABORT);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* 10.1 ULP-to-SCTP
1078c2ecf20Sopenharmony_ci * E) Send
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * Format: SEND(association id, buffer address, byte count [,context]
1108c2ecf20Sopenharmony_ci *         [,stream id] [,life time] [,destination transport address]
1118c2ecf20Sopenharmony_ci *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
1128c2ecf20Sopenharmony_ci * -> result
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * This is the main method to send user data via SCTP.
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * Mandatory attributes:
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci *  o association id - local handle to the SCTP association
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci *  o buffer address - the location where the user message to be
1218c2ecf20Sopenharmony_ci *    transmitted is stored;
1228c2ecf20Sopenharmony_ci *
1238c2ecf20Sopenharmony_ci *  o byte count - The size of the user data in number of bytes;
1248c2ecf20Sopenharmony_ci *
1258c2ecf20Sopenharmony_ci * Optional attributes:
1268c2ecf20Sopenharmony_ci *
1278c2ecf20Sopenharmony_ci *  o context - an optional 32 bit integer that will be carried in the
1288c2ecf20Sopenharmony_ci *    sending failure notification to the ULP if the transportation of
1298c2ecf20Sopenharmony_ci *    this User Message fails.
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci *  o stream id - to indicate which stream to send the data on. If not
1328c2ecf20Sopenharmony_ci *    specified, stream 0 will be used.
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci *  o life time - specifies the life time of the user data. The user data
1358c2ecf20Sopenharmony_ci *    will not be sent by SCTP after the life time expires. This
1368c2ecf20Sopenharmony_ci *    parameter can be used to avoid efforts to transmit stale
1378c2ecf20Sopenharmony_ci *    user messages. SCTP notifies the ULP if the data cannot be
1388c2ecf20Sopenharmony_ci *    initiated to transport (i.e. sent to the destination via SCTP's
1398c2ecf20Sopenharmony_ci *    send primitive) within the life time variable. However, the
1408c2ecf20Sopenharmony_ci *    user data will be transmitted if SCTP has attempted to transmit a
1418c2ecf20Sopenharmony_ci *    chunk before the life time expired.
1428c2ecf20Sopenharmony_ci *
1438c2ecf20Sopenharmony_ci *  o destination transport address - specified as one of the destination
1448c2ecf20Sopenharmony_ci *    transport addresses of the peer endpoint to which this packet
1458c2ecf20Sopenharmony_ci *    should be sent. Whenever possible, SCTP should use this destination
1468c2ecf20Sopenharmony_ci *    transport address for sending the packets, instead of the current
1478c2ecf20Sopenharmony_ci *    primary path.
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci *  o unorder flag - this flag, if present, indicates that the user
1508c2ecf20Sopenharmony_ci *    would like the data delivered in an unordered fashion to the peer
1518c2ecf20Sopenharmony_ci *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
1528c2ecf20Sopenharmony_ci *    message).
1538c2ecf20Sopenharmony_ci *
1548c2ecf20Sopenharmony_ci *  o no-bundle flag - instructs SCTP not to bundle this user data with
1558c2ecf20Sopenharmony_ci *    other outbound DATA chunks. SCTP MAY still bundle even when
1568c2ecf20Sopenharmony_ci *    this flag is present, when faced with network congestion.
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci *  o payload protocol-id - A 32 bit unsigned integer that is to be
1598c2ecf20Sopenharmony_ci *    passed to the peer indicating the type of payload protocol data
1608c2ecf20Sopenharmony_ci *    being transmitted. This value is passed as opaque data by SCTP.
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(SEND);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* 10.1 ULP-to-SCTP
1668c2ecf20Sopenharmony_ci * J) Request Heartbeat
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * Format: REQUESTHEARTBEAT(association id, destination transport address)
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * -> result
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * Instructs the local endpoint to perform a HeartBeat on the specified
1738c2ecf20Sopenharmony_ci * destination transport address of the given association. The returned
1748c2ecf20Sopenharmony_ci * result should indicate whether the transmission of the HEARTBEAT
1758c2ecf20Sopenharmony_ci * chunk to the destination address is successful.
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * Mandatory attributes:
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * o association id - local handle to the SCTP association
1808c2ecf20Sopenharmony_ci *
1818c2ecf20Sopenharmony_ci * o destination transport address - the transport address of the
1828c2ecf20Sopenharmony_ci *   association on which a heartbeat should be issued.
1838c2ecf20Sopenharmony_ci */
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(REQUESTHEARTBEAT);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* ADDIP
1888c2ecf20Sopenharmony_ci* 3.1.1 Address Configuration Change Chunk (ASCONF)
1898c2ecf20Sopenharmony_ci*
1908c2ecf20Sopenharmony_ci* This chunk is used to communicate to the remote endpoint one of the
1918c2ecf20Sopenharmony_ci* configuration change requests that MUST be acknowledged.  The
1928c2ecf20Sopenharmony_ci* information carried in the ASCONF Chunk uses the form of a
1938c2ecf20Sopenharmony_ci* Type-Length-Value (TLV), as described in "3.2.1 Optional/
1948c2ecf20Sopenharmony_ci* Variable-length Parameter Format" in RFC2960 [5], forall variable
1958c2ecf20Sopenharmony_ci* parameters.
1968c2ecf20Sopenharmony_ci*/
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(ASCONF);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* RE-CONFIG 5.1 */
2018c2ecf20Sopenharmony_ciDECLARE_PRIMITIVE(RECONF);
202