blob: d8dab0328d869d0ab1f3b05effc4d22c9cb7ef7f [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 Welte77911b02018-08-14 23:47:30 +020062
63static void *worker_main(void *arg);
64
65/***********************************************************************
66* bankd core / main thread
67***********************************************************************/
68
Harald Welte43ab79f2018-10-03 23:34:21 +020069int asn_debug;
70
Harald Welte77911b02018-08-14 23:47:30 +020071static void bankd_init(struct bankd *bankd)
72{
Harald Welte25075972019-03-11 17:33:17 +010073 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Weltef94b9ee2018-09-25 15:04:21 +020074 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +010075 log_set_print_level(osmo_stderr_target, 1);
76 log_set_print_category(osmo_stderr_target, 1);
77 log_set_print_category_hex(osmo_stderr_target, 0);
78 osmo_fsm_log_addr(0);
Harald Weltea9d7ad12021-12-08 21:09:12 +010079 log_set_print_tid(osmo_stderr_target, 1);
80 log_enable_multithread();
Harald Weltef94b9ee2018-09-25 15:04:21 +020081
Harald Welte43ab79f2018-10-03 23:34:21 +020082 asn_debug = 0;
83
Harald Welte77911b02018-08-14 23:47:30 +020084 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010085 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020086 INIT_LLIST_HEAD(&bankd->workers);
87 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020088
Harald Weltea0f39502019-03-09 20:59:34 +010089 /* set some defaults, overridden by commandline/config */
Harald Welte2513d812019-04-01 21:03:02 +020090 bankd->srvc.bankd.bank_id = 1;
91 bankd->srvc.bankd.num_slots = 8;
Harald Weltea0f39502019-03-09 20:59:34 +010092
Harald Weltef1dd1622018-09-24 14:54:23 +020093 bankd->comp_id.type = ComponentType_remsimBankd;
94 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
95 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
96 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
97 /* FIXME: other members of app_comp_id */
98
Harald Welte45c948c2018-09-23 19:26:52 +020099 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
Harald Welte77911b02018-08-14 23:47:30 +0200100}
101
102/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200103static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200104{
105 struct bankd_worker *worker;
106 int rc;
107
108 worker = talloc_zero(bankd, struct bankd_worker);
109 if (!worker)
110 return NULL;
111
112 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200113 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100114 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200115
116 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
117
118 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
119 if (rc != 0) {
120 talloc_free(worker);
121 return NULL;
122 }
123
124 pthread_mutex_lock(&bankd->workers_mutex);
125 llist_add_tail(&worker->list, &bankd->workers);
126 pthread_mutex_unlock(&bankd->workers_mutex);
127
128 return worker;
129}
130
131static bool terminate = false;
132
Harald Weltea4967832019-12-05 22:04:57 +0100133/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
134static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
135{
136 struct bankd_worker *worker;
137 pthread_mutex_lock(&g_bankd->workers_mutex);
138 llist_for_each_entry(worker, &g_bankd->workers, list) {
139 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
140 continue;
141 if (cs && (cs->client_id != worker->client.clslot.client_id ||
142 cs->slot_nr != worker->client.clslot.slot_nr))
143 continue;
144
145 pthread_kill(worker->thread, sig);
146 break;
147 }
148 pthread_mutex_unlock(&g_bankd->workers_mutex);
149}
150
Harald Weltec650a4d2019-12-04 15:02:27 +0100151/* Remove a mapping */
152static void bankd_srvc_remove_mapping(struct slot_mapping *map)
153{
154 struct bank_slot bs = map->bank;
155
156 slotmap_del(g_bankd->slotmaps, map);
157
158 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100159 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100160}
161
Harald Welte707c85a2019-03-09 12:56:35 +0100162/* handle incoming messages from server */
163static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
164{
Harald Welte454f5e22019-03-09 21:38:34 +0100165 const CreateMappingReq_t *creq = NULL;
166 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100167 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100168 struct slot_mapping *map;
169 struct bank_slot bs;
170 struct client_slot cs;
171 RsproPDU_t *resp;
172
Harald Welte079c0682022-04-27 09:26:52 +0200173 LOGPFSML(srvc->fi, LOGL_DEBUG, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100174
175 switch (pdu->msg.present) {
176 case RsproPDUchoice_PR_connectBankRes:
Harald Welte5ae0f312022-04-25 15:53:04 +0200177 if (pdu->msg.choice.connectBankRes.identity.type != ComponentType_remsimServer) {
178 LOGPFSML(srvc->fi, LOGL_ERROR, "Server connection to a ComponentType(%ld) != RemsimServer? "
179 "Check your IP/Port configuration\n",
180 pdu->msg.choice.connectBankRes.identity.type);
181 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_DISCONNECT, NULL);
182 return -1;
183 }
Harald Welte707c85a2019-03-09 12:56:35 +0100184 /* Store 'identity' of server in srvc->peer_comp_id */
185 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
186 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
187 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100188 case RsproPDUchoice_PR_createMappingReq:
189 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200190 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100191 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200192 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100193 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200194 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100195 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200196 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100197 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100198 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100199 rspro2bank_slot(&bs, &creq->bank);
200 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100201 /* check if map exists */
202 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
203 if (map) {
204 if (client_slot_equals(&map->client, &cs)) {
205 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
206 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
207 goto send_resp;
208 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200209 LOGPFSML(srvc->fi, LOGL_NOTICE, "implicitly removing slotmap\n");
Harald Weltee6fa46a2019-12-04 15:06:33 +0100210 bankd_srvc_remove_mapping(map);
211 }
212 }
Harald Welte454f5e22019-03-09 21:38:34 +0100213 /* Add a new mapping */
214 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100215 if (!map) {
216 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100217 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100218 } else {
219 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100220 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100221 }
Harald Welte454f5e22019-03-09 21:38:34 +0100222 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100223send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100224 server_conn_send_rspro(srvc, resp);
225 break;
226 case RsproPDUchoice_PR_removeMappingReq:
227 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200228 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100229 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100230 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100231 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200232 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100233 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100234 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100235 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100236 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100237 rspro2bank_slot(&bs, &rreq->bank);
238 /* Remove a mapping */
239 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100240 if (!map) {
241 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100242 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100243 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100244 rspro2client_slot(&cs, &rreq->client);
245 if (!client_slot_equals(&map->client, &cs)) {
Harald Welte079c0682022-04-27 09:26:52 +0200246 LOGPFSML(srvc->fi, LOGL_NOTICE, "ClientId in removeMappingReq != map\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100247 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
248 } else {
Harald Welte079c0682022-04-27 09:26:52 +0200249 LOGPFSML(srvc->fi, LOGL_INFO, "removing slotmap\n");
Harald Welted9fb9392019-12-04 19:05:01 +0100250 bankd_srvc_remove_mapping(map);
251 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
252 }
Harald Welte454f5e22019-03-09 21:38:34 +0100253 }
254 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100255 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100256 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100257 case RsproPDUchoice_PR_resetStateReq:
258 /* delete all slotmaps */
259 slotmap_del_all(g_bankd->slotmaps);
260 /* notify all workers about maps having disappeared */
261 pthread_mutex_lock(&g_bankd->workers_mutex);
262 llist_for_each_entry(worker, &g_bankd->workers, list) {
263 pthread_kill(worker->thread, SIGMAPDEL);
264 }
265 pthread_mutex_unlock(&g_bankd->workers_mutex);
266 /* send response to server */
267 resp = rspro_gen_ResetStateRes(ResultCode_ok);
268 server_conn_send_rspro(srvc, resp);
269 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100270 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100271 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
272 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100273 return -1;
274 }
275
276 return 0;
277}
278
Harald Welte5bae20b2019-04-01 09:36:42 +0200279static void printf_help()
280{
281 printf(
282" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100283" -V --version Print the version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +0200284" -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200285" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
286" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
Harald Welte55f12b82022-01-16 14:34:12 +0100287" -b --bank-id <1-1023> Bank Identifier of this SIM bank (default: 1)\n"
288" -n --num-slots <1-1023> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200289" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
290" connections (default: INADDR_ANY)\n"
291" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
292" connectionss (default: 9999)\n"
293 );
294}
295
296static int g_bind_port = 9999;
297static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100298
Harald Weltef8c6eeb2021-12-08 20:33:00 +0100299static void handle_options(int argc, char **argv)
Harald Welte707c85a2019-03-09 12:56:35 +0100300{
Harald Welte5bae20b2019-04-01 09:36:42 +0200301 while (1) {
302 int option_index = 0, c;
303 static const struct option long_options[] = {
304 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100305 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +0200306 { "debug", 1, 0, 'd' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200307 { "server-host", 1, 0, 'i' },
308 { "server-port", 1, 0, 'p' },
309 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200310 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200311 { "component-name", 1, 0, 'N' },
312 { "bind-ip", 1, 0, 'I' },
313 { "bind-port", 1, 0, 'P' },
314 { 0, 0, 0, 0 }
315 };
316
Harald Welte80a01102020-05-25 14:55:12 +0200317 c = getopt_long(argc, argv, "hVd:i:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200318 if (c == -1)
319 break;
320
321 switch (c) {
322 case 'h':
323 printf_help();
324 exit(0);
325 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100326 case 'V':
327 printf("osmo-remsim-bankd version %s\n", VERSION);
328 exit(0);
329 break;
Harald Welte80a01102020-05-25 14:55:12 +0200330 case 'd':
331 log_parse_category_mask(osmo_stderr_target, optarg);
332 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200333 case 'i':
334 g_bankd->srvc.server_host = optarg;
335 break;
336 case 'p':
337 g_bankd->srvc.server_port = atoi(optarg);
338 break;
339 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200340 g_bankd->srvc.bankd.bank_id = atoi(optarg);
341 break;
342 case 'n':
343 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200344 break;
345 case 'N':
346 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
347 break;
348 case 'I':
349 g_bind_ip = optarg;
350 break;
351 case 'P':
352 g_bind_port = atoi(optarg);
353 break;
354 }
355 }
Harald Welte707c85a2019-03-09 12:56:35 +0100356}
357
Harald Welte77911b02018-08-14 23:47:30 +0200358int main(int argc, char **argv)
359{
Harald Weltef4b16f12019-03-09 20:58:17 +0100360 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200361 int i, rc;
362
Harald Weltef4b16f12019-03-09 20:58:17 +0100363 g_bankd = talloc_zero(NULL, struct bankd);
364 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200365
Harald Weltef4b16f12019-03-09 20:58:17 +0100366 bankd_init(g_bankd);
367
368 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100369 srvc->server_host = "localhost";
370 srvc->server_port = 9998;
371 srvc->handle_rx = bankd_srvc_handle_rx;
372 srvc->own_comp_id.type = ComponentType_remsimBankd;
373 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
374 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
375 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
376
377 handle_options(argc, argv);
378
Harald Welte25075972019-03-11 17:33:17 +0100379 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100380 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100381 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100382 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100383
Harald Weltee89fecd2019-04-04 09:23:13 +0200384 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
385 * read once during bankd initialization, when the worker threads haven't
386 * started yet */
Harald Weltee9e505c2022-05-03 11:58:01 +0200387 rc = bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv");
388 if (rc)
389 exit(1);
Harald Weltee89fecd2019-04-04 09:23:13 +0200390
Harald Welte707c85a2019-03-09 12:56:35 +0100391 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100392 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100393 if (rc < 0) {
394 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
395 exit(1);
396 }
Harald Welted2192e22019-11-07 18:10:57 +0100397 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100398
399 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200400 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 +0200401 if (rc < 0) {
402 fprintf(stderr, "Unable to create TCP socket at %s:%d: %s\n",
403 g_bind_ip ? g_bind_ip : "INADDR_ANY", g_bind_port, strerror(errno));
Harald Welte12534e72018-08-15 23:37:29 +0200404 exit(1);
Harald Weltee9e505c2022-05-03 11:58:01 +0200405 }
Harald Weltef4b16f12019-03-09 20:58:17 +0100406 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200407
Harald Weltea0f39502019-03-09 20:59:34 +0100408 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200409 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200410 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100411 w = bankd_create_worker(g_bankd, i);
Harald Weltee9e505c2022-05-03 11:58:01 +0200412 if (!w) {
413 fprintf(stderr, "Error creating bankd worker thread\n");
Harald Welte77911b02018-08-14 23:47:30 +0200414 exit(21);
Harald Weltee9e505c2022-05-03 11:58:01 +0200415 }
Harald Welte77911b02018-08-14 23:47:30 +0200416 }
417
418 while (1) {
419 if (terminate)
420 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100421 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200422 }
423
Harald Weltef4b16f12019-03-09 20:58:17 +0100424 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200425 exit(0);
426}
427
428
429
430/***********************************************************************
431 * bankd worker thread
432 ***********************************************************************/
433
Harald Welte00a96732019-03-11 17:18:02 +0100434static __thread struct bankd_worker *g_worker;
435
Harald Welte8d858292018-08-15 23:36:46 +0200436struct value_string worker_state_names[] = {
437 { BW_ST_INIT, "INIT" },
438 { BW_ST_ACCEPTING, "ACCEPTING" },
439 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
440 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200441 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200442 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
443 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100444 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200445 { 0, NULL }
446};
447
Harald Welte1f699b42019-03-28 13:41:08 +0100448static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
449
Harald Welte8d858292018-08-15 23:36:46 +0200450static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
451{
452 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
453 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200454 worker->timeout = 0;
455}
456
457static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
458 unsigned int timeout_secs)
459{
460 LOGW(worker, "Changing state to %s (timeout=%u)\n",
461 get_value_string(worker_state_names, new_state), timeout_secs);
462 worker->state = new_state;
463 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200464}
Harald Welte77911b02018-08-14 23:47:30 +0200465
Harald Welte00a96732019-03-11 17:18:02 +0100466/* signal handler for receiving SIGMAPDEL from main thread */
467static void handle_sig_mapdel(int sig)
468{
469 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
470 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100471 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
472 g_worker->slot.bank_id = 0xffff;
473 g_worker->slot.slot_nr = 0xffff;
474 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
475 }
Harald Welte00a96732019-03-11 17:18:02 +0100476}
477
Harald Welte0fd77a52019-12-05 22:05:39 +0100478/* signal handler for receiving SIGMAPADD from main thread */
479static void handle_sig_mapadd(int sig)
480{
481 LOGW(g_worker, "SIGMAPADD received\n");
482 /* do nothing */
483}
484
Harald Welte25075972019-03-11 17:33:17 +0100485static void handle_sig_usr1(int sig)
486{
487 OSMO_ASSERT(sig == SIGUSR1);
488
489 if (pthread_equal(g_bankd->main, pthread_self())) {
490 struct bankd_worker *worker;
491 /* main thread */
492 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200493 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100494
495 /* iterate over worker threads and ask them to dump their talloc state */
496 pthread_mutex_lock(&g_bankd->workers_mutex);
497 llist_for_each_entry(worker, &g_bankd->workers, list) {
498 pthread_kill(worker->thread, SIGUSR1);
499 }
500 pthread_mutex_unlock(&g_bankd->workers_mutex);
501 } else {
502 /* worker thread */
503 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200504 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100505 }
506}
507
Harald Welte77911b02018-08-14 23:47:30 +0200508static void worker_cleanup(void *arg)
509{
510 struct bankd_worker *worker = (struct bankd_worker *) arg;
511 struct bankd *bankd = worker->bankd;
512
513 /* FIXME: should we still do this? in the thread ?!? */
514 pthread_mutex_lock(&bankd->workers_mutex);
515 llist_del(&worker->list);
516 talloc_free(worker); /* FIXME: is this safe? */
517 pthread_mutex_unlock(&bankd->workers_mutex);
518}
519
Harald Welteaf614732018-08-17 22:10:05 +0200520static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200521{
Harald Welte297d72e2019-03-28 18:42:35 +0100522 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200523
Harald Welte150d6d62018-10-03 23:07:47 +0200524 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
525
Harald Welte694df832018-10-03 22:47:52 +0200526 if (!worker->reader.name) {
527 /* resolve PC/SC reader name from slot_id -> name map */
528 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
529 if (!worker->reader.name) {
530 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
531 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100532 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200533 }
534 }
Harald Welte45c948c2018-09-23 19:26:52 +0200535 OSMO_ASSERT(worker->reader.name);
536
Harald Welte297d72e2019-03-28 18:42:35 +0100537 rc = worker->ops->open_card(worker);
538 if (rc < 0)
539 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100540
Harald Welte57593f02018-09-23 19:30:31 +0200541 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200542 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200543
Harald Welteaf614732018-08-17 22:10:05 +0200544 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200545}
Harald Welte77911b02018-08-14 23:47:30 +0200546
547
Harald Welte00a96732019-03-11 17:18:02 +0100548static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200549{
550 struct ipaccess_head *hh;
551 uint16_t len;
552 int needed, rc;
553
554 if (buf_size < sizeof(*hh))
555 return -1;
556
557 hh = (struct ipaccess_head *) buf;
558
Harald Welte00a96732019-03-11 17:18:02 +0100559 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
560 * in case of a signal being received */
561
562restart_hdr:
563 /* 1) blocking recv from the socket (IPA header) */
564 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
565 if (rc == -1 && errno == EINTR) {
566 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
567 return -23;
568 goto restart_hdr;
569 } else if (rc < 0)
570 return rc;
571 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200572 return -2;
573
574 len = ntohs(hh->len);
575 needed = len; //- sizeof(*hh);
576
Harald Welte00a96732019-03-11 17:18:02 +0100577restart_body:
578 /* 2) blocking recv from the socket (payload) */
579 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
580 if (rc == -1 && errno == EINTR) {
581 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
582 return -23;
583 goto restart_body;
584 } else if (rc < 0)
585 return rc;
586 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200587 return -3;
588
589 return len;
590}
591
Harald Welte796a7492018-09-23 19:31:28 +0200592static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
593{
594 struct msgb *msg = rspro_enc_msg(pdu);
595 int rc;
596
597 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100598 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200599 LOGW(worker, "error encoding RSPRO\n");
600 return -1;
601 }
602
Harald Weltefd471192018-09-24 14:51:14 +0200603 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200604 /* prepend the header */
605 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200606 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200607
608 /* actually send it through the socket */
609 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
610 if (rc == msgb_length(msg))
611 rc = 0;
612 else {
613 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
614 rc = -1;
615 }
616
617 msgb_free(msg);
618
619 return rc;
620}
621
Harald Welte150d6d62018-10-03 23:07:47 +0200622/* attempt to obtain slot-map */
623static int worker_try_slotmap(struct bankd_worker *worker)
624{
Harald Weltecbd18962019-03-03 19:02:38 +0100625 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200626
Harald Weltecbd18962019-03-03 19:02:38 +0100627 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200628 if (!slmap) {
629 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
630 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
631 /* check in 10s if the map has been installed meanwhile by main thread */
632 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
633 return -1;
634 } else {
635 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
636 slmap->client.client_id, slmap->client.slot_nr,
637 slmap->bank.bank_id, slmap->bank.slot_nr);
638 worker->slot = slmap->bank;
639 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
640 return worker_open_card(worker);
641 }
642}
643
Harald Welte1f699b42019-03-28 13:41:08 +0100644/* inform the remote end (client) about the (new) ATR */
645static int worker_send_atr(struct bankd_worker *worker)
646{
647 RsproPDU_t *set_atr;
648 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
649 worker->client.clslot.slot_nr,
650 worker->card.atr, worker->card.atr_len);
651 if (!set_atr)
652 return -1;
653 return worker_send_rspro(worker, set_atr);
654}
Harald Welte150d6d62018-10-03 23:07:47 +0200655
Harald Weltecce2aad2018-08-16 14:44:37 +0200656static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
657{
Harald Welteaf614732018-08-17 22:10:05 +0200658 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100659 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200660 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100661 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200662
Harald Weltecce2aad2018-08-16 14:44:37 +0200663 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
664
Harald Welte9f7ca612021-12-08 15:29:28 +0100665 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200666 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
667 /* FIXME: store somewhere? */
668
669 if (worker->state != BW_ST_CONN_WAIT_ID) {
670 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100671 rc = -102;
672 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200673 }
674
Harald Welte371d0262018-08-16 15:23:58 +0200675 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200676 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100677 res = ResultCode_illegalClientId;
678 rc = -103;
679 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200680 }
Harald Welte371d0262018-08-16 15:23:58 +0200681 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
682 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200683 worker_set_state(worker, BW_ST_CONN_CLIENT);
684
Harald Welte150d6d62018-10-03 23:07:47 +0200685 if (worker_try_slotmap(worker) >= 0)
686 res = ResultCode_ok;
687 else
Harald Welte3e689872018-09-24 14:52:56 +0200688 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200689
Harald Welte3e689872018-09-24 14:52:56 +0200690 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100691 rc = worker_send_rspro(worker, resp);
692 if (rc < 0)
693 return rc;
694
695 if (res == ResultCode_ok)
696 rc = worker_send_atr(worker);
697
698 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100699
700respond_and_err:
701 if (res) {
702 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
703 worker_send_rspro(worker, resp);
704 }
705 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200706}
707
Harald Welte796a7492018-09-23 19:31:28 +0200708static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
709{
710 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200711 uint8_t rx_buf[1024];
712 DWORD rx_buf_len = sizeof(rx_buf);
713 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100714 struct client_slot clslot;
715 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100716 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200717
Harald Welte9f7ca612021-12-08 15:29:28 +0100718 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
719 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200720
721 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
722 LOGW(worker, "Unexpected tpduModemToCaard\n");
723 return -104;
724 }
725
Harald Weltee1d32892019-03-27 20:47:42 +0100726 /* Validate that toBankSlot / fromClientSlot match our expectations */
727 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
728 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
729 if (!bank_slot_equals(&worker->slot, &bslot)) {
730 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
731 bslot.bank_id, bslot.slot_nr);
732 return -105;
733 }
734 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
735 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
736 clslot.client_id, clslot.slot_nr);
737 return -106;
738 }
Harald Welte796a7492018-09-23 19:31:28 +0200739
Harald Welte297d72e2019-03-28 18:42:35 +0100740 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
741 rx_buf, &rx_buf_len);
742 if (rc < 0)
743 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200744
745 /* encode response PDU and send it */
746 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
747 rx_buf, rx_buf_len);
748 worker_send_rspro(worker, pdu_resp);
749
750 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200751}
752
Harald Weltebbd18bd2019-12-16 13:11:50 +0100753static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
754{
755 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
756 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
757 int rc = 0;
758
Harald Welte9f7ca612021-12-08 15:29:28 +0100759 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100760 sps->resetActive ? "ACTIVE" : "INACTIVE",
761 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
762 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
763
764 /* perform cold or warm reset */
765 if (sps->vccPresent && *sps->vccPresent == 0)
766 rc = worker->ops->reset_card(worker, true);
767 else if (sps->resetActive)
768 rc = worker->ops->reset_card(worker, false);
769
770 return rc;
771}
772
Harald Welte77911b02018-08-14 23:47:30 +0200773/* handle one incoming RSPRO message from a client inside a worker thread */
774static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
775{
Harald Weltecce2aad2018-08-16 14:44:37 +0200776 int rc = -100;
777
Harald Welte77911b02018-08-14 23:47:30 +0200778 switch (pdu->msg.present) {
779 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200780 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200781 break;
782 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200783 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200784 break;
785 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100786 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200787 rc = 0;
788 break;
789 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100790 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200791 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200792 break;
793 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100794 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200795 rc = -101;
796 break;
Harald Welte77911b02018-08-14 23:47:30 +0200797 }
798
Harald Weltecce2aad2018-08-16 14:44:37 +0200799 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200800}
801
Harald Welte694df832018-10-03 22:47:52 +0200802static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
803{
804 struct timeval tout = { timeout_secs, 0 };
805 fd_set readset;
806
807 FD_ZERO(&readset);
808 FD_SET(fd, &readset);
809 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
810}
811
Harald Welte77911b02018-08-14 23:47:30 +0200812/* body of the main transceive loop */
813static int worker_transceive_loop(struct bankd_worker *worker)
814{
815 struct ipaccess_head *hh;
816 struct ipaccess_head_ext *hh_ext;
817 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
818 asn_dec_rval_t rval;
819 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200820 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200821
Harald Welte00a96732019-03-11 17:18:02 +0100822restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200823 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100824 if (rc == -1 && errno == EINTR) {
825 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
826 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100827 else
828 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100829 goto restart_wait;
830 } else if (rc < 0)
831 return rc;
832 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200833 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200834 switch (worker->state) {
835 case BW_ST_CONN_CLIENT_WAIT_MAP:
836 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100837 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200838 break;
839 case BW_ST_CONN_CLIENT_MAPPED:
840 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100841 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200842 break;
843 default:
844 OSMO_ASSERT(0);
845 }
Harald Welte1f699b42019-03-28 13:41:08 +0100846 if (rc == 0)
847 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200848 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200849 return 0;
850 };
851
Harald Welte77911b02018-08-14 23:47:30 +0200852 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100853 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200854 if (rc < 0)
855 return rc;
856 data_len = rc;
857
858 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200859 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200860 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200861 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200862 }
Harald Welte77911b02018-08-14 23:47:30 +0200863
Harald Welte5a3613a2018-10-11 12:56:21 +0200864 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100865 switch (hh->data[0]) {
866 case IPAC_MSGT_PING:
867 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100868 case IPAC_MSGT_ID_ACK:
869 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100870 default:
871 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
872 break;
873 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200874 return 0;
875 }
876
Harald Welte77911b02018-08-14 23:47:30 +0200877 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200878 if (data_len < sizeof(*hh_ext)) {
879 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200880 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200881 }
Harald Welte77911b02018-08-14 23:47:30 +0200882 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200883 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
884 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200885 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200886 }
Harald Welte77911b02018-08-14 23:47:30 +0200887
888 /* 2) ASN1 BER decode of the message */
889 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200890 if (rval.code != RC_OK) {
891 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200892 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200893 }
Harald Welte77911b02018-08-14 23:47:30 +0200894
895 /* 3) handling of the message, possibly resulting in PCSC commands */
896 rc = worker_handle_rspro(worker, pdu);
897 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200898 if (rc < 0) {
899 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200900 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200901 }
Harald Welte77911b02018-08-14 23:47:30 +0200902
903 /* everything OK if we reach here */
904 return 0;
905}
906
Harald Welted6dfb8c2018-08-16 14:46:53 +0200907/* obtain an ascii representation of the client IP/port */
908static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
909{
910 char hostbuf[32], portbuf[32];
911 int rc;
912
913 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
914 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
915 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
916 if (rc != 0) {
917 out[0] = '\0';
918 return -1;
919 }
920 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
921 return 0;
922}
923
Harald Welte77911b02018-08-14 23:47:30 +0200924/* worker thread main function */
925static void *worker_main(void *arg)
926{
Harald Welte77911b02018-08-14 23:47:30 +0200927 void *top_ctx;
928 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200929
Harald Welte00a96732019-03-11 17:18:02 +0100930 g_worker = (struct bankd_worker *) arg;
931
Harald Welte00a96732019-03-11 17:18:02 +0100932 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200933
Harald Welte77911b02018-08-14 23:47:30 +0200934 /* not permitted in multithreaded environment */
935 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100936 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
937 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200938
Harald Welte286a2be2019-03-11 17:36:58 +0100939 /* set the thread name */
940 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
941 pthread_setname_np(pthread_self(), g_worker->name);
942
Harald Welte77911b02018-08-14 23:47:30 +0200943 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100944 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200945
Harald Welte602b6f72019-12-04 21:51:02 +0100946 g_worker->slot.bank_id = 0xffff;
947 g_worker->slot.slot_nr = 0xffff;
948
Harald Welte77911b02018-08-14 23:47:30 +0200949 /* we continuously perform the same loop here, recycling the worker thread
950 * once the client connection is gone or we have some trouble with the card/reader */
951 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200952 char buf[128];
953
Harald Welte00a96732019-03-11 17:18:02 +0100954 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200955
Harald Welte00a96732019-03-11 17:18:02 +0100956 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200957 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100958 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
959 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200960 if (rc < 0) {
961 continue;
962 }
Harald Welte00a96732019-03-11 17:18:02 +0100963 g_worker->client.fd = rc;
964 worker_client_addrstr(buf, sizeof(buf), g_worker);
965 LOGW(g_worker, "Accepted connection from %s\n", buf);
966 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200967
968 /* run the main worker transceive loop body until there was some error */
969 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100970 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200971 if (rc < 0)
972 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100973 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
974 break;
Harald Welte77911b02018-08-14 23:47:30 +0200975 }
976
Harald Welte00a96732019-03-11 17:18:02 +0100977 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200978
Harald Welte77911b02018-08-14 23:47:30 +0200979 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100980 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100981 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100982 if (g_worker->reader.name)
983 g_worker->reader.name = NULL;
984 if (g_worker->client.fd >= 0)
985 close(g_worker->client.fd);
986 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
987 g_worker->client.fd = -1;
988 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200989 }
990
991 pthread_cleanup_pop(1);
992 talloc_free(top_ctx);
993 pthread_exit(NULL);
994}