blob: 7851a2bb2084b5410a5040a9730f29542891f707 [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 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
Harald Welte77911b02018-08-14 23:47:30 +020095}
96
97/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020098static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020099{
100 struct bankd_worker *worker;
101 int rc;
102
103 worker = talloc_zero(bankd, struct bankd_worker);
104 if (!worker)
105 return NULL;
106
107 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200108 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100109 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200110
111 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
112
113 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
114 if (rc != 0) {
115 talloc_free(worker);
116 return NULL;
117 }
118
119 pthread_mutex_lock(&bankd->workers_mutex);
120 llist_add_tail(&worker->list, &bankd->workers);
121 pthread_mutex_unlock(&bankd->workers_mutex);
122
123 return worker;
124}
125
126static bool terminate = false;
127
Harald Weltec650a4d2019-12-04 15:02:27 +0100128/* Remove a mapping */
129static void bankd_srvc_remove_mapping(struct slot_mapping *map)
130{
131 struct bank_slot bs = map->bank;
132
133 slotmap_del(g_bankd->slotmaps, map);
134
135 /* kill/reset the respective worker, if any! */
136 struct bankd_worker *worker;
137 pthread_mutex_lock(&g_bankd->workers_mutex);
138 llist_for_each_entry(worker, &g_bankd->workers, list) {
139 if (bs.bank_id == worker->slot.bank_id &&
140 bs.slot_nr == worker->slot.slot_nr) {
141 pthread_kill(worker->thread, SIGMAPDEL);
142 break;
143 }
144 }
145 pthread_mutex_unlock(&g_bankd->workers_mutex);
146}
147
Harald Welte707c85a2019-03-09 12:56:35 +0100148/* handle incoming messages from server */
149static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
150{
Harald Welte454f5e22019-03-09 21:38:34 +0100151 const CreateMappingReq_t *creq = NULL;
152 const RemoveMappingReq_t *rreq = NULL;
153 struct slot_mapping *map;
154 struct bank_slot bs;
155 struct client_slot cs;
156 RsproPDU_t *resp;
157
158 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100159
160 switch (pdu->msg.present) {
161 case RsproPDUchoice_PR_connectBankRes:
162 /* Store 'identity' of server in srvc->peer_comp_id */
163 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
164 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
165 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100166 case RsproPDUchoice_PR_createMappingReq:
167 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200168 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100169 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200170 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100171 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200172 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100173 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200174 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100175 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100176 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100177 rspro2bank_slot(&bs, &creq->bank);
178 rspro2client_slot(&cs, &creq->client);
179 /* Add a new mapping */
180 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100181 if (!map) {
182 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100183 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100184 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100185 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
186 }
187 server_conn_send_rspro(srvc, resp);
188 break;
189 case RsproPDUchoice_PR_removeMappingReq:
190 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200191 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100192 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200193 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100194 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200195 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100196 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200197 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100198 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100199 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100200 rspro2bank_slot(&bs, &rreq->bank);
201 /* Remove a mapping */
202 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100203 if (!map) {
204 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100205 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100206 } else {
207 LOGPFSM(srvc->fi, "removing slotmap\n");
Harald Weltec650a4d2019-12-04 15:02:27 +0100208 bankd_srvc_remove_mapping(map);
Harald Welte454f5e22019-03-09 21:38:34 +0100209 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
210 }
211 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100212 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100213 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100214 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100215 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
216 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100217 return -1;
218 }
219
220 return 0;
221}
222
Harald Welte5bae20b2019-04-01 09:36:42 +0200223static void printf_help()
224{
225 printf(
226" -h --help Print this help message\n"
227" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
228" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
229" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200230" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200231" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
232" connections (default: INADDR_ANY)\n"
233" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
234" connectionss (default: 9999)\n"
235 );
236}
237
238static int g_bind_port = 9999;
239static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100240
Harald Welte707c85a2019-03-09 12:56:35 +0100241void handle_options(int argc, char **argv)
242{
Harald Welte5bae20b2019-04-01 09:36:42 +0200243 while (1) {
244 int option_index = 0, c;
245 static const struct option long_options[] = {
246 { "help", 0, 0, 'h' },
247 { "server-host", 1, 0, 'i' },
248 { "server-port", 1, 0, 'p' },
249 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200250 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200251 { "component-name", 1, 0, 'N' },
252 { "bind-ip", 1, 0, 'I' },
253 { "bind-port", 1, 0, 'P' },
254 { 0, 0, 0, 0 }
255 };
256
Harald Welte2513d812019-04-01 21:03:02 +0200257 c = getopt_long(argc, argv, "hi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200258 if (c == -1)
259 break;
260
261 switch (c) {
262 case 'h':
263 printf_help();
264 exit(0);
265 break;
266 case 'i':
267 g_bankd->srvc.server_host = optarg;
268 break;
269 case 'p':
270 g_bankd->srvc.server_port = atoi(optarg);
271 break;
272 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200273 g_bankd->srvc.bankd.bank_id = atoi(optarg);
274 break;
275 case 'n':
276 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200277 break;
278 case 'N':
279 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
280 break;
281 case 'I':
282 g_bind_ip = optarg;
283 break;
284 case 'P':
285 g_bind_port = atoi(optarg);
286 break;
287 }
288 }
Harald Welte707c85a2019-03-09 12:56:35 +0100289}
290
Harald Welte77911b02018-08-14 23:47:30 +0200291int main(int argc, char **argv)
292{
Harald Weltef4b16f12019-03-09 20:58:17 +0100293 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200294 int i, rc;
295
Harald Weltef4b16f12019-03-09 20:58:17 +0100296 g_bankd = talloc_zero(NULL, struct bankd);
297 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200298
Harald Weltef4b16f12019-03-09 20:58:17 +0100299 bankd_init(g_bankd);
300
301 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100302 srvc->server_host = "localhost";
303 srvc->server_port = 9998;
304 srvc->handle_rx = bankd_srvc_handle_rx;
305 srvc->own_comp_id.type = ComponentType_remsimBankd;
306 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
307 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
308 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
309
310 handle_options(argc, argv);
311
Harald Welte25075972019-03-11 17:33:17 +0100312 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100313 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100314 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100315
Harald Weltee89fecd2019-04-04 09:23:13 +0200316 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
317 * read once during bankd initialization, when the worker threads haven't
318 * started yet */
319 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
320
Harald Welte707c85a2019-03-09 12:56:35 +0100321 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100322 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100323 if (rc < 0) {
324 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
325 exit(1);
326 }
327
328 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200329 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 +0200330 if (rc < 0)
331 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100332 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200333
Harald Weltea0f39502019-03-09 20:59:34 +0100334 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200335 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200336 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100337 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200338 if (!w)
339 exit(21);
340 }
341
342 while (1) {
343 if (terminate)
344 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100345 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200346 }
347
Harald Weltef4b16f12019-03-09 20:58:17 +0100348 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200349 exit(0);
350}
351
352
353
354/***********************************************************************
355 * bankd worker thread
356 ***********************************************************************/
357
Harald Welte00a96732019-03-11 17:18:02 +0100358static __thread struct bankd_worker *g_worker;
359
Harald Welte8d858292018-08-15 23:36:46 +0200360struct value_string worker_state_names[] = {
361 { BW_ST_INIT, "INIT" },
362 { BW_ST_ACCEPTING, "ACCEPTING" },
363 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
364 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200365 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200366 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
367 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100368 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200369 { 0, NULL }
370};
371
Harald Welte1f699b42019-03-28 13:41:08 +0100372static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
373
Harald Welte8d858292018-08-15 23:36:46 +0200374static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
375{
376 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
377 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200378 worker->timeout = 0;
379}
380
381static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
382 unsigned int timeout_secs)
383{
384 LOGW(worker, "Changing state to %s (timeout=%u)\n",
385 get_value_string(worker_state_names, new_state), timeout_secs);
386 worker->state = new_state;
387 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200388}
Harald Welte77911b02018-08-14 23:47:30 +0200389
Harald Welte00a96732019-03-11 17:18:02 +0100390/* signal handler for receiving SIGMAPDEL from main thread */
391static void handle_sig_mapdel(int sig)
392{
393 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
394 OSMO_ASSERT(sig == SIGMAPDEL);
395 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
396}
397
Harald Welte25075972019-03-11 17:33:17 +0100398static void handle_sig_usr1(int sig)
399{
400 OSMO_ASSERT(sig == SIGUSR1);
401
402 if (pthread_equal(g_bankd->main, pthread_self())) {
403 struct bankd_worker *worker;
404 /* main thread */
405 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200406 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100407
408 /* iterate over worker threads and ask them to dump their talloc state */
409 pthread_mutex_lock(&g_bankd->workers_mutex);
410 llist_for_each_entry(worker, &g_bankd->workers, list) {
411 pthread_kill(worker->thread, SIGUSR1);
412 }
413 pthread_mutex_unlock(&g_bankd->workers_mutex);
414 } else {
415 /* worker thread */
416 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200417 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100418 }
419}
420
Harald Welte77911b02018-08-14 23:47:30 +0200421static void worker_cleanup(void *arg)
422{
423 struct bankd_worker *worker = (struct bankd_worker *) arg;
424 struct bankd *bankd = worker->bankd;
425
426 /* FIXME: should we still do this? in the thread ?!? */
427 pthread_mutex_lock(&bankd->workers_mutex);
428 llist_del(&worker->list);
429 talloc_free(worker); /* FIXME: is this safe? */
430 pthread_mutex_unlock(&bankd->workers_mutex);
431}
432
Harald Welteaf614732018-08-17 22:10:05 +0200433static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200434{
Harald Welte297d72e2019-03-28 18:42:35 +0100435 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200436
Harald Welte150d6d62018-10-03 23:07:47 +0200437 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
438
Harald Welte694df832018-10-03 22:47:52 +0200439 if (!worker->reader.name) {
440 /* resolve PC/SC reader name from slot_id -> name map */
441 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
442 if (!worker->reader.name) {
443 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
444 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100445 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200446 }
447 }
Harald Welte45c948c2018-09-23 19:26:52 +0200448 OSMO_ASSERT(worker->reader.name);
449
Harald Welte297d72e2019-03-28 18:42:35 +0100450 rc = worker->ops->open_card(worker);
451 if (rc < 0)
452 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100453
Harald Welte57593f02018-09-23 19:30:31 +0200454 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200455 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200456
Harald Welteaf614732018-08-17 22:10:05 +0200457 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200458}
Harald Welte77911b02018-08-14 23:47:30 +0200459
460
Harald Welte00a96732019-03-11 17:18:02 +0100461static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200462{
463 struct ipaccess_head *hh;
464 uint16_t len;
465 int needed, rc;
466
467 if (buf_size < sizeof(*hh))
468 return -1;
469
470 hh = (struct ipaccess_head *) buf;
471
Harald Welte00a96732019-03-11 17:18:02 +0100472 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
473 * in case of a signal being received */
474
475restart_hdr:
476 /* 1) blocking recv from the socket (IPA header) */
477 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
478 if (rc == -1 && errno == EINTR) {
479 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
480 return -23;
481 goto restart_hdr;
482 } else if (rc < 0)
483 return rc;
484 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200485 return -2;
486
487 len = ntohs(hh->len);
488 needed = len; //- sizeof(*hh);
489
Harald Welte00a96732019-03-11 17:18:02 +0100490restart_body:
491 /* 2) blocking recv from the socket (payload) */
492 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
493 if (rc == -1 && errno == EINTR) {
494 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
495 return -23;
496 goto restart_body;
497 } else if (rc < 0)
498 return rc;
499 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200500 return -3;
501
502 return len;
503}
504
Harald Welte796a7492018-09-23 19:31:28 +0200505static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
506{
507 struct msgb *msg = rspro_enc_msg(pdu);
508 int rc;
509
510 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100511 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200512 LOGW(worker, "error encoding RSPRO\n");
513 return -1;
514 }
515
Harald Weltefd471192018-09-24 14:51:14 +0200516 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200517 /* prepend the header */
518 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200519 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200520
521 /* actually send it through the socket */
522 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
523 if (rc == msgb_length(msg))
524 rc = 0;
525 else {
526 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
527 rc = -1;
528 }
529
530 msgb_free(msg);
531
532 return rc;
533}
534
Harald Welte150d6d62018-10-03 23:07:47 +0200535/* attempt to obtain slot-map */
536static int worker_try_slotmap(struct bankd_worker *worker)
537{
Harald Weltecbd18962019-03-03 19:02:38 +0100538 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200539
Harald Weltecbd18962019-03-03 19:02:38 +0100540 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200541 if (!slmap) {
542 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
543 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
544 /* check in 10s if the map has been installed meanwhile by main thread */
545 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
546 return -1;
547 } else {
548 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
549 slmap->client.client_id, slmap->client.slot_nr,
550 slmap->bank.bank_id, slmap->bank.slot_nr);
551 worker->slot = slmap->bank;
552 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
553 return worker_open_card(worker);
554 }
555}
556
Harald Welte1f699b42019-03-28 13:41:08 +0100557/* inform the remote end (client) about the (new) ATR */
558static int worker_send_atr(struct bankd_worker *worker)
559{
560 RsproPDU_t *set_atr;
561 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
562 worker->client.clslot.slot_nr,
563 worker->card.atr, worker->card.atr_len);
564 if (!set_atr)
565 return -1;
566 return worker_send_rspro(worker, set_atr);
567}
Harald Welte150d6d62018-10-03 23:07:47 +0200568
Harald Weltecce2aad2018-08-16 14:44:37 +0200569static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
570{
Harald Welteaf614732018-08-17 22:10:05 +0200571 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100572 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200573 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100574 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200575
Harald Weltecce2aad2018-08-16 14:44:37 +0200576 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
577
Harald Weltecce2aad2018-08-16 14:44:37 +0200578 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
579 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
580 /* FIXME: store somewhere? */
581
582 if (worker->state != BW_ST_CONN_WAIT_ID) {
583 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100584 rc = -102;
585 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200586 }
587
Harald Welte371d0262018-08-16 15:23:58 +0200588 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200589 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100590 res = ResultCode_illegalClientId;
591 rc = -103;
592 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200593 }
Harald Welte371d0262018-08-16 15:23:58 +0200594 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
595 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200596 worker_set_state(worker, BW_ST_CONN_CLIENT);
597
Harald Welte150d6d62018-10-03 23:07:47 +0200598 if (worker_try_slotmap(worker) >= 0)
599 res = ResultCode_ok;
600 else
Harald Welte3e689872018-09-24 14:52:56 +0200601 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200602
Harald Welte3e689872018-09-24 14:52:56 +0200603 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100604 rc = worker_send_rspro(worker, resp);
605 if (rc < 0)
606 return rc;
607
608 if (res == ResultCode_ok)
609 rc = worker_send_atr(worker);
610
611 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100612
613respond_and_err:
614 if (res) {
615 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
616 worker_send_rspro(worker, resp);
617 }
618 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200619}
620
Harald Welte796a7492018-09-23 19:31:28 +0200621static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
622{
623 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200624 uint8_t rx_buf[1024];
625 DWORD rx_buf_len = sizeof(rx_buf);
626 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100627 struct client_slot clslot;
628 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100629 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200630
631 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
632
633 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
634 LOGW(worker, "Unexpected tpduModemToCaard\n");
635 return -104;
636 }
637
Harald Weltee1d32892019-03-27 20:47:42 +0100638 /* Validate that toBankSlot / fromClientSlot match our expectations */
639 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
640 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
641 if (!bank_slot_equals(&worker->slot, &bslot)) {
642 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
643 bslot.bank_id, bslot.slot_nr);
644 return -105;
645 }
646 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
647 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
648 clslot.client_id, clslot.slot_nr);
649 return -106;
650 }
Harald Welte796a7492018-09-23 19:31:28 +0200651
Harald Welte297d72e2019-03-28 18:42:35 +0100652 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
653 rx_buf, &rx_buf_len);
654 if (rc < 0)
655 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200656
657 /* encode response PDU and send it */
658 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
659 rx_buf, rx_buf_len);
660 worker_send_rspro(worker, pdu_resp);
661
662 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200663}
664
Harald Welte77911b02018-08-14 23:47:30 +0200665/* handle one incoming RSPRO message from a client inside a worker thread */
666static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
667{
Harald Weltecce2aad2018-08-16 14:44:37 +0200668 int rc = -100;
669
Harald Welte736e8312019-03-31 13:08:16 +0200670 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
671
Harald Welte77911b02018-08-14 23:47:30 +0200672 switch (pdu->msg.present) {
673 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200674 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200675 break;
676 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200677 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200678 break;
679 case RsproPDUchoice_PR_clientSlotStatusInd:
680 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200681 rc = 0;
682 break;
683 case RsproPDUchoice_PR_setAtrRes:
684 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200685 break;
686 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200687 rc = -101;
688 break;
Harald Welte77911b02018-08-14 23:47:30 +0200689 }
690
Harald Weltecce2aad2018-08-16 14:44:37 +0200691 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200692}
693
Harald Welte694df832018-10-03 22:47:52 +0200694static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
695{
696 struct timeval tout = { timeout_secs, 0 };
697 fd_set readset;
698
699 FD_ZERO(&readset);
700 FD_SET(fd, &readset);
701 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
702}
703
Harald Welte77911b02018-08-14 23:47:30 +0200704/* body of the main transceive loop */
705static int worker_transceive_loop(struct bankd_worker *worker)
706{
707 struct ipaccess_head *hh;
708 struct ipaccess_head_ext *hh_ext;
709 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
710 asn_dec_rval_t rval;
711 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200712 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200713
Harald Welte00a96732019-03-11 17:18:02 +0100714restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200715 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100716 if (rc == -1 && errno == EINTR) {
717 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
718 return -23;
719 goto restart_wait;
720 } else if (rc < 0)
721 return rc;
722 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200723 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200724 switch (worker->state) {
725 case BW_ST_CONN_CLIENT_WAIT_MAP:
726 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100727 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200728 break;
729 case BW_ST_CONN_CLIENT_MAPPED:
730 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100731 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200732 break;
733 default:
734 OSMO_ASSERT(0);
735 }
Harald Welte1f699b42019-03-28 13:41:08 +0100736 if (rc == 0)
737 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200738 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200739 return 0;
740 };
741
Harald Welte77911b02018-08-14 23:47:30 +0200742 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100743 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200744 if (rc < 0)
745 return rc;
746 data_len = rc;
747
748 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200749 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200750 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200751 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200752 }
Harald Welte77911b02018-08-14 23:47:30 +0200753
Harald Welte5a3613a2018-10-11 12:56:21 +0200754 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100755 switch (hh->data[0]) {
756 case IPAC_MSGT_PING:
757 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100758 case IPAC_MSGT_ID_ACK:
759 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100760 default:
761 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
762 break;
763 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200764 return 0;
765 }
766
Harald Welte77911b02018-08-14 23:47:30 +0200767 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200768 if (data_len < sizeof(*hh_ext)) {
769 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200770 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200771 }
Harald Welte77911b02018-08-14 23:47:30 +0200772 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200773 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
774 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200775 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200776 }
Harald Welte77911b02018-08-14 23:47:30 +0200777
778 /* 2) ASN1 BER decode of the message */
779 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200780 if (rval.code != RC_OK) {
781 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200782 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200783 }
Harald Welte77911b02018-08-14 23:47:30 +0200784
785 /* 3) handling of the message, possibly resulting in PCSC commands */
786 rc = worker_handle_rspro(worker, pdu);
787 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200788 if (rc < 0) {
789 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200790 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200791 }
Harald Welte77911b02018-08-14 23:47:30 +0200792
793 /* everything OK if we reach here */
794 return 0;
795}
796
Harald Welted6dfb8c2018-08-16 14:46:53 +0200797/* obtain an ascii representation of the client IP/port */
798static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
799{
800 char hostbuf[32], portbuf[32];
801 int rc;
802
803 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
804 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
805 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
806 if (rc != 0) {
807 out[0] = '\0';
808 return -1;
809 }
810 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
811 return 0;
812}
813
Harald Welte77911b02018-08-14 23:47:30 +0200814/* worker thread main function */
815static void *worker_main(void *arg)
816{
Harald Welte77911b02018-08-14 23:47:30 +0200817 void *top_ctx;
818 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200819
Harald Welte00a96732019-03-11 17:18:02 +0100820 g_worker = (struct bankd_worker *) arg;
821
Harald Welte00a96732019-03-11 17:18:02 +0100822 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200823
Harald Welte77911b02018-08-14 23:47:30 +0200824 /* not permitted in multithreaded environment */
825 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100826 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
827 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200828
Harald Welte286a2be2019-03-11 17:36:58 +0100829 /* set the thread name */
830 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
831 pthread_setname_np(pthread_self(), g_worker->name);
832
Harald Welte77911b02018-08-14 23:47:30 +0200833 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100834 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200835
836 /* we continuously perform the same loop here, recycling the worker thread
837 * once the client connection is gone or we have some trouble with the card/reader */
838 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200839 char buf[128];
840
Harald Welte00a96732019-03-11 17:18:02 +0100841 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200842
Harald Welte00a96732019-03-11 17:18:02 +0100843 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200844 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100845 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
846 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200847 if (rc < 0) {
848 continue;
849 }
Harald Welte00a96732019-03-11 17:18:02 +0100850 g_worker->client.fd = rc;
851 worker_client_addrstr(buf, sizeof(buf), g_worker);
852 LOGW(g_worker, "Accepted connection from %s\n", buf);
853 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200854
855 /* run the main worker transceive loop body until there was some error */
856 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100857 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200858 if (rc < 0)
859 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100860 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
861 break;
Harald Welte77911b02018-08-14 23:47:30 +0200862 }
863
Harald Welte00a96732019-03-11 17:18:02 +0100864 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200865
Harald Welte77911b02018-08-14 23:47:30 +0200866 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100867 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100868 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100869 if (g_worker->reader.name)
870 g_worker->reader.name = NULL;
871 if (g_worker->client.fd >= 0)
872 close(g_worker->client.fd);
873 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
874 g_worker->client.fd = -1;
875 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200876 }
877
878 pthread_cleanup_pop(1);
879 talloc_free(top_ctx);
880 pthread_exit(NULL);
881}