Holger Hans Peter Freyther | 07fc097 | 2012-10-24 21:52:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * This file contains helper routines for MGCP Gateway handling. |
| 3 | * |
| 4 | * The first thing to remember is that each BSC has its own namespace/range |
| 5 | * of endpoints. Whenever a BSSMAP ASSIGNMENT REQUEST is received this code |
| 6 | * will be called to select an endpoint on the BSC. The mapping from original |
| 7 | * multiplex/timeslot to BSC multiplex'/timeslot' will be stored. |
| 8 | * |
| 9 | * The second part is to take messages on the public MGCP GW interface |
| 10 | * and forward them to the right BSC. This requires the MSC to first |
| 11 | * assign the timeslot. This assumption has been true so far. We are using |
| 12 | * the policy_cb of the MGCP protocol code to decide if the request should |
| 13 | * be immediately answered or delayed. An extension "Z: noanswer" is used |
| 14 | * to request the BSC to not respond. This is saving some bytes of bandwidth |
| 15 | * and as we are using TCP to forward the message we know it will arrive. |
| 16 | * The mgcp_do_read method reads these messages and hands them to the protocol |
| 17 | * parsing code which will call the mentioned policy_cb. The bsc_mgcp_forward |
| 18 | * method is used on the way back from the BSC to the network. |
| 19 | * |
| 20 | * The third part is to patch messages forwarded to the BSC. This includes |
| 21 | * the endpoint number, the ports to be used inside the SDP file and maybe |
| 22 | * some other bits. |
| 23 | * |
| 24 | */ |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 25 | /* |
Holger Hans Peter Freyther | 07fc097 | 2012-10-24 21:52:16 +0200 | [diff] [blame] | 26 | * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 27 | * (C) 2010-2012 by On-Waves |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 28 | * All Rights Reserved |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 31 | * it under the terms of the GNU Affero General Public License as published by |
| 32 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 38 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 39 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 40 | * You should have received a copy of the GNU Affero General Public License |
| 41 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 42 | * |
| 43 | */ |
| 44 | |
| 45 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | ab22335 | 2013-04-22 09:07:39 +0200 | [diff] [blame] | 46 | #include <openbsc/bsc_nat_callstats.h> |
Holger Hans Peter Freyther | c2b31ed | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 47 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 48 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 49 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 19c530c | 2010-10-13 23:52:01 +0200 | [diff] [blame] | 50 | #include <openbsc/ipaccess.h> |
Neels Hofmeyr | 979cd26 | 2017-07-19 16:48:42 +0200 | [diff] [blame] | 51 | #include <osmocom/legacy_mgcp/mgcp.h> |
| 52 | #include <osmocom/legacy_mgcp/mgcp_internal.h> |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 53 | #include <openbsc/osmux.h> |
| 54 | |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 55 | #include <osmocom/ctrl/control_cmd.h> |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 56 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 57 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 58 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 59 | #include <osmocom/core/talloc.h> |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 60 | #include <osmocom/gsm/gsm0808.h> |
| 61 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 62 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 63 | #include <netinet/in.h> |
| 64 | #include <arpa/inet.h> |
| 65 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 66 | #include <errno.h> |
| 67 | #include <unistd.h> |
| 68 | |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 69 | static void send_direct(struct bsc_nat *nat, struct msgb *output) |
Holger Hans Peter Freyther | 77956aa | 2012-11-05 13:52:53 +0100 | [diff] [blame] | 70 | { |
| 71 | if (osmo_wqueue_enqueue(&nat->mgcp_cfg->gw_fd, output) != 0) { |
| 72 | LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n"); |
| 73 | msgb_free(output); |
| 74 | } |
| 75 | } |
| 76 | |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 77 | static void mgcp_queue_for_call_agent(struct bsc_nat *nat, struct msgb *output) |
| 78 | { |
| 79 | if (nat->mgcp_ipa) |
| 80 | bsc_nat_send_mgcp_to_msc(nat, output); |
| 81 | else |
| 82 | send_direct(nat, output); |
| 83 | } |
| 84 | |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 85 | int bsc_mgcp_nr_multiplexes(int max_endpoints) |
| 86 | { |
| 87 | int div = max_endpoints / 32; |
| 88 | |
| 89 | if ((max_endpoints % 32) != 0) |
| 90 | div += 1; |
| 91 | |
| 92 | return div; |
| 93 | } |
| 94 | |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 95 | static int bsc_init_endps_if_needed(struct bsc_connection *con) |
| 96 | { |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 97 | int multiplexes; |
| 98 | |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 99 | /* we have done that */ |
| 100 | if (con->_endpoint_status) |
| 101 | return 0; |
| 102 | |
| 103 | /* we have no config... */ |
| 104 | if (!con->cfg) |
| 105 | return -1; |
| 106 | |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 107 | multiplexes = bsc_mgcp_nr_multiplexes(con->cfg->max_endpoints); |
| 108 | con->number_multiplexes = multiplexes; |
| 109 | con->max_endpoints = con->cfg->max_endpoints; |
| 110 | con->_endpoint_status = talloc_zero_array(con, char, 32 * multiplexes + 1); |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 111 | return con->_endpoint_status == NULL; |
| 112 | } |
| 113 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 114 | static int bsc_assign_endpoint(struct bsc_connection *bsc, struct nat_sccp_connection *con) |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 115 | { |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 116 | int multiplex; |
| 117 | int timeslot; |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 118 | const int number_endpoints = bsc->max_endpoints; |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 119 | int i; |
| 120 | |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 121 | mgcp_endpoint_to_timeslot(bsc->last_endpoint, &multiplex, ×lot); |
| 122 | timeslot += 1; |
| 123 | |
| 124 | for (i = 0; i < number_endpoints; ++i) { |
| 125 | int endpoint; |
| 126 | |
| 127 | /* Wrap around timeslots */ |
| 128 | if (timeslot == 0) |
| 129 | timeslot = 1; |
| 130 | |
| 131 | if (timeslot == 0x1f) { |
| 132 | timeslot = 1; |
| 133 | multiplex += 1; |
| 134 | } |
| 135 | |
| 136 | /* Wrap around the multiplex */ |
| 137 | if (multiplex >= bsc->number_multiplexes) |
| 138 | multiplex = 0; |
| 139 | |
| 140 | endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot); |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 141 | |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 142 | /* Now check if we are allowed to assign this one */ |
| 143 | if (endpoint >= bsc->max_endpoints) { |
| 144 | multiplex = 0; |
| 145 | timeslot = 1; |
| 146 | endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot); |
| 147 | } |
| 148 | |
| 149 | |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 150 | if (bsc->_endpoint_status[endpoint] == 0) { |
| 151 | bsc->_endpoint_status[endpoint] = 1; |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 152 | con->bsc_endp = endpoint; |
| 153 | bsc->last_endpoint = endpoint; |
| 154 | return 0; |
| 155 | } |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 156 | |
| 157 | timeslot += 1; |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | static uint16_t create_cic(int endpoint) |
| 164 | { |
| 165 | int timeslot, multiplex; |
| 166 | |
| 167 | mgcp_endpoint_to_timeslot(endpoint, &multiplex, ×lot); |
| 168 | return (multiplex << 5) | (timeslot & 0x1f); |
| 169 | } |
| 170 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 171 | int bsc_mgcp_assign_patch(struct nat_sccp_connection *con, struct msgb *msg) |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 172 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 173 | struct nat_sccp_connection *mcon; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 174 | struct tlv_parsed tp; |
Holger Hans Peter Freyther | e2c1520 | 2010-07-23 19:09:21 +0800 | [diff] [blame] | 175 | uint16_t cic; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 176 | uint8_t timeslot; |
| 177 | uint8_t multiplex; |
Holger Hans Peter Freyther | 1406977 | 2010-11-04 17:14:41 +0100 | [diff] [blame] | 178 | unsigned int endp; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 179 | |
| 180 | if (!msg->l3h) { |
| 181 | LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n"); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | if (msgb_l3len(msg) < 3) { |
| 186 | LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n"); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 191 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) { |
| 192 | LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n"); |
| 193 | return -1; |
| 194 | } |
| 195 | |
Daniel Willmann | 8a485f0 | 2014-06-27 17:05:47 +0200 | [diff] [blame] | 196 | cic = ntohs(tlvp_val16_unal(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 197 | timeslot = cic & 0x1f; |
| 198 | multiplex = (cic & ~0x1f) >> 5; |
| 199 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 200 | |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 201 | endp = mgcp_timeslot_to_endpoint(multiplex, timeslot); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 202 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 203 | if (endp >= con->bsc->nat->mgcp_cfg->trunk.number_endpoints) { |
Holger Hans Peter Freyther | 1406977 | 2010-11-04 17:14:41 +0100 | [diff] [blame] | 204 | LOGP(DNAT, LOGL_ERROR, |
| 205 | "MSC attempted to assign bad endpoint 0x%x\n", |
| 206 | endp); |
| 207 | return -1; |
| 208 | } |
| 209 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 210 | /* find stale connections using that endpoint */ |
| 211 | llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 212 | if (mcon->msc_endp == endp) { |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 213 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 214 | "Endpoint %d was assigned to 0x%x and now 0x%x\n", |
| 215 | endp, |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 216 | sccp_src_ref_to_int(&mcon->patched_ref), |
| 217 | sccp_src_ref_to_int(&con->patched_ref)); |
| 218 | bsc_mgcp_dlcx(mcon); |
| 219 | } |
| 220 | } |
| 221 | |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 222 | con->msc_endp = endp; |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 223 | if (bsc_init_endps_if_needed(con->bsc) != 0) |
| 224 | return -1; |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 225 | if (bsc_assign_endpoint(con->bsc, con) != 0) |
| 226 | return -1; |
| 227 | |
| 228 | /* |
| 229 | * now patch the message for the new CIC... |
| 230 | * still assumed to be one multiplex only |
| 231 | */ |
| 232 | cic = htons(create_cic(con->bsc_endp)); |
| 233 | memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE), |
| 234 | &cic, sizeof(cic)); |
| 235 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 239 | static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i) |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 240 | { |
| 241 | if (nat->bsc_endpoints[i].transaction_id) { |
| 242 | talloc_free(nat->bsc_endpoints[i].transaction_id); |
| 243 | nat->bsc_endpoints[i].transaction_id = NULL; |
| 244 | } |
| 245 | |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 246 | nat->bsc_endpoints[i].transaction_state = 0; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 247 | nat->bsc_endpoints[i].bsc = NULL; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 250 | void bsc_mgcp_free_endpoints(struct bsc_nat *nat) |
| 251 | { |
| 252 | int i; |
| 253 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 254 | for (i = 1; i < nat->mgcp_cfg->trunk.number_endpoints; ++i){ |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 255 | bsc_mgcp_free_endpoint(nat, i); |
Holger Hans Peter Freyther | cb6ad70 | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 256 | mgcp_release_endp(&nat->mgcp_cfg->trunk.endpoints[i]); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 257 | } |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 258 | } |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 259 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 260 | /* send a MDCX where we do not want a response */ |
Holger Hans Peter Freyther | 601180f | 2010-08-29 23:40:33 +0800 | [diff] [blame] | 261 | static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp) |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 262 | { |
| 263 | char buf[2096]; |
| 264 | int len; |
| 265 | |
| 266 | len = snprintf(buf, sizeof(buf), |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 267 | "MDCX 23 %x@mgw MGCP 1.0\r\n" |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 268 | "Z: noanswer\r\n" |
| 269 | "\r\n" |
| 270 | "c=IN IP4 %s\r\n" |
| 271 | "m=audio %d RTP/AVP 255\r\n", |
Holger Hans Peter Freyther | e6ed009 | 2015-08-20 14:58:19 +0200 | [diff] [blame] | 272 | port, mgcp_bts_src_addr(endp), |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 273 | endp->bts_end.local_port); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 274 | if (len < 0) { |
Holger Hans Peter Freyther | 7456891 | 2012-09-14 15:51:43 +0200 | [diff] [blame] | 275 | LOGP(DMGCP, LOGL_ERROR, "snprintf for MDCX failed.\n"); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 276 | return; |
| 277 | } |
Holger Hans Peter Freyther | d38aa45 | 2010-08-30 11:58:49 +0800 | [diff] [blame] | 278 | |
Holger Hans Peter Freyther | 6de9d0b | 2012-10-30 16:32:19 +0100 | [diff] [blame] | 279 | bsc_write_mgcp(bsc, (uint8_t *) buf, len); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 280 | } |
| 281 | |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 282 | static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint, int trans) |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 283 | { |
| 284 | char buf[2096]; |
| 285 | int len; |
| 286 | |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 287 | /* |
| 288 | * The following is a bit of a spec violation. According to the |
| 289 | * MGCP grammar the transaction id is are upto 9 digits but we |
| 290 | * prefix it with an alpha numeric value so we can easily recognize |
| 291 | * it as a response. |
| 292 | */ |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 293 | len = snprintf(buf, sizeof(buf), |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 294 | "DLCX nat-%u %x@mgw MGCP 1.0\r\n", |
| 295 | trans, endpoint); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 296 | if (len < 0) { |
| 297 | LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n"); |
| 298 | return; |
| 299 | } |
| 300 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 301 | bsc_write_mgcp(bsc, (uint8_t *) buf, len); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 304 | void bsc_mgcp_init(struct nat_sccp_connection *con) |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 305 | { |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 306 | con->msc_endp = -1; |
| 307 | con->bsc_endp = -1; |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 308 | } |
| 309 | |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 310 | /** |
| 311 | * This code will remember the network side of the audio statistics and |
| 312 | * once the internal DLCX response arrives this can be combined with the |
| 313 | * the BSC side and forwarded as a trap. |
| 314 | */ |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 315 | static void remember_pending_dlcx(struct nat_sccp_connection *con, uint32_t transaction) |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 316 | { |
| 317 | struct bsc_nat_call_stats *stats; |
| 318 | struct bsc_connection *bsc = con->bsc; |
| 319 | struct mgcp_endpoint *endp; |
| 320 | |
| 321 | stats = talloc_zero(bsc, struct bsc_nat_call_stats); |
| 322 | if (!stats) { |
| 323 | LOGP(DNAT, LOGL_NOTICE, |
| 324 | "Failed to allocate statistics for endpoint 0x%x\n", |
| 325 | con->msc_endp); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | /* take the endpoint here */ |
| 330 | endp = &bsc->nat->mgcp_cfg->trunk.endpoints[con->msc_endp]; |
| 331 | |
| 332 | stats->remote_ref = con->remote_ref; |
| 333 | stats->src_ref = con->patched_ref; |
| 334 | |
| 335 | stats->ci = endp->ci; |
| 336 | stats->bts_rtp_port = endp->bts_end.rtp_port; |
| 337 | stats->bts_addr = endp->bts_end.addr; |
| 338 | stats->net_rtp_port = endp->net_end.rtp_port; |
| 339 | stats->net_addr = endp->net_end.addr; |
| 340 | |
| 341 | stats->net_ps = endp->net_end.packets; |
| 342 | stats->net_os = endp->net_end.octets; |
| 343 | stats->bts_pr = endp->bts_end.packets; |
| 344 | stats->bts_or = endp->bts_end.octets; |
| 345 | mgcp_state_calc_loss(&endp->bts_state, &endp->bts_end, |
| 346 | &stats->bts_expected, &stats->bts_loss); |
| 347 | stats->bts_jitter = mgcp_state_calc_jitter(&endp->bts_state); |
| 348 | |
| 349 | stats->trans_id = transaction; |
| 350 | stats->msc_endpoint = con->msc_endp; |
| 351 | |
Holger Hans Peter Freyther | 7c831ec | 2012-11-12 18:36:00 +0100 | [diff] [blame] | 352 | /* |
| 353 | * Too many pending requests.. let's remove the first two items. |
| 354 | */ |
| 355 | if (!llist_empty(&bsc->pending_dlcx) && |
| 356 | bsc->pending_dlcx_count >= bsc->cfg->max_endpoints * 3) { |
| 357 | struct bsc_nat_call_stats *tmp; |
| 358 | LOGP(DNAT, LOGL_ERROR, |
| 359 | "Too many(%d) pending DLCX responses on BSC: %d\n", |
| 360 | bsc->pending_dlcx_count, bsc->cfg->nr); |
| 361 | bsc->pending_dlcx_count -= 1; |
| 362 | tmp = (struct bsc_nat_call_stats *) bsc->pending_dlcx.next; |
| 363 | llist_del(&tmp->entry); |
| 364 | talloc_free(tmp); |
| 365 | } |
| 366 | |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 367 | bsc->pending_dlcx_count += 1; |
| 368 | llist_add_tail(&stats->entry, &bsc->pending_dlcx); |
| 369 | } |
| 370 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 371 | void bsc_mgcp_dlcx(struct nat_sccp_connection *con) |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 372 | { |
| 373 | /* send a DLCX down the stream */ |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 374 | if (con->bsc_endp != -1 && con->bsc->_endpoint_status) { |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 375 | LOGP(DNAT, LOGL_NOTICE, |
Holger Hans Peter Freyther | 931ad6a | 2012-11-12 18:00:25 +0100 | [diff] [blame] | 376 | "Endpoint 0x%x was allocated for bsc: %d. Freeing it.\n", |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 377 | con->bsc_endp, con->bsc->cfg->nr); |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 378 | if (con->bsc->_endpoint_status[con->bsc_endp] != 1) |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 379 | LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp); |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 380 | remember_pending_dlcx(con, con->bsc->next_transaction); |
Holger Hans Peter Freyther | ed500e3 | 2011-02-25 17:09:07 +0100 | [diff] [blame] | 381 | con->bsc->_endpoint_status[con->bsc_endp] = 0; |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 382 | bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp, con->bsc->next_transaction++); |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 383 | bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | bsc_mgcp_init(con); |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 387 | |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Search for the pending request |
| 392 | */ |
| 393 | static void handle_dlcx_response(struct bsc_connection *bsc, struct msgb *msg, |
| 394 | int code, const char *transaction) |
| 395 | { |
| 396 | uint32_t trans_id = UINT32_MAX; |
| 397 | uint32_t b_ps, b_os, n_pr, n_or, jitter; |
| 398 | int loss; |
| 399 | struct bsc_nat_call_stats *tmp, *stat = NULL; |
| 400 | struct ctrl_cmd *cmd; |
| 401 | |
| 402 | /* parse the transaction identifier */ |
| 403 | int rc = sscanf(transaction, "nat-%u", &trans_id); |
| 404 | if (rc != 1) { |
| 405 | LOGP(DNAT, LOGL_ERROR, "Can not parse transaction id: '%s'\n", |
| 406 | transaction); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* find the answer for the request we made */ |
| 411 | llist_for_each_entry(tmp, &bsc->pending_dlcx, entry) { |
| 412 | if (trans_id != tmp->trans_id) |
| 413 | continue; |
| 414 | |
| 415 | stat = tmp; |
| 416 | break; |
| 417 | } |
| 418 | |
| 419 | if (!stat) { |
| 420 | LOGP(DNAT, LOGL_ERROR, |
| 421 | "Can not find transaction for: %u\n", trans_id); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | /* attempt to parse the data now */ |
| 426 | rc = mgcp_parse_stats(msg, &b_ps, &b_os, &n_pr, &n_or, &loss, &jitter); |
| 427 | if (rc != 0) |
| 428 | LOGP(DNAT, LOGL_ERROR, |
| 429 | "Can not parse connection statistics: %d\n", rc); |
| 430 | |
| 431 | /* send a trap now */ |
| 432 | cmd = ctrl_cmd_create(bsc, CTRL_TYPE_TRAP); |
| 433 | if (!cmd) { |
| 434 | LOGP(DNAT, LOGL_ERROR, |
| 435 | "Creating a ctrl cmd failed.\n"); |
| 436 | goto free_stat; |
| 437 | } |
| 438 | |
| 439 | cmd->id = "0"; |
Holger Hans Peter Freyther | ac04d8d | 2012-11-13 21:54:36 +0100 | [diff] [blame] | 440 | cmd->variable = talloc_asprintf(cmd, "net.0.bsc.%d.call_stats.v2", |
| 441 | bsc->cfg->nr); |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 442 | cmd->reply = talloc_asprintf(cmd, |
Holger Hans Peter Freyther | ac04d8d | 2012-11-13 21:54:36 +0100 | [diff] [blame] | 443 | "mg_ip_addr=%s,mg_port=%d,", |
| 444 | inet_ntoa(stat->net_addr), |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 445 | stat->net_rtp_port); |
| 446 | cmd->reply = talloc_asprintf_append(cmd->reply, |
| 447 | "endpoint_ip_addr=%s,endpoint_port=%d,", |
| 448 | inet_ntoa(stat->bts_addr), |
| 449 | stat->bts_rtp_port); |
| 450 | cmd->reply = talloc_asprintf_append(cmd->reply, |
| 451 | "nat_pkt_in=%u,nat_pkt_out=%u," |
| 452 | "nat_bytes_in=%u,nat_bytes_out=%u," |
| 453 | "nat_jitter=%u,nat_pkt_lost=%d,", |
| 454 | stat->bts_pr, stat->net_ps, |
| 455 | stat->bts_or, stat->net_os, |
| 456 | stat->bts_jitter, stat->bts_loss); |
| 457 | cmd->reply = talloc_asprintf_append(cmd->reply, |
| 458 | "bsc_pkt_in=%u,bsc_pkt_out=%u," |
| 459 | "bsc_bytes_in=%u,bsc_bytes_out=%u," |
Holger Hans Peter Freyther | 7d7054e | 2012-11-12 19:05:16 +0100 | [diff] [blame] | 460 | "bsc_jitter=%u,bsc_pkt_lost=%d,", |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 461 | n_pr, b_ps, |
| 462 | n_or, b_os, |
| 463 | jitter, loss); |
Holger Hans Peter Freyther | 7d7054e | 2012-11-12 19:05:16 +0100 | [diff] [blame] | 464 | cmd->reply = talloc_asprintf_append(cmd->reply, |
| 465 | "sccp_src_ref=%u,sccp_dst_ref=%u", |
| 466 | sccp_src_ref_to_int(&stat->src_ref), |
| 467 | sccp_src_ref_to_int(&stat->remote_ref)); |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 468 | |
| 469 | /* send it and be done */ |
| 470 | ctrl_cmd_send_to_all(bsc->nat->ctrl, cmd); |
| 471 | talloc_free(cmd); |
| 472 | |
| 473 | free_stat: |
| 474 | bsc->pending_dlcx_count -= 1; |
| 475 | llist_del(&stat->entry); |
| 476 | talloc_free(stat); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 480 | struct nat_sccp_connection *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint) |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 481 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 482 | struct nat_sccp_connection *con = NULL; |
| 483 | struct nat_sccp_connection *sccp; |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 484 | |
| 485 | llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 486 | if (sccp->msc_endp == -1) |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 487 | continue; |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 488 | if (sccp->msc_endp != endpoint) |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 489 | continue; |
| 490 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 491 | con = sccp; |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 494 | if (con) |
| 495 | return con; |
Holger Hans Peter Freyther | eb52e89 | 2010-04-18 02:14:45 +0800 | [diff] [blame] | 496 | |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 497 | LOGP(DMGCP, LOGL_ERROR, |
| 498 | "Failed to find the connection for endpoint: 0x%x\n", endpoint); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 499 | return NULL; |
| 500 | } |
| 501 | |
Holger Hans Peter Freyther | 20626dd | 2015-10-02 18:15:18 +0200 | [diff] [blame] | 502 | static int nat_osmux_only(struct mgcp_config *mgcp_cfg, struct bsc_config *bsc_cfg) |
| 503 | { |
| 504 | if (mgcp_cfg->osmux == OSMUX_USAGE_ONLY) |
| 505 | return 1; |
| 506 | if (bsc_cfg->osmux == OSMUX_USAGE_ONLY) |
| 507 | return 1; |
| 508 | return 0; |
| 509 | } |
| 510 | |
Holger Hans Peter Freyther | cb3c2c9 | 2012-10-24 21:53:07 +0200 | [diff] [blame] | 511 | static int bsc_mgcp_policy_cb(struct mgcp_trunk_config *tcfg, int endpoint, int state, const char *transaction_id) |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 512 | { |
| 513 | struct bsc_nat *nat; |
| 514 | struct bsc_endpoint *bsc_endp; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 515 | struct nat_sccp_connection *sccp; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 516 | struct mgcp_endpoint *mgcp_endp; |
| 517 | struct msgb *bsc_msg; |
| 518 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 519 | nat = tcfg->cfg->data; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 520 | bsc_endp = &nat->bsc_endpoints[endpoint]; |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 521 | mgcp_endp = &nat->mgcp_cfg->trunk.endpoints[endpoint]; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 522 | |
Holger Hans Peter Freyther | fef7612 | 2010-04-24 21:05:18 +0800 | [diff] [blame] | 523 | if (bsc_endp->transaction_id) { |
| 524 | LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n", |
| 525 | endpoint, bsc_endp->transaction_id); |
| 526 | talloc_free(bsc_endp->transaction_id); |
| 527 | bsc_endp->transaction_id = NULL; |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 528 | bsc_endp->transaction_state = 0; |
Holger Hans Peter Freyther | fef7612 | 2010-04-24 21:05:18 +0800 | [diff] [blame] | 529 | } |
| 530 | bsc_endp->bsc = NULL; |
| 531 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 532 | sccp = bsc_mgcp_find_con(nat, endpoint); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 533 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 534 | if (!sccp) { |
Holger Hans Peter Freyther | 3d194d9 | 2010-04-24 21:02:01 +0800 | [diff] [blame] | 535 | LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for change on endpoint: 0x%x state: %d\n", endpoint, state); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 536 | |
| 537 | switch (state) { |
| 538 | case MGCP_ENDP_CRCX: |
| 539 | return MGCP_POLICY_REJECT; |
| 540 | break; |
| 541 | case MGCP_ENDP_DLCX: |
| 542 | return MGCP_POLICY_CONT; |
| 543 | break; |
| 544 | case MGCP_ENDP_MDCX: |
| 545 | return MGCP_POLICY_CONT; |
| 546 | break; |
| 547 | default: |
| 548 | LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state); |
| 549 | return MGCP_POLICY_CONT; |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 554 | /* Allocate a Osmux circuit ID */ |
Holger Hans Peter Freyther | 20626dd | 2015-10-02 18:15:18 +0200 | [diff] [blame] | 555 | if (state == MGCP_ENDP_CRCX) { |
| 556 | if (nat->mgcp_cfg->osmux && sccp->bsc->cfg->osmux) { |
Holger Hans Peter Freyther | 1afe7c7 | 2015-10-04 11:11:11 +0200 | [diff] [blame] | 557 | osmux_allocate_cid(mgcp_endp); |
| 558 | if (mgcp_endp->osmux.allocated_cid < 0 && |
| 559 | nat_osmux_only(nat->mgcp_cfg, sccp->bsc->cfg)) { |
Holger Hans Peter Freyther | 20626dd | 2015-10-02 18:15:18 +0200 | [diff] [blame] | 560 | LOGP(DMGCP, LOGL_ERROR, |
| 561 | "Rejecting usage of endpoint\n"); |
| 562 | return MGCP_POLICY_REJECT; |
| 563 | } |
| 564 | } |
| 565 | } |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 566 | |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 567 | /* we need to generate a new and patched message */ |
Jacob Erlbeck | 3dff27d | 2013-11-29 13:43:48 +0100 | [diff] [blame] | 568 | bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, |
Holger Hans Peter Freyther | e6ed009 | 2015-08-20 14:58:19 +0200 | [diff] [blame] | 569 | sccp->bsc_endp, mgcp_bts_src_addr(mgcp_endp), |
Holger Hans Peter Freyther | 1afe7c7 | 2015-10-04 11:11:11 +0200 | [diff] [blame] | 570 | mgcp_endp->bts_end.local_port, |
| 571 | mgcp_endp->osmux.allocated_cid, |
Holger Hans Peter Freyther | 7f100c9 | 2015-04-23 20:25:17 -0400 | [diff] [blame] | 572 | &mgcp_endp->net_end.codec.payload_type, |
| 573 | nat->sdp_ensure_amr_mode_set); |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 574 | if (!bsc_msg) { |
| 575 | LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n"); |
| 576 | return MGCP_POLICY_CONT; |
| 577 | } |
| 578 | |
| 579 | |
Holger Hans Peter Freyther | b3e0a03 | 2010-04-04 18:11:49 +0200 | [diff] [blame] | 580 | bsc_endp->transaction_id = talloc_strdup(nat, transaction_id); |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 581 | bsc_endp->transaction_state = state; |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 582 | bsc_endp->bsc = sccp->bsc; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 583 | |
| 584 | /* we need to update some bits */ |
| 585 | if (state == MGCP_ENDP_CRCX) { |
| 586 | struct sockaddr_in sock; |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 587 | |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 588 | /* Annotate the allocated Osmux CID until the bsc confirms that |
| 589 | * it agrees to use Osmux for this voice flow. |
| 590 | */ |
Holger Hans Peter Freyther | 1afe7c7 | 2015-10-04 11:11:11 +0200 | [diff] [blame] | 591 | if (mgcp_endp->osmux.allocated_cid >= 0 && |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 592 | mgcp_endp->osmux.state != OSMUX_STATE_ENABLED) { |
Daniel Willmann | 5754206 | 2016-06-29 16:24:42 +0200 | [diff] [blame] | 593 | mgcp_endp->osmux.state = OSMUX_STATE_NEGOTIATING; |
Holger Hans Peter Freyther | 1afe7c7 | 2015-10-04 11:11:11 +0200 | [diff] [blame] | 594 | mgcp_endp->osmux.cid = mgcp_endp->osmux.allocated_cid; |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 597 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 598 | if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) { |
Holger Hans Peter Freyther | 92febd3 | 2010-04-06 11:17:07 +0200 | [diff] [blame] | 599 | LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n", |
| 600 | errno, strerror(errno)); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 601 | } else { |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 602 | mgcp_endp->bts_end.addr = sock.sin_addr; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 603 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 604 | |
Holger Hans Peter Freyther | e66cac3 | 2010-08-05 01:50:44 +0800 | [diff] [blame] | 605 | /* send the message and a fake MDCX to force sending of a dummy packet */ |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 606 | bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 601180f | 2010-08-29 23:40:33 +0800 | [diff] [blame] | 607 | bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 608 | return MGCP_POLICY_DEFER; |
| 609 | } else if (state == MGCP_ENDP_DLCX) { |
| 610 | /* we will free the endpoint now and send a DLCX to the BSC */ |
Holger Hans Peter Freyther | 3a347f0 | 2010-05-01 10:31:53 +0800 | [diff] [blame] | 611 | msgb_free(bsc_msg); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 612 | bsc_mgcp_dlcx(sccp); |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 613 | |
| 614 | /* libmgcp clears the MGCP endpoint for us */ |
| 615 | if (mgcp_endp->osmux.state == OSMUX_STATE_ENABLED) |
Holger Hans Peter Freyther | 1afe7c7 | 2015-10-04 11:11:11 +0200 | [diff] [blame] | 616 | osmux_release_cid(mgcp_endp); |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 617 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 618 | return MGCP_POLICY_CONT; |
| 619 | } else { |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 620 | bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 621 | return MGCP_POLICY_DEFER; |
| 622 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 623 | } |
| 624 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 625 | /* |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 626 | * We do have a failure, free data downstream.. |
| 627 | */ |
| 628 | static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp, |
| 629 | struct bsc_connection *bsc) |
| 630 | { |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 631 | LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n", |
| 632 | ENDPOINT_NUMBER(endp), bsc_endp->transaction_state); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 633 | |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 634 | /* if a CRCX failed... send a DLCX down the stream */ |
| 635 | if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 636 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 637 | con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp)); |
| 638 | if (!con) { |
| 639 | LOGP(DMGCP, LOGL_ERROR, |
| 640 | "No SCCP connection for endp 0x%x\n", |
| 641 | ENDPOINT_NUMBER(endp)); |
| 642 | } else { |
| 643 | if (con->bsc == bsc) { |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 644 | bsc_mgcp_send_dlcx(bsc, con->bsc_endp, con->bsc->next_transaction++); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 645 | } else { |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 646 | LOGP(DMGCP, LOGL_ERROR, |
| 647 | "Endpoint belongs to a different BSC\n"); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 648 | } |
| 649 | } |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 650 | } |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 651 | |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 652 | bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp)); |
Holger Hans Peter Freyther | cb6ad70 | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 653 | mgcp_release_endp(endp); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 654 | } |
| 655 | |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 656 | static void bsc_mgcp_osmux_confirm(struct mgcp_endpoint *endp, const char *str) |
| 657 | { |
| 658 | unsigned int osmux_cid; |
| 659 | char *res; |
| 660 | |
| 661 | res = strstr(str, "X-Osmux: "); |
| 662 | if (!res) { |
| 663 | LOGP(DMGCP, LOGL_INFO, |
| 664 | "BSC doesn't want to use Osmux, failing back to RTP\n"); |
| 665 | goto err; |
| 666 | } |
| 667 | |
| 668 | if (sscanf(res, "X-Osmux: %u", &osmux_cid) != 1) { |
| 669 | LOGP(DMGCP, LOGL_ERROR, "Failed to parse Osmux CID '%s'\n", |
| 670 | str); |
| 671 | goto err; |
| 672 | } |
| 673 | |
| 674 | if (endp->osmux.cid != osmux_cid) { |
Pablo Neira Ayuso | a1efcc2 | 2014-08-28 14:48:47 +0200 | [diff] [blame] | 675 | LOGP(DMGCP, LOGL_ERROR, |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 676 | "BSC sent us wrong CID %u, we expected %u", |
| 677 | osmux_cid, endp->osmux.cid); |
| 678 | goto err; |
| 679 | } |
| 680 | |
| 681 | LOGP(DMGCP, LOGL_NOTICE, "bsc accepted to use Osmux (cid=%u)\n", |
| 682 | osmux_cid); |
Daniel Willmann | 5754206 | 2016-06-29 16:24:42 +0200 | [diff] [blame] | 683 | endp->osmux.state = OSMUX_STATE_ACTIVATING; |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 684 | return; |
| 685 | err: |
Holger Hans Peter Freyther | 1afe7c7 | 2015-10-04 11:11:11 +0200 | [diff] [blame] | 686 | osmux_release_cid(endp); |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 687 | endp->osmux.state = OSMUX_STATE_DISABLED; |
| 688 | } |
| 689 | |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 690 | /* |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 691 | * We have received a msg from the BSC. We will see if we know |
| 692 | * this transaction and if it belongs to the BSC. Then we will |
| 693 | * need to patch the content to point to the local network and we |
| 694 | * need to update the I: that was assigned by the BSS. |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 695 | * |
| 696 | * Only responses to CRCX and DLCX should arrive here. The DLCX |
| 697 | * needs to be handled specially to combine the two statistics. |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 698 | */ |
| 699 | void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg) |
| 700 | { |
| 701 | struct msgb *output; |
| 702 | struct bsc_endpoint *bsc_endp = NULL; |
| 703 | struct mgcp_endpoint *endp = NULL; |
Holger Hans Peter Freyther | d2dd6e8 | 2010-04-06 11:06:11 +0200 | [diff] [blame] | 704 | int i, code; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 705 | char transaction_id[60]; |
| 706 | |
| 707 | /* Some assumption that our buffer is big enough.. and null terminate */ |
| 708 | if (msgb_l2len(msg) > 2000) { |
| 709 | LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n"); |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | msg->l2h[msgb_l2len(msg)] = '\0'; |
| 714 | |
| 715 | if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) { |
| 716 | LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n"); |
| 717 | return; |
| 718 | } |
| 719 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 720 | for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) { |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 721 | if (bsc->nat->bsc_endpoints[i].bsc != bsc) |
| 722 | continue; |
Holger Hans Peter Freyther | 3f7c7d0 | 2010-04-05 18:00:05 +0200 | [diff] [blame] | 723 | /* no one listening? a bug? */ |
| 724 | if (!bsc->nat->bsc_endpoints[i].transaction_id) |
| 725 | continue; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 726 | if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0) |
| 727 | continue; |
| 728 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 729 | endp = &bsc->nat->mgcp_cfg->trunk.endpoints[i]; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 730 | bsc_endp = &bsc->nat->bsc_endpoints[i]; |
| 731 | break; |
| 732 | } |
| 733 | |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 734 | if (!bsc_endp && strncmp("nat-", transaction_id, 4) == 0) { |
| 735 | handle_dlcx_response(bsc, msg, code, transaction_id); |
| 736 | return; |
| 737 | } |
| 738 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 739 | if (!bsc_endp) { |
Holger Hans Peter Freyther | b9ac37d | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 740 | LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n", |
| 741 | transaction_id, (const char *) msg->l2h); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 745 | endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h); |
Holger Hans Peter Freyther | b84b5f6 | 2010-08-06 08:34:46 +0800 | [diff] [blame] | 746 | if (endp->ci == CI_UNUSED) { |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 747 | free_chan_downstream(endp, bsc_endp, bsc); |
Holger Hans Peter Freyther | b84b5f6 | 2010-08-06 08:34:46 +0800 | [diff] [blame] | 748 | return; |
| 749 | } |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 750 | |
Daniel Willmann | 5754206 | 2016-06-29 16:24:42 +0200 | [diff] [blame] | 751 | if (endp->osmux.state == OSMUX_STATE_NEGOTIATING) |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 752 | bsc_mgcp_osmux_confirm(endp, (const char *) msg->l2h); |
| 753 | |
Holger Hans Peter Freyther | 20626dd | 2015-10-02 18:15:18 +0200 | [diff] [blame] | 754 | /* If we require osmux and it is disabled.. fail */ |
| 755 | if (nat_osmux_only(bsc->nat->mgcp_cfg, bsc->cfg) && |
| 756 | endp->osmux.state == OSMUX_STATE_DISABLED) { |
| 757 | LOGP(DMGCP, LOGL_ERROR, |
| 758 | "Failed to activate osmux endpoint 0x%x\n", |
| 759 | ENDPOINT_NUMBER(endp)); |
| 760 | free_chan_downstream(endp, bsc_endp, bsc); |
| 761 | return; |
| 762 | } |
| 763 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 764 | /* free some stuff */ |
| 765 | talloc_free(bsc_endp->transaction_id); |
| 766 | bsc_endp->transaction_id = NULL; |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 767 | bsc_endp->transaction_state = 0; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 768 | |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 769 | /* |
| 770 | * rewrite the information. In case the endpoint was deleted |
| 771 | * there should be nothing for us to rewrite so putting endp->rtp_port |
| 772 | * with the value of 0 should be no problem. |
| 773 | */ |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 774 | output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1, |
Holger Hans Peter Freyther | e6ed009 | 2015-08-20 14:58:19 +0200 | [diff] [blame] | 775 | mgcp_net_src_addr(endp), |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 776 | endp->net_end.local_port, -1, |
Holger Hans Peter Freyther | 7f100c9 | 2015-04-23 20:25:17 -0400 | [diff] [blame] | 777 | &endp->bts_end.codec.payload_type, |
| 778 | bsc->nat->sdp_ensure_amr_mode_set); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 779 | if (!output) { |
| 780 | LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n"); |
| 781 | return; |
| 782 | } |
| 783 | |
Holger Hans Peter Freyther | 77956aa | 2012-11-05 13:52:53 +0100 | [diff] [blame] | 784 | mgcp_queue_for_call_agent(bsc->nat, output); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]) |
| 788 | { |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 789 | int rc; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 790 | /* we want to parse two strings */ |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 791 | rc = sscanf(str, "%3d %59s\n", code, transaction) != 2; |
| 792 | transaction[59] = '\0'; |
| 793 | return rc; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 796 | uint32_t bsc_mgcp_extract_ci(const char *str) |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 797 | { |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 798 | unsigned int ci; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 799 | char *res = strstr(str, "I: "); |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 800 | if (!res) { |
| 801 | LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 802 | return CI_UNUSED; |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 803 | } |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 804 | |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 805 | if (sscanf(res, "I: %u", &ci) != 1) { |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 806 | LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 807 | return CI_UNUSED; |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 808 | } |
| 809 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 810 | return ci; |
| 811 | } |
| 812 | |
Holger Hans Peter Freyther | 9592c45 | 2012-01-17 15:58:48 +0100 | [diff] [blame] | 813 | /** |
| 814 | * Create a new MGCPCommand based on the input and endpoint from a message |
| 815 | */ |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 816 | static void patch_mgcp(struct msgb *output, const char *op, const char *tok, |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 817 | int endp, int len, int cr, int osmux_cid) |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 818 | { |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 819 | int slen; |
| 820 | int ret; |
| 821 | char buf[40]; |
Holger Hans Peter Freyther | 6962127 | 2015-10-08 16:58:13 +0200 | [diff] [blame] | 822 | char osmux_extension[strlen("\nX-Osmux: 255") + 1]; |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 823 | |
| 824 | buf[0] = buf[39] = '\0'; |
| 825 | ret = sscanf(tok, "%*s %s", buf); |
Holger Hans Peter Freyther | 9592c45 | 2012-01-17 15:58:48 +0100 | [diff] [blame] | 826 | if (ret != 1) { |
| 827 | LOGP(DMGCP, LOGL_ERROR, |
| 828 | "Failed to find Endpoint in: %s\n", tok); |
| 829 | return; |
| 830 | } |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 831 | |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 832 | if (osmux_cid >= 0) |
Holger Hans Peter Freyther | 6962127 | 2015-10-08 16:58:13 +0200 | [diff] [blame] | 833 | sprintf(osmux_extension, "\nX-Osmux: %u", osmux_cid & 0xff); |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 834 | else |
| 835 | osmux_extension[0] = '\0'; |
| 836 | |
Pablo Neira Ayuso | cab6e75 | 2014-02-05 18:56:17 +0100 | [diff] [blame] | 837 | slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s%s", |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 838 | op, buf, endp, osmux_extension, cr ? "\r\n" : "\n"); |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 839 | output->l3h = msgb_put(output, slen); |
| 840 | } |
| 841 | |
| 842 | /* we need to replace some strings... */ |
Jacob Erlbeck | 3dff27d | 2013-11-29 13:43:48 +0100 | [diff] [blame] | 843 | struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 844 | const char *ip, int port, int osmux_cid, |
Holger Hans Peter Freyther | e9f7c99 | 2015-08-13 18:31:09 +0200 | [diff] [blame] | 845 | int *first_payload_type, int ensure_mode_set) |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 846 | { |
Holger Hans Peter Freyther | 3dfe8a1 | 2012-11-07 11:36:58 +0100 | [diff] [blame] | 847 | static const char crcx_str[] = "CRCX "; |
| 848 | static const char dlcx_str[] = "DLCX "; |
| 849 | static const char mdcx_str[] = "MDCX "; |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 850 | |
Holger Hans Peter Freyther | 3dfe8a1 | 2012-11-07 11:36:58 +0100 | [diff] [blame] | 851 | static const char ip_str[] = "c=IN IP4 "; |
| 852 | static const char aud_str[] = "m=audio "; |
| 853 | static const char fmt_str[] = "a=fmtp:"; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 854 | |
| 855 | char buf[128]; |
| 856 | char *running, *token; |
| 857 | struct msgb *output; |
| 858 | |
Holger Hans Peter Freyther | e2f34d5 | 2012-11-06 13:16:26 +0100 | [diff] [blame] | 859 | /* keep state to add the a=fmtp line */ |
| 860 | int found_fmtp = 0; |
| 861 | int payload = -1; |
| 862 | int cr = 1; |
| 863 | |
| 864 | if (length > 4096 - 256) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 865 | LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n"); |
| 866 | return NULL; |
| 867 | } |
| 868 | |
| 869 | output = msgb_alloc_headroom(4096, 128, "MGCP rewritten"); |
| 870 | if (!output) { |
| 871 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n"); |
| 872 | return NULL; |
| 873 | } |
| 874 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 875 | running = input; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 876 | output->l2h = output->data; |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 877 | output->l3h = output->l2h; |
Holger Hans Peter Freyther | 9e5300a | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 878 | for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 879 | int len = strlen(token); |
Holger Hans Peter Freyther | e2f34d5 | 2012-11-06 13:16:26 +0100 | [diff] [blame] | 880 | cr = len > 0 && token[len - 1] == '\r'; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 881 | |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 882 | if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) { |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 883 | patch_mgcp(output, "CRCX", token, endpoint, len, cr, osmux_cid); |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 884 | } else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) { |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 885 | patch_mgcp(output, "DLCX", token, endpoint, len, cr, -1); |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 886 | } else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) { |
Pablo Neira Ayuso | b769f3c | 2014-08-27 17:02:52 +0200 | [diff] [blame] | 887 | patch_mgcp(output, "MDCX", token, endpoint, len, cr, -1); |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 888 | } else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 889 | output->l3h = msgb_put(output, strlen(ip_str)); |
| 890 | memcpy(output->l3h, ip_str, strlen(ip_str)); |
| 891 | output->l3h = msgb_put(output, strlen(ip)); |
| 892 | memcpy(output->l3h, ip, strlen(ip)); |
Holger Hans Peter Freyther | 9e5300a | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 893 | |
| 894 | if (cr) { |
| 895 | output->l3h = msgb_put(output, 2); |
| 896 | output->l3h[0] = '\r'; |
| 897 | output->l3h[1] = '\n'; |
| 898 | } else { |
| 899 | output->l3h = msgb_put(output, 1); |
| 900 | output->l3h[0] = '\n'; |
| 901 | } |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 902 | } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) { |
Holger Hans Peter Freyther | e9f7c99 | 2015-08-13 18:31:09 +0200 | [diff] [blame] | 903 | int offset; |
| 904 | if (sscanf(token, "m=audio %*d RTP/AVP %n%d", &offset, &payload) != 1) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 905 | LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n"); |
| 906 | msgb_free(output); |
| 907 | return NULL; |
| 908 | } |
| 909 | |
Holger Hans Peter Freyther | e9f7c99 | 2015-08-13 18:31:09 +0200 | [diff] [blame] | 910 | snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %s\n", |
| 911 | port, &token[offset]); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 912 | buf[sizeof(buf)-1] = '\0'; |
| 913 | |
| 914 | output->l3h = msgb_put(output, strlen(buf)); |
| 915 | memcpy(output->l3h, buf, strlen(buf)); |
Holger Hans Peter Freyther | e2f34d5 | 2012-11-06 13:16:26 +0100 | [diff] [blame] | 916 | } else if (strncmp(fmt_str, token, (sizeof fmt_str) - 1) == 0) { |
| 917 | found_fmtp = 1; |
| 918 | goto copy; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 919 | } else { |
Holger Hans Peter Freyther | e2f34d5 | 2012-11-06 13:16:26 +0100 | [diff] [blame] | 920 | copy: |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 921 | output->l3h = msgb_put(output, len + 1); |
| 922 | memcpy(output->l3h, token, len); |
| 923 | output->l3h[len] = '\n'; |
| 924 | } |
| 925 | } |
| 926 | |
Holger Hans Peter Freyther | e2f34d5 | 2012-11-06 13:16:26 +0100 | [diff] [blame] | 927 | /* |
| 928 | * the above code made sure that we have 128 bytes lefts. So we can |
| 929 | * safely append another line. |
| 930 | */ |
Holger Hans Peter Freyther | 7f100c9 | 2015-04-23 20:25:17 -0400 | [diff] [blame] | 931 | if (ensure_mode_set && !found_fmtp && payload != -1) { |
Holger Hans Peter Freyther | e2f34d5 | 2012-11-06 13:16:26 +0100 | [diff] [blame] | 932 | snprintf(buf, sizeof(buf) - 1, "a=fmtp:%d mode-set=2%s", |
| 933 | payload, cr ? "\r\n" : "\n"); |
| 934 | buf[sizeof(buf) - 1] = '\0'; |
| 935 | output->l3h = msgb_put(output, strlen(buf)); |
| 936 | memcpy(output->l3h, buf, strlen(buf)); |
| 937 | } |
| 938 | |
Holger Hans Peter Freyther | e9f7c99 | 2015-08-13 18:31:09 +0200 | [diff] [blame] | 939 | if (payload != -1 && first_payload_type) |
| 940 | *first_payload_type = payload; |
Jacob Erlbeck | 3dff27d | 2013-11-29 13:43:48 +0100 | [diff] [blame] | 941 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 942 | return output; |
| 943 | } |
| 944 | |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 945 | /* |
| 946 | * This comes from the MSC and we will now parse it. The caller needs |
| 947 | * to free the msgb. |
| 948 | */ |
| 949 | void bsc_nat_handle_mgcp(struct bsc_nat *nat, struct msgb *msg) |
| 950 | { |
| 951 | struct msgb *resp; |
| 952 | |
| 953 | if (!nat->mgcp_ipa) { |
| 954 | LOGP(DMGCP, LOGL_ERROR, "MGCP message not allowed on IPA.\n"); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | if (msgb_l2len(msg) > sizeof(nat->mgcp_msg) - 1) { |
| 959 | LOGP(DMGCP, LOGL_ERROR, "MGCP msg too big for handling.\n"); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | memcpy(nat->mgcp_msg, msg->l2h, msgb_l2len(msg)); |
| 964 | nat->mgcp_length = msgb_l2len(msg); |
| 965 | nat->mgcp_msg[nat->mgcp_length] = '\0'; |
| 966 | |
| 967 | /* now handle the message */ |
| 968 | resp = mgcp_handle_message(nat->mgcp_cfg, msg); |
| 969 | |
| 970 | /* we do have a direct answer... e.g. AUEP */ |
| 971 | if (resp) |
| 972 | mgcp_queue_for_call_agent(nat, resp); |
| 973 | |
| 974 | return; |
| 975 | } |
| 976 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 977 | static int mgcp_do_read(struct osmo_fd *fd) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 978 | { |
| 979 | struct bsc_nat *nat; |
| 980 | struct msgb *msg, *resp; |
| 981 | int rc; |
| 982 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 983 | nat = fd->data; |
| 984 | |
| 985 | rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1); |
| 986 | if (rc <= 0) { |
| 987 | LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno); |
| 988 | return -1; |
| 989 | } |
| 990 | |
| 991 | nat->mgcp_msg[rc] = '\0'; |
| 992 | nat->mgcp_length = rc; |
| 993 | |
| 994 | msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read"); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 995 | if (!msg) { |
| 996 | LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n"); |
| 997 | return -1; |
| 998 | } |
| 999 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1000 | msg->l2h = msgb_put(msg, rc); |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 1001 | memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg)); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1002 | resp = mgcp_handle_message(nat->mgcp_cfg, msg); |
| 1003 | msgb_free(msg); |
| 1004 | |
| 1005 | /* we do have a direct answer... e.g. AUEP */ |
Holger Hans Peter Freyther | 77956aa | 2012-11-05 13:52:53 +0100 | [diff] [blame] | 1006 | if (resp) |
| 1007 | mgcp_queue_for_call_agent(nat, resp); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1012 | static int mgcp_do_write(struct osmo_fd *bfd, struct msgb *msg) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1013 | { |
| 1014 | int rc; |
| 1015 | |
| 1016 | rc = write(bfd->fd, msg->data, msg->len); |
| 1017 | |
| 1018 | if (rc != msg->len) { |
| 1019 | LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n"); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | |
| 1023 | return rc; |
| 1024 | } |
| 1025 | |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 1026 | static int init_mgcp_socket(struct bsc_nat *nat, struct mgcp_config *cfg) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1027 | { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1028 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 1029 | int on; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 1030 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1031 | cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 1032 | if (cfg->gw_fd.bfd.fd < 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1033 | LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno); |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |
| 1037 | on = 1; |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1038 | setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1039 | |
Holger Hans Peter Freyther | 9bec10e | 2013-07-05 07:48:04 +0200 | [diff] [blame] | 1040 | memset(&addr, 0, sizeof(addr)); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1041 | addr.sin_family = AF_INET; |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1042 | addr.sin_port = htons(cfg->source_port); |
| 1043 | inet_aton(cfg->source_addr, &addr.sin_addr); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1044 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1045 | if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | d000c27 | 2011-03-29 17:12:07 +0200 | [diff] [blame] | 1046 | LOGP(DMGCP, LOGL_ERROR, "Failed to bind on %s:%d errno: %d\n", |
| 1047 | cfg->source_addr, cfg->source_port, errno); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1048 | close(cfg->gw_fd.bfd.fd); |
| 1049 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1050 | return -1; |
| 1051 | } |
| 1052 | |
| 1053 | addr.sin_port = htons(2727); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1054 | inet_aton(cfg->call_agent_addr, &addr.sin_addr); |
| 1055 | if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1056 | LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n", |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1057 | cfg->call_agent_addr, errno); |
| 1058 | close(cfg->gw_fd.bfd.fd); |
| 1059 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1060 | return -1; |
| 1061 | } |
| 1062 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 1063 | osmo_wqueue_init(&cfg->gw_fd, 10); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1064 | cfg->gw_fd.bfd.when = BSC_FD_READ; |
| 1065 | cfg->gw_fd.bfd.data = nat; |
| 1066 | cfg->gw_fd.read_cb = mgcp_do_read; |
| 1067 | cfg->gw_fd.write_cb = mgcp_do_write; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1068 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1069 | if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1070 | LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n"); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1071 | close(cfg->gw_fd.bfd.fd); |
| 1072 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1073 | return -1; |
| 1074 | } |
| 1075 | |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | int bsc_mgcp_nat_init(struct bsc_nat *nat) |
| 1080 | { |
| 1081 | struct mgcp_config *cfg = nat->mgcp_cfg; |
| 1082 | |
| 1083 | if (!cfg->call_agent_addr) { |
| 1084 | LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n"); |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
| 1088 | if (cfg->bts_ip) { |
| 1089 | LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n"); |
| 1090 | return -1; |
| 1091 | } |
| 1092 | |
| 1093 | /* initialize the MGCP socket */ |
| 1094 | if (!nat->mgcp_ipa) { |
| 1095 | int rc = init_mgcp_socket(nat, cfg); |
| 1096 | if (rc != 0) |
| 1097 | return rc; |
| 1098 | } |
| 1099 | |
| 1100 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 1101 | /* some more MGCP config handling */ |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1102 | cfg->data = nat; |
| 1103 | cfg->policy_cb = bsc_mgcp_policy_cb; |
Holger Hans Peter Freyther | d5e6c23 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 1104 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1105 | if (cfg->bts_ip) |
| 1106 | talloc_free(cfg->bts_ip); |
| 1107 | cfg->bts_ip = ""; |
Holger Hans Peter Freyther | d5e6c23 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 1108 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 1109 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 1110 | struct bsc_endpoint, |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1111 | cfg->trunk.number_endpoints + 1); |
Holger Hans Peter Freyther | 3c79214 | 2010-09-19 04:34:04 +0800 | [diff] [blame] | 1112 | if (!nat->bsc_endpoints) { |
| 1113 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n"); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 1114 | close(cfg->gw_fd.bfd.fd); |
| 1115 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | 3c79214 | 2010-09-19 04:34:04 +0800 | [diff] [blame] | 1116 | return -1; |
| 1117 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 1118 | |
Holger Hans Peter Freyther | f2eedff | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 1119 | if (mgcp_reset_transcoder(cfg) < 0) { |
| 1120 | LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n"); |
| 1121 | talloc_free(nat->bsc_endpoints); |
| 1122 | nat->bsc_endpoints = NULL; |
| 1123 | close(cfg->gw_fd.bfd.fd); |
| 1124 | cfg->gw_fd.bfd.fd = -1; |
| 1125 | return -1; |
| 1126 | } |
| 1127 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 1130 | |
| 1131 | void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc) |
| 1132 | { |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 1133 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 1134 | int i; |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 1135 | |
| 1136 | if (bsc->cfg) |
| 1137 | ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS]; |
| 1138 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 1139 | for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) { |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 1140 | struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i]; |
| 1141 | |
| 1142 | if (bsc_endp->bsc != bsc) |
| 1143 | continue; |
| 1144 | |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 1145 | if (ctr) |
| 1146 | rate_ctr_inc(ctr); |
| 1147 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 1148 | bsc_mgcp_free_endpoint(bsc->nat, i); |
Holger Hans Peter Freyther | cb6ad70 | 2014-07-22 15:00:52 +0200 | [diff] [blame] | 1149 | mgcp_release_endp(&bsc->nat->mgcp_cfg->trunk.endpoints[i]); |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 1150 | } |
| 1151 | } |