blob: fa4726cffccf96e8e50a3d88fa614ff883cbc258 [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
23#include "../config.h"
24
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
247static inline const char *osmo_mncc_name(uint32_t msg_type) {
248 return get_value_string(osmo_mncc_names, msg_type);
249}
250
251static void mncc_dump_rtp(struct msgb *str, const uint8_t *msg, unsigned int len)
252{
253 const struct gsm_mncc_rtp *rtp = (const struct gsm_mncc_rtp *) msg;
254 struct in_addr ia;
255 if (len < sizeof(*rtp)) {
256 msgb_printf(str, "short MNCC RTP message (%u bytes)", len);
257 return;
258 }
259
260 ia.s_addr = rtp->ip;
261 msgb_printf(str, "%s(ref=0x%08x, ip=%s, port=%u, pt=%u, pt_mt=%u)",
262 osmo_mncc_name(rtp->msg_type), rtp->callref, inet_ntoa(ia),
263 ntohs(rtp->port), rtp->payload_type, rtp->payload_msg_type);
264}
265
266static void mncc_dump_data(struct msgb *str, const uint8_t *msg, unsigned int len)
267{
268 const struct gsm_data_frame *data = (const struct gsm_data_frame *) msg;
269 if (len < sizeof(*data)) {
270 msgb_printf(str, "short MNCC DATA message (%u bytes)", len);
271 return;
272 }
273
274 msgb_printf(str, "%s(ref=0x%08x, data=%s)", osmo_mncc_name(data->msg_type), data->callref,
275 osmo_hexdump_nospc(data->data, len - sizeof(*data)));
276}
277
278static void mncc_dump_hello(struct msgb *str, const uint8_t *msg, unsigned int len)
279{
280 const struct gsm_mncc_hello *hello = (const struct gsm_mncc_hello *) msg;
281 if (len < sizeof(*hello)) {
282 msgb_printf(str, "short MNCC HELLO message (%u bytes)", len);
283 return;
284 }
285
286 msgb_printf(str, "%s(ver=%u, mncc_sz=%u, data_size=%u called_off=%u, signal_off=%u, "
287 "emerg_off=%u, lchan_t_off=%u)\n", osmo_mncc_name(hello->msg_type),
288 hello->version, hello->mncc_size, hello->data_frame_size, hello->called_offset,
289 hello->signal_offset, hello->emergency_offset, hello->lchan_type_offset);
290}
291
292static void msg_dump_number(struct msgb *str, const char *pfx, const struct gsm_mncc_number *num)
293{
294 msgb_printf(str, "%s(%d,%d,%d,%d,%s)", pfx, num->type, num->plan, num->present, num->screen,
295 num->number);
296}
297
298static void mncc_dump_bridge(struct msgb *str, const uint8_t *msg, unsigned int len)
299{
300 const struct gsm_mncc_bridge *bridge = (const struct gsm_mncc_bridge *)msg;
301 if (len < sizeof(*bridge)) {
302 msgb_printf(str, "short MNCC BRIDGE message (%u bytes)", len);
303 return;
304 }
305
306 msgb_printf(str, "%s(call_a=0x%08x, call_b=0x%08x)", osmo_mncc_name(bridge->msg_type),
307 bridge->callref[0], bridge->callref[1]);
308}
309
310static void mncc_dump_sign(struct msgb *str, const uint8_t *msg, unsigned int len)
311{
312 const struct gsm_mncc *sign = (const struct gsm_mncc *) msg;
313 if (len < sizeof(*sign)) {
314 msgb_printf(str, "short MNCC SIGN message (%u bytes)", len);
315 return;
316 }
317
318 msgb_printf(str, "%s(ref=0x%08x, imsi=%s", osmo_mncc_name(sign->msg_type), sign->callref,
319 sign->imsi);
320 //if (sign->fields & MNCC_F_BEARER_CAP)
321 // msgb_printf(str, ", bcap=%s", osmo_hexdump_nospc());
322 if (sign->fields & MNCC_F_CALLED)
323 msg_dump_number(str, ", called=", &sign->called);
324 if (sign->fields & MNCC_F_CALLING)
325 msg_dump_number(str, ", calling=", &sign->calling);
326 if (sign->fields & MNCC_F_REDIRECTING)
327 msg_dump_number(str, ", redirecting=", &sign->redirecting);
328 if (sign->fields & MNCC_F_CONNECTED)
329 msg_dump_number(str, ", connected=", &sign->connected);
330 if (sign->fields & MNCC_F_CAUSE) {
331 msgb_printf(str, ", cause=(%d,%d,%d,%d,%d,'%s')", sign->cause.location,
332 sign->cause.coding, sign->cause.rec, sign->cause.rec_val,
333 sign->cause.value, sign->cause.diag_len ? sign->cause.diag : "");
334 }
335 if (sign->fields & MNCC_F_USERUSER) {
336 msgb_printf(str, ", useruser=(%u, '%s')", sign->useruser.proto,
337 sign->useruser.info);
338 }
339 if (sign->fields & MNCC_F_PROGRESS) {
340 msgb_printf(str, ", progress=(%d, %d, %d)", sign->progress.coding,
341 sign->progress.location, sign->progress.descr);
342 }
343 if (sign->fields & MNCC_F_EMERGENCY)
344 msgb_printf(str, ", emergency=%d", sign->emergency);
345 if (sign->fields & MNCC_F_FACILITY)
346 msgb_printf(str, ", facility='%s'", sign->facility.info);
347 if (sign->fields & MNCC_F_SSVERSION)
348 msgb_printf(str, ", ssversion='%s'", sign->ssversion.info);
349 if (sign->fields & MNCC_F_CCCAP)
350 msgb_printf(str, ", cccap=(%d, %d)", sign->cccap.dtmf, sign->cccap.pcp);
351 if (sign->fields & MNCC_F_KEYPAD)
352 msgb_printf(str, ", keypad=%d", sign->keypad);
353 if (sign->fields & MNCC_F_SIGNAL)
354 msgb_printf(str, ", signal=%d", sign->signal);
355
356 msgb_printf(str, ", clir.sup=%d, clir.inv=%d, more=%d, notify=%d)", sign->clir.sup,
357 sign->clir.inv, sign->more, sign->notify);
358 /* lchan_type/lchan_mode? */
359}
360
361
362struct msgb *osmo_mncc_stringify(const uint8_t *msg, unsigned int len)
363{
364 uint32_t msg_type;
365 struct msgb *str = msgb_alloc(2048, __func__);
366
367 OSMO_ASSERT(str);
368
369 if (len <= sizeof(msg_type)) {
370 msgb_printf(str, "short MNCC message (%d bytes)", len);
371 return NULL;
372 }
373
374 msg_type = *(const uint32_t *)msg;
375 switch (msg_type) {
376 case MNCC_RTP_CREATE:
377 case MNCC_RTP_CONNECT:
378 case MNCC_RTP_FREE:
379 mncc_dump_rtp(str, msg, len);
380 break;
381 case GSM_TCHF_FRAME:
382 case GSM_TCHF_FRAME_EFR:
383 case GSM_TCHH_FRAME:
384 case GSM_TCH_FRAME_AMR:
385 case GSM_BAD_FRAME:
386 mncc_dump_data(str, msg, len);
387 break;
388 case MNCC_SOCKET_HELLO:
389 mncc_dump_hello(str, msg, len);
390 break;
391 case MNCC_BRIDGE:
392 mncc_dump_bridge(str, msg, len);
393 break;
394 default:
395 mncc_dump_sign(str, msg, len);
396 break;
397 }
398 return str;
399}
400
401void _osmo_mncc_log(int ss, int level, const char *file, int line, const char *prefix,
402 const uint8_t *msg, unsigned int len)
403{
404 struct msgb *str;
405 if (!log_check_level(ss, level))
406 return;
407
408 str = osmo_mncc_stringify(msg, len);
409 if (!str)
410 return;
411
412 logp2(ss, level, file, line, 0, "%s%s\n", prefix, str->data);
413 msgb_free(str);
414}
415
416#endif /* HAVE_SYS_SOCKET_H */