blob: 864b5d73d0334a3f236cd229db0a437ff7801c44 [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>
25#include <openbsc/ipaccess.h>
26#include <openbsc/gsm_data.h>
27#include <openbsc/osmo_msc_data.h>
28#include <openbsc/debug.h>
29
30#include <osmocore/gsm0808.h>
31
32#include <osmocom/sccp/sccp.h>
33
34#include <sys/socket.h>
35#include <netinet/tcp.h>
36#include <unistd.h>
37
38
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080039static void initialize_if_needed(struct bsc_msc_connection *conn);
40static void send_id_get_response(struct osmo_msc_data *data, int fd);
41static void send_ping(struct osmo_msc_data *data);
42
43/*
44 * MGCP forwarding code
45 */
46static int mgcp_do_read(struct bsc_fd *fd)
47{
48 struct osmo_msc_data *data = (struct osmo_msc_data *) fd->data;
49 struct msgb *mgcp;
50 int ret;
51
52 mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
53 if (!mgcp) {
54 LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
55 return -1;
56 }
57
58 ret = read(fd->fd, mgcp->data, 4096 - 128);
59 if (ret <= 0) {
60 LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
61 msgb_free(mgcp);
62 return -1;
63 } else if (ret > 4096 - 128) {
64 LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
65 msgb_free(mgcp);
66 return -1;
67 }
68
69 mgcp->l2h = msgb_put(mgcp, ret);
70 msc_queue_write(data->msc_con, mgcp, NAT_IPAC_PROTO_MGCP);
71 return 0;
72}
73
74static int mgcp_do_write(struct bsc_fd *fd, struct msgb *msg)
75{
76 int ret;
77
78 LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
79
80 ret = write(fd->fd, msg->data, msg->len);
81 if (ret != msg->len)
82 LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno));
83
84 return ret;
85}
86
87static void mgcp_forward(struct osmo_msc_data *data, struct msgb *msg)
88{
89 struct msgb *mgcp;
90
91 if (msgb_l2len(msg) > 4096) {
92 LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n");
93 return;
94 }
95
96 mgcp = msgb_alloc(4096, "mgcp_to_gw");
97 if (!mgcp) {
98 LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n");
99 return;
100 }
101
102 msgb_put(mgcp, msgb_l2len(msg));
103 memcpy(mgcp->data, msg->l2h, mgcp->len);
104 if (write_queue_enqueue(&data->mgcp_agent, mgcp) != 0) {
105 LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n");
106 msgb_free(mgcp);
107 }
108}
109
110static int mgcp_create_port(struct osmo_msc_data *data)
111{
112 int on;
113 struct sockaddr_in addr;
114
115 data->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
116 if (data->mgcp_agent.bfd.fd < 0) {
117 LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
118 return -1;
119 }
120
121 on = 1;
122 setsockopt(data->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
123
124 /* try to bind the socket */
125 memset(&addr, 0, sizeof(addr));
126 addr.sin_family = AF_INET;
127 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
128 addr.sin_port = 0;
129
130 if (bind(data->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
131 LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n");
132 close(data->mgcp_agent.bfd.fd);
133 data->mgcp_agent.bfd.fd = -1;
134 return -1;
135 }
136
137 /* connect to the remote */
138 addr.sin_port = htons(2427);
139 if (connect(data->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) {
140 LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno));
141 close(data->mgcp_agent.bfd.fd);
142 data->mgcp_agent.bfd.fd = -1;
143 return -1;
144 }
145
146 write_queue_init(&data->mgcp_agent, 10);
147 data->mgcp_agent.bfd.when = BSC_FD_READ;
148 data->mgcp_agent.bfd.data = data;
149 data->mgcp_agent.read_cb = mgcp_do_read;
150 data->mgcp_agent.write_cb = mgcp_do_write;
151
152 if (bsc_register_fd(&data->mgcp_agent.bfd) != 0) {
153 LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n");
154 close(data->mgcp_agent.bfd.fd);
155 data->mgcp_agent.bfd.fd = -1;
156 return -1;
157 }
158
159 return 0;
160}
161
162/*
163 * Send data to the network
164 */
Holger Hans Peter Freyther60980382010-09-16 02:22:20 +0800165int msc_queue_write(struct bsc_msc_connection *conn, struct msgb *msg, int proto)
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800166{
167 ipaccess_prepend_header(msg, proto);
168 if (write_queue_enqueue(&conn->write_queue, msg) != 0) {
169 LOGP(DMSC, LOGL_FATAL, "Failed to queue IPA/%d\n", proto);
170 msgb_free(msg);
171 return -1;
172 }
173
174 return 0;
175}
176
177static int msc_sccp_do_write(struct bsc_fd *fd, struct msgb *msg)
178{
179 int ret;
180
181 LOGP(DMSC, LOGL_DEBUG, "Sending SCCP to MSC: %u\n", msgb_l2len(msg));
182 LOGP(DMI, LOGL_DEBUG, "MSC TX %s\n", hexdump(msg->l2h, msgb_l2len(msg)));
183
184 ret = write(fd->fd, msg->data, msg->len);
185 if (ret < msg->len)
186 perror("MSC: Failed to send SCCP");
187
188 return ret;
189}
190
191static int ipaccess_a_fd_cb(struct bsc_fd *bfd)
192{
193 int error;
194 struct msgb *msg = ipaccess_read_msg(bfd, &error);
195 struct ipaccess_head *hh;
196 struct gsm_network *net = (struct gsm_network *) bfd->data;
197 struct osmo_msc_data *data = net->msc_data;
198
199 if (!msg) {
200 if (error == 0) {
201 LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n");
202 bsc_msc_lost(data->msc_con);
203 return -1;
204 }
205
206 LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
207 return -1;
208 }
209
210 LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
211
212 /* handle base message handling */
213 hh = (struct ipaccess_head *) msg->data;
214 ipaccess_rcvmsg_base(msg, bfd);
215
216 /* initialize the networking. This includes sending a GSM08.08 message */
217 if (hh->proto == IPAC_PROTO_IPACCESS) {
218 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
219 initialize_if_needed(data->msc_con);
220 else if (msg->l2h[0] == IPAC_MSGT_ID_GET) {
221 send_id_get_response(data, bfd->fd);
222 } else if (msg->l2h[0] == IPAC_MSGT_PONG) {
223 bsc_del_timer(&data->pong_timer);
224 }
225 } else if (hh->proto == IPAC_PROTO_SCCP) {
226 sccp_system_incoming(msg);
227 } else if (hh->proto == NAT_IPAC_PROTO_MGCP) {
228 mgcp_forward(data, msg);
229 }
230
231 msgb_free(msg);
232 return 0;
233}
234
235static void send_ping(struct osmo_msc_data *data)
236{
237 struct msgb *msg;
238
239 msg = msgb_alloc_headroom(4096, 128, "ping");
240 if (!msg) {
241 LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n");
242 return;
243 }
244
245 msg->l2h = msgb_put(msg, 1);
246 msg->l2h[0] = IPAC_MSGT_PING;
247
248 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
249}
250
251static void msc_ping_timeout_cb(void *_data)
252{
253 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
254 if (data->ping_timeout < 0)
255 return;
256
257 send_ping(data);
258
259 /* send another ping in 20 seconds */
260 bsc_schedule_timer(&data->ping_timer, data->ping_timeout, 0);
261
262 /* also start a pong timer */
263 bsc_schedule_timer(&data->pong_timer, data->pong_timeout, 0);
264}
265
266static void msc_pong_timeout_cb(void *_data)
267{
268 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
269
270 LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n");
271 bsc_msc_lost(data->msc_con);
272}
273
274static void msc_connection_connected(struct bsc_msc_connection *con)
275{
276 struct osmo_msc_data *data;
277 int ret, on;
278 on = 1;
279 ret = setsockopt(con->write_queue.bfd.fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
280 if (ret != 0)
281 LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
282
283 data = (struct osmo_msc_data *) con->write_queue.bfd.data;
284 msc_ping_timeout_cb(con);
285}
286
287/*
288 * The connection to the MSC was lost and we will need to free all
289 * resources and then attempt to reconnect.
290 */
291static void msc_connection_was_lost(struct bsc_msc_connection *msc)
292{
293 struct osmo_msc_data *data;
294
295 LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n");
296
297#if 0
298 struct bss_sccp_connection_data *bss, *tmp;
299 llist_for_each_entry_safe(bss, tmp, &active_connections, active_connections) {
300 bss_force_close(bss);
301 }
302#else
303#warning "This needs to be ported..."
304#endif
305
306 data = (struct osmo_msc_data *) msc->write_queue.bfd.data;
307 bsc_del_timer(&data->ping_timer);
308 bsc_del_timer(&data->pong_timer);
309
310 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}