18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/* SCTP kernel Implementation
38c2ecf20Sopenharmony_ci * (C) Copyright IBM Corp. 2001, 2004
48c2ecf20Sopenharmony_ci * Copyright (C) 1999-2001 Cisco, Motorola
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is part of the SCTP kernel implementation
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * These are the definitions needed for the command object.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Please send any bug reports or fixes you make to the
118c2ecf20Sopenharmony_ci * email address(es):
128c2ecf20Sopenharmony_ci *    lksctp developers <linux-sctp@vger.kernel.org>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Written or modified by:
158c2ecf20Sopenharmony_ci *   La Monte H.P. Yarroll <piggy@acm.org>
168c2ecf20Sopenharmony_ci *   Karl Knutson <karl@athena.chicago.il.us>
178c2ecf20Sopenharmony_ci *   Ardelle Fan <ardelle.fan@intel.com>
188c2ecf20Sopenharmony_ci *   Sridhar Samudrala <sri@us.ibm.com>
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#ifndef __net_sctp_command_h__
228c2ecf20Sopenharmony_ci#define __net_sctp_command_h__
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <net/sctp/constants.h>
258c2ecf20Sopenharmony_ci#include <net/sctp/structs.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cienum sctp_verb {
298c2ecf20Sopenharmony_ci	SCTP_CMD_NOP = 0,	/* Do nothing. */
308c2ecf20Sopenharmony_ci	SCTP_CMD_NEW_ASOC,	/* Register a new association.  */
318c2ecf20Sopenharmony_ci	SCTP_CMD_DELETE_TCB,	/* Delete the current association. */
328c2ecf20Sopenharmony_ci	SCTP_CMD_NEW_STATE,	/* Enter a new state.  */
338c2ecf20Sopenharmony_ci	SCTP_CMD_REPORT_TSN,	/* Record the arrival of a TSN.  */
348c2ecf20Sopenharmony_ci	SCTP_CMD_GEN_SACK,	/* Send a Selective ACK (maybe).  */
358c2ecf20Sopenharmony_ci	SCTP_CMD_PROCESS_SACK,	/* Process an inbound SACK.  */
368c2ecf20Sopenharmony_ci	SCTP_CMD_GEN_INIT_ACK,	/* Generate an INIT ACK chunk.  */
378c2ecf20Sopenharmony_ci	SCTP_CMD_PEER_INIT,	/* Process a INIT from the peer.  */
388c2ecf20Sopenharmony_ci	SCTP_CMD_GEN_COOKIE_ECHO, /* Generate a COOKIE ECHO chunk. */
398c2ecf20Sopenharmony_ci	SCTP_CMD_CHUNK_ULP,	/* Send a chunk to the sockets layer.  */
408c2ecf20Sopenharmony_ci	SCTP_CMD_EVENT_ULP,	/* Send a notification to the sockets layer. */
418c2ecf20Sopenharmony_ci	SCTP_CMD_REPLY,		/* Send a chunk to our peer.  */
428c2ecf20Sopenharmony_ci	SCTP_CMD_SEND_PKT,	/* Send a full packet to our peer.  */
438c2ecf20Sopenharmony_ci	SCTP_CMD_RETRAN,	/* Mark a transport for retransmission.  */
448c2ecf20Sopenharmony_ci	SCTP_CMD_ECN_CE,        /* Do delayed CE processing.   */
458c2ecf20Sopenharmony_ci	SCTP_CMD_ECN_ECNE,	/* Do delayed ECNE processing. */
468c2ecf20Sopenharmony_ci	SCTP_CMD_ECN_CWR,	/* Do delayed CWR processing.  */
478c2ecf20Sopenharmony_ci	SCTP_CMD_TIMER_START,	/* Start a timer.  */
488c2ecf20Sopenharmony_ci	SCTP_CMD_TIMER_START_ONCE, /* Start a timer once */
498c2ecf20Sopenharmony_ci	SCTP_CMD_TIMER_RESTART,	/* Restart a timer. */
508c2ecf20Sopenharmony_ci	SCTP_CMD_TIMER_STOP,	/* Stop a timer. */
518c2ecf20Sopenharmony_ci	SCTP_CMD_INIT_CHOOSE_TRANSPORT, /* Choose transport for an INIT. */
528c2ecf20Sopenharmony_ci	SCTP_CMD_INIT_COUNTER_RESET, /* Reset init counter. */
538c2ecf20Sopenharmony_ci	SCTP_CMD_INIT_COUNTER_INC,   /* Increment init counter. */
548c2ecf20Sopenharmony_ci	SCTP_CMD_INIT_RESTART,  /* High level, do init timer work. */
558c2ecf20Sopenharmony_ci	SCTP_CMD_COOKIEECHO_RESTART,  /* High level, do cookie-echo timer work. */
568c2ecf20Sopenharmony_ci	SCTP_CMD_INIT_FAILED,   /* High level, do init failure work. */
578c2ecf20Sopenharmony_ci	SCTP_CMD_REPORT_DUP,	/* Report a duplicate TSN.  */
588c2ecf20Sopenharmony_ci	SCTP_CMD_STRIKE,	/* Mark a strike against a transport.  */
598c2ecf20Sopenharmony_ci	SCTP_CMD_HB_TIMERS_START,    /* Start the heartbeat timers. */
608c2ecf20Sopenharmony_ci	SCTP_CMD_HB_TIMER_UPDATE,    /* Update a heartbeat timers.  */
618c2ecf20Sopenharmony_ci	SCTP_CMD_HB_TIMERS_STOP,     /* Stop the heartbeat timers.  */
628c2ecf20Sopenharmony_ci	SCTP_CMD_TRANSPORT_HB_SENT,  /* Reset the status of a transport. */
638c2ecf20Sopenharmony_ci	SCTP_CMD_TRANSPORT_IDLE,     /* Do manipulations on idle transport */
648c2ecf20Sopenharmony_ci	SCTP_CMD_TRANSPORT_ON,       /* Mark the transport as active. */
658c2ecf20Sopenharmony_ci	SCTP_CMD_REPORT_ERROR,   /* Pass this error back out of the sm. */
668c2ecf20Sopenharmony_ci	SCTP_CMD_REPORT_BAD_TAG, /* Verification tags didn't match. */
678c2ecf20Sopenharmony_ci	SCTP_CMD_PROCESS_CTSN,   /* Sideeffect from shutdown. */
688c2ecf20Sopenharmony_ci	SCTP_CMD_ASSOC_FAILED,	 /* Handle association failure. */
698c2ecf20Sopenharmony_ci	SCTP_CMD_DISCARD_PACKET, /* Discard the whole packet. */
708c2ecf20Sopenharmony_ci	SCTP_CMD_GEN_SHUTDOWN,   /* Generate a SHUTDOWN chunk. */
718c2ecf20Sopenharmony_ci	SCTP_CMD_UPDATE_ASSOC,   /* Update association information. */
728c2ecf20Sopenharmony_ci	SCTP_CMD_PURGE_OUTQUEUE, /* Purge all data waiting to be sent. */
738c2ecf20Sopenharmony_ci	SCTP_CMD_SETUP_T2,       /* Hi-level, setup T2-shutdown parms.  */
748c2ecf20Sopenharmony_ci	SCTP_CMD_RTO_PENDING,	 /* Set transport's rto_pending. */
758c2ecf20Sopenharmony_ci	SCTP_CMD_PART_DELIVER,	 /* Partial data delivery considerations. */
768c2ecf20Sopenharmony_ci	SCTP_CMD_RENEGE,         /* Renege data on an association. */
778c2ecf20Sopenharmony_ci	SCTP_CMD_SETUP_T4,	 /* ADDIP, setup T4 RTO timer parms. */
788c2ecf20Sopenharmony_ci	SCTP_CMD_PROCESS_OPERR,  /* Process an ERROR chunk. */
798c2ecf20Sopenharmony_ci	SCTP_CMD_REPORT_FWDTSN,	 /* Report new cumulative TSN Ack. */
808c2ecf20Sopenharmony_ci	SCTP_CMD_PROCESS_FWDTSN, /* Skips were reported, so process further. */
818c2ecf20Sopenharmony_ci	SCTP_CMD_CLEAR_INIT_TAG, /* Clears association peer's inittag. */
828c2ecf20Sopenharmony_ci	SCTP_CMD_DEL_NON_PRIMARY, /* Removes non-primary peer transports. */
838c2ecf20Sopenharmony_ci	SCTP_CMD_T3_RTX_TIMERS_STOP, /* Stops T3-rtx pending timers */
848c2ecf20Sopenharmony_ci	SCTP_CMD_FORCE_PRIM_RETRAN,  /* Forces retrans. over primary path. */
858c2ecf20Sopenharmony_ci	SCTP_CMD_SET_SK_ERR,	 /* Set sk_err */
868c2ecf20Sopenharmony_ci	SCTP_CMD_ASSOC_CHANGE,	 /* generate and send assoc_change event */
878c2ecf20Sopenharmony_ci	SCTP_CMD_ADAPTATION_IND, /* generate and send adaptation event */
888c2ecf20Sopenharmony_ci	SCTP_CMD_PEER_NO_AUTH,   /* generate and send authentication event */
898c2ecf20Sopenharmony_ci	SCTP_CMD_ASSOC_SHKEY,    /* generate the association shared keys */
908c2ecf20Sopenharmony_ci	SCTP_CMD_T1_RETRAN,	 /* Mark for retransmission after T1 timeout  */
918c2ecf20Sopenharmony_ci	SCTP_CMD_UPDATE_INITTAG, /* Update peer inittag */
928c2ecf20Sopenharmony_ci	SCTP_CMD_SEND_MSG,	 /* Send the whole use message */
938c2ecf20Sopenharmony_ci	SCTP_CMD_PURGE_ASCONF_QUEUE, /* Purge all asconf queues.*/
948c2ecf20Sopenharmony_ci	SCTP_CMD_SET_ASOC,	 /* Restore association context */
958c2ecf20Sopenharmony_ci	SCTP_CMD_LAST
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* How many commands can you put in an struct sctp_cmd_seq?
998c2ecf20Sopenharmony_ci * This is a rather arbitrary number, ideally derived from a careful
1008c2ecf20Sopenharmony_ci * analysis of the state functions, but in reality just taken from
1018c2ecf20Sopenharmony_ci * thin air in the hopes othat we don't trigger a kernel panic.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ci#define SCTP_MAX_NUM_COMMANDS 20
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciunion sctp_arg {
1068c2ecf20Sopenharmony_ci	void *zero_all;	/* Set to NULL to clear the entire union */
1078c2ecf20Sopenharmony_ci	__s32 i32;
1088c2ecf20Sopenharmony_ci	__u32 u32;
1098c2ecf20Sopenharmony_ci	__be32 be32;
1108c2ecf20Sopenharmony_ci	__u16 u16;
1118c2ecf20Sopenharmony_ci	__u8 u8;
1128c2ecf20Sopenharmony_ci	int error;
1138c2ecf20Sopenharmony_ci	__be16 err;
1148c2ecf20Sopenharmony_ci	enum sctp_state state;
1158c2ecf20Sopenharmony_ci	enum sctp_event_timeout to;
1168c2ecf20Sopenharmony_ci	struct sctp_chunk *chunk;
1178c2ecf20Sopenharmony_ci	struct sctp_association *asoc;
1188c2ecf20Sopenharmony_ci	struct sctp_transport *transport;
1198c2ecf20Sopenharmony_ci	struct sctp_bind_addr *bp;
1208c2ecf20Sopenharmony_ci	struct sctp_init_chunk *init;
1218c2ecf20Sopenharmony_ci	struct sctp_ulpevent *ulpevent;
1228c2ecf20Sopenharmony_ci	struct sctp_packet *packet;
1238c2ecf20Sopenharmony_ci	struct sctp_sackhdr *sackh;
1248c2ecf20Sopenharmony_ci	struct sctp_datamsg *msg;
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/* We are simulating ML type constructors here.
1288c2ecf20Sopenharmony_ci *
1298c2ecf20Sopenharmony_ci * SCTP_ARG_CONSTRUCTOR(NAME, TYPE, ELT) builds a function called
1308c2ecf20Sopenharmony_ci * SCTP_NAME() which takes an argument of type TYPE and returns an
1318c2ecf20Sopenharmony_ci * union sctp_arg.  It does this by inserting the sole argument into
1328c2ecf20Sopenharmony_ci * the ELT union element of a local union sctp_arg.
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci * E.g., SCTP_ARG_CONSTRUCTOR(I32, __s32, i32) builds SCTP_I32(arg),
1358c2ecf20Sopenharmony_ci * which takes an __s32 and returns a union sctp_arg containing the
1368c2ecf20Sopenharmony_ci * __s32.  So, after foo = SCTP_I32(arg), foo.i32 == arg.
1378c2ecf20Sopenharmony_ci */
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define SCTP_ARG_CONSTRUCTOR(name, type, elt) \
1408c2ecf20Sopenharmony_cistatic inline union sctp_arg	\
1418c2ecf20Sopenharmony_ciSCTP_## name (type arg)		\
1428c2ecf20Sopenharmony_ci{ union sctp_arg retval;\
1438c2ecf20Sopenharmony_ci  retval.zero_all = NULL;\
1448c2ecf20Sopenharmony_ci  retval.elt = arg;\
1458c2ecf20Sopenharmony_ci  return retval;\
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(I32,	__s32, i32)
1498c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(U32,	__u32, u32)
1508c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(BE32,	__be32, be32)
1518c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(U16,	__u16, u16)
1528c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(U8,	__u8, u8)
1538c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(ERROR,     int, error)
1548c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(PERR,      __be16, err)	/* protocol error */
1558c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(STATE,	enum sctp_state, state)
1568c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(TO,	enum sctp_event_timeout, to)
1578c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(CHUNK,	struct sctp_chunk *, chunk)
1588c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(ASOC,	struct sctp_association *, asoc)
1598c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(TRANSPORT,	struct sctp_transport *, transport)
1608c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(BA,	struct sctp_bind_addr *, bp)
1618c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(PEER_INIT,	struct sctp_init_chunk *, init)
1628c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(ULPEVENT,  struct sctp_ulpevent *, ulpevent)
1638c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(PACKET,	struct sctp_packet *, packet)
1648c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(SACKH,	struct sctp_sackhdr *, sackh)
1658c2ecf20Sopenharmony_ciSCTP_ARG_CONSTRUCTOR(DATAMSG,	struct sctp_datamsg *, msg)
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic inline union sctp_arg SCTP_FORCE(void)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	return SCTP_I32(1);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic inline union sctp_arg SCTP_NOFORCE(void)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return SCTP_I32(0);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic inline union sctp_arg SCTP_NULL(void)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	union sctp_arg retval;
1808c2ecf20Sopenharmony_ci	retval.zero_all = NULL;
1818c2ecf20Sopenharmony_ci	return retval;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistruct sctp_cmd {
1858c2ecf20Sopenharmony_ci	union sctp_arg obj;
1868c2ecf20Sopenharmony_ci	enum sctp_verb verb;
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistruct sctp_cmd_seq {
1908c2ecf20Sopenharmony_ci	struct sctp_cmd cmds[SCTP_MAX_NUM_COMMANDS];
1918c2ecf20Sopenharmony_ci	struct sctp_cmd *last_used_slot;
1928c2ecf20Sopenharmony_ci	struct sctp_cmd *next_cmd;
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/* Initialize a block of memory as a command sequence.
1978c2ecf20Sopenharmony_ci * Return 0 if the initialization fails.
1988c2ecf20Sopenharmony_ci */
1998c2ecf20Sopenharmony_cistatic inline int sctp_init_cmd_seq(struct sctp_cmd_seq *seq)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	/* cmds[] is filled backwards to simplify the overflow BUG() check */
2028c2ecf20Sopenharmony_ci	seq->last_used_slot = seq->cmds + SCTP_MAX_NUM_COMMANDS;
2038c2ecf20Sopenharmony_ci	seq->next_cmd = seq->last_used_slot;
2048c2ecf20Sopenharmony_ci	return 1;		/* We always succeed.  */
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/* Add a command to an struct sctp_cmd_seq.
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci * Use the SCTP_* constructors defined by SCTP_ARG_CONSTRUCTOR() above
2118c2ecf20Sopenharmony_ci * to wrap data which goes in the obj argument.
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_cistatic inline void sctp_add_cmd_sf(struct sctp_cmd_seq *seq,
2148c2ecf20Sopenharmony_ci				   enum sctp_verb verb, union sctp_arg obj)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	struct sctp_cmd *cmd = seq->last_used_slot - 1;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	BUG_ON(cmd < seq->cmds);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	cmd->verb = verb;
2218c2ecf20Sopenharmony_ci	cmd->obj = obj;
2228c2ecf20Sopenharmony_ci	seq->last_used_slot = cmd;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci/* Return the next command structure in an sctp_cmd_seq.
2268c2ecf20Sopenharmony_ci * Return NULL at the end of the sequence.
2278c2ecf20Sopenharmony_ci */
2288c2ecf20Sopenharmony_cistatic inline struct sctp_cmd *sctp_next_cmd(struct sctp_cmd_seq *seq)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	if (seq->next_cmd <= seq->last_used_slot)
2318c2ecf20Sopenharmony_ci		return NULL;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return --seq->next_cmd;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#endif /* __net_sctp_command_h__ */
237