blob: 226486c320402228f4f5d4fc6ab0c8544eba27d6 [file] [log] [blame]
Holger Hans Peter Freytheraddd6da2012-10-24 21:52:16 +02001/**
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 Freytherdccb9152010-06-15 18:49:53 +080025/*
Holger Hans Peter Freytheraddd6da2012-10-24 21:52:16 +020026 * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
27 * (C) 2010-2012 by On-Waves
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +080028 * All Rights Reserved
29 *
30 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +010031 * 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 Freytherdccb9152010-06-15 18:49:53 +080033 * (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 Welte0e3e88e2011-01-01 15:25:50 +010038 * GNU Affero General Public License for more details.
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +080039 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010040 * 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 Freytherdccb9152010-06-15 18:49:53 +080042 *
43 */
44
45#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther4a9dd3b2010-07-31 05:17:17 +080046#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +080047#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +080048#include <openbsc/debug.h>
Holger Hans Peter Freytherd5cd3042010-10-13 23:52:01 +020049#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther1f278392010-03-31 09:16:56 +020050#include <openbsc/mgcp.h>
51#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +020052#include <openbsc/control_cmd.h>
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +080053
Harald Welte90e5eae2010-08-03 15:11:51 +020054#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +080055
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010056#include <osmocom/core/talloc.h>
Harald Welte8b1713a2011-03-23 18:26:56 +010057#include <osmocom/gsm/gsm0808.h>
58#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +020059
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +080060#include <netinet/in.h>
61#include <arpa/inet.h>
62
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +020063#include <errno.h>
64#include <unistd.h>
65
Holger Hans Peter Freythercd8bea92012-11-05 14:54:56 +010066static void send_direct(struct bsc_nat *nat, struct msgb *output)
Holger Hans Peter Freytherdceba0f2012-11-05 13:52:53 +010067{
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 Freythercd8bea92012-11-05 14:54:56 +010074static 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 Freytherb28f5a02011-02-27 11:04:27 +010082int 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 Freyther95751642011-02-25 17:09:07 +010092static int bsc_init_endps_if_needed(struct bsc_connection *con)
93{
Holger Hans Peter Freytherb28f5a02011-02-27 11:04:27 +010094 int multiplexes;
95
Holger Hans Peter Freyther95751642011-02-25 17:09:07 +010096 /* 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 Freytherb28f5a02011-02-27 11:04:27 +0100104 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 Freyther95751642011-02-25 17:09:07 +0100108 return con->_endpoint_status == NULL;
109}
110
Holger Hans Peter Freyther926c5fd2010-08-28 18:22:14 +0800111static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
112{
Holger Hans Peter Freyther473b38a2011-02-26 11:38:00 +0100113 int multiplex;
114 int timeslot;
Holger Hans Peter Freytherb28f5a02011-02-27 11:04:27 +0100115 const int number_endpoints = bsc->max_endpoints;
Holger Hans Peter Freyther926c5fd2010-08-28 18:22:14 +0800116 int i;
117
Holger Hans Peter Freyther473b38a2011-02-26 11:38:00 +0100118 mgcp_endpoint_to_timeslot(bsc->last_endpoint, &multiplex, &timeslot);
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 Freyther926c5fd2010-08-28 18:22:14 +0800138
Holger Hans Peter Freytherb28f5a02011-02-27 11:04:27 +0100139 /* 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 Freyther95751642011-02-25 17:09:07 +0100147 if (bsc->_endpoint_status[endpoint] == 0) {
148 bsc->_endpoint_status[endpoint] = 1;
Holger Hans Peter Freyther926c5fd2010-08-28 18:22:14 +0800149 con->bsc_endp = endpoint;
150 bsc->last_endpoint = endpoint;
151 return 0;
152 }
Holger Hans Peter Freyther473b38a2011-02-26 11:38:00 +0100153
154 timeslot += 1;
Holger Hans Peter Freyther926c5fd2010-08-28 18:22:14 +0800155 }
156
157 return -1;
158}
159
160static uint16_t create_cic(int endpoint)
161{
162 int timeslot, multiplex;
163
164 mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
165 return (multiplex << 5) | (timeslot & 0x1f);
166}
167
168int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800169{
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800170 struct sccp_connections *mcon;
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800171 struct tlv_parsed tp;
Holger Hans Peter Freytherbd116392010-07-23 19:09:21 +0800172 uint16_t cic;
Holger Hans Peter Freyther0b776352010-07-23 19:08:55 +0800173 uint8_t timeslot;
174 uint8_t multiplex;
Holger Hans Peter Freythere355a432010-11-04 17:14:41 +0100175 unsigned int endp;
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800176
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 Freytherbd116392010-07-23 19:09:21 +0800193 cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
Holger Hans Peter Freytherdccb9152010-06-15 18:49:53 +0800194 timeslot = cic & 0x1f;
195 multiplex = (cic & ~0x1f) >> 5;
196
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800197
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800198 endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800199
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100200 if (endp >= con->bsc->nat->mgcp_cfg->trunk.number_endpoints) {
Holger Hans Peter Freythere355a432010-11-04 17:14:41 +0100201 LOGP(DNAT, LOGL_ERROR,
202 "MSC attempted to assign bad endpoint 0x%x\n",
203 endp);
204 return -1;
205 }
206
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800207 /* find stale connections using that endpoint */
208 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800209 if (mcon->msc_endp == endp) {
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800210 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800211 "Endpoint %d was assigned to 0x%x and now 0x%x\n",
212 endp,
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800213 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 Freyther7089d532010-08-28 16:08:39 +0800219 con->msc_endp = endp;
Holger Hans Peter Freyther95751642011-02-25 17:09:07 +0100220 if (bsc_init_endps_if_needed(con->bsc) != 0)
221 return -1;
Holger Hans Peter Freyther926c5fd2010-08-28 18:22:14 +0800222 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 Freytherdccb9152010-06-15 18:49:53 +0800233 return 0;
234}
235
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800236static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200237{
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 Freyther9a877822010-08-06 09:05:05 +0800243 nat->bsc_endpoints[i].transaction_state = 0;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200244 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200245}
246
Holger Hans Peter Freyther1f278392010-03-31 09:16:56 +0200247void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
248{
249 int i;
250
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100251 for (i = 1; i < nat->mgcp_cfg->trunk.number_endpoints; ++i){
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200252 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100253 mgcp_free_endp(&nat->mgcp_cfg->trunk.endpoints[i]);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800254 }
Holger Hans Peter Freyther1f278392010-03-31 09:16:56 +0200255}
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200256
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800257/* send a MDCX where we do not want a response */
Holger Hans Peter Freyther2b5ae6b2010-08-29 23:40:33 +0800258static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800259{
260 char buf[2096];
261 int len;
262
263 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf0ac14a2010-04-22 13:06:24 +0800264 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800265 "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 Freyther2b5ae6b2010-08-29 23:40:33 +0800269 port,
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800270 bsc->nat->mgcp_cfg->source_addr,
Holger Hans Peter Freyther148c5d32010-08-05 03:08:35 +0800271 endp->bts_end.local_port);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800272 if (len < 0) {
Holger Hans Peter Freyther1cef0ba2012-09-14 15:51:43 +0200273 LOGP(DMGCP, LOGL_ERROR, "snprintf for MDCX failed.\n");
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800274 return;
275 }
Holger Hans Peter Freyther14f728e2010-08-30 11:58:49 +0800276
Holger Hans Peter Freyther053d93f2012-10-30 16:32:19 +0100277 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800278}
279
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200280static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint, int trans)
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800281{
282 char buf[2096];
283 int len;
284
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200285 /*
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 Freyther36330952010-04-22 12:08:17 +0800291 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200292 "DLCX nat-%u %x@mgw MGCP 1.0\r\n",
293 trans, endpoint);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800294 if (len < 0) {
295 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
296 return;
297 }
298
Holger Hans Peter Freyther0b776352010-07-23 19:08:55 +0800299 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800300}
301
302void bsc_mgcp_init(struct sccp_connections *con)
303{
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800304 con->msc_endp = -1;
305 con->bsc_endp = -1;
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800306}
307
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200308/**
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 */
313static void remember_pending_dlcx(struct sccp_connections *con, uint32_t transaction)
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 Freyther4dc87ee2012-11-12 18:36:00 +0100350 /*
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 Freyther931d6982012-10-24 21:53:40 +0200365 bsc->pending_dlcx_count += 1;
366 llist_add_tail(&stats->entry, &bsc->pending_dlcx);
367}
368
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800369void bsc_mgcp_dlcx(struct sccp_connections *con)
370{
371 /* send a DLCX down the stream */
Holger Hans Peter Freyther95751642011-02-25 17:09:07 +0100372 if (con->bsc_endp != -1 && con->bsc->_endpoint_status) {
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200373 LOGP(DNAT, LOGL_NOTICE,
Holger Hans Peter Freyther755c0122012-11-12 18:00:25 +0100374 "Endpoint 0x%x was allocated for bsc: %d. Freeing it.\n",
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200375 con->bsc_endp, con->bsc->cfg->nr);
Holger Hans Peter Freyther95751642011-02-25 17:09:07 +0100376 if (con->bsc->_endpoint_status[con->bsc_endp] != 1)
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800377 LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200378 remember_pending_dlcx(con, con->bsc->next_transaction);
Holger Hans Peter Freyther95751642011-02-25 17:09:07 +0100379 con->bsc->_endpoint_status[con->bsc_endp] = 0;
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200380 bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp, con->bsc->next_transaction++);
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800381 bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800382 }
383
384 bsc_mgcp_init(con);
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200385
386}
387
388/*
389 * Search for the pending request
390 */
391static 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 Freyther58e4a8e2012-11-12 19:05:16 +0100438 cmd->variable = "nat.call_stats.v2";
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200439 cmd->reply = talloc_asprintf(cmd,
440 "bsc_id=%d,mg_ip_addr=%s,mg_port=%d,",
441 bsc->cfg->nr, inet_ntoa(stat->net_addr),
442 stat->net_rtp_port);
443 cmd->reply = talloc_asprintf_append(cmd->reply,
444 "endpoint_ip_addr=%s,endpoint_port=%d,",
445 inet_ntoa(stat->bts_addr),
446 stat->bts_rtp_port);
447 cmd->reply = talloc_asprintf_append(cmd->reply,
448 "nat_pkt_in=%u,nat_pkt_out=%u,"
449 "nat_bytes_in=%u,nat_bytes_out=%u,"
450 "nat_jitter=%u,nat_pkt_lost=%d,",
451 stat->bts_pr, stat->net_ps,
452 stat->bts_or, stat->net_os,
453 stat->bts_jitter, stat->bts_loss);
454 cmd->reply = talloc_asprintf_append(cmd->reply,
455 "bsc_pkt_in=%u,bsc_pkt_out=%u,"
456 "bsc_bytes_in=%u,bsc_bytes_out=%u,"
Holger Hans Peter Freyther58e4a8e2012-11-12 19:05:16 +0100457 "bsc_jitter=%u,bsc_pkt_lost=%d,",
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200458 n_pr, b_ps,
459 n_or, b_os,
460 jitter, loss);
Holger Hans Peter Freyther58e4a8e2012-11-12 19:05:16 +0100461 cmd->reply = talloc_asprintf_append(cmd->reply,
462 "sccp_src_ref=%u,sccp_dst_ref=%u",
463 sccp_src_ref_to_int(&stat->src_ref),
464 sccp_src_ref_to_int(&stat->remote_ref));
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200465
466 /* send it and be done */
467 ctrl_cmd_send_to_all(bsc->nat->ctrl, cmd);
468 talloc_free(cmd);
469
470free_stat:
471 bsc->pending_dlcx_count -= 1;
472 llist_del(&stat->entry);
473 talloc_free(stat);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800474}
475
476
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800477struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freyther89179982010-04-01 03:55:27 +0200478{
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800479 struct sccp_connections *con = NULL;
Holger Hans Peter Freyther89179982010-04-01 03:55:27 +0200480 struct sccp_connections *sccp;
481
482 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800483 if (sccp->msc_endp == -1)
Holger Hans Peter Freyther89179982010-04-01 03:55:27 +0200484 continue;
Holger Hans Peter Freyther7089d532010-08-28 16:08:39 +0800485 if (sccp->msc_endp != endpoint)
Holger Hans Peter Freyther89179982010-04-01 03:55:27 +0200486 continue;
487
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800488 con = sccp;
Holger Hans Peter Freyther89179982010-04-01 03:55:27 +0200489 }
490
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800491 if (con)
492 return con;
Holger Hans Peter Freyther259b90b2010-04-18 02:14:45 +0800493
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200494 LOGP(DMGCP, LOGL_ERROR,
495 "Failed to find the connection for endpoint: 0x%x\n", endpoint);
Holger Hans Peter Freyther89179982010-04-01 03:55:27 +0200496 return NULL;
497}
498
Holger Hans Peter Freyther7f1ae4a2012-10-24 21:53:07 +0200499static int bsc_mgcp_policy_cb(struct mgcp_trunk_config *tcfg, int endpoint, int state, const char *transaction_id)
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200500{
501 struct bsc_nat *nat;
502 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800503 struct sccp_connections *sccp;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200504 struct mgcp_endpoint *mgcp_endp;
505 struct msgb *bsc_msg;
506
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100507 nat = tcfg->cfg->data;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200508 bsc_endp = &nat->bsc_endpoints[endpoint];
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100509 mgcp_endp = &nat->mgcp_cfg->trunk.endpoints[endpoint];
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200510
Holger Hans Peter Freyther6bed0482010-04-24 21:05:18 +0800511 if (bsc_endp->transaction_id) {
512 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
513 endpoint, bsc_endp->transaction_id);
514 talloc_free(bsc_endp->transaction_id);
515 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther9a877822010-08-06 09:05:05 +0800516 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther6bed0482010-04-24 21:05:18 +0800517 }
518 bsc_endp->bsc = NULL;
519
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800520 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200521
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800522 if (!sccp) {
Holger Hans Peter Freyther2743ec42010-04-24 21:02:01 +0800523 LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for change on endpoint: 0x%x state: %d\n", endpoint, state);
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200524
525 switch (state) {
526 case MGCP_ENDP_CRCX:
527 return MGCP_POLICY_REJECT;
528 break;
529 case MGCP_ENDP_DLCX:
530 return MGCP_POLICY_CONT;
531 break;
532 case MGCP_ENDP_MDCX:
533 return MGCP_POLICY_CONT;
534 break;
535 default:
536 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
537 return MGCP_POLICY_CONT;
538 break;
539 }
540 }
541
Holger Hans Peter Freyther8afdcba2010-04-07 09:53:54 +0200542 /* we need to generate a new and patched message */
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800543 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
Holger Hans Peter Freyther148c5d32010-08-05 03:08:35 +0800544 nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
Holger Hans Peter Freyther8afdcba2010-04-07 09:53:54 +0200545 if (!bsc_msg) {
546 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
547 return MGCP_POLICY_CONT;
548 }
549
550
Holger Hans Peter Freythere85bc492010-04-04 18:11:49 +0200551 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther9a877822010-08-06 09:05:05 +0800552 bsc_endp->transaction_state = state;
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800553 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200554
555 /* we need to update some bits */
556 if (state == MGCP_ENDP_CRCX) {
557 struct sockaddr_in sock;
558 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther98d15ef2010-04-18 02:26:16 +0800559 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther75749d02010-04-06 11:17:07 +0200560 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
561 errno, strerror(errno));
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200562 } else {
Holger Hans Peter Freytherfab76aa2010-08-05 01:34:51 +0800563 mgcp_endp->bts_end.addr = sock.sin_addr;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200564 }
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200565
Holger Hans Peter Freytherf48e4b62010-08-05 01:50:44 +0800566 /* send the message and a fake MDCX to force sending of a dummy packet */
Holger Hans Peter Freythera63d35c2011-01-07 16:54:46 +0100567 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther2b5ae6b2010-08-29 23:40:33 +0800568 bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800569 return MGCP_POLICY_DEFER;
570 } else if (state == MGCP_ENDP_DLCX) {
571 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freytherfbd69642010-05-01 10:31:53 +0800572 msgb_free(bsc_msg);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800573 bsc_mgcp_dlcx(sccp);
574 return MGCP_POLICY_CONT;
575 } else {
Holger Hans Peter Freythera63d35c2011-01-07 16:54:46 +0100576 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800577 return MGCP_POLICY_DEFER;
578 }
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200579}
580
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200581/*
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800582 * We do have a failure, free data downstream..
583 */
584static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
585 struct bsc_connection *bsc)
586{
Holger Hans Peter Freytheraa861612010-08-28 16:46:27 +0800587 LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
588 ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800589
Holger Hans Peter Freytheraa861612010-08-28 16:46:27 +0800590 /* if a CRCX failed... send a DLCX down the stream */
591 if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
592 struct sccp_connections *con;
593 con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
594 if (!con) {
595 LOGP(DMGCP, LOGL_ERROR,
596 "No SCCP connection for endp 0x%x\n",
597 ENDPOINT_NUMBER(endp));
598 } else {
599 if (con->bsc == bsc) {
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200600 bsc_mgcp_send_dlcx(bsc, con->bsc_endp, con->bsc->next_transaction++);
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800601 } else {
Holger Hans Peter Freytheraa861612010-08-28 16:46:27 +0800602 LOGP(DMGCP, LOGL_ERROR,
603 "Endpoint belongs to a different BSC\n");
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800604 }
605 }
Holger Hans Peter Freytheraa861612010-08-28 16:46:27 +0800606 }
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800607
Holger Hans Peter Freytheraa861612010-08-28 16:46:27 +0800608 bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
609 mgcp_free_endp(endp);
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800610}
611
612/*
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200613 * We have received a msg from the BSC. We will see if we know
614 * this transaction and if it belongs to the BSC. Then we will
615 * need to patch the content to point to the local network and we
616 * need to update the I: that was assigned by the BSS.
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200617 *
618 * Only responses to CRCX and DLCX should arrive here. The DLCX
619 * needs to be handled specially to combine the two statistics.
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200620 */
621void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
622{
623 struct msgb *output;
624 struct bsc_endpoint *bsc_endp = NULL;
625 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freyther65b745e2010-04-06 11:06:11 +0200626 int i, code;
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200627 char transaction_id[60];
628
629 /* Some assumption that our buffer is big enough.. and null terminate */
630 if (msgb_l2len(msg) > 2000) {
631 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
632 return;
633 }
634
635 msg->l2h[msgb_l2len(msg)] = '\0';
636
637 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
638 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
639 return;
640 }
641
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100642 for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200643 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
644 continue;
Holger Hans Peter Freytherf9c8d0f2010-04-05 18:00:05 +0200645 /* no one listening? a bug? */
646 if (!bsc->nat->bsc_endpoints[i].transaction_id)
647 continue;
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200648 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
649 continue;
650
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100651 endp = &bsc->nat->mgcp_cfg->trunk.endpoints[i];
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200652 bsc_endp = &bsc->nat->bsc_endpoints[i];
653 break;
654 }
655
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200656 if (!bsc_endp && strncmp("nat-", transaction_id, 4) == 0) {
657 handle_dlcx_response(bsc, msg, code, transaction_id);
658 return;
659 }
660
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200661 if (!bsc_endp) {
Holger Hans Peter Freytherc12e1932010-04-05 17:58:52 +0200662 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
663 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200664 return;
665 }
666
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800667 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freythera9f132d2010-08-06 08:34:46 +0800668 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freythere3ed2be2010-08-06 19:16:34 +0800669 free_chan_downstream(endp, bsc_endp, bsc);
Holger Hans Peter Freythera9f132d2010-08-06 08:34:46 +0800670 return;
671 }
Holger Hans Peter Freyther8afdcba2010-04-07 09:53:54 +0200672
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200673 /* free some stuff */
674 talloc_free(bsc_endp->transaction_id);
675 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther9a877822010-08-06 09:05:05 +0800676 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200677
Holger Hans Peter Freyther8afdcba2010-04-07 09:53:54 +0200678 /*
679 * rewrite the information. In case the endpoint was deleted
680 * there should be nothing for us to rewrite so putting endp->rtp_port
681 * with the value of 0 should be no problem.
682 */
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800683 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
Holger Hans Peter Freyther148c5d32010-08-05 03:08:35 +0800684 bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
Holger Hans Peter Freyther55edb782010-04-05 22:56:49 +0200685
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200686 if (!output) {
687 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
688 return;
689 }
690
Holger Hans Peter Freytherdceba0f2012-11-05 13:52:53 +0100691 mgcp_queue_for_call_agent(bsc->nat, output);
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200692}
693
694int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
695{
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200696 int rc;
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200697 /* we want to parse two strings */
Holger Hans Peter Freyther931d6982012-10-24 21:53:40 +0200698 rc = sscanf(str, "%3d %59s\n", code, transaction) != 2;
699 transaction[59] = '\0';
700 return rc;
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200701}
702
Holger Hans Peter Freyther526b4d32010-08-06 08:26:54 +0800703uint32_t bsc_mgcp_extract_ci(const char *str)
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200704{
Holger Hans Peter Freyther526b4d32010-08-06 08:26:54 +0800705 unsigned int ci;
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200706 char *res = strstr(str, "I: ");
Holger Hans Peter Freythercbc63542010-08-06 08:19:05 +0800707 if (!res) {
708 LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200709 return CI_UNUSED;
Holger Hans Peter Freythercbc63542010-08-06 08:19:05 +0800710 }
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200711
Holger Hans Peter Freyther526b4d32010-08-06 08:26:54 +0800712 if (sscanf(res, "I: %u", &ci) != 1) {
Holger Hans Peter Freythercbc63542010-08-06 08:19:05 +0800713 LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200714 return CI_UNUSED;
Holger Hans Peter Freythercbc63542010-08-06 08:19:05 +0800715 }
716
Holger Hans Peter Freyther9fec0102010-04-01 10:16:28 +0200717 return ci;
718}
719
Holger Hans Peter Freyther97a5a482012-01-17 15:58:48 +0100720/**
721 * Create a new MGCPCommand based on the input and endpoint from a message
722 */
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800723static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
724 int endp, int len, int cr)
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200725{
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800726 int slen;
727 int ret;
728 char buf[40];
729
730 buf[0] = buf[39] = '\0';
731 ret = sscanf(tok, "%*s %s", buf);
Holger Hans Peter Freyther97a5a482012-01-17 15:58:48 +0100732 if (ret != 1) {
733 LOGP(DMGCP, LOGL_ERROR,
734 "Failed to find Endpoint in: %s\n", tok);
735 return;
736 }
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800737
738 slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
739 op, buf, endp, cr ? "\r\n" : "\n");
740 output->l3h = msgb_put(output, slen);
741}
742
743/* we need to replace some strings... */
744struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
745{
Holger Hans Peter Freytherfd87aa02012-11-07 11:36:58 +0100746 static const char crcx_str[] = "CRCX ";
747 static const char dlcx_str[] = "DLCX ";
748 static const char mdcx_str[] = "MDCX ";
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800749
Holger Hans Peter Freytherfd87aa02012-11-07 11:36:58 +0100750 static const char ip_str[] = "c=IN IP4 ";
751 static const char aud_str[] = "m=audio ";
752 static const char fmt_str[] = "a=fmtp:";
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200753
754 char buf[128];
755 char *running, *token;
756 struct msgb *output;
757
Holger Hans Peter Freythera3d65192012-11-06 13:16:26 +0100758 /* keep state to add the a=fmtp line */
759 int found_fmtp = 0;
760 int payload = -1;
761 int cr = 1;
762
763 if (length > 4096 - 256) {
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200764 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
765 return NULL;
766 }
767
768 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
769 if (!output) {
770 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
771 return NULL;
772 }
773
Holger Hans Peter Freyther0d0aaa62010-04-04 18:09:10 +0200774 running = input;
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200775 output->l2h = output->data;
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800776 output->l3h = output->l2h;
Holger Hans Peter Freyther32370652010-04-04 19:34:44 +0200777 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200778 int len = strlen(token);
Holger Hans Peter Freythera3d65192012-11-06 13:16:26 +0100779 cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200780
Holger Hans Peter Freyther4e6129a2010-08-30 13:44:32 +0800781 if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
782 patch_mgcp(output, "CRCX", token, endpoint, len, cr);
783 } else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
784 patch_mgcp(output, "DLCX", token, endpoint, len, cr);
785 } else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
786 patch_mgcp(output, "MDCX", token, endpoint, len, cr);
787 } else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200788 output->l3h = msgb_put(output, strlen(ip_str));
789 memcpy(output->l3h, ip_str, strlen(ip_str));
790 output->l3h = msgb_put(output, strlen(ip));
791 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther32370652010-04-04 19:34:44 +0200792
793 if (cr) {
794 output->l3h = msgb_put(output, 2);
795 output->l3h[0] = '\r';
796 output->l3h[1] = '\n';
797 } else {
798 output->l3h = msgb_put(output, 1);
799 output->l3h[0] = '\n';
800 }
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200801 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200802 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
803 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
804 msgb_free(output);
805 return NULL;
806 }
807
Holger Hans Peter Freyther32370652010-04-04 19:34:44 +0200808 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
809 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200810 buf[sizeof(buf)-1] = '\0';
811
812 output->l3h = msgb_put(output, strlen(buf));
813 memcpy(output->l3h, buf, strlen(buf));
Holger Hans Peter Freythera3d65192012-11-06 13:16:26 +0100814 } else if (strncmp(fmt_str, token, (sizeof fmt_str) - 1) == 0) {
815 found_fmtp = 1;
816 goto copy;
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200817 } else {
Holger Hans Peter Freythera3d65192012-11-06 13:16:26 +0100818copy:
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200819 output->l3h = msgb_put(output, len + 1);
820 memcpy(output->l3h, token, len);
821 output->l3h[len] = '\n';
822 }
823 }
824
Holger Hans Peter Freythera3d65192012-11-06 13:16:26 +0100825 /*
826 * the above code made sure that we have 128 bytes lefts. So we can
827 * safely append another line.
828 */
829 if (!found_fmtp && payload != -1) {
830 snprintf(buf, sizeof(buf) - 1, "a=fmtp:%d mode-set=2%s",
831 payload, cr ? "\r\n" : "\n");
832 buf[sizeof(buf) - 1] = '\0';
833 output->l3h = msgb_put(output, strlen(buf));
834 memcpy(output->l3h, buf, strlen(buf));
835 }
836
Holger Hans Peter Freytheracd64202010-04-01 06:48:52 +0200837 return output;
838}
839
Holger Hans Peter Freythercd8bea92012-11-05 14:54:56 +0100840/*
841 * This comes from the MSC and we will now parse it. The caller needs
842 * to free the msgb.
843 */
844void bsc_nat_handle_mgcp(struct bsc_nat *nat, struct msgb *msg)
845{
846 struct msgb *resp;
847
848 if (!nat->mgcp_ipa) {
849 LOGP(DMGCP, LOGL_ERROR, "MGCP message not allowed on IPA.\n");
850 return;
851 }
852
853 if (msgb_l2len(msg) > sizeof(nat->mgcp_msg) - 1) {
854 LOGP(DMGCP, LOGL_ERROR, "MGCP msg too big for handling.\n");
855 return;
856 }
857
858 memcpy(nat->mgcp_msg, msg->l2h, msgb_l2len(msg));
859 nat->mgcp_length = msgb_l2len(msg);
860 nat->mgcp_msg[nat->mgcp_length] = '\0';
861
862 /* now handle the message */
863 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
864
865 /* we do have a direct answer... e.g. AUEP */
866 if (resp)
867 mgcp_queue_for_call_agent(nat, resp);
868
869 return;
870}
871
Pablo Neira Ayuso04d24cd2011-05-06 12:11:23 +0200872static int mgcp_do_read(struct osmo_fd *fd)
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200873{
874 struct bsc_nat *nat;
875 struct msgb *msg, *resp;
876 int rc;
877
Holger Hans Peter Freyther0d0aaa62010-04-04 18:09:10 +0200878 nat = fd->data;
879
880 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
881 if (rc <= 0) {
882 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
883 return -1;
884 }
885
886 nat->mgcp_msg[rc] = '\0';
887 nat->mgcp_length = rc;
888
889 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200890 if (!msg) {
891 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
892 return -1;
893 }
894
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200895 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther0d0aaa62010-04-04 18:09:10 +0200896 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200897 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
898 msgb_free(msg);
899
900 /* we do have a direct answer... e.g. AUEP */
Holger Hans Peter Freytherdceba0f2012-11-05 13:52:53 +0100901 if (resp)
902 mgcp_queue_for_call_agent(nat, resp);
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200903
904 return 0;
905}
906
Pablo Neira Ayuso04d24cd2011-05-06 12:11:23 +0200907static int mgcp_do_write(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200908{
909 int rc;
910
911 rc = write(bfd->fd, msg->data, msg->len);
912
913 if (rc != msg->len) {
914 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
915 return -1;
916 }
917
918 return rc;
919}
920
Holger Hans Peter Freythercd8bea92012-11-05 14:54:56 +0100921static int init_mgcp_socket(struct bsc_nat *nat, struct mgcp_config *cfg)
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200922{
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200923 struct sockaddr_in addr;
Holger Hans Peter Freythercd8bea92012-11-05 14:54:56 +0100924 int on;
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200925
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800926 cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
927 if (cfg->gw_fd.bfd.fd < 0) {
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200928 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
929 return -1;
930 }
931
932 on = 1;
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800933 setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200934
935 addr.sin_family = AF_INET;
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800936 addr.sin_port = htons(cfg->source_port);
937 inet_aton(cfg->source_addr, &addr.sin_addr);
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200938
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800939 if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther39b520b2011-03-29 17:12:07 +0200940 LOGP(DMGCP, LOGL_ERROR, "Failed to bind on %s:%d errno: %d\n",
941 cfg->source_addr, cfg->source_port, errno);
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800942 close(cfg->gw_fd.bfd.fd);
943 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200944 return -1;
945 }
946
947 addr.sin_port = htons(2727);
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800948 inet_aton(cfg->call_agent_addr, &addr.sin_addr);
949 if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200950 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800951 cfg->call_agent_addr, errno);
952 close(cfg->gw_fd.bfd.fd);
953 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200954 return -1;
955 }
956
Pablo Neira Ayusoda088502011-05-06 12:09:47 +0200957 osmo_wqueue_init(&cfg->gw_fd, 10);
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800958 cfg->gw_fd.bfd.when = BSC_FD_READ;
959 cfg->gw_fd.bfd.data = nat;
960 cfg->gw_fd.read_cb = mgcp_do_read;
961 cfg->gw_fd.write_cb = mgcp_do_write;
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200962
Pablo Neira Ayuso04d24cd2011-05-06 12:11:23 +0200963 if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) {
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200964 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800965 close(cfg->gw_fd.bfd.fd);
966 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +0200967 return -1;
968 }
969
Holger Hans Peter Freythercd8bea92012-11-05 14:54:56 +0100970 return 0;
971}
972
973int bsc_mgcp_nat_init(struct bsc_nat *nat)
974{
975 struct mgcp_config *cfg = nat->mgcp_cfg;
976
977 if (!cfg->call_agent_addr) {
978 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
979 return -1;
980 }
981
982 if (cfg->bts_ip) {
983 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
984 return -1;
985 }
986
987 /* initialize the MGCP socket */
988 if (!nat->mgcp_ipa) {
989 int rc = init_mgcp_socket(nat, cfg);
990 if (rc != 0)
991 return rc;
992 }
993
994
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +0200995 /* some more MGCP config handling */
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +0800996 cfg->data = nat;
997 cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +0100998 cfg->trunk.force_realloc = 1;
Holger Hans Peter Freyther774d1d32010-08-05 10:08:36 +0000999
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +08001000 if (cfg->bts_ip)
1001 talloc_free(cfg->bts_ip);
1002 cfg->bts_ip = "";
Holger Hans Peter Freyther774d1d32010-08-05 10:08:36 +00001003
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +02001004 nat->bsc_endpoints = talloc_zero_array(nat,
1005 struct bsc_endpoint,
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +01001006 cfg->trunk.number_endpoints + 1);
Holger Hans Peter Freyther0d44a392010-09-19 04:34:04 +08001007 if (!nat->bsc_endpoints) {
1008 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n");
Holger Hans Peter Freyther90d38172010-09-18 21:13:54 +08001009 close(cfg->gw_fd.bfd.fd);
1010 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther0d44a392010-09-19 04:34:04 +08001011 return -1;
1012 }
Holger Hans Peter Freyther8ddcc152010-04-01 08:21:33 +02001013
Holger Hans Peter Freytherf8fd1062010-09-19 04:36:07 +08001014 if (mgcp_reset_transcoder(cfg) < 0) {
1015 LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n");
1016 talloc_free(nat->bsc_endpoints);
1017 nat->bsc_endpoints = NULL;
1018 close(cfg->gw_fd.bfd.fd);
1019 cfg->gw_fd.bfd.fd = -1;
1020 return -1;
1021 }
1022
Holger Hans Peter Freyther3468be32010-03-31 13:02:22 +02001023 return 0;
1024}
Holger Hans Peter Freytherc0ff1d02010-04-05 23:09:27 +02001025
1026void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
1027{
Holger Hans Peter Freyther3357a7b2010-06-17 18:29:42 +08001028 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freytherc0ff1d02010-04-05 23:09:27 +02001029 int i;
Holger Hans Peter Freyther3357a7b2010-06-17 18:29:42 +08001030
1031 if (bsc->cfg)
1032 ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
1033
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +01001034 for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
Holger Hans Peter Freytherc0ff1d02010-04-05 23:09:27 +02001035 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
1036
1037 if (bsc_endp->bsc != bsc)
1038 continue;
1039
Holger Hans Peter Freyther3357a7b2010-06-17 18:29:42 +08001040 if (ctr)
1041 rate_ctr_inc(ctr);
1042
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +08001043 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freytherab56ce12011-02-28 00:56:17 +01001044 mgcp_free_endp(&bsc->nat->mgcp_cfg->trunk.endpoints[i]);
Holger Hans Peter Freytherc0ff1d02010-04-05 23:09:27 +02001045 }
1046}