-// Handle ARP requests
-void processarp(u8 * buf, int len)
-{
- ipt ip;
- sessionidt s;
-
- CSTAT(call_processarp);
- STAT(arp_recv);
-
- if (len != 46)
- {
- log(0, 0, 0, 0, "Unexpected length ARP %d bytes\n", len);
- STAT(arp_errors);
- return;
- }
- if (*(u16 *) (buf + 16) != htons(PKTARP))
- {
- log(0, 0, 0, 0, "Unexpected ARP type %04X\n", ntohs(*(u16 *) (buf + 16)));
- STAT(arp_errors);
- return;
- }
- if (*(u16 *) (buf + 18) != htons(0x0001))
- {
- log(0, 0, 0, 0, "Unexpected ARP hard type %04X\n", ntohs(*(u16 *) (buf + 18)));
- STAT(arp_errors);
- return;
- }
- if (*(u16 *) (buf + 20) != htons(PKTIP))
- {
- log(0, 0, 0, 0, "Unexpected ARP prot type %04X\n", ntohs(*(u16 *) (buf + 20)));
- STAT(arp_errors);
- return;
- }
- if (buf[22] != 6)
- {
- log(0, 0, 0, 0, "Unexpected ARP hard len %d\n", buf[22]);
- STAT(arp_errors);
- return;
- }
- if (buf[23] != 4)
- {
- log(0, 0, 0, 0, "Unexpected ARP prot len %d\n", buf[23]);
- STAT(arp_errors);
- return;
- }
- if (*(u16 *) (buf + 24) != htons(0x0001))
- {
- log(0, 0, 0, 0, "Unexpected ARP op %04X\n", ntohs(*(u16 *) (buf + 24)));
- STAT(arp_errors);
- return;
- }
- ip = *(u32 *) (buf + 42);
- // look up session
- s = sessionbyip(ip);
- if (s)
- {
- log(3, ntohl(ip), s, session[s].tunnel, "ARP reply for %s\n", inet_toa(ip));
- memcpy(buf + 4, buf + 10, 6); // set destination as source
- *(u16 *) (buf + 10) = htons(tapmac[0]); // set source address
- *(u16 *) (buf + 12) = htons(tapmac[1]);
- *(u16 *) (buf + 14) = htons(tapmac[2]);
- *(u16 *) (buf + 24) = htons(0x0002); // ARP reply
- memcpy(buf + 26, buf + 10, 6); // sender ethernet
- memcpy(buf + 36, buf + 4, 6); // target ethernet
- *(u32 *) (buf + 42) = *(u32 *) (buf + 32); // target IP
- *(u32 *) (buf + 32) = ip; // sender IP
- write(tapfd, buf, len);
- STAT(arp_replies);
- }
- else
- {
- log(3, ntohl(ip), 0, 0, "ARP request for unknown IP %s\n", inet_toa(ip));
- STAT(arp_discarded);
- }
-}
-