diff options
-rw-r--r-- | networking/udhcp/Kbuild | 2 | ||||
-rw-r--r-- | networking/udhcp/clientpacket.c | 272 | ||||
-rw-r--r-- | networking/udhcp/common.h | 1 | ||||
-rw-r--r-- | networking/udhcp/dhcpc.c | 245 | ||||
-rw-r--r-- | networking/udhcp/dhcpc.h | 15 |
5 files changed, 240 insertions, 295 deletions
diff --git a/networking/udhcp/Kbuild b/networking/udhcp/Kbuild index 473077e44..693f6c873 100644 --- a/networking/udhcp/Kbuild +++ b/networking/udhcp/Kbuild | |||
@@ -10,7 +10,7 @@ lib-y:= | |||
10 | lib-$(CONFIG_UDHCPC) += common.o options.o packet.o signalpipe.o socket.o | 10 | lib-$(CONFIG_UDHCPC) += common.o options.o packet.o signalpipe.o socket.o |
11 | lib-$(CONFIG_UDHCPD) += common.o options.o packet.o signalpipe.o socket.o | 11 | lib-$(CONFIG_UDHCPD) += common.o options.o packet.o signalpipe.o socket.o |
12 | 12 | ||
13 | lib-$(CONFIG_UDHCPC) += dhcpc.o clientpacket.o script.o | 13 | lib-$(CONFIG_UDHCPC) += dhcpc.o script.o |
14 | lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o | 14 | lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o |
15 | lib-$(CONFIG_DUMPLEASES) += dumpleases.o | 15 | lib-$(CONFIG_DUMPLEASES) += dumpleases.o |
16 | lib-$(CONFIG_DHCPRELAY) += dhcprelay.o | 16 | lib-$(CONFIG_DHCPRELAY) += dhcprelay.o |
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c deleted file mode 100644 index b4a75be02..000000000 --- a/networking/udhcp/clientpacket.c +++ /dev/null | |||
@@ -1,272 +0,0 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* clientpacket.c | ||
3 | * | ||
4 | * Packet generation and dispatching functions for the DHCP client. | ||
5 | * | ||
6 | * Russ Dill <Russ.Dill@asu.edu> July 2001 | ||
7 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
9 | */ | ||
10 | #include "common.h" | ||
11 | #include "dhcpd.h" | ||
12 | #include "dhcpc.h" | ||
13 | #include "options.h" | ||
14 | |||
15 | //#include <features.h> | ||
16 | #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION | ||
17 | #include <netpacket/packet.h> | ||
18 | #include <net/ethernet.h> | ||
19 | #else | ||
20 | #include <asm/types.h> | ||
21 | #include <linux/if_packet.h> | ||
22 | #include <linux/if_ether.h> | ||
23 | #endif | ||
24 | |||
25 | |||
26 | /* Create a random xid */ | ||
27 | uint32_t FAST_FUNC random_xid(void) | ||
28 | { | ||
29 | static smallint initialized; | ||
30 | |||
31 | if (!initialized) { | ||
32 | srand(monotonic_us()); | ||
33 | initialized = 1; | ||
34 | } | ||
35 | return rand(); | ||
36 | } | ||
37 | |||
38 | |||
39 | /* Initialize the packet with the proper defaults */ | ||
40 | static void init_packet(struct dhcp_packet *packet, char type) | ||
41 | { | ||
42 | udhcp_init_header(packet, type); | ||
43 | memcpy(packet->chaddr, client_config.client_mac, 6); | ||
44 | if (client_config.clientid) | ||
45 | add_option_string(packet->options, client_config.clientid); | ||
46 | if (client_config.hostname) | ||
47 | add_option_string(packet->options, client_config.hostname); | ||
48 | if (client_config.fqdn) | ||
49 | add_option_string(packet->options, client_config.fqdn); | ||
50 | if (type != DHCPDECLINE | ||
51 | && type != DHCPRELEASE | ||
52 | && client_config.vendorclass | ||
53 | ) { | ||
54 | add_option_string(packet->options, client_config.vendorclass); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | |||
59 | /* Add a parameter request list for stubborn DHCP servers. Pull the data | ||
60 | * from the struct in options.c. Don't do bounds checking here because it | ||
61 | * goes towards the head of the packet. */ | ||
62 | static void add_param_req_option(struct dhcp_packet *packet) | ||
63 | { | ||
64 | uint8_t c; | ||
65 | int end = end_option(packet->options); | ||
66 | int i, len = 0; | ||
67 | |||
68 | for (i = 0; (c = dhcp_options[i].code) != 0; i++) { | ||
69 | if (( (dhcp_options[i].flags & OPTION_REQ) | ||
70 | && !client_config.no_default_options | ||
71 | ) | ||
72 | || (client_config.opt_mask[c >> 3] & (1 << (c & 7))) | ||
73 | ) { | ||
74 | packet->options[end + OPT_DATA + len] = c; | ||
75 | len++; | ||
76 | } | ||
77 | } | ||
78 | if (len) { | ||
79 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; | ||
80 | packet->options[end + OPT_LEN] = len; | ||
81 | packet->options[end + OPT_DATA + len] = DHCP_END; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | /* RFC 2131 | ||
86 | * 4.4.4 Use of broadcast and unicast | ||
87 | * | ||
88 | * The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM | ||
89 | * messages, unless the client knows the address of a DHCP server. | ||
90 | * The client unicasts DHCPRELEASE messages to the server. Because | ||
91 | * the client is declining the use of the IP address supplied by the server, | ||
92 | * the client broadcasts DHCPDECLINE messages. | ||
93 | * | ||
94 | * When the DHCP client knows the address of a DHCP server, in either | ||
95 | * INIT or REBOOTING state, the client may use that address | ||
96 | * in the DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address. | ||
97 | * The client may also use unicast to send DHCPINFORM messages | ||
98 | * to a known DHCP server. If the client receives no response to DHCP | ||
99 | * messages sent to the IP address of a known DHCP server, the DHCP | ||
100 | * client reverts to using the IP broadcast address. | ||
101 | */ | ||
102 | |||
103 | static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet) | ||
104 | { | ||
105 | return udhcp_send_raw_packet(packet, | ||
106 | /*src*/ INADDR_ANY, CLIENT_PORT, | ||
107 | /*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR, | ||
108 | client_config.ifindex); | ||
109 | } | ||
110 | |||
111 | |||
112 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ | ||
113 | int FAST_FUNC send_discover(uint32_t xid, uint32_t requested) | ||
114 | { | ||
115 | struct dhcp_packet packet; | ||
116 | |||
117 | init_packet(&packet, DHCPDISCOVER); | ||
118 | packet.xid = xid; | ||
119 | if (requested) | ||
120 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
121 | /* Explicitly saying that we want RFC-compliant packets helps | ||
122 | * some buggy DHCP servers to NOT send bigger packets */ | ||
123 | add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576)); | ||
124 | add_param_req_option(&packet); | ||
125 | |||
126 | bb_info_msg("Sending discover..."); | ||
127 | return raw_bcast_from_client_config_ifindex(&packet); | ||
128 | } | ||
129 | |||
130 | |||
131 | /* Broadcast a DHCP request message */ | ||
132 | /* RFC 2131 3.1 paragraph 3: | ||
133 | * "The client _broadcasts_ a DHCPREQUEST message..." | ||
134 | */ | ||
135 | int FAST_FUNC send_select(uint32_t xid, uint32_t server, uint32_t requested) | ||
136 | { | ||
137 | struct dhcp_packet packet; | ||
138 | struct in_addr addr; | ||
139 | |||
140 | init_packet(&packet, DHCPREQUEST); | ||
141 | packet.xid = xid; | ||
142 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
143 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
144 | add_param_req_option(&packet); | ||
145 | |||
146 | addr.s_addr = requested; | ||
147 | bb_info_msg("Sending select for %s...", inet_ntoa(addr)); | ||
148 | return raw_bcast_from_client_config_ifindex(&packet); | ||
149 | } | ||
150 | |||
151 | |||
152 | /* Unicast or broadcast a DHCP renew message */ | ||
153 | int FAST_FUNC send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) | ||
154 | { | ||
155 | struct dhcp_packet packet; | ||
156 | |||
157 | init_packet(&packet, DHCPREQUEST); | ||
158 | packet.xid = xid; | ||
159 | packet.ciaddr = ciaddr; | ||
160 | add_param_req_option(&packet); | ||
161 | |||
162 | bb_info_msg("Sending renew..."); | ||
163 | if (server) | ||
164 | return udhcp_send_kernel_packet(&packet, | ||
165 | ciaddr, CLIENT_PORT, | ||
166 | server, SERVER_PORT); | ||
167 | return raw_bcast_from_client_config_ifindex(&packet); | ||
168 | } | ||
169 | |||
170 | |||
171 | #if ENABLE_FEATURE_UDHCPC_ARPING | ||
172 | /* Broadcast a DHCP decline message */ | ||
173 | int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested) | ||
174 | { | ||
175 | struct dhcp_packet packet; | ||
176 | |||
177 | init_packet(&packet, DHCPDECLINE); | ||
178 | packet.xid = xid; | ||
179 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
180 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
181 | |||
182 | bb_info_msg("Sending decline..."); | ||
183 | return raw_bcast_from_client_config_ifindex(&packet); | ||
184 | } | ||
185 | #endif | ||
186 | |||
187 | |||
188 | /* Unicast a DHCP release message */ | ||
189 | int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr) | ||
190 | { | ||
191 | struct dhcp_packet packet; | ||
192 | |||
193 | init_packet(&packet, DHCPRELEASE); | ||
194 | packet.xid = random_xid(); | ||
195 | packet.ciaddr = ciaddr; | ||
196 | |||
197 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
198 | |||
199 | bb_info_msg("Sending release..."); | ||
200 | return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); | ||
201 | } | ||
202 | |||
203 | |||
204 | /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */ | ||
205 | int FAST_FUNC udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) | ||
206 | { | ||
207 | int bytes; | ||
208 | struct ip_udp_dhcp_packet packet; | ||
209 | uint16_t check; | ||
210 | |||
211 | memset(&packet, 0, sizeof(packet)); | ||
212 | bytes = safe_read(fd, &packet, sizeof(packet)); | ||
213 | if (bytes < 0) { | ||
214 | log1("Packet read error, ignoring"); | ||
215 | /* NB: possible down interface, etc. Caller should pause. */ | ||
216 | return bytes; /* returns -1 */ | ||
217 | } | ||
218 | |||
219 | if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) { | ||
220 | log1("Packet is too short, ignoring"); | ||
221 | return -2; | ||
222 | } | ||
223 | |||
224 | if (bytes < ntohs(packet.ip.tot_len)) { | ||
225 | /* packet is bigger than sizeof(packet), we did partial read */ | ||
226 | log1("Oversized packet, ignoring"); | ||
227 | return -2; | ||
228 | } | ||
229 | |||
230 | /* ignore any extra garbage bytes */ | ||
231 | bytes = ntohs(packet.ip.tot_len); | ||
232 | |||
233 | /* make sure its the right packet for us, and that it passes sanity checks */ | ||
234 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION | ||
235 | || packet.ip.ihl != (sizeof(packet.ip) >> 2) | ||
236 | || packet.udp.dest != htons(CLIENT_PORT) | ||
237 | /* || bytes > (int) sizeof(packet) - can't happen */ | ||
238 | || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) | ||
239 | ) { | ||
240 | log1("Unrelated/bogus packet, ignoring"); | ||
241 | return -2; | ||
242 | } | ||
243 | |||
244 | /* verify IP checksum */ | ||
245 | check = packet.ip.check; | ||
246 | packet.ip.check = 0; | ||
247 | if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) { | ||
248 | log1("Bad IP header checksum, ignoring"); | ||
249 | return -2; | ||
250 | } | ||
251 | |||
252 | /* verify UDP checksum. IP header has to be modified for this */ | ||
253 | memset(&packet.ip, 0, offsetof(struct iphdr, protocol)); | ||
254 | /* ip.xx fields which are not memset: protocol, check, saddr, daddr */ | ||
255 | packet.ip.tot_len = packet.udp.len; /* yes, this is needed */ | ||
256 | check = packet.udp.check; | ||
257 | packet.udp.check = 0; | ||
258 | if (check && check != udhcp_checksum(&packet, bytes)) { | ||
259 | log1("Packet with bad UDP checksum received, ignoring"); | ||
260 | return -2; | ||
261 | } | ||
262 | |||
263 | memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp))); | ||
264 | |||
265 | if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) { | ||
266 | bb_info_msg("Packet with bad magic, ignoring"); | ||
267 | return -2; | ||
268 | } | ||
269 | log1("Got valid DHCP packet"); | ||
270 | udhcp_dump_packet(dhcp_pkt); | ||
271 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); | ||
272 | } | ||
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index 70a2dc7c4..d740ca56c 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h | |||
@@ -136,7 +136,6 @@ uint16_t udhcp_checksum(void *addr, int count) FAST_FUNC; | |||
136 | 136 | ||
137 | void udhcp_init_header(struct dhcp_packet *packet, char type) FAST_FUNC; | 137 | void udhcp_init_header(struct dhcp_packet *packet, char type) FAST_FUNC; |
138 | 138 | ||
139 | /*int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd); - in dhcpc.h */ | ||
140 | int udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) FAST_FUNC; | 139 | int udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) FAST_FUNC; |
141 | 140 | ||
142 | int udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, | 141 | int udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, |
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index a03e25ca4..d51d8b82f 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
@@ -63,6 +63,245 @@ static smallint state; | |||
63 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ | 63 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ |
64 | 64 | ||
65 | 65 | ||
66 | /* Create a random xid */ | ||
67 | static uint32_t random_xid(void) | ||
68 | { | ||
69 | static smallint initialized; | ||
70 | |||
71 | if (!initialized) { | ||
72 | srand(monotonic_us()); | ||
73 | initialized = 1; | ||
74 | } | ||
75 | return rand(); | ||
76 | } | ||
77 | |||
78 | /* Initialize the packet with the proper defaults */ | ||
79 | static void init_packet(struct dhcp_packet *packet, char type) | ||
80 | { | ||
81 | udhcp_init_header(packet, type); | ||
82 | memcpy(packet->chaddr, client_config.client_mac, 6); | ||
83 | if (client_config.clientid) | ||
84 | add_option_string(packet->options, client_config.clientid); | ||
85 | if (client_config.hostname) | ||
86 | add_option_string(packet->options, client_config.hostname); | ||
87 | if (client_config.fqdn) | ||
88 | add_option_string(packet->options, client_config.fqdn); | ||
89 | if (type != DHCPDECLINE | ||
90 | && type != DHCPRELEASE | ||
91 | && client_config.vendorclass | ||
92 | ) { | ||
93 | add_option_string(packet->options, client_config.vendorclass); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | /* Add a parameter request list for stubborn DHCP servers. Pull the data | ||
98 | * from the struct in options.c. Don't do bounds checking here because it | ||
99 | * goes towards the head of the packet. */ | ||
100 | static void add_param_req_option(struct dhcp_packet *packet) | ||
101 | { | ||
102 | uint8_t c; | ||
103 | int end = end_option(packet->options); | ||
104 | int i, len = 0; | ||
105 | |||
106 | for (i = 0; (c = dhcp_options[i].code) != 0; i++) { | ||
107 | if (( (dhcp_options[i].flags & OPTION_REQ) | ||
108 | && !client_config.no_default_options | ||
109 | ) | ||
110 | || (client_config.opt_mask[c >> 3] & (1 << (c & 7))) | ||
111 | ) { | ||
112 | packet->options[end + OPT_DATA + len] = c; | ||
113 | len++; | ||
114 | } | ||
115 | } | ||
116 | if (len) { | ||
117 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; | ||
118 | packet->options[end + OPT_LEN] = len; | ||
119 | packet->options[end + OPT_DATA + len] = DHCP_END; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | /* RFC 2131 | ||
124 | * 4.4.4 Use of broadcast and unicast | ||
125 | * | ||
126 | * The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM | ||
127 | * messages, unless the client knows the address of a DHCP server. | ||
128 | * The client unicasts DHCPRELEASE messages to the server. Because | ||
129 | * the client is declining the use of the IP address supplied by the server, | ||
130 | * the client broadcasts DHCPDECLINE messages. | ||
131 | * | ||
132 | * When the DHCP client knows the address of a DHCP server, in either | ||
133 | * INIT or REBOOTING state, the client may use that address | ||
134 | * in the DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address. | ||
135 | * The client may also use unicast to send DHCPINFORM messages | ||
136 | * to a known DHCP server. If the client receives no response to DHCP | ||
137 | * messages sent to the IP address of a known DHCP server, the DHCP | ||
138 | * client reverts to using the IP broadcast address. | ||
139 | */ | ||
140 | |||
141 | static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet) | ||
142 | { | ||
143 | return udhcp_send_raw_packet(packet, | ||
144 | /*src*/ INADDR_ANY, CLIENT_PORT, | ||
145 | /*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR, | ||
146 | client_config.ifindex); | ||
147 | } | ||
148 | |||
149 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ | ||
150 | static int send_discover(uint32_t xid, uint32_t requested) | ||
151 | { | ||
152 | struct dhcp_packet packet; | ||
153 | |||
154 | init_packet(&packet, DHCPDISCOVER); | ||
155 | packet.xid = xid; | ||
156 | if (requested) | ||
157 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
158 | /* Explicitly saying that we want RFC-compliant packets helps | ||
159 | * some buggy DHCP servers to NOT send bigger packets */ | ||
160 | add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576)); | ||
161 | add_param_req_option(&packet); | ||
162 | |||
163 | bb_info_msg("Sending discover..."); | ||
164 | return raw_bcast_from_client_config_ifindex(&packet); | ||
165 | } | ||
166 | |||
167 | /* Broadcast a DHCP request message */ | ||
168 | /* RFC 2131 3.1 paragraph 3: | ||
169 | * "The client _broadcasts_ a DHCPREQUEST message..." | ||
170 | */ | ||
171 | static int send_select(uint32_t xid, uint32_t server, uint32_t requested) | ||
172 | { | ||
173 | struct dhcp_packet packet; | ||
174 | struct in_addr addr; | ||
175 | |||
176 | init_packet(&packet, DHCPREQUEST); | ||
177 | packet.xid = xid; | ||
178 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
179 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
180 | add_param_req_option(&packet); | ||
181 | |||
182 | addr.s_addr = requested; | ||
183 | bb_info_msg("Sending select for %s...", inet_ntoa(addr)); | ||
184 | return raw_bcast_from_client_config_ifindex(&packet); | ||
185 | } | ||
186 | |||
187 | /* Unicast or broadcast a DHCP renew message */ | ||
188 | static int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) | ||
189 | { | ||
190 | struct dhcp_packet packet; | ||
191 | |||
192 | init_packet(&packet, DHCPREQUEST); | ||
193 | packet.xid = xid; | ||
194 | packet.ciaddr = ciaddr; | ||
195 | add_param_req_option(&packet); | ||
196 | |||
197 | bb_info_msg("Sending renew..."); | ||
198 | if (server) | ||
199 | return udhcp_send_kernel_packet(&packet, | ||
200 | ciaddr, CLIENT_PORT, | ||
201 | server, SERVER_PORT); | ||
202 | return raw_bcast_from_client_config_ifindex(&packet); | ||
203 | } | ||
204 | |||
205 | #if ENABLE_FEATURE_UDHCPC_ARPING | ||
206 | /* Broadcast a DHCP decline message */ | ||
207 | static int send_decline(uint32_t xid, uint32_t server, uint32_t requested) | ||
208 | { | ||
209 | struct dhcp_packet packet; | ||
210 | |||
211 | init_packet(&packet, DHCPDECLINE); | ||
212 | packet.xid = xid; | ||
213 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); | ||
214 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
215 | |||
216 | bb_info_msg("Sending decline..."); | ||
217 | return raw_bcast_from_client_config_ifindex(&packet); | ||
218 | } | ||
219 | #endif | ||
220 | |||
221 | /* Unicast a DHCP release message */ | ||
222 | static int send_release(uint32_t server, uint32_t ciaddr) | ||
223 | { | ||
224 | struct dhcp_packet packet; | ||
225 | |||
226 | init_packet(&packet, DHCPRELEASE); | ||
227 | packet.xid = random_xid(); | ||
228 | packet.ciaddr = ciaddr; | ||
229 | |||
230 | add_simple_option(packet.options, DHCP_SERVER_ID, server); | ||
231 | |||
232 | bb_info_msg("Sending release..."); | ||
233 | return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); | ||
234 | } | ||
235 | |||
236 | /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */ | ||
237 | static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) | ||
238 | { | ||
239 | int bytes; | ||
240 | struct ip_udp_dhcp_packet packet; | ||
241 | uint16_t check; | ||
242 | |||
243 | memset(&packet, 0, sizeof(packet)); | ||
244 | bytes = safe_read(fd, &packet, sizeof(packet)); | ||
245 | if (bytes < 0) { | ||
246 | log1("Packet read error, ignoring"); | ||
247 | /* NB: possible down interface, etc. Caller should pause. */ | ||
248 | return bytes; /* returns -1 */ | ||
249 | } | ||
250 | |||
251 | if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) { | ||
252 | log1("Packet is too short, ignoring"); | ||
253 | return -2; | ||
254 | } | ||
255 | |||
256 | if (bytes < ntohs(packet.ip.tot_len)) { | ||
257 | /* packet is bigger than sizeof(packet), we did partial read */ | ||
258 | log1("Oversized packet, ignoring"); | ||
259 | return -2; | ||
260 | } | ||
261 | |||
262 | /* ignore any extra garbage bytes */ | ||
263 | bytes = ntohs(packet.ip.tot_len); | ||
264 | |||
265 | /* make sure its the right packet for us, and that it passes sanity checks */ | ||
266 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION | ||
267 | || packet.ip.ihl != (sizeof(packet.ip) >> 2) | ||
268 | || packet.udp.dest != htons(CLIENT_PORT) | ||
269 | /* || bytes > (int) sizeof(packet) - can't happen */ | ||
270 | || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) | ||
271 | ) { | ||
272 | log1("Unrelated/bogus packet, ignoring"); | ||
273 | return -2; | ||
274 | } | ||
275 | |||
276 | /* verify IP checksum */ | ||
277 | check = packet.ip.check; | ||
278 | packet.ip.check = 0; | ||
279 | if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) { | ||
280 | log1("Bad IP header checksum, ignoring"); | ||
281 | return -2; | ||
282 | } | ||
283 | |||
284 | /* verify UDP checksum. IP header has to be modified for this */ | ||
285 | memset(&packet.ip, 0, offsetof(struct iphdr, protocol)); | ||
286 | /* ip.xx fields which are not memset: protocol, check, saddr, daddr */ | ||
287 | packet.ip.tot_len = packet.udp.len; /* yes, this is needed */ | ||
288 | check = packet.udp.check; | ||
289 | packet.udp.check = 0; | ||
290 | if (check && check != udhcp_checksum(&packet, bytes)) { | ||
291 | log1("Packet with bad UDP checksum received, ignoring"); | ||
292 | return -2; | ||
293 | } | ||
294 | |||
295 | memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp))); | ||
296 | |||
297 | if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) { | ||
298 | bb_info_msg("Packet with bad magic, ignoring"); | ||
299 | return -2; | ||
300 | } | ||
301 | log1("Got valid DHCP packet"); | ||
302 | udhcp_dump_packet(dhcp_pkt); | ||
303 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); | ||
304 | } | ||
66 | 305 | ||
67 | static int udhcp_raw_socket(int ifindex) | 306 | static int udhcp_raw_socket(int ifindex) |
68 | { | 307 | { |
@@ -135,7 +374,6 @@ static int udhcp_raw_socket(int ifindex) | |||
135 | return fd; | 374 | return fd; |
136 | } | 375 | } |
137 | 376 | ||
138 | |||
139 | /* just a little helper */ | 377 | /* just a little helper */ |
140 | static void change_listen_mode(int new_mode) | 378 | static void change_listen_mode(int new_mode) |
141 | { | 379 | { |
@@ -157,7 +395,6 @@ static void change_listen_mode(int new_mode) | |||
157 | /* else LISTEN_NONE: sockfd stays closed */ | 395 | /* else LISTEN_NONE: sockfd stays closed */ |
158 | } | 396 | } |
159 | 397 | ||
160 | |||
161 | /* perform a renew */ | 398 | /* perform a renew */ |
162 | static void perform_renew(void) | 399 | static void perform_renew(void) |
163 | { | 400 | { |
@@ -181,7 +418,6 @@ static void perform_renew(void) | |||
181 | } | 418 | } |
182 | } | 419 | } |
183 | 420 | ||
184 | |||
185 | /* perform a release */ | 421 | /* perform a release */ |
186 | static void perform_release(uint32_t requested_ip, uint32_t server_addr) | 422 | static void perform_release(uint32_t requested_ip, uint32_t server_addr) |
187 | { | 423 | { |
@@ -204,7 +440,6 @@ static void perform_release(uint32_t requested_ip, uint32_t server_addr) | |||
204 | state = RELEASED; | 440 | state = RELEASED; |
205 | } | 441 | } |
206 | 442 | ||
207 | |||
208 | #if BB_MMU | 443 | #if BB_MMU |
209 | static void client_background(void) | 444 | static void client_background(void) |
210 | { | 445 | { |
@@ -215,7 +450,6 @@ static void client_background(void) | |||
215 | } | 450 | } |
216 | #endif | 451 | #endif |
217 | 452 | ||
218 | |||
219 | static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) | 453 | static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) |
220 | { | 454 | { |
221 | uint8_t *storage; | 455 | uint8_t *storage; |
@@ -227,7 +461,6 @@ static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) | |||
227 | return storage; | 461 | return storage; |
228 | } | 462 | } |
229 | 463 | ||
230 | |||
231 | int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 464 | int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
232 | int udhcpc_main(int argc UNUSED_PARAM, char **argv) | 465 | int udhcpc_main(int argc UNUSED_PARAM, char **argv) |
233 | { | 466 | { |
diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h index c9827b6e7..a8861562e 100644 --- a/networking/udhcp/dhcpc.h +++ b/networking/udhcp/dhcpc.h | |||
@@ -1,5 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* dhcpc.h */ | ||
3 | #ifndef UDHCP_DHCPC_H | 2 | #ifndef UDHCP_DHCPC_H |
4 | #define UDHCP_DHCPC_H 1 | 3 | #define UDHCP_DHCPC_H 1 |
5 | 4 | ||
@@ -30,20 +29,6 @@ struct client_config_t { | |||
30 | #define CLIENT_PORT 68 | 29 | #define CLIENT_PORT 68 |
31 | #endif | 30 | #endif |
32 | 31 | ||
33 | |||
34 | /*** clientpacket.h ***/ | ||
35 | |||
36 | uint32_t random_xid(void) FAST_FUNC; | ||
37 | int send_discover(uint32_t xid, uint32_t requested) FAST_FUNC; | ||
38 | int send_select(uint32_t xid, uint32_t server, uint32_t requested) FAST_FUNC; | ||
39 | #if ENABLE_FEATURE_UDHCPC_ARPING | ||
40 | int send_decline(uint32_t xid, uint32_t server, uint32_t requested) FAST_FUNC; | ||
41 | #endif | ||
42 | int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) FAST_FUNC; | ||
43 | int send_release(uint32_t server, uint32_t ciaddr) FAST_FUNC; | ||
44 | |||
45 | int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) FAST_FUNC; | ||
46 | |||
47 | POP_SAVED_FUNCTION_VISIBILITY | 32 | POP_SAVED_FUNCTION_VISIBILITY |
48 | 33 | ||
49 | #endif | 34 | #endif |