[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / net / arp.c
1 /*
2 * Copyright (C) 2006 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 <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <byteswap.h>
26 #include <errno.h>
27 #include <ipxe/if_ether.h>
28 #include <ipxe/if_arp.h>
29 #include <ipxe/iobuf.h>
30 #include <ipxe/netdevice.h>
31 #include <ipxe/neighbour.h>
32 #include <ipxe/arp.h>
33
34 /** @file
35 *
36 * Address Resolution Protocol
37 *
38 * This file implements the address resolution protocol as defined in
39 * RFC826. The implementation is media-independent and
40 * protocol-independent; it is not limited to Ethernet or to IPv4.
41 *
42 */
43
44 struct net_protocol arp_protocol __net_protocol;
45
46 /**
47 * Transmit ARP request
48 *
49 * @v netdev Network device
50 * @v net_protocol Network-layer protocol
51 * @v net_dest Destination network-layer address
52 * @v net_source Source network-layer address
53 * @ret rc Return status code
54 */
55 static int arp_tx_request ( struct net_device *netdev,
56 struct net_protocol *net_protocol,
57 const void *net_dest, const void *net_source ) {
58 struct ll_protocol *ll_protocol = netdev->ll_protocol;
59 struct io_buffer *iobuf;
60 struct arphdr *arphdr;
61 int rc;
62
63 /* Allocate ARP packet */
64 iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( *arphdr ) +
65 ( 2 * ( MAX_LL_ADDR_LEN + MAX_NET_ADDR_LEN ) ) );
66 if ( ! iobuf )
67 return -ENOMEM;
68 iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
69
70 /* Build up ARP request */
71 arphdr = iob_put ( iobuf, sizeof ( *arphdr ) );
72 arphdr->ar_hrd = ll_protocol->ll_proto;
73 arphdr->ar_hln = ll_protocol->ll_addr_len;
74 arphdr->ar_pro = net_protocol->net_proto;
75 arphdr->ar_pln = net_protocol->net_addr_len;
76 arphdr->ar_op = htons ( ARPOP_REQUEST );
77 memcpy ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
78 netdev->ll_addr, ll_protocol->ll_addr_len );
79 memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
80 net_source, net_protocol->net_addr_len );
81 memset ( iob_put ( iobuf, ll_protocol->ll_addr_len ),
82 0, ll_protocol->ll_addr_len );
83 memcpy ( iob_put ( iobuf, net_protocol->net_addr_len ),
84 net_dest, net_protocol->net_addr_len );
85
86 /* Transmit ARP request */
87 if ( ( rc = net_tx ( iobuf, netdev, &arp_protocol,
88 netdev->ll_broadcast, netdev->ll_addr ) ) != 0 ) {
89 DBGC ( netdev, "ARP %s %s %s could not transmit request: %s\n",
90 netdev->name, net_protocol->name,
91 net_protocol->ntoa ( net_dest ), strerror ( rc ) );
92 return rc;
93 }
94
95 return 0;
96 }
97
98 /** ARP neighbour discovery protocol */
99 struct neighbour_discovery arp_discovery = {
100 .name = "ARP",
101 .tx_request = arp_tx_request,
102 };
103
104 /**
105 * Identify ARP protocol
106 *
107 * @v net_proto Network-layer protocol, in network-endian order
108 * @ret arp_net_protocol ARP protocol, or NULL
109 *
110 */
111 static struct arp_net_protocol * arp_find_protocol ( uint16_t net_proto ) {
112 struct arp_net_protocol *arp_net_protocol;
113
114 for_each_table_entry ( arp_net_protocol, ARP_NET_PROTOCOLS ) {
115 if ( arp_net_protocol->net_protocol->net_proto == net_proto )
116 return arp_net_protocol;
117 }
118 return NULL;
119 }
120
121 /**
122 * Process incoming ARP packets
123 *
124 * @v iobuf I/O buffer
125 * @v netdev Network device
126 * @v ll_source Link-layer source address
127 * @v flags Packet flags
128 * @ret rc Return status code
129 */
130 static int arp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
131 const void *ll_dest __unused,
132 const void *ll_source __unused,
133 unsigned int flags __unused ) {
134 struct arphdr *arphdr = iobuf->data;
135 struct arp_net_protocol *arp_net_protocol;
136 struct net_protocol *net_protocol;
137 struct ll_protocol *ll_protocol;
138 int rc;
139
140 /* Identify network-layer and link-layer protocols */
141 arp_net_protocol = arp_find_protocol ( arphdr->ar_pro );
142 if ( ! arp_net_protocol ) {
143 rc = -EPROTONOSUPPORT;
144 goto done;
145 }
146 net_protocol = arp_net_protocol->net_protocol;
147 ll_protocol = netdev->ll_protocol;
148
149 /* Sanity checks */
150 if ( ( arphdr->ar_hrd != ll_protocol->ll_proto ) ||
151 ( arphdr->ar_hln != ll_protocol->ll_addr_len ) ||
152 ( arphdr->ar_pln != net_protocol->net_addr_len ) ) {
153 rc = -EINVAL;
154 goto done;
155 }
156
157 /* Update neighbour cache entry for this sender, if any */
158 neighbour_update ( netdev, net_protocol, arp_sender_pa ( arphdr ),
159 arp_sender_ha ( arphdr ) );
160
161 /* If it's not a request, there's nothing more to do */
162 if ( arphdr->ar_op != htons ( ARPOP_REQUEST ) ) {
163 rc = 0;
164 goto done;
165 }
166
167 /* See if we own the target protocol address */
168 if ( arp_net_protocol->check ( netdev, arp_target_pa ( arphdr ) ) != 0){
169 rc = 0;
170 goto done;
171 }
172
173 /* Change request to a reply */
174 DBGC2 ( netdev, "ARP %s %s %s reply => %s %s\n",
175 netdev->name, net_protocol->name,
176 net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
177 ll_protocol->name, ll_protocol->ntoa ( netdev->ll_addr ) );
178 arphdr->ar_op = htons ( ARPOP_REPLY );
179 memswap ( arp_sender_ha ( arphdr ), arp_target_ha ( arphdr ),
180 arphdr->ar_hln + arphdr->ar_pln );
181 memcpy ( arp_sender_ha ( arphdr ), netdev->ll_addr, arphdr->ar_hln );
182
183 /* Send reply */
184 if ( ( rc = net_tx ( iob_disown ( iobuf ), netdev, &arp_protocol,
185 arp_target_ha ( arphdr ),
186 netdev->ll_addr ) ) != 0 ) {
187 DBGC ( netdev, "ARP %s %s %s could not transmit reply: %s\n",
188 netdev->name, net_protocol->name,
189 net_protocol->ntoa ( arp_target_pa ( arphdr ) ),
190 strerror ( rc ) );
191 goto done;
192 }
193
194 /* Success */
195 rc = 0;
196
197 done:
198 free_iob ( iobuf );
199 return rc;
200 }
201
202 /**
203 * Transcribe ARP address
204 *
205 * @v net_addr ARP address
206 * @ret string "<ARP>"
207 *
208 * This operation is meaningless for the ARP protocol.
209 */
210 static const char * arp_ntoa ( const void *net_addr __unused ) {
211 return "<ARP>";
212 }
213
214 /** ARP network protocol */
215 struct net_protocol arp_protocol __net_protocol = {
216 .name = "ARP",
217 .net_proto = htons ( ETH_P_ARP ),
218 .rx = arp_rx,
219 .ntoa = arp_ntoa,
220 };