1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2001 Convergence integrated media GmbH
9 * ULE Decapsulation code:
10 * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH.
11 * and Department of Scientific Computing
12 * Paris Lodron University of Salzburg.
16 * ULE Decaps according to RFC 4326.
21 * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt
23 * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt:
24 * ULE Extension header handling.
25 * Bugreports by Moritz Vieth and Hanno Tersteegen,
26 * Fraunhofer Institute for Open Communication Systems
27 * Competence Center for Advanced Satellite Communications.
28 * Bugfixes and robustness improvements.
29 * Filtering on dest MAC addresses, if present (D-Bit = 0)
30 * DVB_ULE_DEBUG compile-time option.
31 * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by
33 * Paris Lodron University of Salzburg.
37 * FIXME / TODO (dvb_net.c):
39 * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
43 #define pr_fmt(fmt) "dvb_net: " fmt
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/netdevice.h>
48 #include <linux/nospec.h>
49 #include <linux/etherdevice.h>
50 #include <linux/dvb/net.h>
51 #include <linux/uio.h>
52 #include <linux/uaccess.h>
53 #include <linux/crc32.h>
54 #include <linux/mutex.h>
55 #include <linux/sched.h>
57 #include <media/dvb_demux.h>
58 #include <media/dvb_net.h>
60 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
63 for (j = 0; j < cnt; j++)
64 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
69 #define DVB_NET_MULTICAST_MAX 10
73 * The code inside DVB_ULE_DEBUG keeps a history of the
74 * last 100 TS cells processed.
76 static unsigned char ule_hist[100*TS_SZ] = { 0 };
77 static unsigned char *ule_where = ule_hist, ule_dump;
79 static void hexdump(const unsigned char *buf, unsigned short len)
81 print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
88 struct net_device *net;
90 struct dmx_demux *demux;
91 struct dmx_section_feed *secfeed;
92 struct dmx_section_filter *secfilter;
93 struct dmx_ts_feed *tsfeed;
95 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
96 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
99 #define RX_MODE_MULTI 1
100 #define RX_MODE_ALL_MULTI 2
101 #define RX_MODE_PROMISC 3
102 struct work_struct set_multicast_list_wq;
103 struct work_struct restart_net_feed_wq;
104 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
105 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
106 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
107 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
108 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
109 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
110 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
111 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
112 unsigned char ule_dbit; /* Whether the DestMAC address present
113 * or not (bit is set). */
114 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
115 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
116 unsigned long ts_count; /* Current ts cell counter. */
122 * Determine the packet's protocol ID. The rule here is that we
123 * assume 802.3 if the type field is short enough to be a length.
124 * This is normal practice and works for any 'now in use' protocol.
126 * stolen from eth.c out of the linux kernel, hacked for dvb-device
129 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
130 struct net_device *dev)
135 skb_reset_mac_header(skb);
136 skb_pull(skb,dev->hard_header_len);
139 if (*eth->h_dest & 1) {
140 if(ether_addr_equal(eth->h_dest,dev->broadcast))
141 skb->pkt_type=PACKET_BROADCAST;
143 skb->pkt_type=PACKET_MULTICAST;
146 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
152 * This is a magic hack to spot IPX packets. Older Novell breaks
153 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
154 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
155 * won't work for fault tolerant netware but does for the rest.
157 if (*(unsigned short *)rawp == 0xFFFF)
158 return htons(ETH_P_802_3);
163 return htons(ETH_P_802_2);
174 /* ULE Extension Header handlers. */
177 #define ULE_BRIDGED 1
179 #define ULE_OPTEXTHDR_PADDING 0
181 static int ule_test_sndu( struct dvb_net_priv *p )
186 static int ule_bridged_sndu( struct dvb_net_priv *p )
188 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
189 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
190 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
191 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
192 if(framelen != ntohs(hdr->h_proto)) {
198 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
199 * It must be the final (or only) extension header specified in the header chain of a SNDU."
200 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
206 static int ule_exthdr_padding(struct dvb_net_priv *p)
212 * Handle ULE extension headers.
213 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
214 * Returns: >= 0: nr. of bytes consumed by next extension header
215 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
217 static int handle_one_ule_extension( struct dvb_net_priv *p )
219 /* Table of mandatory extension header handlers. The header type is the index. */
220 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
221 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
223 /* Table of optional extension header handlers. The header type is the index. */
224 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
225 { [0] = ule_exthdr_padding, [1] = NULL, };
228 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
229 unsigned char htype = p->ule_sndu_type & 0x00FF;
231 /* Discriminate mandatory and optional extension headers. */
233 /* Mandatory extension header */
234 if (ule_mandatory_ext_handlers[htype]) {
235 ext_len = ule_mandatory_ext_handlers[htype]( p );
237 p->ule_next_hdr += ext_len;
238 if (!p->ule_bridged) {
239 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
240 p->ule_next_hdr += 2;
242 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
243 /* This assures the extension handling loop will terminate. */
246 // else: extension handler failed or SNDU should be discarded
248 ext_len = -1; /* SNDU has to be discarded. */
250 /* Optional extension header. Calculate the length. */
252 /* Process the optional extension header according to its type. */
253 if (ule_optional_ext_handlers[htype])
254 (void)ule_optional_ext_handlers[htype]( p );
255 p->ule_next_hdr += ext_len;
256 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
258 * note: the length of the next header type is included in the
259 * length of THIS optional extension header
266 static int handle_ule_extensions( struct dvb_net_priv *p )
268 int total_ext_len = 0, l;
270 p->ule_next_hdr = p->ule_skb->data;
272 l = handle_one_ule_extension( p );
274 return l; /* Stop extension header processing and discard SNDU. */
276 pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
277 p->ule_next_hdr, (int)p->ule_sndu_type,
280 } while (p->ule_sndu_type < ETH_P_802_3_MIN);
282 return total_ext_len;
286 /* Prepare for a new ULE SNDU: reset the decoder state. */
287 static inline void reset_ule( struct dvb_net_priv *p )
290 p->ule_next_hdr = NULL;
292 p->ule_sndu_type = 0;
293 p->ule_sndu_type_1 = 0;
294 p->ule_sndu_remain = 0;
300 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
301 * TS cells of a single PID.
304 struct dvb_net_ule_handle {
305 struct net_device *dev;
306 struct dvb_net_priv *priv;
310 unsigned long skipped;
311 const u8 *ts, *ts_end, *from_where;
312 u8 ts_remain, how_much, new_ts;
316 static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h)
318 /* We are about to process a new TS cell. */
321 if (ule_where >= &ule_hist[100*TS_SZ])
322 ule_where = ule_hist;
323 memcpy(ule_where, h->ts, TS_SZ);
325 hexdump(ule_where, TS_SZ);
332 * Check TS h->error conditions: sync_byte, transport_error_indicator,
333 * scrambling_control .
335 if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) ||
336 ((h->ts[3] & TS_SC) != 0)) {
337 pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
338 h->priv->ts_count, h->ts[0],
339 (h->ts[1] & TS_TEI) >> 7,
340 (h->ts[3] & TS_SC) >> 6);
342 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
343 if (h->priv->ule_skb) {
344 dev_kfree_skb(h->priv->ule_skb);
345 /* Prepare for next SNDU. */
346 h->dev->stats.rx_errors++;
347 h->dev->stats.rx_frame_errors++;
350 h->priv->need_pusi = 1;
352 /* Continue with next TS cell. */
359 h->from_where = h->ts + 4;
364 static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h)
366 if (h->ts[1] & TS_PUSI) {
367 /* Find beginning of first ULE SNDU in current TS cell. */
368 /* Synchronize continuity counter. */
369 h->priv->tscc = h->ts[3] & 0x0F;
370 /* There is a pointer field here. */
371 if (h->ts[4] > h->ts_remain) {
372 pr_err("%lu: Invalid ULE packet (pointer field %d)\n",
373 h->priv->ts_count, h->ts[4]);
378 /* Skip to destination of pointer field. */
379 h->from_where = &h->ts[5] + h->ts[4];
380 h->ts_remain -= 1 + h->ts[4];
392 static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h)
394 /* Check continuity counter. */
395 if ((h->ts[3] & 0x0F) == h->priv->tscc)
396 h->priv->tscc = (h->priv->tscc + 1) & 0x0F;
398 /* TS discontinuity handling: */
399 pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n",
400 h->priv->ts_count, h->ts[3] & 0x0F,
402 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
403 if (h->priv->ule_skb) {
404 dev_kfree_skb(h->priv->ule_skb);
405 /* Prepare for next SNDU. */
406 // reset_ule(h->priv); moved to below.
407 h->dev->stats.rx_errors++;
408 h->dev->stats.rx_frame_errors++;
411 /* skip to next PUSI. */
412 h->priv->need_pusi = 1;
416 * If we still have an incomplete payload, but PUSI is
417 * set; some TS cells are missing.
418 * This is only possible here, if we missed exactly 16 TS
419 * cells (continuity counter wrap).
421 if (h->ts[1] & TS_PUSI) {
422 if (!h->priv->need_pusi) {
423 if (!(*h->from_where < (h->ts_remain-1)) ||
424 *h->from_where != h->priv->ule_sndu_remain) {
426 * Pointer field is invalid.
427 * Drop this TS cell and any started ULE SNDU.
429 pr_warn("%lu: Invalid pointer field: %u.\n",
434 * Drop partly decoded SNDU, reset state,
437 if (h->priv->ule_skb) {
439 dev_kfree_skb(h->priv->ule_skb);
442 if (h->error || h->priv->ule_sndu_remain) {
443 h->dev->stats.rx_errors++;
444 h->dev->stats.rx_frame_errors++;
449 h->priv->need_pusi = 1;
453 * Skip pointer field (we're processing a
459 h->priv->need_pusi = 0;
461 if (h->priv->ule_sndu_remain > 183) {
463 * Current SNDU lacks more data than there
464 * could be available in the current TS cell.
466 h->dev->stats.rx_errors++;
467 h->dev->stats.rx_length_errors++;
468 pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d). Flushing incomplete payload.\n",
470 h->priv->ule_sndu_remain,
471 h->ts[4], h->ts_remain);
472 dev_kfree_skb(h->priv->ule_skb);
473 /* Prepare for next SNDU. */
476 * Resync: go to where pointer field points to:
477 * start of next ULE SNDU.
479 h->from_where += h->ts[4];
480 h->ts_remain -= h->ts[4];
488 * Start a new payload with skb.
489 * Find ULE header. It is only guaranteed that the
490 * length field (2 bytes) is contained in the current
492 * Check h.ts_remain has to be >= 2 here.
494 static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h)
496 if (h->ts_remain < 2) {
497 pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n",
499 h->priv->ule_sndu_len = 0;
500 h->priv->need_pusi = 1;
505 if (!h->priv->ule_sndu_len) {
506 /* Got at least two bytes, thus extrace the SNDU length. */
507 h->priv->ule_sndu_len = h->from_where[0] << 8 |
509 if (h->priv->ule_sndu_len & 0x8000) {
510 /* D-Bit is set: no dest mac present. */
511 h->priv->ule_sndu_len &= 0x7FFF;
512 h->priv->ule_dbit = 1;
514 h->priv->ule_dbit = 0;
516 if (h->priv->ule_sndu_len < 5) {
517 pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n",
519 h->priv->ule_sndu_len);
520 h->dev->stats.rx_errors++;
521 h->dev->stats.rx_length_errors++;
522 h->priv->ule_sndu_len = 0;
523 h->priv->need_pusi = 1;
529 h->ts_remain -= 2; /* consume the 2 bytes SNDU length. */
533 h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2;
535 * State of current TS:
536 * h->ts_remain (remaining bytes in the current TS cell)
537 * 0 ule_type is not available now, we need the next TS cell
538 * 1 the first byte of the ule_type is present
539 * >=2 full ULE header present, maybe some payload data as well.
541 switch (h->ts_remain) {
543 h->priv->ule_sndu_remain--;
544 h->priv->ule_sndu_type = h->from_where[0] << 8;
546 /* first byte of ule_type is set. */
547 h->priv->ule_sndu_type_1 = 1;
557 default: /* complete ULE header is present in current TS. */
558 /* Extract ULE type field. */
559 if (h->priv->ule_sndu_type_1) {
560 h->priv->ule_sndu_type_1 = 0;
561 h->priv->ule_sndu_type |= h->from_where[0];
562 h->from_where += 1; /* points to payload start. */
565 /* Complete type is present in new TS. */
566 h->priv->ule_sndu_type = h->from_where[0] << 8 |
568 h->from_where += 2; /* points to payload start. */
575 * Allocate the skb (decoder target buffer) with the correct size,
578 * prepare for the largest case: bridged SNDU with MAC address
581 h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len +
582 ETH_HLEN + ETH_ALEN);
583 if (!h->priv->ule_skb) {
584 pr_notice("%s: Memory squeeze, dropping packet.\n",
586 h->dev->stats.rx_dropped++;
590 /* This includes the CRC32 _and_ dest mac, if !dbit. */
591 h->priv->ule_sndu_remain = h->priv->ule_sndu_len;
592 h->priv->ule_skb->dev = h->dev;
594 * Leave space for Ethernet or bridged SNDU header
595 * (eth hdr plus one MAC addr).
597 skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN);
603 static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h)
605 static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff };
608 * The destination MAC address is the next data in the skb. It comes
609 * before any extension headers.
611 * Check if the payload of this SNDU should be passed up the stack.
613 if (h->priv->rx_mode == RX_MODE_PROMISC)
616 if (h->priv->ule_skb->data[0] & 0x01) {
617 /* multicast or broadcast */
618 if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) {
620 if (h->priv->rx_mode == RX_MODE_MULTI) {
623 for (i = 0; i < h->priv->multi_num &&
624 !ether_addr_equal(h->priv->ule_skb->data,
625 h->priv->multi_macs[i]);
628 if (i == h->priv->multi_num)
630 } else if (h->priv->rx_mode != RX_MODE_ALL_MULTI)
631 return 1; /* no broadcast; */
634 * all multicast mode: accept all multicast packets
637 /* else: broadcast */
638 } else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr))
645 static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h,
647 u32 ule_crc, u32 expected_crc)
649 u8 dest_addr[ETH_ALEN];
651 if (ule_crc != expected_crc) {
652 pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
653 h->priv->ts_count, ule_crc, expected_crc,
654 h->priv->ule_sndu_len, h->priv->ule_sndu_type,
657 *(unsigned short *)h->from_where : 0);
660 hexdump(iov[0].iov_base, iov[0].iov_len);
661 hexdump(iov[1].iov_base, iov[1].iov_len);
662 hexdump(iov[2].iov_base, iov[2].iov_len);
664 if (ule_where == ule_hist) {
665 hexdump(&ule_hist[98*TS_SZ], TS_SZ);
666 hexdump(&ule_hist[99*TS_SZ], TS_SZ);
667 } else if (ule_where == &ule_hist[TS_SZ]) {
668 hexdump(&ule_hist[99*TS_SZ], TS_SZ);
669 hexdump(ule_hist, TS_SZ);
671 hexdump(ule_where - TS_SZ - TS_SZ, TS_SZ);
672 hexdump(ule_where - TS_SZ, TS_SZ);
677 h->dev->stats.rx_errors++;
678 h->dev->stats.rx_crc_errors++;
679 dev_kfree_skb(h->priv->ule_skb);
684 /* CRC32 verified OK. */
686 /* CRC32 was OK, so remove it from skb. */
687 h->priv->ule_skb->tail -= 4;
688 h->priv->ule_skb->len -= 4;
690 if (!h->priv->ule_dbit) {
691 if (dvb_net_ule_should_drop(h)) {
693 "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n",
694 h->priv->ule_skb->data, h->dev->dev_addr);
695 dev_kfree_skb(h->priv->ule_skb);
699 skb_copy_from_linear_data(h->priv->ule_skb, dest_addr,
701 skb_pull(h->priv->ule_skb, ETH_ALEN);
703 /* dest_addr buffer is only valid if h->priv->ule_dbit == 0 */
704 eth_zero_addr(dest_addr);
707 /* Handle ULE Extension Headers. */
708 if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) {
709 /* There is an extension header. Handle it accordingly. */
710 int l = handle_ule_extensions(h->priv);
714 * Mandatory extension header unknown or TEST SNDU.
718 // pr_warn("Dropping SNDU, extension headers.\n" );
719 dev_kfree_skb(h->priv->ule_skb);
722 skb_pull(h->priv->ule_skb, l);
726 * Construct/assure correct ethernet header.
727 * Note: in bridged mode (h->priv->ule_bridged != 0)
728 * we already have the (original) ethernet
729 * header at the start of the payload (after
730 * optional dest. address and any extension
733 if (!h->priv->ule_bridged) {
734 skb_push(h->priv->ule_skb, ETH_HLEN);
735 h->ethh = (struct ethhdr *)h->priv->ule_skb->data;
736 memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN);
737 eth_zero_addr(h->ethh->h_source);
738 h->ethh->h_proto = htons(h->priv->ule_sndu_type);
740 /* else: skb is in correct state; nothing to do. */
741 h->priv->ule_bridged = 0;
743 /* Stuff into kernel's protocol stack. */
744 h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb,
747 * If D-bit is set (i.e. destination MAC address not present),
748 * receive the packet anyhow.
751 if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
752 h->priv->ule_skb->pkt_type = PACKET_HOST;
754 h->dev->stats.rx_packets++;
755 h->dev->stats.rx_bytes += h->priv->ule_skb->len;
756 netif_rx(h->priv->ule_skb);
759 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len)
762 struct dvb_net_ule_handle h = {
764 .priv = netdev_priv(dev),
779 * For all TS cells in current buffer.
780 * Appearently, we are called for every single TS cell.
782 for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len;
783 h.ts < h.ts_end; /* no incr. */) {
785 /* We are about to process a new TS cell. */
786 if (dvb_net_ule_new_ts_cell(&h))
790 /* Synchronize on PUSI, if required. */
791 if (h.priv->need_pusi) {
792 if (dvb_net_ule_ts_pusi(&h))
797 if (dvb_net_ule_new_ts(&h))
801 /* Check if new payload needs to be started. */
802 if (h.priv->ule_skb == NULL) {
803 ret = dvb_net_ule_new_payload(&h);
810 /* Copy data into our current skb. */
811 h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain);
812 skb_put_data(h.priv->ule_skb, h.from_where, h.how_much);
813 h.priv->ule_sndu_remain -= h.how_much;
814 h.ts_remain -= h.how_much;
815 h.from_where += h.how_much;
817 /* Check for complete payload. */
818 if (h.priv->ule_sndu_remain <= 0) {
819 /* Check CRC32, we've got it in our skb already. */
820 __be16 ulen = htons(h.priv->ule_sndu_len);
821 __be16 utype = htons(h.priv->ule_sndu_type);
823 struct kvec iov[3] = {
824 { &ulen, sizeof ulen },
825 { &utype, sizeof utype },
826 { h.priv->ule_skb->data,
827 h.priv->ule_skb->len - 4 }
829 u32 ule_crc = ~0L, expected_crc;
830 if (h.priv->ule_dbit) {
831 /* Set D-bit for CRC32 verification,
832 * if it was set originally. */
833 ulen |= htons(0x8000);
836 ule_crc = iov_crc32(ule_crc, iov, 3);
837 tail = skb_tail_pointer(h.priv->ule_skb);
838 expected_crc = *(tail - 4) << 24 |
843 dvb_net_ule_check_crc(&h, iov, ule_crc, expected_crc);
845 /* Prepare for next SNDU. */
849 /* More data in current TS (look at the bytes following the CRC32)? */
850 if (h.ts_remain >= 2 && *((unsigned short *)h.from_where) != 0xFFFF) {
851 /* Next ULE SNDU starts right there. */
853 h.priv->ule_skb = NULL;
854 h.priv->ule_sndu_type_1 = 0;
855 h.priv->ule_sndu_len = 0;
856 // pr_warn("More data in current TS: [%#x %#x %#x %#x]\n",
857 // *(h.from_where + 0), *(h.from_where + 1),
858 // *(h.from_where + 2), *(h.from_where + 3));
859 // pr_warn("h.ts @ %p, stopped @ %p:\n", h.ts, h.from_where + 0);
860 // hexdump(h.ts, 188);
865 if (h.priv->ule_skb == NULL) {
866 h.priv->need_pusi = 1;
867 h.priv->ule_sndu_type_1 = 0;
868 h.priv->ule_sndu_len = 0;
871 } /* for all available TS cells */
874 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
875 const u8 *buffer2, size_t buffer2_len,
876 struct dmx_ts_feed *feed,
879 struct net_device *dev = feed->priv;
882 pr_warn("buffer2 not NULL: %p.\n", buffer2);
883 if (buffer1_len > 32768)
884 pr_warn("length > 32k: %zu.\n", buffer1_len);
885 /* pr_info("TS callback: %u bytes, %u TS cells @ %p.\n",
886 buffer1_len, buffer1_len / TS_SZ, buffer1); */
887 dvb_net_ule(dev, buffer1, buffer1_len);
892 static void dvb_net_sec(struct net_device *dev,
893 const u8 *pkt, int pkt_len)
897 struct net_device_stats *stats = &dev->stats;
900 /* note: pkt_len includes a 32bit checksum */
902 pr_warn("%s: IP/MPE packet length = %d too small.\n",
905 stats->rx_length_errors++;
908 /* it seems some ISPs manage to screw up here, so we have to
909 * relax the error checks... */
911 if ((pkt[5] & 0xfd) != 0xc1) {
912 /* drop scrambled or broken packets */
914 if ((pkt[5] & 0x3c) != 0x00) {
918 stats->rx_crc_errors++;
922 /* handle LLC/SNAP, see rfc-1042 */
923 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
930 /* FIXME: assemble datagram from multiple sections */
932 stats->rx_frame_errors++;
936 /* we have 14 byte ethernet header (ip header follows);
937 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
939 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
940 //pr_notice("%s: Memory squeeze, dropping packet.\n", dev->name);
944 skb_reserve(skb, 2); /* longword align L3 header */
947 /* copy L3 payload */
948 eth = skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
949 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
951 /* create ethernet header: */
959 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
965 /* protocol numbers are from rfc-1700 or
966 * http://www.iana.org/assignments/ethernet-numbers
968 if (pkt[12] >> 4 == 6) { /* version field from IP header */
969 eth[12] = 0x86; /* IPv6 */
972 eth[12] = 0x08; /* IPv4 */
977 skb->protocol = dvb_net_eth_type_trans(skb, dev);
980 stats->rx_bytes+=skb->len;
984 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
985 const u8 *buffer2, size_t buffer2_len,
986 struct dmx_section_filter *filter, u32 *buffer_flags)
988 struct net_device *dev = filter->priv;
991 * we rely on the DVB API definition where exactly one complete
992 * section is delivered in buffer1
994 dvb_net_sec (dev, buffer1, buffer1_len);
998 static netdev_tx_t dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
1001 return NETDEV_TX_OK;
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};
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)
1013 struct dvb_net_priv *priv = netdev_priv(dev);
1017 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
1019 pr_err("%s: could not get filter\n", dev->name);
1023 (*secfilter)->priv=(void *) dev;
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);
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];
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];
1045 netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
1050 static int dvb_net_feed_start(struct net_device *dev)
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;
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__);
1063 priv->secfilter=NULL;
1064 priv->tsfeed = NULL;
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);
1071 pr_err("%s: could not allocate section feed\n",
1076 ret = priv->secfeed->set(priv->secfeed, priv->pid, 1);
1079 pr_err("%s: could not set section feed\n", dev->name);
1080 priv->demux->release_section_feed(priv->demux, priv->secfeed);
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);
1090 switch (priv->rx_mode) {
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);
1098 case RX_MODE_ALL_MULTI:
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);
1104 case RX_MODE_PROMISC:
1106 netdev_dbg(dev, "set secfilter\n");
1107 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
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);
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);
1120 pr_err("%s: could not allocate ts feed\n", dev->name);
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 */
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;
1140 netdev_dbg(dev, "start filtering\n");
1141 priv->tsfeed->start_filtering(priv->tsfeed);
1146 mutex_unlock(&priv->mutex);
1150 static int dvb_net_feed_stop(struct net_device *dev)
1152 struct dvb_net_priv *priv = netdev_priv(dev);
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);
1163 if (priv->secfilter) {
1164 netdev_dbg(dev, "release secfilter\n");
1165 priv->secfeed->release_filter(priv->secfeed,
1167 priv->secfilter=NULL;
1170 for (i=0; i<priv->multi_num; i++) {
1171 if (priv->multi_secfilter[i]) {
1172 netdev_dbg(dev, "release multi_filter[%d]\n",
1174 priv->secfeed->release_filter(priv->secfeed,
1175 priv->multi_secfilter[i]);
1176 priv->multi_secfilter[i] = NULL;
1180 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1181 priv->secfeed = NULL;
1183 pr_err("%s: no feed to stop\n", dev->name);
1184 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1186 if (priv->tsfeed->is_filtering) {
1187 netdev_dbg(dev, "stop tsfeed\n");
1188 priv->tsfeed->stop_filtering(priv->tsfeed);
1190 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1191 priv->tsfeed = NULL;
1194 pr_err("%s: no ts feed to stop\n", dev->name);
1197 mutex_unlock(&priv->mutex);
1202 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
1204 struct dvb_net_priv *priv = netdev_priv(dev);
1206 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1209 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
1216 static void wq_set_multicast_list (struct work_struct *work)
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;
1222 dvb_net_feed_stop(dev);
1223 priv->rx_mode = RX_MODE_UNI;
1224 netif_addr_lock_bh(dev);
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;
1235 netdev_dbg(dev, "set_mc_list, %d entries\n",
1236 netdev_mc_count(dev));
1238 priv->rx_mode = RX_MODE_MULTI;
1239 priv->multi_num = 0;
1241 netdev_for_each_mc_addr(ha, dev)
1242 dvb_set_mc_filter(dev, ha->addr);
1245 netif_addr_unlock_bh(dev);
1246 dvb_net_feed_start(dev);
1250 static void dvb_net_set_multicast_list (struct net_device *dev)
1252 struct dvb_net_priv *priv = netdev_priv(dev);
1253 schedule_work(&priv->set_multicast_list_wq);
1257 static void wq_restart_net_feed (struct work_struct *work)
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;
1263 if (netif_running(dev)) {
1264 dvb_net_feed_stop(dev);
1265 dvb_net_feed_start(dev);
1270 static int dvb_net_set_mac (struct net_device *dev, void *p)
1272 struct dvb_net_priv *priv = netdev_priv(dev);
1273 struct sockaddr *addr=p;
1275 eth_hw_addr_set(dev, addr->sa_data);
1277 if (netif_running(dev))
1278 schedule_work(&priv->restart_net_feed_wq);
1284 static int dvb_net_open(struct net_device *dev)
1286 struct dvb_net_priv *priv = netdev_priv(dev);
1289 dvb_net_feed_start(dev);
1294 static int dvb_net_stop(struct net_device *dev)
1296 struct dvb_net_priv *priv = netdev_priv(dev);
1299 return dvb_net_feed_stop(dev);
1302 static const struct header_ops dvb_header_ops = {
1303 .create = eth_header,
1304 .parse = eth_header_parse,
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,
1317 static void dvb_net_setup(struct net_device *dev)
1321 dev->header_ops = &dvb_header_ops;
1322 dev->netdev_ops = &dvb_netdev_ops;
1324 dev->max_mtu = 4096;
1326 dev->flags |= IFF_NOARP;
1329 static int get_if(struct dvb_net *dvbnet)
1333 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1334 if (!dvbnet->state[i])
1337 if (i == DVB_NET_DEVICES_MAX)
1344 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1346 struct net_device *net;
1347 struct dvb_net_priv *priv;
1351 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1353 if ((if_num = get_if(dvbnet)) < 0)
1356 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1357 NET_NAME_UNKNOWN, dvb_net_setup);
1361 if (dvbnet->dvbdev->id)
1362 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1363 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1365 /* compatibility fix to keep dvb0_0 format */
1366 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1367 dvbnet->dvbdev->adapter->num, if_num);
1370 eth_hw_addr_set(net, dvbnet->dvbdev->adapter->proposed_mac);
1372 dvbnet->device[if_num] = net;
1374 priv = netdev_priv(net);
1376 priv->demux = dvbnet->demux;
1378 priv->rx_mode = RX_MODE_UNI;
1379 priv->need_pusi = 1;
1381 priv->feedtype = feedtype;
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);
1388 net->base_addr = pid;
1390 if ((result = register_netdev(net)) < 0) {
1391 dvbnet->device[if_num] = NULL;
1395 pr_info("created network interface %s\n", net->name);
1400 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1402 struct net_device *net = dvbnet->device[num];
1403 struct dvb_net_priv *priv;
1405 if (!dvbnet->state[num])
1407 priv = netdev_priv(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;
1423 static int dvb_net_do_ioctl(struct file *file,
1424 unsigned int cmd, void *parg)
1426 struct dvb_device *dvbdev = file->private_data;
1427 struct dvb_net *dvbnet = dvbdev->priv;
1430 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1433 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1434 return -ERESTARTSYS;
1439 struct dvb_net_if *dvbnetif = parg;
1442 if (!capable(CAP_SYS_ADMIN)) {
1447 if (!try_module_get(dvbdev->adapter->module)) {
1452 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1454 module_put(dvbdev->adapter->module);
1458 dvbnetif->if_num=result;
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;
1468 if (if_num >= DVB_NET_DEVICES_MAX) {
1472 if_num = array_index_nospec(if_num, DVB_NET_DEVICES_MAX);
1474 if (!dvbnet->state[if_num]) {
1479 netdev = dvbnet->device[if_num];
1481 priv_data = netdev_priv(netdev);
1482 dvbnetif->pid=priv_data->pid;
1483 dvbnetif->feedtype=priv_data->feedtype;
1488 if (!capable(CAP_SYS_ADMIN)) {
1492 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1496 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1498 module_put(dvbdev->adapter->module);
1502 /* binary compatibility cruft */
1503 case __NET_ADD_IF_OLD:
1505 struct __dvb_net_if_old *dvbnetif = parg;
1508 if (!capable(CAP_SYS_ADMIN)) {
1513 if (!try_module_get(dvbdev->adapter->module)) {
1518 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1520 module_put(dvbdev->adapter->module);
1524 dvbnetif->if_num=result;
1527 case __NET_GET_IF_OLD:
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;
1534 if (if_num >= DVB_NET_DEVICES_MAX) {
1538 if_num = array_index_nospec(if_num, DVB_NET_DEVICES_MAX);
1540 if (!dvbnet->state[if_num]) {
1545 netdev = dvbnet->device[if_num];
1547 priv_data = netdev_priv(netdev);
1548 dvbnetif->pid=priv_data->pid;
1557 mutex_unlock(&dvbnet->ioctl_mutex);
1561 static long dvb_net_ioctl(struct file *file,
1562 unsigned int cmd, unsigned long arg)
1564 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1567 static int locked_dvb_net_open(struct inode *inode, struct file *file)
1569 struct dvb_device *dvbdev = file->private_data;
1570 struct dvb_net *dvbnet = dvbdev->priv;
1573 if (mutex_lock_interruptible(&dvbnet->remove_mutex))
1574 return -ERESTARTSYS;
1577 mutex_unlock(&dvbnet->remove_mutex);
1581 ret = dvb_generic_open(inode, file);
1583 mutex_unlock(&dvbnet->remove_mutex);
1588 static int dvb_net_close(struct inode *inode, struct file *file)
1590 struct dvb_device *dvbdev = file->private_data;
1591 struct dvb_net *dvbnet = dvbdev->priv;
1593 mutex_lock(&dvbnet->remove_mutex);
1595 dvb_generic_release(inode, file);
1597 if (dvbdev->users == 1 && dvbnet->exit == 1) {
1598 mutex_unlock(&dvbnet->remove_mutex);
1599 wake_up(&dvbdev->wait_queue);
1601 mutex_unlock(&dvbnet->remove_mutex);
1608 static const struct file_operations dvb_net_fops = {
1609 .owner = THIS_MODULE,
1610 .unlocked_ioctl = dvb_net_ioctl,
1611 .open = locked_dvb_net_open,
1612 .release = dvb_net_close,
1613 .llseek = noop_llseek,
1616 static const struct dvb_device dvbdev_net = {
1620 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1623 .fops = &dvb_net_fops,
1626 void dvb_net_release (struct dvb_net *dvbnet)
1630 mutex_lock(&dvbnet->remove_mutex);
1632 mutex_unlock(&dvbnet->remove_mutex);
1634 if (dvbnet->dvbdev->users < 1)
1635 wait_event(dvbnet->dvbdev->wait_queue,
1636 dvbnet->dvbdev->users == 1);
1638 dvb_unregister_device(dvbnet->dvbdev);
1640 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1641 if (!dvbnet->state[i])
1643 dvb_net_remove_if(dvbnet, i);
1646 EXPORT_SYMBOL(dvb_net_release);
1649 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1650 struct dmx_demux *dmx)
1654 mutex_init(&dvbnet->ioctl_mutex);
1655 mutex_init(&dvbnet->remove_mutex);
1656 dvbnet->demux = dmx;
1658 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1659 dvbnet->state[i] = 0;
1661 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
1662 dvbnet, DVB_DEVICE_NET, 0);
1664 EXPORT_SYMBOL(dvb_net_init);