ethin.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2008 by egnite GmbH
  3. * Copyright (C) 2001-2003 by egnite Software GmbH
  4. * Copyright (c) 1993 by Digital Equipment Corporation
  5. * Copyright (c) 1983, 1993 by The Regents of the University of California
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the copyright holders nor the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  32. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * For additional information see http://www.ethernut.de/
  36. */
  37. /*!
  38. * \file net/ethin.c
  39. * \brief Ethernet input functions.
  40. *
  41. * \verbatim
  42. * $Id: ethin.c 4407 2012-08-05 17:06:22Z haraldkipp $
  43. * \endverbatim
  44. */
  45. #include <sys/types.h>
  46. #include <netinet/if_ether.h>
  47. #include <netinet/ip.h>
  48. #include <netinet/in.h>
  49. #include <net/ether.h>
  50. /*!
  51. * \addtogroup xgEthernet
  52. */
  53. /*@{*/
  54. /*!
  55. * \brief Pointer to an optional demultiplexer.
  56. *
  57. * This pointer will be set on the first call to NutRegisterEthHandler().
  58. */
  59. int (*ether_demux) (NUTDEVICE *, NETBUF *);
  60. /*!
  61. * \brief Handle incoming Ethernet frames.
  62. *
  63. * Splits the Ethernet frame into the data link and the
  64. * network part. Then the frame is routed to the proper
  65. * handler, based on the type field in the Ethernet header.
  66. *
  67. * If the frame neither contains an IP nor an ARP type telegram and if
  68. * no registered handler exists or accepts the frame, then it is silently
  69. * discarded.
  70. *
  71. * \note This routine is called by the device driver on
  72. * incoming Ethernet packets. Applications typically do
  73. * not call this function.
  74. *
  75. * \param dev Identifies the device that received the frame.
  76. * \param nb Pointer to a network buffer structure containing
  77. * the Ethernet frame.
  78. */
  79. void NutEtherInput(NUTDEVICE * dev, NETBUF * nb)
  80. {
  81. ETHERHDR *eh;
  82. /*
  83. * Split the Ethernet frame.
  84. */
  85. eh = (ETHERHDR *) nb->nb_dl.vp;
  86. nb->nb_nw.vp = eh + 1;
  87. nb->nb_nw.sz = nb->nb_dl.sz - sizeof(ETHERHDR);
  88. nb->nb_dl.sz = sizeof(ETHERHDR);
  89. /* Route frame to the correct handler. Process registered handlers
  90. ** first, IP next and ARP last. */
  91. if (ether_demux == NULL || (*ether_demux) (dev, nb)) {
  92. switch (ntohs(eh->ether_type)) {
  93. case ETHERTYPE_IP:
  94. NutIpInput(dev, nb);
  95. break;
  96. case ETHERTYPE_ARP:
  97. NutArpInput(dev, nb);
  98. break;
  99. default:
  100. /* No handler found. Silently discard the frame. */
  101. NutNetBufFree(nb);
  102. #ifdef NUT_PERFMON
  103. {
  104. IFNET *nif = (IFNET *) dev->dev_icb;
  105. nif->if_in_unknown_protos++;
  106. }
  107. #endif
  108. break;
  109. }
  110. }
  111. }
  112. /*@}*/