xref: /linux/drivers/media/dvb-core/dvb_net.c (revision 32786fdc9506aeba98278c1844d4bfb766863832)
1 /*
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  *
15  * ULE Decaps according to RFC 4326.
16  *
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.
45  * 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.
48  */
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  *
55  */
56 
57 #define pr_fmt(fmt) "dvb_net: " fmt
58 
59 #include <linux/module.h>
60 #include <linux/kernel.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/dvb/net.h>
64 #include <linux/uio.h>
65 #include <asm/uaccess.h>
66 #include <linux/crc32.h>
67 #include <linux/mutex.h>
68 #include <linux/sched.h>
69 
70 #include "dvb_demux.h"
71 #include "dvb_net.h"
72 
73 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
74 {
75 	unsigned int j;
76 	for (j = 0; j < cnt; j++)
77 		c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
78 	return c;
79 }
80 
81 
82 #define DVB_NET_MULTICAST_MAX 10
83 
84 #undef ULE_DEBUG
85 
86 #ifdef ULE_DEBUG
87 
88 static void hexdump(const unsigned char *buf, unsigned short len)
89 {
90 	print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
91 }
92 
93 #endif
94 
95 struct dvb_net_priv {
96 	int in_use;
97 	u16 pid;
98 	struct net_device *net;
99 	struct dvb_net *host;
100 	struct dmx_demux *demux;
101 	struct dmx_section_feed *secfeed;
102 	struct dmx_section_filter *secfilter;
103 	struct dmx_ts_feed *tsfeed;
104 	int multi_num;
105 	struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
106 	unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
107 	int rx_mode;
108 #define RX_MODE_UNI 0
109 #define RX_MODE_MULTI 1
110 #define RX_MODE_ALL_MULTI 2
111 #define RX_MODE_PROMISC 3
112 	struct work_struct set_multicast_list_wq;
113 	struct work_struct restart_net_feed_wq;
114 	unsigned char feedtype;			/* Either FEED_TYPE_ or FEED_TYPE_ULE */
115 	int need_pusi;				/* Set to 1, if synchronization on PUSI required. */
116 	unsigned char tscc;			/* TS continuity counter after sync on PUSI. */
117 	struct sk_buff *ule_skb;		/* ULE SNDU decodes into this buffer. */
118 	unsigned char *ule_next_hdr;		/* Pointer into skb to next ULE extension header. */
119 	unsigned short ule_sndu_len;		/* ULE SNDU length in bytes, w/o D-Bit. */
120 	unsigned short ule_sndu_type;		/* ULE SNDU type field, complete. */
121 	unsigned char ule_sndu_type_1;		/* ULE SNDU type field, if split across 2 TS cells. */
122 	unsigned char ule_dbit;			/* Whether the DestMAC address present
123 						 * or not (bit is set). */
124 	unsigned char ule_bridged;		/* Whether the ULE_BRIDGED extension header was found. */
125 	int ule_sndu_remain;			/* Nr. of bytes still required for current ULE SNDU. */
126 	unsigned long ts_count;			/* Current ts cell counter. */
127 	struct mutex mutex;
128 };
129 
130 
131 /**
132  *	Determine the packet's protocol ID. The rule here is that we
133  *	assume 802.3 if the type field is short enough to be a length.
134  *	This is normal practice and works for any 'now in use' protocol.
135  *
136  *  stolen from eth.c out of the linux kernel, hacked for dvb-device
137  *  by Michael Holzt <kju@debian.org>
138  */
139 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
140 				      struct net_device *dev)
141 {
142 	struct ethhdr *eth;
143 	unsigned char *rawp;
144 
145 	skb_reset_mac_header(skb);
146 	skb_pull(skb,dev->hard_header_len);
147 	eth = eth_hdr(skb);
148 
149 	if (*eth->h_dest & 1) {
150 		if(ether_addr_equal(eth->h_dest,dev->broadcast))
151 			skb->pkt_type=PACKET_BROADCAST;
152 		else
153 			skb->pkt_type=PACKET_MULTICAST;
154 	}
155 
156 	if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
157 		return eth->h_proto;
158 
159 	rawp = skb->data;
160 
161 	/**
162 	 *	This is a magic hack to spot IPX packets. Older Novell breaks
163 	 *	the protocol design and runs IPX over 802.3 without an 802.2 LLC
164 	 *	layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
165 	 *	won't work for fault tolerant netware but does for the rest.
166 	 */
167 	if (*(unsigned short *)rawp == 0xFFFF)
168 		return htons(ETH_P_802_3);
169 
170 	/**
171 	 *	Real 802.2 LLC
172 	 */
173 	return htons(ETH_P_802_2);
174 }
175 
176 #define TS_SZ	188
177 #define TS_SYNC	0x47
178 #define TS_TEI	0x80
179 #define TS_SC	0xC0
180 #define TS_PUSI	0x40
181 #define TS_AF_A	0x20
182 #define TS_AF_D	0x10
183 
184 /* ULE Extension Header handlers. */
185 
186 #define ULE_TEST	0
187 #define ULE_BRIDGED	1
188 
189 #define ULE_OPTEXTHDR_PADDING 0
190 
191 static int ule_test_sndu( struct dvb_net_priv *p )
192 {
193 	return -1;
194 }
195 
196 static int ule_bridged_sndu( struct dvb_net_priv *p )
197 {
198 	struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
199 	if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
200 		int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
201 		/* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
202 		if(framelen != ntohs(hdr->h_proto)) {
203 			return -1;
204 		}
205 	}
206 	/* Note:
207 	 * From RFC4326:
208 	 *  "A bridged SNDU is a Mandatory Extension Header of Type 1.
209 	 *   It must be the final (or only) extension header specified in the header chain of a SNDU."
210 	 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
211 	 */
212 	p->ule_bridged = 1;
213 	return 0;
214 }
215 
216 static int ule_exthdr_padding(struct dvb_net_priv *p)
217 {
218 	return 0;
219 }
220 
221 /** Handle ULE extension headers.
222  *  Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
223  *  Returns: >= 0: nr. of bytes consumed by next extension header
224  *	     -1:   Mandatory extension header that is not recognized or TEST SNDU; discard.
225  */
226 static int handle_one_ule_extension( struct dvb_net_priv *p )
227 {
228 	/* Table of mandatory extension header handlers.  The header type is the index. */
229 	static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
230 		{ [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL,  };
231 
232 	/* Table of optional extension header handlers.  The header type is the index. */
233 	static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
234 		{ [0] = ule_exthdr_padding, [1] = NULL, };
235 
236 	int ext_len = 0;
237 	unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
238 	unsigned char htype = p->ule_sndu_type & 0x00FF;
239 
240 	/* Discriminate mandatory and optional extension headers. */
241 	if (hlen == 0) {
242 		/* Mandatory extension header */
243 		if (ule_mandatory_ext_handlers[htype]) {
244 			ext_len = ule_mandatory_ext_handlers[htype]( p );
245 			if(ext_len >= 0) {
246 				p->ule_next_hdr += ext_len;
247 				if (!p->ule_bridged) {
248 					p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
249 					p->ule_next_hdr += 2;
250 				} else {
251 					p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
252 					/* This assures the extension handling loop will terminate. */
253 				}
254 			}
255 			// else: extension handler failed or SNDU should be discarded
256 		} else
257 			ext_len = -1;	/* SNDU has to be discarded. */
258 	} else {
259 		/* Optional extension header.  Calculate the length. */
260 		ext_len = hlen << 1;
261 		/* Process the optional extension header according to its type. */
262 		if (ule_optional_ext_handlers[htype])
263 			(void)ule_optional_ext_handlers[htype]( p );
264 		p->ule_next_hdr += ext_len;
265 		p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
266 		/*
267 		 * note: the length of the next header type is included in the
268 		 * length of THIS optional extension header
269 		 */
270 	}
271 
272 	return ext_len;
273 }
274 
275 static int handle_ule_extensions( struct dvb_net_priv *p )
276 {
277 	int total_ext_len = 0, l;
278 
279 	p->ule_next_hdr = p->ule_skb->data;
280 	do {
281 		l = handle_one_ule_extension( p );
282 		if (l < 0)
283 			return l;	/* Stop extension header processing and discard SNDU. */
284 		total_ext_len += l;
285 #ifdef ULE_DEBUG
286 		pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
287 			 p->ule_next_hdr, (int)p->ule_sndu_type,
288 			 l, total_ext_len);
289 #endif
290 
291 	} while (p->ule_sndu_type < ETH_P_802_3_MIN);
292 
293 	return total_ext_len;
294 }
295 
296 
297 /** Prepare for a new ULE SNDU: reset the decoder state. */
298 static inline void reset_ule( struct dvb_net_priv *p )
299 {
300 	p->ule_skb = NULL;
301 	p->ule_next_hdr = NULL;
302 	p->ule_sndu_len = 0;
303 	p->ule_sndu_type = 0;
304 	p->ule_sndu_type_1 = 0;
305 	p->ule_sndu_remain = 0;
306 	p->ule_dbit = 0xFF;
307 	p->ule_bridged = 0;
308 }
309 
310 /**
311  * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
312  * TS cells of a single PID.
313  */
314 
315 struct dvb_net_ule_handle {
316 	struct net_device *dev;
317 	struct dvb_net_priv *priv;
318 	struct ethhdr *ethh;
319 	const u8 *buf;
320 	size_t buf_len;
321 	unsigned long skipped;
322 	const u8 *ts, *ts_end, *from_where;
323 	u8 ts_remain, how_much, new_ts;
324 	bool error;
325 #ifdef ULE_DEBUG
326 	/*
327 	 * The code inside ULE_DEBUG keeps a history of the
328 	 * last 100 TS cells processed.
329 	 */
330 	static unsigned char ule_hist[100*TS_SZ];
331 	static unsigned char *ule_where = ule_hist, ule_dump;
332 #endif
333 };
334 
335 static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h)
336 {
337 	/* We are about to process a new TS cell. */
338 
339 #ifdef ULE_DEBUG
340 	if (h->ule_where >= &h->ule_hist[100*TS_SZ])
341 		h->ule_where = h->ule_hist;
342 	memcpy(h->ule_where, h->ts, TS_SZ);
343 	if (h->ule_dump) {
344 		hexdump(h->ule_where, TS_SZ);
345 		h->ule_dump = 0;
346 	}
347 	h->ule_where += TS_SZ;
348 #endif
349 
350 	/*
351 	 * Check TS h->error conditions: sync_byte, transport_error_indicator,
352 	 * scrambling_control .
353 	 */
354 	if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) ||
355 	    ((h->ts[3] & TS_SC) != 0)) {
356 		pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
357 			h->priv->ts_count, h->ts[0],
358 			(h->ts[1] & TS_TEI) >> 7,
359 			(h->ts[3] & TS_SC) >> 6);
360 
361 		/* Drop partly decoded SNDU, reset state, resync on PUSI. */
362 		if (h->priv->ule_skb) {
363 			dev_kfree_skb(h->priv->ule_skb);
364 			/* Prepare for next SNDU. */
365 			h->dev->stats.rx_errors++;
366 			h->dev->stats.rx_frame_errors++;
367 		}
368 		reset_ule(h->priv);
369 		h->priv->need_pusi = 1;
370 
371 		/* Continue with next TS cell. */
372 		h->ts += TS_SZ;
373 		h->priv->ts_count++;
374 		return 1;
375 	}
376 
377 	h->ts_remain = 184;
378 	h->from_where = h->ts + 4;
379 
380 	return 0;
381 }
382 
383 static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h)
384 {
385 	if (h->ts[1] & TS_PUSI) {
386 		/* Find beginning of first ULE SNDU in current TS cell. */
387 		/* Synchronize continuity counter. */
388 		h->priv->tscc = h->ts[3] & 0x0F;
389 		/* There is a pointer field here. */
390 		if (h->ts[4] > h->ts_remain) {
391 			pr_err("%lu: Invalid ULE packet (pointer field %d)\n",
392 				h->priv->ts_count, h->ts[4]);
393 			h->ts += TS_SZ;
394 			h->priv->ts_count++;
395 			return 1;
396 		}
397 		/* Skip to destination of pointer field. */
398 		h->from_where = &h->ts[5] + h->ts[4];
399 		h->ts_remain -= 1 + h->ts[4];
400 		h->skipped = 0;
401 	} else {
402 		h->skipped++;
403 		h->ts += TS_SZ;
404 		h->priv->ts_count++;
405 		return 1;
406 	}
407 
408 	return 0;
409 }
410 
411 static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h)
412 {
413 	/* Check continuity counter. */
414 	if ((h->ts[3] & 0x0F) == h->priv->tscc)
415 		h->priv->tscc = (h->priv->tscc + 1) & 0x0F;
416 	else {
417 		/* TS discontinuity handling: */
418 		pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n",
419 			h->priv->ts_count, h->ts[3] & 0x0F,
420 			h->priv->tscc);
421 		/* Drop partly decoded SNDU, reset state, resync on PUSI. */
422 		if (h->priv->ule_skb) {
423 			dev_kfree_skb(h->priv->ule_skb);
424 			/* Prepare for next SNDU. */
425 			// reset_ule(h->priv);  moved to below.
426 			h->dev->stats.rx_errors++;
427 			h->dev->stats.rx_frame_errors++;
428 		}
429 		reset_ule(h->priv);
430 		/* skip to next PUSI. */
431 		h->priv->need_pusi = 1;
432 		return 1;
433 	}
434 	/*
435 	 * If we still have an incomplete payload, but PUSI is
436 	 * set; some TS cells are missing.
437 	 * This is only possible here, if we missed exactly 16 TS
438 	 * cells (continuity counter wrap).
439 	 */
440 	if (h->ts[1] & TS_PUSI) {
441 		if (!h->priv->need_pusi) {
442 			if (!(*h->from_where < (h->ts_remain-1)) ||
443 			    *h->from_where != h->priv->ule_sndu_remain) {
444 				/*
445 				 * Pointer field is invalid.
446 				 * Drop this TS cell and any started ULE SNDU.
447 				 */
448 				pr_warn("%lu: Invalid pointer field: %u.\n",
449 					h->priv->ts_count,
450 					*h->from_where);
451 
452 				/*
453 				 * Drop partly decoded SNDU, reset state,
454 				 * resync on PUSI.
455 				 */
456 				if (h->priv->ule_skb) {
457 					h->error = true;
458 					dev_kfree_skb(h->priv->ule_skb);
459 				}
460 
461 				if (h->error || h->priv->ule_sndu_remain) {
462 					h->dev->stats.rx_errors++;
463 					h->dev->stats.rx_frame_errors++;
464 					h->error = false;
465 				}
466 
467 				reset_ule(h->priv);
468 				h->priv->need_pusi = 1;
469 				return 1;
470 			}
471 			/*
472 			 * Skip pointer field (we're processing a
473 			 * packed payload).
474 			 */
475 			h->from_where += 1;
476 			h->ts_remain -= 1;
477 		} else
478 			h->priv->need_pusi = 0;
479 
480 		if (h->priv->ule_sndu_remain > 183) {
481 			/*
482 			 * Current SNDU lacks more data than there
483 			 * could be available in the current TS cell.
484 			 */
485 			h->dev->stats.rx_errors++;
486 			h->dev->stats.rx_length_errors++;
487 			pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d).  Flushing incomplete payload.\n",
488 				h->priv->ts_count,
489 				h->priv->ule_sndu_remain,
490 				h->ts[4], h->ts_remain);
491 			dev_kfree_skb(h->priv->ule_skb);
492 			/* Prepare for next SNDU. */
493 			reset_ule(h->priv);
494 			/*
495 			 * Resync: go to where pointer field points to:
496 			 * start of next ULE SNDU.
497 			 */
498 			h->from_where += h->ts[4];
499 			h->ts_remain -= h->ts[4];
500 		}
501 	}
502 	return 0;
503 }
504 
505 
506 /*
507  * Start a new payload with skb.
508  * Find ULE header.  It is only guaranteed that the
509  * length field (2 bytes) is contained in the current
510  * TS.
511  * Check h.ts_remain has to be >= 2 here.
512  */
513 static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h)
514 {
515 	if (h->ts_remain < 2) {
516 		pr_warn("Invalid payload packing: only %d bytes left in TS.  Resyncing.\n",
517 			h->ts_remain);
518 		h->priv->ule_sndu_len = 0;
519 		h->priv->need_pusi = 1;
520 		h->ts += TS_SZ;
521 		return 1;
522 	}
523 
524 	if (!h->priv->ule_sndu_len) {
525 		/* Got at least two bytes, thus extrace the SNDU length. */
526 		h->priv->ule_sndu_len = h->from_where[0] << 8 |
527 					h->from_where[1];
528 		if (h->priv->ule_sndu_len & 0x8000) {
529 			/* D-Bit is set: no dest mac present. */
530 			h->priv->ule_sndu_len &= 0x7FFF;
531 			h->priv->ule_dbit = 1;
532 		} else
533 			h->priv->ule_dbit = 0;
534 
535 		if (h->priv->ule_sndu_len < 5) {
536 			pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n",
537 				h->priv->ts_count,
538 				h->priv->ule_sndu_len);
539 			h->dev->stats.rx_errors++;
540 			h->dev->stats.rx_length_errors++;
541 			h->priv->ule_sndu_len = 0;
542 			h->priv->need_pusi = 1;
543 			h->new_ts = 1;
544 			h->ts += TS_SZ;
545 			h->priv->ts_count++;
546 			return 1;
547 		}
548 		h->ts_remain -= 2;	/* consume the 2 bytes SNDU length. */
549 		h->from_where += 2;
550 	}
551 
552 	h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2;
553 	/*
554 	 * State of current TS:
555 	 *   h->ts_remain (remaining bytes in the current TS cell)
556 	 *   0	ule_type is not available now, we need the next TS cell
557 	 *   1	the first byte of the ule_type is present
558 	 * >=2	full ULE header present, maybe some payload data as well.
559 	 */
560 	switch (h->ts_remain) {
561 	case 1:
562 		h->priv->ule_sndu_remain--;
563 		h->priv->ule_sndu_type = h->from_where[0] << 8;
564 
565 		/* first byte of ule_type is set. */
566 		h->priv->ule_sndu_type_1 = 1;
567 		h->ts_remain -= 1;
568 		h->from_where += 1;
569 		/* fallthrough */
570 	case 0:
571 		h->new_ts = 1;
572 		h->ts += TS_SZ;
573 		h->priv->ts_count++;
574 		return 1;
575 
576 	default: /* complete ULE header is present in current TS. */
577 		/* Extract ULE type field. */
578 		if (h->priv->ule_sndu_type_1) {
579 			h->priv->ule_sndu_type_1 = 0;
580 			h->priv->ule_sndu_type |= h->from_where[0];
581 			h->from_where += 1; /* points to payload start. */
582 			h->ts_remain -= 1;
583 		} else {
584 			/* Complete type is present in new TS. */
585 			h->priv->ule_sndu_type = h->from_where[0] << 8 |
586 						 h->from_where[1];
587 			h->from_where += 2; /* points to payload start. */
588 			h->ts_remain -= 2;
589 		}
590 		break;
591 	}
592 
593 	/*
594 	 * Allocate the skb (decoder target buffer) with the correct size,
595 	 * as follows:
596 	 *
597 	 * prepare for the largest case: bridged SNDU with MAC address
598 	 * (dbit = 0).
599 	 */
600 	h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len +
601 					 ETH_HLEN + ETH_ALEN);
602 	if (!h->priv->ule_skb) {
603 		pr_notice("%s: Memory squeeze, dropping packet.\n",
604 			  h->dev->name);
605 		h->dev->stats.rx_dropped++;
606 		return -1;
607 	}
608 
609 	/* This includes the CRC32 _and_ dest mac, if !dbit. */
610 	h->priv->ule_sndu_remain = h->priv->ule_sndu_len;
611 	h->priv->ule_skb->dev = h->dev;
612 	/*
613 	 * Leave space for Ethernet or bridged SNDU header
614 	 * (eth hdr plus one MAC addr).
615 	 */
616 	skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN);
617 
618 	return 0;
619 }
620 
621 
622 static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h)
623 {
624 	static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff };
625 
626 	/*
627 	 * The destination MAC address is the next data in the skb.  It comes
628 	 * before any extension headers.
629 	 *
630 	 * Check if the payload of this SNDU should be passed up the stack.
631 	 */
632 	if (h->priv->rx_mode == RX_MODE_PROMISC)
633 		return 0;
634 
635 	if (h->priv->ule_skb->data[0] & 0x01) {
636 		/* multicast or broadcast */
637 		if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) {
638 			/* multicast */
639 			if (h->priv->rx_mode == RX_MODE_MULTI) {
640 				int i;
641 
642 				for (i = 0; i < h->priv->multi_num &&
643 				     !ether_addr_equal(h->priv->ule_skb->data,
644 						       h->priv->multi_macs[i]);
645 				     i++)
646 					;
647 				if (i == h->priv->multi_num)
648 					return 1;
649 			} else if (h->priv->rx_mode != RX_MODE_ALL_MULTI)
650 				return 1; /* no broadcast; */
651 			/*
652 			 * else:
653 			 * all multicast mode: accept all multicast packets
654 			 */
655 		}
656 		/* else: broadcast */
657 	} else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr))
658 		return 1;
659 
660 	return 0;
661 }
662 
663 
664 static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h,
665 				  u32 ule_crc, u32 expected_crc)
666 {
667 	u8 dest_addr[ETH_ALEN];
668 
669 	if (ule_crc != expected_crc) {
670 		pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
671 			h->priv->ts_count, ule_crc, expected_crc,
672 			h->priv->ule_sndu_len, h->priv->ule_sndu_type,
673 			h->ts_remain,
674 			h->ts_remain > 2 ?
675 				*(unsigned short *)h->from_where : 0);
676 
677 	#ifdef ULE_DEBUG
678 		hexdump(iov[0].iov_base, iov[0].iov_len);
679 		hexdump(iov[1].iov_base, iov[1].iov_len);
680 		hexdump(iov[2].iov_base, iov[2].iov_len);
681 
682 		if (h->ule_where == h->ule_hist) {
683 			hexdump(&h->ule_hist[98*TS_SZ], TS_SZ);
684 			hexdump(&h->ule_hist[99*TS_SZ], TS_SZ);
685 		} else if (h->ule_where == &h->ule_hist[TS_SZ]) {
686 			hexdump(&h->ule_hist[99*TS_SZ], TS_SZ);
687 			hexdump(h->ule_hist, TS_SZ);
688 		} else {
689 			hexdump(h->ule_where - TS_SZ - TS_SZ, TS_SZ);
690 			hexdump(h->ule_where - TS_SZ, TS_SZ);
691 		}
692 		h->ule_dump = 1;
693 	#endif
694 
695 		h->dev->stats.rx_errors++;
696 		h->dev->stats.rx_crc_errors++;
697 		dev_kfree_skb(h->priv->ule_skb);
698 
699 		return;
700 	}
701 
702 	/* CRC32 verified OK. */
703 
704 	/* CRC32 was OK, so remove it from skb. */
705 	h->priv->ule_skb->tail -= 4;
706 	h->priv->ule_skb->len -= 4;
707 
708 	if (!h->priv->ule_dbit) {
709 		if (dvb_net_ule_should_drop(h)) {
710 #ifdef ULE_DEBUG
711 			netdev_dbg(h->dev,
712 				   "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n",
713 				   h->priv->ule_skb->data, h->dev->dev_addr);
714 #endif
715 			dev_kfree_skb(h->priv->ule_skb);
716 			return;
717 		}
718 
719 		skb_copy_from_linear_data(h->priv->ule_skb, dest_addr,
720 					  ETH_ALEN);
721 		skb_pull(h->priv->ule_skb, ETH_ALEN);
722 	}
723 
724 	/* Handle ULE Extension Headers. */
725 	if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) {
726 		/* There is an extension header.  Handle it accordingly. */
727 		int l = handle_ule_extensions(h->priv);
728 
729 		if (l < 0) {
730 			/*
731 			 * Mandatory extension header unknown or TEST SNDU.
732 			 * Drop it.
733 			 */
734 
735 			// pr_warn("Dropping SNDU, extension headers.\n" );
736 			dev_kfree_skb(h->priv->ule_skb);
737 			return;
738 		}
739 		skb_pull(h->priv->ule_skb, l);
740 	}
741 
742 	/*
743 	 * Construct/assure correct ethernet header.
744 	 * Note: in bridged mode (h->priv->ule_bridged != 0)
745 	 * we already have the (original) ethernet
746 	 * header at the start of the payload (after
747 	 * optional dest. address and any extension
748 	 * headers).
749 	 */
750 	if (!h->priv->ule_bridged) {
751 		skb_push(h->priv->ule_skb, ETH_HLEN);
752 		h->ethh = (struct ethhdr *)h->priv->ule_skb->data;
753 		if (!h->priv->ule_dbit) {
754 			/*
755 			 * dest_addr buffer is only valid if
756 			 * h->priv->ule_dbit == 0
757 			 */
758 			memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN);
759 			eth_zero_addr(h->ethh->h_source);
760 		} else /* zeroize source and dest */
761 			memset(h->ethh, 0, ETH_ALEN * 2);
762 
763 		h->ethh->h_proto = htons(h->priv->ule_sndu_type);
764 	}
765 	/* else:  skb is in correct state; nothing to do. */
766 	h->priv->ule_bridged = 0;
767 
768 	/* Stuff into kernel's protocol stack. */
769 	h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb,
770 							   h->dev);
771 	/*
772 	 * If D-bit is set (i.e. destination MAC address not present),
773 	 * receive the packet anyhow.
774 	 */
775 #if 0
776 	if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
777 		h->priv->ule_skb->pkt_type = PACKET_HOST;
778 #endif
779 	h->dev->stats.rx_packets++;
780 	h->dev->stats.rx_bytes += h->priv->ule_skb->len;
781 	netif_rx(h->priv->ule_skb);
782 }
783 
784 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len)
785 {
786 	int ret;
787 	struct dvb_net_ule_handle h = {
788 		.dev = dev,
789 		.buf = buf,
790 		.buf_len = buf_len,
791 		.skipped = 0L,
792 		.ts = NULL,
793 		.ts_end = NULL,
794 		.from_where = NULL,
795 		.ts_remain = 0,
796 		.how_much = 0,
797 		.new_ts = 1,
798 		.ethh = NULL,
799 		.error = false,
800 #ifdef ULE_DEBUG
801 		.ule_where = ule_hist,
802 #endif
803 	};
804 
805 	/*
806 	 * For all TS cells in current buffer.
807 	 * Appearently, we are called for every single TS cell.
808 	 */
809 	for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len;
810 	     h.ts < h.ts_end; /* no incr. */) {
811 		if (h.new_ts) {
812 			/* We are about to process a new TS cell. */
813 			if (dvb_net_ule_new_ts_cell(&h))
814 				continue;
815 		}
816 
817 		/* Synchronize on PUSI, if required. */
818 		if (h.priv->need_pusi) {
819 			if (dvb_net_ule_ts_pusi(&h))
820 				continue;
821 		}
822 
823 		if (h.new_ts) {
824 			if (dvb_net_ule_new_ts(&h))
825 				continue;
826 		}
827 
828 		/* Check if new payload needs to be started. */
829 		if (h.priv->ule_skb == NULL) {
830 			ret = dvb_net_ule_new_payload(&h);
831 			if (ret < 0)
832 				return;
833 			if (ret)
834 				continue;
835 		}
836 
837 		/* Copy data into our current skb. */
838 		h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain);
839 		memcpy(skb_put(h.priv->ule_skb, h.how_much),
840 		       h.from_where, h.how_much);
841 		h.priv->ule_sndu_remain -= h.how_much;
842 		h.ts_remain -= h.how_much;
843 		h.from_where += h.how_much;
844 
845 		/* Check for complete payload. */
846 		if (h.priv->ule_sndu_remain <= 0) {
847 			/* Check CRC32, we've got it in our skb already. */
848 			__be16 ulen = htons(h.priv->ule_sndu_len);
849 			__be16 utype = htons(h.priv->ule_sndu_type);
850 			const u8 *tail;
851 			struct kvec iov[3] = {
852 				{ &ulen, sizeof ulen },
853 				{ &utype, sizeof utype },
854 				{ h.priv->ule_skb->data,
855 				  h.priv->ule_skb->len - 4 }
856 			};
857 			u32 ule_crc = ~0L, expected_crc;
858 			if (h.priv->ule_dbit) {
859 				/* Set D-bit for CRC32 verification,
860 				 * if it was set originally. */
861 				ulen |= htons(0x8000);
862 			}
863 
864 			ule_crc = iov_crc32(ule_crc, iov, 3);
865 			tail = skb_tail_pointer(h.priv->ule_skb);
866 			expected_crc = *(tail - 4) << 24 |
867 				       *(tail - 3) << 16 |
868 				       *(tail - 2) << 8 |
869 				       *(tail - 1);
870 
871 			dvb_net_ule_check_crc(&h, ule_crc, expected_crc);
872 
873 			/* Prepare for next SNDU. */
874 			reset_ule(h.priv);
875 		}
876 
877 		/* More data in current TS (look at the bytes following the CRC32)? */
878 		if (h.ts_remain >= 2 && *((unsigned short *)h.from_where) != 0xFFFF) {
879 			/* Next ULE SNDU starts right there. */
880 			h.new_ts = 0;
881 			h.priv->ule_skb = NULL;
882 			h.priv->ule_sndu_type_1 = 0;
883 			h.priv->ule_sndu_len = 0;
884 			// pr_warn("More data in current TS: [%#x %#x %#x %#x]\n",
885 			//	*(h.from_where + 0), *(h.from_where + 1),
886 			//	*(h.from_where + 2), *(h.from_where + 3));
887 			// pr_warn("h.ts @ %p, stopped @ %p:\n", h.ts, h.from_where + 0);
888 			// hexdump(h.ts, 188);
889 		} else {
890 			h.new_ts = 1;
891 			h.ts += TS_SZ;
892 			h.priv->ts_count++;
893 			if (h.priv->ule_skb == NULL) {
894 				h.priv->need_pusi = 1;
895 				h.priv->ule_sndu_type_1 = 0;
896 				h.priv->ule_sndu_len = 0;
897 			}
898 		}
899 	}	/* for all available TS cells */
900 }
901 
902 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
903 			       const u8 *buffer2, size_t buffer2_len,
904 			       struct dmx_ts_feed *feed)
905 {
906 	struct net_device *dev = feed->priv;
907 
908 	if (buffer2)
909 		pr_warn("buffer2 not NULL: %p.\n", buffer2);
910 	if (buffer1_len > 32768)
911 		pr_warn("length > 32k: %zu.\n", buffer1_len);
912 	/* pr_info("TS callback: %u bytes, %u TS cells @ %p.\n",
913 		  buffer1_len, buffer1_len / TS_SZ, buffer1); */
914 	dvb_net_ule(dev, buffer1, buffer1_len);
915 	return 0;
916 }
917 
918 
919 static void dvb_net_sec(struct net_device *dev,
920 			const u8 *pkt, int pkt_len)
921 {
922 	u8 *eth;
923 	struct sk_buff *skb;
924 	struct net_device_stats *stats = &dev->stats;
925 	int snap = 0;
926 
927 	/* note: pkt_len includes a 32bit checksum */
928 	if (pkt_len < 16) {
929 		pr_warn("%s: IP/MPE packet length = %d too small.\n",
930 			dev->name, pkt_len);
931 		stats->rx_errors++;
932 		stats->rx_length_errors++;
933 		return;
934 	}
935 /* it seems some ISPs manage to screw up here, so we have to
936  * relax the error checks... */
937 #if 0
938 	if ((pkt[5] & 0xfd) != 0xc1) {
939 		/* drop scrambled or broken packets */
940 #else
941 	if ((pkt[5] & 0x3c) != 0x00) {
942 		/* drop scrambled */
943 #endif
944 		stats->rx_errors++;
945 		stats->rx_crc_errors++;
946 		return;
947 	}
948 	if (pkt[5] & 0x02) {
949 		/* handle LLC/SNAP, see rfc-1042 */
950 		if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
951 			stats->rx_dropped++;
952 			return;
953 		}
954 		snap = 8;
955 	}
956 	if (pkt[7]) {
957 		/* FIXME: assemble datagram from multiple sections */
958 		stats->rx_errors++;
959 		stats->rx_frame_errors++;
960 		return;
961 	}
962 
963 	/* we have 14 byte ethernet header (ip header follows);
964 	 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
965 	 */
966 	if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
967 		//pr_notice("%s: Memory squeeze, dropping packet.\n", dev->name);
968 		stats->rx_dropped++;
969 		return;
970 	}
971 	skb_reserve(skb, 2);    /* longword align L3 header */
972 	skb->dev = dev;
973 
974 	/* copy L3 payload */
975 	eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
976 	memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
977 
978 	/* create ethernet header: */
979 	eth[0]=pkt[0x0b];
980 	eth[1]=pkt[0x0a];
981 	eth[2]=pkt[0x09];
982 	eth[3]=pkt[0x08];
983 	eth[4]=pkt[0x04];
984 	eth[5]=pkt[0x03];
985 
986 	eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
987 
988 	if (snap) {
989 		eth[12] = pkt[18];
990 		eth[13] = pkt[19];
991 	} else {
992 		/* protocol numbers are from rfc-1700 or
993 		 * http://www.iana.org/assignments/ethernet-numbers
994 		 */
995 		if (pkt[12] >> 4 == 6) { /* version field from IP header */
996 			eth[12] = 0x86;	/* IPv6 */
997 			eth[13] = 0xdd;
998 		} else {
999 			eth[12] = 0x08;	/* IPv4 */
1000 			eth[13] = 0x00;
1001 		}
1002 	}
1003 
1004 	skb->protocol = dvb_net_eth_type_trans(skb, dev);
1005 
1006 	stats->rx_packets++;
1007 	stats->rx_bytes+=skb->len;
1008 	netif_rx(skb);
1009 }
1010 
1011 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
1012 		 const u8 *buffer2, size_t buffer2_len,
1013 		 struct dmx_section_filter *filter)
1014 {
1015 	struct net_device *dev = filter->priv;
1016 
1017 	/**
1018 	 * we rely on the DVB API definition where exactly one complete
1019 	 * section is delivered in buffer1
1020 	 */
1021 	dvb_net_sec (dev, buffer1, buffer1_len);
1022 	return 0;
1023 }
1024 
1025 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
1026 {
1027 	dev_kfree_skb(skb);
1028 	return NETDEV_TX_OK;
1029 }
1030 
1031 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1032 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
1033 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
1034 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1035 
1036 static int dvb_net_filter_sec_set(struct net_device *dev,
1037 		   struct dmx_section_filter **secfilter,
1038 		   u8 *mac, u8 *mac_mask)
1039 {
1040 	struct dvb_net_priv *priv = netdev_priv(dev);
1041 	int ret;
1042 
1043 	*secfilter=NULL;
1044 	ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
1045 	if (ret<0) {
1046 		pr_err("%s: could not get filter\n", dev->name);
1047 		return ret;
1048 	}
1049 
1050 	(*secfilter)->priv=(void *) dev;
1051 
1052 	memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
1053 	memset((*secfilter)->filter_mask,  0x00, DMX_MAX_FILTER_SIZE);
1054 	memset((*secfilter)->filter_mode,  0xff, DMX_MAX_FILTER_SIZE);
1055 
1056 	(*secfilter)->filter_value[0]=0x3e;
1057 	(*secfilter)->filter_value[3]=mac[5];
1058 	(*secfilter)->filter_value[4]=mac[4];
1059 	(*secfilter)->filter_value[8]=mac[3];
1060 	(*secfilter)->filter_value[9]=mac[2];
1061 	(*secfilter)->filter_value[10]=mac[1];
1062 	(*secfilter)->filter_value[11]=mac[0];
1063 
1064 	(*secfilter)->filter_mask[0] = 0xff;
1065 	(*secfilter)->filter_mask[3] = mac_mask[5];
1066 	(*secfilter)->filter_mask[4] = mac_mask[4];
1067 	(*secfilter)->filter_mask[8] = mac_mask[3];
1068 	(*secfilter)->filter_mask[9] = mac_mask[2];
1069 	(*secfilter)->filter_mask[10] = mac_mask[1];
1070 	(*secfilter)->filter_mask[11]=mac_mask[0];
1071 
1072 	netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
1073 
1074 	return 0;
1075 }
1076 
1077 static int dvb_net_feed_start(struct net_device *dev)
1078 {
1079 	int ret = 0, i;
1080 	struct dvb_net_priv *priv = netdev_priv(dev);
1081 	struct dmx_demux *demux = priv->demux;
1082 	unsigned char *mac = (unsigned char *) dev->dev_addr;
1083 
1084 	netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode);
1085 	mutex_lock(&priv->mutex);
1086 	if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
1087 		pr_err("%s: BUG %d\n", __func__, __LINE__);
1088 
1089 	priv->secfeed=NULL;
1090 	priv->secfilter=NULL;
1091 	priv->tsfeed = NULL;
1092 
1093 	if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1094 		netdev_dbg(dev, "alloc secfeed\n");
1095 		ret=demux->allocate_section_feed(demux, &priv->secfeed,
1096 					 dvb_net_sec_callback);
1097 		if (ret<0) {
1098 			pr_err("%s: could not allocate section feed\n",
1099 			       dev->name);
1100 			goto error;
1101 		}
1102 
1103 		ret = priv->secfeed->set(priv->secfeed, priv->pid, 1);
1104 
1105 		if (ret<0) {
1106 			pr_err("%s: could not set section feed\n", dev->name);
1107 			priv->demux->release_section_feed(priv->demux, priv->secfeed);
1108 			priv->secfeed=NULL;
1109 			goto error;
1110 		}
1111 
1112 		if (priv->rx_mode != RX_MODE_PROMISC) {
1113 			netdev_dbg(dev, "set secfilter\n");
1114 			dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
1115 		}
1116 
1117 		switch (priv->rx_mode) {
1118 		case RX_MODE_MULTI:
1119 			for (i = 0; i < priv->multi_num; i++) {
1120 				netdev_dbg(dev, "set multi_secfilter[%d]\n", i);
1121 				dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1122 						       priv->multi_macs[i], mask_normal);
1123 			}
1124 			break;
1125 		case RX_MODE_ALL_MULTI:
1126 			priv->multi_num=1;
1127 			netdev_dbg(dev, "set multi_secfilter[0]\n");
1128 			dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1129 					       mac_allmulti, mask_allmulti);
1130 			break;
1131 		case RX_MODE_PROMISC:
1132 			priv->multi_num=0;
1133 			netdev_dbg(dev, "set secfilter\n");
1134 			dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1135 			break;
1136 		}
1137 
1138 		netdev_dbg(dev, "start filtering\n");
1139 		priv->secfeed->start_filtering(priv->secfeed);
1140 	} else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1141 		ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
1142 
1143 		/* we have payloads encapsulated in TS */
1144 		netdev_dbg(dev, "alloc tsfeed\n");
1145 		ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1146 		if (ret < 0) {
1147 			pr_err("%s: could not allocate ts feed\n", dev->name);
1148 			goto error;
1149 		}
1150 
1151 		/* Set netdevice pointer for ts decaps callback. */
1152 		priv->tsfeed->priv = (void *)dev;
1153 		ret = priv->tsfeed->set(priv->tsfeed,
1154 					priv->pid, /* pid */
1155 					TS_PACKET, /* type */
1156 					DMX_PES_OTHER, /* pes type */
1157 					timeout    /* timeout */
1158 					);
1159 
1160 		if (ret < 0) {
1161 			pr_err("%s: could not set ts feed\n", dev->name);
1162 			priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1163 			priv->tsfeed = NULL;
1164 			goto error;
1165 		}
1166 
1167 		netdev_dbg(dev, "start filtering\n");
1168 		priv->tsfeed->start_filtering(priv->tsfeed);
1169 	} else
1170 		ret = -EINVAL;
1171 
1172 error:
1173 	mutex_unlock(&priv->mutex);
1174 	return ret;
1175 }
1176 
1177 static int dvb_net_feed_stop(struct net_device *dev)
1178 {
1179 	struct dvb_net_priv *priv = netdev_priv(dev);
1180 	int i, ret = 0;
1181 
1182 	mutex_lock(&priv->mutex);
1183 	if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1184 		if (priv->secfeed) {
1185 			if (priv->secfeed->is_filtering) {
1186 				netdev_dbg(dev, "stop secfeed\n");
1187 				priv->secfeed->stop_filtering(priv->secfeed);
1188 			}
1189 
1190 			if (priv->secfilter) {
1191 				netdev_dbg(dev, "release secfilter\n");
1192 				priv->secfeed->release_filter(priv->secfeed,
1193 							      priv->secfilter);
1194 				priv->secfilter=NULL;
1195 			}
1196 
1197 			for (i=0; i<priv->multi_num; i++) {
1198 				if (priv->multi_secfilter[i]) {
1199 					netdev_dbg(dev, "release multi_filter[%d]\n",
1200 						   i);
1201 					priv->secfeed->release_filter(priv->secfeed,
1202 								      priv->multi_secfilter[i]);
1203 					priv->multi_secfilter[i] = NULL;
1204 				}
1205 			}
1206 
1207 			priv->demux->release_section_feed(priv->demux, priv->secfeed);
1208 			priv->secfeed = NULL;
1209 		} else
1210 			pr_err("%s: no feed to stop\n", dev->name);
1211 	} else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1212 		if (priv->tsfeed) {
1213 			if (priv->tsfeed->is_filtering) {
1214 				netdev_dbg(dev, "stop tsfeed\n");
1215 				priv->tsfeed->stop_filtering(priv->tsfeed);
1216 			}
1217 			priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1218 			priv->tsfeed = NULL;
1219 		}
1220 		else
1221 			pr_err("%s: no ts feed to stop\n", dev->name);
1222 	} else
1223 		ret = -EINVAL;
1224 	mutex_unlock(&priv->mutex);
1225 	return ret;
1226 }
1227 
1228 
1229 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
1230 {
1231 	struct dvb_net_priv *priv = netdev_priv(dev);
1232 
1233 	if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1234 		return -ENOMEM;
1235 
1236 	memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
1237 
1238 	priv->multi_num++;
1239 	return 0;
1240 }
1241 
1242 
1243 static void wq_set_multicast_list (struct work_struct *work)
1244 {
1245 	struct dvb_net_priv *priv =
1246 		container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1247 	struct net_device *dev = priv->net;
1248 
1249 	dvb_net_feed_stop(dev);
1250 	priv->rx_mode = RX_MODE_UNI;
1251 	netif_addr_lock_bh(dev);
1252 
1253 	if (dev->flags & IFF_PROMISC) {
1254 		netdev_dbg(dev, "promiscuous mode\n");
1255 		priv->rx_mode = RX_MODE_PROMISC;
1256 	} else if ((dev->flags & IFF_ALLMULTI)) {
1257 		netdev_dbg(dev, "allmulti mode\n");
1258 		priv->rx_mode = RX_MODE_ALL_MULTI;
1259 	} else if (!netdev_mc_empty(dev)) {
1260 		struct netdev_hw_addr *ha;
1261 
1262 		netdev_dbg(dev, "set_mc_list, %d entries\n",
1263 			   netdev_mc_count(dev));
1264 
1265 		priv->rx_mode = RX_MODE_MULTI;
1266 		priv->multi_num = 0;
1267 
1268 		netdev_for_each_mc_addr(ha, dev)
1269 			dvb_set_mc_filter(dev, ha->addr);
1270 	}
1271 
1272 	netif_addr_unlock_bh(dev);
1273 	dvb_net_feed_start(dev);
1274 }
1275 
1276 
1277 static void dvb_net_set_multicast_list (struct net_device *dev)
1278 {
1279 	struct dvb_net_priv *priv = netdev_priv(dev);
1280 	schedule_work(&priv->set_multicast_list_wq);
1281 }
1282 
1283 
1284 static void wq_restart_net_feed (struct work_struct *work)
1285 {
1286 	struct dvb_net_priv *priv =
1287 		container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1288 	struct net_device *dev = priv->net;
1289 
1290 	if (netif_running(dev)) {
1291 		dvb_net_feed_stop(dev);
1292 		dvb_net_feed_start(dev);
1293 	}
1294 }
1295 
1296 
1297 static int dvb_net_set_mac (struct net_device *dev, void *p)
1298 {
1299 	struct dvb_net_priv *priv = netdev_priv(dev);
1300 	struct sockaddr *addr=p;
1301 
1302 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1303 
1304 	if (netif_running(dev))
1305 		schedule_work(&priv->restart_net_feed_wq);
1306 
1307 	return 0;
1308 }
1309 
1310 
1311 static int dvb_net_open(struct net_device *dev)
1312 {
1313 	struct dvb_net_priv *priv = netdev_priv(dev);
1314 
1315 	priv->in_use++;
1316 	dvb_net_feed_start(dev);
1317 	return 0;
1318 }
1319 
1320 
1321 static int dvb_net_stop(struct net_device *dev)
1322 {
1323 	struct dvb_net_priv *priv = netdev_priv(dev);
1324 
1325 	priv->in_use--;
1326 	return dvb_net_feed_stop(dev);
1327 }
1328 
1329 static const struct header_ops dvb_header_ops = {
1330 	.create		= eth_header,
1331 	.parse		= eth_header_parse,
1332 };
1333 
1334 
1335 static const struct net_device_ops dvb_netdev_ops = {
1336 	.ndo_open		= dvb_net_open,
1337 	.ndo_stop		= dvb_net_stop,
1338 	.ndo_start_xmit		= dvb_net_tx,
1339 	.ndo_set_rx_mode	= dvb_net_set_multicast_list,
1340 	.ndo_set_mac_address    = dvb_net_set_mac,
1341 	.ndo_validate_addr	= eth_validate_addr,
1342 };
1343 
1344 static void dvb_net_setup(struct net_device *dev)
1345 {
1346 	ether_setup(dev);
1347 
1348 	dev->header_ops		= &dvb_header_ops;
1349 	dev->netdev_ops		= &dvb_netdev_ops;
1350 	dev->mtu		= 4096;
1351 	dev->max_mtu		= 4096;
1352 
1353 	dev->flags |= IFF_NOARP;
1354 }
1355 
1356 static int get_if(struct dvb_net *dvbnet)
1357 {
1358 	int i;
1359 
1360 	for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1361 		if (!dvbnet->state[i])
1362 			break;
1363 
1364 	if (i == DVB_NET_DEVICES_MAX)
1365 		return -1;
1366 
1367 	dvbnet->state[i]=1;
1368 	return i;
1369 }
1370 
1371 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1372 {
1373 	struct net_device *net;
1374 	struct dvb_net_priv *priv;
1375 	int result;
1376 	int if_num;
1377 
1378 	if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1379 		return -EINVAL;
1380 	if ((if_num = get_if(dvbnet)) < 0)
1381 		return -EINVAL;
1382 
1383 	net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1384 			   NET_NAME_UNKNOWN, dvb_net_setup);
1385 	if (!net)
1386 		return -ENOMEM;
1387 
1388 	if (dvbnet->dvbdev->id)
1389 		snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1390 			 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1391 	else
1392 		/* compatibility fix to keep dvb0_0 format */
1393 		snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1394 			 dvbnet->dvbdev->adapter->num, if_num);
1395 
1396 	net->addr_len = 6;
1397 	memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1398 
1399 	dvbnet->device[if_num] = net;
1400 
1401 	priv = netdev_priv(net);
1402 	priv->net = net;
1403 	priv->demux = dvbnet->demux;
1404 	priv->pid = pid;
1405 	priv->rx_mode = RX_MODE_UNI;
1406 	priv->need_pusi = 1;
1407 	priv->tscc = 0;
1408 	priv->feedtype = feedtype;
1409 	reset_ule(priv);
1410 
1411 	INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1412 	INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1413 	mutex_init(&priv->mutex);
1414 
1415 	net->base_addr = pid;
1416 
1417 	if ((result = register_netdev(net)) < 0) {
1418 		dvbnet->device[if_num] = NULL;
1419 		free_netdev(net);
1420 		return result;
1421 	}
1422 	pr_info("created network interface %s\n", net->name);
1423 
1424 	return if_num;
1425 }
1426 
1427 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1428 {
1429 	struct net_device *net = dvbnet->device[num];
1430 	struct dvb_net_priv *priv;
1431 
1432 	if (!dvbnet->state[num])
1433 		return -EINVAL;
1434 	priv = netdev_priv(net);
1435 	if (priv->in_use)
1436 		return -EBUSY;
1437 
1438 	dvb_net_stop(net);
1439 	flush_work(&priv->set_multicast_list_wq);
1440 	flush_work(&priv->restart_net_feed_wq);
1441 	pr_info("removed network interface %s\n", net->name);
1442 	unregister_netdev(net);
1443 	dvbnet->state[num]=0;
1444 	dvbnet->device[num] = NULL;
1445 	free_netdev(net);
1446 
1447 	return 0;
1448 }
1449 
1450 static int dvb_net_do_ioctl(struct file *file,
1451 		  unsigned int cmd, void *parg)
1452 {
1453 	struct dvb_device *dvbdev = file->private_data;
1454 	struct dvb_net *dvbnet = dvbdev->priv;
1455 	int ret = 0;
1456 
1457 	if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1458 		return -EPERM;
1459 
1460 	if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1461 		return -ERESTARTSYS;
1462 
1463 	switch (cmd) {
1464 	case NET_ADD_IF:
1465 	{
1466 		struct dvb_net_if *dvbnetif = parg;
1467 		int result;
1468 
1469 		if (!capable(CAP_SYS_ADMIN)) {
1470 			ret = -EPERM;
1471 			goto ioctl_error;
1472 		}
1473 
1474 		if (!try_module_get(dvbdev->adapter->module)) {
1475 			ret = -EPERM;
1476 			goto ioctl_error;
1477 		}
1478 
1479 		result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1480 		if (result<0) {
1481 			module_put(dvbdev->adapter->module);
1482 			ret = result;
1483 			goto ioctl_error;
1484 		}
1485 		dvbnetif->if_num=result;
1486 		break;
1487 	}
1488 	case NET_GET_IF:
1489 	{
1490 		struct net_device *netdev;
1491 		struct dvb_net_priv *priv_data;
1492 		struct dvb_net_if *dvbnetif = parg;
1493 
1494 		if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1495 		    !dvbnet->state[dvbnetif->if_num]) {
1496 			ret = -EINVAL;
1497 			goto ioctl_error;
1498 		}
1499 
1500 		netdev = dvbnet->device[dvbnetif->if_num];
1501 
1502 		priv_data = netdev_priv(netdev);
1503 		dvbnetif->pid=priv_data->pid;
1504 		dvbnetif->feedtype=priv_data->feedtype;
1505 		break;
1506 	}
1507 	case NET_REMOVE_IF:
1508 	{
1509 		if (!capable(CAP_SYS_ADMIN)) {
1510 			ret = -EPERM;
1511 			goto ioctl_error;
1512 		}
1513 		if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1514 			ret = -EINVAL;
1515 			goto ioctl_error;
1516 		}
1517 		ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1518 		if (!ret)
1519 			module_put(dvbdev->adapter->module);
1520 		break;
1521 	}
1522 
1523 	/* binary compatibility cruft */
1524 	case __NET_ADD_IF_OLD:
1525 	{
1526 		struct __dvb_net_if_old *dvbnetif = parg;
1527 		int result;
1528 
1529 		if (!capable(CAP_SYS_ADMIN)) {
1530 			ret = -EPERM;
1531 			goto ioctl_error;
1532 		}
1533 
1534 		if (!try_module_get(dvbdev->adapter->module)) {
1535 			ret = -EPERM;
1536 			goto ioctl_error;
1537 		}
1538 
1539 		result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1540 		if (result<0) {
1541 			module_put(dvbdev->adapter->module);
1542 			ret = result;
1543 			goto ioctl_error;
1544 		}
1545 		dvbnetif->if_num=result;
1546 		break;
1547 	}
1548 	case __NET_GET_IF_OLD:
1549 	{
1550 		struct net_device *netdev;
1551 		struct dvb_net_priv *priv_data;
1552 		struct __dvb_net_if_old *dvbnetif = parg;
1553 
1554 		if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1555 		    !dvbnet->state[dvbnetif->if_num]) {
1556 			ret = -EINVAL;
1557 			goto ioctl_error;
1558 		}
1559 
1560 		netdev = dvbnet->device[dvbnetif->if_num];
1561 
1562 		priv_data = netdev_priv(netdev);
1563 		dvbnetif->pid=priv_data->pid;
1564 		break;
1565 	}
1566 	default:
1567 		ret = -ENOTTY;
1568 		break;
1569 	}
1570 
1571 ioctl_error:
1572 	mutex_unlock(&dvbnet->ioctl_mutex);
1573 	return ret;
1574 }
1575 
1576 static long dvb_net_ioctl(struct file *file,
1577 	      unsigned int cmd, unsigned long arg)
1578 {
1579 	return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1580 }
1581 
1582 static int dvb_net_close(struct inode *inode, struct file *file)
1583 {
1584 	struct dvb_device *dvbdev = file->private_data;
1585 	struct dvb_net *dvbnet = dvbdev->priv;
1586 
1587 	dvb_generic_release(inode, file);
1588 
1589 	if(dvbdev->users == 1 && dvbnet->exit == 1)
1590 		wake_up(&dvbdev->wait_queue);
1591 	return 0;
1592 }
1593 
1594 
1595 static const struct file_operations dvb_net_fops = {
1596 	.owner = THIS_MODULE,
1597 	.unlocked_ioctl = dvb_net_ioctl,
1598 	.open =	dvb_generic_open,
1599 	.release = dvb_net_close,
1600 	.llseek = noop_llseek,
1601 };
1602 
1603 static const struct dvb_device dvbdev_net = {
1604 	.priv = NULL,
1605 	.users = 1,
1606 	.writers = 1,
1607 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1608 	.name = "dvb-net",
1609 #endif
1610 	.fops = &dvb_net_fops,
1611 };
1612 
1613 void dvb_net_release (struct dvb_net *dvbnet)
1614 {
1615 	int i;
1616 
1617 	dvbnet->exit = 1;
1618 	if (dvbnet->dvbdev->users < 1)
1619 		wait_event(dvbnet->dvbdev->wait_queue,
1620 				dvbnet->dvbdev->users==1);
1621 
1622 	dvb_unregister_device(dvbnet->dvbdev);
1623 
1624 	for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1625 		if (!dvbnet->state[i])
1626 			continue;
1627 		dvb_net_remove_if(dvbnet, i);
1628 	}
1629 }
1630 EXPORT_SYMBOL(dvb_net_release);
1631 
1632 
1633 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1634 		  struct dmx_demux *dmx)
1635 {
1636 	int i;
1637 
1638 	mutex_init(&dvbnet->ioctl_mutex);
1639 	dvbnet->demux = dmx;
1640 
1641 	for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1642 		dvbnet->state[i] = 0;
1643 
1644 	return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
1645 			     dvbnet, DVB_DEVICE_NET, 0);
1646 }
1647 EXPORT_SYMBOL(dvb_net_init);
1648