udhcpc: merge clientsocket.c into dhcpc.c. +4 bytes.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-03-22 13:42:13 +01:00
parent 4f0408b5bc
commit 6b24d5354b
4 changed files with 105 additions and 125 deletions

View File

@ -6,19 +6,14 @@
#
lib-y:=
lib-$(CONFIG_UDHCPC) += common.o options.o packet.o \
signalpipe.o socket.o
lib-$(CONFIG_UDHCPD) += common.o options.o packet.o \
signalpipe.o socket.o
lib-$(CONFIG_UDHCPC) += dhcpc.o clientpacket.o clientsocket.o \
script.o
lib-$(CONFIG_UDHCPC) += common.o options.o packet.o signalpipe.o socket.o
lib-$(CONFIG_UDHCPD) += common.o options.o packet.o signalpipe.o socket.o
UDHCPC_NEEDS_ARPING-$(CONFIG_FEATURE_UDHCPC_ARPING) = y
lib-$(UDHCPC_NEEDS_ARPING-y) += arpping.o
lib-$(CONFIG_UDHCPC) += dhcpc.o clientpacket.o script.o
lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o
lib-$(CONFIG_DUMPLEASES) += dumpleases.o
lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o
lib-$(CONFIG_DUMPLEASES) += dumpleases.o
lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
lib-$(CONFIG_FEATURE_UDHCPC_ARPING) += arpping.o
lib-$(CONFIG_FEATURE_UDHCP_RFC3397) += domain_codec.o

View File

@ -1,108 +0,0 @@
/* vi: set sw=4 ts=4: */
/*
* clientsocket.c -- DHCP client socket creation
*
* udhcp client
*
* Russ Dill <Russ.Dill@asu.edu> July 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "common.h"
#include "dhcpd.h"
#include "dhcpc.h"
//#include <features.h>
#include <asm/types.h>
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION)
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#endif
#include <linux/filter.h>
int FAST_FUNC udhcp_raw_socket(int ifindex)
{
int fd;
struct sockaddr_ll sock;
/*
* Comment:
*
* I've selected not to see LL header, so BPF doesn't see it, too.
* The filter may also pass non-IP and non-ARP packets, but we do
* a more complete check when receiving the message in userspace.
*
* and filter shamelessly stolen from:
*
* http://www.flamewarmaster.de/software/dhcpclient/
*
* There are a few other interesting ideas on that page (look under
* "Motivation"). Use of netlink events is most interesting. Think
* of various network servers listening for events and reconfiguring.
* That would obsolete sending HUP signals and/or make use of restarts.
*
* Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
* License: GPL v2.
*
* TODO: make conditional?
*/
#define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
static const struct sock_filter filter_instr[] = {
/* check for udp */
BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0), /* L5, L1, is UDP? */
/* ugly check for arp on ethernet-like and IPv4 */
BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4), /* L3, L4 */
/* skip IP header */
BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
/* check udp source and destination ports */
BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1), /* L3, L4 */
/* returns */
BPF_STMT(BPF_RET|BPF_K, 0x0fffffff ), /* L3: pass */
BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
};
static const struct sock_fprog filter_prog = {
.len = sizeof(filter_instr) / sizeof(filter_instr[0]),
/* casting const away: */
.filter = (struct sock_filter *) filter_instr,
};
log1("Opening raw socket on ifindex %d", ifindex); //log2?
fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
log1("Got raw socket fd %d", fd); //log2?
if (SERVER_PORT == 67 && CLIENT_PORT == 68) {
/* Use only if standard ports are in use */
/* Ignoring error (kernel may lack support for this) */
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
sizeof(filter_prog)) >= 0)
log1("Attached filter to raw socket fd %d", fd); // log?
}
sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
log1("Created raw socket");
return fd;
}

View File

@ -165,7 +165,6 @@ void udhcp_sp_setup(void) FAST_FUNC;
int udhcp_sp_fd_set(fd_set *rfds, int extra_fd) FAST_FUNC;
int udhcp_sp_read(const fd_set *rfds) FAST_FUNC;
int udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) FAST_FUNC;
int udhcp_raw_socket(int ifindex) FAST_FUNC;
int udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) FAST_FUNC;
/* Returns 1 if no reply received */
int arpping(uint32_t test_nip,

View File

@ -1,11 +1,22 @@
/* vi: set sw=4 ts=4: */
/* dhcpc.c
*
/*
* udhcp DHCP client
*
* Russ Dill <Russ.Dill@asu.edu> July 2001
*
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <syslog.h>
/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
@ -15,6 +26,16 @@
#include "dhcpc.h"
#include "options.h"
#include <asm/types.h>
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION)
# include <netpacket/packet.h>
# include <net/ethernet.h>
#else
# include <linux/if_packet.h>
# include <linux/if_ether.h>
#endif
#include <linux/filter.h>
static int sockfd = -1;
@ -42,6 +63,79 @@ static smallint state;
/* struct client_config_t client_config is in bb_common_bufsiz1 */
static int udhcp_raw_socket(int ifindex)
{
int fd;
struct sockaddr_ll sock;
/*
* Comment:
*
* I've selected not to see LL header, so BPF doesn't see it, too.
* The filter may also pass non-IP and non-ARP packets, but we do
* a more complete check when receiving the message in userspace.
*
* and filter shamelessly stolen from:
*
* http://www.flamewarmaster.de/software/dhcpclient/
*
* There are a few other interesting ideas on that page (look under
* "Motivation"). Use of netlink events is most interesting. Think
* of various network servers listening for events and reconfiguring.
* That would obsolete sending HUP signals and/or make use of restarts.
*
* Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
* License: GPL v2.
*
* TODO: make conditional?
*/
#define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
static const struct sock_filter filter_instr[] = {
/* check for udp */
BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0), /* L5, L1, is UDP? */
/* ugly check for arp on ethernet-like and IPv4 */
BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4), /* L3, L4 */
/* skip IP header */
BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
/* check udp source and destination ports */
BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1), /* L3, L4 */
/* returns */
BPF_STMT(BPF_RET|BPF_K, 0x0fffffff ), /* L3: pass */
BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
};
static const struct sock_fprog filter_prog = {
.len = sizeof(filter_instr) / sizeof(filter_instr[0]),
/* casting const away: */
.filter = (struct sock_filter *) filter_instr,
};
log1("Opening raw socket on ifindex %d", ifindex); //log2?
fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
log1("Got raw socket fd %d", fd); //log2?
if (SERVER_PORT == 67 && CLIENT_PORT == 68) {
/* Use only if standard ports are in use */
/* Ignoring error (kernel may lack support for this) */
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
sizeof(filter_prog)) >= 0)
log1("Attached filter to raw socket fd %d", fd); // log?
}
sock.sll_family = AF_PACKET;
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
log1("Created raw socket");
return fd;
}
/* just a little helper */
static void change_listen_mode(int new_mode)
{
@ -60,7 +154,7 @@ static void change_listen_mode(int new_mode)
sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
else if (new_mode != LISTEN_NONE)
sockfd = udhcp_raw_socket(client_config.ifindex);
/* else LISTEN_NONE: sockfd stay closed */
/* else LISTEN_NONE: sockfd stays closed */
}