Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[media] dvb-net: split the logic at dvb_net_ule() into other functions

This function is too big and too complex, making really hard
to understand what's there.

Split it into sub-routines, in order to make it easier to be
understood, and to allow gcc to better parse it.

As a bonus, it gets rid of a goto in the middle of a routine.

Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>

+467 -361
+467 -361
drivers/media/dvb-core/dvb_net.c
··· 332 332 #endif 333 333 }; 334 334 335 + static int dvb_net_ule_new_ts_cell(struct dvb_net_ule_handle *h) 336 + { 337 + /* We are about to process a new TS cell. */ 338 + 339 + #ifdef ULE_DEBUG 340 + if (h->ule_where >= &h->ule_hist[100*TS_SZ]) 341 + h->ule_where = h->ule_hist; 342 + memcpy(h->ule_where, h->ts, TS_SZ); 343 + if (h->ule_dump) { 344 + hexdump(h->ule_where, TS_SZ); 345 + h->ule_dump = 0; 346 + } 347 + h->ule_where += TS_SZ; 348 + #endif 349 + 350 + /* 351 + * Check TS h->error conditions: sync_byte, transport_error_indicator, 352 + * scrambling_control . 353 + */ 354 + if ((h->ts[0] != TS_SYNC) || (h->ts[1] & TS_TEI) || 355 + ((h->ts[3] & TS_SC) != 0)) { 356 + pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n", 357 + h->priv->ts_count, h->ts[0], 358 + (h->ts[1] & TS_TEI) >> 7, 359 + (h->ts[3] & TS_SC) >> 6); 360 + 361 + /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 362 + if (h->priv->ule_skb) { 363 + dev_kfree_skb(h->priv->ule_skb); 364 + /* Prepare for next SNDU. */ 365 + h->dev->stats.rx_errors++; 366 + h->dev->stats.rx_frame_errors++; 367 + } 368 + reset_ule(h->priv); 369 + h->priv->need_pusi = 1; 370 + 371 + /* Continue with next TS cell. */ 372 + h->ts += TS_SZ; 373 + h->priv->ts_count++; 374 + return 1; 375 + } 376 + 377 + h->ts_remain = 184; 378 + h->from_where = h->ts + 4; 379 + 380 + return 0; 381 + } 382 + 383 + static int dvb_net_ule_ts_pusi(struct dvb_net_ule_handle *h) 384 + { 385 + if (h->ts[1] & TS_PUSI) { 386 + /* Find beginning of first ULE SNDU in current TS cell. */ 387 + /* Synchronize continuity counter. */ 388 + h->priv->tscc = h->ts[3] & 0x0F; 389 + /* There is a pointer field here. */ 390 + if (h->ts[4] > h->ts_remain) { 391 + pr_err("%lu: Invalid ULE packet (pointer field %d)\n", 392 + h->priv->ts_count, h->ts[4]); 393 + h->ts += TS_SZ; 394 + h->priv->ts_count++; 395 + return 1; 396 + } 397 + /* Skip to destination of pointer field. */ 398 + h->from_where = &h->ts[5] + h->ts[4]; 399 + h->ts_remain -= 1 + h->ts[4]; 400 + h->skipped = 0; 401 + } else { 402 + h->skipped++; 403 + h->ts += TS_SZ; 404 + h->priv->ts_count++; 405 + return 1; 406 + } 407 + 408 + return 0; 409 + } 410 + 411 + static int dvb_net_ule_new_ts(struct dvb_net_ule_handle *h) 412 + { 413 + /* Check continuity counter. */ 414 + if ((h->ts[3] & 0x0F) == h->priv->tscc) 415 + h->priv->tscc = (h->priv->tscc + 1) & 0x0F; 416 + else { 417 + /* TS discontinuity handling: */ 418 + pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n", 419 + h->priv->ts_count, h->ts[3] & 0x0F, 420 + h->priv->tscc); 421 + /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 422 + if (h->priv->ule_skb) { 423 + dev_kfree_skb(h->priv->ule_skb); 424 + /* Prepare for next SNDU. */ 425 + // reset_ule(h->priv); moved to below. 426 + h->dev->stats.rx_errors++; 427 + h->dev->stats.rx_frame_errors++; 428 + } 429 + reset_ule(h->priv); 430 + /* skip to next PUSI. */ 431 + h->priv->need_pusi = 1; 432 + return 1; 433 + } 434 + /* 435 + * If we still have an incomplete payload, but PUSI is 436 + * set; some TS cells are missing. 437 + * This is only possible here, if we missed exactly 16 TS 438 + * cells (continuity counter wrap). 439 + */ 440 + if (h->ts[1] & TS_PUSI) { 441 + if (!h->priv->need_pusi) { 442 + if (!(*h->from_where < (h->ts_remain-1)) || 443 + *h->from_where != h->priv->ule_sndu_remain) { 444 + /* 445 + * Pointer field is invalid. 446 + * Drop this TS cell and any started ULE SNDU. 447 + */ 448 + pr_warn("%lu: Invalid pointer field: %u.\n", 449 + h->priv->ts_count, 450 + *h->from_where); 451 + 452 + /* 453 + * Drop partly decoded SNDU, reset state, 454 + * resync on PUSI. 455 + */ 456 + if (h->priv->ule_skb) { 457 + h->error = true; 458 + dev_kfree_skb(h->priv->ule_skb); 459 + } 460 + 461 + if (h->error || h->priv->ule_sndu_remain) { 462 + h->dev->stats.rx_errors++; 463 + h->dev->stats.rx_frame_errors++; 464 + h->error = false; 465 + } 466 + 467 + reset_ule(h->priv); 468 + h->priv->need_pusi = 1; 469 + return 1; 470 + } 471 + /* 472 + * Skip pointer field (we're processing a 473 + * packed payload). 474 + */ 475 + h->from_where += 1; 476 + h->ts_remain -= 1; 477 + } else 478 + h->priv->need_pusi = 0; 479 + 480 + if (h->priv->ule_sndu_remain > 183) { 481 + /* 482 + * Current SNDU lacks more data than there 483 + * could be available in the current TS cell. 484 + */ 485 + h->dev->stats.rx_errors++; 486 + h->dev->stats.rx_length_errors++; 487 + pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h->ts_remain %d). Flushing incomplete payload.\n", 488 + h->priv->ts_count, 489 + h->priv->ule_sndu_remain, 490 + h->ts[4], h->ts_remain); 491 + dev_kfree_skb(h->priv->ule_skb); 492 + /* Prepare for next SNDU. */ 493 + reset_ule(h->priv); 494 + /* 495 + * Resync: go to where pointer field points to: 496 + * start of next ULE SNDU. 497 + */ 498 + h->from_where += h->ts[4]; 499 + h->ts_remain -= h->ts[4]; 500 + } 501 + } 502 + return 0; 503 + } 504 + 505 + 506 + /* 507 + * Start a new payload with skb. 508 + * Find ULE header. It is only guaranteed that the 509 + * length field (2 bytes) is contained in the current 510 + * TS. 511 + * Check h.ts_remain has to be >= 2 here. 512 + */ 513 + static int dvb_net_ule_new_payload(struct dvb_net_ule_handle *h) 514 + { 515 + if (h->ts_remain < 2) { 516 + pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n", 517 + h->ts_remain); 518 + h->priv->ule_sndu_len = 0; 519 + h->priv->need_pusi = 1; 520 + h->ts += TS_SZ; 521 + return 1; 522 + } 523 + 524 + if (!h->priv->ule_sndu_len) { 525 + /* Got at least two bytes, thus extrace the SNDU length. */ 526 + h->priv->ule_sndu_len = h->from_where[0] << 8 | 527 + h->from_where[1]; 528 + if (h->priv->ule_sndu_len & 0x8000) { 529 + /* D-Bit is set: no dest mac present. */ 530 + h->priv->ule_sndu_len &= 0x7FFF; 531 + h->priv->ule_dbit = 1; 532 + } else 533 + h->priv->ule_dbit = 0; 534 + 535 + if (h->priv->ule_sndu_len < 5) { 536 + pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n", 537 + h->priv->ts_count, 538 + h->priv->ule_sndu_len); 539 + h->dev->stats.rx_errors++; 540 + h->dev->stats.rx_length_errors++; 541 + h->priv->ule_sndu_len = 0; 542 + h->priv->need_pusi = 1; 543 + h->new_ts = 1; 544 + h->ts += TS_SZ; 545 + h->priv->ts_count++; 546 + return 1; 547 + } 548 + h->ts_remain -= 2; /* consume the 2 bytes SNDU length. */ 549 + h->from_where += 2; 550 + } 551 + 552 + h->priv->ule_sndu_remain = h->priv->ule_sndu_len + 2; 553 + /* 554 + * State of current TS: 555 + * h->ts_remain (remaining bytes in the current TS cell) 556 + * 0 ule_type is not available now, we need the next TS cell 557 + * 1 the first byte of the ule_type is present 558 + * >=2 full ULE header present, maybe some payload data as well. 559 + */ 560 + switch (h->ts_remain) { 561 + case 1: 562 + h->priv->ule_sndu_remain--; 563 + h->priv->ule_sndu_type = h->from_where[0] << 8; 564 + 565 + /* first byte of ule_type is set. */ 566 + h->priv->ule_sndu_type_1 = 1; 567 + h->ts_remain -= 1; 568 + h->from_where += 1; 569 + /* fallthrough */ 570 + case 0: 571 + h->new_ts = 1; 572 + h->ts += TS_SZ; 573 + h->priv->ts_count++; 574 + return 1; 575 + 576 + default: /* complete ULE header is present in current TS. */ 577 + /* Extract ULE type field. */ 578 + if (h->priv->ule_sndu_type_1) { 579 + h->priv->ule_sndu_type_1 = 0; 580 + h->priv->ule_sndu_type |= h->from_where[0]; 581 + h->from_where += 1; /* points to payload start. */ 582 + h->ts_remain -= 1; 583 + } else { 584 + /* Complete type is present in new TS. */ 585 + h->priv->ule_sndu_type = h->from_where[0] << 8 | 586 + h->from_where[1]; 587 + h->from_where += 2; /* points to payload start. */ 588 + h->ts_remain -= 2; 589 + } 590 + break; 591 + } 592 + 593 + /* 594 + * Allocate the skb (decoder target buffer) with the correct size, 595 + * as follows: 596 + * 597 + * prepare for the largest case: bridged SNDU with MAC address 598 + * (dbit = 0). 599 + */ 600 + h->priv->ule_skb = dev_alloc_skb(h->priv->ule_sndu_len + 601 + ETH_HLEN + ETH_ALEN); 602 + if (!h->priv->ule_skb) { 603 + pr_notice("%s: Memory squeeze, dropping packet.\n", 604 + h->dev->name); 605 + h->dev->stats.rx_dropped++; 606 + return -1; 607 + } 608 + 609 + /* This includes the CRC32 _and_ dest mac, if !dbit. */ 610 + h->priv->ule_sndu_remain = h->priv->ule_sndu_len; 611 + h->priv->ule_skb->dev = h->dev; 612 + /* 613 + * Leave space for Ethernet or bridged SNDU header 614 + * (eth hdr plus one MAC addr). 615 + */ 616 + skb_reserve(h->priv->ule_skb, ETH_HLEN + ETH_ALEN); 617 + 618 + return 0; 619 + } 620 + 621 + 622 + static int dvb_net_ule_should_drop(struct dvb_net_ule_handle *h) 623 + { 624 + static const u8 bc_addr[ETH_ALEN] = { [0 ... ETH_ALEN - 1] = 0xff }; 625 + 626 + /* 627 + * The destination MAC address is the next data in the skb. It comes 628 + * before any extension headers. 629 + * 630 + * Check if the payload of this SNDU should be passed up the stack. 631 + */ 632 + if (h->priv->rx_mode == RX_MODE_PROMISC) 633 + return 0; 634 + 635 + if (h->priv->ule_skb->data[0] & 0x01) { 636 + /* multicast or broadcast */ 637 + if (!ether_addr_equal(h->priv->ule_skb->data, bc_addr)) { 638 + /* multicast */ 639 + if (h->priv->rx_mode == RX_MODE_MULTI) { 640 + int i; 641 + 642 + for (i = 0; i < h->priv->multi_num && 643 + !ether_addr_equal(h->priv->ule_skb->data, 644 + h->priv->multi_macs[i]); 645 + i++) 646 + ; 647 + if (i == h->priv->multi_num) 648 + return 1; 649 + } else if (h->priv->rx_mode != RX_MODE_ALL_MULTI) 650 + return 1; /* no broadcast; */ 651 + /* 652 + * else: 653 + * all multicast mode: accept all multicast packets 654 + */ 655 + } 656 + /* else: broadcast */ 657 + } else if (!ether_addr_equal(h->priv->ule_skb->data, h->dev->dev_addr)) 658 + return 1; 659 + 660 + return 0; 661 + } 662 + 663 + 664 + static void dvb_net_ule_check_crc(struct dvb_net_ule_handle *h, 665 + u32 ule_crc, u32 expected_crc) 666 + { 667 + u8 dest_addr[ETH_ALEN]; 668 + 669 + if (ule_crc != expected_crc) { 670 + pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, ts_remain %d, next 2: %x.\n", 671 + h->priv->ts_count, ule_crc, expected_crc, 672 + h->priv->ule_sndu_len, h->priv->ule_sndu_type, 673 + h->ts_remain, 674 + h->ts_remain > 2 ? 675 + *(unsigned short *)h->from_where : 0); 676 + 677 + #ifdef ULE_DEBUG 678 + hexdump(iov[0].iov_base, iov[0].iov_len); 679 + hexdump(iov[1].iov_base, iov[1].iov_len); 680 + hexdump(iov[2].iov_base, iov[2].iov_len); 681 + 682 + if (h->ule_where == h->ule_hist) { 683 + hexdump(&h->ule_hist[98*TS_SZ], TS_SZ); 684 + hexdump(&h->ule_hist[99*TS_SZ], TS_SZ); 685 + } else if (h->ule_where == &h->ule_hist[TS_SZ]) { 686 + hexdump(&h->ule_hist[99*TS_SZ], TS_SZ); 687 + hexdump(h->ule_hist, TS_SZ); 688 + } else { 689 + hexdump(h->ule_where - TS_SZ - TS_SZ, TS_SZ); 690 + hexdump(h->ule_where - TS_SZ, TS_SZ); 691 + } 692 + h->ule_dump = 1; 693 + #endif 694 + 695 + h->dev->stats.rx_errors++; 696 + h->dev->stats.rx_crc_errors++; 697 + dev_kfree_skb(h->priv->ule_skb); 698 + 699 + return; 700 + } 701 + 702 + /* CRC32 verified OK. */ 703 + 704 + /* CRC32 was OK, so remove it from skb. */ 705 + h->priv->ule_skb->tail -= 4; 706 + h->priv->ule_skb->len -= 4; 707 + 708 + if (!h->priv->ule_dbit) { 709 + if (dvb_net_ule_should_drop(h)) { 710 + #ifdef ULE_DEBUG 711 + netdev_dbg(h->dev, 712 + "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h->dev addr: %pM\n", 713 + h->priv->ule_skb->data, h->dev->dev_addr); 714 + #endif 715 + dev_kfree_skb(h->priv->ule_skb); 716 + return; 717 + } 718 + 719 + skb_copy_from_linear_data(h->priv->ule_skb, dest_addr, 720 + ETH_ALEN); 721 + skb_pull(h->priv->ule_skb, ETH_ALEN); 722 + } 723 + 724 + /* Handle ULE Extension Headers. */ 725 + if (h->priv->ule_sndu_type < ETH_P_802_3_MIN) { 726 + /* There is an extension header. Handle it accordingly. */ 727 + int l = handle_ule_extensions(h->priv); 728 + 729 + if (l < 0) { 730 + /* 731 + * Mandatory extension header unknown or TEST SNDU. 732 + * Drop it. 733 + */ 734 + 735 + // pr_warn("Dropping SNDU, extension headers.\n" ); 736 + dev_kfree_skb(h->priv->ule_skb); 737 + return; 738 + } 739 + skb_pull(h->priv->ule_skb, l); 740 + } 741 + 742 + /* 743 + * Construct/assure correct ethernet header. 744 + * Note: in bridged mode (h->priv->ule_bridged != 0) 745 + * we already have the (original) ethernet 746 + * header at the start of the payload (after 747 + * optional dest. address and any extension 748 + * headers). 749 + */ 750 + if (!h->priv->ule_bridged) { 751 + skb_push(h->priv->ule_skb, ETH_HLEN); 752 + h->ethh = (struct ethhdr *)h->priv->ule_skb->data; 753 + if (!h->priv->ule_dbit) { 754 + /* 755 + * dest_addr buffer is only valid if 756 + * h->priv->ule_dbit == 0 757 + */ 758 + memcpy(h->ethh->h_dest, dest_addr, ETH_ALEN); 759 + eth_zero_addr(h->ethh->h_source); 760 + } else /* zeroize source and dest */ 761 + memset(h->ethh, 0, ETH_ALEN * 2); 762 + 763 + h->ethh->h_proto = htons(h->priv->ule_sndu_type); 764 + } 765 + /* else: skb is in correct state; nothing to do. */ 766 + h->priv->ule_bridged = 0; 767 + 768 + /* Stuff into kernel's protocol stack. */ 769 + h->priv->ule_skb->protocol = dvb_net_eth_type_trans(h->priv->ule_skb, 770 + h->dev); 771 + /* 772 + * If D-bit is set (i.e. destination MAC address not present), 773 + * receive the packet anyhow. 774 + */ 775 + #if 0 776 + if (h->priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST) 777 + h->priv->ule_skb->pkt_type = PACKET_HOST; 778 + #endif 779 + h->dev->stats.rx_packets++; 780 + h->dev->stats.rx_bytes += h->priv->ule_skb->len; 781 + netif_rx(h->priv->ule_skb); 782 + } 783 + 335 784 static void dvb_net_ule(struct net_device *dev, const u8 *buf, size_t buf_len) 336 785 { 786 + int ret; 337 787 struct dvb_net_ule_handle h = { 338 788 .dev = dev, 339 789 .buf = buf, ··· 802 352 #endif 803 353 }; 804 354 805 - /* For all TS cells in current buffer. 355 + /* 356 + * For all TS cells in current buffer. 806 357 * Appearently, we are called for every single TS cell. 807 358 */ 808 - for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len; h.ts < h.ts_end; /* no incr. */ ) { 359 + for (h.ts = h.buf, h.ts_end = h.buf + h.buf_len; 360 + h.ts < h.ts_end; /* no incr. */) { 809 361 if (h.new_ts) { 810 362 /* We are about to process a new TS cell. */ 811 - 812 - #ifdef ULE_DEBUG 813 - if (h.ule_where >= &h.ule_hist[100*TS_SZ]) h.ule_where = h.ule_hist; 814 - memcpy( h.ule_where, h.ts, TS_SZ ); 815 - if (h.ule_dump) { 816 - hexdump( h.ule_where, TS_SZ ); 817 - h.ule_dump = 0; 818 - } 819 - h.ule_where += TS_SZ; 820 - #endif 821 - 822 - /* Check TS h.error conditions: sync_byte, transport_error_indicator, scrambling_control . */ 823 - if ((h.ts[0] != TS_SYNC) || (h.ts[1] & TS_TEI) || ((h.ts[3] & TS_SC) != 0)) { 824 - pr_warn("%lu: Invalid TS cell: SYNC %#x, TEI %u, SC %#x.\n", 825 - h.priv->ts_count, h.ts[0], 826 - (h.ts[1] & TS_TEI) >> 7, 827 - (h.ts[3] & TS_SC) >> 6); 828 - 829 - /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 830 - if (h.priv->ule_skb) { 831 - dev_kfree_skb( h.priv->ule_skb ); 832 - /* Prepare for next SNDU. */ 833 - h.dev->stats.rx_errors++; 834 - h.dev->stats.rx_frame_errors++; 835 - } 836 - reset_ule(h.priv); 837 - h.priv->need_pusi = 1; 838 - 839 - /* Continue with next TS cell. */ 840 - h.ts += TS_SZ; 841 - h.priv->ts_count++; 363 + if (dvb_net_ule_new_ts_cell(&h)) 842 364 continue; 843 - } 844 - 845 - h.ts_remain = 184; 846 - h.from_where = h.ts + 4; 847 365 } 366 + 848 367 /* Synchronize on PUSI, if required. */ 849 368 if (h.priv->need_pusi) { 850 - if (h.ts[1] & TS_PUSI) { 851 - /* Find beginning of first ULE SNDU in current TS cell. */ 852 - /* Synchronize continuity counter. */ 853 - h.priv->tscc = h.ts[3] & 0x0F; 854 - /* There is a pointer field here. */ 855 - if (h.ts[4] > h.ts_remain) { 856 - pr_err("%lu: Invalid ULE packet (pointer field %d)\n", 857 - h.priv->ts_count, h.ts[4]); 858 - h.ts += TS_SZ; 859 - h.priv->ts_count++; 860 - continue; 861 - } 862 - /* Skip to destination of pointer field. */ 863 - h.from_where = &h.ts[5] + h.ts[4]; 864 - h.ts_remain -= 1 + h.ts[4]; 865 - h.skipped = 0; 866 - } else { 867 - h.skipped++; 868 - h.ts += TS_SZ; 869 - h.priv->ts_count++; 369 + if (dvb_net_ule_ts_pusi(&h)) 870 370 continue; 871 - } 872 371 } 873 372 874 373 if (h.new_ts) { 875 - /* Check continuity counter. */ 876 - if ((h.ts[3] & 0x0F) == h.priv->tscc) 877 - h.priv->tscc = (h.priv->tscc + 1) & 0x0F; 878 - else { 879 - /* TS discontinuity handling: */ 880 - pr_warn("%lu: TS discontinuity: got %#x, expected %#x.\n", 881 - h.priv->ts_count, h.ts[3] & 0x0F, 882 - h.priv->tscc); 883 - /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 884 - if (h.priv->ule_skb) { 885 - dev_kfree_skb( h.priv->ule_skb ); 886 - /* Prepare for next SNDU. */ 887 - // reset_ule(h.priv); moved to below. 888 - h.dev->stats.rx_errors++; 889 - h.dev->stats.rx_frame_errors++; 890 - } 891 - reset_ule(h.priv); 892 - /* skip to next PUSI. */ 893 - h.priv->need_pusi = 1; 374 + if (dvb_net_ule_new_ts(&h)) 894 375 continue; 895 - } 896 - /* If we still have an incomplete payload, but PUSI is 897 - * set; some TS cells are missing. 898 - * This is only possible here, if we missed exactly 16 TS 899 - * cells (continuity counter wrap). */ 900 - if (h.ts[1] & TS_PUSI) { 901 - if (! h.priv->need_pusi) { 902 - if (!(*h.from_where < (h.ts_remain-1)) || *h.from_where != h.priv->ule_sndu_remain) { 903 - /* Pointer field is invalid. Drop this TS cell and any started ULE SNDU. */ 904 - pr_warn("%lu: Invalid pointer field: %u.\n", 905 - h.priv->ts_count, 906 - *h.from_where); 907 - 908 - /* Drop partly decoded SNDU, reset state, resync on PUSI. */ 909 - if (h.priv->ule_skb) { 910 - h.error = true; 911 - dev_kfree_skb(h.priv->ule_skb); 912 - } 913 - 914 - if (h.error || h.priv->ule_sndu_remain) { 915 - h.dev->stats.rx_errors++; 916 - h.dev->stats.rx_frame_errors++; 917 - h.error = false; 918 - } 919 - 920 - reset_ule(h.priv); 921 - h.priv->need_pusi = 1; 922 - continue; 923 - } 924 - /* Skip pointer field (we're processing a 925 - * packed payload). */ 926 - h.from_where += 1; 927 - h.ts_remain -= 1; 928 - } else 929 - h.priv->need_pusi = 0; 930 - 931 - if (h.priv->ule_sndu_remain > 183) { 932 - /* Current SNDU lacks more data than there could be available in the 933 - * current TS cell. */ 934 - h.dev->stats.rx_errors++; 935 - h.dev->stats.rx_length_errors++; 936 - pr_warn("%lu: Expected %d more SNDU bytes, but got PUSI (pf %d, h.ts_remain %d). Flushing incomplete payload.\n", 937 - h.priv->ts_count, 938 - h.priv->ule_sndu_remain, 939 - h.ts[4], h.ts_remain); 940 - dev_kfree_skb(h.priv->ule_skb); 941 - /* Prepare for next SNDU. */ 942 - reset_ule(h.priv); 943 - /* Resync: go to where pointer field points to: start of next ULE SNDU. */ 944 - h.from_where += h.ts[4]; 945 - h.ts_remain -= h.ts[4]; 946 - } 947 - } 948 376 } 949 377 950 378 /* Check if new payload needs to be started. */ 951 379 if (h.priv->ule_skb == NULL) { 952 - /* Start a new payload with skb. 953 - * Find ULE header. It is only guaranteed that the 954 - * length field (2 bytes) is contained in the current 955 - * TS. 956 - * Check h.ts_remain has to be >= 2 here. */ 957 - if (h.ts_remain < 2) { 958 - pr_warn("Invalid payload packing: only %d bytes left in TS. Resyncing.\n", 959 - h.ts_remain); 960 - h.priv->ule_sndu_len = 0; 961 - h.priv->need_pusi = 1; 962 - h.ts += TS_SZ; 963 - continue; 964 - } 965 - 966 - if (! h.priv->ule_sndu_len) { 967 - /* Got at least two bytes, thus extrace the SNDU length. */ 968 - h.priv->ule_sndu_len = h.from_where[0] << 8 | h.from_where[1]; 969 - if (h.priv->ule_sndu_len & 0x8000) { 970 - /* D-Bit is set: no dest mac present. */ 971 - h.priv->ule_sndu_len &= 0x7FFF; 972 - h.priv->ule_dbit = 1; 973 - } else 974 - h.priv->ule_dbit = 0; 975 - 976 - if (h.priv->ule_sndu_len < 5) { 977 - pr_warn("%lu: Invalid ULE SNDU length %u. Resyncing.\n", 978 - h.priv->ts_count, 979 - h.priv->ule_sndu_len); 980 - h.dev->stats.rx_errors++; 981 - h.dev->stats.rx_length_errors++; 982 - h.priv->ule_sndu_len = 0; 983 - h.priv->need_pusi = 1; 984 - h.new_ts = 1; 985 - h.ts += TS_SZ; 986 - h.priv->ts_count++; 987 - continue; 988 - } 989 - h.ts_remain -= 2; /* consume the 2 bytes SNDU length. */ 990 - h.from_where += 2; 991 - } 992 - 993 - h.priv->ule_sndu_remain = h.priv->ule_sndu_len + 2; 994 - /* 995 - * State of current TS: 996 - * h.ts_remain (remaining bytes in the current TS cell) 997 - * 0 ule_type is not available now, we need the next TS cell 998 - * 1 the first byte of the ule_type is present 999 - * >=2 full ULE header present, maybe some payload data as well. 1000 - */ 1001 - switch (h.ts_remain) { 1002 - case 1: 1003 - h.priv->ule_sndu_remain--; 1004 - h.priv->ule_sndu_type = h.from_where[0] << 8; 1005 - h.priv->ule_sndu_type_1 = 1; /* first byte of ule_type is set. */ 1006 - h.ts_remain -= 1; h.from_where += 1; 1007 - /* Continue w/ next TS. */ 1008 - case 0: 1009 - h.new_ts = 1; 1010 - h.ts += TS_SZ; 1011 - h.priv->ts_count++; 1012 - continue; 1013 - 1014 - default: /* complete ULE header is present in current TS. */ 1015 - /* Extract ULE type field. */ 1016 - if (h.priv->ule_sndu_type_1) { 1017 - h.priv->ule_sndu_type_1 = 0; 1018 - h.priv->ule_sndu_type |= h.from_where[0]; 1019 - h.from_where += 1; /* points to payload start. */ 1020 - h.ts_remain -= 1; 1021 - } else { 1022 - /* Complete type is present in new TS. */ 1023 - h.priv->ule_sndu_type = h.from_where[0] << 8 | h.from_where[1]; 1024 - h.from_where += 2; /* points to payload start. */ 1025 - h.ts_remain -= 2; 1026 - } 1027 - break; 1028 - } 1029 - 1030 - /* Allocate the skb (decoder target buffer) with the correct size, as follows: 1031 - * prepare for the largest case: bridged SNDU with MAC address (dbit = 0). */ 1032 - h.priv->ule_skb = dev_alloc_skb( h.priv->ule_sndu_len + ETH_HLEN + ETH_ALEN ); 1033 - if (h.priv->ule_skb == NULL) { 1034 - pr_notice("%s: Memory squeeze, dropping packet.\n", 1035 - h.dev->name); 1036 - h.dev->stats.rx_dropped++; 380 + ret = dvb_net_ule_new_payload(&h); 381 + if (ret < 0) 1037 382 return; 1038 - } 1039 - 1040 - /* This includes the CRC32 _and_ dest mac, if !dbit. */ 1041 - h.priv->ule_sndu_remain = h.priv->ule_sndu_len; 1042 - h.priv->ule_skb->dev = h.dev; 1043 - /* Leave space for Ethernet or bridged SNDU header (eth hdr plus one MAC addr). */ 1044 - skb_reserve( h.priv->ule_skb, ETH_HLEN + ETH_ALEN ); 383 + if (ret) 384 + continue; 1045 385 } 1046 386 1047 387 /* Copy data into our current skb. */ 1048 388 h.how_much = min(h.priv->ule_sndu_remain, (int)h.ts_remain); 1049 - memcpy(skb_put(h.priv->ule_skb, h.how_much), h.from_where, h.how_much); 389 + memcpy(skb_put(h.priv->ule_skb, h.how_much), 390 + h.from_where, h.how_much); 1050 391 h.priv->ule_sndu_remain -= h.how_much; 1051 392 h.ts_remain -= h.how_much; 1052 393 h.from_where += h.how_much; ··· 851 610 struct kvec iov[3] = { 852 611 { &ulen, sizeof ulen }, 853 612 { &utype, sizeof utype }, 854 - { h.priv->ule_skb->data, h.priv->ule_skb->len - 4 } 613 + { h.priv->ule_skb->data, 614 + h.priv->ule_skb->len - 4 } 855 615 }; 856 616 u32 ule_crc = ~0L, expected_crc; 857 617 if (h.priv->ule_dbit) { ··· 867 625 *(tail - 3) << 16 | 868 626 *(tail - 2) << 8 | 869 627 *(tail - 1); 870 - if (ule_crc != expected_crc) { 871 - pr_warn("%lu: CRC32 check FAILED: %08x / %08x, SNDU len %d type %#x, h.ts_remain %d, next 2: %x.\n", 872 - h.priv->ts_count, ule_crc, expected_crc, 873 - h.priv->ule_sndu_len, h.priv->ule_sndu_type, 874 - h.ts_remain, 875 - h.ts_remain > 2 ? *(unsigned short *)h.from_where : 0); 876 628 877 - #ifdef ULE_DEBUG 878 - hexdump( iov[0].iov_base, iov[0].iov_len ); 879 - hexdump( iov[1].iov_base, iov[1].iov_len ); 880 - hexdump( iov[2].iov_base, iov[2].iov_len ); 629 + dvb_net_ule_check_crc(&h, ule_crc, expected_crc); 881 630 882 - if (h.ule_where == h.ule_hist) { 883 - hexdump( &h.ule_hist[98*TS_SZ], TS_SZ ); 884 - hexdump( &h.ule_hist[99*TS_SZ], TS_SZ ); 885 - } else if (h.ule_where == &h.ule_hist[TS_SZ]) { 886 - hexdump( &h.ule_hist[99*TS_SZ], TS_SZ ); 887 - hexdump( h.ule_hist, TS_SZ ); 888 - } else { 889 - hexdump( h.ule_where - TS_SZ - TS_SZ, TS_SZ ); 890 - hexdump( h.ule_where - TS_SZ, TS_SZ ); 891 - } 892 - h.ule_dump = 1; 893 - #endif 894 - 895 - h.dev->stats.rx_errors++; 896 - h.dev->stats.rx_crc_errors++; 897 - dev_kfree_skb(h.priv->ule_skb); 898 - } else { 899 - /* CRC32 verified OK. */ 900 - u8 dest_addr[ETH_ALEN]; 901 - static const u8 bc_addr[ETH_ALEN] = 902 - { [ 0 ... ETH_ALEN-1] = 0xff }; 903 - 904 - /* CRC32 was OK. Remove it from skb. */ 905 - h.priv->ule_skb->tail -= 4; 906 - h.priv->ule_skb->len -= 4; 907 - 908 - if (!h.priv->ule_dbit) { 909 - /* 910 - * The destination MAC address is the 911 - * next data in the skb. It comes 912 - * before any extension headers. 913 - * 914 - * Check if the payload of this SNDU 915 - * should be passed up the stack. 916 - */ 917 - register int drop = 0; 918 - if (h.priv->rx_mode != RX_MODE_PROMISC) { 919 - if (h.priv->ule_skb->data[0] & 0x01) { 920 - /* multicast or broadcast */ 921 - if (!ether_addr_equal(h.priv->ule_skb->data, bc_addr)) { 922 - /* multicast */ 923 - if (h.priv->rx_mode == RX_MODE_MULTI) { 924 - int i; 925 - for(i = 0; i < h.priv->multi_num && 926 - !ether_addr_equal(h.priv->ule_skb->data, 927 - h.priv->multi_macs[i]); i++) 928 - ; 929 - if (i == h.priv->multi_num) 930 - drop = 1; 931 - } else if (h.priv->rx_mode != RX_MODE_ALL_MULTI) 932 - drop = 1; /* no broadcast; */ 933 - /* else: all multicast mode: accept all multicast packets */ 934 - } 935 - /* else: broadcast */ 936 - } 937 - else if (!ether_addr_equal(h.priv->ule_skb->data, h.dev->dev_addr)) 938 - drop = 1; 939 - /* else: destination address matches the MAC address of our receiver device */ 940 - } 941 - /* else: promiscuous mode; pass everything up the stack */ 942 - 943 - if (drop) { 944 - #ifdef ULE_DEBUG 945 - netdev_dbg(h.dev, "Dropping SNDU: MAC destination address does not match: dest addr: %pM, h.dev addr: %pM\n", 946 - h.priv->ule_skb->data, h.dev->dev_addr); 947 - #endif 948 - dev_kfree_skb(h.priv->ule_skb); 949 - goto sndu_done; 950 - } 951 - else 952 - { 953 - skb_copy_from_linear_data(h.priv->ule_skb, 954 - dest_addr, 955 - ETH_ALEN); 956 - skb_pull(h.priv->ule_skb, ETH_ALEN); 957 - } 958 - } 959 - 960 - /* Handle ULE Extension Headers. */ 961 - if (h.priv->ule_sndu_type < ETH_P_802_3_MIN) { 962 - /* There is an extension header. Handle it accordingly. */ 963 - int l = handle_ule_extensions(h.priv); 964 - if (l < 0) { 965 - /* Mandatory extension header unknown or TEST SNDU. Drop it. */ 966 - // pr_warn("Dropping SNDU, extension headers.\n" ); 967 - dev_kfree_skb(h.priv->ule_skb); 968 - goto sndu_done; 969 - } 970 - skb_pull(h.priv->ule_skb, l); 971 - } 972 - 973 - /* 974 - * Construct/assure correct ethernet header. 975 - * Note: in bridged mode (h.priv->ule_bridged != 976 - * 0) we already have the (original) ethernet 977 - * header at the start of the payload (after 978 - * optional dest. address and any extension 979 - * headers). 980 - */ 981 - 982 - if (!h.priv->ule_bridged) { 983 - skb_push(h.priv->ule_skb, ETH_HLEN); 984 - h.ethh = (struct ethhdr *)h.priv->ule_skb->data; 985 - if (!h.priv->ule_dbit) { 986 - /* dest_addr buffer is only valid if h.priv->ule_dbit == 0 */ 987 - memcpy(h.ethh->h_dest, dest_addr, ETH_ALEN); 988 - eth_zero_addr(h.ethh->h_source); 989 - } 990 - else /* zeroize source and dest */ 991 - memset( h.ethh, 0, ETH_ALEN*2 ); 992 - 993 - h.ethh->h_proto = htons(h.priv->ule_sndu_type); 994 - } 995 - /* else: skb is in correct state; nothing to do. */ 996 - h.priv->ule_bridged = 0; 997 - 998 - /* Stuff into kernel's protocol stack. */ 999 - h.priv->ule_skb->protocol = dvb_net_eth_type_trans(h.priv->ule_skb, h.dev); 1000 - /* If D-bit is set (i.e. destination MAC address not present), 1001 - * receive the packet anyhow. */ 1002 - /* if (h.priv->ule_dbit && skb->pkt_type == PACKET_OTHERHOST) 1003 - h.priv->ule_skb->pkt_type = PACKET_HOST; */ 1004 - h.dev->stats.rx_packets++; 1005 - h.dev->stats.rx_bytes += h.priv->ule_skb->len; 1006 - netif_rx(h.priv->ule_skb); 1007 - } 1008 - sndu_done: 1009 631 /* Prepare for next SNDU. */ 1010 632 reset_ule(h.priv); 1011 633 }