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