1 /* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2006-2008 Roy Marples <roy (at) 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 <arpa/inet.h> 32 #include <netinet/in.h> 33 34 #include <stdint.h> 35 36 #include "common.h" 37 38 /* Max MTU - defines dhcp option length */ 39 #define MTU_MAX 1500 40 #define MTU_MIN 576 41 42 /* UDP port numbers for DHCP */ 43 #define DHCP_SERVER_PORT 67 44 #define DHCP_CLIENT_PORT 68 45 46 #define MAGIC_COOKIE 0x63825363 47 #define BROADCAST_FLAG 0x8000 48 49 /* DHCP message OP code */ 50 #define DHCP_BOOTREQUEST 1 51 #define DHCP_BOOTREPLY 2 52 53 /* DHCP message type */ 54 #define DHCP_DISCOVER 1 55 #define DHCP_OFFER 2 56 #define DHCP_REQUEST 3 57 #define DHCP_DECLINE 4 58 #define DHCP_ACK 5 59 #define DHCP_NAK 6 60 #define DHCP_RELEASE 7 61 #define DHCP_INFORM 8 62 63 /* Constants taken from RFC 2131. */ 64 #define T1 0.5 65 #define T2 0.875 66 #define DHCP_BASE 4 67 #define DHCP_MAX 64 68 #define DHCP_RAND_MIN -1 69 #define DHCP_RAND_MAX 1 70 #define DHCP_ARP_FAIL 10 71 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 80 81 /* DHCP options */ 82 enum DHO { 83 DHO_PAD = 0, 84 DHO_SUBNETMASK = 1, 85 DHO_ROUTER = 3, 86 DHO_DNSSERVER = 6, 87 DHO_HOSTNAME = 12, 88 DHO_DNSDOMAIN = 15, 89 DHO_MTU = 26, 90 DHO_BROADCAST = 28, 91 DHO_STATICROUTE = 33, 92 DHO_NISDOMAIN = 40, 93 DHO_NISSERVER = 41, 94 DHO_NTPSERVER = 42, 95 DHO_VENDOR = 43, 96 DHO_IPADDRESS = 50, 97 DHO_LEASETIME = 51, 98 DHO_OPTIONSOVERLOADED = 52, 99 DHO_MESSAGETYPE = 53, 100 DHO_SERVERID = 54, 101 DHO_PARAMETERREQUESTLIST = 55, 102 DHO_MESSAGE = 56, 103 DHO_MAXMESSAGESIZE = 57, 104 DHO_RENEWALTIME = 58, 105 DHO_REBINDTIME = 59, 106 DHO_VENDORCLASSID = 60, 107 DHO_CLIENTID = 61, 108 DHO_USERCLASS = 77, /* RFC 3004 */ 109 DHO_FQDN = 81, 110 DHO_DNSSEARCH = 119, /* RFC 3397 */ 111 DHO_CSR = 121, /* RFC 3442 */ 112 DHO_MSCSR = 249, /* MS code for RFC 3442 */ 113 DHO_END = 255 114 }; 115 116 /* FQDN values - lsnybble used in flags 117 * hsnybble to create order 118 * and to allow 0x00 to mean disable 119 */ 120 enum FQDN { 121 FQDN_DISABLE = 0x00, 122 FQDN_NONE = 0x18, 123 FQDN_PTR = 0x20, 124 FQDN_BOTH = 0x31 125 }; 126 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) 134 135 /* Some crappy DHCP servers require the BOOTP minimum length */ 136 #define BOOTP_MESSAGE_LENTH_MIN 300 137 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 */ 145 uint16_t flags; 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 */ 153 uint32_t cookie; 154 uint8_t options[DHCP_OPTION_LEN]; /* message options - cookie */ 155 } _packed; 156 157 struct dhcp_lease { 158 struct in_addr addr; 159 struct in_addr net; 160 struct in_addr brd; 161 uint32_t leasetime; 162 uint32_t renewaltime; 163 uint32_t rebindtime; 164 struct in_addr server; 165 time_t leasedfrom; 166 struct timeval boundtime; 167 uint8_t frominfo; 168 uint32_t cookie; 169 }; 170 171 #include "dhcpcd.h" 172 #include "if-options.h" 173 #include "net.h" 174 175 #define add_option_mask(var, val) (var[val >> 3] |= 1 << (val & 7)) 176 #define del_option_mask(var, val) (var[val >> 3] &= ~(1 << (val & 7))) 177 #define has_option_mask(var, val) (var[val >> 3] & (1 << (val & 7))) 178 int make_option_mask(uint8_t *, const char *, int); 179 void print_options(void); 180 char *get_option_string(const struct dhcp_message *, uint8_t); 181 int get_option_addr(struct in_addr *, const struct dhcp_message *, uint8_t); 182 int get_option_uint32(uint32_t *, const struct dhcp_message *, uint8_t); 183 int get_option_uint16(uint16_t *, const struct dhcp_message *, uint8_t); 184 int get_option_uint8(uint8_t *, const struct dhcp_message *, uint8_t); 185 #define is_bootp(m) (m && \ 186 !IN_LINKLOCAL(htonl((m)->yiaddr)) && \ 187 get_option_uint8(NULL, m, DHO_MESSAGETYPE) == -1) 188 struct rt *get_option_routes(const struct dhcp_message *, const char *, int *); 189 ssize_t configure_env(char **, const char *, const struct dhcp_message *, 190 const struct if_options *); 191 192 int dhcp_message_add_addr(struct dhcp_message *, uint8_t, struct in_addr); 193 ssize_t make_message(struct dhcp_message **, const struct interface *, 194 uint8_t); 195 int valid_dhcp_packet(unsigned char *); 196 197 ssize_t write_lease(const struct interface *, const struct dhcp_message *); 198 struct dhcp_message *read_lease(const struct interface *); 199 void get_lease(struct dhcp_lease *, const struct dhcp_message *); 200 201 #endif 202