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