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.

icmpv6.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <byteswap.h>
  4. #include <errno.h>
  5. #include <ipxe/in.h>
  6. #include <ipxe/ip6.h>
  7. #include <ipxe/if_ether.h>
  8. #include <ipxe/iobuf.h>
  9. #include <ipxe/ndp.h>
  10. #include <ipxe/icmp6.h>
  11. #include <ipxe/tcpip.h>
  12. #include <ipxe/netdevice.h>
  13. /**
  14. * Send neighbour solicitation packet
  15. *
  16. * @v netdev Network device
  17. * @v src Source address
  18. * @v dest Destination address
  19. *
  20. * This function prepares a neighbour solicitation packet and sends it to the
  21. * network layer.
  22. */
  23. int icmp6_send_solicit ( struct net_device *netdev, struct in6_addr *src __unused,
  24. struct in6_addr *dest ) {
  25. union {
  26. struct sockaddr_in6 sin6;
  27. struct sockaddr_tcpip st;
  28. } st_dest;
  29. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  30. struct neighbour_solicit *nsolicit;
  31. struct io_buffer *iobuf = alloc_iob ( sizeof ( *nsolicit ) + MIN_IOB_LEN );
  32. iob_reserve ( iobuf, MAX_HDR_LEN );
  33. nsolicit = iob_put ( iobuf, sizeof ( *nsolicit ) );
  34. /* Fill up the headers */
  35. memset ( nsolicit, 0, sizeof ( *nsolicit ) );
  36. nsolicit->type = ICMP6_NSOLICIT;
  37. nsolicit->code = 0;
  38. nsolicit->target = *dest;
  39. nsolicit->opt_type = 1;
  40. nsolicit->opt_len = ( 2 + ll_protocol->ll_addr_len ) / 8;
  41. memcpy ( nsolicit->opt_ll_addr, netdev->ll_addr,
  42. netdev->ll_protocol->ll_addr_len );
  43. /* Partial checksum */
  44. nsolicit->csum = 0;
  45. nsolicit->csum = tcpip_chksum ( nsolicit, sizeof ( *nsolicit ) );
  46. /* Solicited multicast address */
  47. st_dest.sin6.sin_family = AF_INET6;
  48. st_dest.sin6.sin6_addr.in6_u.u6_addr8[0] = 0xff;
  49. st_dest.sin6.sin6_addr.in6_u.u6_addr8[2] = 0x02;
  50. st_dest.sin6.sin6_addr.in6_u.u6_addr16[1] = 0x0000;
  51. st_dest.sin6.sin6_addr.in6_u.u6_addr32[1] = 0x00000000;
  52. st_dest.sin6.sin6_addr.in6_u.u6_addr16[4] = 0x0000;
  53. st_dest.sin6.sin6_addr.in6_u.u6_addr16[5] = 0x0001;
  54. st_dest.sin6.sin6_addr.in6_u.u6_addr32[3] = dest->in6_u.u6_addr32[3];
  55. st_dest.sin6.sin6_addr.in6_u.u6_addr8[13] = 0xff;
  56. /* Send packet over IP6 */
  57. return tcpip_tx ( iobuf, &icmp6_protocol, NULL, &st_dest.st,
  58. NULL, &nsolicit->csum );
  59. }
  60. /**
  61. * Process ICMP6 headers
  62. *
  63. * @v iobuf I/O buffer
  64. * @v st_src Source address
  65. * @v st_dest Destination address
  66. */
  67. static int icmp6_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src,
  68. struct sockaddr_tcpip *st_dest, __unused uint16_t pshdr_csum ) {
  69. struct icmp6_header *icmp6hdr = iobuf->data;
  70. /* Sanity check */
  71. if ( iob_len ( iobuf ) < sizeof ( *icmp6hdr ) ) {
  72. DBG ( "Packet too short (%zd bytes)\n", iob_len ( iobuf ) );
  73. free_iob ( iobuf );
  74. return -EINVAL;
  75. }
  76. /* TODO: Verify checksum */
  77. /* Process the ICMP header */
  78. switch ( icmp6hdr->type ) {
  79. case ICMP6_NADVERT:
  80. return ndp_process_advert ( iobuf, st_src, st_dest );
  81. }
  82. return -ENOSYS;
  83. }
  84. #if 0
  85. void icmp6_test_nadvert (struct net_device *netdev, struct sockaddr_in6 *server_p, char *ll_addr) {
  86. struct sockaddr_in6 server;
  87. memcpy ( &server, server_p, sizeof ( server ) );
  88. struct io_buffer *rxiobuf = alloc_iob ( 500 );
  89. iob_reserve ( rxiobuf, MAX_HDR_LEN );
  90. struct neighbour_advert *nadvert = iob_put ( rxiobuf, sizeof ( *nadvert ) );
  91. nadvert->type = 136;
  92. nadvert->code = 0;
  93. nadvert->flags = ICMP6_FLAGS_SOLICITED;
  94. nadvert->csum = 0xffff;
  95. nadvert->target = server.sin6_addr;
  96. nadvert->opt_type = 2;
  97. nadvert->opt_len = 1;
  98. memcpy ( nadvert->opt_ll_addr, ll_addr, 6 );
  99. struct ip6_header *ip6hdr = iob_push ( rxiobuf, sizeof ( *ip6hdr ) );
  100. ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );
  101. ip6hdr->hop_limit = 255;
  102. ip6hdr->nxt_hdr = 58;
  103. ip6hdr->payload_len = htons ( sizeof ( *nadvert ) );
  104. ip6hdr->src = server.sin6_addr;
  105. ip6hdr->dest = server.sin6_addr;
  106. hex_dump ( rxiobuf->data, iob_len ( rxiobuf ) );
  107. net_rx ( rxiobuf, netdev, htons ( ETH_P_IPV6 ), ll_addr );
  108. }
  109. #endif
  110. /** ICMP6 protocol */
  111. struct tcpip_protocol icmp6_protocol __tcpip_protocol = {
  112. .name = "ICMP6",
  113. .rx = icmp6_rx,
  114. .tcpip_proto = IP_ICMP6, // 58
  115. };