Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / icmpv4.c
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <string.h>
23 #include <errno.h>
24 #include <ipxe/iobuf.h>
25 #include <ipxe/in.h>
26 #include <ipxe/tcpip.h>
27 #include <ipxe/icmp.h>
28
29 /** @file
30  *
31  * ICMPv4 protocol
32  *
33  */
34
35 struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol;
36
37 /**
38  * Process a received packet
39  *
40  * @v iobuf             I/O buffer
41  * @v netdev            Network device
42  * @v st_src            Partially-filled source address
43  * @v st_dest           Partially-filled destination address
44  * @v pshdr_csum        Pseudo-header checksum
45  * @ret rc              Return status code
46  */
47 static int icmpv4_rx ( struct io_buffer *iobuf,
48                        struct net_device *netdev __unused,
49                        struct sockaddr_tcpip *st_src,
50                        struct sockaddr_tcpip *st_dest __unused,
51                        uint16_t pshdr_csum __unused ) {
52         struct icmp_header *icmp = iobuf->data;
53         size_t len = iob_len ( iobuf );
54         unsigned int csum;
55         unsigned int type;
56         int rc;
57
58         /* Sanity check */
59         if ( len < sizeof ( *icmp ) ) {
60                 DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
61                       len, sizeof ( *icmp ) );
62                 rc = -EINVAL;
63                 goto discard;
64         }
65
66         /* Verify checksum */
67         csum = tcpip_chksum ( icmp, len );
68         if ( csum != 0 ) {
69                 DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
70                       csum );
71                 DBG_HD ( icmp, len );
72                 rc = -EINVAL;
73                 goto discard;
74         }
75
76         /* Handle ICMP packet */
77         type = icmp->type;
78         switch ( type ) {
79         case ICMP_ECHO_REQUEST:
80                 return icmp_rx_echo_request ( iobuf, st_src,
81                                               &icmpv4_echo_protocol );
82         case ICMP_ECHO_REPLY:
83                 return icmp_rx_echo_reply ( iobuf, st_src );
84         default:
85                 DBG ( "ICMP ignoring type %d\n", type );
86                 rc = 0;
87                 break;
88         }
89
90  discard:
91         free_iob ( iobuf );
92         return rc;
93 }
94
95 /** ICMPv4 TCP/IP protocol */
96 struct tcpip_protocol icmpv4_protocol __tcpip_protocol = {
97         .name = "ICMPv4",
98         .rx = icmpv4_rx,
99         .tcpip_proto = IP_ICMP,
100 };
101
102 /** ICMPv4 echo protocol */
103 struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol = {
104         .family = AF_INET,
105         .request = ICMP_ECHO_REQUEST,
106         .reply = ICMP_ECHO_REPLY,
107         .tcpip_protocol = &icmpv4_protocol,
108         .net_checksum = 0,
109 };