blob: 7cb46d2448756f68f4bc684f956ca775323629a8 [file] [log] [blame]
Harald Welte77911b02018-08-14 23:47:30 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <unistd.h>
5
6#include <pthread.h>
7
8#include <wintypes.h>
9#include <winscard.h>
10#include <pcsclite.h>
11
Harald Welted6dfb8c2018-08-16 14:46:53 +020012#include <sys/socket.h>
13#include <netdb.h>
14
Harald Welte12534e72018-08-15 23:37:29 +020015#include <osmocom/core/socket.h>
Harald Welte77911b02018-08-14 23:47:30 +020016#include <osmocom/core/linuxlist.h>
17
18#include <osmocom/gsm/ipa.h>
19#include <osmocom/gsm/protocol/ipaccess.h>
20
21#include <asn1c/asn_application.h>
22#include <osmocom/rspro/RsproPDU.h>
23
24#include "bankd.h"
25
26__thread void *talloc_asn1_ctx;
27
28static void *worker_main(void *arg);
29
30/***********************************************************************
31* bankd core / main thread
32***********************************************************************/
33
34static void bankd_init(struct bankd *bankd)
35{
36 /* intialize members of 'bankd' */
37 INIT_LLIST_HEAD(&bankd->slot_mappings);
38 pthread_rwlock_init(&bankd->slot_mappings_rwlock, NULL);
39 INIT_LLIST_HEAD(&bankd->workers);
40 pthread_mutex_init(&bankd->workers_mutex, NULL);
Harald Welte45c948c2018-09-23 19:26:52 +020041
42 /* Np lock or mutex required for the pcsc_slot_names list, as this is only
43 * read once during bankd initialization, when the worker threads haven't
44 * started yet */
45 INIT_LLIST_HEAD(&bankd->pcsc_slot_names);
46 OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0);
Harald Welte77911b02018-08-14 23:47:30 +020047}
48
49/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020050static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020051{
52 struct bankd_worker *worker;
53 int rc;
54
55 worker = talloc_zero(bankd, struct bankd_worker);
56 if (!worker)
57 return NULL;
58
59 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020060 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020061
62 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
63
64 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
65 if (rc != 0) {
66 talloc_free(worker);
67 return NULL;
68 }
69
70 pthread_mutex_lock(&bankd->workers_mutex);
71 llist_add_tail(&worker->list, &bankd->workers);
72 pthread_mutex_unlock(&bankd->workers_mutex);
73
74 return worker;
75}
76
77static bool terminate = false;
78
79int main(int argc, char **argv)
80{
81 struct bankd *bankd = talloc_zero(NULL, struct bankd);
82 int i, rc;
83
84 OSMO_ASSERT(bankd);
85 bankd_init(bankd);
86
Harald Welte12534e72018-08-15 23:37:29 +020087 /* create listening socket */
88 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
89 if (rc < 0)
90 exit(1);
91 bankd->accept_fd = rc;
92
93 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +020094 for (i = 0; i < 10; i++) {
95 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +020096 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +020097 if (!w)
98 exit(21);
99 }
100
101 while (1) {
102 if (terminate)
103 break;
Harald Welte7f684a02018-08-16 14:43:50 +0200104 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +0200105 }
106
107 talloc_free(bankd);
108 exit(0);
109}
110
111
112
113/***********************************************************************
114 * bankd worker thread
115 ***********************************************************************/
116
Harald Welte8d858292018-08-15 23:36:46 +0200117struct value_string worker_state_names[] = {
118 { BW_ST_INIT, "INIT" },
119 { BW_ST_ACCEPTING, "ACCEPTING" },
120 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
121 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200122 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200123 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
124 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
125 { 0, NULL }
126};
127
Harald Welteceb3e682018-08-16 14:47:11 +0200128#define LOGW(w, fmt, args...) \
129 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
130 __FILE__, __LINE__, ## args)
131
Harald Welteaf614732018-08-17 22:10:05 +0200132#define PCSC_ERROR(w, rv, text) \
133if (rv != SCARD_S_SUCCESS) { \
134 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
135 goto end; \
136} else { \
137 LOGW((w), ": OK\n\n"); \
138}
139
Harald Welte8d858292018-08-15 23:36:46 +0200140static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
141{
142 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
143 worker->state = new_state;
144}
Harald Welte77911b02018-08-14 23:47:30 +0200145
146static void worker_cleanup(void *arg)
147{
148 struct bankd_worker *worker = (struct bankd_worker *) arg;
149 struct bankd *bankd = worker->bankd;
150
151 /* FIXME: should we still do this? in the thread ?!? */
152 pthread_mutex_lock(&bankd->workers_mutex);
153 llist_del(&worker->list);
154 talloc_free(worker); /* FIXME: is this safe? */
155 pthread_mutex_unlock(&bankd->workers_mutex);
156}
157
158
Harald Welteaf614732018-08-17 22:10:05 +0200159static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200160{
Harald Welteaf614732018-08-17 22:10:05 +0200161 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200162
Harald Welte45c948c2018-09-23 19:26:52 +0200163 /* resolve PC/SC reader name from slot_id -> name map */
164 worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot);
165 OSMO_ASSERT(worker->reader.name);
166
167 LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name);
168
Harald Welte77911b02018-08-14 23:47:30 +0200169 /* The PC/SC context must be created inside the thread where we'll later use it */
170 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
Harald Welteaf614732018-08-17 22:10:05 +0200171 PCSC_ERROR(worker, rc, "SCardEstablishContext")
Harald Welte77911b02018-08-14 23:47:30 +0200172
173 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
174 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
Harald Welteaf614732018-08-17 22:10:05 +0200175 PCSC_ERROR(worker, rc, "SCardConnect")
Harald Welte77911b02018-08-14 23:47:30 +0200176
Harald Welteaf614732018-08-17 22:10:05 +0200177 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200178end:
Harald Welteaf614732018-08-17 22:10:05 +0200179 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200180}
Harald Welte77911b02018-08-14 23:47:30 +0200181
182
183static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
184{
185 struct ipaccess_head *hh;
186 uint16_t len;
187 int needed, rc;
188
189 if (buf_size < sizeof(*hh))
190 return -1;
191
192 hh = (struct ipaccess_head *) buf;
193
194 /* 1) blocking read from the socket (IPA header) */
195 rc = read(fd, buf, sizeof(*hh));
196 if (rc < sizeof(*hh))
197 return -2;
198
199 len = ntohs(hh->len);
200 needed = len; //- sizeof(*hh);
201
202 /* 2) blocking read from the socket (payload) */
203 rc = read(fd, buf+sizeof(*hh), needed);
204 if (rc < needed)
205 return -3;
206
207 return len;
208}
209
Harald Weltecce2aad2018-08-16 14:44:37 +0200210static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
211{
Harald Welteaf614732018-08-17 22:10:05 +0200212 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
213 struct bankd_slot_mapping *slmap;
214
Harald Weltecce2aad2018-08-16 14:44:37 +0200215 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
216
Harald Weltecce2aad2018-08-16 14:44:37 +0200217
218 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
219 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
220 /* FIXME: store somewhere? */
221
222 if (worker->state != BW_ST_CONN_WAIT_ID) {
223 LOGW(worker, "Unexpected connectClientReq\n");
224 return -102;
225 }
226
Harald Welte371d0262018-08-16 15:23:58 +0200227 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200228 LOGW(worker, "missing clientID, aborting\n");
229 return -103;
230 }
Harald Welte371d0262018-08-16 15:23:58 +0200231 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
232 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200233 worker_set_state(worker, BW_ST_CONN_CLIENT);
234
Harald Welteaf614732018-08-17 22:10:05 +0200235 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
236 if (!slmap) {
237 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
238 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
239 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
240 /* FIXME: how to update the map in case a map is installed later */
241 } else {
242 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
243 slmap->client.client_id, slmap->client.slot_nr,
244 slmap->bank.bank_id, slmap->bank.slot_nr);
245 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
246 /* FIXME: actually open the mapped reader/card/slot */
247 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200248
249 return 0;
250}
251
Harald Welte77911b02018-08-14 23:47:30 +0200252/* handle one incoming RSPRO message from a client inside a worker thread */
253static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
254{
Harald Weltecce2aad2018-08-16 14:44:37 +0200255 int rc = -100;
256
Harald Welte77911b02018-08-14 23:47:30 +0200257 switch (pdu->msg.present) {
258 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200259 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200260 break;
261 case RsproPDUchoice_PR_tpduModemToCard:
262 /* FIXME */
263 break;
264 case RsproPDUchoice_PR_clientSlotStatusInd:
265 /* FIXME */
266 break;
267 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200268 rc = -101;
269 break;
Harald Welte77911b02018-08-14 23:47:30 +0200270 }
271
Harald Weltecce2aad2018-08-16 14:44:37 +0200272 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200273}
274
275/* body of the main transceive loop */
276static int worker_transceive_loop(struct bankd_worker *worker)
277{
278 struct ipaccess_head *hh;
279 struct ipaccess_head_ext *hh_ext;
280 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
281 asn_dec_rval_t rval;
282 int data_len, rc;
283 RsproPDU_t *pdu;
284
285 /* 1) blocking read of entire IPA message from the socket */
286 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
287 if (rc < 0)
288 return rc;
289 data_len = rc;
290
291 hh = (struct ipaccess_head *) buf;
292 if (hh->proto != IPAC_PROTO_OSMO)
293 return -4;
294
295 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
296 if (data_len < sizeof(*hh_ext))
297 return -5;
298 data_len -= sizeof(*hh_ext);
299 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
300 return -6;
301
302 /* 2) ASN1 BER decode of the message */
303 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
304 if (rval.code != RC_OK)
305 return -7;
306
307 /* 3) handling of the message, possibly resulting in PCSC commands */
308 rc = worker_handle_rspro(worker, pdu);
309 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
310 if (rc < 0)
311 return rc;
312
313 /* everything OK if we reach here */
314 return 0;
315}
316
Harald Welted6dfb8c2018-08-16 14:46:53 +0200317/* obtain an ascii representation of the client IP/port */
318static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
319{
320 char hostbuf[32], portbuf[32];
321 int rc;
322
323 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
324 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
325 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
326 if (rc != 0) {
327 out[0] = '\0';
328 return -1;
329 }
330 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
331 return 0;
332}
333
Harald Welte77911b02018-08-14 23:47:30 +0200334/* worker thread main function */
335static void *worker_main(void *arg)
336{
337 struct bankd_worker *worker = (struct bankd_worker *) arg;
338 void *top_ctx;
339 int rc;
340
Harald Welte8d858292018-08-15 23:36:46 +0200341 worker_set_state(worker, BW_ST_INIT);
342
Harald Welte77911b02018-08-14 23:47:30 +0200343 /* not permitted in multithreaded environment */
344 talloc_disable_null_tracking();
345 top_ctx = talloc_named_const(NULL, 0, "top");
346 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
347
348 /* push cleanup helper */
349 pthread_cleanup_push(&worker_cleanup, worker);
350
351 /* we continuously perform the same loop here, recycling the worker thread
352 * once the client connection is gone or we have some trouble with the card/reader */
353 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200354 char buf[128];
355
Harald Welte77911b02018-08-14 23:47:30 +0200356 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
357
Harald Welte8d858292018-08-15 23:36:46 +0200358 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200359 /* first wait for an incoming TCP connection */
360 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
361 &worker->client.peer_addr_len);
362 if (rc < 0) {
363 continue;
364 }
365 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200366 worker_client_addrstr(buf, sizeof(buf), worker);
367 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200368 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200369
370 /* run the main worker transceive loop body until there was some error */
371 while (1) {
372 rc = worker_transceive_loop(worker);
373 if (rc < 0)
374 break;
375 }
376
Harald Welted6dfb8c2018-08-16 14:46:53 +0200377 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
378
Harald Welte77911b02018-08-14 23:47:30 +0200379 /* clean-up: reset to sane state */
380 if (worker->reader.pcsc.hCard) {
381 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
382 worker->reader.pcsc.hCard = 0;
383 }
384 if (worker->reader.pcsc.hContext) {
385 SCardReleaseContext(worker->reader.pcsc.hContext);
386 worker->reader.pcsc.hContext = 0;
387 }
388 if (worker->client.fd >= 0)
389 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200390 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200391 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200392 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200393 }
394
395 pthread_cleanup_pop(1);
396 talloc_free(top_ctx);
397 pthread_exit(NULL);
398}