blob: 8a86b3025637d69c33a91ed7c697f5ad261fedb1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dvb_net.c
3 *
4 * Copyright (C) 2001 Convergence integrated media GmbH
5 * Ralph Metzler <ralph@convergence.de>
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
7 *
8 * ULE Decapsulation code:
9 * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH.
10 * and Department of Scientific Computing
11 * Paris Lodron University of Salzburg.
12 * Hilmar Linder <hlinder@cosy.sbg.ac.at>
13 * and Wolfram Stering <wstering@cosy.sbg.ac.at>
14 *
Christian Praehauser18232ca2006-05-22 10:31:47 -030015 * ULE Decaps according to RFC 4326.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31 */
32
33/*
34 * ULE ChangeLog:
35 * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt
36 *
37 * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt:
38 * ULE Extension header handling.
39 * Bugreports by Moritz Vieth and Hanno Tersteegen,
40 * Fraunhofer Institute for Open Communication Systems
41 * Competence Center for Advanced Satellite Communications.
42 * Bugfixes and robustness improvements.
43 * Filtering on dest MAC addresses, if present (D-Bit = 0)
44 * ULE_DEBUG compile-time option.
Christian Praehauser18232ca2006-05-22 10:31:47 -030045 * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by
46 * Christian Praehauser <cpraehaus@cosy.sbg.ac.at>,
47 * Paris Lodron University of Salzburg.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50/*
51 * FIXME / TODO (dvb_net.c):
52 *
53 * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
54 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57#include <linux/module.h>
58#include <linux/kernel.h>
59#include <linux/netdevice.h>
60#include <linux/etherdevice.h>
61#include <linux/dvb/net.h>
62#include <linux/uio.h>
63#include <asm/uaccess.h>
64#include <linux/crc32.h>
Ingo Molnar3593cab2006-02-07 06:49:14 -020065#include <linux/mutex.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040066#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include "dvb_demux.h"
69#include "dvb_net.h"
70
71static int dvb_net_debug;
72module_param(dvb_net_debug, int, 0444);
73MODULE_PARM_DESC(dvb_net_debug, "enable debug messages");
74
75#define dprintk(x...) do { if (dvb_net_debug) printk(x); } while (0)
76
77
78static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
79{
80 unsigned int j;
81 for (j = 0; j < cnt; j++)
82 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
83 return c;
84}
85
86
87#define DVB_NET_MULTICAST_MAX 10
88
89#undef ULE_DEBUG
90
91#ifdef ULE_DEBUG
92
Christian Praehauser18232ca2006-05-22 10:31:47 -030093#define MAC_ADDR_PRINTFMT "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
94#define MAX_ADDR_PRINTFMT_ARGS(macap) (macap)[0],(macap)[1],(macap)[2],(macap)[3],(macap)[4],(macap)[5]
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#define isprint(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
97
98static void hexdump( const unsigned char *buf, unsigned short len )
99{
100 char str[80], octet[10];
101 int ofs, i, l;
102
103 for (ofs = 0; ofs < len; ofs += 16) {
104 sprintf( str, "%03d: ", ofs );
105
106 for (i = 0; i < 16; i++) {
107 if ((i + ofs) < len)
108 sprintf( octet, "%02x ", buf[ofs + i] );
109 else
110 strcpy( octet, " " );
111
112 strcat( str, octet );
113 }
114 strcat( str, " " );
115 l = strlen( str );
116
117 for (i = 0; (i < 16) && ((i + ofs) < len); i++)
118 str[l++] = isprint( buf[ofs + i] ) ? buf[ofs + i] : '.';
119
120 str[l] = '\0';
121 printk( KERN_WARNING "%s\n", str );
122 }
123}
124
125#endif
126
127struct dvb_net_priv {
128 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 u16 pid;
David Howellsc4028952006-11-22 14:57:56 +0000130 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct dvb_net *host;
132 struct dmx_demux *demux;
133 struct dmx_section_feed *secfeed;
134 struct dmx_section_filter *secfilter;
135 struct dmx_ts_feed *tsfeed;
136 int multi_num;
137 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
138 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
139 int rx_mode;
140#define RX_MODE_UNI 0
141#define RX_MODE_MULTI 1
142#define RX_MODE_ALL_MULTI 2
143#define RX_MODE_PROMISC 3
144 struct work_struct set_multicast_list_wq;
145 struct work_struct restart_net_feed_wq;
146 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
147 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
148 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
149 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
150 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
151 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
152 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
153 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
154 unsigned char ule_dbit; /* Whether the DestMAC address present
155 * or not (bit is set). */
156 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
157 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
158 unsigned long ts_count; /* Current ts cell counter. */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200159 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162
163/**
164 * Determine the packet's protocol ID. The rule here is that we
165 * assume 802.3 if the type field is short enough to be a length.
166 * This is normal practice and works for any 'now in use' protocol.
167 *
168 * stolen from eth.c out of the linux kernel, hacked for dvb-device
169 * by Michael Holzt <kju@debian.org>
170 */
Al Viro12fbcef2008-05-21 00:31:11 -0300171static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct net_device *dev)
173{
174 struct ethhdr *eth;
175 unsigned char *rawp;
176
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700177 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 skb_pull(skb,dev->hard_header_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 eth = eth_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (*eth->h_dest & 1) {
dingtianhong5e231c22013-12-26 19:41:10 +0800182 if(ether_addr_equal(eth->h_dest,dev->broadcast))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 skb->pkt_type=PACKET_BROADCAST;
184 else
185 skb->pkt_type=PACKET_MULTICAST;
186 }
187
Simon Hormane5c5d222013-03-28 13:38:25 +0900188 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return eth->h_proto;
190
191 rawp = skb->data;
192
193 /**
194 * This is a magic hack to spot IPX packets. Older Novell breaks
195 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
196 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
197 * won't work for fault tolerant netware but does for the rest.
198 */
199 if (*(unsigned short *)rawp == 0xFFFF)
200 return htons(ETH_P_802_3);
201
202 /**
203 * Real 802.2 LLC
204 */
205 return htons(ETH_P_802_2);
206}
207
208#define TS_SZ 188
209#define TS_SYNC 0x47
210#define TS_TEI 0x80
211#define TS_SC 0xC0
212#define TS_PUSI 0x40
213#define TS_AF_A 0x20
214#define TS_AF_D 0x10
215
216/* ULE Extension Header handlers. */
217
218#define ULE_TEST 0
219#define ULE_BRIDGED 1
220
Christian Praehauser18232ca2006-05-22 10:31:47 -0300221#define ULE_OPTEXTHDR_PADDING 0
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int ule_test_sndu( struct dvb_net_priv *p )
224{
225 return -1;
226}
227
228static int ule_bridged_sndu( struct dvb_net_priv *p )
229{
Christian Praehauser18232ca2006-05-22 10:31:47 -0300230 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
Simon Hormane5c5d222013-03-28 13:38:25 +0900231 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300232 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
Simon Hormane5c5d222013-03-28 13:38:25 +0900233 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300234 if(framelen != ntohs(hdr->h_proto)) {
235 return -1;
236 }
237 }
238 /* Note:
239 * From RFC4326:
240 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
241 * It must be the final (or only) extension header specified in the header chain of a SNDU."
242 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244 p->ule_bridged = 1;
245 return 0;
246}
247
Christian Praehauser18232ca2006-05-22 10:31:47 -0300248static int ule_exthdr_padding(struct dvb_net_priv *p)
249{
250 return 0;
251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/** Handle ULE extension headers.
254 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
255 * Returns: >= 0: nr. of bytes consumed by next extension header
256 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
257 */
258static int handle_one_ule_extension( struct dvb_net_priv *p )
259{
260 /* Table of mandatory extension header handlers. The header type is the index. */
261 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
262 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
263
264 /* Table of optional extension header handlers. The header type is the index. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300265 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
266 { [0] = ule_exthdr_padding, [1] = NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 int ext_len = 0;
269 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
270 unsigned char htype = p->ule_sndu_type & 0x00FF;
271
272 /* Discriminate mandatory and optional extension headers. */
273 if (hlen == 0) {
274 /* Mandatory extension header */
275 if (ule_mandatory_ext_handlers[htype]) {
276 ext_len = ule_mandatory_ext_handlers[htype]( p );
Christian Praehauser18232ca2006-05-22 10:31:47 -0300277 if(ext_len >= 0) {
278 p->ule_next_hdr += ext_len;
279 if (!p->ule_bridged) {
Al Viro12fbcef2008-05-21 00:31:11 -0300280 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300281 p->ule_next_hdr += 2;
282 } else {
Al Viro12fbcef2008-05-21 00:31:11 -0300283 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
Christian Praehauser18232ca2006-05-22 10:31:47 -0300284 /* This assures the extension handling loop will terminate. */
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Christian Praehauser18232ca2006-05-22 10:31:47 -0300287 // else: extension handler failed or SNDU should be discarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 } else
289 ext_len = -1; /* SNDU has to be discarded. */
290 } else {
291 /* Optional extension header. Calculate the length. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300292 ext_len = hlen << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Process the optional extension header according to its type. */
294 if (ule_optional_ext_handlers[htype])
295 (void)ule_optional_ext_handlers[htype]( p );
296 p->ule_next_hdr += ext_len;
Al Viro12fbcef2008-05-21 00:31:11 -0300297 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
Christian Praehauser18232ca2006-05-22 10:31:47 -0300298 /*
299 * note: the length of the next header type is included in the
300 * length of THIS optional extension header
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 return ext_len;
305}
306
307static int handle_ule_extensions( struct dvb_net_priv *p )
308{
309 int total_ext_len = 0, l;
310
311 p->ule_next_hdr = p->ule_skb->data;
312 do {
313 l = handle_one_ule_extension( p );
Christian Praehauser18232ca2006-05-22 10:31:47 -0300314 if (l < 0)
315 return l; /* Stop extension header processing and discard SNDU. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 total_ext_len += l;
Christian Praehauser18232ca2006-05-22 10:31:47 -0300317#ifdef ULE_DEBUG
318 dprintk("handle_ule_extensions: ule_next_hdr=%p, ule_sndu_type=%i, "
319 "l=%i, total_ext_len=%i\n", p->ule_next_hdr,
320 (int) p->ule_sndu_type, l, total_ext_len);
321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Simon Hormane5c5d222013-03-28 13:38:25 +0900323 } while (p->ule_sndu_type < ETH_P_802_3_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return total_ext_len;
326}
327
328
329/** Prepare for a new ULE SNDU: reset the decoder state. */
330static inline void reset_ule( struct dvb_net_priv *p )
331{
332 p->ule_skb = NULL;
333 p->ule_next_hdr = NULL;
334 p->ule_sndu_len = 0;
335 p->ule_sndu_type = 0;
336 p->ule_sndu_type_1 = 0;
337 p->ule_sndu_remain = 0;
338 p->ule_dbit = 0xFF;
339 p->ule_bridged = 0;
340}
341
342/**
343 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
344 * TS cells of a single PID.
345 */
346static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
347{
Wang Chen0eade1f2008-12-03 21:13:13 -0800348 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long skipped = 0L;
Trent Piepho372280d2007-07-13 18:46:19 -0300350 const u8 *ts, *ts_end, *from_where = NULL;
351 u8 ts_remain = 0, how_much = 0, new_ts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct ethhdr *ethh = NULL;
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300353 bool error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355#ifdef ULE_DEBUG
356 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
357 static unsigned char ule_hist[100*TS_SZ];
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300358 static unsigned char *ule_where = ule_hist, ule_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* For all TS cells in current buffer.
362 * Appearently, we are called for every single TS cell.
363 */
Trent Piepho372280d2007-07-13 18:46:19 -0300364 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (new_ts) {
367 /* We are about to process a new TS cell. */
368
369#ifdef ULE_DEBUG
370 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
371 memcpy( ule_where, ts, TS_SZ );
372 if (ule_dump) {
373 hexdump( ule_where, TS_SZ );
374 ule_dump = 0;
375 }
376 ule_where += TS_SZ;
377#endif
378
379 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
380 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
381 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
382 priv->ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
383
384 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
385 if (priv->ule_skb) {
386 dev_kfree_skb( priv->ule_skb );
387 /* Prepare for next SNDU. */
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800388 dev->stats.rx_errors++;
389 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 reset_ule(priv);
392 priv->need_pusi = 1;
393
394 /* Continue with next TS cell. */
395 ts += TS_SZ;
396 priv->ts_count++;
397 continue;
398 }
399
400 ts_remain = 184;
401 from_where = ts + 4;
402 }
403 /* Synchronize on PUSI, if required. */
404 if (priv->need_pusi) {
405 if (ts[1] & TS_PUSI) {
406 /* Find beginning of first ULE SNDU in current TS cell. */
407 /* Synchronize continuity counter. */
408 priv->tscc = ts[3] & 0x0F;
409 /* There is a pointer field here. */
410 if (ts[4] > ts_remain) {
411 printk(KERN_ERR "%lu: Invalid ULE packet "
412 "(pointer field %d)\n", priv->ts_count, ts[4]);
413 ts += TS_SZ;
414 priv->ts_count++;
415 continue;
416 }
417 /* Skip to destination of pointer field. */
418 from_where = &ts[5] + ts[4];
419 ts_remain -= 1 + ts[4];
420 skipped = 0;
421 } else {
422 skipped++;
423 ts += TS_SZ;
424 priv->ts_count++;
425 continue;
426 }
427 }
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (new_ts) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300430 /* Check continuity counter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ((ts[3] & 0x0F) == priv->tscc)
432 priv->tscc = (priv->tscc + 1) & 0x0F;
433 else {
434 /* TS discontinuity handling: */
435 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
Christian Praehauser18232ca2006-05-22 10:31:47 -0300436 "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
438 if (priv->ule_skb) {
439 dev_kfree_skb( priv->ule_skb );
440 /* Prepare for next SNDU. */
441 // reset_ule(priv); moved to below.
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800442 dev->stats.rx_errors++;
443 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 reset_ule(priv);
446 /* skip to next PUSI. */
447 priv->need_pusi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 continue;
449 }
450 /* If we still have an incomplete payload, but PUSI is
451 * set; some TS cells are missing.
452 * This is only possible here, if we missed exactly 16 TS
453 * cells (continuity counter wrap). */
454 if (ts[1] & TS_PUSI) {
455 if (! priv->need_pusi) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300456 if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
458 printk(KERN_WARNING "%lu: Invalid pointer "
459 "field: %u.\n", priv->ts_count, *from_where);
460
461 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
462 if (priv->ule_skb) {
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300463 error = true;
464 dev_kfree_skb(priv->ule_skb);
465 }
466
467 if (error || priv->ule_sndu_remain) {
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800468 dev->stats.rx_errors++;
469 dev->stats.rx_frame_errors++;
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300470 error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 reset_ule(priv);
474 priv->need_pusi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 continue;
476 }
477 /* Skip pointer field (we're processing a
478 * packed payload). */
479 from_where += 1;
480 ts_remain -= 1;
481 } else
482 priv->need_pusi = 0;
483
484 if (priv->ule_sndu_remain > 183) {
485 /* Current SNDU lacks more data than there could be available in the
486 * current TS cell. */
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800487 dev->stats.rx_errors++;
488 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
490 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
491 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
492 dev_kfree_skb(priv->ule_skb);
493 /* Prepare for next SNDU. */
494 reset_ule(priv);
495 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
496 from_where += ts[4];
497 ts_remain -= ts[4];
498 }
499 }
500 }
501
502 /* Check if new payload needs to be started. */
503 if (priv->ule_skb == NULL) {
504 /* Start a new payload with skb.
505 * Find ULE header. It is only guaranteed that the
506 * length field (2 bytes) is contained in the current
507 * TS.
508 * Check ts_remain has to be >= 2 here. */
509 if (ts_remain < 2) {
510 printk(KERN_WARNING "Invalid payload packing: only %d "
511 "bytes left in TS. Resyncing.\n", ts_remain);
512 priv->ule_sndu_len = 0;
513 priv->need_pusi = 1;
Ang Way Chuang29e1fa32010-02-25 09:45:03 +0800514 ts += TS_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 continue;
516 }
517
518 if (! priv->ule_sndu_len) {
519 /* Got at least two bytes, thus extrace the SNDU length. */
520 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
521 if (priv->ule_sndu_len & 0x8000) {
522 /* D-Bit is set: no dest mac present. */
523 priv->ule_sndu_len &= 0x7FFF;
524 priv->ule_dbit = 1;
525 } else
526 priv->ule_dbit = 0;
527
Christian Praehauser18232ca2006-05-22 10:31:47 -0300528 if (priv->ule_sndu_len < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
530 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800531 dev->stats.rx_errors++;
532 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 priv->ule_sndu_len = 0;
534 priv->need_pusi = 1;
535 new_ts = 1;
536 ts += TS_SZ;
537 priv->ts_count++;
538 continue;
539 }
540 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
541 from_where += 2;
542 }
543
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300544 priv->ule_sndu_remain = priv->ule_sndu_len + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /*
546 * State of current TS:
547 * ts_remain (remaining bytes in the current TS cell)
548 * 0 ule_type is not available now, we need the next TS cell
549 * 1 the first byte of the ule_type is present
550 * >=2 full ULE header present, maybe some payload data as well.
551 */
552 switch (ts_remain) {
553 case 1:
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300554 priv->ule_sndu_remain--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 priv->ule_sndu_type = from_where[0] << 8;
556 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
557 ts_remain -= 1; from_where += 1;
558 /* Continue w/ next TS. */
559 case 0:
560 new_ts = 1;
561 ts += TS_SZ;
562 priv->ts_count++;
563 continue;
564
565 default: /* complete ULE header is present in current TS. */
566 /* Extract ULE type field. */
567 if (priv->ule_sndu_type_1) {
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300568 priv->ule_sndu_type_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 priv->ule_sndu_type |= from_where[0];
570 from_where += 1; /* points to payload start. */
571 ts_remain -= 1;
572 } else {
573 /* Complete type is present in new TS. */
574 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
575 from_where += 2; /* points to payload start. */
576 ts_remain -= 2;
577 }
578 break;
579 }
580
581 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
582 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
583 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
584 if (priv->ule_skb == NULL) {
585 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
586 dev->name);
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800587 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return;
589 }
590
591 /* This includes the CRC32 _and_ dest mac, if !dbit. */
592 priv->ule_sndu_remain = priv->ule_sndu_len;
593 priv->ule_skb->dev = dev;
594 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
595 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
596 }
597
598 /* Copy data into our current skb. */
599 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
600 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
601 priv->ule_sndu_remain -= how_much;
602 ts_remain -= how_much;
603 from_where += how_much;
604
605 /* Check for complete payload. */
606 if (priv->ule_sndu_remain <= 0) {
607 /* Check CRC32, we've got it in our skb already. */
Al Viro12fbcef2008-05-21 00:31:11 -0300608 __be16 ulen = htons(priv->ule_sndu_len);
609 __be16 utype = htons(priv->ule_sndu_type);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700610 const u8 *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct kvec iov[3] = {
612 { &ulen, sizeof ulen },
613 { &utype, sizeof utype },
614 { priv->ule_skb->data, priv->ule_skb->len - 4 }
615 };
Ang Way Chuangdedcefb2006-12-14 13:51:44 -0300616 u32 ule_crc = ~0L, expected_crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (priv->ule_dbit) {
618 /* Set D-bit for CRC32 verification,
619 * if it was set originally. */
Al Virof51b10e2008-06-22 14:20:49 -0300620 ulen |= htons(0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 ule_crc = iov_crc32(ule_crc, iov, 3);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700624 tail = skb_tail_pointer(priv->ule_skb);
625 expected_crc = *(tail - 4) << 24 |
626 *(tail - 3) << 16 |
627 *(tail - 2) << 8 |
628 *(tail - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (ule_crc != expected_crc) {
Michael Krufky5ef35be2006-12-14 13:53:33 -0300630 printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 priv->ts_count, ule_crc, expected_crc, priv->ule_sndu_len, priv->ule_sndu_type, ts_remain, ts_remain > 2 ? *(unsigned short *)from_where : 0);
632
633#ifdef ULE_DEBUG
634 hexdump( iov[0].iov_base, iov[0].iov_len );
635 hexdump( iov[1].iov_base, iov[1].iov_len );
636 hexdump( iov[2].iov_base, iov[2].iov_len );
637
638 if (ule_where == ule_hist) {
639 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
640 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
641 } else if (ule_where == &ule_hist[TS_SZ]) {
642 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
643 hexdump( ule_hist, TS_SZ );
644 } else {
645 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
646 hexdump( ule_where - TS_SZ, TS_SZ );
647 }
648 ule_dump = 1;
649#endif
650
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800651 dev->stats.rx_errors++;
652 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dev_kfree_skb(priv->ule_skb);
654 } else {
655 /* CRC32 verified OK. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300656 u8 dest_addr[ETH_ALEN];
657 static const u8 bc_addr[ETH_ALEN] =
658 { [ 0 ... ETH_ALEN-1] = 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* CRC32 was OK. Remove it from skb. */
661 priv->ule_skb->tail -= 4;
662 priv->ule_skb->len -= 4;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!priv->ule_dbit) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300665 /*
666 * The destination MAC address is the
667 * next data in the skb. It comes
668 * before any extension headers.
669 *
670 * Check if the payload of this SNDU
671 * should be passed up the stack.
672 */
673 register int drop = 0;
674 if (priv->rx_mode != RX_MODE_PROMISC) {
675 if (priv->ule_skb->data[0] & 0x01) {
676 /* multicast or broadcast */
dingtianhong5e231c22013-12-26 19:41:10 +0800677 if (!ether_addr_equal(priv->ule_skb->data, bc_addr)) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300678 /* multicast */
679 if (priv->rx_mode == RX_MODE_MULTI) {
680 int i;
dingtianhong5e231c22013-12-26 19:41:10 +0800681 for(i = 0; i < priv->multi_num &&
682 !ether_addr_equal(priv->ule_skb->data,
683 priv->multi_macs[i]); i++)
Christian Praehauser18232ca2006-05-22 10:31:47 -0300684 ;
685 if (i == priv->multi_num)
686 drop = 1;
687 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
688 drop = 1; /* no broadcast; */
689 /* else: all multicast mode: accept all multicast packets */
690 }
691 /* else: broadcast */
692 }
dingtianhong5e231c22013-12-26 19:41:10 +0800693 else if (!ether_addr_equal(priv->ule_skb->data, dev->dev_addr))
Christian Praehauser18232ca2006-05-22 10:31:47 -0300694 drop = 1;
695 /* else: destination address matches the MAC address of our receiver device */
696 }
Joe Perchesc84e6032008-02-03 17:18:59 +0200697 /* else: promiscuous mode; pass everything up the stack */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300698
699 if (drop) {
700#ifdef ULE_DEBUG
701 dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
702 MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
703#endif
704 dev_kfree_skb(priv->ule_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto sndu_done;
706 }
Christian Praehauser18232ca2006-05-22 10:31:47 -0300707 else
708 {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300709 skb_copy_from_linear_data(priv->ule_skb,
710 dest_addr,
711 ETH_ALEN);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300712 skb_pull(priv->ule_skb, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
Christian Praehauser18232ca2006-05-22 10:31:47 -0300715
716 /* Handle ULE Extension Headers. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900717 if (priv->ule_sndu_type < ETH_P_802_3_MIN) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300718 /* There is an extension header. Handle it accordingly. */
719 int l = handle_ule_extensions(priv);
720 if (l < 0) {
721 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
722 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
723 dev_kfree_skb(priv->ule_skb);
724 goto sndu_done;
725 }
726 skb_pull(priv->ule_skb, l);
727 }
728
729 /*
730 * Construct/assure correct ethernet header.
731 * Note: in bridged mode (priv->ule_bridged !=
732 * 0) we already have the (original) ethernet
733 * header at the start of the payload (after
734 * optional dest. address and any extension
735 * headers).
736 */
737
738 if (!priv->ule_bridged) {
739 skb_push(priv->ule_skb, ETH_HLEN);
740 ethh = (struct ethhdr *)priv->ule_skb->data;
741 if (!priv->ule_dbit) {
742 /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
743 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
744 memset(ethh->h_source, 0, ETH_ALEN);
745 }
746 else /* zeroize source and dest */
747 memset( ethh, 0, ETH_ALEN*2 );
748
749 ethh->h_proto = htons(priv->ule_sndu_type);
750 }
751 /* else: skb is in correct state; nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 priv->ule_bridged = 0;
753
754 /* Stuff into kernel's protocol stack. */
755 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
756 /* If D-bit is set (i.e. destination MAC address not present),
757 * receive the packet anyhow. */
758 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
759 priv->ule_skb->pkt_type = PACKET_HOST; */
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800760 dev->stats.rx_packets++;
761 dev->stats.rx_bytes += priv->ule_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 netif_rx(priv->ule_skb);
763 }
764 sndu_done:
765 /* Prepare for next SNDU. */
766 reset_ule(priv);
767 }
768
769 /* More data in current TS (look at the bytes following the CRC32)? */
770 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
771 /* Next ULE SNDU starts right there. */
772 new_ts = 0;
773 priv->ule_skb = NULL;
774 priv->ule_sndu_type_1 = 0;
775 priv->ule_sndu_len = 0;
776 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
777 // *(from_where + 0), *(from_where + 1),
778 // *(from_where + 2), *(from_where + 3));
779 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
780 // hexdump(ts, 188);
781 } else {
782 new_ts = 1;
783 ts += TS_SZ;
784 priv->ts_count++;
785 if (priv->ule_skb == NULL) {
786 priv->need_pusi = 1;
787 priv->ule_sndu_type_1 = 0;
788 priv->ule_sndu_len = 0;
789 }
790 }
791 } /* for all available TS cells */
792}
793
794static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
795 const u8 *buffer2, size_t buffer2_len,
796 struct dmx_ts_feed *feed, enum dmx_success success)
797{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700798 struct net_device *dev = feed->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Al Viro5fa12472008-03-29 03:07:38 +0000800 if (buffer2)
801 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (buffer1_len > 32768)
803 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
804 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800805 buffer1_len, buffer1_len / TS_SZ, buffer1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 dvb_net_ule(dev, buffer1, buffer1_len);
807 return 0;
808}
809
810
Michael Krufky71be2582007-08-23 22:33:28 -0300811static void dvb_net_sec(struct net_device *dev,
812 const u8 *pkt, int pkt_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800814 u8 *eth;
815 struct sk_buff *skb;
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800816 struct net_device_stats *stats = &dev->stats;
Johannes Stezenbach59142332005-05-16 21:54:25 -0700817 int snap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* note: pkt_len includes a 32bit checksum */
820 if (pkt_len < 16) {
821 printk("%s: IP/MPE packet length = %d too small.\n",
822 dev->name, pkt_len);
823 stats->rx_errors++;
824 stats->rx_length_errors++;
825 return;
826 }
827/* it seems some ISPs manage to screw up here, so we have to
828 * relax the error checks... */
829#if 0
830 if ((pkt[5] & 0xfd) != 0xc1) {
831 /* drop scrambled or broken packets */
832#else
833 if ((pkt[5] & 0x3c) != 0x00) {
834 /* drop scrambled */
835#endif
836 stats->rx_errors++;
837 stats->rx_crc_errors++;
838 return;
839 }
840 if (pkt[5] & 0x02) {
Johannes Stezenbach59142332005-05-16 21:54:25 -0700841 /* handle LLC/SNAP, see rfc-1042 */
842 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
843 stats->rx_dropped++;
844 return;
845 }
846 snap = 8;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (pkt[7]) {
849 /* FIXME: assemble datagram from multiple sections */
850 stats->rx_errors++;
851 stats->rx_frame_errors++;
852 return;
853 }
854
855 /* we have 14 byte ethernet header (ip header follows);
Johannes Stezenbach59142332005-05-16 21:54:25 -0700856 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
Johannes Stezenbach59142332005-05-16 21:54:25 -0700858 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
860 stats->rx_dropped++;
861 return;
862 }
863 skb_reserve(skb, 2); /* longword align L3 header */
864 skb->dev = dev;
865
866 /* copy L3 payload */
Johannes Stezenbach59142332005-05-16 21:54:25 -0700867 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
868 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /* create ethernet header: */
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800871 eth[0]=pkt[0x0b];
872 eth[1]=pkt[0x0a];
873 eth[2]=pkt[0x09];
874 eth[3]=pkt[0x08];
875 eth[4]=pkt[0x04];
876 eth[5]=pkt[0x03];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800878 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Johannes Stezenbach59142332005-05-16 21:54:25 -0700880 if (snap) {
881 eth[12] = pkt[18];
882 eth[13] = pkt[19];
883 } else {
884 /* protocol numbers are from rfc-1700 or
885 * http://www.iana.org/assignments/ethernet-numbers
886 */
887 if (pkt[12] >> 4 == 6) { /* version field from IP header */
888 eth[12] = 0x86; /* IPv6 */
889 eth[13] = 0xdd;
890 } else {
891 eth[12] = 0x08; /* IPv4 */
892 eth[13] = 0x00;
893 }
894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 skb->protocol = dvb_net_eth_type_trans(skb, dev);
897
898 stats->rx_packets++;
899 stats->rx_bytes+=skb->len;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800900 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
904 const u8 *buffer2, size_t buffer2_len,
905 struct dmx_section_filter *filter,
906 enum dmx_success success)
907{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800908 struct net_device *dev = filter->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /**
911 * we rely on the DVB API definition where exactly one complete
912 * section is delivered in buffer1
913 */
Mauro Carvalho Chehab04b35ab2007-08-20 16:45:35 -0300914 dvb_net_sec (dev, buffer1, buffer1_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
918static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
919{
920 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000921 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
925static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
926static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
927static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
928
929static int dvb_net_filter_sec_set(struct net_device *dev,
930 struct dmx_section_filter **secfilter,
931 u8 *mac, u8 *mac_mask)
932{
Wang Chen0eade1f2008-12-03 21:13:13 -0800933 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int ret;
935
936 *secfilter=NULL;
937 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
938 if (ret<0) {
939 printk("%s: could not get filter\n", dev->name);
940 return ret;
941 }
942
943 (*secfilter)->priv=(void *) dev;
944
945 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
946 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
947 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
948
949 (*secfilter)->filter_value[0]=0x3e;
950 (*secfilter)->filter_value[3]=mac[5];
951 (*secfilter)->filter_value[4]=mac[4];
952 (*secfilter)->filter_value[8]=mac[3];
953 (*secfilter)->filter_value[9]=mac[2];
954 (*secfilter)->filter_value[10]=mac[1];
955 (*secfilter)->filter_value[11]=mac[0];
956
957 (*secfilter)->filter_mask[0] = 0xff;
958 (*secfilter)->filter_mask[3] = mac_mask[5];
959 (*secfilter)->filter_mask[4] = mac_mask[4];
960 (*secfilter)->filter_mask[8] = mac_mask[3];
961 (*secfilter)->filter_mask[9] = mac_mask[2];
962 (*secfilter)->filter_mask[10] = mac_mask[1];
963 (*secfilter)->filter_mask[11]=mac_mask[0];
964
hartleys0e05a612010-01-05 06:45:21 +0000965 dprintk("%s: filter mac=%pM\n", dev->name, mac);
966 dprintk("%s: filter mask=%pM\n", dev->name, mac_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 return 0;
969}
970
971static int dvb_net_feed_start(struct net_device *dev)
972{
Ralph Metzler1130ca42005-12-01 00:51:54 -0800973 int ret = 0, i;
Wang Chen0eade1f2008-12-03 21:13:13 -0800974 struct dvb_net_priv *priv = netdev_priv(dev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800975 struct dmx_demux *demux = priv->demux;
976 unsigned char *mac = (unsigned char *) dev->dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300978 dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200979 mutex_lock(&priv->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300981 printk("%s: BUG %d\n", __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 priv->secfeed=NULL;
984 priv->secfilter=NULL;
985 priv->tsfeed = NULL;
986
987 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300988 dprintk("%s: alloc secfeed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ret=demux->allocate_section_feed(demux, &priv->secfeed,
990 dvb_net_sec_callback);
991 if (ret<0) {
992 printk("%s: could not allocate section feed\n", dev->name);
Ralph Metzler1130ca42005-12-01 00:51:54 -0800993 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995
Andreas Oberritter5d2cd162005-09-09 13:02:24 -0700996 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 if (ret<0) {
999 printk("%s: could not set section feed\n", dev->name);
1000 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1001 priv->secfeed=NULL;
Ralph Metzler1130ca42005-12-01 00:51:54 -08001002 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
1005 if (priv->rx_mode != RX_MODE_PROMISC) {
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001006 dprintk("%s: set secfilter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
1008 }
1009
1010 switch (priv->rx_mode) {
1011 case RX_MODE_MULTI:
1012 for (i = 0; i < priv->multi_num; i++) {
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001013 dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1015 priv->multi_macs[i], mask_normal);
1016 }
1017 break;
1018 case RX_MODE_ALL_MULTI:
1019 priv->multi_num=1;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001020 dprintk("%s: set multi_secfilter[0]\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1022 mac_allmulti, mask_allmulti);
1023 break;
1024 case RX_MODE_PROMISC:
1025 priv->multi_num=0;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001026 dprintk("%s: set secfilter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1028 break;
1029 }
1030
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001031 dprintk("%s: start filtering\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 priv->secfeed->start_filtering(priv->secfeed);
1033 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
Christian Praehauser18232ca2006-05-22 10:31:47 -03001034 struct timespec timeout = { 0, 10000000 }; // 10 msec
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /* we have payloads encapsulated in TS */
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001037 dprintk("%s: alloc tsfeed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1039 if (ret < 0) {
1040 printk("%s: could not allocate ts feed\n", dev->name);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001041 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 /* Set netdevice pointer for ts decaps callback. */
1045 priv->tsfeed->priv = (void *)dev;
Christian Praehauser18232ca2006-05-22 10:31:47 -03001046 ret = priv->tsfeed->set(priv->tsfeed,
1047 priv->pid, /* pid */
1048 TS_PACKET, /* type */
Mauro Carvalho Chehabfde04ab2013-04-04 13:25:30 -03001049 DMX_PES_OTHER, /* pes type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 32768, /* circular buffer size */
Christian Praehauser18232ca2006-05-22 10:31:47 -03001051 timeout /* timeout */
1052 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (ret < 0) {
1055 printk("%s: could not set ts feed\n", dev->name);
1056 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1057 priv->tsfeed = NULL;
Ralph Metzler1130ca42005-12-01 00:51:54 -08001058 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001061 dprintk("%s: start filtering\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 priv->tsfeed->start_filtering(priv->tsfeed);
1063 } else
Ralph Metzler1130ca42005-12-01 00:51:54 -08001064 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Ralph Metzler1130ca42005-12-01 00:51:54 -08001066error:
Ingo Molnar3593cab2006-02-07 06:49:14 -02001067 mutex_unlock(&priv->mutex);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001068 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071static int dvb_net_feed_stop(struct net_device *dev)
1072{
Wang Chen0eade1f2008-12-03 21:13:13 -08001073 struct dvb_net_priv *priv = netdev_priv(dev);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001074 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001076 dprintk("%s\n", __func__);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001077 mutex_lock(&priv->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1079 if (priv->secfeed) {
1080 if (priv->secfeed->is_filtering) {
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001081 dprintk("%s: stop secfeed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 priv->secfeed->stop_filtering(priv->secfeed);
1083 }
1084
1085 if (priv->secfilter) {
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001086 dprintk("%s: release secfilter\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 priv->secfeed->release_filter(priv->secfeed,
1088 priv->secfilter);
1089 priv->secfilter=NULL;
1090 }
1091
1092 for (i=0; i<priv->multi_num; i++) {
1093 if (priv->multi_secfilter[i]) {
1094 dprintk("%s: release multi_filter[%d]\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001095 __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 priv->secfeed->release_filter(priv->secfeed,
1097 priv->multi_secfilter[i]);
1098 priv->multi_secfilter[i] = NULL;
1099 }
1100 }
1101
1102 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1103 priv->secfeed = NULL;
1104 } else
1105 printk("%s: no feed to stop\n", dev->name);
1106 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1107 if (priv->tsfeed) {
1108 if (priv->tsfeed->is_filtering) {
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001109 dprintk("%s: stop tsfeed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 priv->tsfeed->stop_filtering(priv->tsfeed);
1111 }
1112 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1113 priv->tsfeed = NULL;
1114 }
1115 else
1116 printk("%s: no ts feed to stop\n", dev->name);
1117 } else
Ralph Metzler1130ca42005-12-01 00:51:54 -08001118 ret = -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001119 mutex_unlock(&priv->mutex);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123
Jiri Pirko22bedad32010-04-01 21:22:57 +00001124static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Wang Chen0eade1f2008-12-03 21:13:13 -08001126 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1129 return -ENOMEM;
1130
Jiri Pirko22bedad32010-04-01 21:22:57 +00001131 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 priv->multi_num++;
1134 return 0;
1135}
1136
1137
David Howellsc4028952006-11-22 14:57:56 +00001138static void wq_set_multicast_list (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
David Howellsc4028952006-11-22 14:57:56 +00001140 struct dvb_net_priv *priv =
1141 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1142 struct net_device *dev = priv->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 dvb_net_feed_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 priv->rx_mode = RX_MODE_UNI;
David S. Millerb9e40852008-07-15 00:15:08 -07001146 netif_addr_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (dev->flags & IFF_PROMISC) {
1149 dprintk("%s: promiscuous mode\n", dev->name);
1150 priv->rx_mode = RX_MODE_PROMISC;
1151 } else if ((dev->flags & IFF_ALLMULTI)) {
1152 dprintk("%s: allmulti mode\n", dev->name);
1153 priv->rx_mode = RX_MODE_ALL_MULTI;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001154 } else if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001155 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 dprintk("%s: set_mc_list, %d entries\n",
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001158 dev->name, netdev_mc_count(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 priv->rx_mode = RX_MODE_MULTI;
1161 priv->multi_num = 0;
1162
Jiri Pirko22bedad32010-04-01 21:22:57 +00001163 netdev_for_each_mc_addr(ha, dev)
1164 dvb_set_mc_filter(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
David S. Millerb9e40852008-07-15 00:15:08 -07001167 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 dvb_net_feed_start(dev);
1169}
1170
1171
1172static void dvb_net_set_multicast_list (struct net_device *dev)
1173{
Wang Chen0eade1f2008-12-03 21:13:13 -08001174 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 schedule_work(&priv->set_multicast_list_wq);
1176}
1177
1178
David Howellsc4028952006-11-22 14:57:56 +00001179static void wq_restart_net_feed (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
David Howellsc4028952006-11-22 14:57:56 +00001181 struct dvb_net_priv *priv =
1182 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1183 struct net_device *dev = priv->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (netif_running(dev)) {
1186 dvb_net_feed_stop(dev);
1187 dvb_net_feed_start(dev);
1188 }
1189}
1190
1191
1192static int dvb_net_set_mac (struct net_device *dev, void *p)
1193{
Wang Chen0eade1f2008-12-03 21:13:13 -08001194 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 struct sockaddr *addr=p;
1196
1197 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1198
1199 if (netif_running(dev))
1200 schedule_work(&priv->restart_net_feed_wq);
1201
1202 return 0;
1203}
1204
1205
1206static int dvb_net_open(struct net_device *dev)
1207{
Wang Chen0eade1f2008-12-03 21:13:13 -08001208 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 priv->in_use++;
1211 dvb_net_feed_start(dev);
1212 return 0;
1213}
1214
1215
1216static int dvb_net_stop(struct net_device *dev)
1217{
Wang Chen0eade1f2008-12-03 21:13:13 -08001218 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 priv->in_use--;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001221 return dvb_net_feed_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001224static const struct header_ops dvb_header_ops = {
1225 .create = eth_header,
1226 .parse = eth_header_parse,
1227 .rebuild = eth_rebuild_header,
1228};
1229
Stephen Hemmingerfb875332009-01-07 18:02:53 -08001230
1231static const struct net_device_ops dvb_netdev_ops = {
1232 .ndo_open = dvb_net_open,
1233 .ndo_stop = dvb_net_stop,
1234 .ndo_start_xmit = dvb_net_tx,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001235 .ndo_set_rx_mode = dvb_net_set_multicast_list,
Stephen Hemmingerfb875332009-01-07 18:02:53 -08001236 .ndo_set_mac_address = dvb_net_set_mac,
1237 .ndo_change_mtu = eth_change_mtu,
1238 .ndo_validate_addr = eth_validate_addr,
1239};
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241static void dvb_net_setup(struct net_device *dev)
1242{
1243 ether_setup(dev);
1244
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001245 dev->header_ops = &dvb_header_ops;
Stephen Hemmingerfb875332009-01-07 18:02:53 -08001246 dev->netdev_ops = &dvb_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 dev->mtu = 4096;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 dev->flags |= IFF_NOARP;
1250}
1251
1252static int get_if(struct dvb_net *dvbnet)
1253{
1254 int i;
1255
1256 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1257 if (!dvbnet->state[i])
1258 break;
1259
1260 if (i == DVB_NET_DEVICES_MAX)
1261 return -1;
1262
1263 dvbnet->state[i]=1;
1264 return i;
1265}
1266
1267static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1268{
1269 struct net_device *net;
1270 struct dvb_net_priv *priv;
1271 int result;
1272 int if_num;
1273
1274 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1275 return -EINVAL;
1276 if ((if_num = get_if(dvbnet)) < 0)
1277 return -EINVAL;
1278
1279 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", dvb_net_setup);
1280 if (!net)
1281 return -ENOMEM;
1282
1283 if (dvbnet->dvbdev->id)
1284 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1285 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1286 else
1287 /* compatibility fix to keep dvb0_0 format */
1288 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1289 dvbnet->dvbdev->adapter->num, if_num);
1290
1291 net->addr_len = 6;
1292 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1293
1294 dvbnet->device[if_num] = net;
1295
Wang Chen0eade1f2008-12-03 21:13:13 -08001296 priv = netdev_priv(net);
David Howellsc4028952006-11-22 14:57:56 +00001297 priv->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 priv->demux = dvbnet->demux;
1299 priv->pid = pid;
1300 priv->rx_mode = RX_MODE_UNI;
1301 priv->need_pusi = 1;
1302 priv->tscc = 0;
1303 priv->feedtype = feedtype;
1304 reset_ule(priv);
1305
David Howellsc4028952006-11-22 14:57:56 +00001306 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1307 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001308 mutex_init(&priv->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 net->base_addr = pid;
1311
1312 if ((result = register_netdev(net)) < 0) {
1313 dvbnet->device[if_num] = NULL;
1314 free_netdev(net);
1315 return result;
1316 }
1317 printk("dvb_net: created network interface %s\n", net->name);
1318
1319 return if_num;
1320}
1321
Peter Beutner400b7082006-01-09 15:32:43 -02001322static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 struct net_device *net = dvbnet->device[num];
1325 struct dvb_net_priv *priv;
1326
1327 if (!dvbnet->state[num])
1328 return -EINVAL;
Wang Chen0eade1f2008-12-03 21:13:13 -08001329 priv = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (priv->in_use)
1331 return -EBUSY;
1332
1333 dvb_net_stop(net);
Tejun Heo43829732012-08-20 14:51:24 -07001334 flush_work(&priv->set_multicast_list_wq);
1335 flush_work(&priv->restart_net_feed_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 printk("dvb_net: removed network interface %s\n", net->name);
1337 unregister_netdev(net);
1338 dvbnet->state[num]=0;
1339 dvbnet->device[num] = NULL;
1340 free_netdev(net);
1341
1342 return 0;
1343}
1344
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001345static int dvb_net_do_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 unsigned int cmd, void *parg)
1347{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001348 struct dvb_device *dvbdev = file->private_data;
1349 struct dvb_net *dvbnet = dvbdev->priv;
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001350 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1353 return -EPERM;
1354
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001355 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1356 return -ERESTARTSYS;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 switch (cmd) {
1359 case NET_ADD_IF:
1360 {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001361 struct dvb_net_if *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int result;
1363
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001364 if (!capable(CAP_SYS_ADMIN)) {
1365 ret = -EPERM;
1366 goto ioctl_error;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001369 if (!try_module_get(dvbdev->adapter->module)) {
1370 ret = -EPERM;
1371 goto ioctl_error;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1375 if (result<0) {
1376 module_put(dvbdev->adapter->module);
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001377 ret = result;
1378 goto ioctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 dvbnetif->if_num=result;
1381 break;
1382 }
1383 case NET_GET_IF:
1384 {
1385 struct net_device *netdev;
1386 struct dvb_net_priv *priv_data;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001387 struct dvb_net_if *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001390 !dvbnet->state[dvbnetif->if_num]) {
1391 ret = -EINVAL;
1392 goto ioctl_error;
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 netdev = dvbnet->device[dvbnetif->if_num];
1396
Wang Chen0eade1f2008-12-03 21:13:13 -08001397 priv_data = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 dvbnetif->pid=priv_data->pid;
1399 dvbnetif->feedtype=priv_data->feedtype;
1400 break;
1401 }
1402 case NET_REMOVE_IF:
1403 {
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001404 if (!capable(CAP_SYS_ADMIN)) {
1405 ret = -EPERM;
1406 goto ioctl_error;
1407 }
1408 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1409 ret = -EINVAL;
1410 goto ioctl_error;
1411 }
Hans Verkuile18828e2006-01-09 15:25:28 -02001412 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (!ret)
1414 module_put(dvbdev->adapter->module);
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001418 /* binary compatibility cruft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 case __NET_ADD_IF_OLD:
1420 {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001421 struct __dvb_net_if_old *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int result;
1423
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001424 if (!capable(CAP_SYS_ADMIN)) {
1425 ret = -EPERM;
1426 goto ioctl_error;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001429 if (!try_module_get(dvbdev->adapter->module)) {
1430 ret = -EPERM;
1431 goto ioctl_error;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1435 if (result<0) {
1436 module_put(dvbdev->adapter->module);
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001437 ret = result;
1438 goto ioctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 dvbnetif->if_num=result;
1441 break;
1442 }
1443 case __NET_GET_IF_OLD:
1444 {
1445 struct net_device *netdev;
1446 struct dvb_net_priv *priv_data;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001447 struct __dvb_net_if_old *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001450 !dvbnet->state[dvbnetif->if_num]) {
1451 ret = -EINVAL;
1452 goto ioctl_error;
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 netdev = dvbnet->device[dvbnetif->if_num];
1456
Wang Chen0eade1f2008-12-03 21:13:13 -08001457 priv_data = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 dvbnetif->pid=priv_data->pid;
1459 break;
1460 }
1461 default:
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001462 ret = -ENOTTY;
1463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001465
1466ioctl_error:
1467 mutex_unlock(&dvbnet->ioctl_mutex);
1468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001471static long dvb_net_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned int cmd, unsigned long arg)
1473{
Arnd Bergmann72024f12010-09-11 19:56:45 +02001474 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001477static int dvb_net_close(struct inode *inode, struct file *file)
1478{
1479 struct dvb_device *dvbdev = file->private_data;
1480 struct dvb_net *dvbnet = dvbdev->priv;
1481
Trent Piepho82e67242007-08-17 17:49:41 -03001482 dvb_generic_release(inode, file);
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001483
Al Viro7caf2182013-04-15 13:56:11 -04001484 if(dvbdev->users == 1 && dvbnet->exit == 1)
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001485 wake_up(&dvbdev->wait_queue);
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001486 return 0;
1487}
1488
1489
Jan Engelhardt784e29d2009-01-11 06:12:43 -03001490static const struct file_operations dvb_net_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001492 .unlocked_ioctl = dvb_net_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 .open = dvb_generic_open,
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001494 .release = dvb_net_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001495 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496};
1497
1498static struct dvb_device dvbdev_net = {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001499 .priv = NULL,
1500 .users = 1,
1501 .writers = 1,
1502 .fops = &dvb_net_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503};
1504
1505
1506void dvb_net_release (struct dvb_net *dvbnet)
1507{
1508 int i;
1509
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001510 dvbnet->exit = 1;
1511 if (dvbnet->dvbdev->users < 1)
1512 wait_event(dvbnet->dvbdev->wait_queue,
1513 dvbnet->dvbdev->users==1);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dvb_unregister_device(dvbnet->dvbdev);
1516
1517 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1518 if (!dvbnet->state[i])
1519 continue;
1520 dvb_net_remove_if(dvbnet, i);
1521 }
1522}
1523EXPORT_SYMBOL(dvb_net_release);
1524
1525
1526int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1527 struct dmx_demux *dmx)
1528{
1529 int i;
1530
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001531 mutex_init(&dvbnet->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 dvbnet->demux = dmx;
1533
1534 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1535 dvbnet->state[i] = 0;
1536
Jonathan Nieder58fae672011-12-31 07:54:16 -03001537 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 dvbnet, DVB_DEVICE_NET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540EXPORT_SYMBOL(dvb_net_init);