1 /*
2  *  Tvheadend
3  *  Copyright (C) 2013 Andreas Öman
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 as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __TVH_DESCRAMBLER_H__
20 #define __TVH_DESCRAMBLER_H__
21 
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include "queue.h"
25 #include "descrambler/tvhcsa.h"
26 
27 struct service;
28 struct elementary_stream;
29 struct tvhcsa;
30 struct mpegts_table;
31 struct mpegts_mux;
32 struct th_descrambler_data;
33 
34 #define DESCRAMBLER_NONE 0
35 #define DESCRAMBLER_DES  1
36 #define DESCRAMBLER_AES  2
37 
38 /**
39  * Descrambler superclass
40  *
41  * Created/Destroyed on per-transport basis upon transport start/stop
42  */
43 typedef struct th_descrambler {
44   LIST_ENTRY(th_descrambler) td_service_link;
45 
46   char *td_nicename;
47 
48   enum {
49     DS_UNKNOWN,
50     DS_RESOLVED,
51     DS_FORBIDDEN,
52     DS_IDLE
53   } td_keystate;
54 
55   struct service *td_service;
56 
57   void (*td_stop)       (struct th_descrambler *d);
58   void (*td_caid_change)(struct th_descrambler *d);
59   int  (*td_ecm_reset)  (struct th_descrambler *d);
60   void (*td_ecm_idle)   (struct th_descrambler *d);
61 
62 } th_descrambler_t;
63 
64 typedef struct th_descrambler_runtime {
65   struct service *dr_service;
66   tvhcsa_t dr_csa;
67   uint32_t dr_external:1;
68   uint32_t dr_skip:1;
69   uint32_t dr_quick_ecm:1;
70   uint32_t dr_key:1;
71   uint32_t dr_key_first:1;
72   uint32_t dr_key_const:1;
73   uint8_t  dr_key_index;
74   uint8_t  dr_key_valid;
75   uint8_t  dr_key_changed;
76   uint64_t dr_key_interval;
77   int64_t  dr_key_start;
78   int64_t  dr_key_timestamp[2];
79   int64_t  dr_ecm_start[2];
80   int64_t  dr_ecm_last_key_time;
81   int64_t  dr_last_err;
82   int64_t  dr_force_skip;
83   TAILQ_HEAD(, th_descrambler_data) dr_queue;
84   uint32_t dr_queue_total;
85   tvhlog_limit_t dr_loglimit_key;
86   uint8_t  dr_key_even[16];
87   uint8_t  dr_key_odd[16];
88 } th_descrambler_runtime_t;
89 
90 typedef void (*descrambler_section_callback_t)
91   (void *opaque, int pid, const uint8_t *section, int section_len, int emm);
92 
93 /**
94  * Track required PIDs
95  */
96 typedef struct descrambler_ecmsec {
97   LIST_ENTRY(descrambler_ecmsec) link;
98   LIST_ENTRY(descrambler_ecmsec) active_link;
99   int       refcnt;
100   uint8_t   changed;
101   uint8_t   number;
102   uint8_t   quick_ecm_called;
103   uint8_t  *last_data;
104   int       last_data_len;
105   descrambler_section_callback_t callback;
106   void     *opaque;
107 } descrambler_ecmsec_t;
108 
109 typedef struct descrambler_section {
110   TAILQ_ENTRY(descrambler_section) link;
111   descrambler_section_callback_t callback;
112   void     *opaque;
113   LIST_HEAD(, descrambler_ecmsec) ecmsecs;
114 } descrambler_section_t;
115 
116 typedef struct descrambler_table {
117   TAILQ_ENTRY(descrambler_table) link;
118   struct mpegts_table *table;
119   TAILQ_HEAD(,descrambler_section) sections;
120 } descrambler_table_t;
121 
122 /**
123  * List of CA ids
124  */
125 #define CAID_REMOVE_ME ((uint16_t)-1)
126 
127 typedef struct caid {
128   LIST_ENTRY(caid) link;
129 
130   uint16_t pid;
131   uint16_t caid;
132   uint32_t providerid;
133   uint8_t  use;
134   uint8_t  filter;
135 
136 } caid_t;
137 
138 /**
139  * List of EMM subscribers
140  */
141 #define EMM_PID_UNKNOWN ((uint16_t)-1)
142 
143 typedef struct descrambler_emm {
144   TAILQ_ENTRY(descrambler_emm) link;
145 
146   uint16_t caid;
147   uint16_t pid;
148   unsigned int to_be_removed:1;
149 
150   descrambler_section_callback_t callback;
151   void *opaque;
152 } descrambler_emm_t;
153 
154 LIST_HEAD(caid_list, caid);
155 
156 #define DESCRAMBLER_ECM_PID(pid) ((pid) | (MT_FAST << 16))
157 
158 void descrambler_init          ( void );
159 void descrambler_done          ( void );
160 void descrambler_service_start ( struct service *t );
161 void descrambler_service_stop  ( struct service *t );
162 void descrambler_caid_changed  ( struct service *t );
163 int  descrambler_resolved      ( struct service *t, th_descrambler_t *ignore );
164 void descrambler_external      ( struct service *t, int state );
165 void descrambler_keys          ( th_descrambler_t *t, int type,
166                                  const uint8_t *even, const uint8_t *odd );
167 void descrambler_notify        ( th_descrambler_t *t,
168                                  uint16_t caid, uint32_t provid,
169                                  const char *cardsystem, uint16_t pid, uint32_t ecmtime,
170                                  uint16_t hops, const char *reader, const char *from,
171                                  const char *protocol );
172 int  descrambler_descramble    ( struct service *t,
173                                  struct elementary_stream *st,
174                                  const uint8_t *tsb, int len );
175 void descrambler_flush_table_data( struct service *t );
176 int  descrambler_open_pid      ( struct mpegts_mux *mux, void *opaque, int pid,
177                                  descrambler_section_callback_t callback,
178                                  struct service *service );
179 int  descrambler_close_pid     ( struct mpegts_mux *mux, void *opaque, int pid );
180 void descrambler_flush_tables  ( struct mpegts_mux *mux );
181 void descrambler_cat_data      ( struct mpegts_mux *mux, const uint8_t *data, int len );
182 int  descrambler_open_emm      ( struct mpegts_mux *mux, void *opaque, int caid,
183                                  descrambler_section_callback_t callback );
184 int  descrambler_close_emm     ( struct mpegts_mux *mux, void *opaque, int caid );
185 
186 #endif /* __TVH_DESCRAMBLER_H__ */
187 
188 /* **************************************************************************
189  * Editor
190  *
191  * vim:sts=2:ts=2:sw=2:et
192  * *************************************************************************/
193