blob: 3d0d93fc02bb8cce8d8a4eb2025a8eb41bf2edca [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* MSC related stuff... */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <bsc_data.h>
Holger Hans Peter Freyther0f349f22010-10-06 04:39:08 +080024#include <bsc_ussd.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080025#include <bss_patch.h>
26#include <bssap_sccp.h>
27#include <ipaccess.h>
28#include <mtp_data.h>
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080029#include <cellmgr_debug.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080030
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080031#include <osmocore/tlv.h>
32#include <osmocore/utils.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080033
34#include <arpa/inet.h>
35#include <sys/socket.h>
36#include <netinet/tcp.h>
37
38#include <fcntl.h>
39#include <unistd.h>
40#include <errno.h>
41#include <string.h>
42
43#define RECONNECT_TIME 10, 0
44#define NAT_MUX 0xfc
45
46static void msc_send_id_response(struct bsc_data *bsc);
47static void msc_send(struct bsc_data *bsc, struct msgb *msg, int proto);
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +080048static void msc_schedule_reconnect(struct bsc_data *bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080049
50void mtp_link_slta_recv(struct mtp_link *link)
51{
52 struct msgb *msg;
53 unsigned int sls;
54
55 while (!llist_empty(&link->pending_msgs)) {
56 msg = msgb_dequeue(&link->pending_msgs);
57 sls = (unsigned int) msg->l3h;
58
59 if (mtp_link_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg)) != 0)
60 LOGP(DMSC, LOGL_ERROR, "Could not forward SCCP message.\n");
61
62 msgb_free(msg);
63 }
64}
65
Holger Hans Peter Freythera4ca6d32010-10-06 04:51:44 +080066int send_or_queue_bsc_msg(struct mtp_link *link, int sls, struct msgb *msg)
67{
68 if (link->sltm_pending) {
69 LOGP(DMSC, LOGL_NOTICE, "Queueing msg for pending SLTM.\n");
70 msg->l3h = (uint8_t *) sls;
71 msgb_enqueue(&link->pending_msgs, msg);
72 return 1;
73 }
74
75 if (mtp_link_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg)) != 0)
76 LOGP(DMSC, LOGL_ERROR, "Could not forward SCCP message.\n");
77 return 0;
78}
79
80
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080081void msc_clear_queue(struct bsc_data *data)
82{
83 struct msgb *msg;
84
85 LOGP(DMSC, LOGL_NOTICE, "Clearing the MSC to BSC queue.\n");
86 while (!llist_empty(&data->link.the_link->pending_msgs)) {
87 msg = msgb_dequeue(&data->link.the_link->pending_msgs);
88 msgb_free(msg);
89 }
90}
91
Holger Hans Peter Freyther43d9eec2010-08-07 01:54:19 +080092void msc_close_connection(struct bsc_data *bsc)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080093{
94 struct bsc_fd *bfd = &bsc->msc_connection.bfd;
95
96 close(bfd->fd);
97 bsc_unregister_fd(bfd);
98 bfd->fd = -1;
Holger Hans Peter Freythere33d93c2010-08-07 02:40:35 +080099 bsc->msc_link_down = 1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800100 release_bsc_resources(bsc);
101 bsc_del_timer(&bsc->ping_timeout);
102 bsc_del_timer(&bsc->pong_timeout);
Holger Hans Peter Freyther98f6dd52010-10-01 20:30:53 +0800103 bsc_del_timer(&bsc->msc_timeout);
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +0800104 msc_schedule_reconnect(bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800105}
106
107static void msc_connect_timeout(void *_bsc_data)
108{
109 struct bsc_data *bsc_data = _bsc_data;
110
111 LOGP(DMSC, LOGL_ERROR, "Timeout on the MSC connection.\n");
Holger Hans Peter Freyther43d9eec2010-08-07 01:54:19 +0800112 msc_close_connection(bsc_data);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800113}
114
115static void msc_pong_timeout(void *_bsc_data)
116{
117 struct bsc_data *bsc_data = _bsc_data;
118 LOGP(DMSC, LOGL_ERROR, "MSC didn't respond to ping. Closing.\n");
Holger Hans Peter Freyther43d9eec2010-08-07 01:54:19 +0800119 msc_close_connection(bsc_data);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800120}
121
122static void send_ping(struct bsc_data *bsc)
123{
124 struct msgb *msg;
125
126 msg = msgb_alloc_headroom(4096, 128, "ping");
127 if (!msg) {
128 LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n");
129 return;
130 }
131
132 msg->l2h = msgb_put(msg, 1);
133 msg->l2h[0] = IPAC_MSGT_PING;
134
135 msc_send(bsc, msg, IPAC_PROTO_IPACCESS);
136}
137
138static void msc_ping_timeout(void *_bsc_data)
139{
140 struct bsc_data *bsc_data = _bsc_data;
141
142 if (bsc_data->ping_time < 0)
143 return;
144
145 send_ping(bsc_data);
146
147 /* send another ping in 20 seconds */
148 bsc_schedule_timer(&bsc_data->ping_timeout, bsc_data->ping_time, 0);
149
150 /* also start a pong timer */
151 bsc_schedule_timer(&bsc_data->pong_timeout, bsc_data->pong_time, 0);
152}
153
154/*
155 * callback with IP access data
156 */
157static int ipaccess_a_fd_cb(struct bsc_fd *bfd)
158{
159 int error;
160 struct ipaccess_head *hh;
161 struct mtp_link *link;
162 struct bsc_data *bsc;
163 struct msgb *msg;
164
165 msg = ipaccess_read_msg(bfd, &error);
166
167 bsc = (struct bsc_data *) bfd->data;
168
169 if (!msg) {
170 if (error == 0)
171 fprintf(stderr, "The connection to the MSC was lost, exiting\n");
172 else
173 fprintf(stderr, "Error in the IPA stream.\n");
174
Holger Hans Peter Freyther43d9eec2010-08-07 01:54:19 +0800175 msc_close_connection(bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800176 return -1;
177 }
178
179 LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
180
181 /* handle base message handling */
182 hh = (struct ipaccess_head *) msg->data;
183 ipaccess_rcvmsg_base(msg, bfd);
184
185 link = bsc->link.the_link;
186
187 /* initialize the networking. This includes sending a GSM08.08 message */
188 if (hh->proto == IPAC_PROTO_IPACCESS) {
189 if (bsc->first_contact) {
190 LOGP(DMSC, LOGL_NOTICE, "Connected to MSC. Sending reset.\n");
191 bsc_del_timer(&bsc->msc_timeout);
192 bsc->first_contact = 0;
Holger Hans Peter Freyther0c95c6a2010-08-07 02:37:43 +0800193 bsc->msc_link_down = 0;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800194 msc_send_reset(bsc);
195 }
196 if (msg->l2h[0] == IPAC_MSGT_ID_GET && bsc->token) {
197 msc_send_id_response(bsc);
198 } else if (msg->l2h[0] == IPAC_MSGT_PONG) {
199 bsc_del_timer(&bsc->pong_timeout);
200 }
201 } else if (hh->proto == IPAC_PROTO_SCCP) {
202 struct sccp_parse_result result;
203 int rc;
204 rc = bss_patch_filter_msg(msg, &result);
205
206 if (rc == BSS_FILTER_RESET_ACK) {
207 LOGP(DMSC, LOGL_NOTICE, "Filtering reset ack from the MSC\n");
208 } else if (rc == BSS_FILTER_RLSD) {
209 LOGP(DMSC, LOGL_DEBUG, "Filtering RLSD from the MSC\n");
210 update_con_state(rc, &result, msg, 1, 0);
211 } else if (rc == BSS_FILTER_RLC) {
212 /* if we receive this we have forwarded a RLSD to the network */
213 LOGP(DMSC, LOGL_ERROR, "RLC from the network. BAD!\n");
214 } else if (rc == BSS_FILTER_CLEAR_COMPL) {
215 LOGP(DMSC, LOGL_ERROR, "Clear Complete from the network.\n");
216 } else if (link->sccp_up) {
217 unsigned int sls;
218
219 update_con_state(rc, &result, msg, 1, 0);
220 sls = sls_for_src_ref(result.destination_local_reference);
221
Holger Hans Peter Freyther0f349f22010-10-06 04:39:08 +0800222 /* Check for Location Update Accept */
Holger Hans Peter Freyther52280692010-10-06 16:09:25 +0800223 bsc_ussd_handle_in_msg(bsc, &result, msg);
Holger Hans Peter Freyther0f349f22010-10-06 04:39:08 +0800224
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800225 /* patch a possible PC */
226 bss_rewrite_header_to_bsc(msg, link->opc, link->dpc);
227
228 /* we can not forward it right now */
Holger Hans Peter Freythera4ca6d32010-10-06 04:51:44 +0800229 if (send_or_queue_bsc_msg(link, sls, msg) == 1)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800230 return 0;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800231
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800232 }
233 } else if (hh->proto == NAT_MUX) {
234 mgcp_forward(bsc, msg->l2h, msgb_l2len(msg));
235 } else {
236 LOGP(DMSC, LOGL_ERROR, "Unknown IPA proto 0x%x\n", hh->proto);
237 }
238
239 msgb_free(msg);
240 return 0;
241}
242
243static int ipaccess_write_cb(struct bsc_fd *fd, struct msgb *msg)
244{
245 int rc;
246
247 LOGP(DMSC, LOGL_DEBUG, "Sending to MSC: %s\n", hexdump(msg->data, msg->len));
248 rc = write(fd->fd, msg->data, msg->len);
249 if (rc != msg->len)
250 LOGP(DMSC, LOGL_ERROR, "Could not write to MSC.\n");
251
252 return rc;
253}
254
255/* called in the case of a non blocking connect */
256static int msc_connection_connect(struct bsc_fd *fd, unsigned int what)
257{
258 int rc;
259 int val;
260 socklen_t len = sizeof(val);
261 struct bsc_data *bsc;
262
263 bsc = (struct bsc_data *) fd->data;
264
265 if (fd != &bsc->msc_connection.bfd) {
266 LOGP(DMSC, LOGL_ERROR, "This is only working with the MSC connection.\n");
267 return -1;
268 }
269
270 if ((what & BSC_FD_WRITE) == 0)
271 return -1;
272
273 /* check the socket state */
274 rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len);
275 if (rc != 0) {
276 LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n");
277 goto error;
278 }
279 if (val != 0) {
280 LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC.\n");
281 goto error;
282 }
283
284
285 /* go to full operation */
286 fd->cb = write_queue_bfd_cb;
287 fd->when = BSC_FD_READ;
288 if (!llist_empty(&bsc->msc_connection.msg_queue))
289 fd->when |= BSC_FD_WRITE;
290 return 0;
291
292error:
Holger Hans Peter Freyther62585392010-08-07 02:26:47 +0800293 msc_close_connection(bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800294 return -1;
295}
296
Holger Hans Peter Freyther377ba422010-10-01 20:37:40 +0800297static int setnonblocking(struct bsc_fd *fd)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800298{
299 int flags;
300
301 flags = fcntl(fd->fd, F_GETFL);
302 if (flags < 0) {
303 perror("fcntl get failed");
304 close(fd->fd);
305 fd->fd = -1;
Holger Hans Peter Freyther377ba422010-10-01 20:37:40 +0800306 return -1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800307 }
308
309 flags |= O_NONBLOCK;
310 flags = fcntl(fd->fd, F_SETFL, flags);
311 if (flags < 0) {
312 perror("fcntl get failed");
313 close(fd->fd);
314 fd->fd = -1;
Holger Hans Peter Freyther377ba422010-10-01 20:37:40 +0800315 return -1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800316 }
Holger Hans Peter Freyther377ba422010-10-01 20:37:40 +0800317
318 return 0;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800319}
320
321static int connect_to_msc(struct bsc_fd *fd, const char *ip, int port, int tos)
322{
323 struct sockaddr_in sin;
324 int on = 1, ret;
325
326 LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", ip, port);
327
328 fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
329
330 if (fd->fd < 0) {
331 perror("Creating TCP socket failed");
332 return fd->fd;
333 }
334
335 /* make it non blocking */
Holger Hans Peter Freyther377ba422010-10-01 20:37:40 +0800336 if (setnonblocking(fd) != 0)
337 return -1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800338
339 memset(&sin, 0, sizeof(sin));
340 sin.sin_family = AF_INET;
341 sin.sin_port = htons(port);
342 inet_aton(ip, &sin.sin_addr);
343
344 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
345 ret = setsockopt(fd->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
346 if (ret != 0)
347 LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
348 ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
349 if (ret != 0)
350 LOGP(DMSC, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno));
351
352 ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin));
353
354 if (ret == -1 && errno == EINPROGRESS) {
355 LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n");
356 fd->when = BSC_FD_WRITE;
357 fd->cb = msc_connection_connect;
358 } else if (ret < 0) {
359 perror("Connection failed");
360 close(fd->fd);
361 fd->fd = -1;
362 return ret;
363 } else {
364 fd->when = BSC_FD_READ;
365 fd->cb = write_queue_bfd_cb;
366 }
367
368 ret = bsc_register_fd(fd);
369 if (ret < 0) {
370 perror("Registering the fd failed");
371 close(fd->fd);
372 fd->fd = -1;
373 return ret;
374 }
375
376 return ret;
377}
378
379static void msc_reconnect(void *_data)
380{
381 int rc;
382 struct bsc_data *bsc = (struct bsc_data *) _data;
383
384 bsc_del_timer(&bsc->reconnect_timer);
385 bsc->first_contact = 1;
386
387 rc = connect_to_msc(&bsc->msc_connection.bfd, bsc->msc_address, 5000, bsc->msc_ip_dscp);
388 if (rc < 0) {
389 fprintf(stderr, "Opening the MSC connection failed. Trying again\n");
390 bsc_schedule_timer(&bsc->reconnect_timer, RECONNECT_TIME);
391 return;
392 }
393
394 bsc->msc_timeout.cb = msc_connect_timeout;
395 bsc->msc_timeout.data = bsc;
396 bsc_schedule_timer(&bsc->msc_timeout, bsc->msc_time, 0);
397}
398
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +0800399static void msc_schedule_reconnect(struct bsc_data *bsc)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800400{
401 bsc_schedule_timer(&bsc->reconnect_timer, RECONNECT_TIME);
402}
403
404/*
405 * mgcp forwarding is below
406 */
407static int mgcp_do_write(struct bsc_fd *fd, struct msgb *msg)
408{
409 int ret;
410
411 LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
412
413 ret = write(fd->fd, msg->data, msg->len);
414 if (ret != msg->len)
415 LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno));
416
417 return ret;
418}
419
420static int mgcp_do_read(struct bsc_fd *fd)
421{
422 struct msgb *mgcp;
423 int ret;
424
425 mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
426 if (!mgcp) {
427 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
428 return -1;
429 }
430
431 ret = read(fd->fd, mgcp->data, 4096 - 128);
432 if (ret <= 0) {
433 LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
434 msgb_free(mgcp);
435 return -1;
436 } else if (ret > 4096 - 128) {
437 LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
438 msgb_free(mgcp);
439 return -1;
440 }
441
442 mgcp->l2h = msgb_put(mgcp, ret);
443 msc_send(fd->data, mgcp, NAT_MUX);
444 return 0;
445}
446
Holger Hans Peter Freyther5aa17012010-07-31 04:37:26 +0800447void mgcp_forward(struct bsc_data *bsc, const uint8_t *data, unsigned int length)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800448{
449 struct msgb *mgcp;
450
451 if (length > 4096) {
452 LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n");
453 return;
454 }
455
456 mgcp = msgb_alloc(4096, "mgcp_to_gw");
457 if (!mgcp) {
458 LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n");
459 return;
460 }
461
462 msgb_put(mgcp, length);
463 memcpy(mgcp->data, data, mgcp->len);
464 if (write_queue_enqueue(&bsc->mgcp_agent, mgcp) != 0) {
465 LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n");
466 msgb_free(mgcp);
467 }
468}
469
470static int mgcp_create_port(struct bsc_data *bsc)
471{
472 int on;
473 struct sockaddr_in addr;
474
475 bsc->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
476 if (bsc->mgcp_agent.bfd.fd < 0) {
477 LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
478 return -1;
479 }
480
481 on = 1;
482 setsockopt(bsc->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
483
484 /* try to bind the socket */
485 memset(&addr, 0, sizeof(addr));
486 addr.sin_family = AF_INET;
487 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
488 addr.sin_port = 0;
489
490 if (bind(bsc->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
491 LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n");
492 close(bsc->mgcp_agent.bfd.fd);
493 bsc->mgcp_agent.bfd.fd = -1;
494 return -1;
495 }
496
497 /* connect to the remote */
498 addr.sin_port = htons(2427);
499 if (connect(bsc->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) {
500 LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno));
501 close(bsc->mgcp_agent.bfd.fd);
502 bsc->mgcp_agent.bfd.fd = -1;
503 return -1;
504 }
505
506 write_queue_init(&bsc->mgcp_agent, 10);
507 bsc->mgcp_agent.bfd.data = bsc;
508 bsc->mgcp_agent.bfd.when = BSC_FD_READ;
509 bsc->mgcp_agent.read_cb = mgcp_do_read;
510 bsc->mgcp_agent.write_cb = mgcp_do_write;
511
512 if (bsc_register_fd(&bsc->mgcp_agent.bfd) != 0) {
513 LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n");
514 close(bsc->mgcp_agent.bfd.fd);
515 bsc->mgcp_agent.bfd.fd = -1;
516 return -1;
517 }
518
519 return 0;
520}
521
522int msc_init(struct bsc_data *bsc)
523{
524 write_queue_init(&bsc->msc_connection, 100);
525 bsc->reconnect_timer.cb = msc_reconnect;
526 bsc->reconnect_timer.data = bsc;
527 bsc->msc_connection.read_cb = ipaccess_a_fd_cb;
528 bsc->msc_connection.write_cb = ipaccess_write_cb;
529 bsc->msc_connection.bfd.data = bsc;
Holger Hans Peter Freyther0c95c6a2010-08-07 02:37:43 +0800530 bsc->msc_link_down = 1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800531
532 /* handle the timeout */
533 bsc->ping_timeout.cb = msc_ping_timeout;
534 bsc->ping_timeout.data = bsc;
535 bsc->pong_timeout.cb = msc_pong_timeout;
536 bsc->pong_timeout.data = bsc;
537
538 /* create MGCP port */
539 if (mgcp_create_port(bsc) != 0)
540 return -1;
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +0800541
542 /* now connect to the BSC */
543 msc_schedule_reconnect(bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800544 return 0;
545}
546
547static void msc_send(struct bsc_data *bsc, struct msgb *msg, int proto)
548{
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +0800549 if (bsc->msc_link_down) {
550 LOGP(DMSC, LOGL_NOTICE, "Dropping data due lack of MSC connection.\n");
551 msgb_free(msg);
552 return;
553 }
554
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800555 ipaccess_prepend_header(msg, proto);
556
557 if (write_queue_enqueue(&bsc->msc_connection, msg) != 0) {
558 LOGP(DMSC, LOGL_FATAL, "Failed to queue MSG for the MSC.\n");
559 msgb_free(msg);
560 return;
561 }
562}
563
564void msc_send_rlc(struct bsc_data *bsc,
565 struct sccp_source_reference *src, struct sccp_source_reference *dst)
566{
567 struct msgb *msg;
568
569 msg = create_sccp_rlc(src, dst);
570 if (!msg)
571 return;
572
573 msc_send(bsc, msg, IPAC_PROTO_SCCP);
574}
575
576void msc_send_reset(struct bsc_data *bsc)
577{
578 struct msgb *msg;
579
580 msg = create_reset();
581 if (!msg)
582 return;
583
584 msc_send(bsc, msg, IPAC_PROTO_SCCP);
585 msc_ping_timeout(bsc);
586}
587
588static void msc_send_id_response(struct bsc_data *bsc)
589{
590 struct msgb *msg;
591
592 msg = msgb_alloc_headroom(4096, 128, "id resp");
593 msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP);
594 msgb_l16tv_put(msg, strlen(bsc->token) + 1,
Holger Hans Peter Freyther5aa17012010-07-31 04:37:26 +0800595 IPAC_IDTAG_UNITNAME, (uint8_t *) bsc->token);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800596
597 msc_send(bsc, msg, IPAC_PROTO_IPACCESS);
598}
599
600void msc_send_msg(struct bsc_data *bsc, int rc, struct sccp_parse_result *result, struct msgb *_msg)
601{
602 struct msgb *msg;
603
604 if (bsc->msc_connection.bfd.fd < 0) {
605 LOGP(DMSC, LOGL_ERROR, "No connection to the MSC. dropping\n");
606 return;
607 }
608
Holger Hans Peter Freyther0f349f22010-10-06 04:39:08 +0800609 bsc_ussd_handle_out_msg(bsc, result, _msg);
610
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800611 msg = msgb_alloc_headroom(4096, 128, "SCCP to MSC");
612 if (!msg) {
613 LOGP(DMSC, LOGL_ERROR, "Failed to alloc MSC msg.\n");
614 return;
615 }
616
617 bss_rewrite_header_for_msc(rc, msg, _msg, result);
618 msc_send(bsc, msg, IPAC_PROTO_SCCP);
619}