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