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>
41 #include <icmp_proto.h>
44 cksum (void *addr, ssize_t len)
46 char *data = (char *) addr;
48 uint32_t acc = 0xffff;
51 for (i = 0; (i + 1) < len; i += 2)
54 memcpy (&word, data + i, 2);
63 memcpy (&word, data + len - 1, 1);
72 print_packet (void *pck)
76 printf ("ICMP_PROTO: no data\n");
81 ip = (struct iphdr *) pck;
82 icmp = (struct icmphdr *) (pck + sizeof (struct iphdr));
83 printf ("received packet:\n");
84 printf ("\tiphdr:\n");
85 printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n",
86 ip->ihl, ip->version, __bswap_16 (ip->tot_len), ip->id);
87 printf ("\t\tprotocol: %u\n", ip->protocol);
89 printf ("\t\tsaddr: ");
91 for (i = 0; i < 4; i++)
93 printf ("%u.", ((uint8_t *) & ip->saddr)[i]);
97 printf ("\t\tdaddr: ");
98 for (i = 0; i < 4; i++)
100 printf ("%u.", ((uint8_t *) & ip->daddr)[i]);
103 printf ("\ticmphdr:\n");
104 printf ("\t\ttype: %s\n",
105 (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY");
111 resolve_arp (void *arp)
113 struct arphdr *resp = (struct arphdr *) arp;
115 resp->ar_hrd = __bswap_16 (ARPHRD_ETHER);
117 resp->ar_pro = __bswap_16 (0x0800);
122 resp->ar_op = __bswap_16 (ARPOP_REPLY);
124 return sizeof (struct arphdr);
128 resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp,
131 struct ether_arp *resp = (struct ether_arp *) eth_arp_resp;
133 resolve_arp (&resp->ea_hdr);
135 memcpy (resp->arp_tha, eth_arp->arp_sha, 6);
136 memcpy (resp->arp_tpa, eth_arp->arp_spa, 4);
138 memcpy (resp->arp_sha,
139 (((struct ether_header *) (eth_arp_resp -
141 ether_header)))->ether_shost),
144 memcpy (resp->arp_spa, ip_addr, 4);
146 return sizeof (struct ether_arp);
150 resolve_eth (struct ether_header *eth, void *eth_resp)
152 struct ether_header *resp = (struct ether_header *) eth_resp;
153 memcpy (resp->ether_dhost, eth->ether_shost, 6);
157 for (i = 0; i < 6; i++)
161 memcpy (resp->ether_shost, hw_addr, 6);
163 resp->ether_type = eth->ether_type;
165 return sizeof (struct ether_header);
169 resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])
171 struct iphdr *resp = (struct iphdr *) ip_resp;
175 /*len updated later */
176 resp->tot_len = 0x0000;
181 ((uint8_t *) & resp->saddr)[0] = ip_addr[0];
182 ((uint8_t *) & resp->saddr)[1] = ip_addr[1];
183 ((uint8_t *) & resp->saddr)[2] = ip_addr[2];
184 ((uint8_t *) & resp->saddr)[3] = ip_addr[3];
185 resp->daddr = ip->saddr;
187 /* resp->check = cksum (resp, sizeof (struct iphdr)); */
189 return sizeof (struct iphdr);
193 resolve_icmp (struct icmphdr *icmp, void *icmp_resp)
195 struct icmphdr *resp = (struct icmphdr *) icmp_resp;
198 resp->un.echo.id = icmp->un.echo.id;
199 resp->un.echo.sequence = icmp->un.echo.sequence;
201 /*resp->checksum = cksum (resp, sizeof (struct icmphdr)); */
203 return sizeof (struct icmphdr);
207 resolve_packet (void *in_pck, ssize_t in_size,
208 void *out_pck, uint32_t * out_size, uint8_t ip_addr[4])
210 struct ether_header *eh;
211 struct ether_arp *eah;
212 struct iphdr *ip, *ip_out;
213 struct icmphdr *icmp;
216 if ((in_pck == NULL) || (out_pck == NULL))
219 eh = (struct ether_header *) in_pck;
220 *out_size = resolve_eth (eh, out_pck);
222 if (eh->ether_type == 0x0608)
224 eah = (struct ether_arp *) (in_pck + *out_size);
225 *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);
228 else if (eh->ether_type == 0x0008)
231 print_packet (in_pck + *out_size);
233 ip = (struct iphdr *) (in_pck + *out_size);
234 ip_out = (struct iphdr *) (out_pck + *out_size);
235 *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
236 if (ip->protocol == 1)
238 icmp = (struct icmphdr *) (in_pck + *out_size);
239 *out_size += resolve_icmp (icmp, out_pck + *out_size);
240 ((struct icmphdr *) (out_pck + *out_size -
241 sizeof (struct icmphdr)))->checksum =
242 cksum (out_pck + *out_size - sizeof (struct icmphdr),
243 sizeof (struct icmphdr));
245 memcpy (out_pck + *out_size, in_pck + *out_size,
246 in_size - *out_size);
249 __bswap_16 (*out_size - sizeof (struct ether_header));
250 ip_out->check = cksum (ip_out, sizeof (struct iphdr));
257 generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
261 for (i = 0; i < 6; i++)
265 memcpy (eh->ether_shost, hw_addr, 6);
266 memcpy (eh->ether_dhost, hw_daddr, 6);
268 eh->ether_type = 0x0008;
270 return sizeof (struct ether_header);
274 generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
279 /*len updated later */
280 ip->tot_len = 0x5400;
286 ((uint8_t *) & ip->saddr)[0] = saddr[0];
287 ((uint8_t *) & ip->saddr)[1] = saddr[1];
288 ((uint8_t *) & ip->saddr)[2] = saddr[2];
289 ((uint8_t *) & ip->saddr)[3] = saddr[3];
291 ((uint8_t *) & ip->daddr)[0] = daddr[0];
292 ((uint8_t *) & ip->daddr)[1] = daddr[1];
293 ((uint8_t *) & ip->daddr)[2] = daddr[2];
294 ((uint8_t *) & ip->daddr)[3] = daddr[3];
296 ip->check = cksum (ip, sizeof (struct iphdr));
298 return sizeof (struct iphdr);
302 generate_icmp (struct icmphdr *icmp, uint32_t seq)
304 icmp->type = ICMP_ECHO;
306 icmp->un.echo.id = 0;
307 icmp->un.echo.sequence = seq;
309 return sizeof (struct icmphdr);
313 generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
314 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
316 struct ether_header *eh;
318 struct icmphdr *icmp;
322 eh = (struct ether_header *) pck;
323 *size += generate_eth (eh, hw_daddr);
325 ip = (struct iphdr *) (pck + *size);
326 *size += generate_ip (ip, saddr, daddr);
328 icmp = (struct icmphdr *) (pck + *size);
329 *size += generate_icmp (icmp, seq);
331 ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
332 cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));
334 ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
336 ip->check = cksum (ip, sizeof (struct iphdr));