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

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