Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 27 | #include <time.h> |
| 28 | #include <limits.h> |
| 29 | #include <unistd.h> |
Jacob Erlbeck | 8f518d6 | 2014-03-17 12:30:56 +0100 | [diff] [blame] | 30 | #include <errno.h> |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 31 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
Holger Hans Peter Freyther | 6d419f9 | 2011-02-28 22:06:47 +0100 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 36 | #include <openbsc/mgcp.h> |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 37 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 38 | |
Jacob Erlbeck | 7a6510c | 2013-11-29 13:43:43 +0100 | [diff] [blame] | 39 | #define for_each_non_empty_line(line, save) \ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 40 | for (line = strtok_r(NULL, "\r\n", &save); line;\ |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 41 | line = strtok_r(NULL, "\r\n", &save)) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 42 | |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 43 | |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 44 | static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 45 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 46 | struct mgcp_request { |
| 47 | char *name; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 48 | struct msgb *(*handle_request) (struct mgcp_parse_data *data); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [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 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 55 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data); |
| 56 | static struct msgb *handle_create_con(struct mgcp_parse_data *data); |
| 57 | static struct msgb *handle_delete_con(struct mgcp_parse_data *data); |
| 58 | static struct msgb *handle_modify_con(struct mgcp_parse_data *data); |
| 59 | static struct msgb *handle_rsip(struct mgcp_parse_data *data); |
| 60 | static struct msgb *handle_noti_req(struct mgcp_parse_data *data); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 61 | |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 62 | static void create_transcoder(struct mgcp_endpoint *endp); |
| 63 | static void delete_transcoder(struct mgcp_endpoint *endp); |
| 64 | |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 65 | static int setup_rtp_processing(struct mgcp_endpoint *endp); |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 66 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 67 | static int mgcp_analyze_header(struct mgcp_parse_data *parse, char *data); |
| 68 | |
Holger Hans Peter Freyther | 67f103e | 2014-05-22 15:03:10 +0200 | [diff] [blame] | 69 | static int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line) |
| 70 | { |
| 71 | const size_t line_len = strlen(line); |
| 72 | if (line[0] != '\0' && line_len < 2) { |
| 73 | LOGP(DMGCP, LOGL_ERROR, |
| 74 | "Wrong MGCP option format: '%s' on 0x%x\n", |
| 75 | line, ENDPOINT_NUMBER(endp)); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | return 1; |
| 80 | } |
| 81 | |
Holger Hans Peter Freyther | 526b4d3 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 82 | static uint32_t generate_call_id(struct mgcp_config *cfg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | |
| 86 | /* use the call id */ |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 87 | ++cfg->last_call_id; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 88 | |
| 89 | /* handle wrap around */ |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 90 | if (cfg->last_call_id == CI_UNUSED) |
| 91 | ++cfg->last_call_id; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 92 | |
| 93 | /* callstack can only be of size number_of_endpoints */ |
| 94 | /* verify that the call id is free, e.g. in case of overrun */ |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 95 | for (i = 1; i < cfg->trunk.number_endpoints; ++i) |
| 96 | if (cfg->trunk.endpoints[i].ci == cfg->last_call_id) |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 97 | return generate_call_id(cfg); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 98 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 99 | return cfg->last_call_id; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 102 | /* |
| 103 | * array of function pointers for handling various |
| 104 | * messages. In the future this might be binary sorted |
| 105 | * for performance reasons. |
| 106 | */ |
| 107 | static const struct mgcp_request mgcp_requests [] = { |
| 108 | MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint") |
| 109 | MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection") |
| 110 | MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection") |
| 111 | MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection") |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 112 | MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest") |
Holger Hans Peter Freyther | d60c6e5 | 2010-03-31 06:39:35 +0200 | [diff] [blame] | 113 | |
| 114 | /* SPEC extension */ |
| 115 | MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress") |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 118 | static struct msgb *mgcp_msgb_alloc(void) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 119 | { |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 120 | struct msgb *msg; |
| 121 | msg = msgb_alloc_headroom(4096, 128, "MGCP msg"); |
| 122 | if (!msg) |
| 123 | LOGP(DMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n"); |
| 124 | |
| 125 | return msg; |
| 126 | } |
| 127 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 128 | static struct msgb *do_retransmission(const struct mgcp_endpoint *endp) |
| 129 | { |
| 130 | struct msgb *msg = mgcp_msgb_alloc(); |
| 131 | if (!msg) |
| 132 | return NULL; |
| 133 | |
| 134 | msg->l2h = msgb_put(msg, strlen(endp->last_response)); |
| 135 | memcpy(msg->l2h, endp->last_response, msgb_l2len(msg)); |
| 136 | return msg; |
| 137 | } |
| 138 | |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 139 | static struct msgb *create_resp(struct mgcp_endpoint *endp, int code, |
| 140 | const char *txt, const char *msg, |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 141 | const char *trans, const char *param, |
| 142 | const char *sdp) |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 143 | { |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 144 | int len; |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 145 | struct msgb *res; |
| 146 | |
| 147 | res = mgcp_msgb_alloc(); |
| 148 | if (!res) |
| 149 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 150 | |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 151 | len = snprintf((char *) res->data, 2048, "%d %s%s%s\r\n%s", |
| 152 | code, trans, txt, param ? param : "", sdp ? sdp : ""); |
| 153 | if (len < 0) { |
| 154 | LOGP(DMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n"); |
| 155 | msgb_free(res); |
| 156 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 157 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 158 | |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 159 | res->l2h = msgb_put(res, len); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 160 | LOGP(DMGCP, LOGL_DEBUG, "Generated response: code: %d for '%s'\n", code, res->l2h); |
| 161 | |
| 162 | /* |
| 163 | * Remember the last transmission per endpoint. |
| 164 | */ |
| 165 | if (endp) { |
| 166 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 167 | talloc_free(endp->last_response); |
| 168 | talloc_free(endp->last_trans); |
| 169 | endp->last_trans = talloc_strdup(tcfg->endpoints, trans); |
| 170 | endp->last_response = talloc_strndup(tcfg->endpoints, |
| 171 | (const char *) res->l2h, |
| 172 | msgb_l2len(res)); |
| 173 | } |
| 174 | |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 175 | return res; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 178 | static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp, |
| 179 | int code, const char *msg, |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 180 | const char *trans, const char *param) |
| 181 | { |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 182 | return create_resp(endp, code, " OK", msg, trans, param, NULL); |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 185 | static struct msgb *create_ok_response(struct mgcp_endpoint *endp, |
| 186 | int code, const char *msg, const char *trans) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 187 | { |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 188 | return create_ok_resp_with_param(endp, code, msg, trans, NULL); |
Holger Hans Peter Freyther | 804019a | 2011-01-06 16:19:55 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 191 | static struct msgb *create_err_response(struct mgcp_endpoint *endp, |
| 192 | int code, const char *msg, const char *trans) |
Holger Hans Peter Freyther | 804019a | 2011-01-06 16:19:55 +0100 | [diff] [blame] | 193 | { |
Holger Hans Peter Freyther | 3ba9cce | 2012-12-07 15:14:27 +0100 | [diff] [blame] | 194 | return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 197 | static int write_response_sdp(struct mgcp_endpoint *endp, |
| 198 | char *sdp_record, size_t size, const char *addr) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 199 | { |
Jacob Erlbeck | fe0301a | 2014-03-17 12:40:07 +0100 | [diff] [blame] | 200 | const char *fmtp_extra; |
| 201 | const char *audio_name; |
| 202 | int payload_type; |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 203 | int len; |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 204 | int nchars; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 205 | |
Jacob Erlbeck | fe0301a | 2014-03-17 12:40:07 +0100 | [diff] [blame] | 206 | endp->cfg->get_net_downlink_format_cb(endp, &payload_type, |
| 207 | &audio_name, &fmtp_extra); |
| 208 | |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 209 | len = snprintf(sdp_record, size, |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 210 | "v=0\r\n" |
Holger Hans Peter Freyther | 2e0af51 | 2011-07-20 20:43:15 +0200 | [diff] [blame] | 211 | "o=- %u 23 IN IP4 %s\r\n" |
Holger Hans Peter Freyther | 37ae84a | 2014-11-20 23:17:32 +0100 | [diff] [blame] | 212 | "s=-\r\n" |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 213 | "c=IN IP4 %s\r\n" |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 214 | "t=0 0\r\n", |
| 215 | endp->ci, addr, addr); |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 216 | |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 217 | if (len < 0 || len >= size) |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 218 | goto buffer_too_small; |
| 219 | |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 220 | if (payload_type >= 0) { |
| 221 | nchars = snprintf(sdp_record + len, size - len, |
| 222 | "m=audio %d RTP/AVP %d\r\n", |
| 223 | endp->net_end.local_port, payload_type); |
| 224 | if (nchars < 0 || nchars >= size - len) |
| 225 | goto buffer_too_small; |
| 226 | |
| 227 | len += nchars; |
| 228 | |
Holger Hans Peter Freyther | 60e0992 | 2014-11-19 16:04:45 +0100 | [diff] [blame] | 229 | if (audio_name && endp->tcfg->audio_send_name) { |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 230 | nchars = snprintf(sdp_record + len, size - len, |
| 231 | "a=rtpmap:%d %s\r\n", |
| 232 | payload_type, audio_name); |
| 233 | |
| 234 | if (nchars < 0 || nchars >= size - len) |
| 235 | goto buffer_too_small; |
| 236 | |
| 237 | len += nchars; |
| 238 | } |
| 239 | |
| 240 | if (fmtp_extra) { |
| 241 | nchars = snprintf(sdp_record + len, size - len, |
Jacob Erlbeck | e09e5ba | 2014-06-12 12:42:38 +0200 | [diff] [blame] | 242 | "%s\r\n", fmtp_extra); |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 243 | |
| 244 | if (nchars < 0 || nchars >= size - len) |
| 245 | goto buffer_too_small; |
| 246 | |
| 247 | len += nchars; |
| 248 | } |
| 249 | } |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 250 | if (endp->bts_end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) { |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 251 | nchars = snprintf(sdp_record + len, size - len, |
| 252 | "a=ptime:%d\r\n", |
| 253 | endp->bts_end.packet_duration_ms); |
| 254 | if (nchars < 0 || nchars >= size - len) |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 255 | goto buffer_too_small; |
| 256 | |
| 257 | len += nchars; |
| 258 | } |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 259 | |
| 260 | return len; |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 261 | |
| 262 | buffer_too_small: |
Holger Hans Peter Freyther | f8b5a19 | 2015-01-02 11:25:54 +0100 | [diff] [blame] | 263 | LOGP(DMGCP, LOGL_ERROR, "SDP buffer too small: %zu (needed %d)\n", |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 264 | size, len); |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, |
| 269 | const char *msg, const char *trans_id) |
| 270 | { |
| 271 | const char *addr = endp->cfg->local_ip; |
| 272 | char sdp_record[4096]; |
| 273 | int len; |
| 274 | int nchars; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 275 | char osmux_extension[strlen("\nX-Osmux: 255") + 1]; |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 276 | |
| 277 | if (!addr) |
| 278 | addr = endp->cfg->source_addr; |
| 279 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 280 | if (endp->osmux.state == OSMUX_STATE_ACTIVATING) |
| 281 | sprintf(osmux_extension, "\nX-Osmux: %u", endp->osmux.cid); |
| 282 | else |
| 283 | osmux_extension[0] = '\0'; |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 284 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 285 | len = snprintf(sdp_record, sizeof(sdp_record), |
| 286 | "I: %u%s\n\n", endp->ci, osmux_extension); |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 287 | if (len < 0) |
| 288 | return NULL; |
| 289 | |
| 290 | nchars = write_response_sdp(endp, sdp_record + len, |
| 291 | sizeof(sdp_record) - len - 1, addr); |
| 292 | if (nchars < 0) |
| 293 | return NULL; |
| 294 | |
| 295 | len += nchars; |
| 296 | |
| 297 | sdp_record[sizeof(sdp_record) - 1] = '\0'; |
| 298 | |
| 299 | return create_resp(endp, 200, " OK", msg, trans_id, NULL, sdp_record); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Holger Hans Peter Freyther | 2b1a9e1 | 2014-07-08 13:01:56 +0200 | [diff] [blame] | 302 | static void send_dummy(struct mgcp_endpoint *endp) |
| 303 | { |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 304 | if (endp->osmux.state != OSMUX_STATE_DISABLED) |
Holger Hans Peter Freyther | 2b1a9e1 | 2014-07-08 13:01:56 +0200 | [diff] [blame] | 305 | osmux_send_dummy(endp); |
| 306 | else |
| 307 | mgcp_send_dummy(endp); |
| 308 | } |
| 309 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 310 | /* |
| 311 | * handle incoming messages: |
| 312 | * - this can be a command (four letters, space, transaction id) |
| 313 | * - or a response (three numbers, space, transaction id) |
| 314 | */ |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 315 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 316 | { |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 317 | struct mgcp_parse_data pdata; |
| 318 | int i, code, handled = 0; |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 319 | struct msgb *resp = NULL; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 320 | char *data; |
Jacob Erlbeck | 3c629c6 | 2013-12-03 17:14:44 +0100 | [diff] [blame] | 321 | unsigned char *tail = msg->l2h + msgb_l2len(msg); /* char after l2 data */ |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 322 | |
Holger Hans Peter Freyther | 0421be7 | 2010-03-31 13:26:46 +0200 | [diff] [blame] | 323 | if (msgb_l2len(msg) < 4) { |
Holger Hans Peter Freyther | bed02ba | 2012-12-07 16:46:19 +0100 | [diff] [blame] | 324 | LOGP(DMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 325 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Jacob Erlbeck | 3c629c6 | 2013-12-03 17:14:44 +0100 | [diff] [blame] | 328 | /* Ensure that the msg->l2h is NUL terminated. */ |
| 329 | if (tail[-1] == '\0') |
| 330 | /* nothing to do */; |
| 331 | else if (msgb_tailroom(msg) > 0) |
| 332 | tail[0] = '\0'; |
| 333 | else if (tail[-1] == '\r' || tail[-1] == '\n') |
| 334 | tail[-1] = '\0'; |
| 335 | else { |
| 336 | LOGP(DMGCP, LOGL_ERROR, "Cannot NUL terminate MGCP message: " |
| 337 | "Length: %d, Buffer size: %d\n", |
| 338 | msgb_l2len(msg), msg->data_len); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 342 | /* attempt to treat it as a response */ |
Holger Hans Peter Freyther | 0421be7 | 2010-03-31 13:26:46 +0200 | [diff] [blame] | 343 | if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { |
Holger Hans Peter Freyther | 40faed8 | 2010-02-26 13:30:57 +0100 | [diff] [blame] | 344 | LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); |
Holger Hans Peter Freyther | b139e2c | 2012-12-07 16:07:46 +0100 | [diff] [blame] | 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | msg->l3h = &msg->l2h[4]; |
| 349 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * Check for a duplicate message and respond. |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 353 | */ |
| 354 | memset(&pdata, 0, sizeof(pdata)); |
| 355 | pdata.cfg = cfg; |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 356 | data = strline_r((char *) msg->l3h, &pdata.save); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 357 | pdata.found = mgcp_analyze_header(&pdata, data); |
| 358 | if (pdata.endp && pdata.trans |
| 359 | && pdata.endp->last_trans |
| 360 | && strcmp(pdata.endp->last_trans, pdata.trans) == 0) { |
| 361 | return do_retransmission(pdata.endp); |
| 362 | } |
| 363 | |
Holger Hans Peter Freyther | b139e2c | 2012-12-07 16:07:46 +0100 | [diff] [blame] | 364 | for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { |
| 365 | if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) { |
| 366 | handled = 1; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 367 | resp = mgcp_requests[i].handle_request(&pdata); |
Holger Hans Peter Freyther | b139e2c | 2012-12-07 16:07:46 +0100 | [diff] [blame] | 368 | break; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Holger Hans Peter Freyther | b139e2c | 2012-12-07 16:07:46 +0100 | [diff] [blame] | 372 | if (!handled) |
| 373 | LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]); |
| 374 | |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 375 | return resp; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 378 | /** |
| 379 | * We have a null terminated string with the endpoint name here. We only |
| 380 | * support two kinds. Simple ones as seen on the BSC level and the ones |
| 381 | * seen on the trunk side. |
| 382 | */ |
| 383 | static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg, |
| 384 | const char *mgcp) |
| 385 | { |
| 386 | char *rest = NULL; |
Holger Hans Peter Freyther | 1d13712 | 2011-02-28 14:13:47 +0100 | [diff] [blame] | 387 | struct mgcp_trunk_config *tcfg; |
| 388 | int trunk, endp; |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 389 | |
| 390 | trunk = strtoul(mgcp + 6, &rest, 10); |
Holger Hans Peter Freyther | 72026c1 | 2011-01-07 11:30:21 +0100 | [diff] [blame] | 391 | if (rest == NULL || rest[0] != '/' || trunk < 1) { |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 392 | LOGP(DMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | endp = strtoul(rest + 1, &rest, 10); |
| 397 | if (rest == NULL || rest[0] != '@') { |
Holger Hans Peter Freyther | 72026c1 | 2011-01-07 11:30:21 +0100 | [diff] [blame] | 398 | LOGP(DMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp); |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | /* signalling is on timeslot 1 */ |
| 403 | if (endp == 1) |
| 404 | return NULL; |
| 405 | |
Holger Hans Peter Freyther | 1d13712 | 2011-02-28 14:13:47 +0100 | [diff] [blame] | 406 | tcfg = mgcp_trunk_num(cfg, trunk); |
| 407 | if (!tcfg) { |
| 408 | LOGP(DMGCP, LOGL_ERROR, "The trunk %d is not declared.\n", trunk); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | if (!tcfg->endpoints) { |
| 413 | LOGP(DMGCP, LOGL_ERROR, "Endpoints of trunk %d not allocated.\n", trunk); |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | if (endp < 1 || endp >= tcfg->number_endpoints) { |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 418 | LOGP(DMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp); |
Holger Hans Peter Freyther | 0f1786f | 2011-01-07 11:36:54 +0100 | [diff] [blame] | 419 | return NULL; |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Holger Hans Peter Freyther | 1d13712 | 2011-02-28 14:13:47 +0100 | [diff] [blame] | 422 | return &tcfg->endpoints[endp]; |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 425 | static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 426 | { |
| 427 | char *endptr = NULL; |
| 428 | unsigned int gw = INT_MAX; |
| 429 | |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 430 | if (strncmp(mgcp, "ds/e1", 5) == 0) { |
| 431 | return find_e1_endpoint(cfg, mgcp); |
| 432 | } else { |
| 433 | gw = strtoul(mgcp, &endptr, 16); |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 434 | if (gw > 0 && gw < cfg->trunk.number_endpoints && strcmp(endptr, "@mgw") == 0) |
| 435 | return &cfg->trunk.endpoints[gw]; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Holger Hans Peter Freyther | f0bc075 | 2011-01-06 19:32:52 +0100 | [diff] [blame] | 438 | LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp); |
| 439 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 442 | /** |
| 443 | * @returns 0 when the status line was complete and transaction_id and |
| 444 | * endp out parameters are set. |
| 445 | */ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 446 | static int mgcp_analyze_header(struct mgcp_parse_data *pdata, char *data) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 447 | { |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 448 | int i = 0; |
Holger Hans Peter Freyther | 0b6dca7 | 2013-07-03 10:23:35 +0200 | [diff] [blame] | 449 | char *elem, *save = NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 450 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 451 | pdata->trans = "000000"; |
Holger Hans Peter Freyther | 275c044 | 2010-02-26 13:32:55 +0100 | [diff] [blame] | 452 | |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 453 | for (elem = strtok_r(data, " ", &save); elem; |
| 454 | elem = strtok_r(NULL, " ", &save)) { |
| 455 | switch (i) { |
| 456 | case 0: |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 457 | pdata->trans = elem; |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 458 | break; |
| 459 | case 1: |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 460 | pdata->endp = find_endpoint(pdata->cfg, elem); |
| 461 | if (!pdata->endp) { |
| 462 | LOGP(DMGCP, LOGL_ERROR, |
| 463 | "Unable to find Endpoint `%s'\n", elem); |
| 464 | return -1; |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 465 | } |
| 466 | break; |
| 467 | case 2: |
| 468 | if (strcmp("MGCP", elem)) { |
| 469 | LOGP(DMGCP, LOGL_ERROR, |
| 470 | "MGCP header parsing error\n"); |
| 471 | return -1; |
| 472 | } |
| 473 | break; |
| 474 | case 3: |
| 475 | if (strcmp("1.0", elem)) { |
| 476 | LOGP(DMGCP, LOGL_ERROR, "MGCP version `%s' " |
| 477 | "not supported\n", elem); |
| 478 | return -1; |
| 479 | } |
| 480 | break; |
| 481 | } |
| 482 | i++; |
| 483 | } |
| 484 | |
| 485 | if (i != 4) { |
| 486 | LOGP(DMGCP, LOGL_ERROR, "MGCP status line too short.\n"); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 487 | pdata->trans = "000000"; |
| 488 | pdata->endp = NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 489 | return -1; |
| 490 | } |
| 491 | |
Holger Hans Peter Freyther | 9635463 | 2010-04-01 03:27:04 +0200 | [diff] [blame] | 492 | return 0; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static int verify_call_id(const struct mgcp_endpoint *endp, |
| 496 | const char *callid) |
| 497 | { |
| 498 | if (strcmp(endp->callid, callid) != 0) { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 499 | LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n", |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 500 | ENDPOINT_NUMBER(endp), endp->callid, callid); |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int verify_ci(const struct mgcp_endpoint *endp, |
Holger Hans Peter Freyther | 526b4d3 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 508 | const char *_ci) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 509 | { |
Holger Hans Peter Freyther | 526b4d3 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 510 | uint32_t ci = strtoul(_ci, NULL, 10); |
| 511 | |
| 512 | if (ci != endp->ci) { |
| 513 | LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %u != %s\n", |
| 514 | ENDPOINT_NUMBER(endp), endp->ci, _ci); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 521 | static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 522 | { |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 523 | if (p->found != 0) |
| 524 | return create_err_response(NULL, 500, "AUEP", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 525 | else |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 526 | return create_ok_response(p->endp, 200, "AUEP", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 529 | static int parse_conn_mode(const char *msg, struct mgcp_endpoint *endp) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 530 | { |
| 531 | int ret = 0; |
| 532 | if (strcmp(msg, "recvonly") == 0) |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 533 | endp->conn_mode = MGCP_CONN_RECV_ONLY; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 534 | else if (strcmp(msg, "sendrecv") == 0) |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 535 | endp->conn_mode = MGCP_CONN_RECV_SEND; |
Holger Hans Peter Freyther | 0bfcbee | 2010-09-18 20:21:44 +0800 | [diff] [blame] | 536 | else if (strcmp(msg, "sendonly") == 0) |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 537 | endp->conn_mode = MGCP_CONN_SEND_ONLY; |
Holger Hans Peter Freyther | 0bdf20d | 2010-08-03 02:27:21 +0800 | [diff] [blame] | 538 | else if (strcmp(msg, "loopback") == 0) |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 539 | endp->conn_mode = MGCP_CONN_LOOPBACK; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 540 | else { |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 541 | LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 542 | ret = -1; |
| 543 | } |
| 544 | |
Jacob Erlbeck | f318861 | 2014-01-16 16:50:40 +0100 | [diff] [blame] | 545 | endp->net_end.output_enabled = |
| 546 | endp->conn_mode & MGCP_CONN_SEND_ONLY ? 1 : 0; |
| 547 | endp->bts_end.output_enabled = |
| 548 | endp->conn_mode & MGCP_CONN_RECV_ONLY ? 1 : 0; |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 549 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 550 | return ret; |
| 551 | } |
| 552 | |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 553 | static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_rtp_end *end, |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 554 | struct mgcp_port_range *range, |
| 555 | int (*alloc)(struct mgcp_endpoint *endp, int port)) |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 556 | { |
| 557 | int i; |
| 558 | |
| 559 | if (range->mode == PORT_ALLOC_STATIC) { |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 560 | end->local_alloc = PORT_ALLOC_STATIC; |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /* attempt to find a port */ |
| 565 | for (i = 0; i < 200; ++i) { |
| 566 | int rc; |
| 567 | |
| 568 | if (range->last_port >= range->range_end) |
| 569 | range->last_port = range->range_start; |
| 570 | |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 571 | rc = alloc(endp, range->last_port); |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 572 | |
| 573 | range->last_port += 2; |
| 574 | if (rc == 0) { |
| 575 | end->local_alloc = PORT_ALLOC_DYNAMIC; |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | } |
| 580 | |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 581 | LOGP(DMGCP, LOGL_ERROR, "Allocating a RTP/RTCP port failed 200 times 0x%x.\n", |
| 582 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | static int allocate_ports(struct mgcp_endpoint *endp) |
| 587 | { |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 588 | if (allocate_port(endp, &endp->net_end, &endp->cfg->net_ports, |
| 589 | mgcp_bind_net_rtp_port) != 0) |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 590 | return -1; |
| 591 | |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 592 | if (allocate_port(endp, &endp->bts_end, &endp->cfg->bts_ports, |
| 593 | mgcp_bind_bts_rtp_port) != 0) { |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 594 | mgcp_rtp_end_reset(&endp->net_end); |
| 595 | return -1; |
| 596 | } |
| 597 | |
Holger Hans Peter Freyther | 1b8eaf4 | 2011-02-28 14:51:48 +0100 | [diff] [blame] | 598 | if (endp->cfg->transcoder_ip && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) { |
Holger Hans Peter Freyther | b6345ab | 2010-11-01 20:53:31 +0100 | [diff] [blame] | 599 | if (allocate_port(endp, &endp->trans_net, |
Holger Hans Peter Freyther | 1d9a8eb | 2010-11-01 19:57:50 +0100 | [diff] [blame] | 600 | &endp->cfg->transcoder_ports, |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 601 | mgcp_bind_trans_net_rtp_port) != 0) { |
Holger Hans Peter Freyther | 1d9a8eb | 2010-11-01 19:57:50 +0100 | [diff] [blame] | 602 | mgcp_rtp_end_reset(&endp->net_end); |
| 603 | mgcp_rtp_end_reset(&endp->bts_end); |
| 604 | return -1; |
| 605 | } |
| 606 | |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 607 | if (allocate_port(endp, &endp->trans_bts, |
| 608 | &endp->cfg->transcoder_ports, |
| 609 | mgcp_bind_trans_bts_rtp_port) != 0) { |
| 610 | mgcp_rtp_end_reset(&endp->net_end); |
| 611 | mgcp_rtp_end_reset(&endp->bts_end); |
| 612 | mgcp_rtp_end_reset(&endp->trans_net); |
| 613 | return -1; |
| 614 | } |
| 615 | |
Holger Hans Peter Freyther | 1d9a8eb | 2010-11-01 19:57:50 +0100 | [diff] [blame] | 616 | /* remember that we have set up transcoding */ |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 617 | endp->type = MGCP_RTP_TRANSCODED; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 618 | } |
| 619 | |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 623 | /* Set the LCO from a string (see RFC 3435). |
| 624 | * The string is stored in the 'string' field. A NULL string is handled excatly |
| 625 | * like an empty string, the 'string' field is never NULL after this function |
| 626 | * has been called. */ |
| 627 | static void set_local_cx_options(void *ctx, struct mgcp_lco *lco, |
| 628 | const char *options) |
| 629 | { |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 630 | char *p_opt, *a_opt; |
| 631 | char codec[9]; |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 632 | |
| 633 | talloc_free(lco->string); |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 634 | talloc_free(lco->codec); |
| 635 | lco->codec = NULL; |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 636 | lco->pkt_period_min = lco->pkt_period_max = 0; |
| 637 | lco->string = talloc_strdup(ctx, options ? options : ""); |
| 638 | |
| 639 | p_opt = strstr(lco->string, "p:"); |
| 640 | if (p_opt && sscanf(p_opt, "p:%d-%d", |
| 641 | &lco->pkt_period_min, &lco->pkt_period_max) == 1) |
| 642 | lco->pkt_period_max = lco->pkt_period_min; |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 643 | |
| 644 | a_opt = strstr(lco->string, "a:"); |
| 645 | if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) |
| 646 | lco->codec = talloc_strdup(ctx, codec); |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Jacob Erlbeck | e5546cd | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 649 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 650 | struct mgcp_rtp_end *rtp) |
| 651 | { |
| 652 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
| 653 | |
Jacob Erlbeck | 2b57b4c | 2013-12-03 15:13:12 +0100 | [diff] [blame] | 654 | int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc; |
| 655 | |
Jacob Erlbeck | f12c849 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 656 | rtp->force_aligned_timing = tcfg->force_aligned_timing; |
Jacob Erlbeck | 2b57b4c | 2013-12-03 15:13:12 +0100 | [diff] [blame] | 657 | rtp->force_constant_ssrc = patch_ssrc ? 1 : 0; |
Jacob Erlbeck | e5546cd | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 658 | |
| 659 | LOGP(DMGCP, LOGL_DEBUG, |
| 660 | "Configuring RTP endpoint: local port %d%s%s\n", |
| 661 | ntohs(rtp->rtp_port), |
Jacob Erlbeck | f12c849 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 662 | rtp->force_aligned_timing ? ", force constant timing" : "", |
Jacob Erlbeck | e5546cd | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 663 | rtp->force_constant_ssrc ? ", force constant ssrc" : ""); |
| 664 | } |
| 665 | |
Jacob Erlbeck | d69cf60 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 666 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 667 | struct mgcp_rtp_end *rtp) |
| 668 | { |
| 669 | int f = 0; |
| 670 | |
| 671 | /* Get the number of frames per channel and packet */ |
| 672 | if (rtp->frames_per_packet) |
| 673 | f = rtp->frames_per_packet; |
Holger Hans Peter Freyther | 4046126 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 674 | else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) { |
| 675 | int den = 1000 * rtp->codec.frame_duration_num; |
| 676 | f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den + den/2) |
Jacob Erlbeck | d69cf60 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 677 | / den; |
| 678 | } |
| 679 | |
Holger Hans Peter Freyther | 4046126 | 2014-09-01 10:35:55 +0200 | [diff] [blame] | 680 | return rtp->codec.rate * f * rtp->codec.frame_duration_num / rtp->codec.frame_duration_den; |
Jacob Erlbeck | d69cf60 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 683 | static int mgcp_parse_osmux_cid(const char *line) |
| 684 | { |
Pablo Neira Ayuso | 873c34f | 2014-08-29 08:46:05 +0200 | [diff] [blame] | 685 | int osmux_cid; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 686 | |
Pablo Neira Ayuso | 873c34f | 2014-08-29 08:46:05 +0200 | [diff] [blame] | 687 | if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1) |
| 688 | return -1; |
| 689 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 690 | if (osmux_cid > OSMUX_CID_MAX) { |
| 691 | LOGP(DMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n", |
| 692 | osmux_cid, OSMUX_CID_MAX); |
| 693 | return -1; |
| 694 | } |
| 695 | LOGP(DMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid); |
| 696 | |
| 697 | return osmux_cid; |
| 698 | } |
| 699 | |
| 700 | static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line) |
Pablo Neira Ayuso | 3374a5c | 2014-08-27 14:58:43 +0200 | [diff] [blame] | 701 | { |
| 702 | if (!endp->cfg->osmux_init) { |
| 703 | if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) { |
| 704 | LOGP(DMGCP, LOGL_ERROR, "Cannot init OSMUX\n"); |
| 705 | return -1; |
| 706 | } |
| 707 | LOGP(DMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n"); |
| 708 | } |
| 709 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 710 | return mgcp_parse_osmux_cid(line); |
Pablo Neira Ayuso | 3374a5c | 2014-08-27 14:58:43 +0200 | [diff] [blame] | 711 | } |
| 712 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 713 | static struct msgb *handle_create_con(struct mgcp_parse_data *p) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 714 | { |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 715 | struct mgcp_trunk_config *tcfg; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 716 | struct mgcp_endpoint *endp = p->endp; |
Holger Hans Peter Freyther | 94ed60f | 2010-08-08 16:24:48 +0800 | [diff] [blame] | 717 | int error_code = 400; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 718 | |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 719 | const char *local_options = NULL; |
| 720 | const char *callid = NULL; |
| 721 | const char *mode = NULL; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 722 | char *line; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 723 | int have_sdp = 0, osmux_cid = -1; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 724 | |
| 725 | if (p->found != 0) |
| 726 | return create_err_response(NULL, 510, "CRCX", p->trans); |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 727 | |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 728 | /* parse CallID C: and LocalParameters L: */ |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 729 | for_each_line(line, p->save) { |
Holger Hans Peter Freyther | 67f103e | 2014-05-22 15:03:10 +0200 | [diff] [blame] | 730 | if (!mgcp_check_param(endp, line)) |
| 731 | continue; |
| 732 | |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 733 | switch (line[0]) { |
| 734 | case 'L': |
| 735 | local_options = (const char *) line + 3; |
| 736 | break; |
| 737 | case 'C': |
| 738 | callid = (const char *) line + 3; |
| 739 | break; |
| 740 | case 'M': |
| 741 | mode = (const char *) line + 3; |
| 742 | break; |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 743 | case 'X': |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 744 | /* Osmux is not enabled in this bsc, ignore it so the |
| 745 | * bsc-nat knows that we don't want to use Osmux. |
| 746 | */ |
| 747 | if (!p->endp->cfg->osmux) |
| 748 | break; |
| 749 | |
| 750 | if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) |
| 751 | osmux_cid = mgcp_osmux_setup(endp, line); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 752 | break; |
Jacob Erlbeck | e3440c7 | 2013-11-29 13:43:49 +0100 | [diff] [blame] | 753 | case '\0': |
| 754 | have_sdp = 1; |
| 755 | goto mgcp_header_done; |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 756 | default: |
| 757 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
| 758 | *line, *line, ENDPOINT_NUMBER(endp)); |
| 759 | break; |
| 760 | } |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 761 | } |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 762 | |
Jacob Erlbeck | e3440c7 | 2013-11-29 13:43:49 +0100 | [diff] [blame] | 763 | mgcp_header_done: |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 764 | tcfg = p->endp->tcfg; |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 765 | |
| 766 | /* Check required data */ |
| 767 | if (!callid || !mode) { |
| 768 | LOGP(DMGCP, LOGL_ERROR, "Missing callid and mode in CRCX on 0x%x\n", |
| 769 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 770 | return create_err_response(endp, 400, "CRCX", p->trans); |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Holger Hans Peter Freyther | e15223b | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 773 | if (endp->allocated) { |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 774 | if (tcfg->force_realloc) { |
Holger Hans Peter Freyther | dd67a3b | 2010-04-07 10:51:27 +0200 | [diff] [blame] | 775 | LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n", |
| 776 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 777 | mgcp_release_endp(endp); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 778 | if (p->cfg->realloc_cb) |
| 779 | p->cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | dd67a3b | 2010-04-07 10:51:27 +0200 | [diff] [blame] | 780 | } else { |
| 781 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", |
| 782 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 783 | return create_err_response(endp, 400, "CRCX", p->trans); |
Holger Hans Peter Freyther | dd67a3b | 2010-04-07 10:51:27 +0200 | [diff] [blame] | 784 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 787 | /* copy some parameters */ |
| 788 | endp->callid = talloc_strdup(tcfg->endpoints, callid); |
| 789 | |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 790 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 791 | local_options); |
Holger Hans Peter Freyther | c9c0fe5 | 2011-07-20 20:44:54 +0200 | [diff] [blame] | 792 | |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 793 | if (parse_conn_mode(mode, endp) != 0) { |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 794 | error_code = 517; |
| 795 | goto error2; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 796 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 797 | |
| 798 | /* initialize */ |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 799 | endp->net_end.rtp_port = endp->net_end.rtcp_port = endp->bts_end.rtp_port = endp->bts_end.rtcp_port = 0; |
Jacob Erlbeck | e5546cd | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 800 | mgcp_rtp_end_config(endp, 0, &endp->net_end); |
| 801 | mgcp_rtp_end_config(endp, 0, &endp->bts_end); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 802 | |
| 803 | /* set to zero until we get the info */ |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 804 | memset(&endp->net_end.addr, 0, sizeof(endp->net_end.addr)); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 805 | |
| 806 | /* bind to the port now */ |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 807 | if (allocate_ports(endp) != 0) |
| 808 | goto error2; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 809 | |
| 810 | /* assign a local call identifier or fail */ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 811 | endp->ci = generate_call_id(p->cfg); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 812 | if (endp->ci == CI_UNUSED) |
| 813 | goto error2; |
| 814 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 815 | /* Annotate Osmux circuit ID and set it to activating state until this |
| 816 | * is fully set up from the dummy load. |
| 817 | */ |
| 818 | endp->osmux.state = OSMUX_STATE_DISABLED; |
| 819 | if (osmux_cid >= 0) { |
| 820 | endp->osmux.cid = osmux_cid; |
| 821 | endp->osmux.state = OSMUX_STATE_ACTIVATING; |
| 822 | } |
| 823 | |
Holger Hans Peter Freyther | e15223b | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 824 | endp->allocated = 1; |
Jacob Erlbeck | e3440c7 | 2013-11-29 13:43:49 +0100 | [diff] [blame] | 825 | |
| 826 | /* set up RTP media parameters */ |
Holger Hans Peter Freyther | 2e36bd1 | 2015-08-14 09:24:11 +0200 | [diff] [blame] | 827 | mgcp_set_audio_info(p->cfg, &endp->bts_end.codec, tcfg->audio_payload, tcfg->audio_name); |
Holger Hans Peter Freyther | 4928d1b | 2012-09-03 00:07:39 +0200 | [diff] [blame] | 828 | endp->bts_end.fmtp_extra = talloc_strdup(tcfg->endpoints, |
| 829 | tcfg->audio_fmtp_extra); |
Jacob Erlbeck | 16491a9 | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 830 | if (have_sdp) |
Holger Hans Peter Freyther | 2e36bd1 | 2015-08-14 09:24:11 +0200 | [diff] [blame] | 831 | mgcp_parse_sdp_data(&endp->net_end, p); |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 832 | else if (endp->local_options.codec) |
Holger Hans Peter Freyther | 2e36bd1 | 2015-08-14 09:24:11 +0200 | [diff] [blame] | 833 | mgcp_set_audio_info(p->cfg, &endp->net_end.codec, |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 834 | PTYPE_UNDEFINED, endp->local_options.codec); |
Jacob Erlbeck | 16491a9 | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 835 | |
| 836 | if (p->cfg->bts_force_ptime) { |
| 837 | endp->bts_end.packet_duration_ms = p->cfg->bts_force_ptime; |
| 838 | endp->bts_end.force_output_ptime = 1; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 839 | } |
Holger Hans Peter Freyther | 001dc61 | 2010-02-26 13:41:22 +0100 | [diff] [blame] | 840 | |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 841 | if (setup_rtp_processing(endp) != 0) |
| 842 | goto error2; |
Jacob Erlbeck | 16491a9 | 2014-04-14 10:31:47 +0200 | [diff] [blame] | 843 | |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 844 | /* policy CB */ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 845 | if (p->cfg->policy_cb) { |
| 846 | int rc; |
| 847 | rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), |
| 848 | MGCP_ENDP_CRCX, p->trans); |
| 849 | switch (rc) { |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 850 | case MGCP_POLICY_REJECT: |
| 851 | LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n", |
| 852 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 853 | mgcp_release_endp(endp); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 854 | return create_err_response(endp, 400, "CRCX", p->trans); |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 855 | break; |
| 856 | case MGCP_POLICY_DEFER: |
| 857 | /* stop processing */ |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 858 | create_transcoder(endp); |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 859 | return NULL; |
| 860 | break; |
| 861 | case MGCP_POLICY_CONT: |
| 862 | /* just continue */ |
| 863 | break; |
| 864 | } |
| 865 | } |
| 866 | |
Holger Hans Peter Freyther | 73d0047 | 2010-08-06 20:22:23 +0800 | [diff] [blame] | 867 | LOGP(DMGCP, LOGL_DEBUG, "Creating endpoint on: 0x%x CI: %u port: %u/%u\n", |
Holger Hans Peter Freyther | 148c5d3 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 868 | ENDPOINT_NUMBER(endp), endp->ci, |
| 869 | endp->net_end.local_port, endp->bts_end.local_port); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 870 | if (p->cfg->change_cb) |
| 871 | p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); |
Holger Hans Peter Freyther | 25374bd | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 872 | |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 873 | if (endp->conn_mode & MGCP_CONN_RECV_ONLY && tcfg->keepalive_interval != 0) { |
Holger Hans Peter Freyther | 2b1a9e1 | 2014-07-08 13:01:56 +0200 | [diff] [blame] | 874 | send_dummy(endp); |
Pablo Neira Ayuso | 4a2f659 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 875 | } |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 876 | |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 877 | create_transcoder(endp); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 878 | return create_response_with_sdp(endp, "CRCX", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 879 | error2: |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 880 | mgcp_release_endp(endp); |
Holger Hans Peter Freyther | 02c7a6f | 2010-02-03 09:13:10 +0100 | [diff] [blame] | 881 | LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 882 | return create_err_response(endp, error_code, "CRCX", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 885 | static struct msgb *handle_modify_con(struct mgcp_parse_data *p) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 886 | { |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 887 | struct mgcp_endpoint *endp = p->endp; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 888 | int error_code = 500; |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 889 | int silent = 0; |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 890 | int have_sdp = 0; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 891 | char *line; |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 892 | const char *local_options = NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 893 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 894 | if (p->found != 0) |
| 895 | return create_err_response(NULL, 510, "MDCX", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 896 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 897 | if (endp->ci == CI_UNUSED) { |
| 898 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not " |
| 899 | "holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); |
| 900 | return create_err_response(endp, 400, "MDCX", p->trans); |
| 901 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 902 | |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 903 | for_each_line(line, p->save) { |
Holger Hans Peter Freyther | 67f103e | 2014-05-22 15:03:10 +0200 | [diff] [blame] | 904 | if (!mgcp_check_param(endp, line)) |
| 905 | continue; |
| 906 | |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 907 | switch (line[0]) { |
| 908 | case 'C': { |
| 909 | if (verify_call_id(endp, line + 3) != 0) |
| 910 | goto error3; |
| 911 | break; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 912 | } |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 913 | case 'I': { |
| 914 | if (verify_ci(endp, line + 3) != 0) |
| 915 | goto error3; |
| 916 | break; |
| 917 | } |
| 918 | case 'L': |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 919 | local_options = (const char *) line + 3; |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 920 | break; |
| 921 | case 'M': |
Jacob Erlbeck | ecd157a | 2013-12-19 12:23:24 +0100 | [diff] [blame] | 922 | if (parse_conn_mode(line + 3, endp) != 0) { |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 923 | error_code = 517; |
| 924 | goto error3; |
| 925 | } |
| 926 | endp->orig_mode = endp->conn_mode; |
| 927 | break; |
| 928 | case 'Z': |
| 929 | silent = strcmp("noanswer", line + 3) == 0; |
| 930 | break; |
| 931 | case '\0': |
| 932 | /* SDP file begins */ |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 933 | have_sdp = 1; |
Holger Hans Peter Freyther | 2e36bd1 | 2015-08-14 09:24:11 +0200 | [diff] [blame] | 934 | mgcp_parse_sdp_data(&endp->net_end, p); |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 935 | /* This will exhaust p->save, so the loop will |
| 936 | * terminate next time. |
| 937 | */ |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 938 | break; |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 939 | default: |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 940 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled MGCP option: '%c'/%d on 0x%x\n", |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 941 | line[0], line[0], ENDPOINT_NUMBER(endp)); |
| 942 | break; |
| 943 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 944 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 945 | |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 946 | set_local_cx_options(endp->tcfg->endpoints, &endp->local_options, |
| 947 | local_options); |
| 948 | |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 949 | if (!have_sdp && endp->local_options.codec) |
Holger Hans Peter Freyther | 2e36bd1 | 2015-08-14 09:24:11 +0200 | [diff] [blame] | 950 | mgcp_set_audio_info(p->cfg, &endp->net_end.codec, |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 951 | PTYPE_UNDEFINED, endp->local_options.codec); |
| 952 | |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 953 | if (setup_rtp_processing(endp) != 0) |
| 954 | goto error3; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 955 | |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 956 | /* policy CB */ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 957 | if (p->cfg->policy_cb) { |
| 958 | int rc; |
| 959 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 960 | MGCP_ENDP_MDCX, p->trans); |
| 961 | switch (rc) { |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 962 | case MGCP_POLICY_REJECT: |
| 963 | LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n", |
| 964 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 965 | if (silent) |
| 966 | goto out_silent; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 967 | return create_err_response(endp, 400, "MDCX", p->trans); |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 968 | break; |
| 969 | case MGCP_POLICY_DEFER: |
| 970 | /* stop processing */ |
| 971 | return NULL; |
| 972 | break; |
| 973 | case MGCP_POLICY_CONT: |
| 974 | /* just continue */ |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | |
Jacob Erlbeck | e5546cd | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 979 | mgcp_rtp_end_config(endp, 1, &endp->net_end); |
| 980 | mgcp_rtp_end_config(endp, 1, &endp->bts_end); |
| 981 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 982 | /* modify */ |
Holger Hans Peter Freyther | 73d0047 | 2010-08-06 20:22:23 +0800 | [diff] [blame] | 983 | LOGP(DMGCP, LOGL_DEBUG, "Modified endpoint on: 0x%x Server: %s:%u\n", |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 984 | ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port)); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 985 | if (p->cfg->change_cb) |
| 986 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX); |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 987 | |
Jacob Erlbeck | f318861 | 2014-01-16 16:50:40 +0100 | [diff] [blame] | 988 | if (endp->conn_mode & MGCP_CONN_RECV_ONLY && |
| 989 | endp->tcfg->keepalive_interval != 0) |
Holger Hans Peter Freyther | 2b1a9e1 | 2014-07-08 13:01:56 +0200 | [diff] [blame] | 990 | send_dummy(endp); |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 991 | |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 992 | if (silent) |
| 993 | goto out_silent; |
| 994 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 995 | return create_response_with_sdp(endp, "MDCX", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 996 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 997 | error3: |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 998 | return create_err_response(endp, error_code, "MDCX", p->trans); |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 999 | |
| 1000 | |
| 1001 | out_silent: |
| 1002 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1005 | static struct msgb *handle_delete_con(struct mgcp_parse_data *p) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1006 | { |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1007 | struct mgcp_endpoint *endp = p->endp; |
Holger Hans Peter Freyther | 94ed60f | 2010-08-08 16:24:48 +0800 | [diff] [blame] | 1008 | int error_code = 400; |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 1009 | int silent = 0; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1010 | char *line; |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 1011 | char stats[1048]; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1012 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1013 | if (p->found != 0) |
| 1014 | return create_err_response(NULL, error_code, "DLCX", p->trans); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1015 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1016 | if (!p->endp->allocated) { |
| 1017 | LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", |
| 1018 | ENDPOINT_NUMBER(endp)); |
| 1019 | return create_err_response(endp, 400, "DLCX", p->trans); |
| 1020 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1021 | |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 1022 | for_each_line(line, p->save) { |
Holger Hans Peter Freyther | 67f103e | 2014-05-22 15:03:10 +0200 | [diff] [blame] | 1023 | if (!mgcp_check_param(endp, line)) |
| 1024 | continue; |
| 1025 | |
Harald Welte | ec4f01b | 2012-01-27 12:31:36 +0100 | [diff] [blame] | 1026 | switch (line[0]) { |
| 1027 | case 'C': |
| 1028 | if (verify_call_id(endp, line + 3) != 0) |
| 1029 | goto error3; |
| 1030 | break; |
| 1031 | case 'I': |
| 1032 | if (verify_ci(endp, line + 3) != 0) |
| 1033 | goto error3; |
| 1034 | break; |
| 1035 | case 'Z': |
| 1036 | silent = strcmp("noanswer", line + 3) == 0; |
| 1037 | break; |
| 1038 | default: |
| 1039 | LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", |
| 1040 | line[0], line[0], ENDPOINT_NUMBER(endp)); |
| 1041 | break; |
| 1042 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1043 | } |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1044 | |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 1045 | /* policy CB */ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1046 | if (p->cfg->policy_cb) { |
| 1047 | int rc; |
| 1048 | rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), |
| 1049 | MGCP_ENDP_DLCX, p->trans); |
| 1050 | switch (rc) { |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 1051 | case MGCP_POLICY_REJECT: |
| 1052 | LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n", |
| 1053 | ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 1054 | if (silent) |
| 1055 | goto out_silent; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1056 | return create_err_response(endp, 400, "DLCX", p->trans); |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 1057 | break; |
| 1058 | case MGCP_POLICY_DEFER: |
| 1059 | /* stop processing */ |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1060 | delete_transcoder(endp); |
Holger Hans Peter Freyther | 7a8ff2a | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 1061 | return NULL; |
| 1062 | break; |
| 1063 | case MGCP_POLICY_CONT: |
| 1064 | /* just continue */ |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1069 | /* free the connection */ |
Holger Hans Peter Freyther | 73d0047 | 2010-08-06 20:22:23 +0800 | [diff] [blame] | 1070 | LOGP(DMGCP, LOGL_DEBUG, "Deleted endpoint on: 0x%x Server: %s:%u\n", |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 1071 | ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port)); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1072 | |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 1073 | /* save the statistics of the current call */ |
| 1074 | mgcp_format_stats(endp, stats, sizeof(stats)); |
| 1075 | |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1076 | delete_transcoder(endp); |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 1077 | mgcp_release_endp(endp); |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1078 | if (p->cfg->change_cb) |
| 1079 | p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1080 | |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 1081 | if (silent) |
| 1082 | goto out_silent; |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1083 | return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1084 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1085 | error3: |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1086 | return create_err_response(endp, error_code, "DLCX", p->trans); |
Holger Hans Peter Freyther | e077c91 | 2010-04-22 11:58:13 +0800 | [diff] [blame] | 1087 | |
| 1088 | out_silent: |
| 1089 | return NULL; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1092 | static struct msgb *handle_rsip(struct mgcp_parse_data *p) |
Holger Hans Peter Freyther | d60c6e5 | 2010-03-31 06:39:35 +0200 | [diff] [blame] | 1093 | { |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1094 | if (p->found != 0) { |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 1095 | LOGP(DMGCP, LOGL_ERROR, "Failed to find the endpoint.\n"); |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1099 | if (p->cfg->reset_cb) |
| 1100 | p->cfg->reset_cb(p->endp->tcfg); |
Holger Hans Peter Freyther | d60c6e5 | 2010-03-31 06:39:35 +0200 | [diff] [blame] | 1101 | return NULL; |
| 1102 | } |
| 1103 | |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 1104 | static char extract_tone(const char *line) |
| 1105 | { |
| 1106 | const char *str = strstr(line, "D/"); |
| 1107 | if (!str) |
| 1108 | return CHAR_MAX; |
| 1109 | |
| 1110 | return str[2]; |
| 1111 | } |
| 1112 | |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 1113 | /* |
| 1114 | * This can request like DTMF detection and forward, fax detection... it |
| 1115 | * can also request when the notification should be send and such. We don't |
| 1116 | * do this right now. |
| 1117 | */ |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1118 | static struct msgb *handle_noti_req(struct mgcp_parse_data *p) |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 1119 | { |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1120 | int res = 0; |
| 1121 | char *line; |
Holger Hans Peter Freyther | a229748 | 2013-05-21 17:04:35 +0200 | [diff] [blame] | 1122 | char tone = CHAR_MAX; |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 1123 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1124 | if (p->found != 0) |
| 1125 | return create_err_response(NULL, 400, "RQNT", p->trans); |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 1126 | |
Jacob Erlbeck | 9cec667 | 2013-11-29 13:43:46 +0100 | [diff] [blame] | 1127 | for_each_line(line, p->save) { |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 1128 | switch (line[0]) { |
| 1129 | case 'S': |
| 1130 | tone = extract_tone(line); |
| 1131 | break; |
| 1132 | } |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 1133 | } |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 1134 | |
| 1135 | /* we didn't see a signal request with a tone */ |
| 1136 | if (tone == CHAR_MAX) |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1137 | return create_ok_response(p->endp, 200, "RQNT", p->trans); |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 1138 | |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1139 | if (p->cfg->rqnt_cb) |
| 1140 | res = p->cfg->rqnt_cb(p->endp, tone); |
Holger Hans Peter Freyther | 46880c2 | 2012-11-29 12:54:22 +0100 | [diff] [blame] | 1141 | |
| 1142 | return res == 0 ? |
Holger Hans Peter Freyther | cd25695 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 1143 | create_ok_response(p->endp, 200, "RQNT", p->trans) : |
| 1144 | create_err_response(p->endp, res, "RQNT", p->trans); |
Holger Hans Peter Freyther | ac0968e | 2011-01-25 23:33:54 +0100 | [diff] [blame] | 1145 | } |
| 1146 | |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 1147 | static void mgcp_keepalive_timer_cb(void *_tcfg) |
| 1148 | { |
| 1149 | struct mgcp_trunk_config *tcfg = _tcfg; |
| 1150 | int i; |
| 1151 | LOGP(DMGCP, LOGL_DEBUG, "Triggered trunk %d keepalive timer.\n", |
| 1152 | tcfg->trunk_nr); |
| 1153 | |
| 1154 | if (tcfg->keepalive_interval <= 0) |
| 1155 | return; |
| 1156 | |
| 1157 | for (i = 1; i < tcfg->number_endpoints; ++i) { |
| 1158 | struct mgcp_endpoint *endp = &tcfg->endpoints[i]; |
| 1159 | if (endp->conn_mode == MGCP_CONN_RECV_ONLY) |
Holger Hans Peter Freyther | 2b1a9e1 | 2014-07-08 13:01:56 +0200 | [diff] [blame] | 1160 | send_dummy(endp); |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | LOGP(DMGCP, LOGL_DEBUG, "Rescheduling trunk %d keepalive timer.\n", |
| 1164 | tcfg->trunk_nr); |
| 1165 | osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, 0); |
| 1166 | } |
| 1167 | |
| 1168 | void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval) |
| 1169 | { |
| 1170 | tcfg->keepalive_interval = interval; |
| 1171 | tcfg->keepalive_timer.data = tcfg; |
| 1172 | tcfg->keepalive_timer.cb = mgcp_keepalive_timer_cb; |
| 1173 | |
| 1174 | if (interval <= 0) |
| 1175 | osmo_timer_del(&tcfg->keepalive_timer); |
| 1176 | else |
| 1177 | osmo_timer_schedule(&tcfg->keepalive_timer, |
| 1178 | tcfg->keepalive_interval, 0); |
| 1179 | } |
| 1180 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1181 | struct mgcp_config *mgcp_config_alloc(void) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1182 | { |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1183 | struct mgcp_config *cfg; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1184 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1185 | cfg = talloc_zero(NULL, struct mgcp_config); |
| 1186 | if (!cfg) { |
| 1187 | LOGP(DMGCP, LOGL_FATAL, "Failed to allocate config.\n"); |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
| 1191 | cfg->source_port = 2427; |
| 1192 | cfg->source_addr = talloc_strdup(cfg, "0.0.0.0"); |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1193 | |
Holger Hans Peter Freyther | d582b20 | 2010-09-19 04:21:39 +0800 | [diff] [blame] | 1194 | cfg->transcoder_remote_base = 4000; |
Holger Hans Peter Freyther | 2ea9118 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 1195 | |
| 1196 | cfg->bts_ports.base_port = RTP_PORT_DEFAULT; |
| 1197 | cfg->net_ports.base_port = RTP_PORT_NET_DEFAULT; |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1198 | |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1199 | cfg->rtp_processing_cb = &mgcp_rtp_processing_default; |
| 1200 | cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default; |
| 1201 | |
Jacob Erlbeck | fe0301a | 2014-03-17 12:40:07 +0100 | [diff] [blame] | 1202 | cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default; |
| 1203 | |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1204 | /* default trunk handling */ |
| 1205 | cfg->trunk.cfg = cfg; |
| 1206 | cfg->trunk.trunk_nr = 0; |
| 1207 | cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL; |
| 1208 | cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1209 | cfg->trunk.audio_payload = 126; |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 1210 | cfg->trunk.audio_send_ptime = 1; |
Holger Hans Peter Freyther | 60e0992 | 2014-11-19 16:04:45 +0100 | [diff] [blame] | 1211 | cfg->trunk.audio_send_name = 1; |
Holger Hans Peter Freyther | 51ade84 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 1212 | cfg->trunk.omit_rtcp = 0; |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 1213 | mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE); |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1214 | |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 1215 | INIT_LLIST_HEAD(&cfg->trunks); |
| 1216 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1217 | return cfg; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 1220 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr) |
| 1221 | { |
| 1222 | struct mgcp_trunk_config *trunk; |
| 1223 | |
| 1224 | trunk = talloc_zero(cfg, struct mgcp_trunk_config); |
| 1225 | if (!trunk) { |
| 1226 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate.\n"); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | |
| 1230 | trunk->cfg = cfg; |
| 1231 | trunk->trunk_type = MGCP_TRUNK_E1; |
| 1232 | trunk->trunk_nr = nr; |
| 1233 | trunk->audio_name = talloc_strdup(cfg, "AMR/8000"); |
| 1234 | trunk->audio_payload = 126; |
Jacob Erlbeck | e91324d | 2013-12-10 13:09:37 +0100 | [diff] [blame] | 1235 | trunk->audio_send_ptime = 1; |
Holger Hans Peter Freyther | 60e0992 | 2014-11-19 16:04:45 +0100 | [diff] [blame] | 1236 | trunk->audio_send_name = 1; |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 1237 | trunk->number_endpoints = 33; |
Holger Hans Peter Freyther | 51ade84 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 1238 | trunk->omit_rtcp = 0; |
Jacob Erlbeck | e64bb12 | 2013-12-19 18:53:07 +0100 | [diff] [blame] | 1239 | mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE); |
Holger Hans Peter Freyther | 06bfe79 | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 1240 | llist_add_tail(&trunk->entry, &cfg->trunks); |
| 1241 | return trunk; |
| 1242 | } |
| 1243 | |
| 1244 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index) |
| 1245 | { |
| 1246 | struct mgcp_trunk_config *trunk; |
| 1247 | |
| 1248 | llist_for_each_entry(trunk, &cfg->trunks, entry) |
| 1249 | if (trunk->trunk_nr == index) |
| 1250 | return trunk; |
| 1251 | |
| 1252 | return NULL; |
| 1253 | } |
| 1254 | |
Holger Hans Peter Freyther | ebc15a7 | 2014-09-01 10:40:19 +0200 | [diff] [blame] | 1255 | static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec) |
| 1256 | { |
| 1257 | codec->payload_type = -1; |
| 1258 | talloc_free(codec->subtype_name); |
| 1259 | codec->subtype_name = NULL; |
| 1260 | talloc_free(codec->audio_name); |
| 1261 | codec->audio_name = NULL; |
| 1262 | codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM; |
| 1263 | codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN; |
| 1264 | codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE; |
| 1265 | codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS; |
| 1266 | } |
| 1267 | |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 1268 | static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end) |
| 1269 | { |
Holger Hans Peter Freyther | 6f94bac | 2010-11-02 19:06:13 +0100 | [diff] [blame] | 1270 | if (end->local_alloc == PORT_ALLOC_DYNAMIC) { |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1271 | mgcp_free_rtp_port(end); |
Holger Hans Peter Freyther | 6f94bac | 2010-11-02 19:06:13 +0100 | [diff] [blame] | 1272 | end->local_port = 0; |
| 1273 | } |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1274 | |
Holger Hans Peter Freyther | bfb68b7 | 2010-08-05 03:37:22 +0800 | [diff] [blame] | 1275 | end->packets = 0; |
Holger Hans Peter Freyther | 3e45610 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 1276 | end->octets = 0; |
Jacob Erlbeck | 8c1f4b0 | 2013-12-19 12:13:32 +0100 | [diff] [blame] | 1277 | end->dropped_packets = 0; |
Holger Hans Peter Freyther | bfb68b7 | 2010-08-05 03:37:22 +0800 | [diff] [blame] | 1278 | memset(&end->addr, 0, sizeof(end->addr)); |
Holger Hans Peter Freyther | 6f94bac | 2010-11-02 19:06:13 +0100 | [diff] [blame] | 1279 | end->rtp_port = end->rtcp_port = 0; |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 1280 | end->local_alloc = -1; |
Holger Hans Peter Freyther | 4928d1b | 2012-09-03 00:07:39 +0200 | [diff] [blame] | 1281 | talloc_free(end->fmtp_extra); |
| 1282 | end->fmtp_extra = NULL; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1283 | talloc_free(end->rtp_process_data); |
| 1284 | end->rtp_process_data = NULL; |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 1285 | |
| 1286 | /* Set default values */ |
Jacob Erlbeck | d69cf60 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 1287 | end->frames_per_packet = 0; /* unknown */ |
| 1288 | end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS; |
Jacob Erlbeck | b866d3b | 2014-01-16 16:50:39 +0100 | [diff] [blame] | 1289 | end->output_enabled = 0; |
Holger Hans Peter Freyther | ebc15a7 | 2014-09-01 10:40:19 +0200 | [diff] [blame] | 1290 | |
| 1291 | mgcp_rtp_codec_reset(&end->codec); |
Holger Hans Peter Freyther | 171817c | 2014-09-01 10:50:22 +0200 | [diff] [blame] | 1292 | mgcp_rtp_codec_reset(&end->alt_codec); |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Holger Hans Peter Freyther | bfb68b7 | 2010-08-05 03:37:22 +0800 | [diff] [blame] | 1295 | static void mgcp_rtp_end_init(struct mgcp_rtp_end *end) |
| 1296 | { |
| 1297 | mgcp_rtp_end_reset(end); |
| 1298 | end->rtp.fd = -1; |
| 1299 | end->rtcp.fd = -1; |
| 1300 | } |
| 1301 | |
Holger Hans Peter Freyther | 49e170b | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 1302 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg) |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1303 | { |
| 1304 | int i; |
| 1305 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1306 | /* Initialize all endpoints */ |
Holger Hans Peter Freyther | 49e170b | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 1307 | tcfg->endpoints = _talloc_zero_array(tcfg->cfg, |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1308 | sizeof(struct mgcp_endpoint), |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1309 | tcfg->number_endpoints, "endpoints"); |
| 1310 | if (!tcfg->endpoints) |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1311 | return -1; |
| 1312 | |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1313 | for (i = 0; i < tcfg->number_endpoints; ++i) { |
| 1314 | tcfg->endpoints[i].ci = CI_UNUSED; |
Holger Hans Peter Freyther | 49e170b | 2011-02-28 14:37:03 +0100 | [diff] [blame] | 1315 | tcfg->endpoints[i].cfg = tcfg->cfg; |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1316 | tcfg->endpoints[i].tcfg = tcfg; |
| 1317 | mgcp_rtp_end_init(&tcfg->endpoints[i].net_end); |
| 1318 | mgcp_rtp_end_init(&tcfg->endpoints[i].bts_end); |
| 1319 | mgcp_rtp_end_init(&tcfg->endpoints[i].trans_net); |
| 1320 | mgcp_rtp_end_init(&tcfg->endpoints[i].trans_bts); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 1325 | |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 1326 | void mgcp_release_endp(struct mgcp_endpoint *endp) |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 1327 | { |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 1328 | LOGP(DMGCP, LOGL_DEBUG, "Releasing endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | fd93fc0 | 2010-08-03 15:01:43 +0000 | [diff] [blame] | 1329 | endp->ci = CI_UNUSED; |
Holger Hans Peter Freyther | e15223b | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 1330 | endp->allocated = 0; |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 1331 | |
Holger Hans Peter Freyther | e9564b2 | 2012-12-07 11:52:16 +0100 | [diff] [blame] | 1332 | talloc_free(endp->callid); |
| 1333 | endp->callid = NULL; |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 1334 | |
Jacob Erlbeck | 91cf743 | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 1335 | talloc_free(endp->local_options.string); |
| 1336 | endp->local_options.string = NULL; |
Jacob Erlbeck | 0557c00 | 2014-05-09 19:42:17 +0200 | [diff] [blame] | 1337 | talloc_free(endp->local_options.codec); |
| 1338 | endp->local_options.codec = NULL; |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 1339 | |
Holger Hans Peter Freyther | fab76aa | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 1340 | mgcp_rtp_end_reset(&endp->bts_end); |
| 1341 | mgcp_rtp_end_reset(&endp->net_end); |
Holger Hans Peter Freyther | b6345ab | 2010-11-01 20:53:31 +0100 | [diff] [blame] | 1342 | mgcp_rtp_end_reset(&endp->trans_net); |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 1343 | mgcp_rtp_end_reset(&endp->trans_bts); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 1344 | endp->type = MGCP_RTP_DEFAULT; |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 1345 | |
Holger Hans Peter Freyther | 84ba528 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 1346 | memset(&endp->net_state, 0, sizeof(endp->net_state)); |
| 1347 | memset(&endp->bts_state, 0, sizeof(endp->bts_state)); |
Holger Hans Peter Freyther | b33ded1 | 2010-08-03 02:57:02 +0800 | [diff] [blame] | 1348 | |
| 1349 | endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE; |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 1350 | |
Pablo Neira Ayuso | a8c68ad | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 1351 | if (endp->osmux.state == OSMUX_STATE_ENABLED) |
| 1352 | osmux_disable_endpoint(endp); |
| 1353 | |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 1354 | memset(&endp->taps, 0, sizeof(endp->taps)); |
Holger Hans Peter Freyther | 64dcbe2 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 1355 | } |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1356 | |
Holger Hans Peter Freyther | bb8627a | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 1357 | void mgcp_initialize_endp(struct mgcp_endpoint *endp) |
| 1358 | { |
| 1359 | return mgcp_release_endp(endp); |
| 1360 | } |
| 1361 | |
Holger Hans Peter Freyther | 1adedbb | 2010-09-19 04:39:55 +0800 | [diff] [blame] | 1362 | static int send_trans(struct mgcp_config *cfg, const char *buf, int len) |
| 1363 | { |
| 1364 | struct sockaddr_in addr; |
| 1365 | |
| 1366 | memset(&addr, 0, sizeof(addr)); |
| 1367 | addr.sin_family = AF_INET; |
| 1368 | addr.sin_addr = cfg->transcoder_in; |
| 1369 | addr.sin_port = htons(2427); |
| 1370 | return sendto(cfg->gw_fd.bfd.fd, buf, len, 0, |
| 1371 | (struct sockaddr *) &addr, sizeof(addr)); |
| 1372 | } |
| 1373 | |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1374 | static void send_msg(struct mgcp_endpoint *endp, int endpoint, int port, |
| 1375 | const char *msg, const char *mode) |
| 1376 | { |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1377 | char buf[2096]; |
| 1378 | int len; |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 1379 | int nchars; |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1380 | |
| 1381 | /* hardcoded to AMR right now, we do not know the real type at this point */ |
| 1382 | len = snprintf(buf, sizeof(buf), |
| 1383 | "%s 42 %x@mgw MGCP 1.0\r\n" |
| 1384 | "C: 4256\r\n" |
| 1385 | "M: %s\r\n" |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 1386 | "\r\n", |
| 1387 | msg, endpoint, mode); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1388 | |
| 1389 | if (len < 0) |
| 1390 | return; |
| 1391 | |
Jacob Erlbeck | f2bc639 | 2014-03-14 18:01:20 +0100 | [diff] [blame] | 1392 | nchars = write_response_sdp(endp, buf + len, sizeof(buf) + len - 1, |
| 1393 | endp->cfg->source_addr); |
| 1394 | if (nchars < 0) |
| 1395 | return; |
| 1396 | |
| 1397 | len += nchars; |
| 1398 | |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1399 | buf[sizeof(buf) - 1] = '\0'; |
| 1400 | |
Holger Hans Peter Freyther | 1adedbb | 2010-09-19 04:39:55 +0800 | [diff] [blame] | 1401 | send_trans(endp->cfg, buf, len); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | static void send_dlcx(struct mgcp_endpoint *endp, int endpoint) |
| 1405 | { |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1406 | char buf[2096]; |
| 1407 | int len; |
| 1408 | |
| 1409 | len = snprintf(buf, sizeof(buf), |
| 1410 | "DLCX 43 %x@mgw MGCP 1.0\r\n" |
| 1411 | "C: 4256\r\n" |
| 1412 | , endpoint); |
| 1413 | |
| 1414 | if (len < 0) |
| 1415 | return; |
| 1416 | |
| 1417 | buf[sizeof(buf) - 1] = '\0'; |
| 1418 | |
Holger Hans Peter Freyther | 1adedbb | 2010-09-19 04:39:55 +0800 | [diff] [blame] | 1419 | send_trans(endp->cfg, buf, len); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Harald Welte | ba4f8b2 | 2012-01-27 00:41:39 +0100 | [diff] [blame] | 1422 | static int send_agent(struct mgcp_config *cfg, const char *buf, int len) |
| 1423 | { |
| 1424 | return write(cfg->gw_fd.bfd.fd, buf, len); |
| 1425 | } |
| 1426 | |
| 1427 | int mgcp_send_reset_all(struct mgcp_config *cfg) |
| 1428 | { |
| 1429 | static const char mgcp_reset[] = { |
| 1430 | "RSIP 1 *@mgw MGCP 1.0\r\n" |
| 1431 | }; |
| 1432 | |
| 1433 | return send_agent(cfg, mgcp_reset, sizeof mgcp_reset -1); |
| 1434 | } |
| 1435 | |
| 1436 | int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) |
| 1437 | { |
| 1438 | char buf[128]; |
| 1439 | int len; |
| 1440 | |
| 1441 | len = snprintf(buf, sizeof(buf), |
| 1442 | "RSIP 39 %x@mgw MGCP 1.0\r\n" |
| 1443 | , endpoint); |
| 1444 | if (len < 0) |
| 1445 | return len; |
| 1446 | |
| 1447 | buf[sizeof(buf) - 1] = '\0'; |
| 1448 | |
| 1449 | return send_agent(endp->cfg, buf, len); |
| 1450 | } |
| 1451 | |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1452 | static int setup_rtp_processing(struct mgcp_endpoint *endp) |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1453 | { |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1454 | int rc = 0; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1455 | struct mgcp_config *cfg = endp->cfg; |
| 1456 | |
| 1457 | if (endp->type != MGCP_RTP_DEFAULT) |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1458 | return 0; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1459 | |
| 1460 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1461 | return 0; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1462 | |
| 1463 | if (endp->conn_mode & MGCP_CONN_SEND_ONLY) |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1464 | rc |= cfg->setup_rtp_processing_cb(endp, &endp->net_end, &endp->bts_end); |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1465 | else |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1466 | rc |= cfg->setup_rtp_processing_cb(endp, &endp->net_end, NULL); |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1467 | |
| 1468 | if (endp->conn_mode & MGCP_CONN_RECV_ONLY) |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1469 | rc |= cfg->setup_rtp_processing_cb(endp, &endp->bts_end, &endp->net_end); |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1470 | else |
Holger Hans Peter Freyther | dc468ad | 2015-03-26 20:34:12 +0100 | [diff] [blame] | 1471 | rc |= cfg->setup_rtp_processing_cb(endp, &endp->bts_end, NULL); |
| 1472 | return rc; |
Jacob Erlbeck | 6a16305 | 2014-03-13 14:24:52 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1475 | static void create_transcoder(struct mgcp_endpoint *endp) |
| 1476 | { |
| 1477 | int port; |
| 1478 | int in_endp = ENDPOINT_NUMBER(endp); |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 1479 | int out_endp = endp_back_channel(in_endp); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1480 | |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 1481 | if (endp->type != MGCP_RTP_TRANSCODED) |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1482 | return; |
| 1483 | |
Holger Hans Peter Freyther | c9e3bc6 | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 1484 | send_msg(endp, in_endp, endp->trans_bts.local_port, "CRCX", "sendrecv"); |
| 1485 | send_msg(endp, in_endp, endp->trans_bts.local_port, "MDCX", "sendrecv"); |
Holger Hans Peter Freyther | b6345ab | 2010-11-01 20:53:31 +0100 | [diff] [blame] | 1486 | send_msg(endp, out_endp, endp->trans_net.local_port, "CRCX", "sendrecv"); |
| 1487 | send_msg(endp, out_endp, endp->trans_net.local_port, "MDCX", "sendrecv"); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1488 | |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 1489 | port = rtp_calculate_port(in_endp, endp->cfg->transcoder_remote_base); |
| 1490 | endp->trans_bts.rtp_port = htons(port); |
| 1491 | endp->trans_bts.rtcp_port = htons(port + 1); |
| 1492 | |
Holger Hans Peter Freyther | d582b20 | 2010-09-19 04:21:39 +0800 | [diff] [blame] | 1493 | port = rtp_calculate_port(out_endp, endp->cfg->transcoder_remote_base); |
Holger Hans Peter Freyther | b6345ab | 2010-11-01 20:53:31 +0100 | [diff] [blame] | 1494 | endp->trans_net.rtp_port = htons(port); |
| 1495 | endp->trans_net.rtcp_port = htons(port + 1); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | static void delete_transcoder(struct mgcp_endpoint *endp) |
| 1499 | { |
| 1500 | int in_endp = ENDPOINT_NUMBER(endp); |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 1501 | int out_endp = endp_back_channel(in_endp); |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1502 | |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 1503 | if (endp->type != MGCP_RTP_TRANSCODED) |
Holger Hans Peter Freyther | 017738d | 2010-09-18 06:35:29 +0800 | [diff] [blame] | 1504 | return; |
| 1505 | |
| 1506 | send_dlcx(endp, in_endp); |
| 1507 | send_dlcx(endp, out_endp); |
| 1508 | } |
Holger Hans Peter Freyther | f8fd106 | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 1509 | |
| 1510 | int mgcp_reset_transcoder(struct mgcp_config *cfg) |
| 1511 | { |
Holger Hans Peter Freyther | f8fd106 | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 1512 | if (!cfg->transcoder_ip) |
Holger Hans Peter Freyther | bafc987 | 2010-09-24 04:41:28 +0800 | [diff] [blame] | 1513 | return 0; |
Holger Hans Peter Freyther | f8fd106 | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 1514 | |
| 1515 | static const char mgcp_reset[] = { |
| 1516 | "RSIP 1 13@mgw MGCP 1.0\r\n" |
| 1517 | }; |
| 1518 | |
Holger Hans Peter Freyther | 1adedbb | 2010-09-19 04:39:55 +0800 | [diff] [blame] | 1519 | return send_trans(cfg, mgcp_reset, sizeof mgcp_reset -1); |
Holger Hans Peter Freyther | f8fd106 | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 1520 | } |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 1521 | |
| 1522 | void mgcp_format_stats(struct mgcp_endpoint *endp, char *msg, size_t size) |
| 1523 | { |
Holger Hans Peter Freyther | 8c87a48 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1524 | uint32_t expected, jitter; |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1525 | int ploss; |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 1526 | int nchars; |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1527 | mgcp_state_calc_loss(&endp->net_state, &endp->net_end, |
| 1528 | &expected, &ploss); |
Holger Hans Peter Freyther | 8c87a48 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 1529 | jitter = mgcp_state_calc_jitter(&endp->net_state); |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 1530 | |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 1531 | nchars = snprintf(msg, size, |
| 1532 | "\r\nP: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u", |
| 1533 | endp->bts_end.packets, endp->bts_end.octets, |
| 1534 | endp->net_end.packets, endp->net_end.octets, |
| 1535 | ploss, jitter); |
| 1536 | if (nchars < 0 || nchars >= size) |
| 1537 | goto truncate; |
| 1538 | |
| 1539 | msg += nchars; |
| 1540 | size -= nchars; |
| 1541 | |
| 1542 | /* Error Counter */ |
Pablo Neira Ayuso | 7737aed | 2014-09-17 12:33:09 +0200 | [diff] [blame] | 1543 | nchars = snprintf(msg, size, |
| 1544 | "\r\nX-Osmo-CP: EC TIS=%u, TOS=%u, TIR=%u, TOR=%u", |
| 1545 | endp->net_state.in_stream.err_ts_counter, |
| 1546 | endp->net_state.out_stream.err_ts_counter, |
| 1547 | endp->bts_state.in_stream.err_ts_counter, |
| 1548 | endp->bts_state.out_stream.err_ts_counter); |
| 1549 | if (nchars < 0 || nchars >= size) |
| 1550 | goto truncate; |
| 1551 | |
| 1552 | msg += nchars; |
| 1553 | size -= nchars; |
| 1554 | |
| 1555 | if (endp->osmux.state == OSMUX_STATE_ENABLED) { |
| 1556 | snprintf(msg, size, |
| 1557 | "\r\nX-Osmux-ST: CR=%u, BR=%u", |
| 1558 | endp->osmux.stats.chunks, |
| 1559 | endp->osmux.stats.octets); |
| 1560 | } |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 1561 | truncate: |
Holger Hans Peter Freyther | 8fc6d08 | 2012-09-14 17:18:12 +0200 | [diff] [blame] | 1562 | msg[size - 1] = '\0'; |
| 1563 | } |
Holger Hans Peter Freyther | 931d698 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 1564 | |
| 1565 | int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os, |
| 1566 | uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter) |
| 1567 | { |
| 1568 | char *line, *save; |
| 1569 | int rc; |
| 1570 | |
| 1571 | /* initialize with bad values */ |
| 1572 | *ps = *os = *pr = *_or = *jitter = UINT_MAX; |
| 1573 | *loss = INT_MAX; |
| 1574 | |
| 1575 | |
| 1576 | line = strtok_r((char *) msg->l2h, "\r\n", &save); |
| 1577 | if (!line) |
| 1578 | return -1; |
| 1579 | |
| 1580 | /* this can only parse the message that is created above... */ |
Jacob Erlbeck | 7a6510c | 2013-11-29 13:43:43 +0100 | [diff] [blame] | 1581 | for_each_non_empty_line(line, save) { |
Holger Hans Peter Freyther | 931d698 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 1582 | switch (line[0]) { |
| 1583 | case 'P': |
| 1584 | rc = sscanf(line, "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u", |
| 1585 | ps, os, pr, _or, loss, jitter); |
| 1586 | return rc == 6 ? 0 : -1; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | return -1; |
| 1591 | } |