blob: ecb3d3261851a385b7f03cb48d9fd53c554d6af0 [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 Welte8f893ff2018-10-03 23:21:10 +0200142 /* FIXME: Connect to remsim-server from the main thread, register with
143 * it and await + process any slot mapping or other configuration commands.
144 * Ensure to re-connect as needed. */
145
146 /* we should generalize the SRVC (server connection) FSM from remsim-client
147 * and use it here. As long as only the main thread is using osmo_fsm, things
148 * are safe with regard to other threads */
Harald Welte7f684a02018-08-16 14:43:50 +0200149 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200150 }
151
152 talloc_free(bankd);
153 exit(0);
154}
155
156
157
158/***********************************************************************
159 * bankd worker thread
160 ***********************************************************************/
161
Harald Welte8d858292018-08-15 23:36:46 +0200162struct value_string worker_state_names[] = {
163 { BW_ST_INIT, "INIT" },
164 { BW_ST_ACCEPTING, "ACCEPTING" },
165 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
166 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200167 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200168 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
169 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
170 { 0, NULL }
171};
172
Harald Welteceb3e682018-08-16 14:47:11 +0200173#define LOGW(w, fmt, args...) \
174 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
175 __FILE__, __LINE__, ## args)
176
Harald Welteaf614732018-08-17 22:10:05 +0200177#define PCSC_ERROR(w, rv, text) \
178if (rv != SCARD_S_SUCCESS) { \
179 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
180 goto end; \
181} else { \
Harald Welte7b41d9c2018-10-03 23:15:10 +0200182 LOGW((w), ": OK\n"); \
Harald Welteaf614732018-08-17 22:10:05 +0200183}
184
Harald Welte8d858292018-08-15 23:36:46 +0200185static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
186{
187 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
188 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200189 worker->timeout = 0;
190}
191
192static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
193 unsigned int timeout_secs)
194{
195 LOGW(worker, "Changing state to %s (timeout=%u)\n",
196 get_value_string(worker_state_names, new_state), timeout_secs);
197 worker->state = new_state;
198 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200199}
Harald Welte77911b02018-08-14 23:47:30 +0200200
201static void worker_cleanup(void *arg)
202{
203 struct bankd_worker *worker = (struct bankd_worker *) arg;
204 struct bankd *bankd = worker->bankd;
205
206 /* FIXME: should we still do this? in the thread ?!? */
207 pthread_mutex_lock(&bankd->workers_mutex);
208 llist_del(&worker->list);
209 talloc_free(worker); /* FIXME: is this safe? */
210 pthread_mutex_unlock(&bankd->workers_mutex);
211}
212
213
Harald Welteaf614732018-08-17 22:10:05 +0200214static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200215{
Harald Welteaf614732018-08-17 22:10:05 +0200216 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200217
Harald Welte150d6d62018-10-03 23:07:47 +0200218 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
219
Harald Welte694df832018-10-03 22:47:52 +0200220 if (!worker->reader.name) {
221 /* resolve PC/SC reader name from slot_id -> name map */
222 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
223 if (!worker->reader.name) {
224 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
225 worker->slot.bank_id, worker->slot.slot_nr);
226 rc = -1;
227 goto end;
228 }
229 }
Harald Welte45c948c2018-09-23 19:26:52 +0200230 OSMO_ASSERT(worker->reader.name);
231
Harald Welte694df832018-10-03 22:47:52 +0200232 if (!worker->reader.pcsc.hContext) {
233 LOGW(worker, "Attempting to open PC/SC context\n");
234 /* The PC/SC context must be created inside the thread where we'll later use it */
235 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
236 PCSC_ERROR(worker, rc, "SCardEstablishContext")
237 }
Harald Welte45c948c2018-09-23 19:26:52 +0200238
Harald Welte694df832018-10-03 22:47:52 +0200239 if (!worker->reader.pcsc.hCard) {
240 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
241 DWORD dwActiveProtocol;
242 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
243 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
244 PCSC_ERROR(worker, rc, "SCardConnect")
245 }
Harald Welte77911b02018-08-14 23:47:30 +0200246
Harald Welte57593f02018-09-23 19:30:31 +0200247 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200248 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200249
Harald Welteaf614732018-08-17 22:10:05 +0200250 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200251end:
Harald Welteaf614732018-08-17 22:10:05 +0200252 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200253}
Harald Welte77911b02018-08-14 23:47:30 +0200254
255
256static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
257{
258 struct ipaccess_head *hh;
259 uint16_t len;
260 int needed, rc;
261
262 if (buf_size < sizeof(*hh))
263 return -1;
264
265 hh = (struct ipaccess_head *) buf;
266
267 /* 1) blocking read from the socket (IPA header) */
268 rc = read(fd, buf, sizeof(*hh));
269 if (rc < sizeof(*hh))
270 return -2;
271
272 len = ntohs(hh->len);
273 needed = len; //- sizeof(*hh);
274
275 /* 2) blocking read from the socket (payload) */
276 rc = read(fd, buf+sizeof(*hh), needed);
277 if (rc < needed)
278 return -3;
279
280 return len;
281}
282
Harald Welte796a7492018-09-23 19:31:28 +0200283static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
284{
285 struct msgb *msg = rspro_enc_msg(pdu);
286 int rc;
287
288 if (!msg) {
289 LOGW(worker, "error encoding RSPRO\n");
290 return -1;
291 }
292
Harald Weltefd471192018-09-24 14:51:14 +0200293 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200294 /* prepend the header */
295 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200296 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200297
298 /* actually send it through the socket */
299 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
300 if (rc == msgb_length(msg))
301 rc = 0;
302 else {
303 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
304 rc = -1;
305 }
306
307 msgb_free(msg);
308
309 return rc;
310}
311
Harald Welte150d6d62018-10-03 23:07:47 +0200312/* attempt to obtain slot-map */
313static int worker_try_slotmap(struct bankd_worker *worker)
314{
315 struct bankd_slot_mapping *slmap;
316
317 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
318 if (!slmap) {
319 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
320 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
321 /* check in 10s if the map has been installed meanwhile by main thread */
322 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
323 return -1;
324 } else {
325 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
326 slmap->client.client_id, slmap->client.slot_nr,
327 slmap->bank.bank_id, slmap->bank.slot_nr);
328 worker->slot = slmap->bank;
329 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
330 return worker_open_card(worker);
331 }
332}
333
334
Harald Weltecce2aad2018-08-16 14:44:37 +0200335static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
336{
Harald Welteaf614732018-08-17 22:10:05 +0200337 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte3e689872018-09-24 14:52:56 +0200338 e_ResultCode res;
339 RsproPDU_t *resp;
Harald Welteaf614732018-08-17 22:10:05 +0200340
Harald Weltecce2aad2018-08-16 14:44:37 +0200341 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
342
Harald Weltecce2aad2018-08-16 14:44:37 +0200343 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
344 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
345 /* FIXME: store somewhere? */
346
347 if (worker->state != BW_ST_CONN_WAIT_ID) {
348 LOGW(worker, "Unexpected connectClientReq\n");
349 return -102;
350 }
351
Harald Welte371d0262018-08-16 15:23:58 +0200352 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200353 LOGW(worker, "missing clientID, aborting\n");
354 return -103;
355 }
Harald Welte371d0262018-08-16 15:23:58 +0200356 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
357 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200358 worker_set_state(worker, BW_ST_CONN_CLIENT);
359
Harald Welte150d6d62018-10-03 23:07:47 +0200360 if (worker_try_slotmap(worker) >= 0)
361 res = ResultCode_ok;
362 else
Harald Welte3e689872018-09-24 14:52:56 +0200363 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200364
Harald Welte3e689872018-09-24 14:52:56 +0200365 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
366 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200367}
368
Harald Welte796a7492018-09-23 19:31:28 +0200369static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
370{
371 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
372 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
373 SCARD_IO_REQUEST pioRecvPci;
374 uint8_t rx_buf[1024];
375 DWORD rx_buf_len = sizeof(rx_buf);
376 RsproPDU_t *pdu_resp;
377 long rc;
378
379 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
380
381 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
382 LOGW(worker, "Unexpected tpduModemToCaard\n");
383 return -104;
384 }
385
386 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
387
388 rc = SCardTransmit(worker->reader.pcsc.hCard,
389 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
390 &pioRecvPci, rx_buf, &rx_buf_len);
391 PCSC_ERROR(worker, rc, "SCardTransmit");
392
393 /* encode response PDU and send it */
394 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
395 rx_buf, rx_buf_len);
396 worker_send_rspro(worker, pdu_resp);
397
398 return 0;
399end:
400 return rc;
401}
402
Harald Welte77911b02018-08-14 23:47:30 +0200403/* handle one incoming RSPRO message from a client inside a worker thread */
404static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
405{
Harald Weltecce2aad2018-08-16 14:44:37 +0200406 int rc = -100;
407
Harald Welte77911b02018-08-14 23:47:30 +0200408 switch (pdu->msg.present) {
409 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200410 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200411 break;
412 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200413 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200414 break;
415 case RsproPDUchoice_PR_clientSlotStatusInd:
416 /* FIXME */
417 break;
418 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200419 rc = -101;
420 break;
Harald Welte77911b02018-08-14 23:47:30 +0200421 }
422
Harald Weltecce2aad2018-08-16 14:44:37 +0200423 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200424}
425
Harald Welte694df832018-10-03 22:47:52 +0200426static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
427{
428 struct timeval tout = { timeout_secs, 0 };
429 fd_set readset;
430
431 FD_ZERO(&readset);
432 FD_SET(fd, &readset);
433 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
434}
435
Harald Welte77911b02018-08-14 23:47:30 +0200436/* body of the main transceive loop */
437static int worker_transceive_loop(struct bankd_worker *worker)
438{
439 struct ipaccess_head *hh;
440 struct ipaccess_head_ext *hh_ext;
441 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
442 asn_dec_rval_t rval;
443 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200444 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200445
Harald Welte694df832018-10-03 22:47:52 +0200446 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
447 if (rc == 0) {
448 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200449 switch (worker->state) {
450 case BW_ST_CONN_CLIENT_WAIT_MAP:
451 /* re-check if mapping exists meanwhile? */
452 worker_try_slotmap(worker);
453 break;
454 case BW_ST_CONN_CLIENT_MAPPED:
455 /* re-check if reader/card can be opened meanwhile? */
456 worker_open_card(worker);
457 break;
458 default:
459 OSMO_ASSERT(0);
460 }
461 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200462 return 0;
463 };
464
Harald Welte77911b02018-08-14 23:47:30 +0200465 /* 1) blocking read of entire IPA message from the socket */
466 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
467 if (rc < 0)
468 return rc;
469 data_len = rc;
470
471 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200472 if (hh->proto != IPAC_PROTO_OSMO) {
473 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200474 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200475 }
Harald Welte77911b02018-08-14 23:47:30 +0200476
477 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200478 if (data_len < sizeof(*hh_ext)) {
479 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200480 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200481 }
Harald Welte77911b02018-08-14 23:47:30 +0200482 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200483 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
484 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200485 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200486 }
Harald Welte77911b02018-08-14 23:47:30 +0200487
488 /* 2) ASN1 BER decode of the message */
489 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200490 if (rval.code != RC_OK) {
491 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200492 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200493 }
Harald Welte77911b02018-08-14 23:47:30 +0200494
495 /* 3) handling of the message, possibly resulting in PCSC commands */
496 rc = worker_handle_rspro(worker, pdu);
497 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200498 if (rc < 0) {
499 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200500 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200501 }
Harald Welte77911b02018-08-14 23:47:30 +0200502
503 /* everything OK if we reach here */
504 return 0;
505}
506
Harald Welted6dfb8c2018-08-16 14:46:53 +0200507/* obtain an ascii representation of the client IP/port */
508static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
509{
510 char hostbuf[32], portbuf[32];
511 int rc;
512
513 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
514 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
515 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
516 if (rc != 0) {
517 out[0] = '\0';
518 return -1;
519 }
520 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
521 return 0;
522}
523
Harald Welte77911b02018-08-14 23:47:30 +0200524/* worker thread main function */
525static void *worker_main(void *arg)
526{
527 struct bankd_worker *worker = (struct bankd_worker *) arg;
528 void *top_ctx;
529 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200530 char worker_name[32];
531
532 /* set the thread name */
533 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
534 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200535
Harald Welte8d858292018-08-15 23:36:46 +0200536 worker_set_state(worker, BW_ST_INIT);
537
Harald Welte77911b02018-08-14 23:47:30 +0200538 /* not permitted in multithreaded environment */
539 talloc_disable_null_tracking();
540 top_ctx = talloc_named_const(NULL, 0, "top");
541 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
542
543 /* push cleanup helper */
544 pthread_cleanup_push(&worker_cleanup, worker);
545
546 /* we continuously perform the same loop here, recycling the worker thread
547 * once the client connection is gone or we have some trouble with the card/reader */
548 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200549 char buf[128];
550
Harald Welte77911b02018-08-14 23:47:30 +0200551 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
552
Harald Welte8d858292018-08-15 23:36:46 +0200553 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200554 /* first wait for an incoming TCP connection */
555 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
556 &worker->client.peer_addr_len);
557 if (rc < 0) {
558 continue;
559 }
560 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200561 worker_client_addrstr(buf, sizeof(buf), worker);
562 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200563 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200564
565 /* run the main worker transceive loop body until there was some error */
566 while (1) {
567 rc = worker_transceive_loop(worker);
568 if (rc < 0)
569 break;
570 }
571
Harald Welted6dfb8c2018-08-16 14:46:53 +0200572 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
573
Harald Welte77911b02018-08-14 23:47:30 +0200574 /* clean-up: reset to sane state */
575 if (worker->reader.pcsc.hCard) {
576 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
577 worker->reader.pcsc.hCard = 0;
578 }
579 if (worker->reader.pcsc.hContext) {
580 SCardReleaseContext(worker->reader.pcsc.hContext);
581 worker->reader.pcsc.hContext = 0;
582 }
Harald Welte694df832018-10-03 22:47:52 +0200583 if (worker->reader.name)
584 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200585 if (worker->client.fd >= 0)
586 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200587 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200588 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200589 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200590 }
591
592 pthread_cleanup_pop(1);
593 talloc_free(top_ctx);
594 pthread_exit(NULL);
595}