- return 0;
-}
-
-#ifndef NDA_RTA
-#define NDA_RTA(r) \
- ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
-#endif
-
-static void rtnl_parse_newneigh(struct nlmsghdr *h)
-{
- struct relayd_interface *rif = NULL;
- struct ndmsg *r = NLMSG_DATA(h);
- const uint8_t *lladdr = NULL;
- const uint8_t *ipaddr = NULL;
- struct rtattr *rta;
- int len;
-
- if (r->ndm_family != AF_INET)
- return;
-
- list_for_each_entry(rif, &interfaces, list) {
- if (rif->sll.sll_ifindex == r->ndm_ifindex)
- goto found_interface;
- }
- return;
-
-found_interface:
- len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
- for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
- switch(rta->rta_type) {
- case NDA_LLADDR:
- lladdr = RTA_DATA(rta);
- break;
- case NDA_DST:
- ipaddr = RTA_DATA(rta);
- break;
- default:
- break;
- }
- }
-
- if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
- return;
-
- if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
- return;
-
- DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
- rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
- refresh_host(rif, lladdr, ipaddr);
-}
-
-static void rtnl_parse_packet(void *data, int len)
-{
- struct nlmsghdr *h;
-
- for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
- if (h->nlmsg_type == NLMSG_DONE ||
- h->nlmsg_type == NLMSG_ERROR)
- return;
-
- if (h->nlmsg_seq != rtnl_dump_seq)
- continue;
-
- if (h->nlmsg_type == RTM_NEWNEIGH)
- rtnl_parse_newneigh(h);
- }
-}
-
-static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
-{
- struct sockaddr_nl nladdr;
- static uint8_t buf[16384];
- struct iovec iov = {
- .iov_base = buf,
- .iov_len = sizeof(buf),
- };
- struct msghdr msg = {
- .msg_name = &nladdr,
- .msg_namelen = sizeof(nladdr),
- .msg_iov = &iov,
- .msg_iovlen = 1,
- };
-
- do {
- int len;
-
- len = recvmsg(rtnl_sock.fd, &msg, 0);
- if (len < 0) {
- if (errno == EINTR)
- continue;
-
- return;
- }
-
- if (!len)
- break;
-
- if (nladdr.nl_pid != 0)
- continue;
-
- rtnl_parse_packet(buf, len);
- } while (1);
-}
-
-static int rtnl_init(void)
-{
- struct sockaddr_nl snl_local;
- static struct {
- struct nlmsghdr nlh;
- struct rtgenmsg g;
- } req = {
- .nlh = {
- .nlmsg_len = sizeof(req),
- .nlmsg_type = RTM_GETNEIGH,
- .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
- .nlmsg_pid = 0,
- },
- .g.rtgen_family = AF_INET,
- };
-
- rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
- if (rtnl_sock.fd < 0) {
- perror("socket(AF_NETLINK)");
- return -1;
- }
-
- snl_local.nl_family = AF_NETLINK;
-
- if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
- perror("bind");
- close(rtnl_sock.fd);
- return -1;
- }
-
- rtnl_sock.cb = rtnl_cb;
- uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
-
- rtnl_seq = time(NULL);
- rtnl_dump_seq = rtnl_seq;
- req.nlh.nlmsg_seq = rtnl_seq;
- send(rtnl_sock.fd, &req, sizeof(req), 0);
-
- return 0;