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