blob: 29a13edc6b966a44efeab8cb5f500c7bce16835f [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 Welte25075972019-03-11 17:33:17 +010056
57static void handle_sig_usr1(int sig);
Harald Welte00a96732019-03-11 17:18:02 +010058static void handle_sig_mapdel(int sig);
59
Harald Welte77911b02018-08-14 23:47:30 +020060__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010061struct bankd *g_bankd;
Harald Welte25075972019-03-11 17:33:17 +010062static void *g_tall_ctx;
Harald Welte77911b02018-08-14 23:47:30 +020063
64static void *worker_main(void *arg);
65
66/***********************************************************************
67* bankd core / main thread
68***********************************************************************/
69
Harald Welte43ab79f2018-10-03 23:34:21 +020070int asn_debug;
71
Harald Welte77911b02018-08-14 23:47:30 +020072static void bankd_init(struct bankd *bankd)
73{
Harald Welte25075972019-03-11 17:33:17 +010074 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Weltef94b9ee2018-09-25 15:04:21 +020075 osmo_init_logging2(g_tall_ctx, &log_info);
76
Harald Welte43ab79f2018-10-03 23:34:21 +020077 asn_debug = 0;
78
Harald Welte77911b02018-08-14 23:47:30 +020079 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010080 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020081 INIT_LLIST_HEAD(&bankd->workers);
82 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020083
Harald Weltea0f39502019-03-09 20:59:34 +010084 /* set some defaults, overridden by commandline/config */
Harald Welte2513d812019-04-01 21:03:02 +020085 bankd->srvc.bankd.bank_id = 1;
86 bankd->srvc.bankd.num_slots = 8;
Harald Weltea0f39502019-03-09 20:59:34 +010087
Harald Weltef1dd1622018-09-24 14:54:23 +020088 bankd->comp_id.type = ComponentType_remsimBankd;
89 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
90 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
91 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
92 /* FIXME: other members of app_comp_id */
93
Harald Welte45c948c2018-09-23 19:26:52 +020094 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
Harald Welte77911b02018-08-14 23:47:30 +020095}
96
97/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020098static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020099{
100 struct bankd_worker *worker;
101 int rc;
102
103 worker = talloc_zero(bankd, struct bankd_worker);
104 if (!worker)
105 return NULL;
106
107 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200108 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100109 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200110
111 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
112
113 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
114 if (rc != 0) {
115 talloc_free(worker);
116 return NULL;
117 }
118
119 pthread_mutex_lock(&bankd->workers_mutex);
120 llist_add_tail(&worker->list, &bankd->workers);
121 pthread_mutex_unlock(&bankd->workers_mutex);
122
123 return worker;
124}
125
126static bool terminate = false;
127
Harald Weltea4967832019-12-05 22:04:57 +0100128/* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */
129static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig)
130{
131 struct bankd_worker *worker;
132 pthread_mutex_lock(&g_bankd->workers_mutex);
133 llist_for_each_entry(worker, &g_bankd->workers, list) {
134 if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr))
135 continue;
136 if (cs && (cs->client_id != worker->client.clslot.client_id ||
137 cs->slot_nr != worker->client.clslot.slot_nr))
138 continue;
139
140 pthread_kill(worker->thread, sig);
141 break;
142 }
143 pthread_mutex_unlock(&g_bankd->workers_mutex);
144}
145
Harald Weltec650a4d2019-12-04 15:02:27 +0100146/* Remove a mapping */
147static void bankd_srvc_remove_mapping(struct slot_mapping *map)
148{
149 struct bank_slot bs = map->bank;
150
151 slotmap_del(g_bankd->slotmaps, map);
152
153 /* kill/reset the respective worker, if any! */
Harald Weltea4967832019-12-05 22:04:57 +0100154 send_signal_to_worker(&bs, NULL, SIGMAPDEL);
Harald Weltec650a4d2019-12-04 15:02:27 +0100155}
156
Harald Welte707c85a2019-03-09 12:56:35 +0100157/* handle incoming messages from server */
158static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
159{
Harald Welte454f5e22019-03-09 21:38:34 +0100160 const CreateMappingReq_t *creq = NULL;
161 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100162 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100163 struct slot_mapping *map;
164 struct bank_slot bs;
165 struct client_slot cs;
166 RsproPDU_t *resp;
167
168 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100169
170 switch (pdu->msg.present) {
171 case RsproPDUchoice_PR_connectBankRes:
172 /* Store 'identity' of server in srvc->peer_comp_id */
173 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
174 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
175 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100176 case RsproPDUchoice_PR_createMappingReq:
177 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200178 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100179 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200180 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100181 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200182 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100183 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200184 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100185 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100186 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100187 rspro2bank_slot(&bs, &creq->bank);
188 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100189 /* check if map exists */
190 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
191 if (map) {
192 if (client_slot_equals(&map->client, &cs)) {
193 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
194 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
195 goto send_resp;
196 } else {
197 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
198 bankd_srvc_remove_mapping(map);
199 }
200 }
Harald Welte454f5e22019-03-09 21:38:34 +0100201 /* Add a new mapping */
202 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100203 if (!map) {
204 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100205 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100206 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100207 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
208 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100209send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100210 server_conn_send_rspro(srvc, resp);
211 break;
212 case RsproPDUchoice_PR_removeMappingReq:
213 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200214 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100215 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100216 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100217 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200218 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100219 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100220 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100221 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100222 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100223 rspro2bank_slot(&bs, &rreq->bank);
224 /* Remove a mapping */
225 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100226 if (!map) {
227 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100228 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100229 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100230 rspro2client_slot(&cs, &rreq->client);
231 if (!client_slot_equals(&map->client, &cs)) {
232 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
233 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
234 } else {
235 LOGPFSM(srvc->fi, "removing slotmap\n");
236 bankd_srvc_remove_mapping(map);
237 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
238 }
Harald Welte454f5e22019-03-09 21:38:34 +0100239 }
240 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100241 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100242 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100243 case RsproPDUchoice_PR_resetStateReq:
244 /* delete all slotmaps */
245 slotmap_del_all(g_bankd->slotmaps);
246 /* notify all workers about maps having disappeared */
247 pthread_mutex_lock(&g_bankd->workers_mutex);
248 llist_for_each_entry(worker, &g_bankd->workers, list) {
249 pthread_kill(worker->thread, SIGMAPDEL);
250 }
251 pthread_mutex_unlock(&g_bankd->workers_mutex);
252 /* send response to server */
253 resp = rspro_gen_ResetStateRes(ResultCode_ok);
254 server_conn_send_rspro(srvc, resp);
255 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100256 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100257 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
258 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100259 return -1;
260 }
261
262 return 0;
263}
264
Harald Welte5bae20b2019-04-01 09:36:42 +0200265static void printf_help()
266{
267 printf(
268" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100269" -V --version Print the version of the program\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200270" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
271" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
272" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200273" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200274" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
275" connections (default: INADDR_ANY)\n"
276" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
277" connectionss (default: 9999)\n"
278 );
279}
280
281static int g_bind_port = 9999;
282static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100283
Harald Welte707c85a2019-03-09 12:56:35 +0100284void handle_options(int argc, char **argv)
285{
Harald Welte5bae20b2019-04-01 09:36:42 +0200286 while (1) {
287 int option_index = 0, c;
288 static const struct option long_options[] = {
289 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100290 { "version", 0, 0, 'V' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200291 { "server-host", 1, 0, 'i' },
292 { "server-port", 1, 0, 'p' },
293 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200294 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200295 { "component-name", 1, 0, 'N' },
296 { "bind-ip", 1, 0, 'I' },
297 { "bind-port", 1, 0, 'P' },
298 { 0, 0, 0, 0 }
299 };
300
Harald Weltecd7fcd72019-12-03 21:29:07 +0100301 c = getopt_long(argc, argv, "hVi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200302 if (c == -1)
303 break;
304
305 switch (c) {
306 case 'h':
307 printf_help();
308 exit(0);
309 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100310 case 'V':
311 printf("osmo-remsim-bankd version %s\n", VERSION);
312 exit(0);
313 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200314 case 'i':
315 g_bankd->srvc.server_host = optarg;
316 break;
317 case 'p':
318 g_bankd->srvc.server_port = atoi(optarg);
319 break;
320 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200321 g_bankd->srvc.bankd.bank_id = atoi(optarg);
322 break;
323 case 'n':
324 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200325 break;
326 case 'N':
327 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
328 break;
329 case 'I':
330 g_bind_ip = optarg;
331 break;
332 case 'P':
333 g_bind_port = atoi(optarg);
334 break;
335 }
336 }
Harald Welte707c85a2019-03-09 12:56:35 +0100337}
338
Harald Welte77911b02018-08-14 23:47:30 +0200339int main(int argc, char **argv)
340{
Harald Weltef4b16f12019-03-09 20:58:17 +0100341 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200342 int i, rc;
343
Harald Weltef4b16f12019-03-09 20:58:17 +0100344 g_bankd = talloc_zero(NULL, struct bankd);
345 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200346
Harald Weltef4b16f12019-03-09 20:58:17 +0100347 bankd_init(g_bankd);
348
349 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100350 srvc->server_host = "localhost";
351 srvc->server_port = 9998;
352 srvc->handle_rx = bankd_srvc_handle_rx;
353 srvc->own_comp_id.type = ComponentType_remsimBankd;
354 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
355 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
356 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
357
358 handle_options(argc, argv);
359
Harald Welte25075972019-03-11 17:33:17 +0100360 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100361 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100362 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100363
Harald Weltee89fecd2019-04-04 09:23:13 +0200364 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
365 * read once during bankd initialization, when the worker threads haven't
366 * started yet */
367 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
368
Harald Welte707c85a2019-03-09 12:56:35 +0100369 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100370 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100371 if (rc < 0) {
372 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
373 exit(1);
374 }
Harald Welted2192e22019-11-07 18:10:57 +0100375 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100376
377 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200378 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 +0200379 if (rc < 0)
380 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100381 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200382
Harald Weltea0f39502019-03-09 20:59:34 +0100383 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200384 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200385 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100386 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200387 if (!w)
388 exit(21);
389 }
390
391 while (1) {
392 if (terminate)
393 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100394 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200395 }
396
Harald Weltef4b16f12019-03-09 20:58:17 +0100397 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200398 exit(0);
399}
400
401
402
403/***********************************************************************
404 * bankd worker thread
405 ***********************************************************************/
406
Harald Welte00a96732019-03-11 17:18:02 +0100407static __thread struct bankd_worker *g_worker;
408
Harald Welte8d858292018-08-15 23:36:46 +0200409struct value_string worker_state_names[] = {
410 { BW_ST_INIT, "INIT" },
411 { BW_ST_ACCEPTING, "ACCEPTING" },
412 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
413 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200414 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200415 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
416 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100417 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200418 { 0, NULL }
419};
420
Harald Welte1f699b42019-03-28 13:41:08 +0100421static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
422
Harald Welte8d858292018-08-15 23:36:46 +0200423static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
424{
425 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
426 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200427 worker->timeout = 0;
428}
429
430static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
431 unsigned int timeout_secs)
432{
433 LOGW(worker, "Changing state to %s (timeout=%u)\n",
434 get_value_string(worker_state_names, new_state), timeout_secs);
435 worker->state = new_state;
436 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200437}
Harald Welte77911b02018-08-14 23:47:30 +0200438
Harald Welte00a96732019-03-11 17:18:02 +0100439/* signal handler for receiving SIGMAPDEL from main thread */
440static void handle_sig_mapdel(int sig)
441{
442 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
443 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100444 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
445 g_worker->slot.bank_id = 0xffff;
446 g_worker->slot.slot_nr = 0xffff;
447 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
448 }
Harald Welte00a96732019-03-11 17:18:02 +0100449}
450
Harald Welte25075972019-03-11 17:33:17 +0100451static void handle_sig_usr1(int sig)
452{
453 OSMO_ASSERT(sig == SIGUSR1);
454
455 if (pthread_equal(g_bankd->main, pthread_self())) {
456 struct bankd_worker *worker;
457 /* main thread */
458 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200459 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100460
461 /* iterate over worker threads and ask them to dump their talloc state */
462 pthread_mutex_lock(&g_bankd->workers_mutex);
463 llist_for_each_entry(worker, &g_bankd->workers, list) {
464 pthread_kill(worker->thread, SIGUSR1);
465 }
466 pthread_mutex_unlock(&g_bankd->workers_mutex);
467 } else {
468 /* worker thread */
469 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200470 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100471 }
472}
473
Harald Welte77911b02018-08-14 23:47:30 +0200474static void worker_cleanup(void *arg)
475{
476 struct bankd_worker *worker = (struct bankd_worker *) arg;
477 struct bankd *bankd = worker->bankd;
478
479 /* FIXME: should we still do this? in the thread ?!? */
480 pthread_mutex_lock(&bankd->workers_mutex);
481 llist_del(&worker->list);
482 talloc_free(worker); /* FIXME: is this safe? */
483 pthread_mutex_unlock(&bankd->workers_mutex);
484}
485
Harald Welteaf614732018-08-17 22:10:05 +0200486static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200487{
Harald Welte297d72e2019-03-28 18:42:35 +0100488 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200489
Harald Welte150d6d62018-10-03 23:07:47 +0200490 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
491
Harald Welte694df832018-10-03 22:47:52 +0200492 if (!worker->reader.name) {
493 /* resolve PC/SC reader name from slot_id -> name map */
494 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
495 if (!worker->reader.name) {
496 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
497 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100498 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200499 }
500 }
Harald Welte45c948c2018-09-23 19:26:52 +0200501 OSMO_ASSERT(worker->reader.name);
502
Harald Welte297d72e2019-03-28 18:42:35 +0100503 rc = worker->ops->open_card(worker);
504 if (rc < 0)
505 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100506
Harald Welte57593f02018-09-23 19:30:31 +0200507 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200508 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200509
Harald Welteaf614732018-08-17 22:10:05 +0200510 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200511}
Harald Welte77911b02018-08-14 23:47:30 +0200512
513
Harald Welte00a96732019-03-11 17:18:02 +0100514static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200515{
516 struct ipaccess_head *hh;
517 uint16_t len;
518 int needed, rc;
519
520 if (buf_size < sizeof(*hh))
521 return -1;
522
523 hh = (struct ipaccess_head *) buf;
524
Harald Welte00a96732019-03-11 17:18:02 +0100525 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
526 * in case of a signal being received */
527
528restart_hdr:
529 /* 1) blocking recv from the socket (IPA header) */
530 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
531 if (rc == -1 && errno == EINTR) {
532 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
533 return -23;
534 goto restart_hdr;
535 } else if (rc < 0)
536 return rc;
537 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200538 return -2;
539
540 len = ntohs(hh->len);
541 needed = len; //- sizeof(*hh);
542
Harald Welte00a96732019-03-11 17:18:02 +0100543restart_body:
544 /* 2) blocking recv from the socket (payload) */
545 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
546 if (rc == -1 && errno == EINTR) {
547 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
548 return -23;
549 goto restart_body;
550 } else if (rc < 0)
551 return rc;
552 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200553 return -3;
554
555 return len;
556}
557
Harald Welte796a7492018-09-23 19:31:28 +0200558static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
559{
560 struct msgb *msg = rspro_enc_msg(pdu);
561 int rc;
562
563 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100564 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200565 LOGW(worker, "error encoding RSPRO\n");
566 return -1;
567 }
568
Harald Weltefd471192018-09-24 14:51:14 +0200569 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200570 /* prepend the header */
571 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200572 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200573
574 /* actually send it through the socket */
575 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
576 if (rc == msgb_length(msg))
577 rc = 0;
578 else {
579 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
580 rc = -1;
581 }
582
583 msgb_free(msg);
584
585 return rc;
586}
587
Harald Welte150d6d62018-10-03 23:07:47 +0200588/* attempt to obtain slot-map */
589static int worker_try_slotmap(struct bankd_worker *worker)
590{
Harald Weltecbd18962019-03-03 19:02:38 +0100591 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200592
Harald Weltecbd18962019-03-03 19:02:38 +0100593 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200594 if (!slmap) {
595 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
596 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
597 /* check in 10s if the map has been installed meanwhile by main thread */
598 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
599 return -1;
600 } else {
601 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
602 slmap->client.client_id, slmap->client.slot_nr,
603 slmap->bank.bank_id, slmap->bank.slot_nr);
604 worker->slot = slmap->bank;
605 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
606 return worker_open_card(worker);
607 }
608}
609
Harald Welte1f699b42019-03-28 13:41:08 +0100610/* inform the remote end (client) about the (new) ATR */
611static int worker_send_atr(struct bankd_worker *worker)
612{
613 RsproPDU_t *set_atr;
614 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
615 worker->client.clslot.slot_nr,
616 worker->card.atr, worker->card.atr_len);
617 if (!set_atr)
618 return -1;
619 return worker_send_rspro(worker, set_atr);
620}
Harald Welte150d6d62018-10-03 23:07:47 +0200621
Harald Weltecce2aad2018-08-16 14:44:37 +0200622static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
623{
Harald Welteaf614732018-08-17 22:10:05 +0200624 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100625 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200626 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100627 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200628
Harald Weltecce2aad2018-08-16 14:44:37 +0200629 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
630
Harald Weltecce2aad2018-08-16 14:44:37 +0200631 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
632 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
633 /* FIXME: store somewhere? */
634
635 if (worker->state != BW_ST_CONN_WAIT_ID) {
636 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100637 rc = -102;
638 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200639 }
640
Harald Welte371d0262018-08-16 15:23:58 +0200641 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200642 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100643 res = ResultCode_illegalClientId;
644 rc = -103;
645 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200646 }
Harald Welte371d0262018-08-16 15:23:58 +0200647 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
648 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200649 worker_set_state(worker, BW_ST_CONN_CLIENT);
650
Harald Welte150d6d62018-10-03 23:07:47 +0200651 if (worker_try_slotmap(worker) >= 0)
652 res = ResultCode_ok;
653 else
Harald Welte3e689872018-09-24 14:52:56 +0200654 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200655
Harald Welte3e689872018-09-24 14:52:56 +0200656 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100657 rc = worker_send_rspro(worker, resp);
658 if (rc < 0)
659 return rc;
660
661 if (res == ResultCode_ok)
662 rc = worker_send_atr(worker);
663
664 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100665
666respond_and_err:
667 if (res) {
668 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
669 worker_send_rspro(worker, resp);
670 }
671 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200672}
673
Harald Welte796a7492018-09-23 19:31:28 +0200674static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
675{
676 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200677 uint8_t rx_buf[1024];
678 DWORD rx_buf_len = sizeof(rx_buf);
679 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100680 struct client_slot clslot;
681 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100682 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200683
684 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
685
686 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
687 LOGW(worker, "Unexpected tpduModemToCaard\n");
688 return -104;
689 }
690
Harald Weltee1d32892019-03-27 20:47:42 +0100691 /* Validate that toBankSlot / fromClientSlot match our expectations */
692 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
693 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
694 if (!bank_slot_equals(&worker->slot, &bslot)) {
695 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
696 bslot.bank_id, bslot.slot_nr);
697 return -105;
698 }
699 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
700 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
701 clslot.client_id, clslot.slot_nr);
702 return -106;
703 }
Harald Welte796a7492018-09-23 19:31:28 +0200704
Harald Welte297d72e2019-03-28 18:42:35 +0100705 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
706 rx_buf, &rx_buf_len);
707 if (rc < 0)
708 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200709
710 /* encode response PDU and send it */
711 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
712 rx_buf, rx_buf_len);
713 worker_send_rspro(worker, pdu_resp);
714
715 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200716}
717
Harald Welte77911b02018-08-14 23:47:30 +0200718/* handle one incoming RSPRO message from a client inside a worker thread */
719static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
720{
Harald Weltecce2aad2018-08-16 14:44:37 +0200721 int rc = -100;
722
Harald Welte736e8312019-03-31 13:08:16 +0200723 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
724
Harald Welte77911b02018-08-14 23:47:30 +0200725 switch (pdu->msg.present) {
726 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200727 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200728 break;
729 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200730 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200731 break;
732 case RsproPDUchoice_PR_clientSlotStatusInd:
733 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200734 rc = 0;
735 break;
736 case RsproPDUchoice_PR_setAtrRes:
737 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200738 break;
739 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200740 rc = -101;
741 break;
Harald Welte77911b02018-08-14 23:47:30 +0200742 }
743
Harald Weltecce2aad2018-08-16 14:44:37 +0200744 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200745}
746
Harald Welte694df832018-10-03 22:47:52 +0200747static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
748{
749 struct timeval tout = { timeout_secs, 0 };
750 fd_set readset;
751
752 FD_ZERO(&readset);
753 FD_SET(fd, &readset);
754 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
755}
756
Harald Welte77911b02018-08-14 23:47:30 +0200757/* body of the main transceive loop */
758static int worker_transceive_loop(struct bankd_worker *worker)
759{
760 struct ipaccess_head *hh;
761 struct ipaccess_head_ext *hh_ext;
762 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
763 asn_dec_rval_t rval;
764 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200765 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200766
Harald Welte00a96732019-03-11 17:18:02 +0100767restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200768 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100769 if (rc == -1 && errno == EINTR) {
770 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
771 return -23;
772 goto restart_wait;
773 } else if (rc < 0)
774 return rc;
775 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200776 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200777 switch (worker->state) {
778 case BW_ST_CONN_CLIENT_WAIT_MAP:
779 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100780 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200781 break;
782 case BW_ST_CONN_CLIENT_MAPPED:
783 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100784 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200785 break;
786 default:
787 OSMO_ASSERT(0);
788 }
Harald Welte1f699b42019-03-28 13:41:08 +0100789 if (rc == 0)
790 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200791 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200792 return 0;
793 };
794
Harald Welte77911b02018-08-14 23:47:30 +0200795 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100796 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200797 if (rc < 0)
798 return rc;
799 data_len = rc;
800
801 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200802 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200803 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200804 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200805 }
Harald Welte77911b02018-08-14 23:47:30 +0200806
Harald Welte5a3613a2018-10-11 12:56:21 +0200807 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100808 switch (hh->data[0]) {
809 case IPAC_MSGT_PING:
810 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100811 case IPAC_MSGT_ID_ACK:
812 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100813 default:
814 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
815 break;
816 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200817 return 0;
818 }
819
Harald Welte77911b02018-08-14 23:47:30 +0200820 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200821 if (data_len < sizeof(*hh_ext)) {
822 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200823 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200824 }
Harald Welte77911b02018-08-14 23:47:30 +0200825 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200826 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
827 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200828 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200829 }
Harald Welte77911b02018-08-14 23:47:30 +0200830
831 /* 2) ASN1 BER decode of the message */
832 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200833 if (rval.code != RC_OK) {
834 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200835 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200836 }
Harald Welte77911b02018-08-14 23:47:30 +0200837
838 /* 3) handling of the message, possibly resulting in PCSC commands */
839 rc = worker_handle_rspro(worker, pdu);
840 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200841 if (rc < 0) {
842 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200843 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200844 }
Harald Welte77911b02018-08-14 23:47:30 +0200845
846 /* everything OK if we reach here */
847 return 0;
848}
849
Harald Welted6dfb8c2018-08-16 14:46:53 +0200850/* obtain an ascii representation of the client IP/port */
851static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
852{
853 char hostbuf[32], portbuf[32];
854 int rc;
855
856 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
857 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
858 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
859 if (rc != 0) {
860 out[0] = '\0';
861 return -1;
862 }
863 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
864 return 0;
865}
866
Harald Welte77911b02018-08-14 23:47:30 +0200867/* worker thread main function */
868static void *worker_main(void *arg)
869{
Harald Welte77911b02018-08-14 23:47:30 +0200870 void *top_ctx;
871 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200872
Harald Welte00a96732019-03-11 17:18:02 +0100873 g_worker = (struct bankd_worker *) arg;
874
Harald Welte00a96732019-03-11 17:18:02 +0100875 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200876
Harald Welte77911b02018-08-14 23:47:30 +0200877 /* not permitted in multithreaded environment */
878 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100879 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
880 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200881
Harald Welte286a2be2019-03-11 17:36:58 +0100882 /* set the thread name */
883 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
884 pthread_setname_np(pthread_self(), g_worker->name);
885
Harald Welte77911b02018-08-14 23:47:30 +0200886 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100887 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200888
Harald Welte602b6f72019-12-04 21:51:02 +0100889 g_worker->slot.bank_id = 0xffff;
890 g_worker->slot.slot_nr = 0xffff;
891
Harald Welte77911b02018-08-14 23:47:30 +0200892 /* we continuously perform the same loop here, recycling the worker thread
893 * once the client connection is gone or we have some trouble with the card/reader */
894 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200895 char buf[128];
896
Harald Welte00a96732019-03-11 17:18:02 +0100897 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200898
Harald Welte00a96732019-03-11 17:18:02 +0100899 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200900 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100901 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
902 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200903 if (rc < 0) {
904 continue;
905 }
Harald Welte00a96732019-03-11 17:18:02 +0100906 g_worker->client.fd = rc;
907 worker_client_addrstr(buf, sizeof(buf), g_worker);
908 LOGW(g_worker, "Accepted connection from %s\n", buf);
909 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200910
911 /* run the main worker transceive loop body until there was some error */
912 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100913 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200914 if (rc < 0)
915 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100916 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
917 break;
Harald Welte77911b02018-08-14 23:47:30 +0200918 }
919
Harald Welte00a96732019-03-11 17:18:02 +0100920 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200921
Harald Welte77911b02018-08-14 23:47:30 +0200922 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100923 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100924 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100925 if (g_worker->reader.name)
926 g_worker->reader.name = NULL;
927 if (g_worker->client.fd >= 0)
928 close(g_worker->client.fd);
929 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
930 g_worker->client.fd = -1;
931 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200932 }
933
934 pthread_cleanup_pop(1);
935 talloc_free(top_ctx);
936 pthread_exit(NULL);
937}