icmpv6.c
Go to the documentation of this file.00001 #include <stdint.h>
00002 #include <string.h>
00003 #include <byteswap.h>
00004 #include <errno.h>
00005 #include <gpxe/in.h>
00006 #include <gpxe/ip6.h>
00007 #include <gpxe/if_ether.h>
00008 #include <gpxe/iobuf.h>
00009 #include <gpxe/ndp.h>
00010 #include <gpxe/icmp6.h>
00011 #include <gpxe/tcpip.h>
00012 #include <gpxe/netdevice.h>
00013
00014 struct tcpip_protocol icmp6_protocol;
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 int icmp6_send_solicit ( struct net_device *netdev, struct in6_addr *src __unused,
00027 struct in6_addr *dest ) {
00028 union {
00029 struct sockaddr_in6 sin6;
00030 struct sockaddr_tcpip st;
00031 } st_dest;
00032 struct ll_protocol *ll_protocol = netdev->ll_protocol;
00033 struct neighbour_solicit *nsolicit;
00034 struct io_buffer *iobuf = alloc_iob ( sizeof ( *nsolicit ) + MIN_IOB_LEN );
00035 iob_reserve ( iobuf, MAX_HDR_LEN );
00036 nsolicit = iob_put ( iobuf, sizeof ( *nsolicit ) );
00037
00038
00039 memset ( nsolicit, 0, sizeof ( *nsolicit ) );
00040 nsolicit->type = ICMP6_NSOLICIT;
00041 nsolicit->code = 0;
00042 nsolicit->target = *dest;
00043 nsolicit->opt_type = 1;
00044 nsolicit->opt_len = ( 2 + ll_protocol->ll_addr_len ) / 8;
00045 memcpy ( nsolicit->opt_ll_addr, netdev->ll_addr,
00046 netdev->ll_protocol->ll_addr_len );
00047
00048 nsolicit->csum = 0;
00049 nsolicit->csum = tcpip_chksum ( nsolicit, sizeof ( *nsolicit ) );
00050
00051
00052 st_dest.sin6.sin_family = AF_INET6;
00053 st_dest.sin6.sin6_addr.in6_u.u6_addr8[0] = 0xff;
00054 st_dest.sin6.sin6_addr.in6_u.u6_addr8[2] = 0x02;
00055 st_dest.sin6.sin6_addr.in6_u.u6_addr16[1] = 0x0000;
00056 st_dest.sin6.sin6_addr.in6_u.u6_addr32[1] = 0x00000000;
00057 st_dest.sin6.sin6_addr.in6_u.u6_addr16[4] = 0x0000;
00058 st_dest.sin6.sin6_addr.in6_u.u6_addr16[5] = 0x0001;
00059 st_dest.sin6.sin6_addr.in6_u.u6_addr32[3] = dest->in6_u.u6_addr32[3];
00060 st_dest.sin6.sin6_addr.in6_u.u6_addr8[13] = 0xff;
00061
00062
00063 return tcpip_tx ( iobuf, &icmp6_protocol, NULL, &st_dest.st,
00064 NULL, &nsolicit->csum );
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074 static int icmp6_rx ( struct io_buffer *iobuf, struct sockaddr_tcpip *st_src,
00075 struct sockaddr_tcpip *st_dest, __unused uint16_t pshdr_csum ) {
00076 struct icmp6_header *icmp6hdr = iobuf->data;
00077
00078
00079 if ( iob_len ( iobuf ) < sizeof ( *icmp6hdr ) ) {
00080 DBG ( "Packet too short (%zd bytes)\n", iob_len ( iobuf ) );
00081 free_iob ( iobuf );
00082 return -EINVAL;
00083 }
00084
00085
00086
00087
00088 switch ( icmp6hdr->type ) {
00089 case ICMP6_NADVERT:
00090 return ndp_process_advert ( iobuf, st_src, st_dest );
00091 }
00092 return -ENOSYS;
00093 }
00094
00095 #if 0
00096 void icmp6_test_nadvert (struct net_device *netdev, struct sockaddr_in6 *server_p, char *ll_addr) {
00097
00098 struct sockaddr_in6 server;
00099 memcpy ( &server, server_p, sizeof ( server ) );
00100 struct io_buffer *rxiobuf = alloc_iob ( 500 );
00101 iob_reserve ( rxiobuf, MAX_HDR_LEN );
00102 struct neighbour_advert *nadvert = iob_put ( rxiobuf, sizeof ( *nadvert ) );
00103 nadvert->type = 136;
00104 nadvert->code = 0;
00105 nadvert->flags = ICMP6_FLAGS_SOLICITED;
00106 nadvert->csum = 0xffff;
00107 nadvert->target = server.sin6_addr;
00108 nadvert->opt_type = 2;
00109 nadvert->opt_len = 1;
00110 memcpy ( nadvert->opt_ll_addr, ll_addr, 6 );
00111 struct ip6_header *ip6hdr = iob_push ( rxiobuf, sizeof ( *ip6hdr ) );
00112 ip6hdr->ver_traffic_class_flow_label = htonl ( 0x60000000 );
00113 ip6hdr->hop_limit = 255;
00114 ip6hdr->nxt_hdr = 58;
00115 ip6hdr->payload_len = htons ( sizeof ( *nadvert ) );
00116 ip6hdr->src = server.sin6_addr;
00117 ip6hdr->dest = server.sin6_addr;
00118 hex_dump ( rxiobuf->data, iob_len ( rxiobuf ) );
00119 net_rx ( rxiobuf, netdev, htons ( ETH_P_IPV6 ), ll_addr );
00120 }
00121 #endif
00122
00123
00124 struct tcpip_protocol icmp6_protocol __tcpip_protocol = {
00125 .name = "ICMP6",
00126 .rx = icmp6_rx,
00127 .tcpip_proto = IP_ICMP6,
00128 };