blob: c18a2801c7114390f84cc907a89c01c28cd1593f [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte31c9eca2018-10-03 21:03:34 +020023#define _GNU_SOURCE
24
Harald Welte77911b02018-08-14 23:47:30 +020025#include <stdio.h>
26#include <stdlib.h>
27#include <stdint.h>
Harald Welte00a96732019-03-11 17:18:02 +010028#include <signal.h>
Harald Welte77911b02018-08-14 23:47:30 +020029#include <unistd.h>
Harald Welte707c85a2019-03-09 12:56:35 +010030#include <errno.h>
Harald Welte77911b02018-08-14 23:47:30 +020031
32#include <pthread.h>
33
34#include <wintypes.h>
35#include <winscard.h>
36#include <pcsclite.h>
37
Harald Welted6dfb8c2018-08-16 14:46:53 +020038#include <sys/socket.h>
39#include <netdb.h>
40
Harald Welte12534e72018-08-15 23:37:29 +020041#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020042#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020043#include <osmocom/core/logging.h>
44#include <osmocom/core/application.h>
Harald Welte77911b02018-08-14 23:47:30 +020045
46#include <osmocom/gsm/ipa.h>
47#include <osmocom/gsm/protocol/ipaccess.h>
48
Kévin Redonff5db6e2018-10-11 17:16:18 +020049#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020050#include <osmocom/rspro/RsproPDU.h>
51
52#include "bankd.h"
Harald Welte3cded632019-03-09 12:59:41 +010053#include "rspro_client_fsm.h"
Harald Welte61d98e92019-03-03 15:43:07 +010054#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020055#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020056
Harald Welte00a96732019-03-11 17:18:02 +010057/* signal indicates to worker thread that its map has been deleted */
58#define SIGMAPDEL SIGRTMIN+1
Harald Welte25075972019-03-11 17:33:17 +010059
60static void handle_sig_usr1(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010061static void handle_sig_mapdel(int sig);
62
Harald Welte77911b02018-08-14 23:47:30 +020063__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010064struct bankd *g_bankd;
Harald Welte25075972019-03-11 17:33:17 +010065static void *g_tall_ctx;
Harald Welte77911b02018-08-14 23:47:30 +020066
67static void *worker_main(void *arg);
68
69/***********************************************************************
70* bankd core / main thread
71***********************************************************************/
72
Harald Welte43ab79f2018-10-03 23:34:21 +020073int asn_debug;
74
Harald Welte77911b02018-08-14 23:47:30 +020075static void bankd_init(struct bankd *bankd)
76{
Harald Welte25075972019-03-11 17:33:17 +010077 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Weltef94b9ee2018-09-25 15:04:21 +020078 osmo_init_logging2(g_tall_ctx, &log_info);
79
Harald Welte43ab79f2018-10-03 23:34:21 +020080 asn_debug = 0;
81
Harald Welte77911b02018-08-14 23:47:30 +020082 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010083 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020084 INIT_LLIST_HEAD(&bankd->workers);
85 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020086
Harald Weltea0f39502019-03-09 20:59:34 +010087 /* set some defaults, overridden by commandline/config */
88 bankd->cfg.bank_id = 1;
89 bankd->cfg.num_slots = 8;
90
Harald Weltef1dd1622018-09-24 14:54:23 +020091 bankd->comp_id.type = ComponentType_remsimBankd;
92 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
93 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
94 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
95 /* FIXME: other members of app_comp_id */
96
Harald Welte45c948c2018-09-23 19:26:52 +020097 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
98 * read once during bankd initialization, when the worker threads haven't
99 * started yet */
100 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
101 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Welte77911b02018-08-14 23:47:30 +0200102}
103
104/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200105static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200106{
107 struct bankd_worker *worker;
108 int rc;
109
110 worker = talloc_zero(bankd, struct bankd_worker);
111 if (!worker)
112 return NULL;
113
114 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200115 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +0200116
117 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
118
119 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
120 if (rc != 0) {
121 talloc_free(worker);
122 return NULL;
123 }
124
125 pthread_mutex_lock(&bankd->workers_mutex);
126 llist_add_tail(&worker->list, &bankd->workers);
127 pthread_mutex_unlock(&bankd->workers_mutex);
128
129 return worker;
130}
131
132static bool terminate = false;
133
Harald Welte707c85a2019-03-09 12:56:35 +0100134/* handle incoming messages from server */
135static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
136{
Harald Welte454f5e22019-03-09 21:38:34 +0100137 const CreateMappingReq_t *creq = NULL;
138 const RemoveMappingReq_t *rreq = NULL;
139 struct slot_mapping *map;
140 struct bank_slot bs;
141 struct client_slot cs;
142 RsproPDU_t *resp;
143
144 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100145
146 switch (pdu->msg.present) {
147 case RsproPDUchoice_PR_connectBankRes:
148 /* Store 'identity' of server in srvc->peer_comp_id */
149 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
150 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
151 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100152 case RsproPDUchoice_PR_createMappingReq:
153 creq = &pdu->msg.choice.createMappingReq;
Harald Welte94ba99b2019-03-27 22:42:11 +0100154 if (creq->bank.bankId != g_bankd->cfg.bank_id) {
155 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
156 "(we are %u)\n", creq->bank.bankId, g_bankd->cfg.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100157 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100158 } else if (creq->bank.slotNr >= g_bankd->cfg.num_slots) {
159 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
160 "(we have %u)\n", creq->bank.slotNr, g_bankd->cfg.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100161 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100162 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100163 rspro2bank_slot(&bs, &creq->bank);
164 rspro2client_slot(&cs, &creq->client);
165 /* Add a new mapping */
166 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100167 if (!map) {
168 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100169 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100170 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100171 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
172 }
173 server_conn_send_rspro(srvc, resp);
174 break;
175 case RsproPDUchoice_PR_removeMappingReq:
176 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte94ba99b2019-03-27 22:42:11 +0100177 if (rreq->bank.bankId != g_bankd->cfg.bank_id) {
178 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
179 "(we are %u)\n", creq->bank.bankId, g_bankd->cfg.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100180 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100181 } else if (rreq->bank.slotNr >= g_bankd->cfg.num_slots) {
182 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
183 "(we have %u)\n", creq->bank.slotNr, g_bankd->cfg.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100184 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100185 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100186 rspro2bank_slot(&bs, &rreq->bank);
187 /* Remove a mapping */
188 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100189 if (!map) {
190 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100191 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100192 } else {
193 LOGPFSM(srvc->fi, "removing slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100194 slotmap_del(g_bankd->slotmaps, map);
195 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
Harald Welte00a96732019-03-11 17:18:02 +0100196
197 /* kill/reset the respective worker, if any! */
198 struct bankd_worker *worker;
199 pthread_mutex_lock(&g_bankd->workers_mutex);
200 llist_for_each_entry(worker, &g_bankd->workers, list) {
201 if (bs.bank_id == worker->slot.bank_id &&
202 bs.slot_nr == worker->slot.slot_nr) {
203 pthread_kill(worker->thread, SIGMAPDEL);
204 break;
205 }
206 }
207 pthread_mutex_unlock(&g_bankd->workers_mutex);
Harald Welte454f5e22019-03-09 21:38:34 +0100208 }
209 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100210 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100211 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100212 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100213 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
214 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100215 return -1;
216 }
217
218 return 0;
219}
220
Harald Welte00a96732019-03-11 17:18:02 +0100221
Harald Welte707c85a2019-03-09 12:56:35 +0100222void handle_options(int argc, char **argv)
223{
224}
225
Harald Welte77911b02018-08-14 23:47:30 +0200226int main(int argc, char **argv)
227{
Harald Weltef4b16f12019-03-09 20:58:17 +0100228 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200229 int i, rc;
230
Harald Weltef4b16f12019-03-09 20:58:17 +0100231 g_bankd = talloc_zero(NULL, struct bankd);
232 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200233
Harald Weltef4b16f12019-03-09 20:58:17 +0100234 bankd_init(g_bankd);
235
236 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100237 srvc->server_host = "localhost";
238 srvc->server_port = 9998;
239 srvc->handle_rx = bankd_srvc_handle_rx;
240 srvc->own_comp_id.type = ComponentType_remsimBankd;
241 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
242 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
243 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
244
245 handle_options(argc, argv);
246
Harald Welte25075972019-03-11 17:33:17 +0100247 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100248 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100249 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100250
Harald Welte707c85a2019-03-09 12:56:35 +0100251 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100252 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100253 if (rc < 0) {
254 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
255 exit(1);
256 }
257
258 /* create listening socket for inbound client connections */
Harald Welte12534e72018-08-15 23:37:29 +0200259 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
260 if (rc < 0)
261 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100262 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200263
Harald Weltea0f39502019-03-09 20:59:34 +0100264 /* create worker threads: One per reader/slot! */
265 for (i = 0; i < g_bankd->cfg.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200266 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100267 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200268 if (!w)
269 exit(21);
270 }
271
272 while (1) {
273 if (terminate)
274 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100275 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200276 }
277
Harald Weltef4b16f12019-03-09 20:58:17 +0100278 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200279 exit(0);
280}
281
282
283
284/***********************************************************************
285 * bankd worker thread
286 ***********************************************************************/
287
Harald Welte00a96732019-03-11 17:18:02 +0100288static __thread struct bankd_worker *g_worker;
289
Harald Welte8d858292018-08-15 23:36:46 +0200290struct value_string worker_state_names[] = {
291 { BW_ST_INIT, "INIT" },
292 { BW_ST_ACCEPTING, "ACCEPTING" },
293 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
294 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200295 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200296 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
297 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100298 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200299 { 0, NULL }
300};
301
Harald Welteceb3e682018-08-16 14:47:11 +0200302#define LOGW(w, fmt, args...) \
303 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
304 __FILE__, __LINE__, ## args)
305
Harald Welteaf614732018-08-17 22:10:05 +0200306#define PCSC_ERROR(w, rv, text) \
307if (rv != SCARD_S_SUCCESS) { \
308 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
309 goto end; \
310} else { \
Harald Welte7b41d9c2018-10-03 23:15:10 +0200311 LOGW((w), ": OK\n"); \
Harald Welteaf614732018-08-17 22:10:05 +0200312}
313
Harald Welte1f699b42019-03-28 13:41:08 +0100314static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
315
Harald Welte8d858292018-08-15 23:36:46 +0200316static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
317{
318 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
319 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200320 worker->timeout = 0;
321}
322
323static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
324 unsigned int timeout_secs)
325{
326 LOGW(worker, "Changing state to %s (timeout=%u)\n",
327 get_value_string(worker_state_names, new_state), timeout_secs);
328 worker->state = new_state;
329 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200330}
Harald Welte77911b02018-08-14 23:47:30 +0200331
Harald Welte00a96732019-03-11 17:18:02 +0100332/* signal handler for receiving SIGMAPDEL from main thread */
333static void handle_sig_mapdel(int sig)
334{
335 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
336 OSMO_ASSERT(sig == SIGMAPDEL);
337 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
338}
339
Harald Welte25075972019-03-11 17:33:17 +0100340static void handle_sig_usr1(int sig)
341{
342 OSMO_ASSERT(sig == SIGUSR1);
343
344 if (pthread_equal(g_bankd->main, pthread_self())) {
345 struct bankd_worker *worker;
346 /* main thread */
347 fprintf(stderr, "=== Talloc Report of main thread:\n");
348 talloc_report(g_tall_ctx, stderr);
349
350 /* iterate over worker threads and ask them to dump their talloc state */
351 pthread_mutex_lock(&g_bankd->workers_mutex);
352 llist_for_each_entry(worker, &g_bankd->workers, list) {
353 pthread_kill(worker->thread, SIGUSR1);
354 }
355 pthread_mutex_unlock(&g_bankd->workers_mutex);
356 } else {
357 /* worker thread */
358 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
359 talloc_report(g_worker->tall_ctx, stderr);
360 }
361}
362
Harald Welte77911b02018-08-14 23:47:30 +0200363static void worker_cleanup(void *arg)
364{
365 struct bankd_worker *worker = (struct bankd_worker *) arg;
366 struct bankd *bankd = worker->bankd;
367
368 /* FIXME: should we still do this? in the thread ?!? */
369 pthread_mutex_lock(&bankd->workers_mutex);
370 llist_del(&worker->list);
371 talloc_free(worker); /* FIXME: is this safe? */
372 pthread_mutex_unlock(&bankd->workers_mutex);
373}
374
375
Harald Welteaf614732018-08-17 22:10:05 +0200376static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200377{
Harald Welteaf614732018-08-17 22:10:05 +0200378 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200379
Harald Welte150d6d62018-10-03 23:07:47 +0200380 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
381
Harald Welte694df832018-10-03 22:47:52 +0200382 if (!worker->reader.name) {
383 /* resolve PC/SC reader name from slot_id -> name map */
384 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
385 if (!worker->reader.name) {
386 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
387 worker->slot.bank_id, worker->slot.slot_nr);
388 rc = -1;
389 goto end;
390 }
391 }
Harald Welte45c948c2018-09-23 19:26:52 +0200392 OSMO_ASSERT(worker->reader.name);
393
Harald Welte694df832018-10-03 22:47:52 +0200394 if (!worker->reader.pcsc.hContext) {
395 LOGW(worker, "Attempting to open PC/SC context\n");
396 /* The PC/SC context must be created inside the thread where we'll later use it */
397 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
398 PCSC_ERROR(worker, rc, "SCardEstablishContext")
399 }
Harald Welte45c948c2018-09-23 19:26:52 +0200400
Harald Welte694df832018-10-03 22:47:52 +0200401 if (!worker->reader.pcsc.hCard) {
402 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
403 DWORD dwActiveProtocol;
404 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
405 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
406 PCSC_ERROR(worker, rc, "SCardConnect")
407 }
Harald Welte77911b02018-08-14 23:47:30 +0200408
Harald Welte1f699b42019-03-28 13:41:08 +0100409 /* use DWORD type as this is what the PC/SC API expects */
410 char pbReader[MAX_READERNAME];
411 DWORD dwReaderLen = sizeof(pbReader);
412 DWORD dwAtrLen = worker->card.atr_len = sizeof(worker->card.atr);
413 DWORD dwState, dwProt;
414 rc = SCardStatus(worker->reader.pcsc.hCard, pbReader, &dwReaderLen, &dwState, &dwProt,
415 worker->card.atr, &dwAtrLen);
416 PCSC_ERROR(worker, rc, "SCardStatus")
417 worker->card.atr_len = dwAtrLen;
418 LOGW(worker, "Card ATR: %s\n", osmo_hexdump_nospc(worker->card.atr, worker->card.atr_len));
419
Harald Welte57593f02018-09-23 19:30:31 +0200420 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200421 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200422
Harald Welteaf614732018-08-17 22:10:05 +0200423 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200424end:
Harald Welteaf614732018-08-17 22:10:05 +0200425 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200426}
Harald Welte77911b02018-08-14 23:47:30 +0200427
428
Harald Welte00a96732019-03-11 17:18:02 +0100429static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200430{
431 struct ipaccess_head *hh;
432 uint16_t len;
433 int needed, rc;
434
435 if (buf_size < sizeof(*hh))
436 return -1;
437
438 hh = (struct ipaccess_head *) buf;
439
Harald Welte00a96732019-03-11 17:18:02 +0100440 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
441 * in case of a signal being received */
442
443restart_hdr:
444 /* 1) blocking recv from the socket (IPA header) */
445 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
446 if (rc == -1 && errno == EINTR) {
447 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
448 return -23;
449 goto restart_hdr;
450 } else if (rc < 0)
451 return rc;
452 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200453 return -2;
454
455 len = ntohs(hh->len);
456 needed = len; //- sizeof(*hh);
457
Harald Welte00a96732019-03-11 17:18:02 +0100458restart_body:
459 /* 2) blocking recv from the socket (payload) */
460 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
461 if (rc == -1 && errno == EINTR) {
462 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
463 return -23;
464 goto restart_body;
465 } else if (rc < 0)
466 return rc;
467 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200468 return -3;
469
470 return len;
471}
472
Harald Welte796a7492018-09-23 19:31:28 +0200473static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
474{
475 struct msgb *msg = rspro_enc_msg(pdu);
476 int rc;
477
478 if (!msg) {
479 LOGW(worker, "error encoding RSPRO\n");
480 return -1;
481 }
482
Harald Weltefd471192018-09-24 14:51:14 +0200483 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200484 /* prepend the header */
485 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200486 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200487
488 /* actually send it through the socket */
489 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
490 if (rc == msgb_length(msg))
491 rc = 0;
492 else {
493 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
494 rc = -1;
495 }
496
497 msgb_free(msg);
498
499 return rc;
500}
501
Harald Welte150d6d62018-10-03 23:07:47 +0200502/* attempt to obtain slot-map */
503static int worker_try_slotmap(struct bankd_worker *worker)
504{
Harald Weltecbd18962019-03-03 19:02:38 +0100505 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200506
Harald Weltecbd18962019-03-03 19:02:38 +0100507 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200508 if (!slmap) {
509 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
510 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
511 /* check in 10s if the map has been installed meanwhile by main thread */
512 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
513 return -1;
514 } else {
515 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
516 slmap->client.client_id, slmap->client.slot_nr,
517 slmap->bank.bank_id, slmap->bank.slot_nr);
518 worker->slot = slmap->bank;
519 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
520 return worker_open_card(worker);
521 }
522}
523
Harald Welte1f699b42019-03-28 13:41:08 +0100524/* inform the remote end (client) about the (new) ATR */
525static int worker_send_atr(struct bankd_worker *worker)
526{
527 RsproPDU_t *set_atr;
528 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
529 worker->client.clslot.slot_nr,
530 worker->card.atr, worker->card.atr_len);
531 if (!set_atr)
532 return -1;
533 return worker_send_rspro(worker, set_atr);
534}
Harald Welte150d6d62018-10-03 23:07:47 +0200535
Harald Weltecce2aad2018-08-16 14:44:37 +0200536static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
537{
Harald Welteaf614732018-08-17 22:10:05 +0200538 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100539 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200540 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100541 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200542
Harald Weltecce2aad2018-08-16 14:44:37 +0200543 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
544
Harald Weltecce2aad2018-08-16 14:44:37 +0200545 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
546 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
547 /* FIXME: store somewhere? */
548
549 if (worker->state != BW_ST_CONN_WAIT_ID) {
550 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100551 rc = -102;
552 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200553 }
554
Harald Welte371d0262018-08-16 15:23:58 +0200555 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200556 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100557 res = ResultCode_illegalClientId;
558 rc = -103;
559 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200560 }
Harald Welte371d0262018-08-16 15:23:58 +0200561 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
562 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200563 worker_set_state(worker, BW_ST_CONN_CLIENT);
564
Harald Welte150d6d62018-10-03 23:07:47 +0200565 if (worker_try_slotmap(worker) >= 0)
566 res = ResultCode_ok;
567 else
Harald Welte3e689872018-09-24 14:52:56 +0200568 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200569
Harald Welte3e689872018-09-24 14:52:56 +0200570 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100571 rc = worker_send_rspro(worker, resp);
572 if (rc < 0)
573 return rc;
574
575 if (res == ResultCode_ok)
576 rc = worker_send_atr(worker);
577
578 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100579
580respond_and_err:
581 if (res) {
582 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
583 worker_send_rspro(worker, resp);
584 }
585 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200586}
587
Harald Welte796a7492018-09-23 19:31:28 +0200588static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
589{
590 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
591 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
592 SCARD_IO_REQUEST pioRecvPci;
593 uint8_t rx_buf[1024];
594 DWORD rx_buf_len = sizeof(rx_buf);
595 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100596 struct client_slot clslot;
597 struct bank_slot bslot;
Harald Welte796a7492018-09-23 19:31:28 +0200598 long rc;
599
600 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
601
602 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
603 LOGW(worker, "Unexpected tpduModemToCaard\n");
604 return -104;
605 }
606
Harald Weltee1d32892019-03-27 20:47:42 +0100607 /* Validate that toBankSlot / fromClientSlot match our expectations */
608 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
609 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
610 if (!bank_slot_equals(&worker->slot, &bslot)) {
611 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
612 bslot.bank_id, bslot.slot_nr);
613 return -105;
614 }
615 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
616 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
617 clslot.client_id, clslot.slot_nr);
618 return -106;
619 }
Harald Welte796a7492018-09-23 19:31:28 +0200620
621 rc = SCardTransmit(worker->reader.pcsc.hCard,
622 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
623 &pioRecvPci, rx_buf, &rx_buf_len);
624 PCSC_ERROR(worker, rc, "SCardTransmit");
625
626 /* encode response PDU and send it */
627 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
628 rx_buf, rx_buf_len);
629 worker_send_rspro(worker, pdu_resp);
630
631 return 0;
632end:
633 return rc;
634}
635
Harald Welte77911b02018-08-14 23:47:30 +0200636/* handle one incoming RSPRO message from a client inside a worker thread */
637static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
638{
Harald Weltecce2aad2018-08-16 14:44:37 +0200639 int rc = -100;
640
Harald Welte77911b02018-08-14 23:47:30 +0200641 switch (pdu->msg.present) {
642 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200643 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200644 break;
645 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200646 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200647 break;
648 case RsproPDUchoice_PR_clientSlotStatusInd:
649 /* FIXME */
650 break;
651 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200652 rc = -101;
653 break;
Harald Welte77911b02018-08-14 23:47:30 +0200654 }
655
Harald Weltecce2aad2018-08-16 14:44:37 +0200656 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200657}
658
Harald Welte694df832018-10-03 22:47:52 +0200659static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
660{
661 struct timeval tout = { timeout_secs, 0 };
662 fd_set readset;
663
664 FD_ZERO(&readset);
665 FD_SET(fd, &readset);
666 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
667}
668
Harald Welte77911b02018-08-14 23:47:30 +0200669/* body of the main transceive loop */
670static int worker_transceive_loop(struct bankd_worker *worker)
671{
672 struct ipaccess_head *hh;
673 struct ipaccess_head_ext *hh_ext;
674 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
675 asn_dec_rval_t rval;
676 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200677 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200678
Harald Welte00a96732019-03-11 17:18:02 +0100679restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200680 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100681 if (rc == -1 && errno == EINTR) {
682 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
683 return -23;
684 goto restart_wait;
685 } else if (rc < 0)
686 return rc;
687 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200688 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200689 switch (worker->state) {
690 case BW_ST_CONN_CLIENT_WAIT_MAP:
691 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100692 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200693 break;
694 case BW_ST_CONN_CLIENT_MAPPED:
695 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100696 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200697 break;
698 default:
699 OSMO_ASSERT(0);
700 }
Harald Welte1f699b42019-03-28 13:41:08 +0100701 if (rc == 0)
702 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200703 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200704 return 0;
705 };
706
Harald Welte77911b02018-08-14 23:47:30 +0200707 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100708 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200709 if (rc < 0)
710 return rc;
711 data_len = rc;
712
713 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200714 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200715 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200716 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200717 }
Harald Welte77911b02018-08-14 23:47:30 +0200718
Harald Welte5a3613a2018-10-11 12:56:21 +0200719 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100720 switch (hh->data[0]) {
721 case IPAC_MSGT_PING:
722 return ipa_ccm_send_pong(worker->client.fd);
723 default:
724 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
725 break;
726 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200727 return 0;
728 }
729
Harald Welte77911b02018-08-14 23:47:30 +0200730 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200731 if (data_len < sizeof(*hh_ext)) {
732 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200733 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200734 }
Harald Welte77911b02018-08-14 23:47:30 +0200735 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200736 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
737 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200738 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200739 }
Harald Welte77911b02018-08-14 23:47:30 +0200740
741 /* 2) ASN1 BER decode of the message */
742 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200743 if (rval.code != RC_OK) {
744 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200745 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200746 }
Harald Welte77911b02018-08-14 23:47:30 +0200747
748 /* 3) handling of the message, possibly resulting in PCSC commands */
749 rc = worker_handle_rspro(worker, pdu);
750 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200751 if (rc < 0) {
752 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200753 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200754 }
Harald Welte77911b02018-08-14 23:47:30 +0200755
756 /* everything OK if we reach here */
757 return 0;
758}
759
Harald Welted6dfb8c2018-08-16 14:46:53 +0200760/* obtain an ascii representation of the client IP/port */
761static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
762{
763 char hostbuf[32], portbuf[32];
764 int rc;
765
766 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
767 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
768 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
769 if (rc != 0) {
770 out[0] = '\0';
771 return -1;
772 }
773 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
774 return 0;
775}
776
Harald Welte77911b02018-08-14 23:47:30 +0200777/* worker thread main function */
778static void *worker_main(void *arg)
779{
Harald Welte77911b02018-08-14 23:47:30 +0200780 void *top_ctx;
781 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200782
Harald Welte00a96732019-03-11 17:18:02 +0100783 g_worker = (struct bankd_worker *) arg;
784
Harald Welte00a96732019-03-11 17:18:02 +0100785 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200786
Harald Welte77911b02018-08-14 23:47:30 +0200787 /* not permitted in multithreaded environment */
788 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100789 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
790 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200791
Harald Welte286a2be2019-03-11 17:36:58 +0100792 /* set the thread name */
793 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
794 pthread_setname_np(pthread_self(), g_worker->name);
795
Harald Welte77911b02018-08-14 23:47:30 +0200796 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100797 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200798
799 /* we continuously perform the same loop here, recycling the worker thread
800 * once the client connection is gone or we have some trouble with the card/reader */
801 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200802 char buf[128];
803
Harald Welte00a96732019-03-11 17:18:02 +0100804 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200805
Harald Welte00a96732019-03-11 17:18:02 +0100806 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200807 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100808 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
809 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200810 if (rc < 0) {
811 continue;
812 }
Harald Welte00a96732019-03-11 17:18:02 +0100813 g_worker->client.fd = rc;
814 worker_client_addrstr(buf, sizeof(buf), g_worker);
815 LOGW(g_worker, "Accepted connection from %s\n", buf);
816 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200817
818 /* run the main worker transceive loop body until there was some error */
819 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100820 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200821 if (rc < 0)
822 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100823 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
824 break;
Harald Welte77911b02018-08-14 23:47:30 +0200825 }
826
Harald Welte00a96732019-03-11 17:18:02 +0100827 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200828
Harald Welte77911b02018-08-14 23:47:30 +0200829 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100830 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte00a96732019-03-11 17:18:02 +0100831 if (g_worker->reader.pcsc.hCard) {
832 SCardDisconnect(g_worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
833 g_worker->reader.pcsc.hCard = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200834 }
Harald Welte00a96732019-03-11 17:18:02 +0100835 if (g_worker->reader.pcsc.hContext) {
836 SCardReleaseContext(g_worker->reader.pcsc.hContext);
837 g_worker->reader.pcsc.hContext = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200838 }
Harald Welte00a96732019-03-11 17:18:02 +0100839 if (g_worker->reader.name)
840 g_worker->reader.name = NULL;
841 if (g_worker->client.fd >= 0)
842 close(g_worker->client.fd);
843 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
844 g_worker->client.fd = -1;
845 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200846 }
847
848 pthread_cleanup_pop(1);
849 talloc_free(top_ctx);
850 pthread_exit(NULL);
851}