blob: 8ac5909f73a7fd819370e91ae3aedc48c940401f [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 Welte5bae20b2019-04-01 09:36:42 +020031#include <getopt.h>
Harald Welte77911b02018-08-14 23:47:30 +020032
33#include <pthread.h>
34
Harald Welted6dfb8c2018-08-16 14:46:53 +020035#include <sys/socket.h>
36#include <netdb.h>
37
Harald Welte12534e72018-08-15 23:37:29 +020038#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020039#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020040#include <osmocom/core/logging.h>
41#include <osmocom/core/application.h>
Harald Welte77911b02018-08-14 23:47:30 +020042
43#include <osmocom/gsm/ipa.h>
44#include <osmocom/gsm/protocol/ipaccess.h>
45
Kévin Redonff5db6e2018-10-11 17:16:18 +020046#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020047#include <osmocom/rspro/RsproPDU.h>
48
49#include "bankd.h"
Harald Welte3cded632019-03-09 12:59:41 +010050#include "rspro_client_fsm.h"
Harald Welte61d98e92019-03-03 15:43:07 +010051#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020052#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020053
Harald Welte00a96732019-03-11 17:18:02 +010054/* signal indicates to worker thread that its map has been deleted */
55#define SIGMAPDEL SIGRTMIN+1
Harald Welte25075972019-03-11 17:33:17 +010056
57static void handle_sig_usr1(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010058static void handle_sig_mapdel(int sig);
59
Harald Welte77911b02018-08-14 23:47:30 +020060__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010061struct bankd *g_bankd;
Harald Welte25075972019-03-11 17:33:17 +010062static void *g_tall_ctx;
Harald Welte77911b02018-08-14 23:47:30 +020063
64static void *worker_main(void *arg);
65
66/***********************************************************************
67* bankd core / main thread
68***********************************************************************/
69
Harald Welte43ab79f2018-10-03 23:34:21 +020070int asn_debug;
71
Harald Welte77911b02018-08-14 23:47:30 +020072static void bankd_init(struct bankd *bankd)
73{
Harald Welte25075972019-03-11 17:33:17 +010074 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Weltef94b9ee2018-09-25 15:04:21 +020075 osmo_init_logging2(g_tall_ctx, &log_info);
76
Harald Welte43ab79f2018-10-03 23:34:21 +020077 asn_debug = 0;
78
Harald Welte77911b02018-08-14 23:47:30 +020079 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010080 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020081 INIT_LLIST_HEAD(&bankd->workers);
82 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020083
Harald Weltea0f39502019-03-09 20:59:34 +010084 /* set some defaults, overridden by commandline/config */
Harald Welte2513d812019-04-01 21:03:02 +020085 bankd->srvc.bankd.bank_id = 1;
86 bankd->srvc.bankd.num_slots = 8;
Harald Weltea0f39502019-03-09 20:59:34 +010087
Harald Weltef1dd1622018-09-24 14:54:23 +020088 bankd->comp_id.type = ComponentType_remsimBankd;
89 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
90 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
91 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
92 /* FIXME: other members of app_comp_id */
93
Harald Welte45c948c2018-09-23 19:26:52 +020094 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
95 * read once during bankd initialization, when the worker threads haven't
96 * started yet */
97 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
98 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Welte77911b02018-08-14 23:47:30 +020099}
100
101/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200102static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200103{
104 struct bankd_worker *worker;
105 int rc;
106
107 worker = talloc_zero(bankd, struct bankd_worker);
108 if (!worker)
109 return NULL;
110
111 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200112 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100113 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200114
115 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
116
117 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
118 if (rc != 0) {
119 talloc_free(worker);
120 return NULL;
121 }
122
123 pthread_mutex_lock(&bankd->workers_mutex);
124 llist_add_tail(&worker->list, &bankd->workers);
125 pthread_mutex_unlock(&bankd->workers_mutex);
126
127 return worker;
128}
129
130static bool terminate = false;
131
Harald Welte707c85a2019-03-09 12:56:35 +0100132/* handle incoming messages from server */
133static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
134{
Harald Welte454f5e22019-03-09 21:38:34 +0100135 const CreateMappingReq_t *creq = NULL;
136 const RemoveMappingReq_t *rreq = NULL;
137 struct slot_mapping *map;
138 struct bank_slot bs;
139 struct client_slot cs;
140 RsproPDU_t *resp;
141
142 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100143
144 switch (pdu->msg.present) {
145 case RsproPDUchoice_PR_connectBankRes:
146 /* Store 'identity' of server in srvc->peer_comp_id */
147 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
148 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
149 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100150 case RsproPDUchoice_PR_createMappingReq:
151 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200152 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100153 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200154 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100155 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200156 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100157 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200158 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100159 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100160 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100161 rspro2bank_slot(&bs, &creq->bank);
162 rspro2client_slot(&cs, &creq->client);
163 /* Add a new mapping */
164 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100165 if (!map) {
166 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100167 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100168 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100169 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
170 }
171 server_conn_send_rspro(srvc, resp);
172 break;
173 case RsproPDUchoice_PR_removeMappingReq:
174 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200175 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100176 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200177 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100178 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200179 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100180 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200181 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100182 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100183 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100184 rspro2bank_slot(&bs, &rreq->bank);
185 /* Remove a mapping */
186 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100187 if (!map) {
188 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100189 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100190 } else {
191 LOGPFSM(srvc->fi, "removing slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100192 slotmap_del(g_bankd->slotmaps, map);
193 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
Harald Welte00a96732019-03-11 17:18:02 +0100194
195 /* kill/reset the respective worker, if any! */
196 struct bankd_worker *worker;
197 pthread_mutex_lock(&g_bankd->workers_mutex);
198 llist_for_each_entry(worker, &g_bankd->workers, list) {
199 if (bs.bank_id == worker->slot.bank_id &&
200 bs.slot_nr == worker->slot.slot_nr) {
201 pthread_kill(worker->thread, SIGMAPDEL);
202 break;
203 }
204 }
205 pthread_mutex_unlock(&g_bankd->workers_mutex);
Harald Welte454f5e22019-03-09 21:38:34 +0100206 }
207 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100208 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100209 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100210 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100211 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
212 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100213 return -1;
214 }
215
216 return 0;
217}
218
Harald Welte5bae20b2019-04-01 09:36:42 +0200219static void printf_help()
220{
221 printf(
222" -h --help Print this help message\n"
223" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
224" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
225" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Harald Welte2513d812019-04-01 21:03:02 +0200226" -b --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200227" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
228" connections (default: INADDR_ANY)\n"
229" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
230" connectionss (default: 9999)\n"
231 );
232}
233
234static int g_bind_port = 9999;
235static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100236
Harald Welte707c85a2019-03-09 12:56:35 +0100237void handle_options(int argc, char **argv)
238{
Harald Welte5bae20b2019-04-01 09:36:42 +0200239 while (1) {
240 int option_index = 0, c;
241 static const struct option long_options[] = {
242 { "help", 0, 0, 'h' },
243 { "server-host", 1, 0, 'i' },
244 { "server-port", 1, 0, 'p' },
245 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200246 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200247 { "component-name", 1, 0, 'N' },
248 { "bind-ip", 1, 0, 'I' },
249 { "bind-port", 1, 0, 'P' },
250 { 0, 0, 0, 0 }
251 };
252
Harald Welte2513d812019-04-01 21:03:02 +0200253 c = getopt_long(argc, argv, "hi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200254 if (c == -1)
255 break;
256
257 switch (c) {
258 case 'h':
259 printf_help();
260 exit(0);
261 break;
262 case 'i':
263 g_bankd->srvc.server_host = optarg;
264 break;
265 case 'p':
266 g_bankd->srvc.server_port = atoi(optarg);
267 break;
268 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200269 g_bankd->srvc.bankd.bank_id = atoi(optarg);
270 break;
271 case 'n':
272 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200273 break;
274 case 'N':
275 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
276 break;
277 case 'I':
278 g_bind_ip = optarg;
279 break;
280 case 'P':
281 g_bind_port = atoi(optarg);
282 break;
283 }
284 }
Harald Welte707c85a2019-03-09 12:56:35 +0100285}
286
Harald Welte77911b02018-08-14 23:47:30 +0200287int main(int argc, char **argv)
288{
Harald Weltef4b16f12019-03-09 20:58:17 +0100289 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200290 int i, rc;
291
Harald Weltef4b16f12019-03-09 20:58:17 +0100292 g_bankd = talloc_zero(NULL, struct bankd);
293 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200294
Harald Weltef4b16f12019-03-09 20:58:17 +0100295 bankd_init(g_bankd);
296
297 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100298 srvc->server_host = "localhost";
299 srvc->server_port = 9998;
300 srvc->handle_rx = bankd_srvc_handle_rx;
301 srvc->own_comp_id.type = ComponentType_remsimBankd;
302 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
303 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
304 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
305
306 handle_options(argc, argv);
307
Harald Welte25075972019-03-11 17:33:17 +0100308 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100309 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100310 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100311
Harald Welte707c85a2019-03-09 12:56:35 +0100312 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100313 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100314 if (rc < 0) {
315 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
316 exit(1);
317 }
318
319 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200320 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, g_bind_ip, g_bind_port, OSMO_SOCK_F_BIND);
Harald Welte12534e72018-08-15 23:37:29 +0200321 if (rc < 0)
322 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100323 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200324
Harald Weltea0f39502019-03-09 20:59:34 +0100325 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200326 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200327 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100328 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200329 if (!w)
330 exit(21);
331 }
332
333 while (1) {
334 if (terminate)
335 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100336 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200337 }
338
Harald Weltef4b16f12019-03-09 20:58:17 +0100339 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200340 exit(0);
341}
342
343
344
345/***********************************************************************
346 * bankd worker thread
347 ***********************************************************************/
348
Harald Welte00a96732019-03-11 17:18:02 +0100349static __thread struct bankd_worker *g_worker;
350
Harald Welte8d858292018-08-15 23:36:46 +0200351struct value_string worker_state_names[] = {
352 { BW_ST_INIT, "INIT" },
353 { BW_ST_ACCEPTING, "ACCEPTING" },
354 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
355 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200356 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200357 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
358 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100359 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200360 { 0, NULL }
361};
362
Harald Welte1f699b42019-03-28 13:41:08 +0100363static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
364
Harald Welte8d858292018-08-15 23:36:46 +0200365static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
366{
367 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
368 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200369 worker->timeout = 0;
370}
371
372static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
373 unsigned int timeout_secs)
374{
375 LOGW(worker, "Changing state to %s (timeout=%u)\n",
376 get_value_string(worker_state_names, new_state), timeout_secs);
377 worker->state = new_state;
378 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200379}
Harald Welte77911b02018-08-14 23:47:30 +0200380
Harald Welte00a96732019-03-11 17:18:02 +0100381/* signal handler for receiving SIGMAPDEL from main thread */
382static void handle_sig_mapdel(int sig)
383{
384 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
385 OSMO_ASSERT(sig == SIGMAPDEL);
386 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
387}
388
Harald Welte25075972019-03-11 17:33:17 +0100389static void handle_sig_usr1(int sig)
390{
391 OSMO_ASSERT(sig == SIGUSR1);
392
393 if (pthread_equal(g_bankd->main, pthread_self())) {
394 struct bankd_worker *worker;
395 /* main thread */
396 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200397 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100398
399 /* iterate over worker threads and ask them to dump their talloc state */
400 pthread_mutex_lock(&g_bankd->workers_mutex);
401 llist_for_each_entry(worker, &g_bankd->workers, list) {
402 pthread_kill(worker->thread, SIGUSR1);
403 }
404 pthread_mutex_unlock(&g_bankd->workers_mutex);
405 } else {
406 /* worker thread */
407 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200408 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100409 }
410}
411
Harald Welte77911b02018-08-14 23:47:30 +0200412static void worker_cleanup(void *arg)
413{
414 struct bankd_worker *worker = (struct bankd_worker *) arg;
415 struct bankd *bankd = worker->bankd;
416
417 /* FIXME: should we still do this? in the thread ?!? */
418 pthread_mutex_lock(&bankd->workers_mutex);
419 llist_del(&worker->list);
420 talloc_free(worker); /* FIXME: is this safe? */
421 pthread_mutex_unlock(&bankd->workers_mutex);
422}
423
Harald Welteaf614732018-08-17 22:10:05 +0200424static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200425{
Harald Welte297d72e2019-03-28 18:42:35 +0100426 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200427
Harald Welte150d6d62018-10-03 23:07:47 +0200428 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
429
Harald Welte694df832018-10-03 22:47:52 +0200430 if (!worker->reader.name) {
431 /* resolve PC/SC reader name from slot_id -> name map */
432 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
433 if (!worker->reader.name) {
434 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
435 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100436 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200437 }
438 }
Harald Welte45c948c2018-09-23 19:26:52 +0200439 OSMO_ASSERT(worker->reader.name);
440
Harald Welte297d72e2019-03-28 18:42:35 +0100441 rc = worker->ops->open_card(worker);
442 if (rc < 0)
443 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100444
Harald Welte57593f02018-09-23 19:30:31 +0200445 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200446 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200447
Harald Welteaf614732018-08-17 22:10:05 +0200448 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200449}
Harald Welte77911b02018-08-14 23:47:30 +0200450
451
Harald Welte00a96732019-03-11 17:18:02 +0100452static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200453{
454 struct ipaccess_head *hh;
455 uint16_t len;
456 int needed, rc;
457
458 if (buf_size < sizeof(*hh))
459 return -1;
460
461 hh = (struct ipaccess_head *) buf;
462
Harald Welte00a96732019-03-11 17:18:02 +0100463 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
464 * in case of a signal being received */
465
466restart_hdr:
467 /* 1) blocking recv from the socket (IPA header) */
468 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
469 if (rc == -1 && errno == EINTR) {
470 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
471 return -23;
472 goto restart_hdr;
473 } else if (rc < 0)
474 return rc;
475 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200476 return -2;
477
478 len = ntohs(hh->len);
479 needed = len; //- sizeof(*hh);
480
Harald Welte00a96732019-03-11 17:18:02 +0100481restart_body:
482 /* 2) blocking recv from the socket (payload) */
483 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
484 if (rc == -1 && errno == EINTR) {
485 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
486 return -23;
487 goto restart_body;
488 } else if (rc < 0)
489 return rc;
490 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200491 return -3;
492
493 return len;
494}
495
Harald Welte796a7492018-09-23 19:31:28 +0200496static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
497{
498 struct msgb *msg = rspro_enc_msg(pdu);
499 int rc;
500
501 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100502 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200503 LOGW(worker, "error encoding RSPRO\n");
504 return -1;
505 }
506
Harald Weltefd471192018-09-24 14:51:14 +0200507 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200508 /* prepend the header */
509 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200510 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200511
512 /* actually send it through the socket */
513 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
514 if (rc == msgb_length(msg))
515 rc = 0;
516 else {
517 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
518 rc = -1;
519 }
520
521 msgb_free(msg);
522
523 return rc;
524}
525
Harald Welte150d6d62018-10-03 23:07:47 +0200526/* attempt to obtain slot-map */
527static int worker_try_slotmap(struct bankd_worker *worker)
528{
Harald Weltecbd18962019-03-03 19:02:38 +0100529 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200530
Harald Weltecbd18962019-03-03 19:02:38 +0100531 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200532 if (!slmap) {
533 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
534 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
535 /* check in 10s if the map has been installed meanwhile by main thread */
536 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
537 return -1;
538 } else {
539 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
540 slmap->client.client_id, slmap->client.slot_nr,
541 slmap->bank.bank_id, slmap->bank.slot_nr);
542 worker->slot = slmap->bank;
543 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
544 return worker_open_card(worker);
545 }
546}
547
Harald Welte1f699b42019-03-28 13:41:08 +0100548/* inform the remote end (client) about the (new) ATR */
549static int worker_send_atr(struct bankd_worker *worker)
550{
551 RsproPDU_t *set_atr;
552 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
553 worker->client.clslot.slot_nr,
554 worker->card.atr, worker->card.atr_len);
555 if (!set_atr)
556 return -1;
557 return worker_send_rspro(worker, set_atr);
558}
Harald Welte150d6d62018-10-03 23:07:47 +0200559
Harald Weltecce2aad2018-08-16 14:44:37 +0200560static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
561{
Harald Welteaf614732018-08-17 22:10:05 +0200562 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100563 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200564 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100565 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200566
Harald Weltecce2aad2018-08-16 14:44:37 +0200567 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
568
Harald Weltecce2aad2018-08-16 14:44:37 +0200569 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
570 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
571 /* FIXME: store somewhere? */
572
573 if (worker->state != BW_ST_CONN_WAIT_ID) {
574 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100575 rc = -102;
576 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200577 }
578
Harald Welte371d0262018-08-16 15:23:58 +0200579 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200580 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100581 res = ResultCode_illegalClientId;
582 rc = -103;
583 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200584 }
Harald Welte371d0262018-08-16 15:23:58 +0200585 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
586 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200587 worker_set_state(worker, BW_ST_CONN_CLIENT);
588
Harald Welte150d6d62018-10-03 23:07:47 +0200589 if (worker_try_slotmap(worker) >= 0)
590 res = ResultCode_ok;
591 else
Harald Welte3e689872018-09-24 14:52:56 +0200592 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200593
Harald Welte3e689872018-09-24 14:52:56 +0200594 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100595 rc = worker_send_rspro(worker, resp);
596 if (rc < 0)
597 return rc;
598
599 if (res == ResultCode_ok)
600 rc = worker_send_atr(worker);
601
602 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100603
604respond_and_err:
605 if (res) {
606 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
607 worker_send_rspro(worker, resp);
608 }
609 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200610}
611
Harald Welte796a7492018-09-23 19:31:28 +0200612static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
613{
614 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200615 uint8_t rx_buf[1024];
616 DWORD rx_buf_len = sizeof(rx_buf);
617 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100618 struct client_slot clslot;
619 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100620 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200621
622 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
623
624 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
625 LOGW(worker, "Unexpected tpduModemToCaard\n");
626 return -104;
627 }
628
Harald Weltee1d32892019-03-27 20:47:42 +0100629 /* Validate that toBankSlot / fromClientSlot match our expectations */
630 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
631 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
632 if (!bank_slot_equals(&worker->slot, &bslot)) {
633 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
634 bslot.bank_id, bslot.slot_nr);
635 return -105;
636 }
637 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
638 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
639 clslot.client_id, clslot.slot_nr);
640 return -106;
641 }
Harald Welte796a7492018-09-23 19:31:28 +0200642
Harald Welte297d72e2019-03-28 18:42:35 +0100643 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
644 rx_buf, &rx_buf_len);
645 if (rc < 0)
646 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200647
648 /* encode response PDU and send it */
649 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
650 rx_buf, rx_buf_len);
651 worker_send_rspro(worker, pdu_resp);
652
653 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200654}
655
Harald Welte77911b02018-08-14 23:47:30 +0200656/* handle one incoming RSPRO message from a client inside a worker thread */
657static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
658{
Harald Weltecce2aad2018-08-16 14:44:37 +0200659 int rc = -100;
660
Harald Welte736e8312019-03-31 13:08:16 +0200661 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
662
Harald Welte77911b02018-08-14 23:47:30 +0200663 switch (pdu->msg.present) {
664 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200665 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200666 break;
667 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200668 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200669 break;
670 case RsproPDUchoice_PR_clientSlotStatusInd:
671 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200672 rc = 0;
673 break;
674 case RsproPDUchoice_PR_setAtrRes:
675 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200676 break;
677 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200678 rc = -101;
679 break;
Harald Welte77911b02018-08-14 23:47:30 +0200680 }
681
Harald Weltecce2aad2018-08-16 14:44:37 +0200682 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200683}
684
Harald Welte694df832018-10-03 22:47:52 +0200685static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
686{
687 struct timeval tout = { timeout_secs, 0 };
688 fd_set readset;
689
690 FD_ZERO(&readset);
691 FD_SET(fd, &readset);
692 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
693}
694
Harald Welte77911b02018-08-14 23:47:30 +0200695/* body of the main transceive loop */
696static int worker_transceive_loop(struct bankd_worker *worker)
697{
698 struct ipaccess_head *hh;
699 struct ipaccess_head_ext *hh_ext;
700 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
701 asn_dec_rval_t rval;
702 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200703 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200704
Harald Welte00a96732019-03-11 17:18:02 +0100705restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200706 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100707 if (rc == -1 && errno == EINTR) {
708 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
709 return -23;
710 goto restart_wait;
711 } else if (rc < 0)
712 return rc;
713 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200714 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200715 switch (worker->state) {
716 case BW_ST_CONN_CLIENT_WAIT_MAP:
717 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100718 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200719 break;
720 case BW_ST_CONN_CLIENT_MAPPED:
721 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100722 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200723 break;
724 default:
725 OSMO_ASSERT(0);
726 }
Harald Welte1f699b42019-03-28 13:41:08 +0100727 if (rc == 0)
728 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200729 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200730 return 0;
731 };
732
Harald Welte77911b02018-08-14 23:47:30 +0200733 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100734 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200735 if (rc < 0)
736 return rc;
737 data_len = rc;
738
739 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200740 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200741 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200742 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200743 }
Harald Welte77911b02018-08-14 23:47:30 +0200744
Harald Welte5a3613a2018-10-11 12:56:21 +0200745 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100746 switch (hh->data[0]) {
747 case IPAC_MSGT_PING:
748 return ipa_ccm_send_pong(worker->client.fd);
749 default:
750 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
751 break;
752 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200753 return 0;
754 }
755
Harald Welte77911b02018-08-14 23:47:30 +0200756 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200757 if (data_len < sizeof(*hh_ext)) {
758 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200759 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200760 }
Harald Welte77911b02018-08-14 23:47:30 +0200761 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200762 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
763 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200764 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200765 }
Harald Welte77911b02018-08-14 23:47:30 +0200766
767 /* 2) ASN1 BER decode of the message */
768 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200769 if (rval.code != RC_OK) {
770 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200771 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200772 }
Harald Welte77911b02018-08-14 23:47:30 +0200773
774 /* 3) handling of the message, possibly resulting in PCSC commands */
775 rc = worker_handle_rspro(worker, pdu);
776 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200777 if (rc < 0) {
778 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200779 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200780 }
Harald Welte77911b02018-08-14 23:47:30 +0200781
782 /* everything OK if we reach here */
783 return 0;
784}
785
Harald Welted6dfb8c2018-08-16 14:46:53 +0200786/* obtain an ascii representation of the client IP/port */
787static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
788{
789 char hostbuf[32], portbuf[32];
790 int rc;
791
792 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
793 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
794 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
795 if (rc != 0) {
796 out[0] = '\0';
797 return -1;
798 }
799 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
800 return 0;
801}
802
Harald Welte77911b02018-08-14 23:47:30 +0200803/* worker thread main function */
804static void *worker_main(void *arg)
805{
Harald Welte77911b02018-08-14 23:47:30 +0200806 void *top_ctx;
807 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200808
Harald Welte00a96732019-03-11 17:18:02 +0100809 g_worker = (struct bankd_worker *) arg;
810
Harald Welte00a96732019-03-11 17:18:02 +0100811 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200812
Harald Welte77911b02018-08-14 23:47:30 +0200813 /* not permitted in multithreaded environment */
814 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100815 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
816 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200817
Harald Welte286a2be2019-03-11 17:36:58 +0100818 /* set the thread name */
819 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
820 pthread_setname_np(pthread_self(), g_worker->name);
821
Harald Welte77911b02018-08-14 23:47:30 +0200822 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100823 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200824
825 /* we continuously perform the same loop here, recycling the worker thread
826 * once the client connection is gone or we have some trouble with the card/reader */
827 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200828 char buf[128];
829
Harald Welte00a96732019-03-11 17:18:02 +0100830 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200831
Harald Welte00a96732019-03-11 17:18:02 +0100832 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200833 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100834 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
835 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200836 if (rc < 0) {
837 continue;
838 }
Harald Welte00a96732019-03-11 17:18:02 +0100839 g_worker->client.fd = rc;
840 worker_client_addrstr(buf, sizeof(buf), g_worker);
841 LOGW(g_worker, "Accepted connection from %s\n", buf);
842 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200843
844 /* run the main worker transceive loop body until there was some error */
845 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100846 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200847 if (rc < 0)
848 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100849 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
850 break;
Harald Welte77911b02018-08-14 23:47:30 +0200851 }
852
Harald Welte00a96732019-03-11 17:18:02 +0100853 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200854
Harald Welte77911b02018-08-14 23:47:30 +0200855 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100856 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100857 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100858 if (g_worker->reader.name)
859 g_worker->reader.name = NULL;
860 if (g_worker->client.fd >= 0)
861 close(g_worker->client.fd);
862 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
863 g_worker->client.fd = -1;
864 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200865 }
866
867 pthread_cleanup_pop(1);
868 talloc_free(top_ctx);
869 pthread_exit(NULL);
870}