blob: 6ebb74ef6fe4b1bf733514be0764775f2b7ccaa6 [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>
28#include <unistd.h>
Harald Welte707c85a2019-03-09 12:56:35 +010029#include <errno.h>
Harald Welte77911b02018-08-14 23:47:30 +020030
31#include <pthread.h>
32
33#include <wintypes.h>
34#include <winscard.h>
35#include <pcsclite.h>
36
Harald Welted6dfb8c2018-08-16 14:46:53 +020037#include <sys/socket.h>
38#include <netdb.h>
39
Harald Welte12534e72018-08-15 23:37:29 +020040#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020041#include <osmocom/core/linuxlist.h>
Harald Weltef94b9ee2018-09-25 15:04:21 +020042#include <osmocom/core/logging.h>
43#include <osmocom/core/application.h>
Harald Welte77911b02018-08-14 23:47:30 +020044
45#include <osmocom/gsm/ipa.h>
46#include <osmocom/gsm/protocol/ipaccess.h>
47
Kévin Redonff5db6e2018-10-11 17:16:18 +020048#include <asn_application.h>
Harald Welte77911b02018-08-14 23:47:30 +020049#include <osmocom/rspro/RsproPDU.h>
50
51#include "bankd.h"
Harald Welte3cded632019-03-09 12:59:41 +010052#include "rspro_client_fsm.h"
Harald Welte61d98e92019-03-03 15:43:07 +010053#include "debug.h"
Harald Welte796a7492018-09-23 19:31:28 +020054#include "rspro_util.h"
Harald Welte77911b02018-08-14 23:47:30 +020055
56__thread void *talloc_asn1_ctx;
Harald Weltef4b16f12019-03-09 20:58:17 +010057struct bankd *g_bankd;
Harald Welte77911b02018-08-14 23:47:30 +020058
59static void *worker_main(void *arg);
60
61/***********************************************************************
62* bankd core / main thread
63***********************************************************************/
64
Harald Welte43ab79f2018-10-03 23:34:21 +020065int asn_debug;
66
Harald Welte77911b02018-08-14 23:47:30 +020067static void bankd_init(struct bankd *bankd)
68{
Harald Weltef94b9ee2018-09-25 15:04:21 +020069 void *g_tall_ctx = talloc_named_const(NULL, 0, "global");
70 osmo_init_logging2(g_tall_ctx, &log_info);
71
Harald Welte43ab79f2018-10-03 23:34:21 +020072 asn_debug = 0;
73
Harald Welte77911b02018-08-14 23:47:30 +020074 /* intialize members of 'bankd' */
Harald Weltecbd18962019-03-03 19:02:38 +010075 bankd->slotmaps = slotmap_init(bankd);
Harald Welte77911b02018-08-14 23:47:30 +020076 INIT_LLIST_HEAD(&bankd->workers);
77 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020078
Harald Weltef1dd1622018-09-24 14:54:23 +020079 bankd->comp_id.type = ComponentType_remsimBankd;
80 OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name");
81 OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd");
82 OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION);
83 /* FIXME: other members of app_comp_id */
84
Harald Welte45c948c2018-09-23 19:26:52 +020085 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
86 * read once during bankd initialization, when the worker threads haven't
87 * started yet */
88 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
89 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Weltee72e5732018-09-23 19:31:55 +020090
91 /* HACK HACK HACK */
92 {
93 struct bank_slot bs = { .bank_id = 1, };
94 struct client_slot cs = { .client_id = 23, };
95 int i;
96 for (i = 0; i < 5; i++) {
97 bs.slot_nr = cs.slot_nr = i;
Harald Weltecbd18962019-03-03 19:02:38 +010098 slotmap_add(bankd->slotmaps, &bs, &cs);
Harald Weltee72e5732018-09-23 19:31:55 +020099 }
100 }
Harald Welte77911b02018-08-14 23:47:30 +0200101}
102
103/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +0200104static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +0200105{
106 struct bankd_worker *worker;
107 int rc;
108
109 worker = talloc_zero(bankd, struct bankd_worker);
110 if (!worker)
111 return NULL;
112
113 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +0200114 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +0200115
116 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
117
118 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
119 if (rc != 0) {
120 talloc_free(worker);
121 return NULL;
122 }
123
124 pthread_mutex_lock(&bankd->workers_mutex);
125 llist_add_tail(&worker->list, &bankd->workers);
126 pthread_mutex_unlock(&bankd->workers_mutex);
127
128 return worker;
129}
130
131static bool terminate = false;
132
Harald Welte707c85a2019-03-09 12:56:35 +0100133/* handle incoming messages from server */
134static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
135{
136 struct RsproPDU_t *resp;
137
138 switch (pdu->msg.present) {
139 case RsproPDUchoice_PR_connectBankRes:
140 /* Store 'identity' of server in srvc->peer_comp_id */
141 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity);
142 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
143 break;
144 default:
145 fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present);
146 return -1;
147 }
148
149 return 0;
150}
151
152void handle_options(int argc, char **argv)
153{
154}
155
Harald Welte77911b02018-08-14 23:47:30 +0200156int main(int argc, char **argv)
157{
Harald Weltef4b16f12019-03-09 20:58:17 +0100158 struct rspro_server_conn *srvc;
Harald Welte77911b02018-08-14 23:47:30 +0200159 int i, rc;
160
Harald Weltef4b16f12019-03-09 20:58:17 +0100161 g_bankd = talloc_zero(NULL, struct bankd);
162 OSMO_ASSERT(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200163
Harald Weltef4b16f12019-03-09 20:58:17 +0100164 bankd_init(g_bankd);
165
166 srvc = &g_bankd->srvc;
Harald Welte707c85a2019-03-09 12:56:35 +0100167 srvc->server_host = "localhost";
168 srvc->server_port = 9998;
169 srvc->handle_rx = bankd_srvc_handle_rx;
170 srvc->own_comp_id.type = ComponentType_remsimBankd;
171 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name");
172 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd");
173 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
174
175 handle_options(argc, argv);
176
177 /* Connection towards remsim-server */
Harald Weltef4b16f12019-03-09 20:58:17 +0100178 rc = server_conn_fsm_alloc(g_bankd, srvc);
Harald Welte707c85a2019-03-09 12:56:35 +0100179 if (rc < 0) {
180 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
181 exit(1);
182 }
183
184 /* create listening socket for inbound client connections */
Harald Welte12534e72018-08-15 23:37:29 +0200185 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
186 if (rc < 0)
187 exit(1);
Harald Weltef4b16f12019-03-09 20:58:17 +0100188 g_bankd->accept_fd = rc;
Harald Welte12534e72018-08-15 23:37:29 +0200189
190 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +0200191 for (i = 0; i < 10; i++) {
192 struct bankd_worker *w;
Harald Weltef4b16f12019-03-09 20:58:17 +0100193 w = bankd_create_worker(g_bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +0200194 if (!w)
195 exit(21);
196 }
197
198 while (1) {
199 if (terminate)
200 break;
Harald Welte8f893ff2018-10-03 23:21:10 +0200201 /* FIXME: Connect to remsim-server from the main thread, register with
202 * it and await + process any slot mapping or other configuration commands.
203 * Ensure to re-connect as needed. */
Harald Welte7f684a02018-08-16 14:43:50 +0200204 sleep(1);
Harald Welte707c85a2019-03-09 12:56:35 +0100205 osmo_select_main(0);
Harald Welte77911b02018-08-14 23:47:30 +0200206 }
207
Harald Weltef4b16f12019-03-09 20:58:17 +0100208 talloc_free(g_bankd);
Harald Welte77911b02018-08-14 23:47:30 +0200209 exit(0);
210}
211
212
213
214/***********************************************************************
215 * bankd worker thread
216 ***********************************************************************/
217
Harald Welte8d858292018-08-15 23:36:46 +0200218struct value_string worker_state_names[] = {
219 { BW_ST_INIT, "INIT" },
220 { BW_ST_ACCEPTING, "ACCEPTING" },
221 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
222 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200223 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200224 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
225 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
226 { 0, NULL }
227};
228
Harald Welteceb3e682018-08-16 14:47:11 +0200229#define LOGW(w, fmt, args...) \
230 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
231 __FILE__, __LINE__, ## args)
232
Harald Welteaf614732018-08-17 22:10:05 +0200233#define PCSC_ERROR(w, rv, text) \
234if (rv != SCARD_S_SUCCESS) { \
235 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
236 goto end; \
237} else { \
Harald Welte7b41d9c2018-10-03 23:15:10 +0200238 LOGW((w), ": OK\n"); \
Harald Welteaf614732018-08-17 22:10:05 +0200239}
240
Harald Welte8d858292018-08-15 23:36:46 +0200241static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
242{
243 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
244 worker->state = new_state;
Harald Welte150d6d62018-10-03 23:07:47 +0200245 worker->timeout = 0;
246}
247
248static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state,
249 unsigned int timeout_secs)
250{
251 LOGW(worker, "Changing state to %s (timeout=%u)\n",
252 get_value_string(worker_state_names, new_state), timeout_secs);
253 worker->state = new_state;
254 worker->timeout = timeout_secs;
Harald Welte8d858292018-08-15 23:36:46 +0200255}
Harald Welte77911b02018-08-14 23:47:30 +0200256
257static void worker_cleanup(void *arg)
258{
259 struct bankd_worker *worker = (struct bankd_worker *) arg;
260 struct bankd *bankd = worker->bankd;
261
262 /* FIXME: should we still do this? in the thread ?!? */
263 pthread_mutex_lock(&bankd->workers_mutex);
264 llist_del(&worker->list);
265 talloc_free(worker); /* FIXME: is this safe? */
266 pthread_mutex_unlock(&bankd->workers_mutex);
267}
268
269
Harald Welteaf614732018-08-17 22:10:05 +0200270static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200271{
Harald Welteaf614732018-08-17 22:10:05 +0200272 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200273
Harald Welte150d6d62018-10-03 23:07:47 +0200274 OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED);
275
Harald Welte694df832018-10-03 22:47:52 +0200276 if (!worker->reader.name) {
277 /* resolve PC/SC reader name from slot_id -> name map */
278 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
279 if (!worker->reader.name) {
280 LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n",
281 worker->slot.bank_id, worker->slot.slot_nr);
282 rc = -1;
283 goto end;
284 }
285 }
Harald Welte45c948c2018-09-23 19:26:52 +0200286 OSMO_ASSERT(worker->reader.name);
287
Harald Welte694df832018-10-03 22:47:52 +0200288 if (!worker->reader.pcsc.hContext) {
289 LOGW(worker, "Attempting to open PC/SC context\n");
290 /* The PC/SC context must be created inside the thread where we'll later use it */
291 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
292 PCSC_ERROR(worker, rc, "SCardEstablishContext")
293 }
Harald Welte45c948c2018-09-23 19:26:52 +0200294
Harald Welte694df832018-10-03 22:47:52 +0200295 if (!worker->reader.pcsc.hCard) {
296 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
297 DWORD dwActiveProtocol;
298 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
299 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
300 PCSC_ERROR(worker, rc, "SCardConnect")
301 }
Harald Welte77911b02018-08-14 23:47:30 +0200302
Harald Welte57593f02018-09-23 19:30:31 +0200303 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
Harald Welte150d6d62018-10-03 23:07:47 +0200304 /* FIXME: notify client about this state change */
Harald Welte57593f02018-09-23 19:30:31 +0200305
Harald Welteaf614732018-08-17 22:10:05 +0200306 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200307end:
Harald Welteaf614732018-08-17 22:10:05 +0200308 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200309}
Harald Welte77911b02018-08-14 23:47:30 +0200310
311
312static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
313{
314 struct ipaccess_head *hh;
315 uint16_t len;
316 int needed, rc;
317
318 if (buf_size < sizeof(*hh))
319 return -1;
320
321 hh = (struct ipaccess_head *) buf;
322
323 /* 1) blocking read from the socket (IPA header) */
324 rc = read(fd, buf, sizeof(*hh));
325 if (rc < sizeof(*hh))
326 return -2;
327
328 len = ntohs(hh->len);
329 needed = len; //- sizeof(*hh);
330
331 /* 2) blocking read from the socket (payload) */
332 rc = read(fd, buf+sizeof(*hh), needed);
333 if (rc < needed)
334 return -3;
335
336 return len;
337}
338
Harald Welte796a7492018-09-23 19:31:28 +0200339static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu)
340{
341 struct msgb *msg = rspro_enc_msg(pdu);
342 int rc;
343
344 if (!msg) {
345 LOGW(worker, "error encoding RSPRO\n");
346 return -1;
347 }
348
Harald Weltefd471192018-09-24 14:51:14 +0200349 msg->l2h = msg->data;
Harald Welte796a7492018-09-23 19:31:28 +0200350 /* prepend the header */
351 ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO);
Harald Weltefd471192018-09-24 14:51:14 +0200352 ipa_prepend_header(msg, IPAC_PROTO_OSMO);
Harald Welte796a7492018-09-23 19:31:28 +0200353
354 /* actually send it through the socket */
355 rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg));
356 if (rc == msgb_length(msg))
357 rc = 0;
358 else {
359 LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg));
360 rc = -1;
361 }
362
363 msgb_free(msg);
364
365 return rc;
366}
367
Harald Welte150d6d62018-10-03 23:07:47 +0200368/* attempt to obtain slot-map */
369static int worker_try_slotmap(struct bankd_worker *worker)
370{
Harald Weltecbd18962019-03-03 19:02:38 +0100371 struct slot_mapping *slmap;
Harald Welte150d6d62018-10-03 23:07:47 +0200372
Harald Weltecbd18962019-03-03 19:02:38 +0100373 slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot);
Harald Welte150d6d62018-10-03 23:07:47 +0200374 if (!slmap) {
375 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
376 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
377 /* check in 10s if the map has been installed meanwhile by main thread */
378 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10);
379 return -1;
380 } else {
381 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
382 slmap->client.client_id, slmap->client.slot_nr,
383 slmap->bank.bank_id, slmap->bank.slot_nr);
384 worker->slot = slmap->bank;
385 worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10);
386 return worker_open_card(worker);
387 }
388}
389
390
Harald Weltecce2aad2018-08-16 14:44:37 +0200391static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
392{
Harald Welteaf614732018-08-17 22:10:05 +0200393 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
Harald Welte3e689872018-09-24 14:52:56 +0200394 e_ResultCode res;
395 RsproPDU_t *resp;
Harald Welteaf614732018-08-17 22:10:05 +0200396
Harald Weltecce2aad2018-08-16 14:44:37 +0200397 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
398
Harald Weltecce2aad2018-08-16 14:44:37 +0200399 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
400 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
401 /* FIXME: store somewhere? */
402
403 if (worker->state != BW_ST_CONN_WAIT_ID) {
404 LOGW(worker, "Unexpected connectClientReq\n");
405 return -102;
406 }
407
Harald Welte371d0262018-08-16 15:23:58 +0200408 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200409 LOGW(worker, "missing clientID, aborting\n");
410 return -103;
411 }
Harald Welte371d0262018-08-16 15:23:58 +0200412 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
413 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200414 worker_set_state(worker, BW_ST_CONN_CLIENT);
415
Harald Welte150d6d62018-10-03 23:07:47 +0200416 if (worker_try_slotmap(worker) >= 0)
417 res = ResultCode_ok;
418 else
Harald Welte3e689872018-09-24 14:52:56 +0200419 res = ResultCode_cardNotPresent;
Harald Weltecce2aad2018-08-16 14:44:37 +0200420
Harald Welte3e689872018-09-24 14:52:56 +0200421 resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res);
422 return worker_send_rspro(worker, resp);
Harald Weltecce2aad2018-08-16 14:44:37 +0200423}
424
Harald Welte796a7492018-09-23 19:31:28 +0200425static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu)
426{
427 const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard;
428 const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0;
429 SCARD_IO_REQUEST pioRecvPci;
430 uint8_t rx_buf[1024];
431 DWORD rx_buf_len = sizeof(rx_buf);
432 RsproPDU_t *pdu_resp;
433 long rc;
434
435 LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size));
436
437 if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) {
438 LOGW(worker, "Unexpected tpduModemToCaard\n");
439 return -104;
440 }
441
442 /* FIXME: Validate that toBankSlot / fromClientSlot match our expectations */
443
444 rc = SCardTransmit(worker->reader.pcsc.hCard,
445 pioSendPci, mdm2sim->data.buf, mdm2sim->data.size,
446 &pioRecvPci, rx_buf, &rx_buf_len);
447 PCSC_ERROR(worker, rc, "SCardTransmit");
448
449 /* encode response PDU and send it */
450 pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot,
451 rx_buf, rx_buf_len);
452 worker_send_rspro(worker, pdu_resp);
453
454 return 0;
455end:
456 return rc;
457}
458
Harald Welte77911b02018-08-14 23:47:30 +0200459/* handle one incoming RSPRO message from a client inside a worker thread */
460static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
461{
Harald Weltecce2aad2018-08-16 14:44:37 +0200462 int rc = -100;
463
Harald Welte77911b02018-08-14 23:47:30 +0200464 switch (pdu->msg.present) {
465 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200466 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200467 break;
468 case RsproPDUchoice_PR_tpduModemToCard:
Harald Welte796a7492018-09-23 19:31:28 +0200469 rc = worker_handle_tpduModemToCard(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200470 break;
471 case RsproPDUchoice_PR_clientSlotStatusInd:
472 /* FIXME */
473 break;
474 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200475 rc = -101;
476 break;
Harald Welte77911b02018-08-14 23:47:30 +0200477 }
478
Harald Weltecce2aad2018-08-16 14:44:37 +0200479 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200480}
481
Harald Welte694df832018-10-03 22:47:52 +0200482static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs)
483{
484 struct timeval tout = { timeout_secs, 0 };
485 fd_set readset;
486
487 FD_ZERO(&readset);
488 FD_SET(fd, &readset);
489 return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL);
490}
491
Harald Welte77911b02018-08-14 23:47:30 +0200492/* body of the main transceive loop */
493static int worker_transceive_loop(struct bankd_worker *worker)
494{
495 struct ipaccess_head *hh;
496 struct ipaccess_head_ext *hh_ext;
497 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
498 asn_dec_rval_t rval;
499 int data_len, rc;
Harald Welte9ebbacc2018-09-24 17:43:39 +0200500 RsproPDU_t *pdu = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200501
Harald Welte694df832018-10-03 22:47:52 +0200502 rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout);
503 if (rc == 0) {
504 /* TIMEOUT case */
Harald Welte150d6d62018-10-03 23:07:47 +0200505 switch (worker->state) {
506 case BW_ST_CONN_CLIENT_WAIT_MAP:
507 /* re-check if mapping exists meanwhile? */
508 worker_try_slotmap(worker);
509 break;
510 case BW_ST_CONN_CLIENT_MAPPED:
511 /* re-check if reader/card can be opened meanwhile? */
512 worker_open_card(worker);
513 break;
514 default:
515 OSMO_ASSERT(0);
516 }
517 /* return early, so we do another select rather than the blocking read below */
Harald Welte694df832018-10-03 22:47:52 +0200518 return 0;
519 };
520
Harald Welte77911b02018-08-14 23:47:30 +0200521 /* 1) blocking read of entire IPA message from the socket */
522 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
523 if (rc < 0)
524 return rc;
525 data_len = rc;
526
527 hh = (struct ipaccess_head *) buf;
Harald Welte5a3613a2018-10-11 12:56:21 +0200528 if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) {
Harald Weltee1176cf2018-09-24 14:54:58 +0200529 LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200530 return -4;
Harald Weltee1176cf2018-09-24 14:54:58 +0200531 }
Harald Welte77911b02018-08-14 23:47:30 +0200532
Harald Welte5a3613a2018-10-11 12:56:21 +0200533 if (hh->proto == IPAC_PROTO_IPACCESS) {
534 LOGW(worker, "IPA CCM not implemented yet\n");
535 return 0;
536 }
537
Harald Welte77911b02018-08-14 23:47:30 +0200538 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
Harald Weltee1176cf2018-09-24 14:54:58 +0200539 if (data_len < sizeof(*hh_ext)) {
540 LOGW(worker, "Received short message\n");
Harald Welte77911b02018-08-14 23:47:30 +0200541 return -5;
Harald Weltee1176cf2018-09-24 14:54:58 +0200542 }
Harald Welte77911b02018-08-14 23:47:30 +0200543 data_len -= sizeof(*hh_ext);
Harald Weltee1176cf2018-09-24 14:54:58 +0200544 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) {
545 LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto);
Harald Welte77911b02018-08-14 23:47:30 +0200546 return -6;
Harald Weltee1176cf2018-09-24 14:54:58 +0200547 }
Harald Welte77911b02018-08-14 23:47:30 +0200548
549 /* 2) ASN1 BER decode of the message */
550 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
Harald Weltee1176cf2018-09-24 14:54:58 +0200551 if (rval.code != RC_OK) {
552 LOGW(worker, "Error during BER decode of RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200553 return -7;
Harald Weltee1176cf2018-09-24 14:54:58 +0200554 }
Harald Welte77911b02018-08-14 23:47:30 +0200555
556 /* 3) handling of the message, possibly resulting in PCSC commands */
557 rc = worker_handle_rspro(worker, pdu);
558 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
Harald Weltee1176cf2018-09-24 14:54:58 +0200559 if (rc < 0) {
560 LOGW(worker, "Error handling RSPRO\n");
Harald Welte77911b02018-08-14 23:47:30 +0200561 return rc;
Harald Weltee1176cf2018-09-24 14:54:58 +0200562 }
Harald Welte77911b02018-08-14 23:47:30 +0200563
564 /* everything OK if we reach here */
565 return 0;
566}
567
Harald Welted6dfb8c2018-08-16 14:46:53 +0200568/* obtain an ascii representation of the client IP/port */
569static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
570{
571 char hostbuf[32], portbuf[32];
572 int rc;
573
574 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
575 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
576 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
577 if (rc != 0) {
578 out[0] = '\0';
579 return -1;
580 }
581 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
582 return 0;
583}
584
Harald Welte77911b02018-08-14 23:47:30 +0200585/* worker thread main function */
586static void *worker_main(void *arg)
587{
588 struct bankd_worker *worker = (struct bankd_worker *) arg;
589 void *top_ctx;
590 int rc;
Harald Welte31c9eca2018-10-03 21:03:34 +0200591 char worker_name[32];
592
593 /* set the thread name */
594 snprintf(worker_name, sizeof(worker_name), "bankd-worker(%u)", worker->num);
595 pthread_setname_np(pthread_self(), worker_name);
Harald Welte77911b02018-08-14 23:47:30 +0200596
Harald Welte8d858292018-08-15 23:36:46 +0200597 worker_set_state(worker, BW_ST_INIT);
598
Harald Welte77911b02018-08-14 23:47:30 +0200599 /* not permitted in multithreaded environment */
600 talloc_disable_null_tracking();
601 top_ctx = talloc_named_const(NULL, 0, "top");
602 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
603
604 /* push cleanup helper */
605 pthread_cleanup_push(&worker_cleanup, worker);
606
607 /* we continuously perform the same loop here, recycling the worker thread
608 * once the client connection is gone or we have some trouble with the card/reader */
609 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200610 char buf[128];
611
Harald Welte77911b02018-08-14 23:47:30 +0200612 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
613
Harald Welte8d858292018-08-15 23:36:46 +0200614 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200615 /* first wait for an incoming TCP connection */
616 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
617 &worker->client.peer_addr_len);
618 if (rc < 0) {
619 continue;
620 }
621 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200622 worker_client_addrstr(buf, sizeof(buf), worker);
623 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200624 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200625
626 /* run the main worker transceive loop body until there was some error */
627 while (1) {
628 rc = worker_transceive_loop(worker);
629 if (rc < 0)
630 break;
631 }
632
Harald Welted6dfb8c2018-08-16 14:46:53 +0200633 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
634
Harald Welte77911b02018-08-14 23:47:30 +0200635 /* clean-up: reset to sane state */
636 if (worker->reader.pcsc.hCard) {
637 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
638 worker->reader.pcsc.hCard = 0;
639 }
640 if (worker->reader.pcsc.hContext) {
641 SCardReleaseContext(worker->reader.pcsc.hContext);
642 worker->reader.pcsc.hContext = 0;
643 }
Harald Welte694df832018-10-03 22:47:52 +0200644 if (worker->reader.name)
645 worker->reader.name = NULL;
Harald Welte77911b02018-08-14 23:47:30 +0200646 if (worker->client.fd >= 0)
647 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200648 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200649 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200650 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200651 }
652
653 pthread_cleanup_pop(1);
654 talloc_free(top_ctx);
655 pthread_exit(NULL);
656}