aboutsummaryrefslogtreecommitdiffstats
path: root/src/sccp_sclc.c
blob: dae2c36178cf65be8fd20634f510abf731855f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* SCCP Connectionless Control (SCLC) according to ITU-T Q.713/Q.714 */

/* (C) 2015-2017 by Harald Welte <laforge@gnumonks.org>
 * All Rights reserved
 *
 * This program is free software; 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 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/* This code is a bit of a hybrid between the ITU-T Q.71x specifications
 * for SCCP (particularly its connection-oriented part), and the IETF
 * RFC 3868 (SUA).  The idea here is to have one shared code base of the
 * state machines for SCCP Connection Oriented, and use those both from
 * SCCP and SUA.
 *
 * To do so, all SCCP messages are translated to SUA messages in the
 * input side, and all generated SUA messages are translated to SCCP on
 * the output side.
 *
 * The Choice of going for SUA messages as the "native" format was based
 * on their easier parseability, and the fact that there are features in
 * SUA which classic SCCP cannot handle (like IP addresses in GT).
 * However, all SCCP features can be expressed in SUA.
 *
 * The code only supports Class 2.  No support for Class 3 is intended,
 * but patches are of course alwys welcome.
 *
 * Missing other features:
 *  * Segmentation/Reassembly support
 *  * T(guard) after (re)start
 *  * freezing of local references
 *  * parsing/encoding of IPv4/IPv6 addresses
 *  * use of multiple Routing Contexts in SUA case
 */

#include <string.h>

#include <osmocom/core/utils.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/fsm.h>

#include <osmocom/sigtran/sccp_sap.h>
#include <osmocom/sigtran/protocol/sua.h>
#include <sccp/sccp_types.h>

#include "xua_internal.h"
#include "sccp_internal.h"

/* generate a 'struct xua_msg' of requested type from primitive data */
static struct xua_msg *xua_gen_msg_cl(uint32_t event,
				      struct osmo_scu_prim *prim, int msg_type)
{
	struct xua_msg *xua = xua_msg_alloc();
	struct osmo_scu_unitdata_param *udpar = &prim->u.unitdata;

	if (!xua)
		return NULL;

	switch (msg_type) {
	case SUA_CL_CLDT:
		xua->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDT);
		xua_msg_add_u32(xua, SUA_IEI_ROUTE_CTX, 0); /* FIXME */
		xua_msg_add_u32(xua, SUA_IEI_PROTO_CLASS, 0);
		xua_msg_add_sccp_addr(xua, SUA_IEI_SRC_ADDR, &udpar->calling_addr);
		xua_msg_add_sccp_addr(xua, SUA_IEI_DEST_ADDR, &udpar->called_addr);
		xua_msg_add_u32(xua, SUA_IEI_SEQ_CTRL, udpar->in_sequence_control);
		/* optional: importance, ... correlation id? */
		if (!prim)
			goto prim_needed;
		xua_msg_add_data(xua, SUA_IEI_DATA, msgb_l2len(prim->oph.msg),
				 msgb_l2(prim->oph.msg));
		break;
	default:
		LOGP(DLSCCP, LOGL_ERROR, "Unknown msg_type %u\n", msg_type);
		xua_msg_free(xua);
		return NULL;
	}
	return xua;

prim_needed:
	xua_msg_free(xua);
	LOGP(DLSCCP, LOGL_ERROR, "%s must be called with valid 'prim' "
	     "pointer for msg_type=%u\n", __func__, msg_type);
	return NULL;
}

/* generate xua_msg, encode it and send it to SCRC */
static int xua_gen_encode_and_send(struct osmo_sccp_user *scu, uint32_t event,
				   struct osmo_scu_prim *prim, int msg_type)
{
	struct xua_msg *xua;

	xua = xua_gen_msg_cl(event, prim, msg_type);
	if (!xua)
		return -1;

	return sccp_scrc_rx_sclc_msg(scu->inst, xua);
}

/*! \brief Main entrance function for primitives from SCCP User
 *  \param[in] scu SCCP User who is sending the primitive
 *  \param[on] oph Osmocom primitive header of the primitive
 *  \returns 0 on success; negtive in case of error */
int sccp_sclc_user_sap_down(struct osmo_sccp_user *scu, struct osmo_prim_hdr *oph)
{
	struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
	struct msgb *msg = prim->oph.msg;
	int rc = 0;

	/* we get called from osmo_sccp_user_sap_down() which already
	 * has debug-logged the primitive */

	switch (OSMO_PRIM_HDR(&prim->oph)) {
	case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_REQUEST):
		/* Connectionless by-passes this altogether */
		rc = xua_gen_encode_and_send(scu, -1, prim, SUA_CL_CLDT);
		goto out;
	default:
		LOGP(DLSCCP, LOGL_ERROR, "Received unknown SCCP User "
		     "primitive %s from user\n",
		     osmo_scu_prim_name(&prim->oph));
		rc = -1;
		goto out;
	}

