blob: d814f0dc39fce46f279e1bcdfe276a658580ae80 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +08009 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080018 *
19 */
20
21#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080022#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080024#include <openbsc/debug.h>
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +020025#include <openbsc/ipaccess.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 Freythered500e32011-02-25 17:09:07 +010041static int bsc_init_endps_if_needed(struct bsc_connection *con)
42{
43 /* we have done that */
44 if (con->_endpoint_status)
45 return 0;
46
47 /* we have no config... */
48 if (!con->cfg)
49 return -1;
50
51 con->_endpoint_status = talloc_zero_array(con, char,
52 (32 * con->cfg->number_multiplexes) + 1);
53 return con->_endpoint_status == NULL;
54}
55
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080056static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con)
57{
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010058 const int number_endpoints = 31 * bsc->cfg->number_multiplexes;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080059 int i;
60
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010061 for (i = 1; i <= number_endpoints; ++i) {
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080062 int endpoint = (bsc->last_endpoint + i) % number_endpoints;
63 if (endpoint == 0)
64 endpoint = 1;
65
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +010066 if (bsc->_endpoint_status[endpoint] == 0) {
67 bsc->_endpoint_status[endpoint] = 1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +080068 con->bsc_endp = endpoint;
69 bsc->last_endpoint = endpoint;
70 return 0;
71 }
72 }
73
74 return -1;
75}
76
77static uint16_t create_cic(int endpoint)
78{
79 int timeslot, multiplex;
80
81 mgcp_endpoint_to_timeslot(endpoint, &multiplex, &timeslot);
82 return (multiplex << 5) | (timeslot & 0x1f);
83}
84
85int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080086{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +080087 struct sccp_connections *mcon;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080088 struct tlv_parsed tp;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +080089 uint16_t cic;
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080090 uint8_t timeslot;
91 uint8_t multiplex;
Holger Hans Peter Freyther14069772010-11-04 17:14:41 +010092 unsigned int endp;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +080093
94 if (!msg->l3h) {
95 LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n");
96 return -1;
97 }
98
99 if (msgb_l3len(msg) < 3) {
100 LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n");
101 return -1;
102 }
103
104 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
105 if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) {
106 LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n");
107 return -1;
108 }
109
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800110 cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE));
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800111 timeslot = cic & 0x1f;
112 multiplex = (cic & ~0x1f) >> 5;
113
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800114
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800115 endp = mgcp_timeslot_to_endpoint(multiplex, timeslot);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800116
Holger Hans Peter Freyther14069772010-11-04 17:14:41 +0100117 if (endp >= con->bsc->nat->mgcp_cfg->number_endpoints) {
118 LOGP(DNAT, LOGL_ERROR,
119 "MSC attempted to assign bad endpoint 0x%x\n",
120 endp);
121 return -1;
122 }
123
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800124 /* find stale connections using that endpoint */
125 llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800126 if (mcon->msc_endp == endp) {
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800127 LOGP(DNAT, LOGL_ERROR,
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800128 "Endpoint %d was assigned to 0x%x and now 0x%x\n",
129 endp,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800130 sccp_src_ref_to_int(&mcon->patched_ref),
131 sccp_src_ref_to_int(&con->patched_ref));
132 bsc_mgcp_dlcx(mcon);
133 }
134 }
135
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800136 con->msc_endp = endp;
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100137 if (bsc_init_endps_if_needed(con->bsc) != 0)
138 return -1;
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800139 if (bsc_assign_endpoint(con->bsc, con) != 0)
140 return -1;
141
142 /*
143 * now patch the message for the new CIC...
144 * still assumed to be one multiplex only
145 */
146 cic = htons(create_cic(con->bsc_endp));
147 memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE),
148 &cic, sizeof(cic));
149
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800150 return 0;
151}
152
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800153static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200154{
155 if (nat->bsc_endpoints[i].transaction_id) {
156 talloc_free(nat->bsc_endpoints[i].transaction_id);
157 nat->bsc_endpoints[i].transaction_id = NULL;
158 }
159
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800160 nat->bsc_endpoints[i].transaction_state = 0;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200161 nat->bsc_endpoints[i].bsc = NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200162}
163
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200164void bsc_mgcp_free_endpoints(struct bsc_nat *nat)
165{
166 int i;
167
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800168 for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i){
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200169 bsc_mgcp_free_endpoint(nat, i);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800170 mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]);
171 }
Holger Hans Peter Freyther241e1302010-03-31 09:16:56 +0200172}
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200173
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800174/* send a MDCX where we do not want a response */
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800175static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp)
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800176{
177 char buf[2096];
178 int len;
179
180 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherf42f45b2010-04-22 13:06:24 +0800181 "MDCX 23 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800182 "Z: noanswer\r\n"
183 "\r\n"
184 "c=IN IP4 %s\r\n"
185 "m=audio %d RTP/AVP 255\r\n",
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800186 port,
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800187 bsc->nat->mgcp_cfg->source_addr,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800188 endp->bts_end.local_port);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800189 if (len < 0) {
190 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
191 return;
192 }
Holger Hans Peter Freytherd38aa452010-08-30 11:58:49 +0800193
194 #warning "The MDCX is not send to the BSC. It should"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800195}
196
197static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint)
198{
199 char buf[2096];
200 int len;
201
202 len = snprintf(buf, sizeof(buf),
Holger Hans Peter Freytherbf812fa2010-08-30 12:01:36 +0800203 "DLCX 26 %x@mgw MGCP 1.0\r\n"
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800204 "Z: noanswer\r\n", endpoint);
205 if (len < 0) {
206 LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n");
207 return;
208 }
209
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800210 bsc_write_mgcp(bsc, (uint8_t *) buf, len);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800211}
212
213void bsc_mgcp_init(struct sccp_connections *con)
214{
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800215 con->msc_endp = -1;
216 con->bsc_endp = -1;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800217}
218
219void bsc_mgcp_dlcx(struct sccp_connections *con)
220{
221 /* send a DLCX down the stream */
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100222 if (con->bsc_endp != -1 && con->bsc->_endpoint_status) {
223 if (con->bsc->_endpoint_status[con->bsc_endp] != 1)
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800224 LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp);
Holger Hans Peter Freythered500e32011-02-25 17:09:07 +0100225 con->bsc->_endpoint_status[con->bsc_endp] = 0;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800226 bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp);
227 bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800228 }
229
230 bsc_mgcp_init(con);
231}
232
233
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800234struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200235{
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800236 struct sccp_connections *con = NULL;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200237 struct sccp_connections *sccp;
238
239 llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800240 if (sccp->msc_endp == -1)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200241 continue;
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800242 if (sccp->msc_endp != endpoint)
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200243 continue;
244
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800245 con = sccp;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200246 }
247
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800248 if (con)
249 return con;
Holger Hans Peter Freythereb52e892010-04-18 02:14:45 +0800250
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200251 LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n");
252 return NULL;
253}
254
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200255int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id)
256{
257 struct bsc_nat *nat;
258 struct bsc_endpoint *bsc_endp;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800259 struct sccp_connections *sccp;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200260 struct mgcp_endpoint *mgcp_endp;
261 struct msgb *bsc_msg;
262
263 nat = cfg->data;
264 bsc_endp = &nat->bsc_endpoints[endpoint];
265 mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint];
266
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800267 if (bsc_endp->transaction_id) {
268 LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n",
269 endpoint, bsc_endp->transaction_id);
270 talloc_free(bsc_endp->transaction_id);
271 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800272 bsc_endp->transaction_state = 0;
Holger Hans Peter Freytherfef76122010-04-24 21:05:18 +0800273 }
274 bsc_endp->bsc = NULL;
275
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800276 sccp = bsc_mgcp_find_con(nat, endpoint);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200277
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800278 if (!sccp) {
Holger Hans Peter Freyther3d194d92010-04-24 21:02:01 +0800279 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 +0200280
281 switch (state) {
282 case MGCP_ENDP_CRCX:
283 return MGCP_POLICY_REJECT;
284 break;
285 case MGCP_ENDP_DLCX:
286 return MGCP_POLICY_CONT;
287 break;
288 case MGCP_ENDP_MDCX:
289 return MGCP_POLICY_CONT;
290 break;
291 default:
292 LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state);
293 return MGCP_POLICY_CONT;
294 break;
295 }
296 }
297
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200298 /* we need to generate a new and patched message */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800299 bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800300 nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port);
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200301 if (!bsc_msg) {
302 LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n");
303 return MGCP_POLICY_CONT;
304 }
305
306
Holger Hans Peter Freytherb3e0a032010-04-04 18:11:49 +0200307 bsc_endp->transaction_id = talloc_strdup(nat, transaction_id);
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800308 bsc_endp->transaction_state = state;
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800309 bsc_endp->bsc = sccp->bsc;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200310
311 /* we need to update some bits */
312 if (state == MGCP_ENDP_CRCX) {
313 struct sockaddr_in sock;
314 socklen_t len = sizeof(sock);
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800315 if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) {
Holger Hans Peter Freyther92febd32010-04-06 11:17:07 +0200316 LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n",
317 errno, strerror(errno));
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200318 } else {
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +0800319 mgcp_endp->bts_end.addr = sock.sin_addr;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200320 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200321
Holger Hans Peter Freythere66cac32010-08-05 01:50:44 +0800322 /* send the message and a fake MDCX to force sending of a dummy packet */
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100323 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther601180f2010-08-29 23:40:33 +0800324 bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800325 return MGCP_POLICY_DEFER;
326 } else if (state == MGCP_ENDP_DLCX) {
327 /* we will free the endpoint now and send a DLCX to the BSC */
Holger Hans Peter Freyther3a347f02010-05-01 10:31:53 +0800328 msgb_free(bsc_msg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800329 bsc_mgcp_dlcx(sccp);
330 return MGCP_POLICY_CONT;
331 } else {
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100332 bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800333 return MGCP_POLICY_DEFER;
334 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200335}
336
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200337/*
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800338 * We do have a failure, free data downstream..
339 */
340static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp,
341 struct bsc_connection *bsc)
342{
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800343 LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n",
344 ENDPOINT_NUMBER(endp), bsc_endp->transaction_state);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800345
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800346 /* if a CRCX failed... send a DLCX down the stream */
347 if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) {
348 struct sccp_connections *con;
349 con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp));
350 if (!con) {
351 LOGP(DMGCP, LOGL_ERROR,
352 "No SCCP connection for endp 0x%x\n",
353 ENDPOINT_NUMBER(endp));
354 } else {
355 if (con->bsc == bsc) {
Holger Hans Peter Freyther8574dcf2010-08-29 22:39:07 +0800356 bsc_mgcp_send_dlcx(bsc, con->bsc_endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800357 } else {
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800358 LOGP(DMGCP, LOGL_ERROR,
359 "Endpoint belongs to a different BSC\n");
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800360 }
361 }
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800362 }
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800363
Holger Hans Peter Freytherc021fbe2010-08-28 16:46:27 +0800364 bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp));
365 mgcp_free_endp(endp);
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800366}
367
368/*
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200369 * We have received a msg from the BSC. We will see if we know
370 * this transaction and if it belongs to the BSC. Then we will
371 * need to patch the content to point to the local network and we
372 * need to update the I: that was assigned by the BSS.
373 */
374void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg)
375{
376 struct msgb *output;
377 struct bsc_endpoint *bsc_endp = NULL;
378 struct mgcp_endpoint *endp = NULL;
Holger Hans Peter Freytherd2dd6e82010-04-06 11:06:11 +0200379 int i, code;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200380 char transaction_id[60];
381
382 /* Some assumption that our buffer is big enough.. and null terminate */
383 if (msgb_l2len(msg) > 2000) {
384 LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n");
385 return;
386 }
387
388 msg->l2h[msgb_l2len(msg)] = '\0';
389
390 if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) {
391 LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n");
392 return;
393 }
394
395 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
396 if (bsc->nat->bsc_endpoints[i].bsc != bsc)
397 continue;
Holger Hans Peter Freyther3f7c7d02010-04-05 18:00:05 +0200398 /* no one listening? a bug? */
399 if (!bsc->nat->bsc_endpoints[i].transaction_id)
400 continue;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200401 if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0)
402 continue;
403
404 endp = &bsc->nat->mgcp_cfg->endpoints[i];
405 bsc_endp = &bsc->nat->bsc_endpoints[i];
406 break;
407 }
408
409 if (!bsc_endp) {
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200410 LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n",
411 transaction_id, (const char *) msg->l2h);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200412 return;
413 }
414
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800415 endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800416 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther7d476012010-08-06 19:16:34 +0800417 free_chan_downstream(endp, bsc_endp, bsc);
Holger Hans Peter Freytherb84b5f62010-08-06 08:34:46 +0800418 return;
419 }
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200420
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200421 /* free some stuff */
422 talloc_free(bsc_endp->transaction_id);
423 bsc_endp->transaction_id = NULL;
Holger Hans Peter Freyther5b2726e2010-08-06 09:05:05 +0800424 bsc_endp->transaction_state = 0;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200425
Holger Hans Peter Freytherdd16b422010-04-07 09:53:54 +0200426 /*
427 * rewrite the information. In case the endpoint was deleted
428 * there should be nothing for us to rewrite so putting endp->rtp_port
429 * with the value of 0 should be no problem.
430 */
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800431 output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1,
Holger Hans Peter Freyther58ff2192010-08-05 03:08:35 +0800432 bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port);
Holger Hans Peter Freyther5cc94fb2010-04-05 22:56:49 +0200433
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200434 if (!output) {
435 LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n");
436 return;
437 }
438
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800439 if (write_queue_enqueue(&bsc->nat->mgcp_cfg->gw_fd, output) != 0) {
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200440 LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n");
441 msgb_free(output);
442 }
443}
444
445int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60])
446{
447 /* we want to parse two strings */
448 return sscanf(str, "%3d %59s\n", code, transaction) != 2;
449}
450
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800451uint32_t bsc_mgcp_extract_ci(const char *str)
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200452{
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800453 unsigned int ci;
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200454 char *res = strstr(str, "I: ");
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800455 if (!res) {
456 LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200457 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800458 }
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200459
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800460 if (sscanf(res, "I: %u", &ci) != 1) {
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800461 LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200462 return CI_UNUSED;
Holger Hans Peter Freyther9c31cfc2010-08-06 08:19:05 +0800463 }
464
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200465 return ci;
466}
467
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800468static void patch_mgcp(struct msgb *output, const char *op, const char *tok,
469 int endp, int len, int cr)
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200470{
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800471 int slen;
472 int ret;
473 char buf[40];
474
475 buf[0] = buf[39] = '\0';
476 ret = sscanf(tok, "%*s %s", buf);
477
478 slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s",
479 op, buf, endp, cr ? "\r\n" : "\n");
480 output->l3h = msgb_put(output, slen);
481}
482
483/* we need to replace some strings... */
484struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port)
485{
486 static const char *crcx_str = "CRCX ";
487 static const char *dlcx_str = "DLCX ";
488 static const char *mdcx_str = "MDCX ";
489
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200490 static const char *ip_str = "c=IN IP4 ";
491 static const char *aud_str = "m=audio ";
492
493 char buf[128];
494 char *running, *token;
495 struct msgb *output;
496
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200497 if (length > 4096 - 128) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200498 LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n");
499 return NULL;
500 }
501
502 output = msgb_alloc_headroom(4096, 128, "MGCP rewritten");
503 if (!output) {
504 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n");
505 return NULL;
506 }
507
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200508 running = input;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200509 output->l2h = output->data;
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800510 output->l3h = output->l2h;
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200511 for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200512 int len = strlen(token);
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200513 int cr = len > 0 && token[len - 1] == '\r';
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200514
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800515 if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) {
516 patch_mgcp(output, "CRCX", token, endpoint, len, cr);
517 } else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) {
518 patch_mgcp(output, "DLCX", token, endpoint, len, cr);
519 } else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) {
520 patch_mgcp(output, "MDCX", token, endpoint, len, cr);
521 } else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) {
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200522 output->l3h = msgb_put(output, strlen(ip_str));
523 memcpy(output->l3h, ip_str, strlen(ip_str));
524 output->l3h = msgb_put(output, strlen(ip));
525 memcpy(output->l3h, ip, strlen(ip));
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200526
527 if (cr) {
528 output->l3h = msgb_put(output, 2);
529 output->l3h[0] = '\r';
530 output->l3h[1] = '\n';
531 } else {
532 output->l3h = msgb_put(output, 1);
533 output->l3h[0] = '\n';
534 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200535 } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) {
536 int payload;
537 if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) {
538 LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n");
539 msgb_free(output);
540 return NULL;
541 }
542
Holger Hans Peter Freyther9e5300a2010-04-04 19:34:44 +0200543 snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s",
544 port, payload, cr ? "\r\n" : "\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200545 buf[sizeof(buf)-1] = '\0';
546
547 output->l3h = msgb_put(output, strlen(buf));
548 memcpy(output->l3h, buf, strlen(buf));
549 } else {
550 output->l3h = msgb_put(output, len + 1);
551 memcpy(output->l3h, token, len);
552 output->l3h[len] = '\n';
553 }
554 }
555
556 return output;
557}
558
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200559static int mgcp_do_read(struct bsc_fd *fd)
560{
561 struct bsc_nat *nat;
562 struct msgb *msg, *resp;
563 int rc;
564
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200565 nat = fd->data;
566
567 rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1);
568 if (rc <= 0) {
569 LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno);
570 return -1;
571 }
572
573 nat->mgcp_msg[rc] = '\0';
574 nat->mgcp_length = rc;
575
576 msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read");
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200577 if (!msg) {
578 LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n");
579 return -1;
580 }
581
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200582 msg->l2h = msgb_put(msg, rc);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200583 memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200584 resp = mgcp_handle_message(nat->mgcp_cfg, msg);
585 msgb_free(msg);
586
587 /* we do have a direct answer... e.g. AUEP */
588 if (resp) {
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800589 if (write_queue_enqueue(&nat->mgcp_cfg->gw_fd, resp) != 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200590 LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n");
591 msgb_free(resp);
592 }
593 }
594
595 return 0;
596}
597
598static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg)
599{
600 int rc;
601
602 rc = write(bfd->fd, msg->data, msg->len);
603
604 if (rc != msg->len) {
605 LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n");
606 return -1;
607 }
608
609 return rc;
610}
611
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800612int bsc_mgcp_nat_init(struct bsc_nat *nat)
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200613{
614 int on;
615 struct sockaddr_in addr;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800616 struct mgcp_config *cfg = nat->mgcp_cfg;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200617
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800618 if (!cfg->call_agent_addr) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200619 LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n");
620 return -1;
621 }
622
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800623 if (cfg->bts_ip) {
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200624 LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n");
625 return -1;
626 }
627
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800628 cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
629 if (cfg->gw_fd.bfd.fd < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200630 LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno);
631 return -1;
632 }
633
634 on = 1;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800635 setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200636
637 addr.sin_family = AF_INET;
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800638 addr.sin_port = htons(cfg->source_port);
639 inet_aton(cfg->source_addr, &addr.sin_addr);
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200640
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800641 if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200642 LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800643 close(cfg->gw_fd.bfd.fd);
644 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200645 return -1;
646 }
647
648 addr.sin_port = htons(2727);
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800649 inet_aton(cfg->call_agent_addr, &addr.sin_addr);
650 if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200651 LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800652 cfg->call_agent_addr, errno);
653 close(cfg->gw_fd.bfd.fd);
654 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200655 return -1;
656 }
657
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800658 write_queue_init(&cfg->gw_fd, 10);
659 cfg->gw_fd.bfd.when = BSC_FD_READ;
660 cfg->gw_fd.bfd.data = nat;
661 cfg->gw_fd.read_cb = mgcp_do_read;
662 cfg->gw_fd.write_cb = mgcp_do_write;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200663
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800664 if (bsc_register_fd(&cfg->gw_fd.bfd) != 0) {
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200665 LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n");
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800666 close(cfg->gw_fd.bfd.fd);
667 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200668 return -1;
669 }
670
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200671 /* some more MGCP config handling */
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800672 cfg->data = nat;
673 cfg->policy_cb = bsc_mgcp_policy_cb;
674 cfg->force_realloc = 1;
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000675
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800676 if (cfg->bts_ip)
677 talloc_free(cfg->bts_ip);
678 cfg->bts_ip = "";
Holger Hans Peter Freytherd5e6c232010-08-05 10:08:36 +0000679
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200680 nat->bsc_endpoints = talloc_zero_array(nat,
681 struct bsc_endpoint,
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800682 cfg->number_endpoints + 1);
Holger Hans Peter Freyther3c792142010-09-19 04:34:04 +0800683 if (!nat->bsc_endpoints) {
684 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n");
Holger Hans Peter Freyther249d69a2010-09-18 21:13:54 +0800685 close(cfg->gw_fd.bfd.fd);
686 cfg->gw_fd.bfd.fd = -1;
Holger Hans Peter Freyther3c792142010-09-19 04:34:04 +0800687 return -1;
688 }
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200689
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +0800690 if (mgcp_reset_transcoder(cfg) < 0) {
691 LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n");
692 talloc_free(nat->bsc_endpoints);
693 nat->bsc_endpoints = NULL;
694 close(cfg->gw_fd.bfd.fd);
695 cfg->gw_fd.bfd.fd = -1;
696 return -1;
697 }
698
Holger Hans Peter Freythera7f80182010-03-31 13:02:22 +0200699 return 0;
700}
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200701
702void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc)
703{
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800704 struct rate_ctr *ctr = NULL;
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200705 int i;
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800706
707 if (bsc->cfg)
708 ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS];
709
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200710 for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) {
711 struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i];
712
713 if (bsc_endp->bsc != bsc)
714 continue;
715
Holger Hans Peter Freyther8330c1c2010-06-17 18:29:42 +0800716 if (ctr)
717 rate_ctr_inc(ctr);
718
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800719 bsc_mgcp_free_endpoint(bsc->nat, i);
Holger Hans Peter Freyther26a43892010-04-05 23:09:27 +0200720 mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]);
721 }
722}