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