blob: ed225a9693b15008d825f2c7ca48da96ec237f6a [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' */
Harald Weltecbd18962019-03-03 19:02:38 +010050 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020051 INIT_LLIST_HEAD(&bankd->workers);
52 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020053
Harald Weltef1dd1622018-09-24 14:54:23 +020054 bankd->comp_id.type = ComponentType_remsimBankd;
55 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
56 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
57 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
58 /* FIXME: other members of app_comp_id */
59
Harald Welte45c948c2018-09-23 19:26:52 +020060 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
61 * read once during bankd initialization, when the worker threads haven't
62 * started yet */
63 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
64 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020065
66 /* HACK HACK HACK */
67 {
68 struct bank_slot bs = { .bank_id = 1, };
69 struct client_slot cs = { .client_id = 23, };
70 int i;
71 for (i = 0; i < 5; i++) {
72 bs.slot_nr = cs.slot_nr = i;
Harald Weltecbd18962019-03-03 19:02:38 +010073 slotmap_add(bankd->slotmaps, &bs, &cs);
Harald Weltee72e5732018-09-23 19:31:55 +020074 }
75 }
Harald Welte77911b02018-08-14 23:47:30 +020076}
77
78/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020079static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020080{
81 struct bankd_worker *worker;
82 int rc;
83
84 worker = talloc_zero(bankd, struct bankd_worker);
85 if (!worker)
86 return NULL;
87
88 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020089 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020090
91 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
92
93 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
94 if (rc != 0) {
95 talloc_free(worker);
96 return NULL;
97 }
98
99 pthread_mutex_lock(&bankd->workers_mutex);
100 llist_add_tail(&worker->list, &bankd->workers);
101 pthread_mutex_unlock(&bankd->workers_mutex);
102
103 return worker;
104}
105
106static bool terminate = false;
107
108int main(int argc, char **argv)
109{
110 struct bankd *bankd = talloc_zero(NULL, struct bankd);
111 int i, rc;
112
113 OSMO_ASSERT(bankd);
114 bankd_init(bankd);
115
Harald Welte12534e72018-08-15 23:37:29 +0200116 /* create listening socket */
117 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
118 if (rc < 0)
119 exit(1);
120 bankd->accept_fd = rc;
121
122 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200123 for (i = 0; i < 10; i++) {
124 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +0200125 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200126 if (!w)
127 exit(21);
128 }
129
130 while (1) {
131 if (terminate)
132 break;
Harald Welte8f893ff2018-10-03 23:21:10 +0200133 /* FIXME: Connect to remsim-server from the main thread, register with
134 * it and await + process any slot mapping or other configuration commands.
135 * Ensure to re-connect as needed. */
136
137 /* we should generalize the SRVC (server connection) FSM from remsim-client
138 * and use it here. As long as only the main thread is using osmo_fsm, things
139 * are safe with regard to other threads */
Harald Welte7f684a02018-08-16 14:43:50 +0200140 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200141 }
142
143 talloc_free(bankd);
144 exit(0);
145}
146
147
148
149/***********************************************************************
150 * bankd worker thread
151 ***********************************************************************/
152
Harald Welte8d858292018-08-15 23:36:46 +0200153struct value_string worker_state_names[] = {
154 { BW_ST_INIT, "INIT" },
155 { BW_ST_ACCEPTING, "ACCEPTING" },
156 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
157 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200158 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200159 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
160 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
161 { 0, NULL }
162};
163
Harald Welteceb3e682018-08-16 14:47:11 +0200164#define LOGW(w, fmt, args...) \
165 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
166 __FILE__, __LINE__, ## args)
167
Harald Welteaf614732018-08-17 22:10:05 +0200168#define PCSC_ERROR(w, rv, text) \
169if (rv != SCARD_S_SUCCESS) { \
170 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
171 goto end; \
172} else { \
Harald Welte7b41d9c2018-10-03 23:15:10 +0200173 LOGW((w), ": OK\n"); \
Harald Welteaf614732018-08-17 22:10:05 +0200174}
175
Harald Welte8d858292018-08-15 23:36:46 +0200176static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
177{
178 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
179 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200180 worker->timeout = 0;
181}
182
183static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
184 unsigned int timeout_secs)
185{
186 LOGW(worker, "Changing state to %s (timeout=%u)\n",
187 get_value_string(worker_state_names, new_state), timeout_secs);
188 worker->state = new_state;
189 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200190}
Harald Welte77911b02018-08-14 23:47:30 +0200191
192static void worker_cleanup(void *arg)
193{
194 struct bankd_worker *worker = (struct bankd_worker *) arg;
195 struct bankd *bankd = worker->bankd;
196
197 /* FIXME: should we still do this? in the thread ?!? */
198 pthread_mutex_lock(&bankd->workers_mutex);
199 llist_del(&worker->list);
200 talloc_free(worker); /* FIXME: is this safe? */
201 pthread_mutex_unlock(&bankd->workers_mutex);
202}
203
204
Harald Welteaf614732018-08-17 22:10:05 +0200205static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200206{
Harald Welteaf614732018-08-17 22:10:05 +0200207 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200208
Harald Welte150d6d62018-10-03 23:07:47 +0200209 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
210
Harald Welte694df832018-10-03 22:47:52 +0200211 if (!worker->reader.name) {
212 /* resolve PC/SC reader name from slot_id -> name map */
213 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
214 if (!worker->reader.name) {
215 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
216 worker->slot.bank_id, worker->slot.slot_nr);
217 rc = -1;
218 goto end;
219 }
220 }
Harald Welte45c948c2018-09-23 19:26:52 +0200221 OSMO_ASSERT(worker->reader.name);
222
Harald Welte694df832018-10-03 22:47:52 +0200223 if (!worker->reader.pcsc.hContext) {
224 LOGW(worker, "Attempting to open PC/SC context\n");
225 /* The PC/SC context must be created inside the thread where we'll later use it */
226 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
227 PCSC_ERROR(worker, rc, "SCardEstablishContext")
228 }
Harald Welte45c948c2018-09-23 19:26:52 +0200229
Harald Welte694df832018-10-03 22:47:52 +0200230 if (!worker->reader.pcsc.hCard) {
231 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
232 DWORD dwActiveProtocol;
233 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
234 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
235 PCSC_ERROR(worker, rc, "SCardConnect")
236 }
Harald Welte77911b02018-08-14 23:47:30 +0200237
Harald Welte57593f02018-09-23 19:30:31 +0200238 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200239 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200240
Harald Welteaf614732018-08-17 22:10:05 +0200241 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200242end:
Harald Welteaf614732018-08-17 22:10:05 +0200243 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200244}
Harald Welte77911b02018-08-14 23:47:30 +0200245
246
247static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
248{
249 struct ipaccess_head *hh;
250 uint16_t len;
251 int needed, rc;
252
253 if (buf_size < sizeof(*hh))
254 return -1;
255
256 hh = (struct ipaccess_head *) buf;
257
258 /* 1) blocking read from the socket (IPA header) */
259 rc = read(fd, buf, sizeof(*hh));
260 if (rc < sizeof(*hh))
261 return -2;
262
263 len = ntohs(hh->len);
264 needed = len; //- sizeof(*hh);
265
266 /* 2) blocking read from the socket (payload) */
267 rc = read(fd, buf+sizeof(*hh), needed);
268 if (rc < needed)
269 return -3;
270
271 return len;
272}
273
Harald Welte796a7492018-09-23 19:31:28 +0200274static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
275{
276 struct msgb *msg = rspro_enc_msg(pdu);
277 int rc;
278
279 if (!msg) {
280 LOGW(worker, "error encoding RSPRO\n");
281 return -1;
282 }
283
Harald Weltefd471192018-09-24 14:51:14 +0200284 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200285 /* prepend the header */
286 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200287 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200288
289 /* actually send it through the socket */
290 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
291 if (rc == msgb_length(msg))
292 rc = 0;
293 else {
294 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
295 rc = -1;
296 }
297
298 msgb_free(msg);
299
300 return rc;
301}
302
Harald Welte150d6d62018-10-03 23:07:47 +0200303/* attempt to obtain slot-map */
304static int worker_try_slotmap(struct bankd_worker *worker)
305{
Harald Weltecbd18962019-03-03 19:02:38 +0100306 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200307
Harald Weltecbd18962019-03-03 19:02:38 +0100308 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200309 if (!slmap) {
310 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
311 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
312 /* check in 10s if the map has been installed meanwhile by main thread */
313 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
314 return -1;
315 } else {
316 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
317 slmap->client.client_id, slmap->client.slot_nr,
318 slmap->bank.bank_id, slmap->bank.slot_nr);
319 worker->slot = slmap->bank;
320 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
321 return worker_open_card(worker);
322 }
323}
324
325
Harald Weltecce2aad2018-08-16 14:44:37 +0200326static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
327{
Harald Welteaf614732018-08-17 22:10:05 +0200328 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte3e689872018-09-24 14:52:56 +0200329 e_ResultCode res;
330 RsproPDU_t *resp;
Harald Welteaf614732018-08-17 22:10:05 +0200331
Harald Weltecce2aad2018-08-16 14:44:37 +0200332 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
333
Harald Weltecce2aad2018-08-16 14:44:37 +0200334 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
335 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
336 /* FIXME: store somewhere? */
337
338 if (worker->state != BW_ST_CONN_WAIT_ID) {
339 LOGW(worker, "Unexpected connectClientReq\n");
340 return -102;
341 }
342
Harald Welte371d0262018-08-16 15:23:58 +0200343 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200344 LOGW(worker, "missing clientID, aborting\n");
345 return -103;
346 }
Harald Welte371d0262018-08-16 15:23:58 +0200347 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
348 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200349 worker_set_state(worker, BW_ST_CONN_CLIENT);
350
Harald Welte150d6d62018-10-03 23:07:47 +0200351 if (worker_try_slotmap(worker) >= 0)
352 res = ResultCode_ok;
353 else
Harald Welte3e689872018-09-24 14:52:56 +0200354 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200355
Harald Welte3e689872018-09-24 14:52:56 +0200356 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
357 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200358}
359
Harald Welte796a7492018-09-23 19:31:28 +0200360static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
361{
362 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
363 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
364 SCARD_IO_REQUEST pioRecvPci;
365 uint8_t rx_buf[1024];
366 DWORD rx_buf_len = sizeof(rx_buf);
367 RsproPDU_t *pdu_resp;
368 long rc;
369
370 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
371
372 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
373 LOGW(worker, "Unexpected tpduModemToCaard\n");
374 return -104;
375 }
376
377 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
378
379 rc = SCardTransmit(worker->reader.pcsc.hCard,
380 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
381 &pioRecvPci, rx_buf, &rx_buf_len);
382 PCSC_ERROR(worker, rc, "SCardTransmit");
383
384 /* encode response PDU and send it */
385 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
386 rx_buf, rx_buf_len);
387 worker_send_rspro(worker, pdu_resp);
388
389 return 0;
390end:
391 return rc;
392}
393
Harald Welte77911b02018-08-14 23:47:30 +0200394/* handle one incoming RSPRO message from a client inside a worker thread */
395static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
396{
Harald Weltecce2aad2018-08-16 14:44:37 +0200397 int rc = -100;
398
Harald Welte77911b02018-08-14 23:47:30 +0200399 switch (pdu->msg.present) {
400 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200401 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200402 break;
403 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200404 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200405 break;
406 case RsproPDUchoice_PR_clientSlotStatusInd:
407 /* FIXME */
408 break;
409 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200410 rc = -101;
411 break;
Harald Welte77911b02018-08-14 23:47:30 +0200412 }
413
Harald Weltecce2aad2018-08-16 14:44:37 +0200414 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200415}
416
Harald Welte694df832018-10-03 22:47:52 +0200417static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
418{
419 struct timeval tout = { timeout_secs, 0 };
420 fd_set readset;
421
422 FD_ZERO(&readset);
423 FD_SET(fd, &readset);
424 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
425}
426
Harald Welte77911b02018-08-14 23:47:30 +0200427/* body of the main transceive loop */
428static int worker_transceive_loop(struct bankd_worker *worker)
429{
430 struct ipaccess_head *hh;
431 struct ipaccess_head_ext *hh_ext;
432 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
433 asn_dec_rval_t rval;
434 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200435 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200436
Harald Welte694df832018-10-03 22:47:52 +0200437 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
438 if (rc == 0) {
439 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200440 switch (worker->state) {
441 case BW_ST_CONN_CLIENT_WAIT_MAP:
442 /* re-check if mapping exists meanwhile? */
443 worker_try_slotmap(worker);
444 break;
445 case BW_ST_CONN_CLIENT_MAPPED:
446 /* re-check if reader/card can be opened meanwhile? */
447 worker_open_card(worker);
448 break;
449 default:
450 OSMO_ASSERT(0);
451 }
452 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200453 return 0;
454 };
455
Harald Welte77911b02018-08-14 23:47:30 +0200456 /* 1) blocking read of entire IPA message from the socket */
457 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
458 if (rc < 0)
459 return rc;
460 data_len = rc;
461
462 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200463 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200464 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200465 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200466 }
Harald Welte77911b02018-08-14 23:47:30 +0200467
Harald Welte5a3613a2018-10-11 12:56:21 +0200468 if (hh->proto == IPAC_PROTO_IPACCESS) {
469 LOGW(worker, "IPA CCM not implemented yet\n");
470 return 0;
471 }
472
Harald Welte77911b02018-08-14 23:47:30 +0200473 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200474 if (data_len < sizeof(*hh_ext)) {
475 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200476 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200477 }
Harald Welte77911b02018-08-14 23:47:30 +0200478 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200479 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
480 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200481 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200482 }
Harald Welte77911b02018-08-14 23:47:30 +0200483
484 /* 2) ASN1 BER decode of the message */
485 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200486 if (rval.code != RC_OK) {
487 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200488 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200489 }
Harald Welte77911b02018-08-14 23:47:30 +0200490
491 /* 3) handling of the message, possibly resulting in PCSC commands */
492 rc = worker_handle_rspro(worker, pdu);
493 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200494 if (rc < 0) {
495 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200496 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200497 }
Harald Welte77911b02018-08-14 23:47:30 +0200498
499 /* everything OK if we reach here */
500 return 0;
501}
502
Harald Welted6dfb8c2018-08-16 14:46:53 +0200503/* obtain an ascii representation of the client IP/port */
504static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
505{
506 char hostbuf[32], portbuf[32];
507 int rc;
508
509 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
510 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
511 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
512 if (rc != 0) {
513 out[0] = '\0';
514 return -1;
515 }
516 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
517 return 0;
518}
519
Harald Welte77911b02018-08-14 23:47:30 +0200520/* worker thread main function */
521static void *worker_main(void *arg)
522{
523 struct bankd_worker *worker = (struct bankd_worker *) arg;
524 void *top_ctx;
525 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200526 char worker_name[32];
527
528 /* set the thread name */
529 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
530 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200531
Harald Welte8d858292018-08-15 23:36:46 +0200532 worker_set_state(worker, BW_ST_INIT);
533
Harald Welte77911b02018-08-14 23:47:30 +0200534 /* not permitted in multithreaded environment */
535 talloc_disable_null_tracking();
536 top_ctx = talloc_named_const(NULL, 0, "top");
537 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
538
539 /* push cleanup helper */
540 pthread_cleanup_push(&worker_cleanup, worker);
541
542 /* we continuously perform the same loop here, recycling the worker thread
543 * once the client connection is gone or we have some trouble with the card/reader */
544 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200545 char buf[128];
546
Harald Welte77911b02018-08-14 23:47:30 +0200547 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
548
Harald Welte8d858292018-08-15 23:36:46 +0200549 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200550 /* first wait for an incoming TCP connection */
551 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
552 &worker->client.peer_addr_len);
553 if (rc < 0) {
554 continue;
555 }
556 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200557 worker_client_addrstr(buf, sizeof(buf), worker);
558 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200559 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200560
561 /* run the main worker transceive loop body until there was some error */
562 while (1) {
563 rc = worker_transceive_loop(worker);
564 if (rc < 0)
565 break;
566 }
567
Harald Welted6dfb8c2018-08-16 14:46:53 +0200568 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
569
Harald Welte77911b02018-08-14 23:47:30 +0200570 /* clean-up: reset to sane state */
571 if (worker->reader.pcsc.hCard) {
572 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
573 worker->reader.pcsc.hCard = 0;
574 }
575 if (worker->reader.pcsc.hContext) {
576 SCardReleaseContext(worker->reader.pcsc.hContext);
577 worker->reader.pcsc.hContext = 0;
578 }
Harald Welte694df832018-10-03 22:47:52 +0200579 if (worker->reader.name)
580 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200581 if (worker->client.fd >= 0)
582 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200583 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200584 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200585 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200586 }
587
588 pthread_cleanup_pop(1);
589 talloc_free(top_ctx);
590 pthread_exit(NULL);
591}