blob: 30520091ecf93254638bfd4cccc3079cfc83aa2e [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{
53 struct gsm_mncc *mncc_in = msgb_data(msg);
54 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:
147 mncc_sock_close(state);
148 return -1;
149}
150
151static int mncc_sock_write(struct bsc_fd *bfd)
152{
153 struct mncc_sock_state *state = bfd->data;
154 struct gsm_network *net = state->net;
155 int rc;
156
157 while (!llist_empty(&net->upqueue)) {
Harald Welte49a2dde2010-12-22 22:56:15 +0100158 struct msgb *msg, *msg2;
Harald Welte31bbbf42010-12-23 00:02:51 +0100159 struct gsm_mncc *mncc_prim;
160
161 /* peek at the beginning of the queue */
162 msg = llist_entry(net->upqueue.next, struct msgb, list);
163 mncc_prim = (struct gsm_mncc *)msg->data;
164
165 bfd->when &= ~BSC_FD_WRITE;
166
167 /* try to send it over the socket */
168 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
169 if (rc == 0)
170 goto close;
171 if (rc < 0) {
172 if (errno == EAGAIN) {
173 bfd->when |= BSC_FD_WRITE;
174 break;
175 }
176 goto close;
177 }
178 /* _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
191static int mncc_sock_cb(struct bsc_fd *bfd, unsigned int flags)
192{
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
206/* accept a new connection */
207static int mncc_sock_accept(struct bsc_fd *bfd, unsigned int flags)
208{
209 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
210 struct bsc_fd *conn_bfd = &state->conn_bfd;
211 struct sockaddr_un un_addr;
212 socklen_t len;
213 int rc;
214
215 len = sizeof(un_addr);
216 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
217 if (rc < 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100218 LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100219 return -1;
220 }
221
Harald Welte0d6f9302010-12-23 02:47:28 +0100222 if (conn_bfd->fd >= 0) {
223 LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have "
224 "another active connection ?!?\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100225 /* We already have one MNCC app connected, this is all we support */
226 state->listen_bfd.when &= ~BSC_FD_READ;
227 close(rc);
Harald Welte0d6f9302010-12-23 02:47:28 +0100228 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100229 }
230
231 conn_bfd->fd = rc;
232 conn_bfd->when = BSC_FD_READ;
233 conn_bfd->cb = mncc_sock_cb;
234 conn_bfd->data = state;
235
236 if (bsc_register_fd(conn_bfd) != 0) {
Harald Welte0d6f9302010-12-23 02:47:28 +0100237 LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n");
Harald Welte31bbbf42010-12-23 00:02:51 +0100238 close(conn_bfd->fd);
239 conn_bfd->fd = -1;
240 state->listen_bfd.when |= ~BSC_FD_READ;
241 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;
254 struct bsc_fd *bfd;
255 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
278 rc = bsc_register_fd(bfd);
279 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 */
292int osmo_unixsock_listen(struct bsc_fd *bfd, int type, const char *path)
293{
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}