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 * DVB_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 <media/dvb_demux.h>
68 #include <media/dvb_net.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
83 * The code inside DVB_ULE_DEBUG keeps a history of the
84 * last 100 TS cells processed.
86 static unsigned char ule_hist[100*TS_SZ] = { 0 };
87 static unsigned char *ule_where = ule_hist, ule_dump;
89 static void hexdump(const unsigned char *buf, unsigned short len)
91 print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
98 struct net_device *net;
100 struct dmx_demux *demux;
101 struct dmx_section_feed *secfeed;
102 struct dmx_section_filter *secfilter;
103 struct dmx_ts_feed *tsfeed;
105 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
106 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
108 #define RX_MODE_UNI 0
109 #define RX_MODE_MULTI 1
110 #define RX_MODE_ALL_MULTI 2
111 #define RX_MODE_PROMISC 3
112 struct work_struct set_multicast_list_wq;
113 struct work_struct restart_net_feed_wq;
114 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
115 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
116 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
117 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
118 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
119 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
120 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
121 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
122 unsigned char ule_dbit; /* Whether the DestMAC address present
123 * or not (bit is set). */
124 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
125 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
126 unsigned long ts_count; /* Current ts cell counter. */
132 * Determine the packet's protocol ID. The rule here is that we
133 * assume 802.3 if the type field is short enough to be a length.
134 * This is normal practice and works for any 'now in use' protocol.
136 * stolen from eth.c out of the linux kernel, hacked for dvb-device
139 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
140 struct net_device *dev)
145 skb_reset_mac_header(skb);
146 skb_pull(skb,dev->hard_header_len);
149 if (*eth->h_dest & 1) {
150 if(ether_addr_equal(eth->h_dest,dev->broadcast))
151 skb->pkt_type=PACKET_BROADCAST;
153 skb->pkt_type=PACKET_MULTICAST;
156 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
162 * This is a magic hack to spot IPX packets. Older Novell breaks
163 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
164 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
165 * won't work for fault tolerant netware but does for the rest.
167 if (*(unsigned short *)rawp == 0xFFFF)
168 return htons(ETH_P_802_3);
173 return htons(ETH_P_802_2);
184 /* ULE Extension Header handlers. */
187 #define ULE_BRIDGED 1
189 #define ULE_OPTEXTHDR_PADDING 0
191 static int ule_test_sndu( struct dvb_net_priv *p )
196 static int ule_bridged_sndu( struct dvb_net_priv *p )
198 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
199 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
200 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
201 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
202 if(framelen != ntohs(hdr->h_proto)) {
208 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
209 * It must be the final (or only) extension header specified in the header chain of a SNDU."
210 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
216 static int ule_exthdr_padding(struct dvb_net_priv *p)
222 * Handle ULE extension headers.
223 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
224 * Returns: >= 0: nr. of bytes consumed by next extension header
225 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
227 static int handle_one_ule_extension( struct dvb_net_priv *p )
229 /* Table of mandatory extension header handlers. The header type is the index. */
230 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
231 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
233 /* Table of optional extension header handlers. The header type is the index. */
234 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
235 { [0] = ule_exthdr_padding, [1] = NULL, };
238 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
239 unsigned char htype = p->ule_sndu_type & 0x00FF;
241 /* Discriminate mandatory and optional extension headers. */
243 /* Mandatory extension header */
244 if (ule_mandatory_ext_handlers[htype]) {
245 ext_len = ule_mandatory_ext_handlers[htype]( p );
247 p->ule_next_hdr += ext_len;
248 if (!p->ule_bridged) {
249 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
250 p->ule_next_hdr += 2;
252 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
253 /* This assures the extension handling loop will terminate. */
256 // else: extension handler failed or SNDU should be discarded
258 ext_len = -1; /* SNDU has to be discarded. */
260 /* Optional extension header. Calculate the length. */
262 /* Process the optional extension header according to its type. */
263 if (ule_optional_ext_handlers[htype])
264 (void)ule_optional_ext_handlers[htype]( p );
265 p->ule_next_hdr += ext_len;
266 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
268 * note: the length of the next header type is included in the
269 * length of THIS optional extension header
276 static int handle_ule_extensions( struct dvb_net_priv *p )
278 int total_ext_len = 0, l;
280 p->ule_next_hdr = p->ule_skb->data;
282 l = handle_one_ule_extension( p );
284 return l; /* Stop extension header processing and discard SNDU. */
286 pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
287 p->ule_next_hdr, (int)p->ule_sndu_type,
290 } while (p->ule_sndu_type < ETH_P_802_3_MIN);
292 return total_ext_len;
296 /* Prepare for a new ULE SNDU: reset the decoder state. */
297 static inline void reset_ule( struct dvb_net_priv *p )
300 p->ule_next_hdr = NULL;
302 p->ule_sndu_type = 0;
303 p->ule_sndu_type_1 = 0;
304 p->ule_sndu_remain = 0;
310 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
311 * TS cells of a single PID.
314 struct dvb_net_ule_handle {
315 struct net_device *dev;
316 struct dvb_net_priv *priv;
320 unsigned long skipped;
321 const u8 *ts, *ts_end, *from_where;
322 u8 ts_remain, how_much, new_ts;
326 static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h)
328 /* We are about to process a new TS cell. */
331 if (ule_where >= &ule_hist[100*TS_SZ])
332 ule_where = ule_hist;
333 memcpy(ule_where, h->ts, TS_SZ);
335 hexdump(ule_where, TS_SZ);
342 * Check TS h->error conditions: sync_byte, transport_error_indicator,
343 * scrambling_control .
345 if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) ||
346 ((h->ts[3] & TS_SC) != 0)) {
347 pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
348 h->priv->ts_count, h->ts[0],
349 (h->ts[1] & TS_TEI) >> 7,
350 (h->ts[3] & TS_SC) >> 6);
352 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
353 if (h->priv->ule_skb) {
354 dev_kfree_skb(h->priv->ule_skb);
355 /* Prepare for next SNDU. */
356 h->dev->stats.rx_errors++;
357 h->dev->stats.rx_frame_errors++;
360 h->priv->need_pusi = 1;
362 /* Continue with next TS cell. */
369 h->from_where = h->ts + 4;
374 static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h)
376 if (h->ts[1] & TS_PUSI) {
377 /* Find beginning of first ULE SNDU in current TS cell. */
378 /* Synchronize continuity counter. */
379 h->priv->tscc = h->ts[3] & 0x0F;
380 /* There is a pointer field here. */
381 if (h->ts[4] > h->ts_remain) {
382 pr_err("%lu: Invalid ULE packet (pointer field %d)\n",
383 h->priv->ts_count, h->ts[4]);
388 /* Skip to destination of pointer field. */
389 h->from_where = &h->ts[5] + h->ts[4];
390 h->ts_remain -= 1 + h->ts[4];
402 static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h)
404 /* Check continuity counter. */
405 if ((h->ts[3] & 0x0F) == h->priv->tscc)
406 h->priv->tscc = (h->priv->tscc + 1) & 0x0F;
408 /* TS discontinuity handling: */
409 pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n",
410 h->priv->ts_count, h->ts[3] & 0x0F,
412 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
413 if (h->priv->ule_skb) {
414 dev_kfree_skb(h->priv->ule_skb);
415 /* Prepare for next SNDU. */
416 // reset_ule(h->priv); moved to below.
417 h->dev->stats.rx_errors++;
418 h->dev->stats.rx_frame_errors++;
421 /* skip to next PUSI. */
422 h->priv->need_pusi = 1;
426 * If we still have an incomplete payload, but PUSI is
427 * set; some TS cells are missing.
428 * This is only possible here, if we missed exactly 16 TS
429 * cells (continuity counter wrap).
431 if (h->ts[1] & TS_PUSI) {
432 if (!h->priv->need_pusi) {
433 if (!(*h->from_where < (h->ts_remain-1)) ||
434 *h->from_where != h->priv->ule_sndu_remain) {
436 * Pointer field is invalid.
437 * Drop this TS cell and any started ULE SNDU.
439 pr_warn("%lu: Invalid pointer field: %u.\n",
444 * Drop partly decoded SNDU, reset state,
447 if (h->priv->ule_skb) {
449 dev_kfree_skb(h->priv->ule_skb);
452 if (h->error || h->priv->ule_sndu_remain) {
453 h->dev->stats.rx_errors++;
454 h->dev->stats.rx_frame_errors++;
459 h->priv->need_pusi = 1;
463 * Skip pointer field (we're processing a
469 h->priv->need_pusi = 0;
471 if (h->priv->ule_sndu_remain > 183) {
473 * Current SNDU lacks more data than there
474 * could be available in the current TS cell.
476 h->dev->stats.rx_errors++;
477 h->dev->stats.rx_length_errors++;
478 pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d). Flushing incomplete payload.\n",
480 h->priv->ule_sndu_remain,
481 h->ts[4], h->ts_remain);
482 dev_kfree_skb(h->priv->ule_skb);
483 /* Prepare for next SNDU. */
486 * Resync: go to where pointer field points to:
487 * start of next ULE SNDU.
489 h->from_where += h->ts[4];
490 h->ts_remain -= h->ts[4];
498 * Start a new payload with skb.
499 * Find ULE header. It is only guaranteed that the
500 * length field (2 bytes) is contained in the current
502 * Check h.ts_remain has to be >= 2 here.
504 static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h)
506 if (h->ts_remain < 2) {
507 pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n",
509 h->priv->ule_sndu_len = 0;
510 h->priv->need_pusi = 1;
515 if (!h->priv->ule_sndu_len) {
516 /* Got at least two bytes, thus extrace the SNDU length. */
517 h->priv->ule_sndu_len = h->from_where[0] << 8 |
519 if (h->priv->ule_sndu_len & 0x8000) {
520 /* D-Bit is set: no dest mac present. */
521 h->priv->ule_sndu_len &= 0x7FFF;
522 h->priv->ule_dbit = 1;
524 h->priv->ule_dbit = 0;
526 if (h->priv->ule_sndu_len < 5) {
527 pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n",
529 h->priv->ule_sndu_len);
530 h->dev->stats.rx_errors++;
531 h->dev->stats.rx_length_errors++;
532 h->priv->ule_sndu_len = 0;
533 h->priv->need_pusi = 1;
539 h->ts_remain -= 2; /* consume the 2 bytes SNDU length. */
543 h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2;
545 * State of current TS:
546 * h->ts_remain (remaining bytes in the current TS cell)
547 * 0 ule_type is not available now, we need the next TS cell
548 * 1 the first byte of the ule_type is present
549 * >=2 full ULE header present, maybe some payload data as well.
551 switch (h->ts_remain) {
553 h->priv->ule_sndu_remain--;
554 h->priv->ule_sndu_type = h->from_where[0] << 8;
556 /* first byte of ule_type is set. */
557 h->priv->ule_sndu_type_1 = 1;
567 default: /* complete ULE header is present in current TS. */
568 /* Extract ULE type field. */
569 if (h->priv->ule_sndu_type_1) {
570 h->priv->ule_sndu_type_1 = 0;
571 h->priv->ule_sndu_type |= h->from_where[0];
572 h->from_where += 1; /* points to payload start. */
575 /* Complete type is present in new TS. */
576 h->priv->ule_sndu_type = h->from_where[0] << 8 |
578 h->from_where += 2; /* points to payload start. */
585 * Allocate the skb (decoder target buffer) with the correct size,
588 * prepare for the largest case: bridged SNDU with MAC address
591 h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len +
592 ETH_HLEN + ETH_ALEN);
593 if (!h->priv->ule_skb) {
594 pr_notice("%s: Memory squeeze, dropping packet.\n",
596 h->dev->stats.rx_dropped++;
600 /* This includes the CRC32 _and_ dest mac, if !dbit. */
601 h->priv->ule_sndu_remain = h->priv->ule_sndu_len;
602 h->priv->ule_skb->dev = h->dev;
604 * Leave space for Ethernet or bridged SNDU header
605 * (eth hdr plus one MAC addr).
607 skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN);
613 static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h)
615 static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff };
618 * The destination MAC address is the next data in the skb. It comes
619 * before any extension headers.
621 * Check if the payload of this SNDU should be passed up the stack.
623 if (h->priv->rx_mode == RX_MODE_PROMISC)
626 if (h->priv->ule_skb->data[0] & 0x01) {
627 /* multicast or broadcast */
628 if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) {
630 if (h->priv->rx_mode == RX_MODE_MULTI) {
633 for (i = 0; i < h->priv->multi_num &&
634 !ether_addr_equal(h->priv->ule_skb->data,
635 h->priv->multi_macs[i]);
638 if (i == h->priv->multi_num)
640 } else if (h->priv->rx_mode != RX_MODE_ALL_MULTI)
641 return 1; /* no broadcast; */
644 * all multicast mode: accept all multicast packets
647 /* else: broadcast */
648 } else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr))
655 static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h,
657 u32 ule_crc, u32 expected_crc)
659 u8 dest_addr[ETH_ALEN];
661 if (ule_crc != expected_crc) {
662 pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
663 h->priv->ts_count, ule_crc, expected_crc,
664 h->priv->ule_sndu_len, h->priv->ule_sndu_type,
667 *(unsigned short *)h->from_where : 0);
670 hexdump(iov[0].iov_base, iov[0].iov_len);
671 hexdump(iov[1].iov_base, iov[1].iov_len);
672 hexdump(iov[2].iov_base, iov[2].iov_len);
674 if (ule_where == ule_hist) {
675 hexdump(&ule_hist[98*TS_SZ], TS_SZ);
676 hexdump(&ule_hist[99*TS_SZ], TS_SZ);
677 } else if (ule_where == &ule_hist[TS_SZ]) {
678 hexdump(&ule_hist[99*TS_SZ], TS_SZ);
679 hexdump(ule_hist, TS_SZ);
681 hexdump(ule_where - TS_SZ - TS_SZ, TS_SZ);
682 hexdump(ule_where - TS_SZ, TS_SZ);
687 h->dev->stats.rx_errors++;
688 h->dev->stats.rx_crc_errors++;
689 dev_kfree_skb(h->priv->ule_skb);
694 /* CRC32 verified OK. */
696 /* CRC32 was OK, so remove it from skb. */
697 h->priv->ule_skb->tail -= 4;
698 h->priv->ule_skb->len -= 4;
700 if (!h->priv->ule_dbit) {
701 if (dvb_net_ule_should_drop(h)) {
703 "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n",
704 h->priv->ule_skb->data, h->dev->dev_addr);
705 dev_kfree_skb(h->priv->ule_skb);
709 skb_copy_from_linear_data(h->priv->ule_skb, dest_addr,
711 skb_pull(h->priv->ule_skb, ETH_ALEN);
713 /* dest_addr buffer is only valid if h->priv->ule_dbit == 0 */
714 eth_zero_addr(dest_addr);
717 /* Handle ULE Extension Headers. */
718 if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) {
719 /* There is an extension header. Handle it accordingly. */
720 int l = handle_ule_extensions(h->priv);
724 * Mandatory extension header unknown or TEST SNDU.
728 // pr_warn("Dropping SNDU, extension headers.\n" );
729 dev_kfree_skb(h->priv->ule_skb);
732 skb_pull(h->priv->ule_skb, l);
736 * Construct/assure correct ethernet header.
737 * Note: in bridged mode (h->priv->ule_bridged != 0)
738 * we already have the (original) ethernet
739 * header at the start of the payload (after
740 * optional dest. address and any extension
743 if (!h->priv->ule_bridged) {
744 skb_push(h->priv->ule_skb, ETH_HLEN);
745 h->ethh = (struct ethhdr *)h->priv->ule_skb->data;
746 memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN);
747 eth_zero_addr(h->ethh->h_source);
748 h->ethh->h_proto = htons(h->priv->ule_sndu_type);
750 /* else: skb is in correct state; nothing to do. */
751 h->priv->ule_bridged = 0;
753 /* Stuff into kernel's protocol stack. */
754 h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb,
757 * If D-bit is set (i.e. destination MAC address not present),
758 * receive the packet anyhow.
761 if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
762 h->priv->ule_skb->pkt_type = PACKET_HOST;
764 h->dev->stats.rx_packets++;
765 h->dev->stats.rx_bytes += h->priv->ule_skb->len;
766 netif_rx(h->priv->ule_skb);
769 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len)
772 struct dvb_net_ule_handle h = {
774 .priv = netdev_priv(dev),
789 * For all TS cells in current buffer.
790 * Appearently, we are called for every single TS cell.
792 for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len;
793 h.ts < h.ts_end; /* no incr. */) {
795 /* We are about to process a new TS cell. */
796 if (dvb_net_ule_new_ts_cell(&h))
800 /* Synchronize on PUSI, if required. */
801 if (h.priv->need_pusi) {
802 if (dvb_net_ule_ts_pusi(&h))
807 if (dvb_net_ule_new_ts(&h))
811 /* Check if new payload needs to be started. */
812 if (h.priv->ule_skb == NULL) {
813 ret = dvb_net_ule_new_payload(&h);
820 /* Copy data into our current skb. */
821 h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain);
822 skb_put_data(h.priv->ule_skb, h.from_where, h.how_much);
823 h.priv->ule_sndu_remain -= h.how_much;
824 h.ts_remain -= h.how_much;
825 h.from_where += h.how_much;
827 /* Check for complete payload. */
828 if (h.priv->ule_sndu_remain <= 0) {
829 /* Check CRC32, we've got it in our skb already. */
830 __be16 ulen = htons(h.priv->ule_sndu_len);
831 __be16 utype = htons(h.priv->ule_sndu_type);
833 struct kvec iov[3] = {
834 { &ulen, sizeof ulen },
835 { &utype, sizeof utype },
836 { h.priv->ule_skb->data,
837 h.priv->ule_skb->len - 4 }
839 u32 ule_crc = ~0L, expected_crc;
840 if (h.priv->ule_dbit) {
841 /* Set D-bit for CRC32 verification,
842 * if it was set originally. */
843 ulen |= htons(0x8000);
846 ule_crc = iov_crc32(ule_crc, iov, 3);
847 tail = skb_tail_pointer(h.priv->ule_skb);
848 expected_crc = *(tail - 4) << 24 |
853 dvb_net_ule_check_crc(&h, iov, ule_crc, expected_crc);
855 /* Prepare for next SNDU. */
859 /* More data in current TS (look at the bytes following the CRC32)? */
860 if (h.ts_remain >= 2 && *((unsigned short *)h.from_where) != 0xFFFF) {
861 /* Next ULE SNDU starts right there. */
863 h.priv->ule_skb = NULL;
864 h.priv->ule_sndu_type_1 = 0;
865 h.priv->ule_sndu_len = 0;
866 // pr_warn("More data in current TS: [%#x %#x %#x %#x]\n",
867 // *(h.from_where + 0), *(h.from_where + 1),
868 // *(h.from_where + 2), *(h.from_where + 3));
869 // pr_warn("h.ts @ %p, stopped @ %p:\n", h.ts, h.from_where + 0);
870 // hexdump(h.ts, 188);
875 if (h.priv->ule_skb == NULL) {
876 h.priv->need_pusi = 1;
877 h.priv->ule_sndu_type_1 = 0;
878 h.priv->ule_sndu_len = 0;
881 } /* for all available TS cells */
884 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
885 const u8 *buffer2, size_t buffer2_len,
886 struct dmx_ts_feed *feed)
888 struct net_device *dev = feed->priv;
891 pr_warn("buffer2 not NULL: %p.\n", buffer2);
892 if (buffer1_len > 32768)
893 pr_warn("length > 32k: %zu.\n", buffer1_len);
894 /* pr_info("TS callback: %u bytes, %u TS cells @ %p.\n",
895 buffer1_len, buffer1_len / TS_SZ, buffer1); */
896 dvb_net_ule(dev, buffer1, buffer1_len);
901 static void dvb_net_sec(struct net_device *dev,
902 const u8 *pkt, int pkt_len)
906 struct net_device_stats *stats = &dev->stats;
909 /* note: pkt_len includes a 32bit checksum */
911 pr_warn("%s: IP/MPE packet length = %d too small.\n",
914 stats->rx_length_errors++;
917 /* it seems some ISPs manage to screw up here, so we have to
918 * relax the error checks... */
920 if ((pkt[5] & 0xfd) != 0xc1) {
921 /* drop scrambled or broken packets */
923 if ((pkt[5] & 0x3c) != 0x00) {
927 stats->rx_crc_errors++;
931 /* handle LLC/SNAP, see rfc-1042 */
932 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
939 /* FIXME: assemble datagram from multiple sections */
941 stats->rx_frame_errors++;
945 /* we have 14 byte ethernet header (ip header follows);
946 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
948 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
949 //pr_notice("%s: Memory squeeze, dropping packet.\n", dev->name);
953 skb_reserve(skb, 2); /* longword align L3 header */
956 /* copy L3 payload */
957 eth = skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
958 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
960 /* create ethernet header: */
968 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
974 /* protocol numbers are from rfc-1700 or
975 * http://www.iana.org/assignments/ethernet-numbers
977 if (pkt[12] >> 4 == 6) { /* version field from IP header */
978 eth[12] = 0x86; /* IPv6 */
981 eth[12] = 0x08; /* IPv4 */
986 skb->protocol = dvb_net_eth_type_trans(skb, dev);
989 stats->rx_bytes+=skb->len;
993 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
994 const u8 *buffer2, size_t buffer2_len,
995 struct dmx_section_filter *filter)
997 struct net_device *dev = filter->priv;
1000 * we rely on the DVB API definition where exactly one complete
1001 * section is delivered in buffer1
1003 dvb_net_sec (dev, buffer1, buffer1_len);
1007 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
1010 return NETDEV_TX_OK;
1013 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1014 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
1015 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
1016 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1018 static int dvb_net_filter_sec_set(struct net_device *dev,
1019 struct dmx_section_filter **secfilter,
1020 u8 *mac, u8 *mac_mask)
1022 struct dvb_net_priv *priv = netdev_priv(dev);
1026 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
1028 pr_err("%s: could not get filter\n", dev->name);
1032 (*secfilter)->priv=(void *) dev;
1034 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
1035 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
1036 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
1038 (*secfilter)->filter_value[0]=0x3e;
1039 (*secfilter)->filter_value[3]=mac[5];
1040 (*secfilter)->filter_value[4]=mac[4];
1041 (*secfilter)->filter_value[8]=mac[3];
1042 (*secfilter)->filter_value[9]=mac[2];
1043 (*secfilter)->filter_value[10]=mac[1];
1044 (*secfilter)->filter_value[11]=mac[0];
1046 (*secfilter)->filter_mask[0] = 0xff;
1047 (*secfilter)->filter_mask[3] = mac_mask[5];
1048 (*secfilter)->filter_mask[4] = mac_mask[4];
1049 (*secfilter)->filter_mask[8] = mac_mask[3];
1050 (*secfilter)->filter_mask[9] = mac_mask[2];
1051 (*secfilter)->filter_mask[10] = mac_mask[1];
1052 (*secfilter)->filter_mask[11]=mac_mask[0];
1054 netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
1059 static int dvb_net_feed_start(struct net_device *dev)
1062 struct dvb_net_priv *priv = netdev_priv(dev);
1063 struct dmx_demux *demux = priv->demux;
1064 unsigned char *mac = (unsigned char *) dev->dev_addr;
1066 netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode);
1067 mutex_lock(&priv->mutex);
1068 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
1069 pr_err("%s: BUG %d\n", __func__, __LINE__);
1072 priv->secfilter=NULL;
1073 priv->tsfeed = NULL;
1075 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1076 netdev_dbg(dev, "alloc secfeed\n");
1077 ret=demux->allocate_section_feed(demux, &priv->secfeed,
1078 dvb_net_sec_callback);
1080 pr_err("%s: could not allocate section feed\n",
1085 ret = priv->secfeed->set(priv->secfeed, priv->pid, 1);
1088 pr_err("%s: could not set section feed\n", dev->name);
1089 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1094 if (priv->rx_mode != RX_MODE_PROMISC) {
1095 netdev_dbg(dev, "set secfilter\n");
1096 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
1099 switch (priv->rx_mode) {
1101 for (i = 0; i < priv->multi_num; i++) {
1102 netdev_dbg(dev, "set multi_secfilter[%d]\n", i);
1103 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1104 priv->multi_macs[i], mask_normal);
1107 case RX_MODE_ALL_MULTI:
1109 netdev_dbg(dev, "set multi_secfilter[0]\n");
1110 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1111 mac_allmulti, mask_allmulti);
1113 case RX_MODE_PROMISC:
1115 netdev_dbg(dev, "set secfilter\n");
1116 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1120 netdev_dbg(dev, "start filtering\n");
1121 priv->secfeed->start_filtering(priv->secfeed);
1122 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1123 ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
1125 /* we have payloads encapsulated in TS */
1126 netdev_dbg(dev, "alloc tsfeed\n");
1127 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1129 pr_err("%s: could not allocate ts feed\n", dev->name);
1133 /* Set netdevice pointer for ts decaps callback. */
1134 priv->tsfeed->priv = (void *)dev;
1135 ret = priv->tsfeed->set(priv->tsfeed,
1136 priv->pid, /* pid */
1137 TS_PACKET, /* type */
1138 DMX_PES_OTHER, /* pes type */
1139 timeout /* timeout */
1143 pr_err("%s: could not set ts feed\n", dev->name);
1144 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1145 priv->tsfeed = NULL;
1149 netdev_dbg(dev, "start filtering\n");
1150 priv->tsfeed->start_filtering(priv->tsfeed);
1155 mutex_unlock(&priv->mutex);
1159 static int dvb_net_feed_stop(struct net_device *dev)
1161 struct dvb_net_priv *priv = netdev_priv(dev);
1164 mutex_lock(&priv->mutex);
1165 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1166 if (priv->secfeed) {
1167 if (priv->secfeed->is_filtering) {
1168 netdev_dbg(dev, "stop secfeed\n");
1169 priv->secfeed->stop_filtering(priv->secfeed);
1172 if (priv->secfilter) {
1173 netdev_dbg(dev, "release secfilter\n");
1174 priv->secfeed->release_filter(priv->secfeed,
1176 priv->secfilter=NULL;
1179 for (i=0; i<priv->multi_num; i++) {
1180 if (priv->multi_secfilter[i]) {
1181 netdev_dbg(dev, "release multi_filter[%d]\n",
1183 priv->secfeed->release_filter(priv->secfeed,
1184 priv->multi_secfilter[i]);
1185 priv->multi_secfilter[i] = NULL;
1189 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1190 priv->secfeed = NULL;
1192 pr_err("%s: no feed to stop\n", dev->name);
1193 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1195 if (priv->tsfeed->is_filtering) {
1196 netdev_dbg(dev, "stop tsfeed\n");
1197 priv->tsfeed->stop_filtering(priv->tsfeed);
1199 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1200 priv->tsfeed = NULL;
1203 pr_err("%s: no ts feed to stop\n", dev->name);
1206 mutex_unlock(&priv->mutex);
1211 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
1213 struct dvb_net_priv *priv = netdev_priv(dev);
1215 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1218 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
1225 static void wq_set_multicast_list (struct work_struct *work)
1227 struct dvb_net_priv *priv =
1228 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1229 struct net_device *dev = priv->net;
1231 dvb_net_feed_stop(dev);
1232 priv->rx_mode = RX_MODE_UNI;
1233 netif_addr_lock_bh(dev);
1235 if (dev->flags & IFF_PROMISC) {
1236 netdev_dbg(dev, "promiscuous mode\n");
1237 priv->rx_mode = RX_MODE_PROMISC;
1238 } else if ((dev->flags & IFF_ALLMULTI)) {
1239 netdev_dbg(dev, "allmulti mode\n");
1240 priv->rx_mode = RX_MODE_ALL_MULTI;
1241 } else if (!netdev_mc_empty(dev)) {
1242 struct netdev_hw_addr *ha;
1244 netdev_dbg(dev, "set_mc_list, %d entries\n",
1245 netdev_mc_count(dev));
1247 priv->rx_mode = RX_MODE_MULTI;
1248 priv->multi_num = 0;
1250 netdev_for_each_mc_addr(ha, dev)
1251 dvb_set_mc_filter(dev, ha->addr);
1254 netif_addr_unlock_bh(dev);
1255 dvb_net_feed_start(dev);
1259 static void dvb_net_set_multicast_list (struct net_device *dev)
1261 struct dvb_net_priv *priv = netdev_priv(dev);
1262 schedule_work(&priv->set_multicast_list_wq);
1266 static void wq_restart_net_feed (struct work_struct *work)
1268 struct dvb_net_priv *priv =
1269 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1270 struct net_device *dev = priv->net;
1272 if (netif_running(dev)) {
1273 dvb_net_feed_stop(dev);
1274 dvb_net_feed_start(dev);
1279 static int dvb_net_set_mac (struct net_device *dev, void *p)
1281 struct dvb_net_priv *priv = netdev_priv(dev);
1282 struct sockaddr *addr=p;
1284 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1286 if (netif_running(dev))
1287 schedule_work(&priv->restart_net_feed_wq);
1293 static int dvb_net_open(struct net_device *dev)
1295 struct dvb_net_priv *priv = netdev_priv(dev);
1298 dvb_net_feed_start(dev);
1303 static int dvb_net_stop(struct net_device *dev)
1305 struct dvb_net_priv *priv = netdev_priv(dev);
1308 return dvb_net_feed_stop(dev);
1311 static const struct header_ops dvb_header_ops = {
1312 .create = eth_header,
1313 .parse = eth_header_parse,
1317 static const struct net_device_ops dvb_netdev_ops = {
1318 .ndo_open = dvb_net_open,
1319 .ndo_stop = dvb_net_stop,
1320 .ndo_start_xmit = dvb_net_tx,
1321 .ndo_set_rx_mode = dvb_net_set_multicast_list,
1322 .ndo_set_mac_address = dvb_net_set_mac,
1323 .ndo_validate_addr = eth_validate_addr,
1326 static void dvb_net_setup(struct net_device *dev)
1330 dev->header_ops = &dvb_header_ops;
1331 dev->netdev_ops = &dvb_netdev_ops;
1333 dev->max_mtu = 4096;
1335 dev->flags |= IFF_NOARP;
1338 static int get_if(struct dvb_net *dvbnet)
1342 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1343 if (!dvbnet->state[i])
1346 if (i == DVB_NET_DEVICES_MAX)
1353 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1355 struct net_device *net;
1356 struct dvb_net_priv *priv;
1360 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1362 if ((if_num = get_if(dvbnet)) < 0)
1365 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1366 NET_NAME_UNKNOWN, dvb_net_setup);
1370 if (dvbnet->dvbdev->id)
1371 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1372 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1374 /* compatibility fix to keep dvb0_0 format */
1375 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1376 dvbnet->dvbdev->adapter->num, if_num);
1379 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1381 dvbnet->device[if_num] = net;
1383 priv = netdev_priv(net);
1385 priv->demux = dvbnet->demux;
1387 priv->rx_mode = RX_MODE_UNI;
1388 priv->need_pusi = 1;
1390 priv->feedtype = feedtype;
1393 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1394 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1395 mutex_init(&priv->mutex);
1397 net->base_addr = pid;
1399 if ((result = register_netdev(net)) < 0) {
1400 dvbnet->device[if_num] = NULL;
1404 pr_info("created network interface %s\n", net->name);
1409 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1411 struct net_device *net = dvbnet->device[num];
1412 struct dvb_net_priv *priv;
1414 if (!dvbnet->state[num])
1416 priv = netdev_priv(net);
1421 flush_work(&priv->set_multicast_list_wq);
1422 flush_work(&priv->restart_net_feed_wq);
1423 pr_info("removed network interface %s\n", net->name);
1424 unregister_netdev(net);
1425 dvbnet->state[num]=0;
1426 dvbnet->device[num] = NULL;
1432 static int dvb_net_do_ioctl(struct file *file,
1433 unsigned int cmd, void *parg)
1435 struct dvb_device *dvbdev = file->private_data;
1436 struct dvb_net *dvbnet = dvbdev->priv;
1439 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1442 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1443 return -ERESTARTSYS;
1448 struct dvb_net_if *dvbnetif = parg;
1451 if (!capable(CAP_SYS_ADMIN)) {
1456 if (!try_module_get(dvbdev->adapter->module)) {
1461 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1463 module_put(dvbdev->adapter->module);
1467 dvbnetif->if_num=result;
1472 struct net_device *netdev;
1473 struct dvb_net_priv *priv_data;
1474 struct dvb_net_if *dvbnetif = parg;
1476 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1477 !dvbnet->state[dvbnetif->if_num]) {
1482 netdev = dvbnet->device[dvbnetif->if_num];
1484 priv_data = netdev_priv(netdev);
1485 dvbnetif->pid=priv_data->pid;
1486 dvbnetif->feedtype=priv_data->feedtype;
1491 if (!capable(CAP_SYS_ADMIN)) {
1495 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1499 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1501 module_put(dvbdev->adapter->module);
1505 /* binary compatibility cruft */
1506 case __NET_ADD_IF_OLD:
1508 struct __dvb_net_if_old *dvbnetif = parg;
1511 if (!capable(CAP_SYS_ADMIN)) {
1516 if (!try_module_get(dvbdev->adapter->module)) {
1521 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1523 module_put(dvbdev->adapter->module);
1527 dvbnetif->if_num=result;
1530 case __NET_GET_IF_OLD:
1532 struct net_device *netdev;
1533 struct dvb_net_priv *priv_data;
1534 struct __dvb_net_if_old *dvbnetif = parg;
1536 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1537 !dvbnet->state[dvbnetif->if_num]) {
1542 netdev = dvbnet->device[dvbnetif->if_num];
1544 priv_data = netdev_priv(netdev);
1545 dvbnetif->pid=priv_data->pid;
1554 mutex_unlock(&dvbnet->ioctl_mutex);
1558 static long dvb_net_ioctl(struct file *file,
1559 unsigned int cmd, unsigned long arg)
1561 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1564 static int dvb_net_close(struct inode *inode, struct file *file)
1566 struct dvb_device *dvbdev = file->private_data;
1567 struct dvb_net *dvbnet = dvbdev->priv;
1569 dvb_generic_release(inode, file);
1571 if(dvbdev->users == 1 && dvbnet->exit == 1)
1572 wake_up(&dvbdev->wait_queue);
1577 static const struct file_operations dvb_net_fops = {
1578 .owner = THIS_MODULE,
1579 .unlocked_ioctl = dvb_net_ioctl,
1580 .open = dvb_generic_open,
1581 .release = dvb_net_close,
1582 .llseek = noop_llseek,
1585 static const struct dvb_device dvbdev_net = {
1589 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1592 .fops = &dvb_net_fops,
1595 void dvb_net_release (struct dvb_net *dvbnet)
1600 if (dvbnet->dvbdev->users < 1)
1601 wait_event(dvbnet->dvbdev->wait_queue,
1602 dvbnet->dvbdev->users==1);
1604 dvb_unregister_device(dvbnet->dvbdev);
1606 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1607 if (!dvbnet->state[i])
1609 dvb_net_remove_if(dvbnet, i);
1612 EXPORT_SYMBOL(dvb_net_release);
1615 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1616 struct dmx_demux *dmx)
1620 mutex_init(&dvbnet->ioctl_mutex);
1621 dvbnet->demux = dmx;
1623 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1624 dvbnet->state[i] = 0;
1626 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
1627 dvbnet, DVB_DEVICE_NET, 0);
1629 EXPORT_SYMBOL(dvb_net_init);