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