1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #ifdef MODULE_CCCAM
|
---|
4 |
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include "module-cccam.h"
|
---|
7 | #include <time.h>
|
---|
8 | #include "reader-common.h"
|
---|
9 | #include <poll.h>
|
---|
10 | #include "module-cccshare.h"
|
---|
11 |
|
---|
12 | //Mode names for CMD_05 command:
|
---|
13 | const char *cmd05_mode_name[] = { "UNKNOWN", "PLAIN", "AES", "CC_CRYPT", "RC4",
|
---|
14 | "LEN=0" };
|
---|
15 |
|
---|
16 | //Mode names for CMD_0C command:
|
---|
17 | const char *cmd0c_mode_name[] = { "NONE", "RC6", "RC4", "CC_CRYPT", "AES", "IDEA" };
|
---|
18 |
|
---|
19 | static uint8_t cc_node_id[8];
|
---|
20 |
|
---|
21 | #define getprefix() (!cl?"":(!cl->cc?"":(((struct cc_data *)(cl->cc))->prefix)))
|
---|
22 |
|
---|
23 | void cc_init_crypt(struct cc_crypt_block *block, uint8_t *key, int32_t len) {
|
---|
24 | int32_t i = 0;
|
---|
25 | uint8_t j = 0;
|
---|
26 |
|
---|
27 | for (i = 0; i < 256; i++) {
|
---|
28 | block->keytable[i] = i;
|
---|
29 | }
|
---|
30 |
|
---|
31 | for (i = 0; i < 256; i++) {
|
---|
32 | j += key[i % len] + block->keytable[i];
|
---|
33 | SWAPC(&block->keytable[i], &block->keytable[j]);
|
---|
34 | }
|
---|
35 |
|
---|
36 | block->state = *key;
|
---|
37 | block->counter = 0;
|
---|
38 | block->sum = 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void cc_crypt(struct cc_crypt_block *block, uint8_t *data, int32_t len,
|
---|
42 | cc_crypt_mode_t mode) {
|
---|
43 | int32_t i;
|
---|
44 | uint8_t z;
|
---|
45 |
|
---|
46 | for (i = 0; i < len; i++) {
|
---|
47 | block->counter++;
|
---|
48 | block->sum += block->keytable[block->counter];
|
---|
49 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
50 | z = data[i];
|
---|
51 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
52 | + block->keytable[block->sum]) & 0xff];
|
---|
53 | data[i] ^= block->state;
|
---|
54 | if (!mode)
|
---|
55 | z = data[i];
|
---|
56 | block->state = block->state ^ z;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | void cc_rc4_crypt(struct cc_crypt_block *block, uint8_t *data, int32_t len,
|
---|
61 | cc_crypt_mode_t mode) {
|
---|
62 | int32_t i;
|
---|
63 | uint8_t z;
|
---|
64 |
|
---|
65 | for (i = 0; i < len; i++) {
|
---|
66 | block->counter++;
|
---|
67 | block->sum += block->keytable[block->counter];
|
---|
68 | SWAPC(&block->keytable[block->counter], &block->keytable[block->sum]);
|
---|
69 | z = data[i];
|
---|
70 | data[i] = z ^ block->keytable[(block->keytable[block->counter]
|
---|
71 | + block->keytable[block->sum]) & 0xff];
|
---|
72 | if (!mode)
|
---|
73 | z = data[i];
|
---|
74 | block->state = block->state ^ z;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | void cc_xor(uint8_t *buf) {
|
---|
79 | const char cccam[] = "CCcam";
|
---|
80 | uint8_t i;
|
---|
81 |
|
---|
82 | for (i = 0; i < 8; i++) {
|
---|
83 | buf[8 + i] = i * buf[i];
|
---|
84 | if (i <= 5) {
|
---|
85 | buf[i] ^= cccam[i];
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void cc_cw_crypt(struct s_client *cl, uint8_t *cws, uint32_t cardid) {
|
---|
91 | struct cc_data *cc = cl->cc;
|
---|
92 | int64_t node_id;
|
---|
93 | uint8_t tmp;
|
---|
94 | int32_t i;
|
---|
95 |
|
---|
96 | if (cl->typ != 'c') {
|
---|
97 | node_id = b2ll(8, cc->node_id);
|
---|
98 | } else {
|
---|
99 | node_id = b2ll(8, cc->peer_node_id);
|
---|
100 | }
|
---|
101 |
|
---|
102 | for (i = 0; i < 16; i++) {
|
---|
103 | tmp = cws[i] ^ (node_id >> (4 * i));
|
---|
104 | if (i & 1)
|
---|
105 | tmp = ~tmp;
|
---|
106 | cws[i] = (cardid >> (2 * i)) ^ tmp;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** swap endianness (int) */
|
---|
111 | static void SwapLBi(unsigned char *buff, int32_t len)
|
---|
112 | {
|
---|
113 | #if __BYTE_ORDER != __BIG_ENDIAN
|
---|
114 | return;
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | int32_t i;
|
---|
118 | unsigned char swap[4];
|
---|
119 | for (i = 0; i < len / 4; i++) {
|
---|
120 | memcpy(swap, buff, 4);
|
---|
121 | buff[0] = swap[3];
|
---|
122 | buff[1] = swap[2];
|
---|
123 | buff[2] = swap[1];
|
---|
124 | buff[3] = swap[0];
|
---|
125 | buff += 4;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | void cc_crypt_cmd0c(struct s_client *cl, uint8_t *buf, int32_t len) {
|
---|
130 | struct cc_data *cc = cl->cc;
|
---|
131 | uint8_t *out = cs_malloc(&out, len, QUITERROR);
|
---|
132 |
|
---|
133 | switch (cc->cmd0c_mode) {
|
---|
134 | case MODE_CMD_0x0C_NONE: { // none additional encryption
|
---|
135 | memcpy(out, buf, len);
|
---|
136 | break;
|
---|
137 | }
|
---|
138 | case MODE_CMD_0x0C_RC6 : { //RC6
|
---|
139 | int32_t i;
|
---|
140 | SwapLBi(buf, len);
|
---|
141 | for (i = 0; i < len / 16; i++)
|
---|
142 | rc6_block_decrypt((uint32_t*)(buf+i*16), (uint32_t*)(out+i*16), 1, cc->cmd0c_RC6_cryptkey);
|
---|
143 | SwapLBi(out, len);
|
---|
144 | break;
|
---|
145 | }
|
---|
146 | case MODE_CMD_0x0C_RC4: { // RC4
|
---|
147 | cc_rc4_crypt(&cc->cmd0c_cryptkey, buf, len, ENCRYPT);
|
---|
148 | memcpy(out, buf, len);
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | case MODE_CMD_0x0C_CC_CRYPT: { // cc_crypt
|
---|
152 | cc_crypt(&cc->cmd0c_cryptkey, buf, len, DECRYPT);
|
---|
153 | memcpy(out, buf, len);
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | case MODE_CMD_0x0C_AES: { // AES
|
---|
157 | int32_t i;
|
---|
158 | for (i = 0; i<len / 16; i++)
|
---|
159 | AES_decrypt((unsigned char *) buf + i * 16,
|
---|
160 | (unsigned char *) out + i * 16, &cc->cmd0c_AES_key);
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | case MODE_CMD_0x0C_IDEA : { //IDEA
|
---|
164 | int32_t i=0;
|
---|
165 | int32_t j;
|
---|
166 |
|
---|
167 | while (i < len) {
|
---|
168 | idea_ecb_encrypt(buf + i, out + i, &cc->cmd0c_IDEA_dkey);
|
---|
169 | i += 8;
|
---|
170 | }
|
---|
171 |
|
---|
172 | i = 8;
|
---|
173 | while (i < len) {
|
---|
174 | for (j=0; j < 8; j++)
|
---|
175 | out[j+i] ^= buf[j+i-8];
|
---|
176 | i += 8;
|
---|
177 | }
|
---|
178 |
|
---|
179 | break;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | memcpy(buf, out, len);
|
---|
183 | free(out);
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | void set_cmd0c_cryptkey(struct s_client *cl, uint8_t *key, uint8_t len) {
|
---|
189 | struct cc_data *cc = cl->cc;
|
---|
190 | uint8_t key_buf[32];
|
---|
191 |
|
---|
192 | memset(&key_buf, 0, sizeof(key_buf));
|
---|
193 |
|
---|
194 | if (len > 32)
|
---|
195 | len = 32;
|
---|
196 |
|
---|
197 | memcpy(key_buf, key, len);
|
---|
198 |
|
---|
199 | switch (cc->cmd0c_mode) {
|
---|
200 |
|
---|
201 | case MODE_CMD_0x0C_NONE : { //NONE
|
---|
202 | break;
|
---|
203 | }
|
---|
204 |
|
---|
205 | case MODE_CMD_0x0C_RC6 : { //RC6
|
---|
206 | rc6_key_setup(key_buf, 32, cc->cmd0c_RC6_cryptkey);
|
---|
207 | break;
|
---|
208 | }
|
---|
209 |
|
---|
210 | case MODE_CMD_0x0C_RC4: //RC4
|
---|
211 | case MODE_CMD_0x0C_CC_CRYPT: { //CC_CRYPT
|
---|
212 | cc_init_crypt(&cc->cmd0c_cryptkey, key_buf, 32);
|
---|
213 | break;
|
---|
214 | }
|
---|
215 |
|
---|
216 | case MODE_CMD_0x0C_AES: { //AES
|
---|
217 | memset(&cc->cmd0c_AES_key, 0, sizeof(cc->cmd0c_AES_key));
|
---|
218 | AES_set_decrypt_key((unsigned char *) key_buf, 256, &cc->cmd0c_AES_key);
|
---|
219 | break;
|
---|
220 | }
|
---|
221 |
|
---|
222 | case MODE_CMD_0x0C_IDEA : { //IDEA
|
---|
223 | uint8_t key_buf_idea[16];
|
---|
224 | memcpy(key_buf_idea, key_buf, 16);
|
---|
225 | IDEA_KEY_SCHEDULE ekey;
|
---|
226 |
|
---|
227 | idea_set_encrypt_key(key_buf_idea, &ekey);
|
---|
228 | idea_set_decrypt_key(&ekey,&cc->cmd0c_IDEA_dkey);
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | int32_t sid_eq(struct cc_srvid *srvid1, struct cc_srvid *srvid2) {
|
---|
235 | return (srvid1->sid == srvid2->sid && (srvid1->ecmlen == srvid2->ecmlen || !srvid1->ecmlen || !srvid2->ecmlen));
|
---|
236 | }
|
---|
237 |
|
---|
238 | int32_t is_sid_blocked(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
239 | LL_ITER it = ll_iter_create(card->badsids);
|
---|
240 | struct cc_srvid *srvid;
|
---|
241 | while ((srvid = ll_iter_next(&it))) {
|
---|
242 | if (sid_eq(srvid, srvid_blocked)) {
|
---|
243 | break;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | return (srvid != 0);
|
---|
247 | }
|
---|
248 |
|
---|
249 | int32_t is_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
250 | LL_ITER it = ll_iter_create(card->goodsids);
|
---|
251 | struct cc_srvid *srvid;
|
---|
252 | while ((srvid = ll_iter_next(&it))) {
|
---|
253 | if (sid_eq(srvid, srvid_good)) {
|
---|
254 | break;
|
---|
255 | }
|
---|
256 | }
|
---|
257 | return (srvid != 0);
|
---|
258 | }
|
---|
259 |
|
---|
260 | void add_sid_block(struct s_client *cl __attribute__((unused)), struct cc_card *card,
|
---|
261 | struct cc_srvid *srvid_blocked) {
|
---|
262 | if (is_sid_blocked(card, srvid_blocked))
|
---|
263 | return;
|
---|
264 |
|
---|
265 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
266 | memcpy(srvid, srvid_blocked, sizeof(struct cc_srvid));
|
---|
267 | ll_append(card->badsids, srvid);
|
---|
268 | cs_debug_mask(D_READER, "%s added sid block %04X(%d) for card %08x",
|
---|
269 | getprefix(), srvid_blocked->sid, srvid_blocked->ecmlen,
|
---|
270 | card->id);
|
---|
271 | }
|
---|
272 |
|
---|
273 | void remove_sid_block(struct cc_card *card, struct cc_srvid *srvid_blocked) {
|
---|
274 | LL_ITER it = ll_iter_create(card->badsids);
|
---|
275 | struct cc_srvid *srvid;
|
---|
276 | while ((srvid = ll_iter_next(&it)))
|
---|
277 | if (sid_eq(srvid, srvid_blocked))
|
---|
278 | ll_iter_remove_data(&it);
|
---|
279 | }
|
---|
280 |
|
---|
281 | void remove_good_sid(struct cc_card *card, struct cc_srvid *srvid_good) {
|
---|
282 | LL_ITER it = ll_iter_create(card->goodsids);
|
---|
283 | struct cc_srvid *srvid;
|
---|
284 | while ((srvid = ll_iter_next(&it)))
|
---|
285 | if (sid_eq(srvid, srvid_good))
|
---|
286 | ll_iter_remove_data(&it);
|
---|
287 | }
|
---|
288 |
|
---|
289 | void add_good_sid(struct s_client *cl __attribute__((unused)), struct cc_card *card,
|
---|
290 | struct cc_srvid *srvid_good) {
|
---|
291 | if (is_good_sid(card, srvid_good))
|
---|
292 | return;
|
---|
293 |
|
---|
294 | remove_sid_block(card, srvid_good);
|
---|
295 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
296 | memcpy(srvid, srvid_good, sizeof(struct cc_srvid));
|
---|
297 | ll_append(card->goodsids, srvid);
|
---|
298 | cs_debug_mask(D_READER, "%s added good sid %04X(%d) for card %08x",
|
---|
299 | getprefix(), srvid_good->sid, srvid_good->ecmlen, card->id);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * reader
|
---|
304 | * clears and frees values for reinit
|
---|
305 | */
|
---|
306 | void cc_cli_close(struct s_client *cl, int32_t call_conclose) {
|
---|
307 | struct s_reader *rdr = cl->reader;
|
---|
308 | struct cc_data *cc = cl->cc;
|
---|
309 | if (!rdr || !cc)
|
---|
310 | return;
|
---|
311 |
|
---|
312 | rdr->tcp_connected = 0;
|
---|
313 | rdr->card_status = NO_CARD;
|
---|
314 | rdr->available = 0;
|
---|
315 | rdr->ncd_msgid = 0;
|
---|
316 | rdr->last_s = rdr->last_g = 0;
|
---|
317 |
|
---|
318 | if (cc->mode == CCCAM_MODE_NORMAL && call_conclose)
|
---|
319 | network_tcp_connection_close(cl, cl->udp_fd);
|
---|
320 | else {
|
---|
321 | if (cl->udp_fd)
|
---|
322 | close(cl->udp_fd);
|
---|
323 | cl->udp_fd = 0;
|
---|
324 | cl->pfd = 0;
|
---|
325 | }
|
---|
326 |
|
---|
327 | cc->just_logged_in = 0;
|
---|
328 | }
|
---|
329 |
|
---|
330 | struct cc_extended_ecm_idx *add_extended_ecm_idx(struct s_client *cl,
|
---|
331 | uint8_t send_idx, uint16_t ecm_idx, struct cc_card *card,
|
---|
332 | struct cc_srvid srvid, int8_t free_card) {
|
---|
333 | struct cc_data *cc = cl->cc;
|
---|
334 | struct cc_extended_ecm_idx *eei =
|
---|
335 | cs_malloc(&eei, sizeof(struct cc_extended_ecm_idx), QUITERROR);
|
---|
336 | eei->send_idx = send_idx;
|
---|
337 | eei->ecm_idx = ecm_idx;
|
---|
338 | eei->card = card;
|
---|
339 | eei->srvid = srvid;
|
---|
340 | eei->free_card = free_card;
|
---|
341 | ll_append(cc->extended_ecm_idx, eei);
|
---|
342 | //cs_debug_mask(D_TRACE, "%s add extended ecm-idx: %d:%d", getprefix(), send_idx, ecm_idx);
|
---|
343 | return eei;
|
---|
344 | }
|
---|
345 |
|
---|
346 | struct cc_extended_ecm_idx *get_extended_ecm_idx(struct s_client *cl,
|
---|
347 | uint8_t send_idx, int32_t remove) {
|
---|
348 | struct cc_data *cc = cl->cc;
|
---|
349 | struct cc_extended_ecm_idx *eei;
|
---|
350 | LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
|
---|
351 | while ((eei = ll_iter_next(&it))) {
|
---|
352 | if (eei->send_idx == send_idx) {
|
---|
353 | if (remove)
|
---|
354 | ll_iter_remove(&it);
|
---|
355 | //cs_debug_mask(D_TRACE, "%s get by send-idx: %d FOUND: %d",
|
---|
356 | // getprefix(), send_idx, eei->ecm_idx);
|
---|
357 | return eei;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s get by send-idx: %d NOT FOUND", getprefix(),
|
---|
361 | send_idx);
|
---|
362 | return NULL;
|
---|
363 | }
|
---|
364 |
|
---|
365 | struct cc_extended_ecm_idx *get_extended_ecm_idx_by_idx(struct s_client *cl,
|
---|
366 | uint16_t ecm_idx, int32_t remove) {
|
---|
367 | struct cc_data *cc = cl->cc;
|
---|
368 | struct cc_extended_ecm_idx *eei;
|
---|
369 | LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
|
---|
370 | while ((eei = ll_iter_next(&it))) {
|
---|
371 | if (eei->ecm_idx == ecm_idx) {
|
---|
372 | if (remove)
|
---|
373 | ll_iter_remove(&it);
|
---|
374 | //cs_debug_mask(D_TRACE, "%s get by ecm-idx: %d FOUND: %d",
|
---|
375 | // getprefix(), ecm_idx, eei->send_idx);
|
---|
376 | return eei;
|
---|
377 | }
|
---|
378 | }
|
---|
379 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s get by ecm-idx: %d NOT FOUND", getprefix(),
|
---|
380 | ecm_idx);
|
---|
381 | return NULL;
|
---|
382 | }
|
---|
383 |
|
---|
384 | void cc_reset_pending(struct s_client *cl, int32_t ecm_idx) {
|
---|
385 | int32_t i = 0;
|
---|
386 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
387 | if (cl->ecmtask[i].idx == ecm_idx && cl->ecmtask[i].rc == 101)
|
---|
388 | cl->ecmtask[i].rc = 100; //Mark unused
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | void free_extended_ecm_idx_by_card(struct s_client *cl, struct cc_card *card) {
|
---|
393 | struct cc_data *cc = cl->cc;
|
---|
394 | struct cc_extended_ecm_idx *eei;
|
---|
395 | LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
|
---|
396 | while ((eei = ll_iter_next(&it))) {
|
---|
397 | if (eei->card == card) {
|
---|
398 | cc_reset_pending(cl, eei->ecm_idx);
|
---|
399 | if (eei->free_card)
|
---|
400 | free(eei->card);
|
---|
401 | ll_iter_remove_data(&it);
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | void free_extended_ecm_idx(struct cc_data *cc) {
|
---|
407 | struct cc_extended_ecm_idx *eei;
|
---|
408 | LL_ITER it = ll_iter_create(cc->extended_ecm_idx);
|
---|
409 | while ((eei = ll_iter_next(&it))) {
|
---|
410 | if (eei->free_card)
|
---|
411 | free(eei->card);
|
---|
412 | ll_iter_remove_data(&it);
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | int32_t cc_recv_to(struct s_client *cl, uint8_t *buf, int32_t len) {
|
---|
417 | int32_t rc;
|
---|
418 | struct pollfd pfd;
|
---|
419 |
|
---|
420 | while (1) {
|
---|
421 | pfd.fd = cl->udp_fd;
|
---|
422 | pfd.events = POLLIN | POLLPRI;
|
---|
423 |
|
---|
424 | rc = poll(&pfd, 1, 2000);
|
---|
425 |
|
---|
426 | if (rc < 0) {
|
---|
427 | if (errno==EINTR) continue;
|
---|
428 | return(-1); //error!!
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (rc == 1)
|
---|
432 | break;
|
---|
433 | return (-2); //timeout!!
|
---|
434 | }
|
---|
435 | return recv(cl->udp_fd, buf, len, MSG_WAITALL);
|
---|
436 | }
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * reader
|
---|
440 | * closes the connection and reopens it.
|
---|
441 | */
|
---|
442 | //static void cc_cycle_connection() {
|
---|
443 | // cc_cli_close();
|
---|
444 | // cc_cli_init();
|
---|
445 | //}
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * reader+server:
|
---|
449 | * receive a message
|
---|
450 | */
|
---|
451 | int32_t cc_msg_recv(struct s_client *cl, uint8_t *buf, int32_t maxlen) {
|
---|
452 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
453 |
|
---|
454 | int32_t len;
|
---|
455 | struct cc_data *cc = cl->cc;
|
---|
456 |
|
---|
457 | int32_t handle = cl->udp_fd;
|
---|
458 |
|
---|
459 | if (handle <= 0 || maxlen < 4)
|
---|
460 | return -1;
|
---|
461 |
|
---|
462 | while (cl->cc && cc->mode != CCCAM_MODE_SHUTDOWN && cs_trylock(&cc->lockcmd)) {
|
---|
463 | cs_debug_mask(D_TRACE, "%s trylock recv waiting", getprefix());
|
---|
464 | cs_sleepms(50);
|
---|
465 | }
|
---|
466 | if (!cl->cc || cc->mode == CCCAM_MODE_SHUTDOWN) return -1;
|
---|
467 |
|
---|
468 | len = recv(handle, buf, 4, MSG_WAITALL);
|
---|
469 | if (rdr)
|
---|
470 | rdr->last_g = time(NULL);
|
---|
471 |
|
---|
472 | if (len != 4) { // invalid header length read
|
---|
473 | if (len <= 0)
|
---|
474 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s disconnected by remote server", getprefix());
|
---|
475 | else
|
---|
476 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s invalid header length (expected 4, read %d)", getprefix(), len);
|
---|
477 | cs_unlock(&cc->lockcmd);
|
---|
478 | return -1;
|
---|
479 | }
|
---|
480 |
|
---|
481 | cc_crypt(&cc->block[DECRYPT], buf, 4, DECRYPT);
|
---|
482 | //cs_ddump_mask(D_CLIENT, buf, 4, "cccam: decrypted header:");
|
---|
483 |
|
---|
484 | cc->g_flag = buf[0];
|
---|
485 |
|
---|
486 | int32_t size = (buf[2] << 8) | buf[3];
|
---|
487 | if (size) { // check if any data is expected in msg
|
---|
488 | if (size > maxlen) {
|
---|
489 | cs_unlock(&cc->lockcmd);
|
---|
490 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s message too big (size=%d max=%d)", getprefix(), size, maxlen);
|
---|
491 | return 0;
|
---|
492 | }
|
---|
493 |
|
---|
494 | len = recv(handle, buf + 4, size, MSG_WAITALL); // read rest of msg
|
---|
495 | if (rdr)
|
---|
496 | rdr->last_g = time(NULL);
|
---|
497 |
|
---|
498 | if (len != size) {
|
---|
499 | cs_unlock(&cc->lockcmd);
|
---|
500 | if (len <= 0)
|
---|
501 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s disconnected by remote", getprefix());
|
---|
502 | else
|
---|
503 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s invalid message length read (expected %d, read %d)",
|
---|
504 | getprefix(), size, len);
|
---|
505 | return -1;
|
---|
506 | }
|
---|
507 |
|
---|
508 | cc_crypt(&cc->block[DECRYPT], buf + 4, len, DECRYPT);
|
---|
509 | len += 4;
|
---|
510 | }
|
---|
511 |
|
---|
512 | cs_unlock(&cc->lockcmd);
|
---|
513 |
|
---|
514 | //cs_ddump_mask(cl->typ=='c'?D_CLIENT:D_READER, buf, len, "cccam: full decrypted msg, len=%d:", len);
|
---|
515 |
|
---|
516 | return len;
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * reader+server
|
---|
521 | * send a message
|
---|
522 | */
|
---|
523 | int32_t cc_cmd_send(struct s_client *cl, uint8_t *buf, int32_t len, cc_msg_type_t cmd) {
|
---|
524 | if (!cl->udp_fd) //disconnected
|
---|
525 | return -1;
|
---|
526 |
|
---|
527 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
528 |
|
---|
529 | int32_t n;
|
---|
530 | uint8_t *netbuf = cs_malloc(&netbuf, len + 4, 0);
|
---|
531 | struct cc_data *cc = cl->cc;
|
---|
532 |
|
---|
533 | while (cl->cc && cc->mode != CCCAM_MODE_SHUTDOWN && cs_trylock(&cc->lockcmd)) { //We need this because cc_cmd_send is called from cccshare
|
---|
534 | cs_debug_mask(D_TRACE, "%s trylock send waiting", getprefix());
|
---|
535 | cs_sleepms(50);
|
---|
536 | }
|
---|
537 | if (!cl->cc || cc->mode == CCCAM_MODE_SHUTDOWN) return -1;
|
---|
538 |
|
---|
539 | if (cmd == MSG_NO_HEADER) {
|
---|
540 | memcpy(netbuf, buf, len);
|
---|
541 | } else {
|
---|
542 | // build command message
|
---|
543 | netbuf[0] = cc->g_flag; // flags??
|
---|
544 | netbuf[1] = cmd & 0xff;
|
---|
545 | netbuf[2] = len >> 8;
|
---|
546 | netbuf[3] = len & 0xff;
|
---|
547 | if (buf)
|
---|
548 | memcpy(netbuf + 4, buf, len);
|
---|
549 | len += 4;
|
---|
550 | }
|
---|
551 |
|
---|
552 | //cs_ddump_mask(D_CLIENT, netbuf, len, "cccam: send:");
|
---|
553 | cc_crypt(&cc->block[ENCRYPT], netbuf, len, ENCRYPT);
|
---|
554 |
|
---|
555 | n = send(cl->udp_fd, netbuf, len, 0);
|
---|
556 | if (rdr)
|
---|
557 | rdr->last_s = time(NULL);
|
---|
558 |
|
---|
559 | cs_unlock(&cc->lockcmd);
|
---|
560 |
|
---|
561 | free(netbuf);
|
---|
562 |
|
---|
563 | if (n != len) {
|
---|
564 | if (rdr)
|
---|
565 | cc_cli_close(cl, TRUE);
|
---|
566 | n = -1;
|
---|
567 | }
|
---|
568 |
|
---|
569 | return n;
|
---|
570 | }
|
---|
571 |
|
---|
572 | #define CC_DEFAULT_VERSION 1
|
---|
573 | char *version[] = { "2.0.11", "2.1.1", "2.1.2", "2.1.3", "2.1.4", "2.2.0", "2.2.1", "" };
|
---|
574 | char *build[] = { "2892", "2971", "3094", "3165", "3191", "3290", "3316", "" };
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * reader+server
|
---|
578 | * checks the cccam-version in the configuration
|
---|
579 | */
|
---|
580 | void cc_check_version(char *cc_version, char *cc_build) {
|
---|
581 | int32_t i;
|
---|
582 | for (i = 0; strlen(version[i]); i++) {
|
---|
583 | if (!memcmp(cc_version, version[i], strlen(version[i]))) {
|
---|
584 | memcpy(cc_build, build[i], strlen(build[i]) + 1);
|
---|
585 | cs_debug_mask(D_CLIENT, "cccam: auto build set for version: %s build: %s",
|
---|
586 | cc_version, cc_build);
|
---|
587 | return;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | memcpy(cc_version, version[CC_DEFAULT_VERSION], strlen(
|
---|
591 | version[CC_DEFAULT_VERSION]));
|
---|
592 | memcpy(cc_build, build[CC_DEFAULT_VERSION], strlen(
|
---|
593 | build[CC_DEFAULT_VERSION]));
|
---|
594 |
|
---|
595 | cs_debug_mask(D_CLIENT, "cccam: auto version set: %s build: %s", cc_version, cc_build);
|
---|
596 | }
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * reader
|
---|
600 | * sends own version information to the CCCam server
|
---|
601 | */
|
---|
602 | int32_t cc_send_cli_data(struct s_client *cl) {
|
---|
603 | struct s_reader *rdr = cl->reader;
|
---|
604 |
|
---|
605 | int32_t i;
|
---|
606 | struct cc_data *cc = cl->cc;
|
---|
607 | const int32_t size = 20 + 8 + 6 + 26 + 4 + 28 + 1;
|
---|
608 |
|
---|
609 | cs_debug_mask(D_READER, "cccam: send client data");
|
---|
610 |
|
---|
611 | memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
|
---|
612 |
|
---|
613 | uint8_t *buf = cs_malloc(&buf, size, 0);
|
---|
614 |
|
---|
615 | memcpy(buf, rdr->r_usr, sizeof(rdr->r_usr));
|
---|
616 | memcpy(buf + 20, cc->node_id, 8);
|
---|
617 | buf[28] = rdr->cc_want_emu; // <-- Client want to have EMUs, 0 - NO; 1 - YES
|
---|
618 | memcpy(buf + 29, rdr->cc_version, sizeof(rdr->cc_version)); // cccam version (ascii)
|
---|
619 | memcpy(buf + 61, rdr->cc_build, sizeof(rdr->cc_build)); // build number (ascii)
|
---|
620 |
|
---|
621 | cs_debug_mask(D_READER, "%s sending own version: %s, build: %s", getprefix(),
|
---|
622 | rdr->cc_version, rdr->cc_build);
|
---|
623 |
|
---|
624 | i = cc_cmd_send(cl, buf, size, MSG_CLI_DATA);
|
---|
625 |
|
---|
626 | free(buf);
|
---|
627 |
|
---|
628 | return i;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * server
|
---|
633 | * sends version information to the client
|
---|
634 | */
|
---|
635 | int32_t cc_send_srv_data(struct s_client *cl) {
|
---|
636 | struct cc_data *cc = cl->cc;
|
---|
637 |
|
---|
638 | cs_debug_mask(D_CLIENT, "cccam: send server data");
|
---|
639 |
|
---|
640 | memcpy(cc->node_id, cc_node_id, sizeof(cc_node_id));
|
---|
641 |
|
---|
642 | uint8_t buf[0x48];
|
---|
643 | memset(buf, 0, 0x48);
|
---|
644 |
|
---|
645 | int32_t stealth = cl->account->cccstealth;
|
---|
646 | if (stealth == -1)
|
---|
647 | stealth = cfg.cc_stealth;
|
---|
648 | if (stealth) cc->node_id[7]++;
|
---|
649 |
|
---|
650 | memcpy(buf, cc->node_id, 8);
|
---|
651 |
|
---|
652 | char cc_build[7];
|
---|
653 | cc_check_version((char *) cfg.cc_version, cc_build);
|
---|
654 | memcpy(buf + 8, cfg.cc_version, sizeof(cfg.cc_version)); // cccam version (ascii)
|
---|
655 | memcpy(buf + 40, cc_build, sizeof(cc_build)); // build number (ascii)
|
---|
656 |
|
---|
657 | cs_debug_mask(D_CLIENT, "%s version: %s, build: %s nodeid: %s", getprefix(),
|
---|
658 | cfg.cc_version, cc_build, cs_hexdump(0, cc->peer_node_id, 8));
|
---|
659 |
|
---|
660 | return cc_cmd_send(cl, buf, 0x48, MSG_SRV_DATA);
|
---|
661 | }
|
---|
662 |
|
---|
663 | int32_t loop_check(uint8_t *myid, struct s_client *cl) {
|
---|
664 | if (!cl)
|
---|
665 | return 0;
|
---|
666 |
|
---|
667 | struct cc_data *cc = cl->cc;
|
---|
668 | if (!cc)
|
---|
669 | return 0;
|
---|
670 |
|
---|
671 | return !memcmp(myid, cc->peer_node_id, sizeof(cc->peer_node_id)); // same nodeid? ignore
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * reader
|
---|
676 | * retrieves the next waiting ecm request
|
---|
677 | */
|
---|
678 | int32_t cc_get_nxt_ecm(struct s_client *cl) {
|
---|
679 | struct cc_data *cc = cl->cc;
|
---|
680 | ECM_REQUEST *er, *ern = NULL;
|
---|
681 | int32_t n, i, pending=0;
|
---|
682 | time_t t;
|
---|
683 |
|
---|
684 | t = time(NULL);
|
---|
685 | n = -1;
|
---|
686 | for (i = 0; i < CS_MAXPENDING; i++) {
|
---|
687 | er = &cl->ecmtask[i];
|
---|
688 | if ((t - (uint32_t) er->tps.time > ((cfg.ctimeout + 500)
|
---|
689 | / 1000) + 1) && (er->rc >= 10)) // drop timeouts
|
---|
690 | {
|
---|
691 | er->rc = 0;
|
---|
692 | #ifdef WITH_LB
|
---|
693 | send_reader_stat(cl->reader, er, E_TIMEOUT);
|
---|
694 | #endif
|
---|
695 | }
|
---|
696 |
|
---|
697 | else if (er->rc >= 10 && er->rc != 101) { // stil active and waiting
|
---|
698 | pending++;
|
---|
699 | if (loop_check(cc->peer_node_id, er->client)) {
|
---|
700 | er->rc = E_RDR_NOTFOUND;
|
---|
701 | er->rcEx = E2_CCCAM_LOOP;
|
---|
702 | cs_debug_mask(D_READER, "%s ecm loop detected! client %s (%8X)",
|
---|
703 | getprefix(), er->client->account->usr, er->client->thread);
|
---|
704 | write_ecm_answer(cl->reader, &cl->ecmtask[i]);
|
---|
705 | }
|
---|
706 | else
|
---|
707 | // search for the ecm with the lowest time, this should be the next to go
|
---|
708 | if (n < 0 || (ern->tps.time - er->tps.time < 0)) {
|
---|
709 |
|
---|
710 | //check for already pending:
|
---|
711 | if (cc->extended_mode) {
|
---|
712 | int32_t j,found;
|
---|
713 | ECM_REQUEST *erx;
|
---|
714 | for (found=j=0;j<CS_MAXPENDING;j++) {
|
---|
715 | erx = &cl->ecmtask[j];
|
---|
716 | if (i!=j && erx->rc == 101 &&
|
---|
717 | er->caid==erx->caid &&
|
---|
718 | er->ecmd5==erx->ecmd5) {
|
---|
719 | found=1;
|
---|
720 | break;
|
---|
721 | }
|
---|
722 | }
|
---|
723 | if (!found) {
|
---|
724 | n = i;
|
---|
725 | ern = er;
|
---|
726 | }
|
---|
727 | }
|
---|
728 | else {
|
---|
729 | n = i;
|
---|
730 | ern = er;
|
---|
731 | }
|
---|
732 | }
|
---|
733 | }
|
---|
734 | }
|
---|
735 | cl->pending=pending;
|
---|
736 | return n;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * sends the secret cmd05 answer to the server
|
---|
741 | */
|
---|
742 | int32_t send_cmd05_answer(struct s_client *cl) {
|
---|
743 | struct s_reader *rdr = cl->reader;
|
---|
744 | struct cc_data *cc = cl->cc;
|
---|
745 | if (!cc->cmd05_active || !rdr->available) //exit if not in cmd05 or waiting for ECM answer
|
---|
746 | return 0;
|
---|
747 |
|
---|
748 | cc->cmd05_active--;
|
---|
749 | if (cc->cmd05_active)
|
---|
750 | return 0;
|
---|
751 |
|
---|
752 | uint8_t *data = cc->cmd05_data;
|
---|
753 | cc_cmd05_mode cmd05_mode = MODE_UNKNOWN;
|
---|
754 |
|
---|
755 | // by Project:Keynation
|
---|
756 | switch (cc->cmd05_data_len) {
|
---|
757 | case 0: { //payload 0, return with payload 0!
|
---|
758 | cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
|
---|
759 | cmd05_mode = MODE_LEN0;
|
---|
760 | break;
|
---|
761 | }
|
---|
762 | case 256: {
|
---|
763 | cmd05_mode = cc->cmd05_mode;
|
---|
764 | switch (cmd05_mode) {
|
---|
765 | case MODE_PLAIN: { //Send plain unencrypted back
|
---|
766 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
767 | break;
|
---|
768 | }
|
---|
769 | case MODE_AES: { //encrypt with received aes128 key:
|
---|
770 | AES_KEY key;
|
---|
771 | uint8_t aeskey[16];
|
---|
772 | uint8_t out[256];
|
---|
773 |
|
---|
774 | memcpy(aeskey, cc->cmd05_aeskey, 16);
|
---|
775 | memset(&key, 0, sizeof(key));
|
---|
776 |
|
---|
777 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
778 | int32_t i;
|
---|
779 | for (i = 0; i < 256; i += 16)
|
---|
780 | AES_encrypt((unsigned char *) data + i, (unsigned char *) &out
|
---|
781 | + i, &key);
|
---|
782 |
|
---|
783 | cc_cmd_send(cl, out, 256, MSG_CMD_05);
|
---|
784 | break;
|
---|
785 | }
|
---|
786 | case MODE_CC_CRYPT: { //encrypt with cc_crypt:
|
---|
787 | cc_crypt(&cc->cmd05_cryptkey, data, 256, ENCRYPT);
|
---|
788 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
789 | break;
|
---|
790 | }
|
---|
791 | case MODE_RC4_CRYPT: {//special xor crypt:
|
---|
792 | cc_rc4_crypt(&cc->cmd05_cryptkey, data, 256, DECRYPT);
|
---|
793 | cc_cmd_send(cl, data, 256, MSG_CMD_05);
|
---|
794 | break;
|
---|
795 | }
|
---|
796 | default:
|
---|
797 | cmd05_mode = MODE_UNKNOWN;
|
---|
798 | }
|
---|
799 | break;
|
---|
800 | }
|
---|
801 | default:
|
---|
802 | cmd05_mode = MODE_UNKNOWN;
|
---|
803 | }
|
---|
804 |
|
---|
805 | //unhandled types always needs cycle connection after 50 ECMs!!
|
---|
806 | if (cmd05_mode == MODE_UNKNOWN) {
|
---|
807 | cc_cmd_send(cl, NULL, 0, MSG_CMD_05);
|
---|
808 | if (!cc->max_ecms) { //max_ecms already set?
|
---|
809 | cc->max_ecms = 50;
|
---|
810 | cc->ecm_counter = 0;
|
---|
811 | }
|
---|
812 | }
|
---|
813 | cs_debug_mask(D_READER, "%s sending CMD_05 back! MODE: %s len=%d",
|
---|
814 | getprefix(), cmd05_mode_name[cmd05_mode], cc->cmd05_data_len);
|
---|
815 |
|
---|
816 | cc->cmd05NOK = 1;
|
---|
817 | return 1;
|
---|
818 | }
|
---|
819 |
|
---|
820 | int32_t get_UA_ofs(uint16_t caid) {
|
---|
821 | int32_t ofs = 0;
|
---|
822 | switch (caid >> 8) {
|
---|
823 | case 0x05: //VIACCESS:
|
---|
824 | case 0x0D: //CRYPTOWORKS:
|
---|
825 | ofs = 1;
|
---|
826 | break;
|
---|
827 | case 0x4B: //TONGFANG:
|
---|
828 | case 0x09: //VIDEOGUARD:
|
---|
829 | case 0x0B: //CONAX:
|
---|
830 | case 0x18: //NAGRA:
|
---|
831 | case 0x17: //BETACRYPT
|
---|
832 | case 0x06: //IRDETO:
|
---|
833 | ofs = 2;
|
---|
834 | break;
|
---|
835 | }
|
---|
836 | return ofs;
|
---|
837 | }
|
---|
838 |
|
---|
839 | int32_t UA_len(uint8_t *ua) {
|
---|
840 | int32_t i, len=0;
|
---|
841 | for (i=0;i<8;i++)
|
---|
842 | if (ua[i]) len++;
|
---|
843 | return len;
|
---|
844 | }
|
---|
845 |
|
---|
846 | void UA_left(uint8_t *in, uint8_t *out, int32_t len) {
|
---|
847 | int32_t ofs = 0;
|
---|
848 | int32_t maxlen = 8;
|
---|
849 | int32_t orglen = len;
|
---|
850 | while (len) {
|
---|
851 | memset(out, 0, orglen);
|
---|
852 | memcpy(out, in+ofs, len);
|
---|
853 | if (out[0]) break;
|
---|
854 | ofs++;
|
---|
855 | maxlen--;
|
---|
856 | if (len>maxlen)
|
---|
857 | len=maxlen;
|
---|
858 | }
|
---|
859 | }
|
---|
860 |
|
---|
861 | void UA_right(uint8_t *in, uint8_t *out, int32_t len) {
|
---|
862 | int32_t ofs = 0;
|
---|
863 | while (len) {
|
---|
864 | memcpy(out+ofs, in, len);
|
---|
865 | len--;
|
---|
866 | if (out[len]) break;
|
---|
867 | ofs++;
|
---|
868 | out[0]=0;
|
---|
869 | }
|
---|
870 | }
|
---|
871 |
|
---|
872 | /**
|
---|
873 | * cccam uses UA right justified
|
---|
874 | **/
|
---|
875 | void cc_UA_oscam2cccam(uint8_t *in, uint8_t *out, uint16_t caid) {
|
---|
876 | uint8_t tmp[8];
|
---|
877 | memset(out, 0, 8);
|
---|
878 | memset(tmp, 0, 8);
|
---|
879 | //switch (caid>>8) {
|
---|
880 | // case 0x17: //IRDETO/Betacrypt:
|
---|
881 | // //oscam: AA BB CC DD 00 00 00 00
|
---|
882 | // //cccam: 00 00 00 00 DD AA BB CC
|
---|
883 | // out[4] = in[3]; //Hexbase
|
---|
884 | // out[5] = in[0];
|
---|
885 | // out[6] = in[1];
|
---|
886 | // out[7] = in[2];
|
---|
887 | // return;
|
---|
888 | //
|
---|
889 | // //Place here your own adjustments!
|
---|
890 | //}
|
---|
891 | hexserial_to_newcamd(in, tmp+2, caid);
|
---|
892 | UA_right(tmp, out, 8);
|
---|
893 | }
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * oscam has a special format, depends on offset or type:
|
---|
897 | **/
|
---|
898 | void cc_UA_cccam2oscam(uint8_t *in, uint8_t *out, uint16_t caid) {
|
---|
899 | uint8_t tmp[8];
|
---|
900 | memset(out, 0, 8);
|
---|
901 | memset(tmp, 0, 8);
|
---|
902 | //switch(caid>>8) {
|
---|
903 | // case 0x17: //IRDETO/Betacrypt:
|
---|
904 | // //cccam: 00 00 00 00 DD AA BB CC
|
---|
905 | // //oscam: AA BB CC DD 00 00 00 00
|
---|
906 | // out[0] = in[5];
|
---|
907 | // out[1] = in[6];
|
---|
908 | // out[2] = in[7];
|
---|
909 | // out[3] = in[4]; //Hexbase
|
---|
910 | // return;
|
---|
911 |
|
---|
912 | // //Place here your own adjustments!
|
---|
913 | //}
|
---|
914 | int32_t ofs = get_UA_ofs(caid);
|
---|
915 | int32_t len = 8-ofs;
|
---|
916 | UA_left(in, tmp+ofs, len);
|
---|
917 | newcamd_to_hexserial(tmp, out, caid);
|
---|
918 | }
|
---|
919 |
|
---|
920 | void cc_SA_oscam2cccam(uint8_t *in, uint8_t *out) {
|
---|
921 | memcpy(out, in, 4);
|
---|
922 | }
|
---|
923 |
|
---|
924 | void cc_SA_cccam2oscam(uint8_t *in, uint8_t *out) {
|
---|
925 | memcpy(out, in, 4);
|
---|
926 | }
|
---|
927 |
|
---|
928 | int32_t cc_UA_valid(uint8_t *ua) {
|
---|
929 | int32_t i;
|
---|
930 | for (i = 0; i < 8; i++)
|
---|
931 | if (ua[i])
|
---|
932 | return 1;
|
---|
933 | return 0;
|
---|
934 | }
|
---|
935 |
|
---|
936 | /**
|
---|
937 | * Updates AU Data: UA (Unique ID / Hexserial) und SA (Shared ID - Provider)
|
---|
938 | */
|
---|
939 | void set_au_data(struct s_client *cl, struct s_reader *rdr, struct cc_card *card, ECM_REQUEST *cur_er) {
|
---|
940 | if (rdr->audisabled || !cc_UA_valid(card->hexserial))
|
---|
941 | return;
|
---|
942 |
|
---|
943 | struct cc_data *cc = cl->cc;
|
---|
944 | cc->last_emm_card = card;
|
---|
945 |
|
---|
946 | cc_UA_cccam2oscam(card->hexserial, rdr->hexserial, rdr->caid);
|
---|
947 |
|
---|
948 | cs_debug_mask(D_EMM,
|
---|
949 | "%s au info: caid %04X UA: %s",
|
---|
950 | getprefix(), card->caid, cs_hexdump(0,
|
---|
951 | rdr->hexserial, 8));
|
---|
952 |
|
---|
953 | rdr->nprov = 0;
|
---|
954 | LL_ITER it2 = ll_iter_create(card->providers);
|
---|
955 | struct cc_provider *provider;
|
---|
956 | int32_t p = 0;
|
---|
957 | while ((provider = ll_iter_next(&it2))) {
|
---|
958 | if (!cur_er || provider->prov == cur_er->prid || !provider->prov || !cur_er->prid) {
|
---|
959 | rdr->prid[p][0] = provider->prov >> 24;
|
---|
960 | rdr->prid[p][1] = provider->prov >> 16;
|
---|
961 | rdr->prid[p][2] = provider->prov >> 8;
|
---|
962 | rdr->prid[p][3] = provider->prov & 0xFF;
|
---|
963 | cc_SA_cccam2oscam(provider->sa, rdr->sa[p]);
|
---|
964 |
|
---|
965 | cs_debug_mask(D_EMM, "%s au info: provider: %06lX:%02X%02X%02X%02X", getprefix(),
|
---|
966 | provider->prov,
|
---|
967 | provider->sa[0], provider->sa[1], provider->sa[2], provider->sa[3]);
|
---|
968 |
|
---|
969 | p++;
|
---|
970 | rdr->nprov = p;
|
---|
971 | if (p >= CS_MAXPROV) break;
|
---|
972 | }
|
---|
973 | }
|
---|
974 |
|
---|
975 | if (!rdr->nprov) { //No Providers? Add null-provider:
|
---|
976 | memset(rdr->prid[0], 0, sizeof(rdr->prid[0]));
|
---|
977 | rdr->nprov = 1;
|
---|
978 | }
|
---|
979 |
|
---|
980 | rdr->caid = card->caid;
|
---|
981 | if (cur_er)
|
---|
982 | rdr->auprovid = cur_er->prid;
|
---|
983 | }
|
---|
984 |
|
---|
985 | int32_t same_first_node(struct cc_card *card1, struct cc_card *card2) {
|
---|
986 | uint8_t * node1 = ll_has_elements(card1->remote_nodes);
|
---|
987 | uint8_t * node2 = ll_has_elements(card2->remote_nodes);
|
---|
988 |
|
---|
989 | if (!node1 && !node2) return 1; //both NULL, same!
|
---|
990 |
|
---|
991 | if (!node1 || !node2) return 0; //one NULL, not same!
|
---|
992 |
|
---|
993 | return !memcmp(node1, node2, 8); //same?
|
---|
994 | }
|
---|
995 |
|
---|
996 | int32_t same_card2(struct cc_card *card1, struct cc_card *card2) {
|
---|
997 | return (card1->caid == card2->caid &&
|
---|
998 | card1->card_type == card2->card_type &&
|
---|
999 | card1->sidtab == card2->sidtab &&
|
---|
1000 | card1->grp == card2->grp &&
|
---|
1001 | !memcmp(card1->hexserial, card2->hexserial, sizeof(card1->hexserial)));
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | int32_t same_card(struct cc_card *card1, struct cc_card *card2) {
|
---|
1005 | return (card1->remote_id == card2->remote_id &&
|
---|
1006 | same_card2(card1, card2) &&
|
---|
1007 | same_first_node(card1, card2));
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * reader
|
---|
1012 | * sends a ecm request to the connected CCCam Server
|
---|
1013 | */
|
---|
1014 | int32_t cc_send_ecm(struct s_client *cl, ECM_REQUEST *er, uchar *buf) {
|
---|
1015 | struct s_reader *rdr = cl->reader;
|
---|
1016 |
|
---|
1017 | //cs_debug_mask(D_TRACE, "%s cc_send_ecm", getprefix());
|
---|
1018 | if (!rdr->tcp_connected)
|
---|
1019 | cc_cli_connect(cl);
|
---|
1020 |
|
---|
1021 | int32_t n, h = -1;
|
---|
1022 | struct cc_data *cc = cl->cc;
|
---|
1023 | struct cc_card *card = NULL;
|
---|
1024 | LL_ITER it;
|
---|
1025 | ECM_REQUEST *cur_er;
|
---|
1026 | struct timeb cur_time;
|
---|
1027 | cs_ftime(&cur_time);
|
---|
1028 |
|
---|
1029 | if (!cc || (cl->pfd < 1) || !rdr->tcp_connected) {
|
---|
1030 | if (er) {
|
---|
1031 | er->rc = E_RDR_NOTFOUND;
|
---|
1032 | er->rcEx = E2_CCCAM_NOCARD;
|
---|
1033 | cs_debug_mask(D_READER, "%s server not init! ccinit=%d pfd=%d",
|
---|
1034 | rdr->label, cc ? 1 : 0, cl->pfd);
|
---|
1035 | write_ecm_answer(rdr, er);
|
---|
1036 | }
|
---|
1037 | //cc_cli_close(cl);
|
---|
1038 | return 0;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | if (rdr->tcp_connected != 2) {
|
---|
1042 | cs_debug_mask(D_READER, "%s Waiting for CARDS", getprefix());
|
---|
1043 | return 0;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | //No Card? Waiting for shares
|
---|
1047 | if (!ll_has_elements(cc->cards)) {
|
---|
1048 | rdr->fd_error++;
|
---|
1049 | cs_debug_mask(D_READER, "%s NO CARDS!", getprefix());
|
---|
1050 | return 0;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | cc->just_logged_in = 0;
|
---|
1054 |
|
---|
1055 | if (!cc->extended_mode) {
|
---|
1056 | //Without extended mode, only one ecm at a time could be send
|
---|
1057 | //this is a limitation of "O" CCCam
|
---|
1058 | if (cs_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
1059 | cs_debug_mask(D_READER,
|
---|
1060 | "%s ecm trylock: ecm busy, retrying later after msg-receive",
|
---|
1061 | getprefix());
|
---|
1062 |
|
---|
1063 | struct timeb timeout;
|
---|
1064 | timeout = cc->ecm_time;
|
---|
1065 | uint32_t tt = cfg.ctimeout+500;
|
---|
1066 | timeout.time += tt / 1000;
|
---|
1067 | timeout.millitm += tt % 1000;
|
---|
1068 |
|
---|
1069 | if (comp_timeb(&cur_time, &timeout) < 0) { //TODO: Configuration?
|
---|
1070 | return 0; //pending send...
|
---|
1071 | } else {
|
---|
1072 | cs_debug_mask(D_READER,
|
---|
1073 | "%s unlocked-cycleconnection! timeout %ds",
|
---|
1074 | getprefix(), tt / 1000);
|
---|
1075 | //cc_cycle_connection();
|
---|
1076 | cc_cli_close(cl, TRUE);
|
---|
1077 | return 0;
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 | cs_debug_mask(D_READER, "cccam: ecm trylock: got lock");
|
---|
1081 | }
|
---|
1082 | do {
|
---|
1083 | cc->ecm_time = cur_time;
|
---|
1084 | rdr->available = cc->extended_mode;
|
---|
1085 |
|
---|
1086 | //Search next ECM to send:
|
---|
1087 | if ((n = cc_get_nxt_ecm(cl)) < 0) {
|
---|
1088 | if (!cc->extended_mode) {
|
---|
1089 | rdr->available = 1;
|
---|
1090 | cs_unlock(&cc->ecm_busy);
|
---|
1091 | }
|
---|
1092 | cs_debug_mask(D_READER, "%s no ecm pending!", getprefix());
|
---|
1093 | if (!cc_send_pending_emms(cl))
|
---|
1094 | send_cmd05_answer(cl);
|
---|
1095 | return 0; // no queued ecms
|
---|
1096 | }
|
---|
1097 | cur_er = &cl->ecmtask[n];
|
---|
1098 | cur_er->rc = 101; //mark ECM as already send
|
---|
1099 | cs_debug_mask(D_READER, "cccam: ecm-task %d", cur_er->idx);
|
---|
1100 |
|
---|
1101 | if (buf)
|
---|
1102 | memcpy(buf, cur_er->ecm, cur_er->l);
|
---|
1103 |
|
---|
1104 | struct cc_srvid cur_srvid;
|
---|
1105 | cur_srvid.sid = cur_er->srvid;
|
---|
1106 | cur_srvid.ecmlen = cur_er->l;
|
---|
1107 |
|
---|
1108 | while (cs_trylock(&cc->cards_busy)) {
|
---|
1109 | cs_debug_mask(D_TRACE, "%s trylock send_ecm cards waiting", getprefix());
|
---|
1110 | cs_sleepms(50);
|
---|
1111 | }
|
---|
1112 | //forward_origin:
|
---|
1113 | if (cfg.cc_forward_origin_card && cur_er->origin_reader == rdr && cur_er->origin_card) {
|
---|
1114 | it = ll_iter_create(cc->cards);
|
---|
1115 | struct cc_card *ncard;
|
---|
1116 | while ((ncard = ll_iter_next(&it))) {
|
---|
1117 | if (ncard == cur_er->origin_card) { //Search the origin card
|
---|
1118 | card = ncard; //found it, use it!
|
---|
1119 | break;
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | if (!card) {
|
---|
1125 | it = ll_iter_create(cc->cards);
|
---|
1126 | struct cc_card *ncard, *xcard = NULL;
|
---|
1127 | while ((ncard = ll_iter_next(&it))) {
|
---|
1128 | if (ncard->caid == cur_er->caid) { // caid matches
|
---|
1129 | if (is_sid_blocked(ncard, &cur_srvid))
|
---|
1130 | continue;
|
---|
1131 |
|
---|
1132 | if (ncard->caid>>8==0x18 && (!xcard || ncard->hop < xcard->hop))
|
---|
1133 | xcard = ncard; //remember card (D+ / 1810 fix) if request has no provider, but card has
|
---|
1134 |
|
---|
1135 | if (!cur_er->prid && !ll_count(ncard->providers)) { //card has no providers:
|
---|
1136 | if (h < 0 || ncard->hop < h || (ncard->hop == h
|
---|
1137 | && cc_UA_valid(ncard->hexserial))) {
|
---|
1138 | // ncard is closer
|
---|
1139 | card = ncard;
|
---|
1140 | h = ncard->hop; // ncard has been matched
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | }
|
---|
1144 | else { //card has providers
|
---|
1145 | LL_ITER it2 = ll_iter_create(ncard->providers);
|
---|
1146 | struct cc_provider *provider;
|
---|
1147 | while ((provider = ll_iter_next(&it2))) {
|
---|
1148 | if (provider->prov == cur_er->prid) { // provid matches
|
---|
1149 | if (h < 0 || ncard->hop < h || (ncard->hop == h
|
---|
1150 | && cc_UA_valid(ncard->hexserial))) {
|
---|
1151 | // ncard is closer
|
---|
1152 | card = ncard;
|
---|
1153 | h = ncard->hop; // ncard has been matched
|
---|
1154 | }
|
---|
1155 | }
|
---|
1156 | }
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 | }
|
---|
1160 | if (!card)
|
---|
1161 | card = xcard; //18xx: if request has no provider and we have no card, we try this card
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | if (card) {
|
---|
1165 | uint8_t *ecmbuf = cs_malloc(&ecmbuf, cur_er->l+13, 0);
|
---|
1166 |
|
---|
1167 | // build ecm message
|
---|
1168 | ecmbuf[0] = card->caid >> 8;
|
---|
1169 | ecmbuf[1] = card->caid & 0xff;
|
---|
1170 | ecmbuf[2] = cur_er->prid >> 24;
|
---|
1171 | ecmbuf[3] = cur_er->prid >> 16;
|
---|
1172 | ecmbuf[4] = cur_er->prid >> 8;
|
---|
1173 | ecmbuf[5] = cur_er->prid & 0xff;
|
---|
1174 | ecmbuf[6] = card->id >> 24;
|
---|
1175 | ecmbuf[7] = card->id >> 16;
|
---|
1176 | ecmbuf[8] = card->id >> 8;
|
---|
1177 | ecmbuf[9] = card->id & 0xff;
|
---|
1178 | ecmbuf[10] = cur_er->srvid >> 8;
|
---|
1179 | ecmbuf[11] = cur_er->srvid & 0xff;
|
---|
1180 | ecmbuf[12] = cur_er->l & 0xff;
|
---|
1181 | memcpy(ecmbuf + 13, cur_er->ecm, cur_er->l);
|
---|
1182 |
|
---|
1183 | uint8_t send_idx = 1;
|
---|
1184 | if (cc->extended_mode) {
|
---|
1185 | cc->server_ecm_idx++;
|
---|
1186 | if (cc->server_ecm_idx >= 256)
|
---|
1187 | cc->server_ecm_idx = 1;
|
---|
1188 | cc->g_flag = cc->server_ecm_idx; //Flag is used as index!
|
---|
1189 | send_idx = cc->g_flag;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl, send_idx, FALSE);
|
---|
1193 | if (eei) {
|
---|
1194 | eei->ecm_idx = cur_er->idx;
|
---|
1195 | eei->card = card;
|
---|
1196 | eei->srvid = cur_srvid;
|
---|
1197 | }
|
---|
1198 | else
|
---|
1199 | add_extended_ecm_idx(cl, send_idx, cur_er->idx, card, cur_srvid, 0);
|
---|
1200 |
|
---|
1201 | rdr->cc_currenthops = card->hop;
|
---|
1202 |
|
---|
1203 | cs_debug_mask(D_READER,
|
---|
1204 | "%s sending ecm for sid %04X(%d) to card %08x, hop %d, ecmtask %d",
|
---|
1205 | getprefix(), cur_er->srvid, cur_er->l, card->id, card->hop,
|
---|
1206 | cur_er->idx);
|
---|
1207 | cc_cmd_send(cl, ecmbuf, cur_er->l + 13, MSG_CW_ECM); // send ecm
|
---|
1208 |
|
---|
1209 | free(ecmbuf);
|
---|
1210 |
|
---|
1211 | //For EMM
|
---|
1212 | set_au_data(cl, rdr, card, cur_er);
|
---|
1213 | cs_unlock(&cc->cards_busy);
|
---|
1214 |
|
---|
1215 | if (cc->extended_mode)
|
---|
1216 | continue; //process next pending ecm!
|
---|
1217 | return 0;
|
---|
1218 | } else {
|
---|
1219 | //When connecting, it could happen than ecm requests come before all cards are received.
|
---|
1220 | //So if the last Message was a MSG_NEW_CARD, this "card receiving" is not already done
|
---|
1221 | //if this happens, we do not autoblock it and do not set rc status
|
---|
1222 | //So fallback could resolve it
|
---|
1223 | if (cc->last_msg != MSG_NEW_CARD && cc->last_msg != MSG_NEW_CARD_SIDINFO && !cc->just_logged_in) {
|
---|
1224 | cs_debug_mask(D_READER, "%s no suitable card on server", getprefix());
|
---|
1225 |
|
---|
1226 | cur_er->rc = E_RDR_NOTFOUND;
|
---|
1227 | cur_er->rcEx = E2_CCCAM_NOCARD;
|
---|
1228 | write_ecm_answer(rdr, cur_er);
|
---|
1229 | //cur_er->rc = 1;
|
---|
1230 | //cur_er->rcEx = 0;
|
---|
1231 | //cs_sleepms(300);
|
---|
1232 | rdr->last_s = rdr->last_g;
|
---|
1233 |
|
---|
1234 | //reopen all blocked sids for this srvid:
|
---|
1235 | it = ll_iter_create(cc->cards);
|
---|
1236 | while ((card = ll_iter_next(&it))) {
|
---|
1237 | if (card->caid == cur_er->caid) { // caid matches
|
---|
1238 | LL_ITER it2 = ll_iter_create(card->badsids);
|
---|
1239 | struct cc_srvid *srvid;
|
---|
1240 | while ((srvid = ll_iter_next(&it2)))
|
---|
1241 | if (srvid->ecmlen > 0 && sid_eq(srvid, &cur_srvid)) //ecmlen==0: From remote peer, so do not remove
|
---|
1242 | ll_iter_remove_data(&it2);
|
---|
1243 | }
|
---|
1244 | }
|
---|
1245 | }
|
---|
1246 | }
|
---|
1247 | cs_unlock(&cc->cards_busy);
|
---|
1248 |
|
---|
1249 | //process next pending ecm!
|
---|
1250 | } while (cc->extended_mode);
|
---|
1251 |
|
---|
1252 | if (!cc->extended_mode) {
|
---|
1253 | rdr->available = 1;
|
---|
1254 | cs_unlock(&cc->ecm_busy);
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | return -1;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | int32_t cc_abort_user_ecms(){
|
---|
1262 | int32_t n, i;
|
---|
1263 | time_t t;//, tls;
|
---|
1264 | struct cc_data *cc = rdr->cc;
|
---|
1265 |
|
---|
1266 | t=time((time_t *)0);
|
---|
1267 | for (i=1,n=1; i<CS_MAXPENDING; i++)
|
---|
1268 | {
|
---|
1269 | if ((t-cl->ecmtask[i].tps.time > ((cfg.ctimeout + 500) / 1000) + 1) &&
|
---|
1270 | (cl->ecmtask[i].rc>=10)) // drop timeouts
|
---|
1271 | {
|
---|
1272 | cl->ecmtask[i].rc=0;
|
---|
1273 | }
|
---|
1274 | int32_t td=abs(1000*(ecmtask[i].tps.time-cc->found->tps.time)+ecmtask[i].tps.millitm-cc->found->tps.millitm);
|
---|
1275 | if (ecmtask[i].rc>=10 && ecmtask[i].cidx==cc->found->cidx && &ecmtask[i]!=cc->found){
|
---|
1276 | cs_log("aborting idx:%d caid:%04x client:%d timedelta:%d",ecmtask[i].idx,ecmtask[i].caid,ecmtask[i].cidx,td);
|
---|
1277 | ecmtask[i].rc=0;
|
---|
1278 | ecmtask[i].rcEx=7;
|
---|
1279 | write_ecm_answer(rdr, fd_c2m, &ecmtask[i]);
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 | return n;
|
---|
1283 |
|
---|
1284 | }
|
---|
1285 | */
|
---|
1286 |
|
---|
1287 | int32_t cc_send_pending_emms(struct s_client *cl) {
|
---|
1288 | struct s_reader *rdr = cl->reader;
|
---|
1289 | struct cc_data *cc = cl->cc;
|
---|
1290 |
|
---|
1291 | LL_ITER it = ll_iter_create(cc->pending_emms);
|
---|
1292 | uint8_t *emmbuf;
|
---|
1293 | int32_t size = 0;
|
---|
1294 | if ((emmbuf = ll_iter_next(&it))) {
|
---|
1295 | if (!cc->extended_mode) {
|
---|
1296 | if (cs_trylock(&cc->ecm_busy) == EBUSY) { //Unlock by NOK or ECM ACK
|
---|
1297 | return 0; //send later with cc_send_ecm
|
---|
1298 | }
|
---|
1299 | rdr->available = 0;
|
---|
1300 | }
|
---|
1301 | size = emmbuf[11] + 12;
|
---|
1302 |
|
---|
1303 | cc->just_logged_in = 0;
|
---|
1304 | cs_ftime(&cc->ecm_time);
|
---|
1305 |
|
---|
1306 | cs_debug_mask(D_EMM, "%s emm send for card %08X", getprefix(), b2i(4,
|
---|
1307 | emmbuf + 7));
|
---|
1308 |
|
---|
1309 | cc_cmd_send(cl, emmbuf, size, MSG_EMM_ACK); // send emm
|
---|
1310 |
|
---|
1311 | ll_iter_remove_data(&it);
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | return size;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /**
|
---|
1318 | * READER only:
|
---|
1319 | * find card by hexserial
|
---|
1320 | * */
|
---|
1321 | struct cc_card *get_card_by_hexserial(struct s_client *cl, uint8_t *hexserial,
|
---|
1322 | uint16_t caid) {
|
---|
1323 | struct cc_data *cc = cl->cc;
|
---|
1324 | LL_ITER it = ll_iter_create(cc->cards);
|
---|
1325 | struct cc_card *card;
|
---|
1326 | while ((card = ll_iter_next(&it)))
|
---|
1327 | if (card->caid == caid && memcmp(card->hexserial, hexserial, 8) == 0) { //found it!
|
---|
1328 | break;
|
---|
1329 | }
|
---|
1330 | return card;
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | /**
|
---|
1334 | * EMM Procession
|
---|
1335 | * Copied from http://85.17.209.13:6100/file/8ec3c0c5d257/systems/cardclient/cccam2.c
|
---|
1336 | * ProcessEmm
|
---|
1337 | * */
|
---|
1338 | int32_t cc_send_emm(EMM_PACKET *ep) {
|
---|
1339 | struct s_client *cl = cur_client();
|
---|
1340 | struct s_reader *rdr = cl->reader;
|
---|
1341 |
|
---|
1342 | if (!rdr->tcp_connected)
|
---|
1343 | cc_cli_connect(cl);
|
---|
1344 |
|
---|
1345 | struct cc_data *cc = cl->cc;
|
---|
1346 |
|
---|
1347 | if (!cc || (cl->pfd < 1) || !rdr->tcp_connected) {
|
---|
1348 | cs_debug_mask(D_READER, "%s server not init! ccinit=%d pfd=%d", getprefix(), cc ? 1 : 0,
|
---|
1349 | cl->pfd);
|
---|
1350 | return 0;
|
---|
1351 | }
|
---|
1352 | if (rdr->audisabled) {
|
---|
1353 | cs_debug_mask(D_READER, "%s au is disabled", getprefix());
|
---|
1354 | return 0;
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | uint16_t caid = b2i(2, ep->caid);
|
---|
1358 |
|
---|
1359 | //Last used card is first card of current_cards:
|
---|
1360 | while (cs_trylock(&cc->cards_busy)) {
|
---|
1361 | cs_debug_mask(D_TRACE, "trylock send_emm cards waiting", getprefix());
|
---|
1362 | cs_sleepms(50);
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | struct cc_card *emm_card = cc->last_emm_card;
|
---|
1366 |
|
---|
1367 | if (!emm_card) {
|
---|
1368 | uint8_t hs[8];
|
---|
1369 | cc_UA_oscam2cccam(ep->hexserial, hs, caid);
|
---|
1370 | cs_debug_mask(D_EMM,
|
---|
1371 | "%s au info: searching card for caid %04X oscam-UA: %s",
|
---|
1372 | getprefix(), b2i(2, ep->caid), cs_hexdump(0, ep->hexserial, 8));
|
---|
1373 | cs_debug_mask(D_EMM,
|
---|
1374 | "%s au info: searching card for caid %04X cccam-UA: %s",
|
---|
1375 | getprefix(), b2i(2, ep->caid), cs_hexdump(0, hs, 8));
|
---|
1376 |
|
---|
1377 | emm_card = get_card_by_hexserial(cl, hs, caid);
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (!emm_card) { //Card for emm not found!
|
---|
1381 | cs_debug_mask(D_EMM, "%s emm for client %8X not possible, no card found!",
|
---|
1382 | getprefix(), ep->client->thread);
|
---|
1383 | cs_unlock(&cc->cards_busy);
|
---|
1384 | return 0;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | cs_debug_mask(D_EMM,
|
---|
1388 | "%s emm received for client %8X caid %04X for card %08X",
|
---|
1389 | getprefix(), ep->client->thread, caid, emm_card->id);
|
---|
1390 |
|
---|
1391 | int32_t size = ep->l + 12;
|
---|
1392 | uint8_t *emmbuf = cs_malloc(&emmbuf, size, QUITERROR);
|
---|
1393 | memset(emmbuf, 0, size);
|
---|
1394 |
|
---|
1395 | // build ecm message
|
---|
1396 | emmbuf[0] = ep->caid[0];
|
---|
1397 | emmbuf[1] = ep->caid[1];
|
---|
1398 | emmbuf[2] = 0;
|
---|
1399 | emmbuf[3] = ep->provid[0];
|
---|
1400 | emmbuf[4] = ep->provid[1];
|
---|
1401 | emmbuf[5] = ep->provid[2];
|
---|
1402 | emmbuf[6] = ep->provid[3];
|
---|
1403 | emmbuf[7] = emm_card->id >> 24;
|
---|
1404 | emmbuf[8] = emm_card->id >> 16;
|
---|
1405 | emmbuf[9] = emm_card->id >> 8;
|
---|
1406 | emmbuf[10] = emm_card->id & 0xff;
|
---|
1407 | emmbuf[11] = ep->l;
|
---|
1408 | memcpy(emmbuf + 12, ep->emm, ep->l);
|
---|
1409 |
|
---|
1410 | cs_unlock(&cc->cards_busy);
|
---|
1411 |
|
---|
1412 | ll_append(cc->pending_emms, emmbuf);
|
---|
1413 | cc_send_pending_emms(cl);
|
---|
1414 |
|
---|
1415 | #ifdef WEBIF
|
---|
1416 | rdr->emmwritten[ep->type]++;
|
---|
1417 | #endif
|
---|
1418 | return 1;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | void cc_free_card(struct cc_card *card) {
|
---|
1422 | if (!card)
|
---|
1423 | return;
|
---|
1424 |
|
---|
1425 | ll_destroy_data(card->providers);
|
---|
1426 | ll_destroy_data(card->badsids);
|
---|
1427 | ll_destroy_data(card->goodsids);
|
---|
1428 | ll_destroy_data(card->remote_nodes);
|
---|
1429 |
|
---|
1430 | add_garbage(card);
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | struct cc_card *cc_get_card_by_id(uint32_t card_id, LLIST *cards) {
|
---|
1434 | if (!cards)
|
---|
1435 | return NULL;
|
---|
1436 | LL_ITER it = ll_iter_create(cards);
|
---|
1437 | struct cc_card *card;
|
---|
1438 | while ((card=ll_iter_next(&it))) {
|
---|
1439 | if (card->id==card_id) {
|
---|
1440 | break;
|
---|
1441 | }
|
---|
1442 | }
|
---|
1443 | return card;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | void cc_free_cardlist(LLIST *card_list, int32_t destroy_list) {
|
---|
1447 | if (card_list) {
|
---|
1448 | LL_ITER it = ll_iter_create(card_list);
|
---|
1449 | struct cc_card *card;
|
---|
1450 | while ((card = ll_iter_next(&it))) {
|
---|
1451 | cc_free_card(card);
|
---|
1452 | ll_iter_remove(&it);
|
---|
1453 | }
|
---|
1454 | if (destroy_list)
|
---|
1455 | ll_destroy_data(card_list);
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | /**
|
---|
1460 | * Clears and free the cc datas
|
---|
1461 | */
|
---|
1462 | void cc_free(struct s_client *cl) {
|
---|
1463 | struct cc_data *cc = cl->cc;
|
---|
1464 | if (!cc) return;
|
---|
1465 |
|
---|
1466 | cc->mode = CCCAM_MODE_SHUTDOWN;
|
---|
1467 |
|
---|
1468 | cl->cc=NULL;
|
---|
1469 |
|
---|
1470 | while (cs_trylock(&cc->lockcmd)) cs_sleepms(110);
|
---|
1471 | cs_lock(&cc->ecm_busy);
|
---|
1472 | cs_lock(&cc->cards_busy);
|
---|
1473 |
|
---|
1474 | cs_debug_mask(D_TRACE, "exit cccam1/3");
|
---|
1475 | cc_free_cardlist(cc->cards, TRUE);
|
---|
1476 | ll_destroy_data(cc->pending_emms);
|
---|
1477 | if (cc->extended_ecm_idx)
|
---|
1478 | free_extended_ecm_idx(cc);
|
---|
1479 | ll_destroy_data(cc->extended_ecm_idx);
|
---|
1480 |
|
---|
1481 | cs_unlock(&cc->lockcmd);
|
---|
1482 | pthread_mutex_destroy(&cc->lockcmd);
|
---|
1483 |
|
---|
1484 | cs_debug_mask(D_TRACE, "exit cccam2/3");
|
---|
1485 | cs_unlock(&cc->ecm_busy);
|
---|
1486 | pthread_mutex_destroy(&cc->ecm_busy);
|
---|
1487 |
|
---|
1488 | cs_unlock(&cc->cards_busy);
|
---|
1489 | pthread_mutex_destroy(&cc->cards_busy);
|
---|
1490 |
|
---|
1491 | add_garbage(cc->prefix);
|
---|
1492 | add_garbage(cc);
|
---|
1493 |
|
---|
1494 | cs_debug_mask(D_TRACE, "exit cccam3/3");
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | int32_t is_null_dcw(uint8_t *dcw) {
|
---|
1498 | int32_t i;
|
---|
1499 | for (i = 0; i < 15; i++)
|
---|
1500 | if (dcw[i])
|
---|
1501 | return 0;
|
---|
1502 | return 1;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | /*int32_t is_dcw_corrupted(uchar *dcw)
|
---|
1506 | {
|
---|
1507 | int32_t i;
|
---|
1508 | int32_t c, cs;
|
---|
1509 |
|
---|
1510 | for (i=0; i<16; i+=4)
|
---|
1511 | {
|
---|
1512 | c = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1513 | cs = dcw[i+3];
|
---|
1514 | if (cs!=c) return (1);
|
---|
1515 | }
|
---|
1516 | return 0;
|
---|
1517 | }
|
---|
1518 | */
|
---|
1519 |
|
---|
1520 | int32_t check_extended_mode(struct s_client *cl, char *msg) {
|
---|
1521 | //Extended mode: if PARTNER String is ending with [PARAM], extended mode is activated
|
---|
1522 | //For future compatibilty the syntax should be compatible with
|
---|
1523 | //[PARAM1,PARAM2...PARAMn]
|
---|
1524 | //
|
---|
1525 | // EXT: Extended ECM Mode: Multiple ECMs could be send and received
|
---|
1526 | // ECMs are numbered, Flag (byte[0] is the index
|
---|
1527 | //
|
---|
1528 | // SID: Exchange of good sids/bad sids activated (like cccam 2.2.x)
|
---|
1529 | // card exchange command MSG_NEW_CARD_SIDINFO instead MSG_NEW_CARD is used
|
---|
1530 | //
|
---|
1531 |
|
---|
1532 | struct cc_data *cc = cl->cc;
|
---|
1533 | char *saveptr1 = NULL;
|
---|
1534 | int32_t has_param = 0;
|
---|
1535 | char *p = strtok_r(msg, "[", &saveptr1);
|
---|
1536 | while (p) {
|
---|
1537 | p = strtok_r(NULL, ",]", &saveptr1);
|
---|
1538 | if (p && strncmp(p, "EXT", 3) == 0) {
|
---|
1539 | cc->extended_mode = 1;
|
---|
1540 | cs_debug_mask(D_CLIENT, "%s extended ECM mode", getprefix());
|
---|
1541 | has_param = 1;
|
---|
1542 | }
|
---|
1543 | else if (p && strncmp(p, "SID", 3)==0) {
|
---|
1544 | cc->cccam220 = 1;
|
---|
1545 | cs_debug_mask(D_CLIENT, "%s extra SID mode", getprefix());
|
---|
1546 | has_param = 1;
|
---|
1547 | }
|
---|
1548 | }
|
---|
1549 | return has_param;
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | void cc_idle() {
|
---|
1553 | struct s_client *cl = cur_client();
|
---|
1554 | struct s_reader *rdr = cl->reader;
|
---|
1555 | struct cc_data *cc = cl->cc;
|
---|
1556 |
|
---|
1557 | if (rdr && rdr->cc_keepalive && !rdr->tcp_connected)
|
---|
1558 | cc_cli_connect(cl);
|
---|
1559 |
|
---|
1560 | if (!rdr || !rdr->tcp_connected || !cl || !cc)
|
---|
1561 | return;
|
---|
1562 |
|
---|
1563 | if (rdr->cc_keepalive) {
|
---|
1564 | if (cc->answer_on_keepalive + 55 <= time(NULL)) {
|
---|
1565 | cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE);
|
---|
1566 | cs_debug_mask(D_READER, "cccam: keepalive");
|
---|
1567 | cc->answer_on_keepalive = time(NULL);
|
---|
1568 | }
|
---|
1569 | }
|
---|
1570 | else
|
---|
1571 | {
|
---|
1572 | int32_t rto = abs(rdr->last_s - rdr->last_g);
|
---|
1573 | if (rto >= (rdr->tcp_rto*60))
|
---|
1574 | network_tcp_connection_close(cl, cl->udp_fd);
|
---|
1575 | }
|
---|
1576 | }
|
---|
1577 |
|
---|
1578 | struct cc_card *read_card(uint8_t *buf, int32_t ext) {
|
---|
1579 | struct cc_card *card = cs_malloc(&card, sizeof(struct cc_card), QUITERROR);
|
---|
1580 | memset(card, 0, sizeof(struct cc_card));
|
---|
1581 |
|
---|
1582 | int32_t nprov, nassign = 0, nreject = 0, offset = 21;
|
---|
1583 |
|
---|
1584 | card->providers = ll_create();
|
---|
1585 | card->badsids = ll_create();
|
---|
1586 | card->goodsids = ll_create();
|
---|
1587 | card->remote_nodes = ll_create();
|
---|
1588 | card->id = b2i(4, buf);
|
---|
1589 | card->remote_id = b2i(4, buf + 4);
|
---|
1590 | card->caid = b2i(2, buf + 8);
|
---|
1591 | card->hop = buf[10];
|
---|
1592 | card->reshare = buf[11];
|
---|
1593 | memcpy(card->hexserial, buf + 12, 8); //HEXSERIAL!!
|
---|
1594 |
|
---|
1595 | //cs_debug_mask(D_CLIENT, "cccam: card %08x added, caid %04X, hop %d, key %s, count %d",
|
---|
1596 | // card->id, card->caid, card->hop, cs_hexdump(0, card->hexserial, 8),
|
---|
1597 | // ll_count(cc->cards));
|
---|
1598 |
|
---|
1599 | nprov = buf[20];
|
---|
1600 |
|
---|
1601 | if (ext) {
|
---|
1602 | nassign = buf[21];
|
---|
1603 | nreject = buf[22];
|
---|
1604 |
|
---|
1605 | offset += 2;
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | int32_t i;
|
---|
1609 | for (i = 0; i < nprov; i++) { // providers
|
---|
1610 | struct cc_provider *prov = cs_malloc(&prov, sizeof(struct cc_provider), QUITERROR);
|
---|
1611 | prov->prov = b2i(3, buf + offset);
|
---|
1612 | if (prov->prov == 0xFFFFFF && (card->caid >> 8) == 0x17)
|
---|
1613 | prov->prov = i;
|
---|
1614 | memcpy(prov->sa, buf + offset + 3, 4);
|
---|
1615 | //cs_debug_mask(D_CLIENT, " prov %d, %06x, sa %08x", i + 1, prov->prov, b2i(4,
|
---|
1616 | // prov->sa));
|
---|
1617 |
|
---|
1618 | ll_append(card->providers, prov);
|
---|
1619 | offset+=7;
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | uint8_t *ptr = buf + offset;
|
---|
1623 |
|
---|
1624 | if (ext) {
|
---|
1625 | for (i = 0; i < nassign; i++) {
|
---|
1626 | uint16_t sid = b2i(2, ptr);
|
---|
1627 | //cs_debug_mask(D_CLIENT, " assigned sid = %04X, added to good sid list", sid);
|
---|
1628 |
|
---|
1629 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
1630 | srvid->sid = sid;
|
---|
1631 | srvid->ecmlen = 0;
|
---|
1632 | ll_append(card->goodsids, srvid);
|
---|
1633 | ptr+=2;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | for (i = 0; i < nreject; i++) {
|
---|
1637 | uint16_t sid = b2i(2, ptr);
|
---|
1638 | //cs_debug_mask(D_CLIENT, " rejected sid = %04X, added to sid block list", sid);
|
---|
1639 |
|
---|
1640 | struct cc_srvid *srvid = cs_malloc(&srvid, sizeof(struct cc_srvid), QUITERROR);
|
---|
1641 | srvid->sid = sid;
|
---|
1642 | srvid->ecmlen = 0;
|
---|
1643 | ll_append(card->badsids, srvid);
|
---|
1644 | ptr+=2;
|
---|
1645 | }
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 | int32_t remote_count = ptr[0];
|
---|
1649 | ptr++;
|
---|
1650 | for (i = 0; i < remote_count; i++) {
|
---|
1651 | uint8_t *remote_node = cs_malloc(&remote_node, 8, QUITERROR);
|
---|
1652 | memcpy(remote_node, ptr, 8);
|
---|
1653 | ll_append(card->remote_nodes, remote_node);
|
---|
1654 | ptr+=8;
|
---|
1655 | }
|
---|
1656 | return card;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | void cc_card_removed(struct s_client *cl, uint32_t shareid) {
|
---|
1660 | struct cc_data *cc = cl->cc;
|
---|
1661 | struct cc_card *card;
|
---|
1662 | LL_ITER it = ll_iter_create(cc->cards);
|
---|
1663 |
|
---|
1664 | while ((card = ll_iter_next(&it))) {
|
---|
1665 | if (card->id == shareid) {// && card->sub_id == b2i (3, buf + 9)) {
|
---|
1666 | //cs_debug_mask(D_CLIENT, "cccam: card %08x removed, caid %04X, count %d",
|
---|
1667 | // card->id, card->caid, ll_count(cc->cards));
|
---|
1668 | ll_iter_remove(&it);
|
---|
1669 | if (cc->last_emm_card == card) {
|
---|
1670 | cc->last_emm_card = NULL;
|
---|
1671 | cs_debug_mask(D_READER, "%s current card %08x removed!",
|
---|
1672 | getprefix(), card->id);
|
---|
1673 | }
|
---|
1674 | free_extended_ecm_idx_by_card(cl, card);
|
---|
1675 |
|
---|
1676 | if (card->hop == 1) cc->num_hop1--;
|
---|
1677 | else if (card->hop == 2) cc->num_hop2--;
|
---|
1678 | else cc->num_hopx--;
|
---|
1679 |
|
---|
1680 | if (card->reshare == 0) cc->num_reshare0--;
|
---|
1681 | else if (card->reshare == 1) cc->num_reshare1--;
|
---|
1682 | else if (card->reshare == 2) cc->num_reshare2--;
|
---|
1683 | else cc->num_resharex--;
|
---|
1684 |
|
---|
1685 | cc_free_card(card);
|
---|
1686 | cc->card_removed_count++;
|
---|
1687 | //break;
|
---|
1688 | }
|
---|
1689 | }
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | void move_card_to_end(struct s_client * cl, struct cc_card *card_to_move) {
|
---|
1693 |
|
---|
1694 | struct cc_data *cc = cl->cc;
|
---|
1695 |
|
---|
1696 | LL_ITER it = ll_iter_create(cc->cards);
|
---|
1697 | struct cc_card *card;
|
---|
1698 | while ((card = ll_iter_next(&it))) {
|
---|
1699 | if (card == card_to_move) {
|
---|
1700 | ll_iter_remove(&it);
|
---|
1701 | break;
|
---|
1702 | }
|
---|
1703 | }
|
---|
1704 | if (card) {
|
---|
1705 | cs_debug_mask(D_READER, "%s CMD05: Moving card %08X to the end...", getprefix(), card_to_move->id);
|
---|
1706 | free_extended_ecm_idx_by_card(cl, card);
|
---|
1707 | ll_append(cc->cards, card_to_move);
|
---|
1708 | }
|
---|
1709 | }
|
---|
1710 |
|
---|
1711 |
|
---|
1712 |
|
---|
1713 | /*void fix_dcw(uchar *dcw)
|
---|
1714 | {
|
---|
1715 | int32_t i;
|
---|
1716 | for (i=0; i<16; i+=4)
|
---|
1717 | {
|
---|
1718 | dcw[i+3] = (dcw[i] + dcw[i+1] + dcw[i+2]) & 0xFF;
|
---|
1719 | }
|
---|
1720 | }*/
|
---|
1721 |
|
---|
1722 | void addParam(char *param, char *value)
|
---|
1723 | {
|
---|
1724 | if (strlen(param) < 4)
|
---|
1725 | strcat(param, value);
|
---|
1726 | else {
|
---|
1727 | strcat(param, ",");
|
---|
1728 | strcat(param, value);
|
---|
1729 | }
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | static void chk_peer_node_for_oscam(struct cc_data *cc)
|
---|
1733 | {
|
---|
1734 | if (!cc->is_oscam_cccam) {//Allready discovered oscam-cccam:
|
---|
1735 | uint16_t sum = 0x1234;
|
---|
1736 | uint16_t recv_sum = (cc->peer_node_id[6] << 8)
|
---|
1737 | | cc->peer_node_id[7];
|
---|
1738 | int32_t i;
|
---|
1739 | for (i = 0; i < 6; i++) {
|
---|
1740 | sum += cc->peer_node_id[i];
|
---|
1741 | }
|
---|
1742 | //Create special data to detect oscam-cccam:
|
---|
1743 | cc->is_oscam_cccam = sum == recv_sum;
|
---|
1744 | }
|
---|
1745 | }
|
---|
1746 |
|
---|
1747 | int32_t cc_parse_msg(struct s_client *cl, uint8_t *buf, int32_t l) {
|
---|
1748 | struct s_reader *rdr = (cl->typ == 'c') ? NULL : cl->reader;
|
---|
1749 | int32_t ret = buf[1];
|
---|
1750 | struct cc_data *cc = cl->cc;
|
---|
1751 | if (!cc || cc->mode == CCCAM_MODE_SHUTDOWN)
|
---|
1752 | return -1;
|
---|
1753 |
|
---|
1754 | cs_debug_mask(cl->typ=='c'?D_CLIENT:D_READER, "%s parse_msg=%d", getprefix(), buf[1]);
|
---|
1755 |
|
---|
1756 | uint8_t *data = buf + 4;
|
---|
1757 | memcpy(&cc->receive_buffer, data, l - 4);
|
---|
1758 | cc->last_msg = buf[1];
|
---|
1759 | switch (buf[1]) {
|
---|
1760 | case MSG_CLI_DATA:
|
---|
1761 | cs_debug_mask(D_CLIENT, "cccam: client data ack");
|
---|
1762 | break;
|
---|
1763 | case MSG_SRV_DATA:
|
---|
1764 | l -= 4;
|
---|
1765 | cs_debug_mask(D_READER, "%s MSG_SRV_DATA (payload=%d, hex=%02X)", getprefix(), l, l);
|
---|
1766 | data = cc->receive_buffer;
|
---|
1767 |
|
---|
1768 | if (l == 0x48) { //72 bytes: normal server data
|
---|
1769 | while (cs_trylock(&cc->cards_busy)) {
|
---|
1770 | cs_debug_mask(D_TRACE, "%s trylock MSG_SRV_DATA cards waiting", getprefix());
|
---|
1771 | cs_sleepms(50);
|
---|
1772 | }
|
---|
1773 | cc_free_cardlist(cc->cards, FALSE);
|
---|
1774 | free_extended_ecm_idx(cc);
|
---|
1775 | cc->last_emm_card = NULL;
|
---|
1776 | cc->num_hop1 = 0;
|
---|
1777 | cc->num_hop2 = 0;
|
---|
1778 | cc->num_hopx = 0;
|
---|
1779 | cc->num_reshare0 = 0;
|
---|
1780 | cc->num_reshare1 = 0;
|
---|
1781 | cc->num_reshare2 = 0;
|
---|
1782 | cc->num_resharex = 0;
|
---|
1783 | cs_unlock(&cc->cards_busy);
|
---|
1784 |
|
---|
1785 | memcpy(cc->peer_node_id, data, 8);
|
---|
1786 | memcpy(cc->peer_version, data + 8, 8);
|
---|
1787 |
|
---|
1788 | memcpy(cc->cmd0b_aeskey, cc->peer_node_id, 8);
|
---|
1789 | memcpy(cc->cmd0b_aeskey + 8, cc->peer_version, 8);
|
---|
1790 |
|
---|
1791 | strncpy(cc->remote_version, (char*)data+8, sizeof(cc->remote_version)-1);
|
---|
1792 | strncpy(cc->remote_build, (char*)data+40, sizeof(cc->remote_build)-1);
|
---|
1793 |
|
---|
1794 | cs_debug_mask(D_READER, "%s remove server %s running v%s (%s)", getprefix(), cs_hexdump(0,
|
---|
1795 | cc->peer_node_id, 8), cc->remote_version, cc->remote_build);
|
---|
1796 |
|
---|
1797 | chk_peer_node_for_oscam(cc);
|
---|
1798 | //Trick: when discovered partner is an Oscam Client, then we send him our version string:
|
---|
1799 | if (cc->is_oscam_cccam) {
|
---|
1800 | uint8_t token[256];
|
---|
1801 | snprintf((char *)token, sizeof(token),
|
---|
1802 | "PARTNER: OSCam v%s, build #%s (%s) [EXT,SID]", CS_VERSION,
|
---|
1803 | CS_SVN_VERSION, CS_OSTYPE);
|
---|
1804 | cc_cmd_send(cl, token, strlen((char *)token) + 1, MSG_CW_NOK1);
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1808 | //
|
---|
1809 | //Keyoffset is payload-size:
|
---|
1810 | //
|
---|
1811 | } else if (l >= 0x00 && l <= 0x0F) {
|
---|
1812 | cc->cmd05_offset = l;
|
---|
1813 | //
|
---|
1814 | //16..43 bytes: RC4 encryption:
|
---|
1815 | //
|
---|
1816 | } else if ((l >= 0x10 && l <= 0x1f) || (l >= 0x24 && l <= 0x2b)) {
|
---|
1817 | cc_init_crypt(&cc->cmd05_cryptkey, data, l);
|
---|
1818 | cc->cmd05_mode = MODE_RC4_CRYPT;
|
---|
1819 | //
|
---|
1820 | //32 bytes: set AES128 key for CMD_05, Key=16 bytes offset keyoffset
|
---|
1821 | //
|
---|
1822 | } else if (l == 0x20) {
|
---|
1823 | memcpy(cc->cmd05_aeskey, data + cc->cmd05_offset, 16);
|
---|
1824 | cc->cmd05_mode = MODE_AES;
|
---|
1825 | //
|
---|
1826 | //33 bytes: xor-algo mit payload-bytes, offset keyoffset
|
---|
1827 | //
|
---|
1828 | } else if (l == 0x21) {
|
---|
1829 | cc_init_crypt(&cc->cmd05_cryptkey, data + cc->cmd05_offset, l);
|
---|
1830 | cc->cmd05_mode = MODE_CC_CRYPT;
|
---|
1831 | //
|
---|
1832 | //34 bytes: cmd_05 plain back
|
---|
1833 | //
|
---|
1834 | } else if (l == 0x22) {
|
---|
1835 | cc->cmd05_mode = MODE_PLAIN;
|
---|
1836 | //
|
---|
1837 | //35 bytes: Unknown!! 2 256 byte keys exchange
|
---|
1838 | //
|
---|
1839 | } else if (l == 0x23) {
|
---|
1840 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
1841 | //cycle_connection(); //Absolute unknown handling!
|
---|
1842 | cc_cli_close(cl, TRUE);
|
---|
1843 | //
|
---|
1844 | //44 bytes: set aes128 key, Key=16 bytes [Offset=len(password)]
|
---|
1845 | //
|
---|
1846 | } else if (l == 0x2c) {
|
---|
1847 | memcpy(cc->cmd05_aeskey, data + strlen(rdr->r_pwd), 16);
|
---|
1848 | cc->cmd05_mode = MODE_AES;
|
---|
1849 | //
|
---|
1850 | //45 bytes: set aes128 key, Key=16 bytes [Offset=len(username)]
|
---|
1851 | //
|
---|
1852 | } else if (l == 0x2d) {
|
---|
1853 | memcpy(cc->cmd05_aeskey, data + strlen(rdr->r_usr), 16);
|
---|
1854 | cc->cmd05_mode = MODE_AES;
|
---|
1855 | //
|
---|
1856 | //Unknown!!
|
---|
1857 | //
|
---|
1858 | } else {
|
---|
1859 | cs_debug_mask(D_READER,
|
---|
1860 | "%s received improper MSG_SRV_DATA! No change to current mode, mode=%d",
|
---|
1861 | getprefix(), cc->cmd05_mode);
|
---|
1862 | break;
|
---|
1863 | }
|
---|
1864 | cs_debug_mask(D_READER, "%s MSG_SRV_DATA MODE=%s, len=%d", getprefix(),
|
---|
1865 | cmd05_mode_name[cc->cmd05_mode], l);
|
---|
1866 |
|
---|
1867 | break;
|
---|
1868 | case MSG_NEW_CARD_SIDINFO:
|
---|
1869 | case MSG_NEW_CARD: {
|
---|
1870 | uint16_t caid = b2i(2, buf + 12);
|
---|
1871 | //filter caid==0 and maxhop:
|
---|
1872 | if (!caid || buf[14] >= rdr->cc_maxhop+1)
|
---|
1873 | break;
|
---|
1874 |
|
---|
1875 | //filter mindown:
|
---|
1876 | if (buf[15] < rdr->cc_mindown)
|
---|
1877 | break;
|
---|
1878 |
|
---|
1879 | //caid check
|
---|
1880 | if (!chk_ctab(caid, &rdr->ctab))
|
---|
1881 | break;
|
---|
1882 |
|
---|
1883 | rdr->tcp_connected = 2; //we have card
|
---|
1884 | rdr->card_status = CARD_INSERTED;
|
---|
1885 |
|
---|
1886 | while (cs_trylock(&cc->cards_busy)) {
|
---|
1887 | cs_debug_mask(D_TRACE, "%s trylock MSG_NEW_CARD cards waiting", getprefix());
|
---|
1888 | cs_sleepms(50);
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | struct cc_card *card = read_card(data, buf[1]==MSG_NEW_CARD_SIDINFO);
|
---|
1892 | card->origin_reader = rdr;
|
---|
1893 | card->origin_id = card->id;
|
---|
1894 | card->grp = rdr->grp;
|
---|
1895 | card->card_type = CT_REMOTECARD;
|
---|
1896 | card->rdr_reshare = rdr->cc_reshare;
|
---|
1897 |
|
---|
1898 | //Check if this card is from us:
|
---|
1899 | LL_ITER it = ll_iter_create(card->remote_nodes);
|
---|
1900 | uint8_t *node_id;
|
---|
1901 | while ((node_id = ll_iter_next(&it))) {
|
---|
1902 | if (memcmp(node_id, cc_node_id, sizeof(cc_node_id)) == 0) { //this card is from us!
|
---|
1903 | cs_debug_mask(D_READER, "filtered card because of recursive nodeid: id=%08X, caid=%04X", card->id, card->caid);
|
---|
1904 | cc_free_card(card);
|
---|
1905 | card=NULL;
|
---|
1906 | break;
|
---|
1907 | }
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | //Check Ident filter:
|
---|
1911 | if (card) {
|
---|
1912 | if (!chk_ident(&rdr->ftab, card)) {
|
---|
1913 | cc_free_card(card);
|
---|
1914 | card=NULL;
|
---|
1915 | }
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | if (card) {
|
---|
1919 | //Check if we already have this card:
|
---|
1920 | it = ll_iter_create(cc->cards);
|
---|
1921 | struct cc_card *old_card;
|
---|
1922 | while ((old_card = ll_iter_next(&it))) {
|
---|
1923 | if (old_card->id == card->id || //we aready have this card, delete it
|
---|
1924 | same_card(old_card, card)) {
|
---|
1925 | cc_free_card(card);
|
---|
1926 | card = old_card;
|
---|
1927 | break;
|
---|
1928 | }
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | if (!old_card) {
|
---|
1932 | card->card_type = CT_REMOTECARD;
|
---|
1933 | card->hop++; //inkrementing hop
|
---|
1934 | ll_append(cc->cards, card);
|
---|
1935 | set_au_data(cl, rdr, card, NULL);
|
---|
1936 | cc->card_added_count++;
|
---|
1937 |
|
---|
1938 | if (card->hop == 1) cc->num_hop1++;
|
---|
1939 | else if (card->hop == 2) cc->num_hop2++;
|
---|
1940 | else cc->num_hopx++;
|
---|
1941 |
|
---|
1942 | if (card->reshare == 0) cc->num_reshare0++;
|
---|
1943 | else if (card->reshare == 1) cc->num_reshare1++;
|
---|
1944 | else if (card->reshare == 2) cc->num_reshare2++;
|
---|
1945 | else cc->num_resharex++;
|
---|
1946 | }
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | cs_unlock(&cc->cards_busy);
|
---|
1950 |
|
---|
1951 | break;
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | case MSG_CARD_REMOVED: {
|
---|
1955 | while (cs_trylock(&cc->cards_busy)) {
|
---|
1956 | cs_debug_mask(D_TRACE, "%s trylock MSG_CARD_REMOVED cards waiting", getprefix());
|
---|
1957 | cs_sleepms(50);
|
---|
1958 | }
|
---|
1959 | cc_card_removed(cl, b2i(4, buf + 4));
|
---|
1960 | cs_unlock(&cc->cards_busy);
|
---|
1961 | break;
|
---|
1962 | }
|
---|
1963 |
|
---|
1964 | case MSG_CW_NOK1:
|
---|
1965 | case MSG_CW_NOK2:
|
---|
1966 | if (l > 4) {
|
---|
1967 | //Received NOK with payload:
|
---|
1968 | char *msg = (char*) buf + 4;
|
---|
1969 |
|
---|
1970 | //Check for PARTNER connection:
|
---|
1971 | if (strncmp(msg, "PARTNER:", 8) == 0) {
|
---|
1972 | //When Data starts with "PARTNER:" we have an Oscam-cccam-compatible client/server!
|
---|
1973 |
|
---|
1974 | strncpy(cc->remote_oscam, msg+9, sizeof(cc->remote_oscam)-1);
|
---|
1975 | int32_t has_param = check_extended_mode(cl, msg);
|
---|
1976 | if (!cc->is_oscam_cccam) {
|
---|
1977 | cc->is_oscam_cccam = 1;
|
---|
1978 |
|
---|
1979 | //send params back. At the moment there is only "EXT"
|
---|
1980 | char param[14];
|
---|
1981 | if (!has_param)
|
---|
1982 | param[0] = 0;
|
---|
1983 | else {
|
---|
1984 | cs_strncpy(param, " [", sizeof(param));
|
---|
1985 | if (cc->extended_mode)
|
---|
1986 | addParam(param, "EXT");
|
---|
1987 | if (cc->cccam220)
|
---|
1988 | addParam(param, "SID");
|
---|
1989 | strcat(param, "]");
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 | uchar token[256];
|
---|
1993 | snprintf((char *)token, sizeof(token),
|
---|
1994 | "PARTNER: OSCam v%s, build #%s (%s)%s",
|
---|
1995 | CS_VERSION, CS_SVN_VERSION, CS_OSTYPE, param);
|
---|
1996 | cc_cmd_send(cl, token, strlen((char *)token) + 1, MSG_CW_NOK1);
|
---|
1997 | }
|
---|
1998 | }
|
---|
1999 | return ret;
|
---|
2000 | }
|
---|
2001 |
|
---|
2002 | if (cl->typ == 'c') //for reader only
|
---|
2003 | return ret;
|
---|
2004 |
|
---|
2005 | cc->recv_ecmtask = -1;
|
---|
2006 |
|
---|
2007 | if (cc->just_logged_in)
|
---|
2008 | return -1; // reader restart needed
|
---|
2009 |
|
---|
2010 | while (cs_trylock(&cc->cards_busy)) {
|
---|
2011 | cs_debug_mask(D_TRACE, "%s trylock MSG_CW_NOK cards waiting", getprefix());
|
---|
2012 | cs_sleepms(50);
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl,
|
---|
2016 | cc->extended_mode ? cc->g_flag : 1, TRUE);
|
---|
2017 | if (!eei) {
|
---|
2018 | cs_debug_mask(D_READER, "%s received extended ecm NOK id %d but not found!",
|
---|
2019 | getprefix(), cc->g_flag);
|
---|
2020 | }
|
---|
2021 | else
|
---|
2022 | {
|
---|
2023 | uint16_t ecm_idx = eei->ecm_idx;
|
---|
2024 | cc->recv_ecmtask = ecm_idx;
|
---|
2025 | struct cc_card *card = eei->card;
|
---|
2026 | struct cc_srvid srvid = eei->srvid;
|
---|
2027 | free(eei);
|
---|
2028 |
|
---|
2029 | if (card) {
|
---|
2030 | if (buf[1] == MSG_CW_NOK1) //MSG_CW_NOK1: share no more available
|
---|
2031 | cc_card_removed(cl, card->id);
|
---|
2032 | else if (cc->cmd05NOK) {
|
---|
2033 | move_card_to_end(cl, card);
|
---|
2034 | add_sid_block(cl, card, &srvid);
|
---|
2035 | }
|
---|
2036 | else if (!is_good_sid(card, &srvid)) //MSG_CW_NOK2: can't decode
|
---|
2037 | add_sid_block(cl, card, &srvid);
|
---|
2038 | else
|
---|
2039 | remove_good_sid(card, &srvid);
|
---|
2040 |
|
---|
2041 | if (cfg.cc_forward_origin_card && card->origin_reader == rdr) {
|
---|
2042 | //this card is from us but it can't decode this ecm
|
---|
2043 | //also origin card is only set on cccam clients
|
---|
2044 | //so wie send back the nok to the client
|
---|
2045 | int32_t i = 0;
|
---|
2046 | for (i=0;i<CS_MAXPENDING;i++) {
|
---|
2047 | if (cl->ecmtask[i].idx == ecm_idx) {
|
---|
2048 | cs_debug_mask(D_TRACE, "%s forward card: %s", getprefix(), (buf[1]==MSG_CW_NOK1)?"NOK1":"NOK2");
|
---|
2049 | ECM_REQUEST *er = &cl->ecmtask[i];
|
---|
2050 | er->rc = E_RDR_NOTFOUND;
|
---|
2051 | er->rcEx = (buf[1] == MSG_CW_NOK1)?E2_CCCAM_NOK1:E2_CCCAM_NOK2;
|
---|
2052 | cl->pending--;
|
---|
2053 | write_ecm_answer(rdr, er);
|
---|
2054 | break;
|
---|
2055 | }
|
---|
2056 | }
|
---|
2057 | }
|
---|
2058 | else {
|
---|
2059 | //retry ecm:
|
---|
2060 | cc_reset_pending(cl, ecm_idx);
|
---|
2061 | }
|
---|
2062 | } else
|
---|
2063 | cs_debug_mask(D_READER, "%S NOK: NO CARD!", getprefix());
|
---|
2064 | }
|
---|
2065 | cc->cmd05NOK = 0;
|
---|
2066 | cs_unlock(&cc->cards_busy);
|
---|
2067 |
|
---|
2068 | if (!cc->extended_mode) {
|
---|
2069 | rdr->available = 1;
|
---|
2070 | cs_unlock(&cc->ecm_busy);
|
---|
2071 | }
|
---|
2072 |
|
---|
2073 | cc_send_ecm(cl, NULL, NULL);
|
---|
2074 | break;
|
---|
2075 |
|
---|
2076 | case MSG_CW_ECM:
|
---|
2077 | cc->just_logged_in = 0;
|
---|
2078 | if (cl->typ == 'c') { //SERVER:
|
---|
2079 | ECM_REQUEST *er;
|
---|
2080 |
|
---|
2081 | struct cc_card *server_card = cs_malloc(&server_card, sizeof(struct cc_card), QUITERROR);
|
---|
2082 | memset(server_card, 0, sizeof(struct cc_card));
|
---|
2083 | server_card->id = buf[10] << 24 | buf[11] << 16 | buf[12] << 8
|
---|
2084 | | buf[13];
|
---|
2085 | server_card->caid = b2i(2, data);
|
---|
2086 |
|
---|
2087 | if ((er = get_ecmtask())) {
|
---|
2088 | er->caid = b2i(2, buf + 4);
|
---|
2089 | er->srvid = b2i(2, buf + 14);
|
---|
2090 | er->l = buf[16];
|
---|
2091 | memcpy(er->ecm, buf + 17, er->l);
|
---|
2092 | er->prid = b2i(4, buf + 6);
|
---|
2093 | cc->server_ecm_pending++;
|
---|
2094 | er->idx = ++cc->server_ecm_idx;
|
---|
2095 |
|
---|
2096 | if (cfg.cc_forward_origin_card) { //search my shares for this card:
|
---|
2097 | cs_debug_mask(D_TRACE, "%s forward card: %04X:%04x search share %d", getprefix(), er->caid, er->srvid, server_card->id);
|
---|
2098 | LL_ITER itr = ll_iter_create(get_and_lock_sharelist());
|
---|
2099 | struct cc_card *card;
|
---|
2100 | struct cc_card *rcard = NULL;
|
---|
2101 | while ((card=ll_iter_next(&itr))) {
|
---|
2102 | if (card->id == server_card->id) { //found it
|
---|
2103 | break;
|
---|
2104 | }
|
---|
2105 | }
|
---|
2106 | cs_debug_mask(D_TRACE, "%s forward card: share %d found: %d", getprefix(), server_card->id, card?1:0);
|
---|
2107 |
|
---|
2108 | struct s_reader *ordr = NULL;
|
---|
2109 | if (card && card->origin_reader) { // found own card, now search reader card:
|
---|
2110 | //Search reader in list, because it is maybe offline?
|
---|
2111 | for (ordr=first_active_reader; ordr; ordr=ordr->next) {
|
---|
2112 | if (ordr == card->origin_reader) break;
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | if (!ordr)
|
---|
2116 | cs_debug_mask(D_TRACE, "%s origin reader not found!", getprefix());
|
---|
2117 | else {
|
---|
2118 | cs_debug_mask(D_TRACE, "%s forward card: share %d origin reader %s origin id %d", getprefix(), card->id, ordr->label, card->origin_id);
|
---|
2119 | if (card->origin_id && ordr && ordr->client && ordr->client->cc) { //only if we have a origin from a cccam reader
|
---|
2120 | struct cc_data *rcc = ordr->client->cc;
|
---|
2121 |
|
---|
2122 | itr = ll_iter_create(rcc->cards);
|
---|
2123 | while ((rcard=ll_iter_next(&itr))) {
|
---|
2124 | if (rcard->id == card->origin_id) //found it!
|
---|
2125 | break;
|
---|
2126 | }
|
---|
2127 | }
|
---|
2128 | else
|
---|
2129 | rcard = card;
|
---|
2130 | }
|
---|
2131 | er->origin_reader = ordr;
|
---|
2132 | }
|
---|
2133 |
|
---|
2134 | er->origin_card = rcard;
|
---|
2135 | if (!rcard || !ordr) {
|
---|
2136 | cs_debug_mask(D_TRACE, "%s forward card: share %d not found!", getprefix(), server_card->id);
|
---|
2137 | er->rc = E_NOTFOUND;
|
---|
2138 | er->rcEx = E2_CCCAM_NOK1; //share not found!
|
---|
2139 | }
|
---|
2140 | else
|
---|
2141 | cs_debug_mask(D_TRACE, "%s forward card: share %d forwarded to %s origin as id %d", getprefix(),
|
---|
2142 | card->id, ordr->label, rcard->id);
|
---|
2143 | unlock_sharelist();
|
---|
2144 | }
|
---|
2145 |
|
---|
2146 | cs_debug_mask(
|
---|
2147 | D_CLIENT,
|
---|
2148 | "%s ECM request from client: caid %04x srvid %04x(%d) prid %06x",
|
---|
2149 | getprefix(), er->caid, er->srvid, er->l, er->prid);
|
---|
2150 |
|
---|
2151 | struct cc_srvid srvid;
|
---|
2152 | srvid.sid = er->srvid;
|
---|
2153 | srvid.ecmlen = er->l;
|
---|
2154 | add_extended_ecm_idx(cl, cc->extended_mode ? cc->g_flag : 1,
|
---|
2155 | er->idx, server_card, srvid, 1);
|
---|
2156 |
|
---|
2157 | get_cw(cl, er);
|
---|
2158 |
|
---|
2159 | } else {
|
---|
2160 | cs_debug_mask(D_CLIENT, "%s NO ECMTASK!!!!", getprefix());
|
---|
2161 | free(server_card);
|
---|
2162 | }
|
---|
2163 |
|
---|
2164 | } else { //READER:
|
---|
2165 | while (cs_trylock(&cc->cards_busy)) {
|
---|
2166 | cs_debug_mask(D_TRACE, "%s trylock MSG_CW_ECM cards waiting", getprefix());
|
---|
2167 | cs_sleepms(50);
|
---|
2168 | }
|
---|
2169 | cc->recv_ecmtask = -1;
|
---|
2170 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx(cl,
|
---|
2171 | cc->extended_mode ? cc->g_flag : 1, TRUE);
|
---|
2172 | if (!eei) {
|
---|
2173 | cs_debug_mask(D_READER, "%s received extended ecm id %d but not found!",
|
---|
2174 | getprefix(), cc->g_flag);
|
---|
2175 | }
|
---|
2176 | else
|
---|
2177 | {
|
---|
2178 | uint16_t ecm_idx = eei->ecm_idx;
|
---|
2179 | cc->recv_ecmtask = ecm_idx;
|
---|
2180 | struct cc_card *card = eei->card;
|
---|
2181 | struct cc_srvid srvid = eei->srvid;
|
---|
2182 | free(eei);
|
---|
2183 |
|
---|
2184 | if (card) {
|
---|
2185 |
|
---|
2186 | if (!cc->extended_mode) {
|
---|
2187 | cc_cw_crypt(cl, buf + 4, card->id);
|
---|
2188 | cc_crypt_cmd0c(cl, buf + 4, 16);
|
---|
2189 | }
|
---|
2190 |
|
---|
2191 | memcpy(cc->dcw, buf + 4, 16);
|
---|
2192 | //fix_dcw(cc->dcw);
|
---|
2193 | if (!cc->extended_mode)
|
---|
2194 | cc_crypt(&cc->block[DECRYPT], buf + 4, l - 4, ENCRYPT); // additional crypto step
|
---|
2195 |
|
---|
2196 | if (is_null_dcw(cc->dcw)) {
|
---|
2197 | cs_debug_mask(D_READER, "%s null dcw received! sid=%04X(%d)", getprefix(),
|
---|
2198 | srvid.sid, srvid.ecmlen);
|
---|
2199 | add_sid_block(cl, card, &srvid);
|
---|
2200 | //ecm retry:
|
---|
2201 | cc_reset_pending(cl, ecm_idx);
|
---|
2202 | buf[1] = MSG_CW_NOK2; //So it's really handled like a nok!
|
---|
2203 | } else {
|
---|
2204 | cs_debug_mask(D_READER, "%s cws: %d %s", getprefix(),
|
---|
2205 | ecm_idx, cs_hexdump(0, cc->dcw, 16));
|
---|
2206 | add_good_sid(cl, card, &srvid);
|
---|
2207 |
|
---|
2208 | //check response time, if > fallbacktime, switch cards!
|
---|
2209 | struct timeb tpe;
|
---|
2210 | cs_ftime(&tpe);
|
---|
2211 | uint32_t cwlastresptime = 1000*(tpe.time-cc->ecm_time.time)+tpe.millitm-cc->ecm_time.millitm;
|
---|
2212 | if (cwlastresptime > cfg.ftimeout && !cc->extended_mode) {
|
---|
2213 | cs_debug_mask(D_READER, "%s card %04X is too slow, moving to the end...", getprefix(), card->id);
|
---|
2214 | move_card_to_end(cl, card);
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 | }
|
---|
2218 | } else {
|
---|
2219 | cs_debug_mask(D_READER,
|
---|
2220 | "%s warning: ECM-CWS respond by CCCam server without current card!",
|
---|
2221 | getprefix());
|
---|
2222 | }
|
---|
2223 | }
|
---|
2224 | cs_unlock(&cc->cards_busy);
|
---|
2225 |
|
---|
2226 | if (!cc->extended_mode) {
|
---|
2227 | rdr->available = 1;
|
---|
2228 | cs_unlock(&cc->ecm_busy);
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | //cc_abort_user_ecms();
|
---|
2232 |
|
---|
2233 | cc_send_ecm(cl, NULL, NULL);
|
---|
2234 |
|
---|
2235 | if (cc->max_ecms)
|
---|
2236 | cc->ecm_counter++;
|
---|
2237 | }
|
---|
2238 | break;
|
---|
2239 |
|
---|
2240 | case MSG_KEEPALIVE:
|
---|
2241 | if (cl->typ != 'c') {
|
---|
2242 | cs_debug_mask(D_READER, "cccam: keepalive ack");
|
---|
2243 | } else {
|
---|
2244 | //Checking if last answer is one minute ago:
|
---|
2245 | if (cc->just_logged_in || cc->answer_on_keepalive + 55 <= time(NULL)) {
|
---|
2246 | cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE);
|
---|
2247 | cs_debug_mask(D_CLIENT, "cccam: keepalive");
|
---|
2248 | cc->answer_on_keepalive = time(NULL);
|
---|
2249 | }
|
---|
2250 | }
|
---|
2251 | cc->just_logged_in = 0;
|
---|
2252 | break;
|
---|
2253 |
|
---|
2254 | case MSG_CMD_05:
|
---|
2255 | if (cl->typ != 'c') {
|
---|
2256 | cc->just_logged_in = 0;
|
---|
2257 | l = l - 4;//Header Length=4 Byte
|
---|
2258 |
|
---|
2259 | cs_debug_mask(D_READER, "%s MSG_CMD_05 recvd, payload length=%d mode=%d",
|
---|
2260 | getprefix(), l, cc->cmd05_mode);
|
---|
2261 | cc->cmd05_active = 1;
|
---|
2262 | cc->cmd05_data_len = l;
|
---|
2263 | memcpy(&cc->cmd05_data, buf + 4, l);
|
---|
2264 | if (rdr->available && ll_has_elements(cc->cards))
|
---|
2265 | send_cmd05_answer(cl);
|
---|
2266 | }
|
---|
2267 | break;
|
---|
2268 | case MSG_CMD_0B: {
|
---|
2269 | // by Project:Keynation
|
---|
2270 | cs_debug_mask(D_READER, "%s MSG_CMD_0B received (payload=%d)!",
|
---|
2271 | getprefix(), l - 4);
|
---|
2272 |
|
---|
2273 | AES_KEY key;
|
---|
2274 | uint8_t aeskey[16];
|
---|
2275 | uint8_t out[16];
|
---|
2276 |
|
---|
2277 | memcpy(aeskey, cc->cmd0b_aeskey, 16);
|
---|
2278 | memset(&key, 0, sizeof(key));
|
---|
2279 |
|
---|
2280 | //cs_ddump_mask(D_READER, aeskey, 16, "%s CMD_0B AES key:", getprefix());
|
---|
2281 | //cs_ddump_mask(D_READER, data, 16, "%s CMD_0B received data:", getprefix());
|
---|
2282 |
|
---|
2283 | AES_set_encrypt_key((unsigned char *) &aeskey, 128, &key);
|
---|
2284 | AES_encrypt((unsigned char *) data, (unsigned char *) &out, &key);
|
---|
2285 |
|
---|
2286 | cs_debug_mask(D_TRACE, "%s sending CMD_0B! ", getprefix());
|
---|
2287 | //cs_ddump_mask(D_READER, out, 16, "%s CMD_0B out:", getprefix());
|
---|
2288 | cc_cmd_send(cl, out, 16, MSG_CMD_0B);
|
---|
2289 |
|
---|
2290 | break;
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 | case MSG_CMD_0C: { //New CCCAM 2.2.0 Server/Client fake check!
|
---|
2294 | int32_t len = l-4;
|
---|
2295 |
|
---|
2296 | if (cl->typ == 'c') { //Only im comming from "client"
|
---|
2297 | cs_debug_mask(D_CLIENT, "%s MSG_CMD_0C received (payload=%d)!", getprefix(), len);
|
---|
2298 |
|
---|
2299 | uint8_t bytes[0x20];
|
---|
2300 | if (len < 0x20) //if less then 0x20 bytes, clear others:
|
---|
2301 | memset(data+len, 0, 0x20-len);
|
---|
2302 |
|
---|
2303 | //change first 0x10 bytes to the second:
|
---|
2304 | memcpy(bytes, data+0x10, 0x10);
|
---|
2305 | memcpy(bytes+0x10, data, 0x10);
|
---|
2306 |
|
---|
2307 | //xor data:
|
---|
2308 | int32_t i;
|
---|
2309 | for (i=0;i<0x20;i++)
|
---|
2310 | bytes[i] ^= (data[i] & 0x7F);
|
---|
2311 |
|
---|
2312 | //key is now the 16bit hash of md5:
|
---|
2313 | uint8_t md5hash[0x10];
|
---|
2314 | MD5(data, 0x20, md5hash);
|
---|
2315 | memcpy(bytes, md5hash, 0x10);
|
---|
2316 |
|
---|
2317 | cs_debug_mask(D_CLIENT, "%s sending CMD_0C! ", getprefix());
|
---|
2318 | //cs_ddump_mask(D_CLIENT, bytes, 0x20, "%s CMD_0C out:", getprefix());
|
---|
2319 | cc_cmd_send(cl, bytes, 0x20, MSG_CMD_0C);
|
---|
2320 | }
|
---|
2321 | else //reader
|
---|
2322 | {
|
---|
2323 | // by Project:Keynation + Oscam team
|
---|
2324 | cc_crypt_cmd0c(cl, data, len);
|
---|
2325 |
|
---|
2326 | uint8_t CMD_0x0C_Command = data[0];
|
---|
2327 |
|
---|
2328 | switch (CMD_0x0C_Command) {
|
---|
2329 |
|
---|
2330 | case 0 : { //RC6
|
---|
2331 | cc->cmd0c_mode = MODE_CMD_0x0C_RC6;
|
---|
2332 | break;
|
---|
2333 | }
|
---|
2334 |
|
---|
2335 | case 1: { //RC4
|
---|
2336 | cc->cmd0c_mode = MODE_CMD_0x0C_RC4;
|
---|
2337 | break;
|
---|
2338 | }
|
---|
2339 |
|
---|
2340 | case 2: { //CC_CRYPT
|
---|
2341 | cc->cmd0c_mode = MODE_CMD_0x0C_CC_CRYPT;
|
---|
2342 | break;
|
---|
2343 | }
|
---|
2344 |
|
---|
2345 | case 3: { //AES
|
---|
2346 | cc->cmd0c_mode = MODE_CMD_0x0C_AES;
|
---|
2347 | break;
|
---|
2348 | }
|
---|
2349 |
|
---|
2350 | case 4 : { //IDEA
|
---|
2351 | cc->cmd0c_mode = MODE_CMD_0x0C_IDEA;
|
---|
2352 | break;
|
---|
2353 | }
|
---|
2354 |
|
---|
2355 | default: {
|
---|
2356 | cc->cmd0c_mode = MODE_CMD_0x0C_NONE;
|
---|
2357 | }
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | set_cmd0c_cryptkey(cl, data, len);
|
---|
2361 |
|
---|
2362 | cs_debug_mask(D_READER, "%s received MSG_CMD_0C from server! CMD_0x0C_CMD=%d, MODE=%s",
|
---|
2363 | getprefix(), CMD_0x0C_Command, cmd0c_mode_name[cc->cmd0c_mode]);
|
---|
2364 | }
|
---|
2365 | break;
|
---|
2366 | }
|
---|
2367 |
|
---|
2368 | case MSG_CMD_0D: { //key update for the active cmd0x0c algo
|
---|
2369 | int32_t len = l-4;
|
---|
2370 | if (cc->cmd0c_mode == MODE_CMD_0x0C_NONE)
|
---|
2371 | break;
|
---|
2372 |
|
---|
2373 | cc_crypt_cmd0c(cl, data, len);
|
---|
2374 | set_cmd0c_cryptkey(cl, data, len);
|
---|
2375 |
|
---|
2376 | cs_debug_mask(D_READER, "%s received MSG_CMD_0D from server! MODE=%s",
|
---|
2377 | getprefix(), cmd0c_mode_name[cc->cmd0c_mode]);
|
---|
2378 | break;
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | case MSG_CMD_0E: {
|
---|
2382 | cs_debug_mask(D_READER, "cccam 2.2.x commands not implemented: 0x%02X", buf[1]);
|
---|
2383 | //Unkwon commands...need workout algo
|
---|
2384 | if (cl->typ == 'c') //client connection
|
---|
2385 | {
|
---|
2386 | //switching to an oder version and then disconnect...
|
---|
2387 | cs_strncpy(cfg.cc_version, version[0], sizeof(cfg.cc_version));
|
---|
2388 | ret = -1;
|
---|
2389 | }
|
---|
2390 | else //reader connection
|
---|
2391 | {
|
---|
2392 | cs_strncpy(cl->reader->cc_version, version[0], sizeof(cl->reader->cc_version));
|
---|
2393 | cs_strncpy(cl->reader->cc_build, build[0], sizeof(cl->reader->cc_build));
|
---|
2394 | cc_cli_close(cl, TRUE);
|
---|
2395 | }
|
---|
2396 | break;
|
---|
2397 | }
|
---|
2398 |
|
---|
2399 | case MSG_EMM_ACK: {
|
---|
2400 | cc->just_logged_in = 0;
|
---|
2401 | if (cl->typ == 'c') { //EMM Request received
|
---|
2402 | cc_cmd_send(cl, NULL, 0, MSG_EMM_ACK); //Send back ACK
|
---|
2403 | if (l > 4) {
|
---|
2404 | cs_debug_mask(D_EMM, "%s EMM Request received!", getprefix());
|
---|
2405 |
|
---|
2406 | if (!ll_count(cl->aureader_list)) {
|
---|
2407 | cs_debug_mask(
|
---|
2408 | D_EMM,
|
---|
2409 | "%s EMM Request discarded because au is not assigned to an reader!",
|
---|
2410 | getprefix());
|
---|
2411 | return MSG_EMM_ACK;
|
---|
2412 | }
|
---|
2413 |
|
---|
2414 | EMM_PACKET *emm = cs_malloc(&emm, sizeof(EMM_PACKET), QUITERROR);
|
---|
2415 | memset(emm, 0, sizeof(EMM_PACKET));
|
---|
2416 | emm->caid[0] = buf[4];
|
---|
2417 | emm->caid[1] = buf[5];
|
---|
2418 | emm->provid[0] = buf[7];
|
---|
2419 | emm->provid[1] = buf[8];
|
---|
2420 | emm->provid[2] = buf[9];
|
---|
2421 | emm->provid[3] = buf[10];
|
---|
2422 | //emm->hexserial[0] = buf[11];
|
---|
2423 | //emm->hexserial[1] = buf[12];
|
---|
2424 | //emm->hexserial[2] = buf[13];
|
---|
2425 | //emm->hexserial[3] = buf[14];
|
---|
2426 | emm->l = buf[15];
|
---|
2427 | memcpy(emm->emm, buf + 16, emm->l);
|
---|
2428 | //emm->type = UNKNOWN;
|
---|
2429 | //emm->cidx = cs_idx;
|
---|
2430 | do_emm(cl, emm);
|
---|
2431 | free(emm);
|
---|
2432 | }
|
---|
2433 | } else { //Our EMM Request Ack!
|
---|
2434 | cs_debug_mask(D_EMM, "%s EMM ACK!", getprefix());
|
---|
2435 | if (!cc->extended_mode) {
|
---|
2436 | rdr->available = 1;
|
---|
2437 | cs_unlock(&cc->ecm_busy);
|
---|
2438 | }
|
---|
2439 | cc_send_ecm(cl, NULL, NULL);
|
---|
2440 | }
|
---|
2441 | break;
|
---|
2442 | }
|
---|
2443 | default:
|
---|
2444 | //cs_ddump_mask(D_CLIENT, buf, l, "%s unhandled msg: %d len=%d", getprefix(), buf[1], l);
|
---|
2445 | break;
|
---|
2446 | }
|
---|
2447 |
|
---|
2448 | if (cc->max_ecms && (cc->ecm_counter > cc->max_ecms)) {
|
---|
2449 | cs_debug_mask(D_READER, "%s max ecms (%d) reached, cycle connection!", getprefix(),
|
---|
2450 | cc->max_ecms);
|
---|
2451 | //cc_cycle_connection();
|
---|
2452 | cc_cli_close(cl, TRUE);
|
---|
2453 | //cc_send_ecm(NULL, NULL);
|
---|
2454 | }
|
---|
2455 | return ret;
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | /**
|
---|
2459 | * Reader: write dcw to receive
|
---|
2460 | */
|
---|
2461 | int32_t cc_recv_chk(struct s_client *cl, uchar *dcw, int32_t *rc, uchar *buf, int32_t UNUSED(n)) {
|
---|
2462 | struct cc_data *cc = cl->cc;
|
---|
2463 |
|
---|
2464 | if (buf[1] == MSG_CW_ECM) {
|
---|
2465 | memcpy(dcw, cc->dcw, 16);
|
---|
2466 | //cs_debug_mask(D_CLIENT, "cccam: recv chk - MSG_CW %d - %s", cc->recv_ecmtask,
|
---|
2467 | // cs_hexdump(0, dcw, 16));
|
---|
2468 | *rc = 1;
|
---|
2469 | return (cc->recv_ecmtask);
|
---|
2470 | } else if ((buf[1] == (MSG_CW_NOK1)) || (buf[1] == (MSG_CW_NOK2))) {
|
---|
2471 | *rc = 0;
|
---|
2472 | //if (cc->is_oscam_cccam)
|
---|
2473 | if (cfg.cc_forward_origin_card)
|
---|
2474 | return (cc->recv_ecmtask);
|
---|
2475 | else
|
---|
2476 | return -1;
|
---|
2477 | }
|
---|
2478 |
|
---|
2479 | return (-1);
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | //int32_t is_softfail(int32_t rc)
|
---|
2483 | //{
|
---|
2484 | // //see oscam.c send_dcw() for a full list
|
---|
2485 | // switch(rc)
|
---|
2486 | // {
|
---|
2487 | // case 5: // 5 = timeout
|
---|
2488 | // case 6: // 6 = sleeping
|
---|
2489 | // case 7: // 7 = fake
|
---|
2490 | // case 10:// 10= no card
|
---|
2491 | // case 11:// 11= expdate
|
---|
2492 | // case 12:// 12= disabled
|
---|
2493 | // case 13:// 13= stopped
|
---|
2494 | // case 14:// 100= unhandled
|
---|
2495 | // return TRUE;
|
---|
2496 | // }
|
---|
2497 | // return FALSE;
|
---|
2498 | //}
|
---|
2499 |
|
---|
2500 |
|
---|
2501 |
|
---|
2502 | /**
|
---|
2503 | * Server: send DCW to client
|
---|
2504 | */
|
---|
2505 | void cc_send_dcw(struct s_client *cl, ECM_REQUEST *er) {
|
---|
2506 | uchar buf[16];
|
---|
2507 | struct cc_data *cc = cl->cc;
|
---|
2508 |
|
---|
2509 | memset(buf, 0, sizeof(buf));
|
---|
2510 |
|
---|
2511 | struct cc_extended_ecm_idx *eei = get_extended_ecm_idx_by_idx(cl, er->idx,
|
---|
2512 | TRUE);
|
---|
2513 |
|
---|
2514 | if (er->rc < E_NOTFOUND && eei && eei->card) { //found:
|
---|
2515 | memcpy(buf, er->cw, sizeof(buf));
|
---|
2516 | //fix_dcw(buf);
|
---|
2517 | //cs_debug_mask(D_TRACE, "%s send cw: %s cpti: %d", getprefix(),
|
---|
2518 | // cs_hexdump(0, buf, 16), er->cpti);
|
---|
2519 | if (!cc->extended_mode)
|
---|
2520 | cc_cw_crypt(cl, buf, eei->card->id);
|
---|
2521 | else
|
---|
2522 | cc->g_flag = eei->send_idx;
|
---|
2523 | cc_cmd_send(cl, buf, 16, MSG_CW_ECM);
|
---|
2524 | if (!cc->extended_mode)
|
---|
2525 | cc_crypt(&cc->block[ENCRYPT], buf, 16, ENCRYPT); // additional crypto step
|
---|
2526 |
|
---|
2527 | } else { //NOT found:
|
---|
2528 | //cs_debug_mask(D_TRACE, "%s send cw: NOK cpti: %d", getprefix(),
|
---|
2529 | // er->cpti);
|
---|
2530 |
|
---|
2531 | if (eei && cc->extended_mode)
|
---|
2532 | cc->g_flag = eei->send_idx;
|
---|
2533 |
|
---|
2534 | int32_t nok;
|
---|
2535 | if (!eei || !eei->card)
|
---|
2536 | nok = MSG_CW_NOK1; //share no more available
|
---|
2537 | else {
|
---|
2538 | if (cfg.cc_forward_origin_card && er->origin_card == eei->card)
|
---|
2539 | nok = (er->rcEx==E2_CCCAM_NOK1)?MSG_CW_NOK1:MSG_CW_NOK2;
|
---|
2540 | else
|
---|
2541 | nok = MSG_CW_NOK2; //can't decode
|
---|
2542 | }
|
---|
2543 | cc_cmd_send(cl, NULL, 0, nok);
|
---|
2544 | }
|
---|
2545 | cc->server_ecm_pending--;
|
---|
2546 | if (eei) {
|
---|
2547 | free(eei->card);
|
---|
2548 | free(eei);
|
---|
2549 | }
|
---|
2550 | }
|
---|
2551 |
|
---|
2552 | int32_t cc_recv(struct s_client *cl, uchar *buf, int32_t l) {
|
---|
2553 | int32_t n;
|
---|
2554 | uchar *cbuf;
|
---|
2555 |
|
---|
2556 | if (buf == NULL || l <= 0)
|
---|
2557 | return -1;
|
---|
2558 | cbuf = cs_malloc(&cbuf, l, QUITERROR);
|
---|
2559 | memcpy(cbuf, buf, l); // make a copy of buf
|
---|
2560 |
|
---|
2561 | n = cc_msg_recv(cl, cbuf, l); // recv and decrypt msg
|
---|
2562 |
|
---|
2563 | //cs_ddump_mask(D_CLIENT, cbuf, n, "cccam: received %d bytes from %s", n, remote_txt());
|
---|
2564 | cl->last = time((time_t *) 0);
|
---|
2565 |
|
---|
2566 | if (n <= 0) {
|
---|
2567 | cs_log("%s connection closed by %s", getprefix(), remote_txt());
|
---|
2568 | n = -1;
|
---|
2569 | } else if (n < 4) {
|
---|
2570 | cs_log("%s packet to small (%d bytes)", getprefix(), n);
|
---|
2571 | n = -1;
|
---|
2572 | } else {
|
---|
2573 | // parse it and write it back, if we have received something of value
|
---|
2574 | n = cc_parse_msg(cl, cbuf, n);
|
---|
2575 | memcpy(buf, cbuf, l);
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | NULLFREE(cbuf);
|
---|
2579 |
|
---|
2580 | if (n == -1) {
|
---|
2581 | if (cl->typ != 'c')
|
---|
2582 | cc_cli_close(cl, TRUE);
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | return n;
|
---|
2586 | }
|
---|
2587 |
|
---|
2588 | void cc_init_cc(struct cc_data *cc) {
|
---|
2589 | pthread_mutex_init(&cc->lockcmd, NULL); //No recursive lock
|
---|
2590 | pthread_mutex_init(&cc->ecm_busy, NULL); //No recusive lock
|
---|
2591 | pthread_mutex_init(&cc->cards_busy, NULL); //No (more) recursive lock
|
---|
2592 | }
|
---|
2593 |
|
---|
2594 | /**
|
---|
2595 | * Starting readers to get cards:
|
---|
2596 | **/
|
---|
2597 | int32_t cc_srv_wakeup_readers(struct s_client *cl) {
|
---|
2598 | int32_t wakeup = 0;
|
---|
2599 | struct s_reader *rdr;
|
---|
2600 | for (rdr = first_active_reader; rdr; rdr = rdr->next) {
|
---|
2601 | if (rdr->typ != R_CCCAM)
|
---|
2602 | continue;
|
---|
2603 | if (rdr->tcp_connected == 2)
|
---|
2604 | continue;
|
---|
2605 | if (!(rdr->grp & cl->grp))
|
---|
2606 | continue;
|
---|
2607 | if (rdr->cc_keepalive) //if reader has keepalive but is NOT connected, reader can't connect. so don't ask him
|
---|
2608 | continue;
|
---|
2609 |
|
---|
2610 | //This wakeups the reader:
|
---|
2611 | uchar dummy = 0;
|
---|
2612 | write_to_pipe(rdr->fd, PIP_ID_CIN, &dummy, sizeof(dummy));
|
---|
2613 | wakeup++;
|
---|
2614 | }
|
---|
2615 | return wakeup;
|
---|
2616 | }
|
---|
2617 |
|
---|
2618 |
|
---|
2619 | int32_t check_cccam_compat(struct cc_data *cc) {
|
---|
2620 | int32_t res = 0;
|
---|
2621 | if (strcmp(cfg.cc_version, "2.2.0") == 0 || strcmp(cfg.cc_version, "2.2.1") == 0) {
|
---|
2622 |
|
---|
2623 | if (strcmp(cc->remote_version, "2.2.0") == 0 || strcmp(cc->remote_version, "2.2.1") == 0) {
|
---|
2624 | res = 1;
|
---|
2625 | }
|
---|
2626 | }
|
---|
2627 | return res;
|
---|
2628 | }
|
---|
2629 |
|
---|
2630 | int32_t cc_srv_connect(struct s_client *cl) {
|
---|
2631 | int32_t i, wait_for_keepalive;
|
---|
2632 | uint8_t data[16];
|
---|
2633 | char usr[21], pwd[65];
|
---|
2634 | struct s_auth *account;
|
---|
2635 | struct cc_data *cc;
|
---|
2636 |
|
---|
2637 | memset(usr, 0, sizeof(usr));
|
---|
2638 | memset(pwd, 0, sizeof(pwd));
|
---|
2639 |
|
---|
2640 | // init internals data struct
|
---|
2641 | cc = cs_malloc(&cc, sizeof(struct cc_data), QUITERROR);
|
---|
2642 | cl->cc = cc;
|
---|
2643 | memset(cl->cc, 0, sizeof(struct cc_data));
|
---|
2644 | cc->extended_ecm_idx = ll_create();
|
---|
2645 |
|
---|
2646 | cc_init_cc(cc);
|
---|
2647 | uint8_t *buf = cc->send_buffer;
|
---|
2648 |
|
---|
2649 | cc->mode = CCCAM_MODE_NOTINIT;
|
---|
2650 | cc->server_ecm_pending = 0;
|
---|
2651 | cc->extended_mode = 0;
|
---|
2652 |
|
---|
2653 | int32_t keep_alive = 1;
|
---|
2654 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
2655 | (void *)&keep_alive, sizeof(keep_alive));
|
---|
2656 |
|
---|
2657 | //Create checksum for "O" cccam:
|
---|
2658 | for (i = 0; i < 12; i++) {
|
---|
2659 | data[i] = fast_rnd();
|
---|
2660 | }
|
---|
2661 | for (i = 0; i < 4; i++) {
|
---|
2662 | data[12 + i] = (data[i] + data[4 + i] + data[8 + i]) & 0xff;
|
---|
2663 | }
|
---|
2664 |
|
---|
2665 | cs_debug_mask(D_TRACE, "send ccc checksum");
|
---|
2666 |
|
---|
2667 | send(cl->udp_fd, data, 16, 0);
|
---|
2668 |
|
---|
2669 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
2670 |
|
---|
2671 | SHA_CTX ctx;
|
---|
2672 | SHA1_Init(&ctx);
|
---|
2673 | SHA1_Update(&ctx, data, 16);
|
---|
2674 | SHA1_Final(buf, &ctx);
|
---|
2675 |
|
---|
2676 | cc_init_crypt(&cc->block[ENCRYPT], buf, 20);
|
---|
2677 | cc_crypt(&cc->block[ENCRYPT], data, 16, DECRYPT);
|
---|
2678 | cc_init_crypt(&cc->block[DECRYPT], data, 16);
|
---|
2679 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2680 |
|
---|
2681 | cs_debug_mask(D_TRACE, "receive ccc checksum");
|
---|
2682 | if ((i = cc_recv_to(cl, buf, 20)) == 20) {
|
---|
2683 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: recv:");
|
---|
2684 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2685 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: hash:");
|
---|
2686 | } else
|
---|
2687 | return -1;
|
---|
2688 |
|
---|
2689 | // receive username
|
---|
2690 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
2691 | if ((i = cc_recv_to(cl, buf, 20)) == 20) {
|
---|
2692 | cc_crypt(&cc->block[DECRYPT], buf, 20, DECRYPT);
|
---|
2693 |
|
---|
2694 | strncpy(usr, (char *) buf, sizeof(usr));
|
---|
2695 |
|
---|
2696 | //test for nonprintable characters:
|
---|
2697 | for (i = 0; i < 20; i++) {
|
---|
2698 | if (usr[i] > 0 && usr[i] < 0x20) { //found nonprintable char
|
---|
2699 | cs_log("illegal username received");
|
---|
2700 | return -3;
|
---|
2701 | }
|
---|
2702 | }
|
---|
2703 | //cs_ddump_mask(D_CLIENT, buf, 20, "cccam: username '%s':", usr);
|
---|
2704 | } else
|
---|
2705 | return -2;
|
---|
2706 | cs_debug_mask(D_TRACE, "ccc username received %s", usr);
|
---|
2707 |
|
---|
2708 | cl->crypted = 1;
|
---|
2709 |
|
---|
2710 | //CCCam only supports len=20 usr/pass. So we could have more than one user that matches the first 20 chars.
|
---|
2711 |
|
---|
2712 | //receive password-CCCam encrypted Hash:
|
---|
2713 | if (cc_recv_to(cl, buf, 6) != 6)
|
---|
2714 | return -2;
|
---|
2715 |
|
---|
2716 | cs_debug_mask(D_TRACE, "ccc passwdhash received %s", usr);
|
---|
2717 |
|
---|
2718 | account = cfg.account;
|
---|
2719 | struct cc_crypt_block *save_block = cs_malloc(&save_block, sizeof(struct cc_crypt_block), QUITERROR);
|
---|
2720 | memcpy(save_block, cc->block, sizeof(struct cc_crypt_block));
|
---|
2721 | int32_t found = 0;
|
---|
2722 | while (1) {
|
---|
2723 | while (account) {
|
---|
2724 | if (strncmp(usr, account->usr, 20) == 0) {
|
---|
2725 | memset(pwd, 0, sizeof(pwd));
|
---|
2726 | strncpy(pwd, account->pwd, sizeof(pwd));
|
---|
2727 | found=1;
|
---|
2728 | break;
|
---|
2729 | }
|
---|
2730 | account = account->next;
|
---|
2731 | }
|
---|
2732 |
|
---|
2733 | if (!account)
|
---|
2734 | break;
|
---|
2735 |
|
---|
2736 | // receive passwd / 'CCcam'
|
---|
2737 | memcpy(cc->block, save_block, sizeof(struct cc_crypt_block));
|
---|
2738 | cc_crypt(&cc->block[DECRYPT], (uint8_t *) pwd, strlen(pwd), ENCRYPT);
|
---|
2739 | cc_crypt(&cc->block[DECRYPT], buf, 6, DECRYPT);
|
---|
2740 | //cs_ddump_mask(D_CLIENT, buf, 6, "cccam: pwd check '%s':", buf); //illegal buf-bytes could kill the logger!
|
---|
2741 | if (memcmp(buf, "CCcam\0", 6) == 0) //Password Hash OK!
|
---|
2742 | break; //account is set
|
---|
2743 |
|
---|
2744 | account = account->next;
|
---|
2745 | }
|
---|
2746 | free(save_block);
|
---|
2747 |
|
---|
2748 | if (cs_auth_client(cl, account, NULL)) { //cs_auth_client returns 0 if account is valid/active/accessible
|
---|
2749 | if (!found)
|
---|
2750 | cs_log("account '%s' not found!", usr);
|
---|
2751 | else
|
---|
2752 | cs_log("password for '%s' invalid!", usr);
|
---|
2753 | return -2;
|
---|
2754 | }
|
---|
2755 | if (cl->dup) {
|
---|
2756 | cs_log("account '%s' duplicate login, disconnect!", usr);
|
---|
2757 | return -3;
|
---|
2758 | }
|
---|
2759 | if (cl->disabled) {
|
---|
2760 | cs_log("account '%s' disabled, blocking+disconnect!", usr);
|
---|
2761 | return -2;
|
---|
2762 | }
|
---|
2763 | if (account->cccmaxhops<0) {
|
---|
2764 | cs_log("account '%s' has cccmaxhops<0, cccam can't handle this, disconnect!", usr);
|
---|
2765 | return -3;
|
---|
2766 | }
|
---|
2767 |
|
---|
2768 | cs_debug_mask(D_TRACE, "ccc user authenticated %s", usr);
|
---|
2769 |
|
---|
2770 | cc->prefix = cs_malloc(&cc->prefix, strlen(cl->account->usr)+20, QUITERROR);
|
---|
2771 | snprintf(cc->prefix, strlen(cl->account->usr)+20, "cccam(s) %s: ", cl->account->usr);
|
---|
2772 |
|
---|
2773 | //Starting readers to get cards:
|
---|
2774 | cc_srv_wakeup_readers(cl);
|
---|
2775 |
|
---|
2776 | // send passwd ack
|
---|
2777 | memset(buf, 0, 20);
|
---|
2778 | memcpy(buf, "CCcam\0", 6);
|
---|
2779 | cs_ddump_mask(D_CLIENT, buf, 20, "cccam: send ack:");
|
---|
2780 | cc_crypt(&cc->block[ENCRYPT], buf, 20, ENCRYPT);
|
---|
2781 | send(cl->pfd, buf, 20, 0);
|
---|
2782 |
|
---|
2783 | // recv cli data
|
---|
2784 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
2785 | i = cc_msg_recv(cl, buf, CC_MAXMSGSIZE);
|
---|
2786 | if (i < 0)
|
---|
2787 | return -1;
|
---|
2788 | cs_ddump_mask(D_CLIENT, buf, i, "cccam: cli data:");
|
---|
2789 | memcpy(cc->peer_node_id, buf + 24, 8);
|
---|
2790 | //chk_peer_node_for_oscam(cc);
|
---|
2791 |
|
---|
2792 | strncpy(cc->remote_version, (char*)buf+33, sizeof(cc->remote_version)-1);
|
---|
2793 | strncpy(cc->remote_build, (char*)buf+65, sizeof(cc->remote_build)-1);
|
---|
2794 |
|
---|
2795 | cs_debug_mask(D_CLIENT, "%s client '%s' (%s) running v%s (%s)", getprefix(), buf + 4,
|
---|
2796 | cs_hexdump(0, cc->peer_node_id, 8), cc->remote_version, cc->remote_build);
|
---|
2797 |
|
---|
2798 | // send cli data ack
|
---|
2799 | cc_cmd_send(cl, NULL, 0, MSG_CLI_DATA);
|
---|
2800 |
|
---|
2801 | cs_debug_mask(D_TRACE, "ccc send srv_data %s", usr);
|
---|
2802 | if (cc_send_srv_data(cl) < 0)
|
---|
2803 | return -1;
|
---|
2804 |
|
---|
2805 | cc->cccam220 = check_cccam_compat(cc);
|
---|
2806 | cc->just_logged_in = TRUE;
|
---|
2807 | cc->answer_on_keepalive = time(NULL);
|
---|
2808 |
|
---|
2809 | //Wait for Partner detection (NOK1 with data) before reporting cards
|
---|
2810 | //When Partner is detected, cccam220=1 is set. then we can report extended card data
|
---|
2811 | i = process_input(buf, CC_MAXMSGSIZE, 1);
|
---|
2812 | if (i<=0 && i != -9)
|
---|
2813 | return 0; //disconnected
|
---|
2814 |
|
---|
2815 | if (cc->cccam220)
|
---|
2816 | cs_debug_mask(D_CLIENT, "%s extended sid mode activated", getprefix());
|
---|
2817 | else
|
---|
2818 | cs_debug_mask(D_CLIENT, "%s 2.1.x compatibility mode", getprefix());
|
---|
2819 |
|
---|
2820 | cs_debug_mask(D_TRACE, "ccc send cards %s", usr);
|
---|
2821 |
|
---|
2822 | if (!cc_srv_report_cards(cl))
|
---|
2823 | return -1;
|
---|
2824 | cs_ftime(&cc->ecm_time);
|
---|
2825 |
|
---|
2826 | time_t timeout = time(NULL);
|
---|
2827 | wait_for_keepalive = 100;
|
---|
2828 | cc->mode = CCCAM_MODE_NORMAL;
|
---|
2829 | //some clients, e.g. mgcamd, does not support keepalive. So if not answered, keep connection
|
---|
2830 | // check for client timeout, if timeout occurs try to send keepalive
|
---|
2831 | cs_debug_mask(D_TRACE, "ccc connected and waiting for data %s", usr);
|
---|
2832 | while (cl->pfd && cl->udp_fd && cc->mode == CCCAM_MODE_NORMAL && !cl->dup)
|
---|
2833 | {
|
---|
2834 | i = process_input(buf, CC_MAXMSGSIZE, 10);
|
---|
2835 | if (i <= 0 && i != -9)
|
---|
2836 | break; //Disconnected by client
|
---|
2837 |
|
---|
2838 | //data is parsed!
|
---|
2839 | if (i == MSG_CW_ECM)
|
---|
2840 | timeout = time(NULL);
|
---|
2841 | else if (i == MSG_KEEPALIVE) {
|
---|
2842 | wait_for_keepalive = 0;
|
---|
2843 | timeout = time(NULL);
|
---|
2844 | }
|
---|
2845 |
|
---|
2846 | //new timeout check:
|
---|
2847 | if (time(NULL)-timeout > (time_t)cfg.cmaxidle) {
|
---|
2848 | cs_debug_mask(D_TRACE, "ccc idle %s", usr);
|
---|
2849 | //cs_debug_mask(D_TRACE, "client timeout user %s idle=%d client max idle=%d", usr, cmi, cfg.cmaxidle);
|
---|
2850 | if (cfg.cc_keep_connected || cl->account->ncd_keepalive) {
|
---|
2851 | if (cc_cmd_send(cl, NULL, 0, MSG_KEEPALIVE) < 0)
|
---|
2852 | break;
|
---|
2853 | if (wait_for_keepalive<3 || wait_for_keepalive == 100) {
|
---|
2854 | cs_debug_mask(D_CLIENT, "cccam: keepalive");
|
---|
2855 | cc->answer_on_keepalive = time(NULL);
|
---|
2856 | wait_for_keepalive++;
|
---|
2857 | }
|
---|
2858 | else if (wait_for_keepalive<100) break;
|
---|
2859 | timeout = time(NULL);
|
---|
2860 | } else {
|
---|
2861 | cs_debug_mask(D_CLIENT, "%s keepalive after maxidle is reached",
|
---|
2862 | getprefix());
|
---|
2863 | break; //Disconnect client
|
---|
2864 | }
|
---|
2865 | }
|
---|
2866 | }
|
---|
2867 | cc->mode = CCCAM_MODE_SHUTDOWN;
|
---|
2868 | return 0;
|
---|
2869 | }
|
---|
2870 |
|
---|
2871 | void * cc_srv_init(struct s_client *cl) {
|
---|
2872 | cl->thread = pthread_self();
|
---|
2873 | pthread_setspecific(getclient, cl);
|
---|
2874 |
|
---|
2875 | if (cl->ip)
|
---|
2876 | cs_debug_mask(D_CLIENT, "cccam: new connection from %s", cs_inet_ntoa(cl->ip));
|
---|
2877 |
|
---|
2878 | cl->pfd = cl->udp_fd;
|
---|
2879 | int32_t ret;
|
---|
2880 | if ((ret=cc_srv_connect(cl)) < 0) {
|
---|
2881 | if (errno != 0)
|
---|
2882 | cs_debug_mask(D_CLIENT, "cccam: failed errno: %d (%s)", errno, strerror(errno));
|
---|
2883 | else
|
---|
2884 | cs_debug_mask(D_CLIENT, "cccam: failed ret: %d", ret);
|
---|
2885 | if (ret == -2)
|
---|
2886 | cs_add_violation((uint)cl->ip);
|
---|
2887 | }
|
---|
2888 | cs_disconnect_client(cl);
|
---|
2889 | return NULL; //suppress compiler warning
|
---|
2890 | }
|
---|
2891 |
|
---|
2892 | int32_t cc_cli_connect(struct s_client *cl) {
|
---|
2893 | struct s_reader *rdr = cl->reader;
|
---|
2894 | struct cc_data *cc = cl->cc;
|
---|
2895 | rdr->card_status = CARD_FAILURE;
|
---|
2896 |
|
---|
2897 | if (cc && cc->mode == CCCAM_MODE_SHUTDOWN)
|
---|
2898 | return -99;
|
---|
2899 |
|
---|
2900 | if (!cc) {
|
---|
2901 | // init internals data struct
|
---|
2902 | cc = cs_malloc(&cc, sizeof(struct cc_data), QUITERROR);
|
---|
2903 | memset(cc, 0, sizeof(struct cc_data));
|
---|
2904 | cc_init_cc(cc);
|
---|
2905 | cc->cards = ll_create();
|
---|
2906 | cl->cc = cc;
|
---|
2907 | cc->pending_emms = ll_create();
|
---|
2908 | cc->extended_ecm_idx = ll_create();
|
---|
2909 | } else {
|
---|
2910 | if (cc->cards) {
|
---|
2911 | while (cs_trylock(&cc->cards_busy)) {
|
---|
2912 | cs_debug_mask(D_TRACE, "%s trylock cc_cli_connect cards waiting", getprefix());
|
---|
2913 | cs_sleepms(50);
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | cc_free_cardlist(cc->cards, FALSE);
|
---|
2917 | cs_unlock(&cc->cards_busy);
|
---|
2918 | }
|
---|
2919 | if (cc->extended_ecm_idx)
|
---|
2920 | free_extended_ecm_idx(cc);
|
---|
2921 |
|
---|
2922 | cs_trylock(&cc->ecm_busy);
|
---|
2923 | cs_unlock(&cc->ecm_busy);
|
---|
2924 | }
|
---|
2925 | if (!cc->prefix)
|
---|
2926 | cc->prefix = cs_malloc(&cc->prefix, strlen(cl->reader->label)+20, QUITERROR);
|
---|
2927 | snprintf(cc->prefix, strlen(cl->reader->label)+20, "cccam(r) %s: ", cl->reader->label);
|
---|
2928 |
|
---|
2929 | if (!cl->udp_fd) {
|
---|
2930 | cc_cli_init_int(cl);
|
---|
2931 | return -1; // cc_cli_init_int32_t calls cc_cli_connect, so exit here!
|
---|
2932 | }
|
---|
2933 |
|
---|
2934 | if (is_connect_blocked(rdr)) {
|
---|
2935 | struct timeb cur_time;
|
---|
2936 | cs_ftime(&cur_time);
|
---|
2937 | int32_t time = 1000*(rdr->tcp_block_connect_till.time-cur_time.time)
|
---|
2938 | +rdr->tcp_block_connect_till.millitm-cur_time.millitm;
|
---|
2939 | cs_log("%s connection blocked, retrying in %ds", rdr->label, time/1000);
|
---|
2940 | return -1;
|
---|
2941 | }
|
---|
2942 |
|
---|
2943 | int32_t handle, n;
|
---|
2944 | uint8_t data[20];
|
---|
2945 | uint8_t hash[SHA_DIGEST_LENGTH];
|
---|
2946 | uint8_t *buf = cc->send_buffer;
|
---|
2947 | char pwd[64];
|
---|
2948 |
|
---|
2949 | // check cred config
|
---|
2950 | if (rdr->device[0] == 0 || rdr->r_pwd[0] == 0 || rdr->r_usr[0] == 0
|
---|
2951 | || rdr->r_port == 0) {
|
---|
2952 | cs_log("%s configuration error!", rdr->label);
|
---|
2953 | return -5;
|
---|
2954 | }
|
---|
2955 |
|
---|
2956 | // connect
|
---|
2957 | handle = network_tcp_connection_open();
|
---|
2958 | if (handle <= 0) {
|
---|
2959 | cs_log("%s network connect error!", rdr->label);
|
---|
2960 | return -1;
|
---|
2961 | }
|
---|
2962 | if (errno == EISCONN) {
|
---|
2963 | cc_cli_close(cl, FALSE);
|
---|
2964 |
|
---|
2965 | block_connect(rdr);
|
---|
2966 | return -1;
|
---|
2967 | }
|
---|
2968 |
|
---|
2969 | // get init seed
|
---|
2970 | if ((n = cc_recv_to(cl, data, 16)) != 16) {
|
---|
2971 | int32_t err = errno;
|
---|
2972 | if (n <= 0)
|
---|
2973 | cs_log("%s server blocked connection!", rdr->label);
|
---|
2974 | else
|
---|
2975 | cs_log("%s server does not return 16 bytes (n=%d, errno=%d %s)",
|
---|
2976 | rdr->label, n, err, strerror(errno));
|
---|
2977 | block_connect(rdr);
|
---|
2978 | return -2;
|
---|
2979 | }
|
---|
2980 |
|
---|
2981 |
|
---|
2982 | cc->ecm_counter = 0;
|
---|
2983 | cc->max_ecms = 0;
|
---|
2984 | cc->cmd05_mode = MODE_UNKNOWN;
|
---|
2985 | cc->cmd05_offset = 0;
|
---|
2986 | cc->cmd05_active = 0;
|
---|
2987 | cc->cmd05_data_len = 0;
|
---|
2988 | cc->answer_on_keepalive = time(NULL);
|
---|
2989 | cc->extended_mode = 0;
|
---|
2990 | cc->last_emm_card = NULL;
|
---|
2991 | cc->num_hop1 = 0;
|
---|
2992 | cc->num_hop2 = 0;
|
---|
2993 | cc->num_hopx = 0;
|
---|
2994 | cc->num_reshare0 = 0;
|
---|
2995 | cc->num_reshare1 = 0;
|
---|
2996 | cc->num_reshare2 = 0;
|
---|
2997 | cc->num_resharex = 0;
|
---|
2998 | memset(&cc->cmd05_data, 0, sizeof(cc->cmd05_data));
|
---|
2999 | memset(&cc->receive_buffer, 0, sizeof(cc->receive_buffer));
|
---|
3000 | cc->cmd0c_mode = MODE_CMD_0x0C_NONE;
|
---|
3001 |
|
---|
3002 | cs_ddump_mask(D_CLIENT, data, 16, "cccam: server init seed:");
|
---|
3003 |
|
---|
3004 | uint16_t sum = 0x1234;
|
---|
3005 | uint16_t recv_sum = (data[14] << 8) | data[15];
|
---|
3006 | int32_t i;
|
---|
3007 | for (i = 0; i < 14; i++) {
|
---|
3008 | sum += data[i];
|
---|
3009 | }
|
---|
3010 | //Create special data to detect oscam-cccam:
|
---|
3011 | cc->is_oscam_cccam = sum == recv_sum;
|
---|
3012 |
|
---|
3013 | cc_xor(data); // XOR init bytes with 'CCcam'
|
---|
3014 |
|
---|
3015 | SHA_CTX ctx;
|
---|
3016 | SHA1_Init(&ctx);
|
---|
3017 | SHA1_Update(&ctx, data, 16);
|
---|
3018 | SHA1_Final(hash, &ctx);
|
---|
3019 |
|
---|
3020 | cs_ddump_mask(D_CLIENT, hash, sizeof(hash), "cccam: sha1 hash:");
|
---|
3021 |
|
---|
3022 | //initialisate crypto states
|
---|
3023 | cc_init_crypt(&cc->block[DECRYPT], hash, 20);
|
---|
3024 | cc_crypt(&cc->block[DECRYPT], data, 16, DECRYPT);
|
---|
3025 | cc_init_crypt(&cc->block[ENCRYPT], data, 16);
|
---|
3026 | cc_crypt(&cc->block[ENCRYPT], hash, 20, DECRYPT);
|
---|
3027 |
|
---|
3028 | cc_cmd_send(cl, hash, 20, MSG_NO_HEADER); // send crypted hash to server
|
---|
3029 |
|
---|
3030 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
3031 | memcpy(buf, rdr->r_usr, strlen(rdr->r_usr));
|
---|
3032 | cs_ddump_mask(D_CLIENT, buf, 20, "cccam: username '%s':", buf);
|
---|
3033 | cc_cmd_send(cl, buf, 20, MSG_NO_HEADER); // send usr '0' padded -> 20 bytes
|
---|
3034 |
|
---|
3035 | memset(buf, 0, CC_MAXMSGSIZE);
|
---|
3036 | memset(pwd, 0, sizeof(pwd));
|
---|
3037 |
|
---|
3038 | //cs_debug_mask(D_CLIENT, "cccam: 'CCcam' xor");
|
---|
3039 | memcpy(buf, "CCcam", 5);
|
---|
3040 | strncpy(pwd, rdr->r_pwd, sizeof(pwd) - 1);
|
---|
3041 | cc_crypt(&cc->block[ENCRYPT], (uint8_t *) pwd, strlen(pwd), ENCRYPT);
|
---|
3042 | cc_cmd_send(cl, buf, 6, MSG_NO_HEADER); // send 'CCcam' xor w/ pwd
|
---|
3043 |
|
---|
3044 | if ((n = cc_recv_to(cl, data, 20)) != 20) {
|
---|
3045 | cs_log("%s login failed, pwd ack not received (n = %d)", getprefix(), n);
|
---|
3046 | return -2;
|
---|
3047 | }
|
---|
3048 | cc_crypt(&cc->block[DECRYPT], data, 20, DECRYPT);
|
---|
3049 | cs_ddump_mask(D_CLIENT, data, 20, "cccam: pwd ack received:");
|
---|
3050 |
|
---|
3051 | if (memcmp(data, buf, 5)) { // check server response
|
---|
3052 | cs_log("%s login failed, usr/pwd invalid", getprefix());
|
---|
3053 | return -2;
|
---|
3054 | } else {
|
---|
3055 | cs_debug_mask(D_READER, "%s login succeeded", getprefix());
|
---|
3056 | }
|
---|
3057 |
|
---|
3058 | cs_debug_mask(D_READER, "cccam: last_s=%d, last_g=%d", rdr->last_s, rdr->last_g);
|
---|
3059 |
|
---|
3060 | cl->pfd = cl->udp_fd;
|
---|
3061 | cs_debug_mask(D_READER, "cccam: pfd=%d", cl->pfd);
|
---|
3062 |
|
---|
3063 | if (cc_send_cli_data(cl) <= 0) {
|
---|
3064 | cs_log("%s login failed, could not send client data", getprefix());
|
---|
3065 | return -3;
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | rdr->caid = rdr->ftab.filts[0].caid;
|
---|
3069 | rdr->nprov = rdr->ftab.filts[0].nprids;
|
---|
3070 | for (n = 0; n < rdr->nprov; n++) {
|
---|
3071 | rdr->availkeys[n][0] = 1;
|
---|
3072 | rdr->prid[n][0] = rdr->ftab.filts[0].prids[n] >> 24;
|
---|
3073 | rdr->prid[n][1] = rdr->ftab.filts[0].prids[n] >> 16;
|
---|
3074 | rdr->prid[n][2] = rdr->ftab.filts[0].prids[n] >> 8;
|
---|
3075 | rdr->prid[n][3] = rdr->ftab.filts[0].prids[n] & 0xff;
|
---|
3076 | }
|
---|
3077 |
|
---|
3078 | rdr->card_status = CARD_NEED_INIT;
|
---|
3079 | rdr->last_g = rdr->last_s = time((time_t *) 0);
|
---|
3080 | rdr->tcp_connected = 1;
|
---|
3081 | rdr->available = 1;
|
---|
3082 |
|
---|
3083 | cc->just_logged_in = 1;
|
---|
3084 | cc->mode = CCCAM_MODE_NORMAL;
|
---|
3085 | cl->crypted = 1;
|
---|
3086 |
|
---|
3087 | return 0;
|
---|
3088 | }
|
---|
3089 |
|
---|
3090 | int32_t cc_cli_init_int(struct s_client *cl) {
|
---|
3091 | struct s_reader *rdr = cl->reader;
|
---|
3092 | if (rdr->tcp_connected)
|
---|
3093 | return 1;
|
---|
3094 |
|
---|
3095 | if (cl->pfd) {
|
---|
3096 | close(cl->pfd);
|
---|
3097 | if (cl->pfd == cl->udp_fd)
|
---|
3098 | cl->udp_fd = 0;
|
---|
3099 | cl->pfd = 0;
|
---|
3100 | }
|
---|
3101 |
|
---|
3102 | if (cl->udp_fd) {
|
---|
3103 | close(cl->udp_fd);
|
---|
3104 | cl->udp_fd = 0;
|
---|
3105 | }
|
---|
3106 |
|
---|
3107 | if (rdr->r_port <= 0) {
|
---|
3108 | cs_log("%s invalid port %d for server %s", rdr->label, rdr->r_port,
|
---|
3109 | rdr->device);
|
---|
3110 | return 1;
|
---|
3111 | }
|
---|
3112 | // cl->ip = 0;
|
---|
3113 | // memset((char *) &loc_sa, 0, sizeof(loc_sa));
|
---|
3114 | // loc_sa.sin_family = AF_INET;
|
---|
3115 | //#ifdef LALL
|
---|
3116 | // if (cfg.serverip[0])
|
---|
3117 | // loc_sa.sin_addr.s_addr = inet_addr(cfg.serverip);
|
---|
3118 | // else
|
---|
3119 | //#endif
|
---|
3120 | // loc_sa.sin_addr.s_addr = INADDR_ANY;
|
---|
3121 | // loc_sa.sin_port = htons(rdr->l_port);
|
---|
3122 |
|
---|
3123 |
|
---|
3124 | if ((cl->udp_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0) {
|
---|
3125 | cs_log("%s Socket creation failed (socket=%d, errno=%d %s)", rdr->label,
|
---|
3126 | cl->udp_fd, errno, strerror(errno));
|
---|
3127 | return 1;
|
---|
3128 | }
|
---|
3129 | //cs_log("%s 1 socket created: cs_idx=%d, fd=%d errno=%d", getprefix(), cs_idx, cl->udp_fd, errno);
|
---|
3130 |
|
---|
3131 | #ifdef SO_PRIORITY
|
---|
3132 | if (cfg.netprio)
|
---|
3133 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_PRIORITY,
|
---|
3134 | (void *)&cfg.netprio, sizeof(uintptr_t));
|
---|
3135 | #endif
|
---|
3136 | int32_t keep_alive = 1;
|
---|
3137 | setsockopt(cl->udp_fd, SOL_SOCKET, SO_KEEPALIVE,
|
---|
3138 | (void *)&keep_alive, sizeof(keep_alive));
|
---|
3139 |
|
---|
3140 | rdr->tcp_ito = 1; //60sec...This now invokes ph_idle()
|
---|
3141 | if (rdr->cc_maxhop < 0)
|
---|
3142 | rdr->cc_maxhop = 10;
|
---|
3143 |
|
---|
3144 | memset((char *) &cl->udp_sa, 0, sizeof(cl->udp_sa));
|
---|
3145 | cl->udp_sa.sin_family = AF_INET;
|
---|
3146 | cl->udp_sa.sin_port = htons((uint16_t) rdr->r_port);
|
---|
3147 |
|
---|
3148 | if (rdr->tcp_rto <= 2)
|
---|
3149 | rdr->tcp_rto = 2; // timeout to 120s
|
---|
3150 | cs_debug_mask(D_READER, "cccam: timeout set to: %d", rdr->tcp_rto);
|
---|
3151 | cc_check_version(rdr->cc_version, rdr->cc_build);
|
---|
3152 | cs_debug_mask(D_READER, "proxy reader: %s (%s:%d) cccam v%s build %s, maxhop: %d",
|
---|
3153 | rdr->label, rdr->device, rdr->r_port, rdr->cc_version,
|
---|
3154 | rdr->cc_build, rdr->cc_maxhop);
|
---|
3155 |
|
---|
3156 | return 0;
|
---|
3157 | }
|
---|
3158 |
|
---|
3159 | int32_t cc_cli_init(struct s_client *cl) {
|
---|
3160 | struct cc_data *cc = cl->cc;
|
---|
3161 | struct s_reader *reader = cl->reader;
|
---|
3162 |
|
---|
3163 | if ((cc && cc->mode == CCCAM_MODE_SHUTDOWN))
|
---|
3164 | return -1;
|
---|
3165 |
|
---|
3166 | int32_t res = cc_cli_init_int(cl); //Create socket
|
---|
3167 |
|
---|
3168 | if (res == 0 && reader && (reader->cc_keepalive || !cl->cc) && !reader->tcp_connected) {
|
---|
3169 |
|
---|
3170 | cc_cli_connect(cl); //connect to remote server
|
---|
3171 |
|
---|
3172 | // while (!reader->tcp_connected && reader->cc_keepalive && cfg.reader_restart_seconds > 0) {
|
---|
3173 | //
|
---|
3174 | // if ((cc && cc->mode == CCCAM_MODE_SHUTDOWN))
|
---|
3175 | // return -1;
|
---|
3176 | //
|
---|
3177 | // if (!reader->tcp_connected) {
|
---|
3178 | // cc_cli_close(cl, FALSE);
|
---|
3179 | // res = cc_cli_init_int(cl);
|
---|
3180 | // if (res)
|
---|
3181 | // return res;
|
---|
3182 | // }
|
---|
3183 | // cs_debug_mask(D_READER, "%s restarting reader in %d seconds", reader->label, cfg.reader_restart_seconds);
|
---|
3184 | // cs_sleepms(cfg.reader_restart_seconds*1000);
|
---|
3185 | // cs_debug_mask(D_READER, "%s restarting reader...", reader->label);
|
---|
3186 | // cc_cli_connect(cl);
|
---|
3187 | // }
|
---|
3188 | }
|
---|
3189 | return res;
|
---|
3190 | }
|
---|
3191 |
|
---|
3192 | /**
|
---|
3193 | * return 1 if we are able to send requests:
|
---|
3194 | *
|
---|
3195 | */
|
---|
3196 | int32_t cc_available(struct s_reader *rdr, int32_t checktype) {
|
---|
3197 | if (!rdr || !rdr->client) return 0;
|
---|
3198 |
|
---|
3199 | struct s_client *cl = rdr->client;
|
---|
3200 | if(!cl) return 0;
|
---|
3201 | //cs_debug_mask(D_TRACE, "checking reader %s availibility", rdr->label);
|
---|
3202 | if (!cl->cc || rdr->tcp_connected != 2 || rdr->card_status != CARD_INSERTED) {
|
---|
3203 | //Two cases:
|
---|
3204 | // 1. Keepalive ON but not connected: Do NOT send requests,
|
---|
3205 | // because we can't connect - problem of full running pipes
|
---|
3206 | // 2. Keepalive OFF but not connected: Send requests to connect
|
---|
3207 | // pipe won't run full, because we are reading from pipe to
|
---|
3208 | // get the ecm request
|
---|
3209 | if (rdr->cc_keepalive)
|
---|
3210 | return 0;
|
---|
3211 | }
|
---|
3212 |
|
---|
3213 | if (checktype == AVAIL_CHECK_LOADBALANCE && !rdr->available) {
|
---|
3214 | cs_debug_mask(D_TRACE, "checking reader %s availibility=0 (unavail)",
|
---|
3215 | rdr->label);
|
---|
3216 | return 0; //We are processing EMMs/ECMs
|
---|
3217 | }
|
---|
3218 |
|
---|
3219 | return 1;
|
---|
3220 | }
|
---|
3221 |
|
---|
3222 | /**
|
---|
3223 | *
|
---|
3224 | *
|
---|
3225 | **/
|
---|
3226 | void cc_card_info() {
|
---|
3227 | struct s_client *cl = cur_client();
|
---|
3228 | struct s_reader *rdr = cl->reader;
|
---|
3229 |
|
---|
3230 | if (rdr && !rdr->tcp_connected)
|
---|
3231 | cc_cli_connect(cl);
|
---|
3232 | }
|
---|
3233 |
|
---|
3234 | void cc_cleanup(struct s_client *cl) {
|
---|
3235 | struct cc_data *cc = cl->cc;
|
---|
3236 | if (cc) cc->mode = CCCAM_MODE_SHUTDOWN;
|
---|
3237 |
|
---|
3238 | if (cl->typ != 'c') {
|
---|
3239 | cc_cli_close(cl, FALSE); // we need to close open fd's
|
---|
3240 | }
|
---|
3241 | cc_free(cl);
|
---|
3242 | }
|
---|
3243 |
|
---|
3244 | void cc_update_nodeid()
|
---|
3245 | {
|
---|
3246 | //Partner Detection:
|
---|
3247 | init_rnd();
|
---|
3248 | uint16_t sum = 0x1234; //This is our checksum
|
---|
3249 | int32_t i;
|
---|
3250 | for (i = 0; i < 4; i++) {
|
---|
3251 | cc_node_id[i] = fast_rnd();
|
---|
3252 | sum += cc_node_id[i];
|
---|
3253 | }
|
---|
3254 |
|
---|
3255 | // Partner ID:
|
---|
3256 | cc_node_id[4] = 0x10; // (Oscam 0x10, vPlugServer 0x11, Hadu 0x12,...)
|
---|
3257 | sum += cc_node_id[4];
|
---|
3258 |
|
---|
3259 | // generate checksum for Partner ID:
|
---|
3260 | cc_node_id[5] = 0xAA;
|
---|
3261 | for (i = 0; i < 5; i++) {
|
---|
3262 | cc_node_id[5] ^= cc_node_id[i];
|
---|
3263 | }
|
---|
3264 | sum += cc_node_id[5];
|
---|
3265 |
|
---|
3266 | cc_node_id[6] = sum >> 8;
|
---|
3267 | cc_node_id[7] = sum & 0xff;
|
---|
3268 |
|
---|
3269 | int valid = 0;
|
---|
3270 | if (cfg.cc_use_fixed_nodeid) {
|
---|
3271 | for (i=0;i<8;i++)
|
---|
3272 | if (cfg.cc_fixed_nodeid[i])
|
---|
3273 | valid = 1;
|
---|
3274 | }
|
---|
3275 | if (valid)
|
---|
3276 | memcpy(cc_node_id, cfg.cc_fixed_nodeid, 8);
|
---|
3277 | else
|
---|
3278 | memcpy(cfg.cc_fixed_nodeid, cc_node_id, 8);
|
---|
3279 | }
|
---|
3280 |
|
---|
3281 | void module_cccam(struct s_module *ph) {
|
---|
3282 | cs_strncpy(ph->desc, "cccam", sizeof(ph->desc));
|
---|
3283 | ph->type = MOD_CONN_TCP;
|
---|
3284 | ph->logtxt = ", crypted";
|
---|
3285 | ph->watchdog = 1;
|
---|
3286 | ph->recv = cc_recv;
|
---|
3287 | ph->cleanup = cc_cleanup;
|
---|
3288 | ph->multi = 1;
|
---|
3289 | ph->c_multi = 1;
|
---|
3290 | ph->c_init = cc_cli_init;
|
---|
3291 | ph->c_idle = cc_idle;
|
---|
3292 | ph->c_recv_chk = cc_recv_chk;
|
---|
3293 | ph->c_send_ecm = cc_send_ecm;
|
---|
3294 | ph->c_send_emm = cc_send_emm;
|
---|
3295 | ph->s_ip = cfg.cc_srvip;
|
---|
3296 | ph->s_handler = cc_srv_init;
|
---|
3297 | ph->send_dcw = cc_send_dcw;
|
---|
3298 | ph->c_available = cc_available;
|
---|
3299 | ph->c_card_info = cc_card_info;
|
---|
3300 | static PTAB ptab; //since there is always only 1 cccam server running, this is threadsafe
|
---|
3301 | memset(&ptab, 0, sizeof(PTAB));
|
---|
3302 | int32_t i;
|
---|
3303 | for (i=0;i<CS_MAXPORTS;i++) {
|
---|
3304 | if (!cfg.cc_port[i]) break;
|
---|
3305 | ptab.ports[i].s_port = cfg.cc_port[i];
|
---|
3306 | ptab.nports++;
|
---|
3307 | }
|
---|
3308 |
|
---|
3309 | ph->ptab = &ptab;
|
---|
3310 | ph->num = R_CCCAM;
|
---|
3311 |
|
---|
3312 | cc_update_nodeid();
|
---|
3313 |
|
---|
3314 | if (cfg.cc_port)
|
---|
3315 | init_share();
|
---|
3316 | }
|
---|
3317 | #endif
|
---|