blob: 0a38997bc4c45a000e2d384e36bdfd7d0369af6e [file] [log] [blame]
Harald Welte3e6376d2010-12-22 23:54:51 +01001/* mncc.c - utility routines for the MNCC API between the 04.08
2 * message parsing and the actual Call Control logic */
Harald Welte7ce5e252010-12-22 02:02:48 +01003
Harald Welte57184292018-01-22 01:49:02 +01004/* (C) 2008-2018 by Harald Welte <laforge@gnumonks.org>
Harald Welte4bfdfe72009-06-10 23:11:52 +08005 * (C) 2009 by Andreas Eversberg <Andreas.Eversberg@versatel.de>
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte4bfdfe72009-06-10 23:11:52 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte4bfdfe72009-06-10 23:11:52 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte4bfdfe72009-06-10 23:11:52 +080020 *
21 */
22
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Harald Welte09b7e7f2009-12-12 21:36:53 +010027#include <errno.h>
Harald Welte4bfdfe72009-06-10 23:11:52 +080028
Harald Welteda8a19f2015-12-03 14:35:05 +010029#include <osmocom/core/talloc.h>
30#include <osmocom/core/utils.h>
31
Neels Hofmeyr90843962017-09-04 15:04:35 +020032#include <osmocom/msc/gsm_04_08.h>
33#include <osmocom/msc/debug.h>
34#include <osmocom/msc/mncc.h>
35#include <osmocom/msc/gsm_data.h>
36#include <osmocom/msc/transaction.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020037
Harald Welte4bfdfe72009-06-10 23:11:52 +080038
Harald Welteda8a19f2015-12-03 14:35:05 +010039static const struct value_string mncc_names[] = {
40 { MNCC_SETUP_REQ, "MNCC_SETUP_REQ" },
41 { MNCC_SETUP_IND, "MNCC_SETUP_IND" },
42 { MNCC_SETUP_RSP, "MNCC_SETUP_RSP" },
43 { MNCC_SETUP_CNF, "MNCC_SETUP_CNF" },
44 { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
45 { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
46 { MNCC_CALL_CONF_IND, "MNCC_CALL_CONF_IND" },
47 { MNCC_CALL_PROC_REQ, "MNCC_CALL_PROC_REQ" },
48 { MNCC_PROGRESS_REQ, "MNCC_PROGRESS_REQ" },
49 { MNCC_ALERT_REQ, "MNCC_ALERT_REQ" },
50 { MNCC_ALERT_IND, "MNCC_ALERT_IND" },
51 { MNCC_NOTIFY_REQ, "MNCC_NOTIFY_REQ" },
52 { MNCC_NOTIFY_IND, "MNCC_NOTIFY_IND" },
53 { MNCC_DISC_REQ, "MNCC_DISC_REQ" },
54 { MNCC_DISC_IND, "MNCC_DISC_IND" },
55 { MNCC_REL_REQ, "MNCC_REL_REQ" },
56 { MNCC_REL_IND, "MNCC_REL_IND" },
57 { MNCC_REL_CNF, "MNCC_REL_CNF" },
58 { MNCC_FACILITY_REQ, "MNCC_FACILITY_REQ" },
59 { MNCC_FACILITY_IND, "MNCC_FACILITY_IND" },
60 { MNCC_START_DTMF_IND, "MNCC_START_DTMF_IND" },
61 { MNCC_START_DTMF_RSP, "MNCC_START_DTMF_RSP" },
62 { MNCC_START_DTMF_REJ, "MNCC_START_DTMF_REJ" },
63 { MNCC_STOP_DTMF_IND, "MNCC_STOP_DTMF_IND" },
64 { MNCC_STOP_DTMF_RSP, "MNCC_STOP_DTMF_RSP" },
65 { MNCC_MODIFY_REQ, "MNCC_MODIFY_REQ" },
66 { MNCC_MODIFY_IND, "MNCC_MODIFY_IND" },
67 { MNCC_MODIFY_RSP, "MNCC_MODIFY_RSP" },
68 { MNCC_MODIFY_CNF, "MNCC_MODIFY_CNF" },
69 { MNCC_MODIFY_REJ, "MNCC_MODIFY_REJ" },
70 { MNCC_HOLD_IND, "MNCC_HOLD_IND" },
71 { MNCC_HOLD_CNF, "MNCC_HOLD_CNF" },
72 { MNCC_HOLD_REJ, "MNCC_HOLD_REJ" },
73 { MNCC_RETRIEVE_IND, "MNCC_RETRIEVE_IND" },
74 { MNCC_RETRIEVE_CNF, "MNCC_RETRIEVE_CNF" },
75 { MNCC_RETRIEVE_REJ, "MNCC_RETRIEVE_REJ" },
76 { MNCC_USERINFO_REQ, "MNCC_USERINFO_REQ" },
77 { MNCC_USERINFO_IND, "MNCC_USERINFO_IND" },
78 { MNCC_REJ_REQ, "MNCC_REJ_REQ" },
79 { MNCC_REJ_IND, "MNCC_REJ_IND" },
80 { MNCC_BRIDGE, "MNCC_BRIDGE" },
81 { MNCC_FRAME_RECV, "MNCC_FRAME_RECV" },
82 { MNCC_FRAME_DROP, "MNCC_FRAME_DROP" },
83 { MNCC_LCHAN_MODIFY, "MNCC_LCHAN_MODIFY" },
84 { MNCC_RTP_CREATE, "MNCC_RTP_CREATE" },
85 { MNCC_RTP_CONNECT, "MNCC_RTP_CONNECT" },
86 { MNCC_RTP_FREE, "MNCC_RTP_FREE" },
87 { GSM_TCHF_FRAME, "GSM_TCHF_FRAME" },
88 { GSM_TCHF_FRAME_EFR, "GSM_TCHF_FRAME_EFR" },
89 { GSM_TCHH_FRAME, "GSM_TCHH_FRAME" },
90 { GSM_TCH_FRAME_AMR, "GSM_TCH_FRAME_AMR" },
91 { GSM_BAD_FRAME, "GSM_BAD_FRAME" },
92 { 0, NULL },
93};
Harald Welte4bfdfe72009-06-10 23:11:52 +080094
Harald Welteda8a19f2015-12-03 14:35:05 +010095const char *get_mncc_name(int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +080096{
Harald Welteda8a19f2015-12-03 14:35:05 +010097 return get_value_string(mncc_names, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +080098}
99
Harald Welte4bfdfe72009-06-10 23:11:52 +0800100void mncc_set_cause(struct gsm_mncc *data, int loc, int val)
101{
102 data->fields |= MNCC_F_CAUSE;
Keith Whyteff17f8f2019-08-01 12:20:25 +0200103 data->cause.coding = GSM48_CAUSE_CODING_GSM;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800104 data->cause.location = loc;
105 data->cause.value = val;
106}
Harald Weltefea236e2010-12-23 00:13:47 +0100107
Harald Welte57184292018-01-22 01:49:02 +0100108
109/***********************************************************************
110 * MNCC validation code. Move to libosmocore once headers are merged
111 ************************************************************************/
112
113#define MNCC_F_ALL 0x3fff
114
115static int check_string_terminated(const char *str, unsigned int size)
116{
117 int i;
118 for (i = 0; i < size; i++) {
119 if (str[i] == 0)
120 return 0;
121 }
122 return -EINVAL;
123}
124
125static int mncc_check_number(const struct gsm_mncc_number *num, const char *str)
126{
127 int rc;
128 rc = check_string_terminated(num->number, ARRAY_SIZE(num->number));
129 if (rc < 0)
130 LOGP(DMNCC, LOGL_ERROR, "MNCC %s number not terminated\n", str);
131 return rc;
132}
133
134static int mncc_check_cause(const struct gsm_mncc_cause *cause)
135{
136 if (cause->diag_len > sizeof(cause->diag))
137 return -EINVAL;
138 return 0;
139}
140
141static int mncc_check_useruser(const struct gsm_mncc_useruser *uu)
142{
143 return check_string_terminated(uu->info, ARRAY_SIZE(uu->info));
144}
145
146static int mncc_check_facility(const struct gsm_mncc_facility *fac)
147{
148 return check_string_terminated(fac->info, ARRAY_SIZE(fac->info));
149}
150
151static int mncc_check_ssversion(const struct gsm_mncc_ssversion *ssv)
152{
153 return check_string_terminated(ssv->info, ARRAY_SIZE(ssv->info));
154}
155
156static int mncc_prim_check_sign(const struct gsm_mncc *mncc_prim)
157{
158 int rc;
159
160 if (mncc_prim->fields & ~ MNCC_F_ALL) {
161 LOGP(DMNCC, LOGL_ERROR, "Unknown MNCC field mask 0x%x\n", mncc_prim->fields);
162 return -EINVAL;
163 }
164
165 rc = check_string_terminated(mncc_prim->imsi, sizeof(mncc_prim->imsi));
166 if (rc < 0) {
167 LOGP(DMNCC, LOGL_ERROR, "MNCC IMSI not terminated\n");
168 return rc;
169 }
170
171 if (mncc_prim->fields & MNCC_F_CALLED) {
172 rc = mncc_check_number(&mncc_prim->called, "called");
173 if (rc < 0)
174 return rc;
175 }
176
177 if (mncc_prim->fields & MNCC_F_CALLING) {
178 rc = mncc_check_number(&mncc_prim->calling, "calling");
179 if (rc < 0)
180 return rc;
181 }
182
183 if (mncc_prim->fields & MNCC_F_REDIRECTING) {
184 rc = mncc_check_number(&mncc_prim->redirecting, "redirecting");
185 if (rc < 0)
186 return rc;
187 }
188
189 if (mncc_prim->fields & MNCC_F_CONNECTED) {
190 rc = mncc_check_number(&mncc_prim->connected, "connected");
191 if (rc < 0)
192 return rc;
193 }
194
195 if (mncc_prim->fields & MNCC_F_CAUSE) {
196 rc = mncc_check_cause(&mncc_prim->cause);
197 if (rc < 0)
198 return rc;
199 }
200
201 if (mncc_prim->fields & MNCC_F_USERUSER) {
202 rc = mncc_check_useruser(&mncc_prim->useruser);
203 if (rc < 0)
204 return rc;
205 }
206
207 if (mncc_prim->fields & MNCC_F_FACILITY) {
208 rc = mncc_check_facility(&mncc_prim->facility);
209 if (rc < 0)
210 return rc;
211 }
212
213 if (mncc_prim->fields & MNCC_F_SSVERSION) {
214 rc = mncc_check_ssversion(&mncc_prim->ssversion);
215 if (rc < 0)
216 return rc;
217 }
218
219 if (mncc_prim->fields & MNCC_F_BEARER_CAP) {
220 bool m1_found = false;
221 int i;
222
223 for (i = 0; i < ARRAY_SIZE(mncc_prim->bearer_cap.speech_ver); i++) {
224 if (mncc_prim->bearer_cap.speech_ver[i] == -1) {
225 m1_found = true;
226 break;
227 }
228 }
229 if (!m1_found) {
230 LOGP(DMNCC, LOGL_ERROR, "Unterminated MNCC bearer capability\n");
231 return -EINVAL;
232 }
233 }
234
235 return 0;
236}
237
Neels Hofmeyrb4552052019-10-21 03:00:26 +0200238/* Make sure that the SDP section has a terminating \0. The MNCC message may end after that \0, and if SDP is omitted it
239 * must contain at least one \0 byte. */
240int mncc_check_sdp_termination(const char *label, const struct gsm_mncc *mncc, unsigned int len, const char *sdp)
241{
242 size_t sdp_offset;
243 size_t sdp_data_len;
244 size_t sdp_str_len;
245
246 OSMO_ASSERT(((char*)mncc) < sdp);
247
248 sdp_offset = sdp - (char*)mncc;
249 if (len < sdp_offset)
250 goto too_short;
251
252 sdp_data_len = len - sdp_offset;
253 if (sdp_data_len < 1)
254 goto too_short;
255
256 sdp_str_len = strnlen(sdp, sdp_data_len);
257 /* There must be a \0, so sdp_str_len must be at most sdp_data_len - 1 */
258 if (sdp_str_len >= sdp_data_len)
259 goto too_short;
260 return 0;
261too_short:
262 LOGP(DMNCC, LOGL_ERROR, "Short %s\n", label);
263 return -EINVAL;
264}
265
Harald Welte57184292018-01-22 01:49:02 +0100266int mncc_prim_check(const struct gsm_mncc *mncc_prim, unsigned int len)
267{
268 if (len < sizeof(mncc_prim->msg_type)) {
269 LOGP(DMNCC, LOGL_ERROR, "Short MNCC Header\n");
270 return -EINVAL;
271 }
272
273 switch (mncc_prim->msg_type) {
274 case MNCC_SOCKET_HELLO:
275 if (len < sizeof(struct gsm_mncc_hello)) {
276 LOGP(DMNCC, LOGL_ERROR, "Short MNCC Hello\n");
277 return -EINVAL;
278 }
279 break;
280 case GSM_BAD_FRAME:
281 case GSM_TCH_FRAME_AMR:
282 case GSM_TCHH_FRAME:
283 case GSM_TCHF_FRAME_EFR:
284 case GSM_TCHF_FRAME:
285 if (len < sizeof(struct gsm_data_frame)) {
286 LOGP(DMNCC, LOGL_ERROR, "Short MNCC TCH\n");
287 return -EINVAL;
288 }
289 break;
290 case MNCC_RTP_FREE:
291 case MNCC_RTP_CONNECT:
292 case MNCC_RTP_CREATE:
Neels Hofmeyrb4552052019-10-21 03:00:26 +0200293 return mncc_check_sdp_termination("MNCC RTP", mncc_prim, len, ((struct gsm_mncc_rtp*)mncc_prim)->sdp);
Harald Welte57184292018-01-22 01:49:02 +0100294 case MNCC_LCHAN_MODIFY:
295 case MNCC_FRAME_DROP:
296 case MNCC_FRAME_RECV:
297 /* FIXME */
298 break;
299 case MNCC_BRIDGE:
300 if (len < sizeof(struct gsm_mncc_bridge)) {
301 LOGP(DMNCC, LOGL_ERROR, "Short MNCC BRIDGE\n");
302 return -EINVAL;
303 }
304 break;
305 default:
Neels Hofmeyrb4552052019-10-21 03:00:26 +0200306 if (mncc_check_sdp_termination("MNCC Signalling", mncc_prim, len, mncc_prim->sdp))
Harald Welte57184292018-01-22 01:49:02 +0100307 return -EINVAL;
Harald Welte57184292018-01-22 01:49:02 +0100308 return mncc_prim_check_sign(mncc_prim);
309 }
310 return 0;
311}
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100312
313static uint8_t mncc_speech_ver_to_perm_speech(int speech_ver)
314{
315 /* The speech versions that are transmitted in the Bearer capability
316 * information element, that is transmitted on the Layer 3 (CC)
317 * use a different encoding than the permitted speech version
318 * identifier, that is signalled in the channel type element on the A
319 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
320 * 10.5.103 */
321
322 switch (speech_ver) {
323 case GSM48_BCAP_SV_FR:
324 return GSM0808_PERM_FR1;
325 case GSM48_BCAP_SV_HR:
326 return GSM0808_PERM_HR1;
327 case GSM48_BCAP_SV_EFR:
328 return GSM0808_PERM_FR2;
329 case GSM48_BCAP_SV_AMR_F:
330 return GSM0808_PERM_FR3;
331 case GSM48_BCAP_SV_AMR_H:
332 return GSM0808_PERM_HR3;
333 case GSM48_BCAP_SV_AMR_OFW:
334 return GSM0808_PERM_FR4;
335 case GSM48_BCAP_SV_AMR_OHW:
336 return GSM0808_PERM_HR4;
337 case GSM48_BCAP_SV_AMR_FW:
338 return GSM0808_PERM_FR5;
339 case GSM48_BCAP_SV_AMR_OH:
340 return GSM0808_PERM_HR6;
341 }
342
343 /* If nothing matches, tag the result as invalid */
344 LOGP(DBSSAP, LOGL_ERROR, "Invalid permitted speech version: %d\n", speech_ver);
345 return 0xFF;
346}
347
348/* Convert speech preference field */
349static uint8_t mncc_bc_radio_to_speech_pref(int radio)
350{
351 /* The Radio channel requirement field that is transmitted in the
352 * Bearer capability information element, that is transmitted on the
353 * Layer 3 (CC) uses a different encoding than the Channel rate and
354 * type field that is signalled in the channel type element on the A
355 * interface. (See also 3GPP TS 48.008, 3.2.2.1 and 3GPP TS 24.008,
356 * 10.5.102 */
357
358 switch (radio) {
359 case GSM48_BCAP_RRQ_FR_ONLY:
360 return GSM0808_SPEECH_FULL_BM;
361 case GSM48_BCAP_RRQ_DUAL_FR:
362 return GSM0808_SPEECH_FULL_PREF;
363 case GSM48_BCAP_RRQ_DUAL_HR:
364 return GSM0808_SPEECH_HALF_PREF;
365 }
366
367 LOGP(DBSSAP, LOGL_ERROR, "Invalid radio channel preference: %d; defaulting to full rate.\n", radio);
368 return GSM0808_SPEECH_FULL_BM;
369}
370
371int mncc_bearer_cap_to_channel_type(struct gsm0808_channel_type *ct, const struct gsm_mncc_bearer_cap *bc)
372{
373 unsigned int i;
374 uint8_t sv;
375 unsigned int count = 0;
376 bool only_gsm_hr = true;
377
378 ct->ch_indctr = GSM0808_CHAN_SPEECH;
379
380 for (i = 0; i < ARRAY_SIZE(bc->speech_ver); i++) {
381 if (bc->speech_ver[i] == -1)
382 break;
383 sv = mncc_speech_ver_to_perm_speech(bc->speech_ver[i]);
384 if (sv != 0xFF) {
385 /* Detect if something else than
386 * GSM HR V1 is supported */
387 if (sv == GSM0808_PERM_HR2 ||
388 sv == GSM0808_PERM_HR3 || sv == GSM0808_PERM_HR4 || sv == GSM0808_PERM_HR6)
389 only_gsm_hr = false;
390
391 ct->perm_spch[count] = sv;
392 count++;
393 }
394 }
395 ct->perm_spch_len = count;
396
397 if (only_gsm_hr)
398 /* Note: We must avoid the usage of GSM HR1 as this
399 * codec only offers very poor audio quality. If the
400 * MS only supports GSM HR1 (and full rate), and has
401 * a preference for half rate. Then we will ignore the
402 * preference and assume a preference for full rate. */
403 ct->ch_rate_type = GSM0808_SPEECH_FULL_BM;
404 else
405 ct->ch_rate_type = mncc_bc_radio_to_speech_pref(bc->radio);
406
407 if (count)
408 return 0;
409 else
410 return -EINVAL;
411}