blob: 019466e4ce996053fc4f90671565816b40b4289b [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
27#include <osmocom/mgcp/mgcp_internal.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020028#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029#include <osmocom/mgcp/mgcp_msg.h>
30#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010031#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020032
33/*! Display an mgcp message on the log output.
34 * \param[in] message mgcp message string
35 * \param[in] len message mgcp message string length
36 * \param[in] preamble string to display in logtext in front of each line */
37void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble)
38{
39 unsigned char line[80];
40 unsigned char *ptr;
41 unsigned int consumed = 0;
42 unsigned int consumed_line = 0;
43 unsigned int line_count = 0;
44
45 if (!log_check_level(DLMGCP, LOGL_DEBUG))
46 return;
47
48 while (1) {
49 memset(line, 0, sizeof(line));
50 ptr = line;
51 consumed_line = 0;
52 do {
53 if (*message != '\n' && *message != '\r') {
54 *ptr = *message;
55 ptr++;
56 }
57 message++;
58 consumed++;
59 consumed_line++;
60 } while (*message != '\n' && consumed < len
61 && consumed_line < sizeof(line));
62
63 if (strlen((const char *)line)) {
64 LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
65 preamble, line_count, line);
66 line_count++;
67 }
68
69 if (consumed >= len)
70 return;
71 }
72}
73
74/*! Parse connection mode.
75 * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback)
76 * \param[in] endp pointer to endpoint (only used for log output)
77 * \param[out] associated connection to be modified accordingly
78 * \returns 0 on success, -1 on error */
79int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp,
80 struct mgcp_conn *conn)
81{
82 int ret = 0;
83
84 if (!mode) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020085 LOGPCONN(conn, DLMGCP, LOGL_ERROR,
86 "missing connection mode\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +020087 return -1;
88 }
89 if (!conn)
90 return -1;
91 if (!endp)
92 return -1;
93
Pau Espin Pedrol17058482019-06-26 12:23:02 +020094 if (strcasecmp(mode, "recvonly") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +020095 conn->mode = MGCP_CONN_RECV_ONLY;
Pau Espin Pedrol17058482019-06-26 12:23:02 +020096 else if (strcasecmp(mode, "sendrecv") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 conn->mode = MGCP_CONN_RECV_SEND;
Pau Espin Pedrol17058482019-06-26 12:23:02 +020098 else if (strcasecmp(mode, "sendonly") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +020099 conn->mode = MGCP_CONN_SEND_ONLY;
Pau Espin Pedrol17058482019-06-26 12:23:02 +0200100 else if (strcasecmp(mode, "loopback") == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101 conn->mode = MGCP_CONN_LOOPBACK;
102 else {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200103 LOGPCONN(conn, DLMGCP, LOGL_ERROR,
104 "unknown connection mode: '%s'\n", mode);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105 ret = -1;
106 }
107
108 /* Special handling für RTP connections */
109 if (conn->type == MGCP_CONN_TYPE_RTP) {
110 conn->u.rtp.end.output_enabled =
111 conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0;
112 }
113
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200114 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s\n", mgcp_conn_dump(conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200116 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "connection mode '%s' %d\n",
117 mode, conn->mode);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118
119 /* Special handling für RTP connections */
120 if (conn->type == MGCP_CONN_TYPE_RTP) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200121 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "output_enabled %d\n",
122 conn->u.rtp.end.output_enabled);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 }
124
125 /* The VTY might change the connection mode at any time, so we have
126 * to hold a copy of the original connection mode */
127 conn->mode_orig = conn->mode;
128
129 return ret;
130}
131
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132/*! Analyze and parse the the hader of an MGCP messeage string.
133 * \param[out] pdata caller provided memory to store the parsing results
134 * \param[in] data mgcp message string
135 * \returns when the status line was complete and transaction_id and
136 * endp out parameters are set, -1 on error */
137int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data)
138{
139 int i = 0;
140 char *elem, *save = NULL;
Philipp Maiera49e32a2018-02-01 18:18:50 +0100141 int cause;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142
143 /*! This function will parse the header part of the received
144 * MGCP message. The parsing results are stored in pdata.
145 * The function will also automatically search the pool with
146 * available endpoints in order to find an endpoint that matches
147 * the endpoint string in in the header */
148
149 OSMO_ASSERT(data);
150 pdata->trans = "000000";
151
152 for (elem = strtok_r(data, " ", &save); elem;
153 elem = strtok_r(NULL, " ", &save)) {
154 switch (i) {
155 case 0:
156 pdata->trans = elem;
157 break;
158 case 1:
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200159 pdata->endp = mgcp_endp_by_name(&cause, elem, pdata->cfg);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160 if (!pdata->endp) {
161 LOGP(DLMGCP, LOGL_ERROR,
162 "Unable to find Endpoint `%s'\n", elem);
Neels Hofmeyr0a89e922018-08-20 22:39:53 +0200163 OSMO_ASSERT(cause < 0);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100164 return cause;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165 }
166 break;
167 case 2:
Pau Espin Pedrol9a345922019-06-26 13:06:30 +0200168 if (strcasecmp("MGCP", elem)) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200169 LOGP(DLMGCP, LOGL_ERROR,
170 "MGCP header parsing error\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100171 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 }
173 break;
174 case 3:
175 if (strcmp("1.0", elem)) {
176 LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' "
177 "not supported\n", elem);
Harald Welteabbb6b92017-12-28 13:13:50 +0100178 return -528;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200179 }
180 break;
181 }
182 i++;
183 }
184
185 if (i != 4) {
186 LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
187 pdata->trans = "000000";
188 pdata->endp = NULL;
Harald Welteabbb6b92017-12-28 13:13:50 +0100189 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 }
191
192 return 0;
193}
194
195/*! Extract OSMUX CID from an MGCP parameter line (string).
196 * \param[in] line single parameter line from the MGCP message
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200197 * \returns OSMUX CID, -1 wildcard, -2 on error */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198int mgcp_parse_osmux_cid(const char *line)
199{
200 int osmux_cid;
201
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200202
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200203 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200204 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
205 return -1;
206 }
207
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200208 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrolef6304e2019-04-23 13:24:37 +0200209 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
210 line);
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200211 return -2;
Pau Espin Pedrolef6304e2019-04-23 13:24:37 +0200212 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213
214 if (osmux_cid > OSMUX_CID_MAX) {
215 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
216 osmux_cid, OSMUX_CID_MAX);
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200217 return -2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 }
219 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
220
221 return osmux_cid;
222}
223
224/*! Check MGCP parameter line (string) for plausibility.
225 * \param[in] endp pointer to endpoint (only used for log output)
226 * \param[in] line single parameter line from the MGCP message
227 * \returns 1 when line seems plausible, 0 on error */
228int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line)
229{
230 const size_t line_len = strlen(line);
231 if (line[0] != '\0' && line_len < 2) {
232 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200233 "Wrong MGCP option format: '%s' on %s\n",
234 line, endp->name);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 return 0;
236 }
237
238 /* FIXME: A couple more checks wouldn't hurt... */
239
240 return 1;
241}
242
243/*! Check if the specified callid seems plausible.
244 * \param[in] endp pointer to endpoint
245 * \param{in] callid to verify
246 * \returns 1 when callid seems plausible, 0 on error */
247int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid)
248{
249 /*! This function compares the supplied callid with the called that is
250 * stored in the endpoint structure. */
251
252 if (!endp)
253 return -1;
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200254
255 /* Accept any CallID for "X-Osmo-IGN: C" */
256 if (endp->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID)
257 return 0;
258
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 if (!callid)
260 return -1;
261 if (!endp->callid)
262 return -1;
263
264 if (strcmp(endp->callid, callid) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200265 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
266 "CallIDs mismatch: '%s' != '%s'\n",
267 endp->callid, callid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268 return -1;
269 }
270
271 return 0;
272}
273
274/*! Check if the specified connection id seems plausible.
275 * \param[in] endp pointer to endpoint
276 * \param{in] connection id to verify
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200277 * \returns 0 when connection id is valid and exists, an RFC3435 error code on error.
Neels Hofmeyr8a91d2c2018-09-03 22:51:30 +0200278 */
Philipp Maier01d24a32017-11-21 17:26:09 +0100279int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *conn_id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280{
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200281 /* For invalid conn_ids, return 510 "The transaction could not be executed, because some
282 * unspecified protocol error was detected." */
283
Philipp Maier01d24a32017-11-21 17:26:09 +0100284 /* Check for null identifiers */
285 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200286 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
287 "invalid ConnectionIdentifier (missing)\n");
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200288 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100289 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200290
Philipp Maier01d24a32017-11-21 17:26:09 +0100291 /* Check for empty connection identifiers */
292 if (strlen(conn_id) == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200293 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
294 "invalid ConnectionIdentifier (empty)\n");
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200295 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100296 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297
Philipp Maier01d24a32017-11-21 17:26:09 +0100298 /* Check for over long connection identifiers */
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200299 if (strlen(conn_id) > (MGCP_CONN_ID_MAXLEN-1)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200300 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
301 "invalid ConnectionIdentifier (too long: %zu > %d) 0x%s\n",
302 strlen(conn_id), MGCP_CONN_ID_MAXLEN-1, conn_id);
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200303 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100304 }
305
306 /* Check if connection exists */
307 if (mgcp_conn_get(endp, conn_id))
Philipp Maier87bd9be2017-08-22 16:35:41 +0200308 return 0;
309
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200310 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
311 "no connection found under ConnectionIdentifier 0x%s\n", conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200312
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200313 /* When the conn_id was not found, return error code 515 "The transaction refers to an incorrect
314 * connection-id (may have been already deleted)." */
315 return 515;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316}
317
318/*! Extract individual lines from MCGP message.
319 * \param[in] str MGCP message string, consisting of multiple lines
320 * \param{in] saveptr pointer to next line in str
321 * \returns line, NULL when done */
322char *mgcp_strline(char *str, char **saveptr)
323{
324 char *result;
325
326 /*! The function must be called with *str set to the input string
327 * for the first line. After that saveptr will be initalized.
328 * all consecutive lines are extracted by calling the function
329 * with str set to NULL. When done, the function will return NULL
330 * to indicate that all lines have been parsed. */
331
332 if (str)
333 *saveptr = str;
334
335 result = *saveptr;
336
337 if (*saveptr != NULL) {
338 *saveptr = strpbrk(*saveptr, "\r\n");
339
340 if (*saveptr != NULL) {
341 char *eos = *saveptr;
342
343 if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n')
344 (*saveptr)++;
345 (*saveptr)++;
346 if ((*saveptr)[0] == '\0')
347 *saveptr = NULL;
348
349 *eos = '\0';
350 }
351 }
352
353 return result;
354}