2011-07-25 12:00:57 +05:30
|
|
|
/* dhcp.c - general DHCP protocol handling
|
2011-03-31 09:29:22 +05:30
|
|
|
*
|
2014-03-31 02:53:35 +05:30
|
|
|
* Copyright (c) 2004-2014 Nicholas J. Kain <njkain at gmail dot com>
|
2011-07-25 12:00:57 +05:30
|
|
|
* All rights reserved.
|
2011-03-31 09:29:22 +05:30
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2011-03-31 09:29:22 +05:30
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
2011-03-31 09:29:22 +05:30
|
|
|
*
|
2011-07-25 12:00:57 +05:30
|
|
|
* - Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2011-03-31 09:29:22 +05:30
|
|
|
*/
|
|
|
|
|
2010-12-24 20:11:52 +05:30
|
|
|
#include <stdlib.h>
|
2014-03-18 11:06:14 +05:30
|
|
|
#include <stddef.h>
|
2014-03-20 05:02:45 +05:30
|
|
|
#include <stdio.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2011-06-11 20:49:05 +05:30
|
|
|
#include <arpa/inet.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <errno.h>
|
2014-03-31 02:32:48 +05:30
|
|
|
#include "nk/log.h"
|
|
|
|
#include "nk/io.h"
|
|
|
|
#include "nk/random.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2011-07-02 13:21:44 +05:30
|
|
|
#include "dhcp.h"
|
2011-06-30 09:17:31 +05:30
|
|
|
#include "state.h"
|
2010-12-24 21:10:46 +05:30
|
|
|
#include "arp.h"
|
2010-12-24 20:42:41 +05:30
|
|
|
#include "ifchange.h"
|
2010-12-24 20:11:52 +05:30
|
|
|
#include "sys.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
#include "options.h"
|
2014-04-04 13:42:25 +05:30
|
|
|
#include "sockd.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2011-07-01 07:03:38 +05:30
|
|
|
typedef enum {
|
|
|
|
LM_NONE = 0,
|
|
|
|
LM_COOKED,
|
|
|
|
LM_RAW
|
|
|
|
} listen_mode_t;
|
|
|
|
|
2014-04-04 13:42:25 +05:30
|
|
|
static int get_udp_unicast_socket(void)
|
2011-06-25 20:10:41 +05:30
|
|
|
{
|
2014-04-04 13:42:25 +05:30
|
|
|
return request_sockd_fd("u", 1, NULL);
|
|
|
|
}
|
2011-06-25 20:10:41 +05:30
|
|
|
|
2014-04-04 13:42:25 +05:30
|
|
|
static int get_raw_broadcast_socket(void)
|
|
|
|
{
|
|
|
|
return request_sockd_fd("s", 1, NULL);
|
|
|
|
}
|
2011-06-25 20:10:41 +05:30
|
|
|
|
2014-04-04 13:42:25 +05:30
|
|
|
static int get_udp_listen_socket(struct client_state_t *cs)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
buf[0] = 'u';
|
|
|
|
memcpy(buf + 1, &cs->clientAddr, sizeof cs->clientAddr);
|
|
|
|
return request_sockd_fd(buf, 1 + sizeof cs->clientAddr, NULL);
|
2011-06-25 20:10:41 +05:30
|
|
|
}
|
|
|
|
|
2014-04-04 13:42:25 +05:30
|
|
|
static int get_raw_listen_socket(struct client_state_t *cs)
|
2011-06-25 20:10:41 +05:30
|
|
|
{
|
2014-04-04 13:42:25 +05:30
|
|
|
char resp;
|
|
|
|
int fd = request_sockd_fd("L", 1, &resp);
|
|
|
|
switch (resp) {
|
|
|
|
case 'L': cs->using_dhcp_bpf = 1; break;
|
|
|
|
case 'l': cs->using_dhcp_bpf = 0; break;
|
|
|
|
default: suicide("%s: (%s) expected l or L sockd reply but got %c",
|
|
|
|
client_config.interface, __func__, resp);
|
2011-06-25 20:10:41 +05:30
|
|
|
}
|
2011-06-29 09:26:12 +05:30
|
|
|
return fd;
|
|
|
|
}
|
2011-06-25 20:10:41 +05:30
|
|
|
|
2011-06-29 09:26:12 +05:30
|
|
|
// Broadcast a DHCP message using a UDP socket.
|
2014-04-06 15:54:13 +05:30
|
|
|
static ssize_t send_dhcp_cooked(struct client_state_t *cs,
|
|
|
|
struct dhcpmsg *payload)
|
2011-06-29 09:26:12 +05:30
|
|
|
{
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t ret = -1;
|
2014-04-04 13:42:25 +05:30
|
|
|
int fd = get_udp_unicast_socket();
|
2014-04-06 16:03:14 +05:30
|
|
|
if (fd < 0)
|
2011-06-29 09:26:12 +05:30
|
|
|
goto out;
|
|
|
|
|
|
|
|
struct sockaddr_in raddr = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(DHCP_SERVER_PORT),
|
2011-07-04 04:26:57 +05:30
|
|
|
.sin_addr.s_addr = cs->serverAddr,
|
2011-06-29 09:26:12 +05:30
|
|
|
};
|
2014-04-06 16:03:14 +05:30
|
|
|
if (connect(fd, (struct sockaddr *)&raddr, sizeof(struct sockaddr)) < 0) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_error("%s: (%s) connect failed: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
2011-06-26 02:01:21 +05:30
|
|
|
goto out_fd;
|
2011-06-25 20:16:24 +05:30
|
|
|
}
|
2011-06-29 09:26:12 +05:30
|
|
|
|
2011-07-27 17:19:30 +05:30
|
|
|
// Send packets that are as short as possible.
|
2011-06-29 09:26:12 +05:30
|
|
|
ssize_t endloc = get_end_option_idx(payload);
|
|
|
|
if (endloc < 0) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_error("%s: (%s) No end marker. Not sending.",
|
|
|
|
client_config.interface, __func__);
|
2011-06-26 02:01:21 +05:30
|
|
|
goto out_fd;
|
2011-06-25 20:10:41 +05:30
|
|
|
}
|
2011-06-29 09:26:12 +05:30
|
|
|
size_t payload_len =
|
|
|
|
sizeof *payload - (sizeof payload->options - 1 - endloc);
|
|
|
|
ret = safe_write(fd, (const char *)payload, payload_len);
|
2014-04-04 13:31:49 +05:30
|
|
|
if (ret < 0 || (size_t)ret != payload_len)
|
|
|
|
log_error("%s: (%s) write failed: %d", client_config.interface,
|
|
|
|
__func__, ret);
|
2011-06-29 09:26:12 +05:30
|
|
|
out_fd:
|
2011-06-26 02:01:21 +05:30
|
|
|
close(fd);
|
2011-06-29 09:26:12 +05:30
|
|
|
out:
|
|
|
|
return ret;
|
2011-06-25 20:10:41 +05:30
|
|
|
}
|
2011-06-27 03:07:57 +05:30
|
|
|
|
2011-06-11 20:35:53 +05:30
|
|
|
// Read a packet from a cooked socket. Returns -1 on fatal error, -2 on
|
|
|
|
// transient error.
|
2014-04-06 15:54:13 +05:30
|
|
|
static ssize_t get_cooked_packet(struct dhcpmsg *packet, int fd)
|
2010-11-12 14:32:18 +05:30
|
|
|
{
|
2011-06-27 02:55:00 +05:30
|
|
|
memset(packet, 0, sizeof *packet);
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t bytes = safe_read(fd, (char *)packet, sizeof *packet);
|
|
|
|
if (bytes < 0) {
|
2011-05-30 20:24:05 +05:30
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return -2;
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Read on listen socket failed: %s",
|
|
|
|
client_config.interface, strerror(errno));
|
2011-03-31 09:29:22 +05:30
|
|
|
return -1;
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
|
|
|
return bytes;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
2011-06-27 02:03:07 +05:30
|
|
|
// When summing ones-complement 16-bit values using a 32-bit unsigned
|
|
|
|
// representation, fold the carry bits that have spilled into the upper
|
|
|
|
// 16-bits of the 32-bit unsigned value back into the 16-bit ones-complement
|
|
|
|
// binary value.
|
|
|
|
static inline uint16_t foldcarry(uint32_t v)
|
|
|
|
{
|
|
|
|
v = (v >> 16) + (v & 0xffff);
|
|
|
|
v += v >> 16;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2011-06-26 19:51:02 +05:30
|
|
|
// This function is not suitable for summing buffers that are greater than
|
2011-06-27 03:07:57 +05:30
|
|
|
// 128k bytes in length: failure case will be incorrect checksums via
|
2011-06-26 19:51:02 +05:30
|
|
|
// unsigned overflow, which is a defined operation and is safe. This limit
|
|
|
|
// should not be an issue for IPv4 or IPv6 packet, which are limited to
|
|
|
|
// at most 64k bytes.
|
2011-06-27 02:03:07 +05:30
|
|
|
static uint16_t net_checksum(void *buf, size_t size)
|
2011-06-26 02:25:00 +05:30
|
|
|
{
|
2011-06-27 02:03:07 +05:30
|
|
|
uint32_t sum = 0;
|
2011-06-26 19:51:02 +05:30
|
|
|
int odd = size & 0x01;
|
|
|
|
size_t i;
|
|
|
|
size &= ~((size_t)0x01);
|
|
|
|
size >>= 1;
|
|
|
|
uint8_t *b = buf;
|
|
|
|
for (i = 0; i < size; ++i) {
|
|
|
|
uint16_t hi = b[i*2];
|
|
|
|
uint16_t lo = b[i*2+1];
|
|
|
|
sum += ntohs((lo + (hi << 8)));
|
2011-06-26 02:25:00 +05:30
|
|
|
}
|
2011-06-26 19:51:02 +05:30
|
|
|
if (odd) {
|
|
|
|
uint16_t hi = b[i*2];
|
|
|
|
uint16_t lo = 0;
|
|
|
|
sum += ntohs((lo + (hi << 8)));
|
2011-06-26 02:25:00 +05:30
|
|
|
}
|
2011-06-27 02:03:07 +05:30
|
|
|
return ~foldcarry(sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For two sequences of bytes A and B that return checksums CS(A) and CS(B),
|
|
|
|
// this function will calculate the checksum CS(AB) of the concatenated value
|
|
|
|
// AB given the checksums of the individual parts CS(A) and CS(B).
|
|
|
|
static inline uint16_t net_checksum_add(uint16_t a, uint16_t b)
|
|
|
|
{
|
|
|
|
return ~foldcarry((~a & 0xffff) + (~b & 0xffff));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns 1 if IP checksum is correct, otherwise 0.
|
|
|
|
static int ip_checksum(struct ip_udp_dhcp_packet *packet)
|
|
|
|
{
|
|
|
|
return net_checksum(&packet->ip, sizeof packet->ip) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns 1 if UDP checksum is correct, otherwise 0.
|
|
|
|
static int udp_checksum(struct ip_udp_dhcp_packet *packet)
|
|
|
|
{
|
|
|
|
struct iphdr ph = {
|
|
|
|
.saddr = packet->ip.saddr,
|
|
|
|
.daddr = packet->ip.daddr,
|
|
|
|
.protocol = packet->ip.protocol,
|
|
|
|
.tot_len = packet->udp.len,
|
|
|
|
};
|
|
|
|
uint16_t udpcs = net_checksum(&packet->udp, ntohs(packet->udp.len));
|
|
|
|
uint16_t hdrcs = net_checksum(&ph, sizeof ph);
|
|
|
|
uint16_t cs = net_checksum_add(udpcs, hdrcs);
|
|
|
|
return cs == 0;
|
2011-06-26 02:25:00 +05:30
|
|
|
}
|
|
|
|
|
2011-07-01 13:44:10 +05:30
|
|
|
static int get_raw_packet_validate_bpf(struct ip_udp_dhcp_packet *packet)
|
|
|
|
{
|
|
|
|
if (packet->ip.version != IPVERSION) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: IP version is not IPv4.", client_config.interface);
|
2011-07-01 13:44:10 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (packet->ip.ihl != sizeof packet->ip >> 2) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: IP header length incorrect.",
|
|
|
|
client_config.interface);
|
2011-07-01 13:44:10 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (packet->ip.protocol != IPPROTO_UDP) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: IP header is not UDP: %d",
|
|
|
|
client_config.interface, packet->ip.protocol);
|
2011-07-01 13:44:10 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ntohs(packet->udp.dest) != DHCP_CLIENT_PORT) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: UDP destination port incorrect: %d",
|
|
|
|
client_config.interface, ntohs(packet->udp.dest));
|
2011-07-01 13:44:10 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ntohs(packet->udp.len) !=
|
|
|
|
ntohs(packet->ip.tot_len) - sizeof packet->ip) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: UDP header length incorrect.",
|
|
|
|
client_config.interface);
|
2011-07-01 13:44:10 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-11 20:35:53 +05:30
|
|
|
// Read a packet from a raw socket. Returns -1 on fatal error, -2 on
|
|
|
|
// transient error.
|
2014-04-06 15:54:13 +05:30
|
|
|
static ssize_t get_raw_packet(struct client_state_t *cs,
|
|
|
|
struct dhcpmsg *payload)
|
2011-06-11 20:35:53 +05:30
|
|
|
{
|
|
|
|
struct ip_udp_dhcp_packet packet;
|
2011-06-27 02:55:00 +05:30
|
|
|
memset(&packet, 0, sizeof packet);
|
2011-06-27 02:03:07 +05:30
|
|
|
|
2011-07-01 14:08:38 +05:30
|
|
|
ssize_t inc = safe_read(cs->listenFd, (char *)&packet, sizeof packet);
|
2014-04-06 15:54:13 +05:30
|
|
|
if (inc < 0) {
|
2011-06-11 20:35:53 +05:30
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
return -2;
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: (%s) read error %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
2011-06-11 20:35:53 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2014-04-16 00:53:52 +05:30
|
|
|
size_t iphdrlen = ntohs(packet.ip.tot_len);
|
|
|
|
if ((size_t)inc != iphdrlen) {
|
|
|
|
log_warning("%s: UDP length [%zd] does not match header length field [%zu].",
|
|
|
|
client_config.interface, inc, iphdrlen);
|
2012-07-21 04:18:26 +05:30
|
|
|
return -2;
|
|
|
|
}
|
2011-07-01 13:44:10 +05:30
|
|
|
if (!cs->using_dhcp_bpf && !get_raw_packet_validate_bpf(&packet))
|
2011-06-11 20:35:53 +05:30
|
|
|
return -2;
|
2011-07-01 13:44:10 +05:30
|
|
|
|
2011-06-27 02:03:07 +05:30
|
|
|
if (!ip_checksum(&packet)) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: IP header checksum incorrect.",
|
|
|
|
client_config.interface);
|
2011-06-11 20:35:53 +05:30
|
|
|
return -2;
|
|
|
|
}
|
2011-06-27 02:03:07 +05:30
|
|
|
if (packet.udp.check && !udp_checksum(&packet)) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_error("%s: Packet with bad UDP checksum received. Ignoring.",
|
|
|
|
client_config.interface);
|
2011-06-11 20:35:53 +05:30
|
|
|
return -2;
|
|
|
|
}
|
2014-04-16 00:53:52 +05:30
|
|
|
size_t l = iphdrlen - sizeof packet.ip - sizeof packet.udp;
|
2011-06-27 02:55:00 +05:30
|
|
|
memcpy(payload, &packet.data, l);
|
|
|
|
return l;
|
2011-06-11 20:35:53 +05:30
|
|
|
}
|
|
|
|
|
2011-06-29 09:26:12 +05:30
|
|
|
// Broadcast a DHCP message using a raw socket.
|
2014-04-06 15:54:13 +05:30
|
|
|
static ssize_t send_dhcp_raw(struct dhcpmsg *payload)
|
2011-06-29 09:26:12 +05:30
|
|
|
{
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t ret = -1;
|
2014-04-04 13:42:25 +05:30
|
|
|
int fd = get_raw_broadcast_socket();
|
2014-04-06 16:03:14 +05:30
|
|
|
if (fd < 0)
|
2011-06-29 09:26:12 +05:30
|
|
|
return ret;
|
2010-11-13 01:03:17 +05:30
|
|
|
|
2011-07-27 17:19:30 +05:30
|
|
|
// Send packets that are as short as possible.
|
2011-06-27 22:23:35 +05:30
|
|
|
ssize_t endloc = get_end_option_idx(payload);
|
2011-06-27 02:55:00 +05:30
|
|
|
if (endloc < 0) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_error("%s: (%s) No end marker. Not sending.",
|
|
|
|
client_config.interface, __func__);
|
2011-06-29 09:26:12 +05:30
|
|
|
close(fd);
|
|
|
|
return ret;
|
2011-03-31 01:27:01 +05:30
|
|
|
}
|
2011-06-27 22:23:35 +05:30
|
|
|
size_t padding = sizeof payload->options - 1 - endloc;
|
2011-06-27 02:55:00 +05:30
|
|
|
size_t iud_len = sizeof(struct ip_udp_dhcp_packet) - padding;
|
|
|
|
size_t ud_len = sizeof(struct udp_dhcp_packet) - padding;
|
2011-06-27 03:20:35 +05:30
|
|
|
|
|
|
|
struct iphdr ph = {
|
|
|
|
.saddr = INADDR_ANY,
|
|
|
|
.daddr = INADDR_BROADCAST,
|
|
|
|
.protocol = IPPROTO_UDP,
|
|
|
|
.tot_len = htons(ud_len),
|
|
|
|
};
|
2011-06-27 02:55:00 +05:30
|
|
|
struct ip_udp_dhcp_packet iudmsg = {
|
2011-06-26 19:51:02 +05:30
|
|
|
.ip = {
|
|
|
|
.saddr = INADDR_ANY,
|
|
|
|
.daddr = INADDR_BROADCAST,
|
2011-06-27 03:20:35 +05:30
|
|
|
.protocol = IPPROTO_UDP,
|
|
|
|
.tot_len = htons(iud_len),
|
|
|
|
.ihl = sizeof iudmsg.ip >> 2,
|
|
|
|
.version = IPVERSION,
|
|
|
|
.ttl = IPDEFTTL,
|
2011-06-26 19:51:02 +05:30
|
|
|
},
|
|
|
|
.data = *payload,
|
|
|
|
};
|
2014-04-04 13:42:25 +05:30
|
|
|
iudmsg.udp.source = htons(DHCP_CLIENT_PORT);
|
|
|
|
iudmsg.udp.dest = htons(DHCP_SERVER_PORT);
|
|
|
|
iudmsg.udp.len = htons(ud_len);
|
|
|
|
iudmsg.udp.check = 0;
|
2011-06-27 03:20:35 +05:30
|
|
|
uint16_t udpcs = net_checksum(&iudmsg.udp, ud_len);
|
|
|
|
uint16_t phcs = net_checksum(&ph, sizeof ph);
|
|
|
|
iudmsg.udp.check = net_checksum_add(udpcs, phcs);
|
2011-06-27 02:55:00 +05:30
|
|
|
iudmsg.ip.check = net_checksum(&iudmsg.ip, sizeof iudmsg.ip);
|
|
|
|
|
2014-04-04 13:42:25 +05:30
|
|
|
struct sockaddr_ll da = {
|
|
|
|
.sll_family = AF_PACKET,
|
|
|
|
.sll_protocol = htons(ETH_P_IP),
|
|
|
|
.sll_pkttype = PACKET_BROADCAST,
|
|
|
|
.sll_ifindex = client_config.ifindex,
|
|
|
|
.sll_halen = 6,
|
|
|
|
};
|
|
|
|
memcpy(da.sll_addr, "\xff\xff\xff\xff\xff\xff", 6);
|
2011-06-29 09:26:12 +05:30
|
|
|
ret = safe_sendto(fd, (const char *)&iudmsg, iud_len, 0,
|
|
|
|
(struct sockaddr *)&da, sizeof da);
|
2014-04-07 13:45:02 +05:30
|
|
|
if (ret < 0 || (size_t)ret != iud_len) {
|
|
|
|
if (ret < 0)
|
|
|
|
log_error("%s: (%s) sendto failed: %s", client_config.interface,
|
|
|
|
__func__, strerror(errno));
|
|
|
|
else
|
|
|
|
log_error("%s: (%s) sendto short write: %z < %zu",
|
|
|
|
client_config.interface, __func__, ret, iud_len);
|
|
|
|
}
|
2010-11-13 01:03:17 +05:30
|
|
|
close(fd);
|
2011-06-29 09:26:12 +05:30
|
|
|
return ret;
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
2010-12-24 19:25:59 +05:30
|
|
|
|
2011-06-11 14:33:33 +05:30
|
|
|
// Switch listen socket between raw (if-bound), kernel (ip-bound), and none
|
2011-07-01 07:03:38 +05:30
|
|
|
static void change_listen_mode(struct client_state_t *cs, int new_mode)
|
2010-12-24 20:11:52 +05:30
|
|
|
{
|
|
|
|
cs->listenMode = new_mode;
|
|
|
|
if (cs->listenFd >= 0) {
|
2014-03-13 02:21:10 +05:30
|
|
|
epoll_del(cs->epollFd, cs->listenFd);
|
2010-12-24 20:11:52 +05:30
|
|
|
close(cs->listenFd);
|
|
|
|
cs->listenFd = -1;
|
|
|
|
}
|
2014-04-04 13:42:25 +05:30
|
|
|
switch (cs->listenMode) {
|
|
|
|
default: return;
|
|
|
|
case LM_RAW: cs->listenFd = get_raw_listen_socket(cs); break;
|
|
|
|
case LM_COOKED: cs->listenFd = get_udp_listen_socket(cs); break;
|
|
|
|
}
|
2014-03-31 02:51:27 +05:30
|
|
|
if (cs->listenFd < 0)
|
2014-04-04 13:31:49 +05:30
|
|
|
suicide("%s: FATAL: Couldn't listen on socket: %s",
|
|
|
|
client_config.interface, strerror(errno));
|
2014-03-13 02:21:10 +05:30
|
|
|
epoll_add(cs->epollFd, cs->listenFd);
|
2010-12-24 20:11:52 +05:30
|
|
|
}
|
|
|
|
|
2011-07-01 07:03:38 +05:30
|
|
|
void set_listen_raw(struct client_state_t *cs)
|
|
|
|
{
|
|
|
|
change_listen_mode(cs, LM_RAW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_listen_cooked(struct client_state_t *cs)
|
|
|
|
{
|
|
|
|
change_listen_mode(cs, LM_COOKED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_listen_none(struct client_state_t *cs)
|
|
|
|
{
|
|
|
|
change_listen_mode(cs, LM_NONE);
|
|
|
|
}
|
|
|
|
|
2014-03-11 04:28:53 +05:30
|
|
|
static int validate_dhcp_packet(struct client_state_t *cs, size_t len,
|
2011-07-18 20:17:35 +05:30
|
|
|
struct dhcpmsg *packet, uint8_t *msgtype)
|
2011-07-01 15:03:12 +05:30
|
|
|
{
|
2014-03-18 11:06:14 +05:30
|
|
|
if (len < offsetof(struct dhcpmsg, options)) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: Packet is too short to contain magic cookie. Ignoring.",
|
|
|
|
client_config.interface);
|
2011-07-01 15:03:12 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ntohl(packet->cookie) != DHCP_MAGIC) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: Packet with bad magic number. Ignoring.",
|
|
|
|
client_config.interface);
|
2011-07-01 15:03:12 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (packet->xid != cs->xid) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: Packet XID %lx does not equal our XID %lx. Ignoring.",
|
|
|
|
client_config.interface, packet->xid, cs->xid);
|
2011-07-01 15:03:12 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2011-09-02 08:35:56 +05:30
|
|
|
if (memcmp(packet->chaddr, client_config.arp, sizeof client_config.arp)) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: Packet client MAC %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x does not equal our MAC %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x. Ignoring it.",
|
|
|
|
client_config.interface,
|
|
|
|
packet->chaddr[0], packet->chaddr[1], packet->chaddr[2],
|
|
|
|
packet->chaddr[3], packet->chaddr[4], packet->chaddr[5],
|
|
|
|
client_config.arp[0], client_config.arp[1],
|
|
|
|
client_config.arp[2], client_config.arp[3],
|
|
|
|
client_config.arp[4], client_config.arp[5]);
|
2011-09-02 08:35:56 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2011-07-26 10:34:59 +05:30
|
|
|
*msgtype = get_option_msgtype(packet);
|
|
|
|
if (!*msgtype) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: Packet does not specify a DHCP message type. Ignoring.",
|
|
|
|
client_config.interface);
|
2011-07-01 15:03:12 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2014-03-18 12:43:51 +05:30
|
|
|
char clientid[MAX_DOPT_SIZE];
|
|
|
|
size_t cidlen = get_option_clientid(packet, clientid, MAX_DOPT_SIZE);
|
|
|
|
if (cidlen == 0)
|
|
|
|
return 1;
|
|
|
|
if (memcmp(client_config.clientid, clientid,
|
|
|
|
min_size_t(cidlen, client_config.clientid_len))) {
|
2014-04-04 13:31:49 +05:30
|
|
|
log_warning("%s: Packet clientid does not match our clientid. Ignoring.",
|
|
|
|
client_config.interface);
|
2014-03-18 12:43:51 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2011-07-01 15:03:12 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-12-24 19:25:59 +05:30
|
|
|
void handle_packet(struct client_state_t *cs)
|
|
|
|
{
|
2011-07-18 20:17:35 +05:30
|
|
|
uint8_t msgtype;
|
2011-06-27 02:55:00 +05:30
|
|
|
struct dhcpmsg packet;
|
2010-12-24 19:25:59 +05:30
|
|
|
|
2011-07-01 14:08:38 +05:30
|
|
|
if (cs->listenMode == LM_NONE)
|
2010-12-24 19:25:59 +05:30
|
|
|
return;
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t r = cs->listenMode == LM_RAW ?
|
2011-07-01 14:08:38 +05:30
|
|
|
get_raw_packet(cs, &packet) : get_cooked_packet(&packet, cs->listenFd);
|
2014-03-11 04:28:53 +05:30
|
|
|
if (r < 0) {
|
2011-06-11 13:57:56 +05:30
|
|
|
// Transient issue handled by packet collection functions.
|
2014-03-11 04:28:53 +05:30
|
|
|
if (r == -2 || (r == -1 && errno == EINTR))
|
2011-06-11 13:57:56 +05:30
|
|
|
return;
|
2014-04-04 13:31:49 +05:30
|
|
|
log_error("%s: Error reading from listening socket: %s. Reopening.",
|
|
|
|
client_config.interface, strerror(errno));
|
2011-06-11 13:57:56 +05:30
|
|
|
change_listen_mode(cs, cs->listenMode);
|
2014-03-11 04:28:53 +05:30
|
|
|
return;
|
2010-12-24 19:25:59 +05:30
|
|
|
}
|
2014-03-11 04:28:53 +05:30
|
|
|
size_t len = (size_t)r;
|
2010-12-24 19:25:59 +05:30
|
|
|
|
2011-07-18 20:17:35 +05:30
|
|
|
if (!validate_dhcp_packet(cs, len, &packet, &msgtype))
|
2010-12-24 19:25:59 +05:30
|
|
|
return;
|
2011-07-18 20:17:35 +05:30
|
|
|
packet_action(cs, &packet, msgtype);
|
2010-12-24 19:25:59 +05:30
|
|
|
}
|
2011-06-11 20:49:05 +05:30
|
|
|
|
2011-06-27 22:31:39 +05:30
|
|
|
// Initialize a DHCP client packet that will be sent to a server
|
2011-06-27 21:50:22 +05:30
|
|
|
static struct dhcpmsg init_packet(char type, uint32_t xid)
|
2011-06-11 20:49:05 +05:30
|
|
|
{
|
2011-06-27 21:50:22 +05:30
|
|
|
struct dhcpmsg packet = {
|
|
|
|
.op = 1, // BOOTREQUEST (client)
|
|
|
|
.htype = 1, // ETH_10MB
|
|
|
|
.hlen = 6, // ETH_10MB_LEN
|
|
|
|
.cookie = htonl(DHCP_MAGIC),
|
2011-07-25 12:41:47 +05:30
|
|
|
.options[0] = DCODE_END,
|
2011-06-27 21:50:22 +05:30
|
|
|
.xid = xid,
|
|
|
|
};
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_msgtype(&packet, type);
|
2011-06-27 21:50:22 +05:30
|
|
|
memcpy(packet.chaddr, client_config.arp, 6);
|
2014-03-18 12:43:51 +05:30
|
|
|
add_option_clientid(&packet, client_config.clientid,
|
|
|
|
client_config.clientid_len);
|
2011-06-27 21:50:22 +05:30
|
|
|
return packet;
|
2011-06-11 20:49:05 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t send_discover(struct client_state_t *cs)
|
2011-06-11 20:49:05 +05:30
|
|
|
{
|
2011-07-01 21:07:13 +05:30
|
|
|
struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid);
|
|
|
|
if (cs->clientAddr)
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_reqip(&packet, cs->clientAddr);
|
|
|
|
add_option_maxsize(&packet);
|
2011-06-27 22:23:35 +05:30
|
|
|
add_option_request_list(&packet);
|
2011-07-02 13:18:08 +05:30
|
|
|
add_option_vendor(&packet);
|
2011-07-03 15:33:54 +05:30
|
|
|
add_option_hostname(&packet);
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Discovering DHCP servers...", client_config.interface);
|
2011-06-26 02:25:00 +05:30
|
|
|
return send_dhcp_raw(&packet);
|
2011-06-11 20:49:05 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t send_selecting(struct client_state_t *cs)
|
2011-06-11 20:49:05 +05:30
|
|
|
{
|
2011-07-12 02:32:32 +05:30
|
|
|
char clibuf[INET_ADDRSTRLEN];
|
2011-07-01 21:07:13 +05:30
|
|
|
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_reqip(&packet, cs->clientAddr);
|
|
|
|
add_option_serverid(&packet, cs->serverAddr);
|
|
|
|
add_option_maxsize(&packet);
|
2011-06-27 22:23:35 +05:30
|
|
|
add_option_request_list(&packet);
|
2011-07-02 13:18:08 +05:30
|
|
|
add_option_vendor(&packet);
|
2011-07-03 15:33:54 +05:30
|
|
|
add_option_hostname(&packet);
|
2011-07-12 02:32:32 +05:30
|
|
|
inet_ntop(AF_INET, &(struct in_addr){.s_addr = cs->clientAddr},
|
|
|
|
clibuf, sizeof clibuf);
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Sending a selection request for %s...",
|
|
|
|
client_config.interface, clibuf);
|
2011-06-26 02:25:00 +05:30
|
|
|
return send_dhcp_raw(&packet);
|
2011-06-11 20:49:05 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t send_renew(struct client_state_t *cs)
|
2011-06-11 20:49:05 +05:30
|
|
|
{
|
2011-07-01 21:07:13 +05:30
|
|
|
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
|
|
|
packet.ciaddr = cs->clientAddr;
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_maxsize(&packet);
|
2011-06-27 22:23:35 +05:30
|
|
|
add_option_request_list(&packet);
|
2011-07-02 13:18:08 +05:30
|
|
|
add_option_vendor(&packet);
|
2011-07-03 15:33:54 +05:30
|
|
|
add_option_hostname(&packet);
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Sending a renew request...", client_config.interface);
|
2011-07-04 04:26:57 +05:30
|
|
|
return send_dhcp_cooked(cs, &packet);
|
2011-07-01 21:07:13 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t send_rebind(struct client_state_t *cs)
|
2011-07-01 21:07:13 +05:30
|
|
|
{
|
|
|
|
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
|
|
|
packet.ciaddr = cs->clientAddr;
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_reqip(&packet, cs->clientAddr);
|
|
|
|
add_option_maxsize(&packet);
|
2011-07-01 21:07:13 +05:30
|
|
|
add_option_request_list(&packet);
|
2011-07-02 13:18:08 +05:30
|
|
|
add_option_vendor(&packet);
|
2011-07-03 15:33:54 +05:30
|
|
|
add_option_hostname(&packet);
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Sending a rebind request...", client_config.interface);
|
2011-07-01 21:07:13 +05:30
|
|
|
return send_dhcp_raw(&packet);
|
2011-06-11 20:49:05 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t send_decline(struct client_state_t *cs, uint32_t server)
|
2011-06-11 20:49:05 +05:30
|
|
|
{
|
2011-07-01 21:07:13 +05:30
|
|
|
struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid);
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_reqip(&packet, cs->clientAddr);
|
|
|
|
add_option_serverid(&packet, server);
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Sending a decline message...", client_config.interface);
|
2011-06-26 02:25:00 +05:30
|
|
|
return send_dhcp_raw(&packet);
|
2011-06-11 20:49:05 +05:30
|
|
|
}
|
|
|
|
|
2014-04-06 15:54:13 +05:30
|
|
|
ssize_t send_release(struct client_state_t *cs)
|
2011-06-11 20:49:05 +05:30
|
|
|
{
|
2014-03-13 01:11:34 +05:30
|
|
|
struct dhcpmsg packet = init_packet(DHCPRELEASE,
|
|
|
|
nk_random_u32(&cs->rnd32_state));
|
2011-07-01 21:07:13 +05:30
|
|
|
packet.ciaddr = cs->clientAddr;
|
2011-07-26 09:04:32 +05:30
|
|
|
add_option_reqip(&packet, cs->clientAddr);
|
|
|
|
add_option_serverid(&packet, cs->serverAddr);
|
2014-04-04 13:31:49 +05:30
|
|
|
log_line("%s: Sending a release message...", client_config.interface);
|
2011-07-04 04:26:57 +05:30
|
|
|
return send_dhcp_cooked(cs, &packet);
|
2011-06-11 20:49:05 +05:30
|
|
|
}
|
2011-06-26 02:25:00 +05:30
|
|
|
|