blob: cb9264dfcbed3555a951dc6d908145596c3ac134 [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 Freyther08a1b162010-04-18 02:26:16 +0800199 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200200
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800201 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800202 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 +0200203
204 switch (state) {
205 case MGCP_ENDP_CRCX:
206 return MGCP_POLICY_REJECT;
207 break;
208 case MGCP_ENDP_DLCX:
209 return MGCP_POLICY_CONT;
210 break;
211 case MGCP_ENDP_MDCX:
212 return MGCP_POLICY_CONT;
213 break;
214 default:
215 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
216 return MGCP_POLICY_CONT;
217 break;
218 }
219 }
220
221 if (bsc_endp->transaction_id) {
Holger Hans Peter Freyther842c0c02010-04-21 15:17:45 +0800222 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
223 endpoint, bsc_endp->transaction_id);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200224 talloc_free(bsc_endp->transaction_id);
225 }
226
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200227 /* we need to generate a new and patched message */
228 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
229 nat->mgcp_cfg->source_addr, mgcp_endp->rtp_port);
230 if (!bsc_msg) {
231 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
232 return MGCP_POLICY_CONT;
233 }
234
235
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200236 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800237 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200238
239 /* we need to update some bits */
240 if (state == MGCP_ENDP_CRCX) {
241 struct sockaddr_in sock;
242 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800243 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200244 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
245 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200246 } else {
247 mgcp_endp->bts = sock.sin_addr;
248 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200249
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800250 /* send the message and a fake MDCX for force sending of a dummy packet */
Holger Hans Peter Freyther959bbcf2010-04-22 20:12:13 +0800251 sccp->crcx = 1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800252 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
253 bsc_mgcp_send_mdcx(sccp->bsc, mgcp_endp);
254 return MGCP_POLICY_DEFER;
255 } else if (state == MGCP_ENDP_DLCX) {
256 /* we will free the endpoint now and send a DLCX to the BSC */
257 bsc_mgcp_dlcx(sccp);
258 return MGCP_POLICY_CONT;
259 } else {
260 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
261 return MGCP_POLICY_DEFER;
262 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200263}
264
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200265/*
266 * We have received a msg from the BSC. We will see if we know
267 * this transaction and if it belongs to the BSC. Then we will
268 * need to patch the content to point to the local network and we
269 * need to update the I: that was assigned by the BSS.
270 */
271void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
272{
273 struct msgb *output;
274 struct bsc_endpoint *bsc_endp = NULL;
275 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200276 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200277 char transaction_id[60];
278
279 /* Some assumption that our buffer is big enough.. and null terminate */
280 if (msgb_l2len(msg) > 2000) {
281 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
282 return;
283 }
284
285 msg->l2h[msgb_l2len(msg)] = '\0';
286
287 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
288 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
289 return;
290 }
291
292 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
293 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
294 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200295 /* no one listening? a bug? */
296 if (!bsc->nat->bsc_endpoints[i].transaction_id)
297 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200298 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
299 continue;
300
301 endp = &bsc->nat->mgcp_cfg->endpoints[i];
302 bsc_endp = &bsc->nat->bsc_endpoints[i];
303 break;
304 }
305
306 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200307 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
308 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200309 return;
310 }
311
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800312 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200313
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200314 /* free some stuff */
315 talloc_free(bsc_endp->transaction_id);
316 bsc_endp->transaction_id = NULL;
317
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200318 /*
319 * rewrite the information. In case the endpoint was deleted
320 * there should be nothing for us to rewrite so putting endp->rtp_port
321 * with the value of 0 should be no problem.
322 */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200323 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
324 bsc->nat->mgcp_cfg->source_addr, endp->rtp_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200325
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200326 if (!output) {
327 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
328 return;
329 }
330
331 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
332 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
333 msgb_free(output);
334 }
335}
336
337int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
338{
339 /* we want to parse two strings */
340 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
341}
342
343int bsc_mgcp_extract_ci(const char *str)
344{
345 int ci;
346 char *res = strstr(str, "I: ");
347 if (!res)
348 return CI_UNUSED;
349
Holger Hans Peter Freyther806aca92010-04-05 09:07:39 +0200350 if (sscanf(res, "I: %d", &ci) != 1)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200351 return CI_UNUSED;
352 return ci;
353}
354
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200355/* we need to replace some strings... */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200356struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200357{
358 static const char *ip_str = "c=IN IP4 ";
359 static const char *aud_str = "m=audio ";
360
361 char buf[128];
362 char *running, *token;
363 struct msgb *output;
364
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200365 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200366 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
367 return NULL;
368 }
369
370 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
371 if (!output) {
372 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
373 return NULL;
374 }
375
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200376 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200377 output->l2h = output->data;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200378 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200379 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200380 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200381
382 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
383 output->l3h = msgb_put(output, strlen(ip_str));
384 memcpy(output->l3h, ip_str, strlen(ip_str));
385 output->l3h = msgb_put(output, strlen(ip));
386 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200387
388 if (cr) {
389 output->l3h = msgb_put(output, 2);
390 output->l3h[0] = '\r';
391 output->l3h[1] = '\n';
392 } else {
393 output->l3h = msgb_put(output, 1);
394 output->l3h[0] = '\n';
395 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200396 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
397 int payload;
398 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
399 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
400 msgb_free(output);
401 return NULL;
402 }
403
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200404 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
405 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200406 buf[sizeof(buf)-1] = '\0';
407
408 output->l3h = msgb_put(output, strlen(buf));
409 memcpy(output->l3h, buf, strlen(buf));
410 } else {
411 output->l3h = msgb_put(output, len + 1);
412 memcpy(output->l3h, token, len);
413 output->l3h[len] = '\n';
414 }
415 }
416
417 return output;
418}
419
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200420static int mgcp_do_read(struct bsc_fd *fd)
421{
422 struct bsc_nat *nat;
423 struct msgb *msg, *resp;
424 int rc;
425
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200426 nat = fd->data;
427
428 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
429 if (rc <= 0) {
430 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
431 return -1;
432 }
433
434 nat->mgcp_msg[rc] = '\0';
435 nat->mgcp_length = rc;
436
437 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200438 if (!msg) {
439 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
440 return -1;
441 }
442
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200443 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200444 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200445 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
446 msgb_free(msg);
447
448 /* we do have a direct answer... e.g. AUEP */
449 if (resp) {
450 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
451 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
452 msgb_free(resp);
453 }
454 }
455
456 return 0;
457}
458
459static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
460{
461 int rc;
462
463 rc = write(bfd->fd, msg->data, msg->len);
464
465 if (rc != msg->len) {
466 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
467 return -1;
468 }
469
470 return rc;
471}
472
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800473int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200474{
475 int on;
476 struct sockaddr_in addr;
477
478 if (!nat->mgcp_cfg->call_agent_addr) {
479 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
480 return -1;
481 }
482
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200483 if (nat->mgcp_cfg->bts_ip) {
484 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
485 return -1;
486 }
487
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200488 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
489 if (nat->mgcp_queue.bfd.fd < 0) {
490 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
491 return -1;
492 }
493
494 on = 1;
495 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
496
497 addr.sin_family = AF_INET;
498 addr.sin_port = htons(nat->mgcp_cfg->source_port);
499 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
500
501 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
502 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
503 close(nat->mgcp_queue.bfd.fd);
504 nat->mgcp_queue.bfd.fd = -1;
505 return -1;
506 }
507
508 addr.sin_port = htons(2727);
509 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
510 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
511 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
512 nat->mgcp_cfg->call_agent_addr, errno);
513 close(nat->mgcp_queue.bfd.fd);
514 nat->mgcp_queue.bfd.fd = -1;
515 return -1;
516 }
517
518 write_queue_init(&nat->mgcp_queue, 10);
519 nat->mgcp_queue.bfd.when = BSC_FD_READ;
520 nat->mgcp_queue.bfd.data = nat;
521 nat->mgcp_queue.read_cb = mgcp_do_read;
522 nat->mgcp_queue.write_cb = mgcp_do_write;
523
524 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
525 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
526 close(nat->mgcp_queue.bfd.fd);
527 nat->mgcp_queue.bfd.fd = -1;
528 return -1;
529 }
530
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200531 /* some more MGCP config handling */
Holger Hans Peter Freyther4ad58502010-04-06 11:14:03 +0200532 nat->mgcp_cfg->audio_payload = -1;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200533 nat->mgcp_cfg->data = nat;
534 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freytherbba59342010-04-07 10:52:48 +0200535 nat->mgcp_cfg->force_realloc = 1;
Holger Hans Peter Freyther290d84e2010-04-09 18:51:01 +0200536 nat->mgcp_cfg->bts_ip = "";
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200537 nat->bsc_endpoints = talloc_zero_array(nat,
538 struct bsc_endpoint,
539 nat->mgcp_cfg->number_endpoints + 1);
540
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200541 return 0;
542}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200543
544void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
545{
546 int i;
547 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
548 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
549
550 if (bsc_endp->bsc != bsc)
551 continue;
552
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800553 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200554 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
555 }
556}