1 /* $NetBSD: dhcp.h,v 1.11 2015/08/21 10:39:00 roy Exp $ */ 2 3 /* 4 * dhcpcd - DHCP client daemon 5 * Copyright (c) 2006-2015 Roy Marples <roy@marples.name> 6 * All rights reserved 7 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef DHCP_H 31 #define DHCP_H 32 33 #include <arpa/inet.h> 34 #include <netinet/in.h> 35 36 #include <limits.h> 37 #include <stdint.h> 38 39 #include "arp.h" 40 #include "auth.h" 41 #include "dhcp-common.h" 42 43 /* UDP port numbers for DHCP */ 44 #define DHCP_SERVER_PORT 67 45 #define DHCP_CLIENT_PORT 68 46 47 #define MAGIC_COOKIE 0x63825363 48 #define BROADCAST_FLAG 0x8000 49 50 /* DHCP message OP code */ 51 #define DHCP_BOOTREQUEST 1 52 #define DHCP_BOOTREPLY 2 53 54 /* DHCP message type */ 55 #define DHCP_DISCOVER 1 56 #define DHCP_OFFER 2 57 #define DHCP_REQUEST 3 58 #define DHCP_DECLINE 4 59 #define DHCP_ACK 5 60 #define DHCP_NAK 6 61 #define DHCP_RELEASE 7 62 #define DHCP_INFORM 8 63 #define DHCP_FORCERENEW 9 64 65 /* Constants taken from RFC 2131. */ 66 #define T1 0.5 67 #define T2 0.875 68 #define DHCP_BASE 4 69 #define DHCP_MAX 64 70 #define DHCP_RAND_MIN -1 71 #define DHCP_RAND_MAX 1 72 73 #ifdef RFC2131_STRICT 74 /* Be strictly conformant for section 4.1.1 */ 75 # define DHCP_MIN_DELAY 1 76 # define DHCP_MAX_DELAY 10 77 #else 78 /* or mirror the more modern IPv6RS and DHCPv6 delays */ 79 # define DHCP_MIN_DELAY 0 80 # define DHCP_MAX_DELAY 1 81 #endif 82 83 /* DHCP options */ 84 enum DHO { 85 DHO_PAD = 0, 86 DHO_SUBNETMASK = 1, 87 DHO_ROUTER = 3, 88 DHO_DNSSERVER = 6, 89 DHO_HOSTNAME = 12, 90 DHO_DNSDOMAIN = 15, 91 DHO_MTU = 26, 92 DHO_BROADCAST = 28, 93 DHO_STATICROUTE = 33, 94 DHO_NISDOMAIN = 40, 95 DHO_NISSERVER = 41, 96 DHO_NTPSERVER = 42, 97 DHO_VENDOR = 43, 98 DHO_IPADDRESS = 50, 99 DHO_LEASETIME = 51, 100 DHO_OPTIONSOVERLOADED = 52, 101 DHO_MESSAGETYPE = 53, 102 DHO_SERVERID = 54, 103 DHO_PARAMETERREQUESTLIST = 55, 104 DHO_MESSAGE = 56, 105 DHO_MAXMESSAGESIZE = 57, 106 DHO_RENEWALTIME = 58, 107 DHO_REBINDTIME = 59, 108 DHO_VENDORCLASSID = 60, 109 DHO_CLIENTID = 61, 110 DHO_USERCLASS = 77, /* RFC 3004 */ 111 DHO_RAPIDCOMMIT = 80, /* RFC 4039 */ 112 DHO_FQDN = 81, 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 */ 122 DHO_END = 255 123 }; 124 125 /* FQDN values - lsnybble used in flags 126 * hsnybble to create order 127 * and to allow 0x00 to mean disable 128 */ 129 enum FQDN { 130 FQDN_DISABLE = 0x00, 131 FQDN_NONE = 0x18, 132 FQDN_PTR = 0x20, 133 FQDN_BOTH = 0x31 134 }; 135 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) 143 144 /* Some crappy DHCP servers require the BOOTP minimum length */ 145 #define BOOTP_MESSAGE_LENTH_MIN 300 146 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) 150 # ifndef __packed 151 # define __packed __attribute__((__packed__)) 152 # endif 153 #else 154 # ifndef __packed 155 # define __packed 156 # endif 157 #endif 158 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 */ 166 uint16_t flags; 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 */ 174 uint32_t cookie; 175 uint8_t options[DHCP_OPTION_LEN]; /* message options - cookie */ 176 } __packed; 177 178 struct dhcp_lease { 179 struct in_addr addr; 180 struct in_addr net; 181 struct in_addr brd; 182 uint32_t leasetime; 183 uint32_t renewaltime; 184 uint32_t rebindtime; 185 struct in_addr server; 186 uint8_t frominfo; 187 uint32_t cookie; 188 }; 189 190 enum DHS { 191 DHS_INIT, 192 DHS_DISCOVER, 193 DHS_REQUEST, 194 DHS_BOUND, 195 DHS_RENEW, 196 DHS_REBIND, 197 DHS_REBOOT, 198 DHS_INFORM, 199 DHS_RENEW_REQUESTED, 200 DHS_RELEASE 201 }; 202 203 struct dhcp_state { 204 enum DHS state; 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; 210 const char *reason; 211 time_t interval; 212 time_t nakoff; 213 uint32_t xid; 214 int socket; 215 216 int raw_fd; 217 struct in_addr addr; 218 struct in_addr net; 219 struct in_addr dst; 220 uint8_t added; 221 222 char leasefile[sizeof(LEASEFILE) + IF_NAMESIZE + (IF_SSIDSIZE * 4)]; 223 struct timespec started; 224 unsigned char *clientid; 225 struct authstate auth; 226 size_t arping_index; 227 }; 228 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) 235 236 #include "dhcpcd.h" 237 #include "if-options.h" 238 239 #ifdef INET 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); 243 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 *); 254 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 *, 260 uint8_t); 261 int valid_dhcp_packet(unsigned char *); 262 263 void dhcp_handleifa(int, struct interface *, 264 const struct in_addr *, const struct in_addr *, const struct in_addr *, 265 int); 266 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 *); 277 #else 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) 286 #endif 287 288 #endif 289