dvb: Push down BKL into ioctl functions
[linux-2.6.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
355 #ifdef ULE_DEBUG
356         /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
357         static unsigned char ule_hist[100*TS_SZ];
358         static unsigned char *ule_where = ule_hist, ule_dump;
359 #endif
360
361         /* For all TS cells in current buffer.
362          * Appearently, we are called for every single TS cell.
363          */
364         for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
365
366                 if (new_ts) {
367                         /* We are about to process a new TS cell. */
368
369 #ifdef ULE_DEBUG
370                         if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
371                         memcpy( ule_where, ts, TS_SZ );
372                         if (ule_dump) {
373                                 hexdump( ule_where, TS_SZ );
374                                 ule_dump = 0;
375                         }
376                         ule_where += TS_SZ;
377 #endif
378
379                         /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
380                         if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
381                                 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
382                                        priv->ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
383
384                                 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
385                                 if (priv->ule_skb) {
386                                         dev_kfree_skb( priv->ule_skb );
387                                         /* Prepare for next SNDU. */
388                                         dev->stats.rx_errors++;
389                                         dev->stats.rx_frame_errors++;
390                                 }
391                                 reset_ule(priv);
392                                 priv->need_pusi = 1;
393
394                                 /* Continue with next TS cell. */
395                                 ts += TS_SZ;
396                                 priv->ts_count++;
397                                 continue;
398                         }
399
400                         ts_remain = 184;
401                         from_where = ts + 4;
402                 }
403                 /* Synchronize on PUSI, if required. */
404                 if (priv->need_pusi) {
405                         if (ts[1] & TS_PUSI) {
406                                 /* Find beginning of first ULE SNDU in current TS cell. */
407                                 /* Synchronize continuity counter. */
408                                 priv->tscc = ts[3] & 0x0F;
409                                 /* There is a pointer field here. */
410                                 if (ts[4] > ts_remain) {
411                                         printk(KERN_ERR "%lu: Invalid ULE packet "
412                                                "(pointer field %d)\n", priv->ts_count, ts[4]);
413                                         ts += TS_SZ;
414                                         priv->ts_count++;
415                                         continue;
416                                 }
417                                 /* Skip to destination of pointer field. */
418                                 from_where = &ts[5] + ts[4];
419                                 ts_remain -= 1 + ts[4];
420                                 skipped = 0;
421                         } else {
422                                 skipped++;
423                                 ts += TS_SZ;
424                                 priv->ts_count++;
425                                 continue;
426                         }
427                 }
428
429                 if (new_ts) {
430                         /* Check continuity counter. */
431                         if ((ts[3] & 0x0F) == priv->tscc)
432                                 priv->tscc = (priv->tscc + 1) & 0x0F;
433                         else {
434                                 /* TS discontinuity handling: */
435                                 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
436                                        "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
437                                 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
438                                 if (priv->ule_skb) {
439                                         dev_kfree_skb( priv->ule_skb );
440                                         /* Prepare for next SNDU. */
441                                         // reset_ule(priv);  moved to below.
442                                         dev->stats.rx_errors++;
443                                         dev->stats.rx_frame_errors++;
444                                 }
445                                 reset_ule(priv);
446                                 /* skip to next PUSI. */
447                                 priv->need_pusi = 1;
448                                 continue;
449                         }
450                         /* If we still have an incomplete payload, but PUSI is
451                          * set; some TS cells are missing.
452                          * This is only possible here, if we missed exactly 16 TS
453                          * cells (continuity counter wrap). */
454                         if (ts[1] & TS_PUSI) {
455                                 if (! priv->need_pusi) {
456                                         if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
457                                                 /* Pointer field is invalid.  Drop this TS cell and any started ULE SNDU. */
458                                                 printk(KERN_WARNING "%lu: Invalid pointer "
459                                                        "field: %u.\n", priv->ts_count, *from_where);
460
461                                                 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
462                                                 if (priv->ule_skb) {
463                                                         dev_kfree_skb( priv->ule_skb );
464                                                         dev->stats.rx_errors++;
465                                                         dev->stats.rx_frame_errors++;
466                                                 }
467                                                 reset_ule(priv);
468                                                 priv->need_pusi = 1;
469                                                 continue;
470                                         }
471                                         /* Skip pointer field (we're processing a
472                                          * packed payload). */
473                                         from_where += 1;
474                                         ts_remain -= 1;
475                                 } else
476                                         priv->need_pusi = 0;
477
478                                 if (priv->ule_sndu_remain > 183) {
479                                         /* Current SNDU lacks more data than there could be available in the
480                                          * current TS cell. */
481                                         dev->stats.rx_errors++;
482                                         dev->stats.rx_length_errors++;
483                                         printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
484                                                "got PUSI (pf %d, ts_remain %d).  Flushing incomplete payload.\n",
485                                                priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
486                                         dev_kfree_skb(priv->ule_skb);
487                                         /* Prepare for next SNDU. */
488                                         reset_ule(priv);
489                                         /* Resync: go to where pointer field points to: start of next ULE SNDU. */
490                                         from_where += ts[4];
491                                         ts_remain -= ts[4];
492                                 }
493                         }
494                 }
495
496                 /* Check if new payload needs to be started. */
497                 if (priv->ule_skb == NULL) {
498                         /* Start a new payload with skb.
499                          * Find ULE header.  It is only guaranteed that the
500                          * length field (2 bytes) is contained in the current
501                          * TS.
502                          * Check ts_remain has to be >= 2 here. */
503                         if (ts_remain < 2) {
504                                 printk(KERN_WARNING "Invalid payload packing: only %d "
505                                        "bytes left in TS.  Resyncing.\n", ts_remain);
506                                 priv->ule_sndu_len = 0;
507                                 priv->need_pusi = 1;
508                                 ts += TS_SZ;
509                                 continue;
510                         }
511
512                         if (! priv->ule_sndu_len) {
513                                 /* Got at least two bytes, thus extrace the SNDU length. */
514                                 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
515                                 if (priv->ule_sndu_len & 0x8000) {
516                                         /* D-Bit is set: no dest mac present. */
517                                         priv->ule_sndu_len &= 0x7FFF;
518                                         priv->ule_dbit = 1;
519                                 } else
520                                         priv->ule_dbit = 0;
521
522                                 if (priv->ule_sndu_len < 5) {
523                                         printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
524                                                "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
525                                         dev->stats.rx_errors++;
526                                         dev->stats.rx_length_errors++;
527                                         priv->ule_sndu_len = 0;
528                                         priv->need_pusi = 1;
529                                         new_ts = 1;
530                                         ts += TS_SZ;
531                                         priv->ts_count++;
532                                         continue;
533                                 }
534                                 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
535                                 from_where += 2;
536                         }
537
538                         /*
539                          * State of current TS:
540                          *   ts_remain (remaining bytes in the current TS cell)
541                          *   0  ule_type is not available now, we need the next TS cell
542                          *   1  the first byte of the ule_type is present
543                          * >=2  full ULE header present, maybe some payload data as well.
544                          */
545                         switch (ts_remain) {
546                                 case 1:
547                                         priv->ule_sndu_type = from_where[0] << 8;
548                                         priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
549                                         ts_remain -= 1; from_where += 1;
550                                         /* Continue w/ next TS. */
551                                 case 0:
552                                         new_ts = 1;
553                                         ts += TS_SZ;
554                                         priv->ts_count++;
555                                         continue;
556
557                                 default: /* complete ULE header is present in current TS. */
558                                         /* Extract ULE type field. */
559                                         if (priv->ule_sndu_type_1) {
560                                                 priv->ule_sndu_type |= from_where[0];
561                                                 from_where += 1; /* points to payload start. */
562                                                 ts_remain -= 1;
563                                         } else {
564                                                 /* Complete type is present in new TS. */
565                                                 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
566                                                 from_where += 2; /* points to payload start. */
567                                                 ts_remain -= 2;
568                                         }
569                                         break;
570                         }
571
572                         /* Allocate the skb (decoder target buffer) with the correct size, as follows:
573                          * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
574                         priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
575                         if (priv->ule_skb == NULL) {
576                                 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
577                                        dev->name);
578                                 dev->stats.rx_dropped++;
579                                 return;
580                         }
581
582                         /* This includes the CRC32 _and_ dest mac, if !dbit. */
583                         priv->ule_sndu_remain = priv->ule_sndu_len;
584                         priv->ule_skb->dev = dev;
585                         /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
586                         skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
587                 }
588
589                 /* Copy data into our current skb. */
590                 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
591                 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
592                 priv->ule_sndu_remain -= how_much;
593                 ts_remain -= how_much;
594                 from_where += how_much;
595
596                 /* Check for complete payload. */
597                 if (priv->ule_sndu_remain <= 0) {
598                         /* Check CRC32, we've got it in our skb already. */
599                         __be16 ulen = htons(priv->ule_sndu_len);
600                         __be16 utype = htons(priv->ule_sndu_type);
601                         const u8 *tail;
602                         struct kvec iov[3] = {
603                                 { &ulen, sizeof ulen },
604                                 { &utype, sizeof utype },
605                                 { priv->ule_skb->data, priv->ule_skb->len - 4 }
606                         };
607                         u32 ule_crc = ~0L, expected_crc;
608                         if (priv->ule_dbit) {
609                                 /* Set D-bit for CRC32 verification,
610                                  * if it was set originally. */
611                                 ulen |= htons(0x8000);
612                         }
613
614                         ule_crc = iov_crc32(ule_crc, iov, 3);
615                         tail = skb_tail_pointer(priv->ule_skb);
616                         expected_crc = *(tail - 4) << 24 |
617                                        *(tail - 3) << 16 |
618                                        *(tail - 2) << 8 |
619                                        *(tail - 1);
620                         if (ule_crc != expected_crc) {
621                                 printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
622                                        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);
623
624 #ifdef ULE_DEBUG
625                                 hexdump( iov[0].iov_base, iov[0].iov_len );
626                                 hexdump( iov[1].iov_base, iov[1].iov_len );
627                                 hexdump( iov[2].iov_base, iov[2].iov_len );
628
629                                 if (ule_where == ule_hist) {
630                                         hexdump( &ule_hist[98*TS_SZ], TS_SZ );
631                                         hexdump( &ule_hist[99*TS_SZ], TS_SZ );
632                                 } else if (ule_where == &ule_hist[TS_SZ]) {
633                                         hexdump( &ule_hist[99*TS_SZ], TS_SZ );
634                                         hexdump( ule_hist, TS_SZ );
635                                 } else {
636                                         hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
637                                         hexdump( ule_where - TS_SZ, TS_SZ );
638                                 }
639                                 ule_dump = 1;
640 #endif
641
642                                 dev->stats.rx_errors++;
643                                 dev->stats.rx_crc_errors++;
644                                 dev_kfree_skb(priv->ule_skb);
645                         } else {
646                                 /* CRC32 verified OK. */
647                                 u8 dest_addr[ETH_ALEN];
648                                 static const u8 bc_addr[ETH_ALEN] =
649                                         { [ 0 ... ETH_ALEN-1] = 0xff };
650
651                                 /* CRC32 was OK. Remove it from skb. */
652                                 priv->ule_skb->tail -= 4;
653                                 priv->ule_skb->len -= 4;
654
655                                 if (!priv->ule_dbit) {
656                                         /*
657                                          * The destination MAC address is the
658                                          * next data in the skb.  It comes
659                                          * before any extension headers.
660                                          *
661                                          * Check if the payload of this SNDU
662                                          * should be passed up the stack.
663                                          */
664                                         register int drop = 0;
665                                         if (priv->rx_mode != RX_MODE_PROMISC) {
666                                                 if (priv->ule_skb->data[0] & 0x01) {
667                                                         /* multicast or broadcast */
668                                                         if (memcmp(priv->ule_skb->data, bc_addr, ETH_ALEN)) {
669                                                                 /* multicast */
670                                                                 if (priv->rx_mode == RX_MODE_MULTI) {
671                                                                         int i;
672                                                                         for(i = 0; i < priv->multi_num && memcmp(priv->ule_skb->data, priv->multi_macs[i], ETH_ALEN); i++)
673                                                                                 ;
674                                                                         if (i == priv->multi_num)
675                                                                                 drop = 1;
676                                                                 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
677                                                                         drop = 1; /* no broadcast; */
678                                                                 /* else: all multicast mode: accept all multicast packets */
679                                                         }
680                                                         /* else: broadcast */
681                                                 }
682                                                 else if (memcmp(priv->ule_skb->data, dev->dev_addr, ETH_ALEN))
683                                                         drop = 1;
684                                                 /* else: destination address matches the MAC address of our receiver device */
685                                         }
686                                         /* else: promiscuous mode; pass everything up the stack */
687
688                                         if (drop) {
689 #ifdef ULE_DEBUG
690                                                 dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
691                                                         MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
692 #endif
693                                                 dev_kfree_skb(priv->ule_skb);
694                                                 goto sndu_done;
695                                         }
696                                         else
697                                         {
698                                                 skb_copy_from_linear_data(priv->ule_skb,
699                                                               dest_addr,
700                                                               ETH_ALEN);
701                                                 skb_pull(priv->ule_skb, ETH_ALEN);
702                                         }
703                                 }
704
705                                 /* Handle ULE Extension Headers. */
706                                 if (priv->ule_sndu_type < 1536) {
707                                         /* There is an extension header.  Handle it accordingly. */
708                                         int l = handle_ule_extensions(priv);
709                                         if (l < 0) {
710                                                 /* Mandatory extension header unknown or TEST SNDU.  Drop it. */
711                                                 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
712                                                 dev_kfree_skb(priv->ule_skb);
713                                                 goto sndu_done;
714                                         }
715                                         skb_pull(priv->ule_skb, l);
716                                 }
717
718                                 /*
719                                  * Construct/assure correct ethernet header.
720                                  * Note: in bridged mode (priv->ule_bridged !=
721                                  * 0) we already have the (original) ethernet
722                                  * header at the start of the payload (after
723                                  * optional dest. address and any extension
724                                  * headers).
725                                  */
726
727                                 if (!priv->ule_bridged) {
728                                         skb_push(priv->ule_skb, ETH_HLEN);
729                                         ethh = (struct ethhdr *)priv->ule_skb->data;
730                                         if (!priv->ule_dbit) {
731                                                  /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
732                                                 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
733                                                 memset(ethh->h_source, 0, ETH_ALEN);
734                                         }
735                                         else /* zeroize source and dest */
736                                                 memset( ethh, 0, ETH_ALEN*2 );
737
738                                         ethh->h_proto = htons(priv->ule_sndu_type);
739                                 }
740                                 /* else:  skb is in correct state; nothing to do. */
741                                 priv->ule_bridged = 0;
742
743                                 /* Stuff into kernel's protocol stack. */
744                                 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
745                                 /* If D-bit is set (i.e. destination MAC address not present),
746                                  * receive the packet anyhow. */
747                                 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
748                                         priv->ule_skb->pkt_type = PACKET_HOST; */
749                                 dev->stats.rx_packets++;
750                                 dev->stats.rx_bytes += priv->ule_skb->len;
751                                 netif_rx(priv->ule_skb);
752                         }
753                         sndu_done:
754                         /* Prepare for next SNDU. */
755                         reset_ule(priv);
756                 }
757
758                 /* More data in current TS (look at the bytes following the CRC32)? */
759                 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
760                         /* Next ULE SNDU starts right there. */
761                         new_ts = 0;
762                         priv->ule_skb = NULL;
763                         priv->ule_sndu_type_1 = 0;
764                         priv->ule_sndu_len = 0;
765                         // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
766                         //      *(from_where + 0), *(from_where + 1),
767                         //      *(from_where + 2), *(from_where + 3));
768                         // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
769                         // hexdump(ts, 188);
770                 } else {
771                         new_ts = 1;
772                         ts += TS_SZ;
773                         priv->ts_count++;
774                         if (priv->ule_skb == NULL) {
775                                 priv->need_pusi = 1;
776                                 priv->ule_sndu_type_1 = 0;
777                                 priv->ule_sndu_len = 0;
778                         }
779                 }
780         }       /* for all available TS cells */
781 }
782
783 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
784                                const u8 *buffer2, size_t buffer2_len,
785                                struct dmx_ts_feed *feed, enum dmx_success success)
786 {
787         struct net_device *dev = feed->priv;
788
789         if (buffer2)
790                 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
791         if (buffer1_len > 32768)
792                 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
793         /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
794                   buffer1_len, buffer1_len / TS_SZ, buffer1); */
795         dvb_net_ule(dev, buffer1, buffer1_len);
796         return 0;
797 }
798
799
800 static void dvb_net_sec(struct net_device *dev,
801                         const u8 *pkt, int pkt_len)
802 {
803         u8 *eth;
804         struct sk_buff *skb;
805         struct net_device_stats *stats = &dev->stats;
806         int snap = 0;
807
808         /* note: pkt_len includes a 32bit checksum */
809         if (pkt_len < 16) {
810                 printk("%s: IP/MPE packet length = %d too small.\n",
811                         dev->name, pkt_len);
812                 stats->rx_errors++;
813                 stats->rx_length_errors++;
814                 return;
815         }
816 /* it seems some ISPs manage to screw up here, so we have to
817  * relax the error checks... */
818 #if 0
819         if ((pkt[5] & 0xfd) != 0xc1) {
820                 /* drop scrambled or broken packets */
821 #else
822         if ((pkt[5] & 0x3c) != 0x00) {
823                 /* drop scrambled */
824 #endif
825                 stats->rx_errors++;
826                 stats->rx_crc_errors++;
827                 return;
828         }
829         if (pkt[5] & 0x02) {
830                 /* handle LLC/SNAP, see rfc-1042 */
831                 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
832                         stats->rx_dropped++;
833                         return;
834                 }
835                 snap = 8;
836         }
837         if (pkt[7]) {
838                 /* FIXME: assemble datagram from multiple sections */
839                 stats->rx_errors++;
840                 stats->rx_frame_errors++;
841                 return;
842         }
843
844         /* we have 14 byte ethernet header (ip header follows);
845          * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
846          */
847         if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
848                 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
849                 stats->rx_dropped++;
850                 return;
851         }
852         skb_reserve(skb, 2);    /* longword align L3 header */
853         skb->dev = dev;
854
855         /* copy L3 payload */
856         eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
857         memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
858
859         /* create ethernet header: */
860         eth[0]=pkt[0x0b];
861         eth[1]=pkt[0x0a];
862         eth[2]=pkt[0x09];
863         eth[3]=pkt[0x08];
864         eth[4]=pkt[0x04];
865         eth[5]=pkt[0x03];
866
867         eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
868
869         if (snap) {
870                 eth[12] = pkt[18];
871                 eth[13] = pkt[19];
872         } else {
873                 /* protocol numbers are from rfc-1700 or
874                  * http://www.iana.org/assignments/ethernet-numbers
875                  */
876                 if (pkt[12] >> 4 == 6) { /* version field from IP header */
877                         eth[12] = 0x86; /* IPv6 */
878                         eth[13] = 0xdd;
879                 } else {
880                         eth[12] = 0x08; /* IPv4 */
881                         eth[13] = 0x00;
882                 }
883         }
884
885         skb->protocol = dvb_net_eth_type_trans(skb, dev);
886
887         stats->rx_packets++;
888         stats->rx_bytes+=skb->len;
889         netif_rx(skb);
890 }
891
892 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
893                  const u8 *buffer2, size_t buffer2_len,
894                  struct dmx_section_filter *filter,
895                  enum dmx_success success)
896 {
897         struct net_device *dev = filter->priv;
898
899         /**
900          * we rely on the DVB API definition where exactly one complete
901          * section is delivered in buffer1
902          */
903         dvb_net_sec (dev, buffer1, buffer1_len);
904         return 0;
905 }
906
907 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
908 {
909         dev_kfree_skb(skb);
910         return NETDEV_TX_OK;
911 }
912
913 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
914 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
915 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
916 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
917
918 static int dvb_net_filter_sec_set(struct net_device *dev,
919                    struct dmx_section_filter **secfilter,
920                    u8 *mac, u8 *mac_mask)
921 {
922         struct dvb_net_priv *priv = netdev_priv(dev);
923         int ret;
924
925         *secfilter=NULL;
926         ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
927         if (ret<0) {
928                 printk("%s: could not get filter\n", dev->name);
929                 return ret;
930         }
931
932         (*secfilter)->priv=(void *) dev;
933
934         memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
935         memset((*secfilter)->filter_mask,  0x00, DMX_MAX_FILTER_SIZE);
936         memset((*secfilter)->filter_mode,  0xff, DMX_MAX_FILTER_SIZE);
937
938         (*secfilter)->filter_value[0]=0x3e;
939         (*secfilter)->filter_value[3]=mac[5];
940         (*secfilter)->filter_value[4]=mac[4];
941         (*secfilter)->filter_value[8]=mac[3];
942         (*secfilter)->filter_value[9]=mac[2];
943         (*secfilter)->filter_value[10]=mac[1];
944         (*secfilter)->filter_value[11]=mac[0];
945
946         (*secfilter)->filter_mask[0] = 0xff;
947         (*secfilter)->filter_mask[3] = mac_mask[5];
948         (*secfilter)->filter_mask[4] = mac_mask[4];
949         (*secfilter)->filter_mask[8] = mac_mask[3];
950         (*secfilter)->filter_mask[9] = mac_mask[2];
951         (*secfilter)->filter_mask[10] = mac_mask[1];
952         (*secfilter)->filter_mask[11]=mac_mask[0];
953
954         dprintk("%s: filter mac=%pM\n", dev->name, mac);
955         dprintk("%s: filter mask=%pM\n", dev->name, mac_mask);
956
957         return 0;
958 }
959
960 static int dvb_net_feed_start(struct net_device *dev)
961 {
962         int ret = 0, i;
963         struct dvb_net_priv *priv = netdev_priv(dev);
964         struct dmx_demux *demux = priv->demux;
965         unsigned char *mac = (unsigned char *) dev->dev_addr;
966
967         dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
968         mutex_lock(&priv->mutex);
969         if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
970                 printk("%s: BUG %d\n", __func__, __LINE__);
971
972         priv->secfeed=NULL;
973         priv->secfilter=NULL;
974         priv->tsfeed = NULL;
975
976         if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
977                 dprintk("%s: alloc secfeed\n", __func__);
978                 ret=demux->allocate_section_feed(demux, &priv->secfeed,
979                                          dvb_net_sec_callback);
980                 if (ret<0) {
981                         printk("%s: could not allocate section feed\n", dev->name);
982                         goto error;
983                 }
984
985                 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
986
987                 if (ret<0) {
988                         printk("%s: could not set section feed\n", dev->name);
989                         priv->demux->release_section_feed(priv->demux, priv->secfeed);
990                         priv->secfeed=NULL;
991                         goto error;
992                 }
993
994                 if (priv->rx_mode != RX_MODE_PROMISC) {
995                         dprintk("%s: set secfilter\n", __func__);
996                         dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
997                 }
998
999                 switch (priv->rx_mode) {
1000                 case RX_MODE_MULTI:
1001                         for (i = 0; i < priv->multi_num; i++) {
1002                                 dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
1003                                 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1004                                                        priv->multi_macs[i], mask_normal);
1005                         }
1006                         break;
1007                 case RX_MODE_ALL_MULTI:
1008                         priv->multi_num=1;
1009                         dprintk("%s: set multi_secfilter[0]\n", __func__);
1010                         dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1011                                                mac_allmulti, mask_allmulti);
1012                         break;
1013                 case RX_MODE_PROMISC:
1014                         priv->multi_num=0;
1015                         dprintk("%s: set secfilter\n", __func__);
1016                         dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1017                         break;
1018                 }
1019
1020                 dprintk("%s: start filtering\n", __func__);
1021                 priv->secfeed->start_filtering(priv->secfeed);
1022         } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1023                 struct timespec timeout = { 0, 10000000 }; // 10 msec
1024
1025                 /* we have payloads encapsulated in TS */
1026                 dprintk("%s: alloc tsfeed\n", __func__);
1027                 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1028                 if (ret < 0) {
1029                         printk("%s: could not allocate ts feed\n", dev->name);
1030                         goto error;
1031                 }
1032
1033                 /* Set netdevice pointer for ts decaps callback. */
1034                 priv->tsfeed->priv = (void *)dev;
1035                 ret = priv->tsfeed->set(priv->tsfeed,
1036                                         priv->pid, /* pid */
1037                                         TS_PACKET, /* type */
1038                                         DMX_TS_PES_OTHER, /* pes type */
1039                                         32768,     /* circular buffer size */
1040                                         timeout    /* timeout */
1041                                         );
1042
1043                 if (ret < 0) {
1044                         printk("%s: could not set ts feed\n", dev->name);
1045                         priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1046                         priv->tsfeed = NULL;
1047                         goto error;
1048                 }
1049
1050                 dprintk("%s: start filtering\n", __func__);
1051                 priv->tsfeed->start_filtering(priv->tsfeed);
1052         } else
1053                 ret = -EINVAL;
1054
1055 error:
1056         mutex_unlock(&priv->mutex);
1057         return ret;
1058 }
1059
1060 static int dvb_net_feed_stop(struct net_device *dev)
1061 {
1062         struct dvb_net_priv *priv = netdev_priv(dev);
1063         int i, ret = 0;
1064
1065         dprintk("%s\n", __func__);
1066         mutex_lock(&priv->mutex);
1067         if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1068                 if (priv->secfeed) {
1069                         if (priv->secfeed->is_filtering) {
1070                                 dprintk("%s: stop secfeed\n", __func__);
1071                                 priv->secfeed->stop_filtering(priv->secfeed);
1072                         }
1073
1074                         if (priv->secfilter) {
1075                                 dprintk("%s: release secfilter\n", __func__);
1076                                 priv->secfeed->release_filter(priv->secfeed,
1077                                                               priv->secfilter);
1078                                 priv->secfilter=NULL;
1079                         }
1080
1081                         for (i=0; i<priv->multi_num; i++) {
1082                                 if (priv->multi_secfilter[i]) {
1083                                         dprintk("%s: release multi_filter[%d]\n",
1084                                                 __func__, i);
1085                                         priv->secfeed->release_filter(priv->secfeed,
1086                                                                       priv->multi_secfilter[i]);
1087                                         priv->multi_secfilter[i] = NULL;
1088                                 }
1089                         }
1090
1091                         priv->demux->release_section_feed(priv->demux, priv->secfeed);
1092                         priv->secfeed = NULL;
1093                 } else
1094                         printk("%s: no feed to stop\n", dev->name);
1095         } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1096                 if (priv->tsfeed) {
1097                         if (priv->tsfeed->is_filtering) {
1098                                 dprintk("%s: stop tsfeed\n", __func__);
1099                                 priv->tsfeed->stop_filtering(priv->tsfeed);
1100                         }
1101                         priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1102                         priv->tsfeed = NULL;
1103                 }
1104                 else
1105                         printk("%s: no ts feed to stop\n", dev->name);
1106         } else
1107                 ret = -EINVAL;
1108         mutex_unlock(&priv->mutex);
1109         return ret;
1110 }
1111
1112
1113 static int dvb_set_mc_filter (struct net_device *dev, struct dev_mc_list *mc)
1114 {
1115         struct dvb_net_priv *priv = netdev_priv(dev);
1116
1117         if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1118                 return -ENOMEM;
1119
1120         memcpy(priv->multi_macs[priv->multi_num], mc->dmi_addr, 6);
1121
1122         priv->multi_num++;
1123         return 0;
1124 }
1125
1126
1127 static void wq_set_multicast_list (struct work_struct *work)
1128 {
1129         struct dvb_net_priv *priv =
1130                 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1131         struct net_device *dev = priv->net;
1132
1133         dvb_net_feed_stop(dev);
1134         priv->rx_mode = RX_MODE_UNI;
1135         netif_addr_lock_bh(dev);
1136
1137         if (dev->flags & IFF_PROMISC) {
1138                 dprintk("%s: promiscuous mode\n", dev->name);
1139                 priv->rx_mode = RX_MODE_PROMISC;
1140         } else if ((dev->flags & IFF_ALLMULTI)) {
1141                 dprintk("%s: allmulti mode\n", dev->name);
1142                 priv->rx_mode = RX_MODE_ALL_MULTI;
1143         } else if (!netdev_mc_empty(dev)) {
1144                 int mci;
1145                 struct dev_mc_list *mc;
1146
1147                 dprintk("%s: set_mc_list, %d entries\n",
1148                         dev->name, netdev_mc_count(dev));
1149
1150                 priv->rx_mode = RX_MODE_MULTI;
1151                 priv->multi_num = 0;
1152
1153                 for (mci = 0, mc=dev->mc_list;
1154                      mci < netdev_mc_count(dev);
1155                      mc = mc->next, mci++) {
1156                         dvb_set_mc_filter(dev, mc);
1157                 }
1158         }
1159
1160         netif_addr_unlock_bh(dev);
1161         dvb_net_feed_start(dev);
1162 }
1163
1164
1165 static void dvb_net_set_multicast_list (struct net_device *dev)
1166 {
1167         struct dvb_net_priv *priv = netdev_priv(dev);
1168         schedule_work(&priv->set_multicast_list_wq);
1169 }
1170
1171
1172 static void wq_restart_net_feed (struct work_struct *work)
1173 {
1174         struct dvb_net_priv *priv =
1175                 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1176         struct net_device *dev = priv->net;
1177
1178         if (netif_running(dev)) {
1179                 dvb_net_feed_stop(dev);
1180                 dvb_net_feed_start(dev);
1181         }
1182 }
1183
1184
1185 static int dvb_net_set_mac (struct net_device *dev, void *p)
1186 {
1187         struct dvb_net_priv *priv = netdev_priv(dev);
1188         struct sockaddr *addr=p;
1189
1190         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1191
1192         if (netif_running(dev))
1193                 schedule_work(&priv->restart_net_feed_wq);
1194
1195         return 0;
1196 }
1197
1198
1199 static int dvb_net_open(struct net_device *dev)
1200 {
1201         struct dvb_net_priv *priv = netdev_priv(dev);
1202
1203         priv->in_use++;
1204         dvb_net_feed_start(dev);
1205         return 0;
1206 }
1207
1208
1209 static int dvb_net_stop(struct net_device *dev)
1210 {
1211         struct dvb_net_priv *priv = netdev_priv(dev);
1212
1213         priv->in_use--;
1214         return dvb_net_feed_stop(dev);
1215 }
1216
1217 static const struct header_ops dvb_header_ops = {
1218         .create         = eth_header,
1219         .parse          = eth_header_parse,
1220         .rebuild        = eth_rebuild_header,
1221 };
1222
1223
1224 static const struct net_device_ops dvb_netdev_ops = {
1225         .ndo_open               = dvb_net_open,
1226         .ndo_stop               = dvb_net_stop,
1227         .ndo_start_xmit         = dvb_net_tx,
1228         .ndo_set_multicast_list = dvb_net_set_multicast_list,
1229         .ndo_set_mac_address    = dvb_net_set_mac,
1230         .ndo_change_mtu         = eth_change_mtu,
1231         .ndo_validate_addr      = eth_validate_addr,
1232 };
1233
1234 static void dvb_net_setup(struct net_device *dev)
1235 {
1236         ether_setup(dev);
1237
1238         dev->header_ops         = &dvb_header_ops;
1239         dev->netdev_ops         = &dvb_netdev_ops;
1240         dev->mtu                = 4096;
1241
1242         dev->flags |= IFF_NOARP;
1243 }
1244
1245 static int get_if(struct dvb_net *dvbnet)
1246 {
1247         int i;
1248
1249         for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1250                 if (!dvbnet->state[i])
1251                         break;
1252
1253         if (i == DVB_NET_DEVICES_MAX)
1254                 return -1;
1255
1256         dvbnet->state[i]=1;
1257         return i;
1258 }
1259
1260 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1261 {
1262         struct net_device *net;
1263         struct dvb_net_priv *priv;
1264         int result;
1265         int if_num;
1266
1267         if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1268                 return -EINVAL;
1269         if ((if_num = get_if(dvbnet)) < 0)
1270                 return -EINVAL;
1271
1272         net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", dvb_net_setup);
1273         if (!net)
1274                 return -ENOMEM;
1275
1276         if (dvbnet->dvbdev->id)
1277                 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1278                          dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1279         else
1280                 /* compatibility fix to keep dvb0_0 format */
1281                 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1282                          dvbnet->dvbdev->adapter->num, if_num);
1283
1284         net->addr_len = 6;
1285         memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1286
1287         dvbnet->device[if_num] = net;
1288
1289         priv = netdev_priv(net);
1290         priv->net = net;
1291         priv->demux = dvbnet->demux;
1292         priv->pid = pid;
1293         priv->rx_mode = RX_MODE_UNI;
1294         priv->need_pusi = 1;
1295         priv->tscc = 0;
1296         priv->feedtype = feedtype;
1297         reset_ule(priv);
1298
1299         INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1300         INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1301         mutex_init(&priv->mutex);
1302
1303         net->base_addr = pid;
1304
1305         if ((result = register_netdev(net)) < 0) {
1306                 dvbnet->device[if_num] = NULL;
1307                 free_netdev(net);
1308                 return result;
1309         }
1310         printk("dvb_net: created network interface %s\n", net->name);
1311
1312         return if_num;
1313 }
1314
1315 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1316 {
1317         struct net_device *net = dvbnet->device[num];
1318         struct dvb_net_priv *priv;
1319
1320         if (!dvbnet->state[num])
1321                 return -EINVAL;
1322         priv = netdev_priv(net);
1323         if (priv->in_use)
1324                 return -EBUSY;
1325
1326         dvb_net_stop(net);
1327         flush_scheduled_work();
1328         printk("dvb_net: removed network interface %s\n", net->name);
1329         unregister_netdev(net);
1330         dvbnet->state[num]=0;
1331         dvbnet->device[num] = NULL;
1332         free_netdev(net);
1333
1334         return 0;
1335 }
1336
1337 static int dvb_net_do_ioctl(struct file *file,
1338                   unsigned int cmd, void *parg)
1339 {
1340         struct dvb_device *dvbdev = file->private_data;
1341         struct dvb_net *dvbnet = dvbdev->priv;
1342
1343         if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1344                 return -EPERM;
1345
1346         switch (cmd) {
1347         case NET_ADD_IF:
1348         {
1349                 struct dvb_net_if *dvbnetif = parg;
1350                 int result;
1351
1352                 if (!capable(CAP_SYS_ADMIN))
1353                         return -EPERM;
1354
1355                 if (!try_module_get(dvbdev->adapter->module))
1356                         return -EPERM;
1357
1358                 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1359                 if (result<0) {
1360                         module_put(dvbdev->adapter->module);
1361                         return result;
1362                 }
1363                 dvbnetif->if_num=result;
1364                 break;
1365         }
1366         case NET_GET_IF:
1367         {
1368                 struct net_device *netdev;
1369                 struct dvb_net_priv *priv_data;
1370                 struct dvb_net_if *dvbnetif = parg;
1371
1372                 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1373                     !dvbnet->state[dvbnetif->if_num])
1374                         return -EINVAL;
1375
1376                 netdev = dvbnet->device[dvbnetif->if_num];
1377
1378                 priv_data = netdev_priv(netdev);
1379                 dvbnetif->pid=priv_data->pid;
1380                 dvbnetif->feedtype=priv_data->feedtype;
1381                 break;
1382         }
1383         case NET_REMOVE_IF:
1384         {
1385                 int ret;
1386
1387                 if (!capable(CAP_SYS_ADMIN))
1388                         return -EPERM;
1389                 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX)
1390                         return -EINVAL;
1391                 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1392                 if (!ret)
1393                         module_put(dvbdev->adapter->module);
1394                 return ret;
1395         }
1396
1397         /* binary compatibility cruft */
1398         case __NET_ADD_IF_OLD:
1399         {
1400                 struct __dvb_net_if_old *dvbnetif = parg;
1401                 int result;
1402
1403                 if (!capable(CAP_SYS_ADMIN))
1404                         return -EPERM;
1405
1406                 if (!try_module_get(dvbdev->adapter->module))
1407                         return -EPERM;
1408
1409                 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1410                 if (result<0) {
1411                         module_put(dvbdev->adapter->module);
1412                         return result;
1413                 }
1414                 dvbnetif->if_num=result;
1415                 break;
1416         }
1417         case __NET_GET_IF_OLD:
1418         {
1419                 struct net_device *netdev;
1420                 struct dvb_net_priv *priv_data;
1421                 struct __dvb_net_if_old *dvbnetif = parg;
1422
1423                 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1424                     !dvbnet->state[dvbnetif->if_num])
1425                         return -EINVAL;
1426
1427                 netdev = dvbnet->device[dvbnetif->if_num];
1428
1429                 priv_data = netdev_priv(netdev);
1430                 dvbnetif->pid=priv_data->pid;
1431                 break;
1432         }
1433         default:
1434                 return -ENOTTY;
1435         }
1436         return 0;
1437 }
1438
1439 static long dvb_net_ioctl(struct file *file,
1440               unsigned int cmd, unsigned long arg)
1441 {
1442         int ret;
1443
1444         lock_kernel();
1445         ret = dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1446         unlock_kernel();
1447
1448         return ret;
1449 }
1450
1451 static int dvb_net_close(struct inode *inode, struct file *file)
1452 {
1453         struct dvb_device *dvbdev = file->private_data;
1454         struct dvb_net *dvbnet = dvbdev->priv;
1455
1456         dvb_generic_release(inode, file);
1457
1458         if(dvbdev->users == 1 && dvbnet->exit == 1) {
1459                 fops_put(file->f_op);
1460                 file->f_op = NULL;
1461                 wake_up(&dvbdev->wait_queue);
1462         }
1463         return 0;
1464 }
1465
1466
1467 static const struct file_operations dvb_net_fops = {
1468         .owner = THIS_MODULE,
1469         .unlocked_ioctl = dvb_net_ioctl,
1470         .open = dvb_generic_open,
1471         .release = dvb_net_close,
1472 };
1473
1474 static struct dvb_device dvbdev_net = {
1475         .priv = NULL,
1476         .users = 1,
1477         .writers = 1,
1478         .fops = &dvb_net_fops,
1479 };
1480
1481
1482 void dvb_net_release (struct dvb_net *dvbnet)
1483 {
1484         int i;
1485
1486         dvbnet->exit = 1;
1487         if (dvbnet->dvbdev->users < 1)
1488                 wait_event(dvbnet->dvbdev->wait_queue,
1489                                 dvbnet->dvbdev->users==1);
1490
1491         dvb_unregister_device(dvbnet->dvbdev);
1492
1493         for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1494                 if (!dvbnet->state[i])
1495                         continue;
1496                 dvb_net_remove_if(dvbnet, i);
1497         }
1498 }
1499 EXPORT_SYMBOL(dvb_net_release);
1500
1501
1502 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1503                   struct dmx_demux *dmx)
1504 {
1505         int i;
1506
1507         dvbnet->demux = dmx;
1508
1509         for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1510                 dvbnet->state[i] = 0;
1511
1512         dvb_register_device (adap, &dvbnet->dvbdev, &dvbdev_net,
1513                              dvbnet, DVB_DEVICE_NET);
1514
1515         return 0;
1516 }
1517 EXPORT_SYMBOL(dvb_net_init);