blob: 03101fb26f4170677f557a862ecfb8308eadf144 [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;
153 struct slot_mapping *map;
154 struct bank_slot bs;
155 struct client_slot cs;
156 RsproPDU_t *resp;
157
158 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100159
160 switch (pdu->msg.present) {
161 case RsproPDUchoice_PR_connectBankRes:
162 /* Store 'identity' of server in srvc->peer_comp_id */
163 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
164 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
165 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100166 case RsproPDUchoice_PR_createMappingReq:
167 creq = &pdu->msg.choice.createMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200168 if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100169 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200170 "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100171 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200172 } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100173 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
Harald Welte2513d812019-04-01 21:03:02 +0200174 "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100175 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100176 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100177 rspro2bank_slot(&bs, &creq->bank);
178 rspro2client_slot(&cs, &creq->client);
Harald Weltee6fa46a2019-12-04 15:06:33 +0100179 /* check if map exists */
180 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
181 if (map) {
182 if (client_slot_equals(&map->client, &cs)) {
183 LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n");
184 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
185 goto send_resp;
186 } else {
187 LOGPFSM(srvc->fi, "implicitly removing slotmap\n");
188 bankd_srvc_remove_mapping(map);
189 }
190 }
Harald Welte454f5e22019-03-09 21:38:34 +0100191 /* Add a new mapping */
192 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100193 if (!map) {
194 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100195 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100196 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100197 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
198 }
Harald Weltee6fa46a2019-12-04 15:06:33 +0100199send_resp:
Harald Welte454f5e22019-03-09 21:38:34 +0100200 server_conn_send_rspro(srvc, resp);
201 break;
202 case RsproPDUchoice_PR_removeMappingReq:
203 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte2513d812019-04-01 21:03:02 +0200204 if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100205 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100206 "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100207 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte2513d812019-04-01 21:03:02 +0200208 } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) {
Harald Welte94ba99b2019-03-27 22:42:11 +0100209 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
Harald Welte550b2952019-12-04 21:22:54 +0100210 "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100211 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100212 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100213 rspro2bank_slot(&bs, &rreq->bank);
214 /* Remove a mapping */
215 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100216 if (!map) {
217 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100218 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100219 } else {
220 LOGPFSM(srvc->fi, "removing slotmap\n");
Harald Weltec650a4d2019-12-04 15:02:27 +0100221 bankd_srvc_remove_mapping(map);
Harald Welte454f5e22019-03-09 21:38:34 +0100222 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
223 }
224 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100225 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100226 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100227 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100228 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
229 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100230 return -1;
231 }
232
233 return 0;
234}
235
Harald Welte5bae20b2019-04-01 09:36:42 +0200236static void printf_help()
237{
238 printf(
239" -h --help Print this help message\n"
240" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
241" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
242" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200243" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200244" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
245" connections (default: INADDR_ANY)\n"
246" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
247" connectionss (default: 9999)\n"
248 );
249}
250
251static int g_bind_port = 9999;
252static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100253
Harald Welte707c85a2019-03-09 12:56:35 +0100254void handle_options(int argc, char **argv)
255{
Harald Welte5bae20b2019-04-01 09:36:42 +0200256 while (1) {
257 int option_index = 0, c;
258 static const struct option long_options[] = {
259 { "help", 0, 0, 'h' },
260 { "server-host", 1, 0, 'i' },
261 { "server-port", 1, 0, 'p' },
262 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200263 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200264 { "component-name", 1, 0, 'N' },
265 { "bind-ip", 1, 0, 'I' },
266 { "bind-port", 1, 0, 'P' },
267 { 0, 0, 0, 0 }
268 };
269
Harald Welte2513d812019-04-01 21:03:02 +0200270 c = getopt_long(argc, argv, "hi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200271 if (c == -1)
272 break;
273
274 switch (c) {
275 case 'h':
276 printf_help();
277 exit(0);
278 break;
279 case 'i':
280 g_bankd->srvc.server_host = optarg;
281 break;
282 case 'p':
283 g_bankd->srvc.server_port = atoi(optarg);
284 break;
285 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200286 g_bankd->srvc.bankd.bank_id = atoi(optarg);
287 break;
288 case 'n':
289 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200290 break;
291 case 'N':
292 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
293 break;
294 case 'I':
295 g_bind_ip = optarg;
296 break;
297 case 'P':
298 g_bind_port = atoi(optarg);
299 break;
300 }
301 }
Harald Welte707c85a2019-03-09 12:56:35 +0100302}
303
Harald Welte77911b02018-08-14 23:47:30 +0200304int main(int argc, char **argv)
305{
Harald Weltef4b16f12019-03-09 20:58:17 +0100306 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200307 int i, rc;
308
Harald Weltef4b16f12019-03-09 20:58:17 +0100309 g_bankd = talloc_zero(NULL, struct bankd);
310 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200311
Harald Weltef4b16f12019-03-09 20:58:17 +0100312 bankd_init(g_bankd);
313
314 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100315 srvc->server_host = "localhost";
316 srvc->server_port = 9998;
317 srvc->handle_rx = bankd_srvc_handle_rx;
318 srvc->own_comp_id.type = ComponentType_remsimBankd;
319 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
320 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
321 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
322
323 handle_options(argc, argv);
324
Harald Welte25075972019-03-11 17:33:17 +0100325 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100326 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100327 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100328
Harald Weltee89fecd2019-04-04 09:23:13 +0200329 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
330 * read once during bankd initialization, when the worker threads haven't
331 * started yet */
332 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
333
Harald Welte707c85a2019-03-09 12:56:35 +0100334 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100335 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100336 if (rc < 0) {
337 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
338 exit(1);
339 }
340
341 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200342 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 +0200343 if (rc < 0)
344 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100345 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200346
Harald Weltea0f39502019-03-09 20:59:34 +0100347 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200348 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200349 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100350 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200351 if (!w)
352 exit(21);
353 }
354
355 while (1) {
356 if (terminate)
357 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100358 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200359 }
360
Harald Weltef4b16f12019-03-09 20:58:17 +0100361 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200362 exit(0);
363}
364
365
366
367/***********************************************************************
368 * bankd worker thread
369 ***********************************************************************/
370
Harald Welte00a96732019-03-11 17:18:02 +0100371static __thread struct bankd_worker *g_worker;
372
Harald Welte8d858292018-08-15 23:36:46 +0200373struct value_string worker_state_names[] = {
374 { BW_ST_INIT, "INIT" },
375 { BW_ST_ACCEPTING, "ACCEPTING" },
376 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
377 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200378 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200379 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
380 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100381 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200382 { 0, NULL }
383};
384
Harald Welte1f699b42019-03-28 13:41:08 +0100385static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
386
Harald Welte8d858292018-08-15 23:36:46 +0200387static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
388{
389 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
390 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200391 worker->timeout = 0;
392}
393
394static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
395 unsigned int timeout_secs)
396{
397 LOGW(worker, "Changing state to %s (timeout=%u)\n",
398 get_value_string(worker_state_names, new_state), timeout_secs);
399 worker->state = new_state;
400 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200401}
Harald Welte77911b02018-08-14 23:47:30 +0200402
Harald Welte00a96732019-03-11 17:18:02 +0100403/* signal handler for receiving SIGMAPDEL from main thread */
404static void handle_sig_mapdel(int sig)
405{
406 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
407 OSMO_ASSERT(sig == SIGMAPDEL);
408 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
409}
410
Harald Welte25075972019-03-11 17:33:17 +0100411static void handle_sig_usr1(int sig)
412{
413 OSMO_ASSERT(sig == SIGUSR1);
414
415 if (pthread_equal(g_bankd->main, pthread_self())) {
416 struct bankd_worker *worker;
417 /* main thread */
418 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200419 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100420
421 /* iterate over worker threads and ask them to dump their talloc state */
422 pthread_mutex_lock(&g_bankd->workers_mutex);
423 llist_for_each_entry(worker, &g_bankd->workers, list) {
424 pthread_kill(worker->thread, SIGUSR1);
425 }
426 pthread_mutex_unlock(&g_bankd->workers_mutex);
427 } else {
428 /* worker thread */
429 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200430 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100431 }
432}
433
Harald Welte77911b02018-08-14 23:47:30 +0200434static void worker_cleanup(void *arg)
435{
436 struct bankd_worker *worker = (struct bankd_worker *) arg;
437 struct bankd *bankd = worker->bankd;
438
439 /* FIXME: should we still do this? in the thread ?!? */
440 pthread_mutex_lock(&bankd->workers_mutex);
441 llist_del(&worker->list);
442 talloc_free(worker); /* FIXME: is this safe? */
443 pthread_mutex_unlock(&bankd->workers_mutex);
444}
445
Harald Welteaf614732018-08-17 22:10:05 +0200446static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200447{
Harald Welte297d72e2019-03-28 18:42:35 +0100448 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200449
Harald Welte150d6d62018-10-03 23:07:47 +0200450 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
451
Harald Welte694df832018-10-03 22:47:52 +0200452 if (!worker->reader.name) {
453 /* resolve PC/SC reader name from slot_id -> name map */
454 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
455 if (!worker->reader.name) {
456 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
457 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100458 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200459 }
460 }
Harald Welte45c948c2018-09-23 19:26:52 +0200461 OSMO_ASSERT(worker->reader.name);
462
Harald Welte297d72e2019-03-28 18:42:35 +0100463 rc = worker->ops->open_card(worker);
464 if (rc < 0)
465 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100466
Harald Welte57593f02018-09-23 19:30:31 +0200467 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200468 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200469
Harald Welteaf614732018-08-17 22:10:05 +0200470 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200471}
Harald Welte77911b02018-08-14 23:47:30 +0200472
473
Harald Welte00a96732019-03-11 17:18:02 +0100474static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200475{
476 struct ipaccess_head *hh;
477 uint16_t len;
478 int needed, rc;
479
480 if (buf_size < sizeof(*hh))
481 return -1;
482
483 hh = (struct ipaccess_head *) buf;
484
Harald Welte00a96732019-03-11 17:18:02 +0100485 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
486 * in case of a signal being received */
487
488restart_hdr:
489 /* 1) blocking recv from the socket (IPA header) */
490 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
491 if (rc == -1 && errno == EINTR) {
492 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
493 return -23;
494 goto restart_hdr;
495 } else if (rc < 0)
496 return rc;
497 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200498 return -2;
499
500 len = ntohs(hh->len);
501 needed = len; //- sizeof(*hh);
502
Harald Welte00a96732019-03-11 17:18:02 +0100503restart_body:
504 /* 2) blocking recv from the socket (payload) */
505 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
506 if (rc == -1 && errno == EINTR) {
507 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
508 return -23;
509 goto restart_body;
510 } else if (rc < 0)
511 return rc;
512 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200513 return -3;
514
515 return len;
516}
517
Harald Welte796a7492018-09-23 19:31:28 +0200518static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
519{
520 struct msgb *msg = rspro_enc_msg(pdu);
521 int rc;
522
523 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100524 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200525 LOGW(worker, "error encoding RSPRO\n");
526 return -1;
527 }
528
Harald Weltefd471192018-09-24 14:51:14 +0200529 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200530 /* prepend the header */
531 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200532 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200533
534 /* actually send it through the socket */
535 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
536 if (rc == msgb_length(msg))
537 rc = 0;
538 else {
539 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
540 rc = -1;
541 }
542
543 msgb_free(msg);
544
545 return rc;
546}
547
Harald Welte150d6d62018-10-03 23:07:47 +0200548/* attempt to obtain slot-map */
549static int worker_try_slotmap(struct bankd_worker *worker)
550{
Harald Weltecbd18962019-03-03 19:02:38 +0100551 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200552
Harald Weltecbd18962019-03-03 19:02:38 +0100553 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200554 if (!slmap) {
555 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
556 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
557 /* check in 10s if the map has been installed meanwhile by main thread */
558 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
559 return -1;
560 } else {
561 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
562 slmap->client.client_id, slmap->client.slot_nr,
563 slmap->bank.bank_id, slmap->bank.slot_nr);
564 worker->slot = slmap->bank;
565 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
566 return worker_open_card(worker);
567 }
568}
569
Harald Welte1f699b42019-03-28 13:41:08 +0100570/* inform the remote end (client) about the (new) ATR */
571static int worker_send_atr(struct bankd_worker *worker)
572{
573 RsproPDU_t *set_atr;
574 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
575 worker->client.clslot.slot_nr,
576 worker->card.atr, worker->card.atr_len);
577 if (!set_atr)
578 return -1;
579 return worker_send_rspro(worker, set_atr);
580}
Harald Welte150d6d62018-10-03 23:07:47 +0200581
Harald Weltecce2aad2018-08-16 14:44:37 +0200582static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
583{
Harald Welteaf614732018-08-17 22:10:05 +0200584 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100585 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200586 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100587 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200588
Harald Weltecce2aad2018-08-16 14:44:37 +0200589 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
590
Harald Weltecce2aad2018-08-16 14:44:37 +0200591 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
592 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
593 /* FIXME: store somewhere? */
594
595 if (worker->state != BW_ST_CONN_WAIT_ID) {
596 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100597 rc = -102;
598 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200599 }
600
Harald Welte371d0262018-08-16 15:23:58 +0200601 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200602 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100603 res = ResultCode_illegalClientId;
604 rc = -103;
605 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200606 }
Harald Welte371d0262018-08-16 15:23:58 +0200607 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
608 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200609 worker_set_state(worker, BW_ST_CONN_CLIENT);
610
Harald Welte150d6d62018-10-03 23:07:47 +0200611 if (worker_try_slotmap(worker) >= 0)
612 res = ResultCode_ok;
613 else
Harald Welte3e689872018-09-24 14:52:56 +0200614 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200615
Harald Welte3e689872018-09-24 14:52:56 +0200616 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100617 rc = worker_send_rspro(worker, resp);
618 if (rc < 0)
619 return rc;
620
621 if (res == ResultCode_ok)
622 rc = worker_send_atr(worker);
623
624 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100625
626respond_and_err:
627 if (res) {
628 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
629 worker_send_rspro(worker, resp);
630 }
631 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200632}
633
Harald Welte796a7492018-09-23 19:31:28 +0200634static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
635{
636 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200637 uint8_t rx_buf[1024];
638 DWORD rx_buf_len = sizeof(rx_buf);
639 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100640 struct client_slot clslot;
641 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100642 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200643
644 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
645
646 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
647 LOGW(worker, "Unexpected tpduModemToCaard\n");
648 return -104;
649 }
650
Harald Weltee1d32892019-03-27 20:47:42 +0100651 /* Validate that toBankSlot / fromClientSlot match our expectations */
652 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
653 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
654 if (!bank_slot_equals(&worker->slot, &bslot)) {
655 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
656 bslot.bank_id, bslot.slot_nr);
657 return -105;
658 }
659 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
660 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
661 clslot.client_id, clslot.slot_nr);
662 return -106;
663 }
Harald Welte796a7492018-09-23 19:31:28 +0200664
Harald Welte297d72e2019-03-28 18:42:35 +0100665 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
666 rx_buf, &rx_buf_len);
667 if (rc < 0)
668 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200669
670 /* encode response PDU and send it */
671 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
672 rx_buf, rx_buf_len);
673 worker_send_rspro(worker, pdu_resp);
674
675 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200676}
677
Harald Welte77911b02018-08-14 23:47:30 +0200678/* handle one incoming RSPRO message from a client inside a worker thread */
679static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
680{
Harald Weltecce2aad2018-08-16 14:44:37 +0200681 int rc = -100;
682
Harald Welte736e8312019-03-31 13:08:16 +0200683 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
684
Harald Welte77911b02018-08-14 23:47:30 +0200685 switch (pdu->msg.present) {
686 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200687 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200688 break;
689 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200690 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200691 break;
692 case RsproPDUchoice_PR_clientSlotStatusInd:
693 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200694 rc = 0;
695 break;
696 case RsproPDUchoice_PR_setAtrRes:
697 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200698 break;
699 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200700 rc = -101;
701 break;
Harald Welte77911b02018-08-14 23:47:30 +0200702 }
703
Harald Weltecce2aad2018-08-16 14:44:37 +0200704 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200705}
706
Harald Welte694df832018-10-03 22:47:52 +0200707static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
708{
709 struct timeval tout = { timeout_secs, 0 };
710 fd_set readset;
711
712 FD_ZERO(&readset);
713 FD_SET(fd, &readset);
714 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
715}
716
Harald Welte77911b02018-08-14 23:47:30 +0200717/* body of the main transceive loop */
718static int worker_transceive_loop(struct bankd_worker *worker)
719{
720 struct ipaccess_head *hh;
721 struct ipaccess_head_ext *hh_ext;
722 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
723 asn_dec_rval_t rval;
724 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200725 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200726
Harald Welte00a96732019-03-11 17:18:02 +0100727restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200728 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100729 if (rc == -1 && errno == EINTR) {
730 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
731 return -23;
732 goto restart_wait;
733 } else if (rc < 0)
734 return rc;
735 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200736 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200737 switch (worker->state) {
738 case BW_ST_CONN_CLIENT_WAIT_MAP:
739 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100740 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200741 break;
742 case BW_ST_CONN_CLIENT_MAPPED:
743 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100744 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200745 break;
746 default:
747 OSMO_ASSERT(0);
748 }
Harald Welte1f699b42019-03-28 13:41:08 +0100749 if (rc == 0)
750 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200751 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200752 return 0;
753 };
754
Harald Welte77911b02018-08-14 23:47:30 +0200755 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100756 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200757 if (rc < 0)
758 return rc;
759 data_len = rc;
760
761 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200762 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200763 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200764 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200765 }
Harald Welte77911b02018-08-14 23:47:30 +0200766
Harald Welte5a3613a2018-10-11 12:56:21 +0200767 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100768 switch (hh->data[0]) {
769 case IPAC_MSGT_PING:
770 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100771 case IPAC_MSGT_ID_ACK:
772 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100773 default:
774 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
775 break;
776 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200777 return 0;
778 }
779
Harald Welte77911b02018-08-14 23:47:30 +0200780 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200781 if (data_len < sizeof(*hh_ext)) {
782 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200783 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200784 }
Harald Welte77911b02018-08-14 23:47:30 +0200785 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200786 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
787 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200788 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200789 }
Harald Welte77911b02018-08-14 23:47:30 +0200790
791 /* 2) ASN1 BER decode of the message */
792 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200793 if (rval.code != RC_OK) {
794 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200795 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200796 }
Harald Welte77911b02018-08-14 23:47:30 +0200797
798 /* 3) handling of the message, possibly resulting in PCSC commands */
799 rc = worker_handle_rspro(worker, pdu);
800 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200801 if (rc < 0) {
802 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200803 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200804 }
Harald Welte77911b02018-08-14 23:47:30 +0200805
806 /* everything OK if we reach here */
807 return 0;
808}
809
Harald Welted6dfb8c2018-08-16 14:46:53 +0200810/* obtain an ascii representation of the client IP/port */
811static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
812{
813 char hostbuf[32], portbuf[32];
814 int rc;
815
816 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
817 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
818 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
819 if (rc != 0) {
820 out[0] = '\0';
821 return -1;
822 }
823 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
824 return 0;
825}
826
Harald Welte77911b02018-08-14 23:47:30 +0200827/* worker thread main function */
828static void *worker_main(void *arg)
829{
Harald Welte77911b02018-08-14 23:47:30 +0200830 void *top_ctx;
831 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200832
Harald Welte00a96732019-03-11 17:18:02 +0100833 g_worker = (struct bankd_worker *) arg;
834
Harald Welte00a96732019-03-11 17:18:02 +0100835 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200836
Harald Welte77911b02018-08-14 23:47:30 +0200837 /* not permitted in multithreaded environment */
838 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100839 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
840 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200841
Harald Welte286a2be2019-03-11 17:36:58 +0100842 /* set the thread name */
843 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
844 pthread_setname_np(pthread_self(), g_worker->name);
845
Harald Welte77911b02018-08-14 23:47:30 +0200846 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100847 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200848
849 /* we continuously perform the same loop here, recycling the worker thread
850 * once the client connection is gone or we have some trouble with the card/reader */
851 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200852 char buf[128];
853
Harald Welte00a96732019-03-11 17:18:02 +0100854 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200855
Harald Welte00a96732019-03-11 17:18:02 +0100856 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200857 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100858 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
859 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200860 if (rc < 0) {
861 continue;
862 }
Harald Welte00a96732019-03-11 17:18:02 +0100863 g_worker->client.fd = rc;
864 worker_client_addrstr(buf, sizeof(buf), g_worker);
865 LOGW(g_worker, "Accepted connection from %s\n", buf);
866 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200867
868 /* run the main worker transceive loop body until there was some error */
869 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100870 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200871 if (rc < 0)
872 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100873 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
874 break;
Harald Welte77911b02018-08-14 23:47:30 +0200875 }
876
Harald Welte00a96732019-03-11 17:18:02 +0100877 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200878
Harald Welte77911b02018-08-14 23:47:30 +0200879 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100880 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100881 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100882 if (g_worker->reader.name)
883 g_worker->reader.name = NULL;
884 if (g_worker->client.fd >= 0)
885 close(g_worker->client.fd);
886 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
887 g_worker->client.fd = -1;
888 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200889 }
890
891 pthread_cleanup_pop(1);
892 talloc_free(top_ctx);
893 pthread_exit(NULL);
894}