2006-11-28 04:23:22 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2008-01-08 17:51:23 +08:00
|
|
|
* Copyright 2006-2008 Roy Marples <roy@marples.name>
|
2008-01-16 23:59:14 +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.
|
2006-11-28 04:23:22 +08:00
|
|
|
*/
|
|
|
|
|
2006-12-15 07:17:27 +08:00
|
|
|
#include <sys/types.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
2008-03-21 00:47:51 +08:00
|
|
|
|
2007-04-27 00:16:56 +08:00
|
|
|
#include <net/if.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <netinet/in_systm.h>
|
2007-04-05 23:01:50 +08:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2008-03-21 00:47:51 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <asm/types.h> /* needed for 2.4 kernels for the below header */
|
|
|
|
# include <linux/filter.h>
|
|
|
|
# include <netpacket/packet.h>
|
|
|
|
# define bpf_insn sock_filter
|
2008-05-22 21:59:33 +08:00
|
|
|
# define BPF_SKIPTYPE
|
|
|
|
# define BPF_ETHCOOK -ETH_HLEN
|
2008-06-20 23:37:15 +08:00
|
|
|
# define BPF_WHOLEPACKET 0x0fffffff /* work around buggy LPF filters */
|
2008-04-12 00:14:55 +08:00
|
|
|
#endif
|
|
|
|
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <errno.h>
|
2008-02-11 07:30:08 +08:00
|
|
|
#include <fcntl.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <stdio.h>
|
2007-10-11 21:26:16 +08:00
|
|
|
#include <stdlib.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <string.h>
|
2008-02-11 07:01:44 +08:00
|
|
|
#include <time.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-01-27 19:31:01 +08:00
|
|
|
#include "config.h"
|
2008-04-12 00:14:55 +08:00
|
|
|
#include "common.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "dhcp.h"
|
2008-04-12 00:14:55 +08:00
|
|
|
#include "net.h"
|
2008-03-24 09:29:33 +08:00
|
|
|
#include "bpf-filter.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-01-24 23:01:19 +08:00
|
|
|
/* Broadcast address for IPoIB */
|
2008-01-25 00:49:17 +08:00
|
|
|
static const uint8_t ipv4_bcast_addr[] = {
|
2008-01-24 23:01:19 +08:00
|
|
|
0x00, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
|
|
|
|
};
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
int
|
|
|
|
open_socket(struct interface *iface, int protocol)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2008-05-15 19:12:44 +08:00
|
|
|
int s;
|
2007-07-21 21:00:18 +08:00
|
|
|
union sockunion {
|
|
|
|
struct sockaddr sa;
|
2008-02-22 00:45:01 +08:00
|
|
|
struct sockaddr_in sin;
|
2007-07-21 21:00:18 +08:00
|
|
|
struct sockaddr_ll sll;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
} su;
|
2008-01-27 19:31:01 +08:00
|
|
|
struct sock_fprog pf;
|
2008-06-09 04:07:54 +08:00
|
|
|
int *fd;
|
2008-02-22 00:45:01 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
if ((s = socket(PF_PACKET, SOCK_DGRAM, htons(protocol))) == -1)
|
2008-03-21 00:47:51 +08:00
|
|
|
return -1;
|
2008-04-26 15:39:23 +08:00
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
memset(&su, 0, sizeof(su));
|
2008-01-27 19:31:01 +08:00
|
|
|
su.sll.sll_family = PF_PACKET;
|
2008-03-21 00:47:51 +08:00
|
|
|
su.sll.sll_protocol = htons(protocol);
|
|
|
|
if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) {
|
2008-03-29 16:40:55 +08:00
|
|
|
errno = ENOENT;
|
|
|
|
goto eexit;
|
2008-01-27 19:31:01 +08:00
|
|
|
}
|
|
|
|
/* Install the DHCP filter */
|
2008-03-21 00:47:51 +08:00
|
|
|
memset(&pf, 0, sizeof(pf));
|
2008-06-09 04:07:54 +08:00
|
|
|
#ifdef ENABLE_ARP
|
2008-01-27 19:31:01 +08:00
|
|
|
if (protocol == ETHERTYPE_ARP) {
|
2008-05-22 21:59:33 +08:00
|
|
|
pf.filter = UNCONST(arp_bpf_filter);
|
2008-05-14 20:19:05 +08:00
|
|
|
pf.len = arp_bpf_filter_len;
|
2008-06-09 04:07:54 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2008-05-22 21:59:33 +08:00
|
|
|
pf.filter = UNCONST(dhcp_bpf_filter);
|
2008-05-14 20:19:05 +08:00
|
|
|
pf.len = dhcp_bpf_filter_len;
|
2008-01-27 19:31:01 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0)
|
2008-03-29 16:40:55 +08:00
|
|
|
goto eexit;
|
2008-06-11 06:23:57 +08:00
|
|
|
if (set_cloexec(s) == -1)
|
2008-05-15 22:16:47 +08:00
|
|
|
goto eexit;
|
2008-06-11 06:23:57 +08:00
|
|
|
if (set_nonblock(s) == -1)
|
2008-03-29 16:40:55 +08:00
|
|
|
goto eexit;
|
2008-06-11 06:23:57 +08:00
|
|
|
if (bind(s, &su.sa, sizeof(su)) == -1)
|
2008-05-15 18:10:39 +08:00
|
|
|
goto eexit;
|
2008-06-09 04:07:54 +08:00
|
|
|
#ifdef ENABLE_ARP
|
|
|
|
if (protocol == ETHERTYPE_ARP)
|
|
|
|
fd = &iface->arp_fd;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
fd = &iface->fd;
|
|
|
|
if (*fd != -1)
|
|
|
|
close(*fd);
|
|
|
|
*fd = s;
|
2008-04-12 00:14:55 +08:00
|
|
|
return s;
|
2008-03-29 16:40:55 +08:00
|
|
|
|
|
|
|
eexit:
|
2008-04-12 00:14:55 +08:00
|
|
|
close(s);
|
2008-03-29 16:40:55 +08:00
|
|
|
return -1;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
ssize_t
|
2008-06-09 04:07:54 +08:00
|
|
|
send_raw_packet(const struct interface *iface, int protocol,
|
2008-05-15 18:10:39 +08:00
|
|
|
const void *data, ssize_t len)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2007-07-21 21:00:18 +08:00
|
|
|
union sockunion {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_ll sll;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
} su;
|
2008-06-09 04:07:54 +08:00
|
|
|
int fd;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
memset(&su, 0, sizeof(su));
|
2007-07-21 21:00:18 +08:00
|
|
|
su.sll.sll_family = AF_PACKET;
|
2008-06-09 04:07:54 +08:00
|
|
|
su.sll.sll_protocol = htons(protocol);
|
2008-03-21 00:47:51 +08:00
|
|
|
if (!(su.sll.sll_ifindex = if_nametoindex(iface->name))) {
|
2008-03-29 16:40:55 +08:00
|
|
|
errno = ENOENT;
|
2008-03-21 00:47:51 +08:00
|
|
|
return -1;
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-03-21 00:47:51 +08:00
|
|
|
su.sll.sll_hatype = htons(iface->family);
|
2007-07-21 21:00:18 +08:00
|
|
|
su.sll.sll_halen = iface->hwlen;
|
2008-01-24 23:01:19 +08:00
|
|
|
if (iface->family == ARPHRD_INFINIBAND)
|
2008-03-21 00:47:51 +08:00
|
|
|
memcpy(&su.sll.sll_addr,
|
|
|
|
&ipv4_bcast_addr, sizeof(ipv4_bcast_addr));
|
2008-01-24 23:01:19 +08:00
|
|
|
else
|
2008-03-21 00:47:51 +08:00
|
|
|
memset(&su.sll.sll_addr, 0xff, iface->hwlen);
|
2008-06-09 04:07:54 +08:00
|
|
|
#ifdef ENABLE_ARP
|
|
|
|
if (protocol == ETHERTYPE_ARP)
|
|
|
|
fd = iface->arp_fd;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
fd = iface->fd;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-06-09 04:07:54 +08:00
|
|
|
return sendto(fd, data, len, 0, &su.sa, sizeof(su));
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
ssize_t
|
2008-07-01 02:54:31 +08:00
|
|
|
get_raw_packet(struct interface *iface, int protocol, void *data, ssize_t len)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2008-01-21 23:12:31 +08:00
|
|
|
ssize_t bytes;
|
2008-07-01 02:54:31 +08:00
|
|
|
int fd = -1;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-07-01 02:54:31 +08:00
|
|
|
if (protocol == ETHERTYPE_ARP) {
|
2008-06-09 04:07:54 +08:00
|
|
|
#ifdef ENABLE_ARP
|
|
|
|
fd = iface->arp_fd;
|
|
|
|
#endif
|
2008-07-01 02:54:31 +08:00
|
|
|
} else
|
2008-06-09 04:07:54 +08:00
|
|
|
fd = iface->fd;
|
|
|
|
bytes = read(fd, data, len);
|
2008-05-15 18:10:39 +08:00
|
|
|
if (bytes == -1)
|
2008-05-15 19:12:44 +08:00
|
|
|
return errno == EAGAIN ? 0 : -1;
|
2008-03-21 00:47:51 +08:00
|
|
|
return bytes;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|