blob: 2503ce49cc3efdc9d9f3dccc5c60660d5e4ec2a3 [file] [log] [blame]
Simon Kelleyc8e8f5c2021-01-24 21:59:37 +00001/* dnsmasq is Copyright (c) 2000-2021 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];
Simon Kelleycc921df2019-01-02 22:48:59 +000031 union all_addr addr;
Simon Kelley11867dc2015-12-23 16:15:58 +000032 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 {
Simon Kelleycc921df2019-01-02 22:48:59 +000055 if (arp->addr.addr4.s_addr != ((struct in_addr *)addrp)->s_addr)
Simon Kelley11867dc2015-12-23 16:15:58 +000056 continue;
57 }
Simon Kelley11867dc2015-12-23 16:15:58 +000058 else
59 {
Simon Kelleycc921df2019-01-02 22:48:59 +000060 if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr6, (struct in6_addr *)addrp))
Simon Kelley11867dc2015-12-23 16:15:58 +000061 continue;
62 }
Simon Kelley11867dc2015-12-23 16:15:58 +000063
Simon Kelley33702ab2015-12-28 23:17:15 +000064 if (arp->status == ARP_EMPTY)
Simon Kelley11867dc2015-12-23 16:15:58 +000065 {
Simon Kelley33702ab2015-12-28 23:17:15 +000066 /* existing address, was negative. */
Simon Kelley11867dc2015-12-23 16:15:58 +000067 arp->status = ARP_NEW;
68 arp->hwlen = maclen;
Simon Kelley11867dc2015-12-23 16:15:58 +000069 memcpy(arp->hwaddr, mac, maclen);
70 }
Simon Kelley33702ab2015-12-28 23:17:15 +000071 else if (arp->hwlen == maclen && memcmp(arp->hwaddr, mac, maclen) == 0)
72 /* Existing entry matches - confirm. */
73 arp->status = ARP_FOUND;
74 else
75 continue;
Simon Kelley11867dc2015-12-23 16:15:58 +000076
77 break;
78 }
79
80 if (!arp)
81 {
82 /* New entry */
Simon Kelley33702ab2015-12-28 23:17:15 +000083 if (freelist)
Simon Kelley11867dc2015-12-23 16:15:58 +000084 {
Simon Kelley33702ab2015-12-28 23:17:15 +000085 arp = freelist;
86 freelist = freelist->next;
Simon Kelley11867dc2015-12-23 16:15:58 +000087 }
88 else if (!(arp = whine_malloc(sizeof(struct arp_record))))
89 return 1;
90
91 arp->next = arps;
92 arps = arp;
93 arp->status = ARP_NEW;
94 arp->hwlen = maclen;
95 arp->family = family;
96 memcpy(arp->hwaddr, mac, maclen);
97 if (family == AF_INET)
Simon Kelleycc921df2019-01-02 22:48:59 +000098 arp->addr.addr4.s_addr = ((struct in_addr *)addrp)->s_addr;
Simon Kelley11867dc2015-12-23 16:15:58 +000099 else
Simon Kelleycc921df2019-01-02 22:48:59 +0000100 memcpy(&arp->addr.addr6, addrp, IN6ADDRSZ);
Simon Kelley11867dc2015-12-23 16:15:58 +0000101 }
102
103 return 1;
104}
105
106/* If in lazy mode, we cache absence of ARP entries. */
Simon Kelley33702ab2015-12-28 23:17:15 +0000107int find_mac(union mysockaddr *addr, unsigned char *mac, int lazy, time_t now)
Simon Kelley11867dc2015-12-23 16:15:58 +0000108{
Simon Kelleyd9172752016-01-04 17:17:41 +0000109 struct arp_record *arp, *tmp, **up;
Simon Kelley11867dc2015-12-23 16:15:58 +0000110 int updated = 0;
111
112 again:
113
Simon Kelley33702ab2015-12-28 23:17:15 +0000114 /* If the database is less then INTERVAL old, look in there */
115 if (difftime(now, last) < INTERVAL)
Simon Kelley1e505122016-01-25 21:29:23 +0000116 {
117 /* addr == NULL -> just make cache up-to-date */
118 if (!addr)
119 return 0;
120
121 for (arp = arps; arp; arp = arp->next)
122 {
Simon Kelley2c0c36f2016-05-01 20:57:08 +0100123 if (addr->sa.sa_family != arp->family)
124 continue;
125
126 if (arp->family == AF_INET &&
Simon Kelleycc921df2019-01-02 22:48:59 +0000127 arp->addr.addr4.s_addr != addr->in.sin_addr.s_addr)
Simon Kelley2c0c36f2016-05-01 20:57:08 +0100128 continue;
129
Simon Kelley2c0c36f2016-05-01 20:57:08 +0100130 if (arp->family == AF_INET6 &&
Simon Kelleycc921df2019-01-02 22:48:59 +0000131 !IN6_ARE_ADDR_EQUAL(&arp->addr.addr6, &addr->in6.sin6_addr))
Simon Kelley2c0c36f2016-05-01 20:57:08 +0100132 continue;
Simon Kelley1e505122016-01-25 21:29:23 +0000133
134 /* Only accept positive entries unless in lazy mode. */
135 if (arp->status != ARP_EMPTY || lazy || updated)
136 {
137 if (mac && arp->hwlen != 0)
138 memcpy(mac, arp->hwaddr, arp->hwlen);
139 return arp->hwlen;
140 }
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 */
Simon Kelleybb58f632016-01-14 19:23:10 +0000151 for (arp = arps; arp; arp = arp->next)
Simon Kelley11867dc2015-12-23 16:15:58 +0000152 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;
Simon Kelleyf4d0c662016-01-18 12:51:08 +0000191 arp->hwlen = 0;
192
Simon Kelley11867dc2015-12-23 16:15:58 +0000193 if (addr->sa.sa_family == AF_INET)
Simon Kelleycc921df2019-01-02 22:48:59 +0000194 arp->addr.addr4.s_addr = addr->in.sin_addr.s_addr;
Simon Kelley11867dc2015-12-23 16:15:58 +0000195 else
Simon Kelleycc921df2019-01-02 22:48:59 +0000196 memcpy(&arp->addr.addr6, &addr->in6.sin6_addr, IN6ADDRSZ);
Simon Kelley11867dc2015-12-23 16:15:58 +0000197 }
198
199 return 0;
200}
201
Simon Kelley33702ab2015-12-28 23:17:15 +0000202int do_arp_script_run(void)
203{
204 struct arp_record *arp;
205
206 /* Notify any which went, then move to free list */
207 if (old)
208 {
209#ifdef HAVE_SCRIPT
Simon Kelley1e505122016-01-25 21:29:23 +0000210 if (option_bool(OPT_SCRIPT_ARP))
Simon Kelleye6e751b2016-02-01 17:59:07 +0000211 queue_arp(ACTION_ARP_DEL, old->hwaddr, old->hwlen, old->family, &old->addr);
Simon Kelley33702ab2015-12-28 23:17:15 +0000212#endif
213 arp = old;
214 old = arp->next;
215 arp->next = freelist;
216 freelist = arp;
217 return 1;
218 }
219
220 for (arp = arps; arp; arp = arp->next)
221 if (arp->status == ARP_NEW)
222 {
223#ifdef HAVE_SCRIPT
Simon Kelley1e505122016-01-25 21:29:23 +0000224 if (option_bool(OPT_SCRIPT_ARP))
Simon Kelley33702ab2015-12-28 23:17:15 +0000225 queue_arp(ACTION_ARP, arp->hwaddr, arp->hwlen, arp->family, &arp->addr);
226#endif
227 arp->status = ARP_FOUND;
228 return 1;
229 }
230
231 return 0;
232}
233
Simon Kelley11867dc2015-12-23 16:15:58 +0000234