blob: 26a44c680d9c014eed8706bc795a1213310c34e3 [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* Message parser/generator utilities */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <limits.h>
26
Philipp Maier993ea6b2020-08-04 18:26:50 +020027#include <osmocom/mgcp/mgcp.h>
28#include <osmocom/mgcp/osmux.h>
29#include <osmocom/mgcp/mgcp_protocol.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020030#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020031#include <osmocom/mgcp/mgcp_msg.h>
32#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010033#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier036612b2021-07-19 17:47:49 +020034#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020035
36/*! Display an mgcp message on the log output.
37 * \param[in] message mgcp message string
38 * \param[in] len message mgcp message string length
39 * \param[in] preamble string to display in logtext in front of each line */
40void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble)
41{
42 unsigned char line[80];
43 unsigned char *ptr;
44 unsigned int consumed = 0;
45 unsigned int consumed_line = 0;
46 unsigned int line_count = 0;
47
48 if (!log_check_level(DLMGCP, LOGL_DEBUG))
49 return;
50
51 while (1) {
52 memset(line, 0, sizeof(line));
53 ptr = line;
54 consumed_line = 0;
55 do {
56 if (*message != '\n' && *message != '\r') {
57 *ptr = *message;
58 ptr++;
59 }
60 message++;
61 consumed++;
62 consumed_line++;
63 } while (*message != '\n' && consumed < len
64 && consumed_line < sizeof(line));
65
66 if (strlen((const char *)line)) {
67 LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
68 preamble, line_count, line);
69 line_count++;
70 }
71
72 if (consumed >= len)
73 return;
74 }
75}
76
77/*! Parse connection mode.
78 * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback)
79 * \param[in] endp pointer to endpoint (only used for log output)
80 * \param[out] associated connection to be modified accordingly
81 * \returns 0 on success, -1 on error */
82int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp,
83 struct mgcp_conn *conn)
84{
85 int ret = 0;
86
87 if (!mode) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020088 LOGPCONN(conn, DLMGCP, LOGL_ERROR,
89 "missing connection mode\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +020090 return -1;
91 }
92 if (!conn)
93 return -1;
94 if (!endp)
95 return -1;
96
Pau Espin Pedrol17058482019-06-26 12:23:02 +020097 if (strcasecmp(mode, "recvonly") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +020098 conn->mode = MGCP_CONN_RECV_ONLY;
Pau Espin Pedrol17058482019-06-26 12:23:02 +020099 else if (strcasecmp(mode, "sendrecv") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100 conn->mode = MGCP_CONN_RECV_SEND;
Pau Espin Pedrol17058482019-06-26 12:23:02 +0200101 else if (strcasecmp(mode, "sendonly") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200102 conn->mode = MGCP_CONN_SEND_ONLY;
Pau Espin Pedrol17058482019-06-26 12:23:02 +0200103 else if (strcasecmp(mode, "loopback") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200104 conn->mode = MGCP_CONN_LOOPBACK;
105 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200106 LOGPCONN(conn, DLMGCP, LOGL_ERROR,
107 "unknown connection mode: '%s'\n", mode);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200108 ret = -1;
109 }
110
Pau Espin Pedrol30e01352020-09-21 12:29:41 +0200111 /* Special handling for RTP connections */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200112 if (conn->type == MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol2c401642021-12-24 14:48:26 +0100113 conn->u.rtp.end.output_enabled = !!(conn->mode & MGCP_CONN_SEND_ONLY);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200114 }
115
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200116 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s\n", mgcp_conn_dump(conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200117
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200118 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "connection mode '%s' %d\n",
119 mode, conn->mode);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120
121 /* Special handling für RTP connections */
122 if (conn->type == MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol2c401642021-12-24 14:48:26 +0100123 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "output_enabled %u\n",
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200124 conn->u.rtp.end.output_enabled);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125 }
126
127 /* The VTY might change the connection mode at any time, so we have
128 * to hold a copy of the original connection mode */
129 conn->mode_orig = conn->mode;
130
131 return ret;
132}
133
Philipp Maier87bd9be2017-08-22 16:35:41 +0200134/*! Analyze and parse the the hader of an MGCP messeage string.
Philipp Maier8dc35972021-07-14 11:20:16 +0200135 * \param[out] pdata caller provided memory to store the parsing results.
136 * \param[in] data mgcp message string.
137 * \returns 0 when the status line was complete and parseable, negative (MGCP
138 * cause code) on error. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200139int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data)
140{
141 int i = 0;
142 char *elem, *save = NULL;
143
144 /*! This function will parse the header part of the received
Philipp Maier8dc35972021-07-14 11:20:16 +0200145 * MGCP message. The parsing results are stored in pdata. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200146
147 OSMO_ASSERT(data);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148
149 for (elem = strtok_r(data, " ", &save); elem;
150 elem = strtok_r(NULL, " ", &save)) {
151 switch (i) {
152 case 0:
153 pdata->trans = elem;
154 break;
155 case 1:
Philipp Maier8dc35972021-07-14 11:20:16 +0200156 pdata->epname = elem;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200157 break;
158 case 2:
Pau Espin Pedrol9a345922019-06-26 13:06:30 +0200159 if (strcasecmp("MGCP", elem)) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 LOGP(DLMGCP, LOGL_ERROR,
161 "MGCP header parsing error\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100162 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163 }
164 break;
165 case 3:
Philipp Maier8dc35972021-07-14 11:20:16 +0200166 if (strcmp("1.0", elem))
Harald Welteabbb6b92017-12-28 13:13:50 +0100167 return -528;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 break;
169 }
170 i++;
171 }
172
173 if (i != 4) {
174 LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100175 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 }
177
178 return 0;
179}
180
181/*! Extract OSMUX CID from an MGCP parameter line (string).
182 * \param[in] line single parameter line from the MGCP message
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200183 * \returns OSMUX CID, -1 wildcard, -2 on error */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200184int mgcp_parse_osmux_cid(const char *line)
185{
186 int osmux_cid;
187
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200188
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200189 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200190 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
191 return -1;
192 }
193
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200194 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrolef6304e2019-04-23 13:24:37 +0200195 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
196 line);
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200197 return -2;
Pau Espin Pedrolef6304e2019-04-23 13:24:37 +0200198 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200199
200 if (osmux_cid > OSMUX_CID_MAX) {
201 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
202 osmux_cid, OSMUX_CID_MAX);
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200203 return -2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200204 }
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200205 LOGP(DLMGCP, LOGL_DEBUG, "MGCP client offered Osmux CID %u\n", osmux_cid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206
207 return osmux_cid;
208}
209
210/*! Check MGCP parameter line (string) for plausibility.
Philipp Maier036612b2021-07-19 17:47:49 +0200211 * \param[in] endp pointer to endpoint (only used for log output, may be NULL)
212 * \param[in] trunk pointer to trunk (only used for log output, may be NULL if endp is not NULL)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213 * \param[in] line single parameter line from the MGCP message
Philipp Maier036612b2021-07-19 17:47:49 +0200214 * \returns true when line seems plausible, false on error */
215bool mgcp_check_param(const struct mgcp_endpoint *endp, struct mgcp_trunk *trunk, const char *line)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216{
217 const size_t line_len = strlen(line);
218 if (line[0] != '\0' && line_len < 2) {
Philipp Maier036612b2021-07-19 17:47:49 +0200219 if (endp)
220 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "wrong MGCP option format: '%s'\n", line);
221 else
222 LOGPTRUNK(trunk, DLMGCP, LOGL_NOTICE, "wrong MGCP option format: '%s'\n", line);
223 return false;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200224 }
225
226 /* FIXME: A couple more checks wouldn't hurt... */
227
Philipp Maier036612b2021-07-19 17:47:49 +0200228 return true;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200229}
230
231/*! Check if the specified callid seems plausible.
232 * \param[in] endp pointer to endpoint
233 * \param{in] callid to verify
Pau Espin Pedrol06624e12020-09-21 12:18:52 +0200234 * \returns 0 when callid seems plausible, -1 on error */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid)
236{
237 /*! This function compares the supplied callid with the called that is
238 * stored in the endpoint structure. */
239
240 if (!endp)
241 return -1;
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200242
243 /* Accept any CallID for "X-Osmo-IGN: C" */
244 if (endp->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID)
245 return 0;
246
Philipp Maier87bd9be2017-08-22 16:35:41 +0200247 if (!callid)
248 return -1;
249 if (!endp->callid)
250 return -1;
251
252 if (strcmp(endp->callid, callid) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200253 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
254 "CallIDs mismatch: '%s' != '%s'\n",
255 endp->callid, callid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 return -1;
257 }
258
259 return 0;
260}
261
262/*! Check if the specified connection id seems plausible.
263 * \param[in] endp pointer to endpoint
264 * \param{in] connection id to verify
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200265 * \returns 0 when connection id is valid and exists, an RFC3435 error code on error.
Neels Hofmeyr8a91d2c2018-09-03 22:51:30 +0200266 */
Philipp Maier01d24a32017-11-21 17:26:09 +0100267int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *conn_id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268{
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200269 /* For invalid conn_ids, return 510 "The transaction could not be executed, because some
270 * unspecified protocol error was detected." */
271
Philipp Maier01d24a32017-11-21 17:26:09 +0100272 /* Check for null identifiers */
273 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200274 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
275 "invalid ConnectionIdentifier (missing)\n");
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200276 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100277 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278
Philipp Maier01d24a32017-11-21 17:26:09 +0100279 /* Check for empty connection identifiers */
280 if (strlen(conn_id) == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200281 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
282 "invalid ConnectionIdentifier (empty)\n");
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200283 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100284 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285
Philipp Maier01d24a32017-11-21 17:26:09 +0100286 /* Check for over long connection identifiers */
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200287 if (strlen(conn_id) > (MGCP_CONN_ID_MAXLEN-1)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200288 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
289 "invalid ConnectionIdentifier (too long: %zu > %d) 0x%s\n",
290 strlen(conn_id), MGCP_CONN_ID_MAXLEN-1, conn_id);
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200291 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100292 }
293
294 /* Check if connection exists */
295 if (mgcp_conn_get(endp, conn_id))
Philipp Maier87bd9be2017-08-22 16:35:41 +0200296 return 0;
297
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200298 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
299 "no connection found under ConnectionIdentifier 0x%s\n", conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200300
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200301 /* When the conn_id was not found, return error code 515 "The transaction refers to an incorrect
302 * connection-id (may have been already deleted)." */
303 return 515;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304}
305
306/*! Extract individual lines from MCGP message.
307 * \param[in] str MGCP message string, consisting of multiple lines
308 * \param{in] saveptr pointer to next line in str
309 * \returns line, NULL when done */
310char *mgcp_strline(char *str, char **saveptr)
311{
312 char *result;
313
314 /*! The function must be called with *str set to the input string
315 * for the first line. After that saveptr will be initalized.
316 * all consecutive lines are extracted by calling the function
317 * with str set to NULL. When done, the function will return NULL
318 * to indicate that all lines have been parsed. */
319
320 if (str)
321 *saveptr = str;
322
323 result = *saveptr;
324
325 if (*saveptr != NULL) {
326 *saveptr = strpbrk(*saveptr, "\r\n");
327
328 if (*saveptr != NULL) {
329 char *eos = *saveptr;
330
331 if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n')
332 (*saveptr)++;
333 (*saveptr)++;
334 if ((*saveptr)[0] == '\0')
335 *saveptr = NULL;
336
337 *eos = '\0';
338 }
339 }
340
341 return result;
342}