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",
87 ip->ihl, 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);
139 memcpy (resp->arp_sha,
140 (((struct ether_header *) (eth_arp_resp -
142 ether_header)))->ether_shost),
145 memcpy (resp->arp_spa, ip_addr, 4);
147 return sizeof (struct ether_arp);
151 resolve_eth (struct ether_header *eth, void *eth_resp)
153 struct ether_header *resp = (struct ether_header *) eth_resp;
154 memcpy (resp->ether_dhost, eth->ether_shost, 6);
158 for (i = 0; i < 6; i++)
162 memcpy (resp->ether_shost, hw_addr, 6);
164 resp->ether_type = eth->ether_type;
166 return sizeof (struct ether_header);
170 resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])
172 struct iphdr *resp = (struct iphdr *) ip_resp;
176 /*len updated later */
177 resp->tot_len = 0x0000;
182 ((uint8_t *) & resp->saddr)[0] = ip_addr[0];
183 ((uint8_t *) & resp->saddr)[1] = ip_addr[1];
184 ((uint8_t *) & resp->saddr)[2] = ip_addr[2];
185 ((uint8_t *) & resp->saddr)[3] = ip_addr[3];
186 resp->daddr = ip->saddr;
188 /* resp->check = cksum (resp, sizeof (struct iphdr)); */
190 return sizeof (struct iphdr);
194 resolve_icmp (struct icmphdr *icmp, void *icmp_resp)
196 struct icmphdr *resp = (struct icmphdr *) icmp_resp;
199 resp->un.echo.id = icmp->un.echo.id;
200 resp->un.echo.sequence = icmp->un.echo.sequence;
202 /*resp->checksum = cksum (resp, sizeof (struct icmphdr)); */
204 return sizeof (struct icmphdr);
208 resolve_packet (void *in_pck, ssize_t in_size,
209 void *out_pck, uint32_t * out_size, uint8_t ip_addr[4])
211 struct ether_header *eh;
212 struct ether_arp *eah;
213 struct iphdr *ip, *ip_out;
214 struct icmphdr *icmp;
217 if ((in_pck == NULL) || (out_pck == NULL))
220 eh = (struct ether_header *) in_pck;
221 *out_size = resolve_eth (eh, out_pck);
223 if (eh->ether_type == 0x0608)
225 eah = (struct ether_arp *) (in_pck + *out_size);
226 *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);
229 else if (eh->ether_type == 0x0008)
232 print_packet (in_pck + *out_size);
234 ip = (struct iphdr *) (in_pck + *out_size);
235 ip_out = (struct iphdr *) (out_pck + *out_size);
236 *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
237 if (ip->protocol == 1)
239 icmp = (struct icmphdr *) (in_pck + *out_size);
240 *out_size += resolve_icmp (icmp, out_pck + *out_size);
241 ((struct icmphdr *) (out_pck + *out_size -
242 sizeof (struct icmphdr)))->checksum =
243 cksum (out_pck + *out_size - sizeof (struct icmphdr),
244 sizeof (struct icmphdr));
246 memcpy (out_pck + *out_size, in_pck + *out_size,
247 in_size - *out_size);
250 __bswap_16 (*out_size - sizeof (struct ether_header));
251 ip_out->check = cksum (ip_out, sizeof (struct iphdr));
258 generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
262 for (i = 0; i < 6; i++)
266 memcpy (eh->ether_shost, hw_addr, 6);
267 memcpy (eh->ether_dhost, hw_daddr, 6);
269 eh->ether_type = 0x0008;
271 return sizeof (struct ether_header);
275 generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
280 /*len updated later */
281 ip->tot_len = 0x5400;
287 ((uint8_t *) & ip->saddr)[0] = saddr[0];
288 ((uint8_t *) & ip->saddr)[1] = saddr[1];
289 ((uint8_t *) & ip->saddr)[2] = saddr[2];
290 ((uint8_t *) & ip->saddr)[3] = saddr[3];
292 ((uint8_t *) & ip->daddr)[0] = daddr[0];
293 ((uint8_t *) & ip->daddr)[1] = daddr[1];
294 ((uint8_t *) & ip->daddr)[2] = daddr[2];
295 ((uint8_t *) & ip->daddr)[3] = daddr[3];
297 ip->check = cksum (ip, sizeof (struct iphdr));
299 return sizeof (struct iphdr);
303 generate_icmp (struct icmphdr *icmp, uint32_t seq)
305 icmp->type = ICMP_ECHO;
307 icmp->un.echo.id = 0;
308 icmp->un.echo.sequence = seq;
310 return sizeof (struct icmphdr);
314 generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
315 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
317 struct ether_header *eh;
319 struct icmphdr *icmp;
323 eh = (struct ether_header *) pck;
324 *size += generate_eth (eh, hw_daddr);
326 ip = (struct iphdr *) (pck + *size);
327 *size += generate_ip (ip, saddr, daddr);
329 icmp = (struct icmphdr *) (pck + *size);
330 *size += generate_icmp (icmp, seq);
332 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
333 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
335 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
337 ip->check = cksum (ip, sizeof (struct iphdr));
343 generate_packet2 (void *pck, uint32_t * size, uint8_t saddr[4],
344 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
345 icmpr_flow_mode_t mode)
347 struct ether_header *eh;
349 struct icmphdr *icmp;
353 if (mode == ICMPR_FLOW_MODE_ETH)
355 eh = (struct ether_header *) pck;
356 *size += generate_eth (eh, hw_daddr);
359 ip = (struct iphdr *) (pck + *size);
360 *size += generate_ip (ip, saddr, daddr);
362 icmp = (struct icmphdr *) (pck + *size);
363 *size += generate_icmp (icmp, seq);
365 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
366 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
368 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
370 ip->check = cksum (ip, sizeof (struct iphdr));
375 #define GET_HEADER(out,hdr,src,off) do { \
376 out = (hdr*)(src + off); \
377 off += sizeof (hdr); \
381 resolve_packet2 (void *pck, uint32_t * size, uint8_t ip_addr[4])
383 struct ether_header *eh;
384 struct ether_arp *eah;
386 struct icmphdr *icmp;
392 GET_HEADER (eh, struct ether_header, pck, offset);
394 memcpy (eh->ether_dhost, eh->ether_shost, 6);
395 memcpy (eh->ether_shost, "aaaaaa", 6);
397 if (eh->ether_type == 0x0608)
399 GET_HEADER (eah, struct ether_arp, pck, offset);
400 struct arphdr *arp = &eah->ea_hdr;
402 arp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
403 arp->ar_pro = __bswap_16 (0x0800);
408 arp->ar_op = __bswap_16 (ARPOP_REPLY);
410 memcpy (eah->arp_tha, eah->arp_sha, 6);
411 memcpy (eah->arp_tpa, eah->arp_spa, 4);
413 memcpy (eah->arp_sha, eh->ether_shost, 6);
414 memcpy (eah->arp_spa, ip_addr, 4);
417 else if (eh->ether_type == 0x0008)
419 GET_HEADER (ip, struct iphdr, pck, offset);
421 if (ip->protocol == 1)
426 ip->tot_len = 0x0000;
432 ip->daddr = ip->saddr;
433 ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
434 ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
435 ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
436 ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
438 GET_HEADER (icmp, struct icmphdr, pck, offset);
442 icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
444 /* rest is payload */
447 ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
448 ip->check = cksum (ip, sizeof (struct iphdr));
452 assert (offset == *size && "unsupported protocol");
458 resolve_packet3 (void **pck_, uint32_t * size, uint8_t ip_addr[4])
460 struct ether_header *eh;
462 struct icmphdr *icmp;
464 uint16_t encap_size = sizeof (struct ether_header);
471 offset -= encap_size;
473 GET_HEADER (eh, struct ether_header, pck, offset);
476 memset (hw_daddr, 0, sizeof (uint8_t) * 6);
478 generate_eth (eh, hw_daddr);
480 if (eh->ether_type == 0x0008)
482 GET_HEADER (ip, struct iphdr, pck, offset);
484 if (ip->protocol == 1)
489 ip->tot_len = 0x0000;
495 ip->daddr = ip->saddr;
496 ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
497 ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
498 ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
499 ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
501 GET_HEADER (icmp, struct icmphdr, pck, offset);
505 icmp->checksum = cksum (icmp, sizeof (struct icmphdr));
507 /* rest is payload */
510 ip->tot_len = __bswap_16 (offset - sizeof (struct ether_header));
511 ip->check = cksum (ip, sizeof (struct iphdr));
515 offset += encap_size;
517 assert (offset != *size &&
518 "new packet length must be increased by encap size");
520 /* overwrite packet size */