blob: 36fd651d50609bf7d75d565e2f1dabbb4a143199 [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 */
387 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
388
Harald Welte707c85a2019-03-09 12:56:35 +0100389 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100390 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100391 if (rc < 0) {
392 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
393 exit(1);
394 }
Harald Welted2192e22019-11-07 18:10:57 +0100395 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100396
397 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200398 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, g_bind_ip, g_bind_port, OSMO_SOCK_F_BIND);
Harald Welte12534e72018-08-15 23:37:29 +0200399 if (rc < 0)
400 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100401 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200402
Harald Weltea0f39502019-03-09 20:59:34 +0100403 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200404 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200405 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100406 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200407 if (!w)
408 exit(21);
409 }
410
411 while (1) {
412 if (terminate)
413 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100414 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200415 }
416
Harald Weltef4b16f12019-03-09 20:58:17 +0100417 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200418 exit(0);
419}
420
421
422
423/***********************************************************************
424 * bankd worker thread
425 ***********************************************************************/
426
Harald Welte00a96732019-03-11 17:18:02 +0100427static __thread struct bankd_worker *g_worker;
428
Harald Welte8d858292018-08-15 23:36:46 +0200429struct value_string worker_state_names[] = {
430 { BW_ST_INIT, "INIT" },
431 { BW_ST_ACCEPTING, "ACCEPTING" },
432 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
433 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200434 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200435 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
436 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100437 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200438 { 0, NULL }
439};
440
Harald Welte1f699b42019-03-28 13:41:08 +0100441static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
442
Harald Welte8d858292018-08-15 23:36:46 +0200443static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
444{
445 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
446 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200447 worker->timeout = 0;
448}
449
450static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
451 unsigned int timeout_secs)
452{
453 LOGW(worker, "Changing state to %s (timeout=%u)\n",
454 get_value_string(worker_state_names, new_state), timeout_secs);
455 worker->state = new_state;
456 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200457}
Harald Welte77911b02018-08-14 23:47:30 +0200458
Harald Welte00a96732019-03-11 17:18:02 +0100459/* signal handler for receiving SIGMAPDEL from main thread */
460static void handle_sig_mapdel(int sig)
461{
462 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
463 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100464 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
465 g_worker->slot.bank_id = 0xffff;
466 g_worker->slot.slot_nr = 0xffff;
467 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
468 }
Harald Welte00a96732019-03-11 17:18:02 +0100469}
470
Harald Welte0fd77a52019-12-05 22:05:39 +0100471/* signal handler for receiving SIGMAPADD from main thread */
472static void handle_sig_mapadd(int sig)
473{
474 LOGW(g_worker, "SIGMAPADD received\n");
475 /* do nothing */
476}
477
Harald Welte25075972019-03-11 17:33:17 +0100478static void handle_sig_usr1(int sig)
479{
480 OSMO_ASSERT(sig == SIGUSR1);
481
482 if (pthread_equal(g_bankd->main, pthread_self())) {
483 struct bankd_worker *worker;
484 /* main thread */
485 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200486 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100487
488 /* iterate over worker threads and ask them to dump their talloc state */
489 pthread_mutex_lock(&g_bankd->workers_mutex);
490 llist_for_each_entry(worker, &g_bankd->workers, list) {
491 pthread_kill(worker->thread, SIGUSR1);
492 }
493 pthread_mutex_unlock(&g_bankd->workers_mutex);
494 } else {
495 /* worker thread */
496 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200497 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100498 }
499}
500
Harald Welte77911b02018-08-14 23:47:30 +0200501static void worker_cleanup(void *arg)
502{
503 struct bankd_worker *worker = (struct bankd_worker *) arg;
504 struct bankd *bankd = worker->bankd;
505
506 /* FIXME: should we still do this? in the thread ?!? */
507 pthread_mutex_lock(&bankd->workers_mutex);
508 llist_del(&worker->list);
509 talloc_free(worker); /* FIXME: is this safe? */
510 pthread_mutex_unlock(&bankd->workers_mutex);
511}
512
Harald Welteaf614732018-08-17 22:10:05 +0200513static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200514{
Harald Welte297d72e2019-03-28 18:42:35 +0100515 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200516
Harald Welte150d6d62018-10-03 23:07:47 +0200517 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
518
Harald Welte694df832018-10-03 22:47:52 +0200519 if (!worker->reader.name) {
520 /* resolve PC/SC reader name from slot_id -> name map */
521 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
522 if (!worker->reader.name) {
523 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
524 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100525 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200526 }
527 }
Harald Welte45c948c2018-09-23 19:26:52 +0200528 OSMO_ASSERT(worker->reader.name);
529
Harald Welte297d72e2019-03-28 18:42:35 +0100530 rc = worker->ops->open_card(worker);
531 if (rc < 0)
532 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100533
Harald Welte57593f02018-09-23 19:30:31 +0200534 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200535 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200536
Harald Welteaf614732018-08-17 22:10:05 +0200537 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200538}
Harald Welte77911b02018-08-14 23:47:30 +0200539
540
Harald Welte00a96732019-03-11 17:18:02 +0100541static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200542{
543 struct ipaccess_head *hh;
544 uint16_t len;
545 int needed, rc;
546
547 if (buf_size < sizeof(*hh))
548 return -1;
549
550 hh = (struct ipaccess_head *) buf;
551
Harald Welte00a96732019-03-11 17:18:02 +0100552 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
553 * in case of a signal being received */
554
555restart_hdr:
556 /* 1) blocking recv from the socket (IPA header) */
557 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
558 if (rc == -1 && errno == EINTR) {
559 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
560 return -23;
561 goto restart_hdr;
562 } else if (rc < 0)
563 return rc;
564 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200565 return -2;
566
567 len = ntohs(hh->len);
568 needed = len; //- sizeof(*hh);
569
Harald Welte00a96732019-03-11 17:18:02 +0100570restart_body:
571 /* 2) blocking recv from the socket (payload) */
572 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
573 if (rc == -1 && errno == EINTR) {
574 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
575 return -23;
576 goto restart_body;
577 } else if (rc < 0)
578 return rc;
579 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200580 return -3;
581
582 return len;
583}
584
Harald Welte796a7492018-09-23 19:31:28 +0200585static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
586{
587 struct msgb *msg = rspro_enc_msg(pdu);
588 int rc;
589
590 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100591 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200592 LOGW(worker, "error encoding RSPRO\n");
593 return -1;
594 }
595
Harald Weltefd471192018-09-24 14:51:14 +0200596 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200597 /* prepend the header */
598 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200599 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200600
601 /* actually send it through the socket */
602 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
603 if (rc == msgb_length(msg))
604 rc = 0;
605 else {
606 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
607 rc = -1;
608 }
609
610 msgb_free(msg);
611
612 return rc;
613}
614
Harald Welte150d6d62018-10-03 23:07:47 +0200615/* attempt to obtain slot-map */
616static int worker_try_slotmap(struct bankd_worker *worker)
617{
Harald Weltecbd18962019-03-03 19:02:38 +0100618 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200619
Harald Weltecbd18962019-03-03 19:02:38 +0100620 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200621 if (!slmap) {
622 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
623 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
624 /* check in 10s if the map has been installed meanwhile by main thread */
625 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
626 return -1;
627 } else {
628 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
629 slmap->client.client_id, slmap->client.slot_nr,
630 slmap->bank.bank_id, slmap->bank.slot_nr);
631 worker->slot = slmap->bank;
632 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
633 return worker_open_card(worker);
634 }
635}
636
Harald Welte1f699b42019-03-28 13:41:08 +0100637/* inform the remote end (client) about the (new) ATR */
638static int worker_send_atr(struct bankd_worker *worker)
639{
640 RsproPDU_t *set_atr;
641 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
642 worker->client.clslot.slot_nr,
643 worker->card.atr, worker->card.atr_len);
644 if (!set_atr)
645 return -1;
646 return worker_send_rspro(worker, set_atr);
647}
Harald Welte150d6d62018-10-03 23:07:47 +0200648
Harald Weltecce2aad2018-08-16 14:44:37 +0200649static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
650{
Harald Welteaf614732018-08-17 22:10:05 +0200651 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100652 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200653 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100654 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200655
Harald Weltecce2aad2018-08-16 14:44:37 +0200656 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
657
Harald Welte9f7ca612021-12-08 15:29:28 +0100658 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200659 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
660 /* FIXME: store somewhere? */
661
662 if (worker->state != BW_ST_CONN_WAIT_ID) {
663 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100664 rc = -102;
665 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200666 }
667
Harald Welte371d0262018-08-16 15:23:58 +0200668 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200669 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100670 res = ResultCode_illegalClientId;
671 rc = -103;
672 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200673 }
Harald Welte371d0262018-08-16 15:23:58 +0200674 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
675 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200676 worker_set_state(worker, BW_ST_CONN_CLIENT);
677
Harald Welte150d6d62018-10-03 23:07:47 +0200678 if (worker_try_slotmap(worker) >= 0)
679 res = ResultCode_ok;
680 else
Harald Welte3e689872018-09-24 14:52:56 +0200681 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200682
Harald Welte3e689872018-09-24 14:52:56 +0200683 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100684 rc = worker_send_rspro(worker, resp);
685 if (rc < 0)
686 return rc;
687
688 if (res == ResultCode_ok)
689 rc = worker_send_atr(worker);
690
691 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100692
693respond_and_err:
694 if (res) {
695 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
696 worker_send_rspro(worker, resp);
697 }
698 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200699}
700
Harald Welte796a7492018-09-23 19:31:28 +0200701static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
702{
703 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200704 uint8_t rx_buf[1024];
705 DWORD rx_buf_len = sizeof(rx_buf);
706 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100707 struct client_slot clslot;
708 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100709 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200710
Harald Welte9f7ca612021-12-08 15:29:28 +0100711 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
712 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200713
714 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
715 LOGW(worker, "Unexpected tpduModemToCaard\n");
716 return -104;
717 }
718
Harald Weltee1d32892019-03-27 20:47:42 +0100719 /* Validate that toBankSlot / fromClientSlot match our expectations */
720 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
721 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
722 if (!bank_slot_equals(&worker->slot, &bslot)) {
723 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
724 bslot.bank_id, bslot.slot_nr);
725 return -105;
726 }
727 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
728 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
729 clslot.client_id, clslot.slot_nr);
730 return -106;
731 }
Harald Welte796a7492018-09-23 19:31:28 +0200732
Harald Welte297d72e2019-03-28 18:42:35 +0100733 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
734 rx_buf, &rx_buf_len);
735 if (rc < 0)
736 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200737
738 /* encode response PDU and send it */
739 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
740 rx_buf, rx_buf_len);
741 worker_send_rspro(worker, pdu_resp);
742
743 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200744}
745
Harald Weltebbd18bd2019-12-16 13:11:50 +0100746static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
747{
748 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
749 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
750 int rc = 0;
751
Harald Welte9f7ca612021-12-08 15:29:28 +0100752 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100753 sps->resetActive ? "ACTIVE" : "INACTIVE",
754 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
755 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
756
757 /* perform cold or warm reset */
758 if (sps->vccPresent && *sps->vccPresent == 0)
759 rc = worker->ops->reset_card(worker, true);
760 else if (sps->resetActive)
761 rc = worker->ops->reset_card(worker, false);
762
763 return rc;
764}
765
Harald Welte77911b02018-08-14 23:47:30 +0200766/* handle one incoming RSPRO message from a client inside a worker thread */
767static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
768{
Harald Weltecce2aad2018-08-16 14:44:37 +0200769 int rc = -100;
770
Harald Welte77911b02018-08-14 23:47:30 +0200771 switch (pdu->msg.present) {
772 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200773 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200774 break;
775 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200776 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200777 break;
778 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100779 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200780 rc = 0;
781 break;
782 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100783 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200784 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200785 break;
786 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100787 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200788 rc = -101;
789 break;
Harald Welte77911b02018-08-14 23:47:30 +0200790 }
791
Harald Weltecce2aad2018-08-16 14:44:37 +0200792 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200793}
794
Harald Welte694df832018-10-03 22:47:52 +0200795static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
796{
797 struct timeval tout = { timeout_secs, 0 };
798 fd_set readset;
799
800 FD_ZERO(&readset);
801 FD_SET(fd, &readset);
802 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
803}
804
Harald Welte77911b02018-08-14 23:47:30 +0200805/* body of the main transceive loop */
806static int worker_transceive_loop(struct bankd_worker *worker)
807{
808 struct ipaccess_head *hh;
809 struct ipaccess_head_ext *hh_ext;
810 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
811 asn_dec_rval_t rval;
812 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200813 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200814
Harald Welte00a96732019-03-11 17:18:02 +0100815restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200816 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100817 if (rc == -1 && errno == EINTR) {
818 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
819 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100820 else
821 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100822 goto restart_wait;
823 } else if (rc < 0)
824 return rc;
825 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200826 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200827 switch (worker->state) {
828 case BW_ST_CONN_CLIENT_WAIT_MAP:
829 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100830 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200831 break;
832 case BW_ST_CONN_CLIENT_MAPPED:
833 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100834 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200835 break;
836 default:
837 OSMO_ASSERT(0);
838 }
Harald Welte1f699b42019-03-28 13:41:08 +0100839 if (rc == 0)
840 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200841 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200842 return 0;
843 };
844
Harald Welte77911b02018-08-14 23:47:30 +0200845 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100846 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200847 if (rc < 0)
848 return rc;
849 data_len = rc;
850
851 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200852 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200853 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200854 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200855 }
Harald Welte77911b02018-08-14 23:47:30 +0200856
Harald Welte5a3613a2018-10-11 12:56:21 +0200857 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100858 switch (hh->data[0]) {
859 case IPAC_MSGT_PING:
860 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100861 case IPAC_MSGT_ID_ACK:
862 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100863 default:
864 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
865 break;
866 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200867 return 0;
868 }
869
Harald Welte77911b02018-08-14 23:47:30 +0200870 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200871 if (data_len < sizeof(*hh_ext)) {
872 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200873 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200874 }
Harald Welte77911b02018-08-14 23:47:30 +0200875 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200876 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
877 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200878 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200879 }
Harald Welte77911b02018-08-14 23:47:30 +0200880
881 /* 2) ASN1 BER decode of the message */
882 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200883 if (rval.code != RC_OK) {
884 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200885 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200886 }
Harald Welte77911b02018-08-14 23:47:30 +0200887
888 /* 3) handling of the message, possibly resulting in PCSC commands */
889 rc = worker_handle_rspro(worker, pdu);
890 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200891 if (rc < 0) {
892 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200893 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200894 }
Harald Welte77911b02018-08-14 23:47:30 +0200895
896 /* everything OK if we reach here */
897 return 0;
898}
899
Harald Welted6dfb8c2018-08-16 14:46:53 +0200900/* obtain an ascii representation of the client IP/port */
901static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
902{
903 char hostbuf[32], portbuf[32];
904 int rc;
905
906 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
907 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
908 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
909 if (rc != 0) {
910 out[0] = '\0';
911 return -1;
912 }
913 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
914 return 0;
915}
916
Harald Welte77911b02018-08-14 23:47:30 +0200917/* worker thread main function */
918static void *worker_main(void *arg)
919{
Harald Welte77911b02018-08-14 23:47:30 +0200920 void *top_ctx;
921 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200922
Harald Welte00a96732019-03-11 17:18:02 +0100923 g_worker = (struct bankd_worker *) arg;
924
Harald Welte00a96732019-03-11 17:18:02 +0100925 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200926
Harald Welte77911b02018-08-14 23:47:30 +0200927 /* not permitted in multithreaded environment */
928 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100929 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
930 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200931
Harald Welte286a2be2019-03-11 17:36:58 +0100932 /* set the thread name */
933 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
934 pthread_setname_np(pthread_self(), g_worker->name);
935
Harald Welte77911b02018-08-14 23:47:30 +0200936 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100937 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200938
Harald Welte602b6f72019-12-04 21:51:02 +0100939 g_worker->slot.bank_id = 0xffff;
940 g_worker->slot.slot_nr = 0xffff;
941
Harald Welte77911b02018-08-14 23:47:30 +0200942 /* we continuously perform the same loop here, recycling the worker thread
943 * once the client connection is gone or we have some trouble with the card/reader */
944 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200945 char buf[128];
946
Harald Welte00a96732019-03-11 17:18:02 +0100947 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200948
Harald Welte00a96732019-03-11 17:18:02 +0100949 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200950 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100951 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
952 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200953 if (rc < 0) {
954 continue;
955 }
Harald Welte00a96732019-03-11 17:18:02 +0100956 g_worker->client.fd = rc;
957 worker_client_addrstr(buf, sizeof(buf), g_worker);
958 LOGW(g_worker, "Accepted connection from %s\n", buf);
959 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200960
961 /* run the main worker transceive loop body until there was some error */
962 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100963 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200964 if (rc < 0)
965 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100966 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
967 break;
Harald Welte77911b02018-08-14 23:47:30 +0200968 }
969
Harald Welte00a96732019-03-11 17:18:02 +0100970 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200971
Harald Welte77911b02018-08-14 23:47:30 +0200972 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100973 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100974 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100975 if (g_worker->reader.name)
976 g_worker->reader.name = NULL;
977 if (g_worker->client.fd >= 0)
978 close(g_worker->client.fd);
979 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
980 g_worker->client.fd = -1;
981 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200982 }
983
984 pthread_cleanup_pop(1);
985 talloc_free(top_ctx);
986 pthread_exit(NULL);
987}