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