Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <pthread.h> |
| 7 | |
| 8 | #include <wintypes.h> |
| 9 | #include <winscard.h> |
| 10 | #include <pcsclite.h> |
| 11 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 12 | #include <sys/socket.h> |
| 13 | #include <netdb.h> |
| 14 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 15 | #include <osmocom/core/socket.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 16 | #include <osmocom/core/linuxlist.h> |
| 17 | |
| 18 | #include <osmocom/gsm/ipa.h> |
| 19 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 20 | |
| 21 | #include <asn1c/asn_application.h> |
| 22 | #include <osmocom/rspro/RsproPDU.h> |
| 23 | |
| 24 | #include "bankd.h" |
| 25 | |
| 26 | __thread void *talloc_asn1_ctx; |
| 27 | |
| 28 | static void *worker_main(void *arg); |
| 29 | |
| 30 | /*********************************************************************** |
| 31 | * bankd core / main thread |
| 32 | ***********************************************************************/ |
| 33 | |
| 34 | static void bankd_init(struct bankd *bankd) |
| 35 | { |
| 36 | /* intialize members of 'bankd' */ |
| 37 | INIT_LLIST_HEAD(&bankd->slot_mappings); |
| 38 | pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL); |
| 39 | INIT_LLIST_HEAD(&bankd->workers); |
| 40 | pthread_mutex_init(&bankd->workers_mutex, NULL); |
| 41 | } |
| 42 | |
| 43 | /* create + start a new bankd_worker thread */ |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 44 | 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] | 45 | { |
| 46 | struct bankd_worker *worker; |
| 47 | int rc; |
| 48 | |
| 49 | worker = talloc_zero(bankd, struct bankd_worker); |
| 50 | if (!worker) |
| 51 | return NULL; |
| 52 | |
| 53 | worker->bankd = bankd; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 54 | worker->num = i; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 55 | |
| 56 | /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */ |
| 57 | |
| 58 | rc = pthread_create(&worker->thread, NULL, worker_main, worker); |
| 59 | if (rc != 0) { |
| 60 | talloc_free(worker); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | pthread_mutex_lock(&bankd->workers_mutex); |
| 65 | llist_add_tail(&worker->list, &bankd->workers); |
| 66 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 67 | |
| 68 | return worker; |
| 69 | } |
| 70 | |
| 71 | static bool terminate = false; |
| 72 | |
| 73 | int main(int argc, char **argv) |
| 74 | { |
| 75 | struct bankd *bankd = talloc_zero(NULL, struct bankd); |
| 76 | int i, rc; |
| 77 | |
| 78 | OSMO_ASSERT(bankd); |
| 79 | bankd_init(bankd); |
| 80 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 81 | /* create listening socket */ |
| 82 | rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND); |
| 83 | if (rc < 0) |
| 84 | exit(1); |
| 85 | bankd->accept_fd = rc; |
| 86 | |
| 87 | /* create worker threads. FIXME: one per reader/slot! */ |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 88 | for (i = 0; i < 10; i++) { |
| 89 | struct bankd_worker *w; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 90 | w = bankd_create_worker(bankd, i); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 91 | if (!w) |
| 92 | exit(21); |
| 93 | } |
| 94 | |
| 95 | while (1) { |
| 96 | if (terminate) |
| 97 | break; |
Harald Welte | 7f684a0 | 2018-08-16 14:43:50 +0200 | [diff] [blame] | 98 | sleep(1); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | talloc_free(bankd); |
| 102 | exit(0); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | /*********************************************************************** |
| 108 | * bankd worker thread |
| 109 | ***********************************************************************/ |
| 110 | |
| 111 | #define PCSC_ERROR(rv, text) \ |
| 112 | if (rv != SCARD_S_SUCCESS) { \ |
| 113 | fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ |
| 114 | goto end; \ |
| 115 | } else { \ |
| 116 | printf(text ": OK\n\n"); \ |
| 117 | } |
| 118 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 119 | struct value_string worker_state_names[] = { |
| 120 | { BW_ST_INIT, "INIT" }, |
| 121 | { BW_ST_ACCEPTING, "ACCEPTING" }, |
| 122 | { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" }, |
| 123 | { BW_ST_CONN_CLIENT, "CONN_CLIENT" }, |
| 124 | { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" }, |
| 125 | { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" }, |
| 126 | { 0, NULL } |
| 127 | }; |
| 128 | |
Harald Welte | ceb3e68 | 2018-08-16 14:47:11 +0200 | [diff] [blame] | 129 | #define LOGW(w, fmt, args...) \ |
| 130 | printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \ |
| 131 | __FILE__, __LINE__, ## args) |
| 132 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 133 | static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state) |
| 134 | { |
| 135 | LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state)); |
| 136 | worker->state = new_state; |
| 137 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 138 | |
| 139 | static void worker_cleanup(void *arg) |
| 140 | { |
| 141 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 142 | struct bankd *bankd = worker->bankd; |
| 143 | |
| 144 | /* FIXME: should we still do this? in the thread ?!? */ |
| 145 | pthread_mutex_lock(&bankd->workers_mutex); |
| 146 | llist_del(&worker->list); |
| 147 | talloc_free(worker); /* FIXME: is this safe? */ |
| 148 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | #if 0 |
| 153 | /* function running inside a worker thread; doing some initialization */ |
| 154 | static void worker_init(struct bankd_worker *worker) |
| 155 | { |
| 156 | int rc; |
| 157 | |
| 158 | /* push cleanup helper */ |
| 159 | pthread_cleanup_push(&worker_cleanup, worker); |
| 160 | |
| 161 | /* The PC/SC context must be created inside the thread where we'll later use it */ |
| 162 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext); |
| 163 | PCSC_ERROR(rc, "SCardEstablishContext") |
| 164 | |
| 165 | rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED, |
| 166 | SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL); |
| 167 | PCSC_ERROR(rc, "SCardConnect") |
| 168 | |
| 169 | return; |
| 170 | end: |
| 171 | pthread_exit(NULL); |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | |
| 176 | static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size) |
| 177 | { |
| 178 | struct ipaccess_head *hh; |
| 179 | uint16_t len; |
| 180 | int needed, rc; |
| 181 | |
| 182 | if (buf_size < sizeof(*hh)) |
| 183 | return -1; |
| 184 | |
| 185 | hh = (struct ipaccess_head *) buf; |
| 186 | |
| 187 | /* 1) blocking read from the socket (IPA header) */ |
| 188 | rc = read(fd, buf, sizeof(*hh)); |
| 189 | if (rc < sizeof(*hh)) |
| 190 | return -2; |
| 191 | |
| 192 | len = ntohs(hh->len); |
| 193 | needed = len; //- sizeof(*hh); |
| 194 | |
| 195 | /* 2) blocking read from the socket (payload) */ |
| 196 | rc = read(fd, buf+sizeof(*hh), needed); |
| 197 | if (rc < needed) |
| 198 | return -3; |
| 199 | |
| 200 | return len; |
| 201 | } |
| 202 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 203 | static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 204 | { |
| 205 | OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq); |
| 206 | |
| 207 | const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity; |
| 208 | |
| 209 | LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n", |
| 210 | cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf); |
| 211 | /* FIXME: store somewhere? */ |
| 212 | |
| 213 | if (worker->state != BW_ST_CONN_WAIT_ID) { |
| 214 | LOGW(worker, "Unexpected connectClientReq\n"); |
| 215 | return -102; |
| 216 | } |
| 217 | |
| 218 | if (!pdu->msg.choice.connectClientReq.clientId) { |
| 219 | LOGW(worker, "missing clientID, aborting\n"); |
| 220 | return -103; |
| 221 | } |
| 222 | worker->client.id = *pdu->msg.choice.connectClientReq.clientId; |
| 223 | worker_set_state(worker, BW_ST_CONN_CLIENT); |
| 224 | |
| 225 | /* FIXME: resolve mapping */ |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 230 | /* handle one incoming RSPRO message from a client inside a worker thread */ |
| 231 | static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 232 | { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 233 | int rc = -100; |
| 234 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 235 | switch (pdu->msg.present) { |
| 236 | case RsproPDUchoice_PR_connectClientReq: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 237 | rc = worker_handle_connectClientReq(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 238 | break; |
| 239 | case RsproPDUchoice_PR_tpduModemToCard: |
| 240 | /* FIXME */ |
| 241 | break; |
| 242 | case RsproPDUchoice_PR_clientSlotStatusInd: |
| 243 | /* FIXME */ |
| 244 | break; |
| 245 | default: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 246 | rc = -101; |
| 247 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 250 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* body of the main transceive loop */ |
| 254 | static int worker_transceive_loop(struct bankd_worker *worker) |
| 255 | { |
| 256 | struct ipaccess_head *hh; |
| 257 | struct ipaccess_head_ext *hh_ext; |
| 258 | uint8_t buf[65536]; /* maximum length expressed in 16bit length field */ |
| 259 | asn_dec_rval_t rval; |
| 260 | int data_len, rc; |
| 261 | RsproPDU_t *pdu; |
| 262 | |
| 263 | /* 1) blocking read of entire IPA message from the socket */ |
| 264 | rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf)); |
| 265 | if (rc < 0) |
| 266 | return rc; |
| 267 | data_len = rc; |
| 268 | |
| 269 | hh = (struct ipaccess_head *) buf; |
| 270 | if (hh->proto != IPAC_PROTO_OSMO) |
| 271 | return -4; |
| 272 | |
| 273 | hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh); |
| 274 | if (data_len < sizeof(*hh_ext)) |
| 275 | return -5; |
| 276 | data_len -= sizeof(*hh_ext); |
| 277 | if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) |
| 278 | return -6; |
| 279 | |
| 280 | /* 2) ASN1 BER decode of the message */ |
| 281 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len); |
| 282 | if (rval.code != RC_OK) |
| 283 | return -7; |
| 284 | |
| 285 | /* 3) handling of the message, possibly resulting in PCSC commands */ |
| 286 | rc = worker_handle_rspro(worker, pdu); |
| 287 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
| 288 | if (rc < 0) |
| 289 | return rc; |
| 290 | |
| 291 | /* everything OK if we reach here */ |
| 292 | return 0; |
| 293 | } |
| 294 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 295 | /* obtain an ascii representation of the client IP/port */ |
| 296 | static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker) |
| 297 | { |
| 298 | char hostbuf[32], portbuf[32]; |
| 299 | int rc; |
| 300 | |
| 301 | rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr, |
| 302 | worker->client.peer_addr_len, hostbuf, sizeof(hostbuf), |
| 303 | portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV); |
| 304 | if (rc != 0) { |
| 305 | out[0] = '\0'; |
| 306 | return -1; |
| 307 | } |
| 308 | snprintf(out, outlen, "%s:%s", hostbuf, portbuf); |
| 309 | return 0; |
| 310 | } |
| 311 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 312 | /* worker thread main function */ |
| 313 | static void *worker_main(void *arg) |
| 314 | { |
| 315 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 316 | void *top_ctx; |
| 317 | int rc; |
| 318 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 319 | worker_set_state(worker, BW_ST_INIT); |
| 320 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 321 | /* not permitted in multithreaded environment */ |
| 322 | talloc_disable_null_tracking(); |
| 323 | top_ctx = talloc_named_const(NULL, 0, "top"); |
| 324 | talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1"); |
| 325 | |
| 326 | /* push cleanup helper */ |
| 327 | pthread_cleanup_push(&worker_cleanup, worker); |
| 328 | |
| 329 | /* we continuously perform the same loop here, recycling the worker thread |
| 330 | * once the client connection is gone or we have some trouble with the card/reader */ |
| 331 | while (1) { |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 332 | char buf[128]; |
| 333 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 334 | worker->client.peer_addr_len = sizeof(worker->client.peer_addr); |
| 335 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 336 | worker_set_state(worker, BW_ST_ACCEPTING); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 337 | /* first wait for an incoming TCP connection */ |
| 338 | rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr, |
| 339 | &worker->client.peer_addr_len); |
| 340 | if (rc < 0) { |
| 341 | continue; |
| 342 | } |
| 343 | worker->client.fd = rc; |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 344 | worker_client_addrstr(buf, sizeof(buf), worker); |
| 345 | LOGW(worker, "Accepted connection from %s\n", buf); |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 346 | worker_set_state(worker, BW_ST_CONN_WAIT_ID); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 347 | |
| 348 | /* run the main worker transceive loop body until there was some error */ |
| 349 | while (1) { |
| 350 | rc = worker_transceive_loop(worker); |
| 351 | if (rc < 0) |
| 352 | break; |
| 353 | } |
| 354 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 355 | LOGW(worker, "Error %d occurred: Cleaning up state\n", rc); |
| 356 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 357 | /* clean-up: reset to sane state */ |
| 358 | if (worker->reader.pcsc.hCard) { |
| 359 | SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD); |
| 360 | worker->reader.pcsc.hCard = 0; |
| 361 | } |
| 362 | if (worker->reader.pcsc.hContext) { |
| 363 | SCardReleaseContext(worker->reader.pcsc.hContext); |
| 364 | worker->reader.pcsc.hContext = 0; |
| 365 | } |
| 366 | if (worker->client.fd >= 0) |
| 367 | close(worker->client.fd); |
Harald Welte | 415e8f6 | 2018-08-16 14:47:38 +0200 | [diff] [blame^] | 368 | memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr)); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 369 | worker->client.fd = -1; |
| 370 | } |
| 371 | |
| 372 | pthread_cleanup_pop(1); |
| 373 | talloc_free(top_ctx); |
| 374 | pthread_exit(NULL); |
| 375 | } |