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