Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 1 | /* 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 Welte | 49a2dde | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 28 | #include <assert.h> |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
| 31 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
| 33 | #include <osmocom/core/select.h> |
| 34 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 35 | |
| 36 | #include <openbsc/debug.h> |
| 37 | #include <openbsc/mncc.h> |
| 38 | #include <openbsc/gsm_data.h> |
| 39 | |
| 40 | struct mncc_sock_state { |
| 41 | struct gsm_network *net; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 42 | struct osmo_fd listen_bfd; /* fd for listen socket */ |
| 43 | struct osmo_fd conn_bfd; /* fd for connection to lcr */ |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* FIXME: avoid this */ |
| 47 | static struct mncc_sock_state *g_state; |
| 48 | |
Harald Welte | ea057d9 | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 49 | /* input from CC code into mncc_sock */ |
Holger Hans Peter Freyther | 1cc7184 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 50 | int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg) |
Harald Welte | ea057d9 | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 51 | { |
Holger Hans Peter Freyther | 02d45c0 | 2011-01-06 13:50:52 +0100 | [diff] [blame] | 52 | struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg); |
Harald Welte | ea057d9 | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 53 | 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 Freyther | 1cc7184 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 71 | return -1; |
Harald Welte | ea057d9 | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 72 | } |
| 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 Freyther | 1cc7184 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 79 | return 0; |
Harald Welte | ea057d9 | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 82 | void 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 Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 88 | int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 89 | |
| 90 | static void mncc_sock_close(struct mncc_sock_state *state) |
| 91 | { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 92 | struct osmo_fd *bfd = &state->conn_bfd; |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 93 | |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 94 | LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n"); |
| 95 | |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 96 | close(bfd->fd); |
| 97 | bfd->fd = -1; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 98 | osmo_fd_unregister(bfd); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 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 Welte | 371efe5 | 2010-12-22 23:17:50 +0100 | [diff] [blame] | 104 | |
| 105 | /* release all exisitng calls */ |
| 106 | gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 107 | |
| 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 Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 115 | static int mncc_sock_read(struct osmo_fd *bfd) |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 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 Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 135 | 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 | |
| 146 | close: |
Harald Welte | eb76c7a | 2010-12-23 02:47:53 +0100 | [diff] [blame] | 147 | msgb_free(msg); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 148 | mncc_sock_close(state); |
| 149 | return -1; |
| 150 | } |
| 151 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 152 | static int mncc_sock_write(struct osmo_fd *bfd) |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 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 Welte | 49a2dde | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 159 | struct msgb *msg, *msg2; |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 160 | 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 | |
Peter Stuge | 46f799b | 2011-08-11 04:37:17 +0200 | [diff] [blame] | 168 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 169 | if (!msgb_length(msg)) { |
| 170 | LOGP(DMNCC, LOGL_ERROR, "message type (%d) with ZERO " |
| 171 | "bytes!\n", mncc_prim->msg_type); |
| 172 | goto dontsend; |
| 173 | } |
| 174 | |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 175 | /* try to send it over the socket */ |
| 176 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 177 | if (rc == 0) |
| 178 | goto close; |
| 179 | if (rc < 0) { |
| 180 | if (errno == EAGAIN) { |
| 181 | bfd->when |= BSC_FD_WRITE; |
| 182 | break; |
| 183 | } |
| 184 | goto close; |
| 185 | } |
Peter Stuge | 46f799b | 2011-08-11 04:37:17 +0200 | [diff] [blame] | 186 | |
| 187 | dontsend: |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 188 | /* _after_ we send it, we can deueue */ |
Harald Welte | 49a2dde | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 189 | msg2 = msgb_dequeue(&net->upqueue); |
| 190 | assert(msg == msg2); |
| 191 | msgb_free(msg); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 192 | } |
| 193 | return 0; |
| 194 | |
| 195 | close: |
| 196 | mncc_sock_close(state); |
| 197 | |
| 198 | return -1; |
| 199 | } |
| 200 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 201 | static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 202 | { |
| 203 | int rc = 0; |
| 204 | |
| 205 | if (flags & BSC_FD_READ) |
| 206 | rc = mncc_sock_read(bfd); |
| 207 | if (rc < 0) |
| 208 | return rc; |
| 209 | |
| 210 | if (flags & BSC_FD_WRITE) |
| 211 | rc = mncc_sock_write(bfd); |
| 212 | |
| 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | /* accept a new connection */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 217 | static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 218 | { |
| 219 | struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 220 | struct osmo_fd *conn_bfd = &state->conn_bfd; |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 221 | struct sockaddr_un un_addr; |
| 222 | socklen_t len; |
| 223 | int rc; |
| 224 | |
| 225 | len = sizeof(un_addr); |
| 226 | rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len); |
| 227 | if (rc < 0) { |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 228 | LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n"); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 229 | return -1; |
| 230 | } |
| 231 | |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 232 | if (conn_bfd->fd >= 0) { |
| 233 | LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have " |
| 234 | "another active connection ?!?\n"); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 235 | /* We already have one MNCC app connected, this is all we support */ |
| 236 | state->listen_bfd.when &= ~BSC_FD_READ; |
| 237 | close(rc); |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 238 | return 0; |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | conn_bfd->fd = rc; |
| 242 | conn_bfd->when = BSC_FD_READ; |
| 243 | conn_bfd->cb = mncc_sock_cb; |
| 244 | conn_bfd->data = state; |
| 245 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 246 | if (osmo_fd_register(conn_bfd) != 0) { |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 247 | LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n"); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 248 | close(conn_bfd->fd); |
| 249 | conn_bfd->fd = -1; |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 250 | return -1; |
| 251 | } |
| 252 | |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 253 | LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external " |
| 254 | "call control application\n"); |
| 255 | |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | int mncc_sock_init(struct gsm_network *net) |
| 261 | { |
| 262 | struct mncc_sock_state *state; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 263 | struct osmo_fd *bfd; |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 264 | int rc; |
| 265 | |
| 266 | state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state); |
| 267 | if (!state) |
| 268 | return -ENOMEM; |
| 269 | |
| 270 | state->net = net; |
| 271 | state->conn_bfd.fd = -1; |
| 272 | |
| 273 | bfd = &state->listen_bfd; |
| 274 | |
| 275 | rc = osmo_unixsock_listen(bfd, SOCK_SEQPACKET, "/tmp/bsc_mncc"); |
| 276 | if (rc < 0) { |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 277 | LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s\n", |
| 278 | strerror(errno)); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 279 | talloc_free(state); |
| 280 | return rc; |
| 281 | } |
| 282 | |
| 283 | bfd->when = BSC_FD_READ; |
| 284 | bfd->cb = mncc_sock_accept; |
| 285 | bfd->data = state; |
| 286 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 287 | rc = osmo_fd_register(bfd); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 288 | if (rc < 0) { |
Harald Welte | 0d6f930 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 289 | LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc); |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 290 | close(bfd->fd); |
| 291 | talloc_free(state); |
| 292 | return rc; |
| 293 | } |
| 294 | |
| 295 | g_state = state; |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /* FIXME: move this to libosmocore */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 301 | int osmo_unixsock_listen(struct osmo_fd *bfd, int type, const char *path) |
Harald Welte | 31bbbf4 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 302 | { |
| 303 | struct sockaddr_un local; |
| 304 | unsigned int namelen; |
| 305 | int rc; |
| 306 | |
| 307 | bfd->fd = socket(AF_UNIX, type, 0); |
| 308 | |
| 309 | if (bfd->fd < 0) { |
| 310 | fprintf(stderr, "Failed to create Unix Domain Socket.\n"); |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | local.sun_family = AF_UNIX; |
| 315 | strncpy(local.sun_path, path, sizeof(local.sun_path)); |
| 316 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 317 | unlink(local.sun_path); |
| 318 | |
| 319 | /* we use the same magic that X11 uses in Xtranssock.c for |
| 320 | * calculating the proper length of the sockaddr */ |
| 321 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 322 | local.sun_len = strlen(local.sun_path); |
| 323 | #endif |
| 324 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 325 | namelen = SUN_LEN(&local); |
| 326 | #else |
| 327 | namelen = strlen(local.sun_path) + |
| 328 | offsetof(struct sockaddr_un, sun_path); |
| 329 | #endif |
| 330 | |
| 331 | rc = bind(bfd->fd, (struct sockaddr *) &local, namelen); |
| 332 | if (rc != 0) { |
| 333 | fprintf(stderr, "Failed to bind the unix domain socket. '%s'\n", |
| 334 | local.sun_path); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | if (listen(bfd->fd, 0) != 0) { |
| 339 | fprintf(stderr, "Failed to listen.\n"); |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |