blob: 8beaed4aa6e2e0f47b8095008b7e42d88384e95d [file] [log] [blame]
Simon Kelleyd1ced3a2018-01-01 22:18:03 +00001/* dnsmasq is Copyright (c) 2000-2018 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
Simon Kelley1566bac2016-02-05 14:38:06 +000047#ifndef HAVE_IPV6
48 if (family != AF_INET)
49 return 1;
50#endif
51
Simon Kelley11867dc2015-12-23 16:15:58 +000052 /* Look for existing entry */
53 for (arp = arps; arp; arp = arp->next)
54 {
55 if (family != arp->family || arp->status == ARP_NEW)
56 continue;
57
58 if (family == AF_INET)
59 {
60 if (arp->addr.addr.addr4.s_addr != ((struct in_addr *)addrp)->s_addr)
61 continue;
62 }
63#ifdef HAVE_IPV6
64 else
65 {
66 if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr.addr6, (struct in6_addr *)addrp))
67 continue;
68 }
69#endif
70
Simon Kelley33702ab2015-12-28 23:17:15 +000071 if (arp->status == ARP_EMPTY)
Simon Kelley11867dc2015-12-23 16:15:58 +000072 {
Simon Kelley33702ab2015-12-28 23:17:15 +000073 /* existing address, was negative. */
Simon Kelley11867dc2015-12-23 16:15:58 +000074 arp->status = ARP_NEW;
75 arp->hwlen = maclen;
Simon Kelley11867dc2015-12-23 16:15:58 +000076 memcpy(arp->hwaddr, mac, maclen);
77 }
Simon Kelley33702ab2015-12-28 23:17:15 +000078 else if (arp->hwlen == maclen && memcmp(arp->hwaddr, mac, maclen) == 0)
79 /* Existing entry matches - confirm. */
80 arp->status = ARP_FOUND;
81 else
82 continue;
Simon Kelley11867dc2015-12-23 16:15:58 +000083
84 break;
85 }
86
87 if (!arp)
88 {
89 /* New entry */
Simon Kelley33702ab2015-12-28 23:17:15 +000090 if (freelist)
Simon Kelley11867dc2015-12-23 16:15:58 +000091 {
Simon Kelley33702ab2015-12-28 23:17:15 +000092 arp = freelist;
93 freelist = freelist->next;
Simon Kelley11867dc2015-12-23 16:15:58 +000094 }
95 else if (!(arp = whine_malloc(sizeof(struct arp_record))))
96 return 1;
97
98 arp->next = arps;
99 arps = arp;
100 arp->status = ARP_NEW;
101 arp->hwlen = maclen;
102 arp->family = family;
103 memcpy(arp->hwaddr, mac, maclen);
104 if (family == AF_INET)
105 arp->addr.addr.addr4.s_addr = ((struct in_addr *)addrp)->s_addr;
106#ifdef HAVE_IPV6
107 else
108 memcpy(&arp->addr.addr.addr6, addrp, IN6ADDRSZ);
109#endif
110 }
111
112 return 1;
113}
114
115/* If in lazy mode, we cache absence of ARP entries. */
Simon Kelley33702ab2015-12-28 23:17:15 +0000116int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now)
Simon Kelley11867dc2015-12-23 16:15:58 +0000117{
Simon Kelleyd9172752016-01-04 17:17:41 +0000118 struct arp_record *arp, *tmp, **up;
Simon Kelley11867dc2015-12-23 16:15:58 +0000119 int updated = 0;
120
121 again:
122
Simon Kelley33702ab2015-12-28 23:17:15 +0000123 /* If the database is less then INTERVAL old, look in there */
124 if (difftime(now, last) < INTERVAL)
Simon Kelley1e505122016-01-25 21:29:23 +0000125 {
126 /* addr == NULL -> just make cache up-to-date */
127 if (!addr)
128 return 0;
129
130 for (arp = arps; arp; arp = arp->next)
131 {
Simon Kelley2c0c36f2016-05-01 20:57:08 +0100132 if (addr->sa.sa_family != arp->family)
133 continue;
134
135 if (arp->family == AF_INET &&
136 arp->addr.addr.addr4.s_addr != addr->in.sin_addr.s_addr)
137 continue;
138
Simon Kelley11867dc2015-12-23 16:15:58 +0000139#ifdef HAVE_IPV6
Simon Kelley2c0c36f2016-05-01 20:57:08 +0100140 if (arp->family == AF_INET6 &&
141 !IN6_ARE_ADDR_EQUAL(&arp->addr.addr.addr6, &addr->in6.sin6_addr))
142 continue;
Simon Kelley11867dc2015-12-23 16:15:58 +0000143#endif
Simon Kelley1e505122016-01-25 21:29:23 +0000144
145 /* Only accept positive entries unless in lazy mode. */
146 if (arp->status != ARP_EMPTY || lazy || updated)
147 {
148 if (mac && arp->hwlen != 0)
149 memcpy(mac, arp->hwaddr, arp->hwlen);
150 return arp->hwlen;
151 }
152 }
153 }
154
Simon Kelley11867dc2015-12-23 16:15:58 +0000155 /* Not found, try the kernel */
156 if (!updated)
157 {
158 updated = 1;
Simon Kelley33702ab2015-12-28 23:17:15 +0000159 last = now;
160
Simon Kelley11867dc2015-12-23 16:15:58 +0000161 /* Mark all non-negative entries */
Simon Kelleybb58f632016-01-14 19:23:10 +0000162 for (arp = arps; arp; arp = arp->next)
Simon Kelley11867dc2015-12-23 16:15:58 +0000163 if (arp->status != ARP_EMPTY)
Simon Kelley33702ab2015-12-28 23:17:15 +0000164 arp->status = ARP_MARK;
Simon Kelley11867dc2015-12-23 16:15:58 +0000165
166 iface_enumerate(AF_UNSPEC, NULL, filter_mac);
167
Simon Kelley33702ab2015-12-28 23:17:15 +0000168 /* Remove all unconfirmed entries to old list. */
Simon Kelleyd9172752016-01-04 17:17:41 +0000169 for (arp = arps, up = &arps; arp; arp = tmp)
170 {
171 tmp = arp->next;
Simon Kelley33702ab2015-12-28 23:17:15 +0000172
Simon Kelleyd9172752016-01-04 17:17:41 +0000173 if (arp->status == ARP_MARK)
174 {
175 *up = arp->next;
176 arp->next = old;
177 old = arp;
178 }
179 else
180 up = &arp->next;
181 }
182
Simon Kelley11867dc2015-12-23 16:15:58 +0000183 goto again;
184 }
185
186 /* record failure, so we don't consult the kernel each time
187 we're asked for this address */
Simon Kelley33702ab2015-12-28 23:17:15 +0000188 if (freelist)
Simon Kelley11867dc2015-12-23 16:15:58 +0000189 {
Simon Kelley33702ab2015-12-28 23:17:15 +0000190 arp = freelist;
191 freelist = freelist->next;
Simon Kelley11867dc2015-12-23 16:15:58 +0000192 }
193 else
194 arp = whine_malloc(sizeof(struct arp_record));
195
196 if (arp)
197 {
198 arp->next = arps;
199 arps = arp;
200 arp->status = ARP_EMPTY;
201 arp->family = addr->sa.sa_family;
Simon Kelleyf4d0c662016-01-18 12:51:08 +0000202 arp->hwlen = 0;
203
Simon Kelley11867dc2015-12-23 16:15:58 +0000204 if (addr->sa.sa_family == AF_INET)
205 arp->addr.addr.addr4.s_addr = addr->in.sin_addr.s_addr;
206#ifdef HAVE_IPV6
207 else
208 memcpy(&arp->addr.addr.addr6, &addr->in6.sin6_addr, IN6ADDRSZ);
209#endif
210 }
211
212 return 0;
213}
214
Simon Kelley33702ab2015-12-28 23:17:15 +0000215int do_arp_script_run(void)
216{
217 struct arp_record *arp;
218
219 /* Notify any which went, then move to free list */
220 if (old)
221 {
222#ifdef HAVE_SCRIPT
Simon Kelley1e505122016-01-25 21:29:23 +0000223 if (option_bool(OPT_SCRIPT_ARP))
Simon Kelleye6e751b2016-02-01 17:59:07 +0000224 queue_arp(ACTION_ARP_DEL, old->hwaddr, old->hwlen, old->family, &old->addr);
Simon Kelley33702ab2015-12-28 23:17:15 +0000225#endif
226 arp = old;
227 old = arp->next;
228 arp->next = freelist;
229 freelist = arp;
230 return 1;
231 }
232
233 for (arp = arps; arp; arp = arp->next)
234 if (arp->status == ARP_NEW)
235 {
236#ifdef HAVE_SCRIPT
Simon Kelley1e505122016-01-25 21:29:23 +0000237 if (option_bool(OPT_SCRIPT_ARP))
Simon Kelley33702ab2015-12-28 23:17:15 +0000238 queue_arp(ACTION_ARP, arp->hwaddr, arp->hwlen, arp->family, &arp->addr);
239#endif
240 arp->status = ARP_FOUND;
241 return 1;
242 }
243
244 return 0;
245}
246
Simon Kelley11867dc2015-12-23 16:15:58 +0000247