2
* dhcpcd - DHCP client daemon
3
* Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
#include <sys/types.h>
28
#include <sys/ioctl.h>
29
#include <sys/socket.h>
34
#include <arpa/inet.h>
49
#include "bpf-filter.h"
52
ipv4_opensocket(struct interface *ifp, int protocol)
54
struct dhcp_state *state;
59
struct bpf_version pv;
60
struct bpf_program pf;
65
fd = open(_PATH_BPF, O_RDWR | O_NONBLOCK);
70
device = malloc(sizeof(char) * PATH_MAX);
74
snprintf(device, PATH_MAX, "/dev/bpf%d", n++);
75
fd = open(device, O_RDWR | O_NONBLOCK);
76
} while (fd == -1 && errno == EBUSY);
85
if (ioctl(fd, BIOCVERSION, &pv) == -1)
87
if (pv.bv_major != BPF_MAJOR_VERSION ||
88
pv.bv_minor < BPF_MINOR_VERSION) {
89
syslog(LOG_ERR, "BPF version mismatch - recompile");
93
memset(&ifr, 0, sizeof(ifr));
94
strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
95
if (ioctl(fd, BIOCSETIF, &ifr) == -1)
98
/* Get the required BPF buffer length from the kernel. */
99
if (ioctl(fd, BIOCGBLEN, &buf_len) == -1)
101
if (state->buffer_size != (size_t)buf_len) {
103
state->buffer = malloc(buf_len);
104
if (state->buffer == NULL)
106
state->buffer_size = buf_len;
107
state->buffer_len = state->buffer_pos = 0;
112
if (ioctl(fd, BIOCIMMEDIATE, &flags) == -1)
116
/* Install the DHCP filter */
117
if (protocol == ETHERTYPE_ARP) {
118
pf.bf_insns = UNCONST(arp_bpf_filter);
119
pf.bf_len = arp_bpf_filter_len;
120
fdp = &state->arp_fd;
122
pf.bf_insns = UNCONST(dhcp_bpf_filter);
123
pf.bf_len = dhcp_bpf_filter_len;
124
fdp = &state->raw_fd;
126
if (ioctl(fd, BIOCSETF, &pf) == -1)
128
if (set_cloexec(fd) == -1)
139
state->buffer = NULL;
145
ipv4_sendrawpacket(const struct interface *ifp, int protocol,
146
const void *data, ssize_t len)
149
struct ether_header hw;
151
const struct dhcp_state *state;
153
memset(&hw, 0, ETHER_HDR_LEN);
154
memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN);
155
hw.ether_type = htons(protocol);
156
iov[0].iov_base = &hw;
157
iov[0].iov_len = ETHER_HDR_LEN;
158
iov[1].iov_base = UNCONST(data);
159
iov[1].iov_len = len;
160
state = D_CSTATE(ifp);
161
if (protocol == ETHERTYPE_ARP)
165
return writev(fd, iov, 2);
168
/* BPF requires that we read the entire buffer.
169
* So we pass the buffer in the API so we can loop on >1 packet. */
171
ipv4_getrawpacket(struct interface *ifp, int protocol,
172
void *data, ssize_t len, int *partialcsum)
175
struct bpf_hdr packet;
177
const unsigned char *payload;
178
struct dhcp_state *state;
180
state = D_STATE(ifp);
181
if (protocol == ETHERTYPE_ARP)
186
if (partialcsum != NULL)
187
*partialcsum = 0; /* Not supported on BSD */
190
if (state->buffer_len == 0) {
191
bytes = read(fd, state->buffer, state->buffer_size);
193
return errno == EAGAIN ? 0 : -1;
194
else if ((size_t)bytes < sizeof(packet))
196
state->buffer_len = bytes;
197
state->buffer_pos = 0;
200
memcpy(&packet, state->buffer + state->buffer_pos,
202
if (packet.bh_caplen != packet.bh_datalen)
203
goto next; /* Incomplete packet, drop. */
204
if (state->buffer_pos + packet.bh_caplen + packet.bh_hdrlen >
206
goto next; /* Packet beyond buffer, drop. */
207
payload = state->buffer + packet.bh_hdrlen + ETHER_HDR_LEN;
208
bytes = packet.bh_caplen - ETHER_HDR_LEN;
211
memcpy(data, payload, bytes);
213
state->buffer_pos += BPF_WORDALIGN(packet.bh_hdrlen +
215
if (state->buffer_pos >= state->buffer_len)
216
state->buffer_len = state->buffer_pos = 0;