blob: 66449827ad78823987760ef37ec8827e1b3251a8 [file] [log] [blame]
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +08001/*
2 * Handle the connection to the MSC. This include ping/timeout/reconnect
3 * (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2010 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080025#include <openbsc/debug.h>
Holger Hans Peter Freythere09919b2010-09-16 02:29:45 +080026#include <openbsc/gsm_data.h>
27#include <openbsc/ipaccess.h>
28#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +080029#include <openbsc/signal.h>
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080030
31#include <osmocore/gsm0808.h>
32
33#include <osmocom/sccp/sccp.h>
34
35#include <sys/socket.h>
36#include <netinet/tcp.h>
37#include <unistd.h>
38
39
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080040static void initialize_if_needed(struct bsc_msc_connection *conn);
41static void send_id_get_response(struct osmo_msc_data *data, int fd);
42static void send_ping(struct osmo_msc_data *data);
43
44/*
45 * MGCP forwarding code
46 */
47static int mgcp_do_read(struct bsc_fd *fd)
48{
49 struct osmo_msc_data *data = (struct osmo_msc_data *) fd->data;
50 struct msgb *mgcp;
51 int ret;
52
53 mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
54 if (!mgcp) {
55 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
56 return -1;
57 }
58
59 ret = read(fd->fd, mgcp->data, 4096 - 128);
60 if (ret <= 0) {
61 LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
62 msgb_free(mgcp);
63 return -1;
64 } else if (ret > 4096 - 128) {
65 LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
66 msgb_free(mgcp);
67 return -1;
68 }
69
70 mgcp->l2h = msgb_put(mgcp, ret);
71 msc_queue_write(data->msc_con, mgcp, NAT_IPAC_PROTO_MGCP);
72 return 0;
73}
74
75static int mgcp_do_write(struct bsc_fd *fd, struct msgb *msg)
76{
77 int ret;
78
79 LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
80
81 ret = write(fd->fd, msg->data, msg->len);
82 if (ret != msg->len)
83 LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno));
84
85 return ret;
86}
87
88static void mgcp_forward(struct osmo_msc_data *data, struct msgb *msg)
89{
90 struct msgb *mgcp;
91
92 if (msgb_l2len(msg) > 4096) {
93 LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n");
94 return;
95 }
96
97 mgcp = msgb_alloc(4096, "mgcp_to_gw");
98 if (!mgcp) {
99 LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n");
100 return;
101 }
102
103 msgb_put(mgcp, msgb_l2len(msg));
104 memcpy(mgcp->data, msg->l2h, mgcp->len);
105 if (write_queue_enqueue(&data->mgcp_agent, mgcp) != 0) {
106 LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n");
107 msgb_free(mgcp);
108 }
109}
110
111static int mgcp_create_port(struct osmo_msc_data *data)
112{
113 int on;
114 struct sockaddr_in addr;
115
116 data->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
117 if (data->mgcp_agent.bfd.fd < 0) {
118 LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
119 return -1;
120 }
121
122 on = 1;
123 setsockopt(data->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
124
125 /* try to bind the socket */
126 memset(&addr, 0, sizeof(addr));
127 addr.sin_family = AF_INET;
128 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
129 addr.sin_port = 0;
130
131 if (bind(data->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
132 LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n");
133 close(data->mgcp_agent.bfd.fd);
134 data->mgcp_agent.bfd.fd = -1;
135 return -1;
136 }
137
138 /* connect to the remote */
139 addr.sin_port = htons(2427);
140 if (connect(data->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) {
141 LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno));
142 close(data->mgcp_agent.bfd.fd);
143 data->mgcp_agent.bfd.fd = -1;
144 return -1;
145 }
146
147 write_queue_init(&data->mgcp_agent, 10);
148 data->mgcp_agent.bfd.when = BSC_FD_READ;
149 data->mgcp_agent.bfd.data = data;
150 data->mgcp_agent.read_cb = mgcp_do_read;
151 data->mgcp_agent.write_cb = mgcp_do_write;
152
153 if (bsc_register_fd(&data->mgcp_agent.bfd) != 0) {
154 LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n");
155 close(data->mgcp_agent.bfd.fd);
156 data->mgcp_agent.bfd.fd = -1;
157 return -1;
158 }
159
160 return 0;
161}
162
163/*
164 * Send data to the network
165 */
Holger Hans Peter Freyther60980382010-09-16 02:22:20 +0800166int msc_queue_write(struct bsc_msc_connection *conn, struct msgb *msg, int proto)
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800167{
168 ipaccess_prepend_header(msg, proto);
169 if (write_queue_enqueue(&conn->write_queue, msg) != 0) {
170 LOGP(DMSC, LOGL_FATAL, "Failed to queue IPA/%d\n", proto);
171 msgb_free(msg);
172 return -1;
173 }
174
175 return 0;
176}
177
178static int msc_sccp_do_write(struct bsc_fd *fd, struct msgb *msg)
179{
180 int ret;
181
182 LOGP(DMSC, LOGL_DEBUG, "Sending SCCP to MSC: %u\n", msgb_l2len(msg));
183 LOGP(DMI, LOGL_DEBUG, "MSC TX %s\n", hexdump(msg->l2h, msgb_l2len(msg)));
184
185 ret = write(fd->fd, msg->data, msg->len);
186 if (ret < msg->len)
187 perror("MSC: Failed to send SCCP");
188
189 return ret;
190}
191
192static int ipaccess_a_fd_cb(struct bsc_fd *bfd)
193{
194 int error;
195 struct msgb *msg = ipaccess_read_msg(bfd, &error);
196 struct ipaccess_head *hh;
197 struct gsm_network *net = (struct gsm_network *) bfd->data;
198 struct osmo_msc_data *data = net->msc_data;
199
200 if (!msg) {
201 if (error == 0) {
202 LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n");
203 bsc_msc_lost(data->msc_con);
204 return -1;
205 }
206
207 LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
208 return -1;
209 }
210
211 LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
212
213 /* handle base message handling */
214 hh = (struct ipaccess_head *) msg->data;
215 ipaccess_rcvmsg_base(msg, bfd);
216
217 /* initialize the networking. This includes sending a GSM08.08 message */
218 if (hh->proto == IPAC_PROTO_IPACCESS) {
219 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
220 initialize_if_needed(data->msc_con);
221 else if (msg->l2h[0] == IPAC_MSGT_ID_GET) {
222 send_id_get_response(data, bfd->fd);
223 } else if (msg->l2h[0] == IPAC_MSGT_PONG) {
224 bsc_del_timer(&data->pong_timer);
225 }
226 } else if (hh->proto == IPAC_PROTO_SCCP) {
227 sccp_system_incoming(msg);
228 } else if (hh->proto == NAT_IPAC_PROTO_MGCP) {
229 mgcp_forward(data, msg);
230 }
231
232 msgb_free(msg);
233 return 0;
234}
235
236static void send_ping(struct osmo_msc_data *data)
237{
238 struct msgb *msg;
239
240 msg = msgb_alloc_headroom(4096, 128, "ping");
241 if (!msg) {
242 LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n");
243 return;
244 }
245
246 msg->l2h = msgb_put(msg, 1);
247 msg->l2h[0] = IPAC_MSGT_PING;
248
249 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
250}
251
252static void msc_ping_timeout_cb(void *_data)
253{
254 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
255 if (data->ping_timeout < 0)
256 return;
257
258 send_ping(data);
259
260 /* send another ping in 20 seconds */
261 bsc_schedule_timer(&data->ping_timer, data->ping_timeout, 0);
262
263 /* also start a pong timer */
264 bsc_schedule_timer(&data->pong_timer, data->pong_timeout, 0);
265}
266
267static void msc_pong_timeout_cb(void *_data)
268{
269 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
270
271 LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n");
272 bsc_msc_lost(data->msc_con);
273}
274
275static void msc_connection_connected(struct bsc_msc_connection *con)
276{
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800277 struct msc_signal_data sig;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800278 struct osmo_msc_data *data;
279 int ret, on;
280 on = 1;
281 ret = setsockopt(con->write_queue.bfd.fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
282 if (ret != 0)
283 LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
284
285 data = (struct osmo_msc_data *) con->write_queue.bfd.data;
286 msc_ping_timeout_cb(con);
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800287
288 sig.data = data;
289 dispatch_signal(SS_MSC, S_MSC_CONNECTED, &sig);
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800290}
291
292/*
293 * The connection to the MSC was lost and we will need to free all
294 * resources and then attempt to reconnect.
295 */
296static void msc_connection_was_lost(struct bsc_msc_connection *msc)
297{
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800298 struct msc_signal_data sig;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800299 struct osmo_msc_data *data;
300
301 LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n");
302
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800303 data = (struct osmo_msc_data *) msc->write_queue.bfd.data;
304 bsc_del_timer(&data->ping_timer);
305 bsc_del_timer(&data->pong_timer);
306
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800307 sig.data = data;
308 dispatch_signal(SS_MSC, S_MSC_LOST, &sig);
309
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800310 msc->is_authenticated = 0;
311 bsc_msc_schedule_connect(msc);
312}
313
314static void initialize_if_needed(struct bsc_msc_connection *conn)
315{
316 struct msgb *msg;
317
318 if (!conn->is_authenticated) {
319 /* send a gsm 08.08 reset message from here */
320 msg = gsm0808_create_reset();
321 if (!msg) {
322 LOGP(DMSC, LOGL_ERROR, "Failed to create the reset message.\n");
323 return;
324 }
325
326 sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
327 msgb_free(msg);
328 conn->is_authenticated = 1;
329 }
330}
331
332static void send_id_get_response(struct osmo_msc_data *data, int fd)
333{
334 struct msgb *msg;
335
336 msg = bsc_msc_id_get_resp(data->bsc_token);
337 if (!msg)
338 return;
339 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
340}
341
342int osmo_bsc_msc_init(struct gsm_network *network)
343{
344 struct osmo_msc_data *data = network->msc_data;
345
346 if (mgcp_create_port(data) != 0)
347 return -1;
348
349 data->msc_con = bsc_msc_create(data->msc_ip,
350 data->msc_port,
351 data->msc_ip_dscp);
352 if (!data->msc_con) {
353 LOGP(DMSC, LOGL_ERROR, "Creating the MSC network connection failed.\n");
354 return -1;
355 }
356
357 data->ping_timer.cb = msc_ping_timeout_cb;
358 data->ping_timer.data = data;
359 data->pong_timer.cb = msc_pong_timeout_cb;
360 data->pong_timer.data = data;
361
362 data->msc_con->write_queue.bfd.data = data;
363 data->msc_con->connection_loss = msc_connection_was_lost;
364 data->msc_con->connected = msc_connection_connected;
365 data->msc_con->write_queue.read_cb = ipaccess_a_fd_cb;
366 data->msc_con->write_queue.write_cb = msc_sccp_do_write;
367 bsc_msc_connect(data->msc_con);
368
369 return 0;
370}