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