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