blob: 8ec09664e8a9e5bb81104a7a4510a2548ddf7c75 [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 Welte77911b02018-08-14 23:47:30 +020085 /* intialize 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 Welte77911b02018-08-14 23:47:30 +0200101}
102
103/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200104static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200105{
106 struct bankd_worker *worker;
107 int rc;
108
109 worker = talloc_zero(bankd, struct bankd_worker);
110 if (!worker)
111 return NULL;
112
113 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200114 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100115 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200116
117 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
118
119 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
120 if (rc != 0) {
121 talloc_free(worker);
122 return NULL;
123 }
124
125 pthread_mutex_lock(&bankd->workers_mutex);
126 llist_add_tail(&worker->list, &bankd->workers);
127 pthread_mutex_unlock(&bankd->workers_mutex);
128
129 return worker;
130}
131
132static bool terminate = false;
133
Harald Weltea4967832019-12-05 22:04:57 +0100134/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
135static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
136{
137 struct bankd_worker *worker;
138 pthread_mutex_lock(&g_bankd->workers_mutex);
139 llist_for_each_entry(worker, &g_bankd->workers, list) {
140 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
141 continue;
142 if (cs && (cs->client_id != worker->client.clslot.client_id ||
143 cs->slot_nr != worker->client.clslot.slot_nr))
144 continue;
145
146 pthread_kill(worker->thread, sig);
147 break;
148 }
149 pthread_mutex_unlock(&g_bankd->workers_mutex);
150}
151
Harald Weltec650a4d2019-12-04 15:02:27 +0100152/* Remove a mapping */
153static void bankd_srvc_remove_mapping(struct slot_mapping *map)
154{
155 struct bank_slot bs = map->bank;
156
157 slotmap_del(g_bankd->slotmaps, map);
158
159 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100160 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100161}
162
Harald Welte707c85a2019-03-09 12:56:35 +0100163/* handle incoming messages from server */
164static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
165{
Harald Welte454f5e22019-03-09 21:38:34 +0100166 const CreateMappingReq_t *creq = NULL;
167 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100168 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100169 struct slot_mapping *map;
170 struct bank_slot bs;
171 struct client_slot cs;
172 RsproPDU_t *resp;
173
Harald Welte079c0682022-04-27 09:26:52 +0200174 LOGPFSML(srvc->fi, LOGL_DEBUG, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100175
176 switch (pdu->msg.present) {
177 case RsproPDUchoice_PR_connectBankRes:
Harald Welte5ae0f312022-04-25 15:53:04 +0200178 if (pdu->msg.choice.connectBankRes.identity.type != ComponentType_remsimServer) {
179 LOGPFSML(srvc->fi, LOGL_ERROR, "Server connection to a ComponentType(%ld) != RemsimServer? "
180 "Check your IP/Port configuration\n",
181 pdu->msg.choice.connectBankRes.identity.type);
182 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_DISCONNECT, NULL);
183 return -1;
184 }
Harald Welte707c85a2019-03-09 12:56:35 +0100185 /* Store 'identity' of server in srvc->peer_comp_id */
186 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
187 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
188 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100189 case RsproPDUchoice_PR_createMappingReq:
190 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200191 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100192 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200193 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100194 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200195 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100196 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200197 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100198 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100199 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100200 rspro2bank_slot(&bs, &creq->bank);
201 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100202 /* check if map exists */
203 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
204 if (map) {
205 if (client_slot_equals(&map->client, &cs)) {
206 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
207 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
208 goto send_resp;
209 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200210 LOGPFSML(srvc->fi, LOGL_NOTICE, "implicitly removing slotmap\n");
Harald Weltee6fa46a2019-12-04 15:06:33 +0100211 bankd_srvc_remove_mapping(map);
212 }
213 }
Harald Welte454f5e22019-03-09 21:38:34 +0100214 /* Add a new mapping */
215 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100216 if (!map) {
217 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100218 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100219 } else {
220 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100221 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100222 }
Harald Welte454f5e22019-03-09 21:38:34 +0100223 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100224send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100225 server_conn_send_rspro(srvc, resp);
226 break;
227 case RsproPDUchoice_PR_removeMappingReq:
228 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200229 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100230 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100231 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100232 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200233 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100234 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100235 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100236 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100237 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100238 rspro2bank_slot(&bs, &rreq->bank);
239 /* Remove a mapping */
240 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100241 if (!map) {
242 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100243 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100244 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100245 rspro2client_slot(&cs, &rreq->client);
246 if (!client_slot_equals(&map->client, &cs)) {
Harald Welte079c0682022-04-27 09:26:52 +0200247 LOGPFSML(srvc->fi, LOGL_NOTICE, "ClientId in removeMappingReq != map\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100248 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
249 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200250 LOGPFSML(srvc->fi, LOGL_INFO, "removing slotmap\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100251 bankd_srvc_remove_mapping(map);
252 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
253 }
Harald Welte454f5e22019-03-09 21:38:34 +0100254 }
255 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100256 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100257 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100258 case RsproPDUchoice_PR_resetStateReq:
259 /* delete all slotmaps */
260 slotmap_del_all(g_bankd->slotmaps);
261 /* notify all workers about maps having disappeared */
262 pthread_mutex_lock(&g_bankd->workers_mutex);
263 llist_for_each_entry(worker, &g_bankd->workers, list) {
264 pthread_kill(worker->thread, SIGMAPDEL);
265 }
266 pthread_mutex_unlock(&g_bankd->workers_mutex);
267 /* send response to server */
268 resp = rspro_gen_ResetStateRes(ResultCode_ok);
269 server_conn_send_rspro(srvc, resp);
270 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100271 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100272 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
273 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100274 return -1;
275 }
276
277 return 0;
278}
279
Harald Welte5bae20b2019-04-01 09:36:42 +0200280static void printf_help()
281{
282 printf(
283" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100284" -V --version Print the version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +0200285" -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200286" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
287" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
Harald Welte55f12b82022-01-16 14:34:12 +0100288" -b --bank-id <1-1023> Bank Identifier of this SIM bank (default: 1)\n"
289" -n --num-slots <1-1023> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200290" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
291" connections (default: INADDR_ANY)\n"
292" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
293" connectionss (default: 9999)\n"
294 );
295}
296
297static int g_bind_port = 9999;
298static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100299
Harald Weltef8c6eeb2021-12-08 20:33:00 +0100300static void handle_options(int argc, char **argv)
Harald Welte707c85a2019-03-09 12:56:35 +0100301{
Harald Welte5bae20b2019-04-01 09:36:42 +0200302 while (1) {
303 int option_index = 0, c;
304 static const struct option long_options[] = {
305 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100306 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +0200307 { "debug", 1, 0, 'd' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200308 { "server-host", 1, 0, 'i' },
309 { "server-port", 1, 0, 'p' },
310 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200311 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200312 { "component-name", 1, 0, 'N' },
313 { "bind-ip", 1, 0, 'I' },
314 { "bind-port", 1, 0, 'P' },
315 { 0, 0, 0, 0 }
316 };
317
Harald Welte80a01102020-05-25 14:55:12 +0200318 c = getopt_long(argc, argv, "hVd:i:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200319 if (c == -1)
320 break;
321
322 switch (c) {
323 case 'h':
324 printf_help();
325 exit(0);
326 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100327 case 'V':
328 printf("osmo-remsim-bankd version %s\n", VERSION);
329 exit(0);
330 break;
Harald Welte80a01102020-05-25 14:55:12 +0200331 case 'd':
332 log_parse_category_mask(osmo_stderr_target, optarg);
333 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200334 case 'i':
335 g_bankd->srvc.server_host = optarg;
336 break;
337 case 'p':
338 g_bankd->srvc.server_port = atoi(optarg);
339 break;
340 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200341 g_bankd->srvc.bankd.bank_id = atoi(optarg);
342 break;
343 case 'n':
344 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200345 break;
346 case 'N':
347 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
348 break;
349 case 'I':
350 g_bind_ip = optarg;
351 break;
352 case 'P':
353 g_bind_port = atoi(optarg);
354 break;
355 }
356 }
Harald Welte707c85a2019-03-09 12:56:35 +0100357}
358
Harald Welte77911b02018-08-14 23:47:30 +0200359int main(int argc, char **argv)
360{
Harald Weltef4b16f12019-03-09 20:58:17 +0100361 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200362 int i, rc;
363
Harald Weltef4b16f12019-03-09 20:58:17 +0100364 g_bankd = talloc_zero(NULL, struct bankd);
365 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200366
Harald Welteeea631b2022-05-03 15:14:19 +0200367 if (gethostname(g_hostname, sizeof(g_hostname)) < 0)
368 OSMO_STRLCPY_ARRAY(g_hostname, "unknown");
369
Harald Weltef4b16f12019-03-09 20:58:17 +0100370 bankd_init(g_bankd);
371
372 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100373 srvc->server_host = "localhost";
374 srvc->server_port = 9998;
375 srvc->handle_rx = bankd_srvc_handle_rx;
376 srvc->own_comp_id.type = ComponentType_remsimBankd;
Harald Welteeea631b2022-05-03 15:14:19 +0200377 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, g_hostname);
Harald Welte707c85a2019-03-09 12:56:35 +0100378 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
379 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
380
381 handle_options(argc, argv);
382
Harald Welte25075972019-03-11 17:33:17 +0100383 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100384 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100385 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100386 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100387
Harald Weltee89fecd2019-04-04 09:23:13 +0200388 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
389 * read once during bankd initialization, when the worker threads haven't
390 * started yet */
Harald Weltee9e505c2022-05-03 11:58:01 +0200391 rc = bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv");
392 if (rc)
393 exit(1);
Harald Weltee89fecd2019-04-04 09:23:13 +0200394
Harald Welte707c85a2019-03-09 12:56:35 +0100395 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100396 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100397 if (rc < 0) {
398 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
399 exit(1);
400 }
Harald Welted2192e22019-11-07 18:10:57 +0100401 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100402
403 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200404 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 +0200405 if (rc < 0) {
406 fprintf(stderr, "Unable to create TCP socket at %s:%d: %s\n",
407 g_bind_ip ? g_bind_ip : "INADDR_ANY", g_bind_port, strerror(errno));
Harald Welte12534e72018-08-15 23:37:29 +0200408 exit(1);
Harald Weltee9e505c2022-05-03 11:58:01 +0200409 }
Harald Weltef4b16f12019-03-09 20:58:17 +0100410 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200411
Harald Weltea0f39502019-03-09 20:59:34 +0100412 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200413 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200414 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100415 w = bankd_create_worker(g_bankd, i);
Harald Weltee9e505c2022-05-03 11:58:01 +0200416 if (!w) {
417 fprintf(stderr, "Error creating bankd worker thread\n");
Harald Welte77911b02018-08-14 23:47:30 +0200418 exit(21);
Harald Weltee9e505c2022-05-03 11:58:01 +0200419 }
Harald Welte77911b02018-08-14 23:47:30 +0200420 }
421
422 while (1) {
423 if (terminate)
424 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100425 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200426 }
427
Harald Weltef4b16f12019-03-09 20:58:17 +0100428 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200429 exit(0);
430}
431
432
433
434/***********************************************************************
435 * bankd worker thread
436 ***********************************************************************/
437
Harald Welte00a96732019-03-11 17:18:02 +0100438static __thread struct bankd_worker *g_worker;
439
Harald Welte8d858292018-08-15 23:36:46 +0200440struct value_string worker_state_names[] = {
441 { BW_ST_INIT, "INIT" },
442 { BW_ST_ACCEPTING, "ACCEPTING" },
443 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
444 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200445 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200446 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
447 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100448 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200449 { 0, NULL }
450};
451
Harald Welte1f699b42019-03-28 13:41:08 +0100452static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
453
Harald Welte8d858292018-08-15 23:36:46 +0200454static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
455{
456 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
457 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200458 worker->timeout = 0;
459}
460
461static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
462 unsigned int timeout_secs)
463{
464 LOGW(worker, "Changing state to %s (timeout=%u)\n",
465 get_value_string(worker_state_names, new_state), timeout_secs);
466 worker->state = new_state;
467 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200468}
Harald Welte77911b02018-08-14 23:47:30 +0200469
Harald Welte00a96732019-03-11 17:18:02 +0100470/* signal handler for receiving SIGMAPDEL from main thread */
471static void handle_sig_mapdel(int sig)
472{
473 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
474 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100475 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
476 g_worker->slot.bank_id = 0xffff;
477 g_worker->slot.slot_nr = 0xffff;
478 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
479 }
Harald Welte00a96732019-03-11 17:18:02 +0100480}
481
Harald Welte0fd77a52019-12-05 22:05:39 +0100482/* signal handler for receiving SIGMAPADD from main thread */
483static void handle_sig_mapadd(int sig)
484{
485 LOGW(g_worker, "SIGMAPADD received\n");
486 /* do nothing */
487}
488
Harald Welte25075972019-03-11 17:33:17 +0100489static void handle_sig_usr1(int sig)
490{
491 OSMO_ASSERT(sig == SIGUSR1);
492
493 if (pthread_equal(g_bankd->main, pthread_self())) {
494 struct bankd_worker *worker;
495 /* main thread */
496 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200497 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100498
499 /* iterate over worker threads and ask them to dump their talloc state */
500 pthread_mutex_lock(&g_bankd->workers_mutex);
501 llist_for_each_entry(worker, &g_bankd->workers, list) {
502 pthread_kill(worker->thread, SIGUSR1);
503 }
504 pthread_mutex_unlock(&g_bankd->workers_mutex);
505 } else {
506 /* worker thread */
507 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200508 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100509 }
510}
511
Harald Welte77911b02018-08-14 23:47:30 +0200512static void worker_cleanup(void *arg)
513{
514 struct bankd_worker *worker = (struct bankd_worker *) arg;
515 struct bankd *bankd = worker->bankd;
516
517 /* FIXME: should we still do this? in the thread ?!? */
518 pthread_mutex_lock(&bankd->workers_mutex);
519 llist_del(&worker->list);
520 talloc_free(worker); /* FIXME: is this safe? */
521 pthread_mutex_unlock(&bankd->workers_mutex);
522}
523
Harald Welteaf614732018-08-17 22:10:05 +0200524static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200525{
Harald Welte297d72e2019-03-28 18:42:35 +0100526 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200527
Harald Welte150d6d62018-10-03 23:07:47 +0200528 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
529
Harald Welte694df832018-10-03 22:47:52 +0200530 if (!worker->reader.name) {
531 /* resolve PC/SC reader name from slot_id -> name map */
532 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
533 if (!worker->reader.name) {
534 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
535 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100536 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200537 }
538 }
Harald Welte45c948c2018-09-23 19:26:52 +0200539 OSMO_ASSERT(worker->reader.name);
540
Harald Welte297d72e2019-03-28 18:42:35 +0100541 rc = worker->ops->open_card(worker);
542 if (rc < 0)
543 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100544
Harald Welte57593f02018-09-23 19:30:31 +0200545 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200546 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200547
Harald Welteaf614732018-08-17 22:10:05 +0200548 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200549}
Harald Welte77911b02018-08-14 23:47:30 +0200550
551
Harald Welte00a96732019-03-11 17:18:02 +0100552static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200553{
554 struct ipaccess_head *hh;
555 uint16_t len;
556 int needed, rc;
557
558 if (buf_size < sizeof(*hh))
559 return -1;
560
561 hh = (struct ipaccess_head *) buf;
562
Harald Welte00a96732019-03-11 17:18:02 +0100563 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
564 * in case of a signal being received */
565
566restart_hdr:
567 /* 1) blocking recv from the socket (IPA header) */
568 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
569 if (rc == -1 && errno == EINTR) {
570 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
571 return -23;
572 goto restart_hdr;
573 } else if (rc < 0)
574 return rc;
575 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200576 return -2;
577
578 len = ntohs(hh->len);
579 needed = len; //- sizeof(*hh);
580
Harald Welte00a96732019-03-11 17:18:02 +0100581restart_body:
582 /* 2) blocking recv from the socket (payload) */
583 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
584 if (rc == -1 && errno == EINTR) {
585 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
586 return -23;
587 goto restart_body;
588 } else if (rc < 0)
589 return rc;
590 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200591 return -3;
592
593 return len;
594}
595
Harald Welte796a7492018-09-23 19:31:28 +0200596static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
597{
598 struct msgb *msg = rspro_enc_msg(pdu);
599 int rc;
600
601 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100602 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200603 LOGW(worker, "error encoding RSPRO\n");
604 return -1;
605 }
606
Harald Weltefd471192018-09-24 14:51:14 +0200607 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200608 /* prepend the header */
609 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200610 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200611
612 /* actually send it through the socket */
613 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
614 if (rc == msgb_length(msg))
615 rc = 0;
616 else {
617 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
618 rc = -1;
619 }
620
621 msgb_free(msg);
622
623 return rc;
624}
625
Harald Welte150d6d62018-10-03 23:07:47 +0200626/* attempt to obtain slot-map */
627static int worker_try_slotmap(struct bankd_worker *worker)
628{
Harald Weltecbd18962019-03-03 19:02:38 +0100629 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200630
Harald Weltecbd18962019-03-03 19:02:38 +0100631 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200632 if (!slmap) {
633 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
634 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
635 /* check in 10s if the map has been installed meanwhile by main thread */
636 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
637 return -1;
638 } else {
639 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
640 slmap->client.client_id, slmap->client.slot_nr,
641 slmap->bank.bank_id, slmap->bank.slot_nr);
642 worker->slot = slmap->bank;
643 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
644 return worker_open_card(worker);
645 }
646}
647
Harald Welte1f699b42019-03-28 13:41:08 +0100648/* inform the remote end (client) about the (new) ATR */
649static int worker_send_atr(struct bankd_worker *worker)
650{
651 RsproPDU_t *set_atr;
652 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
653 worker->client.clslot.slot_nr,
654 worker->card.atr, worker->card.atr_len);
655 if (!set_atr)
656 return -1;
657 return worker_send_rspro(worker, set_atr);
658}
Harald Welte150d6d62018-10-03 23:07:47 +0200659
Harald Weltecce2aad2018-08-16 14:44:37 +0200660static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
661{
Harald Welteaf614732018-08-17 22:10:05 +0200662 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100663 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200664 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100665 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200666
Harald Weltecce2aad2018-08-16 14:44:37 +0200667 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
668
Harald Welte9f7ca612021-12-08 15:29:28 +0100669 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200670 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
671 /* FIXME: store somewhere? */
672
673 if (worker->state != BW_ST_CONN_WAIT_ID) {
674 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100675 rc = -102;
676 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200677 }
678
Harald Welte371d0262018-08-16 15:23:58 +0200679 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200680 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100681 res = ResultCode_illegalClientId;
682 rc = -103;
683 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200684 }
Harald Welte371d0262018-08-16 15:23:58 +0200685 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
686 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200687 worker_set_state(worker, BW_ST_CONN_CLIENT);
688
Harald Welte150d6d62018-10-03 23:07:47 +0200689 if (worker_try_slotmap(worker) >= 0)
690 res = ResultCode_ok;
691 else
Harald Welte3e689872018-09-24 14:52:56 +0200692 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200693
Harald Welte3e689872018-09-24 14:52:56 +0200694 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100695 rc = worker_send_rspro(worker, resp);
696 if (rc < 0)
697 return rc;
698
699 if (res == ResultCode_ok)
700 rc = worker_send_atr(worker);
701
702 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100703
704respond_and_err:
705 if (res) {
706 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
707 worker_send_rspro(worker, resp);
708 }
709 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200710}
711
Harald Welte796a7492018-09-23 19:31:28 +0200712static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
713{
714 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200715 uint8_t rx_buf[1024];
716 DWORD rx_buf_len = sizeof(rx_buf);
717 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100718 struct client_slot clslot;
719 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100720 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200721
Harald Welte9f7ca612021-12-08 15:29:28 +0100722 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
723 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200724
725 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
726 LOGW(worker, "Unexpected tpduModemToCaard\n");
727 return -104;
728 }
729
Harald Weltee1d32892019-03-27 20:47:42 +0100730 /* Validate that toBankSlot / fromClientSlot match our expectations */
731 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
732 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
733 if (!bank_slot_equals(&worker->slot, &bslot)) {
734 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
735 bslot.bank_id, bslot.slot_nr);
736 return -105;
737 }
738 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
739 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
740 clslot.client_id, clslot.slot_nr);
741 return -106;
742 }
Harald Welte796a7492018-09-23 19:31:28 +0200743
Harald Welte297d72e2019-03-28 18:42:35 +0100744 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
745 rx_buf, &rx_buf_len);
746 if (rc < 0)
747 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200748
749 /* encode response PDU and send it */
750 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
751 rx_buf, rx_buf_len);
752 worker_send_rspro(worker, pdu_resp);
753
754 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200755}
756
Harald Weltebbd18bd2019-12-16 13:11:50 +0100757static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
758{
759 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
760 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
761 int rc = 0;
762
Harald Welte9f7ca612021-12-08 15:29:28 +0100763 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100764 sps->resetActive ? "ACTIVE" : "INACTIVE",
765 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
766 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
767
768 /* perform cold or warm reset */
769 if (sps->vccPresent && *sps->vccPresent == 0)
770 rc = worker->ops->reset_card(worker, true);
771 else if (sps->resetActive)
772 rc = worker->ops->reset_card(worker, false);
773
774 return rc;
775}
776
Harald Welte77911b02018-08-14 23:47:30 +0200777/* handle one incoming RSPRO message from a client inside a worker thread */
778static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
779{
Harald Weltecce2aad2018-08-16 14:44:37 +0200780 int rc = -100;
781
Harald Welte77911b02018-08-14 23:47:30 +0200782 switch (pdu->msg.present) {
783 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200784 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200785 break;
786 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200787 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200788 break;
789 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100790 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200791 rc = 0;
792 break;
793 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100794 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200795 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200796 break;
797 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100798 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200799 rc = -101;
800 break;
Harald Welte77911b02018-08-14 23:47:30 +0200801 }
802
Harald Weltecce2aad2018-08-16 14:44:37 +0200803 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200804}
805
Harald Welte694df832018-10-03 22:47:52 +0200806static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
807{
808 struct timeval tout = { timeout_secs, 0 };
809 fd_set readset;
810
811 FD_ZERO(&readset);
812 FD_SET(fd, &readset);
813 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
814}
815
Harald Welte77911b02018-08-14 23:47:30 +0200816/* body of the main transceive loop */
817static int worker_transceive_loop(struct bankd_worker *worker)
818{
819 struct ipaccess_head *hh;
820 struct ipaccess_head_ext *hh_ext;
821 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
822 asn_dec_rval_t rval;
823 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200824 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200825
Harald Welte00a96732019-03-11 17:18:02 +0100826restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200827 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100828 if (rc == -1 && errno == EINTR) {
829 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
830 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100831 else
832 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100833 goto restart_wait;
834 } else if (rc < 0)
835 return rc;
836 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200837 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200838 switch (worker->state) {
839 case BW_ST_CONN_CLIENT_WAIT_MAP:
840 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100841 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200842 break;
843 case BW_ST_CONN_CLIENT_MAPPED:
844 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100845 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200846 break;
847 default:
848 OSMO_ASSERT(0);
849 }
Harald Welte1f699b42019-03-28 13:41:08 +0100850 if (rc == 0)
851 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200852 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200853 return 0;
854 };
855
Harald Welte77911b02018-08-14 23:47:30 +0200856 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100857 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200858 if (rc < 0)
859 return rc;
860 data_len = rc;
861
862 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200863 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200864 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200865 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200866 }
Harald Welte77911b02018-08-14 23:47:30 +0200867
Harald Welte5a3613a2018-10-11 12:56:21 +0200868 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100869 switch (hh->data[0]) {
870 case IPAC_MSGT_PING:
871 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100872 case IPAC_MSGT_ID_ACK:
873 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100874 default:
875 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
876 break;
877 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200878 return 0;
879 }
880
Harald Welte77911b02018-08-14 23:47:30 +0200881 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200882 if (data_len < sizeof(*hh_ext)) {
883 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200884 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200885 }
Harald Welte77911b02018-08-14 23:47:30 +0200886 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200887 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
888 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200889 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200890 }
Harald Welte77911b02018-08-14 23:47:30 +0200891
892 /* 2) ASN1 BER decode of the message */
893 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200894 if (rval.code != RC_OK) {
895 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200896 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200897 }
Harald Welte77911b02018-08-14 23:47:30 +0200898
899 /* 3) handling of the message, possibly resulting in PCSC commands */
900 rc = worker_handle_rspro(worker, pdu);
901 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200902 if (rc < 0) {
903 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200904 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200905 }
Harald Welte77911b02018-08-14 23:47:30 +0200906
907 /* everything OK if we reach here */
908 return 0;
909}
910
Harald Welted6dfb8c2018-08-16 14:46:53 +0200911/* obtain an ascii representation of the client IP/port */
912static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
913{
914 char hostbuf[32], portbuf[32];
915 int rc;
916
917 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
918 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
919 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
920 if (rc != 0) {
921 out[0] = '\0';
922 return -1;
923 }
924 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
925 return 0;
926}
927
Harald Welte77911b02018-08-14 23:47:30 +0200928/* worker thread main function */
929static void *worker_main(void *arg)
930{
Harald Welte77911b02018-08-14 23:47:30 +0200931 void *top_ctx;
932 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200933
Harald Welte00a96732019-03-11 17:18:02 +0100934 g_worker = (struct bankd_worker *) arg;
935
Harald Welte00a96732019-03-11 17:18:02 +0100936 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200937
Harald Welte77911b02018-08-14 23:47:30 +0200938 /* not permitted in multithreaded environment */
939 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100940 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
941 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200942
Harald Welte286a2be2019-03-11 17:36:58 +0100943 /* set the thread name */
944 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
945 pthread_setname_np(pthread_self(), g_worker->name);
946
Harald Welte77911b02018-08-14 23:47:30 +0200947 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100948 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200949
Harald Welte602b6f72019-12-04 21:51:02 +0100950 g_worker->slot.bank_id = 0xffff;
951 g_worker->slot.slot_nr = 0xffff;
952
Harald Welte77911b02018-08-14 23:47:30 +0200953 /* we continuously perform the same loop here, recycling the worker thread
954 * once the client connection is gone or we have some trouble with the card/reader */
955 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200956 char buf[128];
957
Harald Welte00a96732019-03-11 17:18:02 +0100958 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200959
Harald Welte00a96732019-03-11 17:18:02 +0100960 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200961 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100962 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
963 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200964 if (rc < 0) {
965 continue;
966 }
Harald Welte00a96732019-03-11 17:18:02 +0100967 g_worker->client.fd = rc;
968 worker_client_addrstr(buf, sizeof(buf), g_worker);
969 LOGW(g_worker, "Accepted connection from %s\n", buf);
970 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200971
972 /* run the main worker transceive loop body until there was some error */
973 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100974 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200975 if (rc < 0)
976 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100977 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
978 break;
Harald Welte77911b02018-08-14 23:47:30 +0200979 }
980
Harald Welte00a96732019-03-11 17:18:02 +0100981 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200982
Harald Welte77911b02018-08-14 23:47:30 +0200983 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100984 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100985 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100986 if (g_worker->reader.name)
987 g_worker->reader.name = NULL;
988 if (g_worker->client.fd >= 0)
989 close(g_worker->client.fd);
990 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
991 g_worker->client.fd = -1;
992 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200993 }
994
995 pthread_cleanup_pop(1);
996 talloc_free(top_ctx);
997 pthread_exit(NULL);
998}