blob: 79bd2a5d63b474adab6e75a5a7b3abbe7374c541 [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
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <openbsc/bsc_nat.h>
23#include <openbsc/gsm_data.h>
24#include <openbsc/bssap.h>
25#include <openbsc/debug.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
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080029#include <sccp/sccp.h>
30
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020031#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080032#include <osmocore/gsm0808.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020033
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080034#include <netinet/in.h>
35#include <arpa/inet.h>
36
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +020037#include <errno.h>
38#include <unistd.h>
39
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080040int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg)
41{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080042 struct sccp_connections *mcon;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080043 struct tlv_parsed tp;
44 u_int16_t cic;
45 u_int8_t timeslot;
46 u_int8_t multiplex;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080047 int combined;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080048
49 if (!msg->l3h) {
50 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
51 return -1;
52 }
53
54 if (msgb_l3len(msg) < 3) {
55 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
56 return -1;
57 }
58
59 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
60 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
61 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
62 return -1;
63 }
64
65 cic = ntohs(*(u_int16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
66 timeslot = cic & 0x1f;
67 multiplex = (cic & ~0x1f) >> 5;
68
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080069
70 combined = (32 * multiplex) + timeslot;
71
72 /* find stale connections using that endpoint */
73 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
74 if (mcon->msc_timeslot == combined) {
75 LOGP(DNAT, LOGL_ERROR,
76 "Timeslot %d was assigned to 0x%x and now 0x%x\n",
77 combined,
78 sccp_src_ref_to_int(&mcon->patched_ref),
79 sccp_src_ref_to_int(&con->patched_ref));
80 bsc_mgcp_dlcx(mcon);
81 }
82 }
83
84 con->msc_timeslot = combined;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080085 con->bsc_timeslot = con->msc_timeslot;
86 return 0;
87}
88
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080089static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020090{
91 if (nat->bsc_endpoints[i].transaction_id) {
92 talloc_free(nat->bsc_endpoints[i].transaction_id);
93 nat->bsc_endpoints[i].transaction_id = NULL;
94 }
95
96 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020097}
98
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +020099void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
100{
101 int i;
102
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800103 for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i){
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200104 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800105 mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]);
106 }
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200107}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200108
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800109/* send a MDCX where we do not want a response */
110static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, struct mgcp_endpoint *endp)
111{
112 char buf[2096];
113 int len;
114
115 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800116 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800117 "Z: noanswer\r\n"
118 "\r\n"
119 "c=IN IP4 %s\r\n"
120 "m=audio %d RTP/AVP 255\r\n",
121 ENDPOINT_NUMBER(endp),
122 bsc->nat->mgcp_cfg->source_addr,
123 endp->rtp_port);
124 if (len < 0) {
125 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
126 return;
127 }
128}
129
130static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
131{
132 char buf[2096];
133 int len;
134
135 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800136 "DLCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800137 "Z: noanswer\r\n", endpoint);
138 if (len < 0) {
139 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
140 return;
141 }
142
143 bsc_write_mgcp(bsc, (u_int8_t *) buf, len);
144}
145
146void bsc_mgcp_init(struct sccp_connections *con)
147{
148 con->msc_timeslot = -1;
149 con->bsc_timeslot = -1;
150}
151
152void bsc_mgcp_dlcx(struct sccp_connections *con)
153{
154 /* send a DLCX down the stream */
155 if (con->bsc_timeslot != -1) {
156 int endp = mgcp_timeslot_to_endpoint(0, con->msc_timeslot);
157 bsc_mgcp_send_dlcx(con->bsc, endp);
158 bsc_mgcp_free_endpoint(con->bsc->nat, endp);
159 }
160
161 bsc_mgcp_init(con);
162}
163
164
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800165struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200166{
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800167 struct sccp_connections *con = NULL;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200168 struct sccp_connections *sccp;
169
170 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
171 if (sccp->msc_timeslot == -1)
172 continue;
173 if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint)
174 continue;
175
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800176 con = sccp;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200177 }
178
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800179 if (con)
180 return con;
Holger Hans Peter Freythereb52e892010-04-18 02:14:45 +0800181
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200182 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
183 return NULL;
184}
185
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200186int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id)
187{
188 struct bsc_nat *nat;
189 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800190 struct sccp_connections *sccp;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200191 struct mgcp_endpoint *mgcp_endp;
192 struct msgb *bsc_msg;
193
194 nat = cfg->data;
195 bsc_endp = &nat->bsc_endpoints[endpoint];
196 mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint];
197
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800198 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200199
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800200 if (!sccp) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200201 LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for a new connection on 0x%x for %d\n", endpoint, state);
202
203 switch (state) {
204 case MGCP_ENDP_CRCX:
205 return MGCP_POLICY_REJECT;
206 break;
207 case MGCP_ENDP_DLCX:
208 return MGCP_POLICY_CONT;
209 break;
210 case MGCP_ENDP_MDCX:
211 return MGCP_POLICY_CONT;
212 break;
213 default:
214 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
215 return MGCP_POLICY_CONT;
216 break;
217 }
218 }
219
220 if (bsc_endp->transaction_id) {
Holger Hans Peter Freyther842c0c02010-04-21 15:17:45 +0800221 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
222 endpoint, bsc_endp->transaction_id);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200223 talloc_free(bsc_endp->transaction_id);
224 }
225
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200226 /* we need to generate a new and patched message */
227 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
228 nat->mgcp_cfg->source_addr, mgcp_endp->rtp_port);
229 if (!bsc_msg) {
230 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
231 return MGCP_POLICY_CONT;
232 }
233
234
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200235 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800236 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200237
238 /* we need to update some bits */
239 if (state == MGCP_ENDP_CRCX) {
240 struct sockaddr_in sock;
241 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800242 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200243 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
244 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200245 } else {
246 mgcp_endp->bts = sock.sin_addr;
247 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200248
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800249 /* send the message and a fake MDCX for force sending of a dummy packet */
250 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
251 bsc_mgcp_send_mdcx(sccp->bsc, mgcp_endp);
252 return MGCP_POLICY_DEFER;
253 } else if (state == MGCP_ENDP_DLCX) {
254 /* we will free the endpoint now and send a DLCX to the BSC */
255 bsc_mgcp_dlcx(sccp);
256 return MGCP_POLICY_CONT;
257 } else {
258 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
259 return MGCP_POLICY_DEFER;
260 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200261}
262
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200263/*
264 * We have received a msg from the BSC. We will see if we know
265 * this transaction and if it belongs to the BSC. Then we will
266 * need to patch the content to point to the local network and we
267 * need to update the I: that was assigned by the BSS.
268 */
269void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
270{
271 struct msgb *output;
272 struct bsc_endpoint *bsc_endp = NULL;
273 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200274 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200275 char transaction_id[60];
276
277 /* Some assumption that our buffer is big enough.. and null terminate */
278 if (msgb_l2len(msg) > 2000) {
279 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
280 return;
281 }
282
283 msg->l2h[msgb_l2len(msg)] = '\0';
284
285 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
286 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
287 return;
288 }
289
290 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
291 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
292 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200293 /* no one listening? a bug? */
294 if (!bsc->nat->bsc_endpoints[i].transaction_id)
295 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200296 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
297 continue;
298
299 endp = &bsc->nat->mgcp_cfg->endpoints[i];
300 bsc_endp = &bsc->nat->bsc_endpoints[i];
301 break;
302 }
303
304 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200305 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
306 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200307 return;
308 }
309
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800310 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200311
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200312 /* free some stuff */
313 talloc_free(bsc_endp->transaction_id);
314 bsc_endp->transaction_id = NULL;
315
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200316 /*
317 * rewrite the information. In case the endpoint was deleted
318 * there should be nothing for us to rewrite so putting endp->rtp_port
319 * with the value of 0 should be no problem.
320 */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200321 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
322 bsc->nat->mgcp_cfg->source_addr, endp->rtp_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200323
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200324 if (!output) {
325 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
326 return;
327 }
328
329 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
330 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
331 msgb_free(output);
332 }
333}
334
335int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
336{
337 /* we want to parse two strings */
338 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
339}
340
341int bsc_mgcp_extract_ci(const char *str)
342{
343 int ci;
344 char *res = strstr(str, "I: ");
345 if (!res)
346 return CI_UNUSED;
347
Holger Hans Peter Freyther806aca92010-04-05 09:07:39 +0200348 if (sscanf(res, "I: %d", &ci) != 1)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200349 return CI_UNUSED;
350 return ci;
351}
352
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200353/* we need to replace some strings... */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200354struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200355{
356 static const char *ip_str = "c=IN IP4 ";
357 static const char *aud_str = "m=audio ";
358
359 char buf[128];
360 char *running, *token;
361 struct msgb *output;
362
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200363 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200364 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
365 return NULL;
366 }
367
368 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
369 if (!output) {
370 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
371 return NULL;
372 }
373
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200374 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200375 output->l2h = output->data;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200376 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200377 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200378 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200379
380 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
381 output->l3h = msgb_put(output, strlen(ip_str));
382 memcpy(output->l3h, ip_str, strlen(ip_str));
383 output->l3h = msgb_put(output, strlen(ip));
384 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200385
386 if (cr) {
387 output->l3h = msgb_put(output, 2);
388 output->l3h[0] = '\r';
389 output->l3h[1] = '\n';
390 } else {
391 output->l3h = msgb_put(output, 1);
392 output->l3h[0] = '\n';
393 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200394 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
395 int payload;
396 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
397 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
398 msgb_free(output);
399 return NULL;
400 }
401
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200402 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
403 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200404 buf[sizeof(buf)-1] = '\0';
405
406 output->l3h = msgb_put(output, strlen(buf));
407 memcpy(output->l3h, buf, strlen(buf));
408 } else {
409 output->l3h = msgb_put(output, len + 1);
410 memcpy(output->l3h, token, len);
411 output->l3h[len] = '\n';
412 }
413 }
414
415 return output;
416}
417
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200418static int mgcp_do_read(struct bsc_fd *fd)
419{
420 struct bsc_nat *nat;
421 struct msgb *msg, *resp;
422 int rc;
423
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200424 nat = fd->data;
425
426 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
427 if (rc <= 0) {
428 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
429 return -1;
430 }
431
432 nat->mgcp_msg[rc] = '\0';
433 nat->mgcp_length = rc;
434
435 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200436 if (!msg) {
437 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
438 return -1;
439 }
440
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200441 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200442 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200443 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
444 msgb_free(msg);
445
446 /* we do have a direct answer... e.g. AUEP */
447 if (resp) {
448 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
449 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
450 msgb_free(resp);
451 }
452 }
453
454 return 0;
455}
456
457static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
458{
459 int rc;
460
461 rc = write(bfd->fd, msg->data, msg->len);
462
463 if (rc != msg->len) {
464 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
465 return -1;
466 }
467
468 return rc;
469}
470
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800471int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200472{
473 int on;
474 struct sockaddr_in addr;
475
476 if (!nat->mgcp_cfg->call_agent_addr) {
477 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
478 return -1;
479 }
480
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200481 if (nat->mgcp_cfg->bts_ip) {
482 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
483 return -1;
484 }
485
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200486 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
487 if (nat->mgcp_queue.bfd.fd < 0) {
488 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
489 return -1;
490 }
491
492 on = 1;
493 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
494
495 addr.sin_family = AF_INET;
496 addr.sin_port = htons(nat->mgcp_cfg->source_port);
497 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
498
499 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
500 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
501 close(nat->mgcp_queue.bfd.fd);
502 nat->mgcp_queue.bfd.fd = -1;
503 return -1;
504 }
505
506 addr.sin_port = htons(2727);
507 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
508 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
509 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
510 nat->mgcp_cfg->call_agent_addr, errno);
511 close(nat->mgcp_queue.bfd.fd);
512 nat->mgcp_queue.bfd.fd = -1;
513 return -1;
514 }
515
516 write_queue_init(&nat->mgcp_queue, 10);
517 nat->mgcp_queue.bfd.when = BSC_FD_READ;
518 nat->mgcp_queue.bfd.data = nat;
519 nat->mgcp_queue.read_cb = mgcp_do_read;
520 nat->mgcp_queue.write_cb = mgcp_do_write;
521
522 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
523 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
524 close(nat->mgcp_queue.bfd.fd);
525 nat->mgcp_queue.bfd.fd = -1;
526 return -1;
527 }
528
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200529 /* some more MGCP config handling */
Holger Hans Peter Freyther4ad58502010-04-06 11:14:03 +0200530 nat->mgcp_cfg->audio_payload = -1;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200531 nat->mgcp_cfg->data = nat;
532 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freytherbba59342010-04-07 10:52:48 +0200533 nat->mgcp_cfg->force_realloc = 1;
Holger Hans Peter Freyther290d84e2010-04-09 18:51:01 +0200534 nat->mgcp_cfg->bts_ip = "";
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200535 nat->bsc_endpoints = talloc_zero_array(nat,
536 struct bsc_endpoint,
537 nat->mgcp_cfg->number_endpoints + 1);
538
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200539 return 0;
540}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200541
542void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
543{
544 int i;
545 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
546 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
547
548 if (bsc_endp->bsc != bsc)
549 continue;
550
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800551 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200552 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
553 }
554}