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