blob: 73a0250eae7c8331c16d3fa53e617843fcc2f513 [file] [log] [blame]
Simon Kelleyc49778d2016-01-06 18:52:33 +00001/* dnsmasq is Copyright (c) 2000-2016 Simon Kelley
Simon Kelley11867dc2015-12-23 16:15:58 +00002
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.
7
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.
12
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/>.
15*/
16
17#include "dnsmasq.h"
18
Simon Kelley33702ab2015-12-28 23:17:15 +000019/* Time between forced re-loads from kernel. */
20#define INTERVAL 90
21
22#define ARP_MARK 0
23#define ARP_FOUND 1 /* Confirmed */
24#define ARP_NEW 2 /* Newly created */
25#define ARP_EMPTY 3 /* No MAC addr */
Simon Kelley11867dc2015-12-23 16:15:58 +000026
27struct arp_record {
Simon Kelley33702ab2015-12-28 23:17:15 +000028 unsigned short hwlen, status;
Simon Kelley11867dc2015-12-23 16:15:58 +000029 int family;
30 unsigned char hwaddr[DHCP_CHADDR_MAX];
31 struct all_addr addr;
32 struct arp_record *next;
33};
34
Simon Kelley33702ab2015-12-28 23:17:15 +000035static struct arp_record *arps = NULL, *old = NULL, *freelist = NULL;
36static time_t last = 0;
Simon Kelley11867dc2015-12-23 16:15:58 +000037
38static int filter_mac(int family, char *addrp, char *mac, size_t maclen, void *parmv)
39{
Simon Kelley11867dc2015-12-23 16:15:58 +000040 struct arp_record *arp;
41
Simon Kelley33702ab2015-12-28 23:17:15 +000042 (void)parmv;
43
Simon Kelley11867dc2015-12-23 16:15:58 +000044 if (maclen > DHCP_CHADDR_MAX)
45 return 1;
46
47 /* Look for existing entry */
48 for (arp = arps; arp; arp = arp->next)
49 {
50 if (family != arp->family || arp->status == ARP_NEW)
51 continue;
52
53 if (family == AF_INET)
54 {
55 if (arp->addr.addr.addr4.s_addr != ((struct in_addr *)addrp)->s_addr)
56 continue;
57 }
58#ifdef HAVE_IPV6
59 else
60 {
61 if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr.addr6, (struct in6_addr *)addrp))
62 continue;
63 }
64#endif
65
Simon Kelley33702ab2015-12-28 23:17:15 +000066 if (arp->status == ARP_EMPTY)
Simon Kelley11867dc2015-12-23 16:15:58 +000067 {
Simon Kelley33702ab2015-12-28 23:17:15 +000068 /* existing address, was negative. */
Simon Kelley11867dc2015-12-23 16:15:58 +000069 arp->status = ARP_NEW;
70 arp->hwlen = maclen;
Simon Kelley11867dc2015-12-23 16:15:58 +000071 memcpy(arp->hwaddr, mac, maclen);
72 }
Simon Kelley33702ab2015-12-28 23:17:15 +000073 else if (arp->hwlen == maclen && memcmp(arp->hwaddr, mac, maclen) == 0)
74 /* Existing entry matches - confirm. */
75 arp->status = ARP_FOUND;
76 else
77 continue;
Simon Kelley11867dc2015-12-23 16:15:58 +000078
79 break;
80 }
81
82 if (!arp)
83 {
84 /* New entry */
Simon Kelley33702ab2015-12-28 23:17:15 +000085 if (freelist)
Simon Kelley11867dc2015-12-23 16:15:58 +000086 {
Simon Kelley33702ab2015-12-28 23:17:15 +000087 arp = freelist;
88 freelist = freelist->next;
Simon Kelley11867dc2015-12-23 16:15:58 +000089 }
90 else if (!(arp = whine_malloc(sizeof(struct arp_record))))
91 return 1;
92
93 arp->next = arps;
94 arps = arp;
95 arp->status = ARP_NEW;
96 arp->hwlen = maclen;
97 arp->family = family;
98 memcpy(arp->hwaddr, mac, maclen);
99 if (family == AF_INET)
100 arp->addr.addr.addr4.s_addr = ((struct in_addr *)addrp)->s_addr;
101#ifdef HAVE_IPV6
102 else
103 memcpy(&arp->addr.addr.addr6, addrp, IN6ADDRSZ);
104#endif
105 }
106
107 return 1;
108}
109
110/* If in lazy mode, we cache absence of ARP entries. */
Simon Kelley33702ab2015-12-28 23:17:15 +0000111int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now)
Simon Kelley11867dc2015-12-23 16:15:58 +0000112{
Simon Kelleyd9172752016-01-04 17:17:41 +0000113 struct arp_record *arp, *tmp, **up;
Simon Kelley11867dc2015-12-23 16:15:58 +0000114 int updated = 0;
115
116 again:
117
Simon Kelley33702ab2015-12-28 23:17:15 +0000118 /* If the database is less then INTERVAL old, look in there */
119 if (difftime(now, last) < INTERVAL)
120 for (arp = arps; arp; arp = arp->next)
121 {
122 if (addr->sa.sa_family == arp->family)
123 {
124 if (arp->addr.addr.addr4.s_addr != addr->in.sin_addr.s_addr)
125 continue;
126 }
Simon Kelley11867dc2015-12-23 16:15:58 +0000127#ifdef HAVE_IPV6
Simon Kelley33702ab2015-12-28 23:17:15 +0000128 else
129 {
130 if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr.addr6, &addr->in6.sin6_addr))
131 continue;
132 }
Simon Kelley11867dc2015-12-23 16:15:58 +0000133#endif
Simon Kelley33702ab2015-12-28 23:17:15 +0000134
135 /* Only accept poitive entries unless in lazy mode. */
136 if (arp->status != ARP_EMPTY || lazy || updated)
137 {
138 if (mac && arp->hwlen != 0)
139 memcpy(mac, arp->hwaddr, arp->hwlen);
140 return arp->hwlen;
141 }
142 }
143
Simon Kelley11867dc2015-12-23 16:15:58 +0000144 /* Not found, try the kernel */
145 if (!updated)
146 {
147 updated = 1;
Simon Kelley33702ab2015-12-28 23:17:15 +0000148 last = now;
149
Simon Kelley11867dc2015-12-23 16:15:58 +0000150 /* Mark all non-negative entries */
151 for (arp = arps, up = &arps; arp; arp = arp->next)
152 if (arp->status != ARP_EMPTY)
Simon Kelley33702ab2015-12-28 23:17:15 +0000153 arp->status = ARP_MARK;
Simon Kelley11867dc2015-12-23 16:15:58 +0000154
155 iface_enumerate(AF_UNSPEC, NULL, filter_mac);
156
Simon Kelley33702ab2015-12-28 23:17:15 +0000157 /* Remove all unconfirmed entries to old list. */
Simon Kelleyd9172752016-01-04 17:17:41 +0000158 for (arp = arps, up = &arps; arp; arp = tmp)
159 {
160 tmp = arp->next;
Simon Kelley33702ab2015-12-28 23:17:15 +0000161
Simon Kelleyd9172752016-01-04 17:17:41 +0000162 if (arp->status == ARP_MARK)
163 {
164 *up = arp->next;
165 arp->next = old;
166 old = arp;
167 }
168 else
169 up = &arp->next;
170 }
171
Simon Kelley11867dc2015-12-23 16:15:58 +0000172 goto again;
173 }
174
175 /* record failure, so we don't consult the kernel each time
176 we're asked for this address */
Simon Kelley33702ab2015-12-28 23:17:15 +0000177 if (freelist)
Simon Kelley11867dc2015-12-23 16:15:58 +0000178 {
Simon Kelley33702ab2015-12-28 23:17:15 +0000179 arp = freelist;
180 freelist = freelist->next;
Simon Kelley11867dc2015-12-23 16:15:58 +0000181 }
182 else
183 arp = whine_malloc(sizeof(struct arp_record));
184
185 if (arp)
186 {
187 arp->next = arps;
188 arps = arp;
189 arp->status = ARP_EMPTY;
190 arp->family = addr->sa.sa_family;
191
192 if (addr->sa.sa_family == AF_INET)
193 arp->addr.addr.addr4.s_addr = addr->in.sin_addr.s_addr;
194#ifdef HAVE_IPV6
195 else
196 memcpy(&arp->addr.addr.addr6, &addr->in6.sin6_addr, IN6ADDRSZ);
197#endif
198 }
199
200 return 0;
201}
202
Simon Kelley33702ab2015-12-28 23:17:15 +0000203int do_arp_script_run(void)
204{
205 struct arp_record *arp;
206
207 /* Notify any which went, then move to free list */
208 if (old)
209 {
210#ifdef HAVE_SCRIPT
211 if (option_bool(OPT_DNS_CLIENT))
212 queue_arp(ACTION_ARP_OLD, old->hwaddr, old->hwlen, old->family, &old->addr);
213#endif
214 arp = old;
215 old = arp->next;
216 arp->next = freelist;
217 freelist = arp;
218 return 1;
219 }
220
221 for (arp = arps; arp; arp = arp->next)
222 if (arp->status == ARP_NEW)
223 {
224#ifdef HAVE_SCRIPT
225 if (option_bool(OPT_DNS_CLIENT))
226 queue_arp(ACTION_ARP, arp->hwaddr, arp->hwlen, arp->family, &arp->addr);
227#endif
228 arp->status = ARP_FOUND;
229 return 1;
230 }
231
232 return 0;
233}
234
Simon Kelley11867dc2015-12-23 16:15:58 +0000235