1 /* 2 * dvb_net.c 3 * 4 * Copyright (C) 2001 Convergence integrated media GmbH 5 * Ralph Metzler <ralph@convergence.de> 6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de> 7 * 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> 14 * 15 * ULE Decaps according to RFC 4326. 16 * 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. 21 * 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. 26 * To obtain the license, point your browser to 27 * http://www.gnu.org/copyleft/gpl.html 28 */ 29 30 /* 31 * ULE ChangeLog: 32 * Feb 2004: hl/ws v1: Implementing draft-fair-ipdvb-ule-01.txt 33 * 34 * Dec 2004: hl/ws v2: Implementing draft-ietf-ipdvb-ule-03.txt: 35 * ULE Extension header handling. 36 * Bugreports by Moritz Vieth and Hanno Tersteegen, 37 * Fraunhofer Institute for Open Communication Systems 38 * Competence Center for Advanced Satellite Communications. 39 * Bugfixes and robustness improvements. 40 * Filtering on dest MAC addresses, if present (D-Bit = 0) 41 * DVB_ULE_DEBUG compile-time option. 42 * Apr 2006: cp v3: Bugfixes and compliency with RFC 4326 (ULE) by 43 * Christian Praehauser <cpraehaus@cosy.sbg.ac.at>, 44 * Paris Lodron University of Salzburg. 45 */ 46 47 /* 48 * FIXME / TODO (dvb_net.c): 49 * 50 * Unloading does not work for 2.6.9 kernels: a refcount doesn't go to zero. 51 * 52 */ 53 54 #define pr_fmt(fmt) "dvb_net: " fmt 55 56 #include <linux/module.h> 57 #include <linux/kernel.h> 58 #include <linux/netdevice.h> 59 #include <linux/etherdevice.h> 60 #include <linux/dvb/net.h> 61 #include <linux/uio.h> 62 #include <linux/uaccess.h> 63 #include <linux/crc32.h> 64 #include <linux/mutex.h> 65 #include <linux/sched.h> 66 67 #include <media/dvb_demux.h> 68 #include <media/dvb_net.h> 69 70 static inline __u32 iov_crc32( __u32 c, struct kvec *iov, unsigned int cnt ) 71 { 72 unsigned int j; 73 for (j = 0; j < cnt; j++) 74 c = crc32_be( c, iov[j].iov_base, iov[j].iov_len ); 75 return c; 76 } 77 78 79 #define DVB_NET_MULTICAST_MAX 10 80 81 #ifdef DVB_ULE_DEBUG 82 /* 83 * The code inside DVB_ULE_DEBUG keeps a history of the 84 * last 100 TS cells processed. 85 */ 86 static unsigned char ule_hist[100*TS_SZ] = { 0 }; 87 static unsigned char *ule_where = ule_hist, ule_dump; 88 89 static void hexdump(const unsigned char *buf, unsigned short len) 90 { 91 print_hex_dump_debug("", DUMP_PREFIX_OFFSET, 16, 1, buf, len, true); 92 } 93 #endif 94 95 struct dvb_net_priv { 96 int in_use; 97 u16 pid; 98 struct net_device *net; 99 struct dvb_net *host; 100 struct dmx_demux *demux; 101 struct dmx_section_feed *secfeed; 102 struct dmx_section_filter *secfilter; 103 struct dmx_ts_feed *tsfeed; 104 int multi_num; 105 struct dmx_section_filter *multi_secfilter[DVB_NET_MULTICAST_MAX]; 106 unsigned char multi_macs[DVB_NET_MULTICAST_MAX][6]; 107 int rx_mode; 108 #define RX_MODE_UNI 0 109 #define RX_MODE_MULTI 1 110 #define RX_MODE_ALL_MULTI 2 111 #define RX_MODE_PROMISC 3 112 struct work_struct set_multicast_list_wq; 113 struct work_struct restart_net_feed_wq; 114 unsigned char feedtype; /* Either FEED_TYPE_ or FEED_TYPE_ULE */ 115 int need_pusi; /* Set to 1, if synchronization on PUSI required. */ 116 unsigned char tscc; /* TS continuity counter after sync on PUSI. */ 117 struct sk_buff *ule_skb; /* ULE SNDU decodes into this buffer. */ 118 unsigned char *ule_next_hdr; /* Pointer into skb to next ULE extension header. */ 119 unsigned short ule_sndu_len; /* ULE SNDU length in bytes, w/o D-Bit. */ 120 unsigned short ule_sndu_type; /* ULE SNDU type field, complete. */ 121 unsigned char ule_sndu_type_1; /* ULE SNDU type field, if split across 2 TS cells. */ 122 unsigned char ule_dbit; /* Whether the DestMAC address present 123 * or not (bit is set). */ 124 unsigned char ule_bridged; /* Whether the ULE_BRIDGED extension header was found. */ 125 int ule_sndu_remain; /* Nr. of bytes still required for current ULE SNDU. */ 126 unsigned long ts_count; /* Current ts cell counter. */ 127 struct mutex mutex; 128 }; 129 130 131 /* 132 * Determine the packet's protocol ID. The rule here is that we 133 * assume 802.3 if the type field is short enough to be a length. 134 * This is normal practice and works for any 'now in use' protocol. 135 * 136 * stolen from eth.c out of the linux kernel, hacked for dvb-device 137 * by Michael Holzt <kju@debian.org> 138 */ 139 static __be16 dvb_net_eth_type_trans(struct sk_buff *skb, 140 struct net_device *dev) 141 { 142 struct ethhdr *eth; 143 unsigned char *rawp; 144 145 skb_reset_mac_header(skb); 146 skb_pull(skb,dev->hard_header_len); 147 eth = eth_hdr(skb); 148 149 if (*eth->h_dest & 1) { 150 if(ether_addr_equal(eth->h_dest,dev->broadcast)) 151 skb->pkt_type=PACKET_BROADCAST; 152 else 153 skb->pkt_type=PACKET_MULTICAST; 154 } 155 156 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) 157 return eth->h_proto; 158 159 rawp = skb->data; 160 161 /* 162 * This is a magic hack to spot IPX packets. Older Novell breaks 163 * the protocol design and runs IPX over 802.3 without an 802.2 LLC 164 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This 165 * won't work for fault tolerant netware but does for the rest. 166 */ 167 if (*(unsigned short *)rawp == 0xFFFF) 168 return htons(ETH_P_802_3); 169 170 /* 171 * Real 802.2 LLC 172 */ 173 return htons(ETH_P_802_2); 174 } 175 176 #define TS_SZ 188 177 #define TS_SYNC 0x47 178 #define TS_TEI 0x80 179 #define TS_SC 0xC0 180 #define TS_PUSI 0x40 181 #define TS_AF_A 0x20 182 #define TS_AF_D 0x10 183 184 /* ULE Extension Header handlers. */ 185 186 #define ULE_TEST 0 187 #define ULE_BRIDGED 1 188 189 #define ULE_OPTEXTHDR_PADDING 0 190 191 static int ule_test_sndu( struct dvb_net_priv *p ) 192 { 193 return -1; 194 } 195 196 static int ule_bridged_sndu( struct dvb_net_priv *p ) 197 { 198 struct ethhdr *hdr = (struct ethhdr*) p->ule_next_hdr; 199 if(ntohs(hdr->h_proto) < ETH_P_802_3_MIN) { 200 int framelen = p->ule_sndu_len - ((p->ule_next_hdr+sizeof(struct ethhdr)) - p->ule_skb->data); 201 /* A frame Type < ETH_P_802_3_MIN for a bridged frame, introduces a LLC Length field. */ 202 if(framelen != ntohs(hdr->h_proto)) { 203 return -1; 204 } 205 } 206 /* Note: 207 * From RFC4326: 208 * "A bridged SNDU is a Mandatory Extension Header of Type 1. 209 * It must be the final (or only) extension header specified in the header chain of a SNDU." 210 * The 'ule_bridged' flag will cause the extension header processing loop to terminate. 211 */ 212 p->ule_bridged = 1; 213 return 0; 214 } 215 216 static int ule_exthdr_padding(struct dvb_net_priv *p) 217 { 218 return 0; 219 } 220 221 /* 222 * Handle ULE extension headers. 223 * Function is called after a successful CRC32 verification of an ULE SNDU to complete its decoding. 224 * Returns: >= 0: nr. of bytes consumed by next extension header 225 * -1: Mandatory extension header that is not recognized or TEST SNDU; discard. 226 */ 227 static int handle_one_ule_extension( struct dvb_net_priv *p ) 228 { 229 /* Table of mandatory extension header handlers. The header type is the index. */ 230 static int (*ule_mandatory_ext_handlers[255])( struct dvb_net_priv *p ) = 231 { [0] = ule_test_sndu, [1] = ule_bridged_sndu, [2] = NULL, }; 232 233 /* Table of optional extension header handlers. The header type is the index. */ 234 static int (*ule_optional_ext_handlers[255])( struct dvb_net_priv *p ) = 235 { [0] = ule_exthdr_padding, [1] = NULL, }; 236 237 int ext_len = 0; 238 unsigned char hlen = (p->ule_sndu_type & 0x0700) >> 8; 239 unsigned char htype = p->ule_sndu_type & 0x00FF; 240 241 /* Discriminate mandatory and optional extension headers. */ 242 if (hlen == 0) { 243 /* Mandatory extension header */ 244 if (ule_mandatory_ext_handlers[htype]) { 245 ext_len = ule_mandatory_ext_handlers[htype]( p ); 246 if(ext_len >= 0) { 247 p->ule_next_hdr += ext_len; 248 if (!p->ule_bridged) { 249 p->ule_sndu_type = ntohs(*(__be16 *)p->ule_next_hdr); 250 p->ule_next_hdr += 2; 251 } else { 252 p->ule_sndu_type = ntohs(*(__be16 *)(p->ule_next_hdr + ((p->ule_dbit ? 2 : 3) * ETH_ALEN))); 253 /* This assures the extension handling loop will terminate. */ 254 } 255 } 256 // else: extension handler failed or SNDU should be discarded 257 } else 258 ext_len = -1; /* SNDU has to be discarded. */ 259 } else { 260 /* Optional extension header. Calculate the length. */ 261 ext_len = hlen << 1; 262 /* Process the optional extension header according to its type. */ 263 if (ule_optional_ext_handlers[htype]) 264 (void)ule_optional_ext_handlers[htype]( p ); 265 p->ule_next_hdr += ext_len; 266 p->ule_sndu_type = ntohs( *(__be16 *)(p->ule_next_hdr-2) ); 267 /* 268 * note: the length of the next header type is included in the 269 * length of THIS optional extension header 270 */ 271 } 272 273 return ext_len; 274 } 275 276 static int handle_ule_extensions( struct dvb_net_priv *p ) 277 { 278 int total_ext_len = 0, l; 279 280 p->ule_next_hdr = p->ule_skb->data; 281 do { 282 l = handle_one_ule_extension( p ); 283 if (l < 0) 284 return l; /* Stop extension header processing and discard SNDU. */ 285 total_ext_len += l; 286 pr_debug("ule_next_hdr=%p, ule_sndu_type=%i, l=%i, total_ext_len=%i\n", 287 p->ule_next_hdr, (int)p->ule_sndu_type, 288 l, total_ext_len); 289 290 } while (p->ule_sndu_type < ETH_P_802_3_MIN); 291 292 return total_ext_len; 293 } 294 295 296 /* Prepare for a new ULE SNDU: reset the decoder state. */ 297 static inline void reset_ule( struct dvb_net_priv *p ) 298 { 299 p->ule_skb = NULL; 300 p->ule_next_hdr = NULL; 301 p->ule_sndu_len = 0; 302 p->ule_sndu_type = 0; 303 p->ule_sndu_type_1 = 0; 304 p->ule_sndu_remain = 0; 305 p->ule_dbit = 0xFF; 306 p->ule_bridged = 0; 307 } 308 309 /* 310 * Decode ULE SNDUs according to draft-ietf-ipdvb-ule-03.txt from a sequence of 311 * TS cells of a single PID. 312 */ 313 314 struct dvb_net_ule_handle { 315 struct net_device *dev; 316 struct dvb_net_priv *priv; 317 struct ethhdr *ethh; 318 const u8 *buf; 319 size_t buf_len; 320 unsigned long skipped; 321 const u8 *ts, *ts_end, *from_where; 322 u8 ts_remain, how_much, new_ts; 323 bool error; 324 }; 325 326 static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h) 327 { 328 /* We are about to process a new TS cell. */ 329 330 #ifdef DVB_ULE_DEBUG 331 if (ule_where >= &ule_hist[100*TS_SZ]) 332 ule_where = ule_hist; 333 memcpy(ule_where, h->ts, TS_SZ); 334 if (ule_dump) { 335 hexdump(ule_where, TS_SZ); 336 ule_dump = 0; 337 } 338 ule_where += TS_SZ; 339 #endif 340 341 /* 342 * Check TS h->error conditions: sync_byte, transport_error_indicator, 343 * scrambling_control . 344 */ 345 if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) || 346 ((h->ts[3] & TS_SC) != 0)) { 347 pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n", 348 h->priv->ts_count, h->ts[0], 349 (h->ts[1] & TS_TEI) >> 7, 350 (h->ts[3] & TS_SC) >> 6); 351 352 /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 353 if (h->priv->ule_skb) { 354 dev_kfree_skb(h->priv->ule_skb); 355 /* Prepare for next SNDU. */ 356 h->dev->stats.rx_errors++; 357 h->dev->stats.rx_frame_errors++; 358 } 359 reset_ule(h->priv); 360 h->priv->need_pusi = 1; 361 362 /* Continue with next TS cell. */ 363 h->ts += TS_SZ; 364 h->priv->ts_count++; 365 return 1; 366 } 367 368 h->ts_remain = 184; 369 h->from_where = h->ts + 4; 370 371 return 0; 372 } 373 374 static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h) 375 { 376 if (h->ts[1] & TS_PUSI) { 377 /* Find beginning of first ULE SNDU in current TS cell. */ 378 /* Synchronize continuity counter. */ 379 h->priv->tscc = h->ts[3] & 0x0F; 380 /* There is a pointer field here. */ 381 if (h->ts[4] > h->ts_remain) { 382 pr_err("%lu: Invalid ULE packet (pointer field %d)\n", 383 h->priv->ts_count, h->ts[4]); 384 h->ts += TS_SZ; 385 h->priv->ts_count++; 386 return 1; 387 } 388 /* Skip to destination of pointer field. */ 389 h->from_where = &h->ts[5] + h->ts[4]; 390 h->ts_remain -= 1 + h->ts[4]; 391 h->skipped = 0; 392 } else { 393 h->skipped++; 394 h->ts += TS_SZ; 395 h->priv->ts_count++; 396 return 1; 397 } 398 399 return 0; 400 } 401 402 static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h) 403 { 404 /* Check continuity counter. */ 405 if ((h->ts[3] & 0x0F) == h->priv->tscc) 406 h->priv->tscc = (h->priv->tscc + 1) & 0x0F; 407 else { 408 /* TS discontinuity handling: */ 409 pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n", 410 h->priv->ts_count, h->ts[3] & 0x0F, 411 h->priv->tscc); 412 /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 413 if (h->priv->ule_skb) { 414 dev_kfree_skb(h->priv->ule_skb); 415 /* Prepare for next SNDU. */ 416 // reset_ule(h->priv); moved to below. 417 h->dev->stats.rx_errors++; 418 h->dev->stats.rx_frame_errors++; 419 } 420 reset_ule(h->priv); 421 /* skip to next PUSI. */ 422 h->priv->need_pusi = 1; 423 return 1; 424 } 425 /* 426 * If we still have an incomplete payload, but PUSI is 427 * set; some TS cells are missing. 428 * This is only possible here, if we missed exactly 16 TS 429 * cells (continuity counter wrap). 430 */ 431 if (h->ts[1] & TS_PUSI) { 432 if (!h->priv->need_pusi) { 433 if (!(*h->from_where < (h->ts_remain-1)) || 434 *h->from_where != h->priv->ule_sndu_remain) { 435 /* 436 * Pointer field is invalid. 437 * Drop this TS cell and any started ULE SNDU. 438 */ 439 pr_warn("%lu: Invalid pointer field: %u.\n", 440 h->priv->ts_count, 441 *h->from_where); 442 443 /* 444 * Drop partly decoded SNDU, reset state, 445 * resync on PUSI. 446 */ 447 if (h->priv->ule_skb) { 448 h->error = true; 449 dev_kfree_skb(h->priv->ule_skb); 450 } 451 452 if (h->error || h->priv->ule_sndu_remain) { 453 h->dev->stats.rx_errors++; 454 h->dev->stats.rx_frame_errors++; 455 h->error = false; 456 } 457 458 reset_ule(h->priv); 459 h->priv->need_pusi = 1; 460 return 1; 461 } 462 /* 463 * Skip pointer field (we're processing a 464 * packed payload). 465 */ 466 h->from_where += 1; 467 h->ts_remain -= 1; 468 } else 469 h->priv->need_pusi = 0; 470 471 if (h->priv->ule_sndu_remain > 183) { 472 /* 473 * Current SNDU lacks more data than there 474 * could be available in the current TS cell. 475 */ 476 h->dev->stats.rx_errors++; 477 h->dev->stats.rx_length_errors++; 478 pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d). Flushing incomplete payload.\n", 479 h->priv->ts_count, 480 h->priv->ule_sndu_remain, 481 h->ts[4], h->ts_remain); 482 dev_kfree_skb(h->priv->ule_skb); 483 /* Prepare for next SNDU. */ 484 reset_ule(h->priv); 485 /* 486 * Resync: go to where pointer field points to: 487 * start of next ULE SNDU. 488 */ 489 h->from_where += h->ts[4]; 490 h->ts_remain -= h->ts[4]; 491 } 492 } 493 return 0; 494 } 495 496 497 /* 498 * Start a new payload with skb. 499 * Find ULE header. It is only guaranteed that the 500 * length field (2 bytes) is contained in the current 501 * TS. 502 * Check h.ts_remain has to be >= 2 here. 503 */ 504 static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h) 505 { 506 if (h->ts_remain < 2) { 507 pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n", 508 h->ts_remain); 509 h->priv->ule_sndu_len = 0; 510 h->priv->need_pusi = 1; 511 h->ts += TS_SZ; 512 return 1; 513 } 514 515 if (!h->priv->ule_sndu_len) { 516 /* Got at least two bytes, thus extrace the SNDU length. */ 517 h->priv->ule_sndu_len = h->from_where[0] << 8 | 518 h->from_where[1]; 519 if (h->priv->ule_sndu_len & 0x8000) { 520 /* D-Bit is set: no dest mac present. */ 521 h->priv->ule_sndu_len &= 0x7FFF; 522 h->priv->ule_dbit = 1; 523 } else 524 h->priv->ule_dbit = 0; 525 526 if (h->priv->ule_sndu_len < 5) { 527 pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n", 528 h->priv->ts_count, 529 h->priv->ule_sndu_len); 530 h->dev->stats.rx_errors++; 531 h->dev->stats.rx_length_errors++; 532 h->priv->ule_sndu_len = 0; 533 h->priv->need_pusi = 1; 534 h->new_ts = 1; 535 h->ts += TS_SZ; 536 h->priv->ts_count++; 537 return 1; 538 } 539 h->ts_remain -= 2; /* consume the 2 bytes SNDU length. */ 540 h->from_where += 2; 541 } 542 543 h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2; 544 /* 545 * State of current TS: 546 * h->ts_remain (remaining bytes in the current TS cell) 547 * 0 ule_type is not available now, we need the next TS cell 548 * 1 the first byte of the ule_type is present 549 * >=2 full ULE header present, maybe some payload data as well. 550 */ 551 switch (h->ts_remain) { 552 case 1: 553 h->priv->ule_sndu_remain--; 554 h->priv->ule_sndu_type = h->from_where[0] << 8; 555 556 /* first byte of ule_type is set. */ 557 h->priv->ule_sndu_type_1 = 1; 558 h->ts_remain -= 1; 559 h->from_where += 1; 560 /* fallthrough */ 561 case 0: 562 h->new_ts = 1; 563 h->ts += TS_SZ; 564 h->priv->ts_count++; 565 return 1; 566 567 default: /* complete ULE header is present in current TS. */ 568 /* Extract ULE type field. */ 569 if (h->priv->ule_sndu_type_1) { 570 h->priv->ule_sndu_type_1 = 0; 571 h->priv->ule_sndu_type |= h->from_where[0]; 572 h->from_where += 1; /* points to payload start. */ 573 h->ts_remain -= 1; 574 } else { 575 /* Complete type is present in new TS. */ 576 h->priv->ule_sndu_type = h->from_where[0] << 8 | 577 h->from_where[1]; 578 h->from_where += 2; /* points to payload start. */ 579 h->ts_remain -= 2; 580 } 581 break; 582 } 583 584 /* 585 * Allocate the skb (decoder target buffer) with the correct size, 586 * as follows: 587 * 588 * prepare for the largest case: bridged SNDU with MAC address 589 * (dbit = 0). 590 */ 591 h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len + 592 ETH_HLEN + ETH_ALEN); 593 if (!h->priv->ule_skb) { 594 pr_notice("%s: Memory squeeze, dropping packet.\n", 595 h->dev->name); 596 h->dev->stats.rx_dropped++; 597 return -1; 598 } 599 600 /* This includes the CRC32 _and_ dest mac, if !dbit. */ 601 h->priv->ule_sndu_remain = h->priv->ule_sndu_len; 602 h->priv->ule_skb->dev = h->dev; 603 /* 604 * Leave space for Ethernet or bridged SNDU header 605 * (eth hdr plus one MAC addr). 606 */ 607 skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN); 608 609 return 0; 610 } 611 612 613 static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h) 614 { 615 static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff }; 616 617 /* 618 * The destination MAC address is the next data in the skb. It comes 619 * before any extension headers. 620 * 621 * Check if the payload of this SNDU should be passed up the stack. 622 */ 623 if (h->priv->rx_mode == RX_MODE_PROMISC) 624 return 0; 625 626 if (h->priv->ule_skb->data[0] & 0x01) { 627 /* multicast or broadcast */ 628 if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) { 629 /* multicast */ 630 if (h->priv->rx_mode == RX_MODE_MULTI) { 631 int i; 632 633 for (i = 0; i < h->priv->multi_num && 634 !ether_addr_equal(h->priv->ule_skb->data, 635 h->priv->multi_macs[i]); 636 i++) 637 ; 638 if (i == h->priv->multi_num) 639 return 1; 640 } else if (h->priv->rx_mode != RX_MODE_ALL_MULTI) 641 return 1; /* no broadcast; */ 642 /* 643 * else: 644 * all multicast mode: accept all multicast packets 645 */ 646 } 647 /* else: broadcast */ 648 } else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr)) 649 return 1; 650 651 return 0; 652 } 653 654 655 static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h, 656 struct kvec iov[3], 657 u32 ule_crc, u32 expected_crc) 658 { 659 u8 dest_addr[ETH_ALEN]; 660 661 if (ule_crc != expected_crc) { 662 pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n", 663 h->priv->ts_count, ule_crc, expected_crc, 664 h->priv->ule_sndu_len, h->priv->ule_sndu_type, 665 h->ts_remain, 666 h->ts_remain > 2 ? 667 *(unsigned short *)h->from_where : 0); 668 669 #ifdef DVB_ULE_DEBUG 670 hexdump(iov[0].iov_base, iov[0].iov_len); 671 hexdump(iov[1].iov_base, iov[1].iov_len); 672 hexdump(iov[2].iov_base, iov[2].iov_len); 673 674 if (ule_where == ule_hist) { 675 hexdump(&ule_hist[98*TS_SZ], TS_SZ); 676 hexdump(&ule_hist[99*TS_SZ], TS_SZ); 677 } else if (ule_where == &ule_hist[TS_SZ]) { 678 hexdump(&ule_hist[99*TS_SZ], TS_SZ); 679 hexdump(ule_hist, TS_SZ); 680 } else { 681 hexdump(ule_where - TS_SZ - TS_SZ, TS_SZ); 682 hexdump(ule_where - TS_SZ, TS_SZ); 683 } 684 ule_dump = 1; 685 #endif 686 687 h->dev->stats.rx_errors++; 688 h->dev->stats.rx_crc_errors++; 689 dev_kfree_skb(h->priv->ule_skb); 690 691 return; 692 } 693 694 /* CRC32 verified OK. */ 695 696 /* CRC32 was OK, so remove it from skb. */ 697 h->priv->ule_skb->tail -= 4; 698 h->priv->ule_skb->len -= 4; 699 700 if (!h->priv->ule_dbit) { 701 if (dvb_net_ule_should_drop(h)) { 702 netdev_dbg(h->dev, 703 "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n", 704 h->priv->ule_skb->data, h->dev->dev_addr); 705 dev_kfree_skb(h->priv->ule_skb); 706 return; 707 } 708 709 skb_copy_from_linear_data(h->priv->ule_skb, dest_addr, 710 ETH_ALEN); 711 skb_pull(h->priv->ule_skb, ETH_ALEN); 712 } else { 713 /* dest_addr buffer is only valid if h->priv->ule_dbit == 0 */ 714 eth_zero_addr(dest_addr); 715 } 716 717 /* Handle ULE Extension Headers. */ 718 if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) { 719 /* There is an extension header. Handle it accordingly. */ 720 int l = handle_ule_extensions(h->priv); 721 722 if (l < 0) { 723 /* 724 * Mandatory extension header unknown or TEST SNDU. 725 * Drop it. 726 */ 727 728 // pr_warn("Dropping SNDU, extension headers.\n" ); 729 dev_kfree_skb(h->priv->ule_skb); 730 return; 731 } 732 skb_pull(h->priv->ule_skb, l); 733 } 734 735 /* 736 * Construct/assure correct ethernet header. 737 * Note: in bridged mode (h->priv->ule_bridged != 0) 738 * we already have the (original) ethernet 739 * header at the start of the payload (after 740 * optional dest. address and any extension 741 * headers). 742 */ 743 if (!h->priv->ule_bridged) { 744 skb_push(h->priv->ule_skb, ETH_HLEN); 745 h->ethh = (struct ethhdr *)h->priv->ule_skb->data; 746 memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN); 747 eth_zero_addr(h->ethh->h_source); 748 h->ethh->h_proto = htons(h->priv->ule_sndu_type); 749 } 750 /* else: skb is in correct state; nothing to do. */ 751 h->priv->ule_bridged = 0; 752 753 /* Stuff into kernel's protocol stack. */ 754 h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb, 755 h->dev); 756 /* 757 * If D-bit is set (i.e. destination MAC address not present), 758 * receive the packet anyhow. 759 */ 760 #if 0 761 if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST) 762 h->priv->ule_skb->pkt_type = PACKET_HOST; 763 #endif 764 h->dev->stats.rx_packets++; 765 h->dev->stats.rx_bytes += h->priv->ule_skb->len; 766 netif_rx(h->priv->ule_skb); 767 } 768 769 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len) 770 { 771 int ret; 772 struct dvb_net_ule_handle h = { 773 .dev = dev, 774 .priv = netdev_priv(dev), 775 .ethh = NULL, 776 .buf = buf, 777 .buf_len = buf_len, 778 .skipped = 0L, 779 .ts = NULL, 780 .ts_end = NULL, 781 .from_where = NULL, 782 .ts_remain = 0, 783 .how_much = 0, 784 .new_ts = 1, 785 .error = false, 786 }; 787 788 /* 789 * For all TS cells in current buffer. 790 * Appearently, we are called for every single TS cell. 791 */ 792 for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len; 793 h.ts < h.ts_end; /* no incr. */) { 794 if (h.new_ts) { 795 /* We are about to process a new TS cell. */ 796 if (dvb_net_ule_new_ts_cell(&h)) 797 continue; 798 } 799 800 /* Synchronize on PUSI, if required. */ 801 if (h.priv->need_pusi) { 802 if (dvb_net_ule_ts_pusi(&h)) 803 continue; 804 } 805 806 if (h.new_ts) { 807 if (dvb_net_ule_new_ts(&h)) 808 continue; 809 } 810 811 /* Check if new payload needs to be started. */ 812 if (h.priv->ule_skb == NULL) { 813 ret = dvb_net_ule_new_payload(&h); 814 if (ret < 0) 815 return; 816 if (ret) 817 continue; 818 } 819 820 /* Copy data into our current skb. */ 821 h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain); 822 skb_put_data(h.priv->ule_skb, h.from_where, h.how_much); 823 h.priv->ule_sndu_remain -= h.how_much; 824 h.ts_remain -= h.how_much; 825 h.from_where += h.how_much; 826 827 /* Check for complete payload. */ 828 if (h.priv->ule_sndu_remain <= 0) { 829 /* Check CRC32, we've got it in our skb already. */ 830 __be16 ulen = htons(h.priv->ule_sndu_len); 831 __be16 utype = htons(h.priv->ule_sndu_type); 832 const u8 *tail; 833 struct kvec iov[3] = { 834 { &ulen, sizeof ulen }, 835 { &utype, sizeof utype }, 836 { h.priv->ule_skb->data, 837 h.priv->ule_skb->len - 4 } 838 }; 839 u32 ule_crc = ~0L, expected_crc; 840 if (h.priv->ule_dbit) { 841 /* Set D-bit for CRC32 verification, 842 * if it was set originally. */ 843 ulen |= htons(0x8000); 844 } 845 846 ule_crc = iov_crc32(ule_crc, iov, 3); 847 tail = skb_tail_pointer(h.priv->ule_skb); 848 expected_crc = *(tail - 4) << 24 | 849 *(tail - 3) << 16 | 850 *(tail - 2) << 8 | 851 *(tail - 1); 852 853 dvb_net_ule_check_crc(&h, iov, ule_crc, expected_crc); 854 855 /* Prepare for next SNDU. */ 856 reset_ule(h.priv); 857 } 858 859 /* More data in current TS (look at the bytes following the CRC32)? */ 860 if (h.ts_remain >= 2 && *((unsigned short *)h.from_where) != 0xFFFF) { 861 /* Next ULE SNDU starts right there. */ 862 h.new_ts = 0; 863 h.priv->ule_skb = NULL; 864 h.priv->ule_sndu_type_1 = 0; 865 h.priv->ule_sndu_len = 0; 866 // pr_warn("More data in current TS: [%#x %#x %#x %#x]\n", 867 // *(h.from_where + 0), *(h.from_where + 1), 868 // *(h.from_where + 2), *(h.from_where + 3)); 869 // pr_warn("h.ts @ %p, stopped @ %p:\n", h.ts, h.from_where + 0); 870 // hexdump(h.ts, 188); 871 } else { 872 h.new_ts = 1; 873 h.ts += TS_SZ; 874 h.priv->ts_count++; 875 if (h.priv->ule_skb == NULL) { 876 h.priv->need_pusi = 1; 877 h.priv->ule_sndu_type_1 = 0; 878 h.priv->ule_sndu_len = 0; 879 } 880 } 881 } /* for all available TS cells */ 882 } 883 884 static int dvb_net_ts_callback(const u8 *buffer1, size_t buffer1_len, 885 const u8 *buffer2, size_t buffer2_len, 886 struct dmx_ts_feed *feed, 887 u32 *buffer_flags) 888 { 889 struct net_device *dev = feed->priv; 890 891 if (buffer2) 892 pr_warn("buffer2 not NULL: %p.\n", buffer2); 893 if (buffer1_len > 32768) 894 pr_warn("length > 32k: %zu.\n", buffer1_len); 895 /* pr_info("TS callback: %u bytes, %u TS cells @ %p.\n", 896 buffer1_len, buffer1_len / TS_SZ, buffer1); */ 897 dvb_net_ule(dev, buffer1, buffer1_len); 898 return 0; 899 } 900 901 902 static void dvb_net_sec(struct net_device *dev, 903 const u8 *pkt, int pkt_len) 904 { 905 u8 *eth; 906 struct sk_buff *skb; 907 struct net_device_stats *stats = &dev->stats; 908 int snap = 0; 909 910 /* note: pkt_len includes a 32bit checksum */ 911 if (pkt_len < 16) { 912 pr_warn("%s: IP/MPE packet length = %d too small.\n", 913 dev->name, pkt_len); 914 stats->rx_errors++; 915 stats->rx_length_errors++; 916 return; 917 } 918 /* it seems some ISPs manage to screw up here, so we have to 919 * relax the error checks... */ 920 #if 0 921 if ((pkt[5] & 0xfd) != 0xc1) { 922 /* drop scrambled or broken packets */ 923 #else 924 if ((pkt[5] & 0x3c) != 0x00) { 925 /* drop scrambled */ 926 #endif 927 stats->rx_errors++; 928 stats->rx_crc_errors++; 929 return; 930 } 931 if (pkt[5] & 0x02) { 932 /* handle LLC/SNAP, see rfc-1042 */ 933 if (pkt_len < 24 || memcmp(&pkt[12], "\xaa\xaa\x03\0\0\0", 6)) { 934 stats->rx_dropped++; 935 return; 936 } 937 snap = 8; 938 } 939 if (pkt[7]) { 940 /* FIXME: assemble datagram from multiple sections */ 941 stats->rx_errors++; 942 stats->rx_frame_errors++; 943 return; 944 } 945 946 /* we have 14 byte ethernet header (ip header follows); 947 * 12 byte MPE header; 4 byte checksum; + 2 byte alignment, 8 byte LLC/SNAP 948 */ 949 if (!(skb = dev_alloc_skb(pkt_len - 4 - 12 + 14 + 2 - snap))) { 950 //pr_notice("%s: Memory squeeze, dropping packet.\n", dev->name); 951 stats->rx_dropped++; 952 return; 953 } 954 skb_reserve(skb, 2); /* longword align L3 header */ 955 skb->dev = dev; 956 957 /* copy L3 payload */ 958 eth = skb_put(skb, pkt_len - 12 - 4 + 14 - snap); 959 memcpy(eth + 14, pkt + 12 + snap, pkt_len - 12 - 4 - snap); 960 961 /* create ethernet header: */ 962 eth[0]=pkt[0x0b]; 963 eth[1]=pkt[0x0a]; 964 eth[2]=pkt[0x09]; 965 eth[3]=pkt[0x08]; 966 eth[4]=pkt[0x04]; 967 eth[5]=pkt[0x03]; 968 969 eth[6]=eth[7]=eth[8]=eth[9]=eth[10]=eth[11]=0; 970 971 if (snap) { 972 eth[12] = pkt[18]; 973 eth[13] = pkt[19]; 974 } else { 975 /* protocol numbers are from rfc-1700 or 976 * http://www.iana.org/assignments/ethernet-numbers 977 */ 978 if (pkt[12] >> 4 == 6) { /* version field from IP header */ 979 eth[12] = 0x86; /* IPv6 */ 980 eth[13] = 0xdd; 981 } else { 982 eth[12] = 0x08; /* IPv4 */ 983 eth[13] = 0x00; 984 } 985 } 986 987 skb->protocol = dvb_net_eth_type_trans(skb, dev); 988 989 stats->rx_packets++; 990 stats->rx_bytes+=skb->len; 991 netif_rx(skb); 992 } 993 994 static int dvb_net_sec_callback(const u8 *buffer1, size_t buffer1_len, 995 const u8 *buffer2, size_t buffer2_len, 996 struct dmx_section_filter *filter, u32 *buffer_flags) 997 { 998 struct net_device *dev = filter->priv; 999 1000 /* 1001 * we rely on the DVB API definition where exactly one complete 1002 * section is delivered in buffer1 1003 */ 1004 dvb_net_sec (dev, buffer1, buffer1_len); 1005 return 0; 1006 } 1007 1008 static int dvb_net_tx(struct sk_buff *skb, struct net_device *dev) 1009 { 1010 dev_kfree_skb(skb); 1011 return NETDEV_TX_OK; 1012 } 1013 1014 static u8 mask_normal[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 1015 static u8 mask_allmulti[6]={0xff, 0xff, 0xff, 0x00, 0x00, 0x00}; 1016 static u8 mac_allmulti[6]={0x01, 0x00, 0x5e, 0x00, 0x00, 0x00}; 1017 static u8 mask_promisc[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 1018 1019 static int dvb_net_filter_sec_set(struct net_device *dev, 1020 struct dmx_section_filter **secfilter, 1021 u8 *mac, u8 *mac_mask) 1022 { 1023 struct dvb_net_priv *priv = netdev_priv(dev); 1024 int ret; 1025 1026 *secfilter=NULL; 1027 ret = priv->secfeed->allocate_filter(priv->secfeed, secfilter); 1028 if (ret<0) { 1029 pr_err("%s: could not get filter\n", dev->name); 1030 return ret; 1031 } 1032 1033 (*secfilter)->priv=(void *) dev; 1034 1035 memset((*secfilter)->filter_value, 0x00, DMX_MAX_FILTER_SIZE); 1036 memset((*secfilter)->filter_mask, 0x00, DMX_MAX_FILTER_SIZE); 1037 memset((*secfilter)->filter_mode, 0xff, DMX_MAX_FILTER_SIZE); 1038 1039 (*secfilter)->filter_value[0]=0x3e; 1040 (*secfilter)->filter_value[3]=mac[5]; 1041 (*secfilter)->filter_value[4]=mac[4]; 1042 (*secfilter)->filter_value[8]=mac[3]; 1043 (*secfilter)->filter_value[9]=mac[2]; 1044 (*secfilter)->filter_value[10]=mac[1]; 1045 (*secfilter)->filter_value[11]=mac[0]; 1046 1047 (*secfilter)->filter_mask[0] = 0xff; 1048 (*secfilter)->filter_mask[3] = mac_mask[5]; 1049 (*secfilter)->filter_mask[4] = mac_mask[4]; 1050 (*secfilter)->filter_mask[8] = mac_mask[3]; 1051 (*secfilter)->filter_mask[9] = mac_mask[2]; 1052 (*secfilter)->filter_mask[10] = mac_mask[1]; 1053 (*secfilter)->filter_mask[11]=mac_mask[0]; 1054 1055 netdev_dbg(dev, "filter mac=%pM mask=%pM\n", mac, mac_mask); 1056 1057 return 0; 1058 } 1059 1060 static int dvb_net_feed_start(struct net_device *dev) 1061 { 1062 int ret = 0, i; 1063 struct dvb_net_priv *priv = netdev_priv(dev); 1064 struct dmx_demux *demux = priv->demux; 1065 unsigned char *mac = (unsigned char *) dev->dev_addr; 1066 1067 netdev_dbg(dev, "rx_mode %i\n", priv->rx_mode); 1068 mutex_lock(&priv->mutex); 1069 if (priv->tsfeed || priv->secfeed || priv->secfilter || priv->multi_secfilter[0]) 1070 pr_err("%s: BUG %d\n", __func__, __LINE__); 1071 1072 priv->secfeed=NULL; 1073 priv->secfilter=NULL; 1074 priv->tsfeed = NULL; 1075 1076 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) { 1077 netdev_dbg(dev, "alloc secfeed\n"); 1078 ret=demux->allocate_section_feed(demux, &priv->secfeed, 1079 dvb_net_sec_callback); 1080 if (ret<0) { 1081 pr_err("%s: could not allocate section feed\n", 1082 dev->name); 1083 goto error; 1084 } 1085 1086 ret = priv->secfeed->set(priv->secfeed, priv->pid, 1); 1087 1088 if (ret<0) { 1089 pr_err("%s: could not set section feed\n", dev->name); 1090 priv->demux->release_section_feed(priv->demux, priv->secfeed); 1091 priv->secfeed=NULL; 1092 goto error; 1093 } 1094 1095 if (priv->rx_mode != RX_MODE_PROMISC) { 1096 netdev_dbg(dev, "set secfilter\n"); 1097 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_normal); 1098 } 1099 1100 switch (priv->rx_mode) { 1101 case RX_MODE_MULTI: 1102 for (i = 0; i < priv->multi_num; i++) { 1103 netdev_dbg(dev, "set multi_secfilter[%d]\n", i); 1104 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[i], 1105 priv->multi_macs[i], mask_normal); 1106 } 1107 break; 1108 case RX_MODE_ALL_MULTI: 1109 priv->multi_num=1; 1110 netdev_dbg(dev, "set multi_secfilter[0]\n"); 1111 dvb_net_filter_sec_set(dev, &priv->multi_secfilter[0], 1112 mac_allmulti, mask_allmulti); 1113 break; 1114 case RX_MODE_PROMISC: 1115 priv->multi_num=0; 1116 netdev_dbg(dev, "set secfilter\n"); 1117 dvb_net_filter_sec_set(dev, &priv->secfilter, mac, mask_promisc); 1118 break; 1119 } 1120 1121 netdev_dbg(dev, "start filtering\n"); 1122 priv->secfeed->start_filtering(priv->secfeed); 1123 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) { 1124 ktime_t timeout = ns_to_ktime(10 * NSEC_PER_MSEC); 1125 1126 /* we have payloads encapsulated in TS */ 1127 netdev_dbg(dev, "alloc tsfeed\n"); 1128 ret = demux->allocate_ts_feed(demux, &priv->tsfeed, dvb_net_ts_callback); 1129 if (ret < 0) { 1130 pr_err("%s: could not allocate ts feed\n", dev->name); 1131 goto error; 1132 } 1133 1134 /* Set netdevice pointer for ts decaps callback. */ 1135 priv->tsfeed->priv = (void *)dev; 1136 ret = priv->tsfeed->set(priv->tsfeed, 1137 priv->pid, /* pid */ 1138 TS_PACKET, /* type */ 1139 DMX_PES_OTHER, /* pes type */ 1140 timeout /* timeout */ 1141 ); 1142 1143 if (ret < 0) { 1144 pr_err("%s: could not set ts feed\n", dev->name); 1145 priv->demux->release_ts_feed(priv->demux, priv->tsfeed); 1146 priv->tsfeed = NULL; 1147 goto error; 1148 } 1149 1150 netdev_dbg(dev, "start filtering\n"); 1151 priv->tsfeed->start_filtering(priv->tsfeed); 1152 } else 1153 ret = -EINVAL; 1154 1155 error: 1156 mutex_unlock(&priv->mutex); 1157 return ret; 1158 } 1159 1160 static int dvb_net_feed_stop(struct net_device *dev) 1161 { 1162 struct dvb_net_priv *priv = netdev_priv(dev); 1163 int i, ret = 0; 1164 1165 mutex_lock(&priv->mutex); 1166 if (priv->feedtype == DVB_NET_FEEDTYPE_MPE) { 1167 if (priv->secfeed) { 1168 if (priv->secfeed->is_filtering) { 1169 netdev_dbg(dev, "stop secfeed\n"); 1170 priv->secfeed->stop_filtering(priv->secfeed); 1171 } 1172 1173 if (priv->secfilter) { 1174 netdev_dbg(dev, "release secfilter\n"); 1175 priv->secfeed->release_filter(priv->secfeed, 1176 priv->secfilter); 1177 priv->secfilter=NULL; 1178 } 1179 1180 for (i=0; i<priv->multi_num; i++) { 1181 if (priv->multi_secfilter[i]) { 1182 netdev_dbg(dev, "release multi_filter[%d]\n", 1183 i); 1184 priv->secfeed->release_filter(priv->secfeed, 1185 priv->multi_secfilter[i]); 1186 priv->multi_secfilter[i] = NULL; 1187 } 1188 } 1189 1190 priv->demux->release_section_feed(priv->demux, priv->secfeed); 1191 priv->secfeed = NULL; 1192 } else 1193 pr_err("%s: no feed to stop\n", dev->name); 1194 } else if (priv->feedtype == DVB_NET_FEEDTYPE_ULE) { 1195 if (priv->tsfeed) { 1196 if (priv->tsfeed->is_filtering) { 1197 netdev_dbg(dev, "stop tsfeed\n"); 1198 priv->tsfeed->stop_filtering(priv->tsfeed); 1199 } 1200 priv->demux->release_ts_feed(priv->demux, priv->tsfeed); 1201 priv->tsfeed = NULL; 1202 } 1203 else 1204 pr_err("%s: no ts feed to stop\n", dev->name); 1205 } else 1206 ret = -EINVAL; 1207 mutex_unlock(&priv->mutex); 1208 return ret; 1209 } 1210 1211 1212 static int dvb_set_mc_filter(struct net_device *dev, unsigned char *addr) 1213 { 1214 struct dvb_net_priv *priv = netdev_priv(dev); 1215 1216 if (priv->multi_num == DVB_NET_MULTICAST_MAX) 1217 return -ENOMEM; 1218 1219 memcpy(priv->multi_macs[priv->multi_num], addr, ETH_ALEN); 1220 1221 priv->multi_num++; 1222 return 0; 1223 } 1224 1225 1226 static void wq_set_multicast_list (struct work_struct *work) 1227 { 1228 struct dvb_net_priv *priv = 1229 container_of(work, struct dvb_net_priv, set_multicast_list_wq); 1230 struct net_device *dev = priv->net; 1231 1232 dvb_net_feed_stop(dev); 1233 priv->rx_mode = RX_MODE_UNI; 1234 netif_addr_lock_bh(dev); 1235 1236 if (dev->flags & IFF_PROMISC) { 1237 netdev_dbg(dev, "promiscuous mode\n"); 1238 priv->rx_mode = RX_MODE_PROMISC; 1239 } else if ((dev->flags & IFF_ALLMULTI)) { 1240 netdev_dbg(dev, "allmulti mode\n"); 1241 priv->rx_mode = RX_MODE_ALL_MULTI; 1242 } else if (!netdev_mc_empty(dev)) { 1243 struct netdev_hw_addr *ha; 1244 1245 netdev_dbg(dev, "set_mc_list, %d entries\n", 1246 netdev_mc_count(dev)); 1247 1248 priv->rx_mode = RX_MODE_MULTI; 1249 priv->multi_num = 0; 1250 1251 netdev_for_each_mc_addr(ha, dev) 1252 dvb_set_mc_filter(dev, ha->addr); 1253 } 1254 1255 netif_addr_unlock_bh(dev); 1256 dvb_net_feed_start(dev); 1257 } 1258 1259 1260 static void dvb_net_set_multicast_list (struct net_device *dev) 1261 { 1262 struct dvb_net_priv *priv = netdev_priv(dev); 1263 schedule_work(&priv->set_multicast_list_wq); 1264 } 1265 1266 1267 static void wq_restart_net_feed (struct work_struct *work) 1268 { 1269 struct dvb_net_priv *priv = 1270 container_of(work, struct dvb_net_priv, restart_net_feed_wq); 1271 struct net_device *dev = priv->net; 1272 1273 if (netif_running(dev)) { 1274 dvb_net_feed_stop(dev); 1275 dvb_net_feed_start(dev); 1276 } 1277 } 1278 1279 1280 static int dvb_net_set_mac (struct net_device *dev, void *p) 1281 { 1282 struct dvb_net_priv *priv = netdev_priv(dev); 1283 struct sockaddr *addr=p; 1284 1285 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); 1286 1287 if (netif_running(dev)) 1288 schedule_work(&priv->restart_net_feed_wq); 1289 1290 return 0; 1291 } 1292 1293 1294 static int dvb_net_open(struct net_device *dev) 1295 { 1296 struct dvb_net_priv *priv = netdev_priv(dev); 1297 1298 priv->in_use++; 1299 dvb_net_feed_start(dev); 1300 return 0; 1301 } 1302 1303 1304 static int dvb_net_stop(struct net_device *dev) 1305 { 1306 struct dvb_net_priv *priv = netdev_priv(dev); 1307 1308 priv->in_use--; 1309 return dvb_net_feed_stop(dev); 1310 } 1311 1312 static const struct header_ops dvb_header_ops = { 1313 .create = eth_header, 1314 .parse = eth_header_parse, 1315 }; 1316 1317 1318 static const struct net_device_ops dvb_netdev_ops = { 1319 .ndo_open = dvb_net_open, 1320 .ndo_stop = dvb_net_stop, 1321 .ndo_start_xmit = dvb_net_tx, 1322 .ndo_set_rx_mode = dvb_net_set_multicast_list, 1323 .ndo_set_mac_address = dvb_net_set_mac, 1324 .ndo_validate_addr = eth_validate_addr, 1325 }; 1326 1327 static void dvb_net_setup(struct net_device *dev) 1328 { 1329 ether_setup(dev); 1330 1331 dev->header_ops = &dvb_header_ops; 1332 dev->netdev_ops = &dvb_netdev_ops; 1333 dev->mtu = 4096; 1334 dev->max_mtu = 4096; 1335 1336 dev->flags |= IFF_NOARP; 1337 } 1338 1339 static int get_if(struct dvb_net *dvbnet) 1340 { 1341 int i; 1342 1343 for (i=0; i<DVB_NET_DEVICES_MAX; i++) 1344 if (!dvbnet->state[i]) 1345 break; 1346 1347 if (i == DVB_NET_DEVICES_MAX) 1348 return -1; 1349 1350 dvbnet->state[i]=1; 1351 return i; 1352 } 1353 1354 static int dvb_net_add_if(struct dvb_net *dvbnet, u16 pid, u8 feedtype) 1355 { 1356 struct net_device *net; 1357 struct dvb_net_priv *priv; 1358 int result; 1359 int if_num; 1360 1361 if (feedtype != DVB_NET_FEEDTYPE_MPE && feedtype != DVB_NET_FEEDTYPE_ULE) 1362 return -EINVAL; 1363 if ((if_num = get_if(dvbnet)) < 0) 1364 return -EINVAL; 1365 1366 net = alloc_netdev(sizeof(struct dvb_net_priv), "dvb", 1367 NET_NAME_UNKNOWN, dvb_net_setup); 1368 if (!net) 1369 return -ENOMEM; 1370 1371 if (dvbnet->dvbdev->id) 1372 snprintf(net->name, IFNAMSIZ, "dvb%d%u%d", 1373 dvbnet->dvbdev->adapter->num, dvbnet->dvbdev->id, if_num); 1374 else 1375 /* compatibility fix to keep dvb0_0 format */ 1376 snprintf(net->name, IFNAMSIZ, "dvb%d_%d", 1377 dvbnet->dvbdev->adapter->num, if_num); 1378 1379 net->addr_len = 6; 1380 memcpy(net->dev_addr, dvbnet->dvbdev->adapter->proposed_mac, 6); 1381 1382 dvbnet->device[if_num] = net; 1383 1384 priv = netdev_priv(net); 1385 priv->net = net; 1386 priv->demux = dvbnet->demux; 1387 priv->pid = pid; 1388 priv->rx_mode = RX_MODE_UNI; 1389 priv->need_pusi = 1; 1390 priv->tscc = 0; 1391 priv->feedtype = feedtype; 1392 reset_ule(priv); 1393 1394 INIT_WORK(&priv->set_multicast_list_wq, wq_set_multicast_list); 1395 INIT_WORK(&priv->restart_net_feed_wq, wq_restart_net_feed); 1396 mutex_init(&priv->mutex); 1397 1398 net->base_addr = pid; 1399 1400 if ((result = register_netdev(net)) < 0) { 1401 dvbnet->device[if_num] = NULL; 1402 free_netdev(net); 1403 return result; 1404 } 1405 pr_info("created network interface %s\n", net->name); 1406 1407 return if_num; 1408 } 1409 1410 static int dvb_net_remove_if(struct dvb_net *dvbnet, unsigned long num) 1411 { 1412 struct net_device *net = dvbnet->device[num]; 1413 struct dvb_net_priv *priv; 1414 1415 if (!dvbnet->state[num]) 1416 return -EINVAL; 1417 priv = netdev_priv(net); 1418 if (priv->in_use) 1419 return -EBUSY; 1420 1421 dvb_net_stop(net); 1422 flush_work(&priv->set_multicast_list_wq); 1423 flush_work(&priv->restart_net_feed_wq); 1424 pr_info("removed network interface %s\n", net->name); 1425 unregister_netdev(net); 1426 dvbnet->state[num]=0; 1427 dvbnet->device[num] = NULL; 1428 free_netdev(net); 1429 1430 return 0; 1431 } 1432 1433 static int dvb_net_do_ioctl(struct file *file, 1434 unsigned int cmd, void *parg) 1435 { 1436 struct dvb_device *dvbdev = file->private_data; 1437 struct dvb_net *dvbnet = dvbdev->priv; 1438 int ret = 0; 1439 1440 if (((file->f_flags&O_ACCMODE)==O_RDONLY)) 1441 return -EPERM; 1442 1443 if (mutex_lock_interruptible(&dvbnet->ioctl_mutex)) 1444 return -ERESTARTSYS; 1445 1446 switch (cmd) { 1447 case NET_ADD_IF: 1448 { 1449 struct dvb_net_if *dvbnetif = parg; 1450 int result; 1451 1452 if (!capable(CAP_SYS_ADMIN)) { 1453 ret = -EPERM; 1454 goto ioctl_error; 1455 } 1456 1457 if (!try_module_get(dvbdev->adapter->module)) { 1458 ret = -EPERM; 1459 goto ioctl_error; 1460 } 1461 1462 result=dvb_net_add_if(dvbnet, dvbnetif->pid, dvbnetif->feedtype); 1463 if (result<0) { 1464 module_put(dvbdev->adapter->module); 1465 ret = result; 1466 goto ioctl_error; 1467 } 1468 dvbnetif->if_num=result; 1469 break; 1470 } 1471 case NET_GET_IF: 1472 { 1473 struct net_device *netdev; 1474 struct dvb_net_priv *priv_data; 1475 struct dvb_net_if *dvbnetif = parg; 1476 1477 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX || 1478 !dvbnet->state[dvbnetif->if_num]) { 1479 ret = -EINVAL; 1480 goto ioctl_error; 1481 } 1482 1483 netdev = dvbnet->device[dvbnetif->if_num]; 1484 1485 priv_data = netdev_priv(netdev); 1486 dvbnetif->pid=priv_data->pid; 1487 dvbnetif->feedtype=priv_data->feedtype; 1488 break; 1489 } 1490 case NET_REMOVE_IF: 1491 { 1492 if (!capable(CAP_SYS_ADMIN)) { 1493 ret = -EPERM; 1494 goto ioctl_error; 1495 } 1496 if ((unsigned long) parg >= DVB_NET_DEVICES_MAX) { 1497 ret = -EINVAL; 1498 goto ioctl_error; 1499 } 1500 ret = dvb_net_remove_if(dvbnet, (unsigned long) parg); 1501 if (!ret) 1502 module_put(dvbdev->adapter->module); 1503 break; 1504 } 1505 1506 /* binary compatibility cruft */ 1507 case __NET_ADD_IF_OLD: 1508 { 1509 struct __dvb_net_if_old *dvbnetif = parg; 1510 int result; 1511 1512 if (!capable(CAP_SYS_ADMIN)) { 1513 ret = -EPERM; 1514 goto ioctl_error; 1515 } 1516 1517 if (!try_module_get(dvbdev->adapter->module)) { 1518 ret = -EPERM; 1519 goto ioctl_error; 1520 } 1521 1522 result=dvb_net_add_if(dvbnet, dvbnetif->pid, DVB_NET_FEEDTYPE_MPE); 1523 if (result<0) { 1524 module_put(dvbdev->adapter->module); 1525 ret = result; 1526 goto ioctl_error; 1527 } 1528 dvbnetif->if_num=result; 1529 break; 1530 } 1531 case __NET_GET_IF_OLD: 1532 { 1533 struct net_device *netdev; 1534 struct dvb_net_priv *priv_data; 1535 struct __dvb_net_if_old *dvbnetif = parg; 1536 1537 if (dvbnetif->if_num >= DVB_NET_DEVICES_MAX || 1538 !dvbnet->state[dvbnetif->if_num]) { 1539 ret = -EINVAL; 1540 goto ioctl_error; 1541 } 1542 1543 netdev = dvbnet->device[dvbnetif->if_num]; 1544 1545 priv_data = netdev_priv(netdev); 1546 dvbnetif->pid=priv_data->pid; 1547 break; 1548 } 1549 default: 1550 ret = -ENOTTY; 1551 break; 1552 } 1553 1554 ioctl_error: 1555 mutex_unlock(&dvbnet->ioctl_mutex); 1556 return ret; 1557 } 1558 1559 static long dvb_net_ioctl(struct file *file, 1560 unsigned int cmd, unsigned long arg) 1561 { 1562 return dvb_usercopy(file, cmd, arg, dvb_net_do_ioctl); 1563 } 1564 1565 static int dvb_net_close(struct inode *inode, struct file *file) 1566 { 1567 struct dvb_device *dvbdev = file->private_data; 1568 struct dvb_net *dvbnet = dvbdev->priv; 1569 1570 dvb_generic_release(inode, file); 1571 1572 if(dvbdev->users == 1 && dvbnet->exit == 1) 1573 wake_up(&dvbdev->wait_queue); 1574 return 0; 1575 } 1576 1577 1578 static const struct file_operations dvb_net_fops = { 1579 .owner = THIS_MODULE, 1580 .unlocked_ioctl = dvb_net_ioctl, 1581 .open = dvb_generic_open, 1582 .release = dvb_net_close, 1583 .llseek = noop_llseek, 1584 }; 1585 1586 static const struct dvb_device dvbdev_net = { 1587 .priv = NULL, 1588 .users = 1, 1589 .writers = 1, 1590 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 1591 .name = "dvb-net", 1592 #endif 1593 .fops = &dvb_net_fops, 1594 }; 1595 1596 void dvb_net_release (struct dvb_net *dvbnet) 1597 { 1598 int i; 1599 1600 dvbnet->exit = 1; 1601 if (dvbnet->dvbdev->users < 1) 1602 wait_event(dvbnet->dvbdev->wait_queue, 1603 dvbnet->dvbdev->users==1); 1604 1605 dvb_unregister_device(dvbnet->dvbdev); 1606 1607 for (i=0; i<DVB_NET_DEVICES_MAX; i++) { 1608 if (!dvbnet->state[i]) 1609 continue; 1610 dvb_net_remove_if(dvbnet, i); 1611 } 1612 } 1613 EXPORT_SYMBOL(dvb_net_release); 1614 1615 1616 int dvb_net_init (struct dvb_adapter *adap, struct dvb_net *dvbnet, 1617 struct dmx_demux *dmx) 1618 { 1619 int i; 1620 1621 mutex_init(&dvbnet->ioctl_mutex); 1622 dvbnet->demux = dmx; 1623 1624 for (i=0; i<DVB_NET_DEVICES_MAX; i++) 1625 dvbnet->state[i] = 0; 1626 1627 return dvb_register_device(adap, &dvbnet->dvbdev, &dvbdev_net, 1628 dvbnet, DVB_DEVICE_NET, 0); 1629 } 1630 EXPORT_SYMBOL(dvb_net_init); 1631