1 /* $NetBSD: dhcp.h,v 1.11 2015/08/21 10:39:00 roy Exp $ */
4 * dhcpcd - DHCP client daemon
5 * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
41 #include "dhcp-common.h"
43 /* UDP port numbers for DHCP */
44 #define DHCP_SERVER_PORT 67
45 #define DHCP_CLIENT_PORT 68
47 #define MAGIC_COOKIE 0x63825363
48 #define BROADCAST_FLAG 0x8000
50 /* DHCP message OP code */
51 #define DHCP_BOOTREQUEST 1
52 #define DHCP_BOOTREPLY 2
54 /* DHCP message type */
55 #define DHCP_DISCOVER 1
57 #define DHCP_REQUEST 3
58 #define DHCP_DECLINE 4
61 #define DHCP_RELEASE 7
63 #define DHCP_FORCERENEW 9
65 /* Constants taken from RFC 2131. */
70 #define DHCP_RAND_MIN -1
71 #define DHCP_RAND_MAX 1
74 /* Be strictly conformant for section 4.1.1 */
75 # define DHCP_MIN_DELAY 1
76 # define DHCP_MAX_DELAY 10
78 /* or mirror the more modern IPv6RS and DHCPv6 delays */
79 # define DHCP_MIN_DELAY 0
80 # define DHCP_MAX_DELAY 1
100 DHO_OPTIONSOVERLOADED
= 52,
101 DHO_MESSAGETYPE
= 53,
103 DHO_PARAMETERREQUESTLIST
= 55,
105 DHO_MAXMESSAGESIZE
= 57,
106 DHO_RENEWALTIME
= 58,
108 DHO_VENDORCLASSID
= 60,
110 DHO_USERCLASS
= 77, /* RFC 3004 */
111 DHO_RAPIDCOMMIT
= 80, /* RFC 4039 */
113 DHO_AUTHENTICATION
= 90, /* RFC 3118 */
114 DHO_AUTOCONFIGURE
= 116, /* RFC 2563 */
115 DHO_DNSSEARCH
= 119, /* RFC 3397 */
116 DHO_CSR
= 121, /* RFC 3442 */
117 DHO_VIVCO
= 124, /* RFC 3925 */
118 DHO_VIVSO
= 125, /* RFC 3925 */
119 DHO_FORCERENEW_NONCE
= 145, /* RFC 6704 */
120 DHO_SIXRD
= 212, /* RFC 5969 */
121 DHO_MSCSR
= 249, /* MS code for RFC 3442 */
125 /* FQDN values - lsnybble used in flags
126 * hsnybble to create order
127 * and to allow 0x00 to mean disable
136 /* Sizes for DHCP options */
137 #define DHCP_CHADDR_LEN 16
138 #define SERVERNAME_LEN 64
139 #define BOOTFILE_LEN 128
140 #define DHCP_UDP_LEN (14 + 20 + 8)
141 #define DHCP_FIXED_LEN (DHCP_UDP_LEN + 226)
142 #define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN)
144 /* Some crappy DHCP servers require the BOOTP minimum length */
145 #define BOOTP_MESSAGE_LENTH_MIN 300
147 /* Don't import common.h as that defines __unused which causes problems
148 * on some Linux systems which define it as part of a structure */
149 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
151 # define __packed __attribute__((__packed__))
159 struct dhcp_message
{
160 uint8_t op
; /* message type */
161 uint8_t hwtype
; /* hardware address type */
162 uint8_t hwlen
; /* hardware address length */
163 uint8_t hwopcount
; /* should be zero in client message */
164 uint32_t xid
; /* transaction id */
165 uint16_t secs
; /* elapsed time in sec. from boot */
167 uint32_t ciaddr
; /* (previously allocated) client IP */
168 uint32_t yiaddr
; /* 'your' client IP address */
169 uint32_t siaddr
; /* should be zero in client's messages */
170 uint32_t giaddr
; /* should be zero in client's messages */
171 uint8_t chaddr
[DHCP_CHADDR_LEN
]; /* client's hardware address */
172 uint8_t servername
[SERVERNAME_LEN
]; /* server host name */
173 uint8_t bootfile
[BOOTFILE_LEN
]; /* boot file name */
175 uint8_t options
[DHCP_OPTION_LEN
]; /* message options - cookie */
183 uint32_t renewaltime
;
185 struct in_addr server
;
205 struct dhcp_message
*sent
;
206 struct dhcp_message
*offer
;
207 struct dhcp_message
*new;
208 struct dhcp_message
*old
;
209 struct dhcp_lease lease
;
222 char leasefile
[sizeof(LEASEFILE
) + IF_NAMESIZE
+ (IF_SSIDSIZE
* 4)];
223 struct timespec started
;
224 unsigned char *clientid
;
225 struct authstate auth
;
229 #define D_STATE(ifp) \
230 ((struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
231 #define D_CSTATE(ifp) \
232 ((const struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP])
233 #define D_STATE_RUNNING(ifp) \
234 (D_CSTATE((ifp)) && D_CSTATE((ifp))->new && D_CSTATE((ifp))->reason)
237 #include "if-options.h"
240 char *decode_rfc3361(const uint8_t *, size_t);
241 ssize_t
decode_rfc3442(char *, size_t, const uint8_t *p
, size_t);
242 ssize_t
decode_rfc5969(char *, size_t, const uint8_t *p
, size_t);
244 void dhcp_printoptions(const struct dhcpcd_ctx
*,
245 const struct dhcp_opt
*, size_t);
246 int get_option_addr(struct dhcpcd_ctx
*,struct in_addr
*,
247 const struct dhcp_message
*, uint8_t);
248 #define IS_BOOTP(i, m) ((m) != NULL && \
249 get_option_uint8((i)->ctx, NULL, (m), DHO_MESSAGETYPE) == -1)
250 uint16_t dhcp_get_mtu(const struct interface
*);
251 struct rt_head
*dhcp_get_routes(struct interface
*);
252 ssize_t
dhcp_env(char **, const char *, const struct dhcp_message
*,
253 const struct interface
*);
255 uint32_t dhcp_xid(const struct interface
*);
256 struct dhcp_message
*dhcp_message_new(const struct in_addr
*addr
,
257 const struct in_addr
*mask
);
258 int dhcp_message_add_addr(struct dhcp_message
*, uint8_t, struct in_addr
);
259 ssize_t
make_message(struct dhcp_message
**, const struct interface
*,
261 int valid_dhcp_packet(unsigned char *);
263 void dhcp_handleifa(int, struct interface
*,
264 const struct in_addr
*, const struct in_addr
*, const struct in_addr
*,
267 void dhcp_drop(struct interface
*, const char *);
268 void dhcp_start(struct interface
*);
269 void dhcp_abort(struct interface
*);
270 void dhcp_discover(void *);
271 void dhcp_inform(struct interface
*);
272 void dhcp_bind(struct interface
*);
273 void dhcp_reboot_newopts(struct interface
*, unsigned long long);
274 void dhcp_close(struct interface
*);
275 void dhcp_free(struct interface
*);
276 int dhcp_dump(struct interface
*);
278 #define dhcp_drop(a, b) {}
279 #define dhcp_start(a) {}
280 #define dhcp_abort(a) {}
281 #define dhcp_reboot(a, b) (b = b)
282 #define dhcp_reboot_newopts(a, b) (b = b)
283 #define dhcp_close(a) {}
284 #define dhcp_free(a) {}
285 #define dhcp_dump(a) (-1)