Harald Welte | 3327288 | 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> |
Holger Hans Peter Freyther | 460d84a | 2012-01-15 00:38:35 +0100 | [diff] [blame] | 5 | * (C) 2012 by Holger Hans Peter Freyther |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
Harald Welte | f5db356 | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 29 | #include <assert.h> |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 30 | #include <sys/socket.h> |
| 31 | #include <sys/un.h> |
| 32 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/talloc.h> |
| 34 | #include <osmocom/core/select.h> |
Vadim Yanitskiy | 590cf19 | 2016-07-20 21:40:54 +0600 | [diff] [blame^] | 35 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 37 | |
| 38 | #include <openbsc/debug.h> |
| 39 | #include <openbsc/mncc.h> |
| 40 | #include <openbsc/gsm_data.h> |
| 41 | |
| 42 | struct mncc_sock_state { |
| 43 | struct gsm_network *net; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 44 | struct osmo_fd listen_bfd; /* fd for listen socket */ |
| 45 | struct osmo_fd conn_bfd; /* fd for connection to lcr */ |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 48 | /* input from CC code into mncc_sock */ |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 49 | int mncc_sock_from_cc(struct gsm_network *net, struct msgb *msg) |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 50 | { |
Holger Hans Peter Freyther | 8fbe3e9 | 2011-01-06 13:50:52 +0100 | [diff] [blame] | 51 | struct gsm_mncc *mncc_in = (struct gsm_mncc *) msgb_data(msg); |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 52 | int msg_type = mncc_in->msg_type; |
| 53 | |
| 54 | /* Check if we currently have a MNCC handler connected */ |
Holger Hans Peter Freyther | 41cc7ea | 2011-10-26 18:37:09 +0200 | [diff] [blame] | 55 | if (net->mncc_state->conn_bfd.fd < 0) { |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 56 | LOGP(DMNCC, LOGL_ERROR, "mncc_sock receives %s for external CC app " |
| 57 | "but socket is gone\n", get_mncc_name(msg_type)); |
Andreas Eversberg | 5e2832e | 2013-03-11 08:12:43 +0100 | [diff] [blame] | 58 | if (!mncc_is_data_frame(msg_type)) { |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 59 | /* release the request */ |
| 60 | struct gsm_mncc mncc_out; |
| 61 | memset(&mncc_out, 0, sizeof(mncc_out)); |
| 62 | mncc_out.callref = mncc_in->callref; |
| 63 | mncc_set_cause(&mncc_out, GSM48_CAUSE_LOC_PRN_S_LU, |
| 64 | GSM48_CC_CAUSE_TEMP_FAILURE); |
| 65 | mncc_tx_to_cc(net, MNCC_REL_REQ, &mncc_out); |
| 66 | } |
| 67 | /* free the original message */ |
| 68 | msgb_free(msg); |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 69 | return -1; |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* FIXME: check for some maximum queue depth? */ |
| 73 | |
| 74 | /* Actually enqueue the message and mark socket write need */ |
| 75 | msgb_enqueue(&net->upqueue, msg); |
Holger Hans Peter Freyther | 41cc7ea | 2011-10-26 18:37:09 +0200 | [diff] [blame] | 76 | net->mncc_state->conn_bfd.when |= BSC_FD_WRITE; |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 77 | return 0; |
Harald Welte | bd93eaf | 2010-12-23 01:26:29 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 80 | static void mncc_sock_close(struct mncc_sock_state *state) |
| 81 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 82 | struct osmo_fd *bfd = &state->conn_bfd; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 83 | |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 84 | LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has LOST connection\n"); |
| 85 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 86 | close(bfd->fd); |
| 87 | bfd->fd = -1; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 88 | osmo_fd_unregister(bfd); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 89 | |
| 90 | /* re-enable the generation of ACCEPT for new connections */ |
| 91 | state->listen_bfd.when |= BSC_FD_READ; |
| 92 | |
Harald Welte | e8d1617 | 2010-12-22 23:17:50 +0100 | [diff] [blame] | 93 | /* release all exisitng calls */ |
| 94 | gsm0408_clear_all_trans(state->net, GSM48_PDISC_CC); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 95 | |
| 96 | /* flush the queue */ |
| 97 | while (!llist_empty(&state->net->upqueue)) { |
| 98 | struct msgb *msg = msgb_dequeue(&state->net->upqueue); |
| 99 | msgb_free(msg); |
| 100 | } |
| 101 | } |
| 102 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 103 | static int mncc_sock_read(struct osmo_fd *bfd) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 104 | { |
| 105 | struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data; |
| 106 | struct gsm_mncc *mncc_prim; |
| 107 | struct msgb *msg; |
| 108 | int rc; |
| 109 | |
| 110 | msg = msgb_alloc(sizeof(*mncc_prim)+256, "mncc_sock_rx"); |
| 111 | if (!msg) |
| 112 | return -ENOMEM; |
| 113 | |
| 114 | mncc_prim = (struct gsm_mncc *) msg->tail; |
| 115 | |
| 116 | rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0); |
| 117 | if (rc == 0) |
| 118 | goto close; |
| 119 | |
| 120 | if (rc < 0) { |
| 121 | if (errno == EAGAIN) |
| 122 | return 0; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 123 | goto close; |
| 124 | } |
| 125 | |
| 126 | rc = mncc_tx_to_cc(state->net, mncc_prim->msg_type, mncc_prim); |
| 127 | |
| 128 | /* as we always synchronously process the message in mncc_send() and |
| 129 | * its callbacks, we can free the message here. */ |
| 130 | msgb_free(msg); |
| 131 | |
| 132 | return rc; |
| 133 | |
| 134 | close: |
Harald Welte | bd0f33c | 2010-12-23 02:47:53 +0100 | [diff] [blame] | 135 | msgb_free(msg); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 136 | mncc_sock_close(state); |
| 137 | return -1; |
| 138 | } |
| 139 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 140 | static int mncc_sock_write(struct osmo_fd *bfd) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 141 | { |
| 142 | struct mncc_sock_state *state = bfd->data; |
| 143 | struct gsm_network *net = state->net; |
| 144 | int rc; |
| 145 | |
| 146 | while (!llist_empty(&net->upqueue)) { |
Harald Welte | f5db356 | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 147 | struct msgb *msg, *msg2; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 148 | struct gsm_mncc *mncc_prim; |
| 149 | |
| 150 | /* peek at the beginning of the queue */ |
| 151 | msg = llist_entry(net->upqueue.next, struct msgb, list); |
| 152 | mncc_prim = (struct gsm_mncc *)msg->data; |
| 153 | |
| 154 | bfd->when &= ~BSC_FD_WRITE; |
| 155 | |
Peter Stuge | 3912c7e | 2011-08-11 04:37:17 +0200 | [diff] [blame] | 156 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 157 | if (!msgb_length(msg)) { |
| 158 | LOGP(DMNCC, LOGL_ERROR, "message type (%d) with ZERO " |
| 159 | "bytes!\n", mncc_prim->msg_type); |
| 160 | goto dontsend; |
| 161 | } |
| 162 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 163 | /* try to send it over the socket */ |
| 164 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 165 | if (rc == 0) |
| 166 | goto close; |
| 167 | if (rc < 0) { |
| 168 | if (errno == EAGAIN) { |
| 169 | bfd->when |= BSC_FD_WRITE; |
| 170 | break; |
| 171 | } |
| 172 | goto close; |
| 173 | } |
Peter Stuge | 3912c7e | 2011-08-11 04:37:17 +0200 | [diff] [blame] | 174 | |
| 175 | dontsend: |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 176 | /* _after_ we send it, we can deueue */ |
Harald Welte | f5db356 | 2010-12-22 22:56:15 +0100 | [diff] [blame] | 177 | msg2 = msgb_dequeue(&net->upqueue); |
| 178 | assert(msg == msg2); |
| 179 | msgb_free(msg); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 180 | } |
| 181 | return 0; |
| 182 | |
| 183 | close: |
| 184 | mncc_sock_close(state); |
| 185 | |
| 186 | return -1; |
| 187 | } |
| 188 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 189 | static int mncc_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 190 | { |
| 191 | int rc = 0; |
| 192 | |
| 193 | if (flags & BSC_FD_READ) |
| 194 | rc = mncc_sock_read(bfd); |
| 195 | if (rc < 0) |
| 196 | return rc; |
| 197 | |
| 198 | if (flags & BSC_FD_WRITE) |
| 199 | rc = mncc_sock_write(bfd); |
| 200 | |
| 201 | return rc; |
| 202 | } |
| 203 | |
Holger Hans Peter Freyther | f0e71e7 | 2011-10-21 14:12:46 +0200 | [diff] [blame] | 204 | /** |
| 205 | * Send a version indication to the remote. |
| 206 | */ |
| 207 | static void queue_hello(struct mncc_sock_state *mncc) |
| 208 | { |
| 209 | struct gsm_mncc_hello *hello; |
| 210 | struct msgb *msg; |
| 211 | |
| 212 | msg = msgb_alloc(512, "mncc hello"); |
| 213 | if (!msg) { |
| 214 | LOGP(DMNCC, LOGL_ERROR, "Failed to allocate hello.\n"); |
| 215 | mncc_sock_close(mncc); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | hello = (struct gsm_mncc_hello *) msgb_put(msg, sizeof(*hello)); |
| 220 | hello->msg_type = MNCC_SOCKET_HELLO; |
| 221 | hello->version = MNCC_SOCK_VERSION; |
Holger Hans Peter Freyther | 460d84a | 2012-01-15 00:38:35 +0100 | [diff] [blame] | 222 | hello->mncc_size = sizeof(struct gsm_mncc); |
| 223 | hello->data_frame_size = sizeof(struct gsm_data_frame); |
| 224 | hello->called_offset = offsetof(struct gsm_mncc, called); |
| 225 | hello->signal_offset = offsetof(struct gsm_mncc, signal); |
| 226 | hello->emergency_offset = offsetof(struct gsm_mncc, emergency); |
| 227 | hello->lchan_type_offset = offsetof(struct gsm_mncc, lchan_type); |
Holger Hans Peter Freyther | f0e71e7 | 2011-10-21 14:12:46 +0200 | [diff] [blame] | 228 | |
| 229 | msgb_enqueue(&mncc->net->upqueue, msg); |
| 230 | mncc->conn_bfd.when |= BSC_FD_WRITE; |
| 231 | } |
| 232 | |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 233 | /* accept a new connection */ |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 234 | static int mncc_sock_accept(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 235 | { |
| 236 | struct mncc_sock_state *state = (struct mncc_sock_state *)bfd->data; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 237 | struct osmo_fd *conn_bfd = &state->conn_bfd; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 238 | struct sockaddr_un un_addr; |
| 239 | socklen_t len; |
| 240 | int rc; |
| 241 | |
| 242 | len = sizeof(un_addr); |
| 243 | rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len); |
| 244 | if (rc < 0) { |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 245 | LOGP(DMNCC, LOGL_ERROR, "Failed to accept a new connection\n"); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 249 | if (conn_bfd->fd >= 0) { |
| 250 | LOGP(DMNCC, LOGL_NOTICE, "MNCC app connects but we already have " |
| 251 | "another active connection ?!?\n"); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 252 | /* We already have one MNCC app connected, this is all we support */ |
| 253 | state->listen_bfd.when &= ~BSC_FD_READ; |
| 254 | close(rc); |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 255 | return 0; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | conn_bfd->fd = rc; |
| 259 | conn_bfd->when = BSC_FD_READ; |
| 260 | conn_bfd->cb = mncc_sock_cb; |
| 261 | conn_bfd->data = state; |
| 262 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 263 | if (osmo_fd_register(conn_bfd) != 0) { |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 264 | LOGP(DMNCC, LOGL_ERROR, "Failed to register new connection fd\n"); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 265 | close(conn_bfd->fd); |
| 266 | conn_bfd->fd = -1; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 267 | return -1; |
| 268 | } |
| 269 | |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 270 | LOGP(DMNCC, LOGL_NOTICE, "MNCC Socket has connection with external " |
| 271 | "call control application\n"); |
| 272 | |
Holger Hans Peter Freyther | f0e71e7 | 2011-10-21 14:12:46 +0200 | [diff] [blame] | 273 | queue_hello(state); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 278 | int mncc_sock_init(struct gsm_network *net, const char *sock_path) |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 279 | { |
| 280 | struct mncc_sock_state *state; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 281 | struct osmo_fd *bfd; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 282 | int rc; |
| 283 | |
| 284 | state = talloc_zero(tall_bsc_ctx, struct mncc_sock_state); |
| 285 | if (!state) |
| 286 | return -ENOMEM; |
| 287 | |
| 288 | state->net = net; |
| 289 | state->conn_bfd.fd = -1; |
| 290 | |
| 291 | bfd = &state->listen_bfd; |
| 292 | |
Vadim Yanitskiy | 590cf19 | 2016-07-20 21:40:54 +0600 | [diff] [blame^] | 293 | bfd->fd = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path, |
| 294 | OSMO_SOCK_F_BIND); |
| 295 | if (bfd->fd < 0) { |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 296 | LOGP(DMNCC, LOGL_ERROR, "Could not create unix socket: %s: %s\n", |
| 297 | sock_path, strerror(errno)); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 298 | talloc_free(state); |
Vadim Yanitskiy | 590cf19 | 2016-07-20 21:40:54 +0600 | [diff] [blame^] | 299 | return -1; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | bfd->when = BSC_FD_READ; |
| 303 | bfd->cb = mncc_sock_accept; |
| 304 | bfd->data = state; |
| 305 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 306 | rc = osmo_fd_register(bfd); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 307 | if (rc < 0) { |
Harald Welte | 5be4c72 | 2010-12-23 02:47:28 +0100 | [diff] [blame] | 308 | LOGP(DMNCC, LOGL_ERROR, "Could not register listen fd: %d\n", rc); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 309 | close(bfd->fd); |
| 310 | talloc_free(state); |
| 311 | return rc; |
| 312 | } |
| 313 | |
Holger Hans Peter Freyther | 41cc7ea | 2011-10-26 18:37:09 +0200 | [diff] [blame] | 314 | net->mncc_state = state; |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 315 | |
Neels Hofmeyr | 54f31c6 | 2016-02-23 14:55:17 +0100 | [diff] [blame] | 316 | LOGP(DMNCC, LOGL_NOTICE, "MNCC socket at %s\n", sock_path); |
Harald Welte | 3327288 | 2010-12-23 00:02:51 +0100 | [diff] [blame] | 317 | return 0; |
| 318 | } |