blob: 194baef5469f92ff5f58cb9ad444b513f815b3c3 [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 Weltec650a4d2019-12-04 15:02:27 +0100128/* Remove a mapping */
129static void bankd_srvc_remove_mapping(struct slot_mapping *map)
130{
131 struct bank_slot bs = map->bank;
132
133 slotmap_del(g_bankd->slotmaps, map);
134
135 /* kill/reset the respective worker, if any! */
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.bank_id == worker->slot.bank_id &&
140 bs.slot_nr == worker->slot.slot_nr) {
141 pthread_kill(worker->thread, SIGMAPDEL);
142 break;
143 }
144 }
145 pthread_mutex_unlock(&g_bankd->workers_mutex);
146}
147
Harald Welte707c85a2019-03-09 12:56:35 +0100148/* handle incoming messages from server */
149static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
150{
Harald Welte454f5e22019-03-09 21:38:34 +0100151 const CreateMappingReq_t *creq = NULL;
152 const RemoveMappingReq_t *rreq = NULL;
Harald Weltef34fb792019-12-04 19:51:32 +0100153 struct bankd_worker *worker;
Harald Welte454f5e22019-03-09 21:38:34 +0100154 struct slot_mapping *map;
155 struct bank_slot bs;
156 struct client_slot cs;
157 RsproPDU_t *resp;
158
159 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100160
161 switch (pdu->msg.present) {
162 case RsproPDUchoice_PR_connectBankRes:
163 /* Store 'identity' of server in srvc->peer_comp_id */
164 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
165 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
166 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100167 case RsproPDUchoice_PR_createMappingReq:
168 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200169 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100170 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200171 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100172 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200173 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100174 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200175 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100176 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100177 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100178 rspro2bank_slot(&bs, &creq->bank);
179 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100180 /* check if map exists */
181 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
182 if (map) {
183 if (client_slot_equals(&map->client, &cs)) {
184 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
185 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
186 goto send_resp;
187 } else {
188 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
189 bankd_srvc_remove_mapping(map);
190 }
191 }
Harald Welte454f5e22019-03-09 21:38:34 +0100192 /* Add a new mapping */
193 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100194 if (!map) {
195 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100196 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100197 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100198 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
199 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100200send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100201 server_conn_send_rspro(srvc, resp);
202 break;
203 case RsproPDUchoice_PR_removeMappingReq:
204 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200205 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100206 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100207 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100208 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200209 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100210 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100211 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100212 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100213 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100214 rspro2bank_slot(&bs, &rreq->bank);
215 /* Remove a mapping */
216 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100217 if (!map) {
218 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100219 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100220 } else {
Harald Welted9fb9392019-12-04 19:05:01 +0100221 rspro2client_slot(&cs, &rreq->client);
222 if (!client_slot_equals(&map->client, &cs)) {
223 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
224 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
225 } else {
226 LOGPFSM(srvc->fi, "removing slotmap\n");
227 bankd_srvc_remove_mapping(map);
228 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
229 }
Harald Welte454f5e22019-03-09 21:38:34 +0100230 }
231 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100232 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100233 break;
Harald Weltef34fb792019-12-04 19:51:32 +0100234 case RsproPDUchoice_PR_resetStateReq:
235 /* delete all slotmaps */
236 slotmap_del_all(g_bankd->slotmaps);
237 /* notify all workers about maps having disappeared */
238 pthread_mutex_lock(&g_bankd->workers_mutex);
239 llist_for_each_entry(worker, &g_bankd->workers, list) {
240 pthread_kill(worker->thread, SIGMAPDEL);
241 }
242 pthread_mutex_unlock(&g_bankd->workers_mutex);
243 /* send response to server */
244 resp = rspro_gen_ResetStateRes(ResultCode_ok);
245 server_conn_send_rspro(srvc, resp);
246 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100247 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100248 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
249 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100250 return -1;
251 }
252
253 return 0;
254}
255
Harald Welte5bae20b2019-04-01 09:36:42 +0200256static void printf_help()
257{
258 printf(
259" -h --help Print this help message\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +0100260" -V --version Print the version of the program\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200261" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
262" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
263" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200264" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200265" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
266" connections (default: INADDR_ANY)\n"
267" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
268" connectionss (default: 9999)\n"
269 );
270}
271
272static int g_bind_port = 9999;
273static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100274
Harald Welte707c85a2019-03-09 12:56:35 +0100275void handle_options(int argc, char **argv)
276{
Harald Welte5bae20b2019-04-01 09:36:42 +0200277 while (1) {
278 int option_index = 0, c;
279 static const struct option long_options[] = {
280 { "help", 0, 0, 'h' },
Harald Weltecd7fcd72019-12-03 21:29:07 +0100281 { "version", 0, 0, 'V' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200282 { "server-host", 1, 0, 'i' },
283 { "server-port", 1, 0, 'p' },
284 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200285 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200286 { "component-name", 1, 0, 'N' },
287 { "bind-ip", 1, 0, 'I' },
288 { "bind-port", 1, 0, 'P' },
289 { 0, 0, 0, 0 }
290 };
291
Harald Weltecd7fcd72019-12-03 21:29:07 +0100292 c = getopt_long(argc, argv, "hVi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200293 if (c == -1)
294 break;
295
296 switch (c) {
297 case 'h':
298 printf_help();
299 exit(0);
300 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +0100301 case 'V':
302 printf("osmo-remsim-bankd version %s\n", VERSION);
303 exit(0);
304 break;
Harald Welte5bae20b2019-04-01 09:36:42 +0200305 case 'i':
306 g_bankd->srvc.server_host = optarg;
307 break;
308 case 'p':
309 g_bankd->srvc.server_port = atoi(optarg);
310 break;
311 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200312 g_bankd->srvc.bankd.bank_id = atoi(optarg);
313 break;
314 case 'n':
315 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200316 break;
317 case 'N':
318 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
319 break;
320 case 'I':
321 g_bind_ip = optarg;
322 break;
323 case 'P':
324 g_bind_port = atoi(optarg);
325 break;
326 }
327 }
Harald Welte707c85a2019-03-09 12:56:35 +0100328}
329
Harald Welte77911b02018-08-14 23:47:30 +0200330int main(int argc, char **argv)
331{
Harald Weltef4b16f12019-03-09 20:58:17 +0100332 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200333 int i, rc;
334
Harald Weltef4b16f12019-03-09 20:58:17 +0100335 g_bankd = talloc_zero(NULL, struct bankd);
336 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200337
Harald Weltef4b16f12019-03-09 20:58:17 +0100338 bankd_init(g_bankd);
339
340 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100341 srvc->server_host = "localhost";
342 srvc->server_port = 9998;
343 srvc->handle_rx = bankd_srvc_handle_rx;
344 srvc->own_comp_id.type = ComponentType_remsimBankd;
345 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
346 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
347 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
348
349 handle_options(argc, argv);
350
Harald Welte25075972019-03-11 17:33:17 +0100351 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100352 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100353 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100354
Harald Weltee89fecd2019-04-04 09:23:13 +0200355 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
356 * read once during bankd initialization, when the worker threads haven't
357 * started yet */
358 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
359
Harald Welte707c85a2019-03-09 12:56:35 +0100360 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100361 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100362 if (rc < 0) {
363 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
364 exit(1);
365 }
Harald Welted2192e22019-11-07 18:10:57 +0100366 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
Harald Welte707c85a2019-03-09 12:56:35 +0100367
368 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200369 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 +0200370 if (rc < 0)
371 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100372 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200373
Harald Weltea0f39502019-03-09 20:59:34 +0100374 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200375 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200376 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100377 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200378 if (!w)
379 exit(21);
380 }
381
382 while (1) {
383 if (terminate)
384 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100385 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200386 }
387
Harald Weltef4b16f12019-03-09 20:58:17 +0100388 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200389 exit(0);
390}
391
392
393
394/***********************************************************************
395 * bankd worker thread
396 ***********************************************************************/
397
Harald Welte00a96732019-03-11 17:18:02 +0100398static __thread struct bankd_worker *g_worker;
399
Harald Welte8d858292018-08-15 23:36:46 +0200400struct value_string worker_state_names[] = {
401 { BW_ST_INIT, "INIT" },
402 { BW_ST_ACCEPTING, "ACCEPTING" },
403 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
404 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200405 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200406 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
407 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100408 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200409 { 0, NULL }
410};
411
Harald Welte1f699b42019-03-28 13:41:08 +0100412static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
413
Harald Welte8d858292018-08-15 23:36:46 +0200414static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
415{
416 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
417 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200418 worker->timeout = 0;
419}
420
421static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
422 unsigned int timeout_secs)
423{
424 LOGW(worker, "Changing state to %s (timeout=%u)\n",
425 get_value_string(worker_state_names, new_state), timeout_secs);
426 worker->state = new_state;
427 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200428}
Harald Welte77911b02018-08-14 23:47:30 +0200429
Harald Welte00a96732019-03-11 17:18:02 +0100430/* signal handler for receiving SIGMAPDEL from main thread */
431static void handle_sig_mapdel(int sig)
432{
433 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
434 OSMO_ASSERT(sig == SIGMAPDEL);
Harald Weltea8d945e2019-12-04 22:24:18 +0100435 if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) {
436 g_worker->slot.bank_id = 0xffff;
437 g_worker->slot.slot_nr = 0xffff;
438 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
439 }
Harald Welte00a96732019-03-11 17:18:02 +0100440}
441
Harald Welte25075972019-03-11 17:33:17 +0100442static void handle_sig_usr1(int sig)
443{
444 OSMO_ASSERT(sig == SIGUSR1);
445
446 if (pthread_equal(g_bankd->main, pthread_self())) {
447 struct bankd_worker *worker;
448 /* main thread */
449 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200450 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100451
452 /* iterate over worker threads and ask them to dump their talloc state */
453 pthread_mutex_lock(&g_bankd->workers_mutex);
454 llist_for_each_entry(worker, &g_bankd->workers, list) {
455 pthread_kill(worker->thread, SIGUSR1);
456 }
457 pthread_mutex_unlock(&g_bankd->workers_mutex);
458 } else {
459 /* worker thread */
460 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200461 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100462 }
463}
464
Harald Welte77911b02018-08-14 23:47:30 +0200465static void worker_cleanup(void *arg)
466{
467 struct bankd_worker *worker = (struct bankd_worker *) arg;
468 struct bankd *bankd = worker->bankd;
469
470 /* FIXME: should we still do this? in the thread ?!? */
471 pthread_mutex_lock(&bankd->workers_mutex);
472 llist_del(&worker->list);
473 talloc_free(worker); /* FIXME: is this safe? */
474 pthread_mutex_unlock(&bankd->workers_mutex);
475}
476
Harald Welteaf614732018-08-17 22:10:05 +0200477static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200478{
Harald Welte297d72e2019-03-28 18:42:35 +0100479 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200480
Harald Welte150d6d62018-10-03 23:07:47 +0200481 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
482
Harald Welte694df832018-10-03 22:47:52 +0200483 if (!worker->reader.name) {
484 /* resolve PC/SC reader name from slot_id -> name map */
485 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
486 if (!worker->reader.name) {
487 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
488 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100489 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200490 }
491 }
Harald Welte45c948c2018-09-23 19:26:52 +0200492 OSMO_ASSERT(worker->reader.name);
493
Harald Welte297d72e2019-03-28 18:42:35 +0100494 rc = worker->ops->open_card(worker);
495 if (rc < 0)
496 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100497
Harald Welte57593f02018-09-23 19:30:31 +0200498 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200499 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200500
Harald Welteaf614732018-08-17 22:10:05 +0200501 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200502}
Harald Welte77911b02018-08-14 23:47:30 +0200503
504
Harald Welte00a96732019-03-11 17:18:02 +0100505static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200506{
507 struct ipaccess_head *hh;
508 uint16_t len;
509 int needed, rc;
510
511 if (buf_size < sizeof(*hh))
512 return -1;
513
514 hh = (struct ipaccess_head *) buf;
515
Harald Welte00a96732019-03-11 17:18:02 +0100516 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
517 * in case of a signal being received */
518
519restart_hdr:
520 /* 1) blocking recv from the socket (IPA header) */
521 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
522 if (rc == -1 && errno == EINTR) {
523 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
524 return -23;
525 goto restart_hdr;
526 } else if (rc < 0)
527 return rc;
528 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200529 return -2;
530
531 len = ntohs(hh->len);
532 needed = len; //- sizeof(*hh);
533
Harald Welte00a96732019-03-11 17:18:02 +0100534restart_body:
535 /* 2) blocking recv from the socket (payload) */
536 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
537 if (rc == -1 && errno == EINTR) {
538 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
539 return -23;
540 goto restart_body;
541 } else if (rc < 0)
542 return rc;
543 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200544 return -3;
545
546 return len;
547}
548
Harald Welte796a7492018-09-23 19:31:28 +0200549static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
550{
551 struct msgb *msg = rspro_enc_msg(pdu);
552 int rc;
553
554 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100555 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200556 LOGW(worker, "error encoding RSPRO\n");
557 return -1;
558 }
559
Harald Weltefd471192018-09-24 14:51:14 +0200560 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200561 /* prepend the header */
562 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200563 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200564
565 /* actually send it through the socket */
566 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
567 if (rc == msgb_length(msg))
568 rc = 0;
569 else {
570 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
571 rc = -1;
572 }
573
574 msgb_free(msg);
575
576 return rc;
577}
578
Harald Welte150d6d62018-10-03 23:07:47 +0200579/* attempt to obtain slot-map */
580static int worker_try_slotmap(struct bankd_worker *worker)
581{
Harald Weltecbd18962019-03-03 19:02:38 +0100582 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200583
Harald Weltecbd18962019-03-03 19:02:38 +0100584 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200585 if (!slmap) {
586 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
587 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
588 /* check in 10s if the map has been installed meanwhile by main thread */
589 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
590 return -1;
591 } else {
592 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
593 slmap->client.client_id, slmap->client.slot_nr,
594 slmap->bank.bank_id, slmap->bank.slot_nr);
595 worker->slot = slmap->bank;
596 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
597 return worker_open_card(worker);
598 }
599}
600
Harald Welte1f699b42019-03-28 13:41:08 +0100601/* inform the remote end (client) about the (new) ATR */
602static int worker_send_atr(struct bankd_worker *worker)
603{
604 RsproPDU_t *set_atr;
605 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
606 worker->client.clslot.slot_nr,
607 worker->card.atr, worker->card.atr_len);
608 if (!set_atr)
609 return -1;
610 return worker_send_rspro(worker, set_atr);
611}
Harald Welte150d6d62018-10-03 23:07:47 +0200612
Harald Weltecce2aad2018-08-16 14:44:37 +0200613static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
614{
Harald Welteaf614732018-08-17 22:10:05 +0200615 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100616 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200617 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100618 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200619
Harald Weltecce2aad2018-08-16 14:44:37 +0200620 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
621
Harald Weltecce2aad2018-08-16 14:44:37 +0200622 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
623 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
624 /* FIXME: store somewhere? */
625
626 if (worker->state != BW_ST_CONN_WAIT_ID) {
627 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100628 rc = -102;
629 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200630 }
631
Harald Welte371d0262018-08-16 15:23:58 +0200632 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200633 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100634 res = ResultCode_illegalClientId;
635 rc = -103;
636 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200637 }
Harald Welte371d0262018-08-16 15:23:58 +0200638 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
639 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200640 worker_set_state(worker, BW_ST_CONN_CLIENT);
641
Harald Welte150d6d62018-10-03 23:07:47 +0200642 if (worker_try_slotmap(worker) >= 0)
643 res = ResultCode_ok;
644 else
Harald Welte3e689872018-09-24 14:52:56 +0200645 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200646
Harald Welte3e689872018-09-24 14:52:56 +0200647 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100648 rc = worker_send_rspro(worker, resp);
649 if (rc < 0)
650 return rc;
651
652 if (res == ResultCode_ok)
653 rc = worker_send_atr(worker);
654
655 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100656
657respond_and_err:
658 if (res) {
659 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
660 worker_send_rspro(worker, resp);
661 }
662 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200663}
664
Harald Welte796a7492018-09-23 19:31:28 +0200665static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
666{
667 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200668 uint8_t rx_buf[1024];
669 DWORD rx_buf_len = sizeof(rx_buf);
670 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100671 struct client_slot clslot;
672 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100673 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200674
675 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
676
677 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
678 LOGW(worker, "Unexpected tpduModemToCaard\n");
679 return -104;
680 }
681
Harald Weltee1d32892019-03-27 20:47:42 +0100682 /* Validate that toBankSlot / fromClientSlot match our expectations */
683 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
684 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
685 if (!bank_slot_equals(&worker->slot, &bslot)) {
686 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
687 bslot.bank_id, bslot.slot_nr);
688 return -105;
689 }
690 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
691 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
692 clslot.client_id, clslot.slot_nr);
693 return -106;
694 }
Harald Welte796a7492018-09-23 19:31:28 +0200695
Harald Welte297d72e2019-03-28 18:42:35 +0100696 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
697 rx_buf, &rx_buf_len);
698 if (rc < 0)
699 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200700
701 /* encode response PDU and send it */
702 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
703 rx_buf, rx_buf_len);
704 worker_send_rspro(worker, pdu_resp);
705
706 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200707}
708
Harald Welte77911b02018-08-14 23:47:30 +0200709/* handle one incoming RSPRO message from a client inside a worker thread */
710static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
711{
Harald Weltecce2aad2018-08-16 14:44:37 +0200712 int rc = -100;
713
Harald Welte736e8312019-03-31 13:08:16 +0200714 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
715
Harald Welte77911b02018-08-14 23:47:30 +0200716 switch (pdu->msg.present) {
717 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200718 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200719 break;
720 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200721 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200722 break;
723 case RsproPDUchoice_PR_clientSlotStatusInd:
724 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200725 rc = 0;
726 break;
727 case RsproPDUchoice_PR_setAtrRes:
728 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200729 break;
730 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200731 rc = -101;
732 break;
Harald Welte77911b02018-08-14 23:47:30 +0200733 }
734
Harald Weltecce2aad2018-08-16 14:44:37 +0200735 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200736}
737
Harald Welte694df832018-10-03 22:47:52 +0200738static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
739{
740 struct timeval tout = { timeout_secs, 0 };
741 fd_set readset;
742
743 FD_ZERO(&readset);
744 FD_SET(fd, &readset);
745 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
746}
747
Harald Welte77911b02018-08-14 23:47:30 +0200748/* body of the main transceive loop */
749static int worker_transceive_loop(struct bankd_worker *worker)
750{
751 struct ipaccess_head *hh;
752 struct ipaccess_head_ext *hh_ext;
753 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
754 asn_dec_rval_t rval;
755 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200756 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200757
Harald Welte00a96732019-03-11 17:18:02 +0100758restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200759 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100760 if (rc == -1 && errno == EINTR) {
761 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
762 return -23;
763 goto restart_wait;
764 } else if (rc < 0)
765 return rc;
766 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200767 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200768 switch (worker->state) {
769 case BW_ST_CONN_CLIENT_WAIT_MAP:
770 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100771 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200772 break;
773 case BW_ST_CONN_CLIENT_MAPPED:
774 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100775 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200776 break;
777 default:
778 OSMO_ASSERT(0);
779 }
Harald Welte1f699b42019-03-28 13:41:08 +0100780 if (rc == 0)
781 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200782 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200783 return 0;
784 };
785
Harald Welte77911b02018-08-14 23:47:30 +0200786 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100787 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200788 if (rc < 0)
789 return rc;
790 data_len = rc;
791
792 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200793 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200794 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200795 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200796 }
Harald Welte77911b02018-08-14 23:47:30 +0200797
Harald Welte5a3613a2018-10-11 12:56:21 +0200798 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100799 switch (hh->data[0]) {
800 case IPAC_MSGT_PING:
801 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100802 case IPAC_MSGT_ID_ACK:
803 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100804 default:
805 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
806 break;
807 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200808 return 0;
809 }
810
Harald Welte77911b02018-08-14 23:47:30 +0200811 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200812 if (data_len < sizeof(*hh_ext)) {
813 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200814 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200815 }
Harald Welte77911b02018-08-14 23:47:30 +0200816 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200817 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
818 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200819 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200820 }
Harald Welte77911b02018-08-14 23:47:30 +0200821
822 /* 2) ASN1 BER decode of the message */
823 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200824 if (rval.code != RC_OK) {
825 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200826 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200827 }
Harald Welte77911b02018-08-14 23:47:30 +0200828
829 /* 3) handling of the message, possibly resulting in PCSC commands */
830 rc = worker_handle_rspro(worker, pdu);
831 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200832 if (rc < 0) {
833 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200834 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200835 }
Harald Welte77911b02018-08-14 23:47:30 +0200836
837 /* everything OK if we reach here */
838 return 0;
839}
840
Harald Welted6dfb8c2018-08-16 14:46:53 +0200841/* obtain an ascii representation of the client IP/port */
842static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
843{
844 char hostbuf[32], portbuf[32];
845 int rc;
846
847 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
848 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
849 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
850 if (rc != 0) {
851 out[0] = '\0';
852 return -1;
853 }
854 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
855 return 0;
856}
857
Harald Welte77911b02018-08-14 23:47:30 +0200858/* worker thread main function */
859static void *worker_main(void *arg)
860{
Harald Welte77911b02018-08-14 23:47:30 +0200861 void *top_ctx;
862 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200863
Harald Welte00a96732019-03-11 17:18:02 +0100864 g_worker = (struct bankd_worker *) arg;
865
Harald Welte00a96732019-03-11 17:18:02 +0100866 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200867
Harald Welte77911b02018-08-14 23:47:30 +0200868 /* not permitted in multithreaded environment */
869 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100870 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
871 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200872
Harald Welte286a2be2019-03-11 17:36:58 +0100873 /* set the thread name */
874 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
875 pthread_setname_np(pthread_self(), g_worker->name);
876
Harald Welte77911b02018-08-14 23:47:30 +0200877 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100878 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200879
Harald Welte602b6f72019-12-04 21:51:02 +0100880 g_worker->slot.bank_id = 0xffff;
881 g_worker->slot.slot_nr = 0xffff;
882
Harald Welte77911b02018-08-14 23:47:30 +0200883 /* we continuously perform the same loop here, recycling the worker thread
884 * once the client connection is gone or we have some trouble with the card/reader */
885 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200886 char buf[128];
887
Harald Welte00a96732019-03-11 17:18:02 +0100888 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200889
Harald Welte00a96732019-03-11 17:18:02 +0100890 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200891 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100892 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
893 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200894 if (rc < 0) {
895 continue;
896 }
Harald Welte00a96732019-03-11 17:18:02 +0100897 g_worker->client.fd = rc;
898 worker_client_addrstr(buf, sizeof(buf), g_worker);
899 LOGW(g_worker, "Accepted connection from %s\n", buf);
900 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200901
902 /* run the main worker transceive loop body until there was some error */
903 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100904 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200905 if (rc < 0)
906 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100907 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
908 break;
Harald Welte77911b02018-08-14 23:47:30 +0200909 }
910
Harald Welte00a96732019-03-11 17:18:02 +0100911 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200912
Harald Welte77911b02018-08-14 23:47:30 +0200913 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100914 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100915 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100916 if (g_worker->reader.name)
917 g_worker->reader.name = NULL;
918 if (g_worker->client.fd >= 0)
919 close(g_worker->client.fd);
920 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
921 g_worker->client.fd = -1;
922 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200923 }
924
925 pthread_cleanup_pop(1);
926 talloc_free(top_ctx);
927 pthread_exit(NULL);
928}