blob: 8a7dc4d6081353b68ae2cbc6ea0d8a826b2bf6c2 [file] [log] [blame]
Harald Welte0bbf67d2018-01-01 20:31:58 +01001/* mncc.c - utility routines for the MNCC API between the 04.08
2 * message parsing and the actual Call Control logic */
3
4/* (C) 2008-2017 by Harald Welte <laforge@gnumonks.org>
5 * (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
9 * 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
11 * (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
16 * GNU Affero General Public License for more details.
17 *
18 * 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/>.
20 *
21 */
22
Pau Espin Pedrol88955fb2023-01-18 18:54:00 +010023#include "config.h"
Harald Welte0bbf67d2018-01-01 20:31:58 +010024
25#ifdef HAVE_SYS_SOCKET_H
26
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <arpa/inet.h>
31#include <netinet/in.h>
32
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/logging.h>
35#include <osmocom/gsm/mncc.h>
36
37/* FIXME FIXME FIXME FIXME FIXME START */
38#define MNCC_SETUP_REQ 0x0101
39#define MNCC_SETUP_IND 0x0102
40#define MNCC_SETUP_RSP 0x0103
41#define MNCC_SETUP_CNF 0x0104
42#define MNCC_SETUP_COMPL_REQ 0x0105
43#define MNCC_SETUP_COMPL_IND 0x0106
44/* MNCC_REJ_* is perfomed via MNCC_REL_* */
45#define MNCC_CALL_CONF_IND 0x0107
46#define MNCC_CALL_PROC_REQ 0x0108
47#define MNCC_PROGRESS_REQ 0x0109
48#define MNCC_ALERT_REQ 0x010a
49#define MNCC_ALERT_IND 0x010b
50#define MNCC_NOTIFY_REQ 0x010c
51#define MNCC_NOTIFY_IND 0x010d
52#define MNCC_DISC_REQ 0x010e
53#define MNCC_DISC_IND 0x010f
54#define MNCC_REL_REQ 0x0110
55#define MNCC_REL_IND 0x0111
56#define MNCC_REL_CNF 0x0112
57#define MNCC_FACILITY_REQ 0x0113
58#define MNCC_FACILITY_IND 0x0114
59#define MNCC_START_DTMF_IND 0x0115
60#define MNCC_START_DTMF_RSP 0x0116
61#define MNCC_START_DTMF_REJ 0x0117
62#define MNCC_STOP_DTMF_IND 0x0118
63#define MNCC_STOP_DTMF_RSP 0x0119
64#define MNCC_MODIFY_REQ 0x011a
65#define MNCC_MODIFY_IND 0x011b
66#define MNCC_MODIFY_RSP 0x011c
67#define MNCC_MODIFY_CNF 0x011d
68#define MNCC_MODIFY_REJ 0x011e
69#define MNCC_HOLD_IND 0x011f
70#define MNCC_HOLD_CNF 0x0120
71#define MNCC_HOLD_REJ 0x0121
72#define MNCC_RETRIEVE_IND 0x0122
73#define MNCC_RETRIEVE_CNF 0x0123
74#define MNCC_RETRIEVE_REJ 0x0124
75#define MNCC_USERINFO_REQ 0x0125
76#define MNCC_USERINFO_IND 0x0126
77#define MNCC_REJ_REQ 0x0127
78#define MNCC_REJ_IND 0x0128
79
80#define MNCC_BRIDGE 0x0200
81#define MNCC_FRAME_RECV 0x0201
82#define MNCC_FRAME_DROP 0x0202
83#define MNCC_LCHAN_MODIFY 0x0203
84#define MNCC_RTP_CREATE 0x0204
85#define MNCC_RTP_CONNECT 0x0205
86#define MNCC_RTP_FREE 0x0206
87
88#define GSM_TCHF_FRAME 0x0300
89#define GSM_TCHF_FRAME_EFR 0x0301
90#define GSM_TCHH_FRAME 0x0302
91#define GSM_TCH_FRAME_AMR 0x0303
92#define GSM_BAD_FRAME 0x03ff
93
94#define MNCC_SOCKET_HELLO 0x0400
95
96#define GSM_MAX_FACILITY 128
97#define GSM_MAX_SSVERSION 128
98#define GSM_MAX_USERUSER 128
99
100#define MNCC_F_BEARER_CAP 0x0001
101#define MNCC_F_CALLED 0x0002
102#define MNCC_F_CALLING 0x0004
103#define MNCC_F_REDIRECTING 0x0008
104#define MNCC_F_CONNECTED 0x0010
105#define MNCC_F_CAUSE 0x0020
106#define MNCC_F_USERUSER 0x0040
107#define MNCC_F_PROGRESS 0x0080
108#define MNCC_F_EMERGENCY 0x0100
109#define MNCC_F_FACILITY 0x0200
110#define MNCC_F_SSVERSION 0x0400
111#define MNCC_F_CCCAP 0x0800
112#define MNCC_F_KEYPAD 0x1000
113#define MNCC_F_SIGNAL 0x2000
114
115struct gsm_mncc {
116 /* context based information */
117 uint32_t msg_type;
118 uint32_t callref;
119
120 /* which fields are present */
121 uint32_t fields;
122
123 /* data derived informations (MNCC_F_ based) */
124 struct gsm_mncc_bearer_cap bearer_cap;
125 struct gsm_mncc_number called;
126 struct gsm_mncc_number calling;
127 struct gsm_mncc_number redirecting;
128 struct gsm_mncc_number connected;
129 struct gsm_mncc_cause cause;
130 struct gsm_mncc_progress progress;
131 struct gsm_mncc_useruser useruser;
132 struct gsm_mncc_facility facility;
133 struct gsm_mncc_cccap cccap;
134 struct gsm_mncc_ssversion ssversion;
135 struct {
136 int sup;
137 int inv;
138 } clir;
139 int signal;
140
141 /* data derived information, not MNCC_F based */
142 int keypad;
143 int more;
144 int notify; /* 0..127 */
145 int emergency;
146 char imsi[16];
147
148 unsigned char lchan_type;
149 unsigned char lchan_mode;
150};
151
152struct gsm_data_frame {
153 uint32_t msg_type;
154 uint32_t callref;
155 unsigned char data[0];
156};
157
158#define MNCC_SOCK_VERSION 5
159struct gsm_mncc_hello {
160 uint32_t msg_type;
161 uint32_t version;
162
163 /* send the sizes of the structs */
164 uint32_t mncc_size;
165 uint32_t data_frame_size;
166
167 /* send some offsets */
168 uint32_t called_offset;
169 uint32_t signal_offset;
170 uint32_t emergency_offset;
171 uint32_t lchan_type_offset;
172};
173
174struct gsm_mncc_rtp {
175 uint32_t msg_type;
176 uint32_t callref;
177 uint32_t ip;
178 uint16_t port;
179 uint32_t payload_type;
180 uint32_t payload_msg_type;
181};
182
183struct gsm_mncc_bridge {
184 uint32_t msg_type;
185 uint32_t callref[2];
186};
187
188/* FIXME FIXME FIXME FIXME FIXME END */
189
190const struct value_string osmo_mncc_names[] = {
191 { MNCC_SETUP_REQ, "MNCC_SETUP_REQ" },
192 { MNCC_SETUP_IND, "MNCC_SETUP_IND" },
193 { MNCC_SETUP_RSP, "MNCC_SETUP_RSP" },
194 { MNCC_SETUP_CNF, "MNCC_SETUP_CNF" },
195 { MNCC_SETUP_COMPL_REQ, "MNCC_SETUP_COMPL_REQ" },
196 { MNCC_SETUP_COMPL_IND, "MNCC_SETUP_COMPL_IND" },
197 { MNCC_CALL_CONF_IND, "MNCC_CALL_CONF_IND" },
198 { MNCC_CALL_PROC_REQ, "MNCC_CALL_PROC_REQ" },
199 { MNCC_PROGRESS_REQ, "MNCC_PROGRESS_REQ" },
200 { MNCC_ALERT_REQ, "MNCC_ALERT_REQ" },
201 { MNCC_ALERT_IND, "MNCC_ALERT_IND" },
202 { MNCC_NOTIFY_REQ, "MNCC_NOTIFY_REQ" },
203 { MNCC_NOTIFY_IND, "MNCC_NOTIFY_IND" },
204 { MNCC_DISC_REQ, "MNCC_DISC_REQ" },
205 { MNCC_DISC_IND, "MNCC_DISC_IND" },
206 { MNCC_REL_REQ, "MNCC_REL_REQ" },
207 { MNCC_REL_IND, "MNCC_REL_IND" },
208 { MNCC_REL_CNF, "MNCC_REL_CNF" },
209 { MNCC_FACILITY_REQ, "MNCC_FACILITY_REQ" },
210 { MNCC_FACILITY_IND, "MNCC_FACILITY_IND" },
211 { MNCC_START_DTMF_IND, "MNCC_START_DTMF_IND" },
212 { MNCC_START_DTMF_RSP, "MNCC_START_DTMF_RSP" },
213 { MNCC_START_DTMF_REJ, "MNCC_START_DTMF_REJ" },
214 { MNCC_STOP_DTMF_IND, "MNCC_STOP_DTMF_IND" },
215 { MNCC_STOP_DTMF_RSP, "MNCC_STOP_DTMF_RSP" },
216 { MNCC_MODIFY_REQ, "MNCC_MODIFY_REQ" },
217 { MNCC_MODIFY_IND, "MNCC_MODIFY_IND" },
218 { MNCC_MODIFY_RSP, "MNCC_MODIFY_RSP" },
219 { MNCC_MODIFY_CNF, "MNCC_MODIFY_CNF" },
220 { MNCC_MODIFY_REJ, "MNCC_MODIFY_REJ" },
221 { MNCC_HOLD_IND, "MNCC_HOLD_IND" },
222 { MNCC_HOLD_CNF, "MNCC_HOLD_CNF" },
223 { MNCC_HOLD_REJ, "MNCC_HOLD_REJ" },
224 { MNCC_RETRIEVE_IND, "MNCC_RETRIEVE_IND" },
225 { MNCC_RETRIEVE_CNF, "MNCC_RETRIEVE_CNF" },
226 { MNCC_RETRIEVE_REJ, "MNCC_RETRIEVE_REJ" },
227 { MNCC_USERINFO_REQ, "MNCC_USERINFO_REQ" },
228 { MNCC_USERINFO_IND, "MNCC_USERINFO_IND" },
229 { MNCC_REJ_REQ, "MNCC_REJ_REQ" },
230 { MNCC_REJ_IND, "MNCC_REJ_IND" },
231 { MNCC_BRIDGE, "MNCC_BRIDGE" },
232 { MNCC_FRAME_RECV, "MNCC_FRAME_RECV" },
233 { MNCC_FRAME_DROP, "MNCC_FRAME_DROP" },
234 { MNCC_LCHAN_MODIFY, "MNCC_LCHAN_MODIFY" },
235 { MNCC_RTP_CREATE, "MNCC_RTP_CREATE" },
236 { MNCC_RTP_CONNECT, "MNCC_RTP_CONNECT" },
237 { MNCC_RTP_FREE, "MNCC_RTP_FREE" },
238 { GSM_TCHF_FRAME, "GSM_TCHF_FRAME" },
239 { GSM_TCHF_FRAME_EFR, "GSM_TCHF_FRAME_EFR" },
240 { GSM_TCHH_FRAME, "GSM_TCHH_FRAME" },
241 { GSM_TCH_FRAME_AMR, "GSM_TCH_FRAME_AMR" },
242 { GSM_BAD_FRAME, "GSM_BAD_FRAME" },
243 { MNCC_SOCKET_HELLO, "MNCC_SOCKET_HELLO" },
244 { 0, NULL },
245};
246
Harald Welte0bbf67d2018-01-01 20:31:58 +0100247static void mncc_dump_rtp(struct msgb *str, const uint8_t *msg, unsigned int len)
248{
249 const struct gsm_mncc_rtp *rtp = (const struct gsm_mncc_rtp *) msg;
250 struct in_addr ia;
251 if (len < sizeof(*rtp)) {
252 msgb_printf(str, "short MNCC RTP message (%u bytes)", len);
253 return;
254 }
255
256 ia.s_addr = rtp->ip;
257 msgb_printf(str, "%s(ref=0x%08x, ip=%s, port=%u, pt=%u, pt_mt=%u)",
258 osmo_mncc_name(rtp->msg_type), rtp->callref, inet_ntoa(ia),
259 ntohs(rtp->port), rtp->payload_type, rtp->payload_msg_type);
260}
261
262static void mncc_dump_data(struct msgb *str, const uint8_t *msg, unsigned int len)
263{
264 const struct gsm_data_frame *data = (const struct gsm_data_frame *) msg;
265 if (len < sizeof(*data)) {
266 msgb_printf(str, "short MNCC DATA message (%u bytes)", len);
267 return;
268 }
269
270 msgb_printf(str, "%s(ref=0x%08x, data=%s)", osmo_mncc_name(data->msg_type), data->callref,
271 osmo_hexdump_nospc(data->data, len - sizeof(*data)));
272}
273
274static void mncc_dump_hello(struct msgb *str, const uint8_t *msg, unsigned int len)
275{
276 const struct gsm_mncc_hello *hello = (const struct gsm_mncc_hello *) msg;
277 if (len < sizeof(*hello)) {
278 msgb_printf(str, "short MNCC HELLO message (%u bytes)", len);
279 return;
280 }
281
282 msgb_printf(str, "%s(ver=%u, mncc_sz=%u, data_size=%u called_off=%u, signal_off=%u, "
283 "emerg_off=%u, lchan_t_off=%u)\n", osmo_mncc_name(hello->msg_type),
284 hello->version, hello->mncc_size, hello->data_frame_size, hello->called_offset,
285 hello->signal_offset, hello->emergency_offset, hello->lchan_type_offset);
286}
287
288static void msg_dump_number(struct msgb *str, const char *pfx, const struct gsm_mncc_number *num)
289{
290 msgb_printf(str, "%s(%d,%d,%d,%d,%s)", pfx, num->type, num->plan, num->present, num->screen,
291 num->number);
292}
293
294static void mncc_dump_bridge(struct msgb *str, const uint8_t *msg, unsigned int len)
295{
296 const struct gsm_mncc_bridge *bridge = (const struct gsm_mncc_bridge *)msg;
297 if (len < sizeof(*bridge)) {
298 msgb_printf(str, "short MNCC BRIDGE message (%u bytes)", len);
299 return;
300 }
301
302 msgb_printf(str, "%s(call_a=0x%08x, call_b=0x%08x)", osmo_mncc_name(bridge->msg_type),
303 bridge->callref[0], bridge->callref[1]);
304}
305
306static void mncc_dump_sign(struct msgb *str, const uint8_t *msg, unsigned int len)
307{
308 const struct gsm_mncc *sign = (const struct gsm_mncc *) msg;
309 if (len < sizeof(*sign)) {
310 msgb_printf(str, "short MNCC SIGN message (%u bytes)", len);
311 return;
312 }
313
314 msgb_printf(str, "%s(ref=0x%08x, imsi=%s", osmo_mncc_name(sign->msg_type), sign->callref,
315 sign->imsi);
316 //if (sign->fields & MNCC_F_BEARER_CAP)
317 // msgb_printf(str, ", bcap=%s", osmo_hexdump_nospc());
318 if (sign->fields & MNCC_F_CALLED)
319 msg_dump_number(str, ", called=", &sign->called);
320 if (sign->fields & MNCC_F_CALLING)
321 msg_dump_number(str, ", calling=", &sign->calling);
322 if (sign->fields & MNCC_F_REDIRECTING)
323 msg_dump_number(str, ", redirecting=", &sign->redirecting);
324 if (sign->fields & MNCC_F_CONNECTED)
325 msg_dump_number(str, ", connected=", &sign->connected);
326 if (sign->fields & MNCC_F_CAUSE) {
327 msgb_printf(str, ", cause=(%d,%d,%d,%d,%d,'%s')", sign->cause.location,
328 sign->cause.coding, sign->cause.rec, sign->cause.rec_val,
329 sign->cause.value, sign->cause.diag_len ? sign->cause.diag : "");
330 }
331 if (sign->fields & MNCC_F_USERUSER) {
332 msgb_printf(str, ", useruser=(%u, '%s')", sign->useruser.proto,
333 sign->useruser.info);
334 }
335 if (sign->fields & MNCC_F_PROGRESS) {
336 msgb_printf(str, ", progress=(%d, %d, %d)", sign->progress.coding,
337 sign->progress.location, sign->progress.descr);
338 }
339 if (sign->fields & MNCC_F_EMERGENCY)
340 msgb_printf(str, ", emergency=%d", sign->emergency);
341 if (sign->fields & MNCC_F_FACILITY)
342 msgb_printf(str, ", facility='%s'", sign->facility.info);
343 if (sign->fields & MNCC_F_SSVERSION)
344 msgb_printf(str, ", ssversion='%s'", sign->ssversion.info);
345 if (sign->fields & MNCC_F_CCCAP)
346 msgb_printf(str, ", cccap=(%d, %d)", sign->cccap.dtmf, sign->cccap.pcp);
347 if (sign->fields & MNCC_F_KEYPAD)
348 msgb_printf(str, ", keypad=%d", sign->keypad);
349 if (sign->fields & MNCC_F_SIGNAL)
350 msgb_printf(str, ", signal=%d", sign->signal);
351
352 msgb_printf(str, ", clir.sup=%d, clir.inv=%d, more=%d, notify=%d)", sign->clir.sup,
353 sign->clir.inv, sign->more, sign->notify);
354 /* lchan_type/lchan_mode? */
355}
356
357
358struct msgb *osmo_mncc_stringify(const uint8_t *msg, unsigned int len)
359{
360 uint32_t msg_type;
361 struct msgb *str = msgb_alloc(2048, __func__);
362
363 OSMO_ASSERT(str);
364
365 if (len <= sizeof(msg_type)) {
366 msgb_printf(str, "short MNCC message (%d bytes)", len);
367 return NULL;
368 }
369
370 msg_type = *(const uint32_t *)msg;
371 switch (msg_type) {
372 case MNCC_RTP_CREATE:
373 case MNCC_RTP_CONNECT:
374 case MNCC_RTP_FREE:
375 mncc_dump_rtp(str, msg, len);
376 break;
377 case GSM_TCHF_FRAME:
378 case GSM_TCHF_FRAME_EFR:
379 case GSM_TCHH_FRAME:
380 case GSM_TCH_FRAME_AMR:
381 case GSM_BAD_FRAME:
382 mncc_dump_data(str, msg, len);
383 break;
384 case MNCC_SOCKET_HELLO:
385 mncc_dump_hello(str, msg, len);
386 break;
387 case MNCC_BRIDGE:
388 mncc_dump_bridge(str, msg, len);
389 break;
390 default:
391 mncc_dump_sign(str, msg, len);
392 break;
393 }
394 return str;
395}
396
397void _osmo_mncc_log(int ss, int level, const char *file, int line, const char *prefix,
398 const uint8_t *msg, unsigned int len)
399{
400 struct msgb *str;
401 if (!log_check_level(ss, level))
402 return;
403
404 str = osmo_mncc_stringify(msg, len);
405 if (!str)
406 return;
407
408 logp2(ss, level, file, line, 0, "%s%s\n", prefix, str->data);
409 msgb_free(str);
410}
411
412#endif /* HAVE_SYS_SOCKET_H */