blob: d8caf075448076a4c5167725cae0a157a197a6ad [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
46/* FIXME: avoid this */
47static struct mncc_sock_state *g_state;
48
Harald Welteea057d92010-12-23 01:26:29 +010049/* input from CC code into mncc_sock */
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010050int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg)
Harald Welteea057d92010-12-23 01:26:29 +010051{
Holger Hans Peter Freyther02d45c02011-01-06 13:50:52 +010052 struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg);
Harald Welteea057d92010-12-23 01:26:29 +010053 int msg_type = mncc_in->msg_type;
54
55 /* Check if we currently have a MNCC handler connected */
56 if (g_state->conn_bfd.fd < 0) {
57 LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app "
58 "but socket is gone\n", get_mncc_name(msg_type));
59 if (msg_type != GSM_TCHF_FRAME &&
60 msg_type != GSM_TCHF_FRAME_EFR) {
61 /* release the request */
62 struct gsm_mncc mncc_out;
63 memset(&mncc_out, 0, sizeof(mncc_out));
64 mncc_out.callref = mncc_in->callref;
65 mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU,
66 GSM48_CC_CAUSE_TEMP_FAILURE);
67 mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out);
68 }
69 /* free the original message */
70 msgb_free(msg);
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010071 return -1;
Harald Welteea057d92010-12-23 01:26:29 +010072 }
73
74 /* FIXME: check for some maximum queue depth? */
75
76 /* Actually enqueue the message and mark socket write need */
77 msgb_enqueue(&net->upqueue, msg);
78 g_state->conn_bfd.when |= BSC_FD_WRITE;
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010079 return 0;
Harald Welteea057d92010-12-23 01:26:29 +010080}
81
Harald Welte31bbbf42010-12-23 00:02:51 +010082void mncc_sock_write_pending(void)
83{
84 g_state->conn_bfd.when |= BSC_FD_WRITE;
85}
86
87/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020088int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path);
Harald Welte31bbbf42010-12-23 00:02:51 +010089
90static void mncc_sock_close(struct mncc_sock_state *state)
91{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020092 struct osmo_fd *bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +010093
Harald Welte0d6f9302010-12-23 02:47:28 +010094 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n");
95
Harald Welte31bbbf42010-12-23 00:02:51 +010096 close(bfd->fd);
97 bfd->fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020098 osmo_fd_unregister(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +010099
100 /* re-enable the generation of ACCEPT for new connections */
101 state->listen_bfd.when |= BSC_FD_READ;
102
103 /* FIXME: make sure we don't enqueue anymore */
Harald Welte371efe52010-12-22 23:17:50 +0100104
105 /* release all exisitng calls */
106 gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC);
Harald Welte31bbbf42010-12-23 00:02:51 +0100107
108 /* flush the queue */
109 while (!llist_empty(&state->net->upqueue)) {
110 struct msgb *msg = msgb_dequeue(&state->net->upqueue);
111 msgb_free(msg);
112 }
113}
114
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200115static int mncc_sock_read(struct osmo_fd *bfd)
Harald Welte31bbbf42010-12-23 00:02:51 +0100116{
117 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
118 struct gsm_mncc *mncc_prim;
119 struct msgb *msg;
120 int rc;
121
122 msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx");
123 if (!msg)
124 return -ENOMEM;
125
126 mncc_prim = (struct gsm_mncc *) msg->tail;
127
128 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
129 if (rc == 0)
130 goto close;
131
132 if (rc < 0) {
133 if (errno == EAGAIN)
134 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100135 goto close;
136 }
137
138 rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim);
139
140 /* as we always synchronously process the message in mncc_send() and
141 * its callbacks, we can free the message here. */
142 msgb_free(msg);
143
144 return rc;
145
146close:
Harald Welteeb76c7a2010-12-23 02:47:53 +0100147 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100148 mncc_sock_close(state);
149 return -1;
150}
151
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200152static int mncc_sock_write(struct osmo_fd *bfd)
Harald Welte31bbbf42010-12-23 00:02:51 +0100153{
154 struct mncc_sock_state *state = bfd->data;
155 struct gsm_network *net = state->net;
156 int rc;
157
158 while (!llist_empty(&net->upqueue)) {
Harald Welte49a2dde2010-12-22 22:56:15 +0100159 struct msgb *msg, *msg2;
Harald Welte31bbbf42010-12-23 00:02:51 +0100160 struct gsm_mncc *mncc_prim;
161
162 /* peek at the beginning of the queue */
163 msg = llist_entry(net->upqueue.next, struct msgb, list);
164 mncc_prim = (struct gsm_mncc *)msg->data;
165
166 bfd->when &= ~BSC_FD_WRITE;
167
Peter Stuge46f799b2011-08-11 04:37:17 +0200168 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
169 if (!msgb_length(msg)) {
170 LOGP(DMNCC, LOGL_ERROR, "message type (%d) with ZERO "
171 "bytes!\n", mncc_prim->msg_type);
172 goto dontsend;
173 }
174
Harald Welte31bbbf42010-12-23 00:02:51 +0100175 /* try to send it over the socket */
176 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
177 if (rc == 0)
178 goto close;
179 if (rc < 0) {
180 if (errno == EAGAIN) {
181 bfd->when |= BSC_FD_WRITE;
182 break;
183 }
184 goto close;
185 }
Peter Stuge46f799b2011-08-11 04:37:17 +0200186
187dontsend:
Harald Welte31bbbf42010-12-23 00:02:51 +0100188 /* _after_ we send it, we can deueue */
Harald Welte49a2dde2010-12-22 22:56:15 +0100189 msg2 = msgb_dequeue(&net->upqueue);
190 assert(msg == msg2);
191 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100192 }
193 return 0;
194
195close:
196 mncc_sock_close(state);
197
198 return -1;
199}
200
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200201static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100202{
203 int rc = 0;
204
205 if (flags & BSC_FD_READ)
206 rc = mncc_sock_read(bfd);
207 if (rc < 0)
208 return rc;
209
210 if (flags & BSC_FD_WRITE)
211 rc = mncc_sock_write(bfd);
212
213 return rc;
214}
215
216/* accept a new connection */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200217static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100218{
219 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200220 struct osmo_fd *conn_bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100221 struct sockaddr_un un_addr;
222 socklen_t len;
223 int rc;
224
225 len = sizeof(un_addr);
226 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
227 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100228 LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100229 return -1;
230 }
231
Harald Welte0d6f9302010-12-23 02:47:28 +0100232 if (conn_bfd->fd >= 0) {
233 LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have "
234 "another active connection ?!?\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100235 /* We already have one MNCC app connected, this is all we support */
236 state->listen_bfd.when &= ~BSC_FD_READ;
237 close(rc);
Harald Welte0d6f9302010-12-23 02:47:28 +0100238 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100239 }
240
241 conn_bfd->fd = rc;
242 conn_bfd->when = BSC_FD_READ;
243 conn_bfd->cb = mncc_sock_cb;
244 conn_bfd->data = state;
245
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200246 if (osmo_fd_register(conn_bfd) != 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100247 LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100248 close(conn_bfd->fd);
249 conn_bfd->fd = -1;
Harald Welte31bbbf42010-12-23 00:02:51 +0100250 return -1;
251 }
252
Harald Welte0d6f9302010-12-23 02:47:28 +0100253 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external "
254 "call control application\n");
255
Harald Welte31bbbf42010-12-23 00:02:51 +0100256 return 0;
257}
258
259
260int mncc_sock_init(struct gsm_network *net)
261{
262 struct mncc_sock_state *state;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200263 struct osmo_fd *bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100264 int rc;
265
266 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
267 if (!state)
268 return -ENOMEM;
269
270 state->net = net;
271 state->conn_bfd.fd = -1;
272
273 bfd = &state->listen_bfd;
274
275 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
276 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100277 LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s\n",
278 strerror(errno));
Harald Welte31bbbf42010-12-23 00:02:51 +0100279 talloc_free(state);
280 return rc;
281 }
282
283 bfd->when = BSC_FD_READ;
284 bfd->cb = mncc_sock_accept;
285 bfd->data = state;
286
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200287 rc = osmo_fd_register(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +0100288 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100289 LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc);
Harald Welte31bbbf42010-12-23 00:02:51 +0100290 close(bfd->fd);
291 talloc_free(state);
292 return rc;
293 }
294
295 g_state = state;
296
297 return 0;
298}
299
300/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200301int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path)
Harald Welte31bbbf42010-12-23 00:02:51 +0100302{
303 struct sockaddr_un local;
304 unsigned int namelen;
305 int rc;
306
307 bfd->fd = socket(AF_UNIX, type, 0);
308
309 if (bfd->fd < 0) {
310 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
311 return -1;
312 }
313
314 local.sun_family = AF_UNIX;
315 strncpy(local.sun_path, path, sizeof(local.sun_path));
316 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
317 unlink(local.sun_path);
318
319 /* we use the same magic that X11 uses in Xtranssock.c for
320 * calculating the proper length of the sockaddr */
321#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
322 local.sun_len = strlen(local.sun_path);
323#endif
324#if defined(BSD44SOCKETS) || defined(SUN_LEN)
325 namelen = SUN_LEN(&local);
326#else
327 namelen = strlen(local.sun_path) +
328 offsetof(struct sockaddr_un, sun_path);
329#endif
330
331 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
332 if (rc != 0) {
333 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
334 local.sun_path);
335 return -1;
336 }
337
338 if (listen(bfd->fd, 0) != 0) {
339 fprintf(stderr, "Failed to listen.\n");
340 return -1;
341 }
342
343 return 0;
344}