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/pkbuff.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 pk_buff *pkb = alloc_pkb ( sizeof ( *nsolicit ) + MIN_PKB_LEN );
  33. pkb_reserve ( pkb, MAX_HDR_LEN );
  34. nsolicit = pkb_put ( pkb, 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 ( pkb, &icmp6_protocol, &st_dest.st ) );
  59. }
  60. /**
  61. * Process ICMP6 headers
  62. *
  63. * @v pkb Packet buffer
  64. * @v st_src Source address
  65. * @v st_dest Destination address
  66. */
  67. static int icmp6_rx ( struct pk_buff *pkb, struct sockaddr_tcpip *st_src,
  68. struct sockaddr_tcpip *st_dest ) {
  69. struct icmp6_header *icmp6hdr = pkb->data;
  70. /* Sanity check */
  71. if ( pkb_len ( pkb ) < sizeof ( *icmp6hdr ) ) {
  72. DBG ( "Packet too short (%d bytes)\n", pkb_len ( pkb ) );
  73. free_pkb ( pkb );
  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 ( pkb, 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 pk_buff *rxpkb = alloc_pkb ( 500 );
  89. pkb_reserve ( rxpkb, MAX_HDR_LEN );
  90. struct neighbour_advert *nadvert = pkb_put ( rxpkb, 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 = pkb_push ( rxpkb, 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 ( rxpkb->data, pkb_len ( rxpkb ) );
  107. net_rx ( rxpkb, 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. .csum_offset = 2,
  116. };