blob: 0b4a71882e8563b8196740805e7988095debbea9 [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 */
85 bankd->cfg.bank_id = 1;
86 bankd->cfg.num_slots = 8;
87
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 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
95 * read once during bankd initialization, when the worker threads haven't
96 * started yet */
97 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
98 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Welte77911b02018-08-14 23:47:30 +020099}
100
101/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200102static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200103{
104 struct bankd_worker *worker;
105 int rc;
106
107 worker = talloc_zero(bankd, struct bankd_worker);
108 if (!worker)
109 return NULL;
110
111 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200112 worker->num = i;
Harald Welte297d72e2019-03-28 18:42:35 +0100113 worker->ops = &pcsc_driver_ops;
Harald Welte77911b02018-08-14 23:47:30 +0200114
115 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
116
117 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
118 if (rc != 0) {
119 talloc_free(worker);
120 return NULL;
121 }
122
123 pthread_mutex_lock(&bankd->workers_mutex);
124 llist_add_tail(&worker->list, &bankd->workers);
125 pthread_mutex_unlock(&bankd->workers_mutex);
126
127 return worker;
128}
129
130static bool terminate = false;
131
Harald Welte707c85a2019-03-09 12:56:35 +0100132/* handle incoming messages from server */
133static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
134{
Harald Welte454f5e22019-03-09 21:38:34 +0100135 const CreateMappingReq_t *creq = NULL;
136 const RemoveMappingReq_t *rreq = NULL;
137 struct slot_mapping *map;
138 struct bank_slot bs;
139 struct client_slot cs;
140 RsproPDU_t *resp;
141
142 LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
Harald Welte707c85a2019-03-09 12:56:35 +0100143
144 switch (pdu->msg.present) {
145 case RsproPDUchoice_PR_connectBankRes:
146 /* Store 'identity' of server in srvc->peer_comp_id */
147 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
148 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
149 break;
Harald Welte454f5e22019-03-09 21:38:34 +0100150 case RsproPDUchoice_PR_createMappingReq:
151 creq = &pdu->msg.choice.createMappingReq;
Harald Welte94ba99b2019-03-27 22:42:11 +0100152 if (creq->bank.bankId != g_bankd->cfg.bank_id) {
153 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu "
154 "(we are %u)\n", creq->bank.bankId, g_bankd->cfg.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100155 resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100156 } else if (creq->bank.slotNr >= g_bankd->cfg.num_slots) {
157 LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu "
158 "(we have %u)\n", creq->bank.slotNr, g_bankd->cfg.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100159 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100160 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100161 rspro2bank_slot(&bs, &creq->bank);
162 rspro2client_slot(&cs, &creq->client);
163 /* Add a new mapping */
164 map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100165 if (!map) {
166 LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100167 resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100168 } else
Harald Welte454f5e22019-03-09 21:38:34 +0100169 resp = rspro_gen_CreateMappingRes(ResultCode_ok);
170 }
171 server_conn_send_rspro(srvc, resp);
172 break;
173 case RsproPDUchoice_PR_removeMappingReq:
174 rreq = &pdu->msg.choice.removeMappingReq;
Harald Welte94ba99b2019-03-27 22:42:11 +0100175 if (rreq->bank.bankId != g_bankd->cfg.bank_id) {
176 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu "
177 "(we are %u)\n", creq->bank.bankId, g_bankd->cfg.bank_id);
Harald Welte454f5e22019-03-09 21:38:34 +0100178 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100179 } else if (rreq->bank.slotNr >= g_bankd->cfg.num_slots) {
180 LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu "
181 "(we have %u)\n", creq->bank.slotNr, g_bankd->cfg.num_slots);
Harald Welte454f5e22019-03-09 21:38:34 +0100182 resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId);
Harald Welte94ba99b2019-03-27 22:42:11 +0100183 } else {
Harald Welte454f5e22019-03-09 21:38:34 +0100184 rspro2bank_slot(&bs, &rreq->bank);
185 /* Remove a mapping */
186 map = slotmap_by_bank(g_bankd->slotmaps, &bs);
Harald Welte94ba99b2019-03-27 22:42:11 +0100187 if (!map) {
188 LOGPFSML(srvc->fi, LOGL_ERROR, "could not find to-be-deleted slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100189 resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap);
Harald Welte94ba99b2019-03-27 22:42:11 +0100190 } else {
191 LOGPFSM(srvc->fi, "removing slotmap\n");
Harald Welte454f5e22019-03-09 21:38:34 +0100192 slotmap_del(g_bankd->slotmaps, map);
193 resp = rspro_gen_RemoveMappingRes(ResultCode_ok);
Harald Welte00a96732019-03-11 17:18:02 +0100194
195 /* kill/reset the respective worker, if any! */
196 struct bankd_worker *worker;
197 pthread_mutex_lock(&g_bankd->workers_mutex);
198 llist_for_each_entry(worker, &g_bankd->workers, list) {
199 if (bs.bank_id == worker->slot.bank_id &&
200 bs.slot_nr == worker->slot.slot_nr) {
201 pthread_kill(worker->thread, SIGMAPDEL);
202 break;
203 }
204 }
205 pthread_mutex_unlock(&g_bankd->workers_mutex);
Harald Welte454f5e22019-03-09 21:38:34 +0100206 }
207 }
Harald Welte942f1ff2019-03-09 21:49:08 +0100208 server_conn_send_rspro(srvc, resp);
Harald Welte454f5e22019-03-09 21:38:34 +0100209 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100210 default:
Harald Welteeb971b52019-03-27 22:41:45 +0100211 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n",
212 pdu->msg.present);
Harald Welte707c85a2019-03-09 12:56:35 +0100213 return -1;
214 }
215
216 return 0;
217}
218
Harald Welte5bae20b2019-04-01 09:36:42 +0200219static void printf_help()
220{
221 printf(
222" -h --help Print this help message\n"
223" -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n"
224" -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n"
225" -b --bank-id <1-65535> Bank Identifier of this SIM bank (default: 1)\n"
226" -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n"
227" connections (default: INADDR_ANY)\n"
228" -P --bind-port <1-65535> Local TCP port to bind for incoming client\n"
229" connectionss (default: 9999)\n"
230 );
231}
232
233static int g_bind_port = 9999;
234static char *g_bind_ip = NULL;
Harald Welte00a96732019-03-11 17:18:02 +0100235
Harald Welte707c85a2019-03-09 12:56:35 +0100236void handle_options(int argc, char **argv)
237{
Harald Welte5bae20b2019-04-01 09:36:42 +0200238 while (1) {
239 int option_index = 0, c;
240 static const struct option long_options[] = {
241 { "help", 0, 0, 'h' },
242 { "server-host", 1, 0, 'i' },
243 { "server-port", 1, 0, 'p' },
244 { "bank-id", 1, 0, 'b' },
245 { "component-name", 1, 0, 'N' },
246 { "bind-ip", 1, 0, 'I' },
247 { "bind-port", 1, 0, 'P' },
248 { 0, 0, 0, 0 }
249 };
250
251 c = getopt_long(argc, argv, "hi:o:b:N:I:P:", long_options, &option_index);
252 if (c == -1)
253 break;
254
255 switch (c) {
256 case 'h':
257 printf_help();
258 exit(0);
259 break;
260 case 'i':
261 g_bankd->srvc.server_host = optarg;
262 break;
263 case 'p':
264 g_bankd->srvc.server_port = atoi(optarg);
265 break;
266 case 'b':
267 g_bankd->cfg.bank_id = atoi(optarg);
268 break;
269 case 'N':
270 OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg);
271 break;
272 case 'I':
273 g_bind_ip = optarg;
274 break;
275 case 'P':
276 g_bind_port = atoi(optarg);
277 break;
278 }
279 }
Harald Welte707c85a2019-03-09 12:56:35 +0100280}
281
Harald Welte77911b02018-08-14 23:47:30 +0200282int main(int argc, char **argv)
283{
Harald Weltef4b16f12019-03-09 20:58:17 +0100284 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200285 int i, rc;
286
Harald Weltef4b16f12019-03-09 20:58:17 +0100287 g_bankd = talloc_zero(NULL, struct bankd);
288 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200289
Harald Weltef4b16f12019-03-09 20:58:17 +0100290 bankd_init(g_bankd);
291
292 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100293 srvc->server_host = "localhost";
294 srvc->server_port = 9998;
295 srvc->handle_rx = bankd_srvc_handle_rx;
296 srvc->own_comp_id.type = ComponentType_remsimBankd;
297 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
298 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
299 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
300
301 handle_options(argc, argv);
302
Harald Welte25075972019-03-11 17:33:17 +0100303 g_bankd->main = pthread_self();
Harald Welte00a96732019-03-11 17:18:02 +0100304 signal(SIGMAPDEL, handle_sig_mapdel);
Harald Welte25075972019-03-11 17:33:17 +0100305 signal(SIGUSR1, handle_sig_usr1);
Harald Welte00a96732019-03-11 17:18:02 +0100306
Harald Welte707c85a2019-03-09 12:56:35 +0100307 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100308 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100309 if (rc < 0) {
310 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
311 exit(1);
312 }
313
314 /* create listening socket for inbound client connections */
Harald Welte5bae20b2019-04-01 09:36:42 +0200315 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 +0200316 if (rc < 0)
317 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100318 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200319
Harald Weltea0f39502019-03-09 20:59:34 +0100320 /* create worker threads: One per reader/slot! */
321 for (i = 0; i < g_bankd->cfg.num_slots; i++) {
Harald Welte77911b02018-08-14 23:47:30 +0200322 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100323 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200324 if (!w)
325 exit(21);
326 }
327
328 while (1) {
329 if (terminate)
330 break;
Harald Welte707c85a2019-03-09 12:56:35 +0100331 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200332 }
333
Harald Weltef4b16f12019-03-09 20:58:17 +0100334 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200335 exit(0);
336}
337
338
339
340/***********************************************************************
341 * bankd worker thread
342 ***********************************************************************/
343
Harald Welte00a96732019-03-11 17:18:02 +0100344static __thread struct bankd_worker *g_worker;
345
Harald Welte8d858292018-08-15 23:36:46 +0200346struct value_string worker_state_names[] = {
347 { BW_ST_INIT, "INIT" },
348 { BW_ST_ACCEPTING, "ACCEPTING" },
349 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
350 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200351 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200352 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
353 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
Harald Welte00a96732019-03-11 17:18:02 +0100354 { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" },
Harald Welte8d858292018-08-15 23:36:46 +0200355 { 0, NULL }
356};
357
Harald Welte1f699b42019-03-28 13:41:08 +0100358static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu);
359
Harald Welte8d858292018-08-15 23:36:46 +0200360static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
361{
362 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
363 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200364 worker->timeout = 0;
365}
366
367static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
368 unsigned int timeout_secs)
369{
370 LOGW(worker, "Changing state to %s (timeout=%u)\n",
371 get_value_string(worker_state_names, new_state), timeout_secs);
372 worker->state = new_state;
373 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200374}
Harald Welte77911b02018-08-14 23:47:30 +0200375
Harald Welte00a96732019-03-11 17:18:02 +0100376/* signal handler for receiving SIGMAPDEL from main thread */
377static void handle_sig_mapdel(int sig)
378{
379 LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n");
380 OSMO_ASSERT(sig == SIGMAPDEL);
381 worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED);
382}
383
Harald Welte25075972019-03-11 17:33:17 +0100384static void handle_sig_usr1(int sig)
385{
386 OSMO_ASSERT(sig == SIGUSR1);
387
388 if (pthread_equal(g_bankd->main, pthread_self())) {
389 struct bankd_worker *worker;
390 /* main thread */
391 fprintf(stderr, "=== Talloc Report of main thread:\n");
Harald Welteb54a51e2019-03-31 15:57:59 +0200392 talloc_report_full(g_tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100393
394 /* iterate over worker threads and ask them to dump their talloc state */
395 pthread_mutex_lock(&g_bankd->workers_mutex);
396 llist_for_each_entry(worker, &g_bankd->workers, list) {
397 pthread_kill(worker->thread, SIGUSR1);
398 }
399 pthread_mutex_unlock(&g_bankd->workers_mutex);
400 } else {
401 /* worker thread */
402 fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name);
Harald Welteb54a51e2019-03-31 15:57:59 +0200403 talloc_report_full(g_worker->tall_ctx, stderr);
Harald Welte25075972019-03-11 17:33:17 +0100404 }
405}
406
Harald Welte77911b02018-08-14 23:47:30 +0200407static void worker_cleanup(void *arg)
408{
409 struct bankd_worker *worker = (struct bankd_worker *) arg;
410 struct bankd *bankd = worker->bankd;
411
412 /* FIXME: should we still do this? in the thread ?!? */
413 pthread_mutex_lock(&bankd->workers_mutex);
414 llist_del(&worker->list);
415 talloc_free(worker); /* FIXME: is this safe? */
416 pthread_mutex_unlock(&bankd->workers_mutex);
417}
418
Harald Welteaf614732018-08-17 22:10:05 +0200419static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200420{
Harald Welte297d72e2019-03-28 18:42:35 +0100421 int rc;
Harald Welte77911b02018-08-14 23:47:30 +0200422
Harald Welte150d6d62018-10-03 23:07:47 +0200423 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
424
Harald Welte694df832018-10-03 22:47:52 +0200425 if (!worker->reader.name) {
426 /* resolve PC/SC reader name from slot_id -> name map */
427 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
428 if (!worker->reader.name) {
429 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
430 worker->slot.bank_id, worker->slot.slot_nr);
Harald Welte297d72e2019-03-28 18:42:35 +0100431 return -1;
Harald Welte694df832018-10-03 22:47:52 +0200432 }
433 }
Harald Welte45c948c2018-09-23 19:26:52 +0200434 OSMO_ASSERT(worker->reader.name);
435
Harald Welte297d72e2019-03-28 18:42:35 +0100436 rc = worker->ops->open_card(worker);
437 if (rc < 0)
438 return rc;
Harald Welte1f699b42019-03-28 13:41:08 +0100439
Harald Welte57593f02018-09-23 19:30:31 +0200440 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200441 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200442
Harald Welteaf614732018-08-17 22:10:05 +0200443 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200444}
Harald Welte77911b02018-08-14 23:47:30 +0200445
446
Harald Welte00a96732019-03-11 17:18:02 +0100447static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size)
Harald Welte77911b02018-08-14 23:47:30 +0200448{
449 struct ipaccess_head *hh;
450 uint16_t len;
451 int needed, rc;
452
453 if (buf_size < sizeof(*hh))
454 return -1;
455
456 hh = (struct ipaccess_head *) buf;
457
Harald Welte00a96732019-03-11 17:18:02 +0100458 /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR
459 * in case of a signal being received */
460
461restart_hdr:
462 /* 1) blocking recv from the socket (IPA header) */
463 rc = recv(worker->client.fd, buf, sizeof(*hh), 0);
464 if (rc == -1 && errno == EINTR) {
465 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
466 return -23;
467 goto restart_hdr;
468 } else if (rc < 0)
469 return rc;
470 else if (rc < sizeof(*hh))
Harald Welte77911b02018-08-14 23:47:30 +0200471 return -2;
472
473 len = ntohs(hh->len);
474 needed = len; //- sizeof(*hh);
475
Harald Welte00a96732019-03-11 17:18:02 +0100476restart_body:
477 /* 2) blocking recv from the socket (payload) */
478 rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0);
479 if (rc == -1 && errno == EINTR) {
480 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
481 return -23;
482 goto restart_body;
483 } else if (rc < 0)
484 return rc;
485 else if (rc < needed)
Harald Welte77911b02018-08-14 23:47:30 +0200486 return -3;
487
488 return len;
489}
490
Harald Welte796a7492018-09-23 19:31:28 +0200491static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
492{
493 struct msgb *msg = rspro_enc_msg(pdu);
494 int rc;
495
496 if (!msg) {
Harald Welte2eee4502019-03-30 08:50:35 +0100497 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Welte796a7492018-09-23 19:31:28 +0200498 LOGW(worker, "error encoding RSPRO\n");
499 return -1;
500 }
501
Harald Weltefd471192018-09-24 14:51:14 +0200502 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200503 /* prepend the header */
504 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200505 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200506
507 /* actually send it through the socket */
508 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
509 if (rc == msgb_length(msg))
510 rc = 0;
511 else {
512 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
513 rc = -1;
514 }
515
516 msgb_free(msg);
517
518 return rc;
519}
520
Harald Welte150d6d62018-10-03 23:07:47 +0200521/* attempt to obtain slot-map */
522static int worker_try_slotmap(struct bankd_worker *worker)
523{
Harald Weltecbd18962019-03-03 19:02:38 +0100524 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200525
Harald Weltecbd18962019-03-03 19:02:38 +0100526 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200527 if (!slmap) {
528 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
529 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
530 /* check in 10s if the map has been installed meanwhile by main thread */
531 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
532 return -1;
533 } else {
534 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
535 slmap->client.client_id, slmap->client.slot_nr,
536 slmap->bank.bank_id, slmap->bank.slot_nr);
537 worker->slot = slmap->bank;
538 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
539 return worker_open_card(worker);
540 }
541}
542
Harald Welte1f699b42019-03-28 13:41:08 +0100543/* inform the remote end (client) about the (new) ATR */
544static int worker_send_atr(struct bankd_worker *worker)
545{
546 RsproPDU_t *set_atr;
547 set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id,
548 worker->client.clslot.slot_nr,
549 worker->card.atr, worker->card.atr_len);
550 if (!set_atr)
551 return -1;
552 return worker_send_rspro(worker, set_atr);
553}
Harald Welte150d6d62018-10-03 23:07:47 +0200554
Harald Weltecce2aad2018-08-16 14:44:37 +0200555static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
556{
Harald Welteaf614732018-08-17 22:10:05 +0200557 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte458e01b2019-03-10 11:14:43 +0100558 RsproPDU_t *resp = NULL;
Harald Welte3e689872018-09-24 14:52:56 +0200559 e_ResultCode res;
Harald Welte458e01b2019-03-10 11:14:43 +0100560 int rc;
Harald Welteaf614732018-08-17 22:10:05 +0200561
Harald Weltecce2aad2018-08-16 14:44:37 +0200562 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
563
Harald Weltecce2aad2018-08-16 14:44:37 +0200564 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
565 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
566 /* FIXME: store somewhere? */
567
568 if (worker->state != BW_ST_CONN_WAIT_ID) {
569 LOGW(worker, "Unexpected connectClientReq\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100570 rc = -102;
571 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200572 }
573
Harald Welte371d0262018-08-16 15:23:58 +0200574 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200575 LOGW(worker, "missing clientID, aborting\n");
Harald Welte458e01b2019-03-10 11:14:43 +0100576 res = ResultCode_illegalClientId;
577 rc = -103;
578 goto respond_and_err;
Harald Weltecce2aad2018-08-16 14:44:37 +0200579 }
Harald Welte371d0262018-08-16 15:23:58 +0200580 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
581 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200582 worker_set_state(worker, BW_ST_CONN_CLIENT);
583
Harald Welte150d6d62018-10-03 23:07:47 +0200584 if (worker_try_slotmap(worker) >= 0)
585 res = ResultCode_ok;
586 else
Harald Welte3e689872018-09-24 14:52:56 +0200587 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200588
Harald Welte3e689872018-09-24 14:52:56 +0200589 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
Harald Welte1f699b42019-03-28 13:41:08 +0100590 rc = worker_send_rspro(worker, resp);
591 if (rc < 0)
592 return rc;
593
594 if (res == ResultCode_ok)
595 rc = worker_send_atr(worker);
596
597 return rc;
Harald Welte458e01b2019-03-10 11:14:43 +0100598
599respond_and_err:
600 if (res) {
601 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
602 worker_send_rspro(worker, resp);
603 }
604 return rc;
Harald Weltecce2aad2018-08-16 14:44:37 +0200605}
606
Harald Welte796a7492018-09-23 19:31:28 +0200607static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
608{
609 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
Harald Welte796a7492018-09-23 19:31:28 +0200610 uint8_t rx_buf[1024];
611 DWORD rx_buf_len = sizeof(rx_buf);
612 RsproPDU_t *pdu_resp;
Harald Weltee1d32892019-03-27 20:47:42 +0100613 struct client_slot clslot;
614 struct bank_slot bslot;
Harald Welte297d72e2019-03-28 18:42:35 +0100615 int rc;
Harald Welte796a7492018-09-23 19:31:28 +0200616
617 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
618
619 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
620 LOGW(worker, "Unexpected tpduModemToCaard\n");
621 return -104;
622 }
623
Harald Weltee1d32892019-03-27 20:47:42 +0100624 /* Validate that toBankSlot / fromClientSlot match our expectations */
625 rspro2client_slot(&clslot, &mdm2sim->fromClientSlot);
626 rspro2bank_slot(&bslot, &mdm2sim->toBankSlot);
627 if (!bank_slot_equals(&worker->slot, &bslot)) {
628 LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n",
629 bslot.bank_id, bslot.slot_nr);
630 return -105;
631 }
632 if (!client_slot_equals(&worker->client.clslot, &clslot)) {
633 LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n",
634 clslot.client_id, clslot.slot_nr);
635 return -106;
636 }
Harald Welte796a7492018-09-23 19:31:28 +0200637
Harald Welte297d72e2019-03-28 18:42:35 +0100638 rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size,
639 rx_buf, &rx_buf_len);
640 if (rc < 0)
641 return rc;
Harald Welte796a7492018-09-23 19:31:28 +0200642
643 /* encode response PDU and send it */
644 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
645 rx_buf, rx_buf_len);
646 worker_send_rspro(worker, pdu_resp);
647
648 return 0;
Harald Welte796a7492018-09-23 19:31:28 +0200649}
650
Harald Welte77911b02018-08-14 23:47:30 +0200651/* handle one incoming RSPRO message from a client inside a worker thread */
652static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
653{
Harald Weltecce2aad2018-08-16 14:44:37 +0200654 int rc = -100;
655
Harald Welte736e8312019-03-31 13:08:16 +0200656 LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu));
657
Harald Welte77911b02018-08-14 23:47:30 +0200658 switch (pdu->msg.present) {
659 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200660 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200661 break;
662 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200663 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200664 break;
665 case RsproPDUchoice_PR_clientSlotStatusInd:
666 /* FIXME */
Harald Welte24a3e322019-03-31 13:08:30 +0200667 rc = 0;
668 break;
669 case RsproPDUchoice_PR_setAtrRes:
670 rc = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200671 break;
672 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200673 rc = -101;
674 break;
Harald Welte77911b02018-08-14 23:47:30 +0200675 }
676
Harald Weltecce2aad2018-08-16 14:44:37 +0200677 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200678}
679
Harald Welte694df832018-10-03 22:47:52 +0200680static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
681{
682 struct timeval tout = { timeout_secs, 0 };
683 fd_set readset;
684
685 FD_ZERO(&readset);
686 FD_SET(fd, &readset);
687 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
688}
689
Harald Welte77911b02018-08-14 23:47:30 +0200690/* body of the main transceive loop */
691static int worker_transceive_loop(struct bankd_worker *worker)
692{
693 struct ipaccess_head *hh;
694 struct ipaccess_head_ext *hh_ext;
695 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
696 asn_dec_rval_t rval;
697 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200698 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200699
Harald Welte00a96732019-03-11 17:18:02 +0100700restart_wait:
Harald Welte694df832018-10-03 22:47:52 +0200701 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
Harald Welte00a96732019-03-11 17:18:02 +0100702 if (rc == -1 && errno == EINTR) {
703 if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
704 return -23;
705 goto restart_wait;
706 } else if (rc < 0)
707 return rc;
708 else if (rc == 0) {
Harald Welte694df832018-10-03 22:47:52 +0200709 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200710 switch (worker->state) {
711 case BW_ST_CONN_CLIENT_WAIT_MAP:
712 /* re-check if mapping exists meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100713 rc = worker_try_slotmap(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200714 break;
715 case BW_ST_CONN_CLIENT_MAPPED:
716 /* re-check if reader/card can be opened meanwhile? */
Harald Welte1f699b42019-03-28 13:41:08 +0100717 rc = worker_open_card(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200718 break;
719 default:
720 OSMO_ASSERT(0);
721 }
Harald Welte1f699b42019-03-28 13:41:08 +0100722 if (rc == 0)
723 worker_send_atr(worker);
Harald Welte150d6d62018-10-03 23:07:47 +0200724 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200725 return 0;
726 };
727
Harald Welte77911b02018-08-14 23:47:30 +0200728 /* 1) blocking read of entire IPA message from the socket */
Harald Welte00a96732019-03-11 17:18:02 +0100729 rc = blocking_ipa_read(worker, buf, sizeof(buf));
Harald Welte77911b02018-08-14 23:47:30 +0200730 if (rc < 0)
731 return rc;
732 data_len = rc;
733
734 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200735 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200736 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200737 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200738 }
Harald Welte77911b02018-08-14 23:47:30 +0200739
Harald Welte5a3613a2018-10-11 12:56:21 +0200740 if (hh->proto == IPAC_PROTO_IPACCESS) {
Harald Welte19f881a2019-03-11 18:39:13 +0100741 switch (hh->data[0]) {
742 case IPAC_MSGT_PING:
743 return ipa_ccm_send_pong(worker->client.fd);
744 default:
745 LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]);
746 break;
747 }
Harald Welte5a3613a2018-10-11 12:56:21 +0200748 return 0;
749 }
750
Harald Welte77911b02018-08-14 23:47:30 +0200751 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200752 if (data_len < sizeof(*hh_ext)) {
753 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200754 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200755 }
Harald Welte77911b02018-08-14 23:47:30 +0200756 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200757 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
758 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200759 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200760 }
Harald Welte77911b02018-08-14 23:47:30 +0200761
762 /* 2) ASN1 BER decode of the message */
763 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200764 if (rval.code != RC_OK) {
765 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200766 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200767 }
Harald Welte77911b02018-08-14 23:47:30 +0200768
769 /* 3) handling of the message, possibly resulting in PCSC commands */
770 rc = worker_handle_rspro(worker, pdu);
771 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200772 if (rc < 0) {
773 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200774 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200775 }
Harald Welte77911b02018-08-14 23:47:30 +0200776
777 /* everything OK if we reach here */
778 return 0;
779}
780
Harald Welted6dfb8c2018-08-16 14:46:53 +0200781/* obtain an ascii representation of the client IP/port */
782static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
783{
784 char hostbuf[32], portbuf[32];
785 int rc;
786
787 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
788 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
789 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
790 if (rc != 0) {
791 out[0] = '\0';
792 return -1;
793 }
794 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
795 return 0;
796}
797
Harald Welte77911b02018-08-14 23:47:30 +0200798/* worker thread main function */
799static void *worker_main(void *arg)
800{
Harald Welte77911b02018-08-14 23:47:30 +0200801 void *top_ctx;
802 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200803
Harald Welte00a96732019-03-11 17:18:02 +0100804 g_worker = (struct bankd_worker *) arg;
805
Harald Welte00a96732019-03-11 17:18:02 +0100806 worker_set_state(g_worker, BW_ST_INIT);
Harald Welte8d858292018-08-15 23:36:46 +0200807
Harald Welte77911b02018-08-14 23:47:30 +0200808 /* not permitted in multithreaded environment */
809 talloc_disable_null_tracking();
Harald Welte25075972019-03-11 17:33:17 +0100810 g_worker->tall_ctx = talloc_named_const(NULL, 0, "top");
811 talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1");
Harald Welte77911b02018-08-14 23:47:30 +0200812
Harald Welte286a2be2019-03-11 17:36:58 +0100813 /* set the thread name */
814 g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num);
815 pthread_setname_np(pthread_self(), g_worker->name);
816
Harald Welte77911b02018-08-14 23:47:30 +0200817 /* push cleanup helper */
Harald Welte00a96732019-03-11 17:18:02 +0100818 pthread_cleanup_push(&worker_cleanup, g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200819
820 /* we continuously perform the same loop here, recycling the worker thread
821 * once the client connection is gone or we have some trouble with the card/reader */
822 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200823 char buf[128];
824
Harald Welte00a96732019-03-11 17:18:02 +0100825 g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr);
Harald Welte77911b02018-08-14 23:47:30 +0200826
Harald Welte00a96732019-03-11 17:18:02 +0100827 worker_set_state(g_worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200828 /* first wait for an incoming TCP connection */
Harald Welte00a96732019-03-11 17:18:02 +0100829 rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr,
830 &g_worker->client.peer_addr_len);
Harald Welte77911b02018-08-14 23:47:30 +0200831 if (rc < 0) {
832 continue;
833 }
Harald Welte00a96732019-03-11 17:18:02 +0100834 g_worker->client.fd = rc;
835 worker_client_addrstr(buf, sizeof(buf), g_worker);
836 LOGW(g_worker, "Accepted connection from %s\n", buf);
837 worker_set_state(g_worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200838
839 /* run the main worker transceive loop body until there was some error */
840 while (1) {
Harald Welte00a96732019-03-11 17:18:02 +0100841 rc = worker_transceive_loop(g_worker);
Harald Welte77911b02018-08-14 23:47:30 +0200842 if (rc < 0)
843 break;
Harald Welte653d6a02019-03-11 18:38:44 +0100844 if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED)
845 break;
Harald Welte77911b02018-08-14 23:47:30 +0200846 }
847
Harald Welte00a96732019-03-11 17:18:02 +0100848 LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc);
Harald Welted6dfb8c2018-08-16 14:46:53 +0200849
Harald Welte77911b02018-08-14 23:47:30 +0200850 /* clean-up: reset to sane state */
Harald Welte1f699b42019-03-28 13:41:08 +0100851 memset(&g_worker->card, 0, sizeof(g_worker->card));
Harald Welte297d72e2019-03-28 18:42:35 +0100852 g_worker->ops->cleanup(g_worker);
Harald Welte00a96732019-03-11 17:18:02 +0100853 if (g_worker->reader.name)
854 g_worker->reader.name = NULL;
855 if (g_worker->client.fd >= 0)
856 close(g_worker->client.fd);
857 memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr));
858 g_worker->client.fd = -1;
859 g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200860 }
861
862 pthread_cleanup_pop(1);
863 talloc_free(top_ctx);
864 pthread_exit(NULL);
865}