summaryrefslogtreecommitdiffstats
path: root/src/net/arp.c
blob: f739e39231e5d54a6da04e53eb8fc0403d6bfbce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <gpxe/if_ether.h>
#include <gpxe/if_arp.h>
#include <gpxe/pkbuff.h>
#include <gpxe/llh.h>
#include <gpxe/netdevice.h>
#include <gpxe/arp.h>

/** @file
 *
 * Address Resolution Protocol
 *
 * This file implements the address resolution protocol as defined in
 * RFC826.  The implementation is media-independent and
 * protocol-independent; it is not limited to Ethernet or to IPv4.
 *
 */

/** An ARP cache entry */
struct arp_entry {
	/** Network-layer protocol */
	uint16_t net_proto;
	/** Link-layer protocol */
	uint16_t ll_proto;
	/** Network-layer address */
	uint8_t net_addr[MAX_NET_ADDR_LEN];
	/** Link-layer address */
	uint8_t ll_addr[MAX_LLH_ADDR_LEN];
};

/** Number of entries in the ARP cache
 *
 * This is a global cache, covering all network interfaces,
 * network-layer protocols and link-layer protocols.
 */
#define NUM_ARP_ENTRIES 4

/** The ARP cache */
static struct arp_entry arp_table[NUM_ARP_ENTRIES];
#define arp_table_end &arp_table[NUM_ARP_ENTRIES]

static unsigned int next_new_arp_entry = 0;

/**
 * Find entry in the ARP cache
 *
 * @v ll_proto		Link-layer protocol
 * @v net_proto		Network-layer protocol
 * @v net_addr		Network-layer address
 * @v net_addr_len	Network-layer address length
 * @ret arp		ARP cache entry, or NULL if not found
 *
 */
static struct arp_entry *
arp_find_entry ( uint16_t ll_proto, uint16_t net_proto, const void *net_addr,
		 size_t net_addr_len ) {
	struct arp_entry *arp;

	for ( arp = arp_table ; arp < arp_table_end ; arp++ ) {
		if ( ( arp->ll_proto == ll_proto ) &&
		     ( arp->net_proto == net_proto ) &&
		     ( memcmp ( arp->net_addr, net_addr, net_addr_len ) == 0 ))
			return arp;
	}
	return NULL;
}

/**
 * Look up media-specific link-layer address in the ARP cache
 *
 * @v netdev		Network device
 * @v pkb		Packet buffer
 * @v ll_addr		Buffer to contain link-layer address
 * @ret rc		Return status code
 *
 * The packet buffer must start with a media-independent link-layer
 * header (a struct @c gpxehdr).  This function will use the ARP cache
 * to look up the link-layer address for the media corresponding to
 * @c netdev and the network-layer address as specified in @c gpxehdr.
 *
 * If no address is found in the ARP cache, an ARP request will be
 * transmitted, -ENOENT will be returned, and the packet buffer
 * contents will be undefined.
 */
int arp_resolve ( struct net_device *netdev, struct pk_buff *pkb,
		  void *ll_addr ) {
	struct gpxehdr *gpxehdr = pkb->data;
	const struct arp_entry *arp;
	struct net_interface *netif;
	struct arphdr *arphdr;

	/* Look for existing entry in ARP table */
	arp = arp_find_entry ( netdev->ll_proto, gpxehdr->net_proto,
			       gpxehdr->net_addr, gpxehdr->net_addr_len );
	if ( arp ) {
		memcpy ( ll_addr, arp->ll_addr, netdev->ll_addr_len );
		return 0;
	}

	/* Find interface for this protocol */
	netif = netdev_find_netif ( netdev, gpxehdr->net_proto );
	if ( ! netif )
		return -EAFNOSUPPORT;

	/* Build up ARP request */
	pkb_unput ( pkb, pkb_len ( pkb ) - sizeof ( *gpxehdr ) );
	arphdr = pkb_put ( pkb, sizeof ( *arphdr ) );
	arphdr->ar_hrd = netdev->ll_proto;
	arphdr->ar_hln = netdev->ll_addr_len;
	arphdr->ar_pro = gpxehdr->net_proto;
	arphdr->ar_pln = gpxehdr->net_addr_len;
	arphdr->ar_op = htons ( ARPOP_REQUEST );
	memcpy ( pkb_put ( pkb, netdev->ll_addr_len ),
		 netdev->ll_addr, netdev->ll_addr_len );
	memcpy ( pkb_put ( pkb, netif->net_addr_len ),
		 netif->net_addr, netif->net_addr_len );
	memset ( pkb_put ( pkb, netdev->ll_addr_len ),
		 0xff, netdev->ll_addr_len );
	memcpy ( pkb_put ( pkb, netif->net_addr_len ),
		 gpxehdr->net_addr, netif->net_addr_len );
	pkb_pull ( pkb, sizeof ( *gpxehdr ) );

	/* Locate ARP interface and send ARP request */
	netif = netdev_find_netif ( netdev, htons ( ETH_P_ARP ) );
	assert ( netif != NULL );
	netif_send ( netif, pkb );

	return -ENOENT;
}

