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