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