blob: 3842b0c6fa9111d16bfe292a682174ad99968bc0 [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 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte31c9eca2018-10-03 21:03:34 +020023#define _GNU_SOURCE
24
Harald Welte77911b02018-08-14 23:47:30 +020025#include <stdio.h>
26#include <stdlib.h>
27#include <stdint.h>
Harald Welte00a96732019-03-11 17:18:02 +010028#include <signal.h>
Harald Welte77911b02018-08-14 23:47:30 +020029#include <unistd.h>
Harald Welte707c85a2019-03-09 12:56:35 +010030#include <errno.h>
Harald Welte5bae20b2019-04-01 09:36:42 +020031#include <getopt.h>
Harald Welte77911b02018-08-14 23:47:30 +020032
33#include <pthread.h>
34
Harald Welted6dfb8c2018-08-16 14:46:53 +020035#include <sys/socket.h>
36#include <netdb.h>
37
Harald Welte12534e72018-08-15 23:37:29 +020038#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020039#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020040#include <osmocom/core/logging.h>
41#include <osmocom/core/application.h>
Harald Welte73bbd542021-12-08 20:39:55 +010042#include <osmocom/core/fsm.h>
Harald Welte77911b02018-08-14 23:47:30 +020043
44#include <osmocom/gsm/ipa.h>
45#include <osmocom/gsm/protocol/ipaccess.h>
46
Kévin Redonff5db6e2018-10-11 17:16:18 +020047#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020048#include <osmocom/rspro/RsproPDU.h>
49
50#include "bankd.h"
Harald Welte3cded632019-03-09 12:59:41 +010051#include "rspro_client_fsm.h"
Harald Welte61d98e92019-03-03 15:43:07 +010052#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020053#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020054
Harald Welte00a96732019-03-11 17:18:02 +010055/* signal indicates to worker thread that its map has been deleted */
56#define SIGMAPDEL SIGRTMIN+1
Harald Welte0fd77a52019-12-05 22:05:39 +010057#define SIGMAPADD SIGRTMIN+2
Harald Welte25075972019-03-11 17:33:17 +010058
59static void handle_sig_usr1(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010060static void handle_sig_mapdel(int sig);
Harald Welte0fd77a52019-12-05 22:05:39 +010061static void handle_sig_mapadd(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010062
Harald Welte77911b02018-08-14 23:47:30 +020063__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010064struct bankd *g_bankd;
Harald Welte25075972019-03-11 17:33:17 +010065static void *g_tall_ctx;
Harald Welte77911b02018-08-14 23:47:30 +020066
67static void *worker_main(void *arg);
68
69/***********************************************************************
70* bankd core / main thread
71***********************************************************************/
72
Harald Welte43ab79f2018-10-03 23:34:21 +020073int asn_debug;
74
Harald Welte77911b02018-08-14 23:47:30 +020075static void bankd_init(struct bankd *bankd)
76{
Harald Welte25075972019-03-11 17:33:17 +010077 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Weltef94b9ee2018-09-25 15:04:21 +020078 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +010079 log_set_print_level(osmo_stderr_target, 1);
80 log_set_print_category(osmo_stderr_target, 1);
81 log_set_print_category_hex(osmo_stderr_target, 0);
82 osmo_fsm_log_addr(0);
Harald Weltef94b9ee2018-09-25 15:04:21 +020083
Harald Welte43ab79f2018-10-03 23:34:21 +020084 asn_debug = 0;
85
Harald Welte77911b02018-08-14 23:47:30 +020086 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010087 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020088 INIT_LLIST_HEAD(&bankd->workers);
89 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020090
Harald Weltea0f39502019-03-09 20:59:34 +010091 /* set some defaults, overridden by commandline/config */
Harald Welte2513d812019-04-01 21:03:02 +020092 bankd->srvc.bankd.bank_id = 1;
93 bankd->srvc.bankd.num_slots = 8;
Harald Weltea0f39502019-03-09 20:59:34 +010094
Harald Weltef1dd1622018-09-24 14:54:23 +020095 bankd->comp_id.type = ComponentType_remsimBankd;
96 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
97 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
98 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
99 /* FIXME: other members of app_comp_id */
100
Harald Welte45c948c2018-09-23 19:26:52 +0200101 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
Harald Welte77911b02018-08-14 23:47:30 +0200102}
103
104/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200105static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200106{
107 struct bankd_worker *worker;
108 int rc;
109
110 worker = talloc_zero(bankd, struct bankd_worker);
111 if (!worker)
112 return NULL;
113
114 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200115 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100116 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200117
118 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
119
120 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
121 if (rc != 0) {
122 talloc_free(worker);
123 return NULL;
124 }
125
126 pthread_mutex_lock(&bankd->workers_mutex);
127 llist_add_tail(&worker->list, &bankd->workers);
128 pthread_mutex_unlock(&bankd->workers_mutex);
129
130 return worker;
131}
132
133static bool terminate = false;
134
Harald Weltea4967832019-12-05 22:04:57 +0100135/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
136static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
137{
138 struct bankd_worker *worker;
139 pthread_mutex_lock(&g_bankd->workers_mutex);
140 llist_for_each_entry(worker, &g_bankd->workers, list) {
141 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
142 continue;
143 if (cs && (cs->client_id != worker->client.clslot.client_id ||
144 cs->slot_nr != worker->client.clslot.slot_nr))
145 continue;
146
147 pthread_kill(worker->thread, sig);
148 break;
149 }
150 pthread_mutex_unlock(&g_bankd->workers_mutex);
151}
152
Harald Weltec650a4d2019-12-04 15:02:27 +0100153/* Remove a mapping */
154static void bankd_srvc_remove_mapping(struct slot_mapping *map)
155{
156 struct bank_slot bs = map->bank;
157
158 slotmap_del(g_bankd->slotmaps, map);
159
160 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100161 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100162}
163
Harald Welte707c85a2019-03-09 12:56:35 +0100164/* handle incoming messages from server */
165static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
166{
Harald Welte454f5e22019-03-09 21:38:34 +0100167 const CreateMappingReq_t *creq = NULL;
168 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100169 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100170 struct slot_mapping *map;
171 struct bank_slot bs;
172 struct client_slot cs;
173 RsproPDU_t *resp;
174
175 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100176
177 switch (pdu->msg.present) {
178 case RsproPDUchoice_PR_connectBankRes:
179 /* Store 'identity' of server in srvc->peer_comp_id */
180 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
181 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
182 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100183 case RsproPDUchoice_PR_createMappingReq:
184 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200185 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100186 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200187 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100188 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200189 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100190 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200191 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100192 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100193 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100194 rspro2bank_slot(&bs, &creq->bank);
195 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100196 /* check if map exists */
197 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
198 if (map) {
199 if (client_slot_equals(&map->client, &cs)) {
200 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
201 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
202 goto send_resp;
203 } else {
204 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
205 bankd_srvc_remove_mapping(map);
206 }
207 }
Harald Welte454f5e22019-03-09 21:38:34 +0100208 /* Add a new mapping */
209 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100210 if (!map) {
211 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100212 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100213 } else {
214 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100215 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100216 }
Harald Welte454f5e22019-03-09 21:38:34 +0100217 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100218send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100219 server_conn_send_rspro(srvc, resp);
220 break;
221 case RsproPDUchoice_PR_removeMappingReq:
222 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200223 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100224 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100225 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100226 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200227 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100228 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100229 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100230 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100231 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100232 rspro2bank_slot(&bs, &rreq->bank);
233 /* Remove a mapping */
234 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100235 if (!map) {
236 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100237 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100238 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100239 rspro2client_slot(&cs, &rreq->client);
240 if (!client_slot_equals(&map->client, &cs)) {
241 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
242 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
243 } else {
244 LOGPFSM(srvc->fi, "removing slotmap\n");
245 bankd_srvc_remove_mapping(map);
246 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
247 }
Harald Welte454f5e22019-03-09 21:38:34 +0100248 }
249 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100250 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100251 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100252 case RsproPDUchoice_PR_resetStateReq:
253 /* delete all slotmaps */
254 slotmap_del_all(g_bankd->slotmaps);
255 /* notify all workers about maps having disappeared */
256 pthread_mutex_lock(&g_bankd->workers_mutex);
257 llist_for_each_entry(worker, &g_bankd->workers, list) {
258 pthread_kill(worker->thread, SIGMAPDEL);
259 }
260 pthread_mutex_unlock(&g_bankd->workers_mutex);
261 /* send response to server */
262 resp = rspro_gen_ResetStateRes(ResultCode_ok);
263 server_conn_send_rspro(srvc, resp);
264 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100265 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100266 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
267 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100268 return -1;
269 }
270
271 return 0;
272}
273
Harald Welte5bae20b2019-04-01 09:36:42 +0200274static void printf_help()
275{
276 printf(
277" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100278" -V --version Print the version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +0200279" -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200280" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
281" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
282" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200283" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200284" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
285" connections (default: INADDR_ANY)\n"
286" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
287" connectionss (default: 9999)\n"
288 );
289}
290
291static int g_bind_port = 9999;
292static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100293
Harald Weltef8c6eeb2021-12-08 20:33:00 +0100294static void handle_options(int argc, char **argv)
Harald Welte707c85a2019-03-09 12:56:35 +0100295{
Harald Welte5bae20b2019-04-01 09:36:42 +0200296 while (1) {
297 int option_index = 0, c;
298 static const struct option long_options[] = {
299 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100300 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +0200301 { "debug", 1, 0, 'd' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200302 { "server-host", 1, 0, 'i' },
303 { "server-port", 1, 0, 'p' },
304 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200305 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200306 { "component-name", 1, 0, 'N' },
307 { "bind-ip", 1, 0, 'I' },
308 { "bind-port", 1, 0, 'P' },
309 { 0, 0, 0, 0 }
310 };
311
Harald Welte80a01102020-05-25 14:55:12 +0200312 c = getopt_long(argc, argv, "hVd:i:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200313 if (c == -1)
314 break;
315
316 switch (c) {
317 case 'h':
318 printf_help();
319 exit(0);
320 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100321 case 'V':
322 printf("osmo-remsim-bankd version %s\n", VERSION);
323 exit(0);
324 break;
Harald Welte80a01102020-05-25 14:55:12 +0200325 case 'd':
326 log_parse_category_mask(osmo_stderr_target, optarg);
327 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200328 case 'i':
329 g_bankd->srvc.server_host = optarg;
330 break;
331 case 'p':
332 g_bankd->srvc.server_port = atoi(optarg);
333 break;
334 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200335 g_bankd->srvc.bankd.bank_id = atoi(optarg);
336 break;
337 case 'n':
338 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200339 break;
340 case 'N':
341 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
342 break;
343 case 'I':
344 g_bind_ip = optarg;
345 break;
346 case 'P':
347 g_bind_port = atoi(optarg);
348 break;
349 }
350 }
Harald Welte707c85a2019-03-09 12:56:35 +0100351}
352
Harald Welte77911b02018-08-14 23:47:30 +0200353int main(int argc, char **argv)
354{
Harald Weltef4b16f12019-03-09 20:58:17 +0100355 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200356 int i, rc;
357
Harald Weltef4b16f12019-03-09 20:58:17 +0100358 g_bankd = talloc_zero(NULL, struct bankd);
359 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200360
Harald Weltef4b16f12019-03-09 20:58:17 +0100361 bankd_init(g_bankd);
362
363 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100364 srvc->server_host = "localhost";
365 srvc->server_port = 9998;
366 srvc->handle_rx = bankd_srvc_handle_rx;
367 srvc->own_comp_id.type = ComponentType_remsimBankd;
368 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
369 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
370 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
371
372 handle_options(argc, argv);
373
Harald Welte25075972019-03-11 17:33:17 +0100374 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100375 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100376 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100377 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100378
Harald Weltee89fecd2019-04-04 09:23:13 +0200379 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
380 * read once during bankd initialization, when the worker threads haven't
381 * started yet */
382 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
383
Harald Welte707c85a2019-03-09 12:56:35 +0100384 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100385 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100386 if (rc < 0) {
387 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
388 exit(1);
389 }
Harald Welted2192e22019-11-07 18:10:57 +0100390 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100391
392 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200393 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 +0200394 if (rc < 0)
395 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100396 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200397
Harald Weltea0f39502019-03-09 20:59:34 +0100398 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200399 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200400 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100401 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200402 if (!w)
403 exit(21);
404 }
405
406 while (1) {
407 if (terminate)
408 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100409 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200410 }
411
Harald Weltef4b16f12019-03-09 20:58:17 +0100412 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200413 exit(0);
414}
415
416
417
418/***********************************************************************
419 * bankd worker thread
420 ***********************************************************************/
421
Harald Welte00a96732019-03-11 17:18:02 +0100422static __thread struct bankd_worker *g_worker;
423
Harald Welte8d858292018-08-15 23:36:46 +0200424struct value_string worker_state_names[] = {
425 { BW_ST_INIT, "INIT" },
426 { BW_ST_ACCEPTING, "ACCEPTING" },
427 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
428 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200429 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200430 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
431 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100432 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200433 { 0, NULL }
434};
435
Harald Welte1f699b42019-03-28 13:41:08 +0100436static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
437
Harald Welte8d858292018-08-15 23:36:46 +0200438static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
439{
440 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
441 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200442 worker->timeout = 0;
443}
444
445static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
446 unsigned int timeout_secs)
447{
448 LOGW(worker, "Changing state to %s (timeout=%u)\n",
449 get_value_string(worker_state_names, new_state), timeout_secs);
450 worker->state = new_state;
451 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200452}
Harald Welte77911b02018-08-14 23:47:30 +0200453
Harald Welte00a96732019-03-11 17:18:02 +0100454/* signal handler for receiving SIGMAPDEL from main thread */
455static void handle_sig_mapdel(int sig)
456{
457 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
458 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100459 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
460 g_worker->slot.bank_id = 0xffff;
461 g_worker->slot.slot_nr = 0xffff;
462 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
463 }
Harald Welte00a96732019-03-11 17:18:02 +0100464}
465
Harald Welte0fd77a52019-12-05 22:05:39 +0100466/* signal handler for receiving SIGMAPADD from main thread */
467static void handle_sig_mapadd(int sig)
468{
469 LOGW(g_worker, "SIGMAPADD received\n");
470 /* do nothing */
471}
472
Harald Welte25075972019-03-11 17:33:17 +0100473static void handle_sig_usr1(int sig)
474{
475 OSMO_ASSERT(sig == SIGUSR1);
476
477 if (pthread_equal(g_bankd->main, pthread_self())) {
478 struct bankd_worker *worker;
479 /* main thread */
480 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200481 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100482
483 /* iterate over worker threads and ask them to dump their talloc state */
484 pthread_mutex_lock(&g_bankd->workers_mutex);
485 llist_for_each_entry(worker, &g_bankd->workers, list) {
486 pthread_kill(worker->thread, SIGUSR1);
487 }
488 pthread_mutex_unlock(&g_bankd->workers_mutex);
489 } else {
490 /* worker thread */
491 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200492 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100493 }
494}
495
Harald Welte77911b02018-08-14 23:47:30 +0200496static void worker_cleanup(void *arg)
497{
498 struct bankd_worker *worker = (struct bankd_worker *) arg;
499 struct bankd *bankd = worker->bankd;
500
501 /* FIXME: should we still do this? in the thread ?!? */
502 pthread_mutex_lock(&bankd->workers_mutex);
503 llist_del(&worker->list);
504 talloc_free(worker); /* FIXME: is this safe? */
505 pthread_mutex_unlock(&bankd->workers_mutex);
506}
507
Harald Welteaf614732018-08-17 22:10:05 +0200508static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200509{
Harald Welte297d72e2019-03-28 18:42:35 +0100510 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200511
Harald Welte150d6d62018-10-03 23:07:47 +0200512 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
513
Harald Welte694df832018-10-03 22:47:52 +0200514 if (!worker->reader.name) {
515 /* resolve PC/SC reader name from slot_id -> name map */
516 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
517 if (!worker->reader.name) {
518 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
519 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100520 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200521 }
522 }
Harald Welte45c948c2018-09-23 19:26:52 +0200523 OSMO_ASSERT(worker->reader.name);
524
Harald Welte297d72e2019-03-28 18:42:35 +0100525 rc = worker->ops->open_card(worker);
526 if (rc < 0)
527 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100528
Harald Welte57593f02018-09-23 19:30:31 +0200529 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200530 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200531
Harald Welteaf614732018-08-17 22:10:05 +0200532 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200533}
Harald Welte77911b02018-08-14 23:47:30 +0200534
535
Harald Welte00a96732019-03-11 17:18:02 +0100536static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200537{
538 struct ipaccess_head *hh;
539 uint16_t len;
540 int needed, rc;
541
542 if (buf_size < sizeof(*hh))
543 return -1;
544
545 hh = (struct ipaccess_head *) buf;
546
Harald Welte00a96732019-03-11 17:18:02 +0100547 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
548 * in case of a signal being received */
549
550restart_hdr:
551 /* 1) blocking recv from the socket (IPA header) */
552 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
553 if (rc == -1 && errno == EINTR) {
554 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
555 return -23;
556 goto restart_hdr;
557 } else if (rc < 0)
558 return rc;
559 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200560 return -2;
561
562 len = ntohs(hh->len);
563 needed = len; //- sizeof(*hh);
564
Harald Welte00a96732019-03-11 17:18:02 +0100565restart_body:
566 /* 2) blocking recv from the socket (payload) */
567 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
568 if (rc == -1 && errno == EINTR) {
569 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
570 return -23;
571 goto restart_body;
572 } else if (rc < 0)
573 return rc;
574 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200575 return -3;
576
577 return len;
578}
579
Harald Welte796a7492018-09-23 19:31:28 +0200580static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
581{
582 struct msgb *msg = rspro_enc_msg(pdu);
583 int rc;
584
585 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100586 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200587 LOGW(worker, "error encoding RSPRO\n");
588 return -1;
589 }
590
Harald Weltefd471192018-09-24 14:51:14 +0200591 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200592 /* prepend the header */
593 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200594 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200595
596 /* actually send it through the socket */
597 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
598 if (rc == msgb_length(msg))
599 rc = 0;
600 else {
601 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
602 rc = -1;
603 }
604
605 msgb_free(msg);
606
607 return rc;
608}
609
Harald Welte150d6d62018-10-03 23:07:47 +0200610/* attempt to obtain slot-map */
611static int worker_try_slotmap(struct bankd_worker *worker)
612{
Harald Weltecbd18962019-03-03 19:02:38 +0100613 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200614
Harald Weltecbd18962019-03-03 19:02:38 +0100615 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200616 if (!slmap) {
617 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
618 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
619 /* check in 10s if the map has been installed meanwhile by main thread */
620 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
621 return -1;
622 } else {
623 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
624 slmap->client.client_id, slmap->client.slot_nr,
625 slmap->bank.bank_id, slmap->bank.slot_nr);
626 worker->slot = slmap->bank;
627 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
628 return worker_open_card(worker);
629 }
630}
631
Harald Welte1f699b42019-03-28 13:41:08 +0100632/* inform the remote end (client) about the (new) ATR */
633static int worker_send_atr(struct bankd_worker *worker)
634{
635 RsproPDU_t *set_atr;
636 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
637 worker->client.clslot.slot_nr,
638 worker->card.atr, worker->card.atr_len);
639 if (!set_atr)
640 return -1;
641 return worker_send_rspro(worker, set_atr);
642}
Harald Welte150d6d62018-10-03 23:07:47 +0200643
Harald Weltecce2aad2018-08-16 14:44:37 +0200644static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
645{
Harald Welteaf614732018-08-17 22:10:05 +0200646 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100647 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200648 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100649 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200650
Harald Weltecce2aad2018-08-16 14:44:37 +0200651 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
652
Harald Welte9f7ca612021-12-08 15:29:28 +0100653 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200654 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
655 /* FIXME: store somewhere? */
656
657 if (worker->state != BW_ST_CONN_WAIT_ID) {
658 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100659 rc = -102;
660 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200661 }
662
Harald Welte371d0262018-08-16 15:23:58 +0200663 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200664 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100665 res = ResultCode_illegalClientId;
666 rc = -103;
667 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200668 }
Harald Welte371d0262018-08-16 15:23:58 +0200669 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
670 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200671 worker_set_state(worker, BW_ST_CONN_CLIENT);
672
Harald Welte150d6d62018-10-03 23:07:47 +0200673 if (worker_try_slotmap(worker) >= 0)
674 res = ResultCode_ok;
675 else
Harald Welte3e689872018-09-24 14:52:56 +0200676 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200677
Harald Welte3e689872018-09-24 14:52:56 +0200678 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100679 rc = worker_send_rspro(worker, resp);
680 if (rc < 0)
681 return rc;
682
683 if (res == ResultCode_ok)
684 rc = worker_send_atr(worker);
685
686 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100687
688respond_and_err:
689 if (res) {
690 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
691 worker_send_rspro(worker, resp);
692 }
693 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200694}
695
Harald Welte796a7492018-09-23 19:31:28 +0200696static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
697{
698 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200699 uint8_t rx_buf[1024];
700 DWORD rx_buf_len = sizeof(rx_buf);
701 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100702 struct client_slot clslot;
703 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100704 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200705
Harald Welte9f7ca612021-12-08 15:29:28 +0100706 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
707 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200708
709 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
710 LOGW(worker, "Unexpected tpduModemToCaard\n");
711 return -104;
712 }
713
Harald Weltee1d32892019-03-27 20:47:42 +0100714 /* Validate that toBankSlot / fromClientSlot match our expectations */
715 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
716 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
717 if (!bank_slot_equals(&worker->slot, &bslot)) {
718 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
719 bslot.bank_id, bslot.slot_nr);
720 return -105;
721 }
722 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
723 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
724 clslot.client_id, clslot.slot_nr);
725 return -106;
726 }
Harald Welte796a7492018-09-23 19:31:28 +0200727
Harald Welte297d72e2019-03-28 18:42:35 +0100728 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
729 rx_buf, &rx_buf_len);
730 if (rc < 0)
731 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200732
733 /* encode response PDU and send it */
734 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
735 rx_buf, rx_buf_len);
736 worker_send_rspro(worker, pdu_resp);
737
738 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200739}
740
Harald Weltebbd18bd2019-12-16 13:11:50 +0100741static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
742{
743 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
744 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
745 int rc = 0;
746
Harald Welte9f7ca612021-12-08 15:29:28 +0100747 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100748 sps->resetActive ? "ACTIVE" : "INACTIVE",
749 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
750 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
751
752 /* perform cold or warm reset */
753 if (sps->vccPresent && *sps->vccPresent == 0)
754 rc = worker->ops->reset_card(worker, true);
755 else if (sps->resetActive)
756 rc = worker->ops->reset_card(worker, false);
757
758 return rc;
759}
760
Harald Welte77911b02018-08-14 23:47:30 +0200761/* handle one incoming RSPRO message from a client inside a worker thread */
762static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
763{
Harald Weltecce2aad2018-08-16 14:44:37 +0200764 int rc = -100;
765
Harald Welte77911b02018-08-14 23:47:30 +0200766 switch (pdu->msg.present) {
767 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200768 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200769 break;
770 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200771 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200772 break;
773 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100774 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200775 rc = 0;
776 break;
777 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100778 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200779 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200780 break;
781 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100782 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200783 rc = -101;
784 break;
Harald Welte77911b02018-08-14 23:47:30 +0200785 }
786
Harald Weltecce2aad2018-08-16 14:44:37 +0200787 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200788}
789
Harald Welte694df832018-10-03 22:47:52 +0200790static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
791{
792 struct timeval tout = { timeout_secs, 0 };
793 fd_set readset;
794
795 FD_ZERO(&readset);
796 FD_SET(fd, &readset);
797 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
798}
799
Harald Welte77911b02018-08-14 23:47:30 +0200800/* body of the main transceive loop */
801static int worker_transceive_loop(struct bankd_worker *worker)
802{
803 struct ipaccess_head *hh;
804 struct ipaccess_head_ext *hh_ext;
805 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
806 asn_dec_rval_t rval;
807 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200808 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200809
Harald Welte00a96732019-03-11 17:18:02 +0100810restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200811 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100812 if (rc == -1 && errno == EINTR) {
813 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
814 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100815 else
816 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100817 goto restart_wait;
818 } else if (rc < 0)
819 return rc;
820 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200821 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200822 switch (worker->state) {
823 case BW_ST_CONN_CLIENT_WAIT_MAP:
824 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100825 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200826 break;
827 case BW_ST_CONN_CLIENT_MAPPED:
828 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100829 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200830 break;
831 default:
832 OSMO_ASSERT(0);
833 }
Harald Welte1f699b42019-03-28 13:41:08 +0100834 if (rc == 0)
835 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200836 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200837 return 0;
838 };
839
Harald Welte77911b02018-08-14 23:47:30 +0200840 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100841 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200842 if (rc < 0)
843 return rc;
844 data_len = rc;
845
846 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200847 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200848 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200849 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200850 }
Harald Welte77911b02018-08-14 23:47:30 +0200851
Harald Welte5a3613a2018-10-11 12:56:21 +0200852 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100853 switch (hh->data[0]) {
854 case IPAC_MSGT_PING:
855 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100856 case IPAC_MSGT_ID_ACK:
857 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100858 default:
859 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
860 break;
861 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200862 return 0;
863 }
864
Harald Welte77911b02018-08-14 23:47:30 +0200865 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200866 if (data_len < sizeof(*hh_ext)) {
867 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200868 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200869 }
Harald Welte77911b02018-08-14 23:47:30 +0200870 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200871 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
872 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200873 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200874 }
Harald Welte77911b02018-08-14 23:47:30 +0200875
876 /* 2) ASN1 BER decode of the message */
877 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200878 if (rval.code != RC_OK) {
879 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200880 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200881 }
Harald Welte77911b02018-08-14 23:47:30 +0200882
883 /* 3) handling of the message, possibly resulting in PCSC commands */
884 rc = worker_handle_rspro(worker, pdu);
885 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200886 if (rc < 0) {
887 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200888 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200889 }
Harald Welte77911b02018-08-14 23:47:30 +0200890
891 /* everything OK if we reach here */
892 return 0;
893}
894
Harald Welted6dfb8c2018-08-16 14:46:53 +0200895/* obtain an ascii representation of the client IP/port */
896static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
897{
898 char hostbuf[32], portbuf[32];
899 int rc;
900
901 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
902 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
903 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
904 if (rc != 0) {
905 out[0] = '\0';
906 return -1;
907 }
908 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
909 return 0;
910}
911
Harald Welte77911b02018-08-14 23:47:30 +0200912/* worker thread main function */
913static void *worker_main(void *arg)
914{
Harald Welte77911b02018-08-14 23:47:30 +0200915 void *top_ctx;
916 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200917
Harald Welte00a96732019-03-11 17:18:02 +0100918 g_worker = (struct bankd_worker *) arg;
919
Harald Welte00a96732019-03-11 17:18:02 +0100920 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200921
Harald Welte77911b02018-08-14 23:47:30 +0200922 /* not permitted in multithreaded environment */
923 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100924 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
925 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200926
Harald Welte286a2be2019-03-11 17:36:58 +0100927 /* set the thread name */
928 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
929 pthread_setname_np(pthread_self(), g_worker->name);
930
Harald Welte77911b02018-08-14 23:47:30 +0200931 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100932 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200933
Harald Welte602b6f72019-12-04 21:51:02 +0100934 g_worker->slot.bank_id = 0xffff;
935 g_worker->slot.slot_nr = 0xffff;
936
Harald Welte77911b02018-08-14 23:47:30 +0200937 /* we continuously perform the same loop here, recycling the worker thread
938 * once the client connection is gone or we have some trouble with the card/reader */
939 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200940 char buf[128];
941
Harald Welte00a96732019-03-11 17:18:02 +0100942 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200943
Harald Welte00a96732019-03-11 17:18:02 +0100944 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200945 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100946 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
947 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200948 if (rc < 0) {
949 continue;
950 }
Harald Welte00a96732019-03-11 17:18:02 +0100951 g_worker->client.fd = rc;
952 worker_client_addrstr(buf, sizeof(buf), g_worker);
953 LOGW(g_worker, "Accepted connection from %s\n", buf);
954 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200955
956 /* run the main worker transceive loop body until there was some error */
957 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100958 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200959 if (rc < 0)
960 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100961 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
962 break;
Harald Welte77911b02018-08-14 23:47:30 +0200963 }
964
Harald Welte00a96732019-03-11 17:18:02 +0100965 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200966
Harald Welte77911b02018-08-14 23:47:30 +0200967 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100968 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100969 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100970 if (g_worker->reader.name)
971 g_worker->reader.name = NULL;
972 if (g_worker->client.fd >= 0)
973 close(g_worker->client.fd);
974 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
975 g_worker->client.fd = -1;
976 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200977 }
978
979 pthread_cleanup_pop(1);
980 talloc_free(top_ctx);
981 pthread_exit(NULL);
982}