1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 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
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>
46 #include "dhcp-common.h"
48 /* UDP port numbers for BOOTP */
52 #define MAGIC_COOKIE 0x63825363
53 #define BROADCAST_FLAG 0x8000
55 /* BOOTP message OP code */
59 /* DHCP message type */
60 #define DHCP_DISCOVER 1
62 #define DHCP_REQUEST 3
63 #define DHCP_DECLINE 4
66 #define DHCP_RELEASE 7
68 #define DHCP_FORCERENEW 9
70 /* Constants taken from RFC 2131. */
75 #define DHCP_RAND_MIN -1
76 #define DHCP_RAND_MAX 1
79 /* Be strictly conformant for section 4.1.1 */
80 # define DHCP_MIN_DELAY 1
81 # define DHCP_MAX_DELAY 10
83 /* or mirror the more modern IPv6RS and DHCPv6 delays */
84 # define DHCP_MIN_DELAY 0
85 # define DHCP_MAX_DELAY 1
105 DHO_OPTSOVERLOADED = 52,
106 DHO_MESSAGETYPE = 53,
108 DHO_PARAMETERREQUESTLIST = 55,
110 DHO_MAXMESSAGESIZE = 57,
111 DHO_RENEWALTIME = 58,
113 DHO_VENDORCLASSID = 60,
115 DHO_USERCLASS = 77, /* RFC 3004 */
116 DHO_RAPIDCOMMIT = 80, /* RFC 4039 */
118 DHO_AUTHENTICATION = 90, /* RFC 3118 */
119 DHO_AUTOCONFIGURE = 116, /* RFC 2563 */
120 DHO_DNSSEARCH = 119, /* RFC 3397 */
121 DHO_CSR = 121, /* RFC 3442 */
122 DHO_VIVCO = 124, /* RFC 3925 */
123 DHO_VIVSO = 125, /* RFC 3925 */
124 DHO_FORCERENEW_NONCE = 145, /* RFC 6704 */
125 DHO_MUDURL = 161, /* draft-ietf-opsawg-mud */
126 DHO_SIXRD = 212, /* RFC 5969 */
127 DHO_MSCSR = 249, /* MS code for RFC 3442 */
131 /* FQDN values - lsnybble used in flags
132 * hsnybble to create order
133 * and to allow 0x00 to mean disable
142 /* Sizes for BOOTP options */
143 #define BOOTP_CHADDR_LEN 16
144 #define BOOTP_SNAME_LEN 64
145 #define BOOTP_FILE_LEN 128
146 #define BOOTP_VEND_LEN 64
148 /* DHCP is basically an extension to BOOTP */
150 uint8_t op; /* message type */
151 uint8_t htype; /* hardware address type */
152 uint8_t hlen; /* hardware address length */
153 uint8_t hops; /* should be zero in client message */
154 uint32_t xid; /* transaction id */
155 uint16_t secs; /* elapsed time in sec. from boot */
156 uint16_t flags; /* such as broadcast flag */
157 uint32_t ciaddr; /* (previously allocated) client IP */
158 uint32_t yiaddr; /* 'your' client IP address */
159 uint32_t siaddr; /* should be zero in client's messages */
160 uint32_t giaddr; /* should be zero in client's messages */
161 uint8_t chaddr[BOOTP_CHADDR_LEN]; /* client's hardware address */
162 uint8_t sname[BOOTP_SNAME_LEN]; /* server host name */
163 uint8_t file[BOOTP_FILE_LEN]; /* boot file name */
164 uint8_t vend[BOOTP_VEND_LEN]; /* vendor specific area */
165 /* DHCP allows a variable length vendor area */
168 #define DHCP_MIN_LEN (offsetof(struct bootp, vend) + 4)
182 uint32_t renewaltime;
184 struct in_addr server;
189 #ifndef DHCP_INFINITE_LIFETIME
190 # define DHCP_INFINITE_LIFETIME (~0U)
218 struct dhcp_lease lease;
220 unsigned int interval;
227 struct ipv4_addr *addr;
230 char leasefile[sizeof(LEASEFILE) + IF_NAMESIZE + (IF_SSIDLEN * 4)];
231 struct timespec started;
232 unsigned char *clientid;
233 struct authstate auth;
235 ssize_t arping_index;
240 #define D_STATE(ifp) \
241 ((struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
242 #define D_CSTATE(ifp) \
243 ((const struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
244 #define D_STATE_RUNNING(ifp) \
245 (D_CSTATE((ifp)) && D_CSTATE((ifp))->new && D_CSTATE((ifp))->reason)
247 #define IS_DHCP(b) ((b)->vend[0] == 0x63 && \
248 (b)->vend[1] == 0x82 && \
249 (b)->vend[2] == 0x53 && \
250 (b)->vend[3] == 0x63)
253 #include "if-options.h"
255 ssize_t print_rfc3361(FILE *, const uint8_t *, size_t);
256 ssize_t print_rfc3442(FILE *, const uint8_t *, size_t);
258 int dhcp_openudp(struct in_addr *);
259 void dhcp_packet(struct interface *, uint8_t *, size_t, unsigned int);
260 void dhcp_recvmsg(struct dhcpcd_ctx *, struct msghdr *);
261 void dhcp_printoptions(const struct dhcpcd_ctx *,
262 const struct dhcp_opt *, size_t);
263 uint16_t dhcp_get_mtu(const struct interface *);
264 int dhcp_get_routes(rb_tree_t *, struct interface *);
265 ssize_t dhcp_env(FILE *, const char *, const struct interface *,
266 const struct bootp *, size_t);
268 struct ipv4_addr *dhcp_handleifa(int, struct ipv4_addr *, pid_t pid);
269 void dhcp_drop(struct interface *, const char *);
270 void dhcp_start(struct interface *);
271 void dhcp_abort(struct interface *);
272 void dhcp_discover(void *);
273 void dhcp_inform(struct interface *);
274 void dhcp_renew(struct interface *);
275 void dhcp_bind(struct interface *);
276 void dhcp_reboot_newopts(struct interface *, unsigned long long);
277 void dhcp_close(struct interface *);
278 void dhcp_free(struct interface *);
279 int dhcp_dump(struct interface *);