blob: 96eee0ef143522077d8999bd01a7f8bfbeb353e8 [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);
41}
42
43/* create + start a new bankd_worker thread */
Harald Welte8d858292018-08-15 23:36:46 +020044static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i)
Harald Welte77911b02018-08-14 23:47:30 +020045{
46 struct bankd_worker *worker;
47 int rc;
48
49 worker = talloc_zero(bankd, struct bankd_worker);
50 if (!worker)
51 return NULL;
52
53 worker->bankd = bankd;
Harald Welte8d858292018-08-15 23:36:46 +020054 worker->num = i;
Harald Welte77911b02018-08-14 23:47:30 +020055
56 /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */
57
58 rc = pthread_create(&worker->thread, NULL, worker_main, worker);
59 if (rc != 0) {
60 talloc_free(worker);
61 return NULL;
62 }
63
64 pthread_mutex_lock(&bankd->workers_mutex);
65 llist_add_tail(&worker->list, &bankd->workers);
66 pthread_mutex_unlock(&bankd->workers_mutex);
67
68 return worker;
69}
70
71static bool terminate = false;
72
73int main(int argc, char **argv)
74{
75 struct bankd *bankd = talloc_zero(NULL, struct bankd);
76 int i, rc;
77
78 OSMO_ASSERT(bankd);
79 bankd_init(bankd);
80
Harald Welte12534e72018-08-15 23:37:29 +020081 /* create listening socket */
82 rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND);
83 if (rc < 0)
84 exit(1);
85 bankd->accept_fd = rc;
86
87 /* create worker threads. FIXME: one per reader/slot! */
Harald Welte77911b02018-08-14 23:47:30 +020088 for (i = 0; i < 10; i++) {
89 struct bankd_worker *w;
Harald Welte8d858292018-08-15 23:36:46 +020090 w = bankd_create_worker(bankd, i);
Harald Welte77911b02018-08-14 23:47:30 +020091 if (!w)
92 exit(21);
93 }
94
95 while (1) {
96 if (terminate)
97 break;
Harald Welte7f684a02018-08-16 14:43:50 +020098 sleep(1);
Harald Welte77911b02018-08-14 23:47:30 +020099 }
100
101 talloc_free(bankd);
102 exit(0);
103}
104
105
106
107/***********************************************************************
108 * bankd worker thread
109 ***********************************************************************/
110
Harald Welte8d858292018-08-15 23:36:46 +0200111struct value_string worker_state_names[] = {
112 { BW_ST_INIT, "INIT" },
113 { BW_ST_ACCEPTING, "ACCEPTING" },
114 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
115 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
Harald Welteaf614732018-08-17 22:10:05 +0200116 { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" },
Harald Welte8d858292018-08-15 23:36:46 +0200117 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
118 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
119 { 0, NULL }
120};
121
Harald Welteceb3e682018-08-16 14:47:11 +0200122#define LOGW(w, fmt, args...) \
123 printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \
124 __FILE__, __LINE__, ## args)
125
Harald Welteaf614732018-08-17 22:10:05 +0200126#define PCSC_ERROR(w, rv, text) \
127if (rv != SCARD_S_SUCCESS) { \
128 LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
129 goto end; \
130} else { \
131 LOGW((w), ": OK\n\n"); \
132}
133
Harald Welte8d858292018-08-15 23:36:46 +0200134static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
135{
136 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
137 worker->state = new_state;
138}
Harald Welte77911b02018-08-14 23:47:30 +0200139
140static void worker_cleanup(void *arg)
141{
142 struct bankd_worker *worker = (struct bankd_worker *) arg;
143 struct bankd *bankd = worker->bankd;
144
145 /* FIXME: should we still do this? in the thread ?!? */
146 pthread_mutex_lock(&bankd->workers_mutex);
147 llist_del(&worker->list);
148 talloc_free(worker); /* FIXME: is this safe? */
149 pthread_mutex_unlock(&bankd->workers_mutex);
150}
151
152
Harald Welteaf614732018-08-17 22:10:05 +0200153static int worker_open_card(struct bankd_worker *worker)
Harald Welte77911b02018-08-14 23:47:30 +0200154{
Harald Welteaf614732018-08-17 22:10:05 +0200155 long rc;
Harald Welte77911b02018-08-14 23:47:30 +0200156
157 /* The PC/SC context must be created inside the thread where we'll later use it */
158 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
Harald Welteaf614732018-08-17 22:10:05 +0200159 PCSC_ERROR(worker, rc, "SCardEstablishContext")
Harald Welte77911b02018-08-14 23:47:30 +0200160
161 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
162 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
Harald Welteaf614732018-08-17 22:10:05 +0200163 PCSC_ERROR(worker, rc, "SCardConnect")
Harald Welte77911b02018-08-14 23:47:30 +0200164
Harald Welteaf614732018-08-17 22:10:05 +0200165 return 0;
Harald Welte77911b02018-08-14 23:47:30 +0200166end:
Harald Welteaf614732018-08-17 22:10:05 +0200167 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200168}
Harald Welte77911b02018-08-14 23:47:30 +0200169
170
171static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
172{
173 struct ipaccess_head *hh;
174 uint16_t len;
175 int needed, rc;
176
177 if (buf_size < sizeof(*hh))
178 return -1;
179
180 hh = (struct ipaccess_head *) buf;
181
182 /* 1) blocking read from the socket (IPA header) */
183 rc = read(fd, buf, sizeof(*hh));
184 if (rc < sizeof(*hh))
185 return -2;
186
187 len = ntohs(hh->len);
188 needed = len; //- sizeof(*hh);
189
190 /* 2) blocking read from the socket (payload) */
191 rc = read(fd, buf+sizeof(*hh), needed);
192 if (rc < needed)
193 return -3;
194
195 return len;
196}
197
Harald Weltecce2aad2018-08-16 14:44:37 +0200198static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
199{
Harald Welteaf614732018-08-17 22:10:05 +0200200 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
201 struct bankd_slot_mapping *slmap;
202
Harald Weltecce2aad2018-08-16 14:44:37 +0200203 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
204
Harald Weltecce2aad2018-08-16 14:44:37 +0200205
206 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
207 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
208 /* FIXME: store somewhere? */
209
210 if (worker->state != BW_ST_CONN_WAIT_ID) {
211 LOGW(worker, "Unexpected connectClientReq\n");
212 return -102;
213 }
214
Harald Welte371d0262018-08-16 15:23:58 +0200215 if (!pdu->msg.choice.connectClientReq.clientSlot) {
Harald Weltecce2aad2018-08-16 14:44:37 +0200216 LOGW(worker, "missing clientID, aborting\n");
217 return -103;
218 }
Harald Welte371d0262018-08-16 15:23:58 +0200219 worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId;
220 worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr;
Harald Weltecce2aad2018-08-16 14:44:37 +0200221 worker_set_state(worker, BW_ST_CONN_CLIENT);
222
Harald Welteaf614732018-08-17 22:10:05 +0200223 slmap = bankd_slotmap_by_client(worker->bankd, &worker->client.clslot);
224 if (!slmap) {
225 LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n",
226 worker->client.clslot.client_id, worker->client.clslot.slot_nr);
227 worker_set_state(worker, BW_ST_CONN_CLIENT_WAIT_MAP);
228 /* FIXME: how to update the map in case a map is installed later */
229 } else {
230 LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n",
231 slmap->client.client_id, slmap->client.slot_nr,
232 slmap->bank.bank_id, slmap->bank.slot_nr);
233 worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED);
234 /* FIXME: actually open the mapped reader/card/slot */
235 }
Harald Weltecce2aad2018-08-16 14:44:37 +0200236
237 return 0;
238}
239
Harald Welte77911b02018-08-14 23:47:30 +0200240/* handle one incoming RSPRO message from a client inside a worker thread */
241static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
242{
Harald Weltecce2aad2018-08-16 14:44:37 +0200243 int rc = -100;
244
Harald Welte77911b02018-08-14 23:47:30 +0200245 switch (pdu->msg.present) {
246 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200247 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200248 break;
249 case RsproPDUchoice_PR_tpduModemToCard:
250 /* FIXME */
251 break;
252 case RsproPDUchoice_PR_clientSlotStatusInd:
253 /* FIXME */
254 break;
255 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200256 rc = -101;
257 break;
Harald Welte77911b02018-08-14 23:47:30 +0200258 }
259
Harald Weltecce2aad2018-08-16 14:44:37 +0200260 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200261}
262
263/* body of the main transceive loop */
264static int worker_transceive_loop(struct bankd_worker *worker)
265{
266 struct ipaccess_head *hh;
267 struct ipaccess_head_ext *hh_ext;
268 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
269 asn_dec_rval_t rval;
270 int data_len, rc;
271 RsproPDU_t *pdu;
272
273 /* 1) blocking read of entire IPA message from the socket */
274 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
275 if (rc < 0)
276 return rc;
277 data_len = rc;
278
279 hh = (struct ipaccess_head *) buf;
280 if (hh->proto != IPAC_PROTO_OSMO)
281 return -4;
282
283 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
284 if (data_len < sizeof(*hh_ext))
285 return -5;
286 data_len -= sizeof(*hh_ext);
287 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
288 return -6;
289
290 /* 2) ASN1 BER decode of the message */
291 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
292 if (rval.code != RC_OK)
293 return -7;
294
295 /* 3) handling of the message, possibly resulting in PCSC commands */
296 rc = worker_handle_rspro(worker, pdu);
297 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
298 if (rc < 0)
299 return rc;
300
301 /* everything OK if we reach here */
302 return 0;
303}
304
Harald Welted6dfb8c2018-08-16 14:46:53 +0200305/* obtain an ascii representation of the client IP/port */
306static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
307{
308 char hostbuf[32], portbuf[32];
309 int rc;
310
311 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
312 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
313 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
314 if (rc != 0) {
315 out[0] = '\0';
316 return -1;
317 }
318 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
319 return 0;
320}
321
Harald Welte77911b02018-08-14 23:47:30 +0200322/* worker thread main function */
323static void *worker_main(void *arg)
324{
325 struct bankd_worker *worker = (struct bankd_worker *) arg;
326 void *top_ctx;
327 int rc;
328
Harald Welte8d858292018-08-15 23:36:46 +0200329 worker_set_state(worker, BW_ST_INIT);
330
Harald Welte77911b02018-08-14 23:47:30 +0200331 /* not permitted in multithreaded environment */
332 talloc_disable_null_tracking();
333 top_ctx = talloc_named_const(NULL, 0, "top");
334 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
335
336 /* push cleanup helper */
337 pthread_cleanup_push(&worker_cleanup, worker);
338
339 /* we continuously perform the same loop here, recycling the worker thread
340 * once the client connection is gone or we have some trouble with the card/reader */
341 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200342 char buf[128];
343
Harald Welte77911b02018-08-14 23:47:30 +0200344 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
345
Harald Welte8d858292018-08-15 23:36:46 +0200346 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200347 /* first wait for an incoming TCP connection */
348 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
349 &worker->client.peer_addr_len);
350 if (rc < 0) {
351 continue;
352 }
353 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200354 worker_client_addrstr(buf, sizeof(buf), worker);
355 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200356 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200357
358 /* run the main worker transceive loop body until there was some error */
359 while (1) {
360 rc = worker_transceive_loop(worker);
361 if (rc < 0)
362 break;
363 }
364
Harald Welted6dfb8c2018-08-16 14:46:53 +0200365 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
366
Harald Welte77911b02018-08-14 23:47:30 +0200367 /* clean-up: reset to sane state */
368 if (worker->reader.pcsc.hCard) {
369 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
370 worker->reader.pcsc.hCard = 0;
371 }
372 if (worker->reader.pcsc.hContext) {
373 SCardReleaseContext(worker->reader.pcsc.hContext);
374 worker->reader.pcsc.hContext = 0;
375 }
376 if (worker->client.fd >= 0)
377 close(worker->client.fd);
Harald Welte415e8f62018-08-16 14:47:38 +0200378 memset(&worker->client.peer_addr, 0, sizeof(worker->client.peer_addr));
Harald Welte77911b02018-08-14 23:47:30 +0200379 worker->client.fd = -1;
Harald Welte371d0262018-08-16 15:23:58 +0200380 worker->client.clslot.client_id = worker->client.clslot.slot_nr = 0;
Harald Welte77911b02018-08-14 23:47:30 +0200381 }
382
383 pthread_cleanup_pop(1);
384 talloc_free(top_ctx);
385 pthread_exit(NULL);
386}