xref: /dragonfly/contrib/dhcpcd/src/arp.c (revision 6a3cbbc2)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - ARP handler
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 
32 #include <arpa/inet.h>
33 
34 #include <net/if.h>
35 #include <netinet/in.h>
36 #include <netinet/if_ether.h>
37 
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #define ELOOP_QUEUE	ELOOP_ARP
45 #include "config.h"
46 #include "arp.h"
47 #include "bpf.h"
48 #include "ipv4.h"
49 #include "common.h"
50 #include "dhcpcd.h"
51 #include "eloop.h"
52 #include "if.h"
53 #include "if-options.h"
54 #include "ipv4ll.h"
55 #include "logerr.h"
56 #include "privsep.h"
57 
58 #if defined(ARP)
59 #define ARP_LEN								\
60 	(FRAMEHDRLEN_MAX +						\
61 	 sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + (2 * HWADDR_LEN))
62 
63 /* ARP debugging can be quite noisy. Enable this for more noise! */
64 //#define	ARP_DEBUG
65 
66 /* Assert the correct structure size for on wire */
67 __CTASSERT(sizeof(struct arphdr) == 8);
68 
69 static ssize_t
70 arp_request(const struct interface *ifp,
71     const struct in_addr *sip, const struct in_addr *tip)
72 {
73 	uint8_t arp_buffer[ARP_LEN];
74 	struct arphdr ar;
75 	size_t len;
76 	uint8_t *p;
77 	const struct iarp_state *state;
78 
79 	ar.ar_hrd = htons(ifp->family);
80 	ar.ar_pro = htons(ETHERTYPE_IP);
81 	ar.ar_hln = ifp->hwlen;
82 	ar.ar_pln = sizeof(tip->s_addr);
83 	ar.ar_op = htons(ARPOP_REQUEST);
84 
85 	p = arp_buffer;
86 	len = 0;
87 
88 #define CHECK(fun, b, l)						\
89 	do {								\
90 		if (len + (l) > sizeof(arp_buffer))			\
91 			goto eexit;					\
92 		fun(p, (b), (l));					\
93 		p += (l);						\
94 		len += (l);						\
95 	} while (/* CONSTCOND */ 0)
96 #define APPEND(b, l)	CHECK(memcpy, b, l)
97 #define ZERO(l)		CHECK(memset, 0, l)
98 
99 	APPEND(&ar, sizeof(ar));
100 	APPEND(ifp->hwaddr, ifp->hwlen);
101 	if (sip != NULL)
102 		APPEND(&sip->s_addr, sizeof(sip->s_addr));
103 	else
104 		ZERO(sizeof(tip->s_addr));
105 	ZERO(ifp->hwlen);
106 	APPEND(&tip->s_addr, sizeof(tip->s_addr));
107 
108 #ifdef PRIVSEP
109 	if (ifp->ctx->options & DHCPCD_PRIVSEP)
110 		return ps_bpf_sendarp(ifp, arp_buffer, len);
111 #endif
112 	state = ARP_CSTATE(ifp);
113 	/* Note that well formed ethernet will add extra padding
114 	 * to ensure that the packet is at least 60 bytes (64 including FCS). */
115 	return bpf_send(ifp, state->bpf_fd, ETHERTYPE_ARP, arp_buffer, len);
116 
117 eexit:
118 	errno = ENOBUFS;
119 	return -1;
120 }
121 
122 static void
123 arp_report_conflicted(const struct arp_state *astate,
124     const struct arp_msg *amsg)
125 {
126 	char abuf[HWADDR_LEN * 3];
127 	char fbuf[HWADDR_LEN * 3];
128 
129 	if (amsg == NULL) {
130 		logerrx("%s: DAD detected %s",
131 		    astate->iface->name, inet_ntoa(astate->addr));
132 		return;
133 	}
134 
135 	hwaddr_ntoa(amsg->sha, astate->iface->hwlen, abuf, sizeof(abuf));
136 	if (bpf_frame_header_len(astate->iface) == 0) {
137 		logerrx("%s: %s claims %s",
138 		    astate->iface->name, abuf, inet_ntoa(astate->addr));
139 		return;
140 	}
141 
142 	logerrx("%s: %s(%s) claims %s",
143 	    astate->iface->name, abuf,
144 	    hwaddr_ntoa(amsg->fsha, astate->iface->hwlen, fbuf, sizeof(fbuf)),
145 	    inet_ntoa(astate->addr));
146 }
147 
148 static void
149 arp_found(struct arp_state *astate, const struct arp_msg *amsg)
150 {
151 	struct interface *ifp;
152 	struct ipv4_addr *ia;
153 #ifndef KERNEL_RFC5227
154 	struct timespec now;
155 #endif
156 
157 	arp_report_conflicted(astate, amsg);
158 	ifp = astate->iface;
159 
160 	/* If we haven't added the address we're doing a probe. */
161 	ia = ipv4_iffindaddr(ifp, &astate->addr, NULL);
162 	if (ia == NULL) {
163 		if (astate->found_cb != NULL)
164 			astate->found_cb(astate, amsg);
165 		return;
166 	}
167 
168 #ifndef KERNEL_RFC5227
169 	/* RFC 3927 Section 2.5 says a defence should
170 	 * broadcast an ARP announcement.
171 	 * Because the kernel will also unicast a reply to the
172 	 * hardware address which requested the IP address
173 	 * the other IPv4LL client will receieve two ARP
174 	 * messages.
175 	 * If another conflict happens within DEFEND_INTERVAL
176 	 * then we must drop our address and negotiate a new one. */
177 	clock_gettime(CLOCK_MONOTONIC, &now);
178 	if (timespecisset(&astate->defend) &&
179 	    eloop_timespec_diff(&now, &astate->defend, NULL) < DEFEND_INTERVAL)
180 		logwarnx("%s: %d second defence failed for %s",
181 		    ifp->name, DEFEND_INTERVAL, inet_ntoa(astate->addr));
182 	else if (arp_request(ifp, &astate->addr, &astate->addr) == -1)
183 		logerr(__func__);
184 	else {
185 		logdebugx("%s: defended address %s",
186 		    ifp->name, inet_ntoa(astate->addr));
187 		astate->defend = now;
188 		return;
189 	}
190 #endif
191 
192 	if (astate->defend_failed_cb != NULL)
193 		astate->defend_failed_cb(astate);
194 }
195 
196 static bool
197 arp_validate(const struct interface *ifp, struct arphdr *arp)
198 {
199 
200 	/* Families must match */
201 	if (arp->ar_hrd != htons(ifp->family))
202 		return false;
203 
204 	/* Protocol must be IP. */
205 	if (arp->ar_pro != htons(ETHERTYPE_IP))
206 		return false;
207 
208 	/* lladdr length matches */
209 	if (arp->ar_hln != ifp->hwlen)
210 		return false;
211 
212 	/* Protocol length must match in_addr_t */
213 	if (arp->ar_pln != sizeof(in_addr_t))
214 		return false;
215 
216 	/* Only these types are recognised */
217 	if (arp->ar_op != htons(ARPOP_REPLY) &&
218 	    arp->ar_op != htons(ARPOP_REQUEST))
219 		return false;
220 
221 	return true;
222 }
223 
224 void
225 arp_packet(struct interface *ifp, uint8_t *data, size_t len)
226 {
227 	size_t fl = bpf_frame_header_len(ifp), falen;
228 	const struct interface *ifn;
229 	struct arphdr ar;
230 	struct arp_msg arm;
231 	const struct iarp_state *state;
232 	struct arp_state *astate, *astaten;
233 	uint8_t *hw_s, *hw_t;
234 
235 	/* Copy the frame header source and destination out */
236 	memset(&arm, 0, sizeof(arm));
237 	if (fl != 0) {
238 		hw_s = bpf_frame_header_src(ifp, data, &falen);
239 		if (hw_s != NULL && falen <= sizeof(arm.fsha))
240 			memcpy(arm.fsha, hw_s, falen);
241 		hw_t = bpf_frame_header_dst(ifp, data, &falen);
242 		if (hw_t != NULL && falen <= sizeof(arm.ftha))
243 			memcpy(arm.ftha, hw_t, falen);
244 
245 		/* Skip past the frame header */
246 		data += fl;
247 		len -= fl;
248 	}
249 
250 	/* We must have a full ARP header */
251 	if (len < sizeof(ar))
252 		return;
253 	memcpy(&ar, data, sizeof(ar));
254 
255 	if (!arp_validate(ifp, &ar)) {
256 #ifdef BPF_DEBUG
257 		logerrx("%s: ARP BPF validation failure", ifp->name);
258 #endif
259 		return;
260 	}
261 
262 	/* Get pointers to the hardware addresses */
263 	hw_s = data + sizeof(ar);
264 	hw_t = hw_s + ar.ar_hln + ar.ar_pln;
265 	/* Ensure we got all the data */
266 	if ((size_t)((hw_t + ar.ar_hln + ar.ar_pln) - data) > len)
267 		return;
268 	/* Ignore messages from ourself */
269 	TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
270 		if (ar.ar_hln == ifn->hwlen &&
271 		    memcmp(hw_s, ifn->hwaddr, ifn->hwlen) == 0)
272 			break;
273 	}
274 	if (ifn) {
275 #ifdef ARP_DEBUG
276 		logdebugx("%s: ignoring ARP from self", ifp->name);
277 #endif
278 		return;
279 	}
280 	/* Copy out the HW and IP addresses */
281 	memcpy(&arm.sha, hw_s, ar.ar_hln);
282 	memcpy(&arm.sip.s_addr, hw_s + ar.ar_hln, ar.ar_pln);
283 	memcpy(&arm.tha, hw_t, ar.ar_hln);
284 	memcpy(&arm.tip.s_addr, hw_t + ar.ar_hln, ar.ar_pln);
285 
286 	/* Match the ARP probe to our states.
287 	 * Ignore Unicast Poll, RFC1122. */
288 	state = ARP_CSTATE(ifp);
289 	if (state == NULL)
290 		return;
291 	TAILQ_FOREACH_SAFE(astate, &state->arp_states, next, astaten) {
292 		if (IN_ARE_ADDR_EQUAL(&arm.sip, &astate->addr) ||
293 		    (IN_IS_ADDR_UNSPECIFIED(&arm.sip) &&
294 		    IN_ARE_ADDR_EQUAL(&arm.tip, &astate->addr) &&
295 		    state->bpf_flags & BPF_BCAST))
296 			arp_found(astate, &arm);
297 	}
298 }
299 
300 static void
301 arp_close(struct interface *ifp)
302 {
303 	struct dhcpcd_ctx *ctx = ifp->ctx;
304 	struct iarp_state *state;
305 
306 #ifdef PRIVSEP
307 	if (IN_PRIVSEP(ctx)) {
308 		if (IN_PRIVSEP_SE(ctx) &&
309 		    ps_bpf_closearp(ifp) == -1)
310 			logerr(__func__);
311 		return;
312 	}
313 #endif
314 
315 	if ((state = ARP_STATE(ifp)) == NULL)
316 		return;
317 
318 	if (state->bpf_fd == -1)
319 		return;
320 	eloop_event_delete(ctx->eloop, state->bpf_fd);
321 	bpf_close(ifp, state->bpf_fd);
322 	state->bpf_fd = -1;
323 	state->bpf_flags |= BPF_EOF;
324 }
325 
326 static void
327 arp_tryfree(struct iarp_state *state)
328 {
329 	struct interface *ifp = state->ifp;
330 
331 	/* If there are no more ARP states, close the socket. */
332 	if (TAILQ_FIRST(&state->arp_states) == NULL) {
333 		arp_close(ifp);
334 		if (state->bpf_flags & BPF_READING)
335 			state->bpf_flags |= BPF_EOF;
336 		else {
337 			free(state);
338 			ifp->if_data[IF_DATA_ARP] = NULL;
339 		}
340 	} else if (state->bpf_fd != -1) {
341 		if (bpf_arp(ifp, state->bpf_fd) == -1)
342 			logerr(__func__);
343 	}
344 }
345 
346 static void
347 arp_read(void *arg)
348 {
349 	struct iarp_state *state = arg;
350 	struct interface *ifp = state->ifp;
351 	uint8_t buf[ARP_LEN];
352 	ssize_t bytes;
353 
354 	/* Some RAW mechanisms are generic file descriptors, not sockets.
355 	 * This means we have no kernel call to just get one packet,
356 	 * so we have to process the entire buffer. */
357 	state->bpf_flags &= ~BPF_EOF;
358 	state->bpf_flags |= BPF_READING;
359 	while (!(state->bpf_flags & BPF_EOF)) {
360 		bytes = bpf_read(ifp, state->bpf_fd, buf, sizeof(buf),
361 				 &state->bpf_flags);
362 		if (bytes == -1) {
363 			logerr("%s: %s", __func__, ifp->name);
364 			arp_close(ifp);
365 			break;
366 		}
367 		arp_packet(ifp, buf, (size_t)bytes);
368 		/* Check we still have a state after processing. */
369 		if ((state = ARP_STATE(ifp)) == NULL)
370 			break;
371 	}
372 	if (state != NULL) {
373 		state->bpf_flags &= ~BPF_READING;
374 		/* Try and free the state if nothing left to do. */
375 		arp_tryfree(state);
376 	}
377 }
378 
379 static int
380 arp_open(struct interface *ifp)
381 {
382 	struct iarp_state *state;
383 
384 #ifdef PRIVSEP
385 	if (IN_PRIVSEP_SE(ifp->ctx))
386 		return ps_bpf_openarp(ifp) == -1 ? -1 : 0;
387 #endif
388 
389 	state = ARP_STATE(ifp);
390 	if (state->bpf_fd == -1) {
391 		state->bpf_fd = bpf_open(ifp, bpf_arp);
392 		if (state->bpf_fd == -1)
393 			return -1;
394 		eloop_event_add(ifp->ctx->eloop, state->bpf_fd, arp_read, state);
395 	}
396 	return state->bpf_fd;
397 }
398 
399 static void
400 arp_probed(void *arg)
401 {
402 	struct arp_state *astate = arg;
403 
404 	timespecclear(&astate->defend);
405 	astate->not_found_cb(astate);
406 }
407 
408 static void
409 arp_probe1(void *arg)
410 {
411 	struct arp_state *astate = arg;
412 	struct interface *ifp = astate->iface;
413 	unsigned int delay;
414 
415 	if (++astate->probes < PROBE_NUM) {
416 		delay = (PROBE_MIN * MSEC_PER_SEC) +
417 		    (arc4random_uniform(
418 		    (PROBE_MAX - PROBE_MIN) * MSEC_PER_SEC));
419 		eloop_timeout_add_msec(ifp->ctx->eloop, delay, arp_probe1, astate);
420 	} else {
421 		delay = ANNOUNCE_WAIT *	MSEC_PER_SEC;
422 		eloop_timeout_add_msec(ifp->ctx->eloop, delay, arp_probed, astate);
423 	}
424 	logdebugx("%s: ARP probing %s (%d of %d), next in %0.1f seconds",
425 	    ifp->name, inet_ntoa(astate->addr),
426 	    astate->probes ? astate->probes : PROBE_NUM, PROBE_NUM,
427 	    (float)delay / MSEC_PER_SEC);
428 	if (arp_request(ifp, NULL, &astate->addr) == -1)
429 		logerr(__func__);
430 }
431 
432 void
433 arp_probe(struct arp_state *astate)
434 {
435 
436 	astate->probes = 0;
437 	logdebugx("%s: probing for %s",
438 	    astate->iface->name, inet_ntoa(astate->addr));
439 	if (!(IN_PRIVSEP(astate->iface->ctx)) && arp_open(astate->iface) == -1)
440 	{
441 		logerr(__func__);
442 		return;
443 	}
444 	arp_probe1(astate);
445 }
446 #endif	/* ARP */
447 
448 struct arp_state *
449 arp_find(struct interface *ifp, const struct in_addr *addr)
450 {
451 	struct iarp_state *state;
452 	struct arp_state *astate;
453 
454 	if ((state = ARP_STATE(ifp)) == NULL)
455 		goto out;
456 	TAILQ_FOREACH(astate, &state->arp_states, next) {
457 		if (astate->addr.s_addr == addr->s_addr && astate->iface == ifp)
458 			return astate;
459 	}
460 out:
461 	errno = ESRCH;
462 	return NULL;
463 }
464 
465 static void
466 arp_announced(void *arg)
467 {
468 	struct arp_state *astate = arg;
469 
470 	if (astate->announced_cb) {
471 		astate->announced_cb(astate);
472 		return;
473 	}
474 
475 	/* Keep the ARP state open to handle ongoing ACD. */
476 }
477 
478 static void
479 arp_announce1(void *arg)
480 {
481 	struct arp_state *astate = arg;
482 	struct interface *ifp = astate->iface;
483 	struct ipv4_addr *ia;
484 
485 	if (++astate->claims < ANNOUNCE_NUM)
486 		logdebugx("%s: ARP announcing %s (%d of %d), "
487 		    "next in %d.0 seconds",
488 		    ifp->name, inet_ntoa(astate->addr),
489 		    astate->claims, ANNOUNCE_NUM, ANNOUNCE_WAIT);
490 	else
491 		logdebugx("%s: ARP announcing %s (%d of %d)",
492 		    ifp->name, inet_ntoa(astate->addr),
493 		    astate->claims, ANNOUNCE_NUM);
494 
495 	/* The kernel will send a Gratuitous ARP for newly added addresses.
496 	 * So we can avoid sending the same.
497 	 * Linux is special and doesn't send one. */
498 	ia = ipv4_iffindaddr(ifp, &astate->addr, NULL);
499 #ifndef __linux__
500 	if (astate->claims == 1 && ia != NULL && ia->flags & IPV4_AF_NEW)
501 		goto skip_request;
502 #endif
503 
504 	if (arp_request(ifp, &astate->addr, &astate->addr) == -1)
505 		logerr(__func__);
506 
507 #ifndef __linux__
508 skip_request:
509 #endif
510 	/* No longer a new address. */
511 	if (ia != NULL)
512 		ia->flags |= ~IPV4_AF_NEW;
513 
514 	eloop_timeout_add_sec(ifp->ctx->eloop, ANNOUNCE_WAIT,
515 	    astate->claims < ANNOUNCE_NUM ? arp_announce1 : arp_announced,
516 	    astate);
517 }
518 
519 void
520 arp_announce(struct arp_state *astate)
521 {
522 	struct iarp_state *state;
523 	struct interface *ifp;
524 	struct arp_state *a2;
525 	int r;
526 
527 	if (!(IN_PRIVSEP(astate->iface->ctx)) && arp_open(astate->iface) == -1)
528 	{
529 		logerr(__func__);
530 		return;
531 	}
532 
533 	/* Cancel any other ARP announcements for this address. */
534 	TAILQ_FOREACH(ifp, astate->iface->ctx->ifaces, next) {
535 		state = ARP_STATE(ifp);
536 		if (state == NULL)
537 			continue;
538 		TAILQ_FOREACH(a2, &state->arp_states, next) {
539 			if (astate == a2 ||
540 			    a2->addr.s_addr != astate->addr.s_addr)
541 				continue;
542 			r = eloop_timeout_delete(a2->iface->ctx->eloop,
543 			    a2->claims < ANNOUNCE_NUM
544 			    ? arp_announce1 : arp_announced,
545 			    a2);
546 			if (r == -1)
547 				logerr(__func__);
548 			else if (r != 0)
549 				logdebugx("%s: ARP announcement "
550 				    "of %s cancelled",
551 				    a2->iface->name,
552 				    inet_ntoa(a2->addr));
553 		}
554 	}
555 
556 	astate->claims = 0;
557 	arp_announce1(astate);
558 }
559 
560 void
561 arp_ifannounceaddr(struct interface *ifp, const struct in_addr *ia)
562 {
563 	struct arp_state *astate;
564 
565 	if (ifp->flags & IFF_NOARP)
566 		return;
567 
568 	astate = arp_find(ifp, ia);
569 	if (astate == NULL) {
570 		astate = arp_new(ifp, ia);
571 		if (astate == NULL)
572 			return;
573 		astate->announced_cb = arp_free;
574 	}
575 	arp_announce(astate);
576 }
577 
578 void
579 arp_announceaddr(struct dhcpcd_ctx *ctx, const struct in_addr *ia)
580 {
581 	struct interface *ifp, *iff = NULL;
582 	struct ipv4_addr *iap;
583 
584 	TAILQ_FOREACH(ifp, ctx->ifaces, next) {
585 		if (!ifp->active || ifp->carrier <= LINK_DOWN)
586 			continue;
587 		iap = ipv4_iffindaddr(ifp, ia, NULL);
588 		if (iap == NULL)
589 			continue;
590 #ifdef IN_IFF_NOTUSEABLE
591 		if (!(iap->addr_flags & IN_IFF_NOTUSEABLE))
592 			continue;
593 #endif
594 		if (iff != NULL && iff->metric < ifp->metric)
595 			continue;
596 		iff = ifp;
597 	}
598 	if (iff == NULL)
599 		return;
600 
601 	arp_ifannounceaddr(iff, ia);
602 }
603 
604 void
605 arp_change(struct arp_state *astate, const struct in_addr *addr)
606 {
607 	struct interface *ifp = astate->iface;
608 	struct iarp_state *state = ARP_STATE(ifp);
609 
610 #ifdef PRIVSEP
611 	if (!IN_IS_ADDR_UNSPECIFIED(&astate->addr) &&
612 	    IN_PRIVSEP_SE(ifp->ctx))
613 	{
614 		if (ps_bpf_deladdr(ifp, &astate->addr) == -1)
615 			logerr(__func__);
616 	}
617 #endif
618 
619 	if (addr != NULL)
620 		astate->addr = *addr;
621 	else
622 		astate->addr.s_addr = INADDR_ANY;
623 
624 #ifdef PRIVSEP
625 	if (addr != NULL && IN_PRIVSEP_SE(ifp->ctx)) {
626 		if (ps_bpf_addaddr(ifp, addr) == -1)
627 			logerr(__func__);
628 	} else
629 #endif
630 	if (state->bpf_fd != -1) {
631 		if (bpf_arp(ifp, state->bpf_fd) == -1)
632 			logerr(__func__); /* try and continue */
633 	}
634 }
635 
636 struct arp_state *
637 arp_new(struct interface *ifp, const struct in_addr *addr)
638 {
639 	struct iarp_state *state;
640 	struct arp_state *astate;
641 
642 	if ((state = ARP_STATE(ifp)) == NULL) {
643 #ifdef PRIVSEP
644 		/* We need to ensure ARP is spawned so we can add to it. */
645 		if (IN_PRIVSEP_SE(ifp->ctx) && arp_open(ifp) == -1) {
646 			logerr(__func__);
647 			return NULL;
648 		}
649 #endif
650 	        ifp->if_data[IF_DATA_ARP] = malloc(sizeof(*state));
651 		state = ARP_STATE(ifp);
652 		if (state == NULL) {
653 			logerr(__func__);
654 			return NULL;
655 		}
656 		state->ifp = ifp;
657 		state->bpf_fd = -1;
658 		state->bpf_flags = 0;
659 		TAILQ_INIT(&state->arp_states);
660 	} else {
661 		if (addr && (astate = arp_find(ifp, addr)))
662 			return astate;
663 	}
664 
665 	if ((astate = calloc(1, sizeof(*astate))) == NULL) {
666 		logerr(__func__);
667 		return NULL;
668 	}
669 	astate->iface = ifp;
670 	state = ARP_STATE(ifp);
671 	TAILQ_INSERT_TAIL(&state->arp_states, astate, next);
672 	arp_change(astate, addr);
673 	return astate;
674 }
675 
676 void
677 arp_cancel(struct arp_state *astate)
678 {
679 
680 	eloop_timeout_delete(astate->iface->ctx->eloop, NULL, astate);
681 }
682 
683 void
684 arp_free(struct arp_state *astate)
685 {
686 	struct interface *ifp;
687 	struct iarp_state *state;
688 
689 	if (astate == NULL)
690 		return;
691 
692 	ifp = astate->iface;
693 	eloop_timeout_delete(ifp->ctx->eloop, NULL, astate);
694 	arp_change(astate, NULL);
695 	state =	ARP_STATE(ifp);
696 	TAILQ_REMOVE(&state->arp_states, astate, next);
697 	if (astate->free_cb)
698 		astate->free_cb(astate);
699 	free(astate);
700 	arp_tryfree(state);
701 }
702 
703 void
704 arp_freeaddr(struct interface *ifp, const struct in_addr *ia)
705 {
706 	struct arp_state *astate;
707 
708 	astate = arp_find(ifp, ia);
709 	arp_free(astate);
710 }
711 
712 void
713 arp_drop(struct interface *ifp)
714 {
715 	struct iarp_state *state;
716 	struct arp_state *astate;
717 
718 	while ((state = ARP_STATE(ifp)) != NULL &&
719 	    (astate = TAILQ_FIRST(&state->arp_states)) != NULL)
720 		arp_free(astate);
721 
722 	/* No need to close because the last free will close */
723 }
724