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