blob: 82ed8c01c7325b7b5ca39b7c821fe050fe061522 [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);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100179 /* check if map exists */
180 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
181 if (map) {
182 if (client_slot_equals(&map->client, &cs)) {
183 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
184 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
185 goto send_resp;
186 } else {
187 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
188 bankd_srvc_remove_mapping(map);
189 }
190 }
Harald Welte454f5e22019-03-09 21:38:34 +0100191 /* Add a new mapping */
192 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100193 if (!map) {
194 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100195 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100196 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100197 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
198 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100199send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100200 server_conn_send_rspro(srvc, resp);
201 break;
202 case RsproPDUchoice_PR_removeMappingReq:
203 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200204 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100205 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100206 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100207 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200208 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100209 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100210 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100211 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100212 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100213 rspro2bank_slot(&bs, &rreq->bank);
214 /* Remove a mapping */
215 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100216 if (!map) {
217 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100218 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100219 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100220 rspro2client_slot(&cs, &rreq->client);
221 if (!client_slot_equals(&map->client, &cs)) {
222 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
223 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
224 } else {
225 LOGPFSM(srvc->fi, "removing slotmap\n");
226 bankd_srvc_remove_mapping(map);
227 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
228 }
Harald Welte454f5e22019-03-09 21:38:34 +0100229 }
230 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100231 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100232 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100233 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100234 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
235 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100236 return -1;
237 }
238
239 return 0;
240}
241
Harald Welte5bae20b2019-04-01 09:36:42 +0200242static void printf_help()
243{
244 printf(
245" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100246" -V --version Print the version of the program\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200247" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
248" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
249" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200250" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200251" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
252" connections (default: INADDR_ANY)\n"
253" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
254" connectionss (default: 9999)\n"
255 );
256}
257
258static int g_bind_port = 9999;
259static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100260
Harald Welte707c85a2019-03-09 12:56:35 +0100261void handle_options(int argc, char **argv)
262{
Harald Welte5bae20b2019-04-01 09:36:42 +0200263 while (1) {
264 int option_index = 0, c;
265 static const struct option long_options[] = {
266 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100267 { "version", 0, 0, 'V' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200268 { "server-host", 1, 0, 'i' },
269 { "server-port", 1, 0, 'p' },
270 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200271 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200272 { "component-name", 1, 0, 'N' },
273 { "bind-ip", 1, 0, 'I' },
274 { "bind-port", 1, 0, 'P' },
275 { 0, 0, 0, 0 }
276 };
277
Harald Weltecd7fcd72019-12-03 21:29:07 +0100278 c = getopt_long(argc, argv, "hVi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200279 if (c == -1)
280 break;
281
282 switch (c) {
283 case 'h':
284 printf_help();
285 exit(0);
286 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100287 case 'V':
288 printf("osmo-remsim-bankd version %s\n", VERSION);
289 exit(0);
290 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200291 case 'i':
292 g_bankd->srvc.server_host = optarg;
293 break;
294 case 'p':
295 g_bankd->srvc.server_port = atoi(optarg);
296 break;
297 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200298 g_bankd->srvc.bankd.bank_id = atoi(optarg);
299 break;
300 case 'n':
301 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200302 break;
303 case 'N':
304 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
305 break;
306 case 'I':
307 g_bind_ip = optarg;
308 break;
309 case 'P':
310 g_bind_port = atoi(optarg);
311 break;
312 }
313 }
Harald Welte707c85a2019-03-09 12:56:35 +0100314}
315
Harald Welte77911b02018-08-14 23:47:30 +0200316int main(int argc, char **argv)
317{
Harald Weltef4b16f12019-03-09 20:58:17 +0100318 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200319 int i, rc;
320
Harald Weltef4b16f12019-03-09 20:58:17 +0100321 g_bankd = talloc_zero(NULL, struct bankd);
322 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200323
Harald Weltef4b16f12019-03-09 20:58:17 +0100324 bankd_init(g_bankd);
325
326 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100327 srvc->server_host = "localhost";
328 srvc->server_port = 9998;
329 srvc->handle_rx = bankd_srvc_handle_rx;
330 srvc->own_comp_id.type = ComponentType_remsimBankd;
331 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
332 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
333 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
334
335 handle_options(argc, argv);
336
Harald Welte25075972019-03-11 17:33:17 +0100337 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100338 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100339 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100340
Harald Weltee89fecd2019-04-04 09:23:13 +0200341 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
342 * read once during bankd initialization, when the worker threads haven't
343 * started yet */
344 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
345
Harald Welte707c85a2019-03-09 12:56:35 +0100346 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100347 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100348 if (rc < 0) {
349 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
350 exit(1);
351 }
352
353 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200354 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 +0200355 if (rc < 0)
356 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100357 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200358
Harald Weltea0f39502019-03-09 20:59:34 +0100359 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200360 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200361 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100362 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200363 if (!w)
364 exit(21);
365 }
366
367 while (1) {
368 if (terminate)
369 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100370 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200371 }
372
Harald Weltef4b16f12019-03-09 20:58:17 +0100373 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200374 exit(0);
375}
376
377
378
379/***********************************************************************
380 * bankd worker thread
381 ***********************************************************************/
382
Harald Welte00a96732019-03-11 17:18:02 +0100383static __thread struct bankd_worker *g_worker;
384
Harald Welte8d858292018-08-15 23:36:46 +0200385struct value_string worker_state_names[] = {
386 { BW_ST_INIT, "INIT" },
387 { BW_ST_ACCEPTING, "ACCEPTING" },
388 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
389 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200390 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200391 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
392 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100393 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200394 { 0, NULL }
395};
396
Harald Welte1f699b42019-03-28 13:41:08 +0100397static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
398
Harald Welte8d858292018-08-15 23:36:46 +0200399static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
400{
401 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
402 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200403 worker->timeout = 0;
404}
405
406static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
407 unsigned int timeout_secs)
408{
409 LOGW(worker, "Changing state to %s (timeout=%u)\n",
410 get_value_string(worker_state_names, new_state), timeout_secs);
411 worker->state = new_state;
412 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200413}
Harald Welte77911b02018-08-14 23:47:30 +0200414
Harald Welte00a96732019-03-11 17:18:02 +0100415/* signal handler for receiving SIGMAPDEL from main thread */
416static void handle_sig_mapdel(int sig)
417{
418 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
419 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Welte602b6f72019-12-04 21:51:02 +0100420 g_worker->slot.bank_id = 0xffff;
421 g_worker->slot.slot_nr = 0xffff;
Harald Welte00a96732019-03-11 17:18:02 +0100422 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
423}
424
Harald Welte25075972019-03-11 17:33:17 +0100425static void handle_sig_usr1(int sig)
426{
427 OSMO_ASSERT(sig == SIGUSR1);
428
429 if (pthread_equal(g_bankd->main, pthread_self())) {
430 struct bankd_worker *worker;
431 /* main thread */
432 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200433 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100434
435 /* iterate over worker threads and ask them to dump their talloc state */
436 pthread_mutex_lock(&g_bankd->workers_mutex);
437 llist_for_each_entry(worker, &g_bankd->workers, list) {
438 pthread_kill(worker->thread, SIGUSR1);
439 }
440 pthread_mutex_unlock(&g_bankd->workers_mutex);
441 } else {
442 /* worker thread */
443 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200444 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100445 }
446}
447
Harald Welte77911b02018-08-14 23:47:30 +0200448static void worker_cleanup(void *arg)
449{
450 struct bankd_worker *worker = (struct bankd_worker *) arg;
451 struct bankd *bankd = worker->bankd;
452
453 /* FIXME: should we still do this? in the thread ?!? */
454 pthread_mutex_lock(&bankd->workers_mutex);
455 llist_del(&worker->list);
456 talloc_free(worker); /* FIXME: is this safe? */
457 pthread_mutex_unlock(&bankd->workers_mutex);
458}
459
Harald Welteaf614732018-08-17 22:10:05 +0200460static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200461{
Harald Welte297d72e2019-03-28 18:42:35 +0100462 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200463
Harald Welte150d6d62018-10-03 23:07:47 +0200464 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
465
Harald Welte694df832018-10-03 22:47:52 +0200466 if (!worker->reader.name) {
467 /* resolve PC/SC reader name from slot_id -> name map */
468 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
469 if (!worker->reader.name) {
470 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
471 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100472 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200473 }
474 }
Harald Welte45c948c2018-09-23 19:26:52 +0200475 OSMO_ASSERT(worker->reader.name);
476
Harald Welte297d72e2019-03-28 18:42:35 +0100477 rc = worker->ops->open_card(worker);
478 if (rc < 0)
479 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100480
Harald Welte57593f02018-09-23 19:30:31 +0200481 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200482 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200483
Harald Welteaf614732018-08-17 22:10:05 +0200484 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200485}
Harald Welte77911b02018-08-14 23:47:30 +0200486
487
Harald Welte00a96732019-03-11 17:18:02 +0100488static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200489{
490 struct ipaccess_head *hh;
491 uint16_t len;
492 int needed, rc;
493
494 if (buf_size < sizeof(*hh))
495 return -1;
496
497 hh = (struct ipaccess_head *) buf;
498
Harald Welte00a96732019-03-11 17:18:02 +0100499 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
500 * in case of a signal being received */
501
502restart_hdr:
503 /* 1) blocking recv from the socket (IPA header) */
504 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
505 if (rc == -1 && errno == EINTR) {
506 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
507 return -23;
508 goto restart_hdr;
509 } else if (rc < 0)
510 return rc;
511 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200512 return -2;
513
514 len = ntohs(hh->len);
515 needed = len; //- sizeof(*hh);
516
Harald Welte00a96732019-03-11 17:18:02 +0100517restart_body:
518 /* 2) blocking recv from the socket (payload) */
519 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
520 if (rc == -1 && errno == EINTR) {
521 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
522 return -23;
523 goto restart_body;
524 } else if (rc < 0)
525 return rc;
526 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200527 return -3;
528
529 return len;
530}
531
Harald Welte796a7492018-09-23 19:31:28 +0200532static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
533{
534 struct msgb *msg = rspro_enc_msg(pdu);
535 int rc;
536
537 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100538 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200539 LOGW(worker, "error encoding RSPRO\n");
540 return -1;
541 }
542
Harald Weltefd471192018-09-24 14:51:14 +0200543 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200544 /* prepend the header */
545 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200546 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200547
548 /* actually send it through the socket */
549 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
550 if (rc == msgb_length(msg))
551 rc = 0;
552 else {
553 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
554 rc = -1;
555 }
556
557 msgb_free(msg);
558
559 return rc;
560}
561
Harald Welte150d6d62018-10-03 23:07:47 +0200562/* attempt to obtain slot-map */
563static int worker_try_slotmap(struct bankd_worker *worker)
564{
Harald Weltecbd18962019-03-03 19:02:38 +0100565 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200566
Harald Weltecbd18962019-03-03 19:02:38 +0100567 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200568 if (!slmap) {
569 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
570 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
571 /* check in 10s if the map has been installed meanwhile by main thread */
572 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
573 return -1;
574 } else {
575 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
576 slmap->client.client_id, slmap->client.slot_nr,
577 slmap->bank.bank_id, slmap->bank.slot_nr);
578 worker->slot = slmap->bank;
579 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
580 return worker_open_card(worker);
581 }
582}
583
Harald Welte1f699b42019-03-28 13:41:08 +0100584/* inform the remote end (client) about the (new) ATR */
585static int worker_send_atr(struct bankd_worker *worker)
586{
587 RsproPDU_t *set_atr;
588 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
589 worker->client.clslot.slot_nr,
590 worker->card.atr, worker->card.atr_len);
591 if (!set_atr)
592 return -1;
593 return worker_send_rspro(worker, set_atr);
594}
Harald Welte150d6d62018-10-03 23:07:47 +0200595
Harald Weltecce2aad2018-08-16 14:44:37 +0200596static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
597{
Harald Welteaf614732018-08-17 22:10:05 +0200598 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100599 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200600 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100601 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200602
Harald Weltecce2aad2018-08-16 14:44:37 +0200603 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
604
Harald Weltecce2aad2018-08-16 14:44:37 +0200605 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
606 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
607 /* FIXME: store somewhere? */
608
609 if (worker->state != BW_ST_CONN_WAIT_ID) {
610 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100611 rc = -102;
612 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200613 }
614
Harald Welte371d0262018-08-16 15:23:58 +0200615 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200616 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100617 res = ResultCode_illegalClientId;
618 rc = -103;
619 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200620 }
Harald Welte371d0262018-08-16 15:23:58 +0200621 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
622 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200623 worker_set_state(worker, BW_ST_CONN_CLIENT);
624
Harald Welte150d6d62018-10-03 23:07:47 +0200625 if (worker_try_slotmap(worker) >= 0)
626 res = ResultCode_ok;
627 else
Harald Welte3e689872018-09-24 14:52:56 +0200628 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200629
Harald Welte3e689872018-09-24 14:52:56 +0200630 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100631 rc = worker_send_rspro(worker, resp);
632 if (rc < 0)
633 return rc;
634
635 if (res == ResultCode_ok)
636 rc = worker_send_atr(worker);
637
638 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100639
640respond_and_err:
641 if (res) {
642 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
643 worker_send_rspro(worker, resp);
644 }
645 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200646}
647
Harald Welte796a7492018-09-23 19:31:28 +0200648static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
649{
650 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200651 uint8_t rx_buf[1024];
652 DWORD rx_buf_len = sizeof(rx_buf);
653 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100654 struct client_slot clslot;
655 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100656 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200657
658 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
659
660 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
661 LOGW(worker, "Unexpected tpduModemToCaard\n");
662 return -104;
663 }
664
Harald Weltee1d32892019-03-27 20:47:42 +0100665 /* Validate that toBankSlot / fromClientSlot match our expectations */
666 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
667 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
668 if (!bank_slot_equals(&worker->slot, &bslot)) {
669 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
670 bslot.bank_id, bslot.slot_nr);
671 return -105;
672 }
673 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
674 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
675 clslot.client_id, clslot.slot_nr);
676 return -106;
677 }
Harald Welte796a7492018-09-23 19:31:28 +0200678
Harald Welte297d72e2019-03-28 18:42:35 +0100679 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
680 rx_buf, &rx_buf_len);
681 if (rc < 0)
682 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200683
684 /* encode response PDU and send it */
685 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
686 rx_buf, rx_buf_len);
687 worker_send_rspro(worker, pdu_resp);
688
689 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200690}
691
Harald Welte77911b02018-08-14 23:47:30 +0200692/* handle one incoming RSPRO message from a client inside a worker thread */
693static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
694{
Harald Weltecce2aad2018-08-16 14:44:37 +0200695 int rc = -100;
696
Harald Welte736e8312019-03-31 13:08:16 +0200697 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
698
Harald Welte77911b02018-08-14 23:47:30 +0200699 switch (pdu->msg.present) {
700 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200701 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200702 break;
703 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200704 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200705 break;
706 case RsproPDUchoice_PR_clientSlotStatusInd:
707 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200708 rc = 0;
709 break;
710 case RsproPDUchoice_PR_setAtrRes:
711 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200712 break;
713 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200714 rc = -101;
715 break;
Harald Welte77911b02018-08-14 23:47:30 +0200716 }
717
Harald Weltecce2aad2018-08-16 14:44:37 +0200718 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200719}
720
Harald Welte694df832018-10-03 22:47:52 +0200721static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
722{
723 struct timeval tout = { timeout_secs, 0 };
724 fd_set readset;
725
726 FD_ZERO(&readset);
727 FD_SET(fd, &readset);
728 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
729}
730
Harald Welte77911b02018-08-14 23:47:30 +0200731/* body of the main transceive loop */
732static int worker_transceive_loop(struct bankd_worker *worker)
733{
734 struct ipaccess_head *hh;
735 struct ipaccess_head_ext *hh_ext;
736 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
737 asn_dec_rval_t rval;
738 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200739 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200740
Harald Welte00a96732019-03-11 17:18:02 +0100741restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200742 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100743 if (rc == -1 && errno == EINTR) {
744 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
745 return -23;
746 goto restart_wait;
747 } else if (rc < 0)
748 return rc;
749 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200750 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200751 switch (worker->state) {
752 case BW_ST_CONN_CLIENT_WAIT_MAP:
753 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100754 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200755 break;
756 case BW_ST_CONN_CLIENT_MAPPED:
757 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100758 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200759 break;
760 default:
761 OSMO_ASSERT(0);
762 }
Harald Welte1f699b42019-03-28 13:41:08 +0100763 if (rc == 0)
764 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200765 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200766 return 0;
767 };
768
Harald Welte77911b02018-08-14 23:47:30 +0200769 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100770 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200771 if (rc < 0)
772 return rc;
773 data_len = rc;
774
775 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200776 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200777 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200778 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200779 }
Harald Welte77911b02018-08-14 23:47:30 +0200780
Harald Welte5a3613a2018-10-11 12:56:21 +0200781 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100782 switch (hh->data[0]) {
783 case IPAC_MSGT_PING:
784 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100785 case IPAC_MSGT_ID_ACK:
786 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100787 default:
788 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
789 break;
790 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200791 return 0;
792 }
793
Harald Welte77911b02018-08-14 23:47:30 +0200794 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200795 if (data_len < sizeof(*hh_ext)) {
796 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200797 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200798 }
Harald Welte77911b02018-08-14 23:47:30 +0200799 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200800 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
801 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200802 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200803 }
Harald Welte77911b02018-08-14 23:47:30 +0200804
805 /* 2) ASN1 BER decode of the message */
806 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200807 if (rval.code != RC_OK) {
808 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200809 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200810 }
Harald Welte77911b02018-08-14 23:47:30 +0200811
812 /* 3) handling of the message, possibly resulting in PCSC commands */
813 rc = worker_handle_rspro(worker, pdu);
814 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200815 if (rc < 0) {
816 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200817 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200818 }
Harald Welte77911b02018-08-14 23:47:30 +0200819
820 /* everything OK if we reach here */
821 return 0;
822}
823
Harald Welted6dfb8c2018-08-16 14:46:53 +0200824/* obtain an ascii representation of the client IP/port */
825static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
826{
827 char hostbuf[32], portbuf[32];
828 int rc;
829
830 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
831 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
832 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
833 if (rc != 0) {
834 out[0] = '\0';
835 return -1;
836 }
837 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
838 return 0;
839}
840
Harald Welte77911b02018-08-14 23:47:30 +0200841/* worker thread main function */
842static void *worker_main(void *arg)
843{
Harald Welte77911b02018-08-14 23:47:30 +0200844 void *top_ctx;
845 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200846
Harald Welte00a96732019-03-11 17:18:02 +0100847 g_worker = (struct bankd_worker *) arg;
848
Harald Welte00a96732019-03-11 17:18:02 +0100849 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200850
Harald Welte77911b02018-08-14 23:47:30 +0200851 /* not permitted in multithreaded environment */
852 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100853 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
854 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200855
Harald Welte286a2be2019-03-11 17:36:58 +0100856 /* set the thread name */
857 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
858 pthread_setname_np(pthread_self(), g_worker->name);
859
Harald Welte77911b02018-08-14 23:47:30 +0200860 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100861 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200862
Harald Welte602b6f72019-12-04 21:51:02 +0100863 g_worker->slot.bank_id = 0xffff;
864 g_worker->slot.slot_nr = 0xffff;
865
Harald Welte77911b02018-08-14 23:47:30 +0200866 /* we continuously perform the same loop here, recycling the worker thread
867 * once the client connection is gone or we have some trouble with the card/reader */
868 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200869 char buf[128];
870
Harald Welte00a96732019-03-11 17:18:02 +0100871 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200872
Harald Welte00a96732019-03-11 17:18:02 +0100873 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200874 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100875 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
876 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200877 if (rc < 0) {
878 continue;
879 }
Harald Welte00a96732019-03-11 17:18:02 +0100880 g_worker->client.fd = rc;
881 worker_client_addrstr(buf, sizeof(buf), g_worker);
882 LOGW(g_worker, "Accepted connection from %s\n", buf);
883 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200884
885 /* run the main worker transceive loop body until there was some error */
886 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100887 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200888 if (rc < 0)
889 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100890 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
891 break;
Harald Welte77911b02018-08-14 23:47:30 +0200892 }
893
Harald Welte00a96732019-03-11 17:18:02 +0100894 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200895
Harald Welte77911b02018-08-14 23:47:30 +0200896 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100897 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100898 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100899 if (g_worker->reader.name)
900 g_worker->reader.name = NULL;
901 if (g_worker->client.fd >= 0)
902 close(g_worker->client.fd);
903 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
904 g_worker->client.fd = -1;
905 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200906 }
907
908 pthread_cleanup_pop(1);
909 talloc_free(top_ctx);
910 pthread_exit(NULL);
911}