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