blob: 5d77c2bd920d41ecb9c6a467f02a8fd2509ceb6c [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;
Holger Hans Peter Freyther959bbcf2010-04-22 20:12:13 +0800150 con->crcx = 0;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800151}
152
153void bsc_mgcp_dlcx(struct sccp_connections *con)
154{
155 /* send a DLCX down the stream */
Holger Hans Peter Freyther959bbcf2010-04-22 20:12:13 +0800156 if (con->bsc_timeslot != -1 && con->crcx) {
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800157 int endp = mgcp_timeslot_to_endpoint(0, con->msc_timeslot);
158 bsc_mgcp_send_dlcx(con->bsc, endp);
159 bsc_mgcp_free_endpoint(con->bsc->nat, endp);
160 }
161
162 bsc_mgcp_init(con);
163}
164
165
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800166struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200167{
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800168 struct sccp_connections *con = NULL;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200169 struct sccp_connections *sccp;
170
171 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
172 if (sccp->msc_timeslot == -1)
173 continue;
174 if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint)
175 continue;
176
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800177 con = sccp;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200178 }
179
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800180 if (con)
181 return con;
Holger Hans Peter Freythereb52e892010-04-18 02:14:45 +0800182
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200183 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
184 return NULL;
185}
186
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200187int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id)
188{
189 struct bsc_nat *nat;
190 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800191 struct sccp_connections *sccp;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200192 struct mgcp_endpoint *mgcp_endp;
193 struct msgb *bsc_msg;
194
195 nat = cfg->data;
196 bsc_endp = &nat->bsc_endpoints[endpoint];
197 mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint];
198
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800199 if (bsc_endp->transaction_id) {
200 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
201 endpoint, bsc_endp->transaction_id);
202 talloc_free(bsc_endp->transaction_id);
203 bsc_endp->transaction_id = NULL;
204 }
205 bsc_endp->bsc = NULL;
206
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800207 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200208
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800209 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800210 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 +0200211
212 switch (state) {
213 case MGCP_ENDP_CRCX:
214 return MGCP_POLICY_REJECT;
215 break;
216 case MGCP_ENDP_DLCX:
217 return MGCP_POLICY_CONT;
218 break;
219 case MGCP_ENDP_MDCX:
220 return MGCP_POLICY_CONT;
221 break;
222 default:
223 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
224 return MGCP_POLICY_CONT;
225 break;
226 }
227 }
228
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200229 /* we need to generate a new and patched message */
230 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
231 nat->mgcp_cfg->source_addr, mgcp_endp->rtp_port);
232 if (!bsc_msg) {
233 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
234 return MGCP_POLICY_CONT;
235 }
236
237
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200238 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800239 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200240
241 /* we need to update some bits */
242 if (state == MGCP_ENDP_CRCX) {
243 struct sockaddr_in sock;
244 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800245 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200246 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
247 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200248 } else {
249 mgcp_endp->bts = sock.sin_addr;
250 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200251
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800252 /* send the message and a fake MDCX for force sending of a dummy packet */
Holger Hans Peter Freyther959bbcf2010-04-22 20:12:13 +0800253 sccp->crcx = 1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800254 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
255 bsc_mgcp_send_mdcx(sccp->bsc, mgcp_endp);
256 return MGCP_POLICY_DEFER;
257 } else if (state == MGCP_ENDP_DLCX) {
258 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freyther3a347f02010-05-01 10:31:53 +0800259 msgb_free(bsc_msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800260 bsc_mgcp_dlcx(sccp);
261 return MGCP_POLICY_CONT;
262 } else {
263 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
264 return MGCP_POLICY_DEFER;
265 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200266}
267
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200268/*
269 * We have received a msg from the BSC. We will see if we know
270 * this transaction and if it belongs to the BSC. Then we will
271 * need to patch the content to point to the local network and we
272 * need to update the I: that was assigned by the BSS.
273 */
274void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
275{
276 struct msgb *output;
277 struct bsc_endpoint *bsc_endp = NULL;
278 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200279 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200280 char transaction_id[60];
281
282 /* Some assumption that our buffer is big enough.. and null terminate */
283 if (msgb_l2len(msg) > 2000) {
284 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
285 return;
286 }
287
288 msg->l2h[msgb_l2len(msg)] = '\0';
289
290 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
291 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
292 return;
293 }
294
295 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
296 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
297 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200298 /* no one listening? a bug? */
299 if (!bsc->nat->bsc_endpoints[i].transaction_id)
300 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200301 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
302 continue;
303
304 endp = &bsc->nat->mgcp_cfg->endpoints[i];
305 bsc_endp = &bsc->nat->bsc_endpoints[i];
306 break;
307 }
308
309 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200310 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
311 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200312 return;
313 }
314
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800315 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200316
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200317 /* free some stuff */
318 talloc_free(bsc_endp->transaction_id);
319 bsc_endp->transaction_id = NULL;
320
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200321 /*
322 * rewrite the information. In case the endpoint was deleted
323 * there should be nothing for us to rewrite so putting endp->rtp_port
324 * with the value of 0 should be no problem.
325 */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200326 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
327 bsc->nat->mgcp_cfg->source_addr, endp->rtp_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200328
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200329 if (!output) {
330 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
331 return;
332 }
333
334 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
335 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
336 msgb_free(output);
337 }
338}
339
340int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
341{
342 /* we want to parse two strings */
343 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
344}
345
346int bsc_mgcp_extract_ci(const char *str)
347{
348 int ci;
349 char *res = strstr(str, "I: ");
350 if (!res)
351 return CI_UNUSED;
352
Holger Hans Peter Freyther806aca92010-04-05 09:07:39 +0200353 if (sscanf(res, "I: %d", &ci) != 1)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200354 return CI_UNUSED;
355 return ci;
356}
357
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200358/* we need to replace some strings... */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200359struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200360{
361 static const char *ip_str = "c=IN IP4 ";
362 static const char *aud_str = "m=audio ";
363
364 char buf[128];
365 char *running, *token;
366 struct msgb *output;
367
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200368 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200369 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
370 return NULL;
371 }
372
373 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
374 if (!output) {
375 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
376 return NULL;
377 }
378
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200379 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200380 output->l2h = output->data;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200381 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200382 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200383 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200384
385 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
386 output->l3h = msgb_put(output, strlen(ip_str));
387 memcpy(output->l3h, ip_str, strlen(ip_str));
388 output->l3h = msgb_put(output, strlen(ip));
389 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200390
391 if (cr) {
392 output->l3h = msgb_put(output, 2);
393 output->l3h[0] = '\r';
394 output->l3h[1] = '\n';
395 } else {
396 output->l3h = msgb_put(output, 1);
397 output->l3h[0] = '\n';
398 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200399 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
400 int payload;
401 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
402 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
403 msgb_free(output);
404 return NULL;
405 }
406
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200407 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
408 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200409 buf[sizeof(buf)-1] = '\0';
410
411 output->l3h = msgb_put(output, strlen(buf));
412 memcpy(output->l3h, buf, strlen(buf));
413 } else {
414 output->l3h = msgb_put(output, len + 1);
415 memcpy(output->l3h, token, len);
416 output->l3h[len] = '\n';
417 }
418 }
419
420 return output;
421}
422
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200423static int mgcp_do_read(struct bsc_fd *fd)
424{
425 struct bsc_nat *nat;
426 struct msgb *msg, *resp;
427 int rc;
428
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200429 nat = fd->data;
430
431 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
432 if (rc <= 0) {
433 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
434 return -1;
435 }
436
437 nat->mgcp_msg[rc] = '\0';
438 nat->mgcp_length = rc;
439
440 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200441 if (!msg) {
442 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
443 return -1;
444 }
445
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200446 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200447 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200448 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
449 msgb_free(msg);
450
451 /* we do have a direct answer... e.g. AUEP */
452 if (resp) {
453 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
454 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
455 msgb_free(resp);
456 }
457 }
458
459 return 0;
460}
461
462static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
463{
464 int rc;
465
466 rc = write(bfd->fd, msg->data, msg->len);
467
468 if (rc != msg->len) {
469 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
470 return -1;
471 }
472
473 return rc;
474}
475
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800476int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200477{
478 int on;
479 struct sockaddr_in addr;
480
481 if (!nat->mgcp_cfg->call_agent_addr) {
482 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
483 return -1;
484 }
485
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200486 if (nat->mgcp_cfg->bts_ip) {
487 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
488 return -1;
489 }
490
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200491 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
492 if (nat->mgcp_queue.bfd.fd < 0) {
493 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
494 return -1;
495 }
496
497 on = 1;
498 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
499
500 addr.sin_family = AF_INET;
501 addr.sin_port = htons(nat->mgcp_cfg->source_port);
502 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
503
504 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
505 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
506 close(nat->mgcp_queue.bfd.fd);
507 nat->mgcp_queue.bfd.fd = -1;
508 return -1;
509 }
510
511 addr.sin_port = htons(2727);
512 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
513 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
514 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
515 nat->mgcp_cfg->call_agent_addr, errno);
516 close(nat->mgcp_queue.bfd.fd);
517 nat->mgcp_queue.bfd.fd = -1;
518 return -1;
519 }
520
521 write_queue_init(&nat->mgcp_queue, 10);
522 nat->mgcp_queue.bfd.when = BSC_FD_READ;
523 nat->mgcp_queue.bfd.data = nat;
524 nat->mgcp_queue.read_cb = mgcp_do_read;
525 nat->mgcp_queue.write_cb = mgcp_do_write;
526
527 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
528 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
529 close(nat->mgcp_queue.bfd.fd);
530 nat->mgcp_queue.bfd.fd = -1;
531 return -1;
532 }
533
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200534 /* some more MGCP config handling */
Holger Hans Peter Freyther4ad58502010-04-06 11:14:03 +0200535 nat->mgcp_cfg->audio_payload = -1;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200536 nat->mgcp_cfg->data = nat;
537 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freytherbba59342010-04-07 10:52:48 +0200538 nat->mgcp_cfg->force_realloc = 1;
Holger Hans Peter Freyther290d84e2010-04-09 18:51:01 +0200539 nat->mgcp_cfg->bts_ip = "";
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200540 nat->bsc_endpoints = talloc_zero_array(nat,
541 struct bsc_endpoint,
542 nat->mgcp_cfg->number_endpoints + 1);
543
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200544 return 0;
545}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200546
547void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
548{
549 int i;
550 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
551 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
552
553 if (bsc_endp->bsc != bsc)
554 continue;
555
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800556 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200557 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
558 }
559}