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> 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);
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");
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);
132 struct ether_arp *resp = (
struct ether_arp *) eth_arp_resp;
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);
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;
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;
190 return sizeof (
struct iphdr);
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;
204 return sizeof (
struct icmphdr);
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;
223 if (eh->ether_type == 0x0608)
225 eah = (
struct ether_arp *) (in_pck + *out_size);
229 else if (eh->ether_type == 0x0008)
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);
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));
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])
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);
305 icmp->type = ICMP_ECHO;
307 icmp->un.echo.id = 0;
308 icmp->un.echo.sequence = seq;
310 return sizeof (
struct icmphdr);
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;
326 ip = (
struct iphdr *) (pck + *size);
329 icmp = (
struct icmphdr *) (pck + *size);
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));
344 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq,
347 struct ether_header *eh;
349 struct icmphdr *icmp;
355 eh = (
struct ether_header *) pck;
359 ip = (
struct iphdr *) (pck + *size);
362 icmp = (
struct icmphdr *) (pck + *size);
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); \ 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)
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));
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");
460 struct ether_header *eh;
461 struct ether_arp *eah;
463 struct icmphdr *icmp;
465 uint16_t encap_size =
sizeof (
struct ether_header);
472 offset -= encap_size;
474 GET_HEADER (eh,
struct ether_header, pck, offset);
477 memset (hw_daddr, 0,
sizeof (uint8_t) * 6);
481 if (eh->ether_type == 0x0008)
485 if (ip->protocol == 1)
490 ip->tot_len = 0x0000;
496 ip->daddr = ip->saddr;
497 ((uint8_t *) & ip->saddr)[0] = ip_addr[0];
498 ((uint8_t *) & ip->saddr)[1] = ip_addr[1];
499 ((uint8_t *) & ip->saddr)[2] = ip_addr[2];
500 ((uint8_t *) & ip->saddr)[3] = ip_addr[3];
502 GET_HEADER (icmp,
struct icmphdr, pck, offset);
506 icmp->checksum =
cksum (icmp,
sizeof (
struct icmphdr));
511 ip->tot_len = __bswap_16 (offset -
sizeof (
struct ether_header));
512 ip->check =
cksum (ip,
sizeof (
struct iphdr));
516 offset += encap_size;
518 assert (offset != *size &&
519 "new packet length must be increased by encap size");
static ssize_t generate_ip(struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
int resolve_packet2(void *pck, uint32_t *size, uint8_t ip_addr[4])
int generate_packet2(void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq, icmpr_flow_mode_t mode)
static ssize_t resolve_arp(void *arp)
memset(h->entries, 0, sizeof(h->entries[0])*entries)
#define GET_HEADER(out, hdr, src, off)
int resolve_packet(void *in_pck, ssize_t in_size, void *out_pck, uint32_t *out_size, uint8_t ip_addr[4])
int print_packet(void *pck)
static ssize_t resolve_eth(struct ether_header *eth, void *eth_resp)
int resolve_packet3(void **pck_, uint32_t *size, uint8_t ip_addr[4])
static ssize_t resolve_eth_arp(struct ether_arp *eth_arp, void *eth_arp_resp, uint8_t ip_addr[4])
static ssize_t resolve_icmp(struct icmphdr *icmp, void *icmp_resp)
static ssize_t generate_icmp(struct icmphdr *icmp, uint32_t seq)
int generate_packet(void *pck, uint32_t *size, uint8_t saddr[4], uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
template key/value backing page structure
static ssize_t generate_eth(struct ether_header *eh, uint8_t hw_daddr[6])
struct clib_bihash_value offset
template key/value backing page structure
static uint16_t cksum(void *addr, ssize_t len)
static ssize_t resolve_ip(struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])