summaryrefslogtreecommitdiff
path: root/networking/udhcp/dhcpc.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/dhcpc.c')
-rw-r--r--networking/udhcp/dhcpc.c102
1 files changed, 98 insertions, 4 deletions
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