blob: 60ba2851450cc9d644fbafa58f46e7e25f1a1dbf [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
Harald Welte48865c22018-09-23 19:30:07 +0200173 DWORD dwActiveProtocol;
Harald Welte77911b02018-08-14 23:47:30 +0200174 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
Harald Welte48865c22018-09-23 19:30:07 +0200175 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol);
Harald Welteaf614732018-08-17 22:10:05 +0200176 PCSC_ERROR(worker, rc, "SCardConnect")
Harald Welte77911b02018-08-14 23:47:30 +0200177
Harald Welte57593f02018-09-23 19:30:31 +0200178 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD);
179
Harald Welteaf614732018-08-17 22:10:05 +0200180 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200181end:
Harald Welteaf614732018-08-17 22:10:05 +0200182 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200183}
Harald Welte77911b02018-08-14 23:47:30 +0200184
185
186static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
187{
188 struct ipaccess_head *hh;
189 uint16_t len;
190 int needed, rc;
191
192 if (buf_size < sizeof(*hh))
193 return -1;
194
195 hh = (struct ipaccess_head *) buf;
196
197 /* 1) blocking read from the socket (IPA header) */
198 rc = read(fd, buf, sizeof(*hh));
199 if (rc < sizeof(*hh))
200 return -2;
201
202 len = ntohs(hh->len);
203 needed = len; //- sizeof(*hh);
204
205 /* 2) blocking read from the socket (payload) */
206 rc = read(fd, buf+sizeof(*hh), needed);
207 if (rc < needed)
208 return -3;
209
210 return len;
211}
212
Harald Weltecce2aad2018-08-16 14:44:37 +0200213static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
214{
Harald Welteaf614732018-08-17 22:10:05 +0200215 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
216 struct bankd_slot_mapping *slmap;
217
Harald Weltecce2aad2018-08-16 14:44:37 +0200218 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
219
Harald Weltecce2aad2018-08-16 14:44:37 +0200220
221 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
222 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
223 /* FIXME: store somewhere? */
224
225 if (worker->state != BW_ST_CONN_WAIT_ID) {
226 LOGW(worker, "Unexpected connectClientReq\n");
227 return -102;
228 }
229
Harald Welte371d0262018-08-16 15:23:58 +0200230 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200231 LOGW(worker, "missing clientID, aborting\n");
232 return -103;
233 }
Harald Welte371d0262018-08-16 15:23:58 +0200234 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
235 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200236 worker_set_state(worker, BW_ST_CONN_CLIENT);
237
Harald Welteaf614732018-08-17 22:10:05 +0200238 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
239 if (!slmap) {
240 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
241 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
242 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
243 /* FIXME: how to update the map in case a map is installed later */
244 } else {
245 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
246 slmap->client.client_id, slmap->client.slot_nr,
247 slmap->bank.bank_id, slmap->bank.slot_nr);
248 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
249 /* FIXME: actually open the mapped reader/card/slot */
250 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200251
252 return 0;
253}
254
Harald Welte77911b02018-08-14 23:47:30 +0200255/* handle one incoming RSPRO message from a client inside a worker thread */
256static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
257{
Harald Weltecce2aad2018-08-16 14:44:37 +0200258 int rc = -100;
259
Harald Welte77911b02018-08-14 23:47:30 +0200260 switch (pdu->msg.present) {
261 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200262 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200263 break;
264 case RsproPDUchoice_PR_tpduModemToCard:
265 /* FIXME */
266 break;
267 case RsproPDUchoice_PR_clientSlotStatusInd:
268 /* FIXME */
269 break;
270 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200271 rc = -101;
272 break;
Harald Welte77911b02018-08-14 23:47:30 +0200273 }
274
Harald Weltecce2aad2018-08-16 14:44:37 +0200275 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200276}
277
278/* body of the main transceive loop */
279static int worker_transceive_loop(struct bankd_worker *worker)
280{
281 struct ipaccess_head *hh;
282 struct ipaccess_head_ext *hh_ext;
283 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
284 asn_dec_rval_t rval;
285 int data_len, rc;
286 RsproPDU_t *pdu;
287
288 /* 1) blocking read of entire IPA message from the socket */
289 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
290 if (rc < 0)
291 return rc;
292 data_len = rc;
293
294 hh = (struct ipaccess_head *) buf;
295 if (hh->proto != IPAC_PROTO_OSMO)
296 return -4;
297
298 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
299 if (data_len < sizeof(*hh_ext))
300 return -5;
301 data_len -= sizeof(*hh_ext);
302 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
303 return -6;
304
305 /* 2) ASN1 BER decode of the message */
306 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
307 if (rval.code != RC_OK)
308 return -7;
309
310 /* 3) handling of the message, possibly resulting in PCSC commands */
311 rc = worker_handle_rspro(worker, pdu);
312 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
313 if (rc < 0)
314 return rc;
315
316 /* everything OK if we reach here */
317 return 0;
318}
319
Harald Welted6dfb8c2018-08-16 14:46:53 +0200320/* obtain an ascii representation of the client IP/port */
321static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
322{
323 char hostbuf[32], portbuf[32];
324 int rc;
325
326 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
327 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
328 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
329 if (rc != 0) {
330 out[0] = '\0';
331 return -1;
332 }
333 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
334 return 0;
335}
336
Harald Welte77911b02018-08-14 23:47:30 +0200337/* worker thread main function */
338static void *worker_main(void *arg)
339{
340 struct bankd_worker *worker = (struct bankd_worker *) arg;
341 void *top_ctx;
342 int rc;
343
Harald Welte8d858292018-08-15 23:36:46 +0200344 worker_set_state(worker, BW_ST_INIT);
345
Harald Welte77911b02018-08-14 23:47:30 +0200346 /* not permitted in multithreaded environment */
347 talloc_disable_null_tracking();
348 top_ctx = talloc_named_const(NULL, 0, "top");
349 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
350
351 /* push cleanup helper */
352 pthread_cleanup_push(&worker_cleanup, worker);
353
354 /* we continuously perform the same loop here, recycling the worker thread
355 * once the client connection is gone or we have some trouble with the card/reader */
356 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200357 char buf[128];
358
Harald Welte77911b02018-08-14 23:47:30 +0200359 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
360
Harald Welte8d858292018-08-15 23:36:46 +0200361 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200362 /* first wait for an incoming TCP connection */
363 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
364 &worker->client.peer_addr_len);
365 if (rc < 0) {
366 continue;
367 }
368 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200369 worker_client_addrstr(buf, sizeof(buf), worker);
370 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200371 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200372
373 /* run the main worker transceive loop body until there was some error */
374 while (1) {
375 rc = worker_transceive_loop(worker);
376 if (rc < 0)
377 break;
378 }
379
Harald Welted6dfb8c2018-08-16 14:46:53 +0200380 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
381
Harald Welte77911b02018-08-14 23:47:30 +0200382 /* clean-up: reset to sane state */
383 if (worker->reader.pcsc.hCard) {
384 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
385 worker->reader.pcsc.hCard = 0;
386 }
387 if (worker->reader.pcsc.hContext) {
388 SCardReleaseContext(worker->reader.pcsc.hContext);
389 worker->reader.pcsc.hContext = 0;
390 }
391 if (worker->client.fd >= 0)
392 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200393 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200394 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200395 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200396 }
397
398 pthread_cleanup_pop(1);
399 talloc_free(top_ctx);
400 pthread_exit(NULL);
401}