📄 dev.c
字号:
350 }
351
352
353 /* Find an interface that can handle addresses for a certain address. */
354 struct device * dev_check(unsigned long addr)
355 {
356 struct device *dev;
357
358 for (dev = dev_base; dev; dev = dev->next) {
359 if (!(dev->flags & IFF_UP))
360 continue;
361 if (!(dev->flags & IFF_POINTOPOINT))
362 continue;
363 if (addr != dev->pa_dstaddr)
364 continue;
365 return dev;
366 }
367 for (dev = dev_base; dev; dev = dev->next) {
368 if (!(dev->flags & IFF_UP))
369 continue;
370 if (dev->flags & IFF_POINTOPOINT)
371 continue;
372 if (dev->pa_mask & (addr ^ dev->pa_addr))
373 continue;
374 return dev;
375 }
376 return NULL;
377 }
378
379
380 /* Prepare an interface for use. */
381 int
382 dev_open(struct device *dev)
383 {
384 int ret = 0;
385
386 if (dev->open)
387 ret = dev->open(dev);
388 if (ret == 0)
389 dev->flags |= (IFF_UP | IFF_RUNNING);
390
391 return(ret);
392 }
393
394
395 /* Completely shutdown an interface. */
396 int
397 dev_close(struct device *dev)
398 {
399 if (dev->flags != 0) {
400 int ct=0;
401 dev->flags = 0;
402 if (dev->stop)
403 dev->stop(dev);
404 rt_flush(dev);
405 dev->pa_addr = 0;
406 dev->pa_dstaddr = 0;
407 dev->pa_brdaddr = 0;
408 dev->pa_mask = 0;
409 /* Purge any queued packets when we down the link */
410 while(ct<DEV_NUMBUFFS)
411 {
412 struct sk_buff *skb;
413 while((skb=skb_dequeue(&dev->buffs[ct]))!=NULL)
414 if(skb->free)
415 kfree_skb(skb,FREE_WRITE);
416 ct++;
417 }
418 }
419
420 return(0);
421 }
422
423
424 /* Send (or queue for sending) a packet. */
425 void
426 dev_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
427 {
428 int where = 0; /* used to say if the packet should go */
429 /* at the front or the back of the */
430 /* queue. */
431
432 DPRINTF((DBG_DEV, "dev_queue_xmit(skb=%X, dev=%X, pri = %d)\n",
433 skb, dev, pri));
434
435 if (dev == NULL) {
436 printk("dev.c: dev_queue_xmit: dev = NULL\n");
437 return;
438 }
439
440 IS_SKB(skb);
441
442 skb->dev = dev;
443 if (skb->next != NULL) {
444 /* Make sure we haven't missed an interrupt. */
445 dev->hard_start_xmit(NULL, dev);
446 return;
447 }
448
449 if (pri < 0) {
450 pri = -pri-1;
451 where = 1;
452 }
453
454 if (pri >= DEV_NUMBUFFS) {
455 printk("bad priority in dev_queue_xmit.\n");
456 pri = 1;
457 }
458
459 if (dev->hard_start_xmit(skb, dev) == 0) {
460 return;
461 }
462
463 /* Put skb into a bidirectional circular linked list. */
464 DPRINTF((DBG_DEV, "dev_queue_xmit dev->buffs[%d]=%X\n",
465 pri, dev->buffs[pri]));
466
467 /* Interrupts should already be cleared by hard_start_xmit. */
468 cli();
469 skb->magic = DEV_QUEUE_MAGIC;
470 if(where)
471 skb_queue_head(&dev->buffs[pri],skb);
472 else
473 skb_queue_tail(&dev->buffs[pri],skb);
474 skb->magic = DEV_QUEUE_MAGIC;
475 sti();
476 }
477
478 /*
479 * Receive a packet from a device driver and queue it for the upper
480 * (protocol) levels. It always succeeds.
481 */
482 void
483 netif_rx(struct sk_buff *skb)
484 {
485 /* Set any necessary flags. */
486 skb->sk = NULL;
487 skb->free = 1;
488
489 /* and add it to the "backlog" queue. */
490 IS_SKB(skb);
491 skb_queue_tail(&backlog,skb);
492
493 /* If any packet arrived, mark it for processing. */
494 if (backlog != NULL) mark_bh(INET_BH);
495
496 return;
497 }
498
499
500 /*
501 * The old interface to fetch a packet from a device driver.
502 * This function is the base level entry point for all drivers that
503 * want to send a packet to the upper (protocol) levels. It takes
504 * care of de-multiplexing the packet to the various modules based
505 * on their protocol ID.
506 *
507 * Return values: 1 <- exit I can't do any more
508 * 0 <- feed me more (i.e. "done", "OK").
509 */
510 int
511 dev_rint(unsigned char *buff, long len, int flags, struct device *dev)
512 {
513 static int dropping = 0;
514 struct sk_buff *skb = NULL;
515 unsigned char *to;
516 int amount, left;
517 int len2;
518
519 if (dev == NULL || buff == NULL || len <= 0) return(1);
520 if (flags & IN_SKBUFF) {
521 skb = (struct sk_buff *) buff;
522 } else {
523 if (dropping) {
524 if (backlog != NULL)
525 return(1);
526 printk("INET: dev_rint: no longer dropping packets.\n");
527 dropping = 0;
528 }
529
530 skb = alloc_skb(sizeof(*skb) + len, GFP_ATOMIC);
531 if (skb == NULL) {
532 printk("dev_rint: packet dropped on %s (no memory) !\n",
533 dev->name);
534 dropping = 1;
535 return(1);
536 }
537 skb->mem_len = sizeof(*skb) + len;
538 skb->mem_addr = (struct sk_buff *) skb;
539
540 /* First we copy the packet into a buffer, and save it for later. */
541 to = skb->data;
542 left = len;
543 len2 = len;
544 while (len2 > 0) {
545 amount = min(len2, (unsigned long) dev->rmem_end -
546 (unsigned long) buff);
547 memcpy(to, buff, amount);
548 len2 -= amount;
549 left -= amount;
550 buff += amount;
551 to += amount;
552 if ((unsigned long) buff == dev->rmem_end)
553 buff = (unsigned char *) dev->rmem_start;
554 }
555 }
556 skb->len = len;
557 skb->dev = dev;
558 skb->free = 1;
559
560 netif_rx(skb);
561 /* OK, all done. */
562 return(0);
563 }
564
565
566 /* This routine causes all interfaces to try to send some data. */
567 void
568 dev_transmit(void)
569 {
570 struct device *dev;
571
572 for (dev = dev_base; dev != NULL; dev = dev->next) {
573 if (!dev->tbusy) {
574 dev_tint(dev);
575 }
576 }
577 }
578
579 static volatile char in_bh = 0;
580
581 int in_inet_bh() /* Used by timer.c */
582 {
583 return(in_bh==0?0:1);
584 }
585
586 /*
587 * This function gets called periodically, to see if we can
588 * process any data that came in from some interface.
589 *
590 */
591 void
592 inet_bh(void *tmp)
593 {
594 struct sk_buff *skb;
595 struct packet_type *ptype;
596 unsigned short type;
597 unsigned char flag = 0;
598 int nitcount;
599
600 /* Atomically check and mark our BUSY state. */
601 if (set_bit(1, (void*)&in_bh))
602 return;
603
604 /* Can we send anything now? */
605 dev_transmit();
606
607 /* Any data left to process? */
608 while((skb=skb_dequeue(&backlog))!=NULL)
609 {
610 nitcount=dev_nit;
611 flag=0;
612 sti();
613 /*
614 * Bump the pointer to the next structure.
615 * This assumes that the basic 'skb' pointer points to
616 * the MAC header, if any (as indicated by its "length"
617 * field). Take care now!
618 */
619 skb->h.raw = skb->data + skb->dev->hard_header_len;
620 skb->len -= skb->dev->hard_header_len;
621
622 /*
623 * Fetch the packet protocol ID. This is also quite ugly, as
624 * it depends on the protocol driver (the interface itself) to
625 * know what the type is, or where to get it from. The Ethernet
626 * interfaces fetch the ID from the two bytes in the Ethernet MAC
627 * header (the h_proto field in struct ethhdr), but drivers like
628 * SLIP and PLIP have no alternative but to force the type to be
629 * IP or something like that. Sigh- FvK
630 */
631 type = skb->dev->type_trans(skb, skb->dev);
632
633 /*
634 * We got a packet ID. Now loop over the "known protocols"
635 * table (which is actually a linked list, but this will
636 * change soon if I get my way- FvK), and forward the packet
637 * to anyone who wants it.
638 */
639 for (ptype = ptype_base; ptype != NULL; ptype = ptype->next) {
640 if (ptype->type == type || ptype->type == NET16(ETH_P_ALL)) {
641 struct sk_buff *skb2;
642
643 if (ptype->type==NET16(ETH_P_ALL))
644 nitcount--;
645 if (ptype->copy || nitcount) { /* copy if we need to */
646 skb2 = alloc_skb(skb->mem_len, GFP_ATOMIC);
647 if (skb2 == NULL)
648 continue;
649 memcpy(skb2, (const void *) skb, skb->mem_len);
650 skb2->mem_addr = skb2;
651 skb2->h.raw = (unsigned char *)(
652 (unsigned long) skb2 +
653 (unsigned long) skb->h.raw -
654 (unsigned long) skb
655 );
656 skb2->free = 1;
657 } else {
658 skb2 = skb;
659 }
660
661 /* This used to be in the 'else' part, but then
662 * we don't have this flag set when we get a
663 * protocol that *does* require copying... -FvK
664 */
665 flag = 1;
666
667 /* Kick the protocol handler. */
668 ptype->func(skb2, skb->dev, ptype);
669 }
670 }
671
672 /*
673 * That's odd. We got an unknown packet. Who's using
674 * stuff like Novell or Amoeba on this network??
675 */
676 if (!flag) {
677 DPRINTF((DBG_DEV,
678 "INET: unknown packet type 0x%04X (ignored)\n", type));
679 skb->sk = NULL;
680 kfree_skb(skb, FREE_WRITE);
681 }
682
683 /* Again, see if we can transmit anything now. */
684 dev_transmit();
685 cli();
686 }
687 in_bh = 0;
688 sti();
689 dev_transmit();
690 }
691
692
693 /*
694 * This routine is called when an device driver (i.e. an
695 * interface) is * ready to transmit a packet.
696 */
697
698 void dev_tint(struct device *dev)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -