blob: c0a127fd6c849522f34356d9453f66fe7ccc85b3 [file] [log] [blame]
Harald Welte31c9eca2018-10-03 21:03:34 +02001#define _GNU_SOURCE
2
Harald Welte77911b02018-08-14 23:47:30 +02003#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 Welted6dfb8c2018-08-16 14:46:53 +020014#include <sys/socket.h>
15#include <netdb.h>
16
Harald Welte12534e72018-08-15 23:37:29 +020017#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020018#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020019#include <osmocom/core/logging.h>
20#include <osmocom/core/application.h>
Harald Welte77911b02018-08-14 23:47:30 +020021
22#include <osmocom/gsm/ipa.h>
23#include <osmocom/gsm/protocol/ipaccess.h>
24
25#include <asn1c/asn_application.h>
26#include <osmocom/rspro/RsproPDU.h>
27
28#include "bankd.h"
Harald Welte796a7492018-09-23 19:31:28 +020029#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020030
31__thread void *talloc_asn1_ctx;
32
33static void *worker_main(void *arg);
34
35/***********************************************************************
36* bankd core / main thread
37***********************************************************************/
38
Harald Weltef94b9ee2018-09-25 15:04:21 +020039static const struct log_info_cat default_categories[] = {
40 [DMAIN] = {
41 .name = "DMAIN",
42 .loglevel = LOGL_DEBUG,
43 .enabled = 1,
44 },
45};
46
47static const struct log_info log_info = {
48 .cat = default_categories,
49 .num_cat = ARRAY_SIZE(default_categories),
50};
51
Harald Welte77911b02018-08-14 23:47:30 +020052static void bankd_init(struct bankd *bankd)
53{
Harald Weltef94b9ee2018-09-25 15:04:21 +020054 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
55 osmo_init_logging2(g_tall_ctx, &log_info);
56
Harald Welte77911b02018-08-14 23:47:30 +020057 /* intialize members of 'bankd' */
58 INIT_LLIST_HEAD(&bankd->slot_mappings);
59 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
60 INIT_LLIST_HEAD(&bankd->workers);
61 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020062
Harald Weltef1dd1622018-09-24 14:54:23 +020063 bankd->comp_id.type = ComponentType_remsimBankd;
64 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
65 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
66 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
67 /* FIXME: other members of app_comp_id */
68
Harald Welte45c948c2018-09-23 19:26:52 +020069 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
70 * read once during bankd initialization, when the worker threads haven't
71 * started yet */
72 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
73 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020074
75 /* HACK HACK HACK */
76 {
77 struct bank_slot bs = { .bank_id = 1, };
78 struct client_slot cs = { .client_id = 23, };
79 int i;
80 for (i = 0; i < 5; i++) {
81 bs.slot_nr = cs.slot_nr = i;
82 bankd_slotmap_add(bankd, &bs, &cs);
83 }
84 }
Harald Welte77911b02018-08-14 23:47:30 +020085}
86
87/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020088static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020089{
90 struct bankd_worker *worker;
91 int rc;
92
93 worker = talloc_zero(bankd, struct bankd_worker);
94 if (!worker)
95 return NULL;
96
97 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020098 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020099
100 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
101
102 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
103 if (rc != 0) {
104 talloc_free(worker);
105 return NULL;
106 }
107
108 pthread_mutex_lock(&bankd->workers_mutex);
109 llist_add_tail(&worker->list, &bankd->workers);
110 pthread_mutex_unlock(&bankd->workers_mutex);
111
112 return worker;
113}
114
115static bool terminate = false;
116
117int main(int argc, char **argv)
118{
119 struct bankd *bankd = talloc_zero(NULL, struct bankd);
120 int i, rc;
121
122 OSMO_ASSERT(bankd);
123 bankd_init(bankd);
124
Harald Welte12534e72018-08-15 23:37:29 +0200125 /* create listening socket */
126 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
127 if (rc < 0)
128 exit(1);
129 bankd->accept_fd = rc;
130
131 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200132 for (i = 0; i < 10; i++) {
133 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200134 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200135 if (!w)
136 exit(21);
137 }
138
139 while (1) {
140 if (terminate)
141 break;
Harald Welte7f684a02018-08-16 14:43:50 +0200142 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200143 }
144
145 talloc_free(bankd);
146 exit(0);
147}
148
149
150
151/***********************************************************************
152 * bankd worker thread
153 ***********************************************************************/
154
Harald Welte8d858292018-08-15 23:36:46 +0200155struct value_string worker_state_names[] = {
156 { BW_ST_INIT, "INIT" },
157 { BW_ST_ACCEPTING, "ACCEPTING" },
158 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
159 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200160 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200161 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
162 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
163 { 0, NULL }
164};
165
Harald Welteceb3e682018-08-16 14:47:11 +0200166#define LOGW(w, fmt, args...) \
167 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
168 __FILE__, __LINE__, ## args)
169
Harald Welteaf614732018-08-17 22:10:05 +0200170#define PCSC_ERROR(w, rv, text) \
171if (rv != SCARD_S_SUCCESS) { \
172 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
173 goto end; \
174} else { \
175 LOGW((w), ": OK\n\n"); \
176}
177
Harald Welte8d858292018-08-15 23:36:46 +0200178static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
179{
180 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
181 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200182 worker->timeout = 0;
183}
184
185static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
186 unsigned int timeout_secs)
187{
188 LOGW(worker, "Changing state to %s (timeout=%u)\n",
189 get_value_string(worker_state_names, new_state), timeout_secs);
190 worker->state = new_state;
191 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200192}
Harald Welte77911b02018-08-14 23:47:30 +0200193
194static void worker_cleanup(void *arg)
195{
196 struct bankd_worker *worker = (struct bankd_worker *) arg;
197 struct bankd *bankd = worker->bankd;
198
199 /* FIXME: should we still do this? in the thread ?!? */
200 pthread_mutex_lock(&bankd->workers_mutex);
201 llist_del(&worker->list);
202 talloc_free(worker); /* FIXME: is this safe? */
203 pthread_mutex_unlock(&bankd->workers_mutex);
204}
205
206
Harald Welteaf614732018-08-17 22:10:05 +0200207static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200208{
Harald Welteaf614732018-08-17 22:10:05 +0200209 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200210
Harald Welte150d6d62018-10-03 23:07:47 +0200211 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
212
Harald Welte694df832018-10-03 22:47:52 +0200213 if (!worker->reader.name) {
214 /* resolve PC/SC reader name from slot_id -> name map */
215 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
216 if (!worker->reader.name) {
217 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
218 worker->slot.bank_id, worker->slot.slot_nr);
219 rc = -1;
220 goto end;
221 }
222 }
Harald Welte45c948c2018-09-23 19:26:52 +0200223 OSMO_ASSERT(worker->reader.name);
224
Harald Welte694df832018-10-03 22:47:52 +0200225 if (!worker->reader.pcsc.hContext) {
226 LOGW(worker, "Attempting to open PC/SC context\n");
227 /* The PC/SC context must be created inside the thread where we'll later use it */
228 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
229 PCSC_ERROR(worker, rc, "SCardEstablishContext")
230 }
Harald Welte45c948c2018-09-23 19:26:52 +0200231
Harald Welte694df832018-10-03 22:47:52 +0200232 if (!worker->reader.pcsc.hCard) {
233 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
234 DWORD dwActiveProtocol;
235 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
236 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
237 PCSC_ERROR(worker, rc, "SCardConnect")
238 }
Harald Welte77911b02018-08-14 23:47:30 +0200239
Harald Welte57593f02018-09-23 19:30:31 +0200240 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200241 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200242
Harald Welteaf614732018-08-17 22:10:05 +0200243 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200244end:
Harald Welteaf614732018-08-17 22:10:05 +0200245 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200246}
Harald Welte77911b02018-08-14 23:47:30 +0200247
248
249static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
250{
251 struct ipaccess_head *hh;
252 uint16_t len;
253 int needed, rc;
254
255 if (buf_size < sizeof(*hh))
256 return -1;
257
258 hh = (struct ipaccess_head *) buf;
259
260 /* 1) blocking read from the socket (IPA header) */
261 rc = read(fd, buf, sizeof(*hh));
262 if (rc < sizeof(*hh))
263 return -2;
264
265 len = ntohs(hh->len);
266 needed = len; //- sizeof(*hh);
267
268 /* 2) blocking read from the socket (payload) */
269 rc = read(fd, buf+sizeof(*hh), needed);
270 if (rc < needed)
271 return -3;
272
273 return len;
274}
275
Harald Welte796a7492018-09-23 19:31:28 +0200276static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
277{
278 struct msgb *msg = rspro_enc_msg(pdu);
279 int rc;
280
281 if (!msg) {
282 LOGW(worker, "error encoding RSPRO\n");
283 return -1;
284 }
285
Harald Weltefd471192018-09-24 14:51:14 +0200286 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200287 /* prepend the header */
288 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200289 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200290
291 /* actually send it through the socket */
292 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
293 if (rc == msgb_length(msg))
294 rc = 0;
295 else {
296 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
297 rc = -1;
298 }
299
300 msgb_free(msg);
301
302 return rc;
303}
304
Harald Welte150d6d62018-10-03 23:07:47 +0200305/* attempt to obtain slot-map */
306static int worker_try_slotmap(struct bankd_worker *worker)
307{
308 struct bankd_slot_mapping *slmap;
309
310 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
311 if (!slmap) {
312 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
313 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
314 /* check in 10s if the map has been installed meanwhile by main thread */
315 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
316 return -1;
317 } else {
318 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
319 slmap->client.client_id, slmap->client.slot_nr,
320 slmap->bank.bank_id, slmap->bank.slot_nr);
321 worker->slot = slmap->bank;
322 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
323 return worker_open_card(worker);
324 }
325}
326
327
Harald Weltecce2aad2018-08-16 14:44:37 +0200328static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
329{
Harald Welteaf614732018-08-17 22:10:05 +0200330 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte3e689872018-09-24 14:52:56 +0200331 e_ResultCode res;
332 RsproPDU_t *resp;
Harald Welteaf614732018-08-17 22:10:05 +0200333
Harald Weltecce2aad2018-08-16 14:44:37 +0200334 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
335
Harald Weltecce2aad2018-08-16 14:44:37 +0200336 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
337 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
338 /* FIXME: store somewhere? */
339
340 if (worker->state != BW_ST_CONN_WAIT_ID) {
341 LOGW(worker, "Unexpected connectClientReq\n");
342 return -102;
343 }
344
Harald Welte371d0262018-08-16 15:23:58 +0200345 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200346 LOGW(worker, "missing clientID, aborting\n");
347 return -103;
348 }
Harald Welte371d0262018-08-16 15:23:58 +0200349 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
350 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200351 worker_set_state(worker, BW_ST_CONN_CLIENT);
352
Harald Welte150d6d62018-10-03 23:07:47 +0200353 if (worker_try_slotmap(worker) >= 0)
354 res = ResultCode_ok;
355 else
Harald Welte3e689872018-09-24 14:52:56 +0200356 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200357
Harald Welte3e689872018-09-24 14:52:56 +0200358 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
359 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200360}
361
Harald Welte796a7492018-09-23 19:31:28 +0200362static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
363{
364 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
365 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
366 SCARD_IO_REQUEST pioRecvPci;
367 uint8_t rx_buf[1024];
368 DWORD rx_buf_len = sizeof(rx_buf);
369 RsproPDU_t *pdu_resp;
370 long rc;
371
372 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
373
374 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
375 LOGW(worker, "Unexpected tpduModemToCaard\n");
376 return -104;
377 }
378
379 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
380
381 rc = SCardTransmit(worker->reader.pcsc.hCard,
382 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
383 &pioRecvPci, rx_buf, &rx_buf_len);
384 PCSC_ERROR(worker, rc, "SCardTransmit");
385
386 /* encode response PDU and send it */
387 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
388 rx_buf, rx_buf_len);
389 worker_send_rspro(worker, pdu_resp);
390
391 return 0;
392end:
393 return rc;
394}
395
Harald Welte77911b02018-08-14 23:47:30 +0200396/* handle one incoming RSPRO message from a client inside a worker thread */
397static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
398{
Harald Weltecce2aad2018-08-16 14:44:37 +0200399 int rc = -100;
400
Harald Welte77911b02018-08-14 23:47:30 +0200401 switch (pdu->msg.present) {
402 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200403 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200404 break;
405 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200406 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200407 break;
408 case RsproPDUchoice_PR_clientSlotStatusInd:
409 /* FIXME */
410 break;
411 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200412 rc = -101;
413 break;
Harald Welte77911b02018-08-14 23:47:30 +0200414 }
415
Harald Weltecce2aad2018-08-16 14:44:37 +0200416 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200417}
418
Harald Welte694df832018-10-03 22:47:52 +0200419static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
420{
421 struct timeval tout = { timeout_secs, 0 };
422 fd_set readset;
423
424 FD_ZERO(&readset);
425 FD_SET(fd, &readset);
426 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
427}
428
Harald Welte77911b02018-08-14 23:47:30 +0200429/* body of the main transceive loop */
430static int worker_transceive_loop(struct bankd_worker *worker)
431{
432 struct ipaccess_head *hh;
433 struct ipaccess_head_ext *hh_ext;
434 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
435 asn_dec_rval_t rval;
436 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200437 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200438
Harald Welte694df832018-10-03 22:47:52 +0200439 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
440 if (rc == 0) {
441 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200442 switch (worker->state) {
443 case BW_ST_CONN_CLIENT_WAIT_MAP:
444 /* re-check if mapping exists meanwhile? */
445 worker_try_slotmap(worker);
446 break;
447 case BW_ST_CONN_CLIENT_MAPPED:
448 /* re-check if reader/card can be opened meanwhile? */
449 worker_open_card(worker);
450 break;
451 default:
452 OSMO_ASSERT(0);
453 }
454 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200455 return 0;
456 };
457
Harald Welte77911b02018-08-14 23:47:30 +0200458 /* 1) blocking read of entire IPA message from the socket */
459 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
460 if (rc < 0)
461 return rc;
462 data_len = rc;
463
464 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200465 if (hh->proto != IPAC_PROTO_OSMO) {
466 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200467 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200468 }
Harald Welte77911b02018-08-14 23:47:30 +0200469
470 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200471 if (data_len < sizeof(*hh_ext)) {
472 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200473 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200474 }
Harald Welte77911b02018-08-14 23:47:30 +0200475 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200476 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
477 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200478 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200479 }
Harald Welte77911b02018-08-14 23:47:30 +0200480
481 /* 2) ASN1 BER decode of the message */
482 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200483 if (rval.code != RC_OK) {
484 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200485 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200486 }
Harald Welte77911b02018-08-14 23:47:30 +0200487
488 /* 3) handling of the message, possibly resulting in PCSC commands */
489 rc = worker_handle_rspro(worker, pdu);
490 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200491 if (rc < 0) {
492 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200493 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200494 }
Harald Welte77911b02018-08-14 23:47:30 +0200495
496 /* everything OK if we reach here */
497 return 0;
498}
499
Harald Welted6dfb8c2018-08-16 14:46:53 +0200500/* obtain an ascii representation of the client IP/port */
501static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
502{
503 char hostbuf[32], portbuf[32];
504 int rc;
505
506 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
507 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
508 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
509 if (rc != 0) {
510 out[0] = '\0';
511 return -1;
512 }
513 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
514 return 0;
515}
516
Harald Welte77911b02018-08-14 23:47:30 +0200517/* worker thread main function */
518static void *worker_main(void *arg)
519{
520 struct bankd_worker *worker = (struct bankd_worker *) arg;
521 void *top_ctx;
522 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200523 char worker_name[32];
524
525 /* set the thread name */
526 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
527 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200528
Harald Welte8d858292018-08-15 23:36:46 +0200529 worker_set_state(worker, BW_ST_INIT);
530
Harald Welte77911b02018-08-14 23:47:30 +0200531 /* not permitted in multithreaded environment */
532 talloc_disable_null_tracking();
533 top_ctx = talloc_named_const(NULL, 0, "top");
534 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
535
536 /* push cleanup helper */
537 pthread_cleanup_push(&worker_cleanup, worker);
538
539 /* we continuously perform the same loop here, recycling the worker thread
540 * once the client connection is gone or we have some trouble with the card/reader */
541 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200542 char buf[128];
543
Harald Welte77911b02018-08-14 23:47:30 +0200544 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
545
Harald Welte8d858292018-08-15 23:36:46 +0200546 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200547 /* first wait for an incoming TCP connection */
548 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
549 &worker->client.peer_addr_len);
550 if (rc < 0) {
551 continue;
552 }
553 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200554 worker_client_addrstr(buf, sizeof(buf), worker);
555 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200556 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200557
558 /* run the main worker transceive loop body until there was some error */
559 while (1) {
560 rc = worker_transceive_loop(worker);
561 if (rc < 0)
562 break;
563 }
564
Harald Welted6dfb8c2018-08-16 14:46:53 +0200565 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
566
Harald Welte77911b02018-08-14 23:47:30 +0200567 /* clean-up: reset to sane state */
568 if (worker->reader.pcsc.hCard) {
569 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
570 worker->reader.pcsc.hCard = 0;
571 }
572 if (worker->reader.pcsc.hContext) {
573 SCardReleaseContext(worker->reader.pcsc.hContext);
574 worker->reader.pcsc.hContext = 0;
575 }
Harald Welte694df832018-10-03 22:47:52 +0200576 if (worker->reader.name)
577 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200578 if (worker->client.fd >= 0)
579 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200580 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200581 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200582 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200583 }
584
585 pthread_cleanup_pop(1);
586 talloc_free(top_ctx);
587 pthread_exit(NULL);
588}