ipdemux.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2008 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file net/ipdemux.c
  36. * \brief Supports IP handlers, registered at runtime.
  37. *
  38. * \verbatim
  39. * $Id: ipdemux.c 3686 2011-12-04 14:20:38Z haraldkipp $
  40. * \endverbatim
  41. */
  42. #include <sys/types.h>
  43. #include <sys/heap.h>
  44. #include <netinet/if_ether.h>
  45. #include <netinet/ip.h>
  46. #include <stdlib.h>
  47. #include <memdebug.h>
  48. /*!
  49. * \addtogroup xgIP
  50. */
  51. /*@{*/
  52. /*!
  53. * \brief Linked list of registered Internet protocols.
  54. */
  55. typedef struct _INET_PROTOCOLS INET_PROTOCOLS;
  56. struct _INET_PROTOCOLS {
  57. INET_PROTOCOLS *inet_next;
  58. uint8_t inet_prot;
  59. int (*inet_input)(NUTDEVICE *, NETBUF *);
  60. };
  61. static INET_PROTOCOLS *in_prots;
  62. /*!
  63. * \brief Forward Ethernet frame to registered handler.
  64. *
  65. * \param dev Identifies the device that received the frame.
  66. * \param nb Pointer to a network buffer structure containing
  67. * the Ethernet frame.
  68. *
  69. * \return 0 if the frame will be processed by a handler. Otherwise
  70. * -1 is returned.
  71. */
  72. static int NutIpDemux(NUTDEVICE * dev, NETBUF * nb)
  73. {
  74. INET_PROTOCOLS *inetp;
  75. uint8_t prot = ((IPHDR *)nb->nb_nw.vp)->ip_p;
  76. for (inetp = in_prots; inetp; inetp = inetp->inet_next) {
  77. if (prot == inetp->inet_prot && inetp->inet_input) {
  78. if ((*inetp->inet_input) (dev, nb) == 0) {
  79. return 0;
  80. }
  81. }
  82. }
  83. return -1;
  84. }
  85. /*!
  86. * \brief Register an additional Ethernet protocol handler.
  87. *
  88. * The specified mask will be applied on the type field of incoming
  89. * frames before compared with the type that had been specified for
  90. * the handler. If more than one handler is registered for an incoming
  91. * Ethernet frame, the handler being registered last is called first.
  92. *
  93. * Each handler should return 0 if it processed the frame, in which
  94. * case it is also assumed, that the handler releases the memory
  95. * allocated for the NETBUF. Otherwise the handler should return -1,
  96. * in which case the frame is passed to the next handler that fits.
  97. *
  98. * The protocol types ETHERTYPE_IP and ETHERTYPE_ARP are pre-registered
  99. * by default and processed by internal handlers after all registered
  100. * handlers for that frame rejected processing. This allows to install
  101. * filters on the Ethernet level.
  102. *
  103. * \param type Ethernet protocol type processed by this handler.
  104. * \param mask Ethernet protocol type mask.
  105. * \param hdlr Pointer to the protocol handler function. If NULL,
  106. * the handler will be temporarily disabled.
  107. *
  108. * \return 0 on success, -1 otherwise.
  109. */
  110. int NutRegisterIpHandler(uint8_t prot, int (*hdlr)(NUTDEVICE *, NETBUF *))
  111. {
  112. INET_PROTOCOLS *inetp;
  113. /* Check existing registrations. */
  114. for (inetp = in_prots; inetp; inetp = inetp->inet_next) {
  115. if (inetp->inet_prot == prot) {
  116. /* Found one. */
  117. break;
  118. }
  119. }
  120. if (inetp == NULL) {
  121. /* No existing entry. Allocate a new one. */
  122. inetp = calloc(1, sizeof(INET_PROTOCOLS));
  123. if (inetp == NULL) {
  124. return -1;
  125. }
  126. /* Set protocol type of our new entry. */
  127. inetp->inet_prot = prot;
  128. if (in_prots == NULL) {
  129. /* This is the first registration. Set the list root ... */
  130. in_prots = inetp;
  131. /* ... and enable our demultiplexer. */
  132. ip_demux = NutIpDemux;
  133. } else {
  134. /* Not the first registration. Insert new handler at the top. */
  135. inetp->inet_next = in_prots;
  136. in_prots = inetp;
  137. }
  138. }
  139. /* Finally set the handler function pointer of the new or existing
  140. ** entry. */
  141. inetp->inet_input = hdlr;
  142. return 0;
  143. }
  144. /*@}*/