out:
	/* the SAP is supposed to consume the primitive/msgb */
	msgb_free(msg);

	return rc;
}

/* Process an incoming CLDT message (from a remote peer) */
static int sclc_rx_cldt(struct osmo_sccp_instance *inst, struct xua_msg *xua)
{
	struct osmo_scu_prim *prim;
	struct osmo_scu_unitdata_param *param;
	struct xua_msg_part *data_ie = xua_msg_find_tag(xua, SUA_IEI_DATA);
	struct msgb *upmsg = sccp_msgb_alloc(__func__);
	struct osmo_sccp_user *scu;
	uint32_t protocol_class;

	/* fill primitive */
	prim = (struct osmo_scu_prim *) msgb_put(upmsg, sizeof(*prim));
	param = &prim->u.unitdata;
	osmo_prim_init(&prim->oph, SCCP_SAP_USER,
			OSMO_SCU_PRIM_N_UNITDATA,
			PRIM_OP_INDICATION, upmsg);
	sua_addr_parse(&param->called_addr, xua, SUA_IEI_DEST_ADDR);
	sua_addr_parse(&param->calling_addr, xua, SUA_IEI_SRC_ADDR);
	param->in_sequence_control = xua_msg_get_u32(xua, SUA_IEI_SEQ_CTRL);
	protocol_class = xua_msg_get_u32(xua, SUA_IEI_PROTO_CLASS);
	param->return_option = protocol_class & 0x80;
	param->importance = xua_msg_get_u32(xua, SUA_IEI_IMPORTANCE);

	scu = sccp_user_find(inst, param->called_addr.ssn,
			     param->called_addr.pc);

	if (!scu) {
		/* FIXME: Send destination unreachable? */
		LOGP(DLSUA, LOGL_NOTICE, "Received SUA message for unequipped SSN %u\n",
			param->called_addr.ssn);
		msgb_free(upmsg);
		return 0;
	}

	/* copy data */
	upmsg->l2h = msgb_put(upmsg, data_ie->len);
	memcpy(upmsg->l2h, data_ie->dat, data_ie->len);

	/* send to user SAP */
	sccp_user_prim_up(scu, prim);

	/* xua_msg is free'd by our caller */
	return 0;
}

static int sclc_rx_cldr(struct osmo_sccp_instance *inst, struct xua_msg *xua)
{
	struct osmo_scu_prim *prim;
	struct osmo_scu_notice_param *param;
	struct xua_msg_part *data_ie = xua_msg_find_tag(xua, SUA_IEI_DATA);
	struct msgb *upmsg = sccp_msgb_alloc(__func__);
	struct osmo_sccp_user *scu;

	/* fill primitive */
	prim = (struct osmo_scu_prim *) msgb_put(upmsg, sizeof(*prim));
	param = &prim->u.notice;
	osmo_prim_init(&prim->oph, SCCP_SAP_USER,
			OSMO_SCU_PRIM_N_NOTICE,
			PRIM_OP_INDICATION, upmsg);

	sua_addr_parse(&param->called_addr, xua, SUA_IEI_DEST_ADDR);
	sua_addr_parse(&param->calling_addr, xua, SUA_IEI_SRC_ADDR);
	param->importance = xua_msg_get_u32(xua, SUA_IEI_IMPORTANCE);
	param->cause = xua_msg_get_u32(xua, SUA_IEI_CAUSE);

	scu = sccp_user_find(inst, param->called_addr.ssn,
			     param->called_addr.pc);
	if (!scu) {
		/* FIXME: Send destination unreachable? */
		LOGP(DLSUA, LOGL_NOTICE, "Received CLDR for unequipped SSN %u\n",
			param->called_addr.ssn);
		msgb_free(upmsg);
		return 0;
	}

	/* copy data */
	upmsg->l2h = msgb_put(upmsg, data_ie->len);
	memcpy(upmsg->l2h, data_ie->dat, data_ie->len);

	/* send to user SAP */
	sccp_user_prim_up(scu, prim);

	/* xua_msg is free'd by our caller */
	return 0;
}

/*! \brief SCRC -> SCLC (connectionless message)
 *  \param[in] inst SCCP Instance in which we operate
 *  \param[in] xua SUA connectionless message
 *  \returns 0 on success; negative on error */
int sccp_sclc_rx_from_scrc(struct osmo_sccp_instance *inst,
			   struct xua_msg *xua)
{
	int rc = -1;

