2008-03-24 09:29:33 +08:00
|
|
|
/*
|
|
|
|
* dhcpcd - DHCP client daemon
|
2014-01-14 22:24:31 +08:00
|
|
|
* Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
|
2008-03-24 09:29:33 +08:00
|
|
|
*
|
|
|
|
* 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>
|
2014-01-14 22:24:31 +08:00
|
|
|
#include <sys/time.h>
|
2008-03-24 09:29:33 +08:00
|
|
|
#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>
|
2008-09-06 02:24:34 +08:00
|
|
|
#include <syslog.h>
|
2008-03-24 09:29:33 +08:00
|
|
|
#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"
|
2013-02-04 22:12:48 +08:00
|
|
|
#include "ipv4.h"
|
2008-03-24 09:45:37 +08:00
|
|
|
#include "bpf-filter.h"
|
2008-03-24 09:29:33 +08:00
|
|
|
|
|
|
|
int
|
2013-02-04 22:12:48 +08:00
|
|
|
ipv4_opensocket(struct interface *ifp, int protocol)
|
2008-03-24 09:29:33 +08:00
|
|
|
{
|
2013-02-04 22:12:48 +08:00
|
|
|
struct dhcp_state *state;
|
2008-03-24 09:29:33 +08:00
|
|
|
int fd = -1;
|
|
|
|
struct ifreq ifr;
|
2008-05-15 18:10:39 +08:00
|
|
|
int buf_len = 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
|
2008-05-15 18:10:39 +08:00
|
|
|
fd = open(_PATH_BPF, O_RDWR | O_NONBLOCK);
|
2008-04-17 17:47:11 +08:00
|
|
|
#else
|
|
|
|
char *device;
|
|
|
|
int n = 0;
|
|
|
|
|
2013-02-18 00:12:54 +08:00
|
|
|
device = malloc(sizeof(char) * PATH_MAX);
|
|
|
|
if (device == NULL)
|
|
|
|
return -1;
|
2008-03-24 09:29:33 +08:00
|
|
|
do {
|
2008-04-17 17:47:11 +08:00
|
|
|
snprintf(device, PATH_MAX, "/dev/bpf%d", n++);
|
2008-05-15 18:10:39 +08:00
|
|
|
fd = open(device, O_RDWR | O_NONBLOCK);
|
2008-03-24 09:29:33 +08:00
|
|
|
} 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;
|
|
|
|
|
2013-02-04 22:12:48 +08:00
|
|
|
state = D_STATE(ifp);
|
|
|
|
|
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) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "BPF version mismatch - recompile");
|
2008-04-12 00:14:55 +08:00
|
|
|
goto eexit;
|
|
|
|
}
|
|
|
|
|
2008-03-24 09:29:33 +08:00
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2013-02-04 22:12:48 +08:00
|
|
|
strlcpy(ifr.ifr_name, ifp->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-05-15 18:10:39 +08:00
|
|
|
if (ioctl(fd, BIOCGBLEN, &buf_len) == -1)
|
2008-04-12 00:14:55 +08:00
|
|
|
goto eexit;
|
2013-02-04 22:12:48 +08:00
|
|
|
if (state->buffer_size != (size_t)buf_len) {
|
|
|
|
free(state->buffer);
|
|
|
|
state->buffer = malloc(buf_len);
|
|
|
|
if (state->buffer == NULL)
|
|
|
|
goto eexit;
|
|
|
|
state->buffer_size = buf_len;
|
|
|
|
state->buffer_len = state->buffer_pos = 0;
|
2008-05-15 20:15:06 +08:00
|
|
|
}
|
2008-03-24 09:29:33 +08:00
|
|
|
|
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) {
|
2008-05-23 02:16:22 +08:00
|
|
|
pf.bf_insns = UNCONST(arp_bpf_filter);
|
2008-05-14 20:05:04 +08:00
|
|
|
pf.bf_len = arp_bpf_filter_len;
|
2008-07-01 00:14:56 +08:00
|
|
|
} else {
|
2008-05-23 02:16:22 +08:00
|
|
|
pf.bf_insns = UNCONST(dhcp_bpf_filter);
|
2008-05-14 20:05:04 +08:00
|
|
|
pf.bf_len = dhcp_bpf_filter_len;
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
if (ioctl(fd, BIOCSETF, &pf) == -1)
|
|
|
|
goto eexit;
|
2008-06-11 06:23:57 +08:00
|
|
|
if (set_cloexec(fd) == -1)
|
|
|
|
goto eexit;
|
2008-03-24 09:29:33 +08:00
|
|
|
return fd;
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
eexit:
|
2013-02-04 22:12:48 +08:00
|
|
|
free(state->buffer);
|
|
|
|
state->buffer = NULL;
|
2008-04-12 00:14:55 +08:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2013-02-04 22:12:48 +08:00
|
|
|
ipv4_sendrawpacket(const struct interface *ifp, int protocol,
|
2009-02-12 01:56:22 +08:00
|
|
|
const void *data, ssize_t len)
|
2008-03-24 09:29:33 +08:00
|
|
|
{
|
|
|
|
struct iovec iov[2];
|
|
|
|
struct ether_header hw;
|
2008-07-10 23:32:03 +08:00
|
|
|
int fd;
|
2013-02-04 22:12:48 +08:00
|
|
|
const struct dhcp_state *state;
|
2008-03-24 09:29:33 +08:00
|
|
|
|
2008-07-04 01:07:24 +08:00
|
|
|
memset(&hw, 0, ETHER_HDR_LEN);
|
2008-03-29 16:40:55 +08:00
|
|
|
memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN);
|
2008-06-09 04:07:54 +08:00
|
|
|
hw.ether_type = htons(protocol);
|
2008-03-29 16:40:55 +08:00
|
|
|
iov[0].iov_base = &hw;
|
2008-07-04 01:07:24 +08:00
|
|
|
iov[0].iov_len = ETHER_HDR_LEN;
|
2008-05-14 20:05:04 +08:00
|
|
|
iov[1].iov_base = UNCONST(data);
|
2008-03-24 09:29:33 +08:00
|
|
|
iov[1].iov_len = len;
|
2013-02-04 22:12:48 +08:00
|
|
|
state = D_CSTATE(ifp);
|
2008-07-10 23:32:03 +08:00
|
|
|
if (protocol == ETHERTYPE_ARP)
|
2013-02-04 22:12:48 +08:00
|
|
|
fd = state->arp_fd;
|
2008-07-10 23:32:03 +08:00
|
|
|
else
|
2013-02-04 22:12:48 +08:00
|
|
|
fd = state->raw_fd;
|
2008-07-10 23:32:03 +08:00
|
|
|
return writev(fd, iov, 2);
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BPF requires that we read the entire buffer.
|
2008-06-09 04:07:54 +08:00
|
|
|
* So we pass the buffer in the API so we can loop on >1 packet. */
|
2008-03-24 09:29:33 +08:00
|
|
|
ssize_t
|
2013-02-04 22:12:48 +08:00
|
|
|
ipv4_getrawpacket(struct interface *ifp, int protocol,
|
2011-03-25 01:59:20 +08:00
|
|
|
void *data, ssize_t len, int *partialcsum)
|
2008-03-24 09:29:33 +08:00
|
|
|
{
|
2008-07-01 00:14:56 +08:00
|
|
|
int fd = -1;
|
2008-05-15 18:10:39 +08:00
|
|
|
struct bpf_hdr packet;
|
2008-05-15 19:12:44 +08:00
|
|
|
ssize_t bytes;
|
2008-06-09 04:07:54 +08:00
|
|
|
const unsigned char *payload;
|
2013-02-04 22:12:48 +08:00
|
|
|
struct dhcp_state *state;
|
2008-06-09 04:07:54 +08:00
|
|
|
|
2013-02-04 22:12:48 +08:00
|
|
|
state = D_STATE(ifp);
|
2008-07-24 01:06:42 +08:00
|
|
|
if (protocol == ETHERTYPE_ARP)
|
2013-02-04 22:12:48 +08:00
|
|
|
fd = state->arp_fd;
|
2008-07-24 01:06:42 +08:00
|
|
|
else
|
2013-02-04 22:12:48 +08:00
|
|
|
fd = state->raw_fd;
|
2008-05-15 18:10:39 +08:00
|
|
|
|
2011-03-25 01:59:20 +08:00
|
|
|
if (partialcsum != NULL)
|
|
|
|
*partialcsum = 0; /* Not supported on BSD */
|
|
|
|
|
2008-05-15 18:10:39 +08:00
|
|
|
for (;;) {
|
2013-02-04 22:12:48 +08:00
|
|
|
if (state->buffer_len == 0) {
|
|
|
|
bytes = read(fd, state->buffer, state->buffer_size);
|
2008-05-15 19:12:44 +08:00
|
|
|
if (bytes == -1)
|
2008-05-15 18:10:39 +08:00
|
|
|
return errno == EAGAIN ? 0 : -1;
|
2008-05-15 19:12:44 +08:00
|
|
|
else if ((size_t)bytes < sizeof(packet))
|
2008-05-15 18:10:39 +08:00
|
|
|
return -1;
|
2013-02-04 22:12:48 +08:00
|
|
|
state->buffer_len = bytes;
|
|
|
|
state->buffer_pos = 0;
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|
2008-05-15 19:12:44 +08:00
|
|
|
bytes = -1;
|
2013-02-04 22:12:48 +08:00
|
|
|
memcpy(&packet, state->buffer + state->buffer_pos,
|
2009-02-12 01:56:22 +08:00
|
|
|
sizeof(packet));
|
2008-05-15 18:10:39 +08:00
|
|
|
if (packet.bh_caplen != packet.bh_datalen)
|
|
|
|
goto next; /* Incomplete packet, drop. */
|
2013-02-04 22:12:48 +08:00
|
|
|
if (state->buffer_pos + packet.bh_caplen + packet.bh_hdrlen >
|
|
|
|
state->buffer_len)
|
2008-05-15 18:10:39 +08:00
|
|
|
goto next; /* Packet beyond buffer, drop. */
|
2014-01-21 00:16:54 +08:00
|
|
|
payload = state->buffer + state->buffer_pos +
|
|
|
|
packet.bh_hdrlen + ETHER_HDR_LEN;
|
2008-07-04 01:07:24 +08:00
|
|
|
bytes = packet.bh_caplen - ETHER_HDR_LEN;
|
2008-06-09 04:07:54 +08:00
|
|
|
if (bytes > len)
|
|
|
|
bytes = len;
|
|
|
|
memcpy(data, payload, bytes);
|
2008-05-15 18:10:39 +08:00
|
|
|
next:
|
2013-02-04 22:12:48 +08:00
|
|
|
state->buffer_pos += BPF_WORDALIGN(packet.bh_hdrlen +
|
2009-02-12 01:56:22 +08:00
|
|
|
packet.bh_caplen);
|
2013-02-04 22:12:48 +08:00
|
|
|
if (state->buffer_pos >= state->buffer_len)
|
|
|
|
state->buffer_len = state->buffer_pos = 0;
|
2008-05-15 19:12:44 +08:00
|
|
|
if (bytes != -1)
|
|
|
|
return bytes;
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
2008-03-24 09:29:33 +08:00
|
|
|
}
|