#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_ether.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#ifdef _STANDALONE
#include <lib/libkern/libkern.h>
#else
#include <arpa/inet.h>
#include <string.h>
#endif
#include "stand.h"
#include "net.h"
struct ether_arp {
struct arphdr ea_hdr;
u_int8_t arp_sha[ETHER_ADDR_LEN];
u_int8_t arp_spa[4];
u_int8_t arp_tha[ETHER_ADDR_LEN];
u_int8_t arp_tpa[4];
};
#define arp_hrd ea_hdr.ar_hrd
#define arp_pro ea_hdr.ar_pro
#define arp_hln ea_hdr.ar_hln
#define arp_pln ea_hdr.ar_pln
#define arp_op ea_hdr.ar_op
#define ARP_NUM 8
struct arp_list {
struct in_addr addr;
u_char ea[ETHER_ADDR_LEN];
} arp_list[ARP_NUM] = {
{ {0xffffffff}, BA }
};
int arp_num = 1;
static ssize_t arpsend(struct iodesc *, void *, size_t);
static ssize_t arprecv(struct iodesc *, void *, size_t, saseconds_t);
u_char *
arpwhohas(struct iodesc *d, struct in_addr addr)
{
int i;
ssize_t ns;
struct ether_arp *ah;
struct arp_list *al;
struct {
struct ether_header eh;
struct {
struct ether_arp arp;
u_char pad[18];
} data;
} wbuf;
struct {
struct ether_header eh;
struct {
struct ether_arp arp;
u_char pad[24];
} data;
} rbuf;
for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
if (addr.s_addr == al->addr.s_addr)
return al->ea;
if (arp_num > ARP_NUM - 1) {
arp_num = 1;
printf("%s: overflowed arp_list!\n", __func__);
}
#ifdef ARP_DEBUG
if (debug)
printf("%s: send request for %s\n", __func__, inet_ntoa(addr));
#endif
(void)memset(&wbuf.data, 0, sizeof(wbuf.data));
ah = &wbuf.data.arp;
ah->arp_hrd = htons(ARPHRD_ETHER);
ah->arp_pro = htons(ETHERTYPE_IP);
ah->arp_hln = sizeof(ah->arp_sha);
ah->arp_pln = sizeof(ah->arp_spa);
ah->arp_op = htons(ARPOP_REQUEST);
MACPY(d->myea, ah->arp_sha);
(void)memcpy(ah->arp_spa, &d->myip, sizeof(ah->arp_spa));
(void)memcpy(ah->arp_tpa, &addr, sizeof(ah->arp_tpa));
al->addr = addr;
ns = sendrecv(d,
arpsend, &wbuf.data, sizeof(wbuf.data),
arprecv, &rbuf.data, sizeof(rbuf.data));
if (ns == -1) {
panic("%s: no response for %s", __func__, inet_ntoa(addr));
}
ah = &rbuf.data.arp;
#ifdef ARP_DEBUG
if (debug) {
printf("%s: response from %s\n", __func__,
ether_sprintf(rbuf.eh.ether_shost));
printf("%s: cacheing %s --> %s\n", __func__,
inet_ntoa(addr), ether_sprintf(ah->arp_sha));
}
#endif
MACPY(ah->arp_sha, al->ea);
++arp_num;
return al->ea;
}
static ssize_t
arpsend(struct iodesc *d, void *pkt, size_t len)
{
#ifdef ARP_DEBUG
if (debug)
printf("%s: called\n", __func__);
#endif
return sendether(d, pkt, len, bcea, ETHERTYPE_ARP);
}
static ssize_t
arprecv(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
{
ssize_t n;
struct ether_arp *ah;
u_int16_t etype;
#ifdef ARP_DEBUG
if (debug)
printf("%s: ", __func__);
#endif
n = readether(d, pkt, len, tleft, &etype);
errno = 0;
if (n == -1 || (size_t)n < sizeof(struct ether_arp)) {
#ifdef ARP_DEBUG
if (debug)
printf("bad len=%zd\n", n);
#endif
return -1;
}
if (etype != ETHERTYPE_ARP) {
#ifdef ARP_DEBUG
if (debug)
printf("not arp type=%d\n", etype);
#endif
return -1;
}
ah = (struct ether_arp *)pkt;
if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
ah->arp_pro != htons(ETHERTYPE_IP) ||
ah->arp_hln != sizeof(ah->arp_sha) ||
ah->arp_pln != sizeof(ah->arp_spa) )
{
#ifdef ARP_DEBUG
if (debug)
printf("bad hrd/pro/hln/pln\n");
#endif
return -1;
}
if (ah->arp_op == htons(ARPOP_REQUEST)) {
#ifdef ARP_DEBUG
if (debug)
printf("is request\n");
#endif
arp_reply(d, ah);
return -1;
}
if (ah->arp_op != htons(ARPOP_REPLY)) {
#ifdef ARP_DEBUG
if (debug)
printf("not ARP reply\n");
#endif
return -1;
}
if (memcmp(&arp_list[arp_num].addr,
ah->arp_spa, sizeof(ah->arp_spa)))
{
#ifdef ARP_DEBUG
if (debug)
printf("unwanted address\n");
#endif
return -1;
}
#ifdef ARP_DEBUG
if (debug)
printf("got it\n");
#endif
return n;
}
void
arp_reply(struct iodesc *d, void *pkt)
{
struct ether_arp *arp = pkt;
if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
arp->arp_pro != htons(ETHERTYPE_IP) ||
arp->arp_hln != sizeof(arp->arp_sha) ||
arp->arp_pln != sizeof(arp->arp_spa) )
{
#ifdef ARP_DEBUG
if (debug)
printf("%s: bad hrd/pro/hln/pln\n", __func__);
#endif
return;
}
if (arp->arp_op != htons(ARPOP_REQUEST)) {
#ifdef ARP_DEBUG
if (debug)
printf("%s: not request!\n", __func__);
#endif
return;
}
if (memcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
return;
#ifdef ARP_DEBUG
if (debug) {
printf("%s: to %s\n", __func__, ether_sprintf(arp->arp_sha));
}
#endif
arp->arp_op = htons(ARPOP_REPLY);
(void)memcpy(arp->arp_tha, arp->arp_sha, sizeof(arp->arp_tha));
(void)memcpy(arp->arp_tpa, arp->arp_spa, sizeof(arp->arp_tpa));
(void)memcpy(arp->arp_sha, d->myea, sizeof(arp->arp_sha));
(void)memcpy(arp->arp_spa, &d->myip, sizeof(arp->arp_spa));
(void) sendether(d, pkt, sizeof(*arp) + 18,
arp->arp_tha, ETHERTYPE_ARP);
}