blob: fde03cd34d08d27326429e8091bd6b1beb2f15f5 [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
Kévin Redonff5db6e2018-10-11 17:16:18 +020025#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020026#include <osmocom/rspro/RsproPDU.h>
27
28#include "bankd.h"
Harald Welte61d98e92019-03-03 15:43:07 +010029#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020030#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020031
32__thread void *talloc_asn1_ctx;
33
34static void *worker_main(void *arg);
35
36/***********************************************************************
37* bankd core / main thread
38***********************************************************************/
39
Harald Welte43ab79f2018-10-03 23:34:21 +020040int asn_debug;
41
Harald Welte77911b02018-08-14 23:47:30 +020042static void bankd_init(struct bankd *bankd)
43{
Harald Weltef94b9ee2018-09-25 15:04:21 +020044 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
45 osmo_init_logging2(g_tall_ctx, &log_info);
46
Harald Welte43ab79f2018-10-03 23:34:21 +020047 asn_debug = 0;
48
Harald Welte77911b02018-08-14 23:47:30 +020049 /* intialize members of 'bankd' */
50 INIT_LLIST_HEAD(&bankd->slot_mappings);
51 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
52 INIT_LLIST_HEAD(&bankd->workers);
53 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020054
Harald Weltef1dd1622018-09-24 14:54:23 +020055 bankd->comp_id.type = ComponentType_remsimBankd;
56 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
57 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
58 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
59 /* FIXME: other members of app_comp_id */
60
Harald Welte45c948c2018-09-23 19:26:52 +020061 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
62 * read once during bankd initialization, when the worker threads haven't
63 * started yet */
64 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
65 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020066
67 /* HACK HACK HACK */
68 {
69 struct bank_slot bs = { .bank_id = 1, };
70 struct client_slot cs = { .client_id = 23, };
71 int i;
72 for (i = 0; i < 5; i++) {
73 bs.slot_nr = cs.slot_nr = i;
74 bankd_slotmap_add(bankd, &bs, &cs);
75 }
76 }
Harald Welte77911b02018-08-14 23:47:30 +020077}
78
79/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020080static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020081{
82 struct bankd_worker *worker;
83 int rc;
84
85 worker = talloc_zero(bankd, struct bankd_worker);
86 if (!worker)
87 return NULL;
88
89 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020090 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020091
92 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
93
94 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
95 if (rc != 0) {
96 talloc_free(worker);
97 return NULL;
98 }
99
100 pthread_mutex_lock(&bankd->workers_mutex);
101 llist_add_tail(&worker->list, &bankd->workers);
102 pthread_mutex_unlock(&bankd->workers_mutex);
103
104 return worker;
105}
106
107static bool terminate = false;
108
109int main(int argc, char **argv)
110{
111 struct bankd *bankd = talloc_zero(NULL, struct bankd);
112 int i, rc;
113
114 OSMO_ASSERT(bankd);
115 bankd_init(bankd);
116
Harald Welte12534e72018-08-15 23:37:29 +0200117 /* create listening socket */
118 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
119 if (rc < 0)
120 exit(1);
121 bankd->accept_fd = rc;
122
123 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200124 for (i = 0; i < 10; i++) {
125 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200126 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200127 if (!w)
128 exit(21);
129 }
130
131 while (1) {
132 if (terminate)
133 break;
Harald Welte8f893ff2018-10-03 23:21:10 +0200134 /* FIXME: Connect to remsim-server from the main thread, register with
135 * it and await + process any slot mapping or other configuration commands.
136 * Ensure to re-connect as needed. */
137
138 /* we should generalize the SRVC (server connection) FSM from remsim-client
139 * and use it here. As long as only the main thread is using osmo_fsm, things
140 * are safe with regard to other threads */
Harald Welte7f684a02018-08-16 14:43:50 +0200141 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200142 }
143
144 talloc_free(bankd);
145 exit(0);
146}
147
148
149
150/***********************************************************************
151 * bankd worker thread
152 ***********************************************************************/
153
Harald Welte8d858292018-08-15 23:36:46 +0200154struct value_string worker_state_names[] = {
155 { BW_ST_INIT, "INIT" },
156 { BW_ST_ACCEPTING, "ACCEPTING" },
157 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
158 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200159 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200160 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
161 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
162 { 0, NULL }
163};
164
Harald Welteceb3e682018-08-16 14:47:11 +0200165#define LOGW(w, fmt, args...) \
166 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
167 __FILE__, __LINE__, ## args)
168
Harald Welteaf614732018-08-17 22:10:05 +0200169#define PCSC_ERROR(w, rv, text) \
170if (rv != SCARD_S_SUCCESS) { \
171 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
172 goto end; \
173} else { \
Harald Welte7b41d9c2018-10-03 23:15:10 +0200174 LOGW((w), ": OK\n"); \
Harald Welteaf614732018-08-17 22:10:05 +0200175}
176
Harald Welte8d858292018-08-15 23:36:46 +0200177static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
178{
179 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
180 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200181 worker->timeout = 0;
182}
183
184static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
185 unsigned int timeout_secs)
186{
187 LOGW(worker, "Changing state to %s (timeout=%u)\n",
188 get_value_string(worker_state_names, new_state), timeout_secs);
189 worker->state = new_state;
190 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200191}
Harald Welte77911b02018-08-14 23:47:30 +0200192
193static void worker_cleanup(void *arg)
194{
195 struct bankd_worker *worker = (struct bankd_worker *) arg;
196 struct bankd *bankd = worker->bankd;
197
198 /* FIXME: should we still do this? in the thread ?!? */
199 pthread_mutex_lock(&bankd->workers_mutex);
200 llist_del(&worker->list);
201 talloc_free(worker); /* FIXME: is this safe? */
202 pthread_mutex_unlock(&bankd->workers_mutex);
203}
204
205
Harald Welteaf614732018-08-17 22:10:05 +0200206static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200207{
Harald Welteaf614732018-08-17 22:10:05 +0200208 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200209
Harald Welte150d6d62018-10-03 23:07:47 +0200210 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
211
Harald Welte694df832018-10-03 22:47:52 +0200212 if (!worker->reader.name) {
213 /* resolve PC/SC reader name from slot_id -> name map */
214 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
215 if (!worker->reader.name) {
216 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
217 worker->slot.bank_id, worker->slot.slot_nr);
218 rc = -1;
219 goto end;
220 }
221 }
Harald Welte45c948c2018-09-23 19:26:52 +0200222 OSMO_ASSERT(worker->reader.name);
223
Harald Welte694df832018-10-03 22:47:52 +0200224 if (!worker->reader.pcsc.hContext) {
225 LOGW(worker, "Attempting to open PC/SC context\n");
226 /* The PC/SC context must be created inside the thread where we'll later use it */
227 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
228 PCSC_ERROR(worker, rc, "SCardEstablishContext")
229 }
Harald Welte45c948c2018-09-23 19:26:52 +0200230
Harald Welte694df832018-10-03 22:47:52 +0200231 if (!worker->reader.pcsc.hCard) {
232 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
233 DWORD dwActiveProtocol;
234 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
235 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
236 PCSC_ERROR(worker, rc, "SCardConnect")
237 }
Harald Welte77911b02018-08-14 23:47:30 +0200238
Harald Welte57593f02018-09-23 19:30:31 +0200239 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200240 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200241
Harald Welteaf614732018-08-17 22:10:05 +0200242 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200243end:
Harald Welteaf614732018-08-17 22:10:05 +0200244 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200245}
Harald Welte77911b02018-08-14 23:47:30 +0200246
247
248static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
249{
250 struct ipaccess_head *hh;
251 uint16_t len;
252 int needed, rc;
253
254 if (buf_size < sizeof(*hh))
255 return -1;
256
257 hh = (struct ipaccess_head *) buf;
258
259 /* 1) blocking read from the socket (IPA header) */
260 rc = read(fd, buf, sizeof(*hh));
261 if (rc < sizeof(*hh))
262 return -2;
263
264 len = ntohs(hh->len);
265 needed = len; //- sizeof(*hh);
266
267 /* 2) blocking read from the socket (payload) */
268 rc = read(fd, buf+sizeof(*hh), needed);
269 if (rc < needed)
270 return -3;
271
272 return len;
273}
274
Harald Welte796a7492018-09-23 19:31:28 +0200275static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
276{
277 struct msgb *msg = rspro_enc_msg(pdu);
278 int rc;
279
280 if (!msg) {
281 LOGW(worker, "error encoding RSPRO\n");
282 return -1;
283 }
284
Harald Weltefd471192018-09-24 14:51:14 +0200285 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200286 /* prepend the header */
287 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200288 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200289
290 /* actually send it through the socket */
291 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
292 if (rc == msgb_length(msg))
293 rc = 0;
294 else {
295 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
296 rc = -1;
297 }
298
299 msgb_free(msg);
300
301 return rc;
302}
303
Harald Welte150d6d62018-10-03 23:07:47 +0200304/* attempt to obtain slot-map */
305static int worker_try_slotmap(struct bankd_worker *worker)
306{
307 struct bankd_slot_mapping *slmap;
308
309 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
310 if (!slmap) {
311 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
312 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
313 /* check in 10s if the map has been installed meanwhile by main thread */
314 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
315 return -1;
316 } else {
317 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
318 slmap->client.client_id, slmap->client.slot_nr,
319 slmap->bank.bank_id, slmap->bank.slot_nr);
320 worker->slot = slmap->bank;
321 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
322 return worker_open_card(worker);
323 }
324}
325
326
Harald Weltecce2aad2018-08-16 14:44:37 +0200327static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
328{
Harald Welteaf614732018-08-17 22:10:05 +0200329 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte3e689872018-09-24 14:52:56 +0200330 e_ResultCode res;
331 RsproPDU_t *resp;
Harald Welteaf614732018-08-17 22:10:05 +0200332
Harald Weltecce2aad2018-08-16 14:44:37 +0200333 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
334
Harald Weltecce2aad2018-08-16 14:44:37 +0200335 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
336 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
337 /* FIXME: store somewhere? */
338
339 if (worker->state != BW_ST_CONN_WAIT_ID) {
340 LOGW(worker, "Unexpected connectClientReq\n");
341 return -102;
342 }
343
Harald Welte371d0262018-08-16 15:23:58 +0200344 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200345 LOGW(worker, "missing clientID, aborting\n");
346 return -103;
347 }
Harald Welte371d0262018-08-16 15:23:58 +0200348 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
349 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200350 worker_set_state(worker, BW_ST_CONN_CLIENT);
351
Harald Welte150d6d62018-10-03 23:07:47 +0200352 if (worker_try_slotmap(worker) >= 0)
353 res = ResultCode_ok;
354 else
Harald Welte3e689872018-09-24 14:52:56 +0200355 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200356
Harald Welte3e689872018-09-24 14:52:56 +0200357 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
358 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200359}
360
Harald Welte796a7492018-09-23 19:31:28 +0200361static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
362{
363 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
364 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
365 SCARD_IO_REQUEST pioRecvPci;
366 uint8_t rx_buf[1024];
367 DWORD rx_buf_len = sizeof(rx_buf);
368 RsproPDU_t *pdu_resp;
369 long rc;
370
371 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
372
373 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
374 LOGW(worker, "Unexpected tpduModemToCaard\n");
375 return -104;
376 }
377
378 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
379
380 rc = SCardTransmit(worker->reader.pcsc.hCard,
381 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
382 &pioRecvPci, rx_buf, &rx_buf_len);
383 PCSC_ERROR(worker, rc, "SCardTransmit");
384
385 /* encode response PDU and send it */
386 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
387 rx_buf, rx_buf_len);
388 worker_send_rspro(worker, pdu_resp);
389
390 return 0;
391end:
392 return rc;
393}
394
Harald Welte77911b02018-08-14 23:47:30 +0200395/* handle one incoming RSPRO message from a client inside a worker thread */
396static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
397{
Harald Weltecce2aad2018-08-16 14:44:37 +0200398 int rc = -100;
399
Harald Welte77911b02018-08-14 23:47:30 +0200400 switch (pdu->msg.present) {
401 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200402 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200403 break;
404 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200405 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200406 break;
407 case RsproPDUchoice_PR_clientSlotStatusInd:
408 /* FIXME */
409 break;
410 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200411 rc = -101;
412 break;
Harald Welte77911b02018-08-14 23:47:30 +0200413 }
414
Harald Weltecce2aad2018-08-16 14:44:37 +0200415 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200416}
417
Harald Welte694df832018-10-03 22:47:52 +0200418static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
419{
420 struct timeval tout = { timeout_secs, 0 };
421 fd_set readset;
422
423 FD_ZERO(&readset);
424 FD_SET(fd, &readset);
425 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
426}
427
Harald Welte77911b02018-08-14 23:47:30 +0200428/* body of the main transceive loop */
429static int worker_transceive_loop(struct bankd_worker *worker)
430{
431 struct ipaccess_head *hh;
432 struct ipaccess_head_ext *hh_ext;
433 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
434 asn_dec_rval_t rval;
435 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200436 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200437
Harald Welte694df832018-10-03 22:47:52 +0200438 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
439 if (rc == 0) {
440 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200441 switch (worker->state) {
442 case BW_ST_CONN_CLIENT_WAIT_MAP:
443 /* re-check if mapping exists meanwhile? */
444 worker_try_slotmap(worker);
445 break;
446 case BW_ST_CONN_CLIENT_MAPPED:
447 /* re-check if reader/card can be opened meanwhile? */
448 worker_open_card(worker);
449 break;
450 default:
451 OSMO_ASSERT(0);
452 }
453 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200454 return 0;
455 };
456
Harald Welte77911b02018-08-14 23:47:30 +0200457 /* 1) blocking read of entire IPA message from the socket */
458 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
459 if (rc < 0)
460 return rc;
461 data_len = rc;
462
463 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200464 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200465 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200466 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200467 }
Harald Welte77911b02018-08-14 23:47:30 +0200468
Harald Welte5a3613a2018-10-11 12:56:21 +0200469 if (hh->proto == IPAC_PROTO_IPACCESS) {
470 LOGW(worker, "IPA CCM not implemented yet\n");
471 return 0;
472 }
473
Harald Welte77911b02018-08-14 23:47:30 +0200474 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200475 if (data_len < sizeof(*hh_ext)) {
476 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200477 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200478 }
Harald Welte77911b02018-08-14 23:47:30 +0200479 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200480 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
481 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200482 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200483 }
Harald Welte77911b02018-08-14 23:47:30 +0200484
485 /* 2) ASN1 BER decode of the message */
486 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200487 if (rval.code != RC_OK) {
488 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200489 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200490 }
Harald Welte77911b02018-08-14 23:47:30 +0200491
492 /* 3) handling of the message, possibly resulting in PCSC commands */
493 rc = worker_handle_rspro(worker, pdu);
494 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200495 if (rc < 0) {
496 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200497 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200498 }
Harald Welte77911b02018-08-14 23:47:30 +0200499
500 /* everything OK if we reach here */
501 return 0;
502}
503
Harald Welted6dfb8c2018-08-16 14:46:53 +0200504/* obtain an ascii representation of the client IP/port */
505static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
506{
507 char hostbuf[32], portbuf[32];
508 int rc;
509
510 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
511 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
512 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
513 if (rc != 0) {
514 out[0] = '\0';
515 return -1;
516 }
517 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
518 return 0;
519}
520
Harald Welte77911b02018-08-14 23:47:30 +0200521/* worker thread main function */
522static void *worker_main(void *arg)
523{
524 struct bankd_worker *worker = (struct bankd_worker *) arg;
525 void *top_ctx;
526 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200527 char worker_name[32];
528
529 /* set the thread name */
530 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
531 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200532
Harald Welte8d858292018-08-15 23:36:46 +0200533 worker_set_state(worker, BW_ST_INIT);
534
Harald Welte77911b02018-08-14 23:47:30 +0200535 /* not permitted in multithreaded environment */
536 talloc_disable_null_tracking();
537 top_ctx = talloc_named_const(NULL, 0, "top");
538 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
539
540 /* push cleanup helper */
541 pthread_cleanup_push(&worker_cleanup, worker);
542
543 /* we continuously perform the same loop here, recycling the worker thread
544 * once the client connection is gone or we have some trouble with the card/reader */
545 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200546 char buf[128];
547
Harald Welte77911b02018-08-14 23:47:30 +0200548 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
549
Harald Welte8d858292018-08-15 23:36:46 +0200550 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200551 /* first wait for an incoming TCP connection */
552 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
553 &worker->client.peer_addr_len);
554 if (rc < 0) {
555 continue;
556 }
557 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200558 worker_client_addrstr(buf, sizeof(buf), worker);
559 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200560 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200561
562 /* run the main worker transceive loop body until there was some error */
563 while (1) {
564 rc = worker_transceive_loop(worker);
565 if (rc < 0)
566 break;
567 }
568
Harald Welted6dfb8c2018-08-16 14:46:53 +0200569 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
570
Harald Welte77911b02018-08-14 23:47:30 +0200571 /* clean-up: reset to sane state */
572 if (worker->reader.pcsc.hCard) {
573 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
574 worker->reader.pcsc.hCard = 0;
575 }
576 if (worker->reader.pcsc.hContext) {
577 SCardReleaseContext(worker->reader.pcsc.hContext);
578 worker->reader.pcsc.hContext = 0;
579 }
Harald Welte694df832018-10-03 22:47:52 +0200580 if (worker->reader.name)
581 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200582 if (worker->client.fd >= 0)
583 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200584 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200585 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200586 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200587 }
588
589 pthread_cleanup_pop(1);
590 talloc_free(top_ctx);
591 pthread_exit(NULL);
592}