blob: 3ab40a8d9364e5b2f0cc2125328683cb5526a008 [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
Harald Welteea057d92010-12-23 01:26:29 +010046/* input from CC code into mncc_sock */
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010047int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg)
Harald Welteea057d92010-12-23 01:26:29 +010048{
Holger Hans Peter Freyther02d45c02011-01-06 13:50:52 +010049 struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg);
Harald Welteea057d92010-12-23 01:26:29 +010050 int msg_type = mncc_in->msg_type;
51
52 /* Check if we currently have a MNCC handler connected */
Holger Hans Peter Freyther694c82d2011-10-26 18:37:09 +020053 if (net->mncc_state->conn_bfd.fd < 0) {
Harald Welteea057d92010-12-23 01:26:29 +010054 LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app "
55 "but socket is gone\n", get_mncc_name(msg_type));
56 if (msg_type != GSM_TCHF_FRAME &&
57 msg_type != GSM_TCHF_FRAME_EFR) {
58 /* release the request */
59 struct gsm_mncc mncc_out;
60 memset(&mncc_out, 0, sizeof(mncc_out));
61 mncc_out.callref = mncc_in->callref;
62 mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU,
63 GSM48_CC_CAUSE_TEMP_FAILURE);
64 mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out);
65 }
66 /* free the original message */
67 msgb_free(msg);
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010068 return -1;
Harald Welteea057d92010-12-23 01:26:29 +010069 }
70
71 /* FIXME: check for some maximum queue depth? */
72
73 /* Actually enqueue the message and mark socket write need */
74 msgb_enqueue(&net->upqueue, msg);
Holger Hans Peter Freyther694c82d2011-10-26 18:37:09 +020075 net->mncc_state->conn_bfd.when |= BSC_FD_WRITE;
Holger Hans Peter Freyther1cc71842011-01-06 14:13:44 +010076 return 0;
Harald Welteea057d92010-12-23 01:26:29 +010077}
78
Harald Welte31bbbf42010-12-23 00:02:51 +010079/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020080int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path);
Harald Welte31bbbf42010-12-23 00:02:51 +010081
82static void mncc_sock_close(struct mncc_sock_state *state)
83{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020084 struct osmo_fd *bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +010085
Harald Welte0d6f9302010-12-23 02:47:28 +010086 LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n");
87
Harald Welte31bbbf42010-12-23 00:02:51 +010088 close(bfd->fd);
89 bfd->fd = -1;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020090 osmo_fd_unregister(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +010091
92 /* re-enable the generation of ACCEPT for new connections */
93 state->listen_bfd.when |= BSC_FD_READ;
94
95 /* FIXME: make sure we don't enqueue anymore */
Harald Welte371efe52010-12-22 23:17:50 +010096
97 /* release all exisitng calls */
98 gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC);
Harald Welte31bbbf42010-12-23 00:02:51 +010099
100 /* flush the queue */
101 while (!llist_empty(&state->net->upqueue)) {
102 struct msgb *msg = msgb_dequeue(&state->net->upqueue);
103 msgb_free(msg);
104 }
105}
106
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200107static int mncc_sock_read(struct osmo_fd *bfd)
Harald Welte31bbbf42010-12-23 00:02:51 +0100108{
109 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
110 struct gsm_mncc *mncc_prim;
111 struct msgb *msg;
112 int rc;
113
114 msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx");
115 if (!msg)
116 return -ENOMEM;
117
118 mncc_prim = (struct gsm_mncc *) msg->tail;
119
120 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
121 if (rc == 0)
122 goto close;
123
124 if (rc < 0) {
125 if (errno == EAGAIN)
126 return 0;
Harald Welte31bbbf42010-12-23 00:02:51 +0100127 goto close;
128 }
129
130 rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim);
131
132 /* as we always synchronously process the message in mncc_send() and
133 * its callbacks, we can free the message here. */
134 msgb_free(msg);
135
136 return rc;
137
138close:
Harald Welteeb76c7a2010-12-23 02:47:53 +0100139 msgb_free(msg);
Harald Welte31bbbf42010-12-23 00:02:51 +0100140 mncc_sock_close(state);
141 return -1;
142}
143
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200144static int mncc_sock_write(struct osmo_fd *bfd)
Harald Welte31bbbf42010-12-23 00:02:51 +0100145{
146 struct mncc_sock_state *state = bfd->data;
147 struct gsm_network *net = state->net;
148 int rc;
149
150 while (!llist_empty(&net->upqueue)) {
Harald Welte49a2dde2010-12-22 22:56:15 +0100151 struct msgb *msg, *msg2;
Harald Welte31bbbf42010-12-23 00:02:51 +0100152 struct gsm_mncc *mncc_prim;
153
154 /* peek at the beginning of the queue */
155 msg = llist_entry(net->upqueue.next, struct msgb, list);
156 mncc_prim = (struct gsm_mncc *)msg->data;
157
158 bfd->when &= ~BSC_FD_WRITE;
159
Peter Stuge46f799b2011-08-11 04:37:17 +0200160 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
161 if (!msgb_length(msg)) {
162 LOGP(DMNCC, LOGL_ERROR, "message type (%d) with ZERO "
163 "bytes!\n", mncc_prim->msg_type);
164 goto dontsend;
165 }
166
Harald Welte31bbbf42010-12-23 00:02:51 +0100167 /* 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 }
Peter Stuge46f799b2011-08-11 04:37:17 +0200178
179dontsend:
Harald Welte31bbbf42010-12-23 00:02:51 +0100180 /* _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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200193static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100194{
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 */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200209static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags)
Harald Welte31bbbf42010-12-23 00:02:51 +0100210{
211 struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200212 struct osmo_fd *conn_bfd = &state->conn_bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100213 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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200238 if (osmo_fd_register(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;
Harald Welte31bbbf42010-12-23 00:02:51 +0100242 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;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200255 struct osmo_fd *bfd;
Harald Welte31bbbf42010-12-23 00:02:51 +0100256 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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200279 rc = osmo_fd_register(bfd);
Harald Welte31bbbf42010-12-23 00:02:51 +0100280 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
Holger Hans Peter Freyther694c82d2011-10-26 18:37:09 +0200287 net->mncc_state = state;
Harald Welte31bbbf42010-12-23 00:02:51 +0100288
289 return 0;
290}
291
292/* FIXME: move this to libosmocore */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200293int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path)
Harald Welte31bbbf42010-12-23 00:02:51 +0100294{
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}