-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmp_arp.c
216 lines (184 loc) · 7.51 KB
/
icmp_arp.c
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
/**
* Copyright (c) 2018-present VoerEir AB - All Rights Reserved.
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Created by Ashok Kumar <[email protected]>, Dec 2018
**/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <rte_ethdev.h>
#include <rte_ip.h>
#include <rte_arp.h>
#include <rte_icmp.h>
#include <rte_udp.h>
#include <rte_mbuf.h>
#include "icmp.h"
#include "ip.h"
#include "utils.h"
uint16_t icmp6_cksum(struct icmp_hdr * icmp_hd, struct ipv6_hdr * ip6_hd)
{
uint32_t icmp_cksum;
icmp_cksum = ipv6_pseudohdr_sum(ip6_hd);
icmp_cksum += (icmp_hd->icmp_type << 8) + icmp_hd->icmp_code;
if(icmp_hd->icmp_type == NDP_NEIGHBOUR_SOLICITATION || icmp_hd->icmp_type == NDP_NEIGHBOUR_ADVERTISEMENT) {
struct ndp_hdr * ndp_hd;
ndp_hd = (struct ndp_hdr *) icmp_hd;
icmp_cksum += (ndp_hd->reserved[0] << 8) + ndp_hd->reserved[1];
icmp_cksum += (ndp_hd->reserved[2] << 8) + ndp_hd->reserved[3];
for(int i=IPV6_ADDR_LEN - 1; i>0; i-= 2) {
icmp_cksum += (ndp_hd->address[i-1] << 8) + ndp_hd->address[i];
}
}
/* reduce 32 bit checksum to 16 bits and complement it */
while (icmp_cksum > 0xFFFF) {
icmp_cksum = (icmp_cksum & 0xFFFF) + (icmp_cksum >> 16);
}
icmp_cksum = (~icmp_cksum) & 0xFFFF;
return (icmp_cksum == 0) ? 0xFFFF : (uint16_t) icmp_cksum;
}
/* generate an echo message from an ipv4 packet.
* ipv4 header field will not be changed */
void process_icmp_echo(struct port_conf *port, struct rte_mbuf *mbuf)
{
struct ipv4_hdr *ip_hdr;
struct icmp_hdr *icmp_hd;
uint16_t queue_id = 0;
struct rte_mbuf *mbuf_arr[1];
struct ether_addr eth_addr;
struct ether_hdr *eth_h;
uint32_t ip_addr;
uint32_t cksum;
eth_h = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
//ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct ipv4_hdr *, sizeof(struct ether_hdr)) + sizeof(struct ether_hdr) ;
ip_hdr = (struct ipv4_hdr *) ð_h[1];
//ipv4_hdr = (struct ipv4_hdr*) (rte_pktmbuf_mtod(mbuf, char *) + mbuf->l2_len);
icmp_hd = (struct icmp_hdr *) ((char *)ip_hdr + sizeof(struct ipv4_hdr));
/*
* Prepare ICMP echo reply to be sent back.
* - switch ethernet source and destinations addresses,
* - use the request IP source address as the reply IP
* destination address,
* - if the request IP destination address is a multicast
* address:
* - choose a reply IP source address different from the
* request IP source address,
* - re-compute the IP header checksum.
* Otherwise:
* - switch the request IP source and destination
* addresses in the reply IP header,
* - keep the IP header checksum unchanged.
* - set IP_ICMP_ECHO_REPLY in ICMP header.
* ICMP checksum is computed by assuming it is valid in the
* echo request and not verified.
*/
ether_addr_copy(ð_h->s_addr, ð_addr);
ether_addr_copy(ð_h->d_addr, ð_h->s_addr);
ether_addr_copy(ð_addr, ð_h->d_addr);
ip_addr = ip_hdr->src_addr;
if (is_multicast_ipv4_addr(ip_hdr->dst_addr)) {
uint32_t ip_src;
ip_src = rte_be_to_cpu_32(ip_addr);
if ((ip_src & 0x00000003) == 1)
ip_src = (ip_src & 0xFFFFFFFC) | 0x00000002;
else
ip_src = (ip_src & 0xFFFFFFFC) | 0x00000001;
ip_hdr->src_addr = rte_cpu_to_be_32(ip_src);
ip_hdr->dst_addr = ip_addr;
ip_hdr->hdr_checksum = ipv4_hdr_cksum(ip_hdr);
} else {
ip_hdr->src_addr = ip_hdr->dst_addr;
ip_hdr->dst_addr = ip_addr;
}
icmp_hd->icmp_type = IP_ICMP_ECHO_REPLY;
cksum = ~icmp_hd->icmp_cksum & 0xffff;
cksum += ~htons(IP_ICMP_ECHO_REQUEST << 8) & 0xffff;
cksum += htons(IP_ICMP_ECHO_REPLY << 8);
cksum = (cksum & 0xffff) + (cksum >> 16);
cksum = (cksum & 0xffff) + (cksum >> 16);
icmp_hd->icmp_cksum = ~cksum;
//no need to change buf->pkt_len
mbuf_arr[0] = mbuf;
rte_eth_tx_burst(port->port_id, port->queue_id, mbuf_arr, 1);
}
/* generate an icmp6 reply message from an ipv6 packet.
*/
void process_icmp6(struct port_conf *port, struct rte_mbuf *mbuf)
{
struct ipv6_hdr *ip6_hd;
struct icmp_hdr *icmp_hd;
uint16_t queue_id = 0;
struct rte_mbuf *mbuf_arr[1];
struct ether_hdr *eth_h;
uint8_t ip6_addr[IPV6_ADDR_LEN];
eth_h = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
ip6_hd = (struct ipv6_hdr *) ð_h[1];
// To convert the stream based interpretation to cpu interpretation
ip6_hd->payload_len = ntohs(ip6_hd->payload_len);
icmp_hd = (struct icmp_hdr *) &ip6_hd[1];
if(icmp_hd->icmp_type == NDP_NEIGHBOUR_SOLICITATION) {
struct ndp_hdr *ndp_hd;
char * p;
ndp_hd = (struct ndp_hdr *) &ip6_hd[1];
// To convert the stream based interpretation to cpu interpretation
ndp_hd->icmp_cksum = ntohs(ndp_hd->icmp_cksum);
memcpy(&ip6_addr, &ip6_hd->src_addr, IPV6_ADDR_LEN);
memcpy(&ip6_hd->src_addr, &port->ipaddr6, IPV6_ADDR_LEN);
memcpy(&ip6_hd->dst_addr, &ip6_addr, IPV6_ADDR_LEN);
// Setting flags that it is in response to solicited request and should refresh the cached-entry.
ndp_hd->reserved[0] = SOLICITED_FLAG | OVERRIDE_FLAG;
ndp_hd->icmp_type = NDP_NEIGHBOUR_ADVERTISEMENT;
ip6_hd->hop_limits = 255; // max limit
ether_addr_copy(ð_h->s_addr, ð_h->d_addr);
ether_addr_copy(&port->eth_addr, ð_h->s_addr);
ndp_hd->icmp_cksum = icmp6_cksum(icmp_hd, ip6_hd);
} else if(icmp_hd->icmp_type == IP_ICMP_ECHO_REQUEST && icmp_hd->icmp_code == 0) {
// TODO: pending reply for icmp echo
return;
}
// Reversing checksum after calculation
icmp_hd->icmp_cksum = htons(icmp_hd->icmp_cksum);
//no need to change buf->pkt_len
mbuf_arr[0] = mbuf;
rte_eth_tx_burst(port->port_id, port->queue_id, mbuf_arr, 1);
}
void process_arp(struct port_conf *port, struct rte_mbuf *mb)
{
struct rte_mbuf *mbuf_arr[1];
struct ether_hdr *eth_hdr;
struct ipv4_hdr *ip_hdr;
uint16_t arp_op;
uint16_t arp_pro;
struct arp_hdr *arp_h;
uint16_t queue_id = 0;
int l2_len;
uint32_t ip_addr;
eth_hdr = rte_pktmbuf_mtod(mb, struct ether_hdr *);
ip_hdr = (struct ipv4_hdr *) ð_hdr[1];
//ip_hdr = rte_pktmbuf_mtod_offset(mb, struct ipv4_hdr *, sizeof(struct ether_hdr));
l2_len = sizeof(struct ether_hdr);
arp_h = (struct arp_hdr *) ((char *)eth_hdr + l2_len);
arp_op = rte_cpu_to_be_16(arp_h->arp_op);
// Do not do anything if ARP is not requested.
if (arp_op != ARP_OP_REQUEST) {
rte_pktmbuf_free(mb);
return;
}
ipv4_uint32_t_addr_dump("\nARP Requested from ip=", arp_h->arp_data.arp_sip);
fflush(stdout);
/* Use source MAC address as destination MAC address. */
ether_addr_copy(ð_hdr->s_addr, ð_hdr->d_addr);
/* Set source MAC address with MAC address of TX port */
ether_addr_copy(&port->eth_addr, ð_hdr->s_addr);
arp_h->arp_op = rte_cpu_to_be_16(ARP_OP_REPLY);
ether_addr_copy(&arp_h->arp_data.arp_sha, &arp_h->arp_data.arp_tha);
ether_addr_copy(&port->eth_addr, &arp_h->arp_data.arp_sha);
/* Swap IP addresses in ARP payload */
ip_addr = arp_h->arp_data.arp_tip;
arp_h->arp_data.arp_tip = arp_h->arp_data.arp_sip;
arp_h->arp_data.arp_sip = port->ipaddr;
mbuf_arr[0] = mb;
rte_eth_tx_burst(port->port_id, port->queue_id, mbuf_arr, 1);
}