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.

icmpv4.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 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. * ICMPv4 protocol
  29. *
  30. */
  31. struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol;
  32. /**
  33. * Process a received packet
  34. *
  35. * @v iobuf I/O buffer
  36. * @v netdev Network device
  37. * @v st_src Partially-filled source address
  38. * @v st_dest Partially-filled destination address
  39. * @v pshdr_csum Pseudo-header checksum
  40. * @ret rc Return status code
  41. */
  42. static int icmpv4_rx ( struct io_buffer *iobuf,
  43. struct net_device *netdev __unused,
  44. struct sockaddr_tcpip *st_src,
  45. struct sockaddr_tcpip *st_dest __unused,
  46. uint16_t pshdr_csum __unused ) {
  47. struct icmp_header *icmp = iobuf->data;
  48. size_t len = iob_len ( iobuf );
  49. unsigned int csum;
  50. unsigned int type;
  51. int rc;
  52. /* Sanity check */
  53. if ( len < sizeof ( *icmp ) ) {
  54. DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
  55. len, sizeof ( *icmp ) );
  56. rc = -EINVAL;
  57. goto discard;
  58. }
  59. /* Verify checksum */
  60. csum = tcpip_chksum ( icmp, len );
  61. if ( csum != 0 ) {
  62. DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
  63. csum );
  64. DBG_HD ( icmp, len );
  65. rc = -EINVAL;
  66. goto discard;
  67. }
  68. /* Handle ICMP packet */
  69. type = icmp->type;
  70. switch ( type ) {
  71. case ICMP_ECHO_REQUEST:
  72. return icmp_rx_echo_request ( iobuf, st_src,
  73. &icmpv4_echo_protocol );
  74. case ICMP_ECHO_REPLY:
  75. return icmp_rx_echo_reply ( iobuf, st_src );
  76. default:
  77. DBG ( "ICMP ignoring type %d\n", type );
  78. rc = 0;
  79. break;
  80. }
  81. discard:
  82. free_iob ( iobuf );
  83. return rc;
  84. }
  85. /** ICMPv4 TCP/IP protocol */
  86. struct tcpip_protocol icmpv4_protocol __tcpip_protocol = {
  87. .name = "ICMPv4",
  88. .rx = icmpv4_rx,
  89. .tcpip_proto = IP_ICMP,
  90. };
  91. /** ICMPv4 echo protocol */
  92. struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol = {
  93. .family = AF_INET,
  94. .request = ICMP_ECHO_REQUEST,
  95. .reply = ICMP_ECHO_REPLY,
  96. .tcpip_protocol = &icmpv4_protocol,
  97. .net_checksum = 0,
  98. };