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