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