initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / media / dvb / dvb-core / dvb_net.c
blob55e591daf717f4fc26efecf98923aca309cad449
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;
353 bool error = false;
355 #ifdef ULE_DEBUG
356 /* The code inside ULE_DEBUG keeps a history of the last 100 TS cells processed. */
357 static unsigned char ule_hist[100*TS_SZ];
358 static unsigned char *ule_where = ule_hist, ule_dump;
359 #endif
361 /* For all TS cells in current buffer.
362 * Appearently, we are called for every single TS cell.
364 for (ts = buf, ts_end = buf + buf_len; ts < ts_end; /* no default incr. */ ) {
366 if (new_ts) {
367 /* We are about to process a new TS cell. */
369 #ifdef ULE_DEBUG
370 if (ule_where >= &ule_hist[100*TS_SZ]) ule_where = ule_hist;
371 memcpy( ule_where, ts, TS_SZ );
372 if (ule_dump) {
373 hexdump( ule_where, TS_SZ );
374 ule_dump = 0;
376 ule_where += TS_SZ;
377 #endif
379 /* Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control . */
380 if ((ts[0] != TS_SYNC) || (ts[1] & TS_TEI) || ((ts[3] & TS_SC) != 0)) {
381 printk(KERN_WARNING "%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n",
382 priv->ts_count, ts[0], ts[1] & TS_TEI >> 7, ts[3] & 0xC0 >> 6);
384 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
385 if (priv->ule_skb) {
386 dev_kfree_skb( priv->ule_skb );
387 /* Prepare for next SNDU. */
388 dev->stats.rx_errors++;
389 dev->stats.rx_frame_errors++;
391 reset_ule(priv);
392 priv->need_pusi = 1;
394 /* Continue with next TS cell. */
395 ts += TS_SZ;
396 priv->ts_count++;
397 continue;
400 ts_remain = 184;
401 from_where = ts + 4;
403 /* Synchronize on PUSI, if required. */
404 if (priv->need_pusi) {
405 if (ts[1] & TS_PUSI) {
406 /* Find beginning of first ULE SNDU in current TS cell. */
407 /* Synchronize continuity counter. */
408 priv->tscc = ts[3] & 0x0F;
409 /* There is a pointer field here. */
410 if (ts[4] > ts_remain) {
411 printk(KERN_ERR "%lu: Invalid ULE packet "
412 "(pointer field %d)\n", priv->ts_count, ts[4]);
413 ts += TS_SZ;
414 priv->ts_count++;
415 continue;
417 /* Skip to destination of pointer field. */
418 from_where = &ts[5] + ts[4];
419 ts_remain -= 1 + ts[4];
420 skipped = 0;
421 } else {
422 skipped++;
423 ts += TS_SZ;
424 priv->ts_count++;
425 continue;
429 if (new_ts) {
430 /* Check continuity counter. */
431 if ((ts[3] & 0x0F) == priv->tscc)
432 priv->tscc = (priv->tscc + 1) & 0x0F;
433 else {
434 /* TS discontinuity handling: */
435 printk(KERN_WARNING "%lu: TS discontinuity: got %#x, "
436 "expected %#x.\n", priv->ts_count, ts[3] & 0x0F, priv->tscc);
437 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
438 if (priv->ule_skb) {
439 dev_kfree_skb( priv->ule_skb );
440 /* Prepare for next SNDU. */
441 // reset_ule(priv); moved to below.
442 dev->stats.rx_errors++;
443 dev->stats.rx_frame_errors++;
445 reset_ule(priv);
446 /* skip to next PUSI. */
447 priv->need_pusi = 1;
448 continue;
450 /* If we still have an incomplete payload, but PUSI is
451 * set; some TS cells are missing.
452 * This is only possible here, if we missed exactly 16 TS
453 * cells (continuity counter wrap). */
454 if (ts[1] & TS_PUSI) {
455 if (! priv->need_pusi) {
456 if (!(*from_where < (ts_remain-1)) || *from_where != priv->ule_sndu_remain) {
457 /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */
458 printk(KERN_WARNING "%lu: Invalid pointer "
459 "field: %u.\n", priv->ts_count, *from_where);
461 /* Drop partly decoded SNDU, reset state, resync on PUSI. */
462 if (priv->ule_skb) {
463 error = true;
464 dev_kfree_skb(priv->ule_skb);
467 if (error || priv->ule_sndu_remain) {
468 dev->stats.rx_errors++;
469 dev->stats.rx_frame_errors++;
470 error = false;
473 reset_ule(priv);
474 priv->need_pusi = 1;
475 continue;
477 /* Skip pointer field (we're processing a
478 * packed payload). */
479 from_where += 1;
480 ts_remain -= 1;
481 } else
482 priv->need_pusi = 0;
484 if (priv->ule_sndu_remain > 183) {
485 /* Current SNDU lacks more data than there could be available in the
486 * current TS cell. */
487 dev->stats.rx_errors++;
488 dev->stats.rx_length_errors++;
489 printk(KERN_WARNING "%lu: Expected %d more SNDU bytes, but "
490 "got PUSI (pf %d, ts_remain %d). Flushing incomplete payload.\n",
491 priv->ts_count, priv->ule_sndu_remain, ts[4], ts_remain);
492 dev_kfree_skb(priv->ule_skb);
493 /* Prepare for next SNDU. */
494 reset_ule(priv);
495 /* Resync: go to where pointer field points to: start of next ULE SNDU. */
496 from_where += ts[4];
497 ts_remain -= ts[4];
502 /* Check if new payload needs to be started. */
503 if (priv->ule_skb == NULL) {
504 /* Start a new payload with skb.
505 * Find ULE header. It is only guaranteed that the
506 * length field (2 bytes) is contained in the current
507 * TS.
508 * Check ts_remain has to be >= 2 here. */
509 if (ts_remain < 2) {
510 printk(KERN_WARNING "Invalid payload packing: only %d "
511 "bytes left in TS. Resyncing.\n", ts_remain);
512 priv->ule_sndu_len = 0;
513 priv->need_pusi = 1;
514 ts += TS_SZ;
515 continue;
518 if (! priv->ule_sndu_len) {
519 /* Got at least two bytes, thus extrace the SNDU length. */
520 priv->ule_sndu_len = from_where[0] << 8 | from_where[1];
521 if (priv->ule_sndu_len & 0x8000) {
522 /* D-Bit is set: no dest mac present. */
523 priv->ule_sndu_len &= 0x7FFF;
524 priv->ule_dbit = 1;
525 } else
526 priv->ule_dbit = 0;
528 if (priv->ule_sndu_len < 5) {
529 printk(KERN_WARNING "%lu: Invalid ULE SNDU length %u. "
530 "Resyncing.\n", priv->ts_count, priv->ule_sndu_len);
531 dev->stats.rx_errors++;
532 dev->stats.rx_length_errors++;
533 priv->ule_sndu_len = 0;
534 priv->need_pusi = 1;
535 new_ts = 1;
536 ts += TS_SZ;
537 priv->ts_count++;
538 continue;
540 ts_remain -= 2; /* consume the 2 bytes SNDU length. */
541 from_where += 2;
544 priv->ule_sndu_remain = priv->ule_sndu_len + 2;
546 * State of current TS:
547 * ts_remain (remaining bytes in the current TS cell)
548 * 0 ule_type is not available now, we need the next TS cell
549 * 1 the first byte of the ule_type is present
550 * >=2 full ULE header present, maybe some payload data as well.
552 switch (ts_remain) {
553 case 1:
554 priv->ule_sndu_remain--;
555 priv->ule_sndu_type = from_where[0] << 8;
556 priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */
557 ts_remain -= 1; from_where += 1;
558 /* Continue w/ next TS. */
559 case 0:
560 new_ts = 1;
561 ts += TS_SZ;
562 priv->ts_count++;
563 continue;
565 default: /* complete ULE header is present in current TS. */
566 /* Extract ULE type field. */
567 if (priv->ule_sndu_type_1) {
568 priv->ule_sndu_type_1 = 0;
569 priv->ule_sndu_type |= from_where[0];
570 from_where += 1; /* points to payload start. */
571 ts_remain -= 1;
572 } else {
573 /* Complete type is present in new TS. */
574 priv->ule_sndu_type = from_where[0] << 8 | from_where[1];
575 from_where += 2; /* points to payload start. */
576 ts_remain -= 2;
578 break;
581 /* Allocate the skb (decoder target buffer) with the correct size, as follows:
582 * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */
583 priv->ule_skb = dev_alloc_skb( priv->ule_sndu_len + ETH_HLEN + ETH_ALEN );
584 if (priv->ule_skb == NULL) {
585 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n",
586 dev->name);
587 dev->stats.rx_dropped++;
588 return;
591 /* This includes the CRC32 _and_ dest mac, if !dbit. */
592 priv->ule_sndu_remain = priv->ule_sndu_len;
593 priv->ule_skb->dev = dev;
594 /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */
595 skb_reserve( priv->ule_skb, ETH_HLEN + ETH_ALEN );
598 /* Copy data into our current skb. */
599 how_much = min(priv->ule_sndu_remain, (int)ts_remain);
600 memcpy(skb_put(priv->ule_skb, how_much), from_where, how_much);
601 priv->ule_sndu_remain -= how_much;
602 ts_remain -= how_much;
603 from_where += how_much;
605 /* Check for complete payload. */
606 if (priv->ule_sndu_remain <= 0) {
607 /* Check CRC32, we've got it in our skb already. */
608 __be16 ulen = htons(priv->ule_sndu_len);
609 __be16 utype = htons(priv->ule_sndu_type);
610 const u8 *tail;
611 struct kvec iov[3] = {
612 { &ulen, sizeof ulen },
613 { &utype, sizeof utype },
614 { priv->ule_skb->data, priv->ule_skb->len - 4 }
616 u32 ule_crc = ~0L, expected_crc;
617 if (priv->ule_dbit) {
618 /* Set D-bit for CRC32 verification,
619 * if it was set originally. */
620 ulen |= htons(0x8000);
623 ule_crc = iov_crc32(ule_crc, iov, 3);
624 tail = skb_tail_pointer(priv->ule_skb);
625 expected_crc = *(tail - 4) << 24 |
626 *(tail - 3) << 16 |
627 *(tail - 2) << 8 |
628 *(tail - 1);
629 if (ule_crc != expected_crc) {
630 printk(KERN_WARNING "%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n",
631 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);
633 #ifdef ULE_DEBUG
634 hexdump( iov[0].iov_base, iov[0].iov_len );
635 hexdump( iov[1].iov_base, iov[1].iov_len );
636 hexdump( iov[2].iov_base, iov[2].iov_len );
638 if (ule_where == ule_hist) {
639 hexdump( &ule_hist[98*TS_SZ], TS_SZ );
640 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
641 } else if (ule_where == &ule_hist[TS_SZ]) {
642 hexdump( &ule_hist[99*TS_SZ], TS_SZ );
643 hexdump( ule_hist, TS_SZ );
644 } else {
645 hexdump( ule_where - TS_SZ - TS_SZ, TS_SZ );
646 hexdump( ule_where - TS_SZ, TS_SZ );
648 ule_dump = 1;
649 #endif
651 dev->stats.rx_errors++;
652 dev->stats.rx_crc_errors++;
653 dev_kfree_skb(priv->ule_skb);
654 } else {
655 /* CRC32 verified OK. */
656 u8 dest_addr[ETH_ALEN];
657 static const u8 bc_addr[ETH_ALEN] =
658 { [ 0 ... ETH_ALEN-1] = 0xff };
660 /* CRC32 was OK. Remove it from skb. */
661 priv->ule_skb->tail -= 4;
662 priv->ule_skb->len -= 4;
664 if (!priv->ule_dbit) {
666 * The destination MAC address is the
667 * next data in the skb. It comes
668 * before any extension headers.
670 * Check if the payload of this SNDU
671 * should be passed up the stack.
673 register int drop = 0;
674 if (priv->rx_mode != RX_MODE_PROMISC) {
675 if (priv->ule_skb->data[0] & 0x01) {
676 /* multicast or broadcast */
677 if (memcmp(priv->ule_skb->data, bc_addr, ETH_ALEN)) {
678 /* multicast */
679 if (priv->rx_mode == RX_MODE_MULTI) {
680 int i;
681 for(i = 0; i < priv->multi_num && memcmp(priv->ule_skb->data, priv->multi_macs[i], ETH_ALEN); i++)
683 if (i == priv->multi_num)
684 drop = 1;
685 } else if (priv->rx_mode != RX_MODE_ALL_MULTI)
686 drop = 1; /* no broadcast; */
687 /* else: all multicast mode: accept all multicast packets */
689 /* else: broadcast */
691 else if (memcmp(priv->ule_skb->data, dev->dev_addr, ETH_ALEN))
692 drop = 1;
693 /* else: destination address matches the MAC address of our receiver device */
695 /* else: promiscuous mode; pass everything up the stack */
697 if (drop) {
698 #ifdef ULE_DEBUG
699 dprintk("Dropping SNDU: MAC destination address does not match: dest addr: "MAC_ADDR_PRINTFMT", dev addr: "MAC_ADDR_PRINTFMT"\n",
700 MAX_ADDR_PRINTFMT_ARGS(priv->ule_skb->data), MAX_ADDR_PRINTFMT_ARGS(dev->dev_addr));
701 #endif
702 dev_kfree_skb(priv->ule_skb);
703 goto sndu_done;
705 else
707 skb_copy_from_linear_data(priv->ule_skb,
708 dest_addr,
709 ETH_ALEN);
710 skb_pull(priv->ule_skb, ETH_ALEN);
714 /* Handle ULE Extension Headers. */
715 if (priv->ule_sndu_type < 1536) {
716 /* There is an extension header. Handle it accordingly. */
717 int l = handle_ule_extensions(priv);
718 if (l < 0) {
719 /* Mandatory extension header unknown or TEST SNDU. Drop it. */
720 // printk( KERN_WARNING "Dropping SNDU, extension headers.\n" );
721 dev_kfree_skb(priv->ule_skb);
722 goto sndu_done;
724 skb_pull(priv->ule_skb, l);
728 * Construct/assure correct ethernet header.
729 * Note: in bridged mode (priv->ule_bridged !=
730 * 0) we already have the (original) ethernet
731 * header at the start of the payload (after
732 * optional dest. address and any extension
733 * headers).
736 if (!priv->ule_bridged) {
737 skb_push(priv->ule_skb, ETH_HLEN);
738 ethh = (struct ethhdr *)priv->ule_skb->data;
739 if (!priv->ule_dbit) {
740 /* dest_addr buffer is only valid if priv->ule_dbit == 0 */
741 memcpy(ethh->h_dest, dest_addr, ETH_ALEN);
742 memset(ethh->h_source, 0, ETH_ALEN);
744 else /* zeroize source and dest */
745 memset( ethh, 0, ETH_ALEN*2 );
747 ethh->h_proto = htons(priv->ule_sndu_type);
749 /* else: skb is in correct state; nothing to do. */
750 priv->ule_bridged = 0;
752 /* Stuff into kernel's protocol stack. */
753 priv->ule_skb->protocol = dvb_net_eth_type_trans(priv->ule_skb, dev);
754 /* If D-bit is set (i.e. destination MAC address not present),
755 * receive the packet anyhow. */
756 /* if (priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST)
757 priv->ule_skb->pkt_type = PACKET_HOST; */
758 dev->stats.rx_packets++;
759 dev->stats.rx_bytes += priv->ule_skb->len;
760 netif_rx(priv->ule_skb);
762 sndu_done:
763 /* Prepare for next SNDU. */
764 reset_ule(priv);
767 /* More data in current TS (look at the bytes following the CRC32)? */
768 if (ts_remain >= 2 && *((unsigned short *)from_where) != 0xFFFF) {
769 /* Next ULE SNDU starts right there. */
770 new_ts = 0;
771 priv->ule_skb = NULL;
772 priv->ule_sndu_type_1 = 0;
773 priv->ule_sndu_len = 0;
774 // printk(KERN_WARNING "More data in current TS: [%#x %#x %#x %#x]\n",
775 // *(from_where + 0), *(from_where + 1),
776 // *(from_where + 2), *(from_where + 3));
777 // printk(KERN_WARNING "ts @ %p, stopped @ %p:\n", ts, from_where + 0);
778 // hexdump(ts, 188);
779 } else {
780 new_ts = 1;
781 ts += TS_SZ;
782 priv->ts_count++;
783 if (priv->ule_skb == NULL) {
784 priv->need_pusi = 1;
785 priv->ule_sndu_type_1 = 0;
786 priv->ule_sndu_len = 0;
789 } /* for all available TS cells */
792 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len,
793 const u8 *buffer2, size_t buffer2_len,
794 struct dmx_ts_feed *feed, enum dmx_success success)
796 struct net_device *dev = feed->priv;
798 if (buffer2)
799 printk(KERN_WARNING "buffer2 not NULL: %p.\n", buffer2);
800 if (buffer1_len > 32768)
801 printk(KERN_WARNING "length > 32k: %zu.\n", buffer1_len);
802 /* printk("TS callback: %u bytes, %u TS cells @ %p.\n",
803 buffer1_len, buffer1_len / TS_SZ, buffer1); */
804 dvb_net_ule(dev, buffer1, buffer1_len);
805 return 0;
809 static void dvb_net_sec(struct net_device *dev,
810 const u8 *pkt, int pkt_len)
812 u8 *eth;
813 struct sk_buff *skb;
814 struct net_device_stats *stats = &dev->stats;
815 int snap = 0;
817 /* note: pkt_len includes a 32bit checksum */
818 if (pkt_len < 16) {
819 printk("%s: IP/MPE packet length = %d too small.\n",
820 dev->name, pkt_len);
821 stats->rx_errors++;
822 stats->rx_length_errors++;
823 return;
825 /* it seems some ISPs manage to screw up here, so we have to
826 * relax the error checks... */
827 #if 0
828 if ((pkt[5] & 0xfd) != 0xc1) {
829 /* drop scrambled or broken packets */
830 #else
831 if ((pkt[5] & 0x3c) != 0x00) {
832 /* drop scrambled */
833 #endif
834 stats->rx_errors++;
835 stats->rx_crc_errors++;
836 return;
838 if (pkt[5] & 0x02) {
839 /* handle LLC/SNAP, see rfc-1042 */
840 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) {
841 stats->rx_dropped++;
842 return;
844 snap = 8;
846 if (pkt[7]) {
847 /* FIXME: assemble datagram from multiple sections */
848 stats->rx_errors++;
849 stats->rx_frame_errors++;
850 return;
853 /* we have 14 byte ethernet header (ip header follows);
854 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP
856 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) {
857 //printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
858 stats->rx_dropped++;
859 return;
861 skb_reserve(skb, 2); /* longword align L3 header */
862 skb->dev = dev;
864 /* copy L3 payload */
865 eth = (u8 *) skb_put(skb, pkt_len - 12 - 4 + 14 - snap);
866 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap);
868 /* create ethernet header: */
869 eth[0]=pkt[0x0b];
870 eth[1]=pkt[0x0a];
871 eth[2]=pkt[0x09];
872 eth[3]=pkt[0x08];
873 eth[4]=pkt[0x04];
874 eth[5]=pkt[0x03];
876 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0;
878 if (snap) {
879 eth[12] = pkt[18];
880 eth[13] = pkt[19];
881 } else {
882 /* protocol numbers are from rfc-1700 or
883 * http://www.iana.org/assignments/ethernet-numbers
885 if (pkt[12] >> 4 == 6) { /* version field from IP header */
886 eth[12] = 0x86; /* IPv6 */
887 eth[13] = 0xdd;
888 } else {
889 eth[12] = 0x08; /* IPv4 */
890 eth[13] = 0x00;
894 skb->protocol = dvb_net_eth_type_trans(skb, dev);
896 stats->rx_packets++;
897 stats->rx_bytes+=skb->len;
898 netif_rx(skb);
901 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len,
902 const u8 *buffer2, size_t buffer2_len,
903 struct dmx_section_filter *filter,
904 enum dmx_success success)
906 struct net_device *dev = filter->priv;
909 * we rely on the DVB API definition where exactly one complete
910 * section is delivered in buffer1
912 dvb_net_sec (dev, buffer1, buffer1_len);
913 return 0;
916 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev)
918 dev_kfree_skb(skb);
919 return NETDEV_TX_OK;
922 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
923 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
924 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
925 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
927 static int dvb_net_filter_sec_set(struct net_device *dev,
928 struct dmx_section_filter **secfilter,
929 u8 *mac, u8 *mac_mask)
931 struct dvb_net_priv *priv = netdev_priv(dev);
932 int ret;
934 *secfilter=NULL;
935 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter);
936 if (ret<0) {
937 printk("%s: could not get filter\n", dev->name);
938 return ret;
941 (*secfilter)->priv=(void *) dev;
943 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE);
944 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE);
945 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE);
947 (*secfilter)->filter_value[0]=0x3e;
948 (*secfilter)->filter_value[3]=mac[5];
949 (*secfilter)->filter_value[4]=mac[4];
950 (*secfilter)->filter_value[8]=mac[3];
951 (*secfilter)->filter_value[9]=mac[2];
952 (*secfilter)->filter_value[10]=mac[1];
953 (*secfilter)->filter_value[11]=mac[0];
955 (*secfilter)->filter_mask[0] = 0xff;
956 (*secfilter)->filter_mask[3] = mac_mask[5];
957 (*secfilter)->filter_mask[4] = mac_mask[4];
958 (*secfilter)->filter_mask[8] = mac_mask[3];
959 (*secfilter)->filter_mask[9] = mac_mask[2];
960 (*secfilter)->filter_mask[10] = mac_mask[1];
961 (*secfilter)->filter_mask[11]=mac_mask[0];
963 dprintk("%s: filter mac=%02x %02x %02x %02x %02x %02x\n",
964 dev->name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
965 dprintk("%s: filter mask=%02x %02x %02x %02x %02x %02x\n",
966 dev->name, mac_mask[0], mac_mask[1], mac_mask[2],
967 mac_mask[3], mac_mask[4], mac_mask[5]);
969 return 0;
972 static int dvb_net_feed_start(struct net_device *dev)
974 int ret = 0, i;
975 struct dvb_net_priv *priv = netdev_priv(dev);
976 struct dmx_demux *demux = priv->demux;
977 unsigned char *mac = (unsigned char *) dev->dev_addr;
979 dprintk("%s: rx_mode %i\n", __func__, priv->rx_mode);
980 mutex_lock(&priv->mutex);
981 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0])
982 printk("%s: BUG %d\n", __func__, __LINE__);
984 priv->secfeed=NULL;
985 priv->secfilter=NULL;
986 priv->tsfeed = NULL;
988 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
989 dprintk("%s: alloc secfeed\n", __func__);
990 ret=demux->allocate_section_feed(demux, &priv->secfeed,
991 dvb_net_sec_callback);
992 if (ret<0) {
993 printk("%s: could not allocate section feed\n", dev->name);
994 goto error;
997 ret = priv->secfeed->set(priv->secfeed, priv->pid, 32768, 1);
999 if (ret<0) {
1000 printk("%s: could not set section feed\n", dev->name);
1001 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1002 priv->secfeed=NULL;
1003 goto error;
1006 if (priv->rx_mode != RX_MODE_PROMISC) {
1007 dprintk("%s: set secfilter\n", __func__);
1008 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal);
1011 switch (priv->rx_mode) {
1012 case RX_MODE_MULTI:
1013 for (i = 0; i < priv->multi_num; i++) {
1014 dprintk("%s: set multi_secfilter[%d]\n", __func__, i);
1015 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i],
1016 priv->multi_macs[i], mask_normal);
1018 break;
1019 case RX_MODE_ALL_MULTI:
1020 priv->multi_num=1;
1021 dprintk("%s: set multi_secfilter[0]\n", __func__);
1022 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0],
1023 mac_allmulti, mask_allmulti);
1024 break;
1025 case RX_MODE_PROMISC:
1026 priv->multi_num=0;
1027 dprintk("%s: set secfilter\n", __func__);
1028 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc);
1029 break;
1032 dprintk("%s: start filtering\n", __func__);
1033 priv->secfeed->start_filtering(priv->secfeed);
1034 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1035 struct timespec timeout = { 0, 10000000 }; // 10 msec
1037 /* we have payloads encapsulated in TS */
1038 dprintk("%s: alloc tsfeed\n", __func__);
1039 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback);
1040 if (ret < 0) {
1041 printk("%s: could not allocate ts feed\n", dev->name);
1042 goto error;
1045 /* Set netdevice pointer for ts decaps callback. */
1046 priv->tsfeed->priv = (void *)dev;
1047 ret = priv->tsfeed->set(priv->tsfeed,
1048 priv->pid, /* pid */
1049 TS_PACKET, /* type */
1050 DMX_TS_PES_OTHER, /* pes type */
1051 32768, /* circular buffer size */
1052 timeout /* timeout */
1055 if (ret < 0) {
1056 printk("%s: could not set ts feed\n", dev->name);
1057 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1058 priv->tsfeed = NULL;
1059 goto error;
1062 dprintk("%s: start filtering\n", __func__);
1063 priv->tsfeed->start_filtering(priv->tsfeed);
1064 } else
1065 ret = -EINVAL;
1067 error:
1068 mutex_unlock(&priv->mutex);
1069 return ret;
1072 static int dvb_net_feed_stop(struct net_device *dev)
1074 struct dvb_net_priv *priv = netdev_priv(dev);
1075 int i, ret = 0;
1077 dprintk("%s\n", __func__);
1078 mutex_lock(&priv->mutex);
1079 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) {
1080 if (priv->secfeed) {
1081 if (priv->secfeed->is_filtering) {
1082 dprintk("%s: stop secfeed\n", __func__);
1083 priv->secfeed->stop_filtering(priv->secfeed);
1086 if (priv->secfilter) {
1087 dprintk("%s: release secfilter\n", __func__);
1088 priv->secfeed->release_filter(priv->secfeed,
1089 priv->secfilter);
1090 priv->secfilter=NULL;
1093 for (i=0; i<priv->multi_num; i++) {
1094 if (priv->multi_secfilter[i]) {
1095 dprintk("%s: release multi_filter[%d]\n",
1096 __func__, i);
1097 priv->secfeed->release_filter(priv->secfeed,
1098 priv->multi_secfilter[i]);
1099 priv->multi_secfilter[i] = NULL;
1103 priv->demux->release_section_feed(priv->demux, priv->secfeed);
1104 priv->secfeed = NULL;
1105 } else
1106 printk("%s: no feed to stop\n", dev->name);
1107 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) {
1108 if (priv->tsfeed) {
1109 if (priv->tsfeed->is_filtering) {
1110 dprintk("%s: stop tsfeed\n", __func__);
1111 priv->tsfeed->stop_filtering(priv->tsfeed);
1113 priv->demux->release_ts_feed(priv->demux, priv->tsfeed);
1114 priv->tsfeed = NULL;
1116 else
1117 printk("%s: no ts feed to stop\n", dev->name);
1118 } else
1119 ret = -EINVAL;
1120 mutex_unlock(&priv->mutex);
1121 return ret;
1125 static int dvb_set_mc_filter (struct net_device *dev, struct dev_mc_list *mc)
1127 struct dvb_net_priv *priv = netdev_priv(dev);
1129 if (priv->multi_num == DVB_NET_MULTICAST_MAX)
1130 return -ENOMEM;
1132 memcpy(priv->multi_macs[priv->multi_num], mc->dmi_addr, 6);
1134 priv->multi_num++;
1135 return 0;
1139 static void wq_set_multicast_list (struct work_struct *work)
1141 struct dvb_net_priv *priv =
1142 container_of(work, struct dvb_net_priv, set_multicast_list_wq);
1143 struct net_device *dev = priv->net;
1145 dvb_net_feed_stop(dev);
1146 priv->rx_mode = RX_MODE_UNI;
1147 netif_addr_lock_bh(dev);
1149 if (dev->flags & IFF_PROMISC) {
1150 dprintk("%s: promiscuous mode\n", dev->name);
1151 priv->rx_mode = RX_MODE_PROMISC;
1152 } else if ((dev->flags & IFF_ALLMULTI)) {
1153 dprintk("%s: allmulti mode\n", dev->name);
1154 priv->rx_mode = RX_MODE_ALL_MULTI;
1155 } else if (dev->mc_count) {
1156 int mci;
1157 struct dev_mc_list *mc;
1159 dprintk("%s: set_mc_list, %d entries\n",
1160 dev->name, dev->mc_count);
1162 priv->rx_mode = RX_MODE_MULTI;
1163 priv->multi_num = 0;
1165 for (mci = 0, mc=dev->mc_list;
1166 mci < dev->mc_count;
1167 mc = mc->next, mci++) {
1168 dvb_set_mc_filter(dev, mc);
1172 netif_addr_unlock_bh(dev);
1173 dvb_net_feed_start(dev);
1177 static void dvb_net_set_multicast_list (struct net_device *dev)
1179 struct dvb_net_priv *priv = netdev_priv(dev);
1180 schedule_work(&priv->set_multicast_list_wq);
1184 static void wq_restart_net_feed (struct work_struct *work)
1186 struct dvb_net_priv *priv =
1187 container_of(work, struct dvb_net_priv, restart_net_feed_wq);
1188 struct net_device *dev = priv->net;
1190 if (netif_running(dev)) {
1191 dvb_net_feed_stop(dev);
1192 dvb_net_feed_start(dev);
1197 static int dvb_net_set_mac (struct net_device *dev, void *p)
1199 struct dvb_net_priv *priv = netdev_priv(dev);
1200 struct sockaddr *addr=p;
1202 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1204 if (netif_running(dev))
1205 schedule_work(&priv->restart_net_feed_wq);
1207 return 0;
1211 static int dvb_net_open(struct net_device *dev)
1213 struct dvb_net_priv *priv = netdev_priv(dev);
1215 priv->in_use++;
1216 dvb_net_feed_start(dev);
1217 return 0;
1221 static int dvb_net_stop(struct net_device *dev)
1223 struct dvb_net_priv *priv = netdev_priv(dev);
1225 priv->in_use--;
1226 return dvb_net_feed_stop(dev);
1229 static const struct header_ops dvb_header_ops = {
1230 .create = eth_header,
1231 .parse = eth_header_parse,
1232 .rebuild = eth_rebuild_header,
1236 static const struct net_device_ops dvb_netdev_ops = {
1237 .ndo_open = dvb_net_open,
1238 .ndo_stop = dvb_net_stop,
1239 .ndo_start_xmit = dvb_net_tx,
1240 .ndo_set_multicast_list = dvb_net_set_multicast_list,
1241 .ndo_set_mac_address = dvb_net_set_mac,
1242 .ndo_change_mtu = eth_change_mtu,
1243 .ndo_validate_addr = eth_validate_addr,
1246 static void dvb_net_setup(struct net_device *dev)
1248 ether_setup(dev);
1250 dev->header_ops = &dvb_header_ops;
1251 dev->netdev_ops = &dvb_netdev_ops;
1252 dev->mtu = 4096;
1253 dev->mc_count = 0;
1255 dev->flags |= IFF_NOARP;
1258 static int get_if(struct dvb_net *dvbnet)
1260 int i;
1262 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1263 if (!dvbnet->state[i])
1264 break;
1266 if (i == DVB_NET_DEVICES_MAX)
1267 return -1;
1269 dvbnet->state[i]=1;
1270 return i;
1273 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype)
1275 struct net_device *net;
1276 struct dvb_net_priv *priv;
1277 int result;
1278 int if_num;
1280 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE)
1281 return -EINVAL;
1282 if ((if_num = get_if(dvbnet)) < 0)
1283 return -EINVAL;
1285 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", dvb_net_setup);
1286 if (!net)
1287 return -ENOMEM;
1289 if (dvbnet->dvbdev->id)
1290 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d",
1291 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num);
1292 else
1293 /* compatibility fix to keep dvb0_0 format */
1294 snprintf(net->name, IFNAMSIZ, "dvb%d_%d",
1295 dvbnet->dvbdev->adapter->num, if_num);
1297 net->addr_len = 6;
1298 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6);
1300 dvbnet->device[if_num] = net;
1302 priv = netdev_priv(net);
1303 priv->net = net;
1304 priv->demux = dvbnet->demux;
1305 priv->pid = pid;
1306 priv->rx_mode = RX_MODE_UNI;
1307 priv->need_pusi = 1;
1308 priv->tscc = 0;
1309 priv->feedtype = feedtype;
1310 reset_ule(priv);
1312 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list);
1313 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed);
1314 mutex_init(&priv->mutex);
1316 net->base_addr = pid;
1318 if ((result = register_netdev(net)) < 0) {
1319 dvbnet->device[if_num] = NULL;
1320 free_netdev(net);
1321 return result;
1323 printk("dvb_net: created network interface %s\n", net->name);
1325 return if_num;
1328 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num)
1330 struct net_device *net = dvbnet->device[num];
1331 struct dvb_net_priv *priv;
1333 if (!dvbnet->state[num])
1334 return -EINVAL;
1335 priv = netdev_priv(net);
1336 if (priv->in_use)
1337 return -EBUSY;
1339 dvb_net_stop(net);
1340 flush_scheduled_work();
1341 printk("dvb_net: removed network interface %s\n", net->name);
1342 unregister_netdev(net);
1343 dvbnet->state[num]=0;
1344 dvbnet->device[num] = NULL;
1345 free_netdev(net);
1347 return 0;
1350 static int dvb_net_do_ioctl(struct inode *inode, struct file *file,
1351 unsigned int cmd, void *parg)
1353 struct dvb_device *dvbdev = file->private_data;
1354 struct dvb_net *dvbnet = dvbdev->priv;
1356 if (((file->f_flags&O_ACCMODE)==O_RDONLY))
1357 return -EPERM;
1359 switch (cmd) {
1360 case NET_ADD_IF:
1362 struct dvb_net_if *dvbnetif = parg;
1363 int result;
1365 if (!capable(CAP_SYS_ADMIN))
1366 return -EPERM;
1368 if (!try_module_get(dvbdev->adapter->module))
1369 return -EPERM;
1371 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype);
1372 if (result<0) {
1373 module_put(dvbdev->adapter->module);
1374 return result;
1376 dvbnetif->if_num=result;
1377 break;
1379 case NET_GET_IF:
1381 struct net_device *netdev;
1382 struct dvb_net_priv *priv_data;
1383 struct dvb_net_if *dvbnetif = parg;
1385 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1386 !dvbnet->state[dvbnetif->if_num])
1387 return -EINVAL;
1389 netdev = dvbnet->device[dvbnetif->if_num];
1391 priv_data = netdev_priv(netdev);
1392 dvbnetif->pid=priv_data->pid;
1393 dvbnetif->feedtype=priv_data->feedtype;
1394 break;
1396 case NET_REMOVE_IF:
1398 int ret;
1400 if (!capable(CAP_SYS_ADMIN))
1401 return -EPERM;
1402 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX)
1403 return -EINVAL;
1404 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg);
1405 if (!ret)
1406 module_put(dvbdev->adapter->module);
1407 return ret;
1410 /* binary compatiblity cruft */
1411 case __NET_ADD_IF_OLD:
1413 struct __dvb_net_if_old *dvbnetif = parg;
1414 int result;
1416 if (!capable(CAP_SYS_ADMIN))
1417 return -EPERM;
1419 if (!try_module_get(dvbdev->adapter->module))
1420 return -EPERM;
1422 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE);
1423 if (result<0) {
1424 module_put(dvbdev->adapter->module);
1425 return result;
1427 dvbnetif->if_num=result;
1428 break;
1430 case __NET_GET_IF_OLD:
1432 struct net_device *netdev;
1433 struct dvb_net_priv *priv_data;
1434 struct __dvb_net_if_old *dvbnetif = parg;
1436 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX ||
1437 !dvbnet->state[dvbnetif->if_num])
1438 return -EINVAL;
1440 netdev = dvbnet->device[dvbnetif->if_num];
1442 priv_data = netdev_priv(netdev);
1443 dvbnetif->pid=priv_data->pid;
1444 break;
1446 default:
1447 return -ENOTTY;
1449 return 0;
1452 static int dvb_net_ioctl(struct inode *inode, struct file *file,
1453 unsigned int cmd, unsigned long arg)
1455 return dvb_usercopy(inode, file, cmd, arg, dvb_net_do_ioctl);
1458 static int dvb_net_close(struct inode *inode, struct file *file)
1460 struct dvb_device *dvbdev = file->private_data;
1461 struct dvb_net *dvbnet = dvbdev->priv;
1463 dvb_generic_release(inode, file);
1465 if(dvbdev->users == 1 && dvbnet->exit == 1) {
1466 fops_put(file->f_op);
1467 file->f_op = NULL;
1468 wake_up(&dvbdev->wait_queue);
1470 return 0;
1474 static const struct file_operations dvb_net_fops = {
1475 .owner = THIS_MODULE,
1476 .ioctl = dvb_net_ioctl,
1477 .open = dvb_generic_open,
1478 .release = dvb_net_close,
1481 static struct dvb_device dvbdev_net = {
1482 .priv = NULL,
1483 .users = 1,
1484 .writers = 1,
1485 .fops = &dvb_net_fops,
1489 void dvb_net_release (struct dvb_net *dvbnet)
1491 int i;
1493 dvbnet->exit = 1;
1494 if (dvbnet->dvbdev->users < 1)
1495 wait_event(dvbnet->dvbdev->wait_queue,
1496 dvbnet->dvbdev->users==1);
1498 dvb_unregister_device(dvbnet->dvbdev);
1500 for (i=0; i<DVB_NET_DEVICES_MAX; i++) {
1501 if (!dvbnet->state[i])
1502 continue;
1503 dvb_net_remove_if(dvbnet, i);
1506 EXPORT_SYMBOL(dvb_net_release);
1509 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet,
1510 struct dmx_demux *dmx)
1512 int i;
1514 dvbnet->demux = dmx;
1516 for (i=0; i<DVB_NET_DEVICES_MAX; i++)
1517 dvbnet->state[i] = 0;
1519 dvb_register_device (adap, &dvbnet->dvbdev, &dvbdev_net,
1520 dvbnet, DVB_DEVICE_NET);
1522 return 0;
1524 EXPORT_SYMBOL(dvb_net_init);