blob: 08129cccb53e7a959a6a69b7f4455e9a773a2300 [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 Welte43ab79f2018-10-03 23:34:21 +020052int asn_debug;
53
Harald Welte77911b02018-08-14 23:47:30 +020054static void bankd_init(struct bankd *bankd)
55{
Harald Weltef94b9ee2018-09-25 15:04:21 +020056 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
57 osmo_init_logging2(g_tall_ctx, &log_info);
58
Harald Welte43ab79f2018-10-03 23:34:21 +020059 asn_debug = 0;
60
Harald Welte77911b02018-08-14 23:47:30 +020061 /* intialize members of 'bankd' */
62 INIT_LLIST_HEAD(&bankd->slot_mappings);
63 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
64 INIT_LLIST_HEAD(&bankd->workers);
65 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020066
Harald Weltef1dd1622018-09-24 14:54:23 +020067 bankd->comp_id.type = ComponentType_remsimBankd;
68 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
69 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
70 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
71 /* FIXME: other members of app_comp_id */
72
Harald Welte45c948c2018-09-23 19:26:52 +020073 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
74 * read once during bankd initialization, when the worker threads haven't
75 * started yet */
76 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
77 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020078
79 /* HACK HACK HACK */
80 {
81 struct bank_slot bs = { .bank_id = 1, };
82 struct client_slot cs = { .client_id = 23, };
83 int i;
84 for (i = 0; i < 5; i++) {
85 bs.slot_nr = cs.slot_nr = i;
86 bankd_slotmap_add(bankd, &bs, &cs);
87 }
88 }
Harald Welte77911b02018-08-14 23:47:30 +020089}
90
91/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020092static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020093{
94 struct bankd_worker *worker;
95 int rc;
96
97 worker = talloc_zero(bankd, struct bankd_worker);
98 if (!worker)
99 return NULL;
100
101 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200102 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +0200103
104 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
105
106 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
107 if (rc != 0) {
108 talloc_free(worker);
109 return NULL;
110 }
111
112 pthread_mutex_lock(&bankd->workers_mutex);
113 llist_add_tail(&worker->list, &bankd->workers);
114 pthread_mutex_unlock(&bankd->workers_mutex);
115
116 return worker;
117}
118
119static bool terminate = false;
120
121int main(int argc, char **argv)
122{
123 struct bankd *bankd = talloc_zero(NULL, struct bankd);
124 int i, rc;
125
126 OSMO_ASSERT(bankd);
127 bankd_init(bankd);
128
Harald Welte12534e72018-08-15 23:37:29 +0200129 /* create listening socket */
130 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
131 if (rc < 0)
132 exit(1);
133 bankd->accept_fd = rc;
134
135 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200136 for (i = 0; i < 10; i++) {
137 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200138 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200139 if (!w)
140 exit(21);
141 }
142
143 while (1) {
144 if (terminate)
145 break;
Harald Welte8f893ff2018-10-03 23:21:10 +0200146 /* FIXME: Connect to remsim-server from the main thread, register with
147 * it and await + process any slot mapping or other configuration commands.
148 * Ensure to re-connect as needed. */
149
150 /* we should generalize the SRVC (server connection) FSM from remsim-client
151 * and use it here. As long as only the main thread is using osmo_fsm, things
152 * are safe with regard to other threads */
Harald Welte7f684a02018-08-16 14:43:50 +0200153 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200154 }
155
156 talloc_free(bankd);
157 exit(0);
158}
159
160
161
162/***********************************************************************
163 * bankd worker thread
164 ***********************************************************************/
165
Harald Welte8d858292018-08-15 23:36:46 +0200166struct value_string worker_state_names[] = {
167 { BW_ST_INIT, "INIT" },
168 { BW_ST_ACCEPTING, "ACCEPTING" },
169 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
170 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200171 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200172 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
173 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
174 { 0, NULL }
175};
176
Harald Welteceb3e682018-08-16 14:47:11 +0200177#define LOGW(w, fmt, args...) \
178 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
179 __FILE__, __LINE__, ## args)
180
Harald Welteaf614732018-08-17 22:10:05 +0200181#define PCSC_ERROR(w, rv, text) \
182if (rv != SCARD_S_SUCCESS) { \
183 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
184 goto end; \
185} else { \
Harald Welte7b41d9c2018-10-03 23:15:10 +0200186 LOGW((w), ": OK\n"); \
Harald Welteaf614732018-08-17 22:10:05 +0200187}
188
Harald Welte8d858292018-08-15 23:36:46 +0200189static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
190{
191 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
192 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200193 worker->timeout = 0;
194}
195
196static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
197 unsigned int timeout_secs)
198{
199 LOGW(worker, "Changing state to %s (timeout=%u)\n",
200 get_value_string(worker_state_names, new_state), timeout_secs);
201 worker->state = new_state;
202 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200203}
Harald Welte77911b02018-08-14 23:47:30 +0200204
205static void worker_cleanup(void *arg)
206{
207 struct bankd_worker *worker = (struct bankd_worker *) arg;
208 struct bankd *bankd = worker->bankd;
209
210 /* FIXME: should we still do this? in the thread ?!? */
211 pthread_mutex_lock(&bankd->workers_mutex);
212 llist_del(&worker->list);
213 talloc_free(worker); /* FIXME: is this safe? */
214 pthread_mutex_unlock(&bankd->workers_mutex);
215}
216
217
Harald Welteaf614732018-08-17 22:10:05 +0200218static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200219{
Harald Welteaf614732018-08-17 22:10:05 +0200220 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200221
Harald Welte150d6d62018-10-03 23:07:47 +0200222 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
223
Harald Welte694df832018-10-03 22:47:52 +0200224 if (!worker->reader.name) {
225 /* resolve PC/SC reader name from slot_id -> name map */
226 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
227 if (!worker->reader.name) {
228 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
229 worker->slot.bank_id, worker->slot.slot_nr);
230 rc = -1;
231 goto end;
232 }
233 }
Harald Welte45c948c2018-09-23 19:26:52 +0200234 OSMO_ASSERT(worker->reader.name);
235
Harald Welte694df832018-10-03 22:47:52 +0200236 if (!worker->reader.pcsc.hContext) {
237 LOGW(worker, "Attempting to open PC/SC context\n");
238 /* The PC/SC context must be created inside the thread where we'll later use it */
239 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
240 PCSC_ERROR(worker, rc, "SCardEstablishContext")
241 }
Harald Welte45c948c2018-09-23 19:26:52 +0200242
Harald Welte694df832018-10-03 22:47:52 +0200243 if (!worker->reader.pcsc.hCard) {
244 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
245 DWORD dwActiveProtocol;
246 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
247 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
248 PCSC_ERROR(worker, rc, "SCardConnect")
249 }
Harald Welte77911b02018-08-14 23:47:30 +0200250
Harald Welte57593f02018-09-23 19:30:31 +0200251 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200252 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200253
Harald Welteaf614732018-08-17 22:10:05 +0200254 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200255end:
Harald Welteaf614732018-08-17 22:10:05 +0200256 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200257}
Harald Welte77911b02018-08-14 23:47:30 +0200258
259
260static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
261{
262 struct ipaccess_head *hh;
263 uint16_t len;
264 int needed, rc;
265
266 if (buf_size < sizeof(*hh))
267 return -1;
268
269 hh = (struct ipaccess_head *) buf;
270
271 /* 1) blocking read from the socket (IPA header) */
272 rc = read(fd, buf, sizeof(*hh));
273 if (rc < sizeof(*hh))
274 return -2;
275
276 len = ntohs(hh->len);
277 needed = len; //- sizeof(*hh);
278
279 /* 2) blocking read from the socket (payload) */
280 rc = read(fd, buf+sizeof(*hh), needed);
281 if (rc < needed)
282 return -3;
283
284 return len;
285}
286
Harald Welte796a7492018-09-23 19:31:28 +0200287static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
288{
289 struct msgb *msg = rspro_enc_msg(pdu);
290 int rc;
291
292 if (!msg) {
293 LOGW(worker, "error encoding RSPRO\n");
294 return -1;
295 }
296
Harald Weltefd471192018-09-24 14:51:14 +0200297 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200298 /* prepend the header */
299 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200300 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200301
302 /* actually send it through the socket */
303 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
304 if (rc == msgb_length(msg))
305 rc = 0;
306 else {
307 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
308 rc = -1;
309 }
310
311 msgb_free(msg);
312
313 return rc;
314}
315
Harald Welte150d6d62018-10-03 23:07:47 +0200316/* attempt to obtain slot-map */
317static int worker_try_slotmap(struct bankd_worker *worker)
318{
319 struct bankd_slot_mapping *slmap;
320
321 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
322 if (!slmap) {
323 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
324 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
325 /* check in 10s if the map has been installed meanwhile by main thread */
326 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
327 return -1;
328 } else {
329 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
330 slmap->client.client_id, slmap->client.slot_nr,
331 slmap->bank.bank_id, slmap->bank.slot_nr);
332 worker->slot = slmap->bank;
333 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
334 return worker_open_card(worker);
335 }
336}
337
338
Harald Weltecce2aad2018-08-16 14:44:37 +0200339static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
340{
Harald Welteaf614732018-08-17 22:10:05 +0200341 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte3e689872018-09-24 14:52:56 +0200342 e_ResultCode res;
343 RsproPDU_t *resp;
Harald Welteaf614732018-08-17 22:10:05 +0200344
Harald Weltecce2aad2018-08-16 14:44:37 +0200345 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
346
Harald Weltecce2aad2018-08-16 14:44:37 +0200347 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
348 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
349 /* FIXME: store somewhere? */
350
351 if (worker->state != BW_ST_CONN_WAIT_ID) {
352 LOGW(worker, "Unexpected connectClientReq\n");
353 return -102;
354 }
355
Harald Welte371d0262018-08-16 15:23:58 +0200356 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200357 LOGW(worker, "missing clientID, aborting\n");
358 return -103;
359 }
Harald Welte371d0262018-08-16 15:23:58 +0200360 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
361 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200362 worker_set_state(worker, BW_ST_CONN_CLIENT);
363
Harald Welte150d6d62018-10-03 23:07:47 +0200364 if (worker_try_slotmap(worker) >= 0)
365 res = ResultCode_ok;
366 else
Harald Welte3e689872018-09-24 14:52:56 +0200367 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200368
Harald Welte3e689872018-09-24 14:52:56 +0200369 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
370 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200371}
372
Harald Welte796a7492018-09-23 19:31:28 +0200373static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
374{
375 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
376 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
377 SCARD_IO_REQUEST pioRecvPci;
378 uint8_t rx_buf[1024];
379 DWORD rx_buf_len = sizeof(rx_buf);
380 RsproPDU_t *pdu_resp;
381 long rc;
382
383 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
384
385 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
386 LOGW(worker, "Unexpected tpduModemToCaard\n");
387 return -104;
388 }
389
390 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
391
392 rc = SCardTransmit(worker->reader.pcsc.hCard,
393 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
394 &pioRecvPci, rx_buf, &rx_buf_len);
395 PCSC_ERROR(worker, rc, "SCardTransmit");
396
397 /* encode response PDU and send it */
398 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
399 rx_buf, rx_buf_len);
400 worker_send_rspro(worker, pdu_resp);
401
402 return 0;
403end:
404 return rc;
405}
406
Harald Welte77911b02018-08-14 23:47:30 +0200407/* handle one incoming RSPRO message from a client inside a worker thread */
408static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
409{
Harald Weltecce2aad2018-08-16 14:44:37 +0200410 int rc = -100;
411
Harald Welte77911b02018-08-14 23:47:30 +0200412 switch (pdu->msg.present) {
413 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200414 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200415 break;
416 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200417 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200418 break;
419 case RsproPDUchoice_PR_clientSlotStatusInd:
420 /* FIXME */
421 break;
422 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200423 rc = -101;
424 break;
Harald Welte77911b02018-08-14 23:47:30 +0200425 }
426
Harald Weltecce2aad2018-08-16 14:44:37 +0200427 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200428}
429
Harald Welte694df832018-10-03 22:47:52 +0200430static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
431{
432 struct timeval tout = { timeout_secs, 0 };
433 fd_set readset;
434
435 FD_ZERO(&readset);
436 FD_SET(fd, &readset);
437 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
438}
439
Harald Welte77911b02018-08-14 23:47:30 +0200440/* body of the main transceive loop */
441static int worker_transceive_loop(struct bankd_worker *worker)
442{
443 struct ipaccess_head *hh;
444 struct ipaccess_head_ext *hh_ext;
445 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
446 asn_dec_rval_t rval;
447 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200448 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200449
Harald Welte694df832018-10-03 22:47:52 +0200450 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
451 if (rc == 0) {
452 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200453 switch (worker->state) {
454 case BW_ST_CONN_CLIENT_WAIT_MAP:
455 /* re-check if mapping exists meanwhile? */
456 worker_try_slotmap(worker);
457 break;
458 case BW_ST_CONN_CLIENT_MAPPED:
459 /* re-check if reader/card can be opened meanwhile? */
460 worker_open_card(worker);
461 break;
462 default:
463 OSMO_ASSERT(0);
464 }
465 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200466 return 0;
467 };
468
Harald Welte77911b02018-08-14 23:47:30 +0200469 /* 1) blocking read of entire IPA message from the socket */
470 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
471 if (rc < 0)
472 return rc;
473 data_len = rc;
474
475 hh = (struct ipaccess_head *) buf;
Harald Weltee1176cf2018-09-24 14:54:58 +0200476 if (hh->proto != IPAC_PROTO_OSMO) {
477 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200478 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200479 }
Harald Welte77911b02018-08-14 23:47:30 +0200480
481 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200482 if (data_len < sizeof(*hh_ext)) {
483 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200484 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200485 }
Harald Welte77911b02018-08-14 23:47:30 +0200486 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200487 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
488 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200489 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200490 }
Harald Welte77911b02018-08-14 23:47:30 +0200491
492 /* 2) ASN1 BER decode of the message */
493 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200494 if (rval.code != RC_OK) {
495 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200496 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200497 }
Harald Welte77911b02018-08-14 23:47:30 +0200498
499 /* 3) handling of the message, possibly resulting in PCSC commands */
500 rc = worker_handle_rspro(worker, pdu);
501 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200502 if (rc < 0) {
503 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200504 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200505 }
Harald Welte77911b02018-08-14 23:47:30 +0200506
507 /* everything OK if we reach here */
508 return 0;
509}
510
Harald Welted6dfb8c2018-08-16 14:46:53 +0200511/* obtain an ascii representation of the client IP/port */
512static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
513{
514 char hostbuf[32], portbuf[32];
515 int rc;
516
517 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
518 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
519 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
520 if (rc != 0) {
521 out[0] = '\0';
522 return -1;
523 }
524 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
525 return 0;
526}
527
Harald Welte77911b02018-08-14 23:47:30 +0200528/* worker thread main function */
529static void *worker_main(void *arg)
530{
531 struct bankd_worker *worker = (struct bankd_worker *) arg;
532 void *top_ctx;
533 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200534 char worker_name[32];
535
536 /* set the thread name */
537 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
538 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200539
Harald Welte8d858292018-08-15 23:36:46 +0200540 worker_set_state(worker, BW_ST_INIT);
541
Harald Welte77911b02018-08-14 23:47:30 +0200542 /* not permitted in multithreaded environment */
543 talloc_disable_null_tracking();
544 top_ctx = talloc_named_const(NULL, 0, "top");
545 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
546
547 /* push cleanup helper */
548 pthread_cleanup_push(&worker_cleanup, worker);
549
550 /* we continuously perform the same loop here, recycling the worker thread
551 * once the client connection is gone or we have some trouble with the card/reader */
552 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200553 char buf[128];
554
Harald Welte77911b02018-08-14 23:47:30 +0200555 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
556
Harald Welte8d858292018-08-15 23:36:46 +0200557 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200558 /* first wait for an incoming TCP connection */
559 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
560 &worker->client.peer_addr_len);
561 if (rc < 0) {
562 continue;
563 }
564 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200565 worker_client_addrstr(buf, sizeof(buf), worker);
566 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200567 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200568
569 /* run the main worker transceive loop body until there was some error */
570 while (1) {
571 rc = worker_transceive_loop(worker);
572 if (rc < 0)
573 break;
574 }
575
Harald Welted6dfb8c2018-08-16 14:46:53 +0200576 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
577
Harald Welte77911b02018-08-14 23:47:30 +0200578 /* clean-up: reset to sane state */
579 if (worker->reader.pcsc.hCard) {
580 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
581 worker->reader.pcsc.hCard = 0;
582 }
583 if (worker->reader.pcsc.hContext) {
584 SCardReleaseContext(worker->reader.pcsc.hContext);
585 worker->reader.pcsc.hContext = 0;
586 }
Harald Welte694df832018-10-03 22:47:52 +0200587 if (worker->reader.name)
588 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200589 if (worker->client.fd >= 0)
590 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200591 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200592 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200593 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200594 }
595
596 pthread_cleanup_pop(1);
597 talloc_free(top_ctx);
598 pthread_exit(NULL);
599}