aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-03-22 13:42:13 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-03-22 13:42:13 +0100
commit6b24d5354bc166f6962b6b42de4ab9c7e6470225 (patch)
treed6d30d430db92f038ee643233a2838f2ef62a011
parent4f0408b5bc6ed726e7f98d6adedede41337b9b74 (diff)
downloadbusybox-w32-6b24d5354bc166f6962b6b42de4ab9c7e6470225.tar.gz
busybox-w32-6b24d5354bc166f6962b6b42de4ab9c7e6470225.tar.bz2
busybox-w32-6b24d5354bc166f6962b6b42de4ab9c7e6470225.zip
udhcpc: merge clientsocket.c into dhcpc.c. +4 bytes.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/udhcp/Kbuild19
-rw-r--r--networking/udhcp/clientsocket.c108
-rw-r--r--networking/udhcp/common.h1
-rw-r--r--networking/udhcp/dhcpc.c102
4 files changed, 105 insertions, 125 deletions
diff --git a/networking/udhcp/Kbuild b/networking/udhcp/Kbuild
index 138a950aa..473077e44 100644
--- a/networking/udhcp/Kbuild
+++ b/networking/udhcp/Kbuild
@@ -6,19 +6,14 @@
6# 6#
7 7
8lib-y:= 8lib-y:=
9lib-$(CONFIG_UDHCPC) += common.o options.o packet.o \
10 signalpipe.o socket.o
11lib-$(CONFIG_UDHCPD) += common.o options.o packet.o \
12 signalpipe.o socket.o
13 9
14lib-$(CONFIG_UDHCPC) += dhcpc.o clientpacket.o clientsocket.o \ 10lib-$(CONFIG_UDHCPC) += common.o options.o packet.o signalpipe.o socket.o
15 script.o 11lib-$(CONFIG_UDHCPD) += common.o options.o packet.o signalpipe.o socket.o
16 12
17UDHCPC_NEEDS_ARPING-$(CONFIG_FEATURE_UDHCPC_ARPING) = y 13lib-$(CONFIG_UDHCPC) += dhcpc.o clientpacket.o script.o
18lib-$(UDHCPC_NEEDS_ARPING-y) += arpping.o 14lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o
15lib-$(CONFIG_DUMPLEASES) += dumpleases.o
16lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
19 17
20lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o 18lib-$(CONFIG_FEATURE_UDHCPC_ARPING) += arpping.o
21
22lib-$(CONFIG_DUMPLEASES) += dumpleases.o
23lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
24lib-$(CONFIG_FEATURE_UDHCP_RFC3397) += domain_codec.o 19lib-$(CONFIG_FEATURE_UDHCP_RFC3397) += domain_codec.o
diff --git a/networking/udhcp/clientsocket.c b/networking/udhcp/clientsocket.c
deleted file mode 100644
index 8e68f31e7..000000000
--- a/networking/udhcp/clientsocket.c
+++ /dev/null
@@ -1,108 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * clientsocket.c -- DHCP client socket creation
4 *
5 * udhcp client
6 *
7 * Russ Dill <Russ.Dill@asu.edu> July 2001
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23#include "common.h"
24#include "dhcpd.h"
25#include "dhcpc.h"
26
27
28//#include <features.h>
29#include <asm/types.h>
30#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION)
31#include <netpacket/packet.h>
32#include <net/ethernet.h>
33#else
34#include <linux/if_packet.h>
35#include <linux/if_ether.h>
36#endif
37#include <linux/filter.h>
38
39int FAST_FUNC udhcp_raw_socket(int ifindex)
40{
41 int fd;
42 struct sockaddr_ll sock;
43
44 /*
45 * Comment:
46 *
47 * I've selected not to see LL header, so BPF doesn't see it, too.
48 * The filter may also pass non-IP and non-ARP packets, but we do
49 * a more complete check when receiving the message in userspace.
50 *
51 * and filter shamelessly stolen from:
52 *
53 * http://www.flamewarmaster.de/software/dhcpclient/
54 *
55 * There are a few other interesting ideas on that page (look under
56 * "Motivation"). Use of netlink events is most interesting. Think
57 * of various network servers listening for events and reconfiguring.
58 * That would obsolete sending HUP signals and/or make use of restarts.
59 *
60 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
61 * License: GPL v2.
62 *
63 * TODO: make conditional?
64 */
65#define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
66 static const struct sock_filter filter_instr[] = {
67 /* check for udp */
68 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
69 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0), /* L5, L1, is UDP? */
70 /* ugly check for arp on ethernet-like and IPv4 */
71 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
72 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4), /* L3, L4 */
73 /* skip IP header */
74 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
75 /* check udp source and destination ports */
76 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
77 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1), /* L3, L4 */
78 /* returns */
79 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff ), /* L3: pass */
80 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
81 };
82 static const struct sock_fprog filter_prog = {
83 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
84 /* casting const away: */
85 .filter = (struct sock_filter *) filter_instr,
86 };
87
88 log1("Opening raw socket on ifindex %d", ifindex); //log2?
89
90 fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
91 log1("Got raw socket fd %d", fd); //log2?
92
93 if (SERVER_PORT == 67 && CLIENT_PORT == 68) {
94 /* Use only if standard ports are in use */
95 /* Ignoring error (kernel may lack support for this) */
96 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
97 sizeof(filter_prog)) >= 0)
98 log1("Attached filter to raw socket fd %d", fd); // log?
99 }
100
101 sock.sll_family = AF_PACKET;
102 sock.sll_protocol = htons(ETH_P_IP);
103 sock.sll_ifindex = ifindex;
104 xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
105 log1("Created raw socket");
106
107 return fd;
108}
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h
index cf3e0cd39..70a2dc7c4 100644
--- a/networking/udhcp/common.h
+++ b/networking/udhcp/common.h
@@ -165,7 +165,6 @@ void udhcp_sp_setup(void) FAST_FUNC;
165int udhcp_sp_fd_set(fd_set *rfds, int extra_fd) FAST_FUNC; 165int udhcp_sp_fd_set(fd_set *rfds, int extra_fd) FAST_FUNC;
166int udhcp_sp_read(const fd_set *rfds) FAST_FUNC; 166int udhcp_sp_read(const fd_set *rfds) FAST_FUNC;
167int udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) FAST_FUNC; 167int udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) FAST_FUNC;
168int udhcp_raw_socket(int ifindex) FAST_FUNC;
169int udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) FAST_FUNC; 168int udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) FAST_FUNC;
170/* Returns 1 if no reply received */ 169/* Returns 1 if no reply received */
171int arpping(uint32_t test_nip, 170int arpping(uint32_t test_nip,
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index 3e2cd1217..a03e25ca4 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -1,11 +1,22 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* dhcpc.c 2/*
3 *
4 * udhcp DHCP client 3 * udhcp DHCP client
5 * 4 *
6 * Russ Dill <Russ.Dill@asu.edu> July 2001 5 * Russ Dill <Russ.Dill@asu.edu> July 2001
7 * 6 *
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
9 */ 20 */
10#include <syslog.h> 21#include <syslog.h>
11/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ 22/* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
@@ -15,6 +26,16 @@
15#include "dhcpc.h" 26#include "dhcpc.h"
16#include "options.h" 27#include "options.h"
17 28
29#include <asm/types.h>
30#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION)
31# include <netpacket/packet.h>
32# include <net/ethernet.h>
33#else
34# include <linux/if_packet.h>
35# include <linux/if_ether.h>
36#endif
37#include <linux/filter.h>
38
18 39
19static int sockfd = -1; 40static int sockfd = -1;
20 41
@@ -42,6 +63,79 @@ static smallint state;
42/* struct client_config_t client_config is in bb_common_bufsiz1 */ 63/* struct client_config_t client_config is in bb_common_bufsiz1 */
43 64
44 65
66
67static int udhcp_raw_socket(int ifindex)
68{
69 int fd;
70 struct sockaddr_ll sock;
71
72 /*
73 * Comment:
74 *
75 * I've selected not to see LL header, so BPF doesn't see it, too.
76 * The filter may also pass non-IP and non-ARP packets, but we do
77 * a more complete check when receiving the message in userspace.
78 *
79 * and filter shamelessly stolen from:
80 *
81 * http://www.flamewarmaster.de/software/dhcpclient/
82 *
83 * There are a few other interesting ideas on that page (look under
84 * "Motivation"). Use of netlink events is most interesting. Think
85 * of various network servers listening for events and reconfiguring.
86 * That would obsolete sending HUP signals and/or make use of restarts.
87 *
88 * Copyright: 2006, 2007 Stefan Rompf <sux@loplof.de>.
89 * License: GPL v2.
90 *
91 * TODO: make conditional?
92 */
93#define SERVER_AND_CLIENT_PORTS ((67 << 16) + 68)
94 static const struct sock_filter filter_instr[] = {
95 /* check for udp */
96 BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
97 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 2, 0), /* L5, L1, is UDP? */
98 /* ugly check for arp on ethernet-like and IPv4 */
99 BPF_STMT(BPF_LD|BPF_W|BPF_ABS, 2), /* L1: */
100 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0x08000604, 3, 4), /* L3, L4 */
101 /* skip IP header */
102 BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0), /* L5: */
103 /* check udp source and destination ports */
104 BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
105 BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1), /* L3, L4 */
106 /* returns */
107 BPF_STMT(BPF_RET|BPF_K, 0x0fffffff ), /* L3: pass */
108 BPF_STMT(BPF_RET|BPF_K, 0), /* L4: reject */
109 };
110 static const struct sock_fprog filter_prog = {
111 .len = sizeof(filter_instr) / sizeof(filter_instr[0]),
112 /* casting const away: */
113 .filter = (struct sock_filter *) filter_instr,
114 };
115
116 log1("Opening raw socket on ifindex %d", ifindex); //log2?
117
118 fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
119 log1("Got raw socket fd %d", fd); //log2?
120
121 if (SERVER_PORT == 67 && CLIENT_PORT == 68) {
122 /* Use only if standard ports are in use */
123 /* Ignoring error (kernel may lack support for this) */
124 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog,
125 sizeof(filter_prog)) >= 0)
126 log1("Attached filter to raw socket fd %d", fd); // log?
127 }
128
129 sock.sll_family = AF_PACKET;
130 sock.sll_protocol = htons(ETH_P_IP);
131 sock.sll_ifindex = ifindex;
132 xbind(fd, (struct sockaddr *) &sock, sizeof(sock));
133 log1("Created raw socket");
134
135 return fd;
136}
137
138
45/* just a little helper */ 139/* just a little helper */
46static void change_listen_mode(int new_mode) 140static void change_listen_mode(int new_mode)
47{ 141{
@@ -60,7 +154,7 @@ static void change_listen_mode(int new_mode)
60 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface); 154 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
61 else if (new_mode != LISTEN_NONE) 155 else if (new_mode != LISTEN_NONE)
62 sockfd = udhcp_raw_socket(client_config.ifindex); 156 sockfd = udhcp_raw_socket(client_config.ifindex);
63 /* else LISTEN_NONE: sockfd stay closed */ 157 /* else LISTEN_NONE: sockfd stays closed */
64} 158}
65 159
66 160