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" |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 25 | #include "rspro_util.h" |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 26 | |
| 27 | __thread void *talloc_asn1_ctx; |
| 28 | |
| 29 | static void *worker_main(void *arg); |
| 30 | |
| 31 | /*********************************************************************** |
| 32 | * bankd core / main thread |
| 33 | ***********************************************************************/ |
| 34 | |
| 35 | static void bankd_init(struct bankd *bankd) |
| 36 | { |
| 37 | /* intialize members of 'bankd' */ |
| 38 | INIT_LLIST_HEAD(&bankd->slot_mappings); |
| 39 | pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL); |
| 40 | INIT_LLIST_HEAD(&bankd->workers); |
| 41 | pthread_mutex_init(&bankd->workers_mutex, NULL); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 42 | |
| 43 | /* Np lock or mutex required for the pcsc_slot_names list, as this is only |
| 44 | * read once during bankd initialization, when the worker threads haven't |
| 45 | * started yet */ |
| 46 | INIT_LLIST_HEAD(&bankd->pcsc_slot_names); |
| 47 | OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0); |
Harald Welte | e72e573 | 2018-09-23 19:31:55 +0200 | [diff] [blame^] | 48 | |
| 49 | /* HACK HACK HACK */ |
| 50 | { |
| 51 | struct bank_slot bs = { .bank_id = 1, }; |
| 52 | struct client_slot cs = { .client_id = 23, }; |
| 53 | int i; |
| 54 | for (i = 0; i < 5; i++) { |
| 55 | bs.slot_nr = cs.slot_nr = i; |
| 56 | bankd_slotmap_add(bankd, &bs, &cs); |
| 57 | } |
| 58 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* create + start a new bankd_worker thread */ |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 62 | 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] | 63 | { |
| 64 | struct bankd_worker *worker; |
| 65 | int rc; |
| 66 | |
| 67 | worker = talloc_zero(bankd, struct bankd_worker); |
| 68 | if (!worker) |
| 69 | return NULL; |
| 70 | |
| 71 | worker->bankd = bankd; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 72 | worker->num = i; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 73 | |
| 74 | /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */ |
| 75 | |
| 76 | rc = pthread_create(&worker->thread, NULL, worker_main, worker); |
| 77 | if (rc != 0) { |
| 78 | talloc_free(worker); |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | pthread_mutex_lock(&bankd->workers_mutex); |
| 83 | llist_add_tail(&worker->list, &bankd->workers); |
| 84 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 85 | |
| 86 | return worker; |
| 87 | } |
| 88 | |
| 89 | static bool terminate = false; |
| 90 | |
| 91 | int main(int argc, char **argv) |
| 92 | { |
| 93 | struct bankd *bankd = talloc_zero(NULL, struct bankd); |
| 94 | int i, rc; |
| 95 | |
| 96 | OSMO_ASSERT(bankd); |
| 97 | bankd_init(bankd); |
| 98 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 99 | /* create listening socket */ |
| 100 | rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND); |
| 101 | if (rc < 0) |
| 102 | exit(1); |
| 103 | bankd->accept_fd = rc; |
| 104 | |
| 105 | /* create worker threads. FIXME: one per reader/slot! */ |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 106 | for (i = 0; i < 10; i++) { |
| 107 | struct bankd_worker *w; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 108 | w = bankd_create_worker(bankd, i); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 109 | if (!w) |
| 110 | exit(21); |
| 111 | } |
| 112 | |
| 113 | while (1) { |
| 114 | if (terminate) |
| 115 | break; |
Harald Welte | 7f684a0 | 2018-08-16 14:43:50 +0200 | [diff] [blame] | 116 | sleep(1); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | talloc_free(bankd); |
| 120 | exit(0); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | /*********************************************************************** |
| 126 | * bankd worker thread |
| 127 | ***********************************************************************/ |
| 128 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 129 | struct value_string worker_state_names[] = { |
| 130 | { BW_ST_INIT, "INIT" }, |
| 131 | { BW_ST_ACCEPTING, "ACCEPTING" }, |
| 132 | { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" }, |
| 133 | { BW_ST_CONN_CLIENT, "CONN_CLIENT" }, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 134 | { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" }, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 135 | { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" }, |
| 136 | { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" }, |
| 137 | { 0, NULL } |
| 138 | }; |
| 139 | |
Harald Welte | ceb3e68 | 2018-08-16 14:47:11 +0200 | [diff] [blame] | 140 | #define LOGW(w, fmt, args...) \ |
| 141 | printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \ |
| 142 | __FILE__, __LINE__, ## args) |
| 143 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 144 | #define PCSC_ERROR(w, rv, text) \ |
| 145 | if (rv != SCARD_S_SUCCESS) { \ |
| 146 | LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ |
| 147 | goto end; \ |
| 148 | } else { \ |
| 149 | LOGW((w), ": OK\n\n"); \ |
| 150 | } |
| 151 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 152 | static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state) |
| 153 | { |
| 154 | LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state)); |
| 155 | worker->state = new_state; |
| 156 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 157 | |
| 158 | static void worker_cleanup(void *arg) |
| 159 | { |
| 160 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 161 | struct bankd *bankd = worker->bankd; |
| 162 | |
| 163 | /* FIXME: should we still do this? in the thread ?!? */ |
| 164 | pthread_mutex_lock(&bankd->workers_mutex); |
| 165 | llist_del(&worker->list); |
| 166 | talloc_free(worker); /* FIXME: is this safe? */ |
| 167 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 168 | } |
| 169 | |
| 170 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 171 | static int worker_open_card(struct bankd_worker *worker) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 172 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 173 | long rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 174 | |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 175 | /* resolve PC/SC reader name from slot_id -> name map */ |
| 176 | worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot); |
| 177 | OSMO_ASSERT(worker->reader.name); |
| 178 | |
| 179 | LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name); |
| 180 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 181 | /* The PC/SC context must be created inside the thread where we'll later use it */ |
| 182 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext); |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 183 | PCSC_ERROR(worker, rc, "SCardEstablishContext") |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 184 | |
Harald Welte | 48865c2 | 2018-09-23 19:30:07 +0200 | [diff] [blame] | 185 | DWORD dwActiveProtocol; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 186 | rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED, |
Harald Welte | 48865c2 | 2018-09-23 19:30:07 +0200 | [diff] [blame] | 187 | SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol); |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 188 | PCSC_ERROR(worker, rc, "SCardConnect") |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 189 | |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 190 | worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD); |
| 191 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 192 | return 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 193 | end: |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 194 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 195 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 196 | |
| 197 | |
| 198 | static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size) |
| 199 | { |
| 200 | struct ipaccess_head *hh; |
| 201 | uint16_t len; |
| 202 | int needed, rc; |
| 203 | |
| 204 | if (buf_size < sizeof(*hh)) |
| 205 | return -1; |
| 206 | |
| 207 | hh = (struct ipaccess_head *) buf; |
| 208 | |
| 209 | /* 1) blocking read from the socket (IPA header) */ |
| 210 | rc = read(fd, buf, sizeof(*hh)); |
| 211 | if (rc < sizeof(*hh)) |
| 212 | return -2; |
| 213 | |
| 214 | len = ntohs(hh->len); |
| 215 | needed = len; //- sizeof(*hh); |
| 216 | |
| 217 | /* 2) blocking read from the socket (payload) */ |
| 218 | rc = read(fd, buf+sizeof(*hh), needed); |
| 219 | if (rc < needed) |
| 220 | return -3; |
| 221 | |
| 222 | return len; |
| 223 | } |
| 224 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 225 | static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu) |
| 226 | { |
| 227 | struct msgb *msg = rspro_enc_msg(pdu); |
| 228 | int rc; |
| 229 | |
| 230 | if (!msg) { |
| 231 | LOGW(worker, "error encoding RSPRO\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | /* prepend the header */ |
| 236 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO); |
| 237 | |
| 238 | /* actually send it through the socket */ |
| 239 | rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg)); |
| 240 | if (rc == msgb_length(msg)) |
| 241 | rc = 0; |
| 242 | else { |
| 243 | LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg)); |
| 244 | rc = -1; |
| 245 | } |
| 246 | |
| 247 | msgb_free(msg); |
| 248 | |
| 249 | return rc; |
| 250 | } |
| 251 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 252 | static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 253 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 254 | const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity; |
| 255 | struct bankd_slot_mapping *slmap; |
| 256 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 257 | OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq); |
| 258 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 259 | |
| 260 | LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n", |
| 261 | cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf); |
| 262 | /* FIXME: store somewhere? */ |
| 263 | |
| 264 | if (worker->state != BW_ST_CONN_WAIT_ID) { |
| 265 | LOGW(worker, "Unexpected connectClientReq\n"); |
| 266 | return -102; |
| 267 | } |
| 268 | |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 269 | if (!pdu->msg.choice.connectClientReq.clientSlot) { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 270 | LOGW(worker, "missing clientID, aborting\n"); |
| 271 | return -103; |
| 272 | } |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 273 | worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId; |
| 274 | worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 275 | worker_set_state(worker, BW_ST_CONN_CLIENT); |
| 276 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 277 | slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot); |
| 278 | if (!slmap) { |
| 279 | LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n", |
| 280 | worker->client.clslot.client_id, worker->client.clslot.slot_nr); |
| 281 | worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP); |
| 282 | /* FIXME: how to update the map in case a map is installed later */ |
| 283 | } else { |
| 284 | LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n", |
| 285 | slmap->client.client_id, slmap->client.slot_nr, |
| 286 | slmap->bank.bank_id, slmap->bank.slot_nr); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 287 | worker->slot = slmap->bank; |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 288 | worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 289 | /* actually open the mapped reader/card/slot */ |
| 290 | worker_open_card(worker); |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 291 | } |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 296 | static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 297 | { |
| 298 | const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard; |
| 299 | const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0; |
| 300 | SCARD_IO_REQUEST pioRecvPci; |
| 301 | uint8_t rx_buf[1024]; |
| 302 | DWORD rx_buf_len = sizeof(rx_buf); |
| 303 | RsproPDU_t *pdu_resp; |
| 304 | long rc; |
| 305 | |
| 306 | LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size)); |
| 307 | |
| 308 | if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) { |
| 309 | LOGW(worker, "Unexpected tpduModemToCaard\n"); |
| 310 | return -104; |
| 311 | } |
| 312 | |
| 313 | /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */ |
| 314 | |
| 315 | rc = SCardTransmit(worker->reader.pcsc.hCard, |
| 316 | pioSendPci, mdm2sim->data.buf, mdm2sim->data.size, |
| 317 | &pioRecvPci, rx_buf, &rx_buf_len); |
| 318 | PCSC_ERROR(worker, rc, "SCardTransmit"); |
| 319 | |
| 320 | /* encode response PDU and send it */ |
| 321 | pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot, |
| 322 | rx_buf, rx_buf_len); |
| 323 | worker_send_rspro(worker, pdu_resp); |
| 324 | |
| 325 | return 0; |
| 326 | end: |
| 327 | return rc; |
| 328 | } |
| 329 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 330 | /* handle one incoming RSPRO message from a client inside a worker thread */ |
| 331 | static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 332 | { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 333 | int rc = -100; |
| 334 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 335 | switch (pdu->msg.present) { |
| 336 | case RsproPDUchoice_PR_connectClientReq: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 337 | rc = worker_handle_connectClientReq(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 338 | break; |
| 339 | case RsproPDUchoice_PR_tpduModemToCard: |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 340 | rc = worker_handle_tpduModemToCard(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 341 | break; |
| 342 | case RsproPDUchoice_PR_clientSlotStatusInd: |
| 343 | /* FIXME */ |
| 344 | break; |
| 345 | default: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 346 | rc = -101; |
| 347 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 350 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* body of the main transceive loop */ |
| 354 | static int worker_transceive_loop(struct bankd_worker *worker) |
| 355 | { |
| 356 | struct ipaccess_head *hh; |
| 357 | struct ipaccess_head_ext *hh_ext; |
| 358 | uint8_t buf[65536]; /* maximum length expressed in 16bit length field */ |
| 359 | asn_dec_rval_t rval; |
| 360 | int data_len, rc; |
| 361 | RsproPDU_t *pdu; |
| 362 | |
| 363 | /* 1) blocking read of entire IPA message from the socket */ |
| 364 | rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf)); |
| 365 | if (rc < 0) |
| 366 | return rc; |
| 367 | data_len = rc; |
| 368 | |
| 369 | hh = (struct ipaccess_head *) buf; |
| 370 | if (hh->proto != IPAC_PROTO_OSMO) |
| 371 | return -4; |
| 372 | |
| 373 | hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh); |
| 374 | if (data_len < sizeof(*hh_ext)) |
| 375 | return -5; |
| 376 | data_len -= sizeof(*hh_ext); |
| 377 | if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) |
| 378 | return -6; |
| 379 | |
| 380 | /* 2) ASN1 BER decode of the message */ |
| 381 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len); |
| 382 | if (rval.code != RC_OK) |
| 383 | return -7; |
| 384 | |
| 385 | /* 3) handling of the message, possibly resulting in PCSC commands */ |
| 386 | rc = worker_handle_rspro(worker, pdu); |
| 387 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
| 388 | if (rc < 0) |
| 389 | return rc; |
| 390 | |
| 391 | /* everything OK if we reach here */ |
| 392 | return 0; |
| 393 | } |
| 394 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 395 | /* obtain an ascii representation of the client IP/port */ |
| 396 | static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker) |
| 397 | { |
| 398 | char hostbuf[32], portbuf[32]; |
| 399 | int rc; |
| 400 | |
| 401 | rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr, |
| 402 | worker->client.peer_addr_len, hostbuf, sizeof(hostbuf), |
| 403 | portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV); |
| 404 | if (rc != 0) { |
| 405 | out[0] = '\0'; |
| 406 | return -1; |
| 407 | } |
| 408 | snprintf(out, outlen, "%s:%s", hostbuf, portbuf); |
| 409 | return 0; |
| 410 | } |
| 411 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 412 | /* worker thread main function */ |
| 413 | static void *worker_main(void *arg) |
| 414 | { |
| 415 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 416 | void *top_ctx; |
| 417 | int rc; |
| 418 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 419 | worker_set_state(worker, BW_ST_INIT); |
| 420 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 421 | /* not permitted in multithreaded environment */ |
| 422 | talloc_disable_null_tracking(); |
| 423 | top_ctx = talloc_named_const(NULL, 0, "top"); |
| 424 | talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1"); |
| 425 | |
| 426 | /* push cleanup helper */ |
| 427 | pthread_cleanup_push(&worker_cleanup, worker); |
| 428 | |
| 429 | /* we continuously perform the same loop here, recycling the worker thread |
| 430 | * once the client connection is gone or we have some trouble with the card/reader */ |
| 431 | while (1) { |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 432 | char buf[128]; |
| 433 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 434 | worker->client.peer_addr_len = sizeof(worker->client.peer_addr); |
| 435 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 436 | worker_set_state(worker, BW_ST_ACCEPTING); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 437 | /* first wait for an incoming TCP connection */ |
| 438 | rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr, |
| 439 | &worker->client.peer_addr_len); |
| 440 | if (rc < 0) { |
| 441 | continue; |
| 442 | } |
| 443 | worker->client.fd = rc; |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 444 | worker_client_addrstr(buf, sizeof(buf), worker); |
| 445 | LOGW(worker, "Accepted connection from %s\n", buf); |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 446 | worker_set_state(worker, BW_ST_CONN_WAIT_ID); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 447 | |
| 448 | /* run the main worker transceive loop body until there was some error */ |
| 449 | while (1) { |
| 450 | rc = worker_transceive_loop(worker); |
| 451 | if (rc < 0) |
| 452 | break; |
| 453 | } |
| 454 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 455 | LOGW(worker, "Error %d occurred: Cleaning up state\n", rc); |
| 456 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 457 | /* clean-up: reset to sane state */ |
| 458 | if (worker->reader.pcsc.hCard) { |
| 459 | SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD); |
| 460 | worker->reader.pcsc.hCard = 0; |
| 461 | } |
| 462 | if (worker->reader.pcsc.hContext) { |
| 463 | SCardReleaseContext(worker->reader.pcsc.hContext); |
| 464 | worker->reader.pcsc.hContext = 0; |
| 465 | } |
| 466 | if (worker->client.fd >= 0) |
| 467 | close(worker->client.fd); |
Harald Welte | 415e8f6 | 2018-08-16 14:47:38 +0200 | [diff] [blame] | 468 | memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr)); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 469 | worker->client.fd = -1; |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 470 | worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | pthread_cleanup_pop(1); |
| 474 | talloc_free(top_ctx); |
| 475 | pthread_exit(NULL); |
| 476 | } |