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 generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
206 for (i = 0; i < 6; i++)
210 memcpy (eh->ether_shost, hw_addr, 6);
211 memcpy (eh->ether_dhost, hw_daddr, 6);
213 eh->ether_type = 0x0008;
215 return sizeof (struct ether_header);
219 generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
224 /*len updated later */
225 ip->tot_len = 0x5400;
231 ((uint8_t *) &ip->saddr)[0] = saddr[0];
232 ((uint8_t *) &ip->saddr)[1] = saddr[1];
233 ((uint8_t *) &ip->saddr)[2] = saddr[2];
234 ((uint8_t *) &ip->saddr)[3] = saddr[3];
236 ((uint8_t *) &ip->daddr)[0] = daddr[0];
237 ((uint8_t *) &ip->daddr)[1] = daddr[1];
238 ((uint8_t *) &ip->daddr)[2] = daddr[2];
239 ((uint8_t *) &ip->daddr)[3] = daddr[3];
241 ip->check = cksum (ip, sizeof (struct iphdr));
243 return sizeof (struct iphdr);
247 generate_icmp (struct icmphdr *icmp, uint32_t seq)
249 icmp->type = ICMP_ECHO;
251 icmp->un.echo.id = 0;
252 icmp->un.echo.sequence = seq;
254 return sizeof (struct icmphdr);
258 generate_packet (void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4],
259 uint8_t hw_daddr[6], uint32_t seq)
261 struct ether_header *eh;
263 struct icmphdr *icmp;
267 eh = (struct ether_header *) pck;
268 *size += generate_eth (eh, hw_daddr);
270 ip = (struct iphdr *) (pck + *size);
271 *size += generate_ip (ip, saddr, daddr);
273 icmp = (struct icmphdr *) (pck + *size);
274 *size += generate_icmp (icmp, seq);
276 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
277 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
279 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
281 ip->check = cksum (ip, sizeof (struct iphdr));
287 generate_packet2 (void *pck, uint32_t *size, uint8_t saddr[4],
288 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
289 icmpr_flow_mode_t mode)
291 struct ether_header *eh;
293 struct icmphdr *icmp;
297 if (mode == ICMPR_FLOW_MODE_ETH)
299 eh = (struct ether_header *) pck;
300 *size += generate_eth (eh, hw_daddr);
303 ip = (struct iphdr *) (pck + *size);
304 *size += generate_ip (ip, saddr, daddr);
306 icmp = (struct icmphdr *) (pck + *size);
307 *size += generate_icmp (icmp, seq);
309 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
310 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
312 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
314 ip->check = cksum (ip, sizeof (struct iphdr));
319 #define GET_HEADER(out, hdr, src, off) \
322 out = (hdr *) (src + off); \
323 off += sizeof (hdr); \
328 resolve_packet (void *pck, uint32_t *size, uint8_t ip_addr[4],
331 struct ether_header *eh;
332 struct ether_arp *eah;
334 struct icmphdr *icmp;
344 GET_HEADER (eh, struct ether_header, pck, offset);
346 memcpy (eh->ether_dhost, eh->ether_shost, 6);
347 memcpy (eh->ether_shost, hw_addr, 6);
349 if (eh->ether_type == 0x0608)
351 GET_HEADER (eah, struct ether_arp, pck, offset);
352 struct arphdr *arp = &eah->ea_hdr;
354 arp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
355 arp->ar_pro = __bswap_16 (0x0800);
360 arp->ar_op = __bswap_16 (ARPOP_REPLY);
362 memcpy (eah->arp_tha, eah->arp_sha, 6);
363 memcpy (eah->arp_tpa, eah->arp_spa, 4);
365 memcpy (eah->arp_sha, eh->ether_shost, 6);
366 memcpy (eah->arp_spa, ip_addr, 4);
369 else if (eh->ether_type == 0x0008)
371 GET_HEADER (ip, struct iphdr, pck, offset);
373 if (ip->protocol == 1)
378 ip->tot_len = 0x0000;
384 ip->daddr = ip->saddr;
385 ((uint8_t *) &ip->saddr)[0] = ip_addr[0];
386 ((uint8_t *) &ip->saddr)[1] = ip_addr[1];
387 ((uint8_t *) &ip->saddr)[2] = ip_addr[2];
388 ((uint8_t *) &ip->saddr)[3] = ip_addr[3];
390 GET_HEADER (icmp, struct icmphdr, pck, offset);
394 icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
396 /* rest is payload */
399 ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
400 ip->check = cksum (ip, sizeof (struct iphdr));
404 assert (offset == *size && "unsupported protocol");
409 resolve_packet_with_encap (void **pck_, uint32_t *size, uint8_t ip_addr[4])
411 struct ether_header *eh;
413 struct icmphdr *icmp;
415 uint16_t encap_size = sizeof (struct ether_header);
422 offset -= encap_size;
424 GET_HEADER (eh, struct ether_header, pck, offset);
427 memset (hw_daddr, 0, sizeof (uint8_t) * 6);
429 generate_eth (eh, hw_daddr);
431 if (eh->ether_type == 0x0008)
433 GET_HEADER (ip, struct iphdr, pck, offset);
435 if (ip->protocol == 1)
440 ip->tot_len = 0x0000;
446 ip->daddr = ip->saddr;
447 ((uint8_t *) &ip->saddr)[0] = ip_addr[0];
448 ((uint8_t *) &ip->saddr)[1] = ip_addr[1];
449 ((uint8_t *) &ip->saddr)[2] = ip_addr[2];
450 ((uint8_t *) &ip->saddr)[3] = ip_addr[3];
452 GET_HEADER (icmp, struct icmphdr, pck, offset);
456 icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
458 /* rest is payload */
461 ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
462 ip->check = cksum (ip, sizeof (struct iphdr));
466 offset += encap_size;
468 assert (offset != *size &&
469 "new packet length must be increased by encap size");
471 /* overwrite packet size */