tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tables.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Process PSI tables
  3. * Copyright (C) 2011 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License (COPYING file) for more details.
  13. *
  14. */
  15. #include "data.h"
  16. #include "tables.h"
  17. #include "camd.h"
  18. #include "filter.h"
  19. #include "libtsfuncs/tsfuncs.h"
  20. #include "libfuncs/libfuncs.h"
  21. extern void show_ts_pack(struct ts *ts, uint16_t pid, char *wtf, char *extra, uint8_t *ts_packet);
  22. #define handle_table_changes(TABLE) \
  23. do { \
  24. show_ts_pack(ts, pid, #TABLE, NULL, ts_packet); \
  25. ts->cur##TABLE = ts_##TABLE##_push_packet(ts->cur##TABLE, ts_packet); \
  26. if (!ts->cur##TABLE->initialized) \
  27. return; \
  28. if (ts_##TABLE##_is_same(ts->TABLE, ts->cur##TABLE)) { \
  29. ts_##TABLE##_clear(ts->cur##TABLE); \
  30. return; \
  31. } \
  32. ts_##TABLE##_free(&ts->TABLE); \
  33. ts->TABLE = ts_##TABLE##_copy(ts->cur##TABLE); \
  34. ts_##TABLE##_clear(ts->cur##TABLE); \
  35. if (ts->debug_level >= 1) \
  36. ts_##TABLE##_dump(ts->TABLE); \
  37. } while(0)
  38. void process_pat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  39. int i;
  40. int num_services = 0;
  41. uint16_t f_service = 0, f_pid = 0;
  42. if (pid != 0x00)
  43. return;
  44. handle_table_changes(pat);
  45. for (i=0;i<ts->pat->programs_num;i++) {
  46. struct ts_pat_program *prg = ts->pat->programs[i];
  47. if (prg->pid && prg->program != 0) {
  48. num_services++;
  49. ts->pmt_pid = prg->pid;
  50. ts->service_id = prg->program;
  51. if (prg->program == ts->forced_service_id) {
  52. f_pid = prg->pid;
  53. f_service = prg->program;
  54. }
  55. }
  56. }
  57. if (f_service && f_pid) {
  58. ts->pmt_pid = f_pid;
  59. ts->service_id = f_service;
  60. }
  61. if (num_services > 1 && !f_service) {
  62. ts_LOGf("PAT | %d services exists. Consider using --input-service parameter.\n",
  63. num_services);
  64. for (i = 0; i < ts->pat->programs_num; i++) {
  65. struct ts_pat_program *prg = ts->pat->programs[i];
  66. if (prg->pid && prg->program != 0) {
  67. ts_LOGf("PAT | Service 0x%04x (%5d) with PMT PID %04x (%d)\n",
  68. prg->program, prg->program,
  69. prg->pid, prg->pid);
  70. }
  71. }
  72. }
  73. ts_LOGf("PAT | Using service 0x%04x (%d), PMT pid: %04x (%d)\n",
  74. ts->service_id, ts->service_id,
  75. ts->pmt_pid, ts->pmt_pid);
  76. if (num_services > 1) {
  77. ts_pat_clear(ts->genpat);
  78. ts->genpat = ts_pat_init(ts->genpat, ts->pat->section_header->ts_id_number);
  79. ts_pat_add_program(ts->genpat, ts->service_id, ts->pmt_pid);
  80. }
  81. }
  82. void process_cat(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  83. if (pid != 0x01)
  84. return;
  85. handle_table_changes(cat);
  86. if (ts->camd.constant_codeword)
  87. return;
  88. if (ts->forced_caid) {
  89. ts->emm_caid = ts->forced_caid;
  90. ts_get_emm_info_by_caid(ts->cat, ts->emm_caid, &ts->emm_pid);
  91. } else {
  92. ts_get_emm_info(ts->cat, ts->req_CA_sys, &ts->emm_caid, &ts->emm_pid);
  93. }
  94. if (ts->forced_emm_pid)
  95. ts_get_emm_info_by_pid(ts->cat, &ts->emm_caid, ts->forced_emm_pid);
  96. if (ts->emm_caid) {
  97. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->emm_caid));
  98. ts_LOGf("--- | EMM CAID: 0x%04x (%s)\n", ts->emm_caid, CA_sys);
  99. if (!ts->forced_emm_pid) {
  100. ts_LOGf("--- | EMM pid : 0x%04x (%s)\n", ts->emm_pid, CA_sys);
  101. } else {
  102. ts_LOGf("--- | EMM pid : 0x%04x (%s) (forced: 0x%04x)\n",
  103. ts->emm_pid, CA_sys, ts->forced_emm_pid);
  104. ts->emm_pid = ts->forced_emm_pid;
  105. }
  106. } else {
  107. ts_LOGf("*** | ERROR: Can't detect EMM pid.\n");
  108. }
  109. }
  110. void process_pmt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  111. int i;
  112. if (!pid || pid != ts->pmt_pid)
  113. return;
  114. handle_table_changes(pmt);
  115. pidmap_clear(&ts->pidmap);
  116. pidmap_set(&ts->pidmap, 0x0000); // PAT
  117. pidmap_set(&ts->pidmap, 0x0011); // SDT
  118. if (ts->nit_passthrough)
  119. pidmap_set(&ts->pidmap, 0x0010); // NIT
  120. if (ts->eit_passthrough)
  121. pidmap_set(&ts->pidmap, 0x0012); // EIT
  122. if (ts->tdt_passthrough)
  123. pidmap_set(&ts->pidmap, 0x0014); // TDT/TOT
  124. pidmap_set(&ts->pidmap, ts->pmt->ts_header.pid); // PMT PID
  125. pidmap_set(&ts->pidmap, ts->pmt->PCR_pid); // PCR
  126. for (i=0;i<ts->pmt->streams_num;i++) {
  127. struct ts_pmt_stream *stream = ts->pmt->streams[i];
  128. pidmap_set(&ts->pidmap, stream->pid); // Data
  129. }
  130. if (ts->camd.constant_codeword)
  131. return;
  132. if (ts->forced_caid) {
  133. ts->ecm_caid = ts->forced_caid;
  134. ts_get_ecm_info_by_caid(ts->pmt, ts->ecm_caid, &ts->ecm_pid);
  135. } else {
  136. ts_get_ecm_info(ts->pmt, ts->req_CA_sys, &ts->ecm_caid, &ts->ecm_pid);
  137. }
  138. if (ts->forced_ecm_pid)
  139. ts_get_ecm_info_by_pid(ts->pmt, &ts->ecm_caid, ts->forced_ecm_pid);
  140. if (ts->ecm_caid) {
  141. char *CA_sys = ts_get_CA_sys_txt(ts_get_CA_sys(ts->ecm_caid));
  142. ts_LOGf("--- | ECM CAID: 0x%04x (%s)\n", ts->ecm_caid, CA_sys);
  143. if (!ts->forced_ecm_pid) {
  144. ts_LOGf("--- | ECM pid : 0x%04x (%s)\n", ts->ecm_pid, CA_sys);
  145. } else {
  146. ts_LOGf("--- | ECM pid : 0x%04x (%s) (forced: 0x%04x)\n",
  147. ts->ecm_pid, CA_sys, ts->forced_ecm_pid);
  148. ts->ecm_pid = ts->forced_ecm_pid;
  149. }
  150. } else {
  151. ts_LOGf("*** | ERROR: Can't detect ECM pid.\n");
  152. }
  153. }
  154. static int sdt_parse_service_name_desc(
  155. int desc_len, uint8_t *desc,
  156. uint8_t *pname_len, uint8_t **pname,
  157. uint8_t *sname_len, uint8_t **sname)
  158. {
  159. int ofs = 0;
  160. *pname_len = 0;
  161. *sname_len = 0;
  162. *pname = NULL;
  163. *sname = NULL;
  164. while (ofs + 2 < desc_len) {
  165. uint8_t tag = desc[ofs++];
  166. uint8_t len = desc[ofs++];
  167. if (tag != 0x48) {
  168. ofs += len;
  169. continue;
  170. }
  171. // Parse descriptor 0x48 - Service name descriptor
  172. // +3 == +1 for service type, +1 for provider len, +1 for service len
  173. if (ofs + 3 > desc_len)
  174. break;
  175. ofs++; // Skip service type
  176. *pname_len = desc[ofs++];
  177. if (*pname_len)
  178. *pname = desc + ofs;
  179. ofs += *pname_len;
  180. if (ofs > desc_len)
  181. break;
  182. *sname_len = desc[ofs++];
  183. if (*sname_len)
  184. *sname = desc + ofs;
  185. ofs += *sname_len;
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. void process_sdt(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  191. int i;
  192. if (pid != 0x11)
  193. return;
  194. handle_table_changes(sdt);
  195. for(i=0;i<ts->sdt->streams_num;i++) {
  196. struct ts_sdt_stream *stream = ts->sdt->streams[i];
  197. uint8_t *pname, *sname;
  198. uint8_t pname_len, sname_len;
  199. if (sdt_parse_service_name_desc(
  200. stream->descriptor_size, stream->descriptor_data,
  201. &pname_len, &pname, &sname_len, &sname))
  202. {
  203. int r;
  204. for (r = 0; r < pname_len; r++) {
  205. if (pname[r] < ' ')
  206. pname[r] = '*';
  207. }
  208. for (r = 0; r < sname_len; r++) {
  209. if (sname[r] < ' ')
  210. sname[r] = '*';
  211. }
  212. ts_LOGf("SDT | Service 0x%04x (%5d) Provider: \"%.*s\" Service: \"%.*s\"\n",
  213. stream->service_id, stream->service_id,
  214. pname_len, (char *)pname,
  215. sname_len, (char *)sname);
  216. } else {
  217. ts_LOGf("SDT | Service 0x%04x (%5d)\n",
  218. stream->service_id, stream->service_id);
  219. }
  220. }
  221. }
  222. #define dump_sz (15)
  223. #define dump_buf_sz (dump_sz * 6)
  224. static void __process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  225. char dump[dump_buf_sz];
  226. show_ts_pack(ts, pid, "emm", NULL, ts_packet);
  227. ts->emm_input_count++;
  228. if (!ts->emm_send)
  229. return;
  230. ts->emm = ts_privsec_push_packet(ts->emm, ts_packet);
  231. if (!ts->emm->initialized)
  232. return;
  233. struct ts_header *th = &ts->emm->ts_header;
  234. struct ts_section_header *sec = ts->emm->section_header;
  235. int emm_ok = 1;
  236. if (ts->emm_filters_num)
  237. emm_ok = filter_match_emm(ts, sec->section_data, sec->section_data_len);
  238. if (ts->debug_level >= 2) {
  239. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  240. ts_LOGf("EMM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d %s %s..\n",
  241. ts->service_id,
  242. ts->emm_caid,
  243. th->pid,
  244. sec->table_id,
  245. sec->section_data_len,
  246. emm_ok == 1 ? "Data:" : "SKIP:",
  247. dump);
  248. }
  249. if (emm_ok)
  250. camd_process_packet(ts, camd_msg_alloc(EMM_MSG, ts->emm_caid, ts->service_id, sec->section_data, sec->section_data_len));
  251. else
  252. ts->emm_skipped_count++;
  253. ts_privsec_copy(ts->emm, ts->last_emm);
  254. ts_privsec_clear(ts->emm);
  255. }
  256. static void __process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  257. char dump[dump_buf_sz];
  258. ts->ecm = ts_privsec_push_packet(ts->ecm, ts_packet);
  259. if (!ts->ecm->initialized)
  260. return;
  261. if (ts->req_CA_sys == CA_IRDETO) {
  262. int type = ts->ecm->section_header->section_data[4];
  263. if (type != ts->irdeto_ecm) {
  264. ts_privsec_clear(ts->ecm);
  265. return;
  266. }
  267. }
  268. struct ts_header *th = &ts->ecm->ts_header;
  269. struct ts_section_header *sec = ts->ecm->section_header;
  270. // ECMs should be in these tables.
  271. if (sec->section_data[0] != 0x80 && sec->section_data[0] != 0x81) {
  272. ts_privsec_clear(ts->ecm);
  273. return;
  274. }
  275. int duplicate = ts_privsec_is_same(ts->ecm, ts->last_ecm);
  276. if (duplicate && !ts->is_cw_error)
  277. ts->ecm_duplicate_count++;
  278. if (!ts->ecm_change_time.tv_sec)
  279. gettimeofday(&ts->ecm_change_time, NULL);
  280. if (!duplicate || ts->is_cw_error) {
  281. if (ts->ecm_cw_log) {
  282. struct timeval tv;
  283. gettimeofday(&tv, NULL);
  284. ts_LOGf("ECC | SID 0x%04x ------------ EcmChng: %5llu ms\n",
  285. ts->service_id,
  286. timeval_diff_msec(&ts->ecm_change_time, &tv));
  287. ts_hex_dump_buf(dump, dump_buf_sz, sec->section_data, min(dump_sz, sec->section_data_len), 0);
  288. ts_LOGf("ECM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: %s..\n",
  289. ts->service_id,
  290. ts->ecm_caid,
  291. th->pid,
  292. sec->table_id,
  293. sec->section_data_len,
  294. dump);
  295. gettimeofday(&ts->ecm_change_time, NULL);
  296. }
  297. ts->is_cw_error = 0;
  298. camd_process_packet(ts, camd_msg_alloc(ECM_MSG, ts->ecm_caid, ts->service_id, sec->section_data, sec->section_data_len));
  299. } else if (ts->debug_level >= 3) {
  300. ts_LOGf("ECM | SID 0x%04x CAID: 0x%04x PID 0x%04x Table: 0x%02x Length: %4d Data: -dup-\n",
  301. ts->service_id,
  302. ts->ecm_caid,
  303. th->pid,
  304. sec->table_id,
  305. sec->section_data_len);
  306. }
  307. ts_privsec_copy(ts->ecm, ts->last_ecm);
  308. ts_privsec_clear(ts->ecm);
  309. show_ts_pack(ts, pid, !duplicate ? "ecm" : "ec+", NULL, ts_packet);
  310. }
  311. // There are cryptosystems that are puting more than one PSI table
  312. // in TS packet. IRDETO is such example. Because libtsfuncs assumes
  313. // that one ts packet can produce maximum 1 PSI table, the following
  314. // workaround is used for EMM/ECM private sections. Basically we detect
  315. // if after the section there is something else than 0xff (filler) and
  316. // if there is something change ts_packet pointer field to point to
  317. // start of the potential section and reparse section.
  318. void process_ecm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  319. int section_end;
  320. if (ts->emm_only)
  321. return;
  322. if (!ts->ecm_pid || ts->ecm_pid != pid)
  323. return;
  324. process_psi:
  325. ts->tmp_ecm = ts_privsec_push_packet(ts->tmp_ecm, ts_packet);
  326. if (!ts->tmp_ecm->initialized) {
  327. __process_ecm(ts, pid, ts_packet);
  328. return;
  329. }
  330. section_end = ts->tmp_ecm->section_header->pointer_field + ts->tmp_ecm->section_header->section_length + 3 + 4 + 1;
  331. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  332. __process_ecm(ts, pid, ts_packet);
  333. ts_packet[4] = ts_packet[4] + ts->tmp_ecm->section_header->section_length + 3;
  334. ts_privsec_clear(ts->tmp_ecm);
  335. goto process_psi;
  336. } else {
  337. __process_ecm(ts, pid, ts_packet);
  338. }
  339. ts_privsec_clear(ts->tmp_ecm);
  340. }
  341. void process_emm(struct ts *ts, uint16_t pid, uint8_t *ts_packet) {
  342. int section_end;
  343. if (!ts->emm_pid || ts->emm_pid != pid)
  344. return;
  345. process_psi:
  346. ts->tmp_emm = ts_privsec_push_packet(ts->tmp_emm, ts_packet);
  347. if (!ts->tmp_emm->initialized) {
  348. __process_emm(ts, pid, ts_packet);
  349. return;
  350. }
  351. section_end = ts->tmp_emm->section_header->pointer_field + ts->tmp_emm->section_header->section_length + 3 + 4 + 1;
  352. if (section_end < 188 && ts_packet[section_end] != 0xff) {
  353. __process_emm(ts, pid, ts_packet);
  354. ts_packet[4] = ts_packet[4] + ts->tmp_emm->section_header->section_length + 3;
  355. ts_privsec_clear(ts->tmp_emm);
  356. goto process_psi;
  357. } else {
  358. __process_emm(ts, pid, ts_packet);
  359. }
  360. ts_privsec_clear(ts->tmp_emm);
  361. }