/**
 * Process incoming ARP packets
 *
 * @v arp_netif		Network interface for ARP packets
 * @v pkb		Packet buffer
 * @ret rc		Return status code
 *
 * This handles ARP requests and responses as detailed in RFC826.  The
 * method detailed within the RFC is pretty optimised, handling
 * requests and responses with basically a single code path and
 * avoiding the need for extraneous ARP requests; read the RFC for
 * details.
 */
int arp_process ( struct net_interface *arp_netif, struct pk_buff *pkb ) {
	struct arphdr *arphdr = pkb->data;
	struct net_device *netdev = arp_netif->netdev;
	struct net_interface *netif;
	struct arp_entry *arp;
	int merge = 0;

	/* Check for correct link-layer protocol and length */
	if ( ( arphdr->ar_hrd != netdev->ll_proto ) ||
	     ( arphdr->ar_hln != netdev->ll_addr_len ) )
		return 0;

	/* See if we have an interface for this network-layer protocol */
	netif = netdev_find_netif ( netdev, arphdr->ar_pro );
	if ( ! netif )
		return 0;
	if ( arphdr->ar_pln != netif->net_addr_len )
		return 0;

	/* See if we have an entry for this sender, and update it if so */
	arp = arp_find_entry ( arphdr->ar_hrd, arphdr->ar_pro,
			       arp_sender_pa ( arphdr ), arphdr->ar_pln );
	if ( arp ) {
		memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
			 arphdr->ar_hln );
		merge = 1;
	}

	/* See if we are the target protocol address */
	if ( memcmp ( arp_target_pa ( arphdr ), netif->net_addr,
		      arphdr->ar_pln ) != 0 )
		return 0;

	/* Create new ARP table entry if necessary */
	if ( ! merge ) {
		arp = &arp_table[next_new_arp_entry++ % NUM_ARP_ENTRIES];
		arp->ll_proto = arphdr->ar_hrd;
		arp->net_proto = arphdr->ar_pro;
		memcpy ( arp->ll_addr, arp_sender_ha ( arphdr ),
			 arphdr->ar_hln );
		memcpy ( arp->net_addr, arp_sender_pa ( arphdr ),
			 arphdr->ar_pln );
	}

	/* If it's not a request, there's nothing more to do */
	if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) )
		return 0;

	/* Change request to a reply, and send it */
	arphdr->ar_op = htons ( ARPOP_REPLY );
	memcpy ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
		 arphdr->ar_hln + arphdr->ar_pln );
	memcpy ( arp_target_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
	memcpy ( arp_target_pa ( arphdr ), netif->net_addr, arphdr->ar_pln );
	netif_send ( arp_netif, pkb );

	return 0;
}

/**
 * Add media-independent link-layer header
 *
 * @v arp_netif		Network interface for ARP packets
 * @v pkb		Packet buffer
 * @ret rc		Return status code
 */
int arp_add_generic_header ( struct net_interface *arp_netif __unused,
			     struct pk_buff *pkb ) {
	struct arphdr *arphdr = pkb->data;
	struct gpxehdr *gpxehdr;

	/* We're ARP; we always know the raw link-layer address we want */
	gpxehdr = pkb_push ( pkb, sizeof ( *gpxehdr ) );
	gpxehdr->net_proto = htons ( ETH_P_ARP );
	gpxehdr->flags = GPXE_FL_RAW;
	gpxehdr->net_addr_len = arphdr->ar_hln;
	memcpy ( gpxehdr->net_addr, arp_target_ha ( arphdr ), arphdr->ar_hln );
	
	return 0;
}