iPXE
icmpv4.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <errno.h>
00028 #include <ipxe/iobuf.h>
00029 #include <ipxe/in.h>
00030 #include <ipxe/tcpip.h>
00031 #include <ipxe/icmp.h>
00032 
00033 /** @file
00034  *
00035  * ICMPv4 protocol
00036  *
00037  */
00038 
00039 struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol;
00040 
00041 /**
00042  * Process a received packet
00043  *
00044  * @v iobuf             I/O buffer
00045  * @v netdev            Network device
00046  * @v st_src            Partially-filled source address
00047  * @v st_dest           Partially-filled destination address
00048  * @v pshdr_csum        Pseudo-header checksum
00049  * @ret rc              Return status code
00050  */
00051 static int icmpv4_rx ( struct io_buffer *iobuf,
00052                        struct net_device *netdev __unused,
00053                        struct sockaddr_tcpip *st_src,
00054                        struct sockaddr_tcpip *st_dest __unused,
00055                        uint16_t pshdr_csum __unused ) {
00056         struct icmp_header *icmp = iobuf->data;
00057         size_t len = iob_len ( iobuf );
00058         unsigned int csum;
00059         unsigned int type;
00060         int rc;
00061 
00062         /* Sanity check */
00063         if ( len < sizeof ( *icmp ) ) {
00064                 DBG ( "ICMP packet too short at %zd bytes (min %zd bytes)\n",
00065                       len, sizeof ( *icmp ) );
00066                 rc = -EINVAL;
00067                 goto discard;
00068         }
00069 
00070         /* Verify checksum */
00071         csum = tcpip_chksum ( icmp, len );
00072         if ( csum != 0 ) {
00073                 DBG ( "ICMP checksum incorrect (is %04x, should be 0000)\n",
00074                       csum );
00075                 DBG_HD ( icmp, len );
00076                 rc = -EINVAL;
00077                 goto discard;
00078         }
00079 
00080         /* Handle ICMP packet */
00081         type = icmp->type;
00082         switch ( type ) {
00083         case ICMP_ECHO_REQUEST:
00084                 return icmp_rx_echo_request ( iobuf, st_src,
00085                                               &icmpv4_echo_protocol );
00086         case ICMP_ECHO_REPLY:
00087                 return icmp_rx_echo_reply ( iobuf, st_src );
00088         default:
00089                 DBG ( "ICMP ignoring type %d\n", type );
00090                 rc = 0;
00091                 break;
00092         }
00093 
00094  discard:
00095         free_iob ( iobuf );
00096         return rc;
00097 }
00098 
00099 /** ICMPv4 TCP/IP protocol */
00100 struct tcpip_protocol icmpv4_protocol __tcpip_protocol = {
00101         .name = "ICMPv4",
00102         .rx = icmpv4_rx,
00103         .tcpip_proto = IP_ICMP,
00104 };
00105 
00106 /** ICMPv4 echo protocol */
00107 struct icmp_echo_protocol icmpv4_echo_protocol __icmp_echo_protocol = {
00108         .family = AF_INET,
00109         .request = ICMP_ECHO_REQUEST,
00110         .reply = ICMP_ECHO_REPLY,
00111         .tcpip_protocol = &icmpv4_protocol,
00112         .net_checksum = 0,
00113 };