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