Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / gpxe / src / net / arp.c
blob011d4fefb15cf8f947ba1fd3bfebd004c33f8c5f
1 /*
2 * Copyright (C) 2006 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stdint.h>
20 #include <string.h>
21 #include <byteswap.h>
22 #include <errno.h>
23 #include <gpxe/if_ether.h>
24 #include <gpxe/if_arp.h>
25 #include <gpxe/iobuf.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/arp.h>
29 /** @file
31 * Address Resolution Protocol
33 * This file implements the address resolution protocol as defined in
34 * RFC826. The implementation is media-independent and
35 * protocol-independent; it is not limited to Ethernet or to IPv4.
39 /** Registered ARP protocols */
40 static struct arp_net_protocol arp_net_protocols[0]
41 __table_start ( struct arp_net_protocol, arp_net_protocols );
42 static struct arp_net_protocol arp_net_protocols_end[0]
43 __table_end ( struct arp_net_protocol, arp_net_protocols );
45 /** An ARP cache entry */
46 struct arp_entry {
47 /** Network-layer protocol */
48 struct net_protocol *net_protocol;
49 /** Link-layer protocol */
50 struct ll_protocol *ll_protocol;
51 /** Network-layer address */
52 uint8_t net_addr[MAX_NET_ADDR_LEN];
53 /** Link-layer address */
54 uint8_t ll_addr[MAX_LL_ADDR_LEN];
57 /** Number of entries in the ARP cache
59 * This is a global cache, covering all network interfaces,
60 * network-layer protocols and link-layer protocols.
62 #define NUM_ARP_ENTRIES 4
64 /** The ARP cache */
65 static struct arp_entry arp_table[NUM_ARP_ENTRIES];
66 #define arp_table_end &arp_table[NUM_ARP_ENTRIES]
68 static unsigned int next_new_arp_entry = 0;
70 struct net_protocol arp_protocol;
72 /**
73 * Find entry in the ARP cache
75 * @v ll_protocol Link-layer protocol
76 * @v net_protocol Network-layer protocol
77 * @v net_addr Network-layer address
78 * @ret arp ARP cache entry, or NULL if not found
81 static struct arp_entry *
82 arp_find_entry ( struct ll_protocol *ll_protocol,
83 struct net_protocol *net_protocol,
84 const void *net_addr ) {
85 struct arp_entry *arp;
87 for ( arp = arp_table ; arp < arp_table_end ; arp++ ) {
88 if ( ( arp->ll_protocol == ll_protocol ) &&
89 ( arp->net_protocol == net_protocol ) &&
90 ( memcmp ( arp->net_addr, net_addr,
91 net_protocol->net_addr_len ) == 0 ) )
92 return arp;
94 return NULL;
97 /**
98 * Look up media-specific link-layer address in the ARP cache
100 * @v netdev Network device
101 * @v net_protocol Network-layer protocol
102 * @v dest_net_addr Destination network-layer address
103 * @v source_net_addr Source network-layer address
104 * @ret dest_ll_addr Destination link layer address
105 * @ret rc Return status code
107 * This function will use the ARP cache to look up the link-layer
108 * address for the link-layer protocol associated with the network
109 * device and the given network-layer protocol and addresses. If
110 * found, the destination link-layer address will be filled in in @c
111 * dest_ll_addr.
113 * If no address is found in the ARP cache, an ARP request will be
114 * transmitted on the specified network device and -ENOENT will be
115 * returned.
117 int arp_resolve ( struct net_device *netdev, struct net_protocol *net_protocol,
118 const void *dest_net_addr, const void *source_net_addr,
119 void *dest_ll_addr ) {
120 struct ll_protocol *ll_protocol = netdev->ll_protocol;
121 const struct arp_entry *arp;
122 struct io_buffer *iobuf;
123 struct arphdr *arphdr;
124 int rc;
126 /* Look for existing entry in ARP table */
127 arp = arp_find_entry ( ll_protocol, net_protocol, dest_net_addr );
128 if ( arp ) {
129 DBG ( "ARP cache hit: %s %s => %s %s\n",
130 net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
131 ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
132 memcpy ( dest_ll_addr, arp->ll_addr, ll_protocol->ll_addr_len);
133 return 0;
135 DBG ( "ARP cache miss: %s %s\n", net_protocol->name,
136 net_protocol->ntoa ( dest_net_addr ) );
138 /* Allocate ARP packet */
139 iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
140 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) );
141 if ( ! iobuf )
142 return -ENOMEM;
143 iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
145 /* Build up ARP request */
146 arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
147 arphdr->ar_hrd = ll_protocol->ll_proto;
148 arphdr->ar_hln = ll_protocol->ll_addr_len;
149 arphdr->ar_pro = net_protocol->net_proto;
150 arphdr->ar_pln = net_protocol->net_addr_len;
151 arphdr->ar_op = htons ( ARPOP_REQUEST );
152 memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
153 netdev->ll_addr, ll_protocol->ll_addr_len );
154 memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
155 source_net_addr, net_protocol->net_addr_len );
156 memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
157 0, ll_protocol->ll_addr_len );
158 memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
159 dest_net_addr, net_protocol->net_addr_len );
161 /* Transmit ARP request */
162 if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
163 ll_protocol->ll_broadcast ) ) != 0 )
164 return rc;
166 return -ENOENT;
170 * Identify ARP protocol
172 * @v net_proto Network-layer protocol, in network-endian order
173 * @ret arp_net_protocol ARP protocol, or NULL
176 static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
177 struct arp_net_protocol *arp_net_protocol;
179 for ( arp_net_protocol = arp_net_protocols ;
180 arp_net_protocol < arp_net_protocols_end ; arp_net_protocol++ ) {
181 if ( arp_net_protocol->net_protocol->net_proto == net_proto ) {
182 return arp_net_protocol;
185 return NULL;
189 * Process incoming ARP packets
191 * @v iobuf I/O buffer
192 * @v netdev Network device
193 * @v ll_source Link-layer source address
194 * @ret rc Return status code
196 * This handles ARP requests and responses as detailed in RFC826. The
197 * method detailed within the RFC is pretty optimised, handling
198 * requests and responses with basically a single code path and
199 * avoiding the need for extraneous ARP requests; read the RFC for
200 * details.
202 static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
203 const void *ll_source __unused ) {
204 struct arphdr *arphdr = iobuf->data;
205 struct arp_net_protocol *arp_net_protocol;
206 struct net_protocol *net_protocol;
207 struct ll_protocol *ll_protocol;
208 struct arp_entry *arp;
209 int merge = 0;
211 /* Identify network-layer and link-layer protocols */
212 arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
213 if ( ! arp_net_protocol )
214 goto done;
215 net_protocol = arp_net_protocol->net_protocol;
216 ll_protocol = netdev->ll_protocol;
218 /* Sanity checks */
219 if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
220 ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
221 ( arphdr->ar_pln != net_protocol->net_addr_len ) )
222 goto done;
224 /* See if we have an entry for this sender, and update it if so */
225 arp = arp_find_entry ( ll_protocol, net_protocol,
226 arp_sender_pa ( arphdr ) );
227 if ( arp ) {
228 memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
229 arphdr->ar_hln );
230 merge = 1;
231 DBG ( "ARP cache update: %s %s => %s %s\n",
232 net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
233 ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
236 /* See if we own the target protocol address */
237 if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0)
238 goto done;
240 /* Create new ARP table entry if necessary */
241 if ( ! merge ) {
242 arp = &arp_table[next_new_arp_entry++ % NUM_ARP_ENTRIES];
243 arp->ll_protocol = ll_protocol;
244 arp->net_protocol = net_protocol;
245 memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
246 arphdr->ar_hln );
247 memcpy ( arp->net_addr, arp_sender_pa ( arphdr ),
248 arphdr->ar_pln);
249 DBG ( "ARP cache add: %s %s => %s %s\n",
250 net_protocol->name, net_protocol->ntoa ( arp->net_addr ),
251 ll_protocol->name, ll_protocol->ntoa ( arp->ll_addr ) );
254 /* If it's not a request, there's nothing more to do */
255 if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) )
256 goto done;
258 /* Change request to a reply */
259 DBG ( "ARP reply: %s %s => %s %s\n", net_protocol->name,
260 net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
261 ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
262 arphdr->ar_op = htons ( ARPOP_REPLY );
263 memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
264 arphdr->ar_hln + arphdr->ar_pln );
265 memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
267 /* Send reply */
268 net_tx ( iobuf, netdev, &arp_protocol, arp_target_ha (arphdr ) );
269 iobuf = NULL;
271 done:
272 free_iob ( iobuf );
273 return 0;
277 * Transcribe ARP address
279 * @v net_addr ARP address
280 * @ret string "<ARP>"
282 * This operation is meaningless for the ARP protocol.
284 static const char * arp_ntoa ( const void *net_addr __unused ) {
285 return "<ARP>";
288 /** ARP protocol */
289 struct net_protocol arp_protocol __net_protocol = {
290 .name = "ARP",
291 .net_proto = htons ( ETH_P_ARP ),
292 .rx = arp_rx,
293 .ntoa = arp_ntoa,