Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame^] | 1 | /* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 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 | |
Harald Welte | 31c9eca | 2018-10-03 21:03:34 +0200 | [diff] [blame] | 23 | #define _GNU_SOURCE |
| 24 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdint.h> |
| 28 | #include <unistd.h> |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 29 | #include <errno.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 30 | |
| 31 | #include <pthread.h> |
| 32 | |
| 33 | #include <wintypes.h> |
| 34 | #include <winscard.h> |
| 35 | #include <pcsclite.h> |
| 36 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 37 | #include <sys/socket.h> |
| 38 | #include <netdb.h> |
| 39 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 40 | #include <osmocom/core/socket.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 41 | #include <osmocom/core/linuxlist.h> |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 42 | #include <osmocom/core/logging.h> |
| 43 | #include <osmocom/core/application.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 44 | |
| 45 | #include <osmocom/gsm/ipa.h> |
| 46 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 47 | |
Kévin Redon | ff5db6e | 2018-10-11 17:16:18 +0200 | [diff] [blame] | 48 | #include <asn_application.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 49 | #include <osmocom/rspro/RsproPDU.h> |
| 50 | |
| 51 | #include "bankd.h" |
Harald Welte | 3cded63 | 2019-03-09 12:59:41 +0100 | [diff] [blame] | 52 | #include "rspro_client_fsm.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 53 | #include "debug.h" |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 54 | #include "rspro_util.h" |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 55 | |
| 56 | __thread void *talloc_asn1_ctx; |
| 57 | |
| 58 | static void *worker_main(void *arg); |
| 59 | |
| 60 | /*********************************************************************** |
| 61 | * bankd core / main thread |
| 62 | ***********************************************************************/ |
| 63 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 64 | int asn_debug; |
| 65 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 66 | static void bankd_init(struct bankd *bankd) |
| 67 | { |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 68 | void *g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 69 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 70 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 71 | asn_debug = 0; |
| 72 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 73 | /* intialize members of 'bankd' */ |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 74 | bankd->slotmaps = slotmap_init(bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 75 | INIT_LLIST_HEAD(&bankd->workers); |
| 76 | pthread_mutex_init(&bankd->workers_mutex, NULL); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 77 | |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 78 | bankd->comp_id.type = ComponentType_remsimBankd; |
| 79 | OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name"); |
| 80 | OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd"); |
| 81 | OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION); |
| 82 | /* FIXME: other members of app_comp_id */ |
| 83 | |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 84 | /* Np lock or mutex required for the pcsc_slot_names list, as this is only |
| 85 | * read once during bankd initialization, when the worker threads haven't |
| 86 | * started yet */ |
| 87 | INIT_LLIST_HEAD(&bankd->pcsc_slot_names); |
| 88 | OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0); |
Harald Welte | e72e573 | 2018-09-23 19:31:55 +0200 | [diff] [blame] | 89 | |
| 90 | /* HACK HACK HACK */ |
| 91 | { |
| 92 | struct bank_slot bs = { .bank_id = 1, }; |
| 93 | struct client_slot cs = { .client_id = 23, }; |
| 94 | int i; |
| 95 | for (i = 0; i < 5; i++) { |
| 96 | bs.slot_nr = cs.slot_nr = i; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 97 | slotmap_add(bankd->slotmaps, &bs, &cs); |
Harald Welte | e72e573 | 2018-09-23 19:31:55 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* create + start a new bankd_worker thread */ |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 103 | static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 104 | { |
| 105 | struct bankd_worker *worker; |
| 106 | int rc; |
| 107 | |
| 108 | worker = talloc_zero(bankd, struct bankd_worker); |
| 109 | if (!worker) |
| 110 | return NULL; |
| 111 | |
| 112 | worker->bankd = bankd; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 113 | worker->num = i; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 114 | |
| 115 | /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */ |
| 116 | |
| 117 | rc = pthread_create(&worker->thread, NULL, worker_main, worker); |
| 118 | if (rc != 0) { |
| 119 | talloc_free(worker); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | pthread_mutex_lock(&bankd->workers_mutex); |
| 124 | llist_add_tail(&worker->list, &bankd->workers); |
| 125 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 126 | |
| 127 | return worker; |
| 128 | } |
| 129 | |
| 130 | static bool terminate = false; |
| 131 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 132 | /* handle incoming messages from server */ |
| 133 | static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 134 | { |
| 135 | struct RsproPDU_t *resp; |
| 136 | |
| 137 | switch (pdu->msg.present) { |
| 138 | case RsproPDUchoice_PR_connectBankRes: |
| 139 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 140 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity); |
| 141 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 142 | break; |
| 143 | default: |
| 144 | fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | void handle_options(int argc, char **argv) |
| 152 | { |
| 153 | } |
| 154 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 155 | int main(int argc, char **argv) |
| 156 | { |
| 157 | struct bankd *bankd = talloc_zero(NULL, struct bankd); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 158 | struct rspro_server_conn *srvc = &bankd->srvc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 159 | int i, rc; |
| 160 | |
| 161 | OSMO_ASSERT(bankd); |
| 162 | bankd_init(bankd); |
| 163 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 164 | srvc->server_host = "localhost"; |
| 165 | srvc->server_port = 9998; |
| 166 | srvc->handle_rx = bankd_srvc_handle_rx; |
| 167 | srvc->own_comp_id.type = ComponentType_remsimBankd; |
| 168 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name"); |
| 169 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd"); |
| 170 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
| 171 | |
| 172 | handle_options(argc, argv); |
| 173 | |
| 174 | /* Connection towards remsim-server */ |
| 175 | rc = server_conn_fsm_alloc(bankd, srvc); |
| 176 | if (rc < 0) { |
| 177 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 178 | exit(1); |
| 179 | } |
| 180 | |
| 181 | /* create listening socket for inbound client connections */ |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 182 | rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND); |
| 183 | if (rc < 0) |
| 184 | exit(1); |
| 185 | bankd->accept_fd = rc; |
| 186 | |
| 187 | /* create worker threads. FIXME: one per reader/slot! */ |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 188 | for (i = 0; i < 10; i++) { |
| 189 | struct bankd_worker *w; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 190 | w = bankd_create_worker(bankd, i); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 191 | if (!w) |
| 192 | exit(21); |
| 193 | } |
| 194 | |
| 195 | while (1) { |
| 196 | if (terminate) |
| 197 | break; |
Harald Welte | 8f893ff | 2018-10-03 23:21:10 +0200 | [diff] [blame] | 198 | /* FIXME: Connect to remsim-server from the main thread, register with |
| 199 | * it and await + process any slot mapping or other configuration commands. |
| 200 | * Ensure to re-connect as needed. */ |
Harald Welte | 7f684a0 | 2018-08-16 14:43:50 +0200 | [diff] [blame] | 201 | sleep(1); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 202 | osmo_select_main(0); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | talloc_free(bankd); |
| 206 | exit(0); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | /*********************************************************************** |
| 212 | * bankd worker thread |
| 213 | ***********************************************************************/ |
| 214 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 215 | struct value_string worker_state_names[] = { |
| 216 | { BW_ST_INIT, "INIT" }, |
| 217 | { BW_ST_ACCEPTING, "ACCEPTING" }, |
| 218 | { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" }, |
| 219 | { BW_ST_CONN_CLIENT, "CONN_CLIENT" }, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 220 | { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" }, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 221 | { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" }, |
| 222 | { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" }, |
| 223 | { 0, NULL } |
| 224 | }; |
| 225 | |
Harald Welte | ceb3e68 | 2018-08-16 14:47:11 +0200 | [diff] [blame] | 226 | #define LOGW(w, fmt, args...) \ |
| 227 | printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \ |
| 228 | __FILE__, __LINE__, ## args) |
| 229 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 230 | #define PCSC_ERROR(w, rv, text) \ |
| 231 | if (rv != SCARD_S_SUCCESS) { \ |
| 232 | LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ |
| 233 | goto end; \ |
| 234 | } else { \ |
Harald Welte | 7b41d9c | 2018-10-03 23:15:10 +0200 | [diff] [blame] | 235 | LOGW((w), ": OK\n"); \ |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 238 | static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state) |
| 239 | { |
| 240 | LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state)); |
| 241 | worker->state = new_state; |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 242 | worker->timeout = 0; |
| 243 | } |
| 244 | |
| 245 | static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state, |
| 246 | unsigned int timeout_secs) |
| 247 | { |
| 248 | LOGW(worker, "Changing state to %s (timeout=%u)\n", |
| 249 | get_value_string(worker_state_names, new_state), timeout_secs); |
| 250 | worker->state = new_state; |
| 251 | worker->timeout = timeout_secs; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 252 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 253 | |
| 254 | static void worker_cleanup(void *arg) |
| 255 | { |
| 256 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 257 | struct bankd *bankd = worker->bankd; |
| 258 | |
| 259 | /* FIXME: should we still do this? in the thread ?!? */ |
| 260 | pthread_mutex_lock(&bankd->workers_mutex); |
| 261 | llist_del(&worker->list); |
| 262 | talloc_free(worker); /* FIXME: is this safe? */ |
| 263 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 264 | } |
| 265 | |
| 266 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 267 | static int worker_open_card(struct bankd_worker *worker) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 268 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 269 | long rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 270 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 271 | OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED); |
| 272 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 273 | if (!worker->reader.name) { |
| 274 | /* resolve PC/SC reader name from slot_id -> name map */ |
| 275 | worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot); |
| 276 | if (!worker->reader.name) { |
| 277 | LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n", |
| 278 | worker->slot.bank_id, worker->slot.slot_nr); |
| 279 | rc = -1; |
| 280 | goto end; |
| 281 | } |
| 282 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 283 | OSMO_ASSERT(worker->reader.name); |
| 284 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 285 | if (!worker->reader.pcsc.hContext) { |
| 286 | LOGW(worker, "Attempting to open PC/SC context\n"); |
| 287 | /* The PC/SC context must be created inside the thread where we'll later use it */ |
| 288 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext); |
| 289 | PCSC_ERROR(worker, rc, "SCardEstablishContext") |
| 290 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 291 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 292 | if (!worker->reader.pcsc.hCard) { |
| 293 | LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name); |
| 294 | DWORD dwActiveProtocol; |
| 295 | rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED, |
| 296 | SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol); |
| 297 | PCSC_ERROR(worker, rc, "SCardConnect") |
| 298 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 299 | |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 300 | worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 301 | /* FIXME: notify client about this state change */ |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 302 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 303 | return 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 304 | end: |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 305 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 306 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 307 | |
| 308 | |
| 309 | static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size) |
| 310 | { |
| 311 | struct ipaccess_head *hh; |
| 312 | uint16_t len; |
| 313 | int needed, rc; |
| 314 | |
| 315 | if (buf_size < sizeof(*hh)) |
| 316 | return -1; |
| 317 | |
| 318 | hh = (struct ipaccess_head *) buf; |
| 319 | |
| 320 | /* 1) blocking read from the socket (IPA header) */ |
| 321 | rc = read(fd, buf, sizeof(*hh)); |
| 322 | if (rc < sizeof(*hh)) |
| 323 | return -2; |
| 324 | |
| 325 | len = ntohs(hh->len); |
| 326 | needed = len; //- sizeof(*hh); |
| 327 | |
| 328 | /* 2) blocking read from the socket (payload) */ |
| 329 | rc = read(fd, buf+sizeof(*hh), needed); |
| 330 | if (rc < needed) |
| 331 | return -3; |
| 332 | |
| 333 | return len; |
| 334 | } |
| 335 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 336 | static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu) |
| 337 | { |
| 338 | struct msgb *msg = rspro_enc_msg(pdu); |
| 339 | int rc; |
| 340 | |
| 341 | if (!msg) { |
| 342 | LOGW(worker, "error encoding RSPRO\n"); |
| 343 | return -1; |
| 344 | } |
| 345 | |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 346 | msg->l2h = msg->data; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 347 | /* prepend the header */ |
| 348 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO); |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 349 | ipa_prepend_header(msg, IPAC_PROTO_OSMO); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 350 | |
| 351 | /* actually send it through the socket */ |
| 352 | rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg)); |
| 353 | if (rc == msgb_length(msg)) |
| 354 | rc = 0; |
| 355 | else { |
| 356 | LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg)); |
| 357 | rc = -1; |
| 358 | } |
| 359 | |
| 360 | msgb_free(msg); |
| 361 | |
| 362 | return rc; |
| 363 | } |
| 364 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 365 | /* attempt to obtain slot-map */ |
| 366 | static int worker_try_slotmap(struct bankd_worker *worker) |
| 367 | { |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 368 | struct slot_mapping *slmap; |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 369 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 370 | slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 371 | if (!slmap) { |
| 372 | LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n", |
| 373 | worker->client.clslot.client_id, worker->client.clslot.slot_nr); |
| 374 | /* check in 10s if the map has been installed meanwhile by main thread */ |
| 375 | worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10); |
| 376 | return -1; |
| 377 | } else { |
| 378 | LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n", |
| 379 | slmap->client.client_id, slmap->client.slot_nr, |
| 380 | slmap->bank.bank_id, slmap->bank.slot_nr); |
| 381 | worker->slot = slmap->bank; |
| 382 | worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10); |
| 383 | return worker_open_card(worker); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 388 | static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 389 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 390 | const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity; |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 391 | e_ResultCode res; |
| 392 | RsproPDU_t *resp; |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 393 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 394 | OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq); |
| 395 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 396 | LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n", |
| 397 | cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf); |
| 398 | /* FIXME: store somewhere? */ |
| 399 | |
| 400 | if (worker->state != BW_ST_CONN_WAIT_ID) { |
| 401 | LOGW(worker, "Unexpected connectClientReq\n"); |
| 402 | return -102; |
| 403 | } |
| 404 | |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 405 | if (!pdu->msg.choice.connectClientReq.clientSlot) { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 406 | LOGW(worker, "missing clientID, aborting\n"); |
| 407 | return -103; |
| 408 | } |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 409 | worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId; |
| 410 | worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 411 | worker_set_state(worker, BW_ST_CONN_CLIENT); |
| 412 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 413 | if (worker_try_slotmap(worker) >= 0) |
| 414 | res = ResultCode_ok; |
| 415 | else |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 416 | res = ResultCode_cardNotPresent; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 417 | |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 418 | resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res); |
| 419 | return worker_send_rspro(worker, resp); |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 422 | static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 423 | { |
| 424 | const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard; |
| 425 | const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0; |
| 426 | SCARD_IO_REQUEST pioRecvPci; |
| 427 | uint8_t rx_buf[1024]; |
| 428 | DWORD rx_buf_len = sizeof(rx_buf); |
| 429 | RsproPDU_t *pdu_resp; |
| 430 | long rc; |
| 431 | |
| 432 | LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size)); |
| 433 | |
| 434 | if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) { |
| 435 | LOGW(worker, "Unexpected tpduModemToCaard\n"); |
| 436 | return -104; |
| 437 | } |
| 438 | |
| 439 | /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */ |
| 440 | |
| 441 | rc = SCardTransmit(worker->reader.pcsc.hCard, |
| 442 | pioSendPci, mdm2sim->data.buf, mdm2sim->data.size, |
| 443 | &pioRecvPci, rx_buf, &rx_buf_len); |
| 444 | PCSC_ERROR(worker, rc, "SCardTransmit"); |
| 445 | |
| 446 | /* encode response PDU and send it */ |
| 447 | pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot, |
| 448 | rx_buf, rx_buf_len); |
| 449 | worker_send_rspro(worker, pdu_resp); |
| 450 | |
| 451 | return 0; |
| 452 | end: |
| 453 | return rc; |
| 454 | } |
| 455 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 456 | /* handle one incoming RSPRO message from a client inside a worker thread */ |
| 457 | static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 458 | { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 459 | int rc = -100; |
| 460 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 461 | switch (pdu->msg.present) { |
| 462 | case RsproPDUchoice_PR_connectClientReq: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 463 | rc = worker_handle_connectClientReq(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 464 | break; |
| 465 | case RsproPDUchoice_PR_tpduModemToCard: |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 466 | rc = worker_handle_tpduModemToCard(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 467 | break; |
| 468 | case RsproPDUchoice_PR_clientSlotStatusInd: |
| 469 | /* FIXME */ |
| 470 | break; |
| 471 | default: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 472 | rc = -101; |
| 473 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 476 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 479 | static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs) |
| 480 | { |
| 481 | struct timeval tout = { timeout_secs, 0 }; |
| 482 | fd_set readset; |
| 483 | |
| 484 | FD_ZERO(&readset); |
| 485 | FD_SET(fd, &readset); |
| 486 | return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL); |
| 487 | } |
| 488 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 489 | /* body of the main transceive loop */ |
| 490 | static int worker_transceive_loop(struct bankd_worker *worker) |
| 491 | { |
| 492 | struct ipaccess_head *hh; |
| 493 | struct ipaccess_head_ext *hh_ext; |
| 494 | uint8_t buf[65536]; /* maximum length expressed in 16bit length field */ |
| 495 | asn_dec_rval_t rval; |
| 496 | int data_len, rc; |
Harald Welte | 9ebbacc | 2018-09-24 17:43:39 +0200 | [diff] [blame] | 497 | RsproPDU_t *pdu = NULL; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 498 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 499 | rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout); |
| 500 | if (rc == 0) { |
| 501 | /* TIMEOUT case */ |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 502 | switch (worker->state) { |
| 503 | case BW_ST_CONN_CLIENT_WAIT_MAP: |
| 504 | /* re-check if mapping exists meanwhile? */ |
| 505 | worker_try_slotmap(worker); |
| 506 | break; |
| 507 | case BW_ST_CONN_CLIENT_MAPPED: |
| 508 | /* re-check if reader/card can be opened meanwhile? */ |
| 509 | worker_open_card(worker); |
| 510 | break; |
| 511 | default: |
| 512 | OSMO_ASSERT(0); |
| 513 | } |
| 514 | /* return early, so we do another select rather than the blocking read below */ |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 515 | return 0; |
| 516 | }; |
| 517 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 518 | /* 1) blocking read of entire IPA message from the socket */ |
| 519 | rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf)); |
| 520 | if (rc < 0) |
| 521 | return rc; |
| 522 | data_len = rc; |
| 523 | |
| 524 | hh = (struct ipaccess_head *) buf; |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 525 | if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) { |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 526 | LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 527 | return -4; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 528 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 529 | |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 530 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 531 | LOGW(worker, "IPA CCM not implemented yet\n"); |
| 532 | return 0; |
| 533 | } |
| 534 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 535 | hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 536 | if (data_len < sizeof(*hh_ext)) { |
| 537 | LOGW(worker, "Received short message\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 538 | return -5; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 539 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 540 | data_len -= sizeof(*hh_ext); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 541 | if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) { |
| 542 | LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 543 | return -6; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 544 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 545 | |
| 546 | /* 2) ASN1 BER decode of the message */ |
| 547 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 548 | if (rval.code != RC_OK) { |
| 549 | LOGW(worker, "Error during BER decode of RSPRO\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 550 | return -7; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 551 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 552 | |
| 553 | /* 3) handling of the message, possibly resulting in PCSC commands */ |
| 554 | rc = worker_handle_rspro(worker, pdu); |
| 555 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 556 | if (rc < 0) { |
| 557 | LOGW(worker, "Error handling RSPRO\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 558 | return rc; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 559 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 560 | |
| 561 | /* everything OK if we reach here */ |
| 562 | return 0; |
| 563 | } |
| 564 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 565 | /* obtain an ascii representation of the client IP/port */ |
| 566 | static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker) |
| 567 | { |
| 568 | char hostbuf[32], portbuf[32]; |
| 569 | int rc; |
| 570 | |
| 571 | rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr, |
| 572 | worker->client.peer_addr_len, hostbuf, sizeof(hostbuf), |
| 573 | portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV); |
| 574 | if (rc != 0) { |
| 575 | out[0] = '\0'; |
| 576 | return -1; |
| 577 | } |
| 578 | snprintf(out, outlen, "%s:%s", hostbuf, portbuf); |
| 579 | return 0; |
| 580 | } |
| 581 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 582 | /* worker thread main function */ |
| 583 | static void *worker_main(void *arg) |
| 584 | { |
| 585 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 586 | void *top_ctx; |
| 587 | int rc; |
Harald Welte | 31c9eca | 2018-10-03 21:03:34 +0200 | [diff] [blame] | 588 | char worker_name[32]; |
| 589 | |
| 590 | /* set the thread name */ |
| 591 | snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num); |
| 592 | pthread_setname_np(pthread_self(), worker_name); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 593 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 594 | worker_set_state(worker, BW_ST_INIT); |
| 595 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 596 | /* not permitted in multithreaded environment */ |
| 597 | talloc_disable_null_tracking(); |
| 598 | top_ctx = talloc_named_const(NULL, 0, "top"); |
| 599 | talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1"); |
| 600 | |
| 601 | /* push cleanup helper */ |
| 602 | pthread_cleanup_push(&worker_cleanup, worker); |
| 603 | |
| 604 | /* we continuously perform the same loop here, recycling the worker thread |
| 605 | * once the client connection is gone or we have some trouble with the card/reader */ |
| 606 | while (1) { |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 607 | char buf[128]; |
| 608 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 609 | worker->client.peer_addr_len = sizeof(worker->client.peer_addr); |
| 610 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 611 | worker_set_state(worker, BW_ST_ACCEPTING); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 612 | /* first wait for an incoming TCP connection */ |
| 613 | rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr, |
| 614 | &worker->client.peer_addr_len); |
| 615 | if (rc < 0) { |
| 616 | continue; |
| 617 | } |
| 618 | worker->client.fd = rc; |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 619 | worker_client_addrstr(buf, sizeof(buf), worker); |
| 620 | LOGW(worker, "Accepted connection from %s\n", buf); |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 621 | worker_set_state(worker, BW_ST_CONN_WAIT_ID); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 622 | |
| 623 | /* run the main worker transceive loop body until there was some error */ |
| 624 | while (1) { |
| 625 | rc = worker_transceive_loop(worker); |
| 626 | if (rc < 0) |
| 627 | break; |
| 628 | } |
| 629 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 630 | LOGW(worker, "Error %d occurred: Cleaning up state\n", rc); |
| 631 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 632 | /* clean-up: reset to sane state */ |
| 633 | if (worker->reader.pcsc.hCard) { |
| 634 | SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD); |
| 635 | worker->reader.pcsc.hCard = 0; |
| 636 | } |
| 637 | if (worker->reader.pcsc.hContext) { |
| 638 | SCardReleaseContext(worker->reader.pcsc.hContext); |
| 639 | worker->reader.pcsc.hContext = 0; |
| 640 | } |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 641 | if (worker->reader.name) |
| 642 | worker->reader.name = NULL; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 643 | if (worker->client.fd >= 0) |
| 644 | close(worker->client.fd); |
Harald Welte | 415e8f6 | 2018-08-16 14:47:38 +0200 | [diff] [blame] | 645 | memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr)); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 646 | worker->client.fd = -1; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 647 | worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | pthread_cleanup_pop(1); |
| 651 | talloc_free(top_ctx); |
| 652 | pthread_exit(NULL); |
| 653 | } |