You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

icmp.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2009 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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ipxe/iobuf.h>
  23. #include <ipxe/in.h>
  24. #include <ipxe/tcpip.h>
  25. #include <ipxe/icmp.h>
  26. /** @file
  27. *
  28. * ICMP protocol
  29. *
  30. */
  31. struct tcpip_protocol icmp_protocol __tcpip_protocol;
  32. /**
  33. * Process a received packet
  34. *
  35. * @v iobuf I/O buffer
  36. * @v st_src Partially-filled source address
  37. * @v st_dest Partially-filled destination address
  38. * @v pshdr_csum Pseudo-header checksum
  39. * @ret rc Return status code
  40. */
  41. static int icmp_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src,
  42. struct sockaddr_tcpip *st_dest,
  43. uint16_t pshdr_csum __unused ) {
  44. struct icmp_header *icmp = iobuf->data;
  45. size_t len = iob_len ( iobuf );
  46. unsigned int csum;
  47. int rc;
  48. /* Sanity check */
  49. if ( len < sizeof ( *icmp ) ) {
  50. DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
  51. len, sizeof ( *icmp ) );
  52. rc = -EINVAL;
  53. goto done;
  54. }
  55. /* Verify checksum */
  56. csum = tcpip_chksum ( icmp, len );
  57. if ( csum != 0 ) {
  58. DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
  59. csum );
  60. DBG_HD ( icmp, len );
  61. rc = -EINVAL;
  62. goto done;
  63. }
  64. /* We respond only to pings */
  65. if ( icmp->type != ICMP_ECHO_REQUEST ) {
  66. DBG ( "ICMP ignoring type %d\n", icmp->type );
  67. rc = 0;
  68. goto done;
  69. }
  70. DBG ( "ICMP responding to ping\n" );
  71. /* Change type to response and recalculate checksum */
  72. icmp->type = ICMP_ECHO_RESPONSE;
  73. icmp->chksum = 0;
  74. icmp->chksum = tcpip_chksum ( icmp, len );
  75. /* Transmit the response */
  76. if ( ( rc = tcpip_tx ( iob_disown ( iobuf ), &icmp_protocol, st_dest,
  77. st_src, NULL, NULL ) ) != 0 ) {
  78. DBG ( "ICMP could not transmit ping response: %s\n",
  79. strerror ( rc ) );
  80. goto done;
  81. }
  82. done:
  83. free_iob ( iobuf );
  84. return rc;
  85. }
  86. /** ICMP TCP/IP protocol */
  87. struct tcpip_protocol icmp_protocol __tcpip_protocol = {
  88. .name = "ICMP",
  89. .rx = icmp_rx,
  90. .tcpip_proto = IP_ICMP,
  91. };