2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2008 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
38 /* Max MTU - defines dhcp option length */
42 /* UDP port numbers for DHCP */
43 #define DHCP_SERVER_PORT 67
44 #define DHCP_CLIENT_PORT 68
46 #define MAGIC_COOKIE 0x63825363
47 #define BROADCAST_FLAG 0x8000
49 /* DHCP message OP code */
50 #define DHCP_BOOTREQUEST 1
51 #define DHCP_BOOTREPLY 2
53 /* DHCP message type */
54 #define DHCP_DISCOVER 1
56 #define DHCP_REQUEST 3
57 #define DHCP_DECLINE 4
60 #define DHCP_RELEASE 7
63 /* Constants taken from RFC 2131. */
68 #define DHCP_RAND_MIN -1
69 #define DHCP_RAND_MAX 1
70 #define DHCP_ARP_FAIL 10
72 /* number of usecs in a second. */
73 #define USECS_SECOND 1000000
74 /* As we use timevals, we should use the usec part for
75 * greater randomisation. */
76 #define DHCP_RAND_MIN_U DHCP_RAND_MIN * USECS_SECOND
77 #define DHCP_RAND_MAX_U DHCP_RAND_MAX * USECS_SECOND
78 #define PROBE_MIN_U PROBE_MIN * USECS_SECOND
79 #define PROBE_MAX_U PROBE_MAX * USECS_SECOND
98 DHO_OPTIONSOVERLOADED
= 52,
101 DHO_PARAMETERREQUESTLIST
= 55,
103 DHO_MAXMESSAGESIZE
= 57,
104 DHO_RENEWALTIME
= 58,
106 DHO_VENDORCLASSID
= 60,
108 DHO_USERCLASS
= 77, /* RFC 3004 */
110 DHO_DNSSEARCH
= 119, /* RFC 3397 */
111 DHO_CSR
= 121, /* RFC 3442 */
112 DHO_MSCSR
= 249, /* MS code for RFC 3442 */
116 /* FQDN values - lsnybble used in flags
117 * hsnybble to create order
118 * and to allow 0x00 to mean disable
127 /* Sizes for DHCP options */
128 #define DHCP_CHADDR_LEN 16
129 #define SERVERNAME_LEN 64
130 #define BOOTFILE_LEN 128
131 #define DHCP_UDP_LEN (14 + 20 + 8)
132 #define DHCP_FIXED_LEN (DHCP_UDP_LEN + 226)
133 #define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN)
135 /* Some crappy DHCP servers require the BOOTP minimum length */
136 #define BOOTP_MESSAGE_LENTH_MIN 300
138 struct dhcp_message
{
139 uint8_t op
; /* message type */
140 uint8_t hwtype
; /* hardware address type */
141 uint8_t hwlen
; /* hardware address length */
142 uint8_t hwopcount
; /* should be zero in client message */
143 uint32_t xid
; /* transaction id */
144 uint16_t secs
; /* elapsed time in sec. from boot */
146 uint32_t ciaddr
; /* (previously allocated) client IP */
147 uint32_t yiaddr
; /* 'your' client IP address */
148 uint32_t siaddr
; /* should be zero in client's messages */
149 uint32_t giaddr
; /* should be zero in client's messages */
150 uint8_t chaddr
[DHCP_CHADDR_LEN
]; /* client's hardware address */
151 uint8_t servername
[SERVERNAME_LEN
]; /* server host name */
152 uint8_t bootfile
[BOOTFILE_LEN
]; /* boot file name */
154 uint8_t options
[DHCP_OPTION_LEN
]; /* message options - cookie */
162 uint32_t renewaltime
;
164 struct in_addr server
;
166 struct timeval boundtime
;
171 #include "if-options.h"
174 #define add_option_mask(var, val) (var[val >> 3] |= 1 << (val & 7))
175 #define del_option_mask(var, val) (var[val >> 3] &= ~(1 << (val & 7)))
176 #define has_option_mask(var, val) (var[val >> 3] & (1 << (val & 7)))
177 int make_option_mask(uint8_t *, const char *, int);
178 void print_options(void);
179 char *get_option_string(const struct dhcp_message
*, uint8_t);
180 int get_option_addr(struct in_addr
*, const struct dhcp_message
*, uint8_t);
181 int get_option_uint32(uint32_t *, const struct dhcp_message
*, uint8_t);
182 int get_option_uint16(uint16_t *, const struct dhcp_message
*, uint8_t);
183 int get_option_uint8(uint8_t *, const struct dhcp_message
*, uint8_t);
184 #define is_bootp(m) (m && \
185 !IN_LINKLOCAL(htonl((m)->yiaddr)) && \
186 get_option_uint8(NULL, m, DHO_MESSAGETYPE) == -1)
187 struct rt
*get_option_routes(const struct dhcp_message
*, const char *, int *);
188 ssize_t
configure_env(char **, const char *, const struct dhcp_message
*,
189 const struct if_options
*);
191 int dhcp_message_add_addr(struct dhcp_message
*, uint8_t, struct in_addr
);
192 ssize_t
make_message(struct dhcp_message
**, const struct interface
*,
194 int valid_dhcp_packet(unsigned char *);
196 ssize_t
write_lease(const struct interface
*, const struct dhcp_message
*);
197 struct dhcp_message
*read_lease(const struct interface
*);
198 void get_lease(struct dhcp_lease
*, const struct dhcp_message
*);