1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2021 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 /* Prefer our macro */
40 #define BIG_ENDIAN 1234
41 #define LITTLE_ENDIAN 4321
42 #if defined(_BIG_ENDIAN)
43 #define BYTE_ORDER BIG_ENDIAN
44 #elif defined(_LITTLE_ENDIAN)
45 #define BYTE_ORDER LITTLE_ENDIAN
51 #if BYTE_ORDER == BIG_ENDIAN
53 #elif BYTE_ORDER == LITTLE_ENDIAN
55 ((((uint32_t)(A) & 0xff000000) >> 24) | \
56 (((uint32_t)(A) & 0x00ff0000) >> 8) | \
57 (((uint32_t)(A) & 0x0000ff00) << 8) | \
58 (((uint32_t)(A) & 0x000000ff) << 24))
59 #endif /* BYTE_ORDER */
62 /* Solaris lacks these defines.
63 * While it supports DaD, to seems to only expose IFF_DUPLICATE
64 * so we have no way of knowing if it's tentative or not.
65 * I don't even know if Solaris has any special treatment for tentative. */
66 # define IN_IFF_TENTATIVE 0x01
67 # define IN_IFF_DUPLICATED 0x02
68 # define IN_IFF_DETACHED 0x00
71 #ifdef IN_IFF_TENTATIVE
72 #define IN_IFF_NOTUSEABLE \
73 (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED | IN_IFF_DETACHED)
76 #define IN_ARE_ADDR_EQUAL(a, b) ((a)->s_addr == (b)->s_addr)
77 #define IN_IS_ADDR_UNSPECIFIED(a) ((a)->s_addr == INADDR_ANY)
84 TAILQ_ENTRY(ipv4_addr) next;
88 struct interface *iface;
95 char saddr[INET_ADDRSTRLEN + 3];
97 char alias[IF_NAMESIZE];
100 TAILQ_HEAD(ipv4_addrhead, ipv4_addr);
102 #define IPV4_AF_STALE (1U << 0)
103 #define IPV4_AF_NEW (1U << 1)
105 #define IPV4_ADDR_EQ(a1, a2) ((a1) && (a1)->addr.s_addr == (a2)->addr.s_addr)
106 #define IPV4_MASK1_EQ(a1, a2) ((a1) && (a1)->mask.s_addr == (a2)->mask.s_addr)
107 #define IPV4_MASK_EQ(a1, a2) (IPV4_ADDR_EQ(a1, a2) && IPV4_MASK1_EQ(a1, a2))
108 #define IPV4_BRD1_EQ(a1, a2) ((a1) && (a1)->brd.s_addr == (a2)->brd.s_addr)
109 #define IPV4_BRD_EQ(a1, a2) (IPV4_MASK_EQ(a1, a2) && IPV4_BRD1_EQ(a1, a2))
112 struct ipv4_addrhead addrs;
115 #define IPV4_STATE(ifp) \
116 ((struct ipv4_state *)(ifp)->if_data[IF_DATA_IPV4])
117 #define IPV4_CSTATE(ifp) \
118 ((const struct ipv4_state *)(ifp)->if_data[IF_DATA_IPV4])
121 struct ipv4_state *ipv4_getstate(struct interface *);
122 int ipv4_ifcmp(const struct interface *, const struct interface *);
123 uint8_t inet_ntocidr(struct in_addr);
124 int inet_cidrtoaddr(int, struct in_addr *);
125 uint32_t ipv4_getnetmask(uint32_t);
126 int ipv4_hasaddr(const struct interface *);
128 bool inet_getroutes(struct dhcpcd_ctx *, rb_tree_t *);
130 #define STATE_ADDED 0x01
131 #define STATE_FAKE 0x02
132 #define STATE_EXPIRED 0x04
134 int ipv4_deladdr(struct ipv4_addr *, int);
135 struct ipv4_addr *ipv4_addaddr(struct interface *,
136 const struct in_addr *, const struct in_addr *, const struct in_addr *,
138 struct ipv4_addr *ipv4_applyaddr(void *);
140 struct ipv4_addr *ipv4_iffindaddr(struct interface *,
141 const struct in_addr *, const struct in_addr *);
142 struct ipv4_addr *ipv4_iffindlladdr(struct interface *);
143 struct ipv4_addr *ipv4_findaddr(struct dhcpcd_ctx *, const struct in_addr *);
144 struct ipv4_addr *ipv4_findmaskaddr(struct dhcpcd_ctx *,
145 const struct in_addr *);
146 struct ipv4_addr *ipv4_findmaskbrd(struct dhcpcd_ctx *,
147 const struct in_addr *);
148 void ipv4_markaddrsstale(struct interface *);
149 void ipv4_deletestaleaddrs(struct interface *);
150 void ipv4_handleifa(struct dhcpcd_ctx *, int, struct if_head *, const char *,
151 const struct in_addr *, const struct in_addr *, const struct in_addr *,
154 void ipv4_free(struct interface *);