#opt domain STRING - client's domain name
#opt search STRING_LIST - search domains
#opt nisdomain STRING
-#opt timezone NUM
+#opt timezone NUM - (localtime - UTC_time) in seconds. signed
#opt tftp STRING - TFTP server name
#opt bootfile STRING - file (kernel image) to load for booting
#opt bootsize NUM - size of that file
#opt dns IP_LIST
#opt wins IP_LIST
#opt nissrv IP_LIST
-#opt namesrv IP_LIST - obsolete, disabled
-#opt logsrv IP_LIST
-#opt cookiesrv IP_LIST - rarely (never?) used, disabled
+#opt ntpsrv IP_LIST
#opt lprsrv IP_LIST
#opt swapsrv IP
-#opt timesrv IP_LIST
-#opt ntpsrv IP_LIST
+# Obsolete options, no longer supported
+#opt logsrv IP_LIST - 704/UDP log server (not syslog!)
+#opt namesrv IP_LIST - IEN 116 name server, obsolete (August 1979!!!)
+#opt cookiesrv IP_LIST - RFC 865 "quote of the day" server, rarely (never?) used
+#opt timesrv IP_LIST - RFC 868 time server, rarely (never?) used
uint32_t cookie; /* fixed first four option bytes (99,130,83,99 dec) */
uint8_t options[DHCP_OPTIONS_BUFSIZE + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS];
} PACKED;
+#define DHCP_PKT_SNAME_LEN 64
+#define DHCP_PKT_FILE_LEN 128
+#define DHCP_PKT_SNAME_LEN_STR "64"
+#define DHCP_PKT_FILE_LEN_STR "128"
struct ip_udp_dhcp_packet {
struct iphdr ip;
server_config.ifindex);
}
-/* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
+/* Send the dhcp packet.
+ * If force broadcast is set, the packet will be broadcast.
+ */
static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
{
if (dhcp_pkt->gateway_nip)
add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
}
-/* add in the bootp options */
-static void add_bootp_options(struct dhcp_packet *packet)
+/* Fill options field, siaddr_nip, and sname and boot_file fields.
+ * TODO: teach this code to use overload option.
+ */
+static void add_server_options(struct dhcp_packet *packet)
{
+ struct option_set *curr = server_config.options;
+
+ while (curr) {
+ if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
+ add_option_string(packet->options, curr->data);
+ curr = curr->next;
+ }
+
packet->siaddr_nip = server_config.siaddr_nip;
+
if (server_config.sname)
strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
if (server_config.boot_file)
static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
{
struct dhcp_packet packet;
- uint32_t req_nip;
uint32_t lease_time_sec = server_config.max_lease_sec;
- uint8_t *req_ip_opt;
const char *p_host_name;
- struct option_set *curr;
struct in_addr addr;
init_packet(&packet, oldpacket, DHCPOFFER);
/* ADDME: if static, short circuit */
if (!static_lease_nip) {
+ uint32_t req_nip;
+ uint8_t *req_ip_opt;
+
/* The client is in our lease/offered table */
if (lease) {
packet.yiaddr = lease->lease_nip;
/* and the IP is in the lease range */
&& ntohl(req_nip) >= server_config.start_ip
&& ntohl(req_nip) <= server_config.end_ip
- /* and is not already taken/offered */
- && (!(lease = find_lease_by_nip(req_nip))
- /* or its taken, but expired */
- || is_expired_lease(lease))
+ /* and */
+ && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
+ || is_expired_lease(lease) /* or is taken, but expired */
+ )
) {
packet.yiaddr = req_nip;
}
}
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
-
- curr = server_config.options;
- while (curr) {
- if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
- add_option_string(packet.options, curr->data);
- curr = curr->next;
- }
-
- add_bootp_options(&packet);
+ add_server_options(&packet);
addr.s_addr = packet.yiaddr;
bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
- return send_packet(&packet, 0);
+ return send_packet(&packet, /*force_bcast:*/ 0);
}
static int send_NAK(struct dhcp_packet *oldpacket)
init_packet(&packet, oldpacket, DHCPNAK);
log1("Sending NAK");
- return send_packet(&packet, 1);
+ return send_packet(&packet, /*force_bcast:*/ 1);
}
static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
{
struct dhcp_packet packet;
- struct option_set *curr;
uint32_t lease_time_sec;
struct in_addr addr;
const char *p_host_name;
lease_time_sec = select_lease_time(oldpacket);
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
-
- curr = server_config.options;
- while (curr) {
- if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
- add_option_string(packet.options, curr->data);
- curr = curr->next;
- }
-
- add_bootp_options(&packet);
+ add_server_options(&packet);
addr.s_addr = packet.yiaddr;
bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
- if (send_packet(&packet, 0) < 0)
+ if (send_packet(&packet, /*force_bcast:*/ 0) < 0)
return -1;
p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
static int send_inform(struct dhcp_packet *oldpacket)
{
struct dhcp_packet packet;
- struct option_set *curr;
init_packet(&packet, oldpacket, DHCPACK);
+ add_server_options(&packet);
- curr = server_config.options;
- while (curr) {
- if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
- add_option_string(packet.options, curr->data);
- curr = curr->next;
- }
-
- add_bootp_options(&packet);
-
- return send_packet(&packet, 0);
+ return send_packet(&packet, /*force_bcast:*/ 0);
}
}
-/* Clear every lease out that chaddr OR yiaddr matches and is nonzero */
-static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
+/* Clear out all leases with matching nonzero chaddr OR yiaddr.
+ * If chaddr == NULL, this is a conflict lease.
+ */
+static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
{
- unsigned i, j;
-
- for (j = 0; j < 16 && !chaddr[j]; j++)
- continue;
+ unsigned i;
for (i = 0; i < server_config.max_leases; i++) {
- if ((j != 16 && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
+ if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
|| (yiaddr && g_leases[i].lease_nip == yiaddr)
) {
memset(&g_leases[i], 0, sizeof(g_leases[i]));
}
-/* Add a lease into the table, clearing out any old ones */
+/* Add a lease into the table, clearing out any old ones
+ * If chaddr == NULL, this is a conflict lease.
+ */
struct dyn_lease* FAST_FUNC add_lease(
const uint8_t *chaddr, uint32_t yiaddr,
leasetime_t leasetime,
struct dyn_lease *oldest;
/* clean out any old ones */
- clear_lease(chaddr, yiaddr);
+ clear_leases(chaddr, yiaddr);
oldest = oldest_expired_lease();
if (oldest) {
- oldest->hostname[0] = '\0';
+ memset(oldest, 0, sizeof(*oldest));
if (hostname) {
char *p;
if (hostname_len > sizeof(oldest->hostname))
p++;
}
}
- memcpy(oldest->lease_mac, chaddr, 6);
+ if (chaddr)
+ memcpy(oldest->lease_mac, chaddr, 6);
oldest->lease_nip = yiaddr;
oldest->expires = time(NULL) + leasetime;
}
/* Check if the IP is taken; if it is, add it to the lease table */
static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
{
- /* 16 zero bytes */
- static const uint8_t blank_chaddr[16] = { 0 };
- /* = { 0 } helps gcc to put it in rodata, not bss */
-
struct in_addr temp;
int r;
temp.s_addr = nip;
bb_info_msg("%s belongs to someone, reserving it for %u seconds",
inet_ntoa(temp), (unsigned)server_config.conflict_time);
- add_lease(blank_chaddr, nip, server_config.conflict_time, NULL, 0);
+ add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
return 0;
}
}
}
- if (oldest_lease && is_expired_lease(oldest_lease)
+ if (oldest_lease
+ && is_expired_lease(oldest_lease)
&& nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
) {
return oldest_lease->lease_nip;
{ OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */
{ OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */
{ OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */
- { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
+// { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */
// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */
{ OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */
- { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
+// { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */
// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */
{ OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */
{ OPTION_STRING | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */
{ OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */
{ OPTION_U16 , 0x1a }, /* DHCP_MTU */
{ OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */
- { OPTION_STRING , 0x28 }, /* nisdomain */
- { OPTION_IP | OPTION_LIST , 0x29 }, /* nissrv */
+ { OPTION_STRING , 0x28 }, /* DHCP_NIS_DOMAIN */
+ { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */
{ OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */
{ OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */
{ OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */
{ OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */
- { OPTION_STRING , 0x38 }, /* DHCP_MESSAGE */
- { OPTION_STRING , 0x42 }, /* tftp */
- { OPTION_STRING , 0x43 }, /* bootfile */
+ { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */
+//TODO: must be combined with 'sname' and 'file' handling:
+ { OPTION_STRING , 0x42 }, /* DHCP_TFTP_SERVER_NAME */
+ { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */
//TODO: not a string, but a set of LASCII strings:
-// { OPTION_STRING , 0x4D }, /* userclass */
+// { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */
#if ENABLE_FEATURE_UDHCP_RFC3397
- { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* search */
+ { OPTION_STR1035 | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */
#endif
{ OPTION_STATIC_ROUTES , 0x79 }, /* DHCP_STATIC_ROUTES */
- /* MSIE's "Web Proxy Autodiscovery Protocol" support */
- { OPTION_STRING , 0xfc }, /* wpad */
+ { OPTION_STRING , 0xfc }, /* DHCP_WPAD */
/* Options below have no match in dhcp_option_strings[],
* are not passed to dhcpc scripts, and cannot be specified
{ OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */
{ OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */
{ OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */
- { OPTION_STRING , 0x3C }, /* DHCP_VENDOR */
- { OPTION_STRING , 0x3D }, /* DHCP_CLIENT_ID */
+ { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */
+//FIXME: handling of this option is not exactly correct:
+ { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */
{ 0, 0 } /* zeroed terminating entry */
};
"subnet" "\0" /* DHCP_SUBNET */
"timezone" "\0" /* DHCP_TIME_OFFSET */
"router" "\0" /* DHCP_ROUTER */
- "timesrv" "\0" /* DHCP_TIME_SERVER */
+// "timesrv" "\0" /* DHCP_TIME_SERVER */
// "namesrv" "\0" /* DHCP_NAME_SERVER */
"dns" "\0" /* DHCP_DNS_SERVER */
- "logsrv" "\0" /* DHCP_LOG_SERVER */
+// "logsrv" "\0" /* DHCP_LOG_SERVER */
// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */
"lprsrv" "\0" /* DHCP_LPR_SERVER */
"hostname" "\0" /* DHCP_HOST_NAME */
"ipttl" "\0" /* DHCP_IP_TTL */
"mtu" "\0" /* DHCP_MTU */
"broadcast" "\0" /* DHCP_BROADCAST */
- "nisdomain" "\0" /* */
- "nissrv" "\0" /* */
+ "nisdomain" "\0" /* DHCP_NIS_DOMAIN */
+ "nissrv" "\0" /* DHCP_NIS_SERVER */
"ntpsrv" "\0" /* DHCP_NTP_SERVER */
"wins" "\0" /* DHCP_WINS_SERVER */
"lease" "\0" /* DHCP_LEASE_TIME */
"serverid" "\0" /* DHCP_SERVER_ID */
- "message" "\0" /* DHCP_MESSAGE */
- "tftp" "\0"
- "bootfile" "\0"
-// "userclass" "\0"
+ "message" "\0" /* DHCP_ERR_MESSAGE */
+ "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */
+ "bootfile" "\0" /* DHCP_BOOT_FILE */
+// "userclass" "\0" /* DHCP_USER_CLASS */
#if ENABLE_FEATURE_UDHCP_RFC3397
- "search" "\0"
+ "search" "\0" /* DHCP_DOMAIN_SEARCH */
#endif
// "staticroutes" is only used to set udhcpc environment, it doesn't work
-// in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet:
- "staticroutes" "\0" /* DHCP_STATIC_ROUTES */
- /* MSIE's "Web Proxy Autodiscovery Protocol" support */
- "wpad" "\0"
+// in udhcpd.conf since OPTION_STATIC_ROUTES is not handled yet
+// by "string->option" conversion code:
+ "staticroutes" "\0"/* DHCP_STATIC_ROUTES */
+ "wpad" "\0" /* DHCP_WPAD */
;
/* add an option string to the options */
/* option bytes: [code][len][data1][data2]..[dataLEN] */
-int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
+void FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string)
{
int end = end_option(optionptr);
if (end + string[OPT_LEN] + 2 + 1 >= DHCP_OPTIONS_BUFSIZE) {
bb_error_msg("option 0x%02x did not fit into the packet",
string[OPT_CODE]);
- return 0;
+ return;
}
log_option("Adding option", string);
memcpy(optionptr + end, string, string[OPT_LEN] + 2);
optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
- return string[OPT_LEN] + 2;
}
/* add a one to four byte option to a packet */
-int FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
+void FAST_FUNC add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
{
const struct dhcp_option *dh;
data <<= 8 * (4 - len);
/* Assignment is unaligned! */
move_to_unaligned32(&option[OPT_DATA], data);
- return add_option_string(optionptr, option);
+ add_option_string(optionptr, option);
+ return;
}
}
bb_error_msg("can't add option 0x%02x", code);
- return 0;
}
/* Do not modify below here unless you know what you are doing!! */
/*****************************************************************/
-/* DHCP protocol -- see RFC 2131 */
+/* DHCP protocol. See RFC 2131 */
#define DHCP_MAGIC 0x63825363
-/* DHCP option codes (partial list) */
+/* DHCP option codes (partial list). See RFC 2132.
+ * Commented out options are handled by common option machinery,
+ * uncommented ones have spacial cases (grep for them to see).
+ */
#define DHCP_PADDING 0x00
#define DHCP_SUBNET 0x01
-#define DHCP_TIME_OFFSET 0x02
-#define DHCP_ROUTER 0x03
-#define DHCP_TIME_SERVER 0x04
-//#define DHCP_NAME_SERVER 0x05 /* _really_ ancient kind of NS */
-#define DHCP_DNS_SERVER 0x06
-#define DHCP_LOG_SERVER 0x07
+//#define DHCP_TIME_OFFSET 0x02 /* (localtime - UTC_time) in seconds. signed */
+//#define DHCP_ROUTER 0x03
+//#define DHCP_TIME_SERVER 0x04 /* RFC 868 time server (32-bit, 0 = 1.1.1900) */
+//#define DHCP_NAME_SERVER 0x05 /* IEN 116 _really_ ancient kind of NS */
+//#define DHCP_DNS_SERVER 0x06
+//#define DHCP_LOG_SERVER 0x07 /* port 704 UDP log (not syslog)
//#define DHCP_COOKIE_SERVER 0x08 /* "quote of the day" server */
-#define DHCP_LPR_SERVER 0x09
+//#define DHCP_LPR_SERVER 0x09
#define DHCP_HOST_NAME 0x0c /* either client informs server or server gives name to client */
-#define DHCP_BOOT_SIZE 0x0d
-#define DHCP_DOMAIN_NAME 0x0f /* server gives domain suffix */
-#define DHCP_SWAP_SERVER 0x10
-#define DHCP_ROOT_PATH 0x11
-#define DHCP_IP_TTL 0x17
-#define DHCP_MTU 0x1a
-#define DHCP_BROADCAST 0x1c
-#define DHCP_NTP_SERVER 0x2a
-#define DHCP_WINS_SERVER 0x2c
+//#define DHCP_BOOT_SIZE 0x0d
+//#define DHCP_DOMAIN_NAME 0x0f /* server gives domain suffix */
+//#define DHCP_SWAP_SERVER 0x10
+//#define DHCP_ROOT_PATH 0x11
+//#define DHCP_IP_TTL 0x17
+//#define DHCP_MTU 0x1a
+//#define DHCP_BROADCAST 0x1c
+//#define DHCP_NIS_DOMAIN 0x28
+//#define DHCP_NIS_SERVER 0x29
+//#define DHCP_NTP_SERVER 0x2a
+//#define DHCP_WINS_SERVER 0x2c
#define DHCP_REQUESTED_IP 0x32 /* sent by client if specific IP is wanted */
#define DHCP_LEASE_TIME 0x33
#define DHCP_OPTION_OVERLOAD 0x34
#define DHCP_MESSAGE_TYPE 0x35
#define DHCP_SERVER_ID 0x36 /* by default server's IP */
#define DHCP_PARAM_REQ 0x37 /* list of options client wants */
-#define DHCP_MESSAGE 0x38 /* error message when sending NAK etc */
+//#define DHCP_ERR_MESSAGE 0x38 /* error message when sending NAK etc */
#define DHCP_MAX_SIZE 0x39
-//#define DHCP_T1 0x3a
-//#define DHCP_T2 0x3b
#define DHCP_VENDOR 0x3c /* client's vendor (a string) */
#define DHCP_CLIENT_ID 0x3d /* by default client's MAC addr, but may be arbitrarily long */
+//#define DHCP_TFTP_SERVER_NAME 0x42 /* same as 'sname' field */
+//#define DHCP_BOOT_FILE 0x43 /* same as 'file' field */
+//#define DHCP_USER_CLASS 0x4d /* RFC 3004. set of LASCII strings. "I am a printer" etc */
#define DHCP_FQDN 0x51 /* client asks to update DNS to map its FQDN to its new IP */
-#define DHCP_STATIC_ROUTES 0x79
-#define DHCP_END 0xFF
+//#define DHCP_DOMAIN_SEARCH 0x77 /* RFC 3397. set of ASCIZ string, DNS-style compressed */
+//#define DHCP_STATIC_ROUTES 0x79 /* RFC 3442. (mask,ip,router) tuples */
+//#define DHCP_WPAD 0xfc /* MSIE's Web Proxy Autodiscovery Protocol */
+#define DHCP_END 0xff
+
+
/* Offsets in option byte sequence */
#define OPT_CODE 0
#define OPT_LEN 1
uint8_t *get_option(struct dhcp_packet *packet, int code) FAST_FUNC;
int end_option(uint8_t *optionptr) FAST_FUNC;
-int add_option_string(uint8_t *optionptr, uint8_t *string) FAST_FUNC;
-int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) FAST_FUNC;
+void add_option_string(uint8_t *optionptr, uint8_t *string) FAST_FUNC;
+void add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data) FAST_FUNC;
#if ENABLE_FEATURE_UDHCP_RFC3397
char *dname_dec(const uint8_t *cstr, int clen, const char *pre) FAST_FUNC;
uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen) FAST_FUNC;
}
if (!(over & FILE_FIELD) && packet->file[0]) {
/* watch out for invalid packets */
- packet->file[sizeof(packet->file) - 1] = '\0';
- *curr = xasprintf("boot_file=%s", packet->file);
+ *curr = xasprintf("boot_file=%."DHCP_PKT_FILE_LEN_STR"s", packet->file);
putenv(*curr++);
}
if (!(over & SNAME_FIELD) && packet->sname[0]) {
/* watch out for invalid packets */
- packet->sname[sizeof(packet->sname) - 1] = '\0';
- *curr = xasprintf("sname=%s", packet->sname);
+ *curr = xasprintf("sname=%."DHCP_PKT_SNAME_LEN_STR"s", packet->sname);
putenv(*curr++);
}
return envp;