blob: 8cfeb9624234a35784bf3c9545e98b5fc39d1259 [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 Welte707c85a2019-03-09 12:56:35 +0100128/* handle incoming messages from server */
129static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
130{
Harald Welte454f5e22019-03-09 21:38:34 +0100131 const CreateMappingReq_t *creq = NULL;
132 const RemoveMappingReq_t *rreq = NULL;
133 struct slot_mapping *map;
134 struct bank_slot bs;
135 struct client_slot cs;
136 RsproPDU_t *resp;
137
138 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100139
140 switch (pdu->msg.present) {
141 case RsproPDUchoice_PR_connectBankRes:
142 /* Store 'identity' of server in srvc->peer_comp_id */
143 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
144 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
145 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100146 case RsproPDUchoice_PR_createMappingReq:
147 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200148 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100149 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200150 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100151 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200152 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100153 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200154 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100155 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100156 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100157 rspro2bank_slot(&bs, &creq->bank);
158 rspro2client_slot(&cs, &creq->client);
159 /* Add a new mapping */
160 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100161 if (!map) {
162 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100163 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100164 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100165 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
166 }
167 server_conn_send_rspro(srvc, resp);
168 break;
169 case RsproPDUchoice_PR_removeMappingReq:
170 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200171 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100172 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200173 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100174 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200175 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100176 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200177 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100178 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100179 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100180 rspro2bank_slot(&bs, &rreq->bank);
181 /* Remove a mapping */
182 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100183 if (!map) {
184 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100185 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100186 } else {
187 LOGPFSM(srvc->fi, "removing slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100188 slotmap_del(g_bankd->slotmaps, map);
189 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
Harald Welte00a96732019-03-11 17:18:02 +0100190
191 /* kill/reset the respective worker, if any! */
192 struct bankd_worker *worker;
193 pthread_mutex_lock(&g_bankd->workers_mutex);
194 llist_for_each_entry(worker, &g_bankd->workers, list) {
195 if (bs.bank_id == worker->slot.bank_id &&
196 bs.slot_nr == worker->slot.slot_nr) {
197 pthread_kill(worker->thread, SIGMAPDEL);
198 break;
199 }
200 }
201 pthread_mutex_unlock(&g_bankd->workers_mutex);
Harald Welte454f5e22019-03-09 21:38:34 +0100202 }
203 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100204 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100205 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100206 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100207 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
208 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100209 return -1;
210 }
211
212 return 0;
213}
214
Harald Welte5bae20b2019-04-01 09:36:42 +0200215static void printf_help()
216{
217 printf(
218" -h --help Print this help message\n"
219" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
220" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
221" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200222" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200223" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
224" connections (default: INADDR_ANY)\n"
225" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
226" connectionss (default: 9999)\n"
227 );
228}
229
230static int g_bind_port = 9999;
231static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100232
Harald Welte707c85a2019-03-09 12:56:35 +0100233void handle_options(int argc, char **argv)
234{
Harald Welte5bae20b2019-04-01 09:36:42 +0200235 while (1) {
236 int option_index = 0, c;
237 static const struct option long_options[] = {
238 { "help", 0, 0, 'h' },
239 { "server-host", 1, 0, 'i' },
240 { "server-port", 1, 0, 'p' },
241 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200242 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200243 { "component-name", 1, 0, 'N' },
244 { "bind-ip", 1, 0, 'I' },
245 { "bind-port", 1, 0, 'P' },
246 { 0, 0, 0, 0 }
247 };
248
Harald Welte2513d812019-04-01 21:03:02 +0200249 c = getopt_long(argc, argv, "hi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200250 if (c == -1)
251 break;
252
253 switch (c) {
254 case 'h':
255 printf_help();
256 exit(0);
257 break;
258 case 'i':
259 g_bankd->srvc.server_host = optarg;
260 break;
261 case 'p':
262 g_bankd->srvc.server_port = atoi(optarg);
263 break;
264 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200265 g_bankd->srvc.bankd.bank_id = atoi(optarg);
266 break;
267 case 'n':
268 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200269 break;
270 case 'N':
271 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
272 break;
273 case 'I':
274 g_bind_ip = optarg;
275 break;
276 case 'P':
277 g_bind_port = atoi(optarg);
278 break;
279 }
280 }
Harald Welte707c85a2019-03-09 12:56:35 +0100281}
282
Harald Welte77911b02018-08-14 23:47:30 +0200283int main(int argc, char **argv)
284{
Harald Weltef4b16f12019-03-09 20:58:17 +0100285 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200286 int i, rc;
287
Harald Weltef4b16f12019-03-09 20:58:17 +0100288 g_bankd = talloc_zero(NULL, struct bankd);
289 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200290
Harald Weltef4b16f12019-03-09 20:58:17 +0100291 bankd_init(g_bankd);
292
293 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100294 srvc->server_host = "localhost";
295 srvc->server_port = 9998;
296 srvc->handle_rx = bankd_srvc_handle_rx;
297 srvc->own_comp_id.type = ComponentType_remsimBankd;
298 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
299 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
300 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
301
302 handle_options(argc, argv);
303
Harald Welte25075972019-03-11 17:33:17 +0100304 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100305 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100306 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100307
Harald Weltee89fecd2019-04-04 09:23:13 +0200308 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
309 * read once during bankd initialization, when the worker threads haven't
310 * started yet */
311 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
312
Harald Welte707c85a2019-03-09 12:56:35 +0100313 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100314 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100315 if (rc < 0) {
316 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
317 exit(1);
318 }
319
320 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200321 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 +0200322 if (rc < 0)
323 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100324 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200325
Harald Weltea0f39502019-03-09 20:59:34 +0100326 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200327 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200328 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100329 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200330 if (!w)
331 exit(21);
332 }
333
334 while (1) {
335 if (terminate)
336 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100337 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200338 }
339
Harald Weltef4b16f12019-03-09 20:58:17 +0100340 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200341 exit(0);
342}
343
344
345
346/***********************************************************************
347 * bankd worker thread
348 ***********************************************************************/
349
Harald Welte00a96732019-03-11 17:18:02 +0100350static __thread struct bankd_worker *g_worker;
351
Harald Welte8d858292018-08-15 23:36:46 +0200352struct value_string worker_state_names[] = {
353 { BW_ST_INIT, "INIT" },
354 { BW_ST_ACCEPTING, "ACCEPTING" },
355 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
356 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200357 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200358 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
359 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100360 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200361 { 0, NULL }
362};
363
Harald Welte1f699b42019-03-28 13:41:08 +0100364static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
365
Harald Welte8d858292018-08-15 23:36:46 +0200366static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
367{
368 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
369 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200370 worker->timeout = 0;
371}
372
373static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
374 unsigned int timeout_secs)
375{
376 LOGW(worker, "Changing state to %s (timeout=%u)\n",
377 get_value_string(worker_state_names, new_state), timeout_secs);
378 worker->state = new_state;
379 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200380}
Harald Welte77911b02018-08-14 23:47:30 +0200381
Harald Welte00a96732019-03-11 17:18:02 +0100382/* signal handler for receiving SIGMAPDEL from main thread */
383static void handle_sig_mapdel(int sig)
384{
385 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
386 OSMO_ASSERT(sig == SIGMAPDEL);
387 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
388}
389
Harald Welte25075972019-03-11 17:33:17 +0100390static void handle_sig_usr1(int sig)
391{
392 OSMO_ASSERT(sig == SIGUSR1);
393
394 if (pthread_equal(g_bankd->main, pthread_self())) {
395 struct bankd_worker *worker;
396 /* main thread */
397 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200398 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100399
400 /* iterate over worker threads and ask them to dump their talloc state */
401 pthread_mutex_lock(&g_bankd->workers_mutex);
402 llist_for_each_entry(worker, &g_bankd->workers, list) {
403 pthread_kill(worker->thread, SIGUSR1);
404 }
405 pthread_mutex_unlock(&g_bankd->workers_mutex);
406 } else {
407 /* worker thread */
408 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200409 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100410 }
411}
412
Harald Welte77911b02018-08-14 23:47:30 +0200413static void worker_cleanup(void *arg)
414{
415 struct bankd_worker *worker = (struct bankd_worker *) arg;
416 struct bankd *bankd = worker->bankd;
417
418 /* FIXME: should we still do this? in the thread ?!? */
419 pthread_mutex_lock(&bankd->workers_mutex);
420 llist_del(&worker->list);
421 talloc_free(worker); /* FIXME: is this safe? */
422 pthread_mutex_unlock(&bankd->workers_mutex);
423}
424
Harald Welteaf614732018-08-17 22:10:05 +0200425static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200426{
Harald Welte297d72e2019-03-28 18:42:35 +0100427 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200428
Harald Welte150d6d62018-10-03 23:07:47 +0200429 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
430
Harald Welte694df832018-10-03 22:47:52 +0200431 if (!worker->reader.name) {
432 /* resolve PC/SC reader name from slot_id -> name map */
433 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
434 if (!worker->reader.name) {
435 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
436 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100437 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200438 }
439 }
Harald Welte45c948c2018-09-23 19:26:52 +0200440 OSMO_ASSERT(worker->reader.name);
441
Harald Welte297d72e2019-03-28 18:42:35 +0100442 rc = worker->ops->open_card(worker);
443 if (rc < 0)
444 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100445
Harald Welte57593f02018-09-23 19:30:31 +0200446 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200447 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200448
Harald Welteaf614732018-08-17 22:10:05 +0200449 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200450}
Harald Welte77911b02018-08-14 23:47:30 +0200451
452
Harald Welte00a96732019-03-11 17:18:02 +0100453static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200454{
455 struct ipaccess_head *hh;
456 uint16_t len;
457 int needed, rc;
458
459 if (buf_size < sizeof(*hh))
460 return -1;
461
462 hh = (struct ipaccess_head *) buf;
463
Harald Welte00a96732019-03-11 17:18:02 +0100464 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
465 * in case of a signal being received */
466
467restart_hdr:
468 /* 1) blocking recv from the socket (IPA header) */
469 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
470 if (rc == -1 && errno == EINTR) {
471 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
472 return -23;
473 goto restart_hdr;
474 } else if (rc < 0)
475 return rc;
476 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200477 return -2;
478
479 len = ntohs(hh->len);
480 needed = len; //- sizeof(*hh);
481
Harald Welte00a96732019-03-11 17:18:02 +0100482restart_body:
483 /* 2) blocking recv from the socket (payload) */
484 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
485 if (rc == -1 && errno == EINTR) {
486 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
487 return -23;
488 goto restart_body;
489 } else if (rc < 0)
490 return rc;
491 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200492 return -3;
493
494 return len;
495}
496
Harald Welte796a7492018-09-23 19:31:28 +0200497static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
498{
499 struct msgb *msg = rspro_enc_msg(pdu);
500 int rc;
501
502 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100503 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200504 LOGW(worker, "error encoding RSPRO\n");
505 return -1;
506 }
507
Harald Weltefd471192018-09-24 14:51:14 +0200508 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200509 /* prepend the header */
510 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200511 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200512
513 /* actually send it through the socket */
514 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
515 if (rc == msgb_length(msg))
516 rc = 0;
517 else {
518 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
519 rc = -1;
520 }
521
522 msgb_free(msg);
523
524 return rc;
525}
526
Harald Welte150d6d62018-10-03 23:07:47 +0200527/* attempt to obtain slot-map */
528static int worker_try_slotmap(struct bankd_worker *worker)
529{
Harald Weltecbd18962019-03-03 19:02:38 +0100530 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200531
Harald Weltecbd18962019-03-03 19:02:38 +0100532 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200533 if (!slmap) {
534 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
535 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
536 /* check in 10s if the map has been installed meanwhile by main thread */
537 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
538 return -1;
539 } else {
540 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
541 slmap->client.client_id, slmap->client.slot_nr,
542 slmap->bank.bank_id, slmap->bank.slot_nr);
543 worker->slot = slmap->bank;
544 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
545 return worker_open_card(worker);
546 }
547}
548
Harald Welte1f699b42019-03-28 13:41:08 +0100549/* inform the remote end (client) about the (new) ATR */
550static int worker_send_atr(struct bankd_worker *worker)
551{
552 RsproPDU_t *set_atr;
553 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
554 worker->client.clslot.slot_nr,
555 worker->card.atr, worker->card.atr_len);
556 if (!set_atr)
557 return -1;
558 return worker_send_rspro(worker, set_atr);
559}
Harald Welte150d6d62018-10-03 23:07:47 +0200560
Harald Weltecce2aad2018-08-16 14:44:37 +0200561static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
562{
Harald Welteaf614732018-08-17 22:10:05 +0200563 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100564 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200565 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100566 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200567
Harald Weltecce2aad2018-08-16 14:44:37 +0200568 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
569
Harald Weltecce2aad2018-08-16 14:44:37 +0200570 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
571 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
572 /* FIXME: store somewhere? */
573
574 if (worker->state != BW_ST_CONN_WAIT_ID) {
575 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100576 rc = -102;
577 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200578 }
579
Harald Welte371d0262018-08-16 15:23:58 +0200580 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200581 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100582 res = ResultCode_illegalClientId;
583 rc = -103;
584 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200585 }
Harald Welte371d0262018-08-16 15:23:58 +0200586 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
587 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200588 worker_set_state(worker, BW_ST_CONN_CLIENT);
589
Harald Welte150d6d62018-10-03 23:07:47 +0200590 if (worker_try_slotmap(worker) >= 0)
591 res = ResultCode_ok;
592 else
Harald Welte3e689872018-09-24 14:52:56 +0200593 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200594
Harald Welte3e689872018-09-24 14:52:56 +0200595 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100596 rc = worker_send_rspro(worker, resp);
597 if (rc < 0)
598 return rc;
599
600 if (res == ResultCode_ok)
601 rc = worker_send_atr(worker);
602
603 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100604
605respond_and_err:
606 if (res) {
607 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
608 worker_send_rspro(worker, resp);
609 }
610 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200611}
612
Harald Welte796a7492018-09-23 19:31:28 +0200613static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
614{
615 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200616 uint8_t rx_buf[1024];
617 DWORD rx_buf_len = sizeof(rx_buf);
618 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100619 struct client_slot clslot;
620 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100621 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200622
623 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
624
625 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
626 LOGW(worker, "Unexpected tpduModemToCaard\n");
627 return -104;
628 }
629
Harald Weltee1d32892019-03-27 20:47:42 +0100630 /* Validate that toBankSlot / fromClientSlot match our expectations */
631 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
632 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
633 if (!bank_slot_equals(&worker->slot, &bslot)) {
634 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
635 bslot.bank_id, bslot.slot_nr);
636 return -105;
637 }
638 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
639 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
640 clslot.client_id, clslot.slot_nr);
641 return -106;
642 }
Harald Welte796a7492018-09-23 19:31:28 +0200643
Harald Welte297d72e2019-03-28 18:42:35 +0100644 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
645 rx_buf, &rx_buf_len);
646 if (rc < 0)
647 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200648
649 /* encode response PDU and send it */
650 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
651 rx_buf, rx_buf_len);
652 worker_send_rspro(worker, pdu_resp);
653
654 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200655}
656
Harald Welte77911b02018-08-14 23:47:30 +0200657/* handle one incoming RSPRO message from a client inside a worker thread */
658static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
659{
Harald Weltecce2aad2018-08-16 14:44:37 +0200660 int rc = -100;
661
Harald Welte736e8312019-03-31 13:08:16 +0200662 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
663
Harald Welte77911b02018-08-14 23:47:30 +0200664 switch (pdu->msg.present) {
665 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200666 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200667 break;
668 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200669 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200670 break;
671 case RsproPDUchoice_PR_clientSlotStatusInd:
672 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200673 rc = 0;
674 break;
675 case RsproPDUchoice_PR_setAtrRes:
676 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200677 break;
678 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200679 rc = -101;
680 break;
Harald Welte77911b02018-08-14 23:47:30 +0200681 }
682
Harald Weltecce2aad2018-08-16 14:44:37 +0200683 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200684}
685
Harald Welte694df832018-10-03 22:47:52 +0200686static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
687{
688 struct timeval tout = { timeout_secs, 0 };
689 fd_set readset;
690
691 FD_ZERO(&readset);
692 FD_SET(fd, &readset);
693 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
694}
695
Harald Welte77911b02018-08-14 23:47:30 +0200696/* body of the main transceive loop */
697static int worker_transceive_loop(struct bankd_worker *worker)
698{
699 struct ipaccess_head *hh;
700 struct ipaccess_head_ext *hh_ext;
701 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
702 asn_dec_rval_t rval;
703 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200704 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200705
Harald Welte00a96732019-03-11 17:18:02 +0100706restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200707 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100708 if (rc == -1 && errno == EINTR) {
709 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
710 return -23;
711 goto restart_wait;
712 } else if (rc < 0)
713 return rc;
714 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200715 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200716 switch (worker->state) {
717 case BW_ST_CONN_CLIENT_WAIT_MAP:
718 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100719 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200720 break;
721 case BW_ST_CONN_CLIENT_MAPPED:
722 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100723 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200724 break;
725 default:
726 OSMO_ASSERT(0);
727 }
Harald Welte1f699b42019-03-28 13:41:08 +0100728 if (rc == 0)
729 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200730 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200731 return 0;
732 };
733
Harald Welte77911b02018-08-14 23:47:30 +0200734 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100735 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200736 if (rc < 0)
737 return rc;
738 data_len = rc;
739
740 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200741 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200742 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200743 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200744 }
Harald Welte77911b02018-08-14 23:47:30 +0200745
Harald Welte5a3613a2018-10-11 12:56:21 +0200746 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100747 switch (hh->data[0]) {
748 case IPAC_MSGT_PING:
749 return ipa_ccm_send_pong(worker->client.fd);
750 default:
751 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
752 break;
753 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200754 return 0;
755 }
756
Harald Welte77911b02018-08-14 23:47:30 +0200757 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200758 if (data_len < sizeof(*hh_ext)) {
759 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200760 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200761 }
Harald Welte77911b02018-08-14 23:47:30 +0200762 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200763 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
764 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200765 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200766 }
Harald Welte77911b02018-08-14 23:47:30 +0200767
768 /* 2) ASN1 BER decode of the message */
769 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200770 if (rval.code != RC_OK) {
771 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200772 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200773 }
Harald Welte77911b02018-08-14 23:47:30 +0200774
775 /* 3) handling of the message, possibly resulting in PCSC commands */
776 rc = worker_handle_rspro(worker, pdu);
777 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200778 if (rc < 0) {
779 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200780 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200781 }
Harald Welte77911b02018-08-14 23:47:30 +0200782
783 /* everything OK if we reach here */
784 return 0;
785}
786
Harald Welted6dfb8c2018-08-16 14:46:53 +0200787/* obtain an ascii representation of the client IP/port */
788static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
789{
790 char hostbuf[32], portbuf[32];
791 int rc;
792
793 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
794 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
795 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
796 if (rc != 0) {
797 out[0] = '\0';
798 return -1;
799 }
800 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
801 return 0;
802}
803
Harald Welte77911b02018-08-14 23:47:30 +0200804/* worker thread main function */
805static void *worker_main(void *arg)
806{
Harald Welte77911b02018-08-14 23:47:30 +0200807 void *top_ctx;
808 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200809
Harald Welte00a96732019-03-11 17:18:02 +0100810 g_worker = (struct bankd_worker *) arg;
811
Harald Welte00a96732019-03-11 17:18:02 +0100812 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200813
Harald Welte77911b02018-08-14 23:47:30 +0200814 /* not permitted in multithreaded environment */
815 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100816 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
817 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200818
Harald Welte286a2be2019-03-11 17:36:58 +0100819 /* set the thread name */
820 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
821 pthread_setname_np(pthread_self(), g_worker->name);
822
Harald Welte77911b02018-08-14 23:47:30 +0200823 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100824 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200825
826 /* we continuously perform the same loop here, recycling the worker thread
827 * once the client connection is gone or we have some trouble with the card/reader */
828 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200829 char buf[128];
830
Harald Welte00a96732019-03-11 17:18:02 +0100831 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200832
Harald Welte00a96732019-03-11 17:18:02 +0100833 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200834 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100835 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
836 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200837 if (rc < 0) {
838 continue;
839 }
Harald Welte00a96732019-03-11 17:18:02 +0100840 g_worker->client.fd = rc;
841 worker_client_addrstr(buf, sizeof(buf), g_worker);
842 LOGW(g_worker, "Accepted connection from %s\n", buf);
843 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200844
845 /* run the main worker transceive loop body until there was some error */
846 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100847 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200848 if (rc < 0)
849 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100850 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
851 break;
Harald Welte77911b02018-08-14 23:47:30 +0200852 }
853
Harald Welte00a96732019-03-11 17:18:02 +0100854 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200855
Harald Welte77911b02018-08-14 23:47:30 +0200856 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100857 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100858 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100859 if (g_worker->reader.name)
860 g_worker->reader.name = NULL;
861 if (g_worker->client.fd >= 0)
862 close(g_worker->client.fd);
863 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
864 g_worker->client.fd = -1;
865 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200866 }
867
868 pthread_cleanup_pop(1);
869 talloc_free(top_ctx);
870 pthread_exit(NULL);
871}