blob: b3e38b307d342b6b108a94282f9172efdf688fbe [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 Welte77911b02018-08-14 23:47:30 +020042
43#include <osmocom/gsm/ipa.h>
44#include <osmocom/gsm/protocol/ipaccess.h>
45
Kévin Redonff5db6e2018-10-11 17:16:18 +020046#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020047#include <osmocom/rspro/RsproPDU.h>
48
49#include "bankd.h"
Harald Welte3cded632019-03-09 12:59:41 +010050#include "rspro_client_fsm.h"
Harald Welte61d98e92019-03-03 15:43:07 +010051#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020052#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020053
Harald Welte00a96732019-03-11 17:18:02 +010054/* signal indicates to worker thread that its map has been deleted */
55#define SIGMAPDEL SIGRTMIN+1
Harald Welte0fd77a52019-12-05 22:05:39 +010056#define SIGMAPADD SIGRTMIN+2
Harald Welte25075972019-03-11 17:33:17 +010057
58static void handle_sig_usr1(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010059static void handle_sig_mapdel(int sig);
Harald Welte0fd77a52019-12-05 22:05:39 +010060static void handle_sig_mapadd(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010061
Harald Welte77911b02018-08-14 23:47:30 +020062__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010063struct bankd *g_bankd;
Harald Welte25075972019-03-11 17:33:17 +010064static void *g_tall_ctx;
Harald Welte77911b02018-08-14 23:47:30 +020065
66static void *worker_main(void *arg);
67
68/***********************************************************************
69* bankd core / main thread
70***********************************************************************/
71
Harald Welte43ab79f2018-10-03 23:34:21 +020072int asn_debug;
73
Harald Welte77911b02018-08-14 23:47:30 +020074static void bankd_init(struct bankd *bankd)
75{
Harald Welte25075972019-03-11 17:33:17 +010076 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Weltef94b9ee2018-09-25 15:04:21 +020077 osmo_init_logging2(g_tall_ctx, &log_info);
78
Harald Welte43ab79f2018-10-03 23:34:21 +020079 asn_debug = 0;
80
Harald Welte77911b02018-08-14 23:47:30 +020081 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010082 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020083 INIT_LLIST_HEAD(&bankd->workers);
84 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020085
Harald Weltea0f39502019-03-09 20:59:34 +010086 /* set some defaults, overridden by commandline/config */
Harald Welte2513d812019-04-01 21:03:02 +020087 bankd->srvc.bankd.bank_id = 1;
88 bankd->srvc.bankd.num_slots = 8;
Harald Weltea0f39502019-03-09 20:59:34 +010089
Harald Weltef1dd1622018-09-24 14:54:23 +020090 bankd->comp_id.type = ComponentType_remsimBankd;
91 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
92 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
93 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
94 /* FIXME: other members of app_comp_id */
95
Harald Welte45c948c2018-09-23 19:26:52 +020096 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
Harald Welte77911b02018-08-14 23:47:30 +020097}
98
99/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200100static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200101{
102 struct bankd_worker *worker;
103 int rc;
104
105 worker = talloc_zero(bankd, struct bankd_worker);
106 if (!worker)
107 return NULL;
108
109 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200110 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100111 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200112
113 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
114
115 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
116 if (rc != 0) {
117 talloc_free(worker);
118 return NULL;
119 }
120
121 pthread_mutex_lock(&bankd->workers_mutex);
122 llist_add_tail(&worker->list, &bankd->workers);
123 pthread_mutex_unlock(&bankd->workers_mutex);
124
125 return worker;
126}
127
128static bool terminate = false;
129
Harald Weltea4967832019-12-05 22:04:57 +0100130/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
131static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
132{
133 struct bankd_worker *worker;
134 pthread_mutex_lock(&g_bankd->workers_mutex);
135 llist_for_each_entry(worker, &g_bankd->workers, list) {
136 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
137 continue;
138 if (cs && (cs->client_id != worker->client.clslot.client_id ||
139 cs->slot_nr != worker->client.clslot.slot_nr))
140 continue;
141
142 pthread_kill(worker->thread, sig);
143 break;
144 }
145 pthread_mutex_unlock(&g_bankd->workers_mutex);
146}
147
Harald Weltec650a4d2019-12-04 15:02:27 +0100148/* Remove a mapping */
149static void bankd_srvc_remove_mapping(struct slot_mapping *map)
150{
151 struct bank_slot bs = map->bank;
152
153 slotmap_del(g_bankd->slotmaps, map);
154
155 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100156 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100157}
158
Harald Welte707c85a2019-03-09 12:56:35 +0100159/* handle incoming messages from server */
160static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
161{
Harald Welte454f5e22019-03-09 21:38:34 +0100162 const CreateMappingReq_t *creq = NULL;
163 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100164 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100165 struct slot_mapping *map;
166 struct bank_slot bs;
167 struct client_slot cs;
168 RsproPDU_t *resp;
169
170 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100171
172 switch (pdu->msg.present) {
173 case RsproPDUchoice_PR_connectBankRes:
174 /* Store 'identity' of server in srvc->peer_comp_id */
175 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
176 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
177 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100178 case RsproPDUchoice_PR_createMappingReq:
179 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200180 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100181 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200182 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100183 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200184 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100185 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200186 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100187 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100188 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100189 rspro2bank_slot(&bs, &creq->bank);
190 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100191 /* check if map exists */
192 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
193 if (map) {
194 if (client_slot_equals(&map->client, &cs)) {
195 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
196 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
197 goto send_resp;
198 } else {
199 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
200 bankd_srvc_remove_mapping(map);
201 }
202 }
Harald Welte454f5e22019-03-09 21:38:34 +0100203 /* Add a new mapping */
204 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100205 if (!map) {
206 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100207 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte0fd77a52019-12-05 22:05:39 +0100208 } else {
209 send_signal_to_worker(NULL, &cs, SIGMAPADD);
Harald Welte454f5e22019-03-09 21:38:34 +0100210 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
Harald Welte0fd77a52019-12-05 22:05:39 +0100211 }
Harald Welte454f5e22019-03-09 21:38:34 +0100212 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100213send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100214 server_conn_send_rspro(srvc, resp);
215 break;
216 case RsproPDUchoice_PR_removeMappingReq:
217 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200218 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100219 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100220 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100221 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200222 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100223 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100224 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100225 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100226 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100227 rspro2bank_slot(&bs, &rreq->bank);
228 /* Remove a mapping */
229 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100230 if (!map) {
231 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100232 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100233 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100234 rspro2client_slot(&cs, &rreq->client);
235 if (!client_slot_equals(&map->client, &cs)) {
236 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
237 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
238 } else {
239 LOGPFSM(srvc->fi, "removing slotmap\n");
240 bankd_srvc_remove_mapping(map);
241 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
242 }
Harald Welte454f5e22019-03-09 21:38:34 +0100243 }
244 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100245 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100246 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100247 case RsproPDUchoice_PR_resetStateReq:
248 /* delete all slotmaps */
249 slotmap_del_all(g_bankd->slotmaps);
250 /* notify all workers about maps having disappeared */
251 pthread_mutex_lock(&g_bankd->workers_mutex);
252 llist_for_each_entry(worker, &g_bankd->workers, list) {
253 pthread_kill(worker->thread, SIGMAPDEL);
254 }
255 pthread_mutex_unlock(&g_bankd->workers_mutex);
256 /* send response to server */
257 resp = rspro_gen_ResetStateRes(ResultCode_ok);
258 server_conn_send_rspro(srvc, resp);
259 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100260 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100261 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
262 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100263 return -1;
264 }
265
266 return 0;
267}
268
Harald Welte5bae20b2019-04-01 09:36:42 +0200269static void printf_help()
270{
271 printf(
272" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100273" -V --version Print the version of the program\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200274" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
275" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
276" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200277" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200278" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
279" connections (default: INADDR_ANY)\n"
280" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
281" connectionss (default: 9999)\n"
282 );
283}
284
285static int g_bind_port = 9999;
286static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100287
Harald Welte707c85a2019-03-09 12:56:35 +0100288void handle_options(int argc, char **argv)
289{
Harald Welte5bae20b2019-04-01 09:36:42 +0200290 while (1) {
291 int option_index = 0, c;
292 static const struct option long_options[] = {
293 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100294 { "version", 0, 0, 'V' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200295 { "server-host", 1, 0, 'i' },
296 { "server-port", 1, 0, 'p' },
297 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200298 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200299 { "component-name", 1, 0, 'N' },
300 { "bind-ip", 1, 0, 'I' },
301 { "bind-port", 1, 0, 'P' },
302 { 0, 0, 0, 0 }
303 };
304
Harald Weltecd7fcd72019-12-03 21:29:07 +0100305 c = getopt_long(argc, argv, "hVi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200306 if (c == -1)
307 break;
308
309 switch (c) {
310 case 'h':
311 printf_help();
312 exit(0);
313 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100314 case 'V':
315 printf("osmo-remsim-bankd version %s\n", VERSION);
316 exit(0);
317 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200318 case 'i':
319 g_bankd->srvc.server_host = optarg;
320 break;
321 case 'p':
322 g_bankd->srvc.server_port = atoi(optarg);
323 break;
324 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200325 g_bankd->srvc.bankd.bank_id = atoi(optarg);
326 break;
327 case 'n':
328 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200329 break;
330 case 'N':
331 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
332 break;
333 case 'I':
334 g_bind_ip = optarg;
335 break;
336 case 'P':
337 g_bind_port = atoi(optarg);
338 break;
339 }
340 }
Harald Welte707c85a2019-03-09 12:56:35 +0100341}
342
Harald Welte77911b02018-08-14 23:47:30 +0200343int main(int argc, char **argv)
344{
Harald Weltef4b16f12019-03-09 20:58:17 +0100345 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200346 int i, rc;
347
Harald Weltef4b16f12019-03-09 20:58:17 +0100348 g_bankd = talloc_zero(NULL, struct bankd);
349 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200350
Harald Weltef4b16f12019-03-09 20:58:17 +0100351 bankd_init(g_bankd);
352
353 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100354 srvc->server_host = "localhost";
355 srvc->server_port = 9998;
356 srvc->handle_rx = bankd_srvc_handle_rx;
357 srvc->own_comp_id.type = ComponentType_remsimBankd;
358 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
359 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
360 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
361
362 handle_options(argc, argv);
363
Harald Welte25075972019-03-11 17:33:17 +0100364 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100365 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte0fd77a52019-12-05 22:05:39 +0100366 signal(SIGMAPADD, handle_sig_mapadd);
Harald Welte25075972019-03-11 17:33:17 +0100367 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100368
Harald Weltee89fecd2019-04-04 09:23:13 +0200369 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
370 * read once during bankd initialization, when the worker threads haven't
371 * started yet */
372 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
373
Harald Welte707c85a2019-03-09 12:56:35 +0100374 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100375 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100376 if (rc < 0) {
377 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
378 exit(1);
379 }
Harald Welted2192e22019-11-07 18:10:57 +0100380 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100381
382 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200383 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 +0200384 if (rc < 0)
385 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100386 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200387
Harald Weltea0f39502019-03-09 20:59:34 +0100388 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200389 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200390 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100391 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200392 if (!w)
393 exit(21);
394 }
395
396 while (1) {
397 if (terminate)
398 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100399 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200400 }
401
Harald Weltef4b16f12019-03-09 20:58:17 +0100402 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200403 exit(0);
404}
405
406
407
408/***********************************************************************
409 * bankd worker thread
410 ***********************************************************************/
411
Harald Welte00a96732019-03-11 17:18:02 +0100412static __thread struct bankd_worker *g_worker;
413
Harald Welte8d858292018-08-15 23:36:46 +0200414struct value_string worker_state_names[] = {
415 { BW_ST_INIT, "INIT" },
416 { BW_ST_ACCEPTING, "ACCEPTING" },
417 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
418 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200419 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200420 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
421 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100422 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200423 { 0, NULL }
424};
425
Harald Welte1f699b42019-03-28 13:41:08 +0100426static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
427
Harald Welte8d858292018-08-15 23:36:46 +0200428static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
429{
430 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
431 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200432 worker->timeout = 0;
433}
434
435static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
436 unsigned int timeout_secs)
437{
438 LOGW(worker, "Changing state to %s (timeout=%u)\n",
439 get_value_string(worker_state_names, new_state), timeout_secs);
440 worker->state = new_state;
441 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200442}
Harald Welte77911b02018-08-14 23:47:30 +0200443
Harald Welte00a96732019-03-11 17:18:02 +0100444/* signal handler for receiving SIGMAPDEL from main thread */
445static void handle_sig_mapdel(int sig)
446{
447 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
448 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100449 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
450 g_worker->slot.bank_id = 0xffff;
451 g_worker->slot.slot_nr = 0xffff;
452 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
453 }
Harald Welte00a96732019-03-11 17:18:02 +0100454}
455
Harald Welte0fd77a52019-12-05 22:05:39 +0100456/* signal handler for receiving SIGMAPADD from main thread */
457static void handle_sig_mapadd(int sig)
458{
459 LOGW(g_worker, "SIGMAPADD received\n");
460 /* do nothing */
461}
462
Harald Welte25075972019-03-11 17:33:17 +0100463static void handle_sig_usr1(int sig)
464{
465 OSMO_ASSERT(sig == SIGUSR1);
466
467 if (pthread_equal(g_bankd->main, pthread_self())) {
468 struct bankd_worker *worker;
469 /* main thread */
470 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200471 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100472
473 /* iterate over worker threads and ask them to dump their talloc state */
474 pthread_mutex_lock(&g_bankd->workers_mutex);
475 llist_for_each_entry(worker, &g_bankd->workers, list) {
476 pthread_kill(worker->thread, SIGUSR1);
477 }
478 pthread_mutex_unlock(&g_bankd->workers_mutex);
479 } else {
480 /* worker thread */
481 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200482 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100483 }
484}
485
Harald Welte77911b02018-08-14 23:47:30 +0200486static void worker_cleanup(void *arg)
487{
488 struct bankd_worker *worker = (struct bankd_worker *) arg;
489 struct bankd *bankd = worker->bankd;
490
491 /* FIXME: should we still do this? in the thread ?!? */
492 pthread_mutex_lock(&bankd->workers_mutex);
493 llist_del(&worker->list);
494 talloc_free(worker); /* FIXME: is this safe? */
495 pthread_mutex_unlock(&bankd->workers_mutex);
496}
497
Harald Welteaf614732018-08-17 22:10:05 +0200498static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200499{
Harald Welte297d72e2019-03-28 18:42:35 +0100500 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200501
Harald Welte150d6d62018-10-03 23:07:47 +0200502 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
503
Harald Welte694df832018-10-03 22:47:52 +0200504 if (!worker->reader.name) {
505 /* resolve PC/SC reader name from slot_id -> name map */
506 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
507 if (!worker->reader.name) {
508 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
509 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100510 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200511 }
512 }
Harald Welte45c948c2018-09-23 19:26:52 +0200513 OSMO_ASSERT(worker->reader.name);
514
Harald Welte297d72e2019-03-28 18:42:35 +0100515 rc = worker->ops->open_card(worker);
516 if (rc < 0)
517 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100518
Harald Welte57593f02018-09-23 19:30:31 +0200519 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200520 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200521
Harald Welteaf614732018-08-17 22:10:05 +0200522 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200523}
Harald Welte77911b02018-08-14 23:47:30 +0200524
525
Harald Welte00a96732019-03-11 17:18:02 +0100526static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200527{
528 struct ipaccess_head *hh;
529 uint16_t len;
530 int needed, rc;
531
532 if (buf_size < sizeof(*hh))
533 return -1;
534
535 hh = (struct ipaccess_head *) buf;
536
Harald Welte00a96732019-03-11 17:18:02 +0100537 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
538 * in case of a signal being received */
539
540restart_hdr:
541 /* 1) blocking recv from the socket (IPA header) */
542 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
543 if (rc == -1 && errno == EINTR) {
544 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
545 return -23;
546 goto restart_hdr;
547 } else if (rc < 0)
548 return rc;
549 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200550 return -2;
551
552 len = ntohs(hh->len);
553 needed = len; //- sizeof(*hh);
554
Harald Welte00a96732019-03-11 17:18:02 +0100555restart_body:
556 /* 2) blocking recv from the socket (payload) */
557 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
558 if (rc == -1 && errno == EINTR) {
559 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
560 return -23;
561 goto restart_body;
562 } else if (rc < 0)
563 return rc;
564 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200565 return -3;
566
567 return len;
568}
569
Harald Welte796a7492018-09-23 19:31:28 +0200570static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
571{
572 struct msgb *msg = rspro_enc_msg(pdu);
573 int rc;
574
575 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100576 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200577 LOGW(worker, "error encoding RSPRO\n");
578 return -1;
579 }
580
Harald Weltefd471192018-09-24 14:51:14 +0200581 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200582 /* prepend the header */
583 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200584 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200585
586 /* actually send it through the socket */
587 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
588 if (rc == msgb_length(msg))
589 rc = 0;
590 else {
591 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
592 rc = -1;
593 }
594
595 msgb_free(msg);
596
597 return rc;
598}
599
Harald Welte150d6d62018-10-03 23:07:47 +0200600/* attempt to obtain slot-map */
601static int worker_try_slotmap(struct bankd_worker *worker)
602{
Harald Weltecbd18962019-03-03 19:02:38 +0100603 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200604
Harald Weltecbd18962019-03-03 19:02:38 +0100605 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200606 if (!slmap) {
607 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
608 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
609 /* check in 10s if the map has been installed meanwhile by main thread */
610 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
611 return -1;
612 } else {
613 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
614 slmap->client.client_id, slmap->client.slot_nr,
615 slmap->bank.bank_id, slmap->bank.slot_nr);
616 worker->slot = slmap->bank;
617 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
618 return worker_open_card(worker);
619 }
620}
621
Harald Welte1f699b42019-03-28 13:41:08 +0100622/* inform the remote end (client) about the (new) ATR */
623static int worker_send_atr(struct bankd_worker *worker)
624{
625 RsproPDU_t *set_atr;
626 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
627 worker->client.clslot.slot_nr,
628 worker->card.atr, worker->card.atr_len);
629 if (!set_atr)
630 return -1;
631 return worker_send_rspro(worker, set_atr);
632}
Harald Welte150d6d62018-10-03 23:07:47 +0200633
Harald Weltecce2aad2018-08-16 14:44:37 +0200634static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
635{
Harald Welteaf614732018-08-17 22:10:05 +0200636 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100637 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200638 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100639 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200640
Harald Weltecce2aad2018-08-16 14:44:37 +0200641 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
642
Harald Weltecce2aad2018-08-16 14:44:37 +0200643 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
644 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
645 /* FIXME: store somewhere? */
646
647 if (worker->state != BW_ST_CONN_WAIT_ID) {
648 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100649 rc = -102;
650 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200651 }
652
Harald Welte371d0262018-08-16 15:23:58 +0200653 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200654 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100655 res = ResultCode_illegalClientId;
656 rc = -103;
657 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200658 }
Harald Welte371d0262018-08-16 15:23:58 +0200659 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
660 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200661 worker_set_state(worker, BW_ST_CONN_CLIENT);
662
Harald Welte150d6d62018-10-03 23:07:47 +0200663 if (worker_try_slotmap(worker) >= 0)
664 res = ResultCode_ok;
665 else
Harald Welte3e689872018-09-24 14:52:56 +0200666 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200667
Harald Welte3e689872018-09-24 14:52:56 +0200668 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100669 rc = worker_send_rspro(worker, resp);
670 if (rc < 0)
671 return rc;
672
673 if (res == ResultCode_ok)
674 rc = worker_send_atr(worker);
675
676 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100677
678respond_and_err:
679 if (res) {
680 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
681 worker_send_rspro(worker, resp);
682 }
683 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200684}
685
Harald Welte796a7492018-09-23 19:31:28 +0200686static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
687{
688 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200689 uint8_t rx_buf[1024];
690 DWORD rx_buf_len = sizeof(rx_buf);
691 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100692 struct client_slot clslot;
693 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100694 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200695
696 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
697
698 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
699 LOGW(worker, "Unexpected tpduModemToCaard\n");
700 return -104;
701 }
702
Harald Weltee1d32892019-03-27 20:47:42 +0100703 /* Validate that toBankSlot / fromClientSlot match our expectations */
704 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
705 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
706 if (!bank_slot_equals(&worker->slot, &bslot)) {
707 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
708 bslot.bank_id, bslot.slot_nr);
709 return -105;
710 }
711 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
712 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
713 clslot.client_id, clslot.slot_nr);
714 return -106;
715 }
Harald Welte796a7492018-09-23 19:31:28 +0200716
Harald Welte297d72e2019-03-28 18:42:35 +0100717 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
718 rx_buf, &rx_buf_len);
719 if (rc < 0)
720 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200721
722 /* encode response PDU and send it */
723 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
724 rx_buf, rx_buf_len);
725 worker_send_rspro(worker, pdu_resp);
726
727 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200728}
729
Harald Weltebbd18bd2019-12-16 13:11:50 +0100730static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu)
731{
732 const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd;
733 const struct SlotPhysStatus *sps = &cssi->slotPhysStatus;
734 int rc = 0;
735
736 LOGW(worker, "clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n",
737 sps->resetActive ? "ACTIVE" : "INACTIVE",
738 sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL",
739 sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL");
740
741 /* perform cold or warm reset */
742 if (sps->vccPresent && *sps->vccPresent == 0)
743 rc = worker->ops->reset_card(worker, true);
744 else if (sps->resetActive)
745 rc = worker->ops->reset_card(worker, false);
746
747 return rc;
748}
749
Harald Welte77911b02018-08-14 23:47:30 +0200750/* handle one incoming RSPRO message from a client inside a worker thread */
751static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
752{
Harald Weltecce2aad2018-08-16 14:44:37 +0200753 int rc = -100;
754
Harald Welte736e8312019-03-31 13:08:16 +0200755 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
756
Harald Welte77911b02018-08-14 23:47:30 +0200757 switch (pdu->msg.present) {
758 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200759 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200760 break;
761 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200762 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200763 break;
764 case RsproPDUchoice_PR_clientSlotStatusInd:
Harald Weltebbd18bd2019-12-16 13:11:50 +0100765 rc = worker_handle_clientSlotStatusInd(worker, pdu);
Harald Welte24a3e322019-03-31 13:08:30 +0200766 rc = 0;
767 break;
768 case RsproPDUchoice_PR_setAtrRes:
769 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200770 break;
771 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200772 rc = -101;
773 break;
Harald Welte77911b02018-08-14 23:47:30 +0200774 }
775
Harald Weltecce2aad2018-08-16 14:44:37 +0200776 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200777}
778
Harald Welte694df832018-10-03 22:47:52 +0200779static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
780{
781 struct timeval tout = { timeout_secs, 0 };
782 fd_set readset;
783
784 FD_ZERO(&readset);
785 FD_SET(fd, &readset);
786 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
787}
788
Harald Welte77911b02018-08-14 23:47:30 +0200789/* body of the main transceive loop */
790static int worker_transceive_loop(struct bankd_worker *worker)
791{
792 struct ipaccess_head *hh;
793 struct ipaccess_head_ext *hh_ext;
794 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
795 asn_dec_rval_t rval;
796 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200797 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200798
Harald Welte00a96732019-03-11 17:18:02 +0100799restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200800 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100801 if (rc == -1 && errno == EINTR) {
802 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
803 return -23;
Harald Welte0fd77a52019-12-05 22:05:39 +0100804 else
805 worker_try_slotmap(worker);
Harald Welte00a96732019-03-11 17:18:02 +0100806 goto restart_wait;
807 } else if (rc < 0)
808 return rc;
809 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200810 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200811 switch (worker->state) {
812 case BW_ST_CONN_CLIENT_WAIT_MAP:
813 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100814 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200815 break;
816 case BW_ST_CONN_CLIENT_MAPPED:
817 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100818 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200819 break;
820 default:
821 OSMO_ASSERT(0);
822 }
Harald Welte1f699b42019-03-28 13:41:08 +0100823 if (rc == 0)
824 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200825 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200826 return 0;
827 };
828
Harald Welte77911b02018-08-14 23:47:30 +0200829 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100830 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200831 if (rc < 0)
832 return rc;
833 data_len = rc;
834
835 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200836 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200837 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200838 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200839 }
Harald Welte77911b02018-08-14 23:47:30 +0200840
Harald Welte5a3613a2018-10-11 12:56:21 +0200841 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100842 switch (hh->data[0]) {
843 case IPAC_MSGT_PING:
844 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100845 case IPAC_MSGT_ID_ACK:
846 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100847 default:
848 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
849 break;
850 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200851 return 0;
852 }
853
Harald Welte77911b02018-08-14 23:47:30 +0200854 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200855 if (data_len < sizeof(*hh_ext)) {
856 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200857 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200858 }
Harald Welte77911b02018-08-14 23:47:30 +0200859 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200860 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
861 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200862 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200863 }
Harald Welte77911b02018-08-14 23:47:30 +0200864
865 /* 2) ASN1 BER decode of the message */
866 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200867 if (rval.code != RC_OK) {
868 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200869 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200870 }
Harald Welte77911b02018-08-14 23:47:30 +0200871
872 /* 3) handling of the message, possibly resulting in PCSC commands */
873 rc = worker_handle_rspro(worker, pdu);
874 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200875 if (rc < 0) {
876 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200877 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200878 }
Harald Welte77911b02018-08-14 23:47:30 +0200879
880 /* everything OK if we reach here */
881 return 0;
882}
883
Harald Welted6dfb8c2018-08-16 14:46:53 +0200884/* obtain an ascii representation of the client IP/port */
885static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
886{
887 char hostbuf[32], portbuf[32];
888 int rc;
889
890 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
891 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
892 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
893 if (rc != 0) {
894 out[0] = '\0';
895 return -1;
896 }
897 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
898 return 0;
899}
900
Harald Welte77911b02018-08-14 23:47:30 +0200901/* worker thread main function */
902static void *worker_main(void *arg)
903{
Harald Welte77911b02018-08-14 23:47:30 +0200904 void *top_ctx;
905 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200906
Harald Welte00a96732019-03-11 17:18:02 +0100907 g_worker = (struct bankd_worker *) arg;
908
Harald Welte00a96732019-03-11 17:18:02 +0100909 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200910
Harald Welte77911b02018-08-14 23:47:30 +0200911 /* not permitted in multithreaded environment */
912 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100913 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
914 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200915
Harald Welte286a2be2019-03-11 17:36:58 +0100916 /* set the thread name */
917 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
918 pthread_setname_np(pthread_self(), g_worker->name);
919
Harald Welte77911b02018-08-14 23:47:30 +0200920 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100921 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200922
Harald Welte602b6f72019-12-04 21:51:02 +0100923 g_worker->slot.bank_id = 0xffff;
924 g_worker->slot.slot_nr = 0xffff;
925
Harald Welte77911b02018-08-14 23:47:30 +0200926 /* we continuously perform the same loop here, recycling the worker thread
927 * once the client connection is gone or we have some trouble with the card/reader */
928 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200929 char buf[128];
930
Harald Welte00a96732019-03-11 17:18:02 +0100931 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200932
Harald Welte00a96732019-03-11 17:18:02 +0100933 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200934 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100935 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
936 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200937 if (rc < 0) {
938 continue;
939 }
Harald Welte00a96732019-03-11 17:18:02 +0100940 g_worker->client.fd = rc;
941 worker_client_addrstr(buf, sizeof(buf), g_worker);
942 LOGW(g_worker, "Accepted connection from %s\n", buf);
943 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200944
945 /* run the main worker transceive loop body until there was some error */
946 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100947 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200948 if (rc < 0)
949 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100950 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
951 break;
Harald Welte77911b02018-08-14 23:47:30 +0200952 }
953
Harald Welte00a96732019-03-11 17:18:02 +0100954 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200955
Harald Welte77911b02018-08-14 23:47:30 +0200956 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100957 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100958 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100959 if (g_worker->reader.name)
960 g_worker->reader.name = NULL;
961 if (g_worker->client.fd >= 0)
962 close(g_worker->client.fd);
963 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
964 g_worker->client.fd = -1;
965 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200966 }
967
968 pthread_cleanup_pop(1);
969 talloc_free(top_ctx);
970 pthread_exit(NULL);
971}