Import dhcpcd-8.0.4 to vendor branch.
[dragonfly.git] / contrib / dhcpcd / src / dhcp.h
blob4b6311a38bb536544f95dd6392e3acb0f0c5e779
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2019 Roy Marples <roy@marples.name>
5 * All rights reserved
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.
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.
29 #ifndef DHCP_H
30 #define DHCP_H
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */
37 #include <netinet/udp.h>
38 #undef __FAVOR_BSD
40 #include <limits.h>
41 #include <stdint.h>
43 #include "arp.h"
44 #include "auth.h"
45 #include "dhcp-common.h"
47 /* UDP port numbers for BOOTP */
48 #define BOOTPS 67
49 #define BOOTPC 68
51 #define MAGIC_COOKIE 0x63825363
52 #define BROADCAST_FLAG 0x8000
54 /* BOOTP message OP code */
55 #define BOOTREQUEST 1
56 #define BOOTREPLY 2
58 /* DHCP message type */
59 #define DHCP_DISCOVER 1
60 #define DHCP_OFFER 2
61 #define DHCP_REQUEST 3
62 #define DHCP_DECLINE 4
63 #define DHCP_ACK 5
64 #define DHCP_NAK 6
65 #define DHCP_RELEASE 7
66 #define DHCP_INFORM 8
67 #define DHCP_FORCERENEW 9
69 /* Constants taken from RFC 2131. */
70 #define T1 0.5
71 #define T2 0.875
72 #define DHCP_BASE 4
73 #define DHCP_MAX 64
74 #define DHCP_RAND_MIN -1
75 #define DHCP_RAND_MAX 1
77 #ifdef RFC2131_STRICT
78 /* Be strictly conformant for section 4.1.1 */
79 # define DHCP_MIN_DELAY 1
80 # define DHCP_MAX_DELAY 10
81 #else
82 /* or mirror the more modern IPv6RS and DHCPv6 delays */
83 # define DHCP_MIN_DELAY 0
84 # define DHCP_MAX_DELAY 1
85 #endif
87 /* DHCP options */
88 enum DHO {
89 DHO_PAD = 0,
90 DHO_SUBNETMASK = 1,
91 DHO_ROUTER = 3,
92 DHO_DNSSERVER = 6,
93 DHO_HOSTNAME = 12,
94 DHO_DNSDOMAIN = 15,
95 DHO_MTU = 26,
96 DHO_BROADCAST = 28,
97 DHO_STATICROUTE = 33,
98 DHO_NISDOMAIN = 40,
99 DHO_NISSERVER = 41,
100 DHO_NTPSERVER = 42,
101 DHO_VENDOR = 43,
102 DHO_IPADDRESS = 50,
103 DHO_LEASETIME = 51,
104 DHO_OPTSOVERLOADED = 52,
105 DHO_MESSAGETYPE = 53,
106 DHO_SERVERID = 54,
107 DHO_PARAMETERREQUESTLIST = 55,
108 DHO_MESSAGE = 56,
109 DHO_MAXMESSAGESIZE = 57,
110 DHO_RENEWALTIME = 58,
111 DHO_REBINDTIME = 59,
112 DHO_VENDORCLASSID = 60,
113 DHO_CLIENTID = 61,
114 DHO_USERCLASS = 77, /* RFC 3004 */
115 DHO_RAPIDCOMMIT = 80, /* RFC 4039 */
116 DHO_FQDN = 81,
117 DHO_AUTHENTICATION = 90, /* RFC 3118 */
118 DHO_AUTOCONFIGURE = 116, /* RFC 2563 */
119 DHO_DNSSEARCH = 119, /* RFC 3397 */
120 DHO_CSR = 121, /* RFC 3442 */
121 DHO_VIVCO = 124, /* RFC 3925 */
122 DHO_VIVSO = 125, /* RFC 3925 */
123 DHO_FORCERENEW_NONCE = 145, /* RFC 6704 */
124 DHO_MUDURL = 161, /* draft-ietf-opsawg-mud */
125 DHO_SIXRD = 212, /* RFC 5969 */
126 DHO_MSCSR = 249, /* MS code for RFC 3442 */
127 DHO_END = 255
130 /* FQDN values - lsnybble used in flags
131 * hsnybble to create order
132 * and to allow 0x00 to mean disable
134 enum FQDN {
135 FQDN_DISABLE = 0x00,
136 FQDN_NONE = 0x18,
137 FQDN_PTR = 0x20,
138 FQDN_BOTH = 0x31
141 /* Sizes for BOOTP options */
142 #define BOOTP_CHADDR_LEN 16
143 #define BOOTP_SNAME_LEN 64
144 #define BOOTP_FILE_LEN 128
145 #define BOOTP_VEND_LEN 64
147 /* DHCP is basically an extension to BOOTP */
148 struct bootp {
149 uint8_t op; /* message type */
150 uint8_t htype; /* hardware address type */
151 uint8_t hlen; /* hardware address length */
152 uint8_t hops; /* should be zero in client message */
153 uint32_t xid; /* transaction id */
154 uint16_t secs; /* elapsed time in sec. from boot */
155 uint16_t flags; /* such as broadcast flag */
156 uint32_t ciaddr; /* (previously allocated) client IP */
157 uint32_t yiaddr; /* 'your' client IP address */
158 uint32_t siaddr; /* should be zero in client's messages */
159 uint32_t giaddr; /* should be zero in client's messages */
160 uint8_t chaddr[BOOTP_CHADDR_LEN]; /* client's hardware address */
161 uint8_t sname[BOOTP_SNAME_LEN]; /* server host name */
162 uint8_t file[BOOTP_FILE_LEN]; /* boot file name */
163 uint8_t vend[BOOTP_VEND_LEN]; /* vendor specific area */
164 /* DHCP allows a variable length vendor area */
167 struct bootp_pkt
169 struct ip ip;
170 struct udphdr udp;
171 struct bootp bootp;
174 struct dhcp_lease {
175 struct in_addr addr;
176 struct in_addr mask;
177 struct in_addr brd;
178 uint32_t leasetime;
179 uint32_t renewaltime;
180 uint32_t rebindtime;
181 struct in_addr server;
182 uint8_t frominfo;
183 uint32_t cookie;
186 #ifndef DHCP_INFINITE_LIFETIME
187 # define DHCP_INFINITE_LIFETIME (~0U)
188 #endif
190 enum DHS {
191 DHS_NONE,
192 DHS_INIT,
193 DHS_DISCOVER,
194 DHS_REQUEST,
195 DHS_PROBE,
196 DHS_BOUND,
197 DHS_RENEW,
198 DHS_REBIND,
199 DHS_REBOOT,
200 DHS_INFORM,
201 DHS_RENEW_REQUESTED,
202 DHS_RELEASE
205 struct dhcp_state {
206 enum DHS state;
207 struct bootp *sent;
208 size_t sent_len;
209 struct bootp *offer;
210 size_t offer_len;
211 struct bootp *new;
212 size_t new_len;
213 struct bootp *old;
214 size_t old_len;
215 struct dhcp_lease lease;
216 const char *reason;
217 time_t interval;
218 time_t nakoff;
219 uint32_t xid;
220 int socket;
222 int bpf_fd;
223 unsigned int bpf_flags;
224 int udp_fd;
225 struct ipv4_addr *addr;
226 uint8_t added;
228 char leasefile[sizeof(LEASEFILE) + IF_NAMESIZE + (IF_SSIDLEN * 4)];
229 struct timespec started;
230 unsigned char *clientid;
231 struct authstate auth;
232 #ifdef ARPING
233 ssize_t arping_index;
234 #endif
237 #ifdef INET
238 #define D_STATE(ifp) \
239 ((struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
240 #define D_CSTATE(ifp) \
241 ((const struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
242 #define D_STATE_RUNNING(ifp) \
243 (D_CSTATE((ifp)) && D_CSTATE((ifp))->new && D_CSTATE((ifp))->reason)
245 #define IS_DHCP(b) ((b)->vend[0] == 0x63 && \
246 (b)->vend[1] == 0x82 && \
247 (b)->vend[2] == 0x53 && \
248 (b)->vend[3] == 0x63)
250 #include "dhcpcd.h"
251 #include "if-options.h"
253 ssize_t print_rfc3361(FILE *, const uint8_t *, size_t);
254 ssize_t print_rfc3442(FILE *, const uint8_t *, size_t);
256 void dhcp_printoptions(const struct dhcpcd_ctx *,
257 const struct dhcp_opt *, size_t);
258 uint16_t dhcp_get_mtu(const struct interface *);
259 int dhcp_get_routes(rb_tree_t *, struct interface *);
260 ssize_t dhcp_env(FILE *, const char *, const struct interface *,
261 const struct bootp *, size_t);
263 void dhcp_handleifa(int, struct ipv4_addr *, pid_t pid);
264 void dhcp_drop(struct interface *, const char *);
265 void dhcp_start(struct interface *);
266 void dhcp_abort(struct interface *);
267 void dhcp_discover(void *);
268 void dhcp_inform(struct interface *);
269 void dhcp_renew(struct interface *);
270 void dhcp_bind(struct interface *);
271 void dhcp_reboot_newopts(struct interface *, unsigned long long);
272 void dhcp_close(struct interface *);
273 void dhcp_free(struct interface *);
274 int dhcp_dump(struct interface *);
275 #endif /* INET */
277 #endif /* DHCP_H */