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 draft-ietf-ipdvb-ule-03.txt.
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.
48 * FIXME / TODO (dvb_net.c):
50 * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero.
52 * TS_FEED callback is called once for every single TS cell although it is
53 * registered (in dvb_net_feed_start()) for 100 TS cells (used for dvb_net_ule()).
57 #include <linux/module.h>
58 #include <linux/kernel.h>
59 #include <linux/netdevice.h>
60 #include <linux/etherdevice.h>
61 #include <linux/dvb/net.h>
62 #include <linux/uio.h>
63 #include <asm/uaccess.h>
64 #include <linux/crc32.h>
65 #include <linux/version.h>
67 #include "dvb_demux.h"
70 static int dvb_net_debug;
71 module_param(dvb_net_debug, int, 0444);
72 MODULE_PARM_DESC(dvb_net_debug, "enable debug messages");
74 #define dprintk(x...) do { if (dvb_net_debug) printk(x); } while (0)
77 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
80 for (j = 0; j < cnt; j++)
81 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
86 #define DVB_NET_MULTICAST_MAX 10
92 #define isprint(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
94 static void hexdump( const unsigned char *buf, unsigned short len )
96 char str[80], octet[10];
99 for (ofs = 0; ofs < len; ofs += 16) {
100 sprintf( str, "%03d: ", ofs );
102 for (i = 0; i < 16; i++) {
104 sprintf( octet, "%02x ", buf[ofs + i] );
106 strcpy( octet, " " );
108 strcat( str, octet );
113 for (i = 0; (i < 16) && ((i + ofs) < len); i++)
114 str[l++] = isprint( buf[ofs + i] ) ? buf[ofs + i] : '.';
117 printk( KERN_WARNING "%s\n", str );
123 struct dvb_net_priv {
125 struct net_device_stats stats;
128 struct dvb_net *host;
129 struct dmx_demux *demux;
130 struct dmx_section_feed *secfeed;
131 struct dmx_section_filter *secfilter;
132 struct dmx_ts_feed *tsfeed;
134 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
135 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
137 #define RX_MODE_UNI 0
138 #define RX_MODE_MULTI 1
139 #define RX_MODE_ALL_MULTI 2
140 #define RX_MODE_PROMISC 3
141 struct work_struct set_multicast_list_wq;
142 struct work_struct restart_net_feed_wq;
143 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
144 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
145 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
146 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
147 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
148 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
149 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
150 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
151 unsigned char ule_dbit; /* Whether the DestMAC address present
152 * or not (bit is set). */
153 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
154 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
155 unsigned long ts_count; /* Current ts cell counter. */
160 * Determine the packet's protocol ID. The rule here is that we
161 * assume 802.3 if the type field is short enough to be a length.
162 * This is normal practice and works for any 'now in use' protocol.
164 * stolen from eth.c out of the linux kernel, hacked for dvb-device
165 * by Michael Holzt <kju@debian.org>
167 static unsigned short dvb_net_eth_type_trans(struct sk_buff *skb,
168 struct net_device *dev)
173 skb->mac.raw=skb->data;
174 skb_pull(skb,dev->hard_header_len);
175 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,8)
176 eth = skb->mac.ethernet;
181 if (*eth->h_dest & 1) {
182 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
183 skb->pkt_type=PACKET_BROADCAST;
185 skb->pkt_type=PACKET_MULTICAST;
188 if (ntohs(eth->h_proto) >= 1536)
194 * This is a magic hack to spot IPX packets. Older Novell breaks
195 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
196 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
197 * won't work for fault tolerant netware but does for the rest.
199 if (*(unsigned short *)rawp == 0xFFFF)
200 return htons(ETH_P_802_3);
205 return htons(ETH_P_802_2);
216 /* ULE Extension Header handlers. */
219 #define ULE_BRIDGED 1
221 int ule_test_sndu( struct dvb_net_priv *p )
226 int ule_bridged_sndu( struct dvb_net_priv *p )
228 /* BRIDGE SNDU handling sucks in draft-ietf-ipdvb-ule-03.txt.
229 * This has to be the last extension header, otherwise it won't work.
237 /** Handle ULE extension headers.
238 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
239 * Returns: >= 0: nr. of bytes consumed by next extension header
240 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
242 static int handle_one_ule_extension( struct dvb_net_priv *p )
244 /* Table of mandatory extension header handlers. The header type is the index. */
245 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
246 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
248 /* Table of optional extension header handlers. The header type is the index. */
249 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) = { NULL, };
252 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
253 unsigned char htype = p->ule_sndu_type & 0x00FF;
255 /* Discriminate mandatory and optional extension headers. */
257 /* Mandatory extension header */
258 if (ule_mandatory_ext_handlers[htype]) {
259 ext_len = ule_mandatory_ext_handlers[htype]( p );
260 p->ule_next_hdr += ext_len;
261 if (! p->ule_bridged) {
262 p->ule_sndu_type = ntohs( *(unsigned short *)p->ule_next_hdr );
263 p->ule_next_hdr += 2;
265 p->ule_sndu_type = ntohs( *(unsigned short *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)) );
266 /* This assures the extension handling loop will terminate. */
269 ext_len = -1; /* SNDU has to be discarded. */
271 /* Optional extension header. Calculate the length. */
273 /* Process the optional extension header according to its type. */
274 if (ule_optional_ext_handlers[htype])
275 (void)ule_optional_ext_handlers[htype]( p );
276 p->ule_next_hdr += ext_len;
277 p->ule_sndu_type = ntohs( *(unsigned short *)p->ule_next_hdr );
278 p->ule_next_hdr += 2;
284 static int handle_ule_extensions( struct dvb_net_priv *p )
286 int total_ext_len = 0, l;
288 p->ule_next_hdr = p->ule_skb->data;
290 l = handle_one_ule_extension( p );
291 if (l == -1) return -1; /* Stop extension header processing and discard SNDU. */
294 } while (p->ule_sndu_type < 1536);
296 return total_ext_len;
300 /** Prepare for a new ULE SNDU: reset the decoder state. */
301 static inline void reset_ule( struct dvb_net_priv *p )
304 p->ule_next_hdr = NULL;
306 p->ule_sndu_type = 0;
307 p->ule_sndu_type_1 = 0;
308 p->ule_sndu_remain = 0;
314 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
315 * TS cells of a single PID.
317 static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
319 struct dvb_net_priv *priv = (struct dvb_net_priv *)dev->priv;
320 unsigned long skipped = 0L;
321 u8 *ts, *ts_end, *from_where = NULL, ts_remain = 0, how_much = 0, new_ts = 1;
322 struct ethhdr *ethh = NULL;
325 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
326 static unsigned char ule_hist[100*TS_SZ];
327 static unsigned char *ule_where = ule_hist, ule_dump = 0;
331 printk( KERN_ERR "NO netdev struct!\n" );
335 /* For all TS cells in current buffer.
336 * Appearently, we are called for every single TS cell.
338 for (ts = (char *)buf, ts_end = (char *)buf + buf_len; ts < ts_end; /* no default incr. */ ) {
341 /* We are about to process a new TS cell. */
344 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
345 memcpy( ule_where, ts, TS_SZ );
347 hexdump( ule_where, TS_SZ );
353 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
354 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
355 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
356 priv->ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
358 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
360 dev_kfree_skb( priv->ule_skb );
361 /* Prepare for next SNDU. */
362 ((struct dvb_net_priv *) dev->priv)->stats.rx_errors++;
363 ((struct dvb_net_priv *) dev->priv)->stats.rx_frame_errors++;
368 /* Continue with next TS cell. */
377 /* Synchronize on PUSI, if required. */
378 if (priv->need_pusi) {
379 if (ts[1] & TS_PUSI) {
380 /* Find beginning of first ULE SNDU in current TS cell. */
381 /* Synchronize continuity counter. */
382 priv->tscc = ts[3] & 0x0F;
383 /* There is a pointer field here. */
384 if (ts[4] > ts_remain) {
385 printk(KERN_ERR "%lu: Invalid ULE packet "
386 "(pointer field %d)\n", priv->ts_count, ts[4]);
391 /* Skip to destination of pointer field. */
392 from_where = &ts[5] + ts[4];
393 ts_remain -= 1 + ts[4];
403 /* Check continuity counter. */
405 if ((ts[3] & 0x0F) == priv->tscc)
406 priv->tscc = (priv->tscc + 1) & 0x0F;
408 /* TS discontinuity handling: */
409 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
410 "exptected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
411 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
413 dev_kfree_skb( priv->ule_skb );
414 /* Prepare for next SNDU. */
415 // reset_ule(priv); moved to below.
416 ((struct dvb_net_priv *) dev->priv)->stats.rx_errors++;
417 ((struct dvb_net_priv *) dev->priv)->stats.rx_frame_errors++;
420 /* skip to next PUSI. */
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). */
430 if (ts[1] & TS_PUSI) {
431 if (! priv->need_pusi) {
432 if (*from_where > 181) {
433 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
434 printk(KERN_WARNING "%lu: Invalid pointer "
435 "field: %u.\n", priv->ts_count, *from_where);
437 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
439 dev_kfree_skb( priv->ule_skb );
440 ((struct dvb_net_priv *) dev->priv)->stats.rx_errors++;
441 ((struct dvb_net_priv *) dev->priv)->stats.rx_frame_errors++;
449 /* Skip pointer field (we're processing a
450 * packed payload). */
456 if (priv->ule_sndu_remain > 183) {
457 /* Current SNDU lacks more data than there could be available in the
458 * current TS cell. */
459 ((struct dvb_net_priv *) dev->priv)->stats.rx_errors++;
460 ((struct dvb_net_priv *) dev->priv)->stats.rx_length_errors++;
461 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
462 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
463 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
464 dev_kfree_skb(priv->ule_skb);
465 /* Prepare for next SNDU. */
467 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
474 /* Check if new payload needs to be started. */
475 if (priv->ule_skb == NULL) {
476 /* Start a new payload with skb.
477 * Find ULE header. It is only guaranteed that the
478 * length field (2 bytes) is contained in the current
480 * Check ts_remain has to be >= 2 here. */
482 printk(KERN_WARNING "Invalid payload packing: only %d "
483 "bytes left in TS. Resyncing.\n", ts_remain);
484 priv->ule_sndu_len = 0;
489 if (! priv->ule_sndu_len) {
490 /* Got at least two bytes, thus extrace the SNDU length. */
491 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
492 if (priv->ule_sndu_len & 0x8000) {
493 /* D-Bit is set: no dest mac present. */
494 priv->ule_sndu_len &= 0x7FFF;
499 if (priv->ule_sndu_len > 32763) {
500 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
501 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
502 priv->ule_sndu_len = 0;
509 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
514 * State of current TS:
515 * ts_remain (remaining bytes in the current TS cell)
516 * 0 ule_type is not available now, we need the next TS cell
517 * 1 the first byte of the ule_type is present
518 * >=2 full ULE header present, maybe some payload data as well.
522 priv->ule_sndu_type = from_where[0] << 8;
523 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
524 ts_remain -= 1; from_where += 1;
525 /* Continue w/ next TS. */
532 default: /* complete ULE header is present in current TS. */
533 /* Extract ULE type field. */
534 if (priv->ule_sndu_type_1) {
535 priv->ule_sndu_type |= from_where[0];
536 from_where += 1; /* points to payload start. */
539 /* Complete type is present in new TS. */
540 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
541 from_where += 2; /* points to payload start. */
547 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
548 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
549 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
550 if (priv->ule_skb == NULL) {
551 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
553 ((struct dvb_net_priv *)dev->priv)->stats.rx_dropped++;
557 /* This includes the CRC32 _and_ dest mac, if !dbit. */
558 priv->ule_sndu_remain = priv->ule_sndu_len;
559 priv->ule_skb->dev = dev;
560 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
561 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
564 /* Copy data into our current skb. */
565 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
566 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
567 priv->ule_sndu_remain -= how_much;
568 ts_remain -= how_much;
569 from_where += how_much;
571 /* Check for complete payload. */
572 if (priv->ule_sndu_remain <= 0) {
573 /* Check CRC32, we've got it in our skb already. */
574 unsigned short ulen = htons(priv->ule_sndu_len);
575 unsigned short utype = htons(priv->ule_sndu_type);
576 struct kvec iov[3] = {
577 { &ulen, sizeof ulen },
578 { &utype, sizeof utype },
579 { priv->ule_skb->data, priv->ule_skb->len - 4 }
581 unsigned long ule_crc = ~0L, expected_crc;
582 if (priv->ule_dbit) {
583 /* Set D-bit for CRC32 verification,
584 * if it was set originally. */
588 ule_crc = iov_crc32(ule_crc, iov, 3);
589 expected_crc = *((u8 *)priv->ule_skb->tail - 4) << 24 |
590 *((u8 *)priv->ule_skb->tail - 3) << 16 |
591 *((u8 *)priv->ule_skb->tail - 2) << 8 |
592 *((u8 *)priv->ule_skb->tail - 1);
593 if (ule_crc != expected_crc) {
594 printk(KERN_WARNING "%lu: CRC32 check FAILED: %#lx / %#lx, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
595 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);
598 hexdump( iov[0].iov_base, iov[0].iov_len );
599 hexdump( iov[1].iov_base, iov[1].iov_len );
600 hexdump( iov[2].iov_base, iov[2].iov_len );
602 if (ule_where == ule_hist) {
603 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
604 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
605 } else if (ule_where == &ule_hist[TS_SZ]) {
606 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
607 hexdump( ule_hist, TS_SZ );
609 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
610 hexdump( ule_where - TS_SZ, TS_SZ );
615 ((struct dvb_net_priv *) dev->priv)->stats.rx_errors++;
616 ((struct dvb_net_priv *) dev->priv)->stats.rx_crc_errors++;
617 dev_kfree_skb(priv->ule_skb);
619 /* CRC32 verified OK. */
620 /* Handle ULE Extension Headers. */
621 if (priv->ule_sndu_type < 1536) {
622 /* There is an extension header. Handle it accordingly. */
623 int l = handle_ule_extensions( priv );
625 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
626 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
627 dev_kfree_skb( priv->ule_skb );
630 skb_pull( priv->ule_skb, l );
633 /* CRC32 was OK. Remove it from skb. */
634 priv->ule_skb->tail -= 4;
635 priv->ule_skb->len -= 4;
637 /* Filter on receiver's destination MAC address, if present. */
638 if (!priv->ule_dbit) {
639 /* The destination MAC address is the next data in the skb. */
640 if (memcmp( priv->ule_skb->data, dev->dev_addr, ETH_ALEN )) {
641 /* MAC addresses don't match. Drop SNDU. */
642 // printk( KERN_WARNING "Dropping SNDU, MAC address.\n" );
643 dev_kfree_skb( priv->ule_skb );
646 if (! priv->ule_bridged) {
647 skb_push( priv->ule_skb, ETH_ALEN + 2 );
648 ethh = (struct ethhdr *)priv->ule_skb->data;
649 memcpy( ethh->h_dest, ethh->h_source, ETH_ALEN );
650 memset( ethh->h_source, 0, ETH_ALEN );
651 ethh->h_proto = htons( priv->ule_sndu_type );
653 /* Skip the Receiver destination MAC address. */
654 skb_pull( priv->ule_skb, ETH_ALEN );
657 if (! priv->ule_bridged) {
658 skb_push( priv->ule_skb, ETH_HLEN );
659 ethh = (struct ethhdr *)priv->ule_skb->data;
660 memcpy( ethh->h_dest, dev->dev_addr, ETH_ALEN );
661 memset( ethh->h_source, 0, ETH_ALEN );
662 ethh->h_proto = htons( priv->ule_sndu_type );
664 /* skb is in correct state; nothing to do. */
667 priv->ule_bridged = 0;
669 /* Stuff into kernel's protocol stack. */
670 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
671 /* If D-bit is set (i.e. destination MAC address not present),
672 * receive the packet anyhow. */
673 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
674 priv->ule_skb->pkt_type = PACKET_HOST; */
675 ((struct dvb_net_priv *) dev->priv)->stats.rx_packets++;
676 ((struct dvb_net_priv *) dev->priv)->stats.rx_bytes += priv->ule_skb->len;
677 netif_rx(priv->ule_skb);
680 /* Prepare for next SNDU. */
684 /* More data in current TS (look at the bytes following the CRC32)? */
685 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
686 /* Next ULE SNDU starts right there. */
688 priv->ule_skb = NULL;
689 priv->ule_sndu_type_1 = 0;
690 priv->ule_sndu_len = 0;
691 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
692 // *(from_where + 0), *(from_where + 1),
693 // *(from_where + 2), *(from_where + 3));
694 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
700 if (priv->ule_skb == NULL) {
702 priv->ule_sndu_type_1 = 0;
703 priv->ule_sndu_len = 0;
706 } /* for all available TS cells */
709 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
710 const u8 *buffer2, size_t buffer2_len,
711 struct dmx_ts_feed *feed, enum dmx_success success)
713 struct net_device *dev = (struct net_device *)feed->priv;
716 printk(KERN_WARNING "buffer2 not 0: %p.\n", buffer2);
717 if (buffer1_len > 32768)
718 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
719 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
720 buffer1_len, buffer1_len / TS_SZ, buffer1); */
721 dvb_net_ule(dev, buffer1, buffer1_len);
726 static void dvb_net_sec(struct net_device *dev, u8 *pkt, int pkt_len)
730 struct net_device_stats *stats = &(((struct dvb_net_priv *) dev->priv)->stats);
732 /* note: pkt_len includes a 32bit checksum */
734 printk("%s: IP/MPE packet length = %d too small.\n",
737 stats->rx_length_errors++;
740 /* it seems some ISPs manage to screw up here, so we have to
741 * relax the error checks... */
743 if ((pkt[5] & 0xfd) != 0xc1) {
744 /* drop scrambled or broken packets */
746 if ((pkt[5] & 0x3c) != 0x00) {
750 stats->rx_crc_errors++;
754 //FIXME: handle LLC/SNAP
759 /* FIXME: assemble datagram from multiple sections */
761 stats->rx_frame_errors++;
765 /* we have 14 byte ethernet header (ip header follows);
766 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment
768 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2))) {
769 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
773 skb_reserve(skb, 2); /* longword align L3 header */
776 /* copy L3 payload */
777 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14);
778 memcpy(eth + 14, pkt + 12, pkt_len - 12 - 4);
780 /* create ethernet header: */
788 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
790 eth[12] = 0x08; /* ETH_P_IP */
793 skb->protocol = dvb_net_eth_type_trans(skb, dev);
796 stats->rx_bytes+=skb->len;
800 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
801 const u8 *buffer2, size_t buffer2_len,
802 struct dmx_section_filter *filter,
803 enum dmx_success success)
805 struct net_device *dev=(struct net_device *) filter->priv;
808 * we rely on the DVB API definition where exactly one complete
809 * section is delivered in buffer1
811 dvb_net_sec (dev, (u8*) buffer1, buffer1_len);
815 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
821 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
822 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
823 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
824 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
826 static int dvb_net_filter_sec_set(struct net_device *dev,
827 struct dmx_section_filter **secfilter,
828 u8 *mac, u8 *mac_mask)
830 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
834 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
836 printk("%s: could not get filter\n", dev->name);
840 (*secfilter)->priv=(void *) dev;
842 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
843 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
844 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
846 (*secfilter)->filter_value[0]=0x3e;
847 (*secfilter)->filter_value[3]=mac[5];
848 (*secfilter)->filter_value[4]=mac[4];
849 (*secfilter)->filter_value[8]=mac[3];
850 (*secfilter)->filter_value[9]=mac[2];
851 (*secfilter)->filter_value[10]=mac[1];
852 (*secfilter)->filter_value[11]=mac[0];
854 (*secfilter)->filter_mask[0] = 0xff;
855 (*secfilter)->filter_mask[3] = mac_mask[5];
856 (*secfilter)->filter_mask[4] = mac_mask[4];
857 (*secfilter)->filter_mask[8] = mac_mask[3];
858 (*secfilter)->filter_mask[9] = mac_mask[2];
859 (*secfilter)->filter_mask[10] = mac_mask[1];
860 (*secfilter)->filter_mask[11]=mac_mask[0];
862 dprintk("%s: filter mac=%02x %02x %02x %02x %02x %02x\n",
863 dev->name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
864 dprintk("%s: filter mask=%02x %02x %02x %02x %02x %02x\n",
865 dev->name, mac_mask[0], mac_mask[1], mac_mask[2],
866 mac_mask[3], mac_mask[4], mac_mask[5]);
871 static int dvb_net_feed_start(struct net_device *dev)
874 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
875 struct dmx_demux *demux = priv->demux;
876 unsigned char *mac = (unsigned char *) dev->dev_addr;
878 dprintk("%s: rx_mode %i\n", __FUNCTION__, priv->rx_mode);
879 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
880 printk("%s: BUG %d\n", __FUNCTION__, __LINE__);
883 priv->secfilter=NULL;
886 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
887 dprintk("%s: alloc secfeed\n", __FUNCTION__);
888 ret=demux->allocate_section_feed(demux, &priv->secfeed,
889 dvb_net_sec_callback);
891 printk("%s: could not allocate section feed\n", dev->name);
895 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 0, 1);
898 printk("%s: could not set section feed\n", dev->name);
899 priv->demux->release_section_feed(priv->demux, priv->secfeed);
904 if (priv->rx_mode != RX_MODE_PROMISC) {
905 dprintk("%s: set secfilter\n", __FUNCTION__);
906 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
909 switch (priv->rx_mode) {
911 for (i = 0; i < priv->multi_num; i++) {
912 dprintk("%s: set multi_secfilter[%d]\n", __FUNCTION__, i);
913 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
914 priv->multi_macs[i], mask_normal);
917 case RX_MODE_ALL_MULTI:
919 dprintk("%s: set multi_secfilter[0]\n", __FUNCTION__);
920 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
921 mac_allmulti, mask_allmulti);
923 case RX_MODE_PROMISC:
925 dprintk("%s: set secfilter\n", __FUNCTION__);
926 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
930 dprintk("%s: start filtering\n", __FUNCTION__);
931 priv->secfeed->start_filtering(priv->secfeed);
932 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
933 struct timespec timeout = { 0, 30000000 }; // 30 msec
935 /* we have payloads encapsulated in TS */
936 dprintk("%s: alloc tsfeed\n", __FUNCTION__);
937 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
939 printk("%s: could not allocate ts feed\n", dev->name);
943 /* Set netdevice pointer for ts decaps callback. */
944 priv->tsfeed->priv = (void *)dev;
945 ret = priv->tsfeed->set(priv->tsfeed, priv->pid,
946 TS_PACKET, DMX_TS_PES_OTHER,
947 188 * 100, /* nr. of bytes delivered per callback */
948 32768, /* circular buffer size */
953 printk("%s: could not set ts feed\n", dev->name);
954 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
959 dprintk("%s: start filtering\n", __FUNCTION__);
960 priv->tsfeed->start_filtering(priv->tsfeed);
967 static int dvb_net_feed_stop(struct net_device *dev)
969 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
972 dprintk("%s\n", __FUNCTION__);
973 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
975 if (priv->secfeed->is_filtering) {
976 dprintk("%s: stop secfeed\n", __FUNCTION__);
977 priv->secfeed->stop_filtering(priv->secfeed);
980 if (priv->secfilter) {
981 dprintk("%s: release secfilter\n", __FUNCTION__);
982 priv->secfeed->release_filter(priv->secfeed,
984 priv->secfilter=NULL;
987 for (i=0; i<priv->multi_num; i++) {
988 if (priv->multi_secfilter[i]) {
989 dprintk("%s: release multi_filter[%d]\n",
991 priv->secfeed->release_filter(priv->secfeed,
992 priv->multi_secfilter[i]);
993 priv->multi_secfilter[i]=NULL;
997 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1000 printk("%s: no feed to stop\n", dev->name);
1001 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1003 if (priv->tsfeed->is_filtering) {
1004 dprintk("%s: stop tsfeed\n", __FUNCTION__);
1005 priv->tsfeed->stop_filtering(priv->tsfeed);
1007 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1008 priv->tsfeed = NULL;
1011 printk("%s: no ts feed to stop\n", dev->name);
1018 static int dvb_set_mc_filter (struct net_device *dev, struct dev_mc_list *mc)
1020 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
1022 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1025 memcpy(priv->multi_macs[priv->multi_num], mc->dmi_addr, 6);
1032 static void wq_set_multicast_list (void *data)
1034 struct net_device *dev = data;
1035 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
1037 dvb_net_feed_stop(dev);
1039 priv->rx_mode = RX_MODE_UNI;
1041 if(dev->flags & IFF_PROMISC) {
1042 dprintk("%s: promiscuous mode\n", dev->name);
1043 priv->rx_mode = RX_MODE_PROMISC;
1044 } else if ((dev->flags & IFF_ALLMULTI)) {
1045 dprintk("%s: allmulti mode\n", dev->name);
1046 priv->rx_mode = RX_MODE_ALL_MULTI;
1047 } else if (dev->mc_count) {
1049 struct dev_mc_list *mc;
1051 dprintk("%s: set_mc_list, %d entries\n",
1052 dev->name, dev->mc_count);
1054 priv->rx_mode = RX_MODE_MULTI;
1055 priv->multi_num = 0;
1057 for (mci = 0, mc=dev->mc_list;
1058 mci < dev->mc_count;
1059 mc = mc->next, mci++) {
1060 dvb_set_mc_filter(dev, mc);
1064 dvb_net_feed_start(dev);
1068 static void dvb_net_set_multicast_list (struct net_device *dev)
1070 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
1071 schedule_work(&priv->set_multicast_list_wq);
1075 static void wq_restart_net_feed (void *data)
1077 struct net_device *dev = data;
1079 if (netif_running(dev)) {
1080 dvb_net_feed_stop(dev);
1081 dvb_net_feed_start(dev);
1086 static int dvb_net_set_mac (struct net_device *dev, void *p)
1088 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
1089 struct sockaddr *addr=p;
1091 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1093 if (netif_running(dev))
1094 schedule_work(&priv->restart_net_feed_wq);
1100 static int dvb_net_open(struct net_device *dev)
1102 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
1105 dvb_net_feed_start(dev);
1110 static int dvb_net_stop(struct net_device *dev)
1112 struct dvb_net_priv *priv = (struct dvb_net_priv*) dev->priv;
1115 return dvb_net_feed_stop(dev);
1118 static struct net_device_stats * dvb_net_get_stats(struct net_device *dev)
1120 return &((struct dvb_net_priv*) dev->priv)->stats;
1124 static void dvb_net_setup(struct net_device *dev)
1128 dev->open = dvb_net_open;
1129 dev->stop = dvb_net_stop;
1130 dev->hard_start_xmit = dvb_net_tx;
1131 dev->get_stats = dvb_net_get_stats;
1132 dev->set_multicast_list = dvb_net_set_multicast_list;
1133 dev->set_mac_address = dvb_net_set_mac;
1136 dev->hard_header_cache = NULL;
1138 dev->flags |= IFF_NOARP;
1141 static int get_if(struct dvb_net *dvbnet)
1145 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1146 if (!dvbnet->state[i])
1149 if (i == DVB_NET_DEVICES_MAX)
1156 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1158 struct net_device *net;
1159 struct dvb_net_priv *priv;
1163 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1165 if ((if_num = get_if(dvbnet)) < 0)
1168 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb",
1173 sprintf(net->name, "dvb%d_%d", dvbnet->dvbdev->adapter->num, if_num);
1176 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1178 dvbnet->device[if_num] = net;
1181 priv->demux = dvbnet->demux;
1183 priv->rx_mode = RX_MODE_UNI;
1184 priv->need_pusi = 1;
1186 priv->feedtype = feedtype;
1189 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list, net);
1190 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed, net);
1192 net->base_addr = pid;
1194 if ((result = register_netdev(net)) < 0) {
1195 dvbnet->device[if_num] = NULL;
1204 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned int num)
1206 struct net_device *net = dvbnet->device[num];
1207 struct dvb_net_priv *priv;
1209 if (!dvbnet->state[num])
1216 flush_scheduled_work();
1217 unregister_netdev(net);
1218 dvbnet->state[num]=0;
1219 dvbnet->device[num] = NULL;
1226 static int dvb_net_do_ioctl(struct inode *inode, struct file *file,
1227 unsigned int cmd, void *parg)
1229 struct dvb_device *dvbdev = (struct dvb_device *) file->private_data;
1230 struct dvb_net *dvbnet = (struct dvb_net *) dvbdev->priv;
1232 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1238 struct dvb_net_if *dvbnetif=(struct dvb_net_if *)parg;
1241 if (!capable(CAP_SYS_ADMIN))
1244 if (!try_module_get(dvbdev->adapter->module))
1247 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1249 module_put(dvbdev->adapter->module);
1252 dvbnetif->if_num=result;
1257 struct net_device *netdev;
1258 struct dvb_net_priv *priv_data;
1259 struct dvb_net_if *dvbnetif=(struct dvb_net_if *)parg;
1261 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1262 !dvbnet->state[dvbnetif->if_num])
1265 netdev = dvbnet->device[dvbnetif->if_num];
1266 priv_data=(struct dvb_net_priv*)netdev->priv;
1267 dvbnetif->pid=priv_data->pid;
1268 dvbnetif->feedtype=priv_data->feedtype;
1275 if (!capable(CAP_SYS_ADMIN))
1277 if ((unsigned int) parg >= DVB_NET_DEVICES_MAX)
1279 ret = dvb_net_remove_if(dvbnet, (unsigned int) parg);
1281 module_put(dvbdev->adapter->module);
1285 /* binary compatiblity cruft */
1286 case __NET_ADD_IF_OLD:
1288 struct __dvb_net_if_old *dvbnetif=(struct __dvb_net_if_old *)parg;
1291 if (!capable(CAP_SYS_ADMIN))
1294 if (!try_module_get(dvbdev->adapter->module))
1297 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1299 module_put(dvbdev->adapter->module);
1302 dvbnetif->if_num=result;
1305 case __NET_GET_IF_OLD:
1307 struct net_device *netdev;
1308 struct dvb_net_priv *priv_data;
1309 struct __dvb_net_if_old *dvbnetif=(struct __dvb_net_if_old *)parg;
1311 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1312 !dvbnet->state[dvbnetif->if_num])
1315 netdev = dvbnet->device[dvbnetif->if_num];
1316 priv_data=(struct dvb_net_priv*)netdev->priv;
1317 dvbnetif->pid=priv_data->pid;
1326 static int dvb_net_ioctl(struct inode *inode, struct file *file,
1327 unsigned int cmd, unsigned long arg)
1329 return dvb_usercopy(inode, file, cmd, arg, dvb_net_do_ioctl);
1332 static struct file_operations dvb_net_fops = {
1333 .owner = THIS_MODULE,
1334 .ioctl = dvb_net_ioctl,
1335 .open = dvb_generic_open,
1336 .release = dvb_generic_release,
1339 static struct dvb_device dvbdev_net = {
1343 .fops = &dvb_net_fops,
1347 void dvb_net_release (struct dvb_net *dvbnet)
1351 dvb_unregister_device(dvbnet->dvbdev);
1353 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1354 if (!dvbnet->state[i])
1356 dvb_net_remove_if(dvbnet, i);
1359 EXPORT_SYMBOL(dvb_net_release);
1362 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1363 struct dmx_demux *dmx)
1367 dvbnet->demux = dmx;
1369 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1370 dvbnet->state[i] = 0;
1372 dvb_register_device (adap, &dvbnet->dvbdev, &dvbdev_net,
1373 dvbnet, DVB_DEVICE_NET);
1377 EXPORT_SYMBOL(dvb_net_init);