1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* Time between forced re-loads from kernel. */
23 #define ARP_FOUND 1 /* Confirmed */
24 #define ARP_NEW 2 /* Newly created */
25 #define ARP_EMPTY 3 /* No MAC addr */
28 unsigned short hwlen, status;
30 unsigned char hwaddr[DHCP_CHADDR_MAX];
32 struct arp_record *next;
35 static struct arp_record *arps = NULL, *old = NULL, *freelist = NULL;
36 static time_t last = 0;
38 static int filter_mac(int family, char *addrp, char *mac, size_t maclen, void *parmv)
40 struct arp_record *arp;
44 if (maclen > DHCP_CHADDR_MAX)
47 /* Look for existing entry */
48 for (arp = arps; arp; arp = arp->next)
50 if (family != arp->family || arp->status == ARP_NEW)
53 if (family == AF_INET)
55 if (arp->addr.addr4.s_addr != ((struct in_addr *)addrp)->s_addr)
60 if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr6, (struct in6_addr *)addrp))
64 if (arp->status == ARP_EMPTY)
66 /* existing address, was negative. */
67 arp->status = ARP_NEW;
69 memcpy(arp->hwaddr, mac, maclen);
71 else if (arp->hwlen == maclen && memcmp(arp->hwaddr, mac, maclen) == 0)
72 /* Existing entry matches - confirm. */
73 arp->status = ARP_FOUND;
86 freelist = freelist->next;
88 else if (!(arp = whine_malloc(sizeof(struct arp_record))))
93 arp->status = ARP_NEW;
96 memcpy(arp->hwaddr, mac, maclen);
97 if (family == AF_INET)
98 arp->addr.addr4.s_addr = ((struct in_addr *)addrp)->s_addr;
100 memcpy(&arp->addr.addr6, addrp, IN6ADDRSZ);
106 /* If in lazy mode, we cache absence of ARP entries. */
107 int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now)
109 struct arp_record *arp, *tmp, **up;
114 /* If the database is less then INTERVAL old, look in there */
115 if (difftime(now, last) < INTERVAL)
117 /* addr == NULL -> just make cache up-to-date */
121 for (arp = arps; arp; arp = arp->next)
123 if (addr->sa.sa_family != arp->family)
126 if (arp->family == AF_INET &&
127 arp->addr.addr4.s_addr != addr->in.sin_addr.s_addr)
130 if (arp->family == AF_INET6 &&
131 !IN6_ARE_ADDR_EQUAL(&arp->addr.addr6, &addr->in6.sin6_addr))
134 /* Only accept positive entries unless in lazy mode. */
135 if (arp->status != ARP_EMPTY || lazy || updated)
137 if (mac && arp->hwlen != 0)
138 memcpy(mac, arp->hwaddr, arp->hwlen);
144 /* Not found, try the kernel */
150 /* Mark all non-negative entries */
151 for (arp = arps; arp; arp = arp->next)
152 if (arp->status != ARP_EMPTY)
153 arp->status = ARP_MARK;
155 iface_enumerate(AF_UNSPEC, NULL, filter_mac);
157 /* Remove all unconfirmed entries to old list. */
158 for (arp = arps, up = &arps; arp; arp = tmp)
162 if (arp->status == ARP_MARK)
175 /* record failure, so we don't consult the kernel each time
176 we're asked for this address */
180 freelist = freelist->next;
183 arp = whine_malloc(sizeof(struct arp_record));
189 arp->status = ARP_EMPTY;
190 arp->family = addr->sa.sa_family;
193 if (addr->sa.sa_family == AF_INET)
194 arp->addr.addr4.s_addr = addr->in.sin_addr.s_addr;
196 memcpy(&arp->addr.addr6, &addr->in6.sin6_addr, IN6ADDRSZ);
202 int do_arp_script_run(void)
204 struct arp_record *arp;
206 /* Notify any which went, then move to free list */
210 if (option_bool(OPT_SCRIPT_ARP))
211 queue_arp(ACTION_ARP_DEL, old->hwaddr, old->hwlen, old->family, &old->addr);
215 arp->next = freelist;
220 for (arp = arps; arp; arp = arp->next)
221 if (arp->status == ARP_NEW)
224 if (option_bool(OPT_SCRIPT_ARP))
225 queue_arp(ACTION_ARP, arp->hwaddr, arp->hwlen, arp->family, &arp->addr);
227 arp->status = ARP_FOUND;