Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

icmp.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <byteswap.h>
  26. #include <errno.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/in.h>
  29. #include <ipxe/tcpip.h>
  30. #include <ipxe/ping.h>
  31. #include <ipxe/crc32.h>
  32. #include <ipxe/icmp.h>
  33. /** @file
  34. *
  35. * ICMP protocol
  36. *
  37. */
  38. /**
  39. * Identify ICMP echo protocol
  40. *
  41. * @v st_family Address family
  42. * @ret echo_protocol ICMP echo protocol, or NULL
  43. */
  44. static struct icmp_echo_protocol * icmp_echo_protocol ( sa_family_t family ) {
  45. struct icmp_echo_protocol *echo_protocol;
  46. for_each_table_entry ( echo_protocol, ICMP_ECHO_PROTOCOLS ) {
  47. if ( echo_protocol->family == family )
  48. return echo_protocol;
  49. }
  50. return NULL;
  51. }
  52. /**
  53. *
  54. * Determine debugging colour for ICMP debug messages
  55. *
  56. * @v st_peer Peer address
  57. * @ret col Debugging colour (for DBGC())
  58. */
  59. static uint32_t icmpcol ( struct sockaddr_tcpip *st_peer ) {
  60. return crc32_le ( 0, st_peer, sizeof ( *st_peer ) );
  61. }
  62. /**
  63. * Transmit ICMP echo packet
  64. *
  65. * @v iobuf I/O buffer
  66. * @v st_dest Destination socket address
  67. * @v echo_protocol ICMP echo protocol
  68. * @ret rc Return status code
  69. */
  70. static int icmp_tx_echo ( struct io_buffer *iobuf,
  71. struct sockaddr_tcpip *st_dest,
  72. struct icmp_echo_protocol *echo_protocol ) {
  73. struct icmp_echo *echo = iobuf->data;
  74. int rc;
  75. /* Set ICMP type and (re)calculate checksum */
  76. echo->icmp.chksum = 0;
  77. echo->icmp.chksum = tcpip_chksum ( echo, iob_len ( iobuf ) );
  78. /* Transmit packet */
  79. if ( ( rc = tcpip_tx ( iobuf, echo_protocol->tcpip_protocol, NULL,
  80. st_dest, NULL,
  81. ( echo_protocol->net_checksum ?
  82. &echo->icmp.chksum : NULL ) ) ) != 0 )
  83. return rc;
  84. return 0;
  85. }
  86. /**
  87. * Transmit ICMP echo request
  88. *
  89. * @v iobuf I/O buffer
  90. * @v st_dest Destination socket address
  91. * @ret rc Return status code
  92. */
  93. int icmp_tx_echo_request ( struct io_buffer *iobuf,
  94. struct sockaddr_tcpip *st_dest ) {
  95. struct icmp_echo *echo = iobuf->data;
  96. struct icmp_echo_protocol *echo_protocol;
  97. int rc;
  98. /* Identify ICMP echo protocol */
  99. echo_protocol = icmp_echo_protocol ( st_dest->st_family );
  100. if ( ! echo_protocol ) {
  101. DBGC ( icmpcol ( st_dest ), "ICMP TX echo request unknown "
  102. "address family %d\n", st_dest->st_family );
  103. free_iob ( iobuf );
  104. return -ENOTSUP;
  105. }
  106. /* Set type */
  107. echo->icmp.type = echo_protocol->request;
  108. /* Transmit request */
  109. DBGC ( icmpcol ( st_dest ), "ICMP TX echo request id %04x seq %04x\n",
  110. ntohs ( echo->ident ), ntohs ( echo->sequence ) );
  111. if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
  112. return rc;
  113. return 0;
  114. }
  115. /**
  116. * Transmit ICMP echo reply
  117. *
  118. * @v iobuf I/O buffer
  119. * @v st_dest Destination socket address
  120. * @ret rc Return status code
  121. */
  122. static int icmp_tx_echo_reply ( struct io_buffer *iobuf,
  123. struct sockaddr_tcpip *st_dest,
  124. struct icmp_echo_protocol *echo_protocol ) {
  125. struct icmp_echo *echo = iobuf->data;
  126. int rc;
  127. /* Set type */
  128. echo->icmp.type = echo_protocol->reply;
  129. /* Transmit reply */
  130. DBGC ( icmpcol ( st_dest ), "ICMP TX echo reply id %04x seq %04x\n",
  131. ntohs ( echo->ident ), ntohs ( echo->sequence ) );
  132. if ( ( rc = icmp_tx_echo ( iobuf, st_dest, echo_protocol ) ) != 0 )
  133. return rc;
  134. return 0;
  135. }
  136. /**
  137. * Process a received ICMP echo request
  138. *
  139. * @v iobuf I/O buffer
  140. * @v st_src Source socket address
  141. * @v echo_protocol ICMP echo protocol
  142. * @ret rc Return status code
  143. */
  144. int icmp_rx_echo_request ( struct io_buffer *iobuf,
  145. struct sockaddr_tcpip *st_src,
  146. struct icmp_echo_protocol *echo_protocol ) {
  147. struct icmp_echo *echo = iobuf->data;
  148. int rc;
  149. /* Sanity check */
  150. if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
  151. DBGC ( icmpcol ( st_src ), "ICMP RX echo request too short at "
  152. "%zd bytes (min %zd bytes)\n",
  153. iob_len ( iobuf ), sizeof ( *echo ) );
  154. free_iob ( iobuf );
  155. return -EINVAL;
  156. }
  157. DBGC ( icmpcol ( st_src ), "ICMP RX echo request id %04x seq %04x\n",
  158. ntohs ( echo->ident ), ntohs ( echo->sequence ) );
  159. /* Transmit echo reply */
  160. if ( ( rc = icmp_tx_echo_reply ( iobuf, st_src, echo_protocol ) ) != 0 )
  161. return rc;
  162. return 0;
  163. }
  164. /**
  165. * Process a received ICMP echo request
  166. *
  167. * @v iobuf I/O buffer
  168. * @v st_src Source socket address
  169. * @ret rc Return status code
  170. */
  171. int icmp_rx_echo_reply ( struct io_buffer *iobuf,
  172. struct sockaddr_tcpip *st_src ) {
  173. struct icmp_echo *echo = iobuf->data;
  174. int rc;
  175. /* Sanity check */
  176. if ( iob_len ( iobuf ) < sizeof ( *echo ) ) {
  177. DBGC ( icmpcol ( st_src ), "ICMP RX echo reply too short at "
  178. "%zd bytes (min %zd bytes)\n",
  179. iob_len ( iobuf ), sizeof ( *echo ) );
  180. free_iob ( iobuf );
  181. return -EINVAL;
  182. }
  183. DBGC ( icmpcol ( st_src ), "ICMP RX echo reply id %04x seq %04x\n",
  184. ntohs ( echo->ident ), ntohs ( echo->sequence ) );
  185. /* Deliver to ping protocol */
  186. if ( ( rc = ping_rx ( iobuf, st_src ) ) != 0 )
  187. return rc;
  188. return 0;
  189. }
  190. /**
  191. * Receive ping reply (when no ping protocol is present)
  192. *
  193. * @v iobuf I/O buffer
  194. * @v st_src Source socket address
  195. * @ret rc Return status code
  196. */
  197. __weak int ping_rx ( struct io_buffer *iobuf,
  198. struct sockaddr_tcpip *st_src __unused ) {
  199. free_iob ( iobuf );
  200. return 0;
  201. }