Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dvb_net.c
0004  *
0005  * Copyright (C) 2001 Convergence integrated media GmbH
0006  *                    Ralph Metzler <ralph@convergence.de>
0007  * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
0008  *
0009  * ULE Decapsulation code:
0010  * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH.
0011  *                      and Department of Scientific Computing
0012  *                          Paris Lodron University of Salzburg.
0013  *                          Hilmar Linder <hlinder@cosy.sbg.ac.at>
0014  *                      and Wolfram Stering <wstering@cosy.sbg.ac.at>
0015  *
0016  * ULE Decaps according to RFC 4326.
0017  */
0018 
0019 /*
0020  * ULE ChangeLog:
0021  * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt
0022  *
0023  * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt:
0024  *                       ULE Extension header handling.
0025  *                     Bugreports by Moritz Vieth and Hanno Tersteegen,
0026  *                       Fraunhofer Institute for Open Communication Systems
0027  *                       Competence Center for Advanced Satellite Communications.
0028  *                     Bugfixes and robustness improvements.
0029  *                     Filtering on dest MAC addresses, if present (D-Bit = 0)
0030  *                     DVB_ULE_DEBUG compile-time option.
0031  * Apr 2006: cp v3:    Bugfixes and compliency with RFC 4326 (ULE) by
0032  *                       Christian Praehauser <cpraehaus@cosy.sbg.ac.at>,
0033  *                       Paris Lodron University of Salzburg.
0034  */
0035 
0036 /*
0037  * FIXME / TODO (dvb_net.c):
0038  *
0039  * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
0040  *
0041  */
0042 
0043 #define pr_fmt(fmt) "dvb_net: " fmt
0044 
0045 #include <linux/module.h>
0046 #include <linux/kernel.h>
0047 #include <linux/netdevice.h>
0048 #include <linux/nospec.h>
0049 #include <linux/etherdevice.h>
0050 #include <linux/dvb/net.h>
0051 #include <linux/uio.h>
0052 #include <linux/uaccess.h>
0053 #include <linux/crc32.h>
0054 #include <linux/mutex.h>
0055 #include <linux/sched.h>
0056 
0057 #include <media/dvb_demux.h>
0058 #include <media/dvb_net.h>
0059 
0060 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
0061 {
0062     unsigned int j;
0063     for (j = 0; j < cnt; j++)
0064         c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
0065     return c;
0066 }
0067 
0068 
0069 #define DVB_NET_MULTICAST_MAX 10
0070 
0071 #ifdef DVB_ULE_DEBUG
0072 /*
0073  * The code inside DVB_ULE_DEBUG keeps a history of the
0074  * last 100 TS cells processed.
0075  */
0076 static unsigned char ule_hist[100*TS_SZ] = { 0 };
0077 static unsigned char *ule_where = ule_hist, ule_dump;
0078 
0079 static void hexdump(const unsigned char *buf, unsigned short len)
0080 {
0081     print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
0082 }
0083 #endif
0084 
0085 struct dvb_net_priv {
0086     int in_use;
0087     u16 pid;
0088     struct net_device *net;
0089     struct dvb_net *host;
0090     struct dmx_demux *demux;
0091     struct dmx_section_feed *secfeed;
0092     struct dmx_section_filter *secfilter;
0093     struct dmx_ts_feed *tsfeed;
0094     int multi_num;
0095     struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
0096     unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
0097     int rx_mode;
0098 #define RX_MODE_UNI 0
0099 #define RX_MODE_MULTI 1
0100 #define RX_MODE_ALL_MULTI 2
0101 #define RX_MODE_PROMISC 3
0102     struct work_struct set_multicast_list_wq;
0103     struct work_struct restart_net_feed_wq;
0104     unsigned char feedtype;         /* Either FEED_TYPE_ or FEED_TYPE_ULE */
0105     int need_pusi;              /* Set to 1, if synchronization on PUSI required. */
0106     unsigned char tscc;         /* TS continuity counter after sync on PUSI. */
0107     struct sk_buff *ule_skb;        /* ULE SNDU decodes into this buffer. */
0108     unsigned char *ule_next_hdr;        /* Pointer into skb to next ULE extension header. */
0109     unsigned short ule_sndu_len;        /* ULE SNDU length in bytes, w/o D-Bit. */
0110     unsigned short ule_sndu_type;       /* ULE SNDU type field, complete. */
0111     unsigned char ule_sndu_type_1;      /* ULE SNDU type field, if split across 2 TS cells. */
0112     unsigned char ule_dbit;         /* Whether the DestMAC address present
0113                          * or not (bit is set). */
0114     unsigned char ule_bridged;      /* Whether the ULE_BRIDGED extension header was found. */
0115     int ule_sndu_remain;            /* Nr. of bytes still required for current ULE SNDU. */
0116     unsigned long ts_count;         /* Current ts cell counter. */
0117     struct mutex mutex;
0118 };
0119 
0120 
0121 /*
0122  *  Determine the packet's protocol ID. The rule here is that we
0123  *  assume 802.3 if the type field is short enough to be a length.
0124  *  This is normal practice and works for any 'now in use' protocol.
0125  *
0126  *  stolen from eth.c out of the linux kernel, hacked for dvb-device
0127  *  by Michael Holzt <kju@debian.org>
0128  */
0129 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
0130                       struct net_device *dev)
0131 {
0132     struct ethhdr *eth;
0133     unsigned char *rawp;
0134 
0135     skb_reset_mac_header(skb);
0136     skb_pull(skb,dev->hard_header_len);
0137     eth = eth_hdr(skb);
0138 
0139     if (*eth->h_dest & 1) {
0140         if(ether_addr_equal(eth->h_dest,dev->broadcast))
0141             skb->pkt_type=PACKET_BROADCAST;
0142         else
0143             skb->pkt_type=PACKET_MULTICAST;
0144     }
0145 
0146     if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
0147         return eth->h_proto;
0148 
0149     rawp = skb->data;
0150 
0151     /*
0152      *  This is a magic hack to spot IPX packets. Older Novell breaks
0153      *  the protocol design and runs IPX over 802.3 without an 802.2 LLC
0154      *  layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
0155      *  won't work for fault tolerant netware but does for the rest.
0156      */
0157     if (*(unsigned short *)rawp == 0xFFFF)
0158         return htons(ETH_P_802_3);
0159 
0160     /*
0161      *  Real 802.2 LLC
0162      */
0163     return htons(ETH_P_802_2);
0164 }
0165 
0166 #define TS_SZ   188
0167 #define TS_SYNC 0x47
0168 #define TS_TEI  0x80
0169 #define TS_SC   0xC0
0170 #define TS_PUSI 0x40
0171 #define TS_AF_A 0x20
0172 #define TS_AF_D 0x10
0173 
0174 /* ULE Extension Header handlers. */
0175 
0176 #define ULE_TEST    0
0177 #define ULE_BRIDGED 1
0178 
0179 #define ULE_OPTEXTHDR_PADDING 0
0180 
0181 static int ule_test_sndu( struct dvb_net_priv *p )
0182 {
0183     return -1;
0184 }
0185 
0186 static int ule_bridged_sndu( struct dvb_net_priv *p )
0187 {
0188     struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
0189     if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
0190         int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
0191         /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
0192         if(framelen != ntohs(hdr->h_proto)) {
0193             return -1;
0194         }
0195     }
0196     /* Note:
0197      * From RFC4326:
0198      *  "A bridged SNDU is a Mandatory Extension Header of Type 1.
0199      *   It must be the final (or only) extension header specified in the header chain of a SNDU."
0200      * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
0201      */
0202     p->ule_bridged = 1;
0203     return 0;
0204 }
0205 
0206 static int ule_exthdr_padding(struct dvb_net_priv *p)
0207 {
0208     return 0;
0209 }
0210 
0211 /*
0212  * Handle ULE extension headers.
0213  *  Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
0214  *  Returns: >= 0: nr. of bytes consumed by next extension header
0215  *       -1:   Mandatory extension header that is not recognized or TEST SNDU; discard.
0216  */
0217 static int handle_one_ule_extension( struct dvb_net_priv *p )
0218 {
0219     /* Table of mandatory extension header handlers.  The header type is the index. */
0220     static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
0221         { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL,  };
0222 
0223     /* Table of optional extension header handlers.  The header type is the index. */
0224     static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
0225         { [0] = ule_exthdr_padding, [1] = NULL, };
0226 
0227     int ext_len = 0;
0228     unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
0229     unsigned char htype = p->ule_sndu_type & 0x00FF;
0230 
0231     /* Discriminate mandatory and optional extension headers. */
0232     if (hlen == 0) {
0233         /* Mandatory extension header */
0234         if (ule_mandatory_ext_handlers[htype]) {
0235             ext_len = ule_mandatory_ext_handlers[htype]( p );
0236             if(ext_len >= 0) {
0237                 p->ule_next_hdr += ext_len;
0238                 if (!p->ule_bridged) {
0239                     p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
0240                     p->ule_next_hdr += 2;
0241                 } else {
0242                     p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
0243                     /* This assures the extension handling loop will terminate. */
0244                 }
0245             }
0246             // else: extension handler failed or SNDU should be discarded
0247         } else
0248             ext_len = -1;   /* SNDU has to be discarded. */
0249     } else {
0250         /* Optional extension header.  Calculate the length. */
0251         ext_len = hlen << 1;
0252         /* Process the optional extension header according to its type. */
0253         if (ule_optional_ext_handlers[htype])
0254             (void)ule_optional_ext_handlers[htype]( p );
0255         p->ule_next_hdr += ext_len;
0256         p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
0257         /*
0258          * note: the length of the next header type is included in the
0259          * length of THIS optional extension header
0260          */
0261     }
0262 
0263     return ext_len;
0264 }
0265 
0266 static int handle_ule_extensions( struct dvb_net_priv *p )
0267 {
0268     int total_ext_len = 0, l;
0269 
0270     p->ule_next_hdr = p->ule_skb->data;
0271     do {
0272         l = handle_one_ule_extension( p );
0273         if (l < 0)
0274             return l;   /* Stop extension header processing and discard SNDU. */
0275         total_ext_len += l;
0276         pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
0277              p->ule_next_hdr, (int)p->ule_sndu_type,
0278              l, total_ext_len);
0279 
0280     } while (p->ule_sndu_type < ETH_P_802_3_MIN);
0281 
0282     return total_ext_len;
0283 }
0284 
0285 
0286 /* Prepare for a new ULE SNDU: reset the decoder state. */
0287 static inline void reset_ule( struct dvb_net_priv *p )
0288 {
0289     p->ule_skb = NULL;
0290     p->ule_next_hdr = NULL;
0291     p->ule_sndu_len = 0;
0292     p->ule_sndu_type = 0;
0293     p->ule_sndu_type_1 = 0;
0294     p->ule_sndu_remain = 0;
0295     p->ule_dbit = 0xFF;
0296     p->ule_bridged = 0;
0297 }
0298 
0299 /*
0300  * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
0301  * TS cells of a single PID.
0302  */
0303 
0304 struct dvb_net_ule_handle {
0305     struct net_device *dev;
0306     struct dvb_net_priv *priv;
0307     struct ethhdr *ethh;
0308     const u8 *buf;
0309     size_t buf_len;
0310     unsigned long skipped;
0311     const u8 *ts, *ts_end, *from_where;
0312     u8 ts_remain, how_much, new_ts;
0313     bool error;
0314 };
0315 
0316 static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h)
0317 {
0318     /* We are about to process a new TS cell. */
0319 
0320 #ifdef DVB_ULE_DEBUG
0321     if (ule_where >= &ule_hist[100*TS_SZ])
0322         ule_where = ule_hist;
0323     memcpy(ule_where, h->ts, TS_SZ);
0324     if (ule_dump) {
0325         hexdump(ule_where, TS_SZ);
0326         ule_dump = 0;
0327     }
0328     ule_where += TS_SZ;
0329 #endif
0330 
0331     /*
0332      * Check TS h->error conditions: sync_byte, transport_error_indicator,
0333      * scrambling_control .
0334      */
0335     if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) ||
0336         ((h->ts[3] & TS_SC) != 0)) {
0337         pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
0338             h->priv->ts_count, h->ts[0],
0339             (h->ts[1] & TS_TEI) >> 7,
0340             (h->ts[3] & TS_SC) >> 6);
0341 
0342         /* Drop partly decoded SNDU, reset state, resync on PUSI. */
0343         if (h->priv->ule_skb) {
0344             dev_kfree_skb(h->priv->ule_skb);
0345             /* Prepare for next SNDU. */
0346             h->dev->stats.rx_errors++;
0347             h->dev->stats.rx_frame_errors++;
0348         }
0349         reset_ule(h->priv);
0350         h->priv->need_pusi = 1;
0351 
0352         /* Continue with next TS cell. */
0353         h->ts += TS_SZ;
0354         h->priv->ts_count++;
0355         return 1;
0356     }
0357 
0358     h->ts_remain = 184;
0359     h->from_where = h->ts + 4;
0360 
0361     return 0;
0362 }
0363 
0364 static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h)
0365 {
0366     if (h->ts[1] & TS_PUSI) {
0367         /* Find beginning of first ULE SNDU in current TS cell. */
0368         /* Synchronize continuity counter. */
0369         h->priv->tscc = h->ts[3] & 0x0F;
0370         /* There is a pointer field here. */
0371         if (h->ts[4] > h->ts_remain) {
0372             pr_err("%lu: Invalid ULE packet (pointer field %d)\n",
0373                 h->priv->ts_count, h->ts[4]);
0374             h->ts += TS_SZ;
0375             h->priv->ts_count++;
0376             return 1;
0377         }
0378         /* Skip to destination of pointer field. */
0379         h->from_where = &h->ts[5] + h->ts[4];
0380         h->ts_remain -= 1 + h->ts[4];
0381         h->skipped = 0;
0382     } else {
0383         h->skipped++;
0384         h->ts += TS_SZ;
0385         h->priv->ts_count++;
0386         return 1;
0387     }
0388 
0389     return 0;
0390 }
0391 
0392 static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h)
0393 {
0394     /* Check continuity counter. */
0395     if ((h->ts[3] & 0x0F) == h->priv->tscc)
0396         h->priv->tscc = (h->priv->tscc + 1) & 0x0F;
0397     else {
0398         /* TS discontinuity handling: */
0399         pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n",
0400             h->priv->ts_count, h->ts[3] & 0x0F,
0401             h->priv->tscc);
0402         /* Drop partly decoded SNDU, reset state, resync on PUSI. */
0403         if (h->priv->ule_skb) {
0404             dev_kfree_skb(h->priv->ule_skb);
0405             /* Prepare for next SNDU. */
0406             // reset_ule(h->priv);  moved to below.
0407             h->dev->stats.rx_errors++;
0408             h->dev->stats.rx_frame_errors++;
0409         }
0410         reset_ule(h->priv);
0411         /* skip to next PUSI. */
0412         h->priv->need_pusi = 1;
0413         return 1;
0414     }
0415     /*
0416      * If we still have an incomplete payload, but PUSI is
0417      * set; some TS cells are missing.
0418      * This is only possible here, if we missed exactly 16 TS
0419      * cells (continuity counter wrap).
0420      */
0421     if (h->ts[1] & TS_PUSI) {
0422         if (!h->priv->need_pusi) {
0423             if (!(*h->from_where < (h->ts_remain-1)) ||
0424                 *h->from_where != h->priv->ule_sndu_remain) {
0425                 /*
0426                  * Pointer field is invalid.
0427                  * Drop this TS cell and any started ULE SNDU.
0428                  */
0429                 pr_warn("%lu: Invalid pointer field: %u.\n",
0430                     h->priv->ts_count,
0431                     *h->from_where);
0432 
0433                 /*
0434                  * Drop partly decoded SNDU, reset state,
0435                  * resync on PUSI.
0436                  */
0437                 if (h->priv->ule_skb) {
0438                     h->error = true;
0439                     dev_kfree_skb(h->priv->ule_skb);
0440                 }
0441 
0442                 if (h->error || h->priv->ule_sndu_remain) {
0443                     h->dev->stats.rx_errors++;
0444                     h->dev->stats.rx_frame_errors++;
0445                     h->error = false;
0446                 }
0447 
0448                 reset_ule(h->priv);
0449                 h->priv->need_pusi = 1;
0450                 return 1;
0451             }
0452             /*
0453              * Skip pointer field (we're processing a
0454              * packed payload).
0455              */
0456             h->from_where += 1;
0457             h->ts_remain -= 1;
0458         } else
0459             h->priv->need_pusi = 0;
0460 
0461         if (h->priv->ule_sndu_remain > 183) {
0462             /*
0463              * Current SNDU lacks more data than there
0464              * could be available in the current TS cell.
0465              */
0466             h->dev->stats.rx_errors++;
0467             h->dev->stats.rx_length_errors++;
0468             pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d).  Flushing incomplete payload.\n",
0469                 h->priv->ts_count,
0470                 h->priv->ule_sndu_remain,
0471                 h->ts[4], h->ts_remain);
0472             dev_kfree_skb(h->priv->ule_skb);
0473             /* Prepare for next SNDU. */
0474             reset_ule(h->priv);
0475             /*
0476              * Resync: go to where pointer field points to:
0477              * start of next ULE SNDU.
0478              */
0479             h->from_where += h->ts[4];
0480             h->ts_remain -= h->ts[4];
0481         }
0482     }
0483     return 0;
0484 }
0485 
0486 
0487 /*
0488  * Start a new payload with skb.
0489  * Find ULE header.  It is only guaranteed that the
0490  * length field (2 bytes) is contained in the current
0491  * TS.
0492  * Check h.ts_remain has to be >= 2 here.
0493  */
0494 static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h)
0495 {
0496     if (h->ts_remain < 2) {
0497         pr_warn("Invalid payload packing: only %d bytes left in TS.  Resyncing.\n",
0498             h->ts_remain);
0499         h->priv->ule_sndu_len = 0;
0500         h->priv->need_pusi = 1;
0501         h->ts += TS_SZ;
0502         return 1;
0503     }
0504 
0505     if (!h->priv->ule_sndu_len) {
0506         /* Got at least two bytes, thus extrace the SNDU length. */
0507         h->priv->ule_sndu_len = h->from_where[0] << 8 |
0508                     h->from_where[1];
0509         if (h->priv->ule_sndu_len & 0x8000) {
0510             /* D-Bit is set: no dest mac present. */
0511             h->priv->ule_sndu_len &= 0x7FFF;
0512             h->priv->ule_dbit = 1;
0513         } else
0514             h->priv->ule_dbit = 0;
0515 
0516         if (h->priv->ule_sndu_len < 5) {
0517             pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n",
0518                 h->priv->ts_count,
0519                 h->priv->ule_sndu_len);
0520             h->dev->stats.rx_errors++;
0521             h->dev->stats.rx_length_errors++;
0522             h->priv->ule_sndu_len = 0;
0523             h->priv->need_pusi = 1;
0524             h->new_ts = 1;
0525             h->ts += TS_SZ;
0526             h->priv->ts_count++;
0527             return 1;
0528         }
0529         h->ts_remain -= 2;  /* consume the 2 bytes SNDU length. */
0530         h->from_where += 2;
0531     }
0532 
0533     h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2;
0534     /*
0535      * State of current TS:
0536      *   h->ts_remain (remaining bytes in the current TS cell)
0537      *   0  ule_type is not available now, we need the next TS cell
0538      *   1  the first byte of the ule_type is present
0539      * >=2  full ULE header present, maybe some payload data as well.
0540      */
0541     switch (h->ts_remain) {
0542     case 1:
0543         h->priv->ule_sndu_remain--;
0544         h->priv->ule_sndu_type = h->from_where[0] << 8;
0545 
0546         /* first byte of ule_type is set. */
0547         h->priv->ule_sndu_type_1 = 1;
0548         h->ts_remain -= 1;
0549         h->from_where += 1;
0550         fallthrough;
0551     case 0:
0552         h->new_ts = 1;
0553         h->ts += TS_SZ;
0554         h->priv->ts_count++;
0555         return 1;
0556 
0557     default: /* complete ULE header is present in current TS. */
0558         /* Extract ULE type field. */
0559         if (h->priv->ule_sndu_type_1) {
0560             h->priv->ule_sndu_type_1 = 0;
0561             h->priv->ule_sndu_type |= h->from_where[0];
0562             h->from_where += 1; /* points to payload start. */
0563             h->ts_remain -= 1;
0564         } else {
0565             /* Complete type is present in new TS. */
0566             h->priv->ule_sndu_type = h->from_where[0] << 8 |
0567                          h->from_where[1];
0568             h->from_where += 2; /* points to payload start. */
0569             h->ts_remain -= 2;
0570         }
0571         break;
0572     }
0573 
0574     /*
0575      * Allocate the skb (decoder target buffer) with the correct size,
0576      * as follows:
0577      *
0578      * prepare for the largest case: bridged SNDU with MAC address
0579      * (dbit = 0).
0580      */
0581     h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len +
0582                      ETH_HLEN + ETH_ALEN);
0583     if (!h->priv->ule_skb) {
0584         pr_notice("%s: Memory squeeze, dropping packet.\n",
0585               h->dev->name);
0586         h->dev->stats.rx_dropped++;
0587         return -1;
0588     }
0589 
0590     /* This includes the CRC32 _and_ dest mac, if !dbit. */
0591     h->priv->ule_sndu_remain = h->priv->ule_sndu_len;
0592     h->priv->ule_skb->dev = h->dev;
0593     /*
0594      * Leave space for Ethernet or bridged SNDU header
0595      * (eth hdr plus one MAC addr).
0596      */
0597     skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN);
0598 
0599     return 0;
0600 }
0601 
0602 
0603 static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h)
0604 {
0605     static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff };
0606 
0607     /*
0608      * The destination MAC address is the next data in the skb.  It comes
0609      * before any extension headers.
0610      *
0611      * Check if the payload of this SNDU should be passed up the stack.
0612      */
0613     if (h->priv->rx_mode == RX_MODE_PROMISC)
0614         return 0;
0615 
0616     if (h->priv->ule_skb->data[0] & 0x01) {
0617         /* multicast or broadcast */
0618         if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) {
0619             /* multicast */
0620             if (h->priv->rx_mode == RX_MODE_MULTI) {
0621                 int i;
0622 
0623                 for (i = 0; i < h->priv->multi_num &&
0624                      !ether_addr_equal(h->priv->ule_skb->data,
0625                                h->priv->multi_macs[i]);
0626                      i++)
0627                     ;
0628                 if (i == h->priv->multi_num)
0629                     return 1;
0630             } else if (h->priv->rx_mode != RX_MODE_ALL_MULTI)
0631                 return 1; /* no broadcast; */
0632             /*
0633              * else:
0634              * all multicast mode: accept all multicast packets
0635              */
0636         }
0637         /* else: broadcast */
0638     } else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr))
0639         return 1;
0640 
0641     return 0;
0642 }
0643 
0644 
0645 static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h,
0646                   struct kvec iov[3],
0647                   u32 ule_crc, u32 expected_crc)
0648 {
0649     u8 dest_addr[ETH_ALEN];
0650 
0651     if (ule_crc != expected_crc) {
0652         pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
0653             h->priv->ts_count, ule_crc, expected_crc,
0654             h->priv->ule_sndu_len, h->priv->ule_sndu_type,
0655             h->ts_remain,
0656             h->ts_remain > 2 ?
0657                 *(unsigned short *)h->from_where : 0);
0658 
0659     #ifdef DVB_ULE_DEBUG
0660         hexdump(iov[0].iov_base, iov[0].iov_len);
0661         hexdump(iov[1].iov_base, iov[1].iov_len);
0662         hexdump(iov[2].iov_base, iov[2].iov_len);
0663 
0664         if (ule_where == ule_hist) {
0665             hexdump(&ule_hist[98*TS_SZ], TS_SZ);
0666             hexdump(&ule_hist[99*TS_SZ], TS_SZ);
0667         } else if (ule_where == &ule_hist[TS_SZ]) {
0668             hexdump(&ule_hist[99*TS_SZ], TS_SZ);
0669             hexdump(ule_hist, TS_SZ);
0670         } else {
0671             hexdump(ule_where - TS_SZ - TS_SZ, TS_SZ);
0672             hexdump(ule_where - TS_SZ, TS_SZ);
0673         }
0674         ule_dump = 1;
0675     #endif
0676 
0677         h->dev->stats.rx_errors++;
0678         h->dev->stats.rx_crc_errors++;
0679         dev_kfree_skb(h->priv->ule_skb);
0680 
0681         return;
0682     }
0683 
0684     /* CRC32 verified OK. */
0685 
0686     /* CRC32 was OK, so remove it from skb. */
0687     h->priv->ule_skb->tail -= 4;
0688     h->priv->ule_skb->len -= 4;
0689 
0690     if (!h->priv->ule_dbit) {
0691         if (dvb_net_ule_should_drop(h)) {
0692             netdev_dbg(h->dev,
0693                    "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n",
0694                    h->priv->ule_skb->data, h->dev->dev_addr);
0695             dev_kfree_skb(h->priv->ule_skb);
0696             return;
0697         }
0698 
0699         skb_copy_from_linear_data(h->priv->ule_skb, dest_addr,
0700                       ETH_ALEN);
0701         skb_pull(h->priv->ule_skb, ETH_ALEN);
0702     } else {
0703         /* dest_addr buffer is only valid if h->priv->ule_dbit == 0 */
0704         eth_zero_addr(dest_addr);
0705     }
0706 
0707     /* Handle ULE Extension Headers. */
0708     if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) {
0709         /* There is an extension header.  Handle it accordingly. */
0710         int l = handle_ule_extensions(h->priv);
0711 
0712         if (l < 0) {
0713             /*
0714              * Mandatory extension header unknown or TEST SNDU.
0715              * Drop it.
0716              */
0717 
0718             // pr_warn("Dropping SNDU, extension headers.\n" );
0719             dev_kfree_skb(h->priv->ule_skb);
0720             return;
0721         }
0722         skb_pull(h->priv->ule_skb, l);
0723     }
0724 
0725     /*
0726      * Construct/assure correct ethernet header.
0727      * Note: in bridged mode (h->priv->ule_bridged != 0)
0728      * we already have the (original) ethernet
0729      * header at the start of the payload (after
0730      * optional dest. address and any extension
0731      * headers).
0732      */
0733     if (!h->priv->ule_bridged) {
0734         skb_push(h->priv->ule_skb, ETH_HLEN);
0735         h->ethh = (struct ethhdr *)h->priv->ule_skb->data;
0736         memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN);
0737         eth_zero_addr(h->ethh->h_source);
0738         h->ethh->h_proto = htons(h->priv->ule_sndu_type);
0739     }
0740     /* else:  skb is in correct state; nothing to do. */
0741     h->priv->ule_bridged = 0;
0742 
0743     /* Stuff into kernel's protocol stack. */
0744     h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb,
0745                                h->dev);
0746     /*
0747      * If D-bit is set (i.e. destination MAC address not present),
0748      * receive the packet anyhow.
0749      */
0750 #if 0
0751     if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
0752         h->priv->ule_skb->pkt_type = PACKET_HOST;
0753 #endif
0754     h->dev->stats.rx_packets++;
0755     h->dev->stats.rx_bytes += h->priv->ule_skb->len;
0756     netif_rx(h->priv->ule_skb);
0757 }
0758 
0759 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len)
0760 {
0761     int ret;
0762     struct dvb_net_ule_handle h = {
0763         .dev = dev,
0764         .priv = netdev_priv(dev),
0765         .ethh = NULL,
0766         .buf = buf,
0767         .buf_len = buf_len,
0768         .skipped = 0L,
0769         .ts = NULL,
0770         .ts_end = NULL,
0771         .from_where = NULL,
0772         .ts_remain = 0,
0773         .how_much = 0,
0774         .new_ts = 1,
0775         .error = false,
0776     };
0777 
0778     /*
0779      * For all TS cells in current buffer.
0780      * Appearently, we are called for every single TS cell.
0781      */
0782     for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len;
0783          h.ts < h.ts_end; /* no incr. */) {
0784         if (h.new_ts) {
0785             /* We are about to process a new TS cell. */
0786             if (dvb_net_ule_new_ts_cell(&h))
0787                 continue;
0788         }
0789 
0790         /* Synchronize on PUSI, if required. */
0791         if (h.priv->need_pusi) {
0792             if (dvb_net_ule_ts_pusi(&h))
0793                 continue;
0794         }
0795 
0796         if (h.new_ts) {
0797             if (dvb_net_ule_new_ts(&h))
0798                 continue;
0799         }
0800 
0801         /* Check if new payload needs to be started. */
0802         if (h.priv->ule_skb == NULL) {
0803             ret = dvb_net_ule_new_payload(&h);
0804             if (ret < 0)
0805                 return;
0806             if (ret)
0807                 continue;
0808         }
0809 
0810         /* Copy data into our current skb. */
0811         h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain);
0812         skb_put_data(h.priv->ule_skb, h.from_where, h.how_much);
0813         h.priv->ule_sndu_remain -= h.how_much;
0814         h.ts_remain -= h.how_much;
0815         h.from_where += h.how_much;
0816 
0817         /* Check for complete payload. */
0818         if (h.priv->ule_sndu_remain <= 0) {
0819             /* Check CRC32, we've got it in our skb already. */
0820             __be16 ulen = htons(h.priv->ule_sndu_len);
0821             __be16 utype = htons(h.priv->ule_sndu_type);
0822             const u8 *tail;
0823             struct kvec iov[3] = {
0824                 { &ulen, sizeof ulen },
0825                 { &utype, sizeof utype },
0826                 { h.priv->ule_skb->data,
0827                   h.priv->ule_skb->len - 4 }
0828             };
0829             u32 ule_crc = ~0L, expected_crc;
0830             if (h.priv->ule_dbit) {
0831                 /* Set D-bit for CRC32 verification,
0832                  * if it was set originally. */
0833                 ulen |= htons(0x8000);
0834             }
0835 
0836             ule_crc = iov_crc32(ule_crc, iov, 3);
0837             tail = skb_tail_pointer(h.priv->ule_skb);
0838             expected_crc = *(tail - 4) << 24 |
0839                        *(tail - 3) << 16 |
0840                        *(tail - 2) << 8 |
0841                        *(tail - 1);
0842 
0843             dvb_net_ule_check_crc(&h, iov, ule_crc, expected_crc);
0844 
0845             /* Prepare for next SNDU. */
0846             reset_ule(h.priv);
0847         }
0848 
0849         /* More data in current TS (look at the bytes following the CRC32)? */
0850         if (h.ts_remain >= 2 && *((unsigned short *)h.from_where) != 0xFFFF) {
0851             /* Next ULE SNDU starts right there. */
0852             h.new_ts = 0;
0853             h.priv->ule_skb = NULL;
0854             h.priv->ule_sndu_type_1 = 0;
0855             h.priv->ule_sndu_len = 0;
0856             // pr_warn("More data in current TS: [%#x %#x %#x %#x]\n",
0857             //  *(h.from_where + 0), *(h.from_where + 1),
0858             //  *(h.from_where + 2), *(h.from_where + 3));
0859             // pr_warn("h.ts @ %p, stopped @ %p:\n", h.ts, h.from_where + 0);
0860             // hexdump(h.ts, 188);
0861         } else {
0862             h.new_ts = 1;
0863             h.ts += TS_SZ;
0864             h.priv->ts_count++;
0865             if (h.priv->ule_skb == NULL) {
0866                 h.priv->need_pusi = 1;
0867                 h.priv->ule_sndu_type_1 = 0;
0868                 h.priv->ule_sndu_len = 0;
0869             }
0870         }
0871     }   /* for all available TS cells */
0872 }
0873 
0874 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
0875                    const u8 *buffer2, size_t buffer2_len,
0876                    struct dmx_ts_feed *feed,
0877                    u32 *buffer_flags)
0878 {
0879     struct net_device *dev = feed->priv;
0880 
0881     if (buffer2)
0882         pr_warn("buffer2 not NULL: %p.\n", buffer2);
0883     if (buffer1_len > 32768)
0884         pr_warn("length > 32k: %zu.\n", buffer1_len);
0885     /* pr_info("TS callback: %u bytes, %u TS cells @ %p.\n",
0886           buffer1_len, buffer1_len / TS_SZ, buffer1); */
0887     dvb_net_ule(dev, buffer1, buffer1_len);
0888     return 0;
0889 }
0890 
0891 
0892 static void dvb_net_sec(struct net_device *dev,
0893             const u8 *pkt, int pkt_len)
0894 {
0895     u8 *eth;
0896     struct sk_buff *skb;
0897     struct net_device_stats *stats = &dev->stats;
0898     int snap = 0;
0899 
0900     /* note: pkt_len includes a 32bit checksum */
0901     if (pkt_len < 16) {
0902         pr_warn("%s: IP/MPE packet length = %d too small.\n",
0903             dev->name, pkt_len);
0904         stats->rx_errors++;
0905         stats->rx_length_errors++;
0906         return;
0907     }
0908 /* it seems some ISPs manage to screw up here, so we have to
0909  * relax the error checks... */
0910 #if 0
0911     if ((pkt[5] & 0xfd) != 0xc1) {
0912         /* drop scrambled or broken packets */
0913 #else
0914     if ((pkt[5] & 0x3c) != 0x00) {
0915         /* drop scrambled */
0916 #endif
0917         stats->rx_errors++;
0918         stats->rx_crc_errors++;
0919         return;
0920     }
0921     if (pkt[5] & 0x02) {
0922         /* handle LLC/SNAP, see rfc-1042 */
0923         if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
0924             stats->rx_dropped++;
0925             return;
0926         }
0927         snap = 8;
0928     }
0929     if (pkt[7]) {
0930         /* FIXME: assemble datagram from multiple sections */
0931         stats->rx_errors++;
0932         stats->rx_frame_errors++;
0933         return;
0934     }
0935 
0936     /* we have 14 byte ethernet header (ip header follows);
0937      * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
0938      */
0939     if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
0940         //pr_notice("%s: Memory squeeze, dropping packet.\n", dev->name);
0941         stats->rx_dropped++;
0942         return;
0943     }
0944     skb_reserve(skb, 2);    /* longword align L3 header */
0945     skb->dev = dev;
0946 
0947     /* copy L3 payload */
0948     eth = skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
0949     memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
0950 
0951     /* create ethernet header: */
0952     eth[0]=pkt[0x0b];
0953     eth[1]=pkt[0x0a];
0954     eth[2]=pkt[0x09];
0955     eth[3]=pkt[0x08];
0956     eth[4]=pkt[0x04];
0957     eth[5]=pkt[0x03];
0958 
0959     eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
0960 
0961     if (snap) {
0962         eth[12] = pkt[18];
0963         eth[13] = pkt[19];
0964     } else {
0965         /* protocol numbers are from rfc-1700 or
0966          * http://www.iana.org/assignments/ethernet-numbers
0967          */
0968         if (pkt[12] >> 4 == 6) { /* version field from IP header */
0969             eth[12] = 0x86; /* IPv6 */
0970             eth[13] = 0xdd;
0971         } else {
0972             eth[12] = 0x08; /* IPv4 */
0973             eth[13] = 0x00;
0974         }
0975     }
0976 
0977     skb->protocol = dvb_net_eth_type_trans(skb, dev);
0978 
0979     stats->rx_packets++;
0980     stats->rx_bytes+=skb->len;
0981     netif_rx(skb);
0982 }
0983 
0984 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
0985          const u8 *buffer2, size_t buffer2_len,
0986          struct dmx_section_filter *filter, u32 *buffer_flags)
0987 {
0988     struct net_device *dev = filter->priv;
0989 
0990     /*
0991      * we rely on the DVB API definition where exactly one complete
0992      * section is delivered in buffer1
0993      */
0994     dvb_net_sec (dev, buffer1, buffer1_len);
0995     return 0;
0996 }
0997 
0998 static netdev_tx_t dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
0999 {
1000     dev_kfree_skb(skb);
1001     return NETDEV_TX_OK;
1002 }
1003 
1004 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1005 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
1006 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
1007 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1008 
1009 static int dvb_net_filter_sec_set(struct net_device *dev,
1010            struct dmx_section_filter **secfilter,
1011            const u8 *mac, u8 *mac_mask)
1012 {
1013     struct dvb_net_priv *priv = netdev_priv(dev);
1014     int ret;
1015 
1016     *secfilter=NULL;
1017     ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
1018     if (ret<0) {
1019         pr_err("%s: could not get filter\n", dev->name);
1020         return ret;
1021     }
1022 
1023     (*secfilter)->priv=(void *) dev;
1024 
1025     memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
1026     memset((*secfilter)->filter_mask,  0x00, DMX_MAX_FILTER_SIZE);
1027     memset((*secfilter)->filter_mode,  0xff, DMX_MAX_FILTER_SIZE);
1028 
1029     (*secfilter)->filter_value[0]=0x3e;
1030     (*secfilter)->filter_value[3]=mac[5];
1031     (*secfilter)->filter_value[4]=mac[4];
1032     (*secfilter)->filter_value[8]=mac[3];
1033     (*secfilter)->filter_value[9]=mac[2];
1034     (*secfilter)->filter_value[10]=mac[1];
1035     (*secfilter)->filter_value[11]=mac[0];
1036 
1037     (*secfilter)->filter_mask[0] = 0xff;
1038     (*secfilter)->filter_mask[3] = mac_mask[5];
1039     (*secfilter)->filter_mask[4] = mac_mask[4];
1040     (*secfilter)->filter_mask[8] = mac_mask[3];
1041     (*secfilter)->filter_mask[9] = mac_mask[2];
1042     (*secfilter)->filter_mask[10] = mac_mask[1];
1043     (*secfilter)->filter_mask[11]=mac_mask[0];
1044 
1045     netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
1046 
1047     return 0;
1048 }
1049 
1050 static int dvb_net_feed_start(struct net_device *dev)
1051 {
1052     int ret = 0, i;
1053     struct dvb_net_priv *priv = netdev_priv(dev);
1054     struct dmx_demux *demux = priv->demux;
1055     const unsigned char *mac = (const unsigned char *) dev->dev_addr;
1056 
1057     netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode);
1058     mutex_lock(&priv->mutex);
1059     if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
1060         pr_err("%s: BUG %d\n", __func__, __LINE__);
1061 
1062     priv->secfeed=NULL;
1063     priv->secfilter=NULL;
1064     priv->tsfeed = NULL;
1065 
1066     if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1067         netdev_dbg(dev, "alloc secfeed\n");
1068         ret=demux->allocate_section_feed(demux, &priv->secfeed,
1069                      dvb_net_sec_callback);
1070         if (ret<0) {
1071             pr_err("%s: could not allocate section feed\n",
1072                    dev->name);
1073             goto error;
1074         }
1075 
1076         ret = priv->secfeed->set(priv->secfeed, priv->pid, 1);
1077 
1078         if (ret<0) {
1079             pr_err("%s: could not set section feed\n", dev->name);
1080             priv->demux->release_section_feed(priv->demux, priv->secfeed);
1081             priv->secfeed=NULL;
1082             goto error;
1083         }
1084 
1085         if (priv->rx_mode != RX_MODE_PROMISC) {
1086             netdev_dbg(dev, "set secfilter\n");
1087             dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
1088         }
1089 
1090         switch (priv->rx_mode) {
1091         case RX_MODE_MULTI:
1092             for (i = 0; i < priv->multi_num; i++) {
1093                 netdev_dbg(dev, "set multi_secfilter[%d]\n", i);
1094                 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1095                                priv->multi_macs[i], mask_normal);
1096             }
1097             break;
1098         case RX_MODE_ALL_MULTI:
1099             priv->multi_num=1;
1100             netdev_dbg(dev, "set multi_secfilter[0]\n");
1101             dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1102                            mac_allmulti, mask_allmulti);
1103             break;
1104         case RX_MODE_PROMISC:
1105             priv->multi_num=0;
1106             netdev_dbg(dev, "set secfilter\n");
1107             dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1108             break;
1109         }
1110 
1111         netdev_dbg(dev, "start filtering\n");
1112         priv->secfeed->start_filtering(priv->secfeed);
1113     } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1114         ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
1115 
1116         /* we have payloads encapsulated in TS */
1117         netdev_dbg(dev, "alloc tsfeed\n");
1118         ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1119         if (ret < 0) {
1120             pr_err("%s: could not allocate ts feed\n", dev->name);
1121             goto error;
1122         }
1123 
1124         /* Set netdevice pointer for ts decaps callback. */
1125         priv->tsfeed->priv = (void *)dev;
1126         ret = priv->tsfeed->set(priv->tsfeed,
1127                     priv->pid, /* pid */
1128                     TS_PACKET, /* type */
1129                     DMX_PES_OTHER, /* pes type */
1130                     timeout    /* timeout */
1131                     );
1132 
1133         if (ret < 0) {
1134             pr_err("%s: could not set ts feed\n", dev->name);
1135             priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1136             priv->tsfeed = NULL;
1137             goto error;
1138         }
1139 
1140         netdev_dbg(dev, "start filtering\n");
1141         priv->tsfeed->start_filtering(priv->tsfeed);
1142     } else
1143         ret = -EINVAL;
1144 
1145 error:
1146     mutex_unlock(&priv->mutex);
1147     return ret;
1148 }
1149 
1150 static int dvb_net_feed_stop(struct net_device *dev)
1151 {
1152     struct dvb_net_priv *priv = netdev_priv(dev);
1153     int i, ret = 0;
1154 
1155     mutex_lock(&priv->mutex);
1156     if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1157         if (priv->secfeed) {
1158             if (priv->secfeed->is_filtering) {
1159                 netdev_dbg(dev, "stop secfeed\n");
1160                 priv->secfeed->stop_filtering(priv->secfeed);
1161             }
1162 
1163             if (priv->secfilter) {
1164                 netdev_dbg(dev, "release secfilter\n");
1165                 priv->secfeed->release_filter(priv->secfeed,
1166                                   priv->secfilter);
1167                 priv->secfilter=NULL;
1168             }
1169 
1170             for (i=0; i<priv->multi_num; i++) {
1171                 if (priv->multi_secfilter[i]) {
1172                     netdev_dbg(dev, "release multi_filter[%d]\n",
1173                            i);
1174                     priv->secfeed->release_filter(priv->secfeed,
1175                                       priv->multi_secfilter[i]);
1176                     priv->multi_secfilter[i] = NULL;
1177                 }
1178             }
1179 
1180             priv->demux->release_section_feed(priv->demux, priv->secfeed);
1181             priv->secfeed = NULL;
1182         } else
1183             pr_err("%s: no feed to stop\n", dev->name);
1184     } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1185         if (priv->tsfeed) {
1186             if (priv->tsfeed->is_filtering) {
1187                 netdev_dbg(dev, "stop tsfeed\n");
1188                 priv->tsfeed->stop_filtering(priv->tsfeed);
1189             }
1190             priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1191             priv->tsfeed = NULL;
1192         }
1193         else
1194             pr_err("%s: no ts feed to stop\n", dev->name);
1195     } else
1196         ret = -EINVAL;
1197     mutex_unlock(&priv->mutex);
1198     return ret;
1199 }
1200 
1201 
1202 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
1203 {
1204     struct dvb_net_priv *priv = netdev_priv(dev);
1205 
1206     if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1207         return -ENOMEM;
1208 
1209     memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
1210 
1211     priv->multi_num++;
1212     return 0;
1213 }
1214 
1215 
1216 static void wq_set_multicast_list (struct work_struct *work)
1217 {
1218     struct dvb_net_priv *priv =
1219         container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1220     struct net_device *dev = priv->net;
1221 
1222     dvb_net_feed_stop(dev);
1223     priv->rx_mode = RX_MODE_UNI;
1224     netif_addr_lock_bh(dev);
1225 
1226     if (dev->flags & IFF_PROMISC) {
1227         netdev_dbg(dev, "promiscuous mode\n");
1228         priv->rx_mode = RX_MODE_PROMISC;
1229     } else if ((dev->flags & IFF_ALLMULTI)) {
1230         netdev_dbg(dev, "allmulti mode\n");
1231         priv->rx_mode = RX_MODE_ALL_MULTI;
1232     } else if (!netdev_mc_empty(dev)) {
1233         struct netdev_hw_addr *ha;
1234 
1235         netdev_dbg(dev, "set_mc_list, %d entries\n",
1236                netdev_mc_count(dev));
1237 
1238         priv->rx_mode = RX_MODE_MULTI;
1239         priv->multi_num = 0;
1240 
1241         netdev_for_each_mc_addr(ha, dev)
1242             dvb_set_mc_filter(dev, ha->addr);
1243     }
1244 
1245     netif_addr_unlock_bh(dev);
1246     dvb_net_feed_start(dev);
1247 }
1248 
1249 
1250 static void dvb_net_set_multicast_list (struct net_device *dev)
1251 {
1252     struct dvb_net_priv *priv = netdev_priv(dev);
1253     schedule_work(&priv->set_multicast_list_wq);
1254 }
1255 
1256 
1257 static void wq_restart_net_feed (struct work_struct *work)
1258 {
1259     struct dvb_net_priv *priv =
1260         container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1261     struct net_device *dev = priv->net;
1262 
1263     if (netif_running(dev)) {
1264         dvb_net_feed_stop(dev);
1265         dvb_net_feed_start(dev);
1266     }
1267 }
1268 
1269 
1270 static int dvb_net_set_mac (struct net_device *dev, void *p)
1271 {
1272     struct dvb_net_priv *priv = netdev_priv(dev);
1273     struct sockaddr *addr=p;
1274 
1275     eth_hw_addr_set(dev, addr->sa_data);
1276 
1277     if (netif_running(dev))
1278         schedule_work(&priv->restart_net_feed_wq);
1279 
1280     return 0;
1281 }
1282 
1283 
1284 static int dvb_net_open(struct net_device *dev)
1285 {
1286     struct dvb_net_priv *priv = netdev_priv(dev);
1287 
1288     priv->in_use++;
1289     dvb_net_feed_start(dev);
1290     return 0;
1291 }
1292 
1293 
1294 static int dvb_net_stop(struct net_device *dev)
1295 {
1296     struct dvb_net_priv *priv = netdev_priv(dev);
1297 
1298     priv->in_use--;
1299     return dvb_net_feed_stop(dev);
1300 }
1301 
1302 static const struct header_ops dvb_header_ops = {
1303     .create     = eth_header,
1304     .parse      = eth_header_parse,
1305 };
1306 
1307 
1308 static const struct net_device_ops dvb_netdev_ops = {
1309     .ndo_open       = dvb_net_open,
1310     .ndo_stop       = dvb_net_stop,
1311     .ndo_start_xmit     = dvb_net_tx,
1312     .ndo_set_rx_mode    = dvb_net_set_multicast_list,
1313     .ndo_set_mac_address    = dvb_net_set_mac,
1314     .ndo_validate_addr  = eth_validate_addr,
1315 };
1316 
1317 static void dvb_net_setup(struct net_device *dev)
1318 {
1319     ether_setup(dev);
1320 
1321     dev->header_ops     = &dvb_header_ops;
1322     dev->netdev_ops     = &dvb_netdev_ops;
1323     dev->mtu        = 4096;
1324     dev->max_mtu        = 4096;
1325 
1326     dev->flags |= IFF_NOARP;
1327 }
1328 
1329 static int get_if(struct dvb_net *dvbnet)
1330 {
1331     int i;
1332 
1333     for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1334         if (!dvbnet->state[i])
1335             break;
1336 
1337     if (i == DVB_NET_DEVICES_MAX)
1338         return -1;
1339 
1340     dvbnet->state[i]=1;
1341     return i;
1342 }
1343 
1344 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1345 {
1346     struct net_device *net;
1347     struct dvb_net_priv *priv;
1348     int result;
1349     int if_num;
1350 
1351     if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1352         return -EINVAL;
1353     if ((if_num = get_if(dvbnet)) < 0)
1354         return -EINVAL;
1355 
1356     net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1357                NET_NAME_UNKNOWN, dvb_net_setup);
1358     if (!net)
1359         return -ENOMEM;
1360 
1361     if (dvbnet->dvbdev->id)
1362         snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1363              dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1364     else
1365         /* compatibility fix to keep dvb0_0 format */
1366         snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1367              dvbnet->dvbdev->adapter->num, if_num);
1368 
1369     net->addr_len = 6;
1370     eth_hw_addr_set(net, dvbnet->dvbdev->adapter->proposed_mac);
1371 
1372     dvbnet->device[if_num] = net;
1373 
1374     priv = netdev_priv(net);
1375     priv->net = net;
1376     priv->demux = dvbnet->demux;
1377     priv->pid = pid;
1378     priv->rx_mode = RX_MODE_UNI;
1379     priv->need_pusi = 1;
1380     priv->tscc = 0;
1381     priv->feedtype = feedtype;
1382     reset_ule(priv);
1383 
1384     INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1385     INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1386     mutex_init(&priv->mutex);
1387 
1388     net->base_addr = pid;
1389 
1390     if ((result = register_netdev(net)) < 0) {
1391         dvbnet->device[if_num] = NULL;
1392         free_netdev(net);
1393         return result;
1394     }
1395     pr_info("created network interface %s\n", net->name);
1396 
1397     return if_num;
1398 }
1399 
1400 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1401 {
1402     struct net_device *net = dvbnet->device[num];
1403     struct dvb_net_priv *priv;
1404 
1405     if (!dvbnet->state[num])
1406         return -EINVAL;
1407     priv = netdev_priv(net);
1408     if (priv->in_use)
1409         return -EBUSY;
1410 
1411     dvb_net_stop(net);
1412     flush_work(&priv->set_multicast_list_wq);
1413     flush_work(&priv->restart_net_feed_wq);
1414     pr_info("removed network interface %s\n", net->name);
1415     unregister_netdev(net);
1416     dvbnet->state[num]=0;
1417     dvbnet->device[num] = NULL;
1418     free_netdev(net);
1419 
1420     return 0;
1421 }
1422 
1423 static int dvb_net_do_ioctl(struct file *file,
1424           unsigned int cmd, void *parg)
1425 {
1426     struct dvb_device *dvbdev = file->private_data;
1427     struct dvb_net *dvbnet = dvbdev->priv;
1428     int ret = 0;
1429 
1430     if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1431         return -EPERM;
1432 
1433     if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1434         return -ERESTARTSYS;
1435 
1436     switch (cmd) {
1437     case NET_ADD_IF:
1438     {
1439         struct dvb_net_if *dvbnetif = parg;
1440         int result;
1441 
1442         if (!capable(CAP_SYS_ADMIN)) {
1443             ret = -EPERM;
1444             goto ioctl_error;
1445         }
1446 
1447         if (!try_module_get(dvbdev->adapter->module)) {
1448             ret = -EPERM;
1449             goto ioctl_error;
1450         }
1451 
1452         result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1453         if (result<0) {
1454             module_put(dvbdev->adapter->module);
1455             ret = result;
1456             goto ioctl_error;
1457         }
1458         dvbnetif->if_num=result;
1459         break;
1460     }
1461     case NET_GET_IF:
1462     {
1463         struct net_device *netdev;
1464         struct dvb_net_priv *priv_data;
1465         struct dvb_net_if *dvbnetif = parg;
1466         int if_num = dvbnetif->if_num;
1467 
1468         if (if_num >= DVB_NET_DEVICES_MAX) {
1469             ret = -EINVAL;
1470             goto ioctl_error;
1471         }
1472         if_num = array_index_nospec(if_num, DVB_NET_DEVICES_MAX);
1473 
1474         if (!dvbnet->state[if_num]) {
1475             ret = -EINVAL;
1476             goto ioctl_error;
1477         }
1478 
1479         netdev = dvbnet->device[if_num];
1480 
1481         priv_data = netdev_priv(netdev);
1482         dvbnetif->pid=priv_data->pid;
1483         dvbnetif->feedtype=priv_data->feedtype;
1484         break;
1485     }
1486     case NET_REMOVE_IF:
1487     {
1488         if (!capable(CAP_SYS_ADMIN)) {
1489             ret = -EPERM;
1490             goto ioctl_error;
1491         }
1492         if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1493             ret = -EINVAL;
1494             goto ioctl_error;
1495         }
1496         ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1497         if (!ret)
1498             module_put(dvbdev->adapter->module);
1499         break;
1500     }
1501 
1502     /* binary compatibility cruft */
1503     case __NET_ADD_IF_OLD:
1504     {
1505         struct __dvb_net_if_old *dvbnetif = parg;
1506         int result;
1507 
1508         if (!capable(CAP_SYS_ADMIN)) {
1509             ret = -EPERM;
1510             goto ioctl_error;
1511         }
1512 
1513         if (!try_module_get(dvbdev->adapter->module)) {
1514             ret = -EPERM;
1515             goto ioctl_error;
1516         }
1517 
1518         result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1519         if (result<0) {
1520             module_put(dvbdev->adapter->module);
1521             ret = result;
1522             goto ioctl_error;
1523         }
1524         dvbnetif->if_num=result;
1525         break;
1526     }
1527     case __NET_GET_IF_OLD:
1528     {
1529         struct net_device *netdev;
1530         struct dvb_net_priv *priv_data;
1531         struct __dvb_net_if_old *dvbnetif = parg;
1532         int if_num = dvbnetif->if_num;
1533 
1534         if (if_num >= DVB_NET_DEVICES_MAX) {
1535             ret = -EINVAL;
1536             goto ioctl_error;
1537         }
1538         if_num = array_index_nospec(if_num, DVB_NET_DEVICES_MAX);
1539 
1540         if (!dvbnet->state[if_num]) {
1541             ret = -EINVAL;
1542             goto ioctl_error;
1543         }
1544 
1545         netdev = dvbnet->device[if_num];
1546 
1547         priv_data = netdev_priv(netdev);
1548         dvbnetif->pid=priv_data->pid;
1549         break;
1550     }
1551     default:
1552         ret = -ENOTTY;
1553         break;
1554     }
1555 
1556 ioctl_error:
1557     mutex_unlock(&dvbnet->ioctl_mutex);
1558     return ret;
1559 }
1560 
1561 static long dvb_net_ioctl(struct file *file,
1562           unsigned int cmd, unsigned long arg)
1563 {
1564     return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1565 }
1566 
1567 static int dvb_net_close(struct inode *inode, struct file *file)
1568 {
1569     struct dvb_device *dvbdev = file->private_data;
1570     struct dvb_net *dvbnet = dvbdev->priv;
1571 
1572     dvb_generic_release(inode, file);
1573 
1574     if(dvbdev->users == 1 && dvbnet->exit == 1)
1575         wake_up(&dvbdev->wait_queue);
1576     return 0;
1577 }
1578 
1579 
1580 static const struct file_operations dvb_net_fops = {
1581     .owner = THIS_MODULE,
1582     .unlocked_ioctl = dvb_net_ioctl,
1583     .open = dvb_generic_open,
1584     .release = dvb_net_close,
1585     .llseek = noop_llseek,
1586 };
1587 
1588 static const struct dvb_device dvbdev_net = {
1589     .priv = NULL,
1590     .users = 1,
1591     .writers = 1,
1592 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1593     .name = "dvb-net",
1594 #endif
1595     .fops = &dvb_net_fops,
1596 };
1597 
1598 void dvb_net_release (struct dvb_net *dvbnet)
1599 {
1600     int i;
1601 
1602     dvbnet->exit = 1;
1603     if (dvbnet->dvbdev->users < 1)
1604         wait_event(dvbnet->dvbdev->wait_queue,
1605                 dvbnet->dvbdev->users==1);
1606 
1607     dvb_unregister_device(dvbnet->dvbdev);
1608 
1609     for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1610         if (!dvbnet->state[i])
1611             continue;
1612         dvb_net_remove_if(dvbnet, i);
1613     }
1614 }
1615 EXPORT_SYMBOL(dvb_net_release);
1616 
1617 
1618 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1619           struct dmx_demux *dmx)
1620 {
1621     int i;
1622 
1623     mutex_init(&dvbnet->ioctl_mutex);
1624     dvbnet->demux = dmx;
1625 
1626     for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1627         dvbnet->state[i] = 0;
1628 
1629     return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
1630                  dvbnet, DVB_DEVICE_NET, 0);
1631 }
1632 EXPORT_SYMBOL(dvb_net_init);