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/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/mutex.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 MAC_ADDR_PRINTFMT "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
93 #define MAX_ADDR_PRINTFMT_ARGS(macap) (macap)[0],(macap)[1],(macap)[2],(macap)[3],(macap)[4],(macap)[5]
95 #define isprint(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
97 static void hexdump( const unsigned char *buf, unsigned short len )
99 char str[80], octet[10];
102 for (ofs = 0; ofs < len; ofs += 16) {
103 sprintf( str, "%03d: ", ofs );
105 for (i = 0; i < 16; i++) {
107 sprintf( octet, "%02x ", buf[ofs + i] );
109 strcpy( octet, " " );
111 strcat( str, octet );
116 for (i = 0; (i < 16) && ((i + ofs) < len); i++)
117 str[l++] = isprint( buf[ofs + i] ) ? buf[ofs + i] : '.';
120 printk( KERN_WARNING "%s\n", str );
126 struct dvb_net_priv {
129 struct net_device *net;
130 struct dvb_net *host;
131 struct dmx_demux *demux;
132 struct dmx_section_feed *secfeed;
133 struct dmx_section_filter *secfilter;
134 struct dmx_ts_feed *tsfeed;
136 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
137 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
139 #define RX_MODE_UNI 0
140 #define RX_MODE_MULTI 1
141 #define RX_MODE_ALL_MULTI 2
142 #define RX_MODE_PROMISC 3
143 struct work_struct set_multicast_list_wq;
144 struct work_struct restart_net_feed_wq;
145 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
146 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
147 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
148 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
149 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
150 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
151 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
152 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
153 unsigned char ule_dbit; /* Whether the DestMAC address present
154 * or not (bit is set). */
155 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
156 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
157 unsigned long ts_count; /* Current ts cell counter. */
163 * Determine the packet's protocol ID. The rule here is that we
164 * assume 802.3 if the type field is short enough to be a length.
165 * This is normal practice and works for any 'now in use' protocol.
167 * stolen from eth.c out of the linux kernel, hacked for dvb-device
168 * by Michael Holzt <kju@debian.org>
170 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
171 struct net_device *dev)
176 skb_reset_mac_header(skb);
177 skb_pull(skb,dev->hard_header_len);
180 if (*eth->h_dest & 1) {
181 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
182 skb->pkt_type=PACKET_BROADCAST;
184 skb->pkt_type=PACKET_MULTICAST;
187 if (ntohs(eth->h_proto) >= 1536)
193 * This is a magic hack to spot IPX packets. Older Novell breaks
194 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
195 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
196 * won't work for fault tolerant netware but does for the rest.
198 if (*(unsigned short *)rawp == 0xFFFF)
199 return htons(ETH_P_802_3);
204 return htons(ETH_P_802_2);
215 /* ULE Extension Header handlers. */
218 #define ULE_BRIDGED 1
220 #define ULE_OPTEXTHDR_PADDING 0
222 static int ule_test_sndu( struct dvb_net_priv *p )
227 static int ule_bridged_sndu( struct dvb_net_priv *p )
229 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
230 if(ntohs(hdr->h_proto) < 1536) {
231 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
232 /* A frame Type < 1536 for a bridged frame, introduces a LLC Length field. */
233 if(framelen != ntohs(hdr->h_proto)) {
239 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
240 * It must be the final (or only) extension header specified in the header chain of a SNDU."
241 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
247 static int ule_exthdr_padding(struct dvb_net_priv *p)
252 /** Handle ULE extension headers.
253 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
254 * Returns: >= 0: nr. of bytes consumed by next extension header
255 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
257 static int handle_one_ule_extension( struct dvb_net_priv *p )
259 /* Table of mandatory extension header handlers. The header type is the index. */
260 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
261 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
263 /* Table of optional extension header handlers. The header type is the index. */
264 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
265 { [0] = ule_exthdr_padding, [1] = NULL, };
268 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
269 unsigned char htype = p->ule_sndu_type & 0x00FF;
271 /* Discriminate mandatory and optional extension headers. */
273 /* Mandatory extension header */
274 if (ule_mandatory_ext_handlers[htype]) {
275 ext_len = ule_mandatory_ext_handlers[htype]( p );
277 p->ule_next_hdr += ext_len;
278 if (!p->ule_bridged) {
279 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
280 p->ule_next_hdr += 2;
282 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
283 /* This assures the extension handling loop will terminate. */
286 // else: extension handler failed or SNDU should be discarded
288 ext_len = -1; /* SNDU has to be discarded. */
290 /* Optional extension header. Calculate the length. */
292 /* Process the optional extension header according to its type. */
293 if (ule_optional_ext_handlers[htype])
294 (void)ule_optional_ext_handlers[htype]( p );
295 p->ule_next_hdr += ext_len;
296 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
298 * note: the length of the next header type is included in the
299 * length of THIS optional extension header
306 static int handle_ule_extensions( struct dvb_net_priv *p )
308 int total_ext_len = 0, l;
310 p->ule_next_hdr = p->ule_skb->data;
312 l = handle_one_ule_extension( p );
314 return l; /* Stop extension header processing and discard SNDU. */
317 dprintk("handle_ule_extensions: ule_next_hdr=%p, ule_sndu_type=%i, "
318 "l=%i, total_ext_len=%i\n", p->ule_next_hdr,
319 (int) p->ule_sndu_type, l, total_ext_len);
322 } while (p->ule_sndu_type < 1536);
324 return total_ext_len;
328 /** Prepare for a new ULE SNDU: reset the decoder state. */
329 static inline void reset_ule( struct dvb_net_priv *p )
332 p->ule_next_hdr = NULL;
334 p->ule_sndu_type = 0;
335 p->ule_sndu_type_1 = 0;
336 p->ule_sndu_remain = 0;
342 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
343 * TS cells of a single PID.
345 static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
347 struct dvb_net_priv *priv = netdev_priv(dev);
348 unsigned long skipped = 0L;
349 const u8 *ts, *ts_end, *from_where = NULL;
350 u8 ts_remain = 0, how_much = 0, new_ts = 1;
351 struct ethhdr *ethh = NULL;
354 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
355 static unsigned char ule_hist[100*TS_SZ];
356 static unsigned char *ule_where = ule_hist, ule_dump;
359 /* For all TS cells in current buffer.
360 * Appearently, we are called for every single TS cell.
362 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
365 /* We are about to process a new TS cell. */
368 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
369 memcpy( ule_where, ts, TS_SZ );
371 hexdump( ule_where, TS_SZ );
377 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
378 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
379 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
380 priv->ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
382 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
384 dev_kfree_skb( priv->ule_skb );
385 /* Prepare for next SNDU. */
386 dev->stats.rx_errors++;
387 dev->stats.rx_frame_errors++;
392 /* Continue with next TS cell. */
401 /* Synchronize on PUSI, if required. */
402 if (priv->need_pusi) {
403 if (ts[1] & TS_PUSI) {
404 /* Find beginning of first ULE SNDU in current TS cell. */
405 /* Synchronize continuity counter. */
406 priv->tscc = ts[3] & 0x0F;
407 /* There is a pointer field here. */
408 if (ts[4] > ts_remain) {
409 printk(KERN_ERR "%lu: Invalid ULE packet "
410 "(pointer field %d)\n", priv->ts_count, ts[4]);
415 /* Skip to destination of pointer field. */
416 from_where = &ts[5] + ts[4];
417 ts_remain -= 1 + ts[4];
428 /* Check continuity counter. */
429 if ((ts[3] & 0x0F) == priv->tscc)
430 priv->tscc = (priv->tscc + 1) & 0x0F;
432 /* TS discontinuity handling: */
433 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
434 "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
435 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
437 dev_kfree_skb( priv->ule_skb );
438 /* Prepare for next SNDU. */
439 // reset_ule(priv); moved to below.
440 dev->stats.rx_errors++;
441 dev->stats.rx_frame_errors++;
444 /* skip to next PUSI. */
448 /* If we still have an incomplete payload, but PUSI is
449 * set; some TS cells are missing.
450 * This is only possible here, if we missed exactly 16 TS
451 * cells (continuity counter wrap). */
452 if (ts[1] & TS_PUSI) {
453 if (! priv->need_pusi) {
454 if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
455 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
456 printk(KERN_WARNING "%lu: Invalid pointer "
457 "field: %u.\n", priv->ts_count, *from_where);
459 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
461 dev_kfree_skb( priv->ule_skb );
462 dev->stats.rx_errors++;
463 dev->stats.rx_frame_errors++;
469 /* Skip pointer field (we're processing a
470 * packed payload). */
476 if (priv->ule_sndu_remain > 183) {
477 /* Current SNDU lacks more data than there could be available in the
478 * current TS cell. */
479 dev->stats.rx_errors++;
480 dev->stats.rx_length_errors++;
481 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
482 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
483 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
484 dev_kfree_skb(priv->ule_skb);
485 /* Prepare for next SNDU. */
487 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
494 /* Check if new payload needs to be started. */
495 if (priv->ule_skb == NULL) {
496 /* Start a new payload with skb.
497 * Find ULE header. It is only guaranteed that the
498 * length field (2 bytes) is contained in the current
500 * Check ts_remain has to be >= 2 here. */
502 printk(KERN_WARNING "Invalid payload packing: only %d "
503 "bytes left in TS. Resyncing.\n", ts_remain);
504 priv->ule_sndu_len = 0;
509 if (! priv->ule_sndu_len) {
510 /* Got at least two bytes, thus extrace the SNDU length. */
511 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
512 if (priv->ule_sndu_len & 0x8000) {
513 /* D-Bit is set: no dest mac present. */
514 priv->ule_sndu_len &= 0x7FFF;
519 if (priv->ule_sndu_len < 5) {
520 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
521 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
522 dev->stats.rx_errors++;
523 dev->stats.rx_length_errors++;
524 priv->ule_sndu_len = 0;
531 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
536 * State of current TS:
537 * ts_remain (remaining bytes in the current TS cell)
538 * 0 ule_type is not available now, we need the next TS cell
539 * 1 the first byte of the ule_type is present
540 * >=2 full ULE header present, maybe some payload data as well.
544 priv->ule_sndu_type = from_where[0] << 8;
545 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
546 ts_remain -= 1; from_where += 1;
547 /* Continue w/ next TS. */
554 default: /* complete ULE header is present in current TS. */
555 /* Extract ULE type field. */
556 if (priv->ule_sndu_type_1) {
557 priv->ule_sndu_type |= from_where[0];
558 from_where += 1; /* points to payload start. */
561 /* Complete type is present in new TS. */
562 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
563 from_where += 2; /* points to payload start. */
569 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
570 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
571 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
572 if (priv->ule_skb == NULL) {
573 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
575 dev->stats.rx_dropped++;
579 /* This includes the CRC32 _and_ dest mac, if !dbit. */
580 priv->ule_sndu_remain = priv->ule_sndu_len;
581 priv->ule_skb->dev = dev;
582 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
583 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
586 /* Copy data into our current skb. */
587 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
588 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
589 priv->ule_sndu_remain -= how_much;
590 ts_remain -= how_much;
591 from_where += how_much;
593 /* Check for complete payload. */
594 if (priv->ule_sndu_remain <= 0) {
595 /* Check CRC32, we've got it in our skb already. */
596 __be16 ulen = htons(priv->ule_sndu_len);
597 __be16 utype = htons(priv->ule_sndu_type);
599 struct kvec iov[3] = {
600 { &ulen, sizeof ulen },
601 { &utype, sizeof utype },
602 { priv->ule_skb->data, priv->ule_skb->len - 4 }
604 u32 ule_crc = ~0L, expected_crc;
605 if (priv->ule_dbit) {
606 /* Set D-bit for CRC32 verification,
607 * if it was set originally. */
608 ulen |= htons(0x8000);
611 ule_crc = iov_crc32(ule_crc, iov, 3);
612 tail = skb_tail_pointer(priv->ule_skb);
613 expected_crc = *(tail - 4) << 24 |
617 if (ule_crc != expected_crc) {
618 printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
619 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);
622 hexdump( iov[0].iov_base, iov[0].iov_len );
623 hexdump( iov[1].iov_base, iov[1].iov_len );
624 hexdump( iov[2].iov_base, iov[2].iov_len );
626 if (ule_where == ule_hist) {
627 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
628 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
629 } else if (ule_where == &ule_hist[TS_SZ]) {
630 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
631 hexdump( ule_hist, TS_SZ );
633 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
634 hexdump( ule_where - TS_SZ, TS_SZ );
639 dev->stats.rx_errors++;
640 dev->stats.rx_crc_errors++;
641 dev_kfree_skb(priv->ule_skb);
643 /* CRC32 verified OK. */
644 u8 dest_addr[ETH_ALEN];
645 static const u8 bc_addr[ETH_ALEN] =
646 { [ 0 ... ETH_ALEN-1] = 0xff };
648 /* CRC32 was OK. Remove it from skb. */
649 priv->ule_skb->tail -= 4;
650 priv->ule_skb->len -= 4;
652 if (!priv->ule_dbit) {
654 * The destination MAC address is the
655 * next data in the skb. It comes
656 * before any extension headers.
658 * Check if the payload of this SNDU
659 * should be passed up the stack.
661 register int drop = 0;
662 if (priv->rx_mode != RX_MODE_PROMISC) {
663 if (priv->ule_skb->data[0] & 0x01) {
664 /* multicast or broadcast */
665 if (memcmp(priv->ule_skb->data, bc_addr, ETH_ALEN)) {
667 if (priv->rx_mode == RX_MODE_MULTI) {
669 for(i = 0; i < priv->multi_num && memcmp(priv->ule_skb->data, priv->multi_macs[i], ETH_ALEN); i++)
671 if (i == priv->multi_num)
673 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
674 drop = 1; /* no broadcast; */
675 /* else: all multicast mode: accept all multicast packets */
677 /* else: broadcast */
679 else if (memcmp(priv->ule_skb->data, dev->dev_addr, ETH_ALEN))
681 /* else: destination address matches the MAC address of our receiver device */
683 /* else: promiscuous mode; pass everything up the stack */
687 dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
688 MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
690 dev_kfree_skb(priv->ule_skb);
695 skb_copy_from_linear_data(priv->ule_skb,
698 skb_pull(priv->ule_skb, ETH_ALEN);
702 /* Handle ULE Extension Headers. */
703 if (priv->ule_sndu_type < 1536) {
704 /* There is an extension header. Handle it accordingly. */
705 int l = handle_ule_extensions(priv);
707 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
708 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
709 dev_kfree_skb(priv->ule_skb);
712 skb_pull(priv->ule_skb, l);
716 * Construct/assure correct ethernet header.
717 * Note: in bridged mode (priv->ule_bridged !=
718 * 0) we already have the (original) ethernet
719 * header at the start of the payload (after
720 * optional dest. address and any extension
724 if (!priv->ule_bridged) {
725 skb_push(priv->ule_skb, ETH_HLEN);
726 ethh = (struct ethhdr *)priv->ule_skb->data;
727 if (!priv->ule_dbit) {
728 /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
729 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
730 memset(ethh->h_source, 0, ETH_ALEN);
732 else /* zeroize source and dest */
733 memset( ethh, 0, ETH_ALEN*2 );
735 ethh->h_proto = htons(priv->ule_sndu_type);
737 /* else: skb is in correct state; nothing to do. */
738 priv->ule_bridged = 0;
740 /* Stuff into kernel's protocol stack. */
741 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
742 /* If D-bit is set (i.e. destination MAC address not present),
743 * receive the packet anyhow. */
744 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
745 priv->ule_skb->pkt_type = PACKET_HOST; */
746 dev->stats.rx_packets++;
747 dev->stats.rx_bytes += priv->ule_skb->len;
748 netif_rx(priv->ule_skb);
751 /* Prepare for next SNDU. */
755 /* More data in current TS (look at the bytes following the CRC32)? */
756 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
757 /* Next ULE SNDU starts right there. */
759 priv->ule_skb = NULL;
760 priv->ule_sndu_type_1 = 0;
761 priv->ule_sndu_len = 0;
762 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
763 // *(from_where + 0), *(from_where + 1),
764 // *(from_where + 2), *(from_where + 3));
765 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
771 if (priv->ule_skb == NULL) {
773 priv->ule_sndu_type_1 = 0;
774 priv->ule_sndu_len = 0;
777 } /* for all available TS cells */
780 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
781 const u8 *buffer2, size_t buffer2_len,
782 struct dmx_ts_feed *feed, enum dmx_success success)
784 struct net_device *dev = feed->priv;
787 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
788 if (buffer1_len > 32768)
789 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
790 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
791 buffer1_len, buffer1_len / TS_SZ, buffer1); */
792 dvb_net_ule(dev, buffer1, buffer1_len);
797 static void dvb_net_sec(struct net_device *dev,
798 const u8 *pkt, int pkt_len)
802 struct net_device_stats *stats = &dev->stats;
805 /* note: pkt_len includes a 32bit checksum */
807 printk("%s: IP/MPE packet length = %d too small.\n",
810 stats->rx_length_errors++;
813 /* it seems some ISPs manage to screw up here, so we have to
814 * relax the error checks... */
816 if ((pkt[5] & 0xfd) != 0xc1) {
817 /* drop scrambled or broken packets */
819 if ((pkt[5] & 0x3c) != 0x00) {
823 stats->rx_crc_errors++;
827 /* handle LLC/SNAP, see rfc-1042 */
828 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
835 /* FIXME: assemble datagram from multiple sections */
837 stats->rx_frame_errors++;
841 /* we have 14 byte ethernet header (ip header follows);
842 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
844 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
845 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
849 skb_reserve(skb, 2); /* longword align L3 header */
852 /* copy L3 payload */
853 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
854 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
856 /* create ethernet header: */
864 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
870 /* protocol numbers are from rfc-1700 or
871 * http://www.iana.org/assignments/ethernet-numbers
873 if (pkt[12] >> 4 == 6) { /* version field from IP header */
874 eth[12] = 0x86; /* IPv6 */
877 eth[12] = 0x08; /* IPv4 */
882 skb->protocol = dvb_net_eth_type_trans(skb, dev);
885 stats->rx_bytes+=skb->len;
889 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
890 const u8 *buffer2, size_t buffer2_len,
891 struct dmx_section_filter *filter,
892 enum dmx_success success)
894 struct net_device *dev = filter->priv;
897 * we rely on the DVB API definition where exactly one complete
898 * section is delivered in buffer1
900 dvb_net_sec (dev, buffer1, buffer1_len);
904 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
910 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
911 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
912 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
913 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
915 static int dvb_net_filter_sec_set(struct net_device *dev,
916 struct dmx_section_filter **secfilter,
917 u8 *mac, u8 *mac_mask)
919 struct dvb_net_priv *priv = netdev_priv(dev);
923 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
925 printk("%s: could not get filter\n", dev->name);
929 (*secfilter)->priv=(void *) dev;
931 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
932 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
933 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
935 (*secfilter)->filter_value[0]=0x3e;
936 (*secfilter)->filter_value[3]=mac[5];
937 (*secfilter)->filter_value[4]=mac[4];
938 (*secfilter)->filter_value[8]=mac[3];
939 (*secfilter)->filter_value[9]=mac[2];
940 (*secfilter)->filter_value[10]=mac[1];
941 (*secfilter)->filter_value[11]=mac[0];
943 (*secfilter)->filter_mask[0] = 0xff;
944 (*secfilter)->filter_mask[3] = mac_mask[5];
945 (*secfilter)->filter_mask[4] = mac_mask[4];
946 (*secfilter)->filter_mask[8] = mac_mask[3];
947 (*secfilter)->filter_mask[9] = mac_mask[2];
948 (*secfilter)->filter_mask[10] = mac_mask[1];
949 (*secfilter)->filter_mask[11]=mac_mask[0];
951 dprintk("%s: filter mac=%02x %02x %02x %02x %02x %02x\n",
952 dev->name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
953 dprintk("%s: filter mask=%02x %02x %02x %02x %02x %02x\n",
954 dev->name, mac_mask[0], mac_mask[1], mac_mask[2],
955 mac_mask[3], mac_mask[4], mac_mask[5]);
960 static int dvb_net_feed_start(struct net_device *dev)
963 struct dvb_net_priv *priv = netdev_priv(dev);
964 struct dmx_demux *demux = priv->demux;
965 unsigned char *mac = (unsigned char *) dev->dev_addr;
967 dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
968 mutex_lock(&priv->mutex);
969 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
970 printk("%s: BUG %d\n", __func__, __LINE__);
973 priv->secfilter=NULL;
976 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
977 dprintk("%s: alloc secfeed\n", __func__);
978 ret=demux->allocate_section_feed(demux, &priv->secfeed,
979 dvb_net_sec_callback);
981 printk("%s: could not allocate section feed\n", dev->name);
985 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
988 printk("%s: could not set section feed\n", dev->name);
989 priv->demux->release_section_feed(priv->demux, priv->secfeed);
994 if (priv->rx_mode != RX_MODE_PROMISC) {
995 dprintk("%s: set secfilter\n", __func__);
996 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
999 switch (priv->rx_mode) {
1001 for (i = 0; i < priv->multi_num; i++) {
1002 dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
1003 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1004 priv->multi_macs[i], mask_normal);
1007 case RX_MODE_ALL_MULTI:
1009 dprintk("%s: set multi_secfilter[0]\n", __func__);
1010 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1011 mac_allmulti, mask_allmulti);
1013 case RX_MODE_PROMISC:
1015 dprintk("%s: set secfilter\n", __func__);
1016 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1020 dprintk("%s: start filtering\n", __func__);
1021 priv->secfeed->start_filtering(priv->secfeed);
1022 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1023 struct timespec timeout = { 0, 10000000 }; // 10 msec
1025 /* we have payloads encapsulated in TS */
1026 dprintk("%s: alloc tsfeed\n", __func__);
1027 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1029 printk("%s: could not allocate ts feed\n", dev->name);
1033 /* Set netdevice pointer for ts decaps callback. */
1034 priv->tsfeed->priv = (void *)dev;
1035 ret = priv->tsfeed->set(priv->tsfeed,
1036 priv->pid, /* pid */
1037 TS_PACKET, /* type */
1038 DMX_TS_PES_OTHER, /* pes type */
1039 32768, /* circular buffer size */
1040 timeout /* timeout */
1044 printk("%s: could not set ts feed\n", dev->name);
1045 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1046 priv->tsfeed = NULL;
1050 dprintk("%s: start filtering\n", __func__);
1051 priv->tsfeed->start_filtering(priv->tsfeed);
1056 mutex_unlock(&priv->mutex);
1060 static int dvb_net_feed_stop(struct net_device *dev)
1062 struct dvb_net_priv *priv = netdev_priv(dev);
1065 dprintk("%s\n", __func__);
1066 mutex_lock(&priv->mutex);
1067 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1068 if (priv->secfeed) {
1069 if (priv->secfeed->is_filtering) {
1070 dprintk("%s: stop secfeed\n", __func__);
1071 priv->secfeed->stop_filtering(priv->secfeed);
1074 if (priv->secfilter) {
1075 dprintk("%s: release secfilter\n", __func__);
1076 priv->secfeed->release_filter(priv->secfeed,
1078 priv->secfilter=NULL;
1081 for (i=0; i<priv->multi_num; i++) {
1082 if (priv->multi_secfilter[i]) {
1083 dprintk("%s: release multi_filter[%d]\n",
1085 priv->secfeed->release_filter(priv->secfeed,
1086 priv->multi_secfilter[i]);
1087 priv->multi_secfilter[i] = NULL;
1091 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1092 priv->secfeed = NULL;
1094 printk("%s: no feed to stop\n", dev->name);
1095 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1097 if (priv->tsfeed->is_filtering) {
1098 dprintk("%s: stop tsfeed\n", __func__);
1099 priv->tsfeed->stop_filtering(priv->tsfeed);
1101 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1102 priv->tsfeed = NULL;
1105 printk("%s: no ts feed to stop\n", dev->name);
1108 mutex_unlock(&priv->mutex);
1113 static int dvb_set_mc_filter (struct net_device *dev, struct dev_mc_list *mc)
1115 struct dvb_net_priv *priv = netdev_priv(dev);
1117 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1120 memcpy(priv->multi_macs[priv->multi_num], mc->dmi_addr, 6);
1127 static void wq_set_multicast_list (struct work_struct *work)
1129 struct dvb_net_priv *priv =
1130 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1131 struct net_device *dev = priv->net;
1133 dvb_net_feed_stop(dev);
1134 priv->rx_mode = RX_MODE_UNI;
1135 netif_addr_lock_bh(dev);
1137 if (dev->flags & IFF_PROMISC) {
1138 dprintk("%s: promiscuous mode\n", dev->name);
1139 priv->rx_mode = RX_MODE_PROMISC;
1140 } else if ((dev->flags & IFF_ALLMULTI)) {
1141 dprintk("%s: allmulti mode\n", dev->name);
1142 priv->rx_mode = RX_MODE_ALL_MULTI;
1143 } else if (dev->mc_count) {
1145 struct dev_mc_list *mc;
1147 dprintk("%s: set_mc_list, %d entries\n",
1148 dev->name, dev->mc_count);
1150 priv->rx_mode = RX_MODE_MULTI;
1151 priv->multi_num = 0;
1153 for (mci = 0, mc=dev->mc_list;
1154 mci < dev->mc_count;
1155 mc = mc->next, mci++) {
1156 dvb_set_mc_filter(dev, mc);
1160 netif_addr_unlock_bh(dev);
1161 dvb_net_feed_start(dev);
1165 static void dvb_net_set_multicast_list (struct net_device *dev)
1167 struct dvb_net_priv *priv = netdev_priv(dev);
1168 schedule_work(&priv->set_multicast_list_wq);
1172 static void wq_restart_net_feed (struct work_struct *work)
1174 struct dvb_net_priv *priv =
1175 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1176 struct net_device *dev = priv->net;
1178 if (netif_running(dev)) {
1179 dvb_net_feed_stop(dev);
1180 dvb_net_feed_start(dev);
1185 static int dvb_net_set_mac (struct net_device *dev, void *p)
1187 struct dvb_net_priv *priv = netdev_priv(dev);
1188 struct sockaddr *addr=p;
1190 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1192 if (netif_running(dev))
1193 schedule_work(&priv->restart_net_feed_wq);
1199 static int dvb_net_open(struct net_device *dev)
1201 struct dvb_net_priv *priv = netdev_priv(dev);
1204 dvb_net_feed_start(dev);
1209 static int dvb_net_stop(struct net_device *dev)
1211 struct dvb_net_priv *priv = netdev_priv(dev);
1214 return dvb_net_feed_stop(dev);
1217 static const struct header_ops dvb_header_ops = {
1218 .create = eth_header,
1219 .parse = eth_header_parse,
1220 .rebuild = eth_rebuild_header,
1224 static const struct net_device_ops dvb_netdev_ops = {
1225 .ndo_open = dvb_net_open,
1226 .ndo_stop = dvb_net_stop,
1227 .ndo_start_xmit = dvb_net_tx,
1228 .ndo_set_multicast_list = dvb_net_set_multicast_list,
1229 .ndo_set_mac_address = dvb_net_set_mac,
1230 .ndo_change_mtu = eth_change_mtu,
1231 .ndo_validate_addr = eth_validate_addr,
1234 static void dvb_net_setup(struct net_device *dev)
1238 dev->header_ops = &dvb_header_ops;
1239 dev->netdev_ops = &dvb_netdev_ops;
1243 dev->flags |= IFF_NOARP;
1246 static int get_if(struct dvb_net *dvbnet)
1250 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1251 if (!dvbnet->state[i])
1254 if (i == DVB_NET_DEVICES_MAX)
1261 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1263 struct net_device *net;
1264 struct dvb_net_priv *priv;
1268 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1270 if ((if_num = get_if(dvbnet)) < 0)
1273 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", dvb_net_setup);
1277 if (dvbnet->dvbdev->id)
1278 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1279 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1281 /* compatibility fix to keep dvb0_0 format */
1282 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1283 dvbnet->dvbdev->adapter->num, if_num);
1286 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1288 dvbnet->device[if_num] = net;
1290 priv = netdev_priv(net);
1292 priv->demux = dvbnet->demux;
1294 priv->rx_mode = RX_MODE_UNI;
1295 priv->need_pusi = 1;
1297 priv->feedtype = feedtype;
1300 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1301 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1302 mutex_init(&priv->mutex);
1304 net->base_addr = pid;
1306 if ((result = register_netdev(net)) < 0) {
1307 dvbnet->device[if_num] = NULL;
1311 printk("dvb_net: created network interface %s\n", net->name);
1316 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1318 struct net_device *net = dvbnet->device[num];
1319 struct dvb_net_priv *priv;
1321 if (!dvbnet->state[num])
1323 priv = netdev_priv(net);
1328 flush_scheduled_work();
1329 printk("dvb_net: removed network interface %s\n", net->name);
1330 unregister_netdev(net);
1331 dvbnet->state[num]=0;
1332 dvbnet->device[num] = NULL;
1338 static int dvb_net_do_ioctl(struct inode *inode, struct file *file,
1339 unsigned int cmd, void *parg)
1341 struct dvb_device *dvbdev = file->private_data;
1342 struct dvb_net *dvbnet = dvbdev->priv;
1344 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1350 struct dvb_net_if *dvbnetif = parg;
1353 if (!capable(CAP_SYS_ADMIN))
1356 if (!try_module_get(dvbdev->adapter->module))
1359 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1361 module_put(dvbdev->adapter->module);
1364 dvbnetif->if_num=result;
1369 struct net_device *netdev;
1370 struct dvb_net_priv *priv_data;
1371 struct dvb_net_if *dvbnetif = parg;
1373 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1374 !dvbnet->state[dvbnetif->if_num])
1377 netdev = dvbnet->device[dvbnetif->if_num];
1379 priv_data = netdev_priv(netdev);
1380 dvbnetif->pid=priv_data->pid;
1381 dvbnetif->feedtype=priv_data->feedtype;
1388 if (!capable(CAP_SYS_ADMIN))
1390 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX)
1392 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1394 module_put(dvbdev->adapter->module);
1398 /* binary compatiblity cruft */
1399 case __NET_ADD_IF_OLD:
1401 struct __dvb_net_if_old *dvbnetif = parg;
1404 if (!capable(CAP_SYS_ADMIN))
1407 if (!try_module_get(dvbdev->adapter->module))
1410 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1412 module_put(dvbdev->adapter->module);
1415 dvbnetif->if_num=result;
1418 case __NET_GET_IF_OLD:
1420 struct net_device *netdev;
1421 struct dvb_net_priv *priv_data;
1422 struct __dvb_net_if_old *dvbnetif = parg;
1424 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1425 !dvbnet->state[dvbnetif->if_num])
1428 netdev = dvbnet->device[dvbnetif->if_num];
1430 priv_data = netdev_priv(netdev);
1431 dvbnetif->pid=priv_data->pid;
1440 static int dvb_net_ioctl(struct inode *inode, struct file *file,
1441 unsigned int cmd, unsigned long arg)
1443 return dvb_usercopy(inode, file, cmd, arg, dvb_net_do_ioctl);
1446 static int dvb_net_close(struct inode *inode, struct file *file)
1448 struct dvb_device *dvbdev = file->private_data;
1449 struct dvb_net *dvbnet = dvbdev->priv;
1451 dvb_generic_release(inode, file);
1453 if(dvbdev->users == 1 && dvbnet->exit == 1) {
1454 fops_put(file->f_op);
1456 wake_up(&dvbdev->wait_queue);
1462 static struct file_operations dvb_net_fops = {
1463 .owner = THIS_MODULE,
1464 .ioctl = dvb_net_ioctl,
1465 .open = dvb_generic_open,
1466 .release = dvb_net_close,
1469 static struct dvb_device dvbdev_net = {
1473 .fops = &dvb_net_fops,
1477 void dvb_net_release (struct dvb_net *dvbnet)
1482 if (dvbnet->dvbdev->users < 1)
1483 wait_event(dvbnet->dvbdev->wait_queue,
1484 dvbnet->dvbdev->users==1);
1486 dvb_unregister_device(dvbnet->dvbdev);
1488 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1489 if (!dvbnet->state[i])
1491 dvb_net_remove_if(dvbnet, i);
1494 EXPORT_SYMBOL(dvb_net_release);
1497 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1498 struct dmx_demux *dmx)
1502 dvbnet->demux = dmx;
1504 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1505 dvbnet->state[i] = 0;
1507 dvb_register_device (adap, &dvbnet->dvbdev, &dvbdev_net,
1508 dvbnet, DVB_DEVICE_NET);
1512 EXPORT_SYMBOL(dvb_net_init);