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

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