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