2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/iobuf.h>
27 #include <ipxe/tcpip.h>
28 #include <ipxe/ping.h>
29 #include <ipxe/icmpv6.h>
37 struct icmp_echo_protocol icmpv6_echo_protocol __icmp_echo_protocol;
40 * Process received ICMPv6 echo request packet
43 * @v netdev Network device
44 * @v sin6_src Source socket address
45 * @v sin6_dest Destination socket address
46 * @ret rc Return status code
48 static int icmpv6_rx_echo_request ( struct io_buffer *iobuf,
49 struct net_device *netdev __unused,
50 struct sockaddr_in6 *sin6_src,
51 struct sockaddr_in6 *sin6_dest __unused ) {
52 struct sockaddr_tcpip *st_src =
53 ( ( struct sockaddr_tcpip * ) sin6_src );
55 return icmp_rx_echo_request ( iobuf, st_src, &icmpv6_echo_protocol );
58 /** ICMPv6 echo request handler */
59 struct icmpv6_handler icmpv6_echo_request_handler __icmpv6_handler = {
60 .type = ICMPV6_ECHO_REQUEST,
61 .rx = icmpv6_rx_echo_request,
65 * Process received ICMPv6 echo reply packet
68 * @v netdev Network device
69 * @v sin6_src Source socket address
70 * @v sin6_dest Destination socket address
71 * @ret rc Return status code
73 static int icmpv6_rx_echo_reply ( struct io_buffer *iobuf,
74 struct net_device *netdev __unused,
75 struct sockaddr_in6 *sin6_src,
76 struct sockaddr_in6 *sin6_dest __unused ) {
77 struct sockaddr_tcpip *st_src =
78 ( ( struct sockaddr_tcpip * ) sin6_src );
80 return icmp_rx_echo_reply ( iobuf, st_src );
83 /** ICMPv6 echo reply handler */
84 struct icmpv6_handler icmpv6_echo_reply_handler __icmpv6_handler = {
85 .type = ICMPV6_ECHO_REPLY,
86 .rx = icmpv6_rx_echo_reply,
90 * Identify ICMPv6 handler
93 * @ret handler ICMPv6 handler, or NULL if not found
95 static struct icmpv6_handler * icmpv6_handler ( unsigned int type ) {
96 struct icmpv6_handler *handler;
98 for_each_table_entry ( handler, ICMPV6_HANDLERS ) {
99 if ( handler->type == type )
106 * Process a received packet
108 * @v iobuf I/O buffer
109 * @v netdev Network device
110 * @v st_src Partially-filled source address
111 * @v st_dest Partially-filled destination address
112 * @v pshdr_csum Pseudo-header checksum
113 * @ret rc Return status code
115 static int icmpv6_rx ( struct io_buffer *iobuf, struct net_device *netdev,
116 struct sockaddr_tcpip *st_src,
117 struct sockaddr_tcpip *st_dest, uint16_t pshdr_csum ) {
118 struct sockaddr_in6 *sin6_src = ( ( struct sockaddr_in6 * ) st_src );
119 struct sockaddr_in6 *sin6_dest = ( ( struct sockaddr_in6 * ) st_dest );
120 struct icmp_header *icmp = iobuf->data;
121 size_t len = iob_len ( iobuf );
122 struct icmpv6_handler *handler;
127 if ( len < sizeof ( *icmp ) ) {
128 DBGC ( netdev, "ICMPv6 packet too short at %zd bytes (min %zd "
129 "bytes)\n", len, sizeof ( *icmp ) );
134 /* Verify checksum */
135 csum = tcpip_continue_chksum ( pshdr_csum, icmp, len );
137 DBGC ( netdev, "ICMPv6 checksum incorrect (is %04x, should be "
139 DBGC_HDA ( netdev, 0, icmp, len );
144 /* Identify handler */
145 handler = icmpv6_handler ( icmp->type );
147 DBGC ( netdev, "ICMPv6 unrecognised type %d\n", icmp->type );
152 /* Pass to handler */
153 if ( ( rc = handler->rx ( iob_disown ( iobuf ), netdev, sin6_src,
154 sin6_dest ) ) != 0 ) {
155 DBGC ( netdev, "ICMPv6 could not handle type %d: %s\n",
156 icmp->type, strerror ( rc ) );
165 /** ICMPv6 TCP/IP protocol */
166 struct tcpip_protocol icmpv6_protocol __tcpip_protocol = {
169 .tcpip_proto = IP_ICMP6,
172 /** ICMPv6 echo protocol */
173 struct icmp_echo_protocol icmpv6_echo_protocol __icmp_echo_protocol = {
175 .request = ICMPV6_ECHO_REQUEST,
176 .reply = ICMPV6_ECHO_REPLY,
177 .tcpip_protocol = &icmpv6_protocol,