blob: f116693617de0244c3bd0bd164bd5abcefa5b144 [file] [log] [blame]
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +08001/* Routines to talk to the MSC using the IPA Protocol */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08004 * (C) 2010 by On-Waves
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +08005 * 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 <openbsc/bsc_msc.h>
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010024#include <openbsc/debug.h>
Holger Hans Peter Freytherf76e7ef2010-06-15 18:52:05 +080025#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010026
27#include <osmocore/write_queue.h>
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080028#include <osmocore/talloc.h>
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +080029
30#include <arpa/inet.h>
31#include <sys/socket.h>
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010032#include <errno.h>
33#include <fcntl.h>
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +080034#include <stdio.h>
35#include <string.h>
36#include <unistd.h>
37
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080038static void connection_loss(struct bsc_msc_connection *con)
39{
40 struct bsc_fd *fd;
41
42 fd = &con->write_queue.bfd;
43
44 close(fd->fd);
45 fd->fd = -1;
46 fd->cb = write_queue_bfd_cb;
47 fd->when = 0;
48
49 con->is_connected = 0;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +080050 con->first_contact = 0;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080051 con->connection_loss(con);
52}
53
Holger Hans Peter Freythere47a91b2010-05-05 22:48:56 +080054static void msc_con_timeout(void *_con)
55{
56 struct bsc_msc_connection *con = _con;
57
58 LOGP(DMSC, LOGL_ERROR, "MSC Connection timeout.\n");
59 bsc_msc_lost(con);
60}
61
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010062/* called in the case of a non blocking connect */
63static int msc_connection_connect(struct bsc_fd *fd, unsigned int what)
64{
65 int rc;
66 int val;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080067 struct bsc_msc_connection *con;
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010068 struct write_queue *queue;
69
70 socklen_t len = sizeof(val);
71
72 if ((what & BSC_FD_WRITE) == 0) {
Holger Hans Peter Freyther62abade2010-08-04 02:27:34 +080073 LOGP(DMSC, LOGL_ERROR, "Callback but not writable.\n");
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010074 return -1;
75 }
76
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080077 queue = container_of(fd, struct write_queue, bfd);
78 con = container_of(queue, struct bsc_msc_connection, write_queue);
79
Holger Hans Peter Freytherd4eed522010-10-07 04:42:03 +080080 /* From here on we will either be connected or reconnect */
81 bsc_del_timer(&con->timeout_timer);
82
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010083 /* check the socket state */
84 rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len);
85 if (rc != 0) {
86 LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n");
87 goto error;
88 }
89 if (val != 0) {
Holger Hans Peter Freyther9d90da92010-04-06 10:25:00 +020090 LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC: %d\n", val);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010091 goto error;
92 }
93
94
95 /* go to full operation */
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +010096 fd->cb = write_queue_bfd_cb;
Holger Hans Peter Freyther0176eb42010-04-08 11:12:32 +020097 fd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +080098
99 con->is_connected = 1;
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200100 LOGP(DMSC, LOGL_NOTICE, "(Re)Connected to the MSC.\n");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800101 if (con->connected)
102 con->connected(con);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100103 return 0;
104
105error:
106 bsc_unregister_fd(fd);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800107 connection_loss(con);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100108 return -1;
109}
110static void setnonblocking(struct bsc_fd *fd)
111{
112 int flags;
113
114 flags = fcntl(fd->fd, F_GETFL);
115 if (flags < 0) {
116 perror("fcntl get failed");
117 close(fd->fd);
118 fd->fd = -1;
119 return;
120 }
121
122 flags |= O_NONBLOCK;
123 flags = fcntl(fd->fd, F_SETFL, flags);
124 if (flags < 0) {
125 perror("fcntl get failed");
126 close(fd->fd);
127 fd->fd = -1;
128 return;
129 }
130}
131
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800132int bsc_msc_connect(struct bsc_msc_connection *con)
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800133{
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800134 struct bsc_fd *fd;
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800135 struct sockaddr_in sin;
136 int on = 1, ret;
137
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800138 LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", con->ip, con->port);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800139
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800140 con->is_connected = 0;
141
142 fd = &con->write_queue.bfd;
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800143 fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800144 fd->priv_nr = 1;
145
146 if (fd->fd < 0) {
147 perror("Creating TCP socket failed");
148 return fd->fd;
149 }
150
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100151 /* make it non blocking */
152 setnonblocking(fd);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800153
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +0800154 /* set the socket priority */
155 ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS,
156 &con->prio, sizeof(con->prio));
157 if (ret != 0)
158 LOGP(DMSC, LOGL_ERROR, "Failed to set prio to %d. %s\n",
159 con->prio, strerror(errno));
160
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800161 memset(&sin, 0, sizeof(sin));
162 sin.sin_family = AF_INET;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800163 sin.sin_port = htons(con->port);
164 inet_aton(con->ip, &sin.sin_addr);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800165
166 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
167 ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin));
168
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100169 if (ret == -1 && errno == EINPROGRESS) {
170 LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n");
171 fd->when = BSC_FD_WRITE;
172 fd->cb = msc_connection_connect;
Holger Hans Peter Freythere47a91b2010-05-05 22:48:56 +0800173 con->timeout_timer.cb = msc_con_timeout;
174 con->timeout_timer.data = con;
175 bsc_schedule_timer(&con->timeout_timer, 20, 0);
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100176 } else if (ret < 0) {
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800177 perror("Connection failed");
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800178 connection_loss(con);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800179 return ret;
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100180 } else {
Holger Hans Peter Freyther0176eb42010-04-08 11:12:32 +0200181 fd->when = BSC_FD_READ | BSC_FD_EXCEPT;
Holger Hans Peter Freyther6c0a04e2010-03-26 10:41:20 +0100182 fd->cb = write_queue_bfd_cb;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800183 con->is_connected = 1;
184 if (con->connected)
185 con->connected(con);
Holger Hans Peter Freythereef86b52010-06-15 18:45:06 +0800186 }
187
188 ret = bsc_register_fd(fd);
189 if (ret < 0) {
190 perror("Registering the fd failed");
191 close(fd->fd);
192 return ret;
193 }
194
195 return ret;
196}
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800197
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +0800198struct bsc_msc_connection *bsc_msc_create(const char *ip, int port, int prio)
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800199{
200 struct bsc_msc_connection *con;
201
202 con = talloc_zero(NULL, struct bsc_msc_connection);
203 if (!con) {
204 LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n");
205 return NULL;
206 }
207
208 con->ip = ip;
209 con->port = port;
Holger Hans Peter Freytherca999a92010-06-15 18:52:38 +0800210 con->prio = prio;
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800211 write_queue_init(&con->write_queue, 100);
212 return con;
213}
214
215void bsc_msc_lost(struct bsc_msc_connection *con)
216{
Holger Hans Peter Freyther03ca97e2010-03-31 13:15:26 +0200217 write_queue_clear(&con->write_queue);
Holger Hans Peter Freytherd4eed522010-10-07 04:42:03 +0800218 bsc_del_timer(&con->timeout_timer);
Holger Hans Peter Freytherfad07532010-10-07 06:07:57 +0800219
220 if (con->write_queue.bfd.fd >= 0)
221 bsc_unregister_fd(&con->write_queue.bfd);
Holger Hans Peter Freytherbaf2abe2010-06-15 18:47:29 +0800222 connection_loss(con);
223}
224
225static void reconnect_msc(void *_msc)
226{
227 struct bsc_msc_connection *con = _msc;
228
229 LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n");
230 bsc_msc_connect(con);
231}
232
233void bsc_msc_schedule_connect(struct bsc_msc_connection *con)
234{
235 LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n");
236 con->reconnect_timer.cb = reconnect_msc;
237 con->reconnect_timer.data = con;
238 bsc_schedule_timer(&con->reconnect_timer, 5, 0);
Holger Hans Peter Freytherf76e7ef2010-06-15 18:52:05 +0800239}
240
241struct msgb *bsc_msc_id_get_resp(const char *token)
242{
243 struct msgb *msg;
244
245 if (!token) {
246 LOGP(DMSC, LOGL_ERROR, "No token specified.\n");
247 return NULL;
248 }
249
250 msg = msgb_alloc_headroom(4096, 128, "id resp");
251 if (!msg) {
252 LOGP(DMSC, LOGL_ERROR, "Failed to create the message.\n");
253 return NULL;
254 }
255
256 msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP);
257 msgb_l16tv_put(msg, strlen(token) + 1,
258 IPAC_IDTAG_UNITNAME, (u_int8_t *) token);
259 return msg;
Holger Hans Peter Freytherbec411b2010-07-05 14:14:18 +0800260}