blob: be2666de3345ae09f6b9f6342a23d403ad2b137f [file] [log] [blame]
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +08001/*
2 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +08009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080018 *
19 */
20
21#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080022#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080024#include <openbsc/debug.h>
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +020025#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020026#include <openbsc/mgcp.h>
27#include <openbsc/mgcp_internal.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080028
Harald Welted5db12c2010-08-03 15:11:51 +020029#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080030
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010031#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010032#include <osmocom/gsm/gsm0808.h>
33#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020034
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080035#include <netinet/in.h>
36#include <arpa/inet.h>
37
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +020038#include <errno.h>
39#include <unistd.h>
40
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010041int bsc_mgcp_nr_multiplexes(int max_endpoints)
42{
43 int div = max_endpoints / 32;
44
45 if ((max_endpoints % 32) != 0)
46 div += 1;
47
48 return div;
49}
50
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010051static int bsc_init_endps_if_needed(struct bsc_connection *con)
52{
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010053 int multiplexes;
54
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010055 /* we have done that */
56 if (con->_endpoint_status)
57 return 0;
58
59 /* we have no config... */
60 if (!con->cfg)
61 return -1;
62
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010063 multiplexes = bsc_mgcp_nr_multiplexes(con->cfg->max_endpoints);
64 con->number_multiplexes = multiplexes;
65 con->max_endpoints = con->cfg->max_endpoints;
66 con->_endpoint_status = talloc_zero_array(con, char, 32 * multiplexes + 1);
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010067 return con->_endpoint_status == NULL;
68}
69
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080070static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
71{
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +010072 int multiplex;
73 int timeslot;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010074 const int number_endpoints = bsc->max_endpoints;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080075 int i;
76
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +010077 mgcp_endpoint_to_timeslot(bsc->last_endpoint, &multiplex, &timeslot);
78 timeslot += 1;
79
80 for (i = 0; i < number_endpoints; ++i) {
81 int endpoint;
82
83 /* Wrap around timeslots */
84 if (timeslot == 0)
85 timeslot = 1;
86
87 if (timeslot == 0x1f) {
88 timeslot = 1;
89 multiplex += 1;
90 }
91
92 /* Wrap around the multiplex */
93 if (multiplex >= bsc->number_multiplexes)
94 multiplex = 0;
95
96 endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080097
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +010098 /* Now check if we are allowed to assign this one */
99 if (endpoint >= bsc->max_endpoints) {
100 multiplex = 0;
101 timeslot = 1;
102 endpoint = mgcp_timeslot_to_endpoint(multiplex, timeslot);
103 }
104
105
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100106 if (bsc->_endpoint_status[endpoint] == 0) {
107 bsc->_endpoint_status[endpoint] = 1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800108 con->bsc_endp = endpoint;
109 bsc->last_endpoint = endpoint;
110 return 0;
111 }
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100112
113 timeslot += 1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800114 }
115
116 return -1;
117}
118
119static uint16_t create_cic(int endpoint)
120{
121 int timeslot, multiplex;
122
123 mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
124 return (multiplex << 5) | (timeslot & 0x1f);
125}
126
127int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800128{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800129 struct sccp_connections *mcon;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800130 struct tlv_parsed tp;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800131 uint16_t cic;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800132 uint8_t timeslot;
133 uint8_t multiplex;
Holger Hans Peter Freyther14069772010-11-04 17:14:41 +0100134 unsigned int endp;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800135
136 if (!msg->l3h) {
137 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
138 return -1;
139 }
140
141 if (msgb_l3len(msg) < 3) {
142 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
143 return -1;
144 }
145
146 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
147 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
148 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
149 return -1;
150 }
151
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800152 cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800153 timeslot = cic & 0x1f;
154 multiplex = (cic & ~0x1f) >> 5;
155
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800156
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800157 endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800158
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100159 if (endp >= con->bsc->nat->mgcp_cfg->trunk.number_endpoints) {
Holger Hans Peter Freyther14069772010-11-04 17:14:41 +0100160 LOGP(DNAT, LOGL_ERROR,
161 "MSC attempted to assign bad endpoint 0x%x\n",
162 endp);
163 return -1;
164 }
165
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800166 /* find stale connections using that endpoint */
167 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800168 if (mcon->msc_endp == endp) {
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800169 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800170 "Endpoint %d was assigned to 0x%x and now 0x%x\n",
171 endp,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800172 sccp_src_ref_to_int(&mcon->patched_ref),
173 sccp_src_ref_to_int(&con->patched_ref));
174 bsc_mgcp_dlcx(mcon);
175 }
176 }
177
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800178 con->msc_endp = endp;
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100179 if (bsc_init_endps_if_needed(con->bsc) != 0)
180 return -1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800181 if (bsc_assign_endpoint(con->bsc, con) != 0)
182 return -1;
183
184 /*
185 * now patch the message for the new CIC...
186 * still assumed to be one multiplex only
187 */
188 cic = htons(create_cic(con->bsc_endp));
189 memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
190 &cic, sizeof(cic));
191
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800192 return 0;
193}
194
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800195static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200196{
197 if (nat->bsc_endpoints[i].transaction_id) {
198 talloc_free(nat->bsc_endpoints[i].transaction_id);
199 nat->bsc_endpoints[i].transaction_id = NULL;
200 }
201
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800202 nat->bsc_endpoints[i].transaction_state = 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200203 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200204}
205
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200206void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
207{
208 int i;
209
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100210 for (i = 1; i < nat->mgcp_cfg->trunk.number_endpoints; ++i){
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200211 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100212 mgcp_free_endp(&nat->mgcp_cfg->trunk.endpoints[i]);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800213 }
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200214}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200215
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800216/* send a MDCX where we do not want a response */
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800217static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800218{
219 char buf[2096];
220 int len;
221
222 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800223 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800224 "Z: noanswer\r\n"
225 "\r\n"
226 "c=IN IP4 %s\r\n"
227 "m=audio %d RTP/AVP 255\r\n",
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800228 port,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800229 bsc->nat->mgcp_cfg->source_addr,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800230 endp->bts_end.local_port);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800231 if (len < 0) {
232 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
233 return;
234 }
Holger Hans Peter Freytherd38aa452010-08-30 11:58:49 +0800235
236 #warning "The MDCX is not send to the BSC. It should"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800237}
238
239static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
240{
241 char buf[2096];
242 int len;
243
244 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherbf812fa2010-08-30 12:01:36 +0800245 "DLCX 26 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800246 "Z: noanswer\r\n", endpoint);
247 if (len < 0) {
248 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
249 return;
250 }
251
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800252 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800253}
254
255void bsc_mgcp_init(struct sccp_connections *con)
256{
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800257 con->msc_endp = -1;
258 con->bsc_endp = -1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800259}
260
261void bsc_mgcp_dlcx(struct sccp_connections *con)
262{
263 /* send a DLCX down the stream */
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100264 if (con->bsc_endp != -1 && con->bsc->_endpoint_status) {
265 if (con->bsc->_endpoint_status[con->bsc_endp] != 1)
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800266 LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100267 con->bsc->_endpoint_status[con->bsc_endp] = 0;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800268 bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
269 bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800270 }
271
272 bsc_mgcp_init(con);
273}
274
275
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800276struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200277{
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800278 struct sccp_connections *con = NULL;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200279 struct sccp_connections *sccp;
280
281 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800282 if (sccp->msc_endp == -1)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200283 continue;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800284 if (sccp->msc_endp != endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200285 continue;
286
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800287 con = sccp;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200288 }
289
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800290 if (con)
291 return con;
Holger Hans Peter Freythereb52e892010-04-18 02:14:45 +0800292
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200293 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
294 return NULL;
295}
296
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100297int 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 +0200298{
299 struct bsc_nat *nat;
300 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800301 struct sccp_connections *sccp;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200302 struct mgcp_endpoint *mgcp_endp;
303 struct msgb *bsc_msg;
304
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100305 nat = tcfg->cfg->data;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200306 bsc_endp = &nat->bsc_endpoints[endpoint];
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100307 mgcp_endp = &nat->mgcp_cfg->trunk.endpoints[endpoint];
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200308
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800309 if (bsc_endp->transaction_id) {
310 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
311 endpoint, bsc_endp->transaction_id);
312 talloc_free(bsc_endp->transaction_id);
313 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800314 bsc_endp->transaction_state = 0;
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800315 }
316 bsc_endp->bsc = NULL;
317
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800318 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200319
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800320 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800321 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 +0200322
323 switch (state) {
324 case MGCP_ENDP_CRCX:
325 return MGCP_POLICY_REJECT;
326 break;
327 case MGCP_ENDP_DLCX:
328 return MGCP_POLICY_CONT;
329 break;
330 case MGCP_ENDP_MDCX:
331 return MGCP_POLICY_CONT;
332 break;
333 default:
334 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
335 return MGCP_POLICY_CONT;
336 break;
337 }
338 }
339
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200340 /* we need to generate a new and patched message */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800341 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800342 nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200343 if (!bsc_msg) {
344 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
345 return MGCP_POLICY_CONT;
346 }
347
348
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200349 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800350 bsc_endp->transaction_state = state;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800351 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200352
353 /* we need to update some bits */
354 if (state == MGCP_ENDP_CRCX) {
355 struct sockaddr_in sock;
356 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800357 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200358 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
359 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200360 } else {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800361 mgcp_endp->bts_end.addr = sock.sin_addr;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200362 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200363
Holger Hans Peter Freythere66cac32010-08-05 01:50:44 +0800364 /* send the message and a fake MDCX to force sending of a dummy packet */
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100365 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800366 bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800367 return MGCP_POLICY_DEFER;
368 } else if (state == MGCP_ENDP_DLCX) {
369 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freyther3a347f02010-05-01 10:31:53 +0800370 msgb_free(bsc_msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800371 bsc_mgcp_dlcx(sccp);
372 return MGCP_POLICY_CONT;
373 } else {
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100374 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800375 return MGCP_POLICY_DEFER;
376 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200377}
378
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200379/*
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800380 * We do have a failure, free data downstream..
381 */
382static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
383 struct bsc_connection *bsc)
384{
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800385 LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
386 ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800387
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800388 /* if a CRCX failed... send a DLCX down the stream */
389 if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
390 struct sccp_connections *con;
391 con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
392 if (!con) {
393 LOGP(DMGCP, LOGL_ERROR,
394 "No SCCP connection for endp 0x%x\n",
395 ENDPOINT_NUMBER(endp));
396 } else {
397 if (con->bsc == bsc) {
Holger Hans Peter Freyther8574dcf2010-08-29 22:39:07 +0800398 bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800399 } else {
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800400 LOGP(DMGCP, LOGL_ERROR,
401 "Endpoint belongs to a different BSC\n");
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800402 }
403 }
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800404 }
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800405
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800406 bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
407 mgcp_free_endp(endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800408}
409
410/*
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200411 * We have received a msg from the BSC. We will see if we know
412 * this transaction and if it belongs to the BSC. Then we will
413 * need to patch the content to point to the local network and we
414 * need to update the I: that was assigned by the BSS.
415 */
416void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
417{
418 struct msgb *output;
419 struct bsc_endpoint *bsc_endp = NULL;
420 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200421 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200422 char transaction_id[60];
423
424 /* Some assumption that our buffer is big enough.. and null terminate */
425 if (msgb_l2len(msg) > 2000) {
426 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
427 return;
428 }
429
430 msg->l2h[msgb_l2len(msg)] = '\0';
431
432 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
433 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
434 return;
435 }
436
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100437 for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200438 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
439 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200440 /* no one listening? a bug? */
441 if (!bsc->nat->bsc_endpoints[i].transaction_id)
442 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200443 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
444 continue;
445
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100446 endp = &bsc->nat->mgcp_cfg->trunk.endpoints[i];
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200447 bsc_endp = &bsc->nat->bsc_endpoints[i];
448 break;
449 }
450
451 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200452 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
453 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200454 return;
455 }
456
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800457 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800458 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800459 free_chan_downstream(endp, bsc_endp, bsc);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800460 return;
461 }
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200462
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200463 /* free some stuff */
464 talloc_free(bsc_endp->transaction_id);
465 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800466 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200467
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200468 /*
469 * rewrite the information. In case the endpoint was deleted
470 * there should be nothing for us to rewrite so putting endp->rtp_port
471 * with the value of 0 should be no problem.
472 */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800473 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800474 bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200475
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200476 if (!output) {
477 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
478 return;
479 }
480
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800481 if (write_queue_enqueue(&bsc->nat->mgcp_cfg->gw_fd, output) != 0) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200482 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
483 msgb_free(output);
484 }
485}
486
487int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
488{
489 /* we want to parse two strings */
490 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
491}
492
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800493uint32_t bsc_mgcp_extract_ci(const char *str)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200494{
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800495 unsigned int ci;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200496 char *res = strstr(str, "I: ");
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800497 if (!res) {
498 LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200499 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800500 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200501
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800502 if (sscanf(res, "I: %u", &ci) != 1) {
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800503 LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200504 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800505 }
506
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200507 return ci;
508}
509
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800510static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
511 int endp, int len, int cr)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200512{
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800513 int slen;
514 int ret;
515 char buf[40];
516
517 buf[0] = buf[39] = '\0';
518 ret = sscanf(tok, "%*s %s", buf);
519
520 slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
521 op, buf, endp, cr ? "\r\n" : "\n");
522 output->l3h = msgb_put(output, slen);
523}
524
525/* we need to replace some strings... */
526struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
527{
528 static const char *crcx_str = "CRCX ";
529 static const char *dlcx_str = "DLCX ";
530 static const char *mdcx_str = "MDCX ";
531
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200532 static const char *ip_str = "c=IN IP4 ";
533 static const char *aud_str = "m=audio ";
534
535 char buf[128];
536 char *running, *token;
537 struct msgb *output;
538
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200539 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200540 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
541 return NULL;
542 }
543
544 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
545 if (!output) {
546 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
547 return NULL;
548 }
549
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200550 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200551 output->l2h = output->data;
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800552 output->l3h = output->l2h;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200553 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200554 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200555 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200556
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800557 if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
558 patch_mgcp(output, "CRCX", token, endpoint, len, cr);
559 } else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
560 patch_mgcp(output, "DLCX", token, endpoint, len, cr);
561 } else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
562 patch_mgcp(output, "MDCX", token, endpoint, len, cr);
563 } else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200564 output->l3h = msgb_put(output, strlen(ip_str));
565 memcpy(output->l3h, ip_str, strlen(ip_str));
566 output->l3h = msgb_put(output, strlen(ip));
567 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200568
569 if (cr) {
570 output->l3h = msgb_put(output, 2);
571 output->l3h[0] = '\r';
572 output->l3h[1] = '\n';
573 } else {
574 output->l3h = msgb_put(output, 1);
575 output->l3h[0] = '\n';
576 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200577 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
578 int payload;
579 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
580 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
581 msgb_free(output);
582 return NULL;
583 }
584
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200585 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
586 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200587 buf[sizeof(buf)-1] = '\0';
588
589 output->l3h = msgb_put(output, strlen(buf));
590 memcpy(output->l3h, buf, strlen(buf));
591 } else {
592 output->l3h = msgb_put(output, len + 1);
593 memcpy(output->l3h, token, len);
594 output->l3h[len] = '\n';
595 }
596 }
597
598 return output;
599}
600
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200601static int mgcp_do_read(struct osmo_fd *fd)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200602{
603 struct bsc_nat *nat;
604 struct msgb *msg, *resp;
605 int rc;
606
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200607 nat = fd->data;
608
609 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
610 if (rc <= 0) {
611 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
612 return -1;
613 }
614
615 nat->mgcp_msg[rc] = '\0';
616 nat->mgcp_length = rc;
617
618 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200619 if (!msg) {
620 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
621 return -1;
622 }
623
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200624 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200625 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200626 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
627 msgb_free(msg);
628
629 /* we do have a direct answer... e.g. AUEP */
630 if (resp) {
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800631 if (write_queue_enqueue(&nat->mgcp_cfg->gw_fd, resp) != 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200632 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
633 msgb_free(resp);
634 }
635 }
636
637 return 0;
638}
639
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200640static int mgcp_do_write(struct osmo_fd *bfd, struct msgb *msg)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200641{
642 int rc;
643
644 rc = write(bfd->fd, msg->data, msg->len);
645
646 if (rc != msg->len) {
647 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
648 return -1;
649 }
650
651 return rc;
652}
653
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800654int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200655{
656 int on;
657 struct sockaddr_in addr;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800658 struct mgcp_config *cfg = nat->mgcp_cfg;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200659
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800660 if (!cfg->call_agent_addr) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200661 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
662 return -1;
663 }
664
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800665 if (cfg->bts_ip) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200666 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
667 return -1;
668 }
669
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800670 cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
671 if (cfg->gw_fd.bfd.fd < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200672 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
673 return -1;
674 }
675
676 on = 1;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800677 setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200678
679 addr.sin_family = AF_INET;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800680 addr.sin_port = htons(cfg->source_port);
681 inet_aton(cfg->source_addr, &addr.sin_addr);
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200682
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800683 if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freytherd000c272011-03-29 17:12:07 +0200684 LOGP(DMGCP, LOGL_ERROR, "Failed to bind on %s:%d errno: %d\n",
685 cfg->source_addr, cfg->source_port, errno);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800686 close(cfg->gw_fd.bfd.fd);
687 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200688 return -1;
689 }
690
691 addr.sin_port = htons(2727);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800692 inet_aton(cfg->call_agent_addr, &addr.sin_addr);
693 if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200694 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800695 cfg->call_agent_addr, errno);
696 close(cfg->gw_fd.bfd.fd);
697 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200698 return -1;
699 }
700
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800701 write_queue_init(&cfg->gw_fd, 10);
702 cfg->gw_fd.bfd.when = BSC_FD_READ;
703 cfg->gw_fd.bfd.data = nat;
704 cfg->gw_fd.read_cb = mgcp_do_read;
705 cfg->gw_fd.write_cb = mgcp_do_write;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200706
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200707 if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200708 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800709 close(cfg->gw_fd.bfd.fd);
710 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200711 return -1;
712 }
713
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200714 /* some more MGCP config handling */
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800715 cfg->data = nat;
716 cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100717 cfg->trunk.force_realloc = 1;
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000718
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800719 if (cfg->bts_ip)
720 talloc_free(cfg->bts_ip);
721 cfg->bts_ip = "";
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000722
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200723 nat->bsc_endpoints = talloc_zero_array(nat,
724 struct bsc_endpoint,
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100725 cfg->trunk.number_endpoints + 1);
Holger Hans Peter Freyther3c792142010-09-19 04:34:04 +0800726 if (!nat->bsc_endpoints) {
727 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n");
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800728 close(cfg->gw_fd.bfd.fd);
729 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther3c792142010-09-19 04:34:04 +0800730 return -1;
731 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200732
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +0800733 if (mgcp_reset_transcoder(cfg) < 0) {
734 LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n");
735 talloc_free(nat->bsc_endpoints);
736 nat->bsc_endpoints = NULL;
737 close(cfg->gw_fd.bfd.fd);
738 cfg->gw_fd.bfd.fd = -1;
739 return -1;
740 }
741
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200742 return 0;
743}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200744
745void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
746{
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800747 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200748 int i;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800749
750 if (bsc->cfg)
751 ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
752
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100753 for (i = 1; i < bsc->nat->mgcp_cfg->trunk.number_endpoints; ++i) {
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200754 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
755
756 if (bsc_endp->bsc != bsc)
757 continue;
758
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800759 if (ctr)
760 rate_ctr_inc(ctr);
761
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800762 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100763 mgcp_free_endp(&bsc->nat->mgcp_cfg->trunk.endpoints[i]);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200764 }
765}