279bdada1d0bcf76f0e0a07b881aaa1324ed34d5
[samplevnf.git] / VNFs / DPPD-PROX / arp.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #ifndef _ARP_H_
18 #define _ARP_H_
19
20 #include <rte_ether.h>
21
22 #define ARP_REQUEST     0x100
23 #define ARP_REPLY       0x200
24
25 struct _arp_ipv4 {
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;
32
33 struct my_arp_t {
34         uint16_t   htype;
35         uint16_t   ptype;
36         uint8_t    hlen;
37         uint8_t    plen;
38         uint16_t   oper;
39         arp_ipv4_t data;
40 } __attribute__((__packed__));
41
42 struct ether_hdr_arp {
43         struct ether_hdr ether_hdr;
44         struct my_arp_t arp;
45 };
46
47 static int arp_is_gratuitous(struct ether_hdr_arp *hdr)
48 {
49         return hdr->arp.data.spa == hdr->arp.data.tpa;
50 }
51
52 static inline void prepare_arp_reply(struct ether_hdr_arp *hdr_arp, struct ether_addr *s_addr)
53 {
54         uint32_t ip_source = hdr_arp->arp.data.spa;
55
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));
61 }
62
63 static void create_mac(struct ether_hdr_arp *hdr, struct ether_addr *addr)
64 {
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);
70 }
71
72 #endif /* _ARP_H_ */