blob: bf7ca822e9b8490ad4196b02f94c653d104781e5 [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 *
Harald Welte3dcdd202019-03-09 13:06:46 +010017 */
18
Harald Welte31c9eca2018-10-03 21:03:34 +020019#define _GNU_SOURCE
20
Harald Welte77911b02018-08-14 23:47:30 +020021#include <stdio.h>
22#include <stdlib.h>
23#include <stdint.h>
Harald Welte00a96732019-03-11 17:18:02 +010024#include <signal.h>
Harald Welte77911b02018-08-14 23:47:30 +020025#include <unistd.h>
Harald Welte707c85a2019-03-09 12:56:35 +010026#include <errno.h>
Harald Welte5bae20b2019-04-01 09:36:42 +020027#include <getopt.h>
Harald Welte77911b02018-08-14 23:47:30 +020028
29#include <pthread.h>
30
Harald Welted6dfb8c2018-08-16 14:46:53 +020031#include <sys/socket.h>
32#include <netdb.h>
33
Harald Welte12534e72018-08-15 23:37:29 +020034#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020035#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020036#include <osmocom/core/logging.h>
37#include <osmocom/core/application.h>
Harald Welte73bbd542021-12-08 20:39:55 +010038#include <osmocom/core/fsm.h>
Harald Welte77911b02018-08-14 23:47:30 +020039
40#include <osmocom/gsm/ipa.h>
41#include <osmocom/gsm/protocol/ipaccess.h>
42
Kévin Redonff5db6e2018-10-11 17:16:18 +020043#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020044#include <osmocom/rspro/RsproPDU.h>
45
46#include "bankd.h"
Harald Welte3cded632019-03-09 12:59:41 +010047#include "rspro_client_fsm.h"
Harald Welte61d98e92019-03-03 15:43:07 +010048#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020049#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020050
Harald Welte00a96732019-03-11 17:18:02 +010051/* signal indicates to worker thread that its map has been deleted */
52#define SIGMAPDEL SIGRTMIN+1
Harald Welte0fd77a52019-12-05 22:05:39 +010053#define SIGMAPADD SIGRTMIN+2
Harald Welte25075972019-03-11 17:33:17 +010054
55static void handle_sig_usr1(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010056static void handle_sig_mapdel(int sig);
Harald Welte0fd77a52019-12-05 22:05:39 +010057static void handle_sig_mapadd(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010058
Harald Welte77911b02018-08-14 23:47:30 +020059__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010060struct bankd *g_bankd;
Harald Welte25075972019-03-11 17:33:17 +010061static void *g_tall_ctx;
Harald Welteeea631b2022-05-03 15:14:19 +020062static char g_hostname[256];
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);
Harald Welte73bbd542021-12-08 20:39:55 +010076 log_set_print_level(osmo_stderr_target, 1);
77 log_set_print_category(osmo_stderr_target, 1);
78 log_set_print_category_hex(osmo_stderr_target, 0);
79 osmo_fsm_log_addr(0);
Harald Weltea9d7ad12021-12-08 21:09:12 +010080 log_set_print_tid(osmo_stderr_target, 1);
81 log_enable_multithread();
Harald Weltef94b9ee2018-09-25 15:04:21 +020082
Harald Welte43ab79f2018-10-03 23:34:21 +020083 asn_debug = 0;
84
Harald Weltea9bb9a92022-05-03 15:28:02 +020085 /* initialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010086 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020087 INIT_LLIST_HEAD(&bankd->workers);
88 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020089
Harald Weltea0f39502019-03-09 20:59:34 +010090 /* set some defaults, overridden by commandline/config */
Harald Welte2513d812019-04-01 21:03:02 +020091 bankd->srvc.bankd.bank_id = 1;
92 bankd->srvc.bankd.num_slots = 8;
Harald Weltea0f39502019-03-09 20:59:34 +010093
Harald Weltef1dd1622018-09-24 14:54:23 +020094 bankd->comp_id.type = ComponentType_remsimBankd;
Harald Welteeea631b2022-05-03 15:14:19 +020095 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, g_hostname);
Harald Weltef1dd1622018-09-24 14:54:23 +020096 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
97 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
98 /* FIXME: other members of app_comp_id */
99
Harald Welte45c948c2018-09-23 19:26:52 +0200100 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
Harald Welte628672b2022-07-13 15:20:13 +0200101
102 bankd->cfg.permit_shared_pcsc = false;
Harald Welte77911b02018-08-14 23:47:30 +0200103}
104
105/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200106static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200107{
108 struct bankd_worker *worker;
109 int rc;
110
111 worker = talloc_zero(bankd, struct bankd_worker);
112 if (!worker)
113 return NULL;
114
115 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200116 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100117 worker->ops = &pcsc_driver_ops;
James Tavaresf74d1da2022-11-14 00:21:12 -0500118 worker->last_vccPresent = true; /* allow cold reset should first indication be false */
119 worker->last_resetActive = false; /* allow warm reset should first indication be true */
Harald Welte77911b02018-08-14 23:47:30 +0200120
121 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
122
123 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
124 if (rc != 0) {
125 talloc_free(worker);
126 return NULL;
127 }
128
129 pthread_mutex_lock(&bankd->workers_mutex);
130 llist_add_tail(&worker->list, &bankd->workers);
131 pthread_mutex_unlock(&bankd->workers_mutex);
132
133 return worker;
134}
135
136static bool terminate = false;
137
Harald Weltea4967832019-12-05 22:04:57 +0100138/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
139static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
140{
141 struct bankd_worker *worker;
142 pthread_mutex_lock(&g_bankd->workers_mutex);
143 llist_for_each_entry(worker, &g_bankd->workers, list) {
144 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
145 continue;
146 if (cs && (cs->client_id != worker->client.clslot.client_id ||
147 cs->slot_nr != worker->client.clslot.slot_nr))
148 continue;
149
150 pthread_kill(worker->thread, sig);
151 break;
152 }
153 pthread_mutex_unlock(&g_bankd->workers_mutex);
154}
155
Harald Weltec650a4d2019-12-04 15:02:27 +0100156/* Remove a mapping */
157static void bankd_srvc_remove_mapping(struct slot_mapping *map)
158{
159 struct bank_slot bs = map->bank;
160
161 slotmap_del(g_bankd->slotmaps, map);
162
163 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100164 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100165}
166
Harald Welte707c85a2019-03-09 12:56:35 +0100167/* handle incoming messages from server */
168static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
169{
Harald Welte454f5e22019-03-09 21:38:34 +0100170 const CreateMappingReq_t *creq = NULL;
171 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100172 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100173 struct slot_mapping *map;
174 struct bank_slot bs;
175 struct client_slot cs;
176 RsproPDU_t *resp;
177
Harald Welte079c0682022-04-27 09:26:52 +0200178 LOGPFSML(srvc->fi, LOGL_DEBUG, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100179
180 switch (pdu->msg.present) {
181 case RsproPDUchoice_PR_connectBankRes:
Harald Welte5ae0f312022-04-25 15:53:04 +0200182 if (pdu->msg.choice.connectBankRes.identity.type != ComponentType_remsimServer) {
183 LOGPFSML(srvc->fi, LOGL_ERROR, "Server connection to a ComponentType(%ld) != RemsimServer? "
184 "Check your IP/Port configuration\n",
185 pdu->msg.choice.connectBankRes.identity.type);
186 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_DISCONNECT, NULL);
187 return -1;
188 }
Harald Welte707c85a2019-03-09 12:56:35 +0100189 /* Store 'identity' of server in srvc->peer_comp_id */
190 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
191 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
192 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100193 case RsproPDUchoice_PR_createMappingReq:
194 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200195 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100196 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200197 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100198 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200199 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100200 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200201 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100202 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100203 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100204 rspro2bank_slot(&bs, &creq->bank);
205 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100206 /* check if map exists */
207 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
208 if (map) {
209 if (client_slot_equals(&map->client, &cs)) {
210 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
211 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
212 goto send_resp;
213 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200214 LOGPFSML(srvc->fi, LOGL_NOTICE, "implicitly removing slotmap\n");
Harald Weltee6fa46a2019-12-04 15:06:33 +0100215 bankd_srvc_remove_mapping(map);
216 }
217 }
Harald Welte454f5e22019-03-09 21:38:34 +0100218 /* Add a new mapping */
219 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100220 if (!map) {
221 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100222 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100223 } else {
224 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100225 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100226 }
Harald Welte454f5e22019-03-09 21:38:34 +0100227 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100228send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100229 server_conn_send_rspro(srvc, resp);
230 break;
231 case RsproPDUchoice_PR_removeMappingReq:
232 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200233 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100234 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100235 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100236 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200237 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100238 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100239 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100240 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100241 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100242 rspro2bank_slot(&bs, &rreq->bank);
243 /* Remove a mapping */
244 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100245 if (!map) {
246 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100247 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100248 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100249 rspro2client_slot(&cs, &rreq->client);
250 if (!client_slot_equals(&map->client, &cs)) {
Harald Welte079c0682022-04-27 09:26:52 +0200251 LOGPFSML(srvc->fi, LOGL_NOTICE, "ClientId in removeMappingReq != map\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100252 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
253 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200254 LOGPFSML(srvc->fi, LOGL_INFO, "removing slotmap\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100255 bankd_srvc_remove_mapping(map);
256 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
257 }
Harald Welte454f5e22019-03-09 21:38:34 +0100258 }
259 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100260 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100261 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100262 case RsproPDUchoice_PR_resetStateReq:
263 /* delete all slotmaps */
264 slotmap_del_all(g_bankd->slotmaps);
265 /* notify all workers about maps having disappeared */
266 pthread_mutex_lock(&g_bankd->workers_mutex);
267 llist_for_each_entry(worker, &g_bankd->workers, list) {
268 pthread_kill(worker->thread, SIGMAPDEL);
269 }
270 pthread_mutex_unlock(&g_bankd->workers_mutex);
271 /* send response to server */
272 resp = rspro_gen_ResetStateRes(ResultCode_ok);
273 server_conn_send_rspro(srvc, resp);
274 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100275 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100276 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
277 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100278 return -1;
279 }
280
281 return 0;
282}
283
Harald Welte5bae20b2019-04-01 09:36:42 +0200284static void printf_help()
285{
286 printf(
287" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100288" -V --version Print the version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +0200289" -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200290" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
291" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
Harald Welte55f12b82022-01-16 14:34:12 +0100292" -b --bank-id <1-1023> Bank Identifier of this SIM bank (default: 1)\n"
293" -n --num-slots <1-1023> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200294" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
295" connections (default: INADDR_ANY)\n"
296" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
297" connectionss (default: 9999)\n"
Harald Welte628672b2022-07-13 15:20:13 +0200298" -s --permit-shared-pcsc Permit SHARED access to PC/SC readers (default: exclusive)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200299 );
300}
301
302static int g_bind_port = 9999;
303static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100304
Harald Weltef8c6eeb2021-12-08 20:33:00 +0100305static void handle_options(int argc, char **argv)
Harald Welte707c85a2019-03-09 12:56:35 +0100306{
Harald Welte5bae20b2019-04-01 09:36:42 +0200307 while (1) {
308 int option_index = 0, c;
309 static const struct option long_options[] = {
310 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100311 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +0200312 { "debug", 1, 0, 'd' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200313 { "server-host", 1, 0, 'i' },
314 { "server-port", 1, 0, 'p' },
315 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200316 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200317 { "component-name", 1, 0, 'N' },
318 { "bind-ip", 1, 0, 'I' },
319 { "bind-port", 1, 0, 'P' },
Harald Welte628672b2022-07-13 15:20:13 +0200320 { "permit-shared-pcsc", 0, 0, 's' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200321 { 0, 0, 0, 0 }
322 };
323
James Tavares2c0a5122022-11-13 23:22:22 -0500324 c = getopt_long(argc, argv, "hVd:i:p:o:b:n:N:I:P:s", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200325 if (c == -1)
326 break;
327
328 switch (c) {
329 case 'h':
330 printf_help();
331 exit(0);
332 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100333 case 'V':
334 printf("osmo-remsim-bankd version %s\n", VERSION);
335 exit(0);
336 break;
Harald Welte80a01102020-05-25 14:55:12 +0200337 case 'd':
338 log_parse_category_mask(osmo_stderr_target, optarg);
339 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200340 case 'i':
341 g_bankd->srvc.server_host = optarg;
342 break;
343 case 'p':
344 g_bankd->srvc.server_port = atoi(optarg);
345 break;
346 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200347 g_bankd->srvc.bankd.bank_id = atoi(optarg);
348 break;
349 case 'n':
350 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200351 break;
352 case 'N':
353 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
354 break;
355 case 'I':
356 g_bind_ip = optarg;
357 break;
358 case 'P':
359 g_bind_port = atoi(optarg);
360 break;
Harald Welte628672b2022-07-13 15:20:13 +0200361 case 's':
362 g_bankd->cfg.permit_shared_pcsc = true;
363 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200364 }
365 }
Harald Welte707c85a2019-03-09 12:56:35 +0100366}
367
Harald Welte77911b02018-08-14 23:47:30 +0200368int main(int argc, char **argv)
369{
Harald Weltef4b16f12019-03-09 20:58:17 +0100370 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200371 int i, rc;
372
Harald Weltef4b16f12019-03-09 20:58:17 +0100373 g_bankd = talloc_zero(NULL, struct bankd);
374 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200375
Harald Welteeea631b2022-05-03 15:14:19 +0200376 if (gethostname(g_hostname, sizeof(g_hostname)) < 0)
377 OSMO_STRLCPY_ARRAY(g_hostname, "unknown");
378
Harald Weltef4b16f12019-03-09 20:58:17 +0100379 bankd_init(g_bankd);
380
381 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100382 srvc->server_host = "localhost";
383 srvc->server_port = 9998;
384 srvc->handle_rx = bankd_srvc_handle_rx;
385 srvc->own_comp_id.type = ComponentType_remsimBankd;
Harald Welteeea631b2022-05-03 15:14:19 +0200386 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, g_hostname);
Harald Welte707c85a2019-03-09 12:56:35 +0100387 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
388 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
389
390 handle_options(argc, argv);
391
Harald Welte25075972019-03-11 17:33:17 +0100392 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100393 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100394 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100395 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100396
Harald Weltee89fecd2019-04-04 09:23:13 +0200397 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
398 * read once during bankd initialization, when the worker threads haven't
399 * started yet */
Harald Weltee9e505c2022-05-03 11:58:01 +0200400 rc = bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv");
401 if (rc)
402 exit(1);
Harald Weltee89fecd2019-04-04 09:23:13 +0200403
Harald Welte707c85a2019-03-09 12:56:35 +0100404 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100405 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100406 if (rc < 0) {
407 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
408 exit(1);
409 }
Harald Welted2192e22019-11-07 18:10:57 +0100410 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100411
412 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200413 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, g_bind_ip, g_bind_port, OSMO_SOCK_F_BIND);
Harald Weltee9e505c2022-05-03 11:58:01 +0200414 if (rc < 0) {
415 fprintf(stderr, "Unable to create TCP socket at %s:%d: %s\n",
416 g_bind_ip ? g_bind_ip : "INADDR_ANY", g_bind_port, strerror(errno));
Harald Welte12534e72018-08-15 23:37:29 +0200417 exit(1);
Harald Weltee9e505c2022-05-03 11:58:01 +0200418 }
Harald Weltef4b16f12019-03-09 20:58:17 +0100419 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200420
Harald Weltea0f39502019-03-09 20:59:34 +0100421 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200422 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200423 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100424 w = bankd_create_worker(g_bankd, i);
Harald Weltee9e505c2022-05-03 11:58:01 +0200425 if (!w) {
426 fprintf(stderr, "Error creating bankd worker thread\n");
Harald Welte77911b02018-08-14 23:47:30 +0200427 exit(21);
Harald Weltee9e505c2022-05-03 11:58:01 +0200428 }
Harald Welte77911b02018-08-14 23:47:30 +0200429 }
430
431 while (1) {
432 if (terminate)
433 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100434 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200435 }
436
Harald Weltef4b16f12019-03-09 20:58:17 +0100437 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200438 exit(0);
439}
440
441
442
443/***********************************************************************
444 * bankd worker thread
445 ***********************************************************************/
446
Harald Welte00a96732019-03-11 17:18:02 +0100447static __thread struct bankd_worker *g_worker;
448
Harald Welte8d858292018-08-15 23:36:46 +0200449struct value_string worker_state_names[] = {
450 { BW_ST_INIT, "INIT" },
451 { BW_ST_ACCEPTING, "ACCEPTING" },
452 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
453 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200454 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200455 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
456 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100457 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200458 { 0, NULL }
459};
460
Harald Welte1f699b42019-03-28 13:41:08 +0100461static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
462
Harald Welte8d858292018-08-15 23:36:46 +0200463static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
464{
465 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
466 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200467 worker->timeout = 0;
468}
469
470static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
471 unsigned int timeout_secs)
472{
473 LOGW(worker, "Changing state to %s (timeout=%u)\n",
474 get_value_string(worker_state_names, new_state), timeout_secs);
475 worker->state = new_state;
476 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200477}
Harald Welte77911b02018-08-14 23:47:30 +0200478
Harald Welte00a96732019-03-11 17:18:02 +0100479/* signal handler for receiving SIGMAPDEL from main thread */
480static void handle_sig_mapdel(int sig)
481{
482 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
483 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100484 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
485 g_worker->slot.bank_id = 0xffff;
486 g_worker->slot.slot_nr = 0xffff;
487 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
488 }
Harald Welte00a96732019-03-11 17:18:02 +0100489}
490
Harald Welte0fd77a52019-12-05 22:05:39 +0100491/* signal handler for receiving SIGMAPADD from main thread */
492static void handle_sig_mapadd(int sig)
493{
494 LOGW(g_worker, "SIGMAPADD received\n");
495 /* do nothing */
496}
497
Harald Welte25075972019-03-11 17:33:17 +0100498static void handle_sig_usr1(int sig)
499{
500 OSMO_ASSERT(sig == SIGUSR1);
501
502 if (pthread_equal(g_bankd->main, pthread_self())) {
503 struct bankd_worker *worker;
504 /* main thread */
505 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200506 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100507
508 /* iterate over worker threads and ask them to dump their talloc state */
509 pthread_mutex_lock(&g_bankd->workers_mutex);
510 llist_for_each_entry(worker, &g_bankd->workers, list) {
511 pthread_kill(worker->thread, SIGUSR1);
512 }
513 pthread_mutex_unlock(&g_bankd->workers_mutex);
514 } else {
515 /* worker thread */
516 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200517 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100518 }
519}
520
Harald Welte77911b02018-08-14 23:47:30 +0200521static void worker_cleanup(void *arg)
522{
523 struct bankd_worker *worker = (struct bankd_worker *) arg;
524 struct bankd *bankd = worker->bankd;
525
526 /* FIXME: should we still do this? in the thread ?!? */
527 pthread_mutex_lock(&bankd->workers_mutex);
528 llist_del(&worker->list);
529 talloc_free(worker); /* FIXME: is this safe? */
530 pthread_mutex_unlock(&bankd->workers_mutex);
531}
532
Harald Welteaf614732018-08-17 22:10:05 +0200533static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200534{
Harald Welte297d72e2019-03-28 18:42:35 +0100535 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200536
Harald Welte150d6d62018-10-03 23:07:47 +0200537 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
538
Harald Welte694df832018-10-03 22:47:52 +0200539 if (!worker->reader.name) {
540 /* resolve PC/SC reader name from slot_id -> name map */
541 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
542 if (!worker->reader.name) {
543 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
544 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100545 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200546 }
547 }
Harald Welte45c948c2018-09-23 19:26:52 +0200548 OSMO_ASSERT(worker->reader.name);
549
Harald Welte297d72e2019-03-28 18:42:35 +0100550 rc = worker->ops->open_card(worker);
551 if (rc < 0)
552 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100553
Harald Welte57593f02018-09-23 19:30:31 +0200554 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200555 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200556
Harald Welteaf614732018-08-17 22:10:05 +0200557 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200558}
Harald Welte77911b02018-08-14 23:47:30 +0200559
560
Harald Welte00a96732019-03-11 17:18:02 +0100561static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200562{
563 struct ipaccess_head *hh;
564 uint16_t len;
565 int needed, rc;
566
567 if (buf_size < sizeof(*hh))
568 return -1;
569
570 hh = (struct ipaccess_head *) buf;
571
Harald Welte00a96732019-03-11 17:18:02 +0100572 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
573 * in case of a signal being received */
574
575restart_hdr:
576 /* 1) blocking recv from the socket (IPA header) */
577 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
578 if (rc == -1 && errno == EINTR) {
579 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
580 return -23;
581 goto restart_hdr;
582 } else if (rc < 0)
583 return rc;
584 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200585 return -2;
586
587 len = ntohs(hh->len);
588 needed = len; //- sizeof(*hh);
589
Harald Welte00a96732019-03-11 17:18:02 +0100590restart_body:
591 /* 2) blocking recv from the socket (payload) */
592 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
593 if (rc == -1 && errno == EINTR) {
594 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
595 return -23;
596 goto restart_body;
597 } else if (rc < 0)
598 return rc;
599 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200600 return -3;
601
602 return len;
603}
604
Harald Welte796a7492018-09-23 19:31:28 +0200605static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
606{
607 struct msgb *msg = rspro_enc_msg(pdu);
608 int rc;
609
610 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100611 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200612 LOGW(worker, "error encoding RSPRO\n");
613 return -1;
614 }
615
Harald Weltefd471192018-09-24 14:51:14 +0200616 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200617 /* prepend the header */
618 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200619 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200620
621 /* actually send it through the socket */
622 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
623 if (rc == msgb_length(msg))
624 rc = 0;
625 else {
626 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
627 rc = -1;
628 }
629
630 msgb_free(msg);
631
632 return rc;
633}
634
Harald Welte150d6d62018-10-03 23:07:47 +0200635/* attempt to obtain slot-map */
636static int worker_try_slotmap(struct bankd_worker *worker)
637{
Harald Weltecbd18962019-03-03 19:02:38 +0100638 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200639
Harald Weltecbd18962019-03-03 19:02:38 +0100640 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200641 if (!slmap) {
642 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
643 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
644 /* check in 10s if the map has been installed meanwhile by main thread */
645 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
646 return -1;
647 } else {
648 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
649 slmap->client.client_id, slmap->client.slot_nr,
650 slmap->bank.bank_id, slmap->bank.slot_nr);
651 worker->slot = slmap->bank;
652 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
653 return worker_open_card(worker);
654 }
655}
656
Harald Welte1f699b42019-03-28 13:41:08 +0100657/* inform the remote end (client) about the (new) ATR */
658static int worker_send_atr(struct bankd_worker *worker)
659{
660 RsproPDU_t *set_atr;
661 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
662 worker->client.clslot.slot_nr,
663 worker->card.atr, worker->card.atr_len);
664 if (!set_atr)
665 return -1;
666 return worker_send_rspro(worker, set_atr);
667}
Harald Welte150d6d62018-10-03 23:07:47 +0200668
Harald Weltecce2aad2018-08-16 14:44:37 +0200669static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
670{
Harald Welteaf614732018-08-17 22:10:05 +0200671 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100672 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200673 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100674 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200675
Harald Weltecce2aad2018-08-16 14:44:37 +0200676 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
677
Harald Welte9f7ca612021-12-08 15:29:28 +0100678 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200679 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
680 /* FIXME: store somewhere? */
681
682 if (worker->state != BW_ST_CONN_WAIT_ID) {
683 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100684 rc = -102;
685 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200686 }
687
Harald Welte371d0262018-08-16 15:23:58 +0200688 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200689 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100690 res = ResultCode_illegalClientId;
691 rc = -103;
692 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200693 }
Harald Welte371d0262018-08-16 15:23:58 +0200694 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
695 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200696 worker_set_state(worker, BW_ST_CONN_CLIENT);
697
Harald Welte150d6d62018-10-03 23:07:47 +0200698 if (worker_try_slotmap(worker) >= 0)
699 res = ResultCode_ok;
700 else
Harald Welte3e689872018-09-24 14:52:56 +0200701 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200702
Harald Welte3e689872018-09-24 14:52:56 +0200703 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100704 rc = worker_send_rspro(worker, resp);
705 if (rc < 0)
706 return rc;
707
708 if (res == ResultCode_ok)
709 rc = worker_send_atr(worker);
710
711 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100712
713respond_and_err:
714 if (res) {
715 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
716 worker_send_rspro(worker, resp);
717 }
718 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200719}
720
Harald Welte796a7492018-09-23 19:31:28 +0200721static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
722{
723 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200724 uint8_t rx_buf[1024];
725 DWORD rx_buf_len = sizeof(rx_buf);
726 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100727 struct client_slot clslot;
728 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100729 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200730
Harald Welte9f7ca612021-12-08 15:29:28 +0100731 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
732 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200733
734 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
735 LOGW(worker, "Unexpected tpduModemToCaard\n");
736 return -104;
737 }
738
Harald Weltee1d32892019-03-27 20:47:42 +0100739 /* Validate that toBankSlot / fromClientSlot match our expectations */
740 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
741 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
742 if (!bank_slot_equals(&worker->slot, &bslot)) {
743 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
744 bslot.bank_id, bslot.slot_nr);
745 return -105;
746 }
747 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
748 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
749 clslot.client_id, clslot.slot_nr);
750 return -106;
751 }
Harald Welte796a7492018-09-23 19:31:28 +0200752
Harald Welte297d72e2019-03-28 18:42:35 +0100753 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
754 rx_buf, &rx_buf_len);
755 if (rc < 0)
756 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200757
Harald Welte001884c2022-07-11 23:38:48 +0200758 LOGW(worker, "Tx RSPRO tpduCardToModem(%s)\n", osmo_hexdump_nospc(rx_buf, rx_buf_len));
Harald Welte796a7492018-09-23 19:31:28 +0200759 /* encode response PDU and send it */
760 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
761 rx_buf, rx_buf_len);
762 worker_send_rspro(worker, pdu_resp);
763
764 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200765}
766
Harald Weltebbd18bd2019-12-16 13:11:50 +0100767static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
768{
769 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
770 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
771 int rc = 0;
772
Harald Welte9f7ca612021-12-08 15:29:28 +0100773 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100774 sps->resetActive ? "ACTIVE" : "INACTIVE",
775 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
776 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
777
778 /* perform cold or warm reset */
James Tavaresf74d1da2022-11-14 00:21:12 -0500779 if (sps->vccPresent && *sps->vccPresent == 0) {
780 /* VCC is not present */
781
782 if (worker->last_vccPresent) {
783 /* falling edge detected on VCC; perform cold reset */
784 rc = worker->ops->reset_card(worker, true);
785 }
786 } else if (sps->resetActive) {
787 if (!worker->last_resetActive) {
788 /* VCC is present (or not reported) and rising edge detected on reset; perform warm reset */
789 rc = worker->ops->reset_card(worker, false);
790 }
791 }
792
793 /* update last known states */
794 if (sps->vccPresent) {
795 worker->last_vccPresent = *sps->vccPresent != 0;
796 }
797
798 worker->last_resetActive = sps->resetActive != 0;
Harald Weltebbd18bd2019-12-16 13:11:50 +0100799
800 return rc;
801}
802
Harald Welte77911b02018-08-14 23:47:30 +0200803/* handle one incoming RSPRO message from a client inside a worker thread */
804static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
805{
Harald Weltecce2aad2018-08-16 14:44:37 +0200806 int rc = -100;
807
Harald Welte77911b02018-08-14 23:47:30 +0200808 switch (pdu->msg.present) {
809 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200810 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200811 break;
812 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200813 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200814 break;
815 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100816 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200817 rc = 0;
818 break;
819 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100820 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200821 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200822 break;
823 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100824 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200825 rc = -101;
826 break;
Harald Welte77911b02018-08-14 23:47:30 +0200827 }
828
James Tavares2c0a5122022-11-13 23:22:22 -0500829 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200830}
831
Harald Welte694df832018-10-03 22:47:52 +0200832static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
833{
834 struct timeval tout = { timeout_secs, 0 };
835 fd_set readset;
836
837 FD_ZERO(&readset);
838 FD_SET(fd, &readset);
839 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
840}
841
Harald Welte77911b02018-08-14 23:47:30 +0200842/* body of the main transceive loop */
843static int worker_transceive_loop(struct bankd_worker *worker)
844{
845 struct ipaccess_head *hh;
846 struct ipaccess_head_ext *hh_ext;
847 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
848 asn_dec_rval_t rval;
849 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200850 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200851
Harald Welte00a96732019-03-11 17:18:02 +0100852restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200853 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100854 if (rc == -1 && errno == EINTR) {
855 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
856 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100857 else
858 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100859 goto restart_wait;
860 } else if (rc < 0)
861 return rc;
862 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200863 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200864 switch (worker->state) {
865 case BW_ST_CONN_CLIENT_WAIT_MAP:
866 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100867 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200868 break;
869 case BW_ST_CONN_CLIENT_MAPPED:
870 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100871 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200872 break;
873 default:
874 OSMO_ASSERT(0);
875 }
Harald Welte1f699b42019-03-28 13:41:08 +0100876 if (rc == 0)
877 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200878 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200879 return 0;
880 };
881
Harald Welte77911b02018-08-14 23:47:30 +0200882 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100883 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200884 if (rc < 0)
885 return rc;
886 data_len = rc;
887
888 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200889 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200890 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200891 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200892 }
Harald Welte77911b02018-08-14 23:47:30 +0200893
Harald Welte5a3613a2018-10-11 12:56:21 +0200894 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100895 switch (hh->data[0]) {
896 case IPAC_MSGT_PING:
897 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100898 case IPAC_MSGT_ID_ACK:
899 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100900 default:
901 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
902 break;
903 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200904 return 0;
905 }
906
Harald Welte77911b02018-08-14 23:47:30 +0200907 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200908 if (data_len < sizeof(*hh_ext)) {
909 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200910 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200911 }
Harald Welte77911b02018-08-14 23:47:30 +0200912 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200913 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
914 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200915 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200916 }
Harald Welte77911b02018-08-14 23:47:30 +0200917
918 /* 2) ASN1 BER decode of the message */
919 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200920 if (rval.code != RC_OK) {
921 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200922 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200923 }
Harald Welte77911b02018-08-14 23:47:30 +0200924
925 /* 3) handling of the message, possibly resulting in PCSC commands */
926 rc = worker_handle_rspro(worker, pdu);
927 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200928 if (rc < 0) {
929 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200930 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200931 }
Harald Welte77911b02018-08-14 23:47:30 +0200932
933 /* everything OK if we reach here */
934 return 0;
935}
936
Harald Welted6dfb8c2018-08-16 14:46:53 +0200937/* obtain an ascii representation of the client IP/port */
938static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
939{
940 char hostbuf[32], portbuf[32];
941 int rc;
942
943 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
944 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
945 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
946 if (rc != 0) {
947 out[0] = '\0';
948 return -1;
949 }
950 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
951 return 0;
952}
953
Harald Welte77911b02018-08-14 23:47:30 +0200954/* worker thread main function */
955static void *worker_main(void *arg)
956{
Harald Welte77911b02018-08-14 23:47:30 +0200957 void *top_ctx;
958 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200959
Harald Welte00a96732019-03-11 17:18:02 +0100960 g_worker = (struct bankd_worker *) arg;
961
Harald Welte00a96732019-03-11 17:18:02 +0100962 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200963
Harald Welte77911b02018-08-14 23:47:30 +0200964 /* not permitted in multithreaded environment */
965 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100966 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
967 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200968
Harald Welte286a2be2019-03-11 17:36:58 +0100969 /* set the thread name */
970 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
971 pthread_setname_np(pthread_self(), g_worker->name);
972
Harald Welte77911b02018-08-14 23:47:30 +0200973 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100974 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200975
Harald Welte602b6f72019-12-04 21:51:02 +0100976 g_worker->slot.bank_id = 0xffff;
977 g_worker->slot.slot_nr = 0xffff;
978
Harald Welte77911b02018-08-14 23:47:30 +0200979 /* we continuously perform the same loop here, recycling the worker thread
980 * once the client connection is gone or we have some trouble with the card/reader */
981 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200982 char buf[128];
983
Harald Welte00a96732019-03-11 17:18:02 +0100984 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200985
Harald Welte00a96732019-03-11 17:18:02 +0100986 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200987 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100988 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
989 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200990 if (rc < 0) {
991 continue;
992 }
Harald Welte00a96732019-03-11 17:18:02 +0100993 g_worker->client.fd = rc;
994 worker_client_addrstr(buf, sizeof(buf), g_worker);
995 LOGW(g_worker, "Accepted connection from %s\n", buf);
996 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200997
998 /* run the main worker transceive loop body until there was some error */
999 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +01001000 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +02001001 if (rc < 0)
1002 break;
Harald Welte653d6a02019-03-11 18:38:44 +01001003 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
1004 break;
Harald Welte77911b02018-08-14 23:47:30 +02001005 }
1006
Harald Weltef4be5e12022-07-24 13:36:46 +02001007 if (rc == -23)
1008 LOGW(g_worker, "Client unmapped: Cleaning up state\n");
1009 else
1010 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +02001011
Harald Welte77911b02018-08-14 23:47:30 +02001012 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +01001013 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +01001014 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +01001015 if (g_worker->reader.name)
1016 g_worker->reader.name = NULL;
1017 if (g_worker->client.fd >= 0)
1018 close(g_worker->client.fd);
1019 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
1020 g_worker->client.fd = -1;
1021 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +02001022 }
1023
1024 pthread_cleanup_pop(1);
1025 talloc_free(top_ctx);
1026 pthread_exit(NULL);
1027}