Debian Codegraph @

src/arp.c

raw
1 /* dnsmasq is Copyright (c) 2000-2026 Simon Kelley
2
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
19 /* 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 */
26
27 struct arp_record {
28 unsigned short hwlen, status;
29 int family;
30 unsigned char hwaddr[DHCP_CHADDR_MAX];
31 union all_addr addr;
32 struct arp_record *next;
33 };
34
35 static struct arp_record *arps = NULL, *old = NULL, *freelist = NULL;
36 static time_t last = 0;
37
38 static int filter_mac(int family, void *addrp, char *mac, size_t maclen, void *parmv)
39 {
40 struct arp_record *arp;
41
42 (void)parmv;
43
44 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.addr4.s_addr != ((struct in_addr *)addrp)->s_addr)
56 continue;
57 }
58 else
59 {
60 if (!IN6_ARE_ADDR_EQUAL(&arp->addr.addr6, (struct in6_addr *)addrp))
61 continue;
62 }
63
64 if (arp->status == ARP_EMPTY)
65 {
66 /* existing address, was negative. */
67 arp->status = ARP_NEW;
68 arp->hwlen = maclen;
69 memcpy(arp->hwaddr, mac, maclen);
70 }
71 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;
76
77 break;
78 }
79
80 if (!arp)
81 {
82 /* New entry */
83 if (freelist)
84 {
85 arp = freelist;
86 freelist = freelist->next;
87 }
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)
98 arp->addr.addr4.s_addr = ((struct in_addr *)addrp)->s_addr;
99 else
100 memcpy(&arp->addr.addr6, addrp, IN6ADDRSZ);
101 }
102
103 return 1;
104 }
105
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)
108 {
109 struct arp_record *arp, *tmp, **up;
110 int updated = 0;
111
112 again:
113
114 /* If the database is less then INTERVAL old, look in there.
115
116 If we're a child process, we always rely on the existing cache we
117 inherited from the parent, since we don't have a netlink socket.
118 */
119 if (difftime(now, last) < INTERVAL || daemon->pipe_to_parent != -1)
120 {
121 /* addr == NULL -> just make cache up-to-date */
122 if (!addr)
123 return 0;
124
125 for (arp = arps; arp; arp = arp->next)
126 {
127 if (addr->sa.sa_family != arp->family)
128 continue;
129
130 if (arp->family == AF_INET &&
131 arp->addr.addr4.s_addr != addr->in.sin_addr.s_addr)
132 continue;
133
134 if (arp->family == AF_INET6 &&
135 !IN6_ARE_ADDR_EQUAL(&arp->addr.addr6, &addr->in6.sin6_addr))
136 continue;
137
138 /* Only accept positive entries unless in lazy mode. */
139 if (arp->status != ARP_EMPTY || lazy || updated)
140 {
141 if (mac && arp->hwlen != 0)
142 memcpy(mac, arp->hwaddr, arp->hwlen);
143 return arp->hwlen;
144 }
145 }
146 }
147
148 /* Not in cache in child, no go. */
149 if (daemon->pipe_to_parent != -1)
150 return 0;
151
152 /* Not found, try the kernel */
153 if (!updated)
154 {
155 updated = 1;
156 last = now;
157
158 /* Mark all non-negative entries */
159 for (arp = arps; arp; arp = arp->next)
160 if (arp->status != ARP_EMPTY)
161 arp->status = ARP_MARK;
162
163 iface_enumerate(AF_UNSPEC, NULL, (callback_t){.af_unspec=filter_mac});
164
165 /* Remove all unconfirmed entries to old list. */
166 for (arp = arps, up = &arps; arp; arp = tmp)
167 {
168 tmp = arp->next;
169
170 if (arp->status == ARP_MARK)
171 {
172 *up = arp->next;
173 arp->next = old;
174 old = arp;
175 }
176 else
177 up = &arp->next;
178 }
179
180 goto again;
181 }
182
183 /* record failure, so we don't consult the kernel each time
184 we're asked for this address */
185 if (freelist)
186 {
187 arp = freelist;
188 freelist = freelist->next;
189 }
190 else
191 arp = whine_malloc(sizeof(struct arp_record));
192
193 if (arp)
194 {
195 arp->next = arps;
196 arps = arp;
197 arp->status = ARP_EMPTY;
198 arp->family = addr->sa.sa_family;
199 arp->hwlen = 0;
200
201 if (addr->sa.sa_family == AF_INET)
202 arp->addr.addr4.s_addr = addr->in.sin_addr.s_addr;
203 else
204 memcpy(&arp->addr.addr6, &addr->in6.sin6_addr, IN6ADDRSZ);
205 }
206
207 return 0;
208 }
209
210 int do_arp_script_run(void)
211 {
212 struct arp_record *arp;
213
214 /* Notify any which went, then move to free list */
215 if (old)
216 {
217 #ifdef HAVE_SCRIPT
218 if (option_bool(OPT_SCRIPT_ARP))
219 queue_arp(ACTION_ARP_DEL, old->hwaddr, old->hwlen, old->family, &old->addr);
220 #endif
221 arp = old;
222 old = arp->next;
223 arp->next = freelist;
224 freelist = arp;
225 return 1;
226 }
227
228 for (arp = arps; arp; arp = arp->next)
229 if (arp->status == ARP_NEW)
230 {
231 #ifdef HAVE_SCRIPT
232 if (option_bool(OPT_SCRIPT_ARP))
233 queue_arp(ACTION_ARP, arp->hwaddr, arp->hwlen, arp->family, &arp->addr);
234 #endif
235 arp->status = ARP_FOUND;
236 return 1;
237 }
238
239 return 0;
240 }