選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

icmp.c 2.6KB

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