blob: 94227e3e59708ffe03ff57b2a754192ead86064b [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 {
Harald Welted9fb9392019-12-04 19:05:01 +0100220 rspro2client_slot(&cs, &rreq->client);
221 if (!client_slot_equals(&map->client, &cs)) {
222 LOGPFSM(srvc->fi, "ClientId in removeMappingReq != map\n");
223 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
224 } else {
225 LOGPFSM(srvc->fi, "removing slotmap\n");
226 bankd_srvc_remove_mapping(map);
227 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
228 }
Harald Welte454f5e22019-03-09 21:38:34 +0100229 }
230 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100231 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100232 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100233 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100234 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
235 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100236 return -1;
237 }
238
239 return 0;
240}
241
Harald Welte5bae20b2019-04-01 09:36:42 +0200242static void printf_help()
243{
244 printf(
245" -h --help Print this help message\n"
246" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
247" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
248" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
Joachim Steiger9c963dc2019-05-25 00:49:32 +0200249" -n --num-slots <1-65535> Number of Slots in this SIM bank (default: 8)\n"
Harald Welte5bae20b2019-04-01 09:36:42 +0200250" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
251" connections (default: INADDR_ANY)\n"
252" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
253" connectionss (default: 9999)\n"
254 );
255}
256
257static int g_bind_port = 9999;
258static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100259
Harald Welte707c85a2019-03-09 12:56:35 +0100260void handle_options(int argc, char **argv)
261{
Harald Welte5bae20b2019-04-01 09:36:42 +0200262 while (1) {
263 int option_index = 0, c;
264 static const struct option long_options[] = {
265 { "help", 0, 0, 'h' },
266 { "server-host", 1, 0, 'i' },
267 { "server-port", 1, 0, 'p' },
268 { "bank-id", 1, 0, 'b' },
Harald Welte2513d812019-04-01 21:03:02 +0200269 { "num-slots", 1, 0, 'n' },
Harald Welte5bae20b2019-04-01 09:36:42 +0200270 { "component-name", 1, 0, 'N' },
271 { "bind-ip", 1, 0, 'I' },
272 { "bind-port", 1, 0, 'P' },
273 { 0, 0, 0, 0 }
274 };
275
Harald Welte2513d812019-04-01 21:03:02 +0200276 c = getopt_long(argc, argv, "hi:o:b:n:N:I:P:", long_options, &option_index);
Harald Welte5bae20b2019-04-01 09:36:42 +0200277 if (c == -1)
278 break;
279
280 switch (c) {
281 case 'h':
282 printf_help();
283 exit(0);
284 break;
285 case 'i':
286 g_bankd->srvc.server_host = optarg;
287 break;
288 case 'p':
289 g_bankd->srvc.server_port = atoi(optarg);
290 break;
291 case 'b':
Harald Welte2513d812019-04-01 21:03:02 +0200292 g_bankd->srvc.bankd.bank_id = atoi(optarg);
293 break;
294 case 'n':
295 g_bankd->srvc.bankd.num_slots = atoi(optarg);
Harald Welte5bae20b2019-04-01 09:36:42 +0200296 break;
297 case 'N':
298 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
299 break;
300 case 'I':
301 g_bind_ip = optarg;
302 break;
303 case 'P':
304 g_bind_port = atoi(optarg);
305 break;
306 }
307 }
Harald Welte707c85a2019-03-09 12:56:35 +0100308}
309
Harald Welte77911b02018-08-14 23:47:30 +0200310int main(int argc, char **argv)
311{
Harald Weltef4b16f12019-03-09 20:58:17 +0100312 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200313 int i, rc;
314
Harald Weltef4b16f12019-03-09 20:58:17 +0100315 g_bankd = talloc_zero(NULL, struct bankd);
316 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200317
Harald Weltef4b16f12019-03-09 20:58:17 +0100318 bankd_init(g_bankd);
319
320 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100321 srvc->server_host = "localhost";
322 srvc->server_port = 9998;
323 srvc->handle_rx = bankd_srvc_handle_rx;
324 srvc->own_comp_id.type = ComponentType_remsimBankd;
325 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
326 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
327 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
328
329 handle_options(argc, argv);
330
Harald Welte25075972019-03-11 17:33:17 +0100331 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100332 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100333 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100334
Harald Weltee89fecd2019-04-04 09:23:13 +0200335 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
336 * read once during bankd initialization, when the worker threads haven't
337 * started yet */
338 OSMO_ASSERT(bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv") == 0);
339
Harald Welte707c85a2019-03-09 12:56:35 +0100340 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100341 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100342 if (rc < 0) {
343 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
344 exit(1);
345 }
346
347 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200348 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 +0200349 if (rc < 0)
350 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100351 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200352
Harald Weltea0f39502019-03-09 20:59:34 +0100353 /* create worker threads: One per reader/slot! */
Harald Welte2513d812019-04-01 21:03:02 +0200354 for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200355 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100356 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200357 if (!w)
358 exit(21);
359 }
360
361 while (1) {
362 if (terminate)
363 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100364 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200365 }
366
Harald Weltef4b16f12019-03-09 20:58:17 +0100367 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200368 exit(0);
369}
370
371
372
373/***********************************************************************
374 * bankd worker thread
375 ***********************************************************************/
376
Harald Welte00a96732019-03-11 17:18:02 +0100377static __thread struct bankd_worker *g_worker;
378
Harald Welte8d858292018-08-15 23:36:46 +0200379struct value_string worker_state_names[] = {
380 { BW_ST_INIT, "INIT" },
381 { BW_ST_ACCEPTING, "ACCEPTING" },
382 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
383 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200384 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200385 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
386 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100387 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200388 { 0, NULL }
389};
390
Harald Welte1f699b42019-03-28 13:41:08 +0100391static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
392
Harald Welte8d858292018-08-15 23:36:46 +0200393static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
394{
395 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
396 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200397 worker->timeout = 0;
398}
399
400static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
401 unsigned int timeout_secs)
402{
403 LOGW(worker, "Changing state to %s (timeout=%u)\n",
404 get_value_string(worker_state_names, new_state), timeout_secs);
405 worker->state = new_state;
406 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200407}
Harald Welte77911b02018-08-14 23:47:30 +0200408
Harald Welte00a96732019-03-11 17:18:02 +0100409/* signal handler for receiving SIGMAPDEL from main thread */
410static void handle_sig_mapdel(int sig)
411{
412 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
413 OSMO_ASSERT(sig == SIGMAPDEL);
414 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
415}
416
Harald Welte25075972019-03-11 17:33:17 +0100417static void handle_sig_usr1(int sig)
418{
419 OSMO_ASSERT(sig == SIGUSR1);
420
421 if (pthread_equal(g_bankd->main, pthread_self())) {
422 struct bankd_worker *worker;
423 /* main thread */
424 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200425 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100426
427 /* iterate over worker threads and ask them to dump their talloc state */
428 pthread_mutex_lock(&g_bankd->workers_mutex);
429 llist_for_each_entry(worker, &g_bankd->workers, list) {
430 pthread_kill(worker->thread, SIGUSR1);
431 }
432 pthread_mutex_unlock(&g_bankd->workers_mutex);
433 } else {
434 /* worker thread */
435 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200436 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100437 }
438}
439
Harald Welte77911b02018-08-14 23:47:30 +0200440static void worker_cleanup(void *arg)
441{
442 struct bankd_worker *worker = (struct bankd_worker *) arg;
443 struct bankd *bankd = worker->bankd;
444
445 /* FIXME: should we still do this? in the thread ?!? */
446 pthread_mutex_lock(&bankd->workers_mutex);
447 llist_del(&worker->list);
448 talloc_free(worker); /* FIXME: is this safe? */
449 pthread_mutex_unlock(&bankd->workers_mutex);
450}
451
Harald Welteaf614732018-08-17 22:10:05 +0200452static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200453{
Harald Welte297d72e2019-03-28 18:42:35 +0100454 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200455
Harald Welte150d6d62018-10-03 23:07:47 +0200456 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
457
Harald Welte694df832018-10-03 22:47:52 +0200458 if (!worker->reader.name) {
459 /* resolve PC/SC reader name from slot_id -> name map */
460 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
461 if (!worker->reader.name) {
462 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
463 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100464 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200465 }
466 }
Harald Welte45c948c2018-09-23 19:26:52 +0200467 OSMO_ASSERT(worker->reader.name);
468
Harald Welte297d72e2019-03-28 18:42:35 +0100469 rc = worker->ops->open_card(worker);
470 if (rc < 0)
471 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100472
Harald Welte57593f02018-09-23 19:30:31 +0200473 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200474 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200475
Harald Welteaf614732018-08-17 22:10:05 +0200476 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200477}
Harald Welte77911b02018-08-14 23:47:30 +0200478
479
Harald Welte00a96732019-03-11 17:18:02 +0100480static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200481{
482 struct ipaccess_head *hh;
483 uint16_t len;
484 int needed, rc;
485
486 if (buf_size < sizeof(*hh))
487 return -1;
488
489 hh = (struct ipaccess_head *) buf;
490
Harald Welte00a96732019-03-11 17:18:02 +0100491 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
492 * in case of a signal being received */
493
494restart_hdr:
495 /* 1) blocking recv from the socket (IPA header) */
496 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
497 if (rc == -1 && errno == EINTR) {
498 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
499 return -23;
500 goto restart_hdr;
501 } else if (rc < 0)
502 return rc;
503 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200504 return -2;
505
506 len = ntohs(hh->len);
507 needed = len; //- sizeof(*hh);
508
Harald Welte00a96732019-03-11 17:18:02 +0100509restart_body:
510 /* 2) blocking recv from the socket (payload) */
511 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
512 if (rc == -1 && errno == EINTR) {
513 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
514 return -23;
515 goto restart_body;
516 } else if (rc < 0)
517 return rc;
518 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200519 return -3;
520
521 return len;
522}
523
Harald Welte796a7492018-09-23 19:31:28 +0200524static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
525{
526 struct msgb *msg = rspro_enc_msg(pdu);
527 int rc;
528
529 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100530 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200531 LOGW(worker, "error encoding RSPRO\n");
532 return -1;
533 }
534
Harald Weltefd471192018-09-24 14:51:14 +0200535 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200536 /* prepend the header */
537 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200538 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200539
540 /* actually send it through the socket */
541 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
542 if (rc == msgb_length(msg))
543 rc = 0;
544 else {
545 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
546 rc = -1;
547 }
548
549 msgb_free(msg);
550
551 return rc;
552}
553
Harald Welte150d6d62018-10-03 23:07:47 +0200554/* attempt to obtain slot-map */
555static int worker_try_slotmap(struct bankd_worker *worker)
556{
Harald Weltecbd18962019-03-03 19:02:38 +0100557 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200558
Harald Weltecbd18962019-03-03 19:02:38 +0100559 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200560 if (!slmap) {
561 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
562 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
563 /* check in 10s if the map has been installed meanwhile by main thread */
564 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
565 return -1;
566 } else {
567 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
568 slmap->client.client_id, slmap->client.slot_nr,
569 slmap->bank.bank_id, slmap->bank.slot_nr);
570 worker->slot = slmap->bank;
571 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
572 return worker_open_card(worker);
573 }
574}
575
Harald Welte1f699b42019-03-28 13:41:08 +0100576/* inform the remote end (client) about the (new) ATR */
577static int worker_send_atr(struct bankd_worker *worker)
578{
579 RsproPDU_t *set_atr;
580 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
581 worker->client.clslot.slot_nr,
582 worker->card.atr, worker->card.atr_len);
583 if (!set_atr)
584 return -1;
585 return worker_send_rspro(worker, set_atr);
586}
Harald Welte150d6d62018-10-03 23:07:47 +0200587
Harald Weltecce2aad2018-08-16 14:44:37 +0200588static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
589{
Harald Welteaf614732018-08-17 22:10:05 +0200590 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100591 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200592 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100593 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200594
Harald Weltecce2aad2018-08-16 14:44:37 +0200595 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
596
Harald Weltecce2aad2018-08-16 14:44:37 +0200597 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
598 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
599 /* FIXME: store somewhere? */
600
601 if (worker->state != BW_ST_CONN_WAIT_ID) {
602 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100603 rc = -102;
604 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200605 }
606
Harald Welte371d0262018-08-16 15:23:58 +0200607 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200608 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100609 res = ResultCode_illegalClientId;
610 rc = -103;
611 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200612 }
Harald Welte371d0262018-08-16 15:23:58 +0200613 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
614 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200615 worker_set_state(worker, BW_ST_CONN_CLIENT);
616
Harald Welte150d6d62018-10-03 23:07:47 +0200617 if (worker_try_slotmap(worker) >= 0)
618 res = ResultCode_ok;
619 else
Harald Welte3e689872018-09-24 14:52:56 +0200620 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200621
Harald Welte3e689872018-09-24 14:52:56 +0200622 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100623 rc = worker_send_rspro(worker, resp);
624 if (rc < 0)
625 return rc;
626
627 if (res == ResultCode_ok)
628 rc = worker_send_atr(worker);
629
630 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100631
632respond_and_err:
633 if (res) {
634 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
635 worker_send_rspro(worker, resp);
636 }
637 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200638}
639
Harald Welte796a7492018-09-23 19:31:28 +0200640static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
641{
642 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200643 uint8_t rx_buf[1024];
644 DWORD rx_buf_len = sizeof(rx_buf);
645 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100646 struct client_slot clslot;
647 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100648 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200649
650 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
651
652 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
653 LOGW(worker, "Unexpected tpduModemToCaard\n");
654 return -104;
655 }
656
Harald Weltee1d32892019-03-27 20:47:42 +0100657 /* Validate that toBankSlot / fromClientSlot match our expectations */
658 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
659 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
660 if (!bank_slot_equals(&worker->slot, &bslot)) {
661 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
662 bslot.bank_id, bslot.slot_nr);
663 return -105;
664 }
665 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
666 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
667 clslot.client_id, clslot.slot_nr);
668 return -106;
669 }
Harald Welte796a7492018-09-23 19:31:28 +0200670
Harald Welte297d72e2019-03-28 18:42:35 +0100671 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
672 rx_buf, &rx_buf_len);
673 if (rc < 0)
674 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200675
676 /* encode response PDU and send it */
677 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
678 rx_buf, rx_buf_len);
679 worker_send_rspro(worker, pdu_resp);
680
681 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200682}
683
Harald Welte77911b02018-08-14 23:47:30 +0200684/* handle one incoming RSPRO message from a client inside a worker thread */
685static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
686{
Harald Weltecce2aad2018-08-16 14:44:37 +0200687 int rc = -100;
688
Harald Welte736e8312019-03-31 13:08:16 +0200689 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
690
Harald Welte77911b02018-08-14 23:47:30 +0200691 switch (pdu->msg.present) {
692 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200693 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200694 break;
695 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200696 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200697 break;
698 case RsproPDUchoice_PR_clientSlotStatusInd:
699 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200700 rc = 0;
701 break;
702 case RsproPDUchoice_PR_setAtrRes:
703 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200704 break;
705 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200706 rc = -101;
707 break;
Harald Welte77911b02018-08-14 23:47:30 +0200708 }
709
Harald Weltecce2aad2018-08-16 14:44:37 +0200710 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200711}
712
Harald Welte694df832018-10-03 22:47:52 +0200713static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
714{
715 struct timeval tout = { timeout_secs, 0 };
716 fd_set readset;
717
718 FD_ZERO(&readset);
719 FD_SET(fd, &readset);
720 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
721}
722
Harald Welte77911b02018-08-14 23:47:30 +0200723/* body of the main transceive loop */
724static int worker_transceive_loop(struct bankd_worker *worker)
725{
726 struct ipaccess_head *hh;
727 struct ipaccess_head_ext *hh_ext;
728 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
729 asn_dec_rval_t rval;
730 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200731 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200732
Harald Welte00a96732019-03-11 17:18:02 +0100733restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200734 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100735 if (rc == -1 && errno == EINTR) {
736 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
737 return -23;
738 goto restart_wait;
739 } else if (rc < 0)
740 return rc;
741 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200742 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200743 switch (worker->state) {
744 case BW_ST_CONN_CLIENT_WAIT_MAP:
745 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100746 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200747 break;
748 case BW_ST_CONN_CLIENT_MAPPED:
749 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100750 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200751 break;
752 default:
753 OSMO_ASSERT(0);
754 }
Harald Welte1f699b42019-03-28 13:41:08 +0100755 if (rc == 0)
756 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200757 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200758 return 0;
759 };
760
Harald Welte77911b02018-08-14 23:47:30 +0200761 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100762 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200763 if (rc < 0)
764 return rc;
765 data_len = rc;
766
767 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200768 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200769 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200770 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200771 }
Harald Welte77911b02018-08-14 23:47:30 +0200772
Harald Welte5a3613a2018-10-11 12:56:21 +0200773 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100774 switch (hh->data[0]) {
775 case IPAC_MSGT_PING:
776 return ipa_ccm_send_pong(worker->client.fd);
Harald Welte667d6942019-12-01 22:34:21 +0100777 case IPAC_MSGT_ID_ACK:
778 return ipa_ccm_send_id_ack(g_worker->client.fd);
Harald Welte19f881a2019-03-11 18:39:13 +0100779 default:
780 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
781 break;
782 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200783 return 0;
784 }
785
Harald Welte77911b02018-08-14 23:47:30 +0200786 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200787 if (data_len < sizeof(*hh_ext)) {
788 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200789 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200790 }
Harald Welte77911b02018-08-14 23:47:30 +0200791 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200792 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
793 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200794 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200795 }
Harald Welte77911b02018-08-14 23:47:30 +0200796
797 /* 2) ASN1 BER decode of the message */
798 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200799 if (rval.code != RC_OK) {
800 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200801 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200802 }
Harald Welte77911b02018-08-14 23:47:30 +0200803
804 /* 3) handling of the message, possibly resulting in PCSC commands */
805 rc = worker_handle_rspro(worker, pdu);
806 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200807 if (rc < 0) {
808 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200809 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200810 }
Harald Welte77911b02018-08-14 23:47:30 +0200811
812 /* everything OK if we reach here */
813 return 0;
814}
815
Harald Welted6dfb8c2018-08-16 14:46:53 +0200816/* obtain an ascii representation of the client IP/port */
817static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
818{
819 char hostbuf[32], portbuf[32];
820 int rc;
821
822 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
823 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
824 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
825 if (rc != 0) {
826 out[0] = '\0';
827 return -1;
828 }
829 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
830 return 0;
831}
832
Harald Welte77911b02018-08-14 23:47:30 +0200833/* worker thread main function */
834static void *worker_main(void *arg)
835{
Harald Welte77911b02018-08-14 23:47:30 +0200836 void *top_ctx;
837 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200838
Harald Welte00a96732019-03-11 17:18:02 +0100839 g_worker = (struct bankd_worker *) arg;
840
Harald Welte00a96732019-03-11 17:18:02 +0100841 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200842
Harald Welte77911b02018-08-14 23:47:30 +0200843 /* not permitted in multithreaded environment */
844 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100845 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
846 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200847
Harald Welte286a2be2019-03-11 17:36:58 +0100848 /* set the thread name */
849 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
850 pthread_setname_np(pthread_self(), g_worker->name);
851
Harald Welte77911b02018-08-14 23:47:30 +0200852 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100853 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200854
855 /* we continuously perform the same loop here, recycling the worker thread
856 * once the client connection is gone or we have some trouble with the card/reader */
857 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200858 char buf[128];
859
Harald Welte00a96732019-03-11 17:18:02 +0100860 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200861
Harald Welte00a96732019-03-11 17:18:02 +0100862 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200863 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100864 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
865 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200866 if (rc < 0) {
867 continue;
868 }
Harald Welte00a96732019-03-11 17:18:02 +0100869 g_worker->client.fd = rc;
870 worker_client_addrstr(buf, sizeof(buf), g_worker);
871 LOGW(g_worker, "Accepted connection from %s\n", buf);
872 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200873
874 /* run the main worker transceive loop body until there was some error */
875 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100876 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200877 if (rc < 0)
878 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100879 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
880 break;
Harald Welte77911b02018-08-14 23:47:30 +0200881 }
882
Harald Welte00a96732019-03-11 17:18:02 +0100883 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200884
Harald Welte77911b02018-08-14 23:47:30 +0200885 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100886 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100887 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100888 if (g_worker->reader.name)
889 g_worker->reader.name = NULL;
890 if (g_worker->client.fd >= 0)
891 close(g_worker->client.fd);
892 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
893 g_worker->client.fd = -1;
894 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200895 }
896
897 pthread_cleanup_pop(1);
898 talloc_free(top_ctx);
899 pthread_exit(NULL);
900}