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