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