blob: 480a8f6d1a1c835982834c640aa57db4616be3c4 [file] [log] [blame]
Holger Hans Peter Freyther07fc0972012-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 Freyther465313e2010-06-15 18:49:53 +080025/*
Holger Hans Peter Freyther07fc0972012-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 Freyther465313e2010-06-15 18:49:53 +080028 * All Rights Reserved
29 *
30 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-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 Freyther465313e2010-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 Welte9af6ddf2011-01-01 15:25:50 +010038 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080039 *
Harald Welte9af6ddf2011-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 Freyther465313e2010-06-15 18:49:53 +080042 *
43 */
44
45#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080046#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080047#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080048#include <openbsc/debug.h>
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +020049#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020050#include <openbsc/mgcp.h>
51#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080052
Harald Welted5db12c2010-08-03 15:11:51 +020053#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080054
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010055#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010056#include <osmocom/gsm/gsm0808.h>
57#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020058
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080059#include <netinet/in.h>
60#include <arpa/inet.h>
61
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +020062#include <errno.h>
63#include <unistd.h>
64
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +010065static void send_direct(struct bsc_nat *nat, struct msgb *output)
Holger Hans Peter Freyther77956aa2012-11-05 13:52:53 +010066{
67 if (osmo_wqueue_enqueue(&nat->mgcp_cfg->gw_fd, output) != 0) {
68 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
69 msgb_free(output);
70 }
71}
72
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +010073static void mgcp_queue_for_call_agent(struct bsc_nat *nat, struct msgb *output)
74{
75 if (nat->mgcp_ipa)
76 bsc_nat_send_mgcp_to_msc(nat, output);
77 else
78 send_direct(nat, output);
79}
80
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010081int bsc_mgcp_nr_multiplexes(int max_endpoints)
82{
83 int div = max_endpoints / 32;
84
85 if ((max_endpoints % 32) != 0)
86 div += 1;
87
88 return div;
89}
90
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010091static int bsc_init_endps_if_needed(struct bsc_connection *con)
92{
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010093 int multiplexes;
94
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010095 /* we have done that */
96 if (con->_endpoint_status)
97 return 0;
98
99 /* we have no config... */
100 if (!con->cfg)
101 return -1;
102
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100103 multiplexes = bsc_mgcp_nr_multiplexes(con->cfg->max_endpoints);
104 con->number_multiplexes = multiplexes;
105 con->max_endpoints = con->cfg->max_endpoints;
106 con->_endpoint_status = talloc_zero_array(con, char, 32 * multiplexes + 1);
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100107 return con->_endpoint_status == NULL;
108}
109
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800110static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
111{
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100112 int multiplex;
113 int timeslot;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100114 const int number_endpoints = bsc->max_endpoints;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800115 int i;
116
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100117 mgcp_endpoint_to_timeslot(bsc->last_endpoint, &multiplex, &timeslot);
118 timeslot += 1;
119
120 for (i = 0; i < number_endpoints; ++i) {
121 int endpoint;
122
123 /* Wrap around timeslots */
124 if (timeslot == 0)
125 timeslot = 1;
126
127 if (timeslot == 0x1f) {
128 timeslot = 1;
129 multiplex += 1;
130 }
131
132 /* Wrap around the multiplex */
133 if (multiplex >= bsc->number_multiplexes)
134 multiplex = 0;
135
136 endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800137
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100138 /* Now check if we are allowed to assign this one */
139 if (endpoint >= bsc->max_endpoints) {
140 multiplex = 0;
141 timeslot = 1;
142 endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
143 }
144
145
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100146 if (bsc->_endpoint_status[endpoint] == 0) {
147 bsc->_endpoint_status[endpoint] = 1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800148 con->bsc_endp = endpoint;
149 bsc->last_endpoint = endpoint;
150 return 0;
151 }
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100152
153 timeslot += 1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800154 }
155
156 return -1;
157}
158
159static uint16_t create_cic(int endpoint)
160{
161 int timeslot, multiplex;
162
163 mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
164 return (multiplex << 5) | (timeslot & 0x1f);
165}
166
167int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800168{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800169 struct sccp_connections *mcon;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800170 struct tlv_parsed tp;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800171 uint16_t cic;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800172 uint8_t timeslot;
173 uint8_t multiplex;
Holger Hans Peter Freyther14069772010-11-04 17:14:41 +0100174 unsigned int endp;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800175
176 if (!msg->l3h) {
177 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
178 return -1;
179 }
180
181 if (msgb_l3len(msg) < 3) {
182 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
183 return -1;
184 }
185
186 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
187 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
188 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
189 return -1;
190 }
191
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800192 cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800193 timeslot = cic & 0x1f;
194 multiplex = (cic & ~0x1f) >> 5;
195
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800196
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800197 endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800198
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100199 if (endp >= con->bsc->nat->mgcp_cfg->trunk.number_endpoints) {
Holger Hans Peter Freyther14069772010-11-04 17:14:41 +0100200 LOGP(DNAT, LOGL_ERROR,
201 "MSC attempted to assign bad endpoint 0x%x\n",
202 endp);
203 return -1;
204 }
205
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800206 /* find stale connections using that endpoint */
207 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800208 if (mcon->msc_endp == endp) {
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800209 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800210 "Endpoint %d was assigned to 0x%x and now 0x%x\n",
211 endp,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800212 sccp_src_ref_to_int(&mcon->patched_ref),
213 sccp_src_ref_to_int(&con->patched_ref));
214 bsc_mgcp_dlcx(mcon);
215 }
216 }
217
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800218 con->msc_endp = endp;
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100219 if (bsc_init_endps_if_needed(con->bsc) != 0)
220 return -1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800221 if (bsc_assign_endpoint(con->bsc, con) != 0)
222 return -1;
223
224 /*
225 * now patch the message for the new CIC...
226 * still assumed to be one multiplex only
227 */
228 cic = htons(create_cic(con->bsc_endp));
229 memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
230 &cic, sizeof(cic));
231
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800232 return 0;
233}
234
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800235static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200236{
237 if (nat->bsc_endpoints[i].transaction_id) {
238 talloc_free(nat->bsc_endpoints[i].transaction_id);
239 nat->bsc_endpoints[i].transaction_id = NULL;
240 }
241
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800242 nat->bsc_endpoints[i].transaction_state = 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200243 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200244}
245
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200246void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
247{
248 int i;
249
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100250 for (i = 1; i < nat->mgcp_cfg->trunk.number_endpoints; ++i){
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200251 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100252 mgcp_free_endp(&nat->mgcp_cfg->trunk.endpoints[i]);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800253 }
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200254}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200255
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800256/* send a MDCX where we do not want a response */
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800257static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800258{
259 char buf[2096];
260 int len;
261
262 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800263 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800264 "Z: noanswer\r\n"
265 "\r\n"
266 "c=IN IP4 %s\r\n"
267 "m=audio %d RTP/AVP 255\r\n",
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800268 port,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800269 bsc->nat->mgcp_cfg->source_addr,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800270 endp->bts_end.local_port);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800271 if (len < 0) {
Holger Hans Peter Freyther74568912012-09-14 15:51:43 +0200272 LOGP(DMGCP, LOGL_ERROR, "snprintf for MDCX failed.\n");
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800273 return;
274 }
Holger Hans Peter Freytherd38aa452010-08-30 11:58:49 +0800275
Holger Hans Peter Freyther6de9d0b2012-10-30 16:32:19 +0100276 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800277}
278
279static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
280{
281 char buf[2096];
282 int len;
283
284 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherbf812fa2010-08-30 12:01:36 +0800285 "DLCX 26 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800286 "Z: noanswer\r\n", endpoint);
287 if (len < 0) {
288 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
289 return;
290 }
291
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800292 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800293}
294
295void bsc_mgcp_init(struct sccp_connections *con)
296{
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800297 con->msc_endp = -1;
298 con->bsc_endp = -1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800299}
300
301void bsc_mgcp_dlcx(struct sccp_connections *con)
302{
303 /* send a DLCX down the stream */
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100304 if (con->bsc_endp != -1 && con->bsc->_endpoint_status) {
305 if (con->bsc->_endpoint_status[con->bsc_endp] != 1)
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800306 LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100307 con->bsc->_endpoint_status[con->bsc_endp] = 0;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800308 bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
309 bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800310 }
311
312 bsc_mgcp_init(con);
313}
314
315
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800316struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200317{
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800318 struct sccp_connections *con = NULL;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200319 struct sccp_connections *sccp;
320
321 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800322 if (sccp->msc_endp == -1)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200323 continue;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800324 if (sccp->msc_endp != endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200325 continue;
326
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800327 con = sccp;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200328 }
329
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800330 if (con)
331 return con;
Holger Hans Peter Freythereb52e892010-04-18 02:14:45 +0800332
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200333 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
334 return NULL;
335}
336
Holger Hans Peter Freythercb3c2c92012-10-24 21:53:07 +0200337static int bsc_mgcp_policy_cb(struct mgcp_trunk_config *tcfg, int endpoint, int state, const char *transaction_id)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200338{
339 struct bsc_nat *nat;
340 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800341 struct sccp_connections *sccp;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200342 struct mgcp_endpoint *mgcp_endp;
343 struct msgb *bsc_msg;
344
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100345 nat = tcfg->cfg->data;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200346 bsc_endp = &nat->bsc_endpoints[endpoint];
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100347 mgcp_endp = &nat->mgcp_cfg->trunk.endpoints[endpoint];
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200348
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800349 if (bsc_endp->transaction_id) {
350 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
351 endpoint, bsc_endp->transaction_id);
352 talloc_free(bsc_endp->transaction_id);
353 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800354 bsc_endp->transaction_state = 0;
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800355 }
356 bsc_endp->bsc = NULL;
357
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800358 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200359
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800360 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800361 LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for change on endpoint: 0x%x state: %d\n", endpoint, state);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200362
363 switch (state) {
364 case MGCP_ENDP_CRCX:
365 return MGCP_POLICY_REJECT;
366 break;
367 case MGCP_ENDP_DLCX:
368 return MGCP_POLICY_CONT;
369 break;
370 case MGCP_ENDP_MDCX:
371 return MGCP_POLICY_CONT;
372 break;
373 default:
374 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
375 return MGCP_POLICY_CONT;
376 break;
377 }
378 }
379
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200380 /* we need to generate a new and patched message */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800381 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800382 nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200383 if (!bsc_msg) {
384 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
385 return MGCP_POLICY_CONT;
386 }
387
388
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200389 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800390 bsc_endp->transaction_state = state;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800391 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200392
393 /* we need to update some bits */
394 if (state == MGCP_ENDP_CRCX) {
395 struct sockaddr_in sock;
396 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800397 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200398 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
399 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200400 } else {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800401 mgcp_endp->bts_end.addr = sock.sin_addr;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200402 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200403
Holger Hans Peter Freythere66cac32010-08-05 01:50:44 +0800404 /* send the message and a fake MDCX to force sending of a dummy packet */
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100405 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800406 bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800407 return MGCP_POLICY_DEFER;
408 } else if (state == MGCP_ENDP_DLCX) {
409 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freyther3a347f02010-05-01 10:31:53 +0800410 msgb_free(bsc_msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800411 bsc_mgcp_dlcx(sccp);
412 return MGCP_POLICY_CONT;
413 } else {
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100414 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800415 return MGCP_POLICY_DEFER;
416 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200417}
418
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200419/*
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800420 * We do have a failure, free data downstream..
421 */
422static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
423 struct bsc_connection *bsc)
424{
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800425 LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
426 ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800427
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800428 /* if a CRCX failed... send a DLCX down the stream */
429 if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
430 struct sccp_connections *con;
431 con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
432 if (!con) {
433 LOGP(DMGCP, LOGL_ERROR,
434 "No SCCP connection for endp 0x%x\n",
435 ENDPOINT_NUMBER(endp));
436 } else {
437 if (con->bsc == bsc) {
Holger Hans Peter Freyther8574dcf2010-08-29 22:39:07 +0800438 bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800439 } else {
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800440 LOGP(DMGCP, LOGL_ERROR,
441 "Endpoint belongs to a different BSC\n");
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800442 }
443 }
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800444 }
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800445
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800446 bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
447 mgcp_free_endp(endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800448}
449
450/*
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200451 * We have received a msg from the BSC. We will see if we know
452 * this transaction and if it belongs to the BSC. Then we will
453 * need to patch the content to point to the local network and we
454 * need to update the I: that was assigned by the BSS.
455 */
456void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
457{
458 struct msgb *output;
459 struct bsc_endpoint *bsc_endp = NULL;
460 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200461 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200462 char transaction_id[60];
463
464 /* Some assumption that our buffer is big enough.. and null terminate */
465 if (msgb_l2len(msg) > 2000) {
466 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
467 return;
468 }
469
470 msg->l2h[msgb_l2len(msg)] = '\0';
471
472 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
473 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
474 return;
475 }
476
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100477 for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200478 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
479 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200480 /* no one listening? a bug? */
481 if (!bsc->nat->bsc_endpoints[i].transaction_id)
482 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200483 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
484 continue;
485
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100486 endp = &bsc->nat->mgcp_cfg->trunk.endpoints[i];
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200487 bsc_endp = &bsc->nat->bsc_endpoints[i];
488 break;
489 }
490
491 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200492 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
493 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200494 return;
495 }
496
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800497 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800498 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800499 free_chan_downstream(endp, bsc_endp, bsc);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800500 return;
501 }
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200502
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200503 /* free some stuff */
504 talloc_free(bsc_endp->transaction_id);
505 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800506 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200507
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200508 /*
509 * rewrite the information. In case the endpoint was deleted
510 * there should be nothing for us to rewrite so putting endp->rtp_port
511 * with the value of 0 should be no problem.
512 */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800513 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800514 bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200515
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200516 if (!output) {
517 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
518 return;
519 }
520
Holger Hans Peter Freyther77956aa2012-11-05 13:52:53 +0100521 mgcp_queue_for_call_agent(bsc->nat, output);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200522}
523
524int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
525{
526 /* we want to parse two strings */
527 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
528}
529
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800530uint32_t bsc_mgcp_extract_ci(const char *str)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200531{
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800532 unsigned int ci;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200533 char *res = strstr(str, "I: ");
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800534 if (!res) {
535 LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200536 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800537 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200538
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800539 if (sscanf(res, "I: %u", &ci) != 1) {
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800540 LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200541 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800542 }
543
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200544 return ci;
545}
546
Holger Hans Peter Freyther9592c452012-01-17 15:58:48 +0100547/**
548 * Create a new MGCPCommand based on the input and endpoint from a message
549 */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800550static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
551 int endp, int len, int cr)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200552{
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800553 int slen;
554 int ret;
555 char buf[40];
556
557 buf[0] = buf[39] = '\0';
558 ret = sscanf(tok, "%*s %s", buf);
Holger Hans Peter Freyther9592c452012-01-17 15:58:48 +0100559 if (ret != 1) {
560 LOGP(DMGCP, LOGL_ERROR,
561 "Failed to find Endpoint in: %s\n", tok);
562 return;
563 }
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800564
565 slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
566 op, buf, endp, cr ? "\r\n" : "\n");
567 output->l3h = msgb_put(output, slen);
568}
569
570/* we need to replace some strings... */
571struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
572{
Holger Hans Peter Freyther3dfe8a12012-11-07 11:36:58 +0100573 static const char crcx_str[] = "CRCX ";
574 static const char dlcx_str[] = "DLCX ";
575 static const char mdcx_str[] = "MDCX ";
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800576
Holger Hans Peter Freyther3dfe8a12012-11-07 11:36:58 +0100577 static const char ip_str[] = "c=IN IP4 ";
578 static const char aud_str[] = "m=audio ";
579 static const char fmt_str[] = "a=fmtp:";
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200580
581 char buf[128];
582 char *running, *token;
583 struct msgb *output;
584
Holger Hans Peter Freythere2f34d52012-11-06 13:16:26 +0100585 /* keep state to add the a=fmtp line */
586 int found_fmtp = 0;
587 int payload = -1;
588 int cr = 1;
589
590 if (length > 4096 - 256) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200591 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
592 return NULL;
593 }
594
595 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
596 if (!output) {
597 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
598 return NULL;
599 }
600
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200601 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200602 output->l2h = output->data;
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800603 output->l3h = output->l2h;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200604 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200605 int len = strlen(token);
Holger Hans Peter Freythere2f34d52012-11-06 13:16:26 +0100606 cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200607
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800608 if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
609 patch_mgcp(output, "CRCX", token, endpoint, len, cr);
610 } else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
611 patch_mgcp(output, "DLCX", token, endpoint, len, cr);
612 } else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
613 patch_mgcp(output, "MDCX", token, endpoint, len, cr);
614 } else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200615 output->l3h = msgb_put(output, strlen(ip_str));
616 memcpy(output->l3h, ip_str, strlen(ip_str));
617 output->l3h = msgb_put(output, strlen(ip));
618 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200619
620 if (cr) {
621 output->l3h = msgb_put(output, 2);
622 output->l3h[0] = '\r';
623 output->l3h[1] = '\n';
624 } else {
625 output->l3h = msgb_put(output, 1);
626 output->l3h[0] = '\n';
627 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200628 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200629 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
630 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
631 msgb_free(output);
632 return NULL;
633 }
634
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200635 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
636 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200637 buf[sizeof(buf)-1] = '\0';
638
639 output->l3h = msgb_put(output, strlen(buf));
640 memcpy(output->l3h, buf, strlen(buf));
Holger Hans Peter Freythere2f34d52012-11-06 13:16:26 +0100641 } else if (strncmp(fmt_str, token, (sizeof fmt_str) - 1) == 0) {
642 found_fmtp = 1;
643 goto copy;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200644 } else {
Holger Hans Peter Freythere2f34d52012-11-06 13:16:26 +0100645copy:
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200646 output->l3h = msgb_put(output, len + 1);
647 memcpy(output->l3h, token, len);
648 output->l3h[len] = '\n';
649 }
650 }
651
Holger Hans Peter Freythere2f34d52012-11-06 13:16:26 +0100652 /*
653 * the above code made sure that we have 128 bytes lefts. So we can
654 * safely append another line.
655 */
656 if (!found_fmtp && payload != -1) {
657 snprintf(buf, sizeof(buf) - 1, "a=fmtp:%d mode-set=2%s",
658 payload, cr ? "\r\n" : "\n");
659 buf[sizeof(buf) - 1] = '\0';
660 output->l3h = msgb_put(output, strlen(buf));
661 memcpy(output->l3h, buf, strlen(buf));
662 }
663
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200664 return output;
665}
666
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100667/*
668 * This comes from the MSC and we will now parse it. The caller needs
669 * to free the msgb.
670 */
671void bsc_nat_handle_mgcp(struct bsc_nat *nat, struct msgb *msg)
672{
673 struct msgb *resp;
674
675 if (!nat->mgcp_ipa) {
676 LOGP(DMGCP, LOGL_ERROR, "MGCP message not allowed on IPA.\n");
677 return;
678 }
679
680 if (msgb_l2len(msg) > sizeof(nat->mgcp_msg) - 1) {
681 LOGP(DMGCP, LOGL_ERROR, "MGCP msg too big for handling.\n");
682 return;
683 }
684
685 memcpy(nat->mgcp_msg, msg->l2h, msgb_l2len(msg));
686 nat->mgcp_length = msgb_l2len(msg);
687 nat->mgcp_msg[nat->mgcp_length] = '\0';
688
689 /* now handle the message */
690 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
691
692 /* we do have a direct answer... e.g. AUEP */
693 if (resp)
694 mgcp_queue_for_call_agent(nat, resp);
695
696 return;
697}
698
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200699static int mgcp_do_read(struct osmo_fd *fd)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200700{
701 struct bsc_nat *nat;
702 struct msgb *msg, *resp;
703 int rc;
704
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200705 nat = fd->data;
706
707 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
708 if (rc <= 0) {
709 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
710 return -1;
711 }
712
713 nat->mgcp_msg[rc] = '\0';
714 nat->mgcp_length = rc;
715
716 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200717 if (!msg) {
718 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
719 return -1;
720 }
721
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200722 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200723 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200724 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
725 msgb_free(msg);
726
727 /* we do have a direct answer... e.g. AUEP */
Holger Hans Peter Freyther77956aa2012-11-05 13:52:53 +0100728 if (resp)
729 mgcp_queue_for_call_agent(nat, resp);
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200730
731 return 0;
732}
733
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200734static int mgcp_do_write(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200735{
736 int rc;
737
738 rc = write(bfd->fd, msg->data, msg->len);
739
740 if (rc != msg->len) {
741 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
742 return -1;
743 }
744
745 return rc;
746}
747
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100748static int init_mgcp_socket(struct bsc_nat *nat, struct mgcp_config *cfg)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200749{
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200750 struct sockaddr_in addr;
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100751 int on;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200752
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800753 cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
754 if (cfg->gw_fd.bfd.fd < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200755 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
756 return -1;
757 }
758
759 on = 1;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800760 setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200761
762 addr.sin_family = AF_INET;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800763 addr.sin_port = htons(cfg->source_port);
764 inet_aton(cfg->source_addr, &addr.sin_addr);
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200765
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800766 if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freytherd000c272011-03-29 17:12:07 +0200767 LOGP(DMGCP, LOGL_ERROR, "Failed to bind on %s:%d errno: %d\n",
768 cfg->source_addr, cfg->source_port, errno);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800769 close(cfg->gw_fd.bfd.fd);
770 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200771 return -1;
772 }
773
774 addr.sin_port = htons(2727);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800775 inet_aton(cfg->call_agent_addr, &addr.sin_addr);
776 if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200777 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800778 cfg->call_agent_addr, errno);
779 close(cfg->gw_fd.bfd.fd);
780 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200781 return -1;
782 }
783
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200784 osmo_wqueue_init(&cfg->gw_fd, 10);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800785 cfg->gw_fd.bfd.when = BSC_FD_READ;
786 cfg->gw_fd.bfd.data = nat;
787 cfg->gw_fd.read_cb = mgcp_do_read;
788 cfg->gw_fd.write_cb = mgcp_do_write;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200789
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200790 if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200791 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800792 close(cfg->gw_fd.bfd.fd);
793 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200794 return -1;
795 }
796
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +0100797 return 0;
798}
799
800int bsc_mgcp_nat_init(struct bsc_nat *nat)
801{
802 struct mgcp_config *cfg = nat->mgcp_cfg;
803
804 if (!cfg->call_agent_addr) {
805 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
806 return -1;
807 }
808
809 if (cfg->bts_ip) {
810 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
811 return -1;
812 }
813
814 /* initialize the MGCP socket */
815 if (!nat->mgcp_ipa) {
816 int rc = init_mgcp_socket(nat, cfg);
817 if (rc != 0)
818 return rc;
819 }
820
821
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200822 /* some more MGCP config handling */
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800823 cfg->data = nat;
824 cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100825 cfg->trunk.force_realloc = 1;
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000826
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800827 if (cfg->bts_ip)
828 talloc_free(cfg->bts_ip);
829 cfg->bts_ip = "";
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000830
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200831 nat->bsc_endpoints = talloc_zero_array(nat,
832 struct bsc_endpoint,
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100833 cfg->trunk.number_endpoints + 1);
Holger Hans Peter Freyther3c792142010-09-19 04:34:04 +0800834 if (!nat->bsc_endpoints) {
835 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n");
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800836 close(cfg->gw_fd.bfd.fd);
837 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther3c792142010-09-19 04:34:04 +0800838 return -1;
839 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200840
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +0800841 if (mgcp_reset_transcoder(cfg) < 0) {
842 LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n");
843 talloc_free(nat->bsc_endpoints);
844 nat->bsc_endpoints = NULL;
845 close(cfg->gw_fd.bfd.fd);
846 cfg->gw_fd.bfd.fd = -1;
847 return -1;
848 }
849
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200850 return 0;
851}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200852
853void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
854{
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800855 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200856 int i;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800857
858 if (bsc->cfg)
859 ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
860
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100861 for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200862 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
863
864 if (bsc_endp->bsc != bsc)
865 continue;
866
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800867 if (ctr)
868 rate_ctr_inc(ctr);
869
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800870 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100871 mgcp_free_endp(&bsc->nat->mgcp_cfg->trunk.endpoints[i]);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200872 }
873}