Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / dvb / dvb-core / dvb_net.c
blob441c0642b30a11b7cd443125a97c490b2ba31d9b
1 /*
2 * dvb_net.c
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
34 * ULE ChangeLog:
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>
66 #include <linux/sched.h>
68 #include "dvb_demux.h"
69 #include "dvb_net.h"
71 static int dvb_net_debug;
72 module_param(dvb_net_debug, int, 0444);
73 MODULE_PARM_DESC(dvb_net_debug, "enable debug messages");
75 #define dprintk(x...) do { if (dvb_net_debug) printk(x); } while (0)
78 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt )
80 unsigned int j;
81 for (j = 0; j < cnt; j++)
82 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len );
83 return c;
87 #define DVB_NET_MULTICAST_MAX 10
89 #undef ULE_DEBUG
91 #ifdef ULE_DEBUG
93 #define MAC_ADDR_PRINTFMT "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
94 #define MAX_ADDR_PRINTFMT_ARGS(macap) (macap)[0],(macap)[1],(macap)[2],(macap)[3],(macap)[4],(macap)[5]
96 #define isprint(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
98 static void hexdump( const unsigned char *buf, unsigned short len )
100 char str[80], octet[10];
101 int ofs, i, l;
103 for (ofs = 0; ofs < len; ofs += 16) {
104 sprintf( str, "%03d: ", ofs );
106 for (i = 0; i < 16; i++) {
107 if ((i + ofs) < len)
108 sprintf( octet, "%02x ", buf[ofs + i] );
109 else
110 strcpy( octet, " " );
112 strcat( str, octet );
114 strcat( str, " " );
115 l = strlen( str );
117 for (i = 0; (i < 16) && ((i + ofs) < len); i++)
118 str[l++] = isprint( buf[ofs + i] ) ? buf[ofs + i] : '.';
120 str[l] = '\0';
121 printk( KERN_WARNING "%s\n", str );
125 #endif
127 struct dvb_net_priv {
128 int in_use;
129 u16 pid;
130 struct net_device *net;
131 struct dvb_net *host;
132 struct dmx_demux *demux;
133 struct dmx_section_feed *secfeed;
134 struct dmx_section_filter *secfilter;
135 struct dmx_ts_feed *tsfeed;
136 int multi_num;
137 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX];
138 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6];
139 int rx_mode;
140 #define RX_MODE_UNI 0
141 #define RX_MODE_MULTI 1
142 #define RX_MODE_ALL_MULTI 2
143 #define RX_MODE_PROMISC 3
144 struct work_struct set_multicast_list_wq;
145 struct work_struct restart_net_feed_wq;
146 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */
147 int need_pusi; /* Set to 1, if synchronization on PUSI required. */
148 unsigned char tscc; /* TS continuity counter after sync on PUSI. */
149 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */
150 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */
151 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */
152 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */
153 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */
154 unsigned char ule_dbit; /* Whether the DestMAC address present
155 * or not (bit is set). */
156 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */
157 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */
158 unsigned long ts_count; /* Current ts cell counter. */
159 struct mutex mutex;
164 * Determine the packet's protocol ID. The rule here is that we
165 * assume 802.3 if the type field is short enough to be a length.
166 * This is normal practice and works for any 'now in use' protocol.
168 * stolen from eth.c out of the linux kernel, hacked for dvb-device
169 * by Michael Holzt <kju@debian.org>
171 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb,
172 struct net_device *dev)
174 struct ethhdr *eth;
175 unsigned char *rawp;
177 skb_reset_mac_header(skb);
178 skb_pull(skb,dev->hard_header_len);
179 eth = eth_hdr(skb);
181 if (*eth->h_dest & 1) {
182 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
183 skb->pkt_type=PACKET_BROADCAST;
184 else
185 skb->pkt_type=PACKET_MULTICAST;
188 if (ntohs(eth->h_proto) >= 1536)
189 return eth->h_proto;
191 rawp = skb->data;
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);
203 * Real 802.2 LLC
205 return htons(ETH_P_802_2);
208 #define TS_SZ 188
209 #define TS_SYNC 0x47
210 #define TS_TEI 0x80
211 #define TS_SC 0xC0
212 #define TS_PUSI 0x40
213 #define TS_AF_A 0x20
214 #define TS_AF_D 0x10
216 /* ULE Extension Header handlers. */
218 #define ULE_TEST 0
219 #define ULE_BRIDGED 1
221 #define ULE_OPTEXTHDR_PADDING 0
223 static int ule_test_sndu( struct dvb_net_priv *p )
225 return -1;
228 static int ule_bridged_sndu( struct dvb_net_priv *p )
230 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr;
231 if(ntohs(hdr->h_proto) < 1536) {
232 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data);
233 /* A frame Type < 1536 for a bridged frame, introduces a LLC Length field. */
234 if(framelen != ntohs(hdr->h_proto)) {
235 return -1;
238 /* Note:
239 * From RFC4326:
240 * "A bridged SNDU is a Mandatory Extension Header of Type 1.
241 * It must be the final (or only) extension header specified in the header chain of a SNDU."
242 * The 'ule_bridged' flag will cause the extension header processing loop to terminate.
244 p->ule_bridged = 1;
245 return 0;
248 static int ule_exthdr_padding(struct dvb_net_priv *p)
250 return 0;
253 /** Handle ULE extension headers.
254 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding.
255 * Returns: >= 0: nr. of bytes consumed by next extension header
256 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard.
258 static int handle_one_ule_extension( struct dvb_net_priv *p )
260 /* Table of mandatory extension header handlers. The header type is the index. */
261 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) =
262 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, };
264 /* Table of optional extension header handlers. The header type is the index. */
265 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) =
266 { [0] = ule_exthdr_padding, [1] = NULL, };
268 int ext_len = 0;
269 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8;
270 unsigned char htype = p->ule_sndu_type & 0x00FF;
272 /* Discriminate mandatory and optional extension headers. */
273 if (hlen == 0) {
274 /* Mandatory extension header */
275 if (ule_mandatory_ext_handlers[htype]) {
276 ext_len = ule_mandatory_ext_handlers[htype]( p );
277 if(ext_len >= 0) {
278 p->ule_next_hdr += ext_len;
279 if (!p->ule_bridged) {
280 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr);
281 p->ule_next_hdr += 2;
282 } else {
283 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN)));
284 /* This assures the extension handling loop will terminate. */
287 // else: extension handler failed or SNDU should be discarded
288 } else
289 ext_len = -1; /* SNDU has to be discarded. */
290 } else {
291 /* Optional extension header. Calculate the length. */
292 ext_len = hlen << 1;
293 /* Process the optional extension header according to its type. */
294 if (ule_optional_ext_handlers[htype])
295 (void)ule_optional_ext_handlers[htype]( p );
296 p->ule_next_hdr += ext_len;
297 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) );
299 * note: the length of the next header type is included in the
300 * length of THIS optional extension header
304 return ext_len;
307 static int handle_ule_extensions( struct dvb_net_priv *p )
309 int total_ext_len = 0, l;
311 p->ule_next_hdr = p->ule_skb->data;
312 do {
313 l = handle_one_ule_extension( p );
314 if (l < 0)
315 return l; /* Stop extension header processing and discard SNDU. */
316 total_ext_len += l;
317 #ifdef ULE_DEBUG
318 dprintk("handle_ule_extensions: ule_next_hdr=%p, ule_sndu_type=%i, "
319 "l=%i, total_ext_len=%i\n", p->ule_next_hdr,
320 (int) p->ule_sndu_type, l, total_ext_len);
321 #endif
323 } while (p->ule_sndu_type < 1536);
325 return total_ext_len;
329 /** Prepare for a new ULE SNDU: reset the decoder state. */
330 static inline void reset_ule( struct dvb_net_priv *p )
332 p->ule_skb = NULL;
333 p->ule_next_hdr = NULL;
334 p->ule_sndu_len = 0;
335 p->ule_sndu_type = 0;
336 p->ule_sndu_type_1 = 0;
337 p->ule_sndu_remain = 0;
338 p->ule_dbit = 0xFF;
339 p->ule_bridged = 0;
343 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of
344 * TS cells of a single PID.
346 static void dvb_net_ule( struct net_device *dev, const u8 *buf, size_t buf_len )
348 struct dvb_net_priv *priv = netdev_priv(dev);
349 unsigned long skipped = 0L;
350 const u8 *ts, *ts_end, *from_where = NULL;
351 u8 ts_remain = 0, how_much = 0, new_ts = 1;
352 struct ethhdr *ethh = NULL;
354 #ifdef ULE_DEBUG
355 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
356 static unsigned char ule_hist[100*TS_SZ];
357 static unsigned char *ule_where = ule_hist, ule_dump;
358 #endif
360 /* For all TS cells in current buffer.
361 * Appearently, we are called for every single TS cell.
363 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
365 if (new_ts) {
366 /* We are about to process a new TS cell. */
368 #ifdef ULE_DEBUG
369 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
370 memcpy( ule_where, ts, TS_SZ );
371 if (ule_dump) {
372 hexdump( ule_where, TS_SZ );
373 ule_dump = 0;
375 ule_where += TS_SZ;
376 #endif
378 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
379 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
380 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
381 priv->ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
383 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
384 if (priv->ule_skb) {
385 dev_kfree_skb( priv->ule_skb );
386 /* Prepare for next SNDU. */
387 dev->stats.rx_errors++;
388 dev->stats.rx_frame_errors++;
390 reset_ule(priv);
391 priv->need_pusi = 1;
393 /* Continue with next TS cell. */
394 ts += TS_SZ;
395 priv->ts_count++;
396 continue;
399 ts_remain = 184;
400 from_where = ts + 4;
402 /* Synchronize on PUSI, if required. */
403 if (priv->need_pusi) {
404 if (ts[1] & TS_PUSI) {
405 /* Find beginning of first ULE SNDU in current TS cell. */
406 /* Synchronize continuity counter. */
407 priv->tscc = ts[3] & 0x0F;
408 /* There is a pointer field here. */
409 if (ts[4] > ts_remain) {
410 printk(KERN_ERR "%lu: Invalid ULE packet "
411 "(pointer field %d)\n", priv->ts_count, ts[4]);
412 ts += TS_SZ;
413 priv->ts_count++;
414 continue;
416 /* Skip to destination of pointer field. */
417 from_where = &ts[5] + ts[4];
418 ts_remain -= 1 + ts[4];
419 skipped = 0;
420 } else {
421 skipped++;
422 ts += TS_SZ;
423 priv->ts_count++;
424 continue;
428 if (new_ts) {
429 /* Check continuity counter. */
430 if ((ts[3] & 0x0F) == priv->tscc)
431 priv->tscc = (priv->tscc + 1) & 0x0F;
432 else {
433 /* TS discontinuity handling: */
434 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
435 "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
436 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
437 if (priv->ule_skb) {
438 dev_kfree_skb( priv->ule_skb );
439 /* Prepare for next SNDU. */
440 // reset_ule(priv); moved to below.
441 dev->stats.rx_errors++;
442 dev->stats.rx_frame_errors++;
444 reset_ule(priv);
445 /* skip to next PUSI. */
446 priv->need_pusi = 1;
447 continue;
449 /* If we still have an incomplete payload, but PUSI is
450 * set; some TS cells are missing.
451 * This is only possible here, if we missed exactly 16 TS
452 * cells (continuity counter wrap). */
453 if (ts[1] & TS_PUSI) {
454 if (! priv->need_pusi) {
455 if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
456 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
457 printk(KERN_WARNING "%lu: Invalid pointer "
458 "field: %u.\n", priv->ts_count, *from_where);
460 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
461 if (priv->ule_skb) {
462 dev_kfree_skb( priv->ule_skb );
463 dev->stats.rx_errors++;
464 dev->stats.rx_frame_errors++;
466 reset_ule(priv);
467 priv->need_pusi = 1;
468 continue;
470 /* Skip pointer field (we're processing a
471 * packed payload). */
472 from_where += 1;
473 ts_remain -= 1;
474 } else
475 priv->need_pusi = 0;
477 if (priv->ule_sndu_remain > 183) {
478 /* Current SNDU lacks more data than there could be available in the
479 * current TS cell. */
480 dev->stats.rx_errors++;
481 dev->stats.rx_length_errors++;
482 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
483 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
484 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
485 dev_kfree_skb(priv->ule_skb);
486 /* Prepare for next SNDU. */
487 reset_ule(priv);
488 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
489 from_where += ts[4];
490 ts_remain -= ts[4];
495 /* Check if new payload needs to be started. */
496 if (priv->ule_skb == NULL) {
497 /* Start a new payload with skb.
498 * Find ULE header. It is only guaranteed that the
499 * length field (2 bytes) is contained in the current
500 * TS.
501 * Check ts_remain has to be >= 2 here. */
502 if (ts_remain < 2) {
503 printk(KERN_WARNING "Invalid payload packing: only %d "
504 "bytes left in TS. Resyncing.\n", ts_remain);
505 priv->ule_sndu_len = 0;
506 priv->need_pusi = 1;
507 ts += TS_SZ;
508 continue;
511 if (! priv->ule_sndu_len) {
512 /* Got at least two bytes, thus extrace the SNDU length. */
513 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
514 if (priv->ule_sndu_len & 0x8000) {
515 /* D-Bit is set: no dest mac present. */
516 priv->ule_sndu_len &= 0x7FFF;
517 priv->ule_dbit = 1;
518 } else
519 priv->ule_dbit = 0;
521 if (priv->ule_sndu_len < 5) {
522 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
523 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
524 dev->stats.rx_errors++;
525 dev->stats.rx_length_errors++;
526 priv->ule_sndu_len = 0;
527 priv->need_pusi = 1;
528 new_ts = 1;
529 ts += TS_SZ;
530 priv->ts_count++;
531 continue;
533 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
534 from_where += 2;
538 * State of current TS:
539 * ts_remain (remaining bytes in the current TS cell)
540 * 0 ule_type is not available now, we need the next TS cell
541 * 1 the first byte of the ule_type is present
542 * >=2 full ULE header present, maybe some payload data as well.
544 switch (ts_remain) {
545 case 1:
546 priv->ule_sndu_type = from_where[0] << 8;
547 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
548 ts_remain -= 1; from_where += 1;
549 /* Continue w/ next TS. */
550 case 0:
551 new_ts = 1;
552 ts += TS_SZ;
553 priv->ts_count++;
554 continue;
556 default: /* complete ULE header is present in current TS. */
557 /* Extract ULE type field. */
558 if (priv->ule_sndu_type_1) {
559 priv->ule_sndu_type |= from_where[0];
560 from_where += 1; /* points to payload start. */
561 ts_remain -= 1;
562 } else {
563 /* Complete type is present in new TS. */
564 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
565 from_where += 2; /* points to payload start. */
566 ts_remain -= 2;
568 break;
571 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
572 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
573 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
574 if (priv->ule_skb == NULL) {
575 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
576 dev->name);
577 dev->stats.rx_dropped++;
578 return;
581 /* This includes the CRC32 _and_ dest mac, if !dbit. */
582 priv->ule_sndu_remain = priv->ule_sndu_len;
583 priv->ule_skb->dev = dev;
584 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
585 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
588 /* Copy data into our current skb. */
589 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
590 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
591 priv->ule_sndu_remain -= how_much;
592 ts_remain -= how_much;
593 from_where += how_much;
595 /* Check for complete payload. */
596 if (priv->ule_sndu_remain <= 0) {
597 /* Check CRC32, we've got it in our skb already. */
598 __be16 ulen = htons(priv->ule_sndu_len);
599 __be16 utype = htons(priv->ule_sndu_type);
600 const u8 *tail;
601 struct kvec iov[3] = {
602 { &ulen, sizeof ulen },
603 { &utype, sizeof utype },
604 { priv->ule_skb->data, priv->ule_skb->len - 4 }
606 u32 ule_crc = ~0L, expected_crc;
607 if (priv->ule_dbit) {
608 /* Set D-bit for CRC32 verification,
609 * if it was set originally. */
610 ulen |= htons(0x8000);
613 ule_crc = iov_crc32(ule_crc, iov, 3);
614 tail = skb_tail_pointer(priv->ule_skb);
615 expected_crc = *(tail - 4) << 24 |
616 *(tail - 3) << 16 |
617 *(tail - 2) << 8 |
618 *(tail - 1);
619 if (ule_crc != expected_crc) {
620 printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
621 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);
623 #ifdef ULE_DEBUG
624 hexdump( iov[0].iov_base, iov[0].iov_len );
625 hexdump( iov[1].iov_base, iov[1].iov_len );
626 hexdump( iov[2].iov_base, iov[2].iov_len );
628 if (ule_where == ule_hist) {
629 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
630 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
631 } else if (ule_where == &ule_hist[TS_SZ]) {
632 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
633 hexdump( ule_hist, TS_SZ );
634 } else {
635 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
636 hexdump( ule_where - TS_SZ, TS_SZ );
638 ule_dump = 1;
639 #endif
641 dev->stats.rx_errors++;
642 dev->stats.rx_crc_errors++;
643 dev_kfree_skb(priv->ule_skb);
644 } else {
645 /* CRC32 verified OK. */
646 u8 dest_addr[ETH_ALEN];
647 static const u8 bc_addr[ETH_ALEN] =
648 { [ 0 ... ETH_ALEN-1] = 0xff };
650 /* CRC32 was OK. Remove it from skb. */
651 priv->ule_skb->tail -= 4;
652 priv->ule_skb->len -= 4;
654 if (!priv->ule_dbit) {
656 * The destination MAC address is the
657 * next data in the skb. It comes
658 * before any extension headers.
660 * Check if the payload of this SNDU
661 * should be passed up the stack.
663 register int drop = 0;
664 if (priv->rx_mode != RX_MODE_PROMISC) {
665 if (priv->ule_skb->data[0] & 0x01) {
666 /* multicast or broadcast */
667 if (memcmp(priv->ule_skb->data, bc_addr, ETH_ALEN)) {
668 /* multicast */
669 if (priv->rx_mode == RX_MODE_MULTI) {
670 int i;
671 for(i = 0; i < priv->multi_num && memcmp(priv->ule_skb->data, priv->multi_macs[i], ETH_ALEN); i++)
673 if (i == priv->multi_num)
674 drop = 1;
675 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
676 drop = 1; /* no broadcast; */
677 /* else: all multicast mode: accept all multicast packets */
679 /* else: broadcast */
681 else if (memcmp(priv->ule_skb->data, dev->dev_addr, ETH_ALEN))
682 drop = 1;
683 /* else: destination address matches the MAC address of our receiver device */
685 /* else: promiscuous mode; pass everything up the stack */
687 if (drop) {
688 #ifdef ULE_DEBUG
689 dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
690 MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
691 #endif
692 dev_kfree_skb(priv->ule_skb);
693 goto sndu_done;
695 else
697 skb_copy_from_linear_data(priv->ule_skb,
698 dest_addr,
699 ETH_ALEN);
700 skb_pull(priv->ule_skb, ETH_ALEN);
704 /* Handle ULE Extension Headers. */
705 if (priv->ule_sndu_type < 1536) {
706 /* There is an extension header. Handle it accordingly. */
707 int l = handle_ule_extensions(priv);
708 if (l < 0) {
709 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
710 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
711 dev_kfree_skb(priv->ule_skb);
712 goto sndu_done;
714 skb_pull(priv->ule_skb, l);
718 * Construct/assure correct ethernet header.
719 * Note: in bridged mode (priv->ule_bridged !=
720 * 0) we already have the (original) ethernet
721 * header at the start of the payload (after
722 * optional dest. address and any extension
723 * headers).
726 if (!priv->ule_bridged) {
727 skb_push(priv->ule_skb, ETH_HLEN);
728 ethh = (struct ethhdr *)priv->ule_skb->data;
729 if (!priv->ule_dbit) {
730 /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
731 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
732 memset(ethh->h_source, 0, ETH_ALEN);
734 else /* zeroize source and dest */
735 memset( ethh, 0, ETH_ALEN*2 );
737 ethh->h_proto = htons(priv->ule_sndu_type);
739 /* else: skb is in correct state; nothing to do. */
740 priv->ule_bridged = 0;
742 /* Stuff into kernel's protocol stack. */
743 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
744 /* If D-bit is set (i.e. destination MAC address not present),
745 * receive the packet anyhow. */
746 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
747 priv->ule_skb->pkt_type = PACKET_HOST; */
748 dev->stats.rx_packets++;
749 dev->stats.rx_bytes += priv->ule_skb->len;
750 netif_rx(priv->ule_skb);
752 sndu_done:
753 /* Prepare for next SNDU. */
754 reset_ule(priv);
757 /* More data in current TS (look at the bytes following the CRC32)? */
758 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
759 /* Next ULE SNDU starts right there. */
760 new_ts = 0;
761 priv->ule_skb = NULL;
762 priv->ule_sndu_type_1 = 0;
763 priv->ule_sndu_len = 0;
764 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
765 // *(from_where + 0), *(from_where + 1),
766 // *(from_where + 2), *(from_where + 3));
767 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
768 // hexdump(ts, 188);
769 } else {
770 new_ts = 1;
771 ts += TS_SZ;
772 priv->ts_count++;
773 if (priv->ule_skb == NULL) {
774 priv->need_pusi = 1;
775 priv->ule_sndu_type_1 = 0;
776 priv->ule_sndu_len = 0;
779 } /* for all available TS cells */
782 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
783 const u8 *buffer2, size_t buffer2_len,
784 struct dmx_ts_feed *feed, enum dmx_success success)
786 struct net_device *dev = feed->priv;
788 if (buffer2)
789 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
790 if (buffer1_len > 32768)
791 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
792 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
793 buffer1_len, buffer1_len / TS_SZ, buffer1); */
794 dvb_net_ule(dev, buffer1, buffer1_len);
795 return 0;
799 static void dvb_net_sec(struct net_device *dev,
800 const u8 *pkt, int pkt_len)
802 u8 *eth;
803 struct sk_buff *skb;
804 struct net_device_stats *stats = &dev->stats;
805 int snap = 0;
807 /* note: pkt_len includes a 32bit checksum */
808 if (pkt_len < 16) {
809 printk("%s: IP/MPE packet length = %d too small.\n",
810 dev->name, pkt_len);
811 stats->rx_errors++;
812 stats->rx_length_errors++;
813 return;
815 /* it seems some ISPs manage to screw up here, so we have to
816 * relax the error checks... */
817 #if 0
818 if ((pkt[5] & 0xfd) != 0xc1) {
819 /* drop scrambled or broken packets */
820 #else
821 if ((pkt[5] & 0x3c) != 0x00) {
822 /* drop scrambled */
823 #endif
824 stats->rx_errors++;
825 stats->rx_crc_errors++;
826 return;
828 if (pkt[5] & 0x02) {
829 /* handle LLC/SNAP, see rfc-1042 */
830 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
831 stats->rx_dropped++;
832 return;
834 snap = 8;
836 if (pkt[7]) {
837 /* FIXME: assemble datagram from multiple sections */
838 stats->rx_errors++;
839 stats->rx_frame_errors++;
840 return;
843 /* we have 14 byte ethernet header (ip header follows);
844 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
846 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
847 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
848 stats->rx_dropped++;
849 return;
851 skb_reserve(skb, 2); /* longword align L3 header */
852 skb->dev = dev;
854 /* copy L3 payload */
855 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
856 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
858 /* create ethernet header: */
859 eth[0]=pkt[0x0b];
860 eth[1]=pkt[0x0a];
861 eth[2]=pkt[0x09];
862 eth[3]=pkt[0x08];
863 eth[4]=pkt[0x04];
864 eth[5]=pkt[0x03];
866 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
868 if (snap) {
869 eth[12] = pkt[18];
870 eth[13] = pkt[19];
871 } else {
872 /* protocol numbers are from rfc-1700 or
873 * http://www.iana.org/assignments/ethernet-numbers
875 if (pkt[12] >> 4 == 6) { /* version field from IP header */
876 eth[12] = 0x86; /* IPv6 */
877 eth[13] = 0xdd;
878 } else {
879 eth[12] = 0x08; /* IPv4 */
880 eth[13] = 0x00;
884 skb->protocol = dvb_net_eth_type_trans(skb, dev);
886 stats->rx_packets++;
887 stats->rx_bytes+=skb->len;
888 netif_rx(skb);
891 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
892 const u8 *buffer2, size_t buffer2_len,
893 struct dmx_section_filter *filter,
894 enum dmx_success success)
896 struct net_device *dev = filter->priv;
899 * we rely on the DVB API definition where exactly one complete
900 * section is delivered in buffer1
902 dvb_net_sec (dev, buffer1, buffer1_len);
903 return 0;
906 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
908 dev_kfree_skb(skb);
909 return NETDEV_TX_OK;
912 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
913 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
914 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
915 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
917 static int dvb_net_filter_sec_set(struct net_device *dev,
918 struct dmx_section_filter **secfilter,
919 u8 *mac, u8 *mac_mask)
921 struct dvb_net_priv *priv = netdev_priv(dev);
922 int ret;
924 *secfilter=NULL;
925 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
926 if (ret<0) {
927 printk("%s: could not get filter\n", dev->name);
928 return ret;
931 (*secfilter)->priv=(void *) dev;
933 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
934 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
935 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
937 (*secfilter)->filter_value[0]=0x3e;
938 (*secfilter)->filter_value[3]=mac[5];
939 (*secfilter)->filter_value[4]=mac[4];
940 (*secfilter)->filter_value[8]=mac[3];
941 (*secfilter)->filter_value[9]=mac[2];
942 (*secfilter)->filter_value[10]=mac[1];
943 (*secfilter)->filter_value[11]=mac[0];
945 (*secfilter)->filter_mask[0] = 0xff;
946 (*secfilter)->filter_mask[3] = mac_mask[5];
947 (*secfilter)->filter_mask[4] = mac_mask[4];
948 (*secfilter)->filter_mask[8] = mac_mask[3];
949 (*secfilter)->filter_mask[9] = mac_mask[2];
950 (*secfilter)->filter_mask[10] = mac_mask[1];
951 (*secfilter)->filter_mask[11]=mac_mask[0];
953 dprintk("%s: filter mac=%pM\n", dev->name, mac);
954 dprintk("%s: filter mask=%pM\n", dev->name, mac_mask);
956 return 0;
959 static int dvb_net_feed_start(struct net_device *dev)
961 int ret = 0, i;
962 struct dvb_net_priv *priv = netdev_priv(dev);
963 struct dmx_demux *demux = priv->demux;
964 unsigned char *mac = (unsigned char *) dev->dev_addr;
966 dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
967 mutex_lock(&priv->mutex);
968 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
969 printk("%s: BUG %d\n", __func__, __LINE__);
971 priv->secfeed=NULL;
972 priv->secfilter=NULL;
973 priv->tsfeed = NULL;
975 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
976 dprintk("%s: alloc secfeed\n", __func__);
977 ret=demux->allocate_section_feed(demux, &priv->secfeed,
978 dvb_net_sec_callback);
979 if (ret<0) {
980 printk("%s: could not allocate section feed\n", dev->name);
981 goto error;
984 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
986 if (ret<0) {
987 printk("%s: could not set section feed\n", dev->name);
988 priv->demux->release_section_feed(priv->demux, priv->secfeed);
989 priv->secfeed=NULL;
990 goto error;
993 if (priv->rx_mode != RX_MODE_PROMISC) {
994 dprintk("%s: set secfilter\n", __func__);
995 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
998 switch (priv->rx_mode) {
999 case RX_MODE_MULTI:
1000 for (i = 0; i < priv->multi_num; i++) {
1001 dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
1002 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1003 priv->multi_macs[i], mask_normal);
1005 break;
1006 case RX_MODE_ALL_MULTI:
1007 priv->multi_num=1;
1008 dprintk("%s: set multi_secfilter[0]\n", __func__);
1009 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1010 mac_allmulti, mask_allmulti);
1011 break;
1012 case RX_MODE_PROMISC:
1013 priv->multi_num=0;
1014 dprintk("%s: set secfilter\n", __func__);
1015 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1016 break;
1019 dprintk("%s: start filtering\n", __func__);
1020 priv->secfeed->start_filtering(priv->secfeed);
1021 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1022 struct timespec timeout = { 0, 10000000 }; // 10 msec
1024 /* we have payloads encapsulated in TS */
1025 dprintk("%s: alloc tsfeed\n", __func__);
1026 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1027 if (ret < 0) {
1028 printk("%s: could not allocate ts feed\n", dev->name);
1029 goto error;
1032 /* Set netdevice pointer for ts decaps callback. */
1033 priv->tsfeed->priv = (void *)dev;
1034 ret = priv->tsfeed->set(priv->tsfeed,
1035 priv->pid, /* pid */
1036 TS_PACKET, /* type */
1037 DMX_TS_PES_OTHER, /* pes type */
1038 32768, /* circular buffer size */
1039 timeout /* timeout */
1042 if (ret < 0) {
1043 printk("%s: could not set ts feed\n", dev->name);
1044 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1045 priv->tsfeed = NULL;
1046 goto error;
1049 dprintk("%s: start filtering\n", __func__);
1050 priv->tsfeed->start_filtering(priv->tsfeed);
1051 } else
1052 ret = -EINVAL;
1054 error:
1055 mutex_unlock(&priv->mutex);
1056 return ret;
1059 static int dvb_net_feed_stop(struct net_device *dev)
1061 struct dvb_net_priv *priv = netdev_priv(dev);
1062 int i, ret = 0;
1064 dprintk("%s\n", __func__);
1065 mutex_lock(&priv->mutex);
1066 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1067 if (priv->secfeed) {
1068 if (priv->secfeed->is_filtering) {
1069 dprintk("%s: stop secfeed\n", __func__);
1070 priv->secfeed->stop_filtering(priv->secfeed);
1073 if (priv->secfilter) {
1074 dprintk("%s: release secfilter\n", __func__);
1075 priv->secfeed->release_filter(priv->secfeed,
1076 priv->secfilter);
1077 priv->secfilter=NULL;
1080 for (i=0; i<priv->multi_num; i++) {
1081 if (priv->multi_secfilter[i]) {
1082 dprintk("%s: release multi_filter[%d]\n",
1083 __func__, i);
1084 priv->secfeed->release_filter(priv->secfeed,
1085 priv->multi_secfilter[i]);
1086 priv->multi_secfilter[i] = NULL;
1090 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1091 priv->secfeed = NULL;
1092 } else
1093 printk("%s: no feed to stop\n", dev->name);
1094 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1095 if (priv->tsfeed) {
1096 if (priv->tsfeed->is_filtering) {
1097 dprintk("%s: stop tsfeed\n", __func__);
1098 priv->tsfeed->stop_filtering(priv->tsfeed);
1100 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1101 priv->tsfeed = NULL;
1103 else
1104 printk("%s: no ts feed to stop\n", dev->name);
1105 } else
1106 ret = -EINVAL;
1107 mutex_unlock(&priv->mutex);
1108 return ret;
1112 static int dvb_set_mc_filter (struct net_device *dev, struct dev_mc_list *mc)
1114 struct dvb_net_priv *priv = netdev_priv(dev);
1116 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1117 return -ENOMEM;
1119 memcpy(priv->multi_macs[priv->multi_num], mc->dmi_addr, 6);
1121 priv->multi_num++;
1122 return 0;
1126 static void wq_set_multicast_list (struct work_struct *work)
1128 struct dvb_net_priv *priv =
1129 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1130 struct net_device *dev = priv->net;
1132 dvb_net_feed_stop(dev);
1133 priv->rx_mode = RX_MODE_UNI;
1134 netif_addr_lock_bh(dev);
1136 if (dev->flags & IFF_PROMISC) {
1137 dprintk("%s: promiscuous mode\n", dev->name);
1138 priv->rx_mode = RX_MODE_PROMISC;
1139 } else if ((dev->flags & IFF_ALLMULTI)) {
1140 dprintk("%s: allmulti mode\n", dev->name);
1141 priv->rx_mode = RX_MODE_ALL_MULTI;
1142 } else if (!netdev_mc_empty(dev)) {
1143 int mci;
1144 struct dev_mc_list *mc;
1146 dprintk("%s: set_mc_list, %d entries\n",
1147 dev->name, netdev_mc_count(dev));
1149 priv->rx_mode = RX_MODE_MULTI;
1150 priv->multi_num = 0;
1152 for (mci = 0, mc=dev->mc_list;
1153 mci < netdev_mc_count(dev);
1154 mc = mc->next, mci++) {
1155 dvb_set_mc_filter(dev, mc);
1159 netif_addr_unlock_bh(dev);
1160 dvb_net_feed_start(dev);
1164 static void dvb_net_set_multicast_list (struct net_device *dev)
1166 struct dvb_net_priv *priv = netdev_priv(dev);
1167 schedule_work(&priv->set_multicast_list_wq);
1171 static void wq_restart_net_feed (struct work_struct *work)
1173 struct dvb_net_priv *priv =
1174 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1175 struct net_device *dev = priv->net;
1177 if (netif_running(dev)) {
1178 dvb_net_feed_stop(dev);
1179 dvb_net_feed_start(dev);
1184 static int dvb_net_set_mac (struct net_device *dev, void *p)
1186 struct dvb_net_priv *priv = netdev_priv(dev);
1187 struct sockaddr *addr=p;
1189 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1191 if (netif_running(dev))
1192 schedule_work(&priv->restart_net_feed_wq);
1194 return 0;
1198 static int dvb_net_open(struct net_device *dev)
1200 struct dvb_net_priv *priv = netdev_priv(dev);
1202 priv->in_use++;
1203 dvb_net_feed_start(dev);
1204 return 0;
1208 static int dvb_net_stop(struct net_device *dev)
1210 struct dvb_net_priv *priv = netdev_priv(dev);
1212 priv->in_use--;
1213 return dvb_net_feed_stop(dev);
1216 static const struct header_ops dvb_header_ops = {
1217 .create = eth_header,
1218 .parse = eth_header_parse,
1219 .rebuild = eth_rebuild_header,
1223 static const struct net_device_ops dvb_netdev_ops = {
1224 .ndo_open = dvb_net_open,
1225 .ndo_stop = dvb_net_stop,
1226 .ndo_start_xmit = dvb_net_tx,
1227 .ndo_set_multicast_list = dvb_net_set_multicast_list,
1228 .ndo_set_mac_address = dvb_net_set_mac,
1229 .ndo_change_mtu = eth_change_mtu,
1230 .ndo_validate_addr = eth_validate_addr,
1233 static void dvb_net_setup(struct net_device *dev)
1235 ether_setup(dev);
1237 dev->header_ops = &dvb_header_ops;
1238 dev->netdev_ops = &dvb_netdev_ops;
1239 dev->mtu = 4096;
1241 dev->flags |= IFF_NOARP;
1244 static int get_if(struct dvb_net *dvbnet)
1246 int i;
1248 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1249 if (!dvbnet->state[i])
1250 break;
1252 if (i == DVB_NET_DEVICES_MAX)
1253 return -1;
1255 dvbnet->state[i]=1;
1256 return i;
1259 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1261 struct net_device *net;
1262 struct dvb_net_priv *priv;
1263 int result;
1264 int if_num;
1266 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1267 return -EINVAL;
1268 if ((if_num = get_if(dvbnet)) < 0)
1269 return -EINVAL;
1271 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", dvb_net_setup);
1272 if (!net)
1273 return -ENOMEM;
1275 if (dvbnet->dvbdev->id)
1276 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1277 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1278 else
1279 /* compatibility fix to keep dvb0_0 format */
1280 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1281 dvbnet->dvbdev->adapter->num, if_num);
1283 net->addr_len = 6;
1284 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1286 dvbnet->device[if_num] = net;
1288 priv = netdev_priv(net);
1289 priv->net = net;
1290 priv->demux = dvbnet->demux;
1291 priv->pid = pid;
1292 priv->rx_mode = RX_MODE_UNI;
1293 priv->need_pusi = 1;
1294 priv->tscc = 0;
1295 priv->feedtype = feedtype;
1296 reset_ule(priv);
1298 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1299 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1300 mutex_init(&priv->mutex);
1302 net->base_addr = pid;
1304 if ((result = register_netdev(net)) < 0) {
1305 dvbnet->device[if_num] = NULL;
1306 free_netdev(net);
1307 return result;
1309 printk("dvb_net: created network interface %s\n", net->name);
1311 return if_num;
1314 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1316 struct net_device *net = dvbnet->device[num];
1317 struct dvb_net_priv *priv;
1319 if (!dvbnet->state[num])
1320 return -EINVAL;
1321 priv = netdev_priv(net);
1322 if (priv->in_use)
1323 return -EBUSY;
1325 dvb_net_stop(net);
1326 flush_scheduled_work();
1327 printk("dvb_net: removed network interface %s\n", net->name);
1328 unregister_netdev(net);
1329 dvbnet->state[num]=0;
1330 dvbnet->device[num] = NULL;
1331 free_netdev(net);
1333 return 0;
1336 static int dvb_net_do_ioctl(struct inode *inode, struct file *file,
1337 unsigned int cmd, void *parg)
1339 struct dvb_device *dvbdev = file->private_data;
1340 struct dvb_net *dvbnet = dvbdev->priv;
1342 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1343 return -EPERM;
1345 switch (cmd) {
1346 case NET_ADD_IF:
1348 struct dvb_net_if *dvbnetif = parg;
1349 int result;
1351 if (!capable(CAP_SYS_ADMIN))
1352 return -EPERM;
1354 if (!try_module_get(dvbdev->adapter->module))
1355 return -EPERM;
1357 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1358 if (result<0) {
1359 module_put(dvbdev->adapter->module);
1360 return result;
1362 dvbnetif->if_num=result;
1363 break;
1365 case NET_GET_IF:
1367 struct net_device *netdev;
1368 struct dvb_net_priv *priv_data;
1369 struct dvb_net_if *dvbnetif = parg;
1371 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1372 !dvbnet->state[dvbnetif->if_num])
1373 return -EINVAL;
1375 netdev = dvbnet->device[dvbnetif->if_num];
1377 priv_data = netdev_priv(netdev);
1378 dvbnetif->pid=priv_data->pid;
1379 dvbnetif->feedtype=priv_data->feedtype;
1380 break;
1382 case NET_REMOVE_IF:
1384 int ret;
1386 if (!capable(CAP_SYS_ADMIN))
1387 return -EPERM;
1388 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX)
1389 return -EINVAL;
1390 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1391 if (!ret)
1392 module_put(dvbdev->adapter->module);
1393 return ret;
1396 /* binary compatibility cruft */
1397 case __NET_ADD_IF_OLD:
1399 struct __dvb_net_if_old *dvbnetif = parg;
1400 int result;
1402 if (!capable(CAP_SYS_ADMIN))
1403 return -EPERM;
1405 if (!try_module_get(dvbdev->adapter->module))
1406 return -EPERM;
1408 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1409 if (result<0) {
1410 module_put(dvbdev->adapter->module);
1411 return result;
1413 dvbnetif->if_num=result;
1414 break;
1416 case __NET_GET_IF_OLD:
1418 struct net_device *netdev;
1419 struct dvb_net_priv *priv_data;
1420 struct __dvb_net_if_old *dvbnetif = parg;
1422 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1423 !dvbnet->state[dvbnetif->if_num])
1424 return -EINVAL;
1426 netdev = dvbnet->device[dvbnetif->if_num];
1428 priv_data = netdev_priv(netdev);
1429 dvbnetif->pid=priv_data->pid;
1430 break;
1432 default:
1433 return -ENOTTY;
1435 return 0;
1438 static int dvb_net_ioctl(struct inode *inode, struct file *file,
1439 unsigned int cmd, unsigned long arg)
1441 return dvb_usercopy(inode, file, cmd, arg, dvb_net_do_ioctl);
1444 static int dvb_net_close(struct inode *inode, struct file *file)
1446 struct dvb_device *dvbdev = file->private_data;
1447 struct dvb_net *dvbnet = dvbdev->priv;
1449 dvb_generic_release(inode, file);
1451 if(dvbdev->users == 1 && dvbnet->exit == 1) {
1452 fops_put(file->f_op);
1453 file->f_op = NULL;
1454 wake_up(&dvbdev->wait_queue);
1456 return 0;
1460 static const struct file_operations dvb_net_fops = {
1461 .owner = THIS_MODULE,
1462 .ioctl = dvb_net_ioctl,
1463 .open = dvb_generic_open,
1464 .release = dvb_net_close,
1467 static struct dvb_device dvbdev_net = {
1468 .priv = NULL,
1469 .users = 1,
1470 .writers = 1,
1471 .fops = &dvb_net_fops,
1475 void dvb_net_release (struct dvb_net *dvbnet)
1477 int i;
1479 dvbnet->exit = 1;
1480 if (dvbnet->dvbdev->users < 1)
1481 wait_event(dvbnet->dvbdev->wait_queue,
1482 dvbnet->dvbdev->users==1);
1484 dvb_unregister_device(dvbnet->dvbdev);
1486 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1487 if (!dvbnet->state[i])
1488 continue;
1489 dvb_net_remove_if(dvbnet, i);
1492 EXPORT_SYMBOL(dvb_net_release);
1495 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1496 struct dmx_demux *dmx)
1498 int i;
1500 dvbnet->demux = dmx;
1502 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1503 dvbnet->state[i] = 0;
1505 dvb_register_device (adap, &dvbnet->dvbdev, &dvbdev_net,
1506 dvbnet, DVB_DEVICE_NET);
1508 return 0;
1510 EXPORT_SYMBOL(dvb_net_init);