1 /* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2006-2008 Roy Marples <roy@marples.name> 4 * All rights reserved 5 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28 #ifndef DHCP_H 29 #define DHCP_H 30 31 #include <netinet/in.h> 32 33 #include <sys/stdint.h> 34 35 #include "dhcp_common.h" 36 37 /* Max MTU - defines dhcp option length */ 38 #define MTU_MAX 1500 39 #define MTU_MIN 576 40 41 /* UDP port numbers for DHCP */ 42 #define DHCP_SERVER_PORT 67 43 #define DHCP_CLIENT_PORT 68 44 45 #define MAGIC_COOKIE 0x63825363 46 #define BROADCAST_FLAG 0x8000 47 48 /* DHCP message OP code */ 49 #define DHCP_BOOTREQUEST 1 50 #define DHCP_BOOTREPLY 2 51 52 /* DHCP message type */ 53 #define DHCP_DISCOVER 1 54 #define DHCP_OFFER 2 55 #define DHCP_REQUEST 3 56 #define DHCP_DECLINE 4 57 #define DHCP_ACK 5 58 #define DHCP_NAK 6 59 #define DHCP_RELEASE 7 60 #define DHCP_INFORM 8 61 62 /* Constants taken from RFC 2131. */ 63 #define T1 0.5 64 #define T2 0.875 65 #define DHCP_BASE 4 66 #define DHCP_MAX 64 67 #define DHCP_RAND_MIN -1 68 #define DHCP_RAND_MAX 1 69 #define DHCP_ARP_FAIL 10 70 71 /* number of usecs in a second. */ 72 #define USECS_SECOND 1000000 73 /* As we use timevals, we should use the usec part for 74 * greater randomisation. */ 75 #define DHCP_RAND_MIN_U DHCP_RAND_MIN * USECS_SECOND 76 #define DHCP_RAND_MAX_U DHCP_RAND_MAX * USECS_SECOND 77 #define PROBE_MIN_U PROBE_MIN * USECS_SECOND 78 #define PROBE_MAX_U PROBE_MAX * USECS_SECOND 79 80 /* DHCP options */ 81 enum DHO { 82 DHO_PAD = 0, 83 DHO_SUBNETMASK = 1, 84 DHO_ROUTER = 3, 85 DHO_DNSSERVER = 6, 86 DHO_HOSTNAME = 12, 87 DHO_DNSDOMAIN = 15, 88 DHO_MTU = 26, 89 DHO_BROADCAST = 28, 90 DHO_STATICROUTE = 33, 91 DHO_NISDOMAIN = 40, 92 DHO_NISSERVER = 41, 93 DHO_NTPSERVER = 42, 94 DHO_VENDOR = 43, 95 DHO_IPADDRESS = 50, 96 DHO_LEASETIME = 51, 97 DHO_OPTIONSOVERLOADED = 52, 98 DHO_MESSAGETYPE = 53, 99 DHO_SERVERID = 54, 100 DHO_PARAMETERREQUESTLIST = 55, 101 DHO_MESSAGE = 56, 102 DHO_MAXMESSAGESIZE = 57, 103 DHO_RENEWALTIME = 58, 104 DHO_REBINDTIME = 59, 105 DHO_VENDORCLASSID = 60, 106 DHO_CLIENTID = 61, 107 DHO_USERCLASS = 77, /* RFC 3004 */ 108 DHO_FQDN = 81, 109 DHO_DNSSEARCH = 119, /* RFC 3397 */ 110 DHO_CSR = 121, /* RFC 3442 */ 111 DHO_MSCSR = 249, /* MS code for RFC 3442 */ 112 DHO_END = 255 113 }; 114 115 /* FQDN values - lsnybble used in flags 116 * hsnybble to create order 117 * and to allow 0x00 to mean disable 118 */ 119 enum FQDN { 120 FQDN_DISABLE = 0x00, 121 FQDN_NONE = 0x18, 122 FQDN_PTR = 0x20, 123 FQDN_BOTH = 0x31 124 }; 125 126 /* Sizes for DHCP options */ 127 #define DHCP_CHADDR_LEN 16 128 #define SERVERNAME_LEN 64 129 #define BOOTFILE_LEN 128 130 #define DHCP_UDP_LEN (14 + 20 + 8) 131 #define DHCP_FIXED_LEN (DHCP_UDP_LEN + 226) 132 #define DHCP_OPTION_LEN (MTU_MAX - DHCP_FIXED_LEN) 133 134 /* Some crappy DHCP servers require the BOOTP minimum length */ 135 #define BOOTP_MESSAGE_LENTH_MIN 300 136 137 struct dhcp_message { 138 uint8_t op; /* message type */ 139 uint8_t hwtype; /* hardware address type */ 140 uint8_t hwlen; /* hardware address length */ 141 uint8_t hwopcount; /* should be zero in client message */ 142 uint32_t xid; /* transaction id */ 143 uint16_t secs; /* elapsed time in sec. from boot */ 144 uint16_t flags; 145 uint32_t ciaddr; /* (previously allocated) client IP */ 146 uint32_t yiaddr; /* 'your' client IP address */ 147 uint32_t siaddr; /* should be zero in client's messages */ 148 uint32_t giaddr; /* should be zero in client's messages */ 149 uint8_t chaddr[DHCP_CHADDR_LEN]; /* client's hardware address */ 150 uint8_t servername[SERVERNAME_LEN]; /* server host name */ 151 uint8_t bootfile[BOOTFILE_LEN]; /* boot file name */ 152 uint32_t cookie; 153 uint8_t options[DHCP_OPTION_LEN]; /* message options - cookie */ 154 } __packed; 155 156 struct dhcp_lease { 157 struct in_addr addr; 158 struct in_addr net; 159 struct in_addr brd; 160 uint32_t leasetime; 161 uint32_t renewaltime; 162 uint32_t rebindtime; 163 struct in_addr server; 164 time_t leasedfrom; 165 struct timeval boundtime; 166 uint8_t frominfo; 167 uint32_t cookie; 168 }; 169 170 #include "dhcp_dhcpcd.h" 171 #include "dhcp_if-options.h" 172 #include "dhcp_net.h" 173 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) != 0) 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 *); 190 191 ssize_t make_message(struct dhcp_message **, const struct interface *, 192 uint8_t); 193 int valid_dhcp_packet(unsigned char *); 194 195 ssize_t write_lease(const struct interface *, const struct dhcp_message *); 196 struct dhcp_message *read_lease(const struct interface *); 197 void get_lease(struct dhcp_lease *, const struct dhcp_message *); 198 199 #endif 200