blob: 955a432bd0b6fdf6a726c8ba30948cb6e0ecc103 [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
111#define PCSC_ERROR(rv, text) \
112if (rv != SCARD_S_SUCCESS) { \
113 fprintf(stderr, text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \
114 goto end; \
115} else { \
116 printf(text ": OK\n\n"); \
117}
118
Harald Welte8d858292018-08-15 23:36:46 +0200119#define LOGW(w, fmt, args...) \
120 printf("[%u] " fmt, (w)->num, args)
121
122struct value_string worker_state_names[] = {
123 { BW_ST_INIT, "INIT" },
124 { BW_ST_ACCEPTING, "ACCEPTING" },
125 { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" },
126 { BW_ST_CONN_CLIENT, "CONN_CLIENT" },
127 { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" },
128 { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" },
129 { 0, NULL }
130};
131
132static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state)
133{
134 LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state));
135 worker->state = new_state;
136}
Harald Welte77911b02018-08-14 23:47:30 +0200137
138static void worker_cleanup(void *arg)
139{
140 struct bankd_worker *worker = (struct bankd_worker *) arg;
141 struct bankd *bankd = worker->bankd;
142
143 /* FIXME: should we still do this? in the thread ?!? */
144 pthread_mutex_lock(&bankd->workers_mutex);
145 llist_del(&worker->list);
146 talloc_free(worker); /* FIXME: is this safe? */
147 pthread_mutex_unlock(&bankd->workers_mutex);
148}
149
150
151#if 0
152/* function running inside a worker thread; doing some initialization */
153static void worker_init(struct bankd_worker *worker)
154{
155 int rc;
156
157 /* push cleanup helper */
158 pthread_cleanup_push(&worker_cleanup, worker);
159
160 /* The PC/SC context must be created inside the thread where we'll later use it */
161 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext);
162 PCSC_ERROR(rc, "SCardEstablishContext")
163
164 rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED,
165 SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, NULL);
166 PCSC_ERROR(rc, "SCardConnect")
167
168 return;
169end:
170 pthread_exit(NULL);
171}
172#endif
173
174
175static int blocking_ipa_read(int fd, uint8_t *buf, unsigned int buf_size)
176{
177 struct ipaccess_head *hh;
178 uint16_t len;
179 int needed, rc;
180
181 if (buf_size < sizeof(*hh))
182 return -1;
183
184 hh = (struct ipaccess_head *) buf;
185
186 /* 1) blocking read from the socket (IPA header) */
187 rc = read(fd, buf, sizeof(*hh));
188 if (rc < sizeof(*hh))
189 return -2;
190
191 len = ntohs(hh->len);
192 needed = len; //- sizeof(*hh);
193
194 /* 2) blocking read from the socket (payload) */
195 rc = read(fd, buf+sizeof(*hh), needed);
196 if (rc < needed)
197 return -3;
198
199 return len;
200}
201
Harald Weltecce2aad2018-08-16 14:44:37 +0200202static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu)
203{
204 OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq);
205
206 const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity;
207
208 LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n",
209 cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf);
210 /* FIXME: store somewhere? */
211
212 if (worker->state != BW_ST_CONN_WAIT_ID) {
213 LOGW(worker, "Unexpected connectClientReq\n");
214 return -102;
215 }
216
217 if (!pdu->msg.choice.connectClientReq.clientId) {
218 LOGW(worker, "missing clientID, aborting\n");
219 return -103;
220 }
221 worker->client.id = *pdu->msg.choice.connectClientReq.clientId;
222 worker_set_state(worker, BW_ST_CONN_CLIENT);
223
224 /* FIXME: resolve mapping */
225
226 return 0;
227}
228
Harald Welte77911b02018-08-14 23:47:30 +0200229/* handle one incoming RSPRO message from a client inside a worker thread */
230static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu)
231{
Harald Weltecce2aad2018-08-16 14:44:37 +0200232 int rc = -100;
233
Harald Welte77911b02018-08-14 23:47:30 +0200234 switch (pdu->msg.present) {
235 case RsproPDUchoice_PR_connectClientReq:
Harald Weltecce2aad2018-08-16 14:44:37 +0200236 rc = worker_handle_connectClientReq(worker, pdu);
Harald Welte77911b02018-08-14 23:47:30 +0200237 break;
238 case RsproPDUchoice_PR_tpduModemToCard:
239 /* FIXME */
240 break;
241 case RsproPDUchoice_PR_clientSlotStatusInd:
242 /* FIXME */
243 break;
244 default:
Harald Weltecce2aad2018-08-16 14:44:37 +0200245 rc = -101;
246 break;
Harald Welte77911b02018-08-14 23:47:30 +0200247 }
248
Harald Weltecce2aad2018-08-16 14:44:37 +0200249 return rc;
Harald Welte77911b02018-08-14 23:47:30 +0200250}
251
252/* body of the main transceive loop */
253static int worker_transceive_loop(struct bankd_worker *worker)
254{
255 struct ipaccess_head *hh;
256 struct ipaccess_head_ext *hh_ext;
257 uint8_t buf[65536]; /* maximum length expressed in 16bit length field */
258 asn_dec_rval_t rval;
259 int data_len, rc;
260 RsproPDU_t *pdu;
261
262 /* 1) blocking read of entire IPA message from the socket */
263 rc = blocking_ipa_read(worker->client.fd, buf, sizeof(buf));
264 if (rc < 0)
265 return rc;
266 data_len = rc;
267
268 hh = (struct ipaccess_head *) buf;
269 if (hh->proto != IPAC_PROTO_OSMO)
270 return -4;
271
272 hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh);
273 if (data_len < sizeof(*hh_ext))
274 return -5;
275 data_len -= sizeof(*hh_ext);
276 if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO)
277 return -6;
278
279 /* 2) ASN1 BER decode of the message */
280 rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len);
281 if (rval.code != RC_OK)
282 return -7;
283
284 /* 3) handling of the message, possibly resulting in PCSC commands */
285 rc = worker_handle_rspro(worker, pdu);
286 ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu);
287 if (rc < 0)
288 return rc;
289
290 /* everything OK if we reach here */
291 return 0;
292}
293
Harald Welted6dfb8c2018-08-16 14:46:53 +0200294/* obtain an ascii representation of the client IP/port */
295static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker)
296{
297 char hostbuf[32], portbuf[32];
298 int rc;
299
300 rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr,
301 worker->client.peer_addr_len, hostbuf, sizeof(hostbuf),
302 portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV);
303 if (rc != 0) {
304 out[0] = '\0';
305 return -1;
306 }
307 snprintf(out, outlen, "%s:%s", hostbuf, portbuf);
308 return 0;
309}
310
Harald Welte77911b02018-08-14 23:47:30 +0200311/* worker thread main function */
312static void *worker_main(void *arg)
313{
314 struct bankd_worker *worker = (struct bankd_worker *) arg;
315 void *top_ctx;
316 int rc;
317
Harald Welte8d858292018-08-15 23:36:46 +0200318 worker_set_state(worker, BW_ST_INIT);
319
Harald Welte77911b02018-08-14 23:47:30 +0200320 /* not permitted in multithreaded environment */
321 talloc_disable_null_tracking();
322 top_ctx = talloc_named_const(NULL, 0, "top");
323 talloc_asn1_ctx = talloc_named_const(top_ctx, 0, "asn1");
324
325 /* push cleanup helper */
326 pthread_cleanup_push(&worker_cleanup, worker);
327
328 /* we continuously perform the same loop here, recycling the worker thread
329 * once the client connection is gone or we have some trouble with the card/reader */
330 while (1) {
Harald Welted6dfb8c2018-08-16 14:46:53 +0200331 char buf[128];
332
Harald Welte77911b02018-08-14 23:47:30 +0200333 worker->client.peer_addr_len = sizeof(worker->client.peer_addr);
334
Harald Welte8d858292018-08-15 23:36:46 +0200335 worker_set_state(worker, BW_ST_ACCEPTING);
Harald Welte77911b02018-08-14 23:47:30 +0200336 /* first wait for an incoming TCP connection */
337 rc = accept(worker->bankd->accept_fd, (struct sockaddr *) &worker->client.peer_addr,
338 &worker->client.peer_addr_len);
339 if (rc < 0) {
340 continue;
341 }
342 worker->client.fd = rc;
Harald Welted6dfb8c2018-08-16 14:46:53 +0200343 worker_client_addrstr(buf, sizeof(buf), worker);
344 LOGW(worker, "Accepted connection from %s\n", buf);
Harald Welte8d858292018-08-15 23:36:46 +0200345 worker_set_state(worker, BW_ST_CONN_WAIT_ID);
Harald Welte77911b02018-08-14 23:47:30 +0200346
347 /* run the main worker transceive loop body until there was some error */
348 while (1) {
349 rc = worker_transceive_loop(worker);
350 if (rc < 0)
351 break;
352 }
353
Harald Welted6dfb8c2018-08-16 14:46:53 +0200354 LOGW(worker, "Error %d occurred: Cleaning up state\n", rc);
355
Harald Welte77911b02018-08-14 23:47:30 +0200356 /* clean-up: reset to sane state */
357 if (worker->reader.pcsc.hCard) {
358 SCardDisconnect(worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD);
359 worker->reader.pcsc.hCard = 0;
360 }
361 if (worker->reader.pcsc.hContext) {
362 SCardReleaseContext(worker->reader.pcsc.hContext);
363 worker->reader.pcsc.hContext = 0;
364 }
365 if (worker->client.fd >= 0)
366 close(worker->client.fd);
367 worker->client.fd = -1;
368 }
369
370 pthread_cleanup_pop(1);
371 talloc_free(top_ctx);
372 pthread_exit(NULL);
373}