xref: /dragonfly/contrib/dhcpcd/src/ipv4.h (revision f984587a)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2023 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 #ifndef IPV4_H
30 #define IPV4_H
31 
32 #include "dhcpcd.h"
33 
34 /* Prefer our macro */
35 #ifdef HTONL
36 #undef HTONL
37 #endif
38 
39 #ifndef BYTE_ORDER
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
46 #else
47 #error Endian unknown
48 #endif
49 #endif
50 
51 #if BYTE_ORDER == BIG_ENDIAN
52 #define HTONL(A) (A)
53 #elif BYTE_ORDER == LITTLE_ENDIAN
54 #define HTONL(A) \
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 */
60 
61 #ifdef __sun
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
69 #endif
70 
71 #ifdef IN_IFF_TENTATIVE
72 #define IN_IFF_NOTUSEABLE \
73         (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED | IN_IFF_DETACHED)
74 #endif
75 
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)
78 
79 #ifdef __linux__
80 #define IP_LIFETIME
81 #endif
82 
83 struct ipv4_addr {
84 	TAILQ_ENTRY(ipv4_addr) next;
85 	struct in_addr addr;
86 	struct in_addr mask;
87 	struct in_addr brd;
88 	struct interface *iface;
89 	int addr_flags;
90 	unsigned int flags;
91 #ifdef IP_LIFETIME
92 	uint32_t vltime;
93 	uint32_t pltime;
94 #endif
95 	char saddr[INET_ADDRSTRLEN + 3];
96 #ifdef ALIAS_ADDR
97 	char alias[IF_NAMESIZE];
98 #endif
99 };
100 TAILQ_HEAD(ipv4_addrhead, ipv4_addr);
101 
102 #define	IPV4_AF_STALE		(1U << 0)
103 #define	IPV4_AF_NEW		(1U << 1)
104 
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))
110 
111 struct ipv4_state {
112 	struct ipv4_addrhead addrs;
113 };
114 
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])
119 
120 #ifdef INET
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 *);
127 
128 bool inet_getroutes(struct dhcpcd_ctx *, rb_tree_t *);
129 
130 #define STATE_ADDED		0x01
131 #define STATE_FAKE		0x02
132 #define STATE_EXPIRED		0x04
133 
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 *,
137     uint32_t, uint32_t);
138 struct ipv4_addr *ipv4_applyaddr(void *);
139 
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 *,
152     int, pid_t);
153 
154 void ipv4_free(struct interface *);
155 #endif /* INET */
156 
157 #endif /* IPV4_H */
158