2008-09-02 21:28:11 +08:00
|
|
|
/*
|
|
|
|
* dhcpcd - DHCP client daemon
|
2011-03-25 01:59:20 +08:00
|
|
|
* Copyright (c) 2006-2011 Roy Marples <roy@marples.name>
|
2008-09-02 21:28:11 +08:00
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* 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 <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
2009-07-14 21:59:30 +08:00
|
|
|
#include <string.h>
|
2008-09-06 02:24:34 +08:00
|
|
|
#include <syslog.h>
|
2008-09-02 21:28:11 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "arp.h"
|
|
|
|
#include "bind.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "dhcpcd.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "if-options.h"
|
|
|
|
#include "ipv4ll.h"
|
|
|
|
#include "net.h"
|
|
|
|
|
2009-03-11 01:28:18 +08:00
|
|
|
#define ARP_LEN \
|
2008-09-04 16:31:00 +08:00
|
|
|
(sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + (2 * HWADDR_LEN))
|
|
|
|
|
|
|
|
static int
|
|
|
|
send_arp(const struct interface *iface, int op, in_addr_t sip, in_addr_t tip)
|
|
|
|
{
|
2008-09-05 21:12:35 +08:00
|
|
|
uint8_t arp_buffer[ARP_LEN];
|
2008-09-04 16:31:00 +08:00
|
|
|
struct arphdr ar;
|
|
|
|
size_t len;
|
|
|
|
uint8_t *p;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
ar.ar_hrd = htons(iface->family);
|
|
|
|
ar.ar_pro = htons(ETHERTYPE_IP);
|
|
|
|
ar.ar_hln = iface->hwlen;
|
|
|
|
ar.ar_pln = sizeof(sip);
|
|
|
|
ar.ar_op = htons(op);
|
2008-09-10 04:52:21 +08:00
|
|
|
memcpy(arp_buffer, &ar, sizeof(ar));
|
|
|
|
p = arp_buffer + sizeof(ar);
|
2008-09-04 16:31:00 +08:00
|
|
|
memcpy(p, iface->hwaddr, iface->hwlen);
|
|
|
|
p += iface->hwlen;
|
|
|
|
memcpy(p, &sip, sizeof(sip));
|
|
|
|
p += sizeof(sip);
|
|
|
|
/* ARP requests should ignore this */
|
|
|
|
retval = iface->hwlen;
|
|
|
|
while (retval--)
|
|
|
|
*p++ = '\0';
|
|
|
|
memcpy(p, &tip, sizeof(tip));
|
|
|
|
p += sizeof(tip);
|
2008-09-10 04:52:21 +08:00
|
|
|
len = p - arp_buffer;
|
2008-09-04 16:31:00 +08:00
|
|
|
retval = send_raw_packet(iface, ETHERTYPE_ARP, arp_buffer, len);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
|
|
|
handle_arp_failure(struct interface *iface)
|
|
|
|
{
|
2010-05-13 00:20:55 +08:00
|
|
|
|
|
|
|
/* If we failed without a magic cookie then we need to try
|
|
|
|
* and defend our IPv4LL address. */
|
|
|
|
if ((iface->state->offer != NULL &&
|
|
|
|
iface->state->offer->cookie != htonl(MAGIC_COOKIE)) ||
|
|
|
|
(iface->state->new != NULL &&
|
|
|
|
iface->state->new->cookie != htonl(MAGIC_COOKIE)))
|
|
|
|
{
|
2008-09-02 21:28:11 +08:00
|
|
|
handle_ipv4ll_failure(iface);
|
|
|
|
return;
|
|
|
|
}
|
2010-05-13 00:20:55 +08:00
|
|
|
|
2009-10-10 03:23:50 +08:00
|
|
|
unlink(iface->leasefile);
|
|
|
|
if (!iface->state->lease.frominfo)
|
2008-09-15 23:20:09 +08:00
|
|
|
send_decline(iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
close_sockets(iface);
|
2008-10-21 23:34:52 +08:00
|
|
|
delete_timeout(NULL, iface);
|
|
|
|
if (iface->state->lease.frominfo)
|
|
|
|
start_interface(iface);
|
|
|
|
else
|
|
|
|
add_timeout_sec(DHCP_ARP_FAIL, start_interface, iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
static void
|
|
|
|
handle_arp_packet(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-09-05 21:12:35 +08:00
|
|
|
uint8_t arp_buffer[ARP_LEN];
|
2008-09-04 16:31:00 +08:00
|
|
|
struct arphdr ar;
|
2008-09-02 21:28:11 +08:00
|
|
|
uint32_t reply_s;
|
|
|
|
uint32_t reply_t;
|
|
|
|
uint8_t *hw_s, *hw_t;
|
|
|
|
ssize_t bytes;
|
|
|
|
struct if_state *state = iface->state;
|
2009-03-31 16:35:38 +08:00
|
|
|
struct if_options *opts = state->options;
|
|
|
|
const char *hwaddr;
|
|
|
|
struct in_addr ina;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
|
|
|
state->fail.s_addr = 0;
|
|
|
|
for(;;) {
|
|
|
|
bytes = get_raw_packet(iface, ETHERTYPE_ARP,
|
2011-03-25 01:59:20 +08:00
|
|
|
arp_buffer, sizeof(arp_buffer), NULL);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (bytes == 0 || bytes == -1)
|
|
|
|
return;
|
|
|
|
/* We must have a full ARP header */
|
2008-09-04 16:31:00 +08:00
|
|
|
if ((size_t)bytes < sizeof(ar))
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2008-09-04 16:31:00 +08:00
|
|
|
memcpy(&ar, arp_buffer, sizeof(ar));
|
2008-09-02 21:28:11 +08:00
|
|
|
/* Protocol must be IP. */
|
2008-09-04 16:31:00 +08:00
|
|
|
if (ar.ar_pro != htons(ETHERTYPE_IP))
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2008-09-04 16:31:00 +08:00
|
|
|
if (ar.ar_pln != sizeof(reply_s))
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
|
|
|
/* Only these types are recognised */
|
2008-09-04 16:31:00 +08:00
|
|
|
if (ar.ar_op != htons(ARPOP_REPLY) &&
|
|
|
|
ar.ar_op != htons(ARPOP_REQUEST))
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get pointers to the hardware addreses */
|
2008-09-04 16:31:00 +08:00
|
|
|
hw_s = arp_buffer + sizeof(ar);
|
|
|
|
hw_t = hw_s + ar.ar_hln + ar.ar_pln;
|
2008-09-02 21:28:11 +08:00
|
|
|
/* Ensure we got all the data */
|
2008-09-04 16:31:00 +08:00
|
|
|
if ((hw_t + ar.ar_hln + ar.ar_pln) - arp_buffer > bytes)
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
|
|
|
/* Ignore messages from ourself */
|
2008-09-04 16:31:00 +08:00
|
|
|
if (ar.ar_hln == iface->hwlen &&
|
2008-09-02 21:28:11 +08:00
|
|
|
memcmp(hw_s, iface->hwaddr, iface->hwlen) == 0)
|
|
|
|
continue;
|
|
|
|
/* Copy out the IP addresses */
|
2008-09-04 16:31:00 +08:00
|
|
|
memcpy(&reply_s, hw_s + ar.ar_hln, ar.ar_pln);
|
|
|
|
memcpy(&reply_t, hw_t + ar.ar_hln, ar.ar_pln);
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2009-03-31 16:35:38 +08:00
|
|
|
/* Check for arping */
|
|
|
|
if (state->arping_index &&
|
|
|
|
state->arping_index <= opts->arping_len &&
|
|
|
|
(reply_s == opts->arping[state->arping_index - 1] ||
|
|
|
|
(reply_s == 0 &&
|
|
|
|
reply_t == opts->arping[state->arping_index - 1])))
|
|
|
|
{
|
|
|
|
ina.s_addr = reply_s;
|
|
|
|
hwaddr = hwaddr_ntoa((unsigned char *)hw_s,
|
|
|
|
(size_t)ar.ar_hln);
|
|
|
|
syslog(LOG_INFO,
|
|
|
|
"%s: found %s on hardware address %s",
|
|
|
|
iface->name, inet_ntoa(ina), hwaddr);
|
|
|
|
if (select_profile(iface, hwaddr) == -1 &&
|
|
|
|
errno == ENOENT)
|
|
|
|
select_profile(iface, inet_ntoa(ina));
|
|
|
|
close_sockets(iface);
|
|
|
|
delete_timeout(NULL, iface);
|
|
|
|
start_interface(iface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
/* Check for conflict */
|
2009-03-11 01:28:18 +08:00
|
|
|
if (state->offer &&
|
2008-09-02 21:28:11 +08:00
|
|
|
(reply_s == state->offer->yiaddr ||
|
2009-02-12 01:56:22 +08:00
|
|
|
(reply_s == 0 && reply_t == state->offer->yiaddr)))
|
2008-09-02 21:28:11 +08:00
|
|
|
state->fail.s_addr = state->offer->yiaddr;
|
|
|
|
|
|
|
|
/* Handle IPv4LL conflicts */
|
|
|
|
if (IN_LINKLOCAL(htonl(iface->addr.s_addr)) &&
|
|
|
|
(reply_s == iface->addr.s_addr ||
|
2009-02-12 01:56:22 +08:00
|
|
|
(reply_s == 0 && reply_t == iface->addr.s_addr)))
|
2008-09-02 21:28:11 +08:00
|
|
|
state->fail.s_addr = iface->addr.s_addr;
|
|
|
|
|
|
|
|
if (state->fail.s_addr) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "%s: hardware address %s claims %s",
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->name,
|
|
|
|
hwaddr_ntoa((unsigned char *)hw_s,
|
|
|
|
(size_t)ar.ar_hln),
|
|
|
|
inet_ntoa(state->fail));
|
2008-09-02 21:28:11 +08:00
|
|
|
errno = EEXIST;
|
|
|
|
handle_arp_failure(iface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-04 00:49:28 +08:00
|
|
|
send_arp_announce(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-09-02 21:28:11 +08:00
|
|
|
struct if_state *state = iface->state;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2011-12-12 05:14:49 +08:00
|
|
|
if (state->new == NULL)
|
|
|
|
return;
|
2008-09-02 21:28:11 +08:00
|
|
|
if (iface->arp_fd == -1) {
|
|
|
|
open_socket(iface, ETHERTYPE_ARP);
|
|
|
|
add_event(iface->arp_fd, handle_arp_packet, iface);
|
|
|
|
}
|
|
|
|
if (++state->claims < ANNOUNCE_NUM)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG,
|
2009-02-12 01:56:22 +08:00
|
|
|
"%s: sending ARP announce (%d of %d), "
|
|
|
|
"next in %d.00 seconds",
|
|
|
|
iface->name, state->claims, ANNOUNCE_NUM, ANNOUNCE_WAIT);
|
2008-09-02 21:28:11 +08:00
|
|
|
else
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG,
|
2009-02-12 01:56:22 +08:00
|
|
|
"%s: sending ARP announce (%d of %d)",
|
|
|
|
iface->name, state->claims, ANNOUNCE_NUM);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (send_arp(iface, ARPOP_REQUEST,
|
2009-02-12 01:56:22 +08:00
|
|
|
state->new->yiaddr, state->new->yiaddr) == -1)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "send_arp: %m");
|
2008-09-02 21:28:11 +08:00
|
|
|
if (state->claims < ANNOUNCE_NUM) {
|
|
|
|
add_timeout_sec(ANNOUNCE_WAIT, send_arp_announce, iface);
|
|
|
|
return;
|
|
|
|
}
|
2010-02-18 06:23:17 +08:00
|
|
|
if (state->new->cookie != htonl(MAGIC_COOKIE)) {
|
2008-09-02 21:28:11 +08:00
|
|
|
/* We should pretend to be at the end
|
2009-03-04 07:41:53 +08:00
|
|
|
* of the DHCP negotation cycle unless we rebooted */
|
|
|
|
if (state->interval != 0)
|
|
|
|
state->interval = 64;
|
2008-09-02 21:28:11 +08:00
|
|
|
state->probes = 0;
|
|
|
|
state->claims = 0;
|
|
|
|
tv.tv_sec = state->interval - DHCP_RAND_MIN;
|
|
|
|
tv.tv_usec = arc4random() % (DHCP_RAND_MAX_U - DHCP_RAND_MIN_U);
|
|
|
|
timernorm(&tv);
|
|
|
|
add_timeout_tv(&tv, start_discover, iface);
|
|
|
|
} else {
|
|
|
|
delete_event(iface->arp_fd);
|
|
|
|
close(iface->arp_fd);
|
|
|
|
iface->arp_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-04 00:49:28 +08:00
|
|
|
send_arp_probe(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-09-02 21:28:11 +08:00
|
|
|
struct if_state *state = iface->state;
|
|
|
|
struct in_addr addr;
|
|
|
|
struct timeval tv;
|
2009-03-31 16:35:38 +08:00
|
|
|
int arping = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2009-04-15 17:01:02 +08:00
|
|
|
if (state->arping_index < state->options->arping_len) {
|
|
|
|
addr.s_addr = state->options->arping[state->arping_index];
|
2009-03-31 16:35:38 +08:00
|
|
|
arping = 1;
|
|
|
|
} else if (state->offer) {
|
2008-11-19 23:48:18 +08:00
|
|
|
if (state->offer->yiaddr)
|
|
|
|
addr.s_addr = state->offer->yiaddr;
|
|
|
|
else
|
|
|
|
addr.s_addr = state->offer->ciaddr;
|
|
|
|
} else
|
2008-11-19 17:48:11 +08:00
|
|
|
addr.s_addr = iface->addr.s_addr;
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
if (iface->arp_fd == -1) {
|
|
|
|
open_socket(iface, ETHERTYPE_ARP);
|
|
|
|
add_event(iface->arp_fd, handle_arp_packet, iface);
|
|
|
|
}
|
|
|
|
if (state->probes == 0) {
|
2009-03-31 16:35:38 +08:00
|
|
|
if (arping)
|
|
|
|
syslog(LOG_INFO, "%s: searching for %s",
|
|
|
|
iface->name, inet_ntoa(addr));
|
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "%s: checking for %s",
|
|
|
|
iface->name, inet_ntoa(addr));
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
if (++state->probes < PROBE_NUM) {
|
|
|
|
tv.tv_sec = PROBE_MIN;
|
|
|
|
tv.tv_usec = arc4random() % (PROBE_MAX_U - PROBE_MIN_U);
|
|
|
|
timernorm(&tv);
|
|
|
|
add_timeout_tv(&tv, send_arp_probe, iface);
|
|
|
|
} else {
|
|
|
|
tv.tv_sec = ANNOUNCE_WAIT;
|
|
|
|
tv.tv_usec = 0;
|
2009-03-31 16:35:38 +08:00
|
|
|
if (arping) {
|
|
|
|
state->probes = 0;
|
2009-04-15 17:01:02 +08:00
|
|
|
if (++state->arping_index < state->options->arping_len)
|
2009-03-31 16:35:38 +08:00
|
|
|
add_timeout_tv(&tv, send_arp_probe, iface);
|
|
|
|
else
|
|
|
|
add_timeout_tv(&tv, start_interface, iface);
|
|
|
|
} else
|
|
|
|
add_timeout_tv(&tv, bind_interface, iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG,
|
2009-02-12 01:56:22 +08:00
|
|
|
"%s: sending ARP probe (%d of %d), next in %0.2f seconds",
|
2009-04-15 17:01:02 +08:00
|
|
|
iface->name, state->probes ? state->probes : PROBE_NUM, PROBE_NUM,
|
|
|
|
timeval_to_double(&tv));
|
2008-11-19 17:48:11 +08:00
|
|
|
if (send_arp(iface, ARPOP_REQUEST, 0, addr.s_addr) == -1)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "send_arp: %m");
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2009-03-31 16:35:38 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
start_arping(struct interface *iface)
|
|
|
|
{
|
|
|
|
iface->state->probes = 0;
|
|
|
|
iface->state->arping_index = 0;
|
|
|
|
send_arp_probe(iface);
|
|
|
|
}
|