blob: fcdbf76f863057b337979c2014f94f505edab6c2 [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
173 LOGPFSM(srvc->fi, "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:
177 /* Store 'identity' of server in srvc->peer_comp_id */
178 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
179 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
180 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100181 case RsproPDUchoice_PR_createMappingReq:
182 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200183 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100184 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200185 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100186 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200187 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100188 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200189 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100190 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100191 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100192 rspro2bank_slot(&bs, &creq->bank);
193 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100194 /* check if map exists */
195 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
196 if (map) {
197 if (client_slot_equals(&map->client, &cs)) {
198 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
199 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
200 goto send_resp;
201 } else {
202 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
203 bankd_srvc_remove_mapping(map);
204 }
205 }
Harald Welte454f5e22019-03-09 21:38:34 +0100206 /* Add a new mapping */
207 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100208 if (!map) {
209 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100210 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100211 } else {
212 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100213 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100214 }
Harald Welte454f5e22019-03-09 21:38:34 +0100215 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100216send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100217 server_conn_send_rspro(srvc, resp);
218 break;
219 case RsproPDUchoice_PR_removeMappingReq:
220 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200221 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100222 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100223 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100224 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200225 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100226 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100227 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100228 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100229 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100230 rspro2bank_slot(&bs, &rreq->bank);
231 /* Remove a mapping */
232 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100233 if (!map) {
234 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100235 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100236 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100237 rspro2client_slot(&cs, &rreq->client);
238 if (!client_slot_equals(&map->client, &cs)) {
239 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
240 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
241 } else {
242 LOGPFSM(srvc->fi, "removing slotmap\n");
243 bankd_srvc_remove_mapping(map);
244 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
245 }
Harald Welte454f5e22019-03-09 21:38:34 +0100246 }
247 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100248 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100249 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100250 case RsproPDUchoice_PR_resetStateReq:
251 /* delete all slotmaps */
252 slotmap_del_all(g_bankd->slotmaps);
253 /* notify all workers about maps having disappeared */
254 pthread_mutex_lock(&g_bankd->workers_mutex);
255 llist_for_each_entry(worker, &g_bankd->workers, list) {
256 pthread_kill(worker->thread, SIGMAPDEL);
257 }
258 pthread_mutex_unlock(&g_bankd->workers_mutex);
259 /* send response to server */
260 resp = rspro_gen_ResetStateRes(ResultCode_ok);
261 server_conn_send_rspro(srvc, resp);
262 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100263 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100264 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
265 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100266 return -1;
267 }
268
269 return 0;
270}
271
Harald Welte5bae20b2019-04-01 09:36:42 +0200272static void printf_help()
273{
274 printf(
275" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100276" -V --version Print the version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +0200277" -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200278" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
279" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
Harald Welte55f12b82022-01-16 14:34:12 +0100280" -b --bank-id <1-1023> Bank Identifier of this SIM bank (default: 1)\n"
281" -n --num-slots <1-1023> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200282" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
283" connections (default: INADDR_ANY)\n"
284" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
285" connectionss (default: 9999)\n"
286 );
287}
288
289static int g_bind_port = 9999;
290static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100291
Harald Weltef8c6eeb2021-12-08 20:33:00 +0100292static void handle_options(int argc, char **argv)
Harald Welte707c85a2019-03-09 12:56:35 +0100293{
Harald Welte5bae20b2019-04-01 09:36:42 +0200294 while (1) {
295 int option_index = 0, c;
296 static const struct option long_options[] = {
297 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100298 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +0200299 { "debug", 1, 0, 'd' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200300 { "server-host", 1, 0, 'i' },
301 { "server-port", 1, 0, 'p' },
302 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200303 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200304 { "component-name", 1, 0, 'N' },
305 { "bind-ip", 1, 0, 'I' },
306 { "bind-port", 1, 0, 'P' },
307 { 0, 0, 0, 0 }
308 };
309
Harald Welte80a01102020-05-25 14:55:12 +0200310 c = getopt_long(argc, argv, "hVd:i:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200311 if (c == -1)
312 break;
313
314 switch (c) {
315 case 'h':
316 printf_help();
317 exit(0);
318 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100319 case 'V':
320 printf("osmo-remsim-bankd version %s\n", VERSION);
321 exit(0);
322 break;
Harald Welte80a01102020-05-25 14:55:12 +0200323 case 'd':
324 log_parse_category_mask(osmo_stderr_target, optarg);
325 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200326 case 'i':
327 g_bankd->srvc.server_host = optarg;
328 break;
329 case 'p':
330 g_bankd->srvc.server_port = atoi(optarg);
331 break;
332 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200333 g_bankd->srvc.bankd.bank_id = atoi(optarg);
334 break;
335 case 'n':
336 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200337 break;
338 case 'N':
339 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
340 break;
341 case 'I':
342 g_bind_ip = optarg;
343 break;
344 case 'P':
345 g_bind_port = atoi(optarg);
346 break;
347 }
348 }
Harald Welte707c85a2019-03-09 12:56:35 +0100349}
350
Harald Welte77911b02018-08-14 23:47:30 +0200351int main(int argc, char **argv)
352{
Harald Weltef4b16f12019-03-09 20:58:17 +0100353 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200354 int i, rc;
355
Harald Weltef4b16f12019-03-09 20:58:17 +0100356 g_bankd = talloc_zero(NULL, struct bankd);
357 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200358
Harald Weltef4b16f12019-03-09 20:58:17 +0100359 bankd_init(g_bankd);
360
361 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100362 srvc->server_host = "localhost";
363 srvc->server_port = 9998;
364 srvc->handle_rx = bankd_srvc_handle_rx;
365 srvc->own_comp_id.type = ComponentType_remsimBankd;
366 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
367 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
368 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
369
370 handle_options(argc, argv);
371
Harald Welte25075972019-03-11 17:33:17 +0100372 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100373 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100374 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100375 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100376
Harald Weltee89fecd2019-04-04 09:23:13 +0200377 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
378 * read once during bankd initialization, when the worker threads haven't
379 * started yet */
380 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
381
Harald Welte707c85a2019-03-09 12:56:35 +0100382 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100383 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100384 if (rc < 0) {
385 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
386 exit(1);
387 }
Harald Welted2192e22019-11-07 18:10:57 +0100388 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100389
390 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200391 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 +0200392 if (rc < 0)
393 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100394 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200395
Harald Weltea0f39502019-03-09 20:59:34 +0100396 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200397 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200398 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100399 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200400 if (!w)
401 exit(21);
402 }
403
404 while (1) {
405 if (terminate)
406 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100407 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200408 }
409
Harald Weltef4b16f12019-03-09 20:58:17 +0100410 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200411 exit(0);
412}
413
414
415
416/***********************************************************************
417 * bankd worker thread
418 ***********************************************************************/
419
Harald Welte00a96732019-03-11 17:18:02 +0100420static __thread struct bankd_worker *g_worker;
421
Harald Welte8d858292018-08-15 23:36:46 +0200422struct value_string worker_state_names[] = {
423 { BW_ST_INIT, "INIT" },
424 { BW_ST_ACCEPTING, "ACCEPTING" },
425 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
426 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200427 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200428 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
429 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100430 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200431 { 0, NULL }
432};
433
Harald Welte1f699b42019-03-28 13:41:08 +0100434static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
435
Harald Welte8d858292018-08-15 23:36:46 +0200436static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
437{
438 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
439 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200440 worker->timeout = 0;
441}
442
443static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
444 unsigned int timeout_secs)
445{
446 LOGW(worker, "Changing state to %s (timeout=%u)\n",
447 get_value_string(worker_state_names, new_state), timeout_secs);
448 worker->state = new_state;
449 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200450}
Harald Welte77911b02018-08-14 23:47:30 +0200451
Harald Welte00a96732019-03-11 17:18:02 +0100452/* signal handler for receiving SIGMAPDEL from main thread */
453static void handle_sig_mapdel(int sig)
454{
455 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
456 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100457 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
458 g_worker->slot.bank_id = 0xffff;
459 g_worker->slot.slot_nr = 0xffff;
460 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
461 }
Harald Welte00a96732019-03-11 17:18:02 +0100462}
463
Harald Welte0fd77a52019-12-05 22:05:39 +0100464/* signal handler for receiving SIGMAPADD from main thread */
465static void handle_sig_mapadd(int sig)
466{
467 LOGW(g_worker, "SIGMAPADD received\n");
468 /* do nothing */
469}
470
Harald Welte25075972019-03-11 17:33:17 +0100471static void handle_sig_usr1(int sig)
472{
473 OSMO_ASSERT(sig == SIGUSR1);
474
475 if (pthread_equal(g_bankd->main, pthread_self())) {
476 struct bankd_worker *worker;
477 /* main thread */
478 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200479 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100480
481 /* iterate over worker threads and ask them to dump their talloc state */
482 pthread_mutex_lock(&g_bankd->workers_mutex);
483 llist_for_each_entry(worker, &g_bankd->workers, list) {
484 pthread_kill(worker->thread, SIGUSR1);
485 }
486 pthread_mutex_unlock(&g_bankd->workers_mutex);
487 } else {
488 /* worker thread */
489 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200490 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100491 }
492}
493
Harald Welte77911b02018-08-14 23:47:30 +0200494static void worker_cleanup(void *arg)
495{
496 struct bankd_worker *worker = (struct bankd_worker *) arg;
497 struct bankd *bankd = worker->bankd;
498
499 /* FIXME: should we still do this? in the thread ?!? */
500 pthread_mutex_lock(&bankd->workers_mutex);
501 llist_del(&worker->list);
502 talloc_free(worker); /* FIXME: is this safe? */
503 pthread_mutex_unlock(&bankd->workers_mutex);
504}
505
Harald Welteaf614732018-08-17 22:10:05 +0200506static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200507{
Harald Welte297d72e2019-03-28 18:42:35 +0100508 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200509
Harald Welte150d6d62018-10-03 23:07:47 +0200510 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
511
Harald Welte694df832018-10-03 22:47:52 +0200512 if (!worker->reader.name) {
513 /* resolve PC/SC reader name from slot_id -> name map */
514 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
515 if (!worker->reader.name) {
516 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
517 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100518 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200519 }
520 }
Harald Welte45c948c2018-09-23 19:26:52 +0200521 OSMO_ASSERT(worker->reader.name);
522
Harald Welte297d72e2019-03-28 18:42:35 +0100523 rc = worker->ops->open_card(worker);
524 if (rc < 0)
525 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100526
Harald Welte57593f02018-09-23 19:30:31 +0200527 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200528 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200529
Harald Welteaf614732018-08-17 22:10:05 +0200530 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200531}
Harald Welte77911b02018-08-14 23:47:30 +0200532
533
Harald Welte00a96732019-03-11 17:18:02 +0100534static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200535{
536 struct ipaccess_head *hh;
537 uint16_t len;
538 int needed, rc;
539
540 if (buf_size < sizeof(*hh))
541 return -1;
542
543 hh = (struct ipaccess_head *) buf;
544
Harald Welte00a96732019-03-11 17:18:02 +0100545 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
546 * in case of a signal being received */
547
548restart_hdr:
549 /* 1) blocking recv from the socket (IPA header) */
550 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
551 if (rc == -1 && errno == EINTR) {
552 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
553 return -23;
554 goto restart_hdr;
555 } else if (rc < 0)
556 return rc;
557 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200558 return -2;
559
560 len = ntohs(hh->len);
561 needed = len; //- sizeof(*hh);
562
Harald Welte00a96732019-03-11 17:18:02 +0100563restart_body:
564 /* 2) blocking recv from the socket (payload) */
565 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
566 if (rc == -1 && errno == EINTR) {
567 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
568 return -23;
569 goto restart_body;
570 } else if (rc < 0)
571 return rc;
572 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200573 return -3;
574
575 return len;
576}
577
Harald Welte796a7492018-09-23 19:31:28 +0200578static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
579{
580 struct msgb *msg = rspro_enc_msg(pdu);
581 int rc;
582
583 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100584 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200585 LOGW(worker, "error encoding RSPRO\n");
586 return -1;
587 }
588
Harald Weltefd471192018-09-24 14:51:14 +0200589 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200590 /* prepend the header */
591 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200592 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200593
594 /* actually send it through the socket */
595 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
596 if (rc == msgb_length(msg))
597 rc = 0;
598 else {
599 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
600 rc = -1;
601 }
602
603 msgb_free(msg);
604
605 return rc;
606}
607
Harald Welte150d6d62018-10-03 23:07:47 +0200608/* attempt to obtain slot-map */
609static int worker_try_slotmap(struct bankd_worker *worker)
610{
Harald Weltecbd18962019-03-03 19:02:38 +0100611 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200612
Harald Weltecbd18962019-03-03 19:02:38 +0100613 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200614 if (!slmap) {
615 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
616 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
617 /* check in 10s if the map has been installed meanwhile by main thread */
618 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
619 return -1;
620 } else {
621 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
622 slmap->client.client_id, slmap->client.slot_nr,
623 slmap->bank.bank_id, slmap->bank.slot_nr);
624 worker->slot = slmap->bank;
625 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
626 return worker_open_card(worker);
627 }
628}
629
Harald Welte1f699b42019-03-28 13:41:08 +0100630/* inform the remote end (client) about the (new) ATR */
631static int worker_send_atr(struct bankd_worker *worker)
632{
633 RsproPDU_t *set_atr;
634 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
635 worker->client.clslot.slot_nr,
636 worker->card.atr, worker->card.atr_len);
637 if (!set_atr)
638 return -1;
639 return worker_send_rspro(worker, set_atr);
640}
Harald Welte150d6d62018-10-03 23:07:47 +0200641
Harald Weltecce2aad2018-08-16 14:44:37 +0200642static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
643{
Harald Welteaf614732018-08-17 22:10:05 +0200644 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100645 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200646 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100647 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200648
Harald Weltecce2aad2018-08-16 14:44:37 +0200649 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
650
Harald Welte9f7ca612021-12-08 15:29:28 +0100651 LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
Harald Weltecce2aad2018-08-16 14:44:37 +0200652 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
653 /* FIXME: store somewhere? */
654
655 if (worker->state != BW_ST_CONN_WAIT_ID) {
656 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100657 rc = -102;
658 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200659 }
660
Harald Welte371d0262018-08-16 15:23:58 +0200661 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200662 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100663 res = ResultCode_illegalClientId;
664 rc = -103;
665 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200666 }
Harald Welte371d0262018-08-16 15:23:58 +0200667 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
668 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200669 worker_set_state(worker, BW_ST_CONN_CLIENT);
670
Harald Welte150d6d62018-10-03 23:07:47 +0200671 if (worker_try_slotmap(worker) >= 0)
672 res = ResultCode_ok;
673 else
Harald Welte3e689872018-09-24 14:52:56 +0200674 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200675
Harald Welte3e689872018-09-24 14:52:56 +0200676 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100677 rc = worker_send_rspro(worker, resp);
678 if (rc < 0)
679 return rc;
680
681 if (res == ResultCode_ok)
682 rc = worker_send_atr(worker);
683
684 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100685
686respond_and_err:
687 if (res) {
688 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
689 worker_send_rspro(worker, resp);
690 }
691 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200692}
693
Harald Welte796a7492018-09-23 19:31:28 +0200694static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
695{
696 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200697 uint8_t rx_buf[1024];
698 DWORD rx_buf_len = sizeof(rx_buf);
699 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100700 struct client_slot clslot;
701 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100702 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200703
Harald Welte9f7ca612021-12-08 15:29:28 +0100704 LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n",
705 osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
Harald Welte796a7492018-09-23 19:31:28 +0200706
707 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
708 LOGW(worker, "Unexpected tpduModemToCaard\n");
709 return -104;
710 }
711
Harald Weltee1d32892019-03-27 20:47:42 +0100712 /* Validate that toBankSlot / fromClientSlot match our expectations */
713 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
714 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
715 if (!bank_slot_equals(&worker->slot, &bslot)) {
716 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
717 bslot.bank_id, bslot.slot_nr);
718 return -105;
719 }
720 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
721 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
722 clslot.client_id, clslot.slot_nr);
723 return -106;
724 }
Harald Welte796a7492018-09-23 19:31:28 +0200725
Harald Welte297d72e2019-03-28 18:42:35 +0100726 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
727 rx_buf, &rx_buf_len);
728 if (rc < 0)
729 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200730
731 /* encode response PDU and send it */
732 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
733 rx_buf, rx_buf_len);
734 worker_send_rspro(worker, pdu_resp);
735
736 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200737}
738
Harald Weltebbd18bd2019-12-16 13:11:50 +0100739static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
740{
741 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
742 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
743 int rc = 0;
744
Harald Welte9f7ca612021-12-08 15:29:28 +0100745 LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
Harald Weltebbd18bd2019-12-16 13:11:50 +0100746 sps->resetActive ? "ACTIVE" : "INACTIVE",
747 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
748 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
749
750 /* perform cold or warm reset */
751 if (sps->vccPresent && *sps->vccPresent == 0)
752 rc = worker->ops->reset_card(worker, true);
753 else if (sps->resetActive)
754 rc = worker->ops->reset_card(worker, false);
755
756 return rc;
757}
758
Harald Welte77911b02018-08-14 23:47:30 +0200759/* handle one incoming RSPRO message from a client inside a worker thread */
760static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
761{
Harald Weltecce2aad2018-08-16 14:44:37 +0200762 int rc = -100;
763
Harald Welte77911b02018-08-14 23:47:30 +0200764 switch (pdu->msg.present) {
765 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200766 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200767 break;
768 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200769 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200770 break;
771 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100772 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200773 rc = 0;
774 break;
775 case RsproPDUchoice_PR_setAtrRes:
Harald Welte9f7ca612021-12-08 15:29:28 +0100776 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte24a3e322019-03-31 13:08:30 +0200777 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200778 break;
779 default:
Harald Welte9f7ca612021-12-08 15:29:28 +0100780 LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu));
Harald Weltecce2aad2018-08-16 14:44:37 +0200781 rc = -101;
782 break;
Harald Welte77911b02018-08-14 23:47:30 +0200783 }
784
Harald Weltecce2aad2018-08-16 14:44:37 +0200785 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200786}
787
Harald Welte694df832018-10-03 22:47:52 +0200788static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
789{
790 struct timeval tout = { timeout_secs, 0 };
791 fd_set readset;
792
793 FD_ZERO(&readset);
794 FD_SET(fd, &readset);
795 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
796}
797
Harald Welte77911b02018-08-14 23:47:30 +0200798/* body of the main transceive loop */
799static int worker_transceive_loop(struct bankd_worker *worker)
800{
801 struct ipaccess_head *hh;
802 struct ipaccess_head_ext *hh_ext;
803 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
804 asn_dec_rval_t rval;
805 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200806 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200807
Harald Welte00a96732019-03-11 17:18:02 +0100808restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200809 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100810 if (rc == -1 && errno == EINTR) {
811 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
812 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100813 else
814 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100815 goto restart_wait;
816 } else if (rc < 0)
817 return rc;
818 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200819 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200820 switch (worker->state) {
821 case BW_ST_CONN_CLIENT_WAIT_MAP:
822 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100823 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200824 break;
825 case BW_ST_CONN_CLIENT_MAPPED:
826 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100827 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200828 break;
829 default:
830 OSMO_ASSERT(0);
831 }
Harald Welte1f699b42019-03-28 13:41:08 +0100832 if (rc == 0)
833 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200834 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200835 return 0;
836 };
837
Harald Welte77911b02018-08-14 23:47:30 +0200838 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100839 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200840 if (rc < 0)
841 return rc;
842 data_len = rc;
843
844 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200845 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200846 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200847 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200848 }
Harald Welte77911b02018-08-14 23:47:30 +0200849
Harald Welte5a3613a2018-10-11 12:56:21 +0200850 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100851 switch (hh->data[0]) {
852 case IPAC_MSGT_PING:
853 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100854 case IPAC_MSGT_ID_ACK:
855 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100856 default:
857 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
858 break;
859 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200860 return 0;
861 }
862
Harald Welte77911b02018-08-14 23:47:30 +0200863 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200864 if (data_len < sizeof(*hh_ext)) {
865 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200866 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200867 }
Harald Welte77911b02018-08-14 23:47:30 +0200868 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200869 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
870 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200871 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200872 }
Harald Welte77911b02018-08-14 23:47:30 +0200873
874 /* 2) ASN1 BER decode of the message */
875 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200876 if (rval.code != RC_OK) {
877 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200878 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200879 }
Harald Welte77911b02018-08-14 23:47:30 +0200880
881 /* 3) handling of the message, possibly resulting in PCSC commands */
882 rc = worker_handle_rspro(worker, pdu);
883 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200884 if (rc < 0) {
885 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200886 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200887 }
Harald Welte77911b02018-08-14 23:47:30 +0200888
889 /* everything OK if we reach here */
890 return 0;
891}
892
Harald Welted6dfb8c2018-08-16 14:46:53 +0200893/* obtain an ascii representation of the client IP/port */
894static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
895{
896 char hostbuf[32], portbuf[32];
897 int rc;
898
899 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
900 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
901 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
902 if (rc != 0) {
903 out[0] = '\0';
904 return -1;
905 }
906 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
907 return 0;
908}
909
Harald Welte77911b02018-08-14 23:47:30 +0200910/* worker thread main function */
911static void *worker_main(void *arg)
912{
Harald Welte77911b02018-08-14 23:47:30 +0200913 void *top_ctx;
914 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200915
Harald Welte00a96732019-03-11 17:18:02 +0100916 g_worker = (struct bankd_worker *) arg;
917
Harald Welte00a96732019-03-11 17:18:02 +0100918 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200919
Harald Welte77911b02018-08-14 23:47:30 +0200920 /* not permitted in multithreaded environment */
921 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100922 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
923 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200924
Harald Welte286a2be2019-03-11 17:36:58 +0100925 /* set the thread name */
926 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
927 pthread_setname_np(pthread_self(), g_worker->name);
928
Harald Welte77911b02018-08-14 23:47:30 +0200929 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100930 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200931
Harald Welte602b6f72019-12-04 21:51:02 +0100932 g_worker->slot.bank_id = 0xffff;
933 g_worker->slot.slot_nr = 0xffff;
934
Harald Welte77911b02018-08-14 23:47:30 +0200935 /* we continuously perform the same loop here, recycling the worker thread
936 * once the client connection is gone or we have some trouble with the card/reader */
937 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200938 char buf[128];
939
Harald Welte00a96732019-03-11 17:18:02 +0100940 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200941
Harald Welte00a96732019-03-11 17:18:02 +0100942 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200943 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100944 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
945 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200946 if (rc < 0) {
947 continue;
948 }
Harald Welte00a96732019-03-11 17:18:02 +0100949 g_worker->client.fd = rc;
950 worker_client_addrstr(buf, sizeof(buf), g_worker);
951 LOGW(g_worker, "Accepted connection from %s\n", buf);
952 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200953
954 /* run the main worker transceive loop body until there was some error */
955 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100956 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200957 if (rc < 0)
958 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100959 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
960 break;
Harald Welte77911b02018-08-14 23:47:30 +0200961 }
962
Harald Welte00a96732019-03-11 17:18:02 +0100963 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200964
Harald Welte77911b02018-08-14 23:47:30 +0200965 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100966 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100967 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100968 if (g_worker->reader.name)
969 g_worker->reader.name = NULL;
970 if (g_worker->client.fd >= 0)
971 close(g_worker->client.fd);
972 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
973 g_worker->client.fd = -1;
974 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200975 }
976
977 pthread_cleanup_pop(1);
978 talloc_free(top_ctx);
979 pthread_exit(NULL);
980}