blob: 91d9f7322c5c23e8a57fa9504bf1d732d0b14d63 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080020 *
21 */
22
23#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080024#include <openbsc/debug.h>
Holger Hans Peter Freythere09919b2010-09-16 02:29:45 +080025#include <openbsc/gsm_data.h>
26#include <openbsc/ipaccess.h>
27#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +080028#include <openbsc/signal.h>
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080029
Harald Welted36ff762011-03-23 18:26:56 +010030#include <osmocom/gsm/gsm0808.h>
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080031
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);
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +010070 msc_queue_write(data->msc_con, mgcp, IPAC_PROTO_MGCP_OLD);
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +080071 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
Holger Hans Peter Freytherf3589642011-02-18 23:39:34 +0100177static int msc_alink_do_write(struct bsc_fd *fd, struct msgb *msg)
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800178{
179 int ret;
180
181 LOGP(DMSC, LOGL_DEBUG, "Sending SCCP to MSC: %u\n", msgb_l2len(msg));
Holger Hans Peter Freytherf3589642011-02-18 23:39:34 +0100182 LOGP(DMI, LOGL_DEBUG, "MSC TX %s\n", hexdump(msg->data, msg->len));
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800183
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
Holger Hans Peter Freythera78b6022011-04-23 15:58:24 +0200191static void osmo_ext_handle(struct osmo_msc_data *msc, struct msgb *msg)
192{
193 struct ipaccess_head *hh;
194 struct ipaccess_head_ext *hh_ext;
195
196 hh = (struct ipaccess_head *) msg->data;
197 hh_ext = (struct ipaccess_head_ext *) hh->data;
198 if (msg->len < sizeof(*hh) + sizeof(*hh_ext)) {
199 LOGP(DMSC, LOGL_ERROR, "Packet too short for extended header.\n");
200 return;
201 }
202
203 msg->l2h = hh_ext->data;
204 if (hh_ext->proto == IPAC_PROTO_EXT_MGCP)
205 mgcp_forward(msc, msg);
206
207}
208
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800209static int ipaccess_a_fd_cb(struct bsc_fd *bfd)
210{
211 int error;
212 struct msgb *msg = ipaccess_read_msg(bfd, &error);
213 struct ipaccess_head *hh;
Holger Hans Peter Freyther0425c382010-11-03 14:46:56 +0100214 struct osmo_msc_data *data = (struct osmo_msc_data *) bfd->data;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800215
216 if (!msg) {
217 if (error == 0) {
218 LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n");
219 bsc_msc_lost(data->msc_con);
220 return -1;
221 }
222
223 LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
224 return -1;
225 }
226
227 LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
228
229 /* handle base message handling */
230 hh = (struct ipaccess_head *) msg->data;
231 ipaccess_rcvmsg_base(msg, bfd);
232
233 /* initialize the networking. This includes sending a GSM08.08 message */
234 if (hh->proto == IPAC_PROTO_IPACCESS) {
235 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
236 initialize_if_needed(data->msc_con);
237 else if (msg->l2h[0] == IPAC_MSGT_ID_GET) {
238 send_id_get_response(data, bfd->fd);
239 } else if (msg->l2h[0] == IPAC_MSGT_PONG) {
240 bsc_del_timer(&data->pong_timer);
241 }
242 } else if (hh->proto == IPAC_PROTO_SCCP) {
243 sccp_system_incoming(msg);
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100244 } else if (hh->proto == IPAC_PROTO_MGCP_OLD) {
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800245 mgcp_forward(data, msg);
Holger Hans Peter Freythera78b6022011-04-23 15:58:24 +0200246 } else if (hh->proto == IPAC_PROTO_OSMO) {
247 osmo_ext_handle(data, msg);
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800248 }
249
250 msgb_free(msg);
251 return 0;
252}
253
254static void send_ping(struct osmo_msc_data *data)
255{
256 struct msgb *msg;
257
258 msg = msgb_alloc_headroom(4096, 128, "ping");
259 if (!msg) {
260 LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n");
261 return;
262 }
263
264 msg->l2h = msgb_put(msg, 1);
265 msg->l2h[0] = IPAC_MSGT_PING;
266
267 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
268}
269
270static void msc_ping_timeout_cb(void *_data)
271{
272 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
273 if (data->ping_timeout < 0)
274 return;
275
276 send_ping(data);
277
278 /* send another ping in 20 seconds */
279 bsc_schedule_timer(&data->ping_timer, data->ping_timeout, 0);
280
281 /* also start a pong timer */
282 bsc_schedule_timer(&data->pong_timer, data->pong_timeout, 0);
283}
284
285static void msc_pong_timeout_cb(void *_data)
286{
287 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
288
289 LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n");
290 bsc_msc_lost(data->msc_con);
291}
292
293static void msc_connection_connected(struct bsc_msc_connection *con)
294{
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800295 struct msc_signal_data sig;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800296 struct osmo_msc_data *data;
297 int ret, on;
298 on = 1;
299 ret = setsockopt(con->write_queue.bfd.fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
300 if (ret != 0)
301 LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
302
303 data = (struct osmo_msc_data *) con->write_queue.bfd.data;
Holger Hans Peter Freyther0425c382010-11-03 14:46:56 +0100304 msc_ping_timeout_cb(data);
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800305
306 sig.data = data;
307 dispatch_signal(SS_MSC, S_MSC_CONNECTED, &sig);
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800308}
309
310/*
311 * The connection to the MSC was lost and we will need to free all
312 * resources and then attempt to reconnect.
313 */
314static void msc_connection_was_lost(struct bsc_msc_connection *msc)
315{
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800316 struct msc_signal_data sig;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800317 struct osmo_msc_data *data;
318
319 LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n");
320
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800321 data = (struct osmo_msc_data *) msc->write_queue.bfd.data;
322 bsc_del_timer(&data->ping_timer);
323 bsc_del_timer(&data->pong_timer);
324
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800325 sig.data = data;
326 dispatch_signal(SS_MSC, S_MSC_LOST, &sig);
327
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800328 msc->is_authenticated = 0;
329 bsc_msc_schedule_connect(msc);
330}
331
332static void initialize_if_needed(struct bsc_msc_connection *conn)
333{
334 struct msgb *msg;
335
336 if (!conn->is_authenticated) {
337 /* send a gsm 08.08 reset message from here */
338 msg = gsm0808_create_reset();
339 if (!msg) {
340 LOGP(DMSC, LOGL_ERROR, "Failed to create the reset message.\n");
341 return;
342 }
343
344 sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
345 msgb_free(msg);
346 conn->is_authenticated = 1;
347 }
348}
349
350static void send_id_get_response(struct osmo_msc_data *data, int fd)
351{
352 struct msgb *msg;
353
354 msg = bsc_msc_id_get_resp(data->bsc_token);
355 if (!msg)
356 return;
357 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
358}
359
360int osmo_bsc_msc_init(struct gsm_network *network)
361{
362 struct osmo_msc_data *data = network->msc_data;
363
364 if (mgcp_create_port(data) != 0)
365 return -1;
366
367 data->msc_con = bsc_msc_create(data->msc_ip,
368 data->msc_port,
369 data->msc_ip_dscp);
370 if (!data->msc_con) {
371 LOGP(DMSC, LOGL_ERROR, "Creating the MSC network connection failed.\n");
372 return -1;
373 }
374
375 data->ping_timer.cb = msc_ping_timeout_cb;
376 data->ping_timer.data = data;
377 data->pong_timer.cb = msc_pong_timeout_cb;
378 data->pong_timer.data = data;
379
380 data->msc_con->write_queue.bfd.data = data;
381 data->msc_con->connection_loss = msc_connection_was_lost;
382 data->msc_con->connected = msc_connection_connected;
383 data->msc_con->write_queue.read_cb = ipaccess_a_fd_cb;
Holger Hans Peter Freytherf3589642011-02-18 23:39:34 +0100384 data->msc_con->write_queue.write_cb = msc_alink_do_write;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800385 bsc_msc_connect(data->msc_con);
386
387 return 0;
388}