icmpin.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file net/icmpin.c
  38. * \brief ICMP input functions.
  39. *
  40. * \verbatim
  41. * $Id: icmpin.c 5079 2013-03-21 19:13:56Z olereinhardt $
  42. * \endverbatim
  43. */
  44. #include <netinet/ip_icmp.h>
  45. #include <netinet/ipcsum.h>
  46. #include <netinet/icmp.h>
  47. #include <sys/socket.h>
  48. #include <netinet/tcp.h>
  49. #include <netinet/udp.h>
  50. #include <netinet/in.h>
  51. #include <errno.h>
  52. /*!
  53. * \addtogroup xgICMP
  54. */
  55. /*@{*/
  56. /*!
  57. * \brief Translation table from icmp error code to errno.
  58. */
  59. static const int icmp_code2errno[16] =
  60. {
  61. ENETUNREACH,
  62. EHOSTUNREACH,
  63. ENOPROTOOPT,
  64. ECONNREFUSED,
  65. EMSGSIZE,
  66. EOPNOTSUPP,
  67. ENETUNREACH,
  68. EHOSTDOWN,
  69. ENETUNREACH,
  70. ENETUNREACH,
  71. EHOSTUNREACH,
  72. ENETUNREACH,
  73. EHOSTUNREACH,
  74. EHOSTUNREACH,
  75. EHOSTUNREACH,
  76. EHOSTUNREACH,
  77. };
  78. /*
  79. * Send out ICMP echo response.
  80. */
  81. static int NutIcmpReflect(NUTDEVICE * dev, uint8_t type, NETBUF * nb)
  82. {
  83. IPHDR *ip;
  84. uint32_t dest;
  85. IFNET *nif;
  86. ip = nb->nb_nw.vp;
  87. dest = ip->ip_src;
  88. nif = dev->dev_icb;
  89. ip->ip_src = nif->if_local_ip;
  90. ip->ip_ttl = MAXTTL;
  91. return NutIcmpOutput(type, dest, nb);
  92. }
  93. /*
  94. * Process incoming ICMP messages for destination unreachable.
  95. */
  96. static int NutIcmpUnreach(NETBUF * nb, int icmp_code)
  97. {
  98. IPHDR *ih;
  99. if (nb->nb_ap.sz < sizeof(IPHDR) + 8)
  100. return -1;
  101. ih = nb->nb_ap.vp;
  102. switch (ih->ip_p) {
  103. case IPPROTO_TCP:
  104. {
  105. TCPHDR *th;
  106. TCPSOCKET *sock_tcp;
  107. th = (TCPHDR *) ((char *) ih) + sizeof(IPHDR);
  108. sock_tcp = NutTcpFindSocket(th->th_dport, th->th_sport, ih->ip_src);
  109. if (sock_tcp == 0)
  110. return -1;
  111. if (sock_tcp->so_state != TCPS_SYN_SENT && sock_tcp->so_state != TCPS_ESTABLISHED)
  112. return -1;
  113. NutTcpAbortSocket(sock_tcp, icmp_code2errno[icmp_code]);
  114. }
  115. break;
  116. #ifndef NUT_UDP_ICMP_EXCLUDE
  117. case IPPROTO_UDP:
  118. {
  119. UDPHDR *uh;
  120. UDPSOCKET *sock_udp;
  121. uh = (UDPHDR *) (((char *) ih) + sizeof(IPHDR));
  122. sock_udp = NutUdpFindSocket(uh->uh_dport);
  123. if (sock_udp == NULL)
  124. return -1;
  125. if (NutUdpSetSocketError(sock_udp, ih->ip_dst, uh->uh_dport, icmp_code2errno[icmp_code]))
  126. return -1;
  127. }
  128. break;
  129. #endif
  130. default:
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. /*!
  136. * \brief Handle incoming ICMP packets.
  137. *
  138. * Incoming ICMP packets are processed in the background.
  139. * NutNet currently handles echo request and destination
  140. * unreachable packets. Any other packet type is silently
  141. * discarded.
  142. *
  143. * \note This routine is called by the IP layer on incoming
  144. * ICMP datagrams. Applications typically do not call
  145. * this function.
  146. *
  147. * \param dev Identifies the device that received the packet.
  148. * \param nb Pointer to a network buffer structure containing
  149. * the ICMP datagram.
  150. */
  151. void NutIcmpInput(NUTDEVICE * dev, NETBUF * nb)
  152. {
  153. ICMPHDR *icp = (ICMPHDR *) nb->nb_tp.vp;
  154. /* Silently discard packets, which are too small. */
  155. if (icp == NULL || nb->nb_tp.sz < sizeof(ICMPHDR)) {
  156. NutNetBufFree(nb);
  157. return;
  158. }
  159. nb->nb_ap.sz = nb->nb_tp.sz - sizeof(ICMPHDR);
  160. if (nb->nb_ap.sz) {
  161. nb->nb_ap.vp = icp + 1;
  162. nb->nb_tp.sz = sizeof(ICMPHDR);
  163. }
  164. switch (icp->icmp_type) {
  165. case ICMP_ECHO:
  166. if (NutIcmpReflect(dev, ICMP_ECHOREPLY, nb)) {
  167. break;
  168. }
  169. case ICMP_UNREACH:
  170. NutIcmpUnreach(nb, icp->icmp_code);
  171. default:
  172. NutNetBufFree(nb);
  173. }
  174. }
  175. /*@}*/