Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <time.h> |
| 28 | #include <limits.h> |
| 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 35 | #include <osmocom/core/stats.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 36 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 37 | #include <osmocom/mgcp/mgcp.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 38 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 39 | #include <osmocom/mgcp/mgcp_internal.h> |
| 40 | #include <osmocom/mgcp/mgcp_stat.h> |
| 41 | #include <osmocom/mgcp/mgcp_msg.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 42 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 43 | #include <osmocom/mgcp/mgcp_sdp.h> |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 44 | #include <osmocom/mgcp/mgcp_codec.h> |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 45 | #include <osmocom/mgcp/mgcp_conn.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 46 | |
| 47 | struct mgcp_request { |
| 48 | char *name; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 49 | struct msgb *(*handle_request) (struct mgcp_parse_data * data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 50 | char *debug_name; |
| 51 | }; |
| 52 | |
| 53 | #define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \ |
| 54 | { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME }, |
| 55 | |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 56 | static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = { |
| 57 | [MGCP_CRCX_SUCCESS] = {"crcx:success", "CRCX command processed successfully."}, |
| 58 | [MGCP_CRCX_FAIL_BAD_ACTION] = {"crcx:bad_action", "bad action in CRCX command."}, |
| 59 | [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in CRCX command."}, |
| 60 | [MGCP_CRCX_FAIL_MISSING_CALLID] = {"crcx:missing_callid", "missing CallId in CRCX command."}, |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 61 | [MGCP_CRCX_FAIL_INVALID_MODE] = {"crcx:invalid_mode", "invalid connection mode in CRCX command."}, |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 62 | [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = {"crcx:limit_exceeded", "limit of concurrent connections was reached."}, |
| 63 | [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = {"crcx:unkown_callid", "unknown CallId in CRCX command."}, |
| 64 | [MGCP_CRCX_FAIL_ALLOC_CONN] = {"crcx:alloc_conn_fail", "connection allocation failure."}, |
| 65 | [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] = {"crcx:no_remote_conn_desc", "no opposite end specified for connection."}, |
| 66 | [MGCP_CRCX_FAIL_START_RTP] = {"crcx:start_rtp_failure", "failure to start RTP processing."}, |
| 67 | [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = {"crcx:conn_rejected", "connection rejected by policy."}, |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 68 | [MGCP_CRCX_FAIL_NO_OSMUX] = {"crcx:no_osmux", "no osmux offered by peer."}, |
| 69 | [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = {"crcx:conn_opt", "connection options invalid."}, |
| 70 | [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = {"crcx:codec_nego", "codec negotiation failure."}, |
| 71 | [MGCP_CRCX_FAIL_BIND_PORT] = {"crcx:bind_port", "port bind failure."}, |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = { |
| 75 | .group_name_prefix = "crcx", |
| 76 | .group_description = "crxc statistics", |
| 77 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 78 | .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc), |
| 79 | .ctr_desc = mgcp_crcx_ctr_desc |
| 80 | }; |
| 81 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 82 | static const struct rate_ctr_desc mgcp_mdcx_ctr_desc[] = { |
| 83 | [MGCP_MDCX_SUCCESS] = {"mdcx:success", "MDCX command processed successfully."}, |
| 84 | [MGCP_MDCX_FAIL_WILDCARD] = {"mdcx:wildcard", "wildcard endpoint names in MDCX commands are unsupported."}, |
| 85 | [MGCP_MDCX_FAIL_NO_CONN] = {"mdcx:no_conn", "endpoint specified in MDCX command has no active connections."}, |
| 86 | [MGCP_MDCX_FAIL_INVALID_CALLID] = {"mdcx:callid", "invalid CallId specified in MDCX command."}, |
| 87 | [MGCP_MDCX_FAIL_INVALID_CONNID] = {"mdcx:connid", "invalid connection ID specified in MDCX command."}, |
| 88 | [MGCP_MDCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in MDCX command."}, |
| 89 | [MGCP_MDCX_FAIL_NO_CONNID] = {"mdcx:no_connid", "no connection ID specified in MDCX command."}, |
| 90 | [MGCP_MDCX_FAIL_CONN_NOT_FOUND] = {"mdcx:conn_not_found", "connection specified in MDCX command does not exist."}, |
| 91 | [MGCP_MDCX_FAIL_INVALID_MODE] = {"mdcx:invalid_mode", "invalid connection mode in MDCX command."}, |
| 92 | [MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS] = {"mdcx:conn_opt", "connection options invalid."}, |
| 93 | [MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC] = {"mdcx:no_remote_conn_desc", "no opposite end specified for connection."}, |
| 94 | [MGCP_MDCX_FAIL_START_RTP] = {"mdcx:start_rtp_failure", "failure to start RTP processing."}, |
| 95 | [MGCP_MDCX_FAIL_REJECTED_BY_POLICY] = {"mdcx:conn_rejected", "connection rejected by policy."}, |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 96 | [MGCP_MDCX_DEFERRED_BY_POLICY] = {"mdcx:conn_deferred", "connection deferred by policy."}, |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | const static struct rate_ctr_group_desc mgcp_mdcx_ctr_group_desc = { |
| 100 | .group_name_prefix = "mdcx", |
| 101 | .group_description = "mdcx statistics", |
| 102 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 103 | .num_ctr = ARRAY_SIZE(mgcp_mdcx_ctr_desc), |
| 104 | .ctr_desc = mgcp_mdcx_ctr_desc |
| 105 | }; |
| 106 | |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 107 | static const struct rate_ctr_desc mgcp_dlcx_ctr_desc[] = { |
| 108 | [MGCP_DLCX_SUCCESS] = {"dlcx:success", "DLCX command processed successfully."}, |
| 109 | [MGCP_DLCX_FAIL_WILDCARD] = {"dlcx:wildcard", "wildcard names in DLCX commands are unsupported."}, |
| 110 | [MGCP_DLCX_FAIL_NO_CONN] = {"dlcx:no_conn", "endpoint specified in DLCX command has no active connections."}, |
| 111 | [MGCP_DLCX_FAIL_INVALID_CALLID] = {"dlcx:callid", "CallId specified in DLCX command mismatches endpoint's CallId ."}, |
| 112 | [MGCP_DLCX_FAIL_INVALID_CONNID] = {"dlcx:connid", "connection ID specified in DLCX command does not exist on endpoint."}, |
| 113 | [MGCP_DLCX_FAIL_UNHANDLED_PARAM] = {"dlcx:unhandled_param", "unhandled parameter in DLCX command."}, |
| 114 | [MGCP_DLCX_FAIL_REJECTED_BY_POLICY] = {"dlcx:rejected", "connection deletion rejected by policy."}, |
| 115 | [MGCP_DLCX_DEFERRED_BY_POLICY] = {"dlcx:deferred", "connection deletion deferred by policy."}, |
| 116 | }; |
| 117 | |
| 118 | const static struct rate_ctr_group_desc mgcp_dlcx_ctr_group_desc = { |
| 119 | .group_name_prefix = "dlcx", |
| 120 | .group_description = "dlcx statistics", |
| 121 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 122 | .num_ctr = ARRAY_SIZE(mgcp_dlcx_ctr_desc), |
| 123 | .ctr_desc = mgcp_dlcx_ctr_desc |
| 124 | }; |
| 125 | |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 126 | const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = { |
| 127 | .group_name_prefix = "all_rtp_conn", |
| 128 | .group_description = "aggregated statistics for all rtp connections", |
| 129 | .class_id = 1, |
| 130 | .num_ctr = ARRAY_SIZE(all_rtp_conn_rate_ctr_desc), |
| 131 | .ctr_desc = all_rtp_conn_rate_ctr_desc |
| 132 | }; |
| 133 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 134 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data); |
| 135 | static struct msgb *handle_create_con(struct mgcp_parse_data *data); |
| 136 | static struct msgb *handle_delete_con(struct mgcp_parse_data *data); |
| 137 | static struct msgb *handle_modify_con(struct mgcp_parse_data *data); |
| 138 | static struct msgb *handle_rsip(struct mgcp_parse_data *data); |
| 139 | static struct msgb *handle_noti_req(struct mgcp_parse_data *data); |
| 140 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 141 | /* Initalize transcoder */ |
| 142 | static int setup_rtp_processing(struct mgcp_endpoint *endp, |
| 143 | struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 144 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 145 | struct mgcp_config *cfg = endp->cfg; |
| 146 | struct mgcp_conn_rtp *conn_src = NULL; |
| 147 | struct mgcp_conn_rtp *conn_dst = conn; |
| 148 | struct mgcp_conn *_conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 149 | |
Pau Espin Pedrol | fa810e8 | 2019-05-06 18:54:10 +0200 | [diff] [blame] | 150 | if (conn->type != MGCP_RTP_DEFAULT && !mgcp_conn_rtp_is_osmux(conn)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 151 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 152 | "RTP-setup: Endpoint is not configured as RTP default, stopping here!\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 156 | if (conn->conn->mode == MGCP_CONN_LOOPBACK) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 157 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 158 | "RTP-setup: Endpoint is in loopback mode, stopping here!\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* Find the "sister" connection */ |
| 163 | llist_for_each_entry(_conn, &endp->conns, entry) { |
| 164 | if (_conn->id != conn->conn->id) { |
| 165 | conn_src = &_conn->u.rtp; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
Philipp Maier | acc1035 | 2018-07-19 18:07:57 +0200 | [diff] [blame] | 170 | return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 173 | /* array of function pointers for handling various |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 174 | * messages. In the future this might be binary sorted |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 175 | * for performance reasons. */ |
| 176 | static const struct mgcp_request mgcp_requests[] = { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 177 | MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint") |
Oliver Smith | 622dd61 | 2019-01-30 14:14:45 +0100 | [diff] [blame] | 178 | MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection") |
| 179 | MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection") |
| 180 | MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection") |
| 181 | MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest") |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 182 | |
Oliver Smith | 622dd61 | 2019-01-30 14:14:45 +0100 | [diff] [blame] | 183 | /* SPEC extension */ |
| 184 | MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress") |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 187 | /* Helper function to allocate some memory for responses and retransmissions */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 188 | static struct msgb *mgcp_msgb_alloc(void) |
| 189 | { |
| 190 | struct msgb *msg; |
| 191 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
| 192 | if (!msg) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 193 | LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 194 | |
| 195 | return msg; |
| 196 | } |
| 197 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 198 | /* Helper function for do_retransmission() and create_resp() */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 199 | static struct msgb *do_retransmission(const struct mgcp_endpoint *endp) |
| 200 | { |
| 201 | struct msgb *msg = mgcp_msgb_alloc(); |
| 202 | if (!msg) |
| 203 | return NULL; |
| 204 | |
| 205 | msg->l2h = msgb_put(msg, strlen(endp->last_response)); |
| 206 | memcpy(msg->l2h, endp->last_response, msgb_l2len(msg)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 207 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 208 | return msg; |
| 209 | } |
| 210 | |
| 211 | static struct msgb *create_resp(struct mgcp_endpoint *endp, int code, |
| 212 | const char *txt, const char *msg, |
| 213 | const char *trans, const char *param, |
| 214 | const char *sdp) |
| 215 | { |
| 216 | int len; |
| 217 | struct msgb *res; |
| 218 | |
| 219 | res = mgcp_msgb_alloc(); |
| 220 | if (!res) |
| 221 | return NULL; |
| 222 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 223 | len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s", |
| 224 | code, trans, txt, param ? param : "", sdp ? sdp : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 225 | if (len < 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 226 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 227 | msgb_free(res); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | res->l2h = msgb_put(res, len); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 232 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 233 | mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * Remember the last transmission per endpoint. |
| 237 | */ |
| 238 | if (endp) { |
| 239 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 240 | talloc_free(endp->last_response); |
| 241 | talloc_free(endp->last_trans); |
| 242 | endp->last_trans = talloc_strdup(tcfg->endpoints, trans); |
| 243 | endp->last_response = talloc_strndup(tcfg->endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 244 | (const char *)res->l2h, |
| 245 | msgb_l2len(res)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return res; |
| 249 | } |
| 250 | |
| 251 | static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 252 | int code, const char *msg, |
| 253 | const char *trans, |
| 254 | const char *param) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 255 | { |
| 256 | return create_resp(endp, code, " OK", msg, trans, param, NULL); |
| 257 | } |
| 258 | |
| 259 | static struct msgb *create_ok_response(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 260 | int code, const char *msg, |
| 261 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 262 | { |
| 263 | return create_ok_resp_with_param(endp, code, msg, trans, NULL); |
| 264 | } |
| 265 | |
| 266 | static struct msgb *create_err_response(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 267 | int code, const char *msg, |
| 268 | const char *trans) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 269 | { |
| 270 | return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL); |
| 271 | } |
| 272 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 273 | /* Add MGCP parameters to a message buffer */ |
| 274 | static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp, |
| 275 | const struct mgcp_conn_rtp *conn) |
| 276 | { |
| 277 | int rc; |
| 278 | |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 279 | /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */ |
Philipp Maier | 207ab51 | 2018-02-02 14:19:26 +0100 | [diff] [blame] | 280 | if (endp->wildcarded_req |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 281 | && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) { |
Philipp Maier | dd4ede3 | 2018-02-01 17:52:52 +0100 | [diff] [blame] | 282 | rc = msgb_printf(msg, "Z: %s%x@%s\r\n", |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 283 | MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, |
| 284 | ENDPOINT_NUMBER(endp), endp->cfg->domain); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 285 | if (rc < 0) |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
Philipp Maier | c3cfae2 | 2018-01-22 12:03:03 +0100 | [diff] [blame] | 289 | rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 290 | if (rc < 0) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 296 | /* Format MGCP response string (with SDP attached) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 297 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 298 | struct mgcp_conn_rtp *conn, |
| 299 | const char *msg, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 300 | const char *trans_id, |
| 301 | bool add_conn_params) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 302 | { |
Pau Espin Pedrol | a93c6e9 | 2019-05-06 15:23:57 +0200 | [diff] [blame] | 303 | /* TODO: we may want to define another local_ip_osmux var to us for |
| 304 | OSMUX connections. Perhaps adding a new internal API to get it based |
| 305 | on conn type */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 306 | const char *addr = endp->cfg->local_ip; |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 307 | struct msgb *sdp; |
| 308 | int rc; |
| 309 | struct msgb *result; |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 310 | char local_ip_addr[INET_ADDRSTRLEN]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 311 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 312 | sdp = msgb_alloc_headroom(4096, 128, "sdp record"); |
| 313 | if (!sdp) |
| 314 | return NULL; |
| 315 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 316 | if (!addr) { |
| 317 | mgcp_get_local_addr(local_ip_addr, conn); |
| 318 | addr = local_ip_addr; |
| 319 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 320 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 321 | /* Attach optional connection parameters */ |
| 322 | if (add_conn_params) { |
| 323 | rc = add_params(sdp, endp, conn); |
| 324 | if (rc < 0) |
| 325 | goto error; |
| 326 | } |
| 327 | |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 328 | /* Attach optional OSMUX parameters */ |
Pau Espin Pedrol | c63f15a | 2019-05-10 16:52:08 +0200 | [diff] [blame] | 329 | if (mgcp_conn_rtp_is_osmux(conn)) { |
Pau Espin Pedrol | 5e8d799 | 2019-04-24 19:56:43 +0200 | [diff] [blame] | 330 | rc = msgb_printf(sdp, "X-Osmux: %u\r\n", conn->osmux.cid); |
Philipp Maier | 3cbfb8a | 2018-01-22 11:39:59 +0100 | [diff] [blame] | 331 | if (rc < 0) |
| 332 | goto error; |
| 333 | } |
| 334 | |
| 335 | /* Attach line break to separate the parameters from the SDP block */ |
Philipp Maier | c3cfae2 | 2018-01-22 12:03:03 +0100 | [diff] [blame] | 336 | rc = msgb_printf(sdp, "\r\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 337 | |
Philipp Maier | 8970c49 | 2017-10-11 13:33:42 +0200 | [diff] [blame] | 338 | rc = mgcp_write_response_sdp(endp, conn, sdp, addr); |
| 339 | if (rc < 0) |
| 340 | goto error; |
| 341 | result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data); |
| 342 | msgb_free(sdp); |
| 343 | return result; |
| 344 | error: |
| 345 | msgb_free(sdp); |
| 346 | return NULL; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 349 | /* Send out dummy packet to keep the connection open, if the connection is an |
| 350 | * osmux connection, send the dummy packet via OSMUX */ |
| 351 | static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 352 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 353 | if (conn->osmux.state != OSMUX_STATE_DISABLED) |
| 354 | osmux_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 355 | else |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 356 | mgcp_send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 359 | /* handle incoming messages: |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 360 | * - this can be a command (four letters, space, transaction id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 361 | * - or a response (three numbers, space, transaction id) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 362 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
| 363 | { |
| 364 | struct mgcp_parse_data pdata; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 365 | int rc, i, code, handled = 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 366 | struct msgb *resp = NULL; |
| 367 | char *data; |
| 368 | |
| 369 | if (msgb_l2len(msg) < 4) { |
| 370 | LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | if (mgcp_msg_terminate_nul(msg)) |
| 375 | return NULL; |
| 376 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 377 | mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 378 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 379 | /* attempt to treat it as a response */ |
| 380 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 381 | LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | msg->l3h = &msg->l2h[4]; |
| 386 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 387 | /* |
| 388 | * Check for a duplicate message and respond. |
| 389 | */ |
| 390 | memset(&pdata, 0, sizeof(pdata)); |
| 391 | pdata.cfg = cfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 392 | data = mgcp_strline((char *)msg->l3h, &pdata.save); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 393 | rc = mgcp_parse_header(&pdata, data); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 394 | if (pdata.endp && pdata.trans |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 395 | && pdata.endp->last_trans |
| 396 | && strcmp(pdata.endp->last_trans, pdata.trans) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 397 | return do_retransmission(pdata.endp); |
| 398 | } |
| 399 | |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 400 | /* check for general parser failure */ |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 401 | if (rc < 0) { |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 402 | LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 403 | return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 406 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 407 | if (strncmp |
| 408 | (mgcp_requests[i].name, (const char *)&msg->l2h[0], |
| 409 | 4) == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 410 | handled = 1; |
| 411 | resp = mgcp_requests[i].handle_request(&pdata); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | if (!handled) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 417 | LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", |
| 418 | &msg->l2h[0]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 419 | |
| 420 | return resp; |
| 421 | } |
| 422 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 423 | /* AUEP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 424 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) |
| 425 | { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 426 | LOGPENDP(p->endp, DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n"); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 427 | return create_ok_response(p->endp, 200, "AUEP", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 430 | /* Try to find a free port by attempting to bind on it. Also handle the |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 431 | * counter that points on the next free port. Since we have a pointer |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 432 | * to the next free port, binding should in work on the first attempt in |
Pau Espin Pedrol | fc80673 | 2019-04-23 00:18:43 +0200 | [diff] [blame] | 433 | * general. In case of failure the next port is tried until the whole port |
| 434 | * range is tried once. */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 435 | static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 436 | { |
| 437 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 438 | struct mgcp_rtp_end *end; |
| 439 | struct mgcp_port_range *range; |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 440 | unsigned int tries; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 441 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 442 | OSMO_ASSERT(conn); |
| 443 | end = &conn->end; |
| 444 | OSMO_ASSERT(end); |
| 445 | |
| 446 | range = &endp->cfg->net_ports; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 447 | |
| 448 | /* attempt to find a port */ |
Philipp Maier | b38fb89 | 2018-05-22 13:52:21 +0200 | [diff] [blame] | 449 | tries = (range->range_end - range->range_start) / 2; |
| 450 | for (i = 0; i < tries; ++i) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 451 | int rc; |
| 452 | |
| 453 | if (range->last_port >= range->range_end) |
| 454 | range->last_port = range->range_start; |
| 455 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 456 | rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 457 | |
| 458 | range->last_port += 2; |
| 459 | if (rc == 0) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | } |
| 464 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 465 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 466 | "Allocating a RTP/RTCP port failed %u times.\n", |
| 467 | tries); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 468 | return -1; |
| 469 | } |
| 470 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 471 | /*! Helper function for check_local_cx_options() to get a pointer of the next |
| 472 | * lco option identifier |
| 473 | * \param[in] lco string |
| 474 | * \returns pointer to the beginning of the LCO identifier, NULL on failure */ |
| 475 | char *get_lco_identifier(const char *options) |
| 476 | { |
| 477 | char *ptr; |
| 478 | unsigned int count = 0; |
| 479 | |
| 480 | /* Jump to the end of the lco identifier */ |
| 481 | ptr = strstr(options, ":"); |
| 482 | if (!ptr) |
| 483 | return NULL; |
| 484 | |
| 485 | /* Walk backwards until the pointer points to the beginning of the |
| 486 | * lco identifier. We know that we stand at the beginning when we |
| 487 | * are either at the beginning of the memory or see a space or |
| 488 | * comma. (this is tolerant, it will accept a:10, b:11 as well as |
| 489 | * a:10,b:11) */ |
| 490 | while (1) { |
| 491 | /* Endless loop protection */ |
| 492 | if (count > 10000) |
| 493 | return NULL; |
| 494 | else if (ptr < options || *ptr == ' ' || *ptr == ',') { |
| 495 | ptr++; |
| 496 | break; |
| 497 | } |
| 498 | ptr--; |
| 499 | count++; |
| 500 | } |
| 501 | |
| 502 | /* Check if we got any result */ |
| 503 | if (*ptr == ':') |
| 504 | return NULL; |
| 505 | |
| 506 | return ptr; |
| 507 | } |
| 508 | |
| 509 | /*! Check the LCO option. This function checks for multiple appearence of LCO |
| 510 | * options, which is illegal |
| 511 | * \param[in] ctx talloc context |
| 512 | * \param[in] lco string |
| 513 | * \returns 0 on success, -1 on failure */ |
| 514 | int check_local_cx_options(void *ctx, const char *options) |
| 515 | { |
| 516 | int i; |
| 517 | char *options_copy; |
| 518 | char *lco_identifier; |
| 519 | char *lco_identifier_end; |
| 520 | char *next_lco_identifier; |
| 521 | |
| 522 | char **lco_seen; |
| 523 | unsigned int lco_seen_n = 0; |
| 524 | |
| 525 | if (!options) |
| 526 | return -1; |
| 527 | |
| 528 | lco_seen = |
| 529 | (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *)); |
| 530 | options_copy = talloc_strdup(ctx, options); |
| 531 | lco_identifier = options_copy; |
| 532 | |
| 533 | do { |
| 534 | /* Move the lco_identifier pointer to the beginning of the |
| 535 | * current lco option identifier */ |
| 536 | lco_identifier = get_lco_identifier(lco_identifier); |
| 537 | if (!lco_identifier) |
| 538 | goto error; |
| 539 | |
| 540 | /* Look ahead to the next LCO option early, since we |
| 541 | * will parse destructively */ |
| 542 | next_lco_identifier = strstr(lco_identifier + 1, ","); |
| 543 | |
| 544 | /* Pinch off the end of the lco field identifier name |
| 545 | * and see if we still got something, also check if |
| 546 | * there is some value after the colon. */ |
| 547 | lco_identifier_end = strstr(lco_identifier, ":"); |
| 548 | if (!lco_identifier_end) |
| 549 | goto error; |
| 550 | if (*(lco_identifier_end + 1) == ' ' |
| 551 | || *(lco_identifier_end + 1) == ',' |
| 552 | || *(lco_identifier_end + 1) == '\0') |
| 553 | goto error; |
| 554 | *lco_identifier_end = '\0'; |
| 555 | if (strlen(lco_identifier) == 0) |
| 556 | goto error; |
| 557 | |
| 558 | /* Check if we have already seen the current field identifier |
| 559 | * before. If yes, we must bail, an LCO must only appear once |
| 560 | * in the LCO string */ |
| 561 | for (i = 0; i < lco_seen_n; i++) { |
Pau Espin Pedrol | 7eb6f2c | 2019-06-26 13:00:52 +0200 | [diff] [blame] | 562 | if (strcasecmp(lco_seen[i], lco_identifier) == 0) |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 563 | goto error; |
| 564 | } |
| 565 | lco_seen[lco_seen_n] = lco_identifier; |
| 566 | lco_seen_n++; |
| 567 | |
| 568 | /* The first identifier must always be found at the beginnning |
| 569 | * of the LCO string */ |
| 570 | if (lco_seen[0] != options_copy) |
| 571 | goto error; |
| 572 | |
| 573 | /* Go to the next lco option */ |
| 574 | lco_identifier = next_lco_identifier; |
| 575 | } while (lco_identifier); |
| 576 | |
| 577 | talloc_free(lco_seen); |
| 578 | talloc_free(options_copy); |
| 579 | return 0; |
| 580 | error: |
| 581 | talloc_free(lco_seen); |
| 582 | talloc_free(options_copy); |
| 583 | return -1; |
| 584 | } |
| 585 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 586 | /* Set the LCO from a string (see RFC 3435). |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 587 | * The string is stored in the 'string' field. A NULL string is handled exactly |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 588 | * like an empty string, the 'string' field is never NULL after this function |
| 589 | * has been called. */ |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 590 | static int set_local_cx_options(void *ctx, struct mgcp_lco *lco, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 591 | const char *options) |
| 592 | { |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 593 | char *lco_id; |
Philipp Maier | 8dbc9ed | 2018-10-26 14:50:25 +0200 | [diff] [blame] | 594 | char codec[17]; |
Pau Espin Pedrol | 83fd8a5 | 2019-06-26 12:55:26 +0200 | [diff] [blame] | 595 | int len; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 596 | |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 597 | if (!options) |
| 598 | return 0; |
| 599 | if (strlen(options) == 0) |
| 600 | return 0; |
| 601 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 602 | /* Make sure the encoding of the LCO is consistant before we proceed */ |
| 603 | if (check_local_cx_options(ctx, options) != 0) { |
| 604 | LOGP(DLMGCP, LOGL_ERROR, |
| 605 | "local CX options: Internal inconsistency in Local Connection Options!\n"); |
| 606 | return 524; |
| 607 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 608 | |
Philipp Maier | 3d7b58d | 2018-06-06 09:35:31 +0200 | [diff] [blame] | 609 | talloc_free(lco->string); |
| 610 | lco->string = talloc_strdup(ctx, options); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 611 | |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 612 | lco_id = lco->string; |
| 613 | while ((lco_id = get_lco_identifier(lco_id))) { |
| 614 | switch (tolower(lco_id[0])) { |
| 615 | case 'p': |
| 616 | if (sscanf(lco_id + 1, ":%d-%d", |
| 617 | &lco->pkt_period_min, &lco->pkt_period_max) == 1) |
| 618 | lco->pkt_period_max = lco->pkt_period_min; |
| 619 | break; |
| 620 | case 'a': |
| 621 | /* FIXME: LCO also supports the negotiation of more then one codec. |
| 622 | * (e.g. a:PCMU;G726-32) But this implementation only supports a single |
| 623 | * codec only. */ |
| 624 | if (sscanf(lco_id + 1, ":%16[^,]", codec) == 1) { |
| 625 | talloc_free(lco->codec); |
Pau Espin Pedrol | 83fd8a5 | 2019-06-26 12:55:26 +0200 | [diff] [blame] | 626 | /* MGCP header is case insensive, and we'll need |
| 627 | codec in uppercase when using it later: */ |
| 628 | len = strlen(codec); |
| 629 | lco->codec = talloc_size(ctx, len + 1); |
| 630 | osmo_str_toupper_buf(lco->codec, len + 1, codec); |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 631 | } |
| 632 | break; |
| 633 | default: |
| 634 | LOGP(DLMGCP, LOGL_NOTICE, |
| 635 | "LCO: unhandled option: '%c'/%d in \"%s\"\n", |
| 636 | *lco_id, *lco_id, lco->string); |
| 637 | break; |
| 638 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 639 | |
Pau Espin Pedrol | fe9a1fe | 2019-06-25 16:59:15 +0200 | [diff] [blame] | 640 | lco_id = strchr(lco_id, ','); |
| 641 | if (!lco_id) |
| 642 | break; |
Philipp Maier | 604410c | 2018-06-06 10:02:16 +0200 | [diff] [blame] | 643 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 644 | |
| 645 | LOGP(DLMGCP, LOGL_DEBUG, |
| 646 | "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n", |
| 647 | lco->pkt_period_max, lco->codec); |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 648 | |
| 649 | /* Check if the packetization fits the 20ms raster */ |
| 650 | if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) { |
| 651 | LOGP(DLMGCP, LOGL_ERROR, |
| 652 | "local CX options: packetization interval is not a multiple of 20ms!\n"); |
| 653 | return 535; |
| 654 | } |
| 655 | |
| 656 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 660 | struct mgcp_rtp_end *rtp) |
| 661 | { |
| 662 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 663 | |
| 664 | int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc; |
| 665 | |
| 666 | rtp->force_aligned_timing = tcfg->force_aligned_timing; |
| 667 | rtp->force_constant_ssrc = patch_ssrc ? 1 : 0; |
Philipp Maier | 9fc8a02 | 2019-02-20 12:26:52 +0100 | [diff] [blame] | 668 | rtp->rfc5993_hr_convert = tcfg->rfc5993_hr_convert; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 669 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 670 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, |
| 671 | "Configuring RTP endpoint: local port %d%s%s\n", |
| 672 | ntohs(rtp->rtp_port), |
| 673 | rtp->force_aligned_timing ? ", force constant timing" : "", |
| 674 | rtp->force_constant_ssrc ? ", force constant ssrc" : ""); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 678 | struct mgcp_rtp_end *rtp) |
| 679 | { |
| 680 | int f = 0; |
| 681 | |
| 682 | /* Get the number of frames per channel and packet */ |
| 683 | if (rtp->frames_per_packet) |
| 684 | f = rtp->frames_per_packet; |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 685 | else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) { |
| 686 | int den = 1000 * rtp->codec->frame_duration_num; |
| 687 | f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den + |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 688 | den / 2) |
| 689 | / den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 692 | return rtp->codec->rate * f * rtp->codec->frame_duration_num / |
| 693 | rtp->codec->frame_duration_den; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Pau Espin Pedrol | 9fb8ddf | 2019-05-08 15:35:36 +0200 | [diff] [blame] | 696 | /*! Initializes osmux socket if not yet initialized. Parses Osmux CID from MGCP line. |
| 697 | * \param[in] endp Endpoint willing to initialize osmux |
| 698 | * \param[in] line Line X-Osmux from MGCP header msg to parse |
| 699 | * \returns OSMUX CID, -1 for wildcard, -2 on parse error, -3 on osmux initalize error |
| 700 | */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 701 | static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line) |
| 702 | { |
| 703 | if (!endp->cfg->osmux_init) { |
| 704 | if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 705 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n"); |
Pau Espin Pedrol | 9fb8ddf | 2019-05-08 15:35:36 +0200 | [diff] [blame] | 706 | return -3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 707 | } |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 708 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | return mgcp_parse_osmux_cid(line); |
| 712 | } |
| 713 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 714 | /* Process codec information contained in CRCX/MDCX */ |
| 715 | static int handle_codec_info(struct mgcp_conn_rtp *conn, |
| 716 | struct mgcp_parse_data *p, int have_sdp, bool crcx) |
| 717 | { |
| 718 | struct mgcp_endpoint *endp = p->endp; |
| 719 | int rc; |
| 720 | char *cmd; |
| 721 | |
| 722 | if (crcx) |
| 723 | cmd = "CRCX"; |
| 724 | else |
| 725 | cmd = "MDCX"; |
| 726 | |
| 727 | /* Collect codec information */ |
| 728 | if (have_sdp) { |
| 729 | /* If we have SDP, we ignore the local connection options and |
| 730 | * use only the SDP information. */ |
| 731 | mgcp_codec_reset_all(conn); |
| 732 | rc = mgcp_parse_sdp_data(endp, conn, p); |
| 733 | if (rc != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 734 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 735 | "%s: sdp not parseable\n", cmd); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 736 | |
| 737 | /* See also RFC 3661: Protocol error */ |
| 738 | return 510; |
| 739 | } |
| 740 | } else if (endp->local_options.codec) { |
| 741 | /* When no SDP is available, we use the codec information from |
| 742 | * the local connection options (if present) */ |
| 743 | mgcp_codec_reset_all(conn); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 744 | rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 745 | if (rc != 0) |
| 746 | goto error; |
| 747 | } |
| 748 | |
| 749 | /* Make sure we always set a sane default codec */ |
| 750 | if (conn->end.codecs_assigned == 0) { |
| 751 | /* When SDP and/or LCO did not supply any codec information, |
| 752 | * than it makes sense to pick a sane default: (payload-type 0, |
| 753 | * PCMU), see also: OS#2658 */ |
| 754 | mgcp_codec_reset_all(conn); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 755 | rc = mgcp_codec_add(conn, 0, NULL, NULL); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 756 | if (rc != 0) |
| 757 | goto error; |
| 758 | } |
| 759 | |
| 760 | /* Make codec decision */ |
| 761 | if (mgcp_codec_decide(conn) != 0) |
| 762 | goto error; |
| 763 | |
| 764 | return 0; |
| 765 | |
| 766 | error: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 767 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 768 | "%s: codec negotiation failure\n", cmd); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 769 | |
| 770 | /* See also RFC 3661: Codec negotiation failure */ |
| 771 | return 534; |
| 772 | } |
| 773 | |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 774 | static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line) |
| 775 | { |
| 776 | char *saveptr = NULL; |
| 777 | |
| 778 | if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER))) |
| 779 | return false; |
| 780 | line += strlen(MGCP_X_OSMO_IGN_HEADER); |
| 781 | |
| 782 | while (1) { |
| 783 | char *token = strtok_r(line, " ", &saveptr); |
| 784 | line = NULL; |
| 785 | if (!token) |
| 786 | break; |
| 787 | |
Pau Espin Pedrol | 6e26c70 | 2019-06-26 13:09:39 +0200 | [diff] [blame] | 788 | if (!strcasecmp(token, "C")) |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 789 | endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID; |
| 790 | else |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 791 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, "received unknown X-Osmo-IGN item '%s'\n", token); |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | return true; |
| 795 | } |
| 796 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 797 | /* CRCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 798 | static struct msgb *handle_create_con(struct mgcp_parse_data *p) |
| 799 | { |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 800 | struct mgcp_trunk_config *tcfg = p->endp->tcfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 801 | struct mgcp_endpoint *endp = p->endp; |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 802 | struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 803 | int error_code = 400; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 804 | const char *local_options = NULL; |
| 805 | const char *callid = NULL; |
| 806 | const char *mode = NULL; |
| 807 | char *line; |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 808 | int have_sdp = 0, osmux_cid = -2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 809 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 810 | struct mgcp_conn *_conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 811 | char conn_name[512]; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 812 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 813 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 814 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 815 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 816 | /* parse CallID C: and LocalParameters L: */ |
| 817 | for_each_line(line, p->save) { |
| 818 | if (!mgcp_check_param(endp, line)) |
| 819 | continue; |
| 820 | |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 821 | switch (toupper(line[0])) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 822 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 823 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 824 | break; |
| 825 | case 'C': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 826 | callid = (const char *)line + 3; |
| 827 | break; |
| 828 | case 'I': |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 829 | /* It is illegal to send a connection identifier |
| 830 | * together with a CRCX, the MGW will assign the |
| 831 | * connection identifier by itself on CRCX */ |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 832 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 833 | return create_err_response(NULL, 523, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 834 | break; |
| 835 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 836 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 837 | break; |
| 838 | case 'X': |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 839 | if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) { |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 840 | /* If osmux is disabled, just skip setting it up */ |
| 841 | if (!p->endp->cfg->osmux) |
| 842 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 843 | osmux_cid = mgcp_osmux_setup(endp, line); |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 847 | if (parse_x_osmo_ign(endp, line)) |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 848 | break; |
Neels Hofmeyr | f2388ea | 2018-08-26 23:36:53 +0200 | [diff] [blame] | 849 | |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 850 | /* Ignore unknown X-headers */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 851 | break; |
| 852 | case '\0': |
| 853 | have_sdp = 1; |
| 854 | goto mgcp_header_done; |
| 855 | default: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 856 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 857 | "CRCX: unhandled option: '%c'/%d\n", *line, *line); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 858 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]); |
Philipp Maier | dd0c522 | 2018-02-02 11:08:48 +0100 | [diff] [blame] | 859 | return create_err_response(NULL, 539, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 860 | break; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | mgcp_header_done: |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 865 | /* Check parameters */ |
| 866 | if (!callid) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 867 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 868 | "CRCX: insufficient parameters, missing callid\n"); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 869 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 870 | return create_err_response(endp, 516, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 873 | if (!mode) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 874 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 875 | "CRCX: insufficient parameters, missing mode\n"); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 876 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 877 | return create_err_response(endp, 517, "CRCX", p->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 878 | } |
| 879 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 880 | /* Check if we are able to accept the creation of another connection */ |
| 881 | if (llist_count(&endp->conns) >= endp->type->max_conns) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 882 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 883 | "CRCX: endpoint full, max. %i connections allowed!\n", |
| 884 | endp->type->max_conns); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 885 | if (tcfg->force_realloc) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 886 | /* There is no more room for a connection, make some |
| 887 | * room by blindly tossing the oldest of the two two |
| 888 | * connections */ |
| 889 | mgcp_conn_free_oldest(endp); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 890 | } else { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 891 | /* There is no more room for a connection, leave |
| 892 | * everything as it is and return with an error */ |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 893 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 894 | return create_err_response(endp, 540, "CRCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 898 | /* Check if this endpoint already serves a call, if so, check if the |
| 899 | * callids match up so that we are sure that this is our call */ |
| 900 | if (endp->callid && mgcp_verify_call_id(endp, callid)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 901 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 902 | "CRCX: already seized by other call (%s)\n", |
| 903 | endp->callid); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 904 | if (tcfg->force_realloc) |
| 905 | /* This is not our call, toss everything by releasing |
| 906 | * the entire endpoint. (rude!) */ |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 907 | mgcp_endp_release(endp); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 908 | else { |
| 909 | /* This is not our call, leave everything as it is and |
| 910 | * return with an error. */ |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 911 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 912 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* Set the callid, creation of another connection will only be possible |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 917 | * when the callid matches up. (Connections are distinguished by their |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 918 | * connection ids) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 919 | endp->callid = talloc_strdup(tcfg->endpoints, callid); |
| 920 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 921 | snprintf(conn_name, sizeof(conn_name), "%s", callid); |
| 922 | _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name); |
| 923 | if (!_conn) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 924 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 925 | "CRCX: unable to allocate RTP connection\n"); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 926 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 927 | goto error2; |
| 928 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 929 | } |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 930 | conn = mgcp_conn_get_rtp(endp, _conn->id); |
| 931 | OSMO_ASSERT(conn); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 932 | |
| 933 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
| 934 | error_code = 517; |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 935 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 936 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 937 | } |
| 938 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 939 | /* Annotate Osmux circuit ID and set it to negotiating state until this |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 940 | * is fully set up from the dummy load. */ |
| 941 | conn->osmux.state = OSMUX_STATE_DISABLED; |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 942 | if (osmux_cid >= -1) { /* -1 is wilcard, alloc next avail CID */ |
Pau Espin Pedrol | 14f8a08 | 2019-05-13 13:10:06 +0200 | [diff] [blame] | 943 | conn->osmux.state = OSMUX_STATE_ACTIVATING; |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 944 | if (conn_osmux_allocate_cid(conn, osmux_cid) == -1) { |
| 945 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]); |
| 946 | goto error2; |
| 947 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 948 | } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 949 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 950 | "CRCX: osmux only and no osmux offered\n"); |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 951 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 952 | goto error2; |
| 953 | } |
| 954 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 955 | /* Set local connection options, if present */ |
| 956 | if (local_options) { |
| 957 | rc = set_local_cx_options(endp->tcfg->endpoints, |
| 958 | &endp->local_options, local_options); |
| 959 | if (rc != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 960 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 961 | "CRCX: inavlid local connection options!\n"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 962 | error_code = rc; |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 963 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 964 | goto error2; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /* Handle codec information and decide for a suitable codec */ |
| 969 | rc = handle_codec_info(conn, p, have_sdp, true); |
| 970 | mgcp_codec_summary(conn); |
| 971 | if (rc) { |
| 972 | error_code = rc; |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 973 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 974 | goto error2; |
| 975 | } |
| 976 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 977 | conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints, |
| 978 | tcfg->audio_fmtp_extra); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 979 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 980 | if (p->cfg->force_ptime) { |
| 981 | conn->end.packet_duration_ms = p->cfg->force_ptime; |
| 982 | conn->end.force_output_ptime = 1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 983 | } |
| 984 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 985 | mgcp_rtp_end_config(endp, 0, &conn->end); |
| 986 | |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 987 | /* check connection mode setting */ |
| 988 | if (conn->conn->mode != MGCP_CONN_LOOPBACK |
| 989 | && conn->conn->mode != MGCP_CONN_RECV_ONLY |
| 990 | && conn->end.rtp_port == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 991 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 992 | "CRCX: selected connection mode type requires an opposite end!\n"); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 993 | error_code = 527; |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 994 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 995 | goto error2; |
| 996 | } |
| 997 | |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 998 | if (allocate_port(endp, conn) != 0) { |
Stefan Sperling | a714abf | 2018-10-29 14:19:54 +0100 | [diff] [blame] | 999 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]); |
Philipp Maier | 1cb1e38 | 2017-11-02 17:16:04 +0100 | [diff] [blame] | 1000 | goto error2; |
| 1001 | } |
| 1002 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1003 | if (setup_rtp_processing(endp, conn) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1004 | LOGPCONN(_conn, DLMGCP, LOGL_ERROR, |
| 1005 | "CRCX: could not start RTP processing!\n"); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 1006 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1007 | goto error2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1008 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1009 | |
| 1010 | /* policy CB */ |
| 1011 | if (p->cfg->policy_cb) { |
| 1012 | int rc; |
| 1013 | rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1014 | MGCP_ENDP_CRCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1015 | switch (rc) { |
| 1016 | case MGCP_POLICY_REJECT: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1017 | LOGPCONN(_conn, DLMGCP, LOGL_NOTICE, |
| 1018 | "CRCX: CRCX rejected by policy\n"); |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1019 | mgcp_endp_release(endp); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 1020 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1021 | return create_err_response(endp, 400, "CRCX", p->trans); |
| 1022 | break; |
| 1023 | case MGCP_POLICY_DEFER: |
| 1024 | /* stop processing */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1025 | return NULL; |
| 1026 | break; |
| 1027 | case MGCP_POLICY_CONT: |
| 1028 | /* just continue */ |
| 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1033 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, |
| 1034 | "CRCX: Creating connection: port: %u\n", conn->end.local_port); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1035 | if (p->cfg->change_cb) |
| 1036 | p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); |
| 1037 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1038 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 1039 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1040 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1041 | && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1042 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1043 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1044 | LOGPCONN(_conn, DLMGCP, LOGL_NOTICE, |
| 1045 | "CRCX: connection successfully created\n"); |
Stefan Sperling | 9270e91 | 2018-10-29 14:10:00 +0100 | [diff] [blame] | 1046 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 1047 | return create_response_with_sdp(endp, conn, "CRCX", p->trans, true); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1048 | error2: |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1049 | mgcp_endp_release(endp); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1050 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1051 | "CRCX: unable to create connection\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1052 | return create_err_response(endp, error_code, "CRCX", p->trans); |
| 1053 | } |
| 1054 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1059 | /* MDCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1060 | static struct msgb *handle_modify_con(struct mgcp_parse_data *p) |
| 1061 | { |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1062 | struct mgcp_trunk_config *tcfg = p->endp->tcfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1063 | struct mgcp_endpoint *endp = p->endp; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1064 | struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1065 | int error_code = 500; |
| 1066 | int silent = 0; |
| 1067 | int have_sdp = 0; |
| 1068 | char *line; |
| 1069 | const char *local_options = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1070 | const char *mode = NULL; |
| 1071 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1072 | const char *conn_id = NULL; |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1073 | int osmux_cid = -2; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 1074 | int rc; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1075 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1076 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1077 | |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1078 | /* Prohibit wildcarded requests */ |
| 1079 | if (endp->wildcarded_req) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1080 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1081 | "MDCX: wildcarded endpoint names not supported.\n"); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1082 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]); |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1083 | return create_err_response(endp, 507, "MDCX", p->trans); |
| 1084 | } |
| 1085 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1086 | if (llist_count(&endp->conns) <= 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1087 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1088 | "MDCX: endpoint is not holding a connection.\n"); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1089 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1090 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 1091 | } |
| 1092 | |
| 1093 | for_each_line(line, p->save) { |
| 1094 | if (!mgcp_check_param(endp, line)) |
| 1095 | continue; |
| 1096 | |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 1097 | switch (toupper(line[0])) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1098 | case 'C': |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1099 | if (mgcp_verify_call_id(endp, line + 3) != 0) { |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1100 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1101 | error_code = 516; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1102 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1103 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1104 | break; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1105 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1106 | conn_id = (const char *)line + 3; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1107 | if ((error_code = mgcp_verify_ci(endp, conn_id))) { |
| 1108 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1109 | goto error3; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1110 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1111 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1112 | case 'L': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1113 | local_options = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1114 | break; |
| 1115 | case 'M': |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1116 | mode = (const char *)line + 3; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1117 | break; |
| 1118 | case 'Z': |
Pau Espin Pedrol | 9b508f6 | 2019-06-26 13:11:22 +0200 | [diff] [blame] | 1119 | silent = strcasecmp("noanswer", line + 3) == 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1120 | break; |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1121 | case 'X': |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 1122 | if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) { |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1123 | /* If osmux is disabled, just skip setting it up */ |
| 1124 | if (!p->endp->cfg->osmux) |
| 1125 | break; |
| 1126 | osmux_cid = mgcp_osmux_setup(endp, line); |
| 1127 | break; |
| 1128 | } |
| 1129 | /* Ignore unknown X-headers */ |
| 1130 | break; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1131 | case '\0': |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1132 | have_sdp = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1133 | goto mgcp_header_done; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | default: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1136 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1137 | "MDCX: Unhandled MGCP option: '%c'/%d\n", |
| 1138 | line[0], line[0]); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1139 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]); |
Philipp Maier | dd0c522 | 2018-02-02 11:08:48 +0100 | [diff] [blame] | 1140 | return create_err_response(NULL, 539, "MDCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1145 | mgcp_header_done: |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1146 | if (!conn_id) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1147 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1148 | "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n"); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1149 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1150 | return create_err_response(endp, 515, "MDCX", p->trans); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | conn = mgcp_conn_get_rtp(endp, conn_id); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1154 | if (!conn) { |
| 1155 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1156 | return create_err_response(endp, 400, "MDCX", p->trans); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1157 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1158 | |
Oliver Smith | e36b775 | 2019-01-22 16:31:36 +0100 | [diff] [blame] | 1159 | mgcp_conn_watchdog_kick(conn->conn); |
| 1160 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1161 | if (mode) { |
| 1162 | if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) { |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1163 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1164 | error_code = 517; |
| 1165 | goto error3; |
| 1166 | } |
| 1167 | } else |
Pau Espin Pedrol | 209eb9f | 2019-04-24 12:03:04 +0200 | [diff] [blame] | 1168 | conn->conn->mode = conn->conn->mode_orig; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1169 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1170 | /* Set local connection options, if present */ |
| 1171 | if (local_options) { |
| 1172 | rc = set_local_cx_options(endp->tcfg->endpoints, |
| 1173 | &endp->local_options, local_options); |
| 1174 | if (rc != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1175 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1176 | "MDCX: invalid local connection options!\n"); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1177 | error_code = rc; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1178 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]); |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1179 | goto error3; |
| 1180 | } |
| 1181 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1182 | |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1183 | /* Handle codec information and decide for a suitable codec */ |
| 1184 | rc = handle_codec_info(conn, p, have_sdp, false); |
| 1185 | mgcp_codec_summary(conn); |
| 1186 | if (rc) { |
Philipp Maier | af07f66 | 2018-02-02 11:34:02 +0100 | [diff] [blame] | 1187 | error_code = rc; |
| 1188 | goto error3; |
Philipp Maier | a390d0b | 2018-01-31 17:30:19 +0100 | [diff] [blame] | 1189 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1190 | |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1191 | /* check connection mode setting */ |
| 1192 | if (conn->conn->mode != MGCP_CONN_LOOPBACK |
| 1193 | && conn->conn->mode != MGCP_CONN_RECV_ONLY |
| 1194 | && conn->end.rtp_port == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1195 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1196 | "MDCX: selected connection mode type requires an opposite end!\n"); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1197 | error_code = 527; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1198 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]); |
Philipp Maier | c3cc654 | 2018-02-02 12:58:42 +0100 | [diff] [blame] | 1199 | goto error3; |
| 1200 | } |
| 1201 | |
Pau Espin Pedrol | 6be2c49 | 2019-05-08 15:22:59 +0200 | [diff] [blame] | 1202 | if (mgcp_conn_rtp_is_osmux(conn)) { |
| 1203 | OSMO_ASSERT(conn->osmux.cid_allocated); |
| 1204 | if (osmux_cid < -1) { |
| 1205 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1206 | "MDCX: Failed to parse Osmux CID!\n"); |
| 1207 | goto error3; |
| 1208 | } else if (osmux_cid == -1) { |
| 1209 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1210 | "MDCX: wilcard in MDCX is not supported!\n"); |
| 1211 | goto error3; |
| 1212 | } else if (osmux_cid != (int) conn->osmux.cid) { |
| 1213 | LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR, |
| 1214 | "MDCX: changing already allocated CID is not supported!\n"); |
| 1215 | goto error3; |
| 1216 | } |
| 1217 | /* TODO: In the future (when we have recvCID!=sendCID), we need to |
| 1218 | tell Osmux code that osmux_cid is to be used as sendCID for |
| 1219 | that conn. */ |
| 1220 | } |
Philipp Maier | bc0346e | 2018-06-07 09:52:16 +0200 | [diff] [blame] | 1221 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1222 | if (setup_rtp_processing(endp, conn) != 0) { |
| 1223 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1224 | goto error3; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1225 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1226 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1227 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1228 | /* policy CB */ |
| 1229 | if (p->cfg->policy_cb) { |
| 1230 | int rc; |
| 1231 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1232 | MGCP_ENDP_MDCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1233 | switch (rc) { |
| 1234 | case MGCP_POLICY_REJECT: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1235 | LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE, |
| 1236 | "MDCX: rejected by policy\n"); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1237 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1238 | if (silent) |
| 1239 | goto out_silent; |
| 1240 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 1241 | break; |
| 1242 | case MGCP_POLICY_DEFER: |
| 1243 | /* stop processing */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1244 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, |
| 1245 | "MDCX: deferred by policy\n"); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1246 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1247 | return NULL; |
| 1248 | break; |
| 1249 | case MGCP_POLICY_CONT: |
| 1250 | /* just continue */ |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1255 | mgcp_rtp_end_config(endp, 1, &conn->end); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1256 | |
| 1257 | /* modify */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1258 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, |
| 1259 | "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn)); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1260 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1261 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 1262 | MGCP_ENDP_MDCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1263 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1264 | /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */ |
| 1265 | OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 1266 | if (conn->conn->mode & MGCP_CONN_RECV_ONLY |
| 1267 | && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1268 | send_dummy(endp, conn); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1269 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1270 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1271 | if (silent) |
| 1272 | goto out_silent; |
| 1273 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1274 | LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE, |
| 1275 | "MDCX: connection successfully modified\n"); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 1276 | return create_response_with_sdp(endp, conn, "MDCX", p->trans, false); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1277 | error3: |
| 1278 | return create_err_response(endp, error_code, "MDCX", p->trans); |
| 1279 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1280 | out_silent: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1281 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1282 | return NULL; |
| 1283 | } |
| 1284 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1285 | /* DLCX command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1286 | static struct msgb *handle_delete_con(struct mgcp_parse_data *p) |
| 1287 | { |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1288 | struct mgcp_trunk_config *tcfg = p->endp->tcfg; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1289 | struct mgcp_endpoint *endp = p->endp; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1290 | struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1291 | int error_code = 400; |
| 1292 | int silent = 0; |
| 1293 | char *line; |
| 1294 | char stats[1048]; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1295 | const char *conn_id = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1296 | struct mgcp_conn_rtp *conn = NULL; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1297 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1298 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1299 | "DLCX: deleting connection ...\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1300 | |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1301 | /* Prohibit wildcarded requests */ |
| 1302 | if (endp->wildcarded_req) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1303 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1304 | "DLCX: wildcarded endpoint names not supported.\n"); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1305 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]); |
Philipp Maier | 5656fbf | 2018-02-02 14:41:58 +0100 | [diff] [blame] | 1306 | return create_err_response(endp, 507, "DLCX", p->trans); |
| 1307 | } |
| 1308 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1309 | if (llist_count(&endp->conns) <= 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1310 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 1311 | "DLCX: endpoint is not holding a connection.\n"); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1312 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]); |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1313 | return create_err_response(endp, 515, "DLCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | for_each_line(line, p->save) { |
| 1317 | if (!mgcp_check_param(endp, line)) |
| 1318 | continue; |
| 1319 | |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 1320 | switch (toupper(line[0])) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1321 | case 'C': |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1322 | if (mgcp_verify_call_id(endp, line + 3) != 0) { |
| 1323 | error_code = 516; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1324 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1325 | goto error3; |
Harald Welte | e35eeae | 2017-12-28 13:47:37 +0100 | [diff] [blame] | 1326 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1327 | break; |
| 1328 | case 'I': |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1329 | conn_id = (const char *)line + 3; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1330 | if ((error_code = mgcp_verify_ci(endp, conn_id))) { |
| 1331 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1332 | goto error3; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1333 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1334 | break; |
| 1335 | case 'Z': |
Pau Espin Pedrol | 9b508f6 | 2019-06-26 13:11:22 +0200 | [diff] [blame] | 1336 | silent = strcasecmp("noanswer", line + 3) == 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1337 | break; |
| 1338 | default: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1339 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1340 | "DLCX: Unhandled MGCP option: '%c'/%d\n", |
| 1341 | line[0], line[0]); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1342 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]); |
Philipp Maier | dd0c522 | 2018-02-02 11:08:48 +0100 | [diff] [blame] | 1343 | return create_err_response(NULL, 539, "DLCX", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /* policy CB */ |
| 1349 | if (p->cfg->policy_cb) { |
| 1350 | int rc; |
| 1351 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1352 | MGCP_ENDP_DLCX, p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1353 | switch (rc) { |
| 1354 | case MGCP_POLICY_REJECT: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1355 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "DLCX: rejected by policy\n"); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1356 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1357 | if (silent) |
| 1358 | goto out_silent; |
| 1359 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 1360 | break; |
| 1361 | case MGCP_POLICY_DEFER: |
| 1362 | /* stop processing */ |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1363 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1364 | return NULL; |
| 1365 | break; |
| 1366 | case MGCP_POLICY_CONT: |
| 1367 | /* just continue */ |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1372 | /* When no connection id is supplied, we will interpret this as a |
| 1373 | * wildcarded DLCX and drop all connections at once. (See also |
| 1374 | * RFC3435 Section F.7) */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1375 | if (!conn_id) { |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1376 | int num_conns = llist_count(&endp->conns); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1377 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1378 | "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n", |
| 1379 | num_conns); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1380 | |
| 1381 | if (num_conns > 0) |
| 1382 | rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns); |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1383 | |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1384 | mgcp_endp_release(endp); |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1385 | |
| 1386 | /* Note: In this case we do not return any statistics, |
| 1387 | * as we assume that the client is not interested in |
| 1388 | * this case. */ |
| 1389 | return create_ok_response(endp, 200, "DLCX", p->trans); |
| 1390 | } |
| 1391 | |
Philipp Maier | f4c0e37 | 2017-10-11 16:06:45 +0200 | [diff] [blame] | 1392 | /* Find the connection */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1393 | conn = mgcp_conn_get_rtp(endp, conn_id); |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1394 | if (!conn) { |
| 1395 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1396 | goto error3; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1397 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1398 | /* save the statistics of the current connection */ |
| 1399 | mgcp_format_stats(stats, sizeof(stats), conn->conn); |
| 1400 | |
| 1401 | /* delete connection */ |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1402 | LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n", |
| 1403 | mgcp_conn_dump(conn->conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1404 | mgcp_conn_free(endp, conn_id); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1405 | LOGPENDP(endp, DLMGCP, LOGL_NOTICE, |
| 1406 | "DLCX: connection successfully deleted\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1407 | |
| 1408 | /* When all connections are closed, the endpoint will be released |
| 1409 | * in order to be ready to be used by another call. */ |
| 1410 | if (llist_count(&endp->conns) <= 0) { |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 1411 | mgcp_endp_release(endp); |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1412 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1413 | } |
| 1414 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1415 | if (p->cfg->change_cb) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1416 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 1417 | MGCP_ENDP_DLCX); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1418 | |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1419 | rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1420 | if (silent) |
| 1421 | goto out_silent; |
| 1422 | return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); |
| 1423 | |
| 1424 | error3: |
| 1425 | return create_err_response(endp, error_code, "DLCX", p->trans); |
| 1426 | |
| 1427 | out_silent: |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 1428 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1429 | return NULL; |
| 1430 | } |
| 1431 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1432 | /* RSIP command handler, processes the received command */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1433 | static struct msgb *handle_rsip(struct mgcp_parse_data *p) |
| 1434 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1435 | /* TODO: Also implement the resetting of a specific endpoint |
| 1436 | * to make mgcp_send_reset_ep() work. Currently this will call |
| 1437 | * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1 |
| 1438 | * to make read_call_agent() reset all endpoints when called |
| 1439 | * next time. In order to selectively reset endpoints some |
| 1440 | * mechanism to distinguish which endpoint shall be resetted |
| 1441 | * is needed */ |
| 1442 | |
| 1443 | LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n"); |
| 1444 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1445 | if (p->cfg->reset_cb) |
| 1446 | p->cfg->reset_cb(p->endp->tcfg); |
| 1447 | return NULL; |
| 1448 | } |
| 1449 | |
| 1450 | static char extract_tone(const char *line) |
| 1451 | { |
| 1452 | const char *str = strstr(line, "D/"); |
| 1453 | if (!str) |
| 1454 | return CHAR_MAX; |
| 1455 | |
| 1456 | return str[2]; |
| 1457 | } |
| 1458 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1459 | /* This can request like DTMF detection and forward, fax detection... it |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1460 | * can also request when the notification should be send and such. We don't |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1461 | * do this right now. */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1462 | static struct msgb *handle_noti_req(struct mgcp_parse_data *p) |
| 1463 | { |
| 1464 | int res = 0; |
| 1465 | char *line; |
| 1466 | char tone = CHAR_MAX; |
| 1467 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1468 | LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n"); |
| 1469 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1470 | for_each_line(line, p->save) { |
Pau Espin Pedrol | 0c6c3c1 | 2019-06-25 17:18:12 +0200 | [diff] [blame] | 1471 | switch (toupper(line[0])) { |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1472 | case 'S': |
| 1473 | tone = extract_tone(line); |
| 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /* we didn't see a signal request with a tone */ |
| 1479 | if (tone == CHAR_MAX) |
| 1480 | return create_ok_response(p->endp, 200, "RQNT", p->trans); |
| 1481 | |
| 1482 | if (p->cfg->rqnt_cb) |
| 1483 | res = p->cfg->rqnt_cb(p->endp, tone); |
| 1484 | |
| 1485 | return res == 0 ? |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1486 | create_ok_response(p->endp, 200, "RQNT", p->trans) : |
| 1487 | create_err_response(p->endp, res, "RQNT", p->trans); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1490 | /* Connection keepalive timer, will take care that dummy packets are send |
Harald Welte | 1d1b98f | 2017-12-25 10:03:40 +0100 | [diff] [blame] | 1491 | * regularly, so that NAT connections stay open */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1492 | static void mgcp_keepalive_timer_cb(void *_tcfg) |
| 1493 | { |
| 1494 | struct mgcp_trunk_config *tcfg = _tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1495 | struct mgcp_conn *conn; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1496 | int i; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1497 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1498 | LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1499 | tcfg->trunk_nr); |
| 1500 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1501 | /* Do not accept invalid configuration values |
| 1502 | * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and |
| 1503 | * values greater 0 */ |
| 1504 | OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE); |
| 1505 | |
| 1506 | /* The dummy packet functionality has been disabled, we will exit |
| 1507 | * immediately, no further timer is scheduled, which means we will no |
| 1508 | * longer send dummy packets even when we did before */ |
| 1509 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER) |
| 1510 | return; |
| 1511 | |
| 1512 | /* In cases where only one dummy packet is sent, we do not need |
| 1513 | * the timer since the functions that handle the CRCX and MDCX are |
| 1514 | * triggering the sending of the dummy packet. So we behave like in |
| 1515 | * the MGCP_KEEPALIVE_NEVER case */ |
| 1516 | if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1517 | return; |
| 1518 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1519 | /* Send walk over all endpoints and send out dummy packets through |
| 1520 | * every connection present on each endpoint */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1521 | for (i = 1; i < tcfg->number_endpoints; ++i) { |
| 1522 | struct mgcp_endpoint *endp = &tcfg->endpoints[i]; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1523 | llist_for_each_entry(conn, &endp->conns, entry) { |
| 1524 | if (conn->mode == MGCP_CONN_RECV_ONLY) |
| 1525 | send_dummy(endp, &conn->u.rtp); |
| 1526 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
Philipp Maier | e726d4f | 2017-11-01 10:41:34 +0100 | [diff] [blame] | 1529 | /* Schedule the keepalive timer for the next round */ |
| 1530 | LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1531 | tcfg->trunk_nr); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1532 | osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, |
| 1533 | 0); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval) |
| 1537 | { |
| 1538 | tcfg->keepalive_interval = interval; |
| 1539 | osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg); |
| 1540 | |
| 1541 | if (interval <= 0) |
| 1542 | osmo_timer_del(&tcfg->keepalive_timer); |
| 1543 | else |
| 1544 | osmo_timer_schedule(&tcfg->keepalive_timer, |
| 1545 | tcfg->keepalive_interval, 0); |
| 1546 | } |
| 1547 | |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 1548 | static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group) |
| 1549 | { |
| 1550 | rate_ctr_group_free(rate_ctr_group); |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1554 | static int alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx) |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 1555 | { |
| 1556 | /* FIXME: Each new rate counter group requires a unique index. At the |
| 1557 | * moment we generate an index using a counter, but perhaps there is |
| 1558 | * a better way of assigning indices? */ |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1559 | static unsigned int crcx_rate_ctr_index = 0; |
| 1560 | static unsigned int mdcx_rate_ctr_index = 0; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1561 | static unsigned int dlcx_rate_ctr_index = 0; |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 1562 | static unsigned int all_rtp_conn_rate_ctr_index = 0; |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 1563 | |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1564 | if (trunk->mgcp_crcx_ctr_group == NULL) { |
| 1565 | trunk->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, crcx_rate_ctr_index); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1566 | if (!trunk->mgcp_crcx_ctr_group) |
| 1567 | return -1; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1568 | talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group); |
| 1569 | crcx_rate_ctr_index++; |
| 1570 | } |
| 1571 | if (trunk->mgcp_mdcx_ctr_group == NULL) { |
| 1572 | trunk->mgcp_mdcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_mdcx_ctr_group_desc, mdcx_rate_ctr_index); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1573 | if (!trunk->mgcp_mdcx_ctr_group) |
| 1574 | return -1; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1575 | talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group); |
| 1576 | mdcx_rate_ctr_index++; |
| 1577 | } |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1578 | if (trunk->mgcp_dlcx_ctr_group == NULL) { |
| 1579 | trunk->mgcp_dlcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_dlcx_ctr_group_desc, dlcx_rate_ctr_index); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1580 | if (!trunk->mgcp_dlcx_ctr_group) |
| 1581 | return -1; |
Stefan Sperling | 8ab3fbb | 2018-10-30 14:57:25 +0100 | [diff] [blame] | 1582 | talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group); |
| 1583 | dlcx_rate_ctr_index++; |
| 1584 | } |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 1585 | if (trunk->all_rtp_conn_stats == NULL) { |
| 1586 | trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc, |
| 1587 | all_rtp_conn_rate_ctr_index); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1588 | if (!trunk->all_rtp_conn_stats) |
| 1589 | return -1; |
Stefan Sperling | ba25eab | 2018-10-30 14:32:31 +0100 | [diff] [blame] | 1590 | talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group); |
| 1591 | all_rtp_conn_rate_ctr_index++; |
| 1592 | } |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1593 | return 0; |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1596 | /*! allocate configuration with default values. |
| 1597 | * (called once at startup by main function) */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1598 | struct mgcp_config *mgcp_config_alloc(void) |
| 1599 | { |
| 1600 | struct mgcp_config *cfg; |
| 1601 | |
| 1602 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1603 | if (!cfg) { |
| 1604 | LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1608 | osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain)); |
| 1609 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1610 | cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; |
| 1611 | cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; |
| 1612 | cfg->net_ports.last_port = cfg->net_ports.range_start; |
| 1613 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1614 | cfg->source_port = 2427; |
| 1615 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1616 | cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0"); |
| 1617 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1618 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1619 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1620 | |
| 1621 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1622 | |
| 1623 | /* default trunk handling */ |
| 1624 | cfg->trunk.cfg = cfg; |
| 1625 | cfg->trunk.trunk_nr = 0; |
| 1626 | cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; |
| 1627 | cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1628 | cfg->trunk.audio_payload = 126; |
| 1629 | cfg->trunk.audio_send_ptime = 1; |
| 1630 | cfg->trunk.audio_send_name = 1; |
| 1631 | cfg->trunk.omit_rtcp = 0; |
| 1632 | mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE); |
Harald Welte | 3ac604e | 2019-05-08 14:07:41 +0200 | [diff] [blame] | 1633 | if (alloc_mgcp_rate_counters(&cfg->trunk, cfg) < 0) { |
| 1634 | talloc_free(cfg); |
| 1635 | return NULL; |
| 1636 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1637 | |
| 1638 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1639 | |
| 1640 | return cfg; |
| 1641 | } |
| 1642 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1643 | /*! allocate configuration with default values. |
| 1644 | * (called once at startup by VTY) |
| 1645 | * \param[in] cfg mgcp configuration |
| 1646 | * \param[in] nr trunk number |
| 1647 | * \returns pointer to allocated trunk configuration */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1648 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) |
| 1649 | { |
| 1650 | struct mgcp_trunk_config *trunk; |
| 1651 | |
| 1652 | trunk = talloc_zero(cfg, struct mgcp_trunk_config); |
| 1653 | if (!trunk) { |
| 1654 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n"); |
| 1655 | return NULL; |
| 1656 | } |
| 1657 | |
| 1658 | trunk->cfg = cfg; |
| 1659 | trunk->trunk_type = MGCP_TRUNK_E1; |
| 1660 | trunk->trunk_nr = nr; |
| 1661 | trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1662 | trunk->audio_payload = 126; |
| 1663 | trunk->audio_send_ptime = 1; |
| 1664 | trunk->audio_send_name = 1; |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1665 | trunk->vty_number_endpoints = 33; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1666 | trunk->omit_rtcp = 0; |
| 1667 | mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE); |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1668 | alloc_mgcp_rate_counters(trunk, trunk); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1669 | llist_add_tail(&trunk->entry, &cfg->trunks); |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 1670 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1671 | return trunk; |
| 1672 | } |
| 1673 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1674 | /*! get trunk configuration by trunk number (index). |
| 1675 | * \param[in] cfg mgcp configuration |
| 1676 | * \param[in] index trunk number |
| 1677 | * \returns pointer to trunk configuration, NULL on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1678 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) |
| 1679 | { |
| 1680 | struct mgcp_trunk_config *trunk; |
| 1681 | |
| 1682 | llist_for_each_entry(trunk, &cfg->trunks, entry) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1683 | if (trunk->trunk_nr == index) |
| 1684 | return trunk; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1685 | |
| 1686 | return NULL; |
| 1687 | } |
| 1688 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1689 | /*! allocate endpoints and set default values. |
| 1690 | * (called once at startup by VTY) |
| 1691 | * \param[in] tcfg trunk configuration |
| 1692 | * \returns 0 on success, -1 on failure */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1693 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) |
| 1694 | { |
| 1695 | int i; |
| 1696 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1697 | tcfg->endpoints = _talloc_zero_array(tcfg->cfg, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1698 | sizeof(struct mgcp_endpoint), |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1699 | tcfg->vty_number_endpoints, |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1700 | "endpoints"); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1701 | if (!tcfg->endpoints) |
| 1702 | return -1; |
| 1703 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1704 | for (i = 0; i < tcfg->vty_number_endpoints; ++i) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1705 | INIT_LLIST_HEAD(&tcfg->endpoints[i].conns); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1706 | tcfg->endpoints[i].cfg = tcfg->cfg; |
| 1707 | tcfg->endpoints[i].tcfg = tcfg; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1708 | |
Pau Espin Pedrol | 9f11dc5 | 2019-04-22 20:48:50 +0200 | [diff] [blame] | 1709 | switch (tcfg->trunk_type) { |
| 1710 | case MGCP_TRUNK_VIRTUAL: |
| 1711 | tcfg->endpoints[i].type = &ep_typeset.rtp; |
| 1712 | break; |
| 1713 | case MGCP_TRUNK_E1: |
| 1714 | /* FIXME: Implement E1 allocation */ |
| 1715 | LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n"); |
| 1716 | break; |
| 1717 | default: |
| 1718 | osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n", |
| 1719 | tcfg->trunk_type, __FILE__, __LINE__); |
| 1720 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
Philipp Maier | fcd0655 | 2017-11-10 17:32:22 +0100 | [diff] [blame] | 1723 | tcfg->number_endpoints = tcfg->vty_number_endpoints; |
Stefan Sperling | aa823bf | 2018-10-29 14:51:41 +0100 | [diff] [blame] | 1724 | alloc_mgcp_rate_counters(tcfg, tcfg->cfg); |
Stefan Sperling | 1e17487 | 2018-10-25 18:36:10 +0200 | [diff] [blame] | 1725 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1726 | return 0; |
| 1727 | } |
| 1728 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1729 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1730 | { |
| 1731 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1732 | } |
| 1733 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1734 | /*! Reset all endpoints by sending RSIP message to self. |
| 1735 | * (called by VTY) |
| 1736 | * \param[in] endp trunk endpoint |
| 1737 | * \param[in] endpoint number |
| 1738 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1739 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1740 | { |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1741 | char buf[MGCP_ENDPOINT_MAXLEN + 128]; |
| 1742 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1743 | int rc; |
| 1744 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1745 | len = snprintf(buf, sizeof(buf), |
| 1746 | "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain); |
| 1747 | if (len < 0) |
| 1748 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1749 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1750 | rc = send_agent(cfg, buf, len); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1751 | if (rc <= 0) |
| 1752 | return -1; |
| 1753 | |
| 1754 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1757 | /*! Reset a single endpoint by sending RSIP message to self. |
| 1758 | * (called by VTY) |
| 1759 | * \param[in] endp trunk endpoint |
| 1760 | * \param[in] endpoint number |
| 1761 | * \returns 0 on success, -1 on error */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1762 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) |
| 1763 | { |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1764 | char buf[MGCP_ENDPOINT_MAXLEN + 128]; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1765 | int len; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1766 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1767 | |
| 1768 | len = snprintf(buf, sizeof(buf), |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 1769 | "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1770 | if (len < 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1771 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1772 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1773 | rc = send_agent(endp->cfg, buf, len); |
| 1774 | if (rc <= 0) |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1775 | return -1; |
| 1776 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1777 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1778 | } |