source: trunk/reader-common.c@ 8449

Last change on this file since 8449 was 8449, checked in by gf, 9 years ago

readers: Do not leak entitlements on card change and reader freeing.

Reader entitlements are leaken when card is changed. Probably nobody
noticed because changing cards is not an event that happen often.

  • Property svn:eol-style set to LF
File size: 10.4 KB
Line 
1#include "globals.h"
2
3#ifdef WITH_CARDREADER
4
5#include "module-led.h"
6#include "oscam-chk.h"
7#include "oscam-client.h"
8#include "oscam-ecm.h"
9#include "oscam-emm.h"
10#include "oscam-net.h"
11#include "oscam-time.h"
12#include "oscam-work.h"
13#include "oscam-reader.h"
14#include "reader-common.h"
15#include "csctapi/atr.h"
16#include "csctapi/icc_async.h"
17
18extern struct s_cardsystem cardsystems[CS_MAX_MOD];
19extern char *RDR_CD_TXT[];
20
21int32_t check_sct_len(const uchar *data, int32_t off)
22{
23 int32_t len = SCT_LEN(data);
24 if (len + off > MAX_LEN) {
25 cs_debug_mask(D_TRACE | D_READER, "check_sct_len(): smartcard section too long %d > %d", len, MAX_LEN - off);
26 len = -1;
27 }
28 return len;
29}
30
31static void reader_nullcard(struct s_reader * reader)
32{
33 memset(&reader->csystem , 0 , sizeof(reader->csystem));
34 memset(reader->hexserial, 0 , sizeof(reader->hexserial));
35 memset(reader->prid , 0xFF, sizeof(reader->prid ));
36 reader->caid=0;
37 memset(reader->availkeys, 0 , sizeof(reader->availkeys));
38 reader->acs=0;
39 reader->nprov=0;
40 reader->reassemble_emm_len=0;
41 memset(reader->reassemble_emm, 0, sizeof(reader->reassemble_emm));
42 cs_clear_entitlement(reader);
43}
44
45int32_t reader_cmd2icc(struct s_reader * reader, const uchar *buf, const int32_t l, uchar * cta_res, uint16_t * p_cta_lr)
46{
47 int32_t rc;
48 *p_cta_lr=CTA_RES_LEN-1; //FIXME not sure whether this one is necessary
49 rdr_ddump_mask(reader, D_READER, buf, l, "write to cardreader");
50 rc=ICC_Async_CardWrite(reader, (uchar *)buf, (uint16_t)l, cta_res, p_cta_lr);
51 return rc;
52}
53
54#define CMD_LEN 5
55
56int32_t card_write(struct s_reader * reader, const uchar *cmd, const uchar *data, uchar *response, uint16_t * response_length)
57{
58 uchar buf[260];
59 // always copy to be able to be able to use const buffer without changing all code
60 memcpy(buf, cmd, CMD_LEN);
61
62 if (data) {
63 if (cmd[4]) memcpy(buf+CMD_LEN, data, cmd[4]);
64 return(reader_cmd2icc(reader, buf, CMD_LEN+cmd[4], response, response_length));
65 }
66 else
67 return(reader_cmd2icc(reader, buf, CMD_LEN, response, response_length));
68}
69
70static inline int reader_use_gpio(struct s_reader * reader) {
71 return reader->use_gpio && reader->detect > 4;
72}
73
74static int32_t reader_card_inserted(struct s_reader * reader)
75{
76 if (!reader_use_gpio(reader) && (reader->detect & 0x7f) > 3)
77 return 1;
78
79 int32_t card;
80 if (ICC_Async_GetStatus (reader, &card)) {
81 rdr_log(reader, "Error getting card status.");
82 return 0; //corresponds with no card inside!!
83 }
84 return (card);
85}
86
87static int32_t reader_activate_card(struct s_reader * reader, ATR * atr, uint16_t deprecated)
88{
89 int32_t i,ret;
90
91 if (reader->card_status != CARD_NEED_INIT)
92 return 0;
93
94 /* Activate card */
95 for (i=0; i<3; i++) {
96 ret = ICC_Async_Activate(reader, atr, deprecated);
97 if (!ret)
98 break;
99 rdr_log(reader, "Error activating card.");
100 led_status_card_activation_error();
101 cs_sleepms(500);
102 }
103 if (ret) return(0);
104
105// rdr_log("ATR: %s", cs_hexdump(1, atr, atr_size, tmp, sizeof(tmp)));//FIXME
106 cs_sleepms(1000);
107 return(1);
108}
109
110void cardreader_get_card_info(struct s_reader *reader)
111{
112 if ((reader->card_status == CARD_NEED_INIT) || (reader->card_status == CARD_INSERTED)) {
113 struct s_client *cl = reader->client;
114 if (cl)
115 cl->last=time((time_t*)0);
116
117 if (reader->csystem.active && reader->csystem.card_info) {
118 reader->csystem.card_info(reader);
119 }
120 }
121}
122
123static int32_t reader_get_cardsystem(struct s_reader * reader, ATR *atr)
124{
125 int32_t i;
126 for (i=0; i<CS_MAX_MOD; i++) {
127 if (cardsystems[i].card_init) {
128 if (cardsystems[i].card_init(reader, atr)) {
129 rdr_log(reader, "found card system %s", cardsystems[i].desc);
130 reader->csystem=cardsystems[i];
131 reader->csystem.active=1;
132 led_status_found_cardsystem();
133 break;
134 }
135 }
136 }
137
138 if (reader->csystem.active==0)
139 {
140 rdr_log(reader, "card system not supported");
141 led_status_unsupported_card_system();
142 }
143
144 return(reader->csystem.active);
145}
146
147void cardreader_do_reset(struct s_reader *reader)
148{
149 reader_nullcard(reader);
150 ATR atr;
151 int32_t ret = 0;
152
153 ret = ICC_Async_Reset(reader, &atr, reader_activate_card, reader_get_cardsystem);
154
155 if (ret == -1)
156 return;
157
158 if (ret == 0) {
159 uint16_t deprecated;
160 for (deprecated = reader->deprecated; deprecated < 2; deprecated++) {
161 if (!reader_activate_card(reader, &atr, deprecated)) break;
162 ret = reader_get_cardsystem(reader, &atr);
163 if (ret)
164 break;
165 if (!deprecated)
166 rdr_log(reader, "Normal mode failed, reverting to Deprecated Mode");
167 }
168 }
169
170 if (!ret)
171 {
172 reader->card_status = CARD_FAILURE;
173 rdr_log(reader, "card initializing error");
174 ICC_Async_DisplayMsg(reader, "AER");
175 led_status_card_activation_error();
176 }
177 else
178 {
179 cardreader_get_card_info(reader);
180 reader->card_status = CARD_INSERTED;
181 do_emm_from_file(reader);
182 ICC_Async_DisplayMsg(reader, "AOK");
183 ICC_Set_Transmit_Timeout(reader);
184 }
185
186 return;
187}
188
189static int32_t cardreader_device_init(struct s_reader *reader)
190{
191 int32_t rc = -1; //FIXME
192 if (ICC_Async_Device_Init(reader))
193 rdr_log(reader, "Cannot open device: %s", reader->device);
194 else
195 rc = OK;
196 return((rc!=OK) ? 2 : 0); //exit code 2 means keep retrying, exit code 0 means all OK
197}
198
199int32_t cardreader_do_checkhealth(struct s_reader * reader)
200{
201 struct s_client *cl = reader->client;
202 if (reader_card_inserted(reader)) {
203 if (reader->card_status == NO_CARD || reader->card_status == UNKNOWN) {
204 rdr_log(reader, "card detected");
205 led_status_card_detected();
206 reader->card_status = CARD_NEED_INIT;
207 add_job(cl, ACTION_READER_RESET, NULL, 0);
208 }
209 } else {
210 rdr_debug_mask(reader, D_READER, "%s: !reader_card_inserted", __func__);
211 if (reader->card_status == CARD_INSERTED || reader->card_status == CARD_NEED_INIT) {
212 rdr_log(reader, "card ejected");
213 reader_nullcard(reader);
214 if (cl) {
215 cl->lastemm = 0;
216 cl->lastecm = 0;
217 }
218 led_status_card_ejected();
219 }
220 reader->card_status = NO_CARD;
221 }
222 rdr_debug_mask(reader, D_READER, "%s: reader->card_status = %d, ret = %d", __func__,
223 reader->card_status, reader->card_status == CARD_INSERTED);
224 return reader->card_status == CARD_INSERTED;
225}
226
227// Check for card inserted or card removed on pysical reader
228void cardreader_checkhealth(struct s_client *cl, struct s_reader *rdr) {
229 if (!rdr || !rdr->enable || !rdr->active)
230 return;
231 add_job(cl, ACTION_READER_CHECK_HEALTH, NULL, 0);
232}
233
234void cardreader_reset(struct s_client *cl) {
235 add_job(cl, ACTION_READER_RESET, NULL, 0);
236}
237
238void cardreader_init_locks(void) {
239 ICC_Async_Init_Locks();
240}
241
242bool cardreader_init(struct s_reader *reader) {
243 struct s_client *client = reader->client;
244 client->typ = 'r';
245 set_localhost_ip(&client->ip);
246 while (cardreader_device_init(reader) == 2) {
247 int8_t i = 0;
248 do {
249 cs_sleepms(2000);
250 if (!ll_contains(configured_readers, reader) || !check_client(client) || reader->enable != 1)
251 return false;
252 i++;
253 } while (i < 30);
254 }
255 if (reader->mhz > 2000) {
256 rdr_log(reader, "Reader initialized (device=%s, detect=%s%s, pll max=%.2f Mhz, wanted cardmhz=%.2f Mhz",
257 reader->device,
258 reader->detect & 0x80 ? "!" : "",
259 RDR_CD_TXT[reader->detect & 0x7f],
260 (float)reader->mhz /100,
261 (float)reader->cardmhz / 100);
262 } else {
263 rdr_log(reader, "Reader initialized (device=%s, detect=%s%s, mhz=%d, cardmhz=%d)",
264 reader->device,
265 reader->detect & 0x80 ? "!" : "",
266 RDR_CD_TXT[reader->detect & 0x7f],
267 reader->mhz,
268 reader->cardmhz);
269 }
270 return true;
271}
272
273void cardreader_close(struct s_reader *reader) {
274 ICC_Async_Close(reader);
275}
276
277void reader_post_process(struct s_reader * reader)
278{
279 // some systems eg. nagra2/3 needs post process after receiving cw from card
280 // To save ECM/CW time we added this function after writing ecm answer
281 if (reader->csystem.active && reader->csystem.post_process) {
282 reader->csystem.post_process(reader);
283 }
284}
285
286int32_t cardreader_do_ecm(struct s_reader *reader, ECM_REQUEST *er, struct s_ecm_answer *ea)
287{
288 int32_t rc=-1;
289 if( (rc=cardreader_do_checkhealth(reader)) ) {
290 rdr_debug_mask(reader, D_READER, "%s: cardreader_do_checkhealth returned rc=%d", __func__, rc);
291 struct s_client *cl = reader->client;
292 if (cl) {
293 cl->last_srvid=er->srvid;
294 cl->last_caid=er->caid;
295 cl->last=time((time_t*)0);
296 }
297
298 if (reader->csystem.active && reader->csystem.do_ecm) {
299 rc=reader->csystem.do_ecm(reader, er, ea);
300 rdr_debug_mask(reader, D_READER, "%s: after csystem.do_ecm rc=%d", __func__, rc);
301 } else
302 rc=0;
303 }
304 rdr_debug_mask(reader, D_READER, "%s: ret rc=%d", __func__, rc);
305 return(rc);
306}
307
308int32_t cardreader_do_emm(struct s_reader *reader, EMM_PACKET *ep)
309{
310 int32_t rc=-1;
311
312 rc=cardreader_do_checkhealth(reader);
313 if (rc) {
314 if ((1<<(ep->emm[0] % 0x80)) & reader->b_nano)
315 return 3;
316
317 if (reader->csystem.active && reader->csystem.do_emm)
318 rc=reader->csystem.do_emm(reader, ep);
319 else
320 rc=0;
321 }
322 return(rc);
323}
324
325void cardreader_process_ecm(struct s_reader *reader, struct s_client *cl, ECM_REQUEST *er) {
326 if (ecm_ratelimit_check(reader, er, 1) != OK) {
327 rdr_debug_mask(reader, D_READER, "%s: ratelimit check failed.", __func__);
328 return; // reader_mode = 1: checkout ratelimiter in reader mode so srvid can be replaced
329 }
330 cs_ddump_mask(D_ATR, er->ecm, er->ecmlen, "ecm:");
331
332 struct timeb tps, tpe;
333 cs_ftime(&tps);
334
335 struct s_ecm_answer ea;
336 memset(&ea, 0, sizeof(struct s_ecm_answer));
337
338 int32_t rc = cardreader_do_ecm(reader, er, &ea);
339 rdr_debug_mask(reader, D_READER, "%s: cardreader_do_ecm returned rc=%d (ERROR=%d)", __func__, rc, ERROR);
340
341 ea.rc = E_FOUND; //default assume found
342 ea.rcEx = 0; //no special flag
343
344 if (rc == ERROR) {
345 char buf[32];
346 rdr_debug_mask(reader, D_READER, "Error processing ecm for caid %04X, srvid %04X, servicename: %s",
347 er->caid, er->srvid, get_servicename(cl, er->srvid, er->caid, buf));
348 ea.rc = E_NOTFOUND;
349 ea.rcEx = 0;
350 ICC_Async_DisplayMsg(reader, "Eer");
351 }
352
353 if (rc == E_CORRUPT) {
354 char buf[32];
355 rdr_debug_mask(reader, D_READER, "Error processing ecm for caid %04X, srvid %04X, servicename: %s",
356 er->caid, er->srvid, get_servicename(cl, er->srvid, er->caid, buf));
357 ea.rc = E_NOTFOUND;
358 ea.rcEx = E2_WRONG_CHKSUM; //flag it as wrong checksum
359 memcpy (ea.msglog,"Invalid ecm type for card",25);
360 }
361 cs_ftime(&tpe);
362 cl->lastecm=time((time_t*)0);
363 char ecmd5[17*3];
364 cs_hexdump(0, er->ecmd5, 16, ecmd5, sizeof(ecmd5));
365
366 rdr_debug_mask(reader, D_READER, "ecm hash: %s real time: %ld ms",
367 ecmd5, 1000 * (tpe.time - tps.time) + tpe.millitm - tps.millitm);
368
369 write_ecm_answer(reader, er, ea.rc, ea.rcEx, ea.cw, ea.msglog);
370
371 reader_post_process(reader);
372}
373
374#endif
Note: See TracBrowser for help on using the repository browser.