blob: 9914f69a4a02b82bbd3717746d0080aa1454d370 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
72{
73 unsigned int j;
74 for (j = 0; j < cnt; j++)
75 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
76 return c;
77}
78
79
80#define DVB_NET_MULTICAST_MAX 10
81
82#undef ULE_DEBUG
83
84#ifdef ULE_DEBUG
85
Joe Perches4bad5d22015-01-28 15:05:52 -030086static void hexdump(const unsigned char *buf, unsigned short len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Joe Perches4bad5d22015-01-28 15:05:52 -030088 print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91#endif
92
93struct dvb_net_priv {
94 int in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 u16 pid;
David Howellsc4028952006-11-22 14:57:56 +000096 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct dvb_net *host;
98 struct dmx_demux *demux;
99 struct dmx_section_feed *secfeed;
100 struct dmx_section_filter *secfilter;
101 struct dmx_ts_feed *tsfeed;
102 int multi_num;
103 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
104 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
105 int rx_mode;
106#define RX_MODE_UNI 0
107#define RX_MODE_MULTI 1
108#define RX_MODE_ALL_MULTI 2
109#define RX_MODE_PROMISC 3
110 struct work_struct set_multicast_list_wq;
111 struct work_struct restart_net_feed_wq;
112 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
113 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
114 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
115 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
116 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
117 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
118 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
119 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
120 unsigned char ule_dbit; /* Whether the DestMAC address present
121 * or not (bit is set). */
122 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
123 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
124 unsigned long ts_count; /* Current ts cell counter. */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200125 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128
129/**
130 * Determine the packet's protocol ID. The rule here is that we
131 * assume 802.3 if the type field is short enough to be a length.
132 * This is normal practice and works for any 'now in use' protocol.
133 *
134 * stolen from eth.c out of the linux kernel, hacked for dvb-device
135 * by Michael Holzt <kju@debian.org>
136 */
Al Viro12fbcef2008-05-21 00:31:11 -0300137static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct net_device *dev)
139{
140 struct ethhdr *eth;
141 unsigned char *rawp;
142
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700143 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 skb_pull(skb,dev->hard_header_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 eth = eth_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 if (*eth->h_dest & 1) {
dingtianhong5e231c22013-12-26 19:41:10 +0800148 if(ether_addr_equal(eth->h_dest,dev->broadcast))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 skb->pkt_type=PACKET_BROADCAST;
150 else
151 skb->pkt_type=PACKET_MULTICAST;
152 }
153
Simon Hormane5c5d222013-03-28 13:38:25 +0900154 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return eth->h_proto;
156
157 rawp = skb->data;
158
159 /**
160 * This is a magic hack to spot IPX packets. Older Novell breaks
161 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
162 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
163 * won't work for fault tolerant netware but does for the rest.
164 */
165 if (*(unsigned short *)rawp == 0xFFFF)
166 return htons(ETH_P_802_3);
167
168 /**
169 * Real 802.2 LLC
170 */
171 return htons(ETH_P_802_2);
172}
173
174#define TS_SZ 188
175#define TS_SYNC 0x47
176#define TS_TEI 0x80
177#define TS_SC 0xC0
178#define TS_PUSI 0x40
179#define TS_AF_A 0x20
180#define TS_AF_D 0x10
181
182/* ULE Extension Header handlers. */
183
184#define ULE_TEST 0
185#define ULE_BRIDGED 1
186
Christian Praehauser18232ca2006-05-22 10:31:47 -0300187#define ULE_OPTEXTHDR_PADDING 0
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int ule_test_sndu( struct dvb_net_priv *p )
190{
191 return -1;
192}
193
194static int ule_bridged_sndu( struct dvb_net_priv *p )
195{
Christian Praehauser18232ca2006-05-22 10:31:47 -0300196 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
Simon Hormane5c5d222013-03-28 13:38:25 +0900197 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300198 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
Simon Hormane5c5d222013-03-28 13:38:25 +0900199 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300200 if(framelen != ntohs(hdr->h_proto)) {
201 return -1;
202 }
203 }
204 /* Note:
205 * From RFC4326:
206 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
207 * It must be the final (or only) extension header specified in the header chain of a SNDU."
208 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
210 p->ule_bridged = 1;
211 return 0;
212}
213
Christian Praehauser18232ca2006-05-22 10:31:47 -0300214static int ule_exthdr_padding(struct dvb_net_priv *p)
215{
216 return 0;
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/** Handle ULE extension headers.
220 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
221 * Returns: >= 0: nr. of bytes consumed by next extension header
222 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
223 */
224static int handle_one_ule_extension( struct dvb_net_priv *p )
225{
226 /* Table of mandatory extension header handlers. The header type is the index. */
227 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
228 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
229
230 /* Table of optional extension header handlers. The header type is the index. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300231 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
232 { [0] = ule_exthdr_padding, [1] = NULL, };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 int ext_len = 0;
235 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
236 unsigned char htype = p->ule_sndu_type & 0x00FF;
237
238 /* Discriminate mandatory and optional extension headers. */
239 if (hlen == 0) {
240 /* Mandatory extension header */
241 if (ule_mandatory_ext_handlers[htype]) {
242 ext_len = ule_mandatory_ext_handlers[htype]( p );
Christian Praehauser18232ca2006-05-22 10:31:47 -0300243 if(ext_len >= 0) {
244 p->ule_next_hdr += ext_len;
245 if (!p->ule_bridged) {
Al Viro12fbcef2008-05-21 00:31:11 -0300246 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300247 p->ule_next_hdr += 2;
248 } else {
Al Viro12fbcef2008-05-21 00:31:11 -0300249 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
Christian Praehauser18232ca2006-05-22 10:31:47 -0300250 /* This assures the extension handling loop will terminate. */
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Christian Praehauser18232ca2006-05-22 10:31:47 -0300253 // else: extension handler failed or SNDU should be discarded
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } else
255 ext_len = -1; /* SNDU has to be discarded. */
256 } else {
257 /* Optional extension header. Calculate the length. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300258 ext_len = hlen << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Process the optional extension header according to its type. */
260 if (ule_optional_ext_handlers[htype])
261 (void)ule_optional_ext_handlers[htype]( p );
262 p->ule_next_hdr += ext_len;
Al Viro12fbcef2008-05-21 00:31:11 -0300263 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
Christian Praehauser18232ca2006-05-22 10:31:47 -0300264 /*
265 * note: the length of the next header type is included in the
266 * length of THIS optional extension header
267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 return ext_len;
271}
272
273static int handle_ule_extensions( struct dvb_net_priv *p )
274{
275 int total_ext_len = 0, l;
276
277 p->ule_next_hdr = p->ule_skb->data;
278 do {
279 l = handle_one_ule_extension( p );
Christian Praehauser18232ca2006-05-22 10:31:47 -0300280 if (l < 0)
281 return l; /* Stop extension header processing and discard SNDU. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 total_ext_len += l;
Christian Praehauser18232ca2006-05-22 10:31:47 -0300283#ifdef ULE_DEBUG
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300284 pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
285 p->ule_next_hdr, (int)p->ule_sndu_type,
286 l, total_ext_len);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Simon Hormane5c5d222013-03-28 13:38:25 +0900289 } while (p->ule_sndu_type < ETH_P_802_3_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return total_ext_len;
292}
293
294
295/** Prepare for a new ULE SNDU: reset the decoder state. */
296static inline void reset_ule( struct dvb_net_priv *p )
297{
298 p->ule_skb = NULL;
299 p->ule_next_hdr = NULL;
300 p->ule_sndu_len = 0;
301 p->ule_sndu_type = 0;
302 p->ule_sndu_type_1 = 0;
303 p->ule_sndu_remain = 0;
304 p->ule_dbit = 0xFF;
305 p->ule_bridged = 0;
306}
307
308/**
309 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
310 * TS cells of a single PID.
311 */
312static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
313{
Wang Chen0eade1f2008-12-03 21:13:13 -0800314 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned long skipped = 0L;
Trent Piepho372280d2007-07-13 18:46:19 -0300316 const u8 *ts, *ts_end, *from_where = NULL;
317 u8 ts_remain = 0, how_much = 0, new_ts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct ethhdr *ethh = NULL;
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300319 bool error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321#ifdef ULE_DEBUG
322 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
323 static unsigned char ule_hist[100*TS_SZ];
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300324 static unsigned char *ule_where = ule_hist, ule_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#endif
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* For all TS cells in current buffer.
328 * Appearently, we are called for every single TS cell.
329 */
Trent Piepho372280d2007-07-13 18:46:19 -0300330 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (new_ts) {
333 /* We are about to process a new TS cell. */
334
335#ifdef ULE_DEBUG
336 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
337 memcpy( ule_where, ts, TS_SZ );
338 if (ule_dump) {
339 hexdump( ule_where, TS_SZ );
340 ule_dump = 0;
341 }
342 ule_where += TS_SZ;
343#endif
344
345 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
346 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
347 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
Joe Perchesf491dbe2014-10-27 02:25:00 -0300348 priv->ts_count, ts[0],
349 (ts[1] & TS_TEI) >> 7,
Dan Carpenter37314522014-10-29 12:47:41 -0300350 (ts[3] & TS_SC) >> 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
353 if (priv->ule_skb) {
354 dev_kfree_skb( priv->ule_skb );
355 /* Prepare for next SNDU. */
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800356 dev->stats.rx_errors++;
357 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 reset_ule(priv);
360 priv->need_pusi = 1;
361
362 /* Continue with next TS cell. */
363 ts += TS_SZ;
364 priv->ts_count++;
365 continue;
366 }
367
368 ts_remain = 184;
369 from_where = ts + 4;
370 }
371 /* Synchronize on PUSI, if required. */
372 if (priv->need_pusi) {
373 if (ts[1] & TS_PUSI) {
374 /* Find beginning of first ULE SNDU in current TS cell. */
375 /* Synchronize continuity counter. */
376 priv->tscc = ts[3] & 0x0F;
377 /* There is a pointer field here. */
378 if (ts[4] > ts_remain) {
379 printk(KERN_ERR "%lu: Invalid ULE packet "
380 "(pointer field %d)\n", priv->ts_count, ts[4]);
381 ts += TS_SZ;
382 priv->ts_count++;
383 continue;
384 }
385 /* Skip to destination of pointer field. */
386 from_where = &ts[5] + ts[4];
387 ts_remain -= 1 + ts[4];
388 skipped = 0;
389 } else {
390 skipped++;
391 ts += TS_SZ;
392 priv->ts_count++;
393 continue;
394 }
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (new_ts) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300398 /* Check continuity counter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if ((ts[3] & 0x0F) == priv->tscc)
400 priv->tscc = (priv->tscc + 1) & 0x0F;
401 else {
402 /* TS discontinuity handling: */
403 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
Christian Praehauser18232ca2006-05-22 10:31:47 -0300404 "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
406 if (priv->ule_skb) {
407 dev_kfree_skb( priv->ule_skb );
408 /* Prepare for next SNDU. */
409 // reset_ule(priv); moved to below.
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800410 dev->stats.rx_errors++;
411 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 reset_ule(priv);
414 /* skip to next PUSI. */
415 priv->need_pusi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 continue;
417 }
418 /* If we still have an incomplete payload, but PUSI is
419 * set; some TS cells are missing.
420 * This is only possible here, if we missed exactly 16 TS
421 * cells (continuity counter wrap). */
422 if (ts[1] & TS_PUSI) {
423 if (! priv->need_pusi) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300424 if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
426 printk(KERN_WARNING "%lu: Invalid pointer "
427 "field: %u.\n", priv->ts_count, *from_where);
428
429 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
430 if (priv->ule_skb) {
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300431 error = true;
432 dev_kfree_skb(priv->ule_skb);
433 }
434
435 if (error || priv->ule_sndu_remain) {
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800436 dev->stats.rx_errors++;
437 dev->stats.rx_frame_errors++;
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300438 error = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 reset_ule(priv);
442 priv->need_pusi = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 continue;
444 }
445 /* Skip pointer field (we're processing a
446 * packed payload). */
447 from_where += 1;
448 ts_remain -= 1;
449 } else
450 priv->need_pusi = 0;
451
452 if (priv->ule_sndu_remain > 183) {
453 /* Current SNDU lacks more data than there could be available in the
454 * current TS cell. */
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800455 dev->stats.rx_errors++;
456 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
458 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
459 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
460 dev_kfree_skb(priv->ule_skb);
461 /* Prepare for next SNDU. */
462 reset_ule(priv);
463 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
464 from_where += ts[4];
465 ts_remain -= ts[4];
466 }
467 }
468 }
469
470 /* Check if new payload needs to be started. */
471 if (priv->ule_skb == NULL) {
472 /* Start a new payload with skb.
473 * Find ULE header. It is only guaranteed that the
474 * length field (2 bytes) is contained in the current
475 * TS.
476 * Check ts_remain has to be >= 2 here. */
477 if (ts_remain < 2) {
478 printk(KERN_WARNING "Invalid payload packing: only %d "
479 "bytes left in TS. Resyncing.\n", ts_remain);
480 priv->ule_sndu_len = 0;
481 priv->need_pusi = 1;
Ang Way Chuang29e1fa32010-02-25 09:45:03 +0800482 ts += TS_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 continue;
484 }
485
486 if (! priv->ule_sndu_len) {
487 /* Got at least two bytes, thus extrace the SNDU length. */
488 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
489 if (priv->ule_sndu_len & 0x8000) {
490 /* D-Bit is set: no dest mac present. */
491 priv->ule_sndu_len &= 0x7FFF;
492 priv->ule_dbit = 1;
493 } else
494 priv->ule_dbit = 0;
495
Christian Praehauser18232ca2006-05-22 10:31:47 -0300496 if (priv->ule_sndu_len < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
498 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800499 dev->stats.rx_errors++;
500 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 priv->ule_sndu_len = 0;
502 priv->need_pusi = 1;
503 new_ts = 1;
504 ts += TS_SZ;
505 priv->ts_count++;
506 continue;
507 }
508 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
509 from_where += 2;
510 }
511
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300512 priv->ule_sndu_remain = priv->ule_sndu_len + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
514 * State of current TS:
515 * ts_remain (remaining bytes in the current TS cell)
516 * 0 ule_type is not available now, we need the next TS cell
517 * 1 the first byte of the ule_type is present
518 * >=2 full ULE header present, maybe some payload data as well.
519 */
520 switch (ts_remain) {
521 case 1:
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300522 priv->ule_sndu_remain--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 priv->ule_sndu_type = from_where[0] << 8;
524 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
525 ts_remain -= 1; from_where += 1;
526 /* Continue w/ next TS. */
527 case 0:
528 new_ts = 1;
529 ts += TS_SZ;
530 priv->ts_count++;
531 continue;
532
533 default: /* complete ULE header is present in current TS. */
534 /* Extract ULE type field. */
535 if (priv->ule_sndu_type_1) {
Ang Way Chuang5c331fc2010-05-27 02:02:09 -0300536 priv->ule_sndu_type_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 priv->ule_sndu_type |= from_where[0];
538 from_where += 1; /* points to payload start. */
539 ts_remain -= 1;
540 } else {
541 /* Complete type is present in new TS. */
542 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
543 from_where += 2; /* points to payload start. */
544 ts_remain -= 2;
545 }
546 break;
547 }
548
549 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
550 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
551 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
552 if (priv->ule_skb == NULL) {
553 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
554 dev->name);
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800555 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return;
557 }
558
559 /* This includes the CRC32 _and_ dest mac, if !dbit. */
560 priv->ule_sndu_remain = priv->ule_sndu_len;
561 priv->ule_skb->dev = dev;
562 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
563 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
564 }
565
566 /* Copy data into our current skb. */
567 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
568 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
569 priv->ule_sndu_remain -= how_much;
570 ts_remain -= how_much;
571 from_where += how_much;
572
573 /* Check for complete payload. */
574 if (priv->ule_sndu_remain <= 0) {
575 /* Check CRC32, we've got it in our skb already. */
Al Viro12fbcef2008-05-21 00:31:11 -0300576 __be16 ulen = htons(priv->ule_sndu_len);
577 __be16 utype = htons(priv->ule_sndu_type);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700578 const u8 *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct kvec iov[3] = {
580 { &ulen, sizeof ulen },
581 { &utype, sizeof utype },
582 { priv->ule_skb->data, priv->ule_skb->len - 4 }
583 };
Ang Way Chuangdedcefb2006-12-14 13:51:44 -0300584 u32 ule_crc = ~0L, expected_crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (priv->ule_dbit) {
586 /* Set D-bit for CRC32 verification,
587 * if it was set originally. */
Al Virof51b10e2008-06-22 14:20:49 -0300588 ulen |= htons(0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 ule_crc = iov_crc32(ule_crc, iov, 3);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700592 tail = skb_tail_pointer(priv->ule_skb);
593 expected_crc = *(tail - 4) << 24 |
594 *(tail - 3) << 16 |
595 *(tail - 2) << 8 |
596 *(tail - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (ule_crc != expected_crc) {
Michael Krufky5ef35be2006-12-14 13:53:33 -0300598 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 -0700599 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);
600
601#ifdef ULE_DEBUG
602 hexdump( iov[0].iov_base, iov[0].iov_len );
603 hexdump( iov[1].iov_base, iov[1].iov_len );
604 hexdump( iov[2].iov_base, iov[2].iov_len );
605
606 if (ule_where == ule_hist) {
607 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
608 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
609 } else if (ule_where == &ule_hist[TS_SZ]) {
610 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
611 hexdump( ule_hist, TS_SZ );
612 } else {
613 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
614 hexdump( ule_where - TS_SZ, TS_SZ );
615 }
616 ule_dump = 1;
617#endif
618
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800619 dev->stats.rx_errors++;
620 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 dev_kfree_skb(priv->ule_skb);
622 } else {
623 /* CRC32 verified OK. */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300624 u8 dest_addr[ETH_ALEN];
625 static const u8 bc_addr[ETH_ALEN] =
626 { [ 0 ... ETH_ALEN-1] = 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* CRC32 was OK. Remove it from skb. */
629 priv->ule_skb->tail -= 4;
630 priv->ule_skb->len -= 4;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (!priv->ule_dbit) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300633 /*
634 * The destination MAC address is the
635 * next data in the skb. It comes
636 * before any extension headers.
637 *
638 * Check if the payload of this SNDU
639 * should be passed up the stack.
640 */
641 register int drop = 0;
642 if (priv->rx_mode != RX_MODE_PROMISC) {
643 if (priv->ule_skb->data[0] & 0x01) {
644 /* multicast or broadcast */
dingtianhong5e231c22013-12-26 19:41:10 +0800645 if (!ether_addr_equal(priv->ule_skb->data, bc_addr)) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300646 /* multicast */
647 if (priv->rx_mode == RX_MODE_MULTI) {
648 int i;
dingtianhong5e231c22013-12-26 19:41:10 +0800649 for(i = 0; i < priv->multi_num &&
650 !ether_addr_equal(priv->ule_skb->data,
651 priv->multi_macs[i]); i++)
Christian Praehauser18232ca2006-05-22 10:31:47 -0300652 ;
653 if (i == priv->multi_num)
654 drop = 1;
655 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
656 drop = 1; /* no broadcast; */
657 /* else: all multicast mode: accept all multicast packets */
658 }
659 /* else: broadcast */
660 }
dingtianhong5e231c22013-12-26 19:41:10 +0800661 else if (!ether_addr_equal(priv->ule_skb->data, dev->dev_addr))
Christian Praehauser18232ca2006-05-22 10:31:47 -0300662 drop = 1;
663 /* else: destination address matches the MAC address of our receiver device */
664 }
Joe Perchesc84e6032008-02-03 17:18:59 +0200665 /* else: promiscuous mode; pass everything up the stack */
Christian Praehauser18232ca2006-05-22 10:31:47 -0300666
667 if (drop) {
668#ifdef ULE_DEBUG
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300669 netdev_dbg(dev, "Dropping SNDU: MAC destination address does not match: dest addr: %pM, dev addr: %pM\n",
670 priv->ule_skb->data, dev->dev_addr);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300671#endif
672 dev_kfree_skb(priv->ule_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto sndu_done;
674 }
Christian Praehauser18232ca2006-05-22 10:31:47 -0300675 else
676 {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300677 skb_copy_from_linear_data(priv->ule_skb,
678 dest_addr,
679 ETH_ALEN);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300680 skb_pull(priv->ule_skb, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
Christian Praehauser18232ca2006-05-22 10:31:47 -0300683
684 /* Handle ULE Extension Headers. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900685 if (priv->ule_sndu_type < ETH_P_802_3_MIN) {
Christian Praehauser18232ca2006-05-22 10:31:47 -0300686 /* There is an extension header. Handle it accordingly. */
687 int l = handle_ule_extensions(priv);
688 if (l < 0) {
689 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
690 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
691 dev_kfree_skb(priv->ule_skb);
692 goto sndu_done;
693 }
694 skb_pull(priv->ule_skb, l);
695 }
696
697 /*
698 * Construct/assure correct ethernet header.
699 * Note: in bridged mode (priv->ule_bridged !=
700 * 0) we already have the (original) ethernet
701 * header at the start of the payload (after
702 * optional dest. address and any extension
703 * headers).
704 */
705
706 if (!priv->ule_bridged) {
707 skb_push(priv->ule_skb, ETH_HLEN);
708 ethh = (struct ethhdr *)priv->ule_skb->data;
709 if (!priv->ule_dbit) {
710 /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
711 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
Vaishali Thakkar4dc102b2015-06-19 23:58:17 -0300712 eth_zero_addr(ethh->h_source);
Christian Praehauser18232ca2006-05-22 10:31:47 -0300713 }
714 else /* zeroize source and dest */
715 memset( ethh, 0, ETH_ALEN*2 );
716
717 ethh->h_proto = htons(priv->ule_sndu_type);
718 }
719 /* else: skb is in correct state; nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 priv->ule_bridged = 0;
721
722 /* Stuff into kernel's protocol stack. */
723 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
724 /* If D-bit is set (i.e. destination MAC address not present),
725 * receive the packet anyhow. */
726 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
727 priv->ule_skb->pkt_type = PACKET_HOST; */
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800728 dev->stats.rx_packets++;
729 dev->stats.rx_bytes += priv->ule_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 netif_rx(priv->ule_skb);
731 }
732 sndu_done:
733 /* Prepare for next SNDU. */
734 reset_ule(priv);
735 }
736
737 /* More data in current TS (look at the bytes following the CRC32)? */
738 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
739 /* Next ULE SNDU starts right there. */
740 new_ts = 0;
741 priv->ule_skb = NULL;
742 priv->ule_sndu_type_1 = 0;
743 priv->ule_sndu_len = 0;
744 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
745 // *(from_where + 0), *(from_where + 1),
746 // *(from_where + 2), *(from_where + 3));
747 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
748 // hexdump(ts, 188);
749 } else {
750 new_ts = 1;
751 ts += TS_SZ;
752 priv->ts_count++;
753 if (priv->ule_skb == NULL) {
754 priv->need_pusi = 1;
755 priv->ule_sndu_type_1 = 0;
756 priv->ule_sndu_len = 0;
757 }
758 }
759 } /* for all available TS cells */
760}
761
762static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
763 const u8 *buffer2, size_t buffer2_len,
Mauro Carvalho Chehab2f684b22015-10-06 19:53:02 -0300764 struct dmx_ts_feed *feed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700766 struct net_device *dev = feed->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Al Viro5fa12472008-03-29 03:07:38 +0000768 if (buffer2)
769 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (buffer1_len > 32768)
771 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
772 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800773 buffer1_len, buffer1_len / TS_SZ, buffer1); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 dvb_net_ule(dev, buffer1, buffer1_len);
775 return 0;
776}
777
778
Michael Krufky71be2582007-08-23 22:33:28 -0300779static void dvb_net_sec(struct net_device *dev,
780 const u8 *pkt, int pkt_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800782 u8 *eth;
783 struct sk_buff *skb;
Stephen Hemmingerfb875332009-01-07 18:02:53 -0800784 struct net_device_stats *stats = &dev->stats;
Johannes Stezenbach59142332005-05-16 21:54:25 -0700785 int snap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* note: pkt_len includes a 32bit checksum */
788 if (pkt_len < 16) {
789 printk("%s: IP/MPE packet length = %d too small.\n",
790 dev->name, pkt_len);
791 stats->rx_errors++;
792 stats->rx_length_errors++;
793 return;
794 }
795/* it seems some ISPs manage to screw up here, so we have to
796 * relax the error checks... */
797#if 0
798 if ((pkt[5] & 0xfd) != 0xc1) {
799 /* drop scrambled or broken packets */
800#else
801 if ((pkt[5] & 0x3c) != 0x00) {
802 /* drop scrambled */
803#endif
804 stats->rx_errors++;
805 stats->rx_crc_errors++;
806 return;
807 }
808 if (pkt[5] & 0x02) {
Johannes Stezenbach59142332005-05-16 21:54:25 -0700809 /* handle LLC/SNAP, see rfc-1042 */
810 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
811 stats->rx_dropped++;
812 return;
813 }
814 snap = 8;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (pkt[7]) {
817 /* FIXME: assemble datagram from multiple sections */
818 stats->rx_errors++;
819 stats->rx_frame_errors++;
820 return;
821 }
822
823 /* we have 14 byte ethernet header (ip header follows);
Johannes Stezenbach59142332005-05-16 21:54:25 -0700824 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Johannes Stezenbach59142332005-05-16 21:54:25 -0700826 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
828 stats->rx_dropped++;
829 return;
830 }
831 skb_reserve(skb, 2); /* longword align L3 header */
832 skb->dev = dev;
833
834 /* copy L3 payload */
Johannes Stezenbach59142332005-05-16 21:54:25 -0700835 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
836 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /* create ethernet header: */
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800839 eth[0]=pkt[0x0b];
840 eth[1]=pkt[0x0a];
841 eth[2]=pkt[0x09];
842 eth[3]=pkt[0x08];
843 eth[4]=pkt[0x04];
844 eth[5]=pkt[0x03];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800846 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Johannes Stezenbach59142332005-05-16 21:54:25 -0700848 if (snap) {
849 eth[12] = pkt[18];
850 eth[13] = pkt[19];
851 } else {
852 /* protocol numbers are from rfc-1700 or
853 * http://www.iana.org/assignments/ethernet-numbers
854 */
855 if (pkt[12] >> 4 == 6) { /* version field from IP header */
856 eth[12] = 0x86; /* IPv6 */
857 eth[13] = 0xdd;
858 } else {
859 eth[12] = 0x08; /* IPv4 */
860 eth[13] = 0x00;
861 }
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 skb->protocol = dvb_net_eth_type_trans(skb, dev);
865
866 stats->rx_packets++;
867 stats->rx_bytes+=skb->len;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800868 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
872 const u8 *buffer2, size_t buffer2_len,
Mauro Carvalho Chehab2f684b22015-10-06 19:53:02 -0300873 struct dmx_section_filter *filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800875 struct net_device *dev = filter->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /**
878 * we rely on the DVB API definition where exactly one complete
879 * section is delivered in buffer1
880 */
Mauro Carvalho Chehab04b35ab2007-08-20 16:45:35 -0300881 dvb_net_sec (dev, buffer1, buffer1_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883}
884
885static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
886{
887 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000888 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
892static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
893static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
894static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
895
896static int dvb_net_filter_sec_set(struct net_device *dev,
897 struct dmx_section_filter **secfilter,
898 u8 *mac, u8 *mac_mask)
899{
Wang Chen0eade1f2008-12-03 21:13:13 -0800900 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 int ret;
902
903 *secfilter=NULL;
904 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
905 if (ret<0) {
906 printk("%s: could not get filter\n", dev->name);
907 return ret;
908 }
909
910 (*secfilter)->priv=(void *) dev;
911
912 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
913 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
914 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
915
916 (*secfilter)->filter_value[0]=0x3e;
917 (*secfilter)->filter_value[3]=mac[5];
918 (*secfilter)->filter_value[4]=mac[4];
919 (*secfilter)->filter_value[8]=mac[3];
920 (*secfilter)->filter_value[9]=mac[2];
921 (*secfilter)->filter_value[10]=mac[1];
922 (*secfilter)->filter_value[11]=mac[0];
923
924 (*secfilter)->filter_mask[0] = 0xff;
925 (*secfilter)->filter_mask[3] = mac_mask[5];
926 (*secfilter)->filter_mask[4] = mac_mask[4];
927 (*secfilter)->filter_mask[8] = mac_mask[3];
928 (*secfilter)->filter_mask[9] = mac_mask[2];
929 (*secfilter)->filter_mask[10] = mac_mask[1];
930 (*secfilter)->filter_mask[11]=mac_mask[0];
931
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300932 netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 return 0;
935}
936
937static int dvb_net_feed_start(struct net_device *dev)
938{
Ralph Metzler1130ca42005-12-01 00:51:54 -0800939 int ret = 0, i;
Wang Chen0eade1f2008-12-03 21:13:13 -0800940 struct dvb_net_priv *priv = netdev_priv(dev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800941 struct dmx_demux *demux = priv->demux;
942 unsigned char *mac = (unsigned char *) dev->dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300944 netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200945 mutex_lock(&priv->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300947 printk("%s: BUG %d\n", __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 priv->secfeed=NULL;
950 priv->secfilter=NULL;
951 priv->tsfeed = NULL;
952
953 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300954 netdev_dbg(dev, "alloc secfeed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ret=demux->allocate_section_feed(demux, &priv->secfeed,
956 dvb_net_sec_callback);
957 if (ret<0) {
958 printk("%s: could not allocate section feed\n", dev->name);
Ralph Metzler1130ca42005-12-01 00:51:54 -0800959 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Andreas Oberritter5d2cd162005-09-09 13:02:24 -0700962 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (ret<0) {
965 printk("%s: could not set section feed\n", dev->name);
966 priv->demux->release_section_feed(priv->demux, priv->secfeed);
967 priv->secfeed=NULL;
Ralph Metzler1130ca42005-12-01 00:51:54 -0800968 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
971 if (priv->rx_mode != RX_MODE_PROMISC) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300972 netdev_dbg(dev, "set secfilter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
974 }
975
976 switch (priv->rx_mode) {
977 case RX_MODE_MULTI:
978 for (i = 0; i < priv->multi_num; i++) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300979 netdev_dbg(dev, "set multi_secfilter[%d]\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
981 priv->multi_macs[i], mask_normal);
982 }
983 break;
984 case RX_MODE_ALL_MULTI:
985 priv->multi_num=1;
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300986 netdev_dbg(dev, "set multi_secfilter[0]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
988 mac_allmulti, mask_allmulti);
989 break;
990 case RX_MODE_PROMISC:
991 priv->multi_num=0;
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300992 netdev_dbg(dev, "set secfilter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
994 break;
995 }
996
Joe Perches5c7c0ca2015-01-28 15:05:51 -0300997 netdev_dbg(dev, "start filtering\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 priv->secfeed->start_filtering(priv->secfeed);
999 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
Arnd Bergmanne95be152016-06-17 17:46:28 -03001000 ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /* we have payloads encapsulated in TS */
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001003 netdev_dbg(dev, "alloc tsfeed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1005 if (ret < 0) {
1006 printk("%s: could not allocate ts feed\n", dev->name);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001007 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
1010 /* Set netdevice pointer for ts decaps callback. */
1011 priv->tsfeed->priv = (void *)dev;
Christian Praehauser18232ca2006-05-22 10:31:47 -03001012 ret = priv->tsfeed->set(priv->tsfeed,
1013 priv->pid, /* pid */
1014 TS_PACKET, /* type */
Mauro Carvalho Chehabfde04ab2013-04-04 13:25:30 -03001015 DMX_PES_OTHER, /* pes type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 32768, /* circular buffer size */
Christian Praehauser18232ca2006-05-22 10:31:47 -03001017 timeout /* timeout */
1018 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 if (ret < 0) {
1021 printk("%s: could not set ts feed\n", dev->name);
1022 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1023 priv->tsfeed = NULL;
Ralph Metzler1130ca42005-12-01 00:51:54 -08001024 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001027 netdev_dbg(dev, "start filtering\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 priv->tsfeed->start_filtering(priv->tsfeed);
1029 } else
Ralph Metzler1130ca42005-12-01 00:51:54 -08001030 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Ralph Metzler1130ca42005-12-01 00:51:54 -08001032error:
Ingo Molnar3593cab2006-02-07 06:49:14 -02001033 mutex_unlock(&priv->mutex);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037static int dvb_net_feed_stop(struct net_device *dev)
1038{
Wang Chen0eade1f2008-12-03 21:13:13 -08001039 struct dvb_net_priv *priv = netdev_priv(dev);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001040 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ingo Molnar3593cab2006-02-07 06:49:14 -02001042 mutex_lock(&priv->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1044 if (priv->secfeed) {
1045 if (priv->secfeed->is_filtering) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001046 netdev_dbg(dev, "stop secfeed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 priv->secfeed->stop_filtering(priv->secfeed);
1048 }
1049
1050 if (priv->secfilter) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001051 netdev_dbg(dev, "release secfilter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 priv->secfeed->release_filter(priv->secfeed,
1053 priv->secfilter);
1054 priv->secfilter=NULL;
1055 }
1056
1057 for (i=0; i<priv->multi_num; i++) {
1058 if (priv->multi_secfilter[i]) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001059 netdev_dbg(dev, "release multi_filter[%d]\n",
1060 i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 priv->secfeed->release_filter(priv->secfeed,
1062 priv->multi_secfilter[i]);
1063 priv->multi_secfilter[i] = NULL;
1064 }
1065 }
1066
1067 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1068 priv->secfeed = NULL;
1069 } else
1070 printk("%s: no feed to stop\n", dev->name);
1071 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1072 if (priv->tsfeed) {
1073 if (priv->tsfeed->is_filtering) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001074 netdev_dbg(dev, "stop tsfeed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 priv->tsfeed->stop_filtering(priv->tsfeed);
1076 }
1077 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1078 priv->tsfeed = NULL;
1079 }
1080 else
1081 printk("%s: no ts feed to stop\n", dev->name);
1082 } else
Ralph Metzler1130ca42005-12-01 00:51:54 -08001083 ret = -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001084 mutex_unlock(&priv->mutex);
Ralph Metzler1130ca42005-12-01 00:51:54 -08001085 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088
Jiri Pirko22bedad32010-04-01 21:22:57 +00001089static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Wang Chen0eade1f2008-12-03 21:13:13 -08001091 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1094 return -ENOMEM;
1095
Jiri Pirko22bedad32010-04-01 21:22:57 +00001096 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 priv->multi_num++;
1099 return 0;
1100}
1101
1102
David Howellsc4028952006-11-22 14:57:56 +00001103static void wq_set_multicast_list (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
David Howellsc4028952006-11-22 14:57:56 +00001105 struct dvb_net_priv *priv =
1106 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1107 struct net_device *dev = priv->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 dvb_net_feed_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 priv->rx_mode = RX_MODE_UNI;
David S. Millerb9e40852008-07-15 00:15:08 -07001111 netif_addr_lock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if (dev->flags & IFF_PROMISC) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001114 netdev_dbg(dev, "promiscuous mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 priv->rx_mode = RX_MODE_PROMISC;
1116 } else if ((dev->flags & IFF_ALLMULTI)) {
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001117 netdev_dbg(dev, "allmulti mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 priv->rx_mode = RX_MODE_ALL_MULTI;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001119 } else if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001120 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Joe Perches5c7c0ca2015-01-28 15:05:51 -03001122 netdev_dbg(dev, "set_mc_list, %d entries\n",
1123 netdev_mc_count(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 priv->rx_mode = RX_MODE_MULTI;
1126 priv->multi_num = 0;
1127
Jiri Pirko22bedad32010-04-01 21:22:57 +00001128 netdev_for_each_mc_addr(ha, dev)
1129 dvb_set_mc_filter(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
David S. Millerb9e40852008-07-15 00:15:08 -07001132 netif_addr_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 dvb_net_feed_start(dev);
1134}
1135
1136
1137static void dvb_net_set_multicast_list (struct net_device *dev)
1138{
Wang Chen0eade1f2008-12-03 21:13:13 -08001139 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 schedule_work(&priv->set_multicast_list_wq);
1141}
1142
1143
David Howellsc4028952006-11-22 14:57:56 +00001144static void wq_restart_net_feed (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
David Howellsc4028952006-11-22 14:57:56 +00001146 struct dvb_net_priv *priv =
1147 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1148 struct net_device *dev = priv->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (netif_running(dev)) {
1151 dvb_net_feed_stop(dev);
1152 dvb_net_feed_start(dev);
1153 }
1154}
1155
1156
1157static int dvb_net_set_mac (struct net_device *dev, void *p)
1158{
Wang Chen0eade1f2008-12-03 21:13:13 -08001159 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct sockaddr *addr=p;
1161
1162 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1163
1164 if (netif_running(dev))
1165 schedule_work(&priv->restart_net_feed_wq);
1166
1167 return 0;
1168}
1169
1170
1171static int dvb_net_open(struct net_device *dev)
1172{
Wang Chen0eade1f2008-12-03 21:13:13 -08001173 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 priv->in_use++;
1176 dvb_net_feed_start(dev);
1177 return 0;
1178}
1179
1180
1181static int dvb_net_stop(struct net_device *dev)
1182{
Wang Chen0eade1f2008-12-03 21:13:13 -08001183 struct dvb_net_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 priv->in_use--;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001186 return dvb_net_feed_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001189static const struct header_ops dvb_header_ops = {
1190 .create = eth_header,
1191 .parse = eth_header_parse,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001192};
1193
Stephen Hemmingerfb875332009-01-07 18:02:53 -08001194
1195static const struct net_device_ops dvb_netdev_ops = {
1196 .ndo_open = dvb_net_open,
1197 .ndo_stop = dvb_net_stop,
1198 .ndo_start_xmit = dvb_net_tx,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001199 .ndo_set_rx_mode = dvb_net_set_multicast_list,
Stephen Hemmingerfb875332009-01-07 18:02:53 -08001200 .ndo_set_mac_address = dvb_net_set_mac,
1201 .ndo_change_mtu = eth_change_mtu,
1202 .ndo_validate_addr = eth_validate_addr,
1203};
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static void dvb_net_setup(struct net_device *dev)
1206{
1207 ether_setup(dev);
1208
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001209 dev->header_ops = &dvb_header_ops;
Stephen Hemmingerfb875332009-01-07 18:02:53 -08001210 dev->netdev_ops = &dvb_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 dev->mtu = 4096;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 dev->flags |= IFF_NOARP;
1214}
1215
1216static int get_if(struct dvb_net *dvbnet)
1217{
1218 int i;
1219
1220 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1221 if (!dvbnet->state[i])
1222 break;
1223
1224 if (i == DVB_NET_DEVICES_MAX)
1225 return -1;
1226
1227 dvbnet->state[i]=1;
1228 return i;
1229}
1230
1231static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1232{
1233 struct net_device *net;
1234 struct dvb_net_priv *priv;
1235 int result;
1236 int if_num;
1237
1238 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1239 return -EINVAL;
1240 if ((if_num = get_if(dvbnet)) < 0)
1241 return -EINVAL;
1242
Tom Gundersenc835a672014-07-14 16:37:24 +02001243 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1244 NET_NAME_UNKNOWN, dvb_net_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!net)
1246 return -ENOMEM;
1247
1248 if (dvbnet->dvbdev->id)
1249 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1250 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1251 else
1252 /* compatibility fix to keep dvb0_0 format */
1253 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1254 dvbnet->dvbdev->adapter->num, if_num);
1255
1256 net->addr_len = 6;
1257 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1258
1259 dvbnet->device[if_num] = net;
1260
Wang Chen0eade1f2008-12-03 21:13:13 -08001261 priv = netdev_priv(net);
David Howellsc4028952006-11-22 14:57:56 +00001262 priv->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 priv->demux = dvbnet->demux;
1264 priv->pid = pid;
1265 priv->rx_mode = RX_MODE_UNI;
1266 priv->need_pusi = 1;
1267 priv->tscc = 0;
1268 priv->feedtype = feedtype;
1269 reset_ule(priv);
1270
David Howellsc4028952006-11-22 14:57:56 +00001271 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1272 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001273 mutex_init(&priv->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 net->base_addr = pid;
1276
1277 if ((result = register_netdev(net)) < 0) {
1278 dvbnet->device[if_num] = NULL;
1279 free_netdev(net);
1280 return result;
1281 }
1282 printk("dvb_net: created network interface %s\n", net->name);
1283
1284 return if_num;
1285}
1286
Peter Beutner400b7082006-01-09 15:32:43 -02001287static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 struct net_device *net = dvbnet->device[num];
1290 struct dvb_net_priv *priv;
1291
1292 if (!dvbnet->state[num])
1293 return -EINVAL;
Wang Chen0eade1f2008-12-03 21:13:13 -08001294 priv = netdev_priv(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (priv->in_use)
1296 return -EBUSY;
1297
1298 dvb_net_stop(net);
Tejun Heo43829732012-08-20 14:51:24 -07001299 flush_work(&priv->set_multicast_list_wq);
1300 flush_work(&priv->restart_net_feed_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 printk("dvb_net: removed network interface %s\n", net->name);
1302 unregister_netdev(net);
1303 dvbnet->state[num]=0;
1304 dvbnet->device[num] = NULL;
1305 free_netdev(net);
1306
1307 return 0;
1308}
1309
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001310static int dvb_net_do_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 unsigned int cmd, void *parg)
1312{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001313 struct dvb_device *dvbdev = file->private_data;
1314 struct dvb_net *dvbnet = dvbdev->priv;
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001315 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1318 return -EPERM;
1319
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001320 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1321 return -ERESTARTSYS;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 switch (cmd) {
1324 case NET_ADD_IF:
1325 {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001326 struct dvb_net_if *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int result;
1328
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001329 if (!capable(CAP_SYS_ADMIN)) {
1330 ret = -EPERM;
1331 goto ioctl_error;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001334 if (!try_module_get(dvbdev->adapter->module)) {
1335 ret = -EPERM;
1336 goto ioctl_error;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1340 if (result<0) {
1341 module_put(dvbdev->adapter->module);
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001342 ret = result;
1343 goto ioctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 dvbnetif->if_num=result;
1346 break;
1347 }
1348 case NET_GET_IF:
1349 {
1350 struct net_device *netdev;
1351 struct dvb_net_priv *priv_data;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001352 struct dvb_net_if *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001355 !dvbnet->state[dvbnetif->if_num]) {
1356 ret = -EINVAL;
1357 goto ioctl_error;
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 netdev = dvbnet->device[dvbnetif->if_num];
1361
Wang Chen0eade1f2008-12-03 21:13:13 -08001362 priv_data = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dvbnetif->pid=priv_data->pid;
1364 dvbnetif->feedtype=priv_data->feedtype;
1365 break;
1366 }
1367 case NET_REMOVE_IF:
1368 {
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001369 if (!capable(CAP_SYS_ADMIN)) {
1370 ret = -EPERM;
1371 goto ioctl_error;
1372 }
1373 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1374 ret = -EINVAL;
1375 goto ioctl_error;
1376 }
Hans Verkuile18828e2006-01-09 15:25:28 -02001377 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!ret)
1379 module_put(dvbdev->adapter->module);
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Dirk Hohndel06fe9fb2009-09-28 21:43:57 -04001383 /* binary compatibility cruft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 case __NET_ADD_IF_OLD:
1385 {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001386 struct __dvb_net_if_old *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int result;
1388
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001389 if (!capable(CAP_SYS_ADMIN)) {
1390 ret = -EPERM;
1391 goto ioctl_error;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001394 if (!try_module_get(dvbdev->adapter->module)) {
1395 ret = -EPERM;
1396 goto ioctl_error;
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1400 if (result<0) {
1401 module_put(dvbdev->adapter->module);
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001402 ret = result;
1403 goto ioctl_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 dvbnetif->if_num=result;
1406 break;
1407 }
1408 case __NET_GET_IF_OLD:
1409 {
1410 struct net_device *netdev;
1411 struct dvb_net_priv *priv_data;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001412 struct __dvb_net_if_old *dvbnetif = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001415 !dvbnet->state[dvbnetif->if_num]) {
1416 ret = -EINVAL;
1417 goto ioctl_error;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 netdev = dvbnet->device[dvbnetif->if_num];
1421
Wang Chen0eade1f2008-12-03 21:13:13 -08001422 priv_data = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 dvbnetif->pid=priv_data->pid;
1424 break;
1425 }
1426 default:
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001427 ret = -ENOTTY;
1428 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001430
1431ioctl_error:
1432 mutex_unlock(&dvbnet->ioctl_mutex);
1433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001436static long dvb_net_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 unsigned int cmd, unsigned long arg)
1438{
Arnd Bergmann72024f12010-09-11 19:56:45 +02001439 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001442static int dvb_net_close(struct inode *inode, struct file *file)
1443{
1444 struct dvb_device *dvbdev = file->private_data;
1445 struct dvb_net *dvbnet = dvbdev->priv;
1446
Trent Piepho82e67242007-08-17 17:49:41 -03001447 dvb_generic_release(inode, file);
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001448
Al Viro7caf2182013-04-15 13:56:11 -04001449 if(dvbdev->users == 1 && dvbnet->exit == 1)
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001450 wake_up(&dvbdev->wait_queue);
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001451 return 0;
1452}
1453
1454
Jan Engelhardt784e29d2009-01-11 06:12:43 -03001455static const struct file_operations dvb_net_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001457 .unlocked_ioctl = dvb_net_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 .open = dvb_generic_open,
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001459 .release = dvb_net_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001460 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461};
1462
Mauro Carvalho Chehabe3d65c32015-01-02 22:53:58 -03001463static const struct dvb_device dvbdev_net = {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001464 .priv = NULL,
1465 .users = 1,
1466 .writers = 1,
Mauro Carvalho Chehabe3d65c32015-01-02 22:53:58 -03001467#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
Mauro Carvalho Chehabe4fd3bc2015-02-18 12:09:27 -03001468 .name = "dvb-net",
Mauro Carvalho Chehabe3d65c32015-01-02 22:53:58 -03001469#endif
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001470 .fops = &dvb_net_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471};
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473void dvb_net_release (struct dvb_net *dvbnet)
1474{
1475 int i;
1476
Markus Rechberger2c4d3362007-04-14 10:19:36 -03001477 dvbnet->exit = 1;
1478 if (dvbnet->dvbdev->users < 1)
1479 wait_event(dvbnet->dvbdev->wait_queue,
1480 dvbnet->dvbdev->users==1);
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 dvb_unregister_device(dvbnet->dvbdev);
1483
1484 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1485 if (!dvbnet->state[i])
1486 continue;
1487 dvb_net_remove_if(dvbnet, i);
1488 }
1489}
1490EXPORT_SYMBOL(dvb_net_release);
1491
1492
1493int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1494 struct dmx_demux *dmx)
1495{
1496 int i;
1497
Nikolaus Schulz30ad64b2012-12-23 18:49:07 -03001498 mutex_init(&dvbnet->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 dvbnet->demux = dmx;
1500
1501 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1502 dvbnet->state[i] = 0;
1503
Jonathan Nieder58fae672011-12-31 07:54:16 -03001504 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -03001505 dvbnet, DVB_DEVICE_NET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507EXPORT_SYMBOL(dvb_net_init);