4 * Copyright (C) 2001 Convergence integrated media GmbH
8 * ULE Decapsulation code:
9 * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH.
10 * and Department of Scientific Computing
11 * Paris Lodron University of Salzburg.
15 * ULE Decaps according to RFC 4326.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * To obtain the license, point your browser to
27 * http://www.gnu.org/copyleft/gpl.html
32 * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt
34 * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt:
35 * ULE Extension header handling.
36 * Bugreports by Moritz Vieth and Hanno Tersteegen,
37 * Fraunhofer Institute for Open Communication Systems
38 * Competence Center for Advanced Satellite Communications.
39 * Bugfixes and robustness improvements.
40 * Filtering on dest MAC addresses, if present (D-Bit = 0)
41 * ULE_DEBUG compile-time option.
42 * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by
44 * Paris Lodron University of Salzburg.
48 * FIXME / TODO (dvb_net.c):
50 * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
54 #define pr_fmt(fmt) "dvb_net: " fmt
56 #include <linux/module.h>
57 #include <linux/kernel.h>
58 #include <linux/netdevice.h>
59 #include <linux/etherdevice.h>
60 #include <linux/dvb/net.h>
61 #include <linux/uio.h>
62 #include <linux/uaccess.h>
63 #include <linux/crc32.h>
64 #include <linux/mutex.h>
65 #include <linux/sched.h>
67 #include "dvb_demux.h"
70 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
73 for (j = 0; j < cnt; j++)
74 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
79 #define DVB_NET_MULTICAST_MAX 10
85 static void hexdump(const unsigned char *buf, unsigned short len)
87 print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
95 struct net_device *net;
97 struct dmx_demux *demux;
98 struct dmx_section_feed *secfeed;
99 struct dmx_section_filter *secfilter;
100 struct dmx_ts_feed *tsfeed;
102 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
103 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
105 #define RX_MODE_UNI 0
106 #define RX_MODE_MULTI 1
107 #define RX_MODE_ALL_MULTI 2
108 #define RX_MODE_PROMISC 3
109 struct work_struct set_multicast_list_wq;
110 struct work_struct restart_net_feed_wq;
111 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
112 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
113 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
114 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
115 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
116 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
117 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
118 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
119 unsigned char ule_dbit; /* Whether the DestMAC address present
120 * or not (bit is set). */
121 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
122 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
123 unsigned long ts_count; /* Current ts cell counter. */
129 * Determine the packet's protocol ID. The rule here is that we
130 * assume 802.3 if the type field is short enough to be a length.
131 * This is normal practice and works for any 'now in use' protocol.
133 * stolen from eth.c out of the linux kernel, hacked for dvb-device
136 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
137 struct net_device *dev)
142 skb_reset_mac_header(skb);
143 skb_pull(skb,dev->hard_header_len);
146 if (*eth->h_dest & 1) {
147 if(ether_addr_equal(eth->h_dest,dev->broadcast))
148 skb->pkt_type=PACKET_BROADCAST;
150 skb->pkt_type=PACKET_MULTICAST;
153 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
159 * This is a magic hack to spot IPX packets. Older Novell breaks
160 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
161 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
162 * won't work for fault tolerant netware but does for the rest.
164 if (*(unsigned short *)rawp == 0xFFFF)
165 return htons(ETH_P_802_3);
170 return htons(ETH_P_802_2);
181 /* ULE Extension Header handlers. */
184 #define ULE_BRIDGED 1
186 #define ULE_OPTEXTHDR_PADDING 0
188 static int ule_test_sndu( struct dvb_net_priv *p )
193 static int ule_bridged_sndu( struct dvb_net_priv *p )
195 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
196 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
197 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
198 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
199 if(framelen != ntohs(hdr->h_proto)) {
205 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
206 * It must be the final (or only) extension header specified in the header chain of a SNDU."
207 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
213 static int ule_exthdr_padding(struct dvb_net_priv *p)
218 /** Handle ULE extension headers.
219 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
220 * Returns: >= 0: nr. of bytes consumed by next extension header
221 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
223 static int handle_one_ule_extension( struct dvb_net_priv *p )
225 /* Table of mandatory extension header handlers. The header type is the index. */
226 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
227 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
229 /* Table of optional extension header handlers. The header type is the index. */
230 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
231 { [0] = ule_exthdr_padding, [1] = NULL, };
234 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
235 unsigned char htype = p->ule_sndu_type & 0x00FF;
237 /* Discriminate mandatory and optional extension headers. */
239 /* Mandatory extension header */
240 if (ule_mandatory_ext_handlers[htype]) {
241 ext_len = ule_mandatory_ext_handlers[htype]( p );
243 p->ule_next_hdr += ext_len;
244 if (!p->ule_bridged) {
245 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
246 p->ule_next_hdr += 2;
248 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
249 /* This assures the extension handling loop will terminate. */
252 // else: extension handler failed or SNDU should be discarded
254 ext_len = -1; /* SNDU has to be discarded. */
256 /* Optional extension header. Calculate the length. */
258 /* Process the optional extension header according to its type. */
259 if (ule_optional_ext_handlers[htype])
260 (void)ule_optional_ext_handlers[htype]( p );
261 p->ule_next_hdr += ext_len;
262 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
264 * note: the length of the next header type is included in the
265 * length of THIS optional extension header
272 static int handle_ule_extensions( struct dvb_net_priv *p )
274 int total_ext_len = 0, l;
276 p->ule_next_hdr = p->ule_skb->data;
278 l = handle_one_ule_extension( p );
280 return l; /* Stop extension header processing and discard SNDU. */
283 pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
284 p->ule_next_hdr, (int)p->ule_sndu_type,
288 } while (p->ule_sndu_type < ETH_P_802_3_MIN);
290 return total_ext_len;
294 /** Prepare for a new ULE SNDU: reset the decoder state. */
295 static inline void reset_ule( struct dvb_net_priv *p )
298 p->ule_next_hdr = NULL;
300 p->ule_sndu_type = 0;
301 p->ule_sndu_type_1 = 0;
302 p->ule_sndu_remain = 0;
308 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
309 * TS cells of a single PID.
312 struct dvb_net_ule_handle {
313 struct net_device *dev;
314 struct dvb_net_priv *priv;
318 unsigned long skipped;
319 const u8 *ts, *ts_end, *from_where;
320 u8 ts_remain, how_much, new_ts;
324 * The code inside ULE_DEBUG keeps a history of the
325 * last 100 TS cells processed.
327 static unsigned char ule_hist[100*TS_SZ];
328 static unsigned char *ule_where = ule_hist, ule_dump;
332 static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h)
334 /* We are about to process a new TS cell. */
337 if (h->ule_where >= &h->ule_hist[100*TS_SZ])
338 h->ule_where = h->ule_hist;
339 memcpy(h->ule_where, h->ts, TS_SZ);
341 hexdump(h->ule_where, TS_SZ);
344 h->ule_where += TS_SZ;
348 * Check TS h->error conditions: sync_byte, transport_error_indicator,
349 * scrambling_control .
351 if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) ||
352 ((h->ts[3] & TS_SC) != 0)) {
353 pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
354 h->priv->ts_count, h->ts[0],
355 (h->ts[1] & TS_TEI) >> 7,
356 (h->ts[3] & TS_SC) >> 6);
358 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
359 if (h->priv->ule_skb) {
360 dev_kfree_skb(h->priv->ule_skb);
361 /* Prepare for next SNDU. */
362 h->dev->stats.rx_errors++;
363 h->dev->stats.rx_frame_errors++;
366 h->priv->need_pusi = 1;
368 /* Continue with next TS cell. */
375 h->from_where = h->ts + 4;
380 static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h)
382 if (h->ts[1] & TS_PUSI) {
383 /* Find beginning of first ULE SNDU in current TS cell. */
384 /* Synchronize continuity counter. */
385 h->priv->tscc = h->ts[3] & 0x0F;
386 /* There is a pointer field here. */
387 if (h->ts[4] > h->ts_remain) {
388 pr_err("%lu: Invalid ULE packet (pointer field %d)\n",
389 h->priv->ts_count, h->ts[4]);
394 /* Skip to destination of pointer field. */
395 h->from_where = &h->ts[5] + h->ts[4];
396 h->ts_remain -= 1 + h->ts[4];
408 static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h)
410 /* Check continuity counter. */
411 if ((h->ts[3] & 0x0F) == h->priv->tscc)
412 h->priv->tscc = (h->priv->tscc + 1) & 0x0F;
414 /* TS discontinuity handling: */
415 pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n",
416 h->priv->ts_count, h->ts[3] & 0x0F,
418 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
419 if (h->priv->ule_skb) {
420 dev_kfree_skb(h->priv->ule_skb);
421 /* Prepare for next SNDU. */
422 // reset_ule(h->priv); moved to below.
423 h->dev->stats.rx_errors++;
424 h->dev->stats.rx_frame_errors++;
427 /* skip to next PUSI. */
428 h->priv->need_pusi = 1;
432 * If we still have an incomplete payload, but PUSI is
433 * set; some TS cells are missing.
434 * This is only possible here, if we missed exactly 16 TS
435 * cells (continuity counter wrap).
437 if (h->ts[1] & TS_PUSI) {
438 if (!h->priv->need_pusi) {
439 if (!(*h->from_where < (h->ts_remain-1)) ||
440 *h->from_where != h->priv->ule_sndu_remain) {
442 * Pointer field is invalid.
443 * Drop this TS cell and any started ULE SNDU.
445 pr_warn("%lu: Invalid pointer field: %u.\n",
450 * Drop partly decoded SNDU, reset state,
453 if (h->priv->ule_skb) {
455 dev_kfree_skb(h->priv->ule_skb);
458 if (h->error || h->priv->ule_sndu_remain) {
459 h->dev->stats.rx_errors++;
460 h->dev->stats.rx_frame_errors++;
465 h->priv->need_pusi = 1;
469 * Skip pointer field (we're processing a
475 h->priv->need_pusi = 0;
477 if (h->priv->ule_sndu_remain > 183) {
479 * Current SNDU lacks more data than there
480 * could be available in the current TS cell.
482 h->dev->stats.rx_errors++;
483 h->dev->stats.rx_length_errors++;
484 pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d). Flushing incomplete payload.\n",
486 h->priv->ule_sndu_remain,
487 h->ts[4], h->ts_remain);
488 dev_kfree_skb(h->priv->ule_skb);
489 /* Prepare for next SNDU. */
492 * Resync: go to where pointer field points to:
493 * start of next ULE SNDU.
495 h->from_where += h->ts[4];
496 h->ts_remain -= h->ts[4];
504 * Start a new payload with skb.
505 * Find ULE header. It is only guaranteed that the
506 * length field (2 bytes) is contained in the current
508 * Check h.ts_remain has to be >= 2 here.
510 static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h)
512 if (h->ts_remain < 2) {
513 pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n",
515 h->priv->ule_sndu_len = 0;
516 h->priv->need_pusi = 1;
521 if (!h->priv->ule_sndu_len) {
522 /* Got at least two bytes, thus extrace the SNDU length. */
523 h->priv->ule_sndu_len = h->from_where[0] << 8 |
525 if (h->priv->ule_sndu_len & 0x8000) {
526 /* D-Bit is set: no dest mac present. */
527 h->priv->ule_sndu_len &= 0x7FFF;
528 h->priv->ule_dbit = 1;
530 h->priv->ule_dbit = 0;
532 if (h->priv->ule_sndu_len < 5) {
533 pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n",
535 h->priv->ule_sndu_len);
536 h->dev->stats.rx_errors++;
537 h->dev->stats.rx_length_errors++;
538 h->priv->ule_sndu_len = 0;
539 h->priv->need_pusi = 1;
545 h->ts_remain -= 2; /* consume the 2 bytes SNDU length. */
549 h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2;
551 * State of current TS:
552 * h->ts_remain (remaining bytes in the current TS cell)
553 * 0 ule_type is not available now, we need the next TS cell
554 * 1 the first byte of the ule_type is present
555 * >=2 full ULE header present, maybe some payload data as well.
557 switch (h->ts_remain) {
559 h->priv->ule_sndu_remain--;
560 h->priv->ule_sndu_type = h->from_where[0] << 8;
562 /* first byte of ule_type is set. */
563 h->priv->ule_sndu_type_1 = 1;
573 default: /* complete ULE header is present in current TS. */
574 /* Extract ULE type field. */
575 if (h->priv->ule_sndu_type_1) {
576 h->priv->ule_sndu_type_1 = 0;
577 h->priv->ule_sndu_type |= h->from_where[0];
578 h->from_where += 1; /* points to payload start. */
581 /* Complete type is present in new TS. */
582 h->priv->ule_sndu_type = h->from_where[0] << 8 |
584 h->from_where += 2; /* points to payload start. */
591 * Allocate the skb (decoder target buffer) with the correct size,
594 * prepare for the largest case: bridged SNDU with MAC address
597 h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len +
598 ETH_HLEN + ETH_ALEN);
599 if (!h->priv->ule_skb) {
600 pr_notice("%s: Memory squeeze, dropping packet.\n",
602 h->dev->stats.rx_dropped++;
606 /* This includes the CRC32 _and_ dest mac, if !dbit. */
607 h->priv->ule_sndu_remain = h->priv->ule_sndu_len;
608 h->priv->ule_skb->dev = h->dev;
610 * Leave space for Ethernet or bridged SNDU header
611 * (eth hdr plus one MAC addr).
613 skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN);
619 static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h)
621 static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff };
624 * The destination MAC address is the next data in the skb. It comes
625 * before any extension headers.
627 * Check if the payload of this SNDU should be passed up the stack.
629 if (h->priv->rx_mode == RX_MODE_PROMISC)
632 if (h->priv->ule_skb->data[0] & 0x01) {
633 /* multicast or broadcast */
634 if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) {
636 if (h->priv->rx_mode == RX_MODE_MULTI) {
639 for (i = 0; i < h->priv->multi_num &&
640 !ether_addr_equal(h->priv->ule_skb->data,
641 h->priv->multi_macs[i]);
644 if (i == h->priv->multi_num)
646 } else if (h->priv->rx_mode != RX_MODE_ALL_MULTI)
647 return 1; /* no broadcast; */
650 * all multicast mode: accept all multicast packets
653 /* else: broadcast */
654 } else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr))
661 static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h,
662 u32 ule_crc, u32 expected_crc)
664 u8 dest_addr[ETH_ALEN];
666 if (ule_crc != expected_crc) {
667 pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
668 h->priv->ts_count, ule_crc, expected_crc,
669 h->priv->ule_sndu_len, h->priv->ule_sndu_type,
672 *(unsigned short *)h->from_where : 0);
675 hexdump(iov[0].iov_base, iov[0].iov_len);
676 hexdump(iov[1].iov_base, iov[1].iov_len);
677 hexdump(iov[2].iov_base, iov[2].iov_len);
679 if (h->ule_where == h->ule_hist) {
680 hexdump(&h->ule_hist[98*TS_SZ], TS_SZ);
681 hexdump(&h->ule_hist[99*TS_SZ], TS_SZ);
682 } else if (h->ule_where == &h->ule_hist[TS_SZ]) {
683 hexdump(&h->ule_hist[99*TS_SZ], TS_SZ);
684 hexdump(h->ule_hist, TS_SZ);
686 hexdump(h->ule_where - TS_SZ - TS_SZ, TS_SZ);
687 hexdump(h->ule_where - TS_SZ, TS_SZ);
692 h->dev->stats.rx_errors++;
693 h->dev->stats.rx_crc_errors++;
694 dev_kfree_skb(h->priv->ule_skb);
699 /* CRC32 verified OK. */
701 /* CRC32 was OK, so remove it from skb. */
702 h->priv->ule_skb->tail -= 4;
703 h->priv->ule_skb->len -= 4;
705 if (!h->priv->ule_dbit) {
706 if (dvb_net_ule_should_drop(h)) {
709 "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n",
710 h->priv->ule_skb->data, h->dev->dev_addr);
712 dev_kfree_skb(h->priv->ule_skb);
716 skb_copy_from_linear_data(h->priv->ule_skb, dest_addr,
718 skb_pull(h->priv->ule_skb, ETH_ALEN);
720 /* dest_addr buffer is only valid if h->priv->ule_dbit == 0 */
721 eth_zero_addr(dest_addr);
724 /* Handle ULE Extension Headers. */
725 if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) {
726 /* There is an extension header. Handle it accordingly. */
727 int l = handle_ule_extensions(h->priv);
731 * Mandatory extension header unknown or TEST SNDU.
735 // pr_warn("Dropping SNDU, extension headers.\n" );
736 dev_kfree_skb(h->priv->ule_skb);
739 skb_pull(h->priv->ule_skb, l);
743 * Construct/assure correct ethernet header.
744 * Note: in bridged mode (h->priv->ule_bridged != 0)
745 * we already have the (original) ethernet
746 * header at the start of the payload (after
747 * optional dest. address and any extension
750 if (!h->priv->ule_bridged) {
751 skb_push(h->priv->ule_skb, ETH_HLEN);
752 h->ethh = (struct ethhdr *)h->priv->ule_skb->data;
753 memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN);
754 eth_zero_addr(h->ethh->h_source);
755 h->ethh->h_proto = htons(h->priv->ule_sndu_type);
757 /* else: skb is in correct state; nothing to do. */
758 h->priv->ule_bridged = 0;
760 /* Stuff into kernel's protocol stack. */
761 h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb,
764 * If D-bit is set (i.e. destination MAC address not present),
765 * receive the packet anyhow.
768 if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
769 h->priv->ule_skb->pkt_type = PACKET_HOST;
771 h->dev->stats.rx_packets++;
772 h->dev->stats.rx_bytes += h->priv->ule_skb->len;
773 netif_rx(h->priv->ule_skb);
776 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len)
779 struct dvb_net_ule_handle h = {
793 .ule_where = ule_hist,
798 * For all TS cells in current buffer.
799 * Appearently, we are called for every single TS cell.
801 for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len;
802 h.ts < h.ts_end; /* no incr. */) {
804 /* We are about to process a new TS cell. */
805 if (dvb_net_ule_new_ts_cell(&h))
809 /* Synchronize on PUSI, if required. */
810 if (h.priv->need_pusi) {
811 if (dvb_net_ule_ts_pusi(&h))
816 if (dvb_net_ule_new_ts(&h))
820 /* Check if new payload needs to be started. */
821 if (h.priv->ule_skb == NULL) {
822 ret = dvb_net_ule_new_payload(&h);
829 /* Copy data into our current skb. */
830 h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain);
831 skb_put_data(h.priv->ule_skb, h.from_where, h.how_much);
832 h.priv->ule_sndu_remain -= h.how_much;
833 h.ts_remain -= h.how_much;
834 h.from_where += h.how_much;
836 /* Check for complete payload. */
837 if (h.priv->ule_sndu_remain <= 0) {
838 /* Check CRC32, we've got it in our skb already. */
839 __be16 ulen = htons(h.priv->ule_sndu_len);
840 __be16 utype = htons(h.priv->ule_sndu_type);
842 struct kvec iov[3] = {
843 { &ulen, sizeof ulen },
844 { &utype, sizeof utype },
845 { h.priv->ule_skb->data,
846 h.priv->ule_skb->len - 4 }
848 u32 ule_crc = ~0L, expected_crc;
849 if (h.priv->ule_dbit) {
850 /* Set D-bit for CRC32 verification,
851 * if it was set originally. */
852 ulen |= htons(0x8000);
855 ule_crc = iov_crc32(ule_crc, iov, 3);
856 tail = skb_tail_pointer(h.priv->ule_skb);
857 expected_crc = *(tail - 4) << 24 |
862 dvb_net_ule_check_crc(&h, ule_crc, expected_crc);
864 /* Prepare for next SNDU. */
868 /* More data in current TS (look at the bytes following the CRC32)? */
869 if (h.ts_remain >= 2 && *((unsigned short *)h.from_where) != 0xFFFF) {
870 /* Next ULE SNDU starts right there. */
872 h.priv->ule_skb = NULL;
873 h.priv->ule_sndu_type_1 = 0;
874 h.priv->ule_sndu_len = 0;
875 // pr_warn("More data in current TS: [%#x %#x %#x %#x]\n",
876 // *(h.from_where + 0), *(h.from_where + 1),
877 // *(h.from_where + 2), *(h.from_where + 3));
878 // pr_warn("h.ts @ %p, stopped @ %p:\n", h.ts, h.from_where + 0);
879 // hexdump(h.ts, 188);
884 if (h.priv->ule_skb == NULL) {
885 h.priv->need_pusi = 1;
886 h.priv->ule_sndu_type_1 = 0;
887 h.priv->ule_sndu_len = 0;
890 } /* for all available TS cells */
893 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
894 const u8 *buffer2, size_t buffer2_len,
895 struct dmx_ts_feed *feed)
897 struct net_device *dev = feed->priv;
900 pr_warn("buffer2 not NULL: %p.\n", buffer2);
901 if (buffer1_len > 32768)
902 pr_warn("length > 32k: %zu.\n", buffer1_len);
903 /* pr_info("TS callback: %u bytes, %u TS cells @ %p.\n",
904 buffer1_len, buffer1_len / TS_SZ, buffer1); */
905 dvb_net_ule(dev, buffer1, buffer1_len);
910 static void dvb_net_sec(struct net_device *dev,
911 const u8 *pkt, int pkt_len)
915 struct net_device_stats *stats = &dev->stats;
918 /* note: pkt_len includes a 32bit checksum */
920 pr_warn("%s: IP/MPE packet length = %d too small.\n",
923 stats->rx_length_errors++;
926 /* it seems some ISPs manage to screw up here, so we have to
927 * relax the error checks... */
929 if ((pkt[5] & 0xfd) != 0xc1) {
930 /* drop scrambled or broken packets */
932 if ((pkt[5] & 0x3c) != 0x00) {
936 stats->rx_crc_errors++;
940 /* handle LLC/SNAP, see rfc-1042 */
941 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
948 /* FIXME: assemble datagram from multiple sections */
950 stats->rx_frame_errors++;
954 /* we have 14 byte ethernet header (ip header follows);
955 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
957 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
958 //pr_notice("%s: Memory squeeze, dropping packet.\n", dev->name);
962 skb_reserve(skb, 2); /* longword align L3 header */
965 /* copy L3 payload */
966 eth = skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
967 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
969 /* create ethernet header: */
977 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
983 /* protocol numbers are from rfc-1700 or
984 * http://www.iana.org/assignments/ethernet-numbers
986 if (pkt[12] >> 4 == 6) { /* version field from IP header */
987 eth[12] = 0x86; /* IPv6 */
990 eth[12] = 0x08; /* IPv4 */
995 skb->protocol = dvb_net_eth_type_trans(skb, dev);
998 stats->rx_bytes+=skb->len;
1002 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
1003 const u8 *buffer2, size_t buffer2_len,
1004 struct dmx_section_filter *filter)
1006 struct net_device *dev = filter->priv;
1009 * we rely on the DVB API definition where exactly one complete
1010 * section is delivered in buffer1
1012 dvb_net_sec (dev, buffer1, buffer1_len);
1016 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
1019 return NETDEV_TX_OK;
1022 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1023 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
1024 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
1025 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1027 static int dvb_net_filter_sec_set(struct net_device *dev,
1028 struct dmx_section_filter **secfilter,
1029 u8 *mac, u8 *mac_mask)
1031 struct dvb_net_priv *priv = netdev_priv(dev);
1035 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
1037 pr_err("%s: could not get filter\n", dev->name);
1041 (*secfilter)->priv=(void *) dev;
1043 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
1044 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
1045 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
1047 (*secfilter)->filter_value[0]=0x3e;
1048 (*secfilter)->filter_value[3]=mac[5];
1049 (*secfilter)->filter_value[4]=mac[4];
1050 (*secfilter)->filter_value[8]=mac[3];
1051 (*secfilter)->filter_value[9]=mac[2];
1052 (*secfilter)->filter_value[10]=mac[1];
1053 (*secfilter)->filter_value[11]=mac[0];
1055 (*secfilter)->filter_mask[0] = 0xff;
1056 (*secfilter)->filter_mask[3] = mac_mask[5];
1057 (*secfilter)->filter_mask[4] = mac_mask[4];
1058 (*secfilter)->filter_mask[8] = mac_mask[3];
1059 (*secfilter)->filter_mask[9] = mac_mask[2];
1060 (*secfilter)->filter_mask[10] = mac_mask[1];
1061 (*secfilter)->filter_mask[11]=mac_mask[0];
1063 netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
1068 static int dvb_net_feed_start(struct net_device *dev)
1071 struct dvb_net_priv *priv = netdev_priv(dev);
1072 struct dmx_demux *demux = priv->demux;
1073 unsigned char *mac = (unsigned char *) dev->dev_addr;
1075 netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode);
1076 mutex_lock(&priv->mutex);
1077 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
1078 pr_err("%s: BUG %d\n", __func__, __LINE__);
1081 priv->secfilter=NULL;
1082 priv->tsfeed = NULL;
1084 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1085 netdev_dbg(dev, "alloc secfeed\n");
1086 ret=demux->allocate_section_feed(demux, &priv->secfeed,
1087 dvb_net_sec_callback);
1089 pr_err("%s: could not allocate section feed\n",
1094 ret = priv->secfeed->set(priv->secfeed, priv->pid, 1);
1097 pr_err("%s: could not set section feed\n", dev->name);
1098 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1103 if (priv->rx_mode != RX_MODE_PROMISC) {
1104 netdev_dbg(dev, "set secfilter\n");
1105 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
1108 switch (priv->rx_mode) {
1110 for (i = 0; i < priv->multi_num; i++) {
1111 netdev_dbg(dev, "set multi_secfilter[%d]\n", i);
1112 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1113 priv->multi_macs[i], mask_normal);
1116 case RX_MODE_ALL_MULTI:
1118 netdev_dbg(dev, "set multi_secfilter[0]\n");
1119 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1120 mac_allmulti, mask_allmulti);
1122 case RX_MODE_PROMISC:
1124 netdev_dbg(dev, "set secfilter\n");
1125 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1129 netdev_dbg(dev, "start filtering\n");
1130 priv->secfeed->start_filtering(priv->secfeed);
1131 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1132 ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
1134 /* we have payloads encapsulated in TS */
1135 netdev_dbg(dev, "alloc tsfeed\n");
1136 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1138 pr_err("%s: could not allocate ts feed\n", dev->name);
1142 /* Set netdevice pointer for ts decaps callback. */
1143 priv->tsfeed->priv = (void *)dev;
1144 ret = priv->tsfeed->set(priv->tsfeed,
1145 priv->pid, /* pid */
1146 TS_PACKET, /* type */
1147 DMX_PES_OTHER, /* pes type */
1148 timeout /* timeout */
1152 pr_err("%s: could not set ts feed\n", dev->name);
1153 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1154 priv->tsfeed = NULL;
1158 netdev_dbg(dev, "start filtering\n");
1159 priv->tsfeed->start_filtering(priv->tsfeed);
1164 mutex_unlock(&priv->mutex);
1168 static int dvb_net_feed_stop(struct net_device *dev)
1170 struct dvb_net_priv *priv = netdev_priv(dev);
1173 mutex_lock(&priv->mutex);
1174 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1175 if (priv->secfeed) {
1176 if (priv->secfeed->is_filtering) {
1177 netdev_dbg(dev, "stop secfeed\n");
1178 priv->secfeed->stop_filtering(priv->secfeed);
1181 if (priv->secfilter) {
1182 netdev_dbg(dev, "release secfilter\n");
1183 priv->secfeed->release_filter(priv->secfeed,
1185 priv->secfilter=NULL;
1188 for (i=0; i<priv->multi_num; i++) {
1189 if (priv->multi_secfilter[i]) {
1190 netdev_dbg(dev, "release multi_filter[%d]\n",
1192 priv->secfeed->release_filter(priv->secfeed,
1193 priv->multi_secfilter[i]);
1194 priv->multi_secfilter[i] = NULL;
1198 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1199 priv->secfeed = NULL;
1201 pr_err("%s: no feed to stop\n", dev->name);
1202 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1204 if (priv->tsfeed->is_filtering) {
1205 netdev_dbg(dev, "stop tsfeed\n");
1206 priv->tsfeed->stop_filtering(priv->tsfeed);
1208 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1209 priv->tsfeed = NULL;
1212 pr_err("%s: no ts feed to stop\n", dev->name);
1215 mutex_unlock(&priv->mutex);
1220 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
1222 struct dvb_net_priv *priv = netdev_priv(dev);
1224 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1227 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
1234 static void wq_set_multicast_list (struct work_struct *work)
1236 struct dvb_net_priv *priv =
1237 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1238 struct net_device *dev = priv->net;
1240 dvb_net_feed_stop(dev);
1241 priv->rx_mode = RX_MODE_UNI;
1242 netif_addr_lock_bh(dev);
1244 if (dev->flags & IFF_PROMISC) {
1245 netdev_dbg(dev, "promiscuous mode\n");
1246 priv->rx_mode = RX_MODE_PROMISC;
1247 } else if ((dev->flags & IFF_ALLMULTI)) {
1248 netdev_dbg(dev, "allmulti mode\n");
1249 priv->rx_mode = RX_MODE_ALL_MULTI;
1250 } else if (!netdev_mc_empty(dev)) {
1251 struct netdev_hw_addr *ha;
1253 netdev_dbg(dev, "set_mc_list, %d entries\n",
1254 netdev_mc_count(dev));
1256 priv->rx_mode = RX_MODE_MULTI;
1257 priv->multi_num = 0;
1259 netdev_for_each_mc_addr(ha, dev)
1260 dvb_set_mc_filter(dev, ha->addr);
1263 netif_addr_unlock_bh(dev);
1264 dvb_net_feed_start(dev);
1268 static void dvb_net_set_multicast_list (struct net_device *dev)
1270 struct dvb_net_priv *priv = netdev_priv(dev);
1271 schedule_work(&priv->set_multicast_list_wq);
1275 static void wq_restart_net_feed (struct work_struct *work)
1277 struct dvb_net_priv *priv =
1278 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1279 struct net_device *dev = priv->net;
1281 if (netif_running(dev)) {
1282 dvb_net_feed_stop(dev);
1283 dvb_net_feed_start(dev);
1288 static int dvb_net_set_mac (struct net_device *dev, void *p)
1290 struct dvb_net_priv *priv = netdev_priv(dev);
1291 struct sockaddr *addr=p;
1293 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1295 if (netif_running(dev))
1296 schedule_work(&priv->restart_net_feed_wq);
1302 static int dvb_net_open(struct net_device *dev)
1304 struct dvb_net_priv *priv = netdev_priv(dev);
1307 dvb_net_feed_start(dev);
1312 static int dvb_net_stop(struct net_device *dev)
1314 struct dvb_net_priv *priv = netdev_priv(dev);
1317 return dvb_net_feed_stop(dev);
1320 static const struct header_ops dvb_header_ops = {
1321 .create = eth_header,
1322 .parse = eth_header_parse,
1326 static const struct net_device_ops dvb_netdev_ops = {
1327 .ndo_open = dvb_net_open,
1328 .ndo_stop = dvb_net_stop,
1329 .ndo_start_xmit = dvb_net_tx,
1330 .ndo_set_rx_mode = dvb_net_set_multicast_list,
1331 .ndo_set_mac_address = dvb_net_set_mac,
1332 .ndo_validate_addr = eth_validate_addr,
1335 static void dvb_net_setup(struct net_device *dev)
1339 dev->header_ops = &dvb_header_ops;
1340 dev->netdev_ops = &dvb_netdev_ops;
1342 dev->max_mtu = 4096;
1344 dev->flags |= IFF_NOARP;
1347 static int get_if(struct dvb_net *dvbnet)
1351 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1352 if (!dvbnet->state[i])
1355 if (i == DVB_NET_DEVICES_MAX)
1362 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1364 struct net_device *net;
1365 struct dvb_net_priv *priv;
1369 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1371 if ((if_num = get_if(dvbnet)) < 0)
1374 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1375 NET_NAME_UNKNOWN, dvb_net_setup);
1379 if (dvbnet->dvbdev->id)
1380 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1381 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1383 /* compatibility fix to keep dvb0_0 format */
1384 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1385 dvbnet->dvbdev->adapter->num, if_num);
1388 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1390 dvbnet->device[if_num] = net;
1392 priv = netdev_priv(net);
1394 priv->demux = dvbnet->demux;
1396 priv->rx_mode = RX_MODE_UNI;
1397 priv->need_pusi = 1;
1399 priv->feedtype = feedtype;
1402 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1403 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1404 mutex_init(&priv->mutex);
1406 net->base_addr = pid;
1408 if ((result = register_netdev(net)) < 0) {
1409 dvbnet->device[if_num] = NULL;
1413 pr_info("created network interface %s\n", net->name);
1418 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1420 struct net_device *net = dvbnet->device[num];
1421 struct dvb_net_priv *priv;
1423 if (!dvbnet->state[num])
1425 priv = netdev_priv(net);
1430 flush_work(&priv->set_multicast_list_wq);
1431 flush_work(&priv->restart_net_feed_wq);
1432 pr_info("removed network interface %s\n", net->name);
1433 unregister_netdev(net);
1434 dvbnet->state[num]=0;
1435 dvbnet->device[num] = NULL;
1441 static int dvb_net_do_ioctl(struct file *file,
1442 unsigned int cmd, void *parg)
1444 struct dvb_device *dvbdev = file->private_data;
1445 struct dvb_net *dvbnet = dvbdev->priv;
1448 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1451 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1452 return -ERESTARTSYS;
1457 struct dvb_net_if *dvbnetif = parg;
1460 if (!capable(CAP_SYS_ADMIN)) {
1465 if (!try_module_get(dvbdev->adapter->module)) {
1470 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1472 module_put(dvbdev->adapter->module);
1476 dvbnetif->if_num=result;
1481 struct net_device *netdev;
1482 struct dvb_net_priv *priv_data;
1483 struct dvb_net_if *dvbnetif = parg;
1485 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1486 !dvbnet->state[dvbnetif->if_num]) {
1491 netdev = dvbnet->device[dvbnetif->if_num];
1493 priv_data = netdev_priv(netdev);
1494 dvbnetif->pid=priv_data->pid;
1495 dvbnetif->feedtype=priv_data->feedtype;
1500 if (!capable(CAP_SYS_ADMIN)) {
1504 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1508 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1510 module_put(dvbdev->adapter->module);
1514 /* binary compatibility cruft */
1515 case __NET_ADD_IF_OLD:
1517 struct __dvb_net_if_old *dvbnetif = parg;
1520 if (!capable(CAP_SYS_ADMIN)) {
1525 if (!try_module_get(dvbdev->adapter->module)) {
1530 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1532 module_put(dvbdev->adapter->module);
1536 dvbnetif->if_num=result;
1539 case __NET_GET_IF_OLD:
1541 struct net_device *netdev;
1542 struct dvb_net_priv *priv_data;
1543 struct __dvb_net_if_old *dvbnetif = parg;
1545 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1546 !dvbnet->state[dvbnetif->if_num]) {
1551 netdev = dvbnet->device[dvbnetif->if_num];
1553 priv_data = netdev_priv(netdev);
1554 dvbnetif->pid=priv_data->pid;
1563 mutex_unlock(&dvbnet->ioctl_mutex);
1567 static long dvb_net_ioctl(struct file *file,
1568 unsigned int cmd, unsigned long arg)
1570 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1573 static int dvb_net_close(struct inode *inode, struct file *file)
1575 struct dvb_device *dvbdev = file->private_data;
1576 struct dvb_net *dvbnet = dvbdev->priv;
1578 dvb_generic_release(inode, file);
1580 if(dvbdev->users == 1 && dvbnet->exit == 1)
1581 wake_up(&dvbdev->wait_queue);
1586 static const struct file_operations dvb_net_fops = {
1587 .owner = THIS_MODULE,
1588 .unlocked_ioctl = dvb_net_ioctl,
1589 .open = dvb_generic_open,
1590 .release = dvb_net_close,
1591 .llseek = noop_llseek,
1594 static const struct dvb_device dvbdev_net = {
1598 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1601 .fops = &dvb_net_fops,
1604 void dvb_net_release (struct dvb_net *dvbnet)
1609 if (dvbnet->dvbdev->users < 1)
1610 wait_event(dvbnet->dvbdev->wait_queue,
1611 dvbnet->dvbdev->users==1);
1613 dvb_unregister_device(dvbnet->dvbdev);
1615 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1616 if (!dvbnet->state[i])
1618 dvb_net_remove_if(dvbnet, i);
1621 EXPORT_SYMBOL(dvb_net_release);
1624 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1625 struct dmx_demux *dmx)
1629 mutex_init(&dvbnet->ioctl_mutex);
1630 dvbnet->demux = dmx;
1632 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1633 dvbnet->state[i] = 0;
1635 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
1636 dvbnet, DVB_DEVICE_NET, 0);
1638 EXPORT_SYMBOL(dvb_net_init);