iPXE
arp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 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 <stdint.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <byteswap.h>
00030 #include <errno.h>
00031 #include <ipxe/if_ether.h>
00032 #include <ipxe/if_arp.h>
00033 #include <ipxe/iobuf.h>
00034 #include <ipxe/netdevice.h>
00035 #include <ipxe/neighbour.h>
00036 #include <ipxe/arp.h>
00037 
00038 /** @file
00039  *
00040  * Address Resolution Protocol
00041  *
00042  * This file implements the address resolution protocol as defined in
00043  * RFC826.  The implementation is media-independent and
00044  * protocol-independent; it is not limited to Ethernet or to IPv4.
00045  *
00046  */
00047 
00048 struct net_protocol arp_protocol __net_protocol;
00049 
00050 /**
00051  * Transmit ARP request
00052  *
00053  * @v netdev            Network device
00054  * @v net_protocol      Network-layer protocol
00055  * @v net_dest          Destination network-layer address
00056  * @v net_source        Source network-layer address
00057  * @ret rc              Return status code
00058  */
00059 int arp_tx_request ( struct net_device *netdev,
00060                      struct net_protocol *net_protocol,
00061                      const void *net_dest, const void *net_source ) {
00062         struct ll_protocol *ll_protocol = netdev->ll_protocol;
00063         struct io_buffer *iobuf;
00064         struct arphdr *arphdr;
00065         int rc;
00066 
00067         /* Allocate ARP packet */
00068         iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
00069                             ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
00070         if ( ! iobuf )
00071                 return -ENOMEM;
00072         iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
00073 
00074         /* Build up ARP request */
00075         arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
00076         arphdr->ar_hrd = ll_protocol->ll_proto;
00077         arphdr->ar_hln = ll_protocol->ll_addr_len;
00078         arphdr->ar_pro = net_protocol->net_proto;
00079         arphdr->ar_pln = net_protocol->net_addr_len;
00080         arphdr->ar_op = htons ( ARPOP_REQUEST );
00081         memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
00082                  netdev->ll_addr, ll_protocol->ll_addr_len );
00083         memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
00084                  net_source, net_protocol->net_addr_len );
00085         memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
00086                  0, ll_protocol->ll_addr_len );
00087         memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
00088                  net_dest, net_protocol->net_addr_len );
00089 
00090         /* Transmit ARP request */
00091         if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
00092                              netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
00093                 DBGC ( netdev, "ARP %s %s %s could not transmit request: %s\n",
00094                        netdev->name, net_protocol->name,
00095                        net_protocol->ntoa ( net_dest ), strerror ( rc ) );
00096                 return rc;
00097         }
00098 
00099         return 0;
00100 }
00101 
00102 /** ARP neighbour discovery protocol */
00103 struct neighbour_discovery arp_discovery = {
00104         .name = "ARP",
00105         .tx_request = arp_tx_request,
00106 };
00107 
00108 /**
00109  * Identify ARP protocol
00110  *
00111  * @v net_proto                 Network-layer protocol, in network-endian order
00112  * @ret arp_net_protocol        ARP protocol, or NULL
00113  *
00114  */
00115 static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
00116         struct arp_net_protocol *arp_net_protocol;
00117 
00118         for_each_table_entry ( arp_net_protocol, ARP_NET_PROTOCOLS ) {
00119                 if ( arp_net_protocol->net_protocol->net_proto == net_proto )
00120                         return arp_net_protocol;
00121         }
00122         return NULL;
00123 }
00124 
00125 /**
00126  * Process incoming ARP packets
00127  *
00128  * @v iobuf             I/O buffer
00129  * @v netdev            Network device
00130  * @v ll_source         Link-layer source address
00131  * @v flags             Packet flags
00132  * @ret rc              Return status code
00133  */
00134 static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
00135                     const void *ll_dest __unused,
00136                     const void *ll_source __unused,
00137                     unsigned int flags __unused ) {
00138         struct arphdr *arphdr = iobuf->data;
00139         struct arp_net_protocol *arp_net_protocol;
00140         struct net_protocol *net_protocol;
00141         struct ll_protocol *ll_protocol;
00142         size_t len = iob_len ( iobuf );
00143         int rc;
00144 
00145         /* Sanity check */
00146         if ( ( len < sizeof ( *arphdr ) ) || ( len < arp_len ( arphdr ) ) ) {
00147                 rc = -EINVAL;
00148                 goto done;
00149         }
00150 
00151         /* Identify network-layer and link-layer protocols */
00152         arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
00153         if ( ! arp_net_protocol ) {
00154                 rc = -EPROTONOSUPPORT;
00155                 goto done;
00156         }
00157         net_protocol = arp_net_protocol->net_protocol;
00158         ll_protocol = netdev->ll_protocol;
00159 
00160         /* Sanity checks */
00161         if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
00162              ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
00163              ( arphdr->ar_pln != net_protocol->net_addr_len ) ) {
00164                 rc = -EINVAL;
00165                 goto done;
00166         }
00167 
00168         /* Update neighbour cache entry for this sender, if any */
00169         neighbour_update ( netdev, net_protocol, arp_sender_pa ( arphdr ),
00170                            arp_sender_ha ( arphdr ) );
00171 
00172         /* If it's not a request, there's nothing more to do */
00173         if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
00174                 rc = 0;
00175                 goto done;
00176         }
00177 
00178         /* See if we own the target protocol address */
00179         if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
00180                 rc = 0;
00181                 goto done;
00182         }
00183 
00184         /* Change request to a reply */
00185         DBGC2 ( netdev, "ARP %s %s %s reply => %s %s\n",
00186                 netdev->name, net_protocol->name,
00187                 net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
00188                 ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
00189         arphdr->ar_op = htons ( ARPOP_REPLY );
00190         memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
00191                  arphdr->ar_hln + arphdr->ar_pln );
00192         memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
00193 
00194         /* Send reply */
00195         if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
00196                              arp_target_ha ( arphdr ),
00197                              netdev->ll_addr ) ) != 0 ) {
00198                 DBGC ( netdev, "ARP %s %s %s could not transmit reply: %s\n",
00199                        netdev->name, net_protocol->name,
00200                        net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
00201                        strerror ( rc ) );
00202                 goto done;
00203         }
00204 
00205         /* Success */
00206         rc = 0;
00207 
00208  done:
00209         free_iob ( iobuf );
00210         return rc;
00211 }
00212 
00213 /**
00214  * Transcribe ARP address
00215  *
00216  * @v net_addr  ARP address
00217  * @ret string  "<ARP>"
00218  *
00219  * This operation is meaningless for the ARP protocol.
00220  */
00221 static const char * arp_ntoa ( const void *net_addr __unused ) {
00222         return "<ARP>";
00223 }
00224 
00225 /** ARP network protocol */
00226 struct net_protocol arp_protocol __net_protocol = {
00227         .name = "ARP",
00228         .net_proto = htons ( ETH_P_ARP ),
00229         .rx = arp_rx,
00230         .ntoa = arp_ntoa,
00231 };