blob: 8ed9301450ba95d1d39bb75990cc6100f3aa1c29 [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 Freyther465313e2010-06-15 18:49:53 +080041int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg)
42{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080043 struct sccp_connections *mcon;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080044 struct tlv_parsed tp;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +080045 uint16_t cic;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080046 uint8_t timeslot;
47 uint8_t multiplex;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +080048 int endp;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080049
50 if (!msg->l3h) {
51 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
52 return -1;
53 }
54
55 if (msgb_l3len(msg) < 3) {
56 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
57 return -1;
58 }
59
60 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
61 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
62 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
63 return -1;
64 }
65
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +080066 cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080067 timeslot = cic & 0x1f;
68 multiplex = (cic & ~0x1f) >> 5;
69
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080070
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +080071 endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080072
73 /* find stale connections using that endpoint */
74 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +080075 if (mcon->msc_endp == endp) {
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080076 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +080077 "Endpoint %d was assigned to 0x%x and now 0x%x\n",
78 endp,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080079 sccp_src_ref_to_int(&mcon->patched_ref),
80 sccp_src_ref_to_int(&con->patched_ref));
81 bsc_mgcp_dlcx(mcon);
82 }
83 }
84
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +080085 con->msc_endp = endp;
86 con->bsc_endp = endp;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080087 return 0;
88}
89
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080090static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020091{
92 if (nat->bsc_endpoints[i].transaction_id) {
93 talloc_free(nat->bsc_endpoints[i].transaction_id);
94 nat->bsc_endpoints[i].transaction_id = NULL;
95 }
96
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +080097 nat->bsc_endpoints[i].transaction_state = 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020098 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020099}
100
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200101void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
102{
103 int i;
104
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800105 for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i){
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200106 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800107 mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]);
108 }
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200109}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200110
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800111/* send a MDCX where we do not want a response */
112static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, struct mgcp_endpoint *endp)
113{
114 char buf[2096];
115 int len;
116
117 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800118 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800119 "Z: noanswer\r\n"
120 "\r\n"
121 "c=IN IP4 %s\r\n"
122 "m=audio %d RTP/AVP 255\r\n",
123 ENDPOINT_NUMBER(endp),
124 bsc->nat->mgcp_cfg->source_addr,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800125 endp->bts_end.local_port);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800126 if (len < 0) {
127 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
128 return;
129 }
130}
131
132static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
133{
134 char buf[2096];
135 int len;
136
137 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800138 "DLCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800139 "Z: noanswer\r\n", endpoint);
140 if (len < 0) {
141 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
142 return;
143 }
144
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800145 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800146}
147
148void bsc_mgcp_init(struct sccp_connections *con)
149{
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800150 con->msc_endp = -1;
151 con->bsc_endp = -1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800152}
153
154void bsc_mgcp_dlcx(struct sccp_connections *con)
155{
156 /* send a DLCX down the stream */
Holger Hans Peter Freyther39cd32e2010-08-28 18:11:07 +0800157 if (con->bsc_endp != -1) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800158 bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
159 bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800160 }
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) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800172 if (sccp->msc_endp == -1)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200173 continue;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800174 if (sccp->msc_endp != endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200175 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;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800204 bsc_endp->transaction_state = 0;
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800205 }
206 bsc_endp->bsc = NULL;
207
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800208 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200209
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800210 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800211 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 +0200212
213 switch (state) {
214 case MGCP_ENDP_CRCX:
215 return MGCP_POLICY_REJECT;
216 break;
217 case MGCP_ENDP_DLCX:
218 return MGCP_POLICY_CONT;
219 break;
220 case MGCP_ENDP_MDCX:
221 return MGCP_POLICY_CONT;
222 break;
223 default:
224 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
225 return MGCP_POLICY_CONT;
226 break;
227 }
228 }
229
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200230 /* we need to generate a new and patched message */
231 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800232 nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200233 if (!bsc_msg) {
234 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
235 return MGCP_POLICY_CONT;
236 }
237
238
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200239 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800240 bsc_endp->transaction_state = state;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800241 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200242
243 /* we need to update some bits */
244 if (state == MGCP_ENDP_CRCX) {
245 struct sockaddr_in sock;
246 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800247 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200248 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
249 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200250 } else {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800251 mgcp_endp->bts_end.addr = sock.sin_addr;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200252 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200253
Holger Hans Peter Freythere66cac32010-08-05 01:50:44 +0800254 /* send the message and a fake MDCX to force sending of a dummy packet */
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800255 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
256 bsc_mgcp_send_mdcx(sccp->bsc, mgcp_endp);
257 return MGCP_POLICY_DEFER;
258 } else if (state == MGCP_ENDP_DLCX) {
259 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freyther3a347f02010-05-01 10:31:53 +0800260 msgb_free(bsc_msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800261 bsc_mgcp_dlcx(sccp);
262 return MGCP_POLICY_CONT;
263 } else {
264 bsc_write(sccp->bsc, bsc_msg, NAT_IPAC_PROTO_MGCP);
265 return MGCP_POLICY_DEFER;
266 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200267}
268
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200269/*
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800270 * We do have a failure, free data downstream..
271 */
272static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
273 struct bsc_connection *bsc)
274{
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800275 LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
276 ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800277
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800278 /* if a CRCX failed... send a DLCX down the stream */
279 if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
280 struct sccp_connections *con;
281 con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
282 if (!con) {
283 LOGP(DMGCP, LOGL_ERROR,
284 "No SCCP connection for endp 0x%x\n",
285 ENDPOINT_NUMBER(endp));
286 } else {
287 if (con->bsc == bsc) {
288 bsc_mgcp_send_dlcx(bsc, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800289 } else {
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800290 LOGP(DMGCP, LOGL_ERROR,
291 "Endpoint belongs to a different BSC\n");
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800292 }
293 }
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800294 }
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800295
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800296 bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
297 mgcp_free_endp(endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800298}
299
300/*
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200301 * We have received a msg from the BSC. We will see if we know
302 * this transaction and if it belongs to the BSC. Then we will
303 * need to patch the content to point to the local network and we
304 * need to update the I: that was assigned by the BSS.
305 */
306void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
307{
308 struct msgb *output;
309 struct bsc_endpoint *bsc_endp = NULL;
310 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200311 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200312 char transaction_id[60];
313
314 /* Some assumption that our buffer is big enough.. and null terminate */
315 if (msgb_l2len(msg) > 2000) {
316 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
317 return;
318 }
319
320 msg->l2h[msgb_l2len(msg)] = '\0';
321
322 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
323 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
324 return;
325 }
326
327 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
328 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
329 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200330 /* no one listening? a bug? */
331 if (!bsc->nat->bsc_endpoints[i].transaction_id)
332 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200333 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
334 continue;
335
336 endp = &bsc->nat->mgcp_cfg->endpoints[i];
337 bsc_endp = &bsc->nat->bsc_endpoints[i];
338 break;
339 }
340
341 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200342 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
343 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200344 return;
345 }
346
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800347 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800348 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800349 free_chan_downstream(endp, bsc_endp, bsc);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800350 return;
351 }
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200352
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200353 /* free some stuff */
354 talloc_free(bsc_endp->transaction_id);
355 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800356 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200357
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200358 /*
359 * rewrite the information. In case the endpoint was deleted
360 * there should be nothing for us to rewrite so putting endp->rtp_port
361 * with the value of 0 should be no problem.
362 */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200363 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg),
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800364 bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200365
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200366 if (!output) {
367 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
368 return;
369 }
370
371 if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) {
372 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
373 msgb_free(output);
374 }
375}
376
377int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
378{
379 /* we want to parse two strings */
380 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
381}
382
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800383uint32_t bsc_mgcp_extract_ci(const char *str)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200384{
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800385 unsigned int ci;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200386 char *res = strstr(str, "I: ");
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800387 if (!res) {
388 LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200389 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800390 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200391
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800392 if (sscanf(res, "I: %u", &ci) != 1) {
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800393 LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200394 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800395 }
396
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200397 return ci;
398}
399
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200400/* we need to replace some strings... */
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200401struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200402{
403 static const char *ip_str = "c=IN IP4 ";
404 static const char *aud_str = "m=audio ";
405
406 char buf[128];
407 char *running, *token;
408 struct msgb *output;
409
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200410 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200411 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
412 return NULL;
413 }
414
415 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
416 if (!output) {
417 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
418 return NULL;
419 }
420
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200421 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200422 output->l2h = output->data;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200423 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200424 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200425 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200426
427 if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
428 output->l3h = msgb_put(output, strlen(ip_str));
429 memcpy(output->l3h, ip_str, strlen(ip_str));
430 output->l3h = msgb_put(output, strlen(ip));
431 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200432
433 if (cr) {
434 output->l3h = msgb_put(output, 2);
435 output->l3h[0] = '\r';
436 output->l3h[1] = '\n';
437 } else {
438 output->l3h = msgb_put(output, 1);
439 output->l3h[0] = '\n';
440 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200441 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
442 int payload;
443 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
444 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
445 msgb_free(output);
446 return NULL;
447 }
448
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200449 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
450 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200451 buf[sizeof(buf)-1] = '\0';
452
453 output->l3h = msgb_put(output, strlen(buf));
454 memcpy(output->l3h, buf, strlen(buf));
455 } else {
456 output->l3h = msgb_put(output, len + 1);
457 memcpy(output->l3h, token, len);
458 output->l3h[len] = '\n';
459 }
460 }
461
462 return output;
463}
464
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200465static int mgcp_do_read(struct bsc_fd *fd)
466{
467 struct bsc_nat *nat;
468 struct msgb *msg, *resp;
469 int rc;
470
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200471 nat = fd->data;
472
473 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
474 if (rc <= 0) {
475 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
476 return -1;
477 }
478
479 nat->mgcp_msg[rc] = '\0';
480 nat->mgcp_length = rc;
481
482 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200483 if (!msg) {
484 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
485 return -1;
486 }
487
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200488 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200489 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200490 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
491 msgb_free(msg);
492
493 /* we do have a direct answer... e.g. AUEP */
494 if (resp) {
495 if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) {
496 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
497 msgb_free(resp);
498 }
499 }
500
501 return 0;
502}
503
504static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
505{
506 int rc;
507
508 rc = write(bfd->fd, msg->data, msg->len);
509
510 if (rc != msg->len) {
511 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
512 return -1;
513 }
514
515 return rc;
516}
517
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800518int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200519{
520 int on;
521 struct sockaddr_in addr;
522
523 if (!nat->mgcp_cfg->call_agent_addr) {
524 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
525 return -1;
526 }
527
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200528 if (nat->mgcp_cfg->bts_ip) {
529 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
530 return -1;
531 }
532
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200533 nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
534 if (nat->mgcp_queue.bfd.fd < 0) {
535 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
536 return -1;
537 }
538
539 on = 1;
540 setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
541
542 addr.sin_family = AF_INET;
543 addr.sin_port = htons(nat->mgcp_cfg->source_port);
544 inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr);
545
546 if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
547 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
548 close(nat->mgcp_queue.bfd.fd);
549 nat->mgcp_queue.bfd.fd = -1;
550 return -1;
551 }
552
553 addr.sin_port = htons(2727);
554 inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr);
555 if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
556 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
557 nat->mgcp_cfg->call_agent_addr, errno);
558 close(nat->mgcp_queue.bfd.fd);
559 nat->mgcp_queue.bfd.fd = -1;
560 return -1;
561 }
562
563 write_queue_init(&nat->mgcp_queue, 10);
564 nat->mgcp_queue.bfd.when = BSC_FD_READ;
565 nat->mgcp_queue.bfd.data = nat;
566 nat->mgcp_queue.read_cb = mgcp_do_read;
567 nat->mgcp_queue.write_cb = mgcp_do_write;
568
569 if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) {
570 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
571 close(nat->mgcp_queue.bfd.fd);
572 nat->mgcp_queue.bfd.fd = -1;
573 return -1;
574 }
575
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200576 /* some more MGCP config handling */
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000577 if (nat->mgcp_cfg->audio_name)
578 talloc_free(nat->mgcp_cfg->audio_name);
579 nat->mgcp_cfg->audio_name = NULL;
580
Holger Hans Peter Freyther4ad58502010-04-06 11:14:03 +0200581 nat->mgcp_cfg->audio_payload = -1;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200582 nat->mgcp_cfg->data = nat;
583 nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb;
Holger Hans Peter Freytherbba59342010-04-07 10:52:48 +0200584 nat->mgcp_cfg->force_realloc = 1;
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000585
586 if (nat->mgcp_cfg->bts_ip)
587 talloc_free(nat->mgcp_cfg->bts_ip);
Holger Hans Peter Freyther290d84e2010-04-09 18:51:01 +0200588 nat->mgcp_cfg->bts_ip = "";
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000589
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200590 nat->bsc_endpoints = talloc_zero_array(nat,
591 struct bsc_endpoint,
592 nat->mgcp_cfg->number_endpoints + 1);
593
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200594 return 0;
595}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200596
597void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
598{
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800599 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200600 int i;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800601
602 if (bsc->cfg)
603 ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
604
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200605 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
606 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
607
608 if (bsc_endp->bsc != bsc)
609 continue;
610
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800611 if (ctr)
612 rate_ctr_inc(ctr);
613
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800614 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200615 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
616 }
617}