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