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