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