2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
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.
15 *------------------------------------------------------------------
20 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
27 #include <sys/prctl.h>
33 #include <linux/icmp.h>
34 #include <arpa/inet.h>
36 #include <netinet/if_ether.h>
37 #include <net/if_arp.h>
38 #include <asm/byteorder.h>
42 #include <icmp_proto.h>
45 cksum (void *addr, ssize_t len)
47 char *data = (char *) addr;
49 uint32_t acc = 0xffff;
52 for (i = 0; (i + 1) < len; i += 2)
55 memcpy (&word, data + i, 2);
64 memcpy (&word, data + len - 1, 1);
73 print_packet (void *pck)
77 printf ("ICMP_PROTO: no data\n");
82 ip = (struct iphdr *) pck;
83 icmp = (struct icmphdr *) (pck + sizeof (struct iphdr));
84 printf ("received packet:\n");
85 printf ("\tiphdr:\n");
86 printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n", ip->ihl,
87 ip->version, __bswap_16 (ip->tot_len), ip->id);
88 printf ("\t\tprotocol: %u\n", ip->protocol);
90 printf ("\t\tsaddr: ");
92 for (i = 0; i < 4; i++)
94 printf ("%u.", ((uint8_t *) &ip->saddr)[i]);
98 printf ("\t\tdaddr: ");
99 for (i = 0; i < 4; i++)
101 printf ("%u.", ((uint8_t *) &ip->daddr)[i]);
104 printf ("\ticmphdr:\n");
105 printf ("\t\ttype: %s\n",
106 (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY");
112 resolve_arp (void *arp)
114 struct arphdr *resp = (struct arphdr *) arp;
116 resp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
118 resp->ar_pro = __bswap_16 (0x0800);
123 resp->ar_op = __bswap_16 (ARPOP_REPLY);
125 return sizeof (struct arphdr);
129 resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp,
132 struct ether_arp *resp = (struct ether_arp *) eth_arp_resp;
134 resolve_arp (&resp->ea_hdr);
136 memcpy (resp->arp_tha, eth_arp->arp_sha, 6);
137 memcpy (resp->arp_tpa, eth_arp->arp_spa, 4);
141 (((struct ether_header *) (eth_arp_resp - sizeof (struct ether_header)))
145 memcpy (resp->arp_spa, ip_addr, 4);
147 return sizeof (struct ether_arp);
151 resolve_eth (struct ether_header *eth, void *eth_resp, uint8_t hw_addr[6])
153 struct ether_header *resp = (struct ether_header *) eth_resp;
154 memcpy (resp->ether_dhost, eth->ether_shost, 6);
156 memcpy (resp->ether_shost, hw_addr, 6);
158 resp->ether_type = eth->ether_type;
160 return sizeof (struct ether_header);
164 resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])
166 struct iphdr *resp = (struct iphdr *) ip_resp;
170 /*len updated later */
171 resp->tot_len = 0x0000;
176 ((uint8_t *) &resp->saddr)[0] = ip_addr[0];
177 ((uint8_t *) &resp->saddr)[1] = ip_addr[1];
178 ((uint8_t *) &resp->saddr)[2] = ip_addr[2];
179 ((uint8_t *) &resp->saddr)[3] = ip_addr[3];
180 resp->daddr = ip->saddr;
182 /* resp->check = cksum (resp, sizeof (struct iphdr)); */
184 return sizeof (struct iphdr);
188 resolve_icmp (struct icmphdr *icmp, void *icmp_resp)
190 struct icmphdr *resp = (struct icmphdr *) icmp_resp;
193 resp->un.echo.id = icmp->un.echo.id;
194 resp->un.echo.sequence = icmp->un.echo.sequence;
196 /*resp->checksum = cksum (resp, sizeof (struct icmphdr)); */
198 return sizeof (struct icmphdr);
202 resolve_packet (void *in_pck, ssize_t in_size, void *out_pck,
203 uint32_t *out_size, uint8_t ip_addr[4], uint8_t hw_addr[6])
205 struct ether_header *eh;
206 struct ether_arp *eah;
207 struct iphdr *ip, *ip_out;
208 struct icmphdr *icmp;
211 if ((in_pck == NULL) || (out_pck == NULL))
214 eh = (struct ether_header *) in_pck;
215 *out_size = resolve_eth (eh, out_pck, hw_addr);
217 if (eh->ether_type == 0x0608)
219 eah = (struct ether_arp *) (in_pck + *out_size);
220 *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);
222 else if (eh->ether_type == 0x0008)
225 print_packet (in_pck + *out_size);
227 ip = (struct iphdr *) (in_pck + *out_size);
228 ip_out = (struct iphdr *) (out_pck + *out_size);
229 *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
230 if (ip->protocol == 1)
232 icmp = (struct icmphdr *) (in_pck + *out_size);
233 *out_size += resolve_icmp (icmp, out_pck + *out_size);
234 ((struct icmphdr *) (out_pck + *out_size - sizeof (struct icmphdr)))
235 ->checksum = cksum (out_pck + *out_size - sizeof (struct icmphdr),
236 sizeof (struct icmphdr));
238 memcpy (out_pck + *out_size, in_pck + *out_size,
239 in_size - *out_size);
242 __bswap_16 (*out_size - sizeof (struct ether_header));
243 ip_out->check = cksum (ip_out, sizeof (struct iphdr));
250 generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
254 for (i = 0; i < 6; i++)
258 memcpy (eh->ether_shost, hw_addr, 6);
259 memcpy (eh->ether_dhost, hw_daddr, 6);
261 eh->ether_type = 0x0008;
263 return sizeof (struct ether_header);
267 generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
272 /*len updated later */
273 ip->tot_len = 0x5400;
279 ((uint8_t *) &ip->saddr)[0] = saddr[0];
280 ((uint8_t *) &ip->saddr)[1] = saddr[1];
281 ((uint8_t *) &ip->saddr)[2] = saddr[2];
282 ((uint8_t *) &ip->saddr)[3] = saddr[3];
284 ((uint8_t *) &ip->daddr)[0] = daddr[0];
285 ((uint8_t *) &ip->daddr)[1] = daddr[1];
286 ((uint8_t *) &ip->daddr)[2] = daddr[2];
287 ((uint8_t *) &ip->daddr)[3] = daddr[3];
289 ip->check = cksum (ip, sizeof (struct iphdr));
291 return sizeof (struct iphdr);
295 generate_icmp (struct icmphdr *icmp, uint32_t seq)
297 icmp->type = ICMP_ECHO;
299 icmp->un.echo.id = 0;
300 icmp->un.echo.sequence = seq;
302 return sizeof (struct icmphdr);
306 generate_packet (void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4],
307 uint8_t hw_daddr[6], uint32_t seq)
309 struct ether_header *eh;
311 struct icmphdr *icmp;
315 eh = (struct ether_header *) pck;
316 *size += generate_eth (eh, hw_daddr);
318 ip = (struct iphdr *) (pck + *size);
319 *size += generate_ip (ip, saddr, daddr);
321 icmp = (struct icmphdr *) (pck + *size);
322 *size += generate_icmp (icmp, seq);
324 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
325 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
327 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
329 ip->check = cksum (ip, sizeof (struct iphdr));
335 generate_packet2 (void *pck, uint32_t *size, uint8_t saddr[4],
336 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
337 icmpr_flow_mode_t mode)
339 struct ether_header *eh;
341 struct icmphdr *icmp;
345 if (mode == ICMPR_FLOW_MODE_ETH)
347 eh = (struct ether_header *) pck;
348 *size += generate_eth (eh, hw_daddr);
351 ip = (struct iphdr *) (pck + *size);
352 *size += generate_ip (ip, saddr, daddr);
354 icmp = (struct icmphdr *) (pck + *size);
355 *size += generate_icmp (icmp, seq);
357 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
358 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
360 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
362 ip->check = cksum (ip, sizeof (struct iphdr));
367 #define GET_HEADER(out, hdr, src, off) \
370 out = (hdr *) (src + off); \
371 off += sizeof (hdr); \
376 resolve_packet_zero_copy (void *pck, uint32_t *size, uint8_t ip_addr[4],
379 struct ether_header *eh;
380 struct ether_arp *eah;
382 struct icmphdr *icmp;
388 GET_HEADER (eh, struct ether_header, pck, offset);
390 memcpy (eh->ether_dhost, eh->ether_shost, 6);
391 memcpy (eh->ether_shost, hw_addr, 6);
393 if (eh->ether_type == 0x0608)
395 GET_HEADER (eah, struct ether_arp, pck, offset);
396 struct arphdr *arp = &eah->ea_hdr;
398 arp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
399 arp->ar_pro = __bswap_16 (0x0800);
404 arp->ar_op = __bswap_16 (ARPOP_REPLY);
406 memcpy (eah->arp_tha, eah->arp_sha, 6);
407 memcpy (eah->arp_tpa, eah->arp_spa, 4);
409 memcpy (eah->arp_sha, eh->ether_shost, 6);
410 memcpy (eah->arp_spa, ip_addr, 4);
413 else if (eh->ether_type == 0x0008)
415 GET_HEADER (ip, struct iphdr, pck, offset);
417 if (ip->protocol == 1)
422 ip->tot_len = 0x0000;
428 ip->daddr = ip->saddr;
429 ((uint8_t *) &ip->saddr)[0] = ip_addr[0];
430 ((uint8_t *) &ip->saddr)[1] = ip_addr[1];
431 ((uint8_t *) &ip->saddr)[2] = ip_addr[2];
432 ((uint8_t *) &ip->saddr)[3] = ip_addr[3];
434 GET_HEADER (icmp, struct icmphdr, pck, offset);
438 icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
440 /* rest is payload */
443 ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
444 ip->check = cksum (ip, sizeof (struct iphdr));
448 assert (offset == *size && "unsupported protocol");
453 resolve_packet_zero_copy_add_encap (void **pck_, uint32_t *size,
456 struct ether_header *eh;
458 struct icmphdr *icmp;
460 uint16_t encap_size = sizeof (struct ether_header);
467 offset -= encap_size;
469 GET_HEADER (eh, struct ether_header, pck, offset);
472 memset (hw_daddr, 0, sizeof (uint8_t) * 6);
474 generate_eth (eh, hw_daddr);
476 if (eh->ether_type == 0x0008)
478 GET_HEADER (ip, struct iphdr, pck, offset);
480 if (ip->protocol == 1)
485 ip->tot_len = 0x0000;
491 ip->daddr = ip->saddr;
492 ((uint8_t *) &ip->saddr)[0] = ip_addr[0];
493 ((uint8_t *) &ip->saddr)[1] = ip_addr[1];
494 ((uint8_t *) &ip->saddr)[2] = ip_addr[2];
495 ((uint8_t *) &ip->saddr)[3] = ip_addr[3];
497 GET_HEADER (icmp, struct icmphdr, pck, offset);
501 icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
503 /* rest is payload */
506 ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
507 ip->check = cksum (ip, sizeof (struct iphdr));
511 offset += encap_size;
513 assert (offset != *size &&
514 "new packet length must be increased by encap size");
516 /* overwrite packet size */