	OSMO_ASSERT(xua->hdr.msg_class == SUA_MSGC_CL);

	switch (xua->hdr.msg_type) {
	case SUA_CL_CLDT:
		rc = sclc_rx_cldt(inst, xua);
		break;
	case SUA_CL_CLDR:
		rc = sclc_rx_cldr(inst, xua);
		break;
	default:
		LOGP(DLSUA, LOGL_NOTICE, "Received unknown/unsupported "
		     "message %s\n", xua_hdr_dump(xua, &xua_dialect_sua));
		break;
	}

	return rc;
}

/* generate a return/refusal message (SUA CLDR == SCCP UDTS) based on
 * the incoming message.  We need to flip all identities between sender
 * and receiver */
static struct xua_msg *gen_ret_msg(struct osmo_sccp_instance *inst,
				   const struct xua_msg *xua_in,
				   uint32_t ret_cause)
{
	struct xua_msg *xua_out = xua_msg_alloc();
	struct osmo_sccp_addr called;

	xua_out->hdr = XUA_HDR(SUA_MSGC_CL, SUA_CL_CLDR);
	xua_msg_add_u32(xua_out, SUA_IEI_ROUTE_CTX, inst->route_ctx);
	xua_msg_add_u32(xua_out, SUA_IEI_CAUSE,
			SUA_CAUSE_T_RETURN | ret_cause);
	/* Swap Calling and Called Party */
	xua_msg_copy_part(xua_out, SUA_IEI_SRC_ADDR, xua_in, SUA_IEI_DEST_ADDR);
	xua_msg_copy_part(xua_out, SUA_IEI_DEST_ADDR, xua_in, SUA_IEI_SRC_ADDR);
	/* TODO: Optional: Hop Count */
	/* Optional: Importance */
	xua_msg_copy_part(xua_out, SUA_IEI_IMPORTANCE,
			  xua_in, SUA_IEI_IMPORTANCE);
	/* Optional: Message Priority */
	xua_msg_copy_part(xua_out, SUA_IEI_MSG_PRIO, xua_in, SUA_IEI_MSG_PRIO);
	/* Optional: Correlation ID */
	xua_msg_copy_part(xua_out, SUA_IEI_CORR_ID, xua_in, SUA_IEI_CORR_ID);
	/* Optional: Segmentation */
	xua_msg_copy_part(xua_out, SUA_IEI_SEGMENTATION,
			  xua_in, SUA_IEI_SEGMENTATION);
	/* Optional: Data */
	xua_msg_copy_part(xua_out, SUA_IEI_DATA, xua_in, SUA_IEI_DATA);

	sua_addr_parse(&called, xua_out, SUA_IEI_DEST_ADDR);
	/* Route on PC + SSN ? */
	if (called.ri == OSMO_SCCP_RI_SSN_PC) {
		/* if no PC, copy OPC into called addr */
		if (!(called.presence & OSMO_SCCP_ADDR_T_PC)) {
			struct osmo_sccp_addr calling;
			sua_addr_parse(&calling, xua_out, SUA_IEI_SRC_ADDR);
			called.presence |= OSMO_SCCP_ADDR_T_PC;
			called.pc = calling.pc;
			/* Re-encode / replace called address */
			xua_msg_free_tag(xua_out, SUA_IEI_DEST_ADDR);
			xua_msg_add_sccp_addr(xua_out, SUA_IEI_DEST_ADDR,
					      &called);
		}
	}
	return xua_out;
}

/*! \brief SCRC -> SCLC (Routing Failure
 *  \param[in] inst SCCP Instance in which we operate
 *  \param[in] xua_in Message that failed to be routed
 *  \param[in] cause SCCP Return Cause */
void sccp_sclc_rx_scrc_rout_fail(struct osmo_sccp_instance *inst,
				 struct xua_msg *xua_in, uint32_t cause)
{
	struct xua_msg *xua_out;

	/* Figure C.12/Q.714 (Sheet 8) Node 9 */
	switch (xua_in->hdr.msg_type) {
	case SUA_CL_CLDT:
		xua_out = gen_ret_msg(inst, xua_in, cause);
		/* TODO: Message Return Option? */
		if (!osmo_ss7_pc_is_local(inst->ss7, xua_in->mtp.opc)) {
			/* non-local originator: send UDTS */
			/* TODO: Assign SLS */
			sccp_scrc_rx_sclc_msg(inst, xua_out);
		} else {
			/* local originator: send N-NOTICE to user */
			/* TODO: N-NOTICE.ind SCLC -> SCU */
			sclc_rx_cldr(inst, xua_out);
			xua_msg_free(xua_out);
		}
		break;
	case SUA_CL_CLDR:
		/* do nothing */
		break;
	}
}