blob: 8b23c6f4a86387835043ac455f030b9eff46679b [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
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);
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +020070 msc_queue_write(data->msc_con, mgcp, IPAC_PROTO_MGCP);
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
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;
Holger Hans Peter Freyther0425c382010-11-03 14:46:56 +0100196 struct osmo_msc_data *data = (struct osmo_msc_data *) bfd->data;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800197
198 if (!msg) {
199 if (error == 0) {
200 LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n");
201 bsc_msc_lost(data->msc_con);
202 return -1;
203 }
204
205 LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
206 return -1;
207 }
208
209 LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]);
210
211 /* handle base message handling */
212 hh = (struct ipaccess_head *) msg->data;
213 ipaccess_rcvmsg_base(msg, bfd);
214
215 /* initialize the networking. This includes sending a GSM08.08 message */
216 if (hh->proto == IPAC_PROTO_IPACCESS) {
217 if (msg->l2h[0] == IPAC_MSGT_ID_ACK)
218 initialize_if_needed(data->msc_con);
219 else if (msg->l2h[0] == IPAC_MSGT_ID_GET) {
220 send_id_get_response(data, bfd->fd);
221 } else if (msg->l2h[0] == IPAC_MSGT_PONG) {
222 bsc_del_timer(&data->pong_timer);
223 }
224 } else if (hh->proto == IPAC_PROTO_SCCP) {
225 sccp_system_incoming(msg);
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +0200226 } else if (hh->proto == IPAC_PROTO_MGCP) {
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800227 mgcp_forward(data, msg);
228 }
229
230 msgb_free(msg);
231 return 0;
232}
233
234static void send_ping(struct osmo_msc_data *data)
235{
236 struct msgb *msg;
237
238 msg = msgb_alloc_headroom(4096, 128, "ping");
239 if (!msg) {
240 LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n");
241 return;
242 }
243
244 msg->l2h = msgb_put(msg, 1);
245 msg->l2h[0] = IPAC_MSGT_PING;
246
247 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
248}
249
250static void msc_ping_timeout_cb(void *_data)
251{
252 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
253 if (data->ping_timeout < 0)
254 return;
255
256 send_ping(data);
257
258 /* send another ping in 20 seconds */
259 bsc_schedule_timer(&data->ping_timer, data->ping_timeout, 0);
260
261 /* also start a pong timer */
262 bsc_schedule_timer(&data->pong_timer, data->pong_timeout, 0);
263}
264
265static void msc_pong_timeout_cb(void *_data)
266{
267 struct osmo_msc_data *data = (struct osmo_msc_data *) _data;
268
269 LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n");
270 bsc_msc_lost(data->msc_con);
271}
272
273static void msc_connection_connected(struct bsc_msc_connection *con)
274{
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800275 struct msc_signal_data sig;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800276 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;
Holger Hans Peter Freyther0425c382010-11-03 14:46:56 +0100284 msc_ping_timeout_cb(data);
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800285
286 sig.data = data;
287 dispatch_signal(SS_MSC, S_MSC_CONNECTED, &sig);
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800288}
289
290/*
291 * The connection to the MSC was lost and we will need to free all
292 * resources and then attempt to reconnect.
293 */
294static void msc_connection_was_lost(struct bsc_msc_connection *msc)
295{
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800296 struct msc_signal_data sig;
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800297 struct osmo_msc_data *data;
298
299 LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n");
300
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800301 data = (struct osmo_msc_data *) msc->write_queue.bfd.data;
302 bsc_del_timer(&data->ping_timer);
303 bsc_del_timer(&data->pong_timer);
304
Holger Hans Peter Freyther0d711632010-09-16 02:30:36 +0800305 sig.data = data;
306 dispatch_signal(SS_MSC, S_MSC_LOST, &sig);
307
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800308 msc->is_authenticated = 0;
309 bsc_msc_schedule_connect(msc);
310}
311
312static void initialize_if_needed(struct bsc_msc_connection *conn)
313{
314 struct msgb *msg;
315
316 if (!conn->is_authenticated) {
317 /* send a gsm 08.08 reset message from here */
318 msg = gsm0808_create_reset();
319 if (!msg) {
320 LOGP(DMSC, LOGL_ERROR, "Failed to create the reset message.\n");
321 return;
322 }
323
324 sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0);
325 msgb_free(msg);
326 conn->is_authenticated = 1;
327 }
328}
329
330static void send_id_get_response(struct osmo_msc_data *data, int fd)
331{
332 struct msgb *msg;
333
334 msg = bsc_msc_id_get_resp(data->bsc_token);
335 if (!msg)
336 return;
337 msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS);
338}
339
340int osmo_bsc_msc_init(struct gsm_network *network)
341{
342 struct osmo_msc_data *data = network->msc_data;
343
344 if (mgcp_create_port(data) != 0)
345 return -1;
346
347 data->msc_con = bsc_msc_create(data->msc_ip,
348 data->msc_port,
349 data->msc_ip_dscp);
350 if (!data->msc_con) {
351 LOGP(DMSC, LOGL_ERROR, "Creating the MSC network connection failed.\n");
352 return -1;
353 }
354
355 data->ping_timer.cb = msc_ping_timeout_cb;
356 data->ping_timer.data = data;
357 data->pong_timer.cb = msc_pong_timeout_cb;
358 data->pong_timer.data = data;
359
360 data->msc_con->write_queue.bfd.data = data;
361 data->msc_con->connection_loss = msc_connection_was_lost;
362 data->msc_con->connected = msc_connection_connected;
363 data->msc_con->write_queue.read_cb = ipaccess_a_fd_cb;
364 data->msc_con->write_queue.write_cb = msc_sccp_do_write;
365 bsc_msc_connect(data->msc_con);
366
367 return 0;
368}