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> 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);
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");
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);
131 struct ether_arp *resp = (
struct ether_arp *) eth_arp_resp;
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);
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;
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;
189 return sizeof (
struct iphdr);
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;
203 return sizeof (
struct icmphdr);
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;
222 if (eh->ether_type == 0x0608)
224 eah = (
struct ether_arp *) (in_pck + *out_size);
228 else if (eh->ether_type == 0x0008)
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);
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));
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])
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);
304 icmp->type = ICMP_ECHO;
306 icmp->un.echo.id = 0;
307 icmp->un.echo.sequence = seq;
309 return sizeof (
struct icmphdr);
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;
325 ip = (
struct iphdr *) (pck + *size);
328 icmp = (
struct icmphdr *) (pck + *size);
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));
sll srl srl sll sra u16x4 i
static ssize_t generate_ip(struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
static ssize_t resolve_arp(void *arp)
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)
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)
static ssize_t generate_eth(struct ether_header *eh, uint8_t hw_daddr[6])
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])