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 -
140 sizeof (
struct ether_header)))->
143 memcpy (resp->arp_spa, ip_addr, 4);
145 return sizeof (
struct ether_arp);
151 struct ether_header *resp = (
struct ether_header *) eth_resp;
152 memcpy (resp->ether_dhost, eth->ether_shost, 6);
156 for (i = 0; i < 6; i++)
160 memcpy (resp->ether_shost, hw_addr, 6);
162 resp->ether_type = eth->ether_type;
164 return sizeof (
struct ether_header);
168 resolve_ip (
struct iphdr *ip,
void *ip_resp, uint8_t ip_addr[4])
170 struct iphdr *resp = (
struct iphdr *) ip_resp;
175 resp->tot_len = 0x5400;
180 ((uint8_t *) & resp->saddr)[0] = ip_addr[0];
181 ((uint8_t *) & resp->saddr)[1] = ip_addr[1];
182 ((uint8_t *) & resp->saddr)[2] = ip_addr[2];
183 ((uint8_t *) & resp->saddr)[3] = ip_addr[3];
184 resp->daddr = ip->saddr;
186 resp->check =
cksum (resp,
sizeof (
struct iphdr));
188 return sizeof (
struct iphdr);
194 struct icmphdr *resp = (
struct icmphdr *) icmp_resp;
195 resp->type = ICMP_ECHOREPLY;
197 resp->un.echo.id = icmp->un.echo.id;
198 resp->un.echo.sequence = icmp->un.echo.sequence;
202 return sizeof (
struct icmphdr);
207 void *out_pck, uint32_t * out_size, uint8_t ip_addr[4])
209 struct ether_header *eh;
210 struct ether_arp *eah;
212 struct icmphdr *icmp;
215 eh = (
struct ether_header *) in_pck;
218 if (eh->ether_type == 0x0608)
220 eah = (
struct ether_arp *) (in_pck + *out_size);
224 else if (eh->ether_type == 0x0008)
229 ip = (
struct iphdr *) (in_pck + *out_size);
230 *out_size +=
resolve_ip (ip, out_pck + *out_size, ip_addr);
231 if (ip->protocol == 1)
233 icmp = (
struct icmphdr *) (in_pck + *out_size);
235 ((
struct icmphdr *) (out_pck + *out_size -
236 sizeof (
struct icmphdr)))->checksum =
237 cksum (out_pck + *out_size -
sizeof (
struct icmphdr),
238 sizeof (
struct icmphdr));
240 memcpy (out_pck + *out_size, in_pck + *out_size,
241 in_size - *out_size);
sll srl srl sll sra u16x4 i
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 uint16_t cksum(void *addr, ssize_t len)
static ssize_t resolve_ip(struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])