2008-03-24 09:29:33 +08:00
|
|
|
/*
|
|
|
|
* dhcpcd - DHCP client daemon
|
|
|
|
* Copyright 2006-2008 Roy Marples <roy@marples.name>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. 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 AUTHOR 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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <net/bpf.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2008-04-17 17:47:11 +08:00
|
|
|
#include <paths.h>
|
2008-03-24 09:29:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2008-04-12 00:14:55 +08:00
|
|
|
#include "common.h"
|
2008-03-24 09:29:33 +08:00
|
|
|
#include "dhcp.h"
|
2008-04-12 00:14:55 +08:00
|
|
|
#include "logger.h"
|
|
|
|
#include "net.h"
|
2008-03-24 09:45:37 +08:00
|
|
|
#include "bpf-filter.h"
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
open_socket(struct interface *iface, int protocol)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
struct ifreq ifr;
|
|
|
|
int buf = 0;
|
2008-04-12 00:14:55 +08:00
|
|
|
struct bpf_version pv;
|
2008-03-24 09:29:33 +08:00
|
|
|
struct bpf_program pf;
|
2008-05-14 19:53:58 +08:00
|
|
|
#ifdef BIOCIMMEDIATE
|
|
|
|
int flags;
|
|
|
|
#endif
|
2008-04-17 17:47:11 +08:00
|
|
|
#ifdef _PATH_BPF
|
|
|
|
fd = open(_PATH_BPF, O_RDWR);
|
|
|
|
#else
|
|
|
|
char *device;
|
|
|
|
int n = 0;
|
|
|
|
|
2008-03-24 09:29:33 +08:00
|
|
|
device = xmalloc(sizeof(char) * PATH_MAX);
|
|
|
|
do {
|
2008-04-17 17:47:11 +08:00
|
|
|
snprintf(device, PATH_MAX, "/dev/bpf%d", n++);
|
2008-03-24 09:29:33 +08:00
|
|
|
fd = open(device, O_RDWR);
|
|
|
|
} while (fd == -1 && errno == EBUSY);
|
|
|
|
free(device);
|
2008-04-17 17:47:11 +08:00
|
|
|
#endif
|
2008-03-24 09:29:33 +08:00
|
|
|
|
2008-03-29 16:40:55 +08:00
|
|
|
if (fd == -1)
|
2008-03-24 09:29:33 +08:00
|
|
|
return -1;
|
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
if (ioctl(fd, BIOCVERSION, &pv) == -1)
|
|
|
|
goto eexit;
|
|
|
|
if (pv.bv_major != BPF_MAJOR_VERSION ||
|
|
|
|
pv.bv_minor < BPF_MINOR_VERSION) {
|
|
|
|
logger(LOG_ERR, "BPF version mismatch - recompile " PACKAGE);
|
|
|
|
goto eexit;
|
|
|
|
}
|
|
|
|
|
2008-03-24 09:29:33 +08:00
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name));
|
2008-04-12 00:14:55 +08:00
|
|
|
if (ioctl(fd, BIOCSETIF, &ifr) == -1)
|
|
|
|
goto eexit;
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
/* Get the required BPF buffer length from the kernel. */
|
2008-04-12 00:14:55 +08:00
|
|
|
if (ioctl(fd, BIOCGBLEN, &buf) == -1)
|
|
|
|
goto eexit;
|
2008-03-24 09:29:33 +08:00
|
|
|
iface->buffer_length = buf;
|
|
|
|
|
2008-05-14 19:53:58 +08:00
|
|
|
#ifdef BIOCIMMEDIATE
|
2008-03-24 09:29:33 +08:00
|
|
|
flags = 1;
|
2008-04-12 00:14:55 +08:00
|
|
|
if (ioctl(fd, BIOCIMMEDIATE, &flags) == -1)
|
|
|
|
goto eexit;
|
2008-05-14 19:53:58 +08:00
|
|
|
#endif
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
/* Install the DHCP filter */
|
|
|
|
if (protocol == ETHERTYPE_ARP) {
|
|
|
|
pf.bf_insns = arp_bpf_filter;
|
|
|
|
pf.bf_len = sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]);
|
|
|
|
} else {
|
|
|
|
pf.bf_insns = dhcp_bpf_filter;
|
|
|
|
pf.bf_len = sizeof(dhcp_bpf_filter)/sizeof(dhcp_bpf_filter[0]);
|
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
if (ioctl(fd, BIOCSETF, &pf) == -1)
|
|
|
|
goto eexit;
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
if (iface->fd > -1)
|
|
|
|
close(iface->fd);
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
close_on_exec(fd);
|
2008-03-24 09:29:33 +08:00
|
|
|
iface->fd = fd;
|
|
|
|
|
|
|
|
return fd;
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
eexit:
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2008-04-21 03:02:19 +08:00
|
|
|
send_raw_packet(const struct interface *iface, int type,
|
|
|
|
const unsigned char *data, ssize_t len)
|
2008-03-24 09:29:33 +08:00
|
|
|
{
|
|
|
|
struct iovec iov[2];
|
|
|
|
struct ether_header hw;
|
|
|
|
|
2008-03-29 16:40:55 +08:00
|
|
|
memset(&hw, 0, sizeof(hw));
|
|
|
|
memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN);
|
|
|
|
hw.ether_type = htons(type);
|
|
|
|
iov[0].iov_base = &hw;
|
|
|
|
iov[0].iov_len = sizeof(hw);
|
2008-03-24 09:29:33 +08:00
|
|
|
iov[1].iov_base = (unsigned char *)data;
|
|
|
|
iov[1].iov_len = len;
|
|
|
|
|
2008-03-29 16:40:55 +08:00
|
|
|
return writev(iface->fd, iov, 2);
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BPF requires that we read the entire buffer.
|
|
|
|
* So we pass the buffer in the API so we can loop on >1 dhcp packet. */
|
|
|
|
ssize_t
|
|
|
|
get_packet(const struct interface *iface, unsigned char *data,
|
2008-03-31 18:23:59 +08:00
|
|
|
unsigned char *buffer, ssize_t *buffer_len, ssize_t *buffer_pos)
|
2008-03-24 09:29:33 +08:00
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
struct bpf_hdr *packet;
|
|
|
|
} bpf;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char *buffer;
|
|
|
|
struct ether_header *hw;
|
|
|
|
} hdr;
|
|
|
|
struct timespec ts;
|
2008-03-31 18:23:59 +08:00
|
|
|
ssize_t len;
|
2008-03-24 09:29:33 +08:00
|
|
|
unsigned char *payload;
|
2008-04-12 00:14:55 +08:00
|
|
|
const uint8_t *d;
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
bpf.buffer = buffer;
|
|
|
|
|
|
|
|
if (*buffer_pos < 1) {
|
|
|
|
memset(bpf.buffer, 0, iface->buffer_length);
|
|
|
|
*buffer_len = read(iface->fd, bpf.buffer, iface->buffer_length);
|
|
|
|
*buffer_pos = 0;
|
|
|
|
if (*buffer_len < 1) {
|
|
|
|
ts.tv_sec = 3;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
bpf.buffer += *buffer_pos;
|
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
for (; bpf.buffer - buffer < *buffer_len;
|
2008-04-26 15:39:23 +08:00
|
|
|
bpf.buffer += BPF_WORDALIGN(bpf.packet->bh_hdrlen +
|
|
|
|
bpf.packet->bh_caplen),
|
|
|
|
*buffer_pos = bpf.buffer - buffer)
|
2008-04-12 00:14:55 +08:00
|
|
|
{
|
|
|
|
/* Ensure we have the whole packet */
|
|
|
|
if (bpf.packet->bh_caplen != bpf.packet->bh_datalen)
|
|
|
|
continue;
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
hdr.buffer = bpf.buffer + bpf.packet->bh_hdrlen;
|
|
|
|
payload = hdr.buffer + sizeof(*hdr.hw);
|
|
|
|
|
|
|
|
/* If it's an ARP reply, then just send it back */
|
|
|
|
if (hdr.hw->ether_type == htons (ETHERTYPE_ARP)) {
|
|
|
|
len = bpf.packet->bh_caplen - sizeof(*hdr.hw);
|
|
|
|
memcpy(data, payload, len);
|
2008-04-12 00:14:55 +08:00
|
|
|
return len;
|
2008-03-24 09:29:33 +08:00
|
|
|
} else {
|
2008-04-12 00:14:55 +08:00
|
|
|
if (valid_udp_packet(payload) >= 0) {
|
|
|
|
len = get_udp_data(&d, payload);
|
|
|
|
memcpy(data, d, len);
|
|
|
|
return len;
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
/* No valid packets left, so return */
|
|
|
|
*buffer_pos = 0;
|
|
|
|
return -1;
|
|
|
|
}
|