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