blob: 697f9560f38448b417fcb0012bf92d90c8530238 [file] [log] [blame]
Harald Welte31bbbf42010-12-23 00:02:51 +01001/* mncc_sock.c: Tie the MNCC interface to a unix domain socket */
2
3/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Andreas Eversberg <Andreas.Eversberg@versatel.de>
5 * 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 <stdio.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <string.h>
27#include <errno.h>
Harald Welte49a2dde2010-12-22 22:56:15 +010028#include <assert.h>
Harald Welte31bbbf42010-12-23 00:02:51 +010029#include <sys/socket.h>
30#include <sys/un.h>
31
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/talloc.h>
33#include <osmocom/core/select.h>
34#include <osmocom/gsm/protocol/gsm_04_08.h>
Harald Welte31bbbf42010-12-23 00:02:51 +010035
36#include <openbsc/debug.h>
37#include <openbsc/mncc.h>
38#include <openbsc/gsm_data.h>
39
40struct mncc_sock_state {
41 struct gsm_network *net;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020042 struct osmo_fd listen_bfd; /* fd for listen socket */
43 struct osmo_fd conn_bfd; /* fd for connection to lcr */
Harald Welte31bbbf42010-12-23 00:02:51 +010044};
45
Harald Welteea057d92010-12-23 01:26:29 +010046/* input from CC code into mncc_sock */
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010047int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg)
Harald Welteea057d92010-12-23 01:26:29 +010048{
Holger Hans Peter Freyther02d45c02011-01-06 13:50:52 +010049 struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg);
Harald Welteea057d92010-12-23 01:26:29 +010050 int msg_type = mncc_in->msg_type;
51
52 /* Check if we currently have a MNCC handler connected */
Holger Hans Peter Freyther694c82d2011-10-26 18:37:09 +020053 if (net->mncc_state->conn_bfd.fd < 0) {
Harald Welteea057d92010-12-23 01:26:29 +010054 LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app "
55 "but socket is gone\n", get_mncc_name(msg_type));
56 if (msg_type != GSM_TCHF_FRAME &&
57 msg_type != GSM_TCHF_FRAME_EFR) {
58 /* release the request */
59 struct gsm_mncc mncc_out;
60 memset(&mncc_out, 0, sizeof(mncc_out));
61 mncc_out.callref = mncc_in->callref;
62 mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU,
63 GSM48_CC_CAUSE_TEMP_FAILURE);
64 mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out);
65 }
66 /* free the original message */
67 msgb_free(msg);
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010068 return -1;
Harald Welteea057d92010-12-23 01:26:29 +010069 }
70
71 /* FIXME: check for some maximum queue depth? */
72
73 /* Actually enqueue the message and mark socket write need */
74 msgb_enqueue(&net->upqueue, msg);
Holger Hans Peter Freyther694c82d2011-10-26 18:37:09 +020075 net->mncc_state->conn_bfd.when |= BSC_FD_WRITE;
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010076 return 0;
Harald Welteea057d92010-12-23 01:26:29 +010077}
78
Harald Welte31bbbf42010-12-23 00:02:51 +010079/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020080int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path);
Harald Welte31bbbf42010-12-23 00:02:51 +010081
82static void mncc_sock_close(struct mncc_sock_state *state)
83{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020084 struct osmo_fd *bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +010085
Harald Welte0d6f9302010-12-23 02:47:28 +010086 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n");
87
Harald Welte31bbbf42010-12-23 00:02:51 +010088 close(bfd->fd);
89 bfd->fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020090 osmo_fd_unregister(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +010091
92 /* re-enable the generation of ACCEPT for new connections */
93 state->listen_bfd.when |= BSC_FD_READ;
94
Harald Welte371efe52010-12-22 23:17:50 +010095 /* release all exisitng calls */
96 gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC);
Harald Welte31bbbf42010-12-23 00:02:51 +010097
98 /* flush the queue */
99 while (!llist_empty(&state->net->upqueue)) {
100 struct msgb *msg = msgb_dequeue(&state->net->upqueue);
101 msgb_free(msg);
102 }
103}
104
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200105static int mncc_sock_read(struct osmo_fd *bfd)
Harald Welte31bbbf42010-12-23 00:02:51 +0100106{
107 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
108 struct gsm_mncc *mncc_prim;
109 struct msgb *msg;
110 int rc;
111
112 msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx");
113 if (!msg)
114 return -ENOMEM;
115
116 mncc_prim = (struct gsm_mncc *) msg->tail;
117
118 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
119 if (rc == 0)
120 goto close;
121
122 if (rc < 0) {
123 if (errno == EAGAIN)
124 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100125 goto close;
126 }
127
128 rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim);
129
130 /* as we always synchronously process the message in mncc_send() and
131 * its callbacks, we can free the message here. */
132 msgb_free(msg);
133
134 return rc;
135
136close:
Harald Welteeb76c7a2010-12-23 02:47:53 +0100137 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100138 mncc_sock_close(state);
139 return -1;
140}
141
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200142static int mncc_sock_write(struct osmo_fd *bfd)
Harald Welte31bbbf42010-12-23 00:02:51 +0100143{
144 struct mncc_sock_state *state = bfd->data;
145 struct gsm_network *net = state->net;
146 int rc;
147
148 while (!llist_empty(&net->upqueue)) {
Harald Welte49a2dde2010-12-22 22:56:15 +0100149 struct msgb *msg, *msg2;
Harald Welte31bbbf42010-12-23 00:02:51 +0100150 struct gsm_mncc *mncc_prim;
151
152 /* peek at the beginning of the queue */
153 msg = llist_entry(net->upqueue.next, struct msgb, list);
154 mncc_prim = (struct gsm_mncc *)msg->data;
155
156 bfd->when &= ~BSC_FD_WRITE;
157
Peter Stuge46f799b2011-08-11 04:37:17 +0200158 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
159 if (!msgb_length(msg)) {
160 LOGP(DMNCC, LOGL_ERROR, "message type (%d) with ZERO "
161 "bytes!\n", mncc_prim->msg_type);
162 goto dontsend;
163 }
164
Harald Welte31bbbf42010-12-23 00:02:51 +0100165 /* try to send it over the socket */
166 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
167 if (rc == 0)
168 goto close;
169 if (rc < 0) {
170 if (errno == EAGAIN) {
171 bfd->when |= BSC_FD_WRITE;
172 break;
173 }
174 goto close;
175 }
Peter Stuge46f799b2011-08-11 04:37:17 +0200176
177dontsend:
Harald Welte31bbbf42010-12-23 00:02:51 +0100178 /* _after_ we send it, we can deueue */
Harald Welte49a2dde2010-12-22 22:56:15 +0100179 msg2 = msgb_dequeue(&net->upqueue);
180 assert(msg == msg2);
181 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100182 }
183 return 0;
184
185close:
186 mncc_sock_close(state);
187
188 return -1;
189}
190
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200191static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100192{
193 int rc = 0;
194
195 if (flags & BSC_FD_READ)
196 rc = mncc_sock_read(bfd);
197 if (rc < 0)
198 return rc;
199
200 if (flags & BSC_FD_WRITE)
201 rc = mncc_sock_write(bfd);
202
203 return rc;
204}
205
Holger Hans Peter Freyther347e7ba2011-10-21 14:12:46 +0200206/**
207 * Send a version indication to the remote.
208 */
209static void queue_hello(struct mncc_sock_state *mncc)
210{
211 struct gsm_mncc_hello *hello;
212 struct msgb *msg;
213
214 msg = msgb_alloc(512, "mncc hello");
215 if (!msg) {
216 LOGP(DMNCC, LOGL_ERROR, "Failed to allocate hello.\n");
217 mncc_sock_close(mncc);
218 return;
219 }
220
221 hello = (struct gsm_mncc_hello *) msgb_put(msg, sizeof(*hello));
222 hello->msg_type = MNCC_SOCKET_HELLO;
223 hello->version = MNCC_SOCK_VERSION;
224
225 msgb_enqueue(&mncc->net->upqueue, msg);
226 mncc->conn_bfd.when |= BSC_FD_WRITE;
227}
228
Harald Welte31bbbf42010-12-23 00:02:51 +0100229/* accept a new connection */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200230static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100231{
232 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200233 struct osmo_fd *conn_bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100234 struct sockaddr_un un_addr;
235 socklen_t len;
236 int rc;
237
238 len = sizeof(un_addr);
239 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
240 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100241 LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100242 return -1;
243 }
244
Harald Welte0d6f9302010-12-23 02:47:28 +0100245 if (conn_bfd->fd >= 0) {
246 LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have "
247 "another active connection ?!?\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100248 /* We already have one MNCC app connected, this is all we support */
249 state->listen_bfd.when &= ~BSC_FD_READ;
250 close(rc);
Harald Welte0d6f9302010-12-23 02:47:28 +0100251 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100252 }
253
254 conn_bfd->fd = rc;
255 conn_bfd->when = BSC_FD_READ;
256 conn_bfd->cb = mncc_sock_cb;
257 conn_bfd->data = state;
258
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200259 if (osmo_fd_register(conn_bfd) != 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100260 LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100261 close(conn_bfd->fd);
262 conn_bfd->fd = -1;
Harald Welte31bbbf42010-12-23 00:02:51 +0100263 return -1;
264 }
265
Harald Welte0d6f9302010-12-23 02:47:28 +0100266 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external "
267 "call control application\n");
268
Holger Hans Peter Freyther347e7ba2011-10-21 14:12:46 +0200269 queue_hello(state);
Harald Welte31bbbf42010-12-23 00:02:51 +0100270 return 0;
271}
272
273
274int mncc_sock_init(struct gsm_network *net)
275{
276 struct mncc_sock_state *state;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200277 struct osmo_fd *bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100278 int rc;
279
280 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
281 if (!state)
282 return -ENOMEM;
283
284 state->net = net;
285 state->conn_bfd.fd = -1;
286
287 bfd = &state->listen_bfd;
288
289 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
290 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100291 LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s\n",
292 strerror(errno));
Harald Welte31bbbf42010-12-23 00:02:51 +0100293 talloc_free(state);
294 return rc;
295 }
296
297 bfd->when = BSC_FD_READ;
298 bfd->cb = mncc_sock_accept;
299 bfd->data = state;
300
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200301 rc = osmo_fd_register(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +0100302 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100303 LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc);
Harald Welte31bbbf42010-12-23 00:02:51 +0100304 close(bfd->fd);
305 talloc_free(state);
306 return rc;
307 }
308
Holger Hans Peter Freyther694c82d2011-10-26 18:37:09 +0200309 net->mncc_state = state;
Harald Welte31bbbf42010-12-23 00:02:51 +0100310
311 return 0;
312}
313
314/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200315int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path)
Harald Welte31bbbf42010-12-23 00:02:51 +0100316{
317 struct sockaddr_un local;
318 unsigned int namelen;
319 int rc;
320
321 bfd->fd = socket(AF_UNIX, type, 0);
322
323 if (bfd->fd < 0) {
324 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
325 return -1;
326 }
327
328 local.sun_family = AF_UNIX;
329 strncpy(local.sun_path, path, sizeof(local.sun_path));
330 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
331 unlink(local.sun_path);
332
333 /* we use the same magic that X11 uses in Xtranssock.c for
334 * calculating the proper length of the sockaddr */
335#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
336 local.sun_len = strlen(local.sun_path);
337#endif
338#if defined(BSD44SOCKETS) || defined(SUN_LEN)
339 namelen = SUN_LEN(&local);
340#else
341 namelen = strlen(local.sun_path) +
342 offsetof(struct sockaddr_un, sun_path);
343#endif
344
345 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
346 if (rc != 0) {
347 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
348 local.sun_path);
349 return -1;
350 }
351
352 if (listen(bfd->fd, 0) != 0) {
353 fprintf(stderr, "Failed to listen.\n");
354 return -1;
355 }
356
357 return 0;
358}