Merge dhcpmsg.[ch] into packet.[ch].
This commit is contained in:
parent
6191a07eb3
commit
5cf572c2fd
@ -1,5 +1,5 @@
|
|||||||
/* arp.c - arp ping checking
|
/* arp.c - arp ping checking
|
||||||
* Time-stamp: <2011-06-02 10:48:31 njk>
|
* Time-stamp: <2011-06-11 11:12:58 njk>
|
||||||
*
|
*
|
||||||
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
|
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
|
||||||
*
|
*
|
||||||
@ -32,7 +32,6 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "arp.h"
|
#include "arp.h"
|
||||||
#include "dhcpmsg.h"
|
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "sys.h"
|
#include "sys.h"
|
||||||
|
281
ndhc/dhcpmsg.c
281
ndhc/dhcpmsg.c
@ -1,281 +0,0 @@
|
|||||||
/* dhcpmsg.c - dhcp packet generation and sending functions
|
|
||||||
* Time-stamp: <2011-06-11 04:47:46 njk>
|
|
||||||
*
|
|
||||||
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
|
||||||
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <features.h>
|
|
||||||
#include <netpacket/packet.h>
|
|
||||||
#include <net/ethernet.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
#include "dhcpmsg.h"
|
|
||||||
#include "packet.h"
|
|
||||||
#include "options.h"
|
|
||||||
#include "config.h"
|
|
||||||
#include "log.h"
|
|
||||||
#include "io.h"
|
|
||||||
|
|
||||||
/* Create a random xid */
|
|
||||||
uint32_t random_xid(void)
|
|
||||||
{
|
|
||||||
static int initialized;
|
|
||||||
if (initialized)
|
|
||||||
return rand();
|
|
||||||
|
|
||||||
uint32_t seed;
|
|
||||||
int fd = open("/dev/urandom", O_RDONLY);
|
|
||||||
if (fd != -1) {
|
|
||||||
int r = safe_read(fd, (char *)&seed, sizeof seed);
|
|
||||||
if (r == -1) {
|
|
||||||
log_warning("Could not read /dev/urandom: %s", strerror(errno));
|
|
||||||
close(fd);
|
|
||||||
seed = time(0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log_warning("Could not open /dev/urandom: %s",
|
|
||||||
strerror(errno));
|
|
||||||
seed = time(0);
|
|
||||||
}
|
|
||||||
srand(seed);
|
|
||||||
initialized = 1;
|
|
||||||
return rand();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initializes dhcp packet header for a -client- packet. */
|
|
||||||
static void init_header(struct dhcpMessage *packet, char type)
|
|
||||||
{
|
|
||||||
memset(packet, 0, DHCP_SIZE);
|
|
||||||
packet->op = 1; // BOOTREQUEST (client)
|
|
||||||
packet->htype = 1; // ETH_10MB
|
|
||||||
packet->hlen = 6; // ETH_10MB_LEN
|
|
||||||
packet->cookie = htonl(DHCP_MAGIC);
|
|
||||||
packet->options[0] = DHCP_END;
|
|
||||||
add_u32_option(packet->options, DHCP_OPTIONS_BUFSIZE, DHCP_MESSAGE_TYPE,
|
|
||||||
type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* initialize a packet with the proper defaults */
|
|
||||||
static void init_packet(struct dhcpMessage *packet, char type)
|
|
||||||
{
|
|
||||||
struct vendor {
|
|
||||||
char vendor;
|
|
||||||
char length;
|
|
||||||
char str[sizeof "ndhc"];
|
|
||||||
} vendor_id = { DHCP_VENDOR, sizeof "ndhc" - 1, "ndhc"};
|
|
||||||
|
|
||||||
init_header(packet, type);
|
|
||||||
memcpy(packet->chaddr, client_config.arp, 6);
|
|
||||||
add_option_string(packet->options, DHCP_OPTIONS_BUFSIZE,
|
|
||||||
client_config.clientid);
|
|
||||||
if (client_config.hostname)
|
|
||||||
add_option_string(packet->options, DHCP_OPTIONS_BUFSIZE,
|
|
||||||
client_config.hostname);
|
|
||||||
add_option_string(packet->options, DHCP_OPTIONS_BUFSIZE,
|
|
||||||
(uint8_t *)&vendor_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define MAC_BCAST_ADDR (uint8_t *)"\xff\xff\xff\xff\xff\xff"
|
|
||||||
/* Wrapper that broadcasts a raw dhcp packet on the bound interface. */
|
|
||||||
static int bcast_raw_packet(struct dhcpMessage *packet)
|
|
||||||
{
|
|
||||||
return raw_packet(packet, INADDR_ANY, DHCP_CLIENT_PORT, INADDR_BROADCAST,
|
|
||||||
DHCP_SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
|
||||||
}
|
|
||||||
#undef MAC_BCAST_ADDR
|
|
||||||
|
|
||||||
/* Broadcast a DHCP discover packet to the network, with an optionally
|
|
||||||
* requested IP */
|
|
||||||
int send_discover(uint32_t xid, uint32_t requested)
|
|
||||||
{
|
|
||||||
struct dhcpMessage packet;
|
|
||||||
|
|
||||||
init_packet(&packet, DHCPDISCOVER);
|
|
||||||
packet.xid = xid;
|
|
||||||
if (requested)
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
|
||||||
requested);
|
|
||||||
|
|
||||||
/* Request a RFC-specified max size to work around buggy servers. */
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE,
|
|
||||||
DHCP_MAX_SIZE, htons(576));
|
|
||||||
add_option_request_list(packet.options, DHCP_OPTIONS_BUFSIZE);
|
|
||||||
log_line("Sending discover...");
|
|
||||||
return bcast_raw_packet(&packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Broadcasts a DHCP request message */
|
|
||||||
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
|
|
||||||
{
|
|
||||||
struct dhcpMessage packet;
|
|
||||||
struct in_addr addr;
|
|
||||||
|
|
||||||
init_packet(&packet, DHCPREQUEST);
|
|
||||||
packet.xid = xid;
|
|
||||||
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
|
||||||
requested);
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_SERVER_ID, server);
|
|
||||||
|
|
||||||
add_option_request_list(packet.options, DHCP_OPTIONS_BUFSIZE);
|
|
||||||
addr.s_addr = requested;
|
|
||||||
log_line("Sending select for %s...", inet_ntoa(addr));
|
|
||||||
return bcast_raw_packet(&packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unicasts or broadcasts a DHCP renew message */
|
|
||||||
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
|
|
||||||
{
|
|
||||||
struct dhcpMessage packet;
|
|
||||||
|
|
||||||
init_packet(&packet, DHCPREQUEST);
|
|
||||||
packet.xid = xid;
|
|
||||||
packet.ciaddr = ciaddr;
|
|
||||||
|
|
||||||
add_option_request_list(packet.options, DHCP_OPTIONS_BUFSIZE);
|
|
||||||
log_line("Sending renew...");
|
|
||||||
if (server)
|
|
||||||
return kernel_packet(&packet, ciaddr, DHCP_CLIENT_PORT, server,
|
|
||||||
DHCP_SERVER_PORT);
|
|
||||||
else
|
|
||||||
return bcast_raw_packet(&packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Broadcast a DHCP decline message */
|
|
||||||
int send_decline(uint32_t xid, uint32_t server, uint32_t requested)
|
|
||||||
{
|
|
||||||
struct dhcpMessage packet;
|
|
||||||
|
|
||||||
/* Fill in: op, htype, hlen, cookie, chaddr, random xid fields,
|
|
||||||
* client-id option (unless -C), message type option:
|
|
||||||
*/
|
|
||||||
init_packet(&packet, DHCPDECLINE);
|
|
||||||
|
|
||||||
/* RFC 2131 says DHCPDECLINE's xid is randomly selected by client,
|
|
||||||
* but in case the server is buggy and wants DHCPDECLINE's xid
|
|
||||||
* to match the xid which started entire handshake,
|
|
||||||
* we use the same xid we used in initial DHCPDISCOVER:
|
|
||||||
*/
|
|
||||||
packet.xid = xid;
|
|
||||||
/* DHCPDECLINE uses "requested ip", not ciaddr, to store offered IP */
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
|
||||||
requested);
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_SERVER_ID, server);
|
|
||||||
|
|
||||||
log_line("Sending decline...");
|
|
||||||
return bcast_raw_packet(&packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unicasts a DHCP release message */
|
|
||||||
int send_release(uint32_t server, uint32_t ciaddr)
|
|
||||||
{
|
|
||||||
struct dhcpMessage packet;
|
|
||||||
|
|
||||||
init_packet(&packet, DHCPRELEASE);
|
|
||||||
packet.xid = random_xid();
|
|
||||||
packet.ciaddr = ciaddr;
|
|
||||||
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
|
||||||
ciaddr);
|
|
||||||
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_SERVER_ID, server);
|
|
||||||
|
|
||||||
log_line("Sending release...");
|
|
||||||
return kernel_packet(&packet, ciaddr, DHCP_CLIENT_PORT, server,
|
|
||||||
DHCP_SERVER_PORT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* return -1 on errors that are fatal for the socket,
|
|
||||||
* -2 for those that aren't */
|
|
||||||
int get_raw_packet(struct dhcpMessage *payload, int fd)
|
|
||||||
{
|
|
||||||
struct ip_udp_dhcp_packet packet;
|
|
||||||
uint16_t check;
|
|
||||||
|
|
||||||
memset(&packet, 0, IP_UPD_DHCP_SIZE);
|
|
||||||
int len = safe_read(fd, (char *)&packet, IP_UPD_DHCP_SIZE);
|
|
||||||
if (len == -1) {
|
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
|
||||||
return -2;
|
|
||||||
log_line("get_raw_packet: read error %s", strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ignore any extra garbage bytes */
|
|
||||||
len = ntohs(packet.ip.tot_len);
|
|
||||||
|
|
||||||
// Validate the IP and UDP headers.
|
|
||||||
if (packet.ip.protocol != IPPROTO_UDP) {
|
|
||||||
log_line("IP header is not UDP: %d", packet.ip.protocol);
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
if (packet.ip.version != IPVERSION) {
|
|
||||||
log_line("IP version is not IPv4");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
if (packet.ip.ihl != sizeof packet.ip >> 2) {
|
|
||||||
log_line("IP header length incorrect");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
check = packet.ip.check;
|
|
||||||
packet.ip.check = 0;
|
|
||||||
if (check != checksum(&packet.ip, sizeof packet.ip)) {
|
|
||||||
log_line("IP header checksum incorrect");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
if (packet.udp.dest != htons(DHCP_CLIENT_PORT)) {
|
|
||||||
log_line("UDP destination port incorrect: %d", ntohs(packet.udp.dest));
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
if (len > IP_UPD_DHCP_SIZE) {
|
|
||||||
log_line("Data longer than that of a IP+UDP+DHCP message: %d", len);
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
if (ntohs(packet.udp.len) != (short)(len - sizeof packet.ip)) {
|
|
||||||
log_line("UDP header length incorrect");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* verify the UDP checksum by replacing the header with a psuedo header */
|
|
||||||
memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
|
|
||||||
/* preserved fields: protocol, check, saddr, daddr */
|
|
||||||
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
|
|
||||||
check = packet.udp.check;
|
|
||||||
packet.udp.check = 0;
|
|
||||||
if (check && check != checksum(&packet, len)) {
|
|
||||||
log_error("Packet with bad UDP checksum received, ignoring");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(payload, &packet.data,
|
|
||||||
len - sizeof packet.ip - sizeof packet.udp);
|
|
||||||
|
|
||||||
log_line("Received a packet via raw socket.");
|
|
||||||
return len - sizeof packet.ip - sizeof packet.udp;
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
/* dhcpmsg.c - dhcp packet generation and sending functions
|
|
||||||
* Time-stamp: <2011-06-11 11:03:22 njk>
|
|
||||||
*
|
|
||||||
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
|
||||||
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef DHCPMSG_H_
|
|
||||||
#define DHCPMSG_H_
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "packet.h"
|
|
||||||
|
|
||||||
#define DHCP_SERVER_PORT 67
|
|
||||||
#define DHCP_CLIENT_PORT 68
|
|
||||||
#define DHCP_MAGIC 0x63825363
|
|
||||||
|
|
||||||
enum {
|
|
||||||
DHCPDISCOVER = 1,
|
|
||||||
DHCPOFFER = 2,
|
|
||||||
DHCPREQUEST = 3,
|
|
||||||
DHCPDECLINE = 4,
|
|
||||||
DHCPACK = 5,
|
|
||||||
DHCPNAK = 6,
|
|
||||||
DHCPRELEASE = 7,
|
|
||||||
DHCPINFORM = 8
|
|
||||||
};
|
|
||||||
|
|
||||||
uint32_t random_xid(void);
|
|
||||||
int send_discover(uint32_t xid, uint32_t requested);
|
|
||||||
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested);
|
|
||||||
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr);
|
|
||||||
int send_decline(uint32_t xid, uint32_t server, uint32_t requested);
|
|
||||||
int send_release(uint32_t server, uint32_t ciaddr);
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,5 +1,5 @@
|
|||||||
/* ndhc.c - DHCP client
|
/* ndhc.c - DHCP client
|
||||||
* Time-stamp: <2011-04-19 16:04:53 njk>
|
* Time-stamp: <2011-06-11 11:13:32 njk>
|
||||||
*
|
*
|
||||||
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
||||||
*
|
*
|
||||||
@ -41,7 +41,6 @@
|
|||||||
#include "ndhc-defines.h"
|
#include "ndhc-defines.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "dhcpmsg.h"
|
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "timeout.h"
|
#include "timeout.h"
|
||||||
#include "sys.h"
|
#include "sys.h"
|
||||||
|
175
ndhc/packet.c
175
ndhc/packet.c
@ -1,5 +1,5 @@
|
|||||||
/* packet.c - send and react to DHCP message packets
|
/* packet.c - send and react to DHCP message packets
|
||||||
* Time-stamp: <2011-06-11 11:03:05 njk>
|
* Time-stamp: <2011-06-11 11:15:09 njk>
|
||||||
*
|
*
|
||||||
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
||||||
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
|
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
|
||||||
@ -23,15 +23,18 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <features.h>
|
#include <features.h>
|
||||||
#include <netpacket/packet.h>
|
#include <netpacket/packet.h>
|
||||||
#include <net/ethernet.h>
|
#include <net/ethernet.h>
|
||||||
|
#include <time.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "dhcpmsg.h"
|
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "arp.h"
|
#include "arp.h"
|
||||||
#include "ifchange.h"
|
#include "ifchange.h"
|
||||||
@ -433,3 +436,171 @@ void handle_packet(struct client_state_t *cs)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Create a random xid */
|
||||||
|
uint32_t random_xid(void)
|
||||||
|
{
|
||||||
|
static int initialized;
|
||||||
|
if (initialized)
|
||||||
|
return rand();
|
||||||
|
|
||||||
|
uint32_t seed;
|
||||||
|
int fd = open("/dev/urandom", O_RDONLY);
|
||||||
|
if (fd != -1) {
|
||||||
|
int r = safe_read(fd, (char *)&seed, sizeof seed);
|
||||||
|
if (r == -1) {
|
||||||
|
log_warning("Could not read /dev/urandom: %s", strerror(errno));
|
||||||
|
close(fd);
|
||||||
|
seed = time(0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log_warning("Could not open /dev/urandom: %s",
|
||||||
|
strerror(errno));
|
||||||
|
seed = time(0);
|
||||||
|
}
|
||||||
|
srand(seed);
|
||||||
|
initialized = 1;
|
||||||
|
return rand();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initializes dhcp packet header for a -client- packet. */
|
||||||
|
static void init_header(struct dhcpMessage *packet, char type)
|
||||||
|
{
|
||||||
|
memset(packet, 0, DHCP_SIZE);
|
||||||
|
packet->op = 1; // BOOTREQUEST (client)
|
||||||
|
packet->htype = 1; // ETH_10MB
|
||||||
|
packet->hlen = 6; // ETH_10MB_LEN
|
||||||
|
packet->cookie = htonl(DHCP_MAGIC);
|
||||||
|
packet->options[0] = DHCP_END;
|
||||||
|
add_u32_option(packet->options, DHCP_OPTIONS_BUFSIZE, DHCP_MESSAGE_TYPE,
|
||||||
|
type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* initialize a packet with the proper defaults */
|
||||||
|
static void init_packet(struct dhcpMessage *packet, char type)
|
||||||
|
{
|
||||||
|
struct vendor {
|
||||||
|
char vendor;
|
||||||
|
char length;
|
||||||
|
char str[sizeof "ndhc"];
|
||||||
|
} vendor_id = { DHCP_VENDOR, sizeof "ndhc" - 1, "ndhc"};
|
||||||
|
|
||||||
|
init_header(packet, type);
|
||||||
|
memcpy(packet->chaddr, client_config.arp, 6);
|
||||||
|
add_option_string(packet->options, DHCP_OPTIONS_BUFSIZE,
|
||||||
|
client_config.clientid);
|
||||||
|
if (client_config.hostname)
|
||||||
|
add_option_string(packet->options, DHCP_OPTIONS_BUFSIZE,
|
||||||
|
client_config.hostname);
|
||||||
|
add_option_string(packet->options, DHCP_OPTIONS_BUFSIZE,
|
||||||
|
(uint8_t *)&vendor_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MAC_BCAST_ADDR (uint8_t *)"\xff\xff\xff\xff\xff\xff"
|
||||||
|
/* Wrapper that broadcasts a raw dhcp packet on the bound interface. */
|
||||||
|
static int bcast_raw_packet(struct dhcpMessage *packet)
|
||||||
|
{
|
||||||
|
return raw_packet(packet, INADDR_ANY, DHCP_CLIENT_PORT, INADDR_BROADCAST,
|
||||||
|
DHCP_SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
|
||||||
|
}
|
||||||
|
#undef MAC_BCAST_ADDR
|
||||||
|
|
||||||
|
/* Broadcast a DHCP discover packet to the network, with an optionally
|
||||||
|
* requested IP */
|
||||||
|
int send_discover(uint32_t xid, uint32_t requested)
|
||||||
|
{
|
||||||
|
struct dhcpMessage packet;
|
||||||
|
|
||||||
|
init_packet(&packet, DHCPDISCOVER);
|
||||||
|
packet.xid = xid;
|
||||||
|
if (requested)
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
||||||
|
requested);
|
||||||
|
|
||||||
|
/* Request a RFC-specified max size to work around buggy servers. */
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE,
|
||||||
|
DHCP_MAX_SIZE, htons(576));
|
||||||
|
add_option_request_list(packet.options, DHCP_OPTIONS_BUFSIZE);
|
||||||
|
log_line("Sending discover...");
|
||||||
|
return bcast_raw_packet(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Broadcasts a DHCP request message */
|
||||||
|
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
|
||||||
|
{
|
||||||
|
struct dhcpMessage packet;
|
||||||
|
struct in_addr addr;
|
||||||
|
|
||||||
|
init_packet(&packet, DHCPREQUEST);
|
||||||
|
packet.xid = xid;
|
||||||
|
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
||||||
|
requested);
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_SERVER_ID, server);
|
||||||
|
|
||||||
|
add_option_request_list(packet.options, DHCP_OPTIONS_BUFSIZE);
|
||||||
|
addr.s_addr = requested;
|
||||||
|
log_line("Sending select for %s...", inet_ntoa(addr));
|
||||||
|
return bcast_raw_packet(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Unicasts or broadcasts a DHCP renew message */
|
||||||
|
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
|
||||||
|
{
|
||||||
|
struct dhcpMessage packet;
|
||||||
|
|
||||||
|
init_packet(&packet, DHCPREQUEST);
|
||||||
|
packet.xid = xid;
|
||||||
|
packet.ciaddr = ciaddr;
|
||||||
|
|
||||||
|
add_option_request_list(packet.options, DHCP_OPTIONS_BUFSIZE);
|
||||||
|
log_line("Sending renew...");
|
||||||
|
if (server)
|
||||||
|
return kernel_packet(&packet, ciaddr, DHCP_CLIENT_PORT, server,
|
||||||
|
DHCP_SERVER_PORT);
|
||||||
|
else
|
||||||
|
return bcast_raw_packet(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Broadcast a DHCP decline message */
|
||||||
|
int send_decline(uint32_t xid, uint32_t server, uint32_t requested)
|
||||||
|
{
|
||||||
|
struct dhcpMessage packet;
|
||||||
|
|
||||||
|
/* Fill in: op, htype, hlen, cookie, chaddr, random xid fields,
|
||||||
|
* client-id option (unless -C), message type option:
|
||||||
|
*/
|
||||||
|
init_packet(&packet, DHCPDECLINE);
|
||||||
|
|
||||||
|
/* RFC 2131 says DHCPDECLINE's xid is randomly selected by client,
|
||||||
|
* but in case the server is buggy and wants DHCPDECLINE's xid
|
||||||
|
* to match the xid which started entire handshake,
|
||||||
|
* we use the same xid we used in initial DHCPDISCOVER:
|
||||||
|
*/
|
||||||
|
packet.xid = xid;
|
||||||
|
/* DHCPDECLINE uses "requested ip", not ciaddr, to store offered IP */
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
||||||
|
requested);
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_SERVER_ID, server);
|
||||||
|
|
||||||
|
log_line("Sending decline...");
|
||||||
|
return bcast_raw_packet(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Unicasts a DHCP release message */
|
||||||
|
int send_release(uint32_t server, uint32_t ciaddr)
|
||||||
|
{
|
||||||
|
struct dhcpMessage packet;
|
||||||
|
|
||||||
|
init_packet(&packet, DHCPRELEASE);
|
||||||
|
packet.xid = random_xid();
|
||||||
|
packet.ciaddr = ciaddr;
|
||||||
|
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_REQUESTED_IP,
|
||||||
|
ciaddr);
|
||||||
|
add_u32_option(packet.options, DHCP_OPTIONS_BUFSIZE, DHCP_SERVER_ID, server);
|
||||||
|
|
||||||
|
log_line("Sending release...");
|
||||||
|
return kernel_packet(&packet, ciaddr, DHCP_CLIENT_PORT, server,
|
||||||
|
DHCP_SERVER_PORT);
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/* packet.h - send and react to DHCP message packets
|
/* packet.h - send and react to DHCP message packets
|
||||||
* Time-stamp: <2011-06-11 11:03:14 njk>
|
* Time-stamp: <2011-06-11 11:12:26 njk>
|
||||||
*
|
*
|
||||||
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
||||||
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
|
* (c) 2001 Russ Dill <Russ.Dill@asu.edu>
|
||||||
@ -27,6 +27,21 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
#define DHCP_SERVER_PORT 67
|
||||||
|
#define DHCP_CLIENT_PORT 68
|
||||||
|
#define DHCP_MAGIC 0x63825363
|
||||||
|
|
||||||
|
enum {
|
||||||
|
DHCPDISCOVER = 1,
|
||||||
|
DHCPOFFER = 2,
|
||||||
|
DHCPREQUEST = 3,
|
||||||
|
DHCPDECLINE = 4,
|
||||||
|
DHCPACK = 5,
|
||||||
|
DHCPNAK = 6,
|
||||||
|
DHCPRELEASE = 7,
|
||||||
|
DHCPINFORM = 8
|
||||||
|
};
|
||||||
|
|
||||||
struct dhcpMessage {
|
struct dhcpMessage {
|
||||||
uint8_t op; // Message type: 1 = BOOTREQUEST for clients.
|
uint8_t op; // Message type: 1 = BOOTREQUEST for clients.
|
||||||
uint8_t htype; // ARP HW address type: always '1' for 10MB ethernet.
|
uint8_t htype; // ARP HW address type: always '1' for 10MB ethernet.
|
||||||
@ -74,4 +89,11 @@ int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip,
|
|||||||
int source_port, uint32_t dest_ip, int dest_port);
|
int source_port, uint32_t dest_ip, int dest_port);
|
||||||
void change_listen_mode(struct client_state_t *cs, int new_mode);
|
void change_listen_mode(struct client_state_t *cs, int new_mode);
|
||||||
void handle_packet(struct client_state_t *cs);
|
void handle_packet(struct client_state_t *cs);
|
||||||
|
uint32_t random_xid(void);
|
||||||
|
int send_discover(uint32_t xid, uint32_t requested);
|
||||||
|
int send_selecting(uint32_t xid, uint32_t server, uint32_t requested);
|
||||||
|
int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr);
|
||||||
|
int send_decline(uint32_t xid, uint32_t server, uint32_t requested);
|
||||||
|
int send_release(uint32_t server, uint32_t ciaddr);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/* timeout.c - callbacks to react to event timeouts
|
/* timeout.c - callbacks to react to event timeouts
|
||||||
* Time-stamp: <2011-03-31 15:18:41 nk>
|
* Time-stamp: <2011-06-11 11:13:22 njk>
|
||||||
*
|
*
|
||||||
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
||||||
*
|
*
|
||||||
@ -25,7 +25,6 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "ifchange.h"
|
#include "ifchange.h"
|
||||||
#include "packet.h"
|
#include "packet.h"
|
||||||
#include "dhcpmsg.h"
|
|
||||||
#include "arp.h"
|
#include "arp.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user