blob: 3f13075ffc54d38f577ce9c35c9222c92fcd41d3 [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
33#include <osmocore/talloc.h>
34#include <osmocore/select.h>
Harald Welteea057d92010-12-23 01:26:29 +010035#include <osmocore/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 */
51void mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg)
52{
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);
72 return;
73 }
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;
80}
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 */
88int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path);
89
90static void mncc_sock_close(struct mncc_sock_state *state)
91{
92 struct bsc_fd *bfd = &state->conn_bfd;
93
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;
98 bsc_unregister_fd(bfd);
99
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
115static int mncc_sock_read(struct bsc_fd *bfd)
116{
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
152static int mncc_sock_write(struct bsc_fd *bfd)
153{
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
192static int mncc_sock_cb(struct bsc_fd *bfd, unsigned int flags)
193{
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 */
208static int mncc_sock_accept(struct bsc_fd *bfd, unsigned int flags)
209{
210 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
211 struct bsc_fd *conn_bfd = &state->conn_bfd;
212 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
237 if (bsc_register_fd(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;
241 state->listen_bfd.when |= ~BSC_FD_READ;
242 return -1;
243 }
244
Harald Welte0d6f9302010-12-23 02:47:28 +0100245 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external "
246 "call control application\n");
247
Harald Welte31bbbf42010-12-23 00:02:51 +0100248 return 0;
249}
250
251
252int mncc_sock_init(struct gsm_network *net)
253{
254 struct mncc_sock_state *state;
255 struct bsc_fd *bfd;
256 int rc;
257
258 state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state);
259 if (!state)
260 return -ENOMEM;
261
262 state->net = net;
263 state->conn_bfd.fd = -1;
264
265 bfd = &state->listen_bfd;
266
267 rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc");
268 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100269 LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s\n",
270 strerror(errno));
Harald Welte31bbbf42010-12-23 00:02:51 +0100271 talloc_free(state);
272 return rc;
273 }
274
275 bfd->when = BSC_FD_READ;
276 bfd->cb = mncc_sock_accept;
277 bfd->data = state;
278
279 rc = bsc_register_fd(bfd);
280 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100281 LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc);
Harald Welte31bbbf42010-12-23 00:02:51 +0100282 close(bfd->fd);
283 talloc_free(state);
284 return rc;
285 }
286
287 g_state = state;
288
289 return 0;
290}
291
292/* FIXME: move this to libosmocore */
293int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path)
294{
295 struct sockaddr_un local;
296 unsigned int namelen;
297 int rc;
298
299 bfd->fd = socket(AF_UNIX, type, 0);
300
301 if (bfd->fd < 0) {
302 fprintf(stderr, "Failed to create Unix Domain Socket.\n");
303 return -1;
304 }
305
306 local.sun_family = AF_UNIX;
307 strncpy(local.sun_path, path, sizeof(local.sun_path));
308 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
309 unlink(local.sun_path);
310
311 /* we use the same magic that X11 uses in Xtranssock.c for
312 * calculating the proper length of the sockaddr */
313#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
314 local.sun_len = strlen(local.sun_path);
315#endif
316#if defined(BSD44SOCKETS) || defined(SUN_LEN)
317 namelen = SUN_LEN(&local);
318#else
319 namelen = strlen(local.sun_path) +
320 offsetof(struct sockaddr_un, sun_path);
321#endif
322
323 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
324 if (rc != 0) {
325 fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n",
326 local.sun_path);
327 return -1;
328 }
329
330 if (listen(bfd->fd, 0) != 0) {
331 fprintf(stderr, "Failed to listen.\n");
332 return -1;
333 }
334
335 return 0;
336}