blob: d8ec2986983b40b46ba0b80981ab4246428480fe [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>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080023#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080024#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080025#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
Harald Welted5db12c2010-08-03 15:11:51 +020029#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080030
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 Freyther69d801e2010-06-15 20:13:33 +080033#include <osmocore/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 Freyther45fd07d2010-08-28 18:22:14 +080041static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
42{
43 const int number_endpoints = ARRAY_SIZE(bsc->endpoint_status);
44 int i;
45
46 for (i = 1; i < number_endpoints; ++i) {
47 int endpoint = (bsc->last_endpoint + i) % number_endpoints;
48 if (endpoint == 0)
49 endpoint = 1;
50
51 if (bsc->endpoint_status[endpoint] == 0) {
52 bsc->endpoint_status[endpoint] = 1;
53 con->bsc_endp = endpoint;
54 bsc->last_endpoint = endpoint;
55 return 0;
56 }
57 }
58
59 return -1;
60}
61
62static uint16_t create_cic(int endpoint)
63{
64 int timeslot, multiplex;
65
66 mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
67 return (multiplex << 5) | (timeslot & 0x1f);
68}
69
70int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080071{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080072 struct sccp_connections *mcon;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080073 struct tlv_parsed tp;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +080074 uint16_t cic;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080075 uint8_t timeslot;
76 uint8_t multiplex;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +080077 int endp;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080078
79 if (!msg->l3h) {
80 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
81 return -1;
82 }
83
84 if (msgb_l3len(msg) < 3) {
85 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
86 return -1;
87 }
88
89 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
90 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
91 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
92 return -1;
93 }
94
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +080095 cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080096 timeslot = cic & 0x1f;
97 multiplex = (cic & ~0x1f) >> 5;
98
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080099
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800100 endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800101
102 /* find stale connections using that endpoint */
103 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800104 if (mcon->msc_endp == endp) {
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800105 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800106 "Endpoint %d was assigned to 0x%x and now 0x%x\n",
107 endp,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800108 sccp_src_ref_to_int(&mcon->patched_ref),
109 sccp_src_ref_to_int(&con->patched_ref));
110 bsc_mgcp_dlcx(mcon);
111 }
112 }
113
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800114 con->msc_endp = endp;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800115 if (bsc_assign_endpoint(con->bsc, con) != 0)
116 return -1;
117
118 /*
119 * now patch the message for the new CIC...
120 * still assumed to be one multiplex only
121 */
122 cic = htons(create_cic(con->bsc_endp));
123 memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
124 &cic, sizeof(cic));
125
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800126 return 0;
127}
128
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800129static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200130{
131 if (nat->bsc_endpoints[i].transaction_id) {
132 talloc_free(nat->bsc_endpoints[i].transaction_id);
133 nat->bsc_endpoints[i].transaction_id = NULL;
134 }
135
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800136 nat->bsc_endpoints[i].transaction_state = 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200137 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200138}
139
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200140void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
141{
142 int i;
143
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800144 for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i){
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200145 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800146 mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]);
147 }
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200148}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200149
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800150/* send a MDCX where we do not want a response */
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800151static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800152{
153 char buf[2096];
154 int len;
155
156 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800157 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800158 "Z: noanswer\r\n"
159 "\r\n"
160 "c=IN IP4 %s\r\n"
161 "m=audio %d RTP/AVP 255\r\n",
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800162 port,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800163 bsc->nat->mgcp_cfg->source_addr,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800164 endp->bts_end.local_port);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800165 if (len < 0) {
166 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
167 return;
168 }
Holger Hans Peter Freytherd38aa452010-08-30 11:58:49 +0800169
170 #warning "The MDCX is not send to the BSC. It should"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800171}
172
173static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
174{
175 char buf[2096];
176 int len;
177
178 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherbf812fa2010-08-30 12:01:36 +0800179 "DLCX 26 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800180 "Z: noanswer\r\n", endpoint);
181 if (len < 0) {
182 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
183 return;
184 }
185
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800186 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800187}
188
189void bsc_mgcp_init(struct sccp_connections *con)
190{
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800191 con->msc_endp = -1;
192 con->bsc_endp = -1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800193}
194
195void bsc_mgcp_dlcx(struct sccp_connections *con)
196{
197 /* send a DLCX down the stream */
Holger Hans Peter Freyther39cd32e2010-08-28 18:11:07 +0800198 if (con->bsc_endp != -1) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800199 bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
200 bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800201 }
202
203 bsc_mgcp_init(con);
204}
205
206
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800207struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200208{
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800209 struct sccp_connections *con = NULL;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200210 struct sccp_connections *sccp;
211
212 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800213 if (sccp->msc_endp == -1)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200214 continue;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800215 if (sccp->msc_endp != endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200216 continue;
217
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800218 con = sccp;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200219 }
220
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800221 if (con)
222 return con;
Holger Hans Peter Freythereb52e892010-04-18 02:14:45 +0800223
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200224 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
225 return NULL;
226}
227
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200228int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id)
229{
230 struct bsc_nat *nat;
231 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800232 struct sccp_connections *sccp;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200233 struct mgcp_endpoint *mgcp_endp;
234 struct msgb *bsc_msg;
235
236 nat = cfg->data;
237 bsc_endp = &nat->bsc_endpoints[endpoint];
238 mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint];
239
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800240 if (bsc_endp->transaction_id) {
241 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
242 endpoint, bsc_endp->transaction_id);
243 talloc_free(bsc_endp->transaction_id);
244 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800245 bsc_endp->transaction_state = 0;
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800246 }
247 bsc_endp->bsc = NULL;
248
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800249 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200250
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800251 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800252 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 +0200253
254 switch (state) {
255 case MGCP_ENDP_CRCX:
256 return MGCP_POLICY_REJECT;
257 break;
258 case MGCP_ENDP_DLCX:
259 return MGCP_POLICY_CONT;
260 break;
261 case MGCP_ENDP_MDCX:
262 return MGCP_POLICY_CONT;
263 break;
264 default:
265 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
266 return MGCP_POLICY_CONT;
267 break;
268 }
269 }
270
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200271 /* we need to generate a new and patched message */
272 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800273 nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200274 if (!bsc_msg) {
275 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
276 return MGCP_POLICY_CONT;
277 }
278
279
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200280 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800281 bsc_endp->transaction_state = state;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800282 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200283
284 /* we need to update some bits */
285 if (state == MGCP_ENDP_CRCX) {
286 struct sockaddr_in sock;
287 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800288 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200289 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
290 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200291 } else {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800292 mgcp_endp->bts_end.addr = sock.sin_addr;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200293 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200294
Holger Hans Peter Freythere66cac32010-08-05 01:50:44 +0800295 /* send the message and a fake MDCX to force sending of a dummy packet */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800296 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800297 bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800298 return MGCP_POLICY_DEFER;
299 } else if (state == MGCP_ENDP_DLCX) {
300 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freyther3a347f02010-05-01 10:31:53 +0800301 msgb_free(bsc_msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800302 bsc_mgcp_dlcx(sccp);
303 return MGCP_POLICY_CONT;
304 } else {
305 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
306 return MGCP_POLICY_DEFER;
307 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200308}
309
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200310/*
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800311 * We do have a failure, free data downstream..
312 */
313static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
314 struct bsc_connection *bsc)
315{
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800316 LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
317 ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800318
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800319 /* if a CRCX failed... send a DLCX down the stream */
320 if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
321 struct sccp_connections *con;
322 con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
323 if (!con) {
324 LOGP(DMGCP, LOGL_ERROR,
325 "No SCCP connection for endp 0x%x\n",
326 ENDPOINT_NUMBER(endp));
327 } else {
328 if (con->bsc == bsc) {
Holger Hans Peter Freyther8574dcf2010-08-29 22:39:07 +0800329 bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800330 } else {
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800331 LOGP(DMGCP, LOGL_ERROR,
332 "Endpoint belongs to a different BSC\n");
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800333 }
334 }
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800335 }
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800336
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800337 bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
338 mgcp_free_endp(endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800339}
340
341/*
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200342 * We have received a msg from the BSC. We will see if we know
343 * this transaction and if it belongs to the BSC. Then we will
344 * need to patch the content to point to the local network and we
345 * need to update the I: that was assigned by the BSS.
346 */
347void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
348{
349 struct msgb *output;
350 struct bsc_endpoint *bsc_endp = NULL;
351 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200352 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200353 char transaction_id[60];
354
355 /* Some assumption that our buffer is big enough.. and null terminate */
356 if (msgb_l2len(msg) > 2000) {
357 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
358 return;
359 }
360
361 msg->l2h[msgb_l2len(msg)] = '\0';
362
363 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
364 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
365 return;
366 }
367
368 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
369 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
370 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200371 /* no one listening? a bug? */
372 if (!bsc->nat->bsc_endpoints[i].transaction_id)
373 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200374 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
375 continue;
376
377 endp = &bsc->nat->mgcp_cfg->endpoints[i];
378 bsc_endp = &bsc->nat->bsc_endpoints[i];
379 break;
380 }
381
382 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200383 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
384 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200385 return;
386 }
387
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800388 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800389 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800390 free_chan_downstream(endp, bsc_endp, bsc);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800391 return;
392 }
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200393
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200394 /* free some stuff */
395 talloc_free(bsc_endp->transaction_id);
396 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800397 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200398
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200399 /*
400 * rewrite the information. In case the endpoint was deleted
401 * there should be nothing for us to rewrite so putting endp->rtp_port
402 * with the value of 0 should be no problem.
403 */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200404 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800405 bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200406
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200407 if (!output) {
408 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
409 return;
410 }
411
412 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
413 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
414 msgb_free(output);
415 }
416}
417
418int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
419{
420 /* we want to parse two strings */
421 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
422}
423
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800424uint32_t bsc_mgcp_extract_ci(const char *str)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200425{
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800426 unsigned int ci;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200427 char *res = strstr(str, "I: ");
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800428 if (!res) {
429 LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200430 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800431 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200432
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800433 if (sscanf(res, "I: %u", &ci) != 1) {
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800434 LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200435 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800436 }
437
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200438 return ci;
439}
440
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200441/* we need to replace some strings... */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200442struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200443{
444 static const char *ip_str = "c=IN IP4 ";
445 static const char *aud_str = "m=audio ";
446
447 char buf[128];
448 char *running, *token;
449 struct msgb *output;
450
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200451 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200452 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
453 return NULL;
454 }
455
456 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
457 if (!output) {
458 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
459 return NULL;
460 }
461
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200462 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200463 output->l2h = output->data;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200464 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200465 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200466 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200467
468 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
469 output->l3h = msgb_put(output, strlen(ip_str));
470 memcpy(output->l3h, ip_str, strlen(ip_str));
471 output->l3h = msgb_put(output, strlen(ip));
472 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200473
474 if (cr) {
475 output->l3h = msgb_put(output, 2);
476 output->l3h[0] = '\r';
477 output->l3h[1] = '\n';
478 } else {
479 output->l3h = msgb_put(output, 1);
480 output->l3h[0] = '\n';
481 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200482 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
483 int payload;
484 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
485 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
486 msgb_free(output);
487 return NULL;
488 }
489
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200490 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
491 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200492 buf[sizeof(buf)-1] = '\0';
493
494 output->l3h = msgb_put(output, strlen(buf));
495 memcpy(output->l3h, buf, strlen(buf));
496 } else {
497 output->l3h = msgb_put(output, len + 1);
498 memcpy(output->l3h, token, len);
499 output->l3h[len] = '\n';
500 }
501 }
502
503 return output;
504}
505
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200506static int mgcp_do_read(struct bsc_fd *fd)
507{
508 struct bsc_nat *nat;
509 struct msgb *msg, *resp;
510 int rc;
511
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200512 nat = fd->data;
513
514 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
515 if (rc <= 0) {
516 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
517 return -1;
518 }
519
520 nat->mgcp_msg[rc] = '\0';
521 nat->mgcp_length = rc;
522
523 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200524 if (!msg) {
525 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
526 return -1;
527 }
528
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200529 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200530 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200531 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
532 msgb_free(msg);
533
534 /* we do have a direct answer... e.g. AUEP */
535 if (resp) {
536 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
537 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
538 msgb_free(resp);
539 }
540 }
541
542 return 0;
543}
544
545static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
546{
547 int rc;
548
549 rc = write(bfd->fd, msg->data, msg->len);
550
551 if (rc != msg->len) {
552 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
553 return -1;
554 }
555
556 return rc;
557}
558
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800559int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200560{
561 int on;
562 struct sockaddr_in addr;
563
564 if (!nat->mgcp_cfg->call_agent_addr) {
565 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
566 return -1;
567 }
568
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200569 if (nat->mgcp_cfg->bts_ip) {
570 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
571 return -1;
572 }
573
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200574 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
575 if (nat->mgcp_queue.bfd.fd < 0) {
576 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
577 return -1;
578 }
579
580 on = 1;
581 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
582
583 addr.sin_family = AF_INET;
584 addr.sin_port = htons(nat->mgcp_cfg->source_port);
585 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
586
587 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
588 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
589 close(nat->mgcp_queue.bfd.fd);
590 nat->mgcp_queue.bfd.fd = -1;
591 return -1;
592 }
593
594 addr.sin_port = htons(2727);
595 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
596 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
597 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
598 nat->mgcp_cfg->call_agent_addr, errno);
599 close(nat->mgcp_queue.bfd.fd);
600 nat->mgcp_queue.bfd.fd = -1;
601 return -1;
602 }
603
604 write_queue_init(&nat->mgcp_queue, 10);
605 nat->mgcp_queue.bfd.when = BSC_FD_READ;
606 nat->mgcp_queue.bfd.data = nat;
607 nat->mgcp_queue.read_cb = mgcp_do_read;
608 nat->mgcp_queue.write_cb = mgcp_do_write;
609
610 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
611 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
612 close(nat->mgcp_queue.bfd.fd);
613 nat->mgcp_queue.bfd.fd = -1;
614 return -1;
615 }
616
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200617 /* some more MGCP config handling */
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000618 if (nat->mgcp_cfg->audio_name)
619 talloc_free(nat->mgcp_cfg->audio_name);
620 nat->mgcp_cfg->audio_name = NULL;
621
Holger Hans Peter Freyther4ad58502010-04-06 11:14:03 +0200622 nat->mgcp_cfg->audio_payload = -1;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200623 nat->mgcp_cfg->data = nat;
624 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freytherbba59342010-04-07 10:52:48 +0200625 nat->mgcp_cfg->force_realloc = 1;
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000626
627 if (nat->mgcp_cfg->bts_ip)
628 talloc_free(nat->mgcp_cfg->bts_ip);
Holger Hans Peter Freyther290d84e2010-04-09 18:51:01 +0200629 nat->mgcp_cfg->bts_ip = "";
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000630
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200631 nat->bsc_endpoints = talloc_zero_array(nat,
632 struct bsc_endpoint,
633 nat->mgcp_cfg->number_endpoints + 1);
634
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200635 return 0;
636}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200637
638void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
639{
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800640 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200641 int i;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800642
643 if (bsc->cfg)
644 ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
645
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200646 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
647 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
648
649 if (bsc_endp->bsc != bsc)
650 continue;
651
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800652 if (ctr)
653 rate_ctr_inc(ctr);
654
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800655 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200656 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
657 }
658}