blob: 5ef9922d290eddd12f97bed3a07c3c9081275637 [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
168 /* try to send it over the socket */
169 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
170 if (rc == 0)
171 goto close;
172 if (rc < 0) {
173 if (errno == EAGAIN) {
174 bfd->when |= BSC_FD_WRITE;
175 break;
176 }
177 goto close;
178 }
179 /* _after_ we send it, we can deueue */
Harald Welte49a2dde2010-12-22 22:56:15 +0100180 msg2 = msgb_dequeue(&net->upqueue);
181 assert(msg == msg2);
182 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100183 }
184 return 0;
185
186close:
187 mncc_sock_close(state);
188
189 return -1;
190}
191
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200192static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100193{
194 int rc = 0;
195
196 if (flags & BSC_FD_READ)
197 rc = mncc_sock_read(bfd);
198 if (rc < 0)
199 return rc;
200
201 if (flags & BSC_FD_WRITE)
202 rc = mncc_sock_write(bfd);
203
204 return rc;
205}
206
207/* accept a new connection */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200208static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100209{
210 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200211 struct osmo_fd *conn_bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100212 struct sockaddr_un un_addr;
213 socklen_t len;
214 int rc;
215
216 len = sizeof(un_addr);
217 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
218 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100219 LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100220 return -1;
221 }
222
Harald Welte0d6f9302010-12-23 02:47:28 +0100223 if (conn_bfd->fd >= 0) {
224 LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have "
225 "another active connection ?!?\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100226 /* We already have one MNCC app connected, this is all we support */
227 state->listen_bfd.when &= ~BSC_FD_READ;
228 close(rc);
Harald Welte0d6f9302010-12-23 02:47:28 +0100229 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100230 }
231
232 conn_bfd->fd = rc;
233 conn_bfd->when = BSC_FD_READ;
234 conn_bfd->cb = mncc_sock_cb;
235 conn_bfd->data = state;
236
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200237 if (osmo_fd_register(conn_bfd) != 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100238 LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100239 close(conn_bfd->fd);
240 conn_bfd->fd = -1;
Harald Welte31bbbf42010-12-23 00:02:51 +0100241 return -1;
242 }
243
Harald Welte0d6f9302010-12-23 02:47:28 +0100244 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external "
245 "call control application\n");
246
Harald Welte31bbbf42010-12-23 00:02:51 +0100247 return 0;
248}
249
250
251int mncc_sock_init(struct gsm_network *net)
252{
253 struct mncc_sock_state *state;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200254 struct osmo_fd *bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100255 int rc;
256
257 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
258 if (!state)
259 return -ENOMEM;
260
261 state->net = net;
262 state->conn_bfd.fd = -1;
263
264 bfd = &state->listen_bfd;
265
266 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
267 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100268 LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s\n",
269 strerror(errno));
Harald Welte31bbbf42010-12-23 00:02:51 +0100270 talloc_free(state);
271 return rc;
272 }
273
274 bfd->when = BSC_FD_READ;
275 bfd->cb = mncc_sock_accept;
276 bfd->data = state;
277
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200278 rc = osmo_fd_register(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +0100279 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100280 LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc);
Harald Welte31bbbf42010-12-23 00:02:51 +0100281 close(bfd->fd);
282 talloc_free(state);
283 return rc;
284 }
285
286 g_state = state;
287
288 return 0;
289}
290
291/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200292int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path)
Harald Welte31bbbf42010-12-23 00:02:51 +0100293{
294 struct sockaddr_un local;
295 unsigned int namelen;
296 int rc;
297
298 bfd->fd = socket(AF_UNIX, type, 0);
299
300 if (bfd->fd < 0) {
301 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
302 return -1;
303 }
304
305 local.sun_family = AF_UNIX;
306 strncpy(local.sun_path, path, sizeof(local.sun_path));
307 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
308 unlink(local.sun_path);
309
310 /* we use the same magic that X11 uses in Xtranssock.c for
311 * calculating the proper length of the sockaddr */
312#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
313 local.sun_len = strlen(local.sun_path);
314#endif
315#if defined(BSD44SOCKETS) || defined(SUN_LEN)
316 namelen = SUN_LEN(&local);
317#else
318 namelen = strlen(local.sun_path) +
319 offsetof(struct sockaddr_un, sun_path);
320#endif
321
322 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
323 if (rc != 0) {
324 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
325 local.sun_path);
326 return -1;
327 }
328
329 if (listen(bfd->fd, 0) != 0) {
330 fprintf(stderr, "Failed to listen.\n");
331 return -1;
332 }
333
334 return 0;
335}