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