2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
20 #include <rte_ether.h>
22 #define ARP_REQUEST 0x100
23 #define ARP_REPLY 0x200
26 struct ether_addr sha; /* Sender hardware address */
27 uint32_t spa; /* Sender protocol address */
28 struct ether_addr tha; /* Target hardware address */
29 uint32_t tpa; /* Target protocol address */
30 } __attribute__((__packed__));
31 typedef struct _arp_ipv4 arp_ipv4_t;
40 } __attribute__((__packed__));
42 struct ether_hdr_arp {
43 struct ether_hdr ether_hdr;
47 static int arp_is_gratuitous(struct ether_hdr_arp *hdr)
49 return hdr->arp.data.spa == hdr->arp.data.tpa;
52 static inline void prepare_arp_reply(struct ether_hdr_arp *hdr_arp, struct ether_addr *s_addr)
54 uint32_t ip_source = hdr_arp->arp.data.spa;
56 hdr_arp->arp.data.spa = hdr_arp->arp.data.tpa;
57 hdr_arp->arp.data.tpa = ip_source;
58 hdr_arp->arp.oper = 0x200;
59 memcpy(&hdr_arp->arp.data.tha, &hdr_arp->arp.data.sha, sizeof(struct ether_addr));
60 memcpy(&hdr_arp->arp.data.sha, s_addr, sizeof(struct ether_addr));
63 static void create_mac(struct ether_hdr_arp *hdr, struct ether_addr *addr)
65 addr->addr_bytes[0] = 0x2;
66 addr->addr_bytes[1] = 0;
67 // Instead of sending a completely random MAC address, create the following MAC:
68 // 02:00:x1:x2:x3:x4 where x1:x2:x3:x4 is the IP address
69 memcpy(addr->addr_bytes + 2, (uint32_t *)&hdr->arp.data.tpa, 4);