Home | History | Annotate | Line # | Download | only in src
      1 /* SPDX-License-Identifier: BSD-2-Clause */
      2 /*
      3  * dhcpcd - DHCP client daemon
      4  * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 #ifndef ARP_H
     30 #define ARP_H
     31 
     32 /* ARP timings from RFC5227 */
     33 #define PROBE_WAIT		 1
     34 #define PROBE_NUM		 3
     35 #define PROBE_MIN		 1
     36 #define PROBE_MAX		 2
     37 #define ANNOUNCE_WAIT		 2
     38 #define ANNOUNCE_NUM		 2
     39 #define ANNOUNCE_INTERVAL	 2
     40 #define MAX_CONFLICTS		10
     41 #define RATE_LIMIT_INTERVAL	60
     42 #define DEFEND_INTERVAL		10
     43 
     44 #include "bpf.h"
     45 #include "dhcpcd.h"
     46 #include "if.h"
     47 
     48 #ifdef IN_IFF_DUPLICATED
     49 /* NetBSD gained RFC 5227 support in the kernel.
     50  * This means dhcpcd doesn't need ARP except for ARPing support
     51  * and ARP announcing an address. */
     52 #if defined(__NetBSD_Version__) && __NetBSD_Version__ >= 799003900
     53 #define KERNEL_RFC5227
     54 #endif
     55 #endif
     56 
     57 struct arp_msg {
     58 	uint16_t op;
     59 	uint8_t sha[HWADDR_LEN];
     60 	struct in_addr sip;
     61 	uint8_t tha[HWADDR_LEN];
     62 	struct in_addr tip;
     63 	/* Frame header and sender to diagnose failures */
     64 	uint8_t fsha[HWADDR_LEN];
     65 	uint8_t ftha[HWADDR_LEN];
     66 };
     67 
     68 struct arp_state {
     69 	TAILQ_ENTRY(arp_state) next;
     70 	struct interface *iface;
     71 	struct in_addr addr;
     72 	struct bpf *bpf;
     73 
     74 	int probes;
     75 	int claims;
     76 	struct timespec defend;
     77 
     78 	void (*found_cb)(struct arp_state *, const struct arp_msg *);
     79 	void (*not_found_cb)(struct arp_state *);
     80 	void (*announced_cb)(struct arp_state *);
     81 	void (*defend_failed_cb)(struct arp_state *);
     82 	void (*free_cb)(struct arp_state *);
     83 };
     84 TAILQ_HEAD(arp_statehead, arp_state);
     85 
     86 struct iarp_state {
     87 	struct arp_statehead arp_states;
     88 };
     89 
     90 #define ARP_STATE(ifp)							       \
     91 	((struct iarp_state *)(ifp)->if_data[IF_DATA_ARP])
     92 #define ARP_CSTATE(ifp)							       \
     93 	((const struct iarp_state *)(ifp)->if_data[IF_DATA_ARP])
     94 
     95 #ifdef ARP
     96 void arp_packet(struct interface *, uint8_t *, size_t, unsigned int);
     97 struct arp_state *arp_new(struct interface *, const struct in_addr *);
     98 void arp_probe(struct arp_state *);
     99 struct arp_state *arp_ifannounceaddr(struct interface *, const struct in_addr *);
    100 struct arp_state * arp_find(struct interface *, const struct in_addr *);
    101 void arp_free(struct arp_state *);
    102 void arp_freeaddr(struct interface *, const struct in_addr *);
    103 void arp_drop(struct interface *);
    104 #endif /* ARP */
    105 #endif /* ARP_H */
    106