blob: b3b8dff852fa4f26808ff7e9ee1ac3f39c28ad2f [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;
Harald Welte77911b02018-08-14 23:47:30 +0200118
119 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
120
121 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
122 if (rc != 0) {
123 talloc_free(worker);
124 return NULL;
125 }
126
127 pthread_mutex_lock(&bankd->workers_mutex);
128 llist_add_tail(&worker->list, &bankd->workers);
129 pthread_mutex_unlock(&bankd->workers_mutex);
130
131 return worker;
132}
133
134static bool terminate = false;
135
Harald Weltea4967832019-12-05 22:04:57 +0100136/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
137static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
138{
139 struct bankd_worker *worker;
140 pthread_mutex_lock(&g_bankd->workers_mutex);
141 llist_for_each_entry(worker, &g_bankd->workers, list) {
142 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
143 continue;
144 if (cs && (cs->client_id != worker->client.clslot.client_id ||
145 cs->slot_nr != worker->client.clslot.slot_nr))
146 continue;
147
148 pthread_kill(worker->thread, sig);
149 break;
150 }
151 pthread_mutex_unlock(&g_bankd->workers_mutex);
152}
153
Harald Weltec650a4d2019-12-04 15:02:27 +0100154/* Remove a mapping */
155static void bankd_srvc_remove_mapping(struct slot_mapping *map)
156{
157 struct bank_slot bs = map->bank;
158
159 slotmap_del(g_bankd->slotmaps, map);
160
161 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100162 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100163}
164
Harald Welte707c85a2019-03-09 12:56:35 +0100165/* handle incoming messages from server */
166static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
167{
Harald Welte454f5e22019-03-09 21:38:34 +0100168 const CreateMappingReq_t *creq = NULL;
169 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100170 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100171 struct slot_mapping *map;
172 struct bank_slot bs;
173 struct client_slot cs;
174 RsproPDU_t *resp;
175
Harald Welte079c0682022-04-27 09:26:52 +0200176 LOGPFSML(srvc->fi, LOGL_DEBUG, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100177
178 switch (pdu->msg.present) {
179 case RsproPDUchoice_PR_connectBankRes:
Harald Welte5ae0f312022-04-25 15:53:04 +0200180 if (pdu->msg.choice.connectBankRes.identity.type != ComponentType_remsimServer) {
181 LOGPFSML(srvc->fi, LOGL_ERROR, "Server connection to a ComponentType(%ld) != RemsimServer? "
182 "Check your IP/Port configuration\n",
183 pdu->msg.choice.connectBankRes.identity.type);
184 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_DISCONNECT, NULL);
185 return -1;
186 }
Harald Welte707c85a2019-03-09 12:56:35 +0100187 /* Store 'identity' of server in srvc->peer_comp_id */
188 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
189 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
190 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100191 case RsproPDUchoice_PR_createMappingReq:
192 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200193 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100194 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200195 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100196 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200197 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100198 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200199 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100200 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100201 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100202 rspro2bank_slot(&bs, &creq->bank);
203 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100204 /* check if map exists */
205 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
206 if (map) {
207 if (client_slot_equals(&map->client, &cs)) {
208 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
209 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
210 goto send_resp;
211 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200212 LOGPFSML(srvc->fi, LOGL_NOTICE, "implicitly removing slotmap\n");
Harald Weltee6fa46a2019-12-04 15:06:33 +0100213 bankd_srvc_remove_mapping(map);
214 }
215 }
Harald Welte454f5e22019-03-09 21:38:34 +0100216 /* Add a new mapping */
217 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100218 if (!map) {
219 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100220 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100221 } else {
222 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100223 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100224 }
Harald Welte454f5e22019-03-09 21:38:34 +0100225 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100226send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100227 server_conn_send_rspro(srvc, resp);
228 break;
229 case RsproPDUchoice_PR_removeMappingReq:
230 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200231 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100232 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100233 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100234 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200235 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100236 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100237 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100238 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100239 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100240 rspro2bank_slot(&bs, &rreq->bank);
241 /* Remove a mapping */
242 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100243 if (!map) {
244 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100245 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100246 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100247 rspro2client_slot(&cs, &rreq->client);
248 if (!client_slot_equals(&map->client, &cs)) {
Harald Welte079c0682022-04-27 09:26:52 +0200249 LOGPFSML(srvc->fi, LOGL_NOTICE, "ClientId in removeMappingReq != map\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100250 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
251 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200252 LOGPFSML(srvc->fi, LOGL_INFO, "removing slotmap\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100253 bankd_srvc_remove_mapping(map);
254 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
255 }
Harald Welte454f5e22019-03-09 21:38:34 +0100256 }
257 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100258 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100259 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100260 case RsproPDUchoice_PR_resetStateReq:
261 /* delete all slotmaps */
262 slotmap_del_all(g_bankd->slotmaps);
263 /* notify all workers about maps having disappeared */
264 pthread_mutex_lock(&g_bankd->workers_mutex);
265 llist_for_each_entry(worker, &g_bankd->workers, list) {
266 pthread_kill(worker->thread, SIGMAPDEL);
267 }
268 pthread_mutex_unlock(&g_bankd->workers_mutex);
269 /* send response to server */
270 resp = rspro_gen_ResetStateRes(ResultCode_ok);
271 server_conn_send_rspro(srvc, resp);
272 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100273 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100274 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
275 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100276 return -1;
277 }
278
279 return 0;
280}
281
Harald Welte5bae20b2019-04-01 09:36:42 +0200282static void printf_help()
283{
284 printf(
285" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100286" -V --version Print the version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +0200287" -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200288" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
289" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
Harald Welte55f12b82022-01-16 14:34:12 +0100290" -b --bank-id <1-1023> Bank Identifier of this SIM bank (default: 1)\n"
291" -n --num-slots <1-1023> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200292" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
293" connections (default: INADDR_ANY)\n"
294" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
295" connectionss (default: 9999)\n"
Harald Welte628672b2022-07-13 15:20:13 +0200296" -s --permit-shared-pcsc Permit SHARED access to PC/SC readers (default: exclusive)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200297 );
298}
299
300static int g_bind_port = 9999;
301static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100302
Harald Weltef8c6eeb2021-12-08 20:33:00 +0100303static void handle_options(int argc, char **argv)
Harald Welte707c85a2019-03-09 12:56:35 +0100304{
Harald Welte5bae20b2019-04-01 09:36:42 +0200305 while (1) {
306 int option_index = 0, c;
307 static const struct option long_options[] = {
308 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100309 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +0200310 { "debug", 1, 0, 'd' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200311 { "server-host", 1, 0, 'i' },
312 { "server-port", 1, 0, 'p' },
313 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200314 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200315 { "component-name", 1, 0, 'N' },
316 { "bind-ip", 1, 0, 'I' },
317 { "bind-port", 1, 0, 'P' },
Harald Welte628672b2022-07-13 15:20:13 +0200318 { "permit-shared-pcsc", 0, 0, 's' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200319 { 0, 0, 0, 0 }
320 };
321
Harald Welte628672b2022-07-13 15:20:13 +0200322 c = getopt_long(argc, argv, "hVd:i:o:b:n:N:I:P:s", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200323 if (c == -1)
324 break;
325
326 switch (c) {
327 case 'h':
328 printf_help();
329 exit(0);
330 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100331 case 'V':
332 printf("osmo-remsim-bankd version %s\n", VERSION);
333 exit(0);
334 break;
Harald Welte80a01102020-05-25 14:55:12 +0200335 case 'd':
336 log_parse_category_mask(osmo_stderr_target, optarg);
337 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200338 case 'i':
339 g_bankd->srvc.server_host = optarg;
340 break;
341 case 'p':
342 g_bankd->srvc.server_port = atoi(optarg);
343 break;
344 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200345 g_bankd->srvc.bankd.bank_id = atoi(optarg);
346 break;
347 case 'n':
348 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200349 break;
350 case 'N':
351 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
352 break;
353 case 'I':
354 g_bind_ip = optarg;
355 break;
356 case 'P':
357 g_bind_port = atoi(optarg);
358 break;
Harald Welte628672b2022-07-13 15:20:13 +0200359 case 's':
360 g_bankd->cfg.permit_shared_pcsc = true;
361 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200362 }
363 }
Harald Welte707c85a2019-03-09 12:56:35 +0100364}
365
Harald Welte77911b02018-08-14 23:47:30 +0200366int main(int argc, char **argv)
367{
Harald Weltef4b16f12019-03-09 20:58:17 +0100368 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200369 int i, rc;
370
Harald Weltef4b16f12019-03-09 20:58:17 +0100371 g_bankd = talloc_zero(NULL, struct bankd);
372 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200373
Harald Welteeea631b2022-05-03 15:14:19 +0200374 if (gethostname(g_hostname, sizeof(g_hostname)) < 0)
375 OSMO_STRLCPY_ARRAY(g_hostname, "unknown");
376
Harald Weltef4b16f12019-03-09 20:58:17 +0100377 bankd_init(g_bankd);
378
379 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100380 srvc->server_host = "localhost";
381 srvc->server_port = 9998;
382 srvc->handle_rx = bankd_srvc_handle_rx;
383 srvc->own_comp_id.type = ComponentType_remsimBankd;
Harald Welteeea631b2022-05-03 15:14:19 +0200384 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, g_hostname);
Harald Welte707c85a2019-03-09 12:56:35 +0100385 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
386 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
387
388 handle_options(argc, argv);
389
Harald Welte25075972019-03-11 17:33:17 +0100390 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100391 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100392 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100393 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100394
Harald Weltee89fecd2019-04-04 09:23:13 +0200395 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
396 * read once during bankd initialization, when the worker threads haven't
397 * started yet */
Harald Weltee9e505c2022-05-03 11:58:01 +0200398 rc = bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv");
399 if (rc)
400 exit(1);
Harald Weltee89fecd2019-04-04 09:23:13 +0200401
Harald Welte707c85a2019-03-09 12:56:35 +0100402 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100403 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100404 if (rc < 0) {
405 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
406 exit(1);
407 }
Harald Welted2192e22019-11-07 18:10:57 +0100408 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100409
410 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200411 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 +0200412 if (rc < 0) {
413 fprintf(stderr, "Unable to create TCP socket at %s:%d: %s\n",
414 g_bind_ip ? g_bind_ip : "INADDR_ANY", g_bind_port, strerror(errno));
Harald Welte12534e72018-08-15 23:37:29 +0200415 exit(1);
Harald Weltee9e505c2022-05-03 11:58:01 +0200416 }
Harald Weltef4b16f12019-03-09 20:58:17 +0100417 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200418
Harald Weltea0f39502019-03-09 20:59:34 +0100419 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200420 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200421 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100422 w = bankd_create_worker(g_bankd, i);
Harald Weltee9e505c2022-05-03 11:58:01 +0200423 if (!w) {
424 fprintf(stderr, "Error creating bankd worker thread\n");
Harald Welte77911b02018-08-14 23:47:30 +0200425 exit(21);
Harald Weltee9e505c2022-05-03 11:58:01 +0200426 }
Harald Welte77911b02018-08-14 23:47:30 +0200427 }
428
429 while (1) {
430 if (terminate)
431 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100432 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200433 }
434
Harald Weltef4b16f12019-03-09 20:58:17 +0100435 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200436 exit(0);
437}
438
439
440
441/***********************************************************************
442 * bankd worker thread
443 ***********************************************************************/
444
Harald Welte00a96732019-03-11 17:18:02 +0100445static __thread struct bankd_worker *g_worker;
446
Harald Welte8d858292018-08-15 23:36:46 +0200447struct value_string worker_state_names[] = {
448 { BW_ST_INIT, "INIT" },
449 { BW_ST_ACCEPTING, "ACCEPTING" },
450 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
451 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200452 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200453 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
454 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100455 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200456 { 0, NULL }
457};
458
Harald Welte1f699b42019-03-28 13:41:08 +0100459static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
460
Harald Welte8d858292018-08-15 23:36:46 +0200461static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
462{
463 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
464 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200465 worker->timeout = 0;
466}
467
468static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
469 unsigned int timeout_secs)
470{
471 LOGW(worker, "Changing state to %s (timeout=%u)\n",
472 get_value_string(worker_state_names, new_state), timeout_secs);
473 worker->state = new_state;
474 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200475}
Harald Welte77911b02018-08-14 23:47:30 +0200476
Harald Welte00a96732019-03-11 17:18:02 +0100477/* signal handler for receiving SIGMAPDEL from main thread */
478static void handle_sig_mapdel(int sig)
479{
480 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
481 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100482 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
483 g_worker->slot.bank_id = 0xffff;
484 g_worker->slot.slot_nr = 0xffff;
485 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
486 }
Harald Welte00a96732019-03-11 17:18:02 +0100487}
488
Harald Welte0fd77a52019-12-05 22:05:39 +0100489/* signal handler for receiving SIGMAPADD from main thread */
490static void handle_sig_mapadd(int sig)
491{
492 LOGW(g_worker, "SIGMAPADD received\n");
493 /* do nothing */
494}
495
Harald Welte25075972019-03-11 17:33:17 +0100496static void handle_sig_usr1(int sig)
497{
498 OSMO_ASSERT(sig == SIGUSR1);
499
500 if (pthread_equal(g_bankd->main, pthread_self())) {
501 struct bankd_worker *worker;
502 /* main thread */
503 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200504 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100505
506 /* iterate over worker threads and ask them to dump their talloc state */
507 pthread_mutex_lock(&g_bankd->workers_mutex);
508 llist_for_each_entry(worker, &g_bankd->workers, list) {
509 pthread_kill(worker->thread, SIGUSR1);
510 }
511 pthread_mutex_unlock(&g_bankd->workers_mutex);
512 } else {
513 /* worker thread */
514 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200515 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100516 }
517}
518
Harald Welte77911b02018-08-14 23:47:30 +0200519static void worker_cleanup(void *arg)
520{
521 struct bankd_worker *worker = (struct bankd_worker *) arg;
522 struct bankd *bankd = worker->bankd;
523
524 /* FIXME: should we still do this? in the thread ?!? */
525 pthread_mutex_lock(&bankd->workers_mutex);
526 llist_del(&worker->list);
527 talloc_free(worker); /* FIXME: is this safe? */
528 pthread_mutex_unlock(&bankd->workers_mutex);
529}
530
Harald Welteaf614732018-08-17 22:10:05 +0200531static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200532{
Harald Welte297d72e2019-03-28 18:42:35 +0100533 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200534
Harald Welte150d6d62018-10-03 23:07:47 +0200535 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
536
Harald Welte694df832018-10-03 22:47:52 +0200537 if (!worker->reader.name) {
538 /* resolve PC/SC reader name from slot_id -> name map */
539 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
540 if (!worker->reader.name) {
541 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
542 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100543 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200544 }
545 }
Harald Welte45c948c2018-09-23 19:26:52 +0200546 OSMO_ASSERT(worker->reader.name);
547
Harald Welte297d72e2019-03-28 18:42:35 +0100548 rc = worker->ops->open_card(worker);
549 if (rc < 0)
550 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100551
Harald Welte57593f02018-09-23 19:30:31 +0200552 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200553 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200554
Harald Welteaf614732018-08-17 22:10:05 +0200555 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200556}
Harald Welte77911b02018-08-14 23:47:30 +0200557
558
Harald Welte00a96732019-03-11 17:18:02 +0100559static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200560{
561 struct ipaccess_head *hh;
562 uint16_t len;
563 int needed, rc;
564
565 if (buf_size < sizeof(*hh))
566 return -1;
567
568 hh = (struct ipaccess_head *) buf;
569
Harald Welte00a96732019-03-11 17:18:02 +0100570 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
571 * in case of a signal being received */
572
573restart_hdr:
574 /* 1) blocking recv from the socket (IPA header) */
575 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
576 if (rc == -1 && errno == EINTR) {
577 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
578 return -23;
579 goto restart_hdr;
580 } else if (rc < 0)
581 return rc;
582 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200583 return -2;
584
585 len = ntohs(hh->len);
586 needed = len; //- sizeof(*hh);
587
Harald Welte00a96732019-03-11 17:18:02 +0100588restart_body:
589 /* 2) blocking recv from the socket (payload) */
590 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
591 if (rc == -1 && errno == EINTR) {
592 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
593 return -23;
594 goto restart_body;
595 } else if (rc < 0)
596 return rc;
597 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200598 return -3;
599
600 return len;
601}
602
Harald Welte796a7492018-09-23 19:31:28 +0200603static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
604{
605 struct msgb *msg = rspro_enc_msg(pdu);
606 int rc;
607
608 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100609 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200610 LOGW(worker, "error encoding RSPRO\n");
611 return -1;
612 }
613
Harald Weltefd471192018-09-24 14:51:14 +0200614 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200615 /* prepend the header */
616 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200617 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200618
619 /* actually send it through the socket */
620 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
621 if (rc == msgb_length(msg))
622 rc = 0;
623 else {
624 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
625 rc = -1;
626 }
627
628 msgb_free(msg);
629
630 return rc;
631}
632
Harald Welte150d6d62018-10-03 23:07:47 +0200633/* attempt to obtain slot-map */
634static int worker_try_slotmap(struct bankd_worker *worker)
635{
Harald Weltecbd18962019-03-03 19:02:38 +0100636 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200637
Harald Weltecbd18962019-03-03 19:02:38 +0100638 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200639 if (!slmap) {
640 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
641 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
642 /* check in 10s if the map has been installed meanwhile by main thread */
643 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
644 return -1;
645 } else {
646 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
647 slmap->client.client_id, slmap->client.slot_nr,
648 slmap->bank.bank_id, slmap->bank.slot_nr);
649 worker->slot = slmap->bank;
650 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
651 return worker_open_card(worker);
652 }
653}
654
Harald Welte1f699b42019-03-28 13:41:08 +0100655/* inform the remote end (client) about the (new) ATR */
656static int worker_send_atr(struct bankd_worker *worker)
657{
658 RsproPDU_t *set_atr;
659 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
660 worker->client.clslot.slot_nr,
661 worker->card.atr, worker->card.atr_len);
662 if (!set_atr)
663 return -1;
664 return worker_send_rspro(worker, set_atr);
665}
Harald Welte150d6d62018-10-03 23:07:47 +0200666
Harald Weltecce2aad2018-08-16 14:44:37 +0200667static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
668{
Harald Welteaf614732018-08-17 22:10:05 +0200669 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100670 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200671 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100672 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200673
Harald Weltecce2aad2018-08-16 14:44:37 +0200674 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
675
Harald Welte9f7ca612021-12-08 15:29:28 +0100676 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200677 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
678 /* FIXME: store somewhere? */
679
680 if (worker->state != BW_ST_CONN_WAIT_ID) {
681 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100682 rc = -102;
683 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200684 }
685
Harald Welte371d0262018-08-16 15:23:58 +0200686 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200687 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100688 res = ResultCode_illegalClientId;
689 rc = -103;
690 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200691 }
Harald Welte371d0262018-08-16 15:23:58 +0200692 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
693 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200694 worker_set_state(worker, BW_ST_CONN_CLIENT);
695
Harald Welte150d6d62018-10-03 23:07:47 +0200696 if (worker_try_slotmap(worker) >= 0)
697 res = ResultCode_ok;
698 else
Harald Welte3e689872018-09-24 14:52:56 +0200699 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200700
Harald Welte3e689872018-09-24 14:52:56 +0200701 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100702 rc = worker_send_rspro(worker, resp);
703 if (rc < 0)
704 return rc;
705
706 if (res == ResultCode_ok)
707 rc = worker_send_atr(worker);
708
709 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100710
711respond_and_err:
712 if (res) {
713 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
714 worker_send_rspro(worker, resp);
715 }
716 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200717}
718
Harald Welte796a7492018-09-23 19:31:28 +0200719static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
720{
721 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200722 uint8_t rx_buf[1024];
723 DWORD rx_buf_len = sizeof(rx_buf);
724 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100725 struct client_slot clslot;
726 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100727 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200728
Harald Welte9f7ca612021-12-08 15:29:28 +0100729 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
730 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200731
732 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
733 LOGW(worker, "Unexpected tpduModemToCaard\n");
734 return -104;
735 }
736
Harald Weltee1d32892019-03-27 20:47:42 +0100737 /* Validate that toBankSlot / fromClientSlot match our expectations */
738 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
739 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
740 if (!bank_slot_equals(&worker->slot, &bslot)) {
741 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
742 bslot.bank_id, bslot.slot_nr);
743 return -105;
744 }
745 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
746 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
747 clslot.client_id, clslot.slot_nr);
748 return -106;
749 }
Harald Welte796a7492018-09-23 19:31:28 +0200750
Harald Welte297d72e2019-03-28 18:42:35 +0100751 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
752 rx_buf, &rx_buf_len);
753 if (rc < 0)
754 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200755
Harald Welte001884c2022-07-11 23:38:48 +0200756 LOGW(worker, "Tx RSPRO tpduCardToModem(%s)\n", osmo_hexdump_nospc(rx_buf, rx_buf_len));
Harald Welte796a7492018-09-23 19:31:28 +0200757 /* encode response PDU and send it */
758 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
759 rx_buf, rx_buf_len);
760 worker_send_rspro(worker, pdu_resp);
761
762 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200763}
764
Harald Weltebbd18bd2019-12-16 13:11:50 +0100765static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
766{
767 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
768 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
769 int rc = 0;
770
Harald Welte9f7ca612021-12-08 15:29:28 +0100771 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100772 sps->resetActive ? "ACTIVE" : "INACTIVE",
773 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
774 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
775
776 /* perform cold or warm reset */
777 if (sps->vccPresent && *sps->vccPresent == 0)
778 rc = worker->ops->reset_card(worker, true);
779 else if (sps->resetActive)
780 rc = worker->ops->reset_card(worker, false);
781
782 return rc;
783}
784
Harald Welte77911b02018-08-14 23:47:30 +0200785/* handle one incoming RSPRO message from a client inside a worker thread */
786static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
787{
Harald Weltecce2aad2018-08-16 14:44:37 +0200788 int rc = -100;
789
Harald Welte77911b02018-08-14 23:47:30 +0200790 switch (pdu->msg.present) {
791 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200792 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200793 break;
794 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200795 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200796 break;
797 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100798 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200799 rc = 0;
800 break;
801 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100802 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200803 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200804 break;
805 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100806 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200807 rc = -101;
808 break;
Harald Welte77911b02018-08-14 23:47:30 +0200809 }
810
Harald Weltecce2aad2018-08-16 14:44:37 +0200811 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200812}
813
Harald Welte694df832018-10-03 22:47:52 +0200814static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
815{
816 struct timeval tout = { timeout_secs, 0 };
817 fd_set readset;
818
819 FD_ZERO(&readset);
820 FD_SET(fd, &readset);
821 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
822}
823
Harald Welte77911b02018-08-14 23:47:30 +0200824/* body of the main transceive loop */
825static int worker_transceive_loop(struct bankd_worker *worker)
826{
827 struct ipaccess_head *hh;
828 struct ipaccess_head_ext *hh_ext;
829 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
830 asn_dec_rval_t rval;
831 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200832 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200833
Harald Welte00a96732019-03-11 17:18:02 +0100834restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200835 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100836 if (rc == -1 && errno == EINTR) {
837 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
838 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100839 else
840 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100841 goto restart_wait;
842 } else if (rc < 0)
843 return rc;
844 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200845 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200846 switch (worker->state) {
847 case BW_ST_CONN_CLIENT_WAIT_MAP:
848 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100849 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200850 break;
851 case BW_ST_CONN_CLIENT_MAPPED:
852 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100853 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200854 break;
855 default:
856 OSMO_ASSERT(0);
857 }
Harald Welte1f699b42019-03-28 13:41:08 +0100858 if (rc == 0)
859 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200860 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200861 return 0;
862 };
863
Harald Welte77911b02018-08-14 23:47:30 +0200864 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100865 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200866 if (rc < 0)
867 return rc;
868 data_len = rc;
869
870 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200871 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200872 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200873 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200874 }
Harald Welte77911b02018-08-14 23:47:30 +0200875
Harald Welte5a3613a2018-10-11 12:56:21 +0200876 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100877 switch (hh->data[0]) {
878 case IPAC_MSGT_PING:
879 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100880 case IPAC_MSGT_ID_ACK:
881 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100882 default:
883 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
884 break;
885 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200886 return 0;
887 }
888
Harald Welte77911b02018-08-14 23:47:30 +0200889 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200890 if (data_len < sizeof(*hh_ext)) {
891 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200892 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200893 }
Harald Welte77911b02018-08-14 23:47:30 +0200894 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200895 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
896 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200897 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200898 }
Harald Welte77911b02018-08-14 23:47:30 +0200899
900 /* 2) ASN1 BER decode of the message */
901 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200902 if (rval.code != RC_OK) {
903 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200904 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200905 }
Harald Welte77911b02018-08-14 23:47:30 +0200906
907 /* 3) handling of the message, possibly resulting in PCSC commands */
908 rc = worker_handle_rspro(worker, pdu);
909 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200910 if (rc < 0) {
911 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200912 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200913 }
Harald Welte77911b02018-08-14 23:47:30 +0200914
915 /* everything OK if we reach here */
916 return 0;
917}
918
Harald Welted6dfb8c2018-08-16 14:46:53 +0200919/* obtain an ascii representation of the client IP/port */
920static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
921{
922 char hostbuf[32], portbuf[32];
923 int rc;
924
925 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
926 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
927 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
928 if (rc != 0) {
929 out[0] = '\0';
930 return -1;
931 }
932 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
933 return 0;
934}
935
Harald Welte77911b02018-08-14 23:47:30 +0200936/* worker thread main function */
937static void *worker_main(void *arg)
938{
Harald Welte77911b02018-08-14 23:47:30 +0200939 void *top_ctx;
940 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200941
Harald Welte00a96732019-03-11 17:18:02 +0100942 g_worker = (struct bankd_worker *) arg;
943
Harald Welte00a96732019-03-11 17:18:02 +0100944 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200945
Harald Welte77911b02018-08-14 23:47:30 +0200946 /* not permitted in multithreaded environment */
947 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100948 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
949 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200950
Harald Welte286a2be2019-03-11 17:36:58 +0100951 /* set the thread name */
952 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
953 pthread_setname_np(pthread_self(), g_worker->name);
954
Harald Welte77911b02018-08-14 23:47:30 +0200955 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100956 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200957
Harald Welte602b6f72019-12-04 21:51:02 +0100958 g_worker->slot.bank_id = 0xffff;
959 g_worker->slot.slot_nr = 0xffff;
960
Harald Welte77911b02018-08-14 23:47:30 +0200961 /* we continuously perform the same loop here, recycling the worker thread
962 * once the client connection is gone or we have some trouble with the card/reader */
963 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200964 char buf[128];
965
Harald Welte00a96732019-03-11 17:18:02 +0100966 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200967
Harald Welte00a96732019-03-11 17:18:02 +0100968 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200969 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100970 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
971 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200972 if (rc < 0) {
973 continue;
974 }
Harald Welte00a96732019-03-11 17:18:02 +0100975 g_worker->client.fd = rc;
976 worker_client_addrstr(buf, sizeof(buf), g_worker);
977 LOGW(g_worker, "Accepted connection from %s\n", buf);
978 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200979
980 /* run the main worker transceive loop body until there was some error */
981 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100982 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200983 if (rc < 0)
984 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100985 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
986 break;
Harald Welte77911b02018-08-14 23:47:30 +0200987 }
988
Harald Weltef4be5e12022-07-24 13:36:46 +0200989 if (rc == -23)
990 LOGW(g_worker, "Client unmapped: Cleaning up state\n");
991 else
992 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200993
Harald Welte77911b02018-08-14 23:47:30 +0200994 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100995 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100996 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100997 if (g_worker->reader.name)
998 g_worker->reader.name = NULL;
999 if (g_worker->client.fd >= 0)
1000 close(g_worker->client.fd);
1001 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
1002 g_worker->client.fd = -1;
1003 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +02001004 }
1005
1006 pthread_cleanup_pop(1);
1007 talloc_free(top_ctx);
1008 pthread_exit(NULL);
1009}