blob: 576ed017a8d93d5a938ae39f70c3d4f25506b2c2 [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/types.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010033#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
35#include <osmocom/gsm/protocol/gsm_04_08.h>
Harald Welte31bbbf42010-12-23 00:02:51 +010036
37#include <openbsc/debug.h>
38#include <openbsc/mncc.h>
39#include <openbsc/gsm_data.h>
40
41struct mncc_sock_state {
42 struct gsm_network *net;
43 struct bsc_fd listen_bfd; /* fd for listen socket */
44 struct bsc_fd conn_bfd; /* fd for connection to lcr */
45};
46
47/* FIXME: avoid this */
48static struct mncc_sock_state *g_state;
49
Harald Welteea057d92010-12-23 01:26:29 +010050/* input from CC code into mncc_sock */
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010051int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg)
Harald Welteea057d92010-12-23 01:26:29 +010052{
Holger Hans Peter Freyther02d45c02011-01-06 13:50:52 +010053 struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg);
Harald Welteea057d92010-12-23 01:26:29 +010054 int msg_type = mncc_in->msg_type;
55
56 /* Check if we currently have a MNCC handler connected */
57 if (g_state->conn_bfd.fd < 0) {
58 LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app "
59 "but socket is gone\n", get_mncc_name(msg_type));
60 if (msg_type != GSM_TCHF_FRAME &&
61 msg_type != GSM_TCHF_FRAME_EFR) {
62 /* release the request */
63 struct gsm_mncc mncc_out;
64 memset(&mncc_out, 0, sizeof(mncc_out));
65 mncc_out.callref = mncc_in->callref;
66 mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU,
67 GSM48_CC_CAUSE_TEMP_FAILURE);
68 mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out);
69 }
70 /* free the original message */
71 msgb_free(msg);
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010072 return -1;
Harald Welteea057d92010-12-23 01:26:29 +010073 }
74
75 /* FIXME: check for some maximum queue depth? */
76
77 /* Actually enqueue the message and mark socket write need */
78 msgb_enqueue(&net->upqueue, msg);
79 g_state->conn_bfd.when |= BSC_FD_WRITE;
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010080 return 0;
Harald Welteea057d92010-12-23 01:26:29 +010081}
82
Harald Welte31bbbf42010-12-23 00:02:51 +010083void mncc_sock_write_pending(void)
84{
85 g_state->conn_bfd.when |= BSC_FD_WRITE;
86}
87
88/* FIXME: move this to libosmocore */
89int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path);
90
91static void mncc_sock_close(struct mncc_sock_state *state)
92{
93 struct bsc_fd *bfd = &state->conn_bfd;
94
Harald Welte0d6f9302010-12-23 02:47:28 +010095 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n");
96
Harald Welte31bbbf42010-12-23 00:02:51 +010097 close(bfd->fd);
98 bfd->fd = -1;
99 bsc_unregister_fd(bfd);
100
101 /* re-enable the generation of ACCEPT for new connections */
102 state->listen_bfd.when |= BSC_FD_READ;
103
104 /* FIXME: make sure we don't enqueue anymore */
Harald Welte371efe52010-12-22 23:17:50 +0100105
106 /* release all exisitng calls */
107 gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC);
Harald Welte31bbbf42010-12-23 00:02:51 +0100108
109 /* flush the queue */
110 while (!llist_empty(&state->net->upqueue)) {
111 struct msgb *msg = msgb_dequeue(&state->net->upqueue);
112 msgb_free(msg);
113 }
114}
115
116static int mncc_sock_read(struct bsc_fd *bfd)
117{
118 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
119 struct gsm_mncc *mncc_prim;
120 struct msgb *msg;
121 int rc;
122
123 msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx");
124 if (!msg)
125 return -ENOMEM;
126
127 mncc_prim = (struct gsm_mncc *) msg->tail;
128
129 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
130 if (rc == 0)
131 goto close;
132
133 if (rc < 0) {
134 if (errno == EAGAIN)
135 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100136 goto close;
137 }
138
139 rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim);
140
141 /* as we always synchronously process the message in mncc_send() and
142 * its callbacks, we can free the message here. */
143 msgb_free(msg);
144
145 return rc;
146
147close:
Harald Welteeb76c7a2010-12-23 02:47:53 +0100148 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100149 mncc_sock_close(state);
150 return -1;
151}
152
153static int mncc_sock_write(struct bsc_fd *bfd)
154{
155 struct mncc_sock_state *state = bfd->data;
156 struct gsm_network *net = state->net;
157 int rc;
158
159 while (!llist_empty(&net->upqueue)) {
Harald Welte49a2dde2010-12-22 22:56:15 +0100160 struct msgb *msg, *msg2;
Harald Welte31bbbf42010-12-23 00:02:51 +0100161 struct gsm_mncc *mncc_prim;
162
163 /* peek at the beginning of the queue */
164 msg = llist_entry(net->upqueue.next, struct msgb, list);
165 mncc_prim = (struct gsm_mncc *)msg->data;
166
167 bfd->when &= ~BSC_FD_WRITE;
168
169 /* try to send it over the socket */
170 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
171 if (rc == 0)
172 goto close;
173 if (rc < 0) {
174 if (errno == EAGAIN) {
175 bfd->when |= BSC_FD_WRITE;
176 break;
177 }
178 goto close;
179 }
180 /* _after_ we send it, we can deueue */
Harald Welte49a2dde2010-12-22 22:56:15 +0100181 msg2 = msgb_dequeue(&net->upqueue);
182 assert(msg == msg2);
183 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100184 }
185 return 0;
186
187close:
188 mncc_sock_close(state);
189
190 return -1;
191}
192
193static int mncc_sock_cb(struct bsc_fd *bfd, unsigned int flags)
194{
195 int rc = 0;
196
197 if (flags & BSC_FD_READ)
198 rc = mncc_sock_read(bfd);
199 if (rc < 0)
200 return rc;
201
202 if (flags & BSC_FD_WRITE)
203 rc = mncc_sock_write(bfd);
204
205 return rc;
206}
207
208/* accept a new connection */
209static int mncc_sock_accept(struct bsc_fd *bfd, unsigned int flags)
210{
211 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
212 struct bsc_fd *conn_bfd = &state->conn_bfd;
213 struct sockaddr_un un_addr;
214 socklen_t len;
215 int rc;
216
217 len = sizeof(un_addr);
218 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
219 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100220 LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100221 return -1;
222 }
223
Harald Welte0d6f9302010-12-23 02:47:28 +0100224 if (conn_bfd->fd >= 0) {
225 LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have "
226 "another active connection ?!?\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100227 /* We already have one MNCC app connected, this is all we support */
228 state->listen_bfd.when &= ~BSC_FD_READ;
229 close(rc);
Harald Welte0d6f9302010-12-23 02:47:28 +0100230 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100231 }
232
233 conn_bfd->fd = rc;
234 conn_bfd->when = BSC_FD_READ;
235 conn_bfd->cb = mncc_sock_cb;
236 conn_bfd->data = state;
237
238 if (bsc_register_fd(conn_bfd) != 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100239 LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100240 close(conn_bfd->fd);
241 conn_bfd->fd = -1;
242 state->listen_bfd.when |= ~BSC_FD_READ;
243 return -1;
244 }
245
Harald Welte0d6f9302010-12-23 02:47:28 +0100246 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external "
247 "call control application\n");
248
Harald Welte31bbbf42010-12-23 00:02:51 +0100249 return 0;
250}
251
252
253int mncc_sock_init(struct gsm_network *net)
254{
255 struct mncc_sock_state *state;
256 struct bsc_fd *bfd;
257 int rc;
258
259 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
260 if (!state)
261 return -ENOMEM;
262
263 state->net = net;
264 state->conn_bfd.fd = -1;
265
266 bfd = &state->listen_bfd;
267
268 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
269 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100270 LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s\n",
271 strerror(errno));
Harald Welte31bbbf42010-12-23 00:02:51 +0100272 talloc_free(state);
273 return rc;
274 }
275
276 bfd->when = BSC_FD_READ;
277 bfd->cb = mncc_sock_accept;
278 bfd->data = state;
279
280 rc = bsc_register_fd(bfd);
281 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100282 LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc);
Harald Welte31bbbf42010-12-23 00:02:51 +0100283 close(bfd->fd);
284 talloc_free(state);
285 return rc;
286 }
287
288 g_state = state;
289
290 return 0;
291}
292
293/* FIXME: move this to libosmocore */
294int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path)
295{
296 struct sockaddr_un local;
297 unsigned int namelen;
298 int rc;
299
300 bfd->fd = socket(AF_UNIX, type, 0);
301
302 if (bfd->fd < 0) {
303 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
304 return -1;
305 }
306
307 local.sun_family = AF_UNIX;
308 strncpy(local.sun_path, path, sizeof(local.sun_path));
309 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
310 unlink(local.sun_path);
311
312 /* we use the same magic that X11 uses in Xtranssock.c for
313 * calculating the proper length of the sockaddr */
314#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
315 local.sun_len = strlen(local.sun_path);
316#endif
317#if defined(BSD44SOCKETS) || defined(SUN_LEN)
318 namelen = SUN_LEN(&local);
319#else
320 namelen = strlen(local.sun_path) +
321 offsetof(struct sockaddr_un, sun_path);
322#endif
323
324 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
325 if (rc != 0) {
326 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
327 local.sun_path);
328 return -1;
329 }
330
331 if (listen(bfd->fd, 0) != 0) {
332 fprintf(stderr, "Failed to listen.\n");
333 return -1;
334 }
335
336 return 0;
337}