您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

icmpv4.c 3.0KB

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