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