icmpout.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/icmpout.c
  38. * \brief ICMP output functions.
  39. *
  40. * \verbatim
  41. * $Id: icmpout.c 3686 2011-12-04 14:20:38Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <netinet/in.h>
  45. #include <netinet/ip_icmp.h>
  46. #include <netinet/ipcsum.h>
  47. #include <netinet/icmp.h>
  48. #include <string.h>
  49. /*!
  50. * \addtogroup xgICMP
  51. */
  52. /*@{*/
  53. /*!
  54. * \brief Send an ICMP datagram.
  55. *
  56. * Known ICMP types are:
  57. *
  58. * - #ICMP_ECHOREPLY Echo reply
  59. * - #ICMP_UNREACH Destination unreachable
  60. * - #ICMP_SOURCEQUENCH Packet lost
  61. * - #ICMP_REDIRECT Shorter route
  62. * - #ICMP_ECHO Echo reply
  63. * - #ICMP_ROUTERADVERT Router advertisement
  64. * - #ICMP_ROUTERSOLICIT Router solicitation
  65. * - #ICMP_TIMXCEED Time exceeded
  66. * - #ICMP_PARAMPROB Bad IP header
  67. * - #ICMP_TSTAMP Timestamp request
  68. * - #ICMP_TSTAMPREPLY Timestamp reply
  69. * - #ICMP_IREQ Information request
  70. * - #ICMP_IREQREPLY Information reply
  71. * - #ICMP_MASKREQ Address mask request
  72. * - #ICMP_MASKREPLY Address mask reply
  73. *
  74. * \param type Type of the ICMP message.
  75. * \param dest Destination IP address.
  76. * \param nb Network buffer structure containing the datagram.
  77. *
  78. * \return 0 on success, -1 otherwise.
  79. */
  80. int NutIcmpOutput(uint8_t type, uint32_t dest, NETBUF * nb)
  81. {
  82. ICMPHDR *icp;
  83. uint16_t csum;
  84. icp = (ICMPHDR *) nb->nb_tp.vp;
  85. icp->icmp_type = type;
  86. icp->icmp_cksum = 0;
  87. csum = NutIpChkSumPartial(0, nb->nb_tp.vp, nb->nb_tp.sz);
  88. icp->icmp_cksum = NutIpChkSum(csum, nb->nb_ap.vp, nb->nb_ap.sz);
  89. return NutIpOutput(IPPROTO_ICMP, dest, nb);
  90. }
  91. /*!
  92. * \brief Send an ICMP message to a given destination.
  93. *
  94. * \param type Type of the ICMP message. See NutIcmpOutput() for
  95. * a list of valid types.
  96. * \param code Type subcode.
  97. * \param spec Type specific data item.
  98. * \param dest IP address of the target.
  99. * \param nb Network buffer structure containing the message to be sent.
  100. * The structure must have been allocated by a previous
  101. * call NutNetBufAlloc() and will be freed if this function
  102. * returns with an error.
  103. *
  104. * \return 0 on success, -1 otherwise.
  105. */
  106. int NutIcmpReply(uint8_t type, uint8_t code, uint32_t spec, uint32_t dest, NETBUF * nb)
  107. {
  108. ICMPHDR *icp;
  109. if ((nb = NutNetBufAlloc(nb, NBAF_TRANSPORT, sizeof(ICMPHDR))) == 0)
  110. return -1;
  111. icp = (ICMPHDR *) nb->nb_tp.vp;
  112. icp->icmp_code = code;
  113. icp->icmp_spec = spec;
  114. return NutIcmpOutput(type, dest, nb);
  115. }
  116. /*!
  117. * \brief Send an ICMP message as a response to a given destination.
  118. *
  119. * \param type Type of the ICMP message. See NutIcmpOutput() for
  120. * a list of valid types.
  121. * \param code Type subcode.
  122. * \param spec Type specific data item.
  123. * \param nb Network buffer structure containing the previously received
  124. * network packet. According to RFC792 the complete IP header
  125. * and the first 8 bytes of the transport netbuf is used as the
  126. * application data for the response. If this function returns
  127. * with an error, the buffer is freed. The destination address is
  128. * taken from the IP header.
  129. *
  130. * \return 0 on success, -1 otherwise.
  131. */
  132. int NutIcmpResponse(uint8_t type, uint8_t code, uint32_t spec, NETBUF * nb)
  133. {
  134. IPHDR *ip;
  135. uint32_t dest;
  136. ip = nb->nb_nw.vp;
  137. dest = ip->ip_src;
  138. if ((nb = NutNetBufAlloc(nb, NBAF_APPLICATION, sizeof(IPHDR) + 8)) == 0)
  139. return -1;
  140. memcpy(nb->nb_ap.vp, nb->nb_nw.vp, sizeof(IPHDR));
  141. memcpy((char *)nb->nb_ap.vp + sizeof(IPHDR), nb->nb_tp.vp, 8);
  142. return NutIcmpReply(type, code, spec, dest, nb);
  143. }
  144. /*@}*/