4 * Copyright (C) 2001 Convergence integrated media GmbH
5 * Ralph Metzler <ralph@convergence.de>
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
8 * ULE Decapsulation code:
9 * Copyright (C) 2003, 2004 gcs - Global Communication & Services GmbH.
10 * and Department of Scientific Computing
11 * Paris Lodron University of Salzburg.
12 * Hilmar Linder <hlinder@cosy.sbg.ac.at>
13 * and Wolfram Stering <wstering@cosy.sbg.ac.at>
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.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
35 * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt
37 * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt:
38 * ULE Extension header handling.
39 * Bugreports by Moritz Vieth and Hanno Tersteegen,
40 * Fraunhofer Institute for Open Communication Systems
41 * Competence Center for Advanced Satellite Communications.
42 * Bugfixes and robustness improvements.
43 * Filtering on dest MAC addresses, if present (D-Bit = 0)
44 * ULE_DEBUG compile-time option.
45 * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by
46 * Christian Praehauser <cpraehaus@cosy.sbg.ac.at>,
47 * Paris Lodron University of Salzburg.
51 * FIXME / TODO (dvb_net.c):
53 * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/netdevice.h>
60 #include <linux/nospec.h>
61 #include <linux/etherdevice.h>
62 #include <linux/dvb/net.h>
63 #include <linux/uio.h>
64 #include <asm/uaccess.h>
65 #include <linux/crc32.h>
66 #include <linux/mutex.h>
67 #include <linux/sched.h>
69 #include "dvb_demux.h"
72 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
75 for (j = 0; j < cnt; j++)
76 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
81 #define DVB_NET_MULTICAST_MAX 10
87 static void hexdump(const unsigned char *buf, unsigned short len)
89 print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true);
97 struct net_device *net;
99 struct dmx_demux *demux;
100 struct dmx_section_feed *secfeed;
101 struct dmx_section_filter *secfilter;
102 struct dmx_ts_feed *tsfeed;
104 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
105 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
107 #define RX_MODE_UNI 0
108 #define RX_MODE_MULTI 1
109 #define RX_MODE_ALL_MULTI 2
110 #define RX_MODE_PROMISC 3
111 struct work_struct set_multicast_list_wq;
112 struct work_struct restart_net_feed_wq;
113 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
114 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
115 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
116 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
117 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
118 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
119 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
120 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
121 unsigned char ule_dbit; /* Whether the DestMAC address present
122 * or not (bit is set). */
123 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
124 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
125 unsigned long ts_count; /* Current ts cell counter. */
131 * Determine the packet's protocol ID. The rule here is that we
132 * assume 802.3 if the type field is short enough to be a length.
133 * This is normal practice and works for any 'now in use' protocol.
135 * stolen from eth.c out of the linux kernel, hacked for dvb-device
136 * by Michael Holzt <kju@debian.org>
138 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
139 struct net_device *dev)
144 skb_reset_mac_header(skb);
145 skb_pull(skb,dev->hard_header_len);
148 if (*eth->h_dest & 1) {
149 if(ether_addr_equal(eth->h_dest,dev->broadcast))
150 skb->pkt_type=PACKET_BROADCAST;
152 skb->pkt_type=PACKET_MULTICAST;
155 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
161 * This is a magic hack to spot IPX packets. Older Novell breaks
162 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
163 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
164 * won't work for fault tolerant netware but does for the rest.
166 if (*(unsigned short *)rawp == 0xFFFF)
167 return htons(ETH_P_802_3);
172 return htons(ETH_P_802_2);
183 /* ULE Extension Header handlers. */
186 #define ULE_BRIDGED 1
188 #define ULE_OPTEXTHDR_PADDING 0
190 static int ule_test_sndu( struct dvb_net_priv *p )
195 static int ule_bridged_sndu( struct dvb_net_priv *p )
197 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
198 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) {
199 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
200 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */
201 if(framelen != ntohs(hdr->h_proto)) {
207 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
208 * It must be the final (or only) extension header specified in the header chain of a SNDU."
209 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
215 static int ule_exthdr_padding(struct dvb_net_priv *p)
220 /** Handle ULE extension headers.
221 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
222 * Returns: >= 0: nr. of bytes consumed by next extension header
223 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
225 static int handle_one_ule_extension( struct dvb_net_priv *p )
227 /* Table of mandatory extension header handlers. The header type is the index. */
228 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
229 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
231 /* Table of optional extension header handlers. The header type is the index. */
232 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
233 { [0] = ule_exthdr_padding, [1] = NULL, };
236 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
237 unsigned char htype = p->ule_sndu_type & 0x00FF;
239 /* Discriminate mandatory and optional extension headers. */
241 /* Mandatory extension header */
242 if (ule_mandatory_ext_handlers[htype]) {
243 ext_len = ule_mandatory_ext_handlers[htype]( p );
245 p->ule_next_hdr += ext_len;
246 if (!p->ule_bridged) {
247 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
248 p->ule_next_hdr += 2;
250 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
251 /* This assures the extension handling loop will terminate. */
254 // else: extension handler failed or SNDU should be discarded
256 ext_len = -1; /* SNDU has to be discarded. */
258 /* Optional extension header. Calculate the length. */
260 /* Process the optional extension header according to its type. */
261 if (ule_optional_ext_handlers[htype])
262 (void)ule_optional_ext_handlers[htype]( p );
263 p->ule_next_hdr += ext_len;
264 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
266 * note: the length of the next header type is included in the
267 * length of THIS optional extension header
274 static int handle_ule_extensions( struct dvb_net_priv *p )
276 int total_ext_len = 0, l;
278 p->ule_next_hdr = p->ule_skb->data;
280 l = handle_one_ule_extension( p );
282 return l; /* Stop extension header processing and discard SNDU. */
285 pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n",
286 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.
313 static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
315 struct dvb_net_priv *priv = netdev_priv(dev);
316 unsigned long skipped = 0L;
317 const u8 *ts, *ts_end, *from_where = NULL;
318 u8 ts_remain = 0, how_much = 0, new_ts = 1;
319 struct ethhdr *ethh = NULL;
323 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
324 static unsigned char ule_hist[100*TS_SZ];
325 static unsigned char *ule_where = ule_hist, ule_dump;
328 /* For all TS cells in current buffer.
329 * Appearently, we are called for every single TS cell.
331 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
334 /* We are about to process a new TS cell. */
337 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
338 memcpy( ule_where, ts, TS_SZ );
340 hexdump( ule_where, TS_SZ );
346 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
347 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
348 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
349 priv->ts_count, ts[0],
350 (ts[1] & TS_TEI) >> 7,
351 (ts[3] & TS_SC) >> 6);
353 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
355 dev_kfree_skb( priv->ule_skb );
356 /* Prepare for next SNDU. */
357 dev->stats.rx_errors++;
358 dev->stats.rx_frame_errors++;
363 /* Continue with next TS cell. */
372 /* Synchronize on PUSI, if required. */
373 if (priv->need_pusi) {
374 if (ts[1] & TS_PUSI) {
375 /* Find beginning of first ULE SNDU in current TS cell. */
376 /* Synchronize continuity counter. */
377 priv->tscc = ts[3] & 0x0F;
378 /* There is a pointer field here. */
379 if (ts[4] > ts_remain) {
380 printk(KERN_ERR "%lu: Invalid ULE packet "
381 "(pointer field %d)\n", priv->ts_count, ts[4]);
386 /* Skip to destination of pointer field. */
387 from_where = &ts[5] + ts[4];
388 ts_remain -= 1 + ts[4];
399 /* Check continuity counter. */
400 if ((ts[3] & 0x0F) == priv->tscc)
401 priv->tscc = (priv->tscc + 1) & 0x0F;
403 /* TS discontinuity handling: */
404 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
405 "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
406 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
408 dev_kfree_skb( priv->ule_skb );
409 /* Prepare for next SNDU. */
410 // reset_ule(priv); moved to below.
411 dev->stats.rx_errors++;
412 dev->stats.rx_frame_errors++;
415 /* skip to next PUSI. */
419 /* If we still have an incomplete payload, but PUSI is
420 * set; some TS cells are missing.
421 * This is only possible here, if we missed exactly 16 TS
422 * cells (continuity counter wrap). */
423 if (ts[1] & TS_PUSI) {
424 if (! priv->need_pusi) {
425 if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
426 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
427 printk(KERN_WARNING "%lu: Invalid pointer "
428 "field: %u.\n", priv->ts_count, *from_where);
430 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
433 dev_kfree_skb(priv->ule_skb);
436 if (error || priv->ule_sndu_remain) {
437 dev->stats.rx_errors++;
438 dev->stats.rx_frame_errors++;
446 /* Skip pointer field (we're processing a
447 * packed payload). */
453 if (priv->ule_sndu_remain > 183) {
454 /* Current SNDU lacks more data than there could be available in the
455 * current TS cell. */
456 dev->stats.rx_errors++;
457 dev->stats.rx_length_errors++;
458 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
459 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
460 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
461 dev_kfree_skb(priv->ule_skb);
462 /* Prepare for next SNDU. */
464 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
471 /* Check if new payload needs to be started. */
472 if (priv->ule_skb == NULL) {
473 /* Start a new payload with skb.
474 * Find ULE header. It is only guaranteed that the
475 * length field (2 bytes) is contained in the current
477 * Check ts_remain has to be >= 2 here. */
479 printk(KERN_WARNING "Invalid payload packing: only %d "
480 "bytes left in TS. Resyncing.\n", ts_remain);
481 priv->ule_sndu_len = 0;
487 if (! priv->ule_sndu_len) {
488 /* Got at least two bytes, thus extrace the SNDU length. */
489 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
490 if (priv->ule_sndu_len & 0x8000) {
491 /* D-Bit is set: no dest mac present. */
492 priv->ule_sndu_len &= 0x7FFF;
497 if (priv->ule_sndu_len < 5) {
498 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
499 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
500 dev->stats.rx_errors++;
501 dev->stats.rx_length_errors++;
502 priv->ule_sndu_len = 0;
509 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
513 priv->ule_sndu_remain = priv->ule_sndu_len + 2;
515 * State of current TS:
516 * ts_remain (remaining bytes in the current TS cell)
517 * 0 ule_type is not available now, we need the next TS cell
518 * 1 the first byte of the ule_type is present
519 * >=2 full ULE header present, maybe some payload data as well.
523 priv->ule_sndu_remain--;
524 priv->ule_sndu_type = from_where[0] << 8;
525 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
526 ts_remain -= 1; from_where += 1;
527 /* Continue w/ next TS. */
534 default: /* complete ULE header is present in current TS. */
535 /* Extract ULE type field. */
536 if (priv->ule_sndu_type_1) {
537 priv->ule_sndu_type_1 = 0;
538 priv->ule_sndu_type |= from_where[0];
539 from_where += 1; /* points to payload start. */
542 /* Complete type is present in new TS. */
543 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
544 from_where += 2; /* points to payload start. */
550 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
551 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
552 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
553 if (priv->ule_skb == NULL) {
554 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
556 dev->stats.rx_dropped++;
560 /* This includes the CRC32 _and_ dest mac, if !dbit. */
561 priv->ule_sndu_remain = priv->ule_sndu_len;
562 priv->ule_skb->dev = dev;
563 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
564 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
567 /* Copy data into our current skb. */
568 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
569 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
570 priv->ule_sndu_remain -= how_much;
571 ts_remain -= how_much;
572 from_where += how_much;
574 /* Check for complete payload. */
575 if (priv->ule_sndu_remain <= 0) {
576 /* Check CRC32, we've got it in our skb already. */
577 __be16 ulen = htons(priv->ule_sndu_len);
578 __be16 utype = htons(priv->ule_sndu_type);
580 struct kvec iov[3] = {
581 { &ulen, sizeof ulen },
582 { &utype, sizeof utype },
583 { priv->ule_skb->data, priv->ule_skb->len - 4 }
585 u32 ule_crc = ~0L, expected_crc;
586 if (priv->ule_dbit) {
587 /* Set D-bit for CRC32 verification,
588 * if it was set originally. */
589 ulen |= htons(0x8000);
592 ule_crc = iov_crc32(ule_crc, iov, 3);
593 tail = skb_tail_pointer(priv->ule_skb);
594 expected_crc = *(tail - 4) << 24 |
598 if (ule_crc != expected_crc) {
599 printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
600 priv->ts_count, ule_crc, expected_crc, priv->ule_sndu_len, priv->ule_sndu_type, ts_remain, ts_remain > 2 ? *(unsigned short *)from_where : 0);
603 hexdump( iov[0].iov_base, iov[0].iov_len );
604 hexdump( iov[1].iov_base, iov[1].iov_len );
605 hexdump( iov[2].iov_base, iov[2].iov_len );
607 if (ule_where == ule_hist) {
608 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
609 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
610 } else if (ule_where == &ule_hist[TS_SZ]) {
611 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
612 hexdump( ule_hist, TS_SZ );
614 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
615 hexdump( ule_where - TS_SZ, TS_SZ );
620 dev->stats.rx_errors++;
621 dev->stats.rx_crc_errors++;
622 dev_kfree_skb(priv->ule_skb);
624 /* CRC32 verified OK. */
625 u8 dest_addr[ETH_ALEN];
626 static const u8 bc_addr[ETH_ALEN] =
627 { [ 0 ... ETH_ALEN-1] = 0xff };
629 /* CRC32 was OK. Remove it from skb. */
630 priv->ule_skb->tail -= 4;
631 priv->ule_skb->len -= 4;
633 if (!priv->ule_dbit) {
635 * The destination MAC address is the
636 * next data in the skb. It comes
637 * before any extension headers.
639 * Check if the payload of this SNDU
640 * should be passed up the stack.
642 register int drop = 0;
643 if (priv->rx_mode != RX_MODE_PROMISC) {
644 if (priv->ule_skb->data[0] & 0x01) {
645 /* multicast or broadcast */
646 if (!ether_addr_equal(priv->ule_skb->data, bc_addr)) {
648 if (priv->rx_mode == RX_MODE_MULTI) {
650 for(i = 0; i < priv->multi_num &&
651 !ether_addr_equal(priv->ule_skb->data,
652 priv->multi_macs[i]); i++)
654 if (i == priv->multi_num)
656 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
657 drop = 1; /* no broadcast; */
658 /* else: all multicast mode: accept all multicast packets */
660 /* else: broadcast */
662 else if (!ether_addr_equal(priv->ule_skb->data, dev->dev_addr))
664 /* else: destination address matches the MAC address of our receiver device */
666 /* else: promiscuous mode; pass everything up the stack */
670 netdev_dbg(dev, "Dropping SNDU: MAC destination address does not match: dest addr: %pM, dev addr: %pM\n",
671 priv->ule_skb->data, dev->dev_addr);
673 dev_kfree_skb(priv->ule_skb);
678 skb_copy_from_linear_data(priv->ule_skb,
681 skb_pull(priv->ule_skb, ETH_ALEN);
685 /* Handle ULE Extension Headers. */
686 if (priv->ule_sndu_type < ETH_P_802_3_MIN) {
687 /* There is an extension header. Handle it accordingly. */
688 int l = handle_ule_extensions(priv);
690 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
691 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
692 dev_kfree_skb(priv->ule_skb);
695 skb_pull(priv->ule_skb, l);
699 * Construct/assure correct ethernet header.
700 * Note: in bridged mode (priv->ule_bridged !=
701 * 0) we already have the (original) ethernet
702 * header at the start of the payload (after
703 * optional dest. address and any extension
707 if (!priv->ule_bridged) {
708 skb_push(priv->ule_skb, ETH_HLEN);
709 ethh = (struct ethhdr *)priv->ule_skb->data;
710 if (!priv->ule_dbit) {
711 /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
712 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
713 eth_zero_addr(ethh->h_source);
715 else /* zeroize source and dest */
716 memset( ethh, 0, ETH_ALEN*2 );
718 ethh->h_proto = htons(priv->ule_sndu_type);
720 /* else: skb is in correct state; nothing to do. */
721 priv->ule_bridged = 0;
723 /* Stuff into kernel's protocol stack. */
724 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
725 /* If D-bit is set (i.e. destination MAC address not present),
726 * receive the packet anyhow. */
727 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
728 priv->ule_skb->pkt_type = PACKET_HOST; */
729 dev->stats.rx_packets++;
730 dev->stats.rx_bytes += priv->ule_skb->len;
731 netif_rx(priv->ule_skb);
734 /* Prepare for next SNDU. */
738 /* More data in current TS (look at the bytes following the CRC32)? */
739 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
740 /* Next ULE SNDU starts right there. */
742 priv->ule_skb = NULL;
743 priv->ule_sndu_type_1 = 0;
744 priv->ule_sndu_len = 0;
745 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
746 // *(from_where + 0), *(from_where + 1),
747 // *(from_where + 2), *(from_where + 3));
748 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
754 if (priv->ule_skb == NULL) {
756 priv->ule_sndu_type_1 = 0;
757 priv->ule_sndu_len = 0;
760 } /* for all available TS cells */
763 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
764 const u8 *buffer2, size_t buffer2_len,
765 struct dmx_ts_feed *feed)
767 struct net_device *dev = feed->priv;
770 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
771 if (buffer1_len > 32768)
772 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
773 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
774 buffer1_len, buffer1_len / TS_SZ, buffer1); */
775 dvb_net_ule(dev, buffer1, buffer1_len);
780 static void dvb_net_sec(struct net_device *dev,
781 const u8 *pkt, int pkt_len)
785 struct net_device_stats *stats = &dev->stats;
788 /* note: pkt_len includes a 32bit checksum */
790 printk("%s: IP/MPE packet length = %d too small.\n",
793 stats->rx_length_errors++;
796 /* it seems some ISPs manage to screw up here, so we have to
797 * relax the error checks... */
799 if ((pkt[5] & 0xfd) != 0xc1) {
800 /* drop scrambled or broken packets */
802 if ((pkt[5] & 0x3c) != 0x00) {
806 stats->rx_crc_errors++;
810 /* handle LLC/SNAP, see rfc-1042 */
811 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
818 /* FIXME: assemble datagram from multiple sections */
820 stats->rx_frame_errors++;
824 /* we have 14 byte ethernet header (ip header follows);
825 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
827 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
828 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
832 skb_reserve(skb, 2); /* longword align L3 header */
835 /* copy L3 payload */
836 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
837 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
839 /* create ethernet header: */
847 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
853 /* protocol numbers are from rfc-1700 or
854 * http://www.iana.org/assignments/ethernet-numbers
856 if (pkt[12] >> 4 == 6) { /* version field from IP header */
857 eth[12] = 0x86; /* IPv6 */
860 eth[12] = 0x08; /* IPv4 */
865 skb->protocol = dvb_net_eth_type_trans(skb, dev);
868 stats->rx_bytes+=skb->len;
872 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
873 const u8 *buffer2, size_t buffer2_len,
874 struct dmx_section_filter *filter)
876 struct net_device *dev = filter->priv;
879 * we rely on the DVB API definition where exactly one complete
880 * section is delivered in buffer1
882 dvb_net_sec (dev, buffer1, buffer1_len);
886 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
892 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
893 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
894 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
895 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
897 static int dvb_net_filter_sec_set(struct net_device *dev,
898 struct dmx_section_filter **secfilter,
899 u8 *mac, u8 *mac_mask)
901 struct dvb_net_priv *priv = netdev_priv(dev);
905 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
907 printk("%s: could not get filter\n", dev->name);
911 (*secfilter)->priv=(void *) dev;
913 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
914 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
915 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
917 (*secfilter)->filter_value[0]=0x3e;
918 (*secfilter)->filter_value[3]=mac[5];
919 (*secfilter)->filter_value[4]=mac[4];
920 (*secfilter)->filter_value[8]=mac[3];
921 (*secfilter)->filter_value[9]=mac[2];
922 (*secfilter)->filter_value[10]=mac[1];
923 (*secfilter)->filter_value[11]=mac[0];
925 (*secfilter)->filter_mask[0] = 0xff;
926 (*secfilter)->filter_mask[3] = mac_mask[5];
927 (*secfilter)->filter_mask[4] = mac_mask[4];
928 (*secfilter)->filter_mask[8] = mac_mask[3];
929 (*secfilter)->filter_mask[9] = mac_mask[2];
930 (*secfilter)->filter_mask[10] = mac_mask[1];
931 (*secfilter)->filter_mask[11]=mac_mask[0];
933 netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask);
938 static int dvb_net_feed_start(struct net_device *dev)
941 struct dvb_net_priv *priv = netdev_priv(dev);
942 struct dmx_demux *demux = priv->demux;
943 unsigned char *mac = (unsigned char *) dev->dev_addr;
945 netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode);
946 mutex_lock(&priv->mutex);
947 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
948 printk("%s: BUG %d\n", __func__, __LINE__);
951 priv->secfilter=NULL;
954 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
955 netdev_dbg(dev, "alloc secfeed\n");
956 ret=demux->allocate_section_feed(demux, &priv->secfeed,
957 dvb_net_sec_callback);
959 printk("%s: could not allocate section feed\n", dev->name);
963 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
966 printk("%s: could not set section feed\n", dev->name);
967 priv->demux->release_section_feed(priv->demux, priv->secfeed);
972 if (priv->rx_mode != RX_MODE_PROMISC) {
973 netdev_dbg(dev, "set secfilter\n");
974 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
977 switch (priv->rx_mode) {
979 for (i = 0; i < priv->multi_num; i++) {
980 netdev_dbg(dev, "set multi_secfilter[%d]\n", i);
981 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
982 priv->multi_macs[i], mask_normal);
985 case RX_MODE_ALL_MULTI:
987 netdev_dbg(dev, "set multi_secfilter[0]\n");
988 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
989 mac_allmulti, mask_allmulti);
991 case RX_MODE_PROMISC:
993 netdev_dbg(dev, "set secfilter\n");
994 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
998 netdev_dbg(dev, "start filtering\n");
999 priv->secfeed->start_filtering(priv->secfeed);
1000 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1001 ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC);
1003 /* we have payloads encapsulated in TS */
1004 netdev_dbg(dev, "alloc tsfeed\n");
1005 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1007 printk("%s: could not allocate ts feed\n", dev->name);
1011 /* Set netdevice pointer for ts decaps callback. */
1012 priv->tsfeed->priv = (void *)dev;
1013 ret = priv->tsfeed->set(priv->tsfeed,
1014 priv->pid, /* pid */
1015 TS_PACKET, /* type */
1016 DMX_PES_OTHER, /* pes type */
1017 32768, /* circular buffer size */
1018 timeout /* timeout */
1022 printk("%s: could not set ts feed\n", dev->name);
1023 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1024 priv->tsfeed = NULL;
1028 netdev_dbg(dev, "start filtering\n");
1029 priv->tsfeed->start_filtering(priv->tsfeed);
1034 mutex_unlock(&priv->mutex);
1038 static int dvb_net_feed_stop(struct net_device *dev)
1040 struct dvb_net_priv *priv = netdev_priv(dev);
1043 mutex_lock(&priv->mutex);
1044 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1045 if (priv->secfeed) {
1046 if (priv->secfeed->is_filtering) {
1047 netdev_dbg(dev, "stop secfeed\n");
1048 priv->secfeed->stop_filtering(priv->secfeed);
1051 if (priv->secfilter) {
1052 netdev_dbg(dev, "release secfilter\n");
1053 priv->secfeed->release_filter(priv->secfeed,
1055 priv->secfilter=NULL;
1058 for (i=0; i<priv->multi_num; i++) {
1059 if (priv->multi_secfilter[i]) {
1060 netdev_dbg(dev, "release multi_filter[%d]\n",
1062 priv->secfeed->release_filter(priv->secfeed,
1063 priv->multi_secfilter[i]);
1064 priv->multi_secfilter[i] = NULL;
1068 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1069 priv->secfeed = NULL;
1071 printk("%s: no feed to stop\n", dev->name);
1072 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1074 if (priv->tsfeed->is_filtering) {
1075 netdev_dbg(dev, "stop tsfeed\n");
1076 priv->tsfeed->stop_filtering(priv->tsfeed);
1078 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1079 priv->tsfeed = NULL;
1082 printk("%s: no ts feed to stop\n", dev->name);
1085 mutex_unlock(&priv->mutex);
1090 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr)
1092 struct dvb_net_priv *priv = netdev_priv(dev);
1094 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1097 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN);
1104 static void wq_set_multicast_list (struct work_struct *work)
1106 struct dvb_net_priv *priv =
1107 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1108 struct net_device *dev = priv->net;
1110 dvb_net_feed_stop(dev);
1111 priv->rx_mode = RX_MODE_UNI;
1112 netif_addr_lock_bh(dev);
1114 if (dev->flags & IFF_PROMISC) {
1115 netdev_dbg(dev, "promiscuous mode\n");
1116 priv->rx_mode = RX_MODE_PROMISC;
1117 } else if ((dev->flags & IFF_ALLMULTI)) {
1118 netdev_dbg(dev, "allmulti mode\n");
1119 priv->rx_mode = RX_MODE_ALL_MULTI;
1120 } else if (!netdev_mc_empty(dev)) {
1121 struct netdev_hw_addr *ha;
1123 netdev_dbg(dev, "set_mc_list, %d entries\n",
1124 netdev_mc_count(dev));
1126 priv->rx_mode = RX_MODE_MULTI;
1127 priv->multi_num = 0;
1129 netdev_for_each_mc_addr(ha, dev)
1130 dvb_set_mc_filter(dev, ha->addr);
1133 netif_addr_unlock_bh(dev);
1134 dvb_net_feed_start(dev);
1138 static void dvb_net_set_multicast_list (struct net_device *dev)
1140 struct dvb_net_priv *priv = netdev_priv(dev);
1141 schedule_work(&priv->set_multicast_list_wq);
1145 static void wq_restart_net_feed (struct work_struct *work)
1147 struct dvb_net_priv *priv =
1148 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1149 struct net_device *dev = priv->net;
1151 if (netif_running(dev)) {
1152 dvb_net_feed_stop(dev);
1153 dvb_net_feed_start(dev);
1158 static int dvb_net_set_mac (struct net_device *dev, void *p)
1160 struct dvb_net_priv *priv = netdev_priv(dev);
1161 struct sockaddr *addr=p;
1163 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1165 if (netif_running(dev))
1166 schedule_work(&priv->restart_net_feed_wq);
1172 static int dvb_net_open(struct net_device *dev)
1174 struct dvb_net_priv *priv = netdev_priv(dev);
1177 dvb_net_feed_start(dev);
1182 static int dvb_net_stop(struct net_device *dev)
1184 struct dvb_net_priv *priv = netdev_priv(dev);
1187 return dvb_net_feed_stop(dev);
1190 static const struct header_ops dvb_header_ops = {
1191 .create = eth_header,
1192 .parse = eth_header_parse,
1196 static const struct net_device_ops dvb_netdev_ops = {
1197 .ndo_open = dvb_net_open,
1198 .ndo_stop = dvb_net_stop,
1199 .ndo_start_xmit = dvb_net_tx,
1200 .ndo_set_rx_mode = dvb_net_set_multicast_list,
1201 .ndo_set_mac_address = dvb_net_set_mac,
1202 .ndo_change_mtu = eth_change_mtu,
1203 .ndo_validate_addr = eth_validate_addr,
1206 static void dvb_net_setup(struct net_device *dev)
1210 dev->header_ops = &dvb_header_ops;
1211 dev->netdev_ops = &dvb_netdev_ops;
1214 dev->flags |= IFF_NOARP;
1217 static int get_if(struct dvb_net *dvbnet)
1221 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1222 if (!dvbnet->state[i])
1225 if (i == DVB_NET_DEVICES_MAX)
1232 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1234 struct net_device *net;
1235 struct dvb_net_priv *priv;
1239 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1241 if ((if_num = get_if(dvbnet)) < 0)
1244 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1245 NET_NAME_UNKNOWN, dvb_net_setup);
1249 if (dvbnet->dvbdev->id)
1250 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1251 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1253 /* compatibility fix to keep dvb0_0 format */
1254 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1255 dvbnet->dvbdev->adapter->num, if_num);
1258 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1260 dvbnet->device[if_num] = net;
1262 priv = netdev_priv(net);
1264 priv->demux = dvbnet->demux;
1266 priv->rx_mode = RX_MODE_UNI;
1267 priv->need_pusi = 1;
1269 priv->feedtype = feedtype;
1272 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1273 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1274 mutex_init(&priv->mutex);
1276 net->base_addr = pid;
1278 if ((result = register_netdev(net)) < 0) {
1279 dvbnet->device[if_num] = NULL;
1283 printk("dvb_net: created network interface %s\n", net->name);
1288 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1290 struct net_device *net = dvbnet->device[num];
1291 struct dvb_net_priv *priv;
1293 if (!dvbnet->state[num])
1295 priv = netdev_priv(net);
1300 flush_work(&priv->set_multicast_list_wq);
1301 flush_work(&priv->restart_net_feed_wq);
1302 printk("dvb_net: removed network interface %s\n", net->name);
1303 unregister_netdev(net);
1304 dvbnet->state[num]=0;
1305 dvbnet->device[num] = NULL;
1311 static int dvb_net_do_ioctl(struct file *file,
1312 unsigned int cmd, void *parg)
1314 struct dvb_device *dvbdev = file->private_data;
1315 struct dvb_net *dvbnet = dvbdev->priv;
1318 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1321 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex))
1322 return -ERESTARTSYS;
1327 struct dvb_net_if *dvbnetif = parg;
1330 if (!capable(CAP_SYS_ADMIN)) {
1335 if (!try_module_get(dvbdev->adapter->module)) {
1340 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1342 module_put(dvbdev->adapter->module);
1346 dvbnetif->if_num=result;
1351 struct net_device *netdev;
1352 struct dvb_net_priv *priv_data;
1353 struct dvb_net_if *dvbnetif = parg;
1354 int if_num = dvbnetif->if_num;
1356 if (if_num >= DVB_NET_DEVICES_MAX) {
1360 if_num = array_index_nospec(if_num, DVB_NET_DEVICES_MAX);
1362 if (!dvbnet->state[if_num]) {
1367 netdev = dvbnet->device[if_num];
1369 priv_data = netdev_priv(netdev);
1370 dvbnetif->pid=priv_data->pid;
1371 dvbnetif->feedtype=priv_data->feedtype;
1376 if (!capable(CAP_SYS_ADMIN)) {
1380 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) {
1384 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1386 module_put(dvbdev->adapter->module);
1390 /* binary compatibility cruft */
1391 case __NET_ADD_IF_OLD:
1393 struct __dvb_net_if_old *dvbnetif = parg;
1396 if (!capable(CAP_SYS_ADMIN)) {
1401 if (!try_module_get(dvbdev->adapter->module)) {
1406 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1408 module_put(dvbdev->adapter->module);
1412 dvbnetif->if_num=result;
1415 case __NET_GET_IF_OLD:
1417 struct net_device *netdev;
1418 struct dvb_net_priv *priv_data;
1419 struct __dvb_net_if_old *dvbnetif = parg;
1420 int if_num = dvbnetif->if_num;
1422 if (if_num >= DVB_NET_DEVICES_MAX) {
1426 if_num = array_index_nospec(if_num, DVB_NET_DEVICES_MAX);
1428 if (!dvbnet->state[if_num]) {
1433 netdev = dvbnet->device[if_num];
1435 priv_data = netdev_priv(netdev);
1436 dvbnetif->pid=priv_data->pid;
1445 mutex_unlock(&dvbnet->ioctl_mutex);
1449 static long dvb_net_ioctl(struct file *file,
1450 unsigned int cmd, unsigned long arg)
1452 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl);
1455 static int dvb_net_close(struct inode *inode, struct file *file)
1457 struct dvb_device *dvbdev = file->private_data;
1458 struct dvb_net *dvbnet = dvbdev->priv;
1460 dvb_generic_release(inode, file);
1462 if(dvbdev->users == 1 && dvbnet->exit == 1)
1463 wake_up(&dvbdev->wait_queue);
1468 static const struct file_operations dvb_net_fops = {
1469 .owner = THIS_MODULE,
1470 .unlocked_ioctl = dvb_net_ioctl,
1471 .open = dvb_generic_open,
1472 .release = dvb_net_close,
1473 .llseek = noop_llseek,
1476 static const struct dvb_device dvbdev_net = {
1480 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1483 .fops = &dvb_net_fops,
1486 void dvb_net_release (struct dvb_net *dvbnet)
1491 if (dvbnet->dvbdev->users < 1)
1492 wait_event(dvbnet->dvbdev->wait_queue,
1493 dvbnet->dvbdev->users==1);
1495 dvb_unregister_device(dvbnet->dvbdev);
1497 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1498 if (!dvbnet->state[i])
1500 dvb_net_remove_if(dvbnet, i);
1503 EXPORT_SYMBOL(dvb_net_release);
1506 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1507 struct dmx_demux *dmx)
1511 mutex_init(&dvbnet->ioctl_mutex);
1512 dvbnet->demux = dmx;
1514 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1515 dvbnet->state[i] = 0;
1517 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net,
1518 dvbnet, DVB_DEVICE_NET, 0);
1520 EXPORT_SYMBOL(dvb_net_init);