aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-12-20 21:11:38 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-12-20 21:11:38 +0000
commitfff145dba309df2a6ba2986ad2b690f7e0858cad (patch)
treeddd42dcde1f1d52b477c63d2c85e6f1f1f075bda
parentf77f369ce89549ae3b0b70bfbac4e9c242ec5298 (diff)
downloadbusybox-w32-fff145dba309df2a6ba2986ad2b690f7e0858cad.tar.gz
busybox-w32-fff145dba309df2a6ba2986ad2b690f7e0858cad.tar.bz2
busybox-w32-fff145dba309df2a6ba2986ad2b690f7e0858cad.zip
udhcp: fix oversized packet sending (introduced by "slack for bad dhcp servers" options);
slight optimizations and function renaming udhcp_send_raw_packet - 391 +391 udhcp_send_kernel_packet - 197 +197 udhcp_recv_packet - 134 +134 get_raw_packet 353 326 -27 udhcp_get_packet 134 - -134 udhcp_kernel_packet 197 - -197 udhcp_raw_packet 391 - -391 ------------------------------------------------------------------------------ (add/remove: 3/3 grow/shrink: 0/1 up/down: 722/-749) Total: -27 bytes
-rw-r--r--networking/udhcp/clientpacket.c36
-rw-r--r--networking/udhcp/common.h10
-rw-r--r--networking/udhcp/dhcpc.c2
-rw-r--r--networking/udhcp/dhcpd.c2
-rw-r--r--networking/udhcp/dhcprelay.c2
-rw-r--r--networking/udhcp/packet.c36
-rw-r--r--networking/udhcp/serverpacket.c4
7 files changed, 49 insertions, 43 deletions
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c
index 54f3f0e49..03473109f 100644
--- a/networking/udhcp/clientpacket.c
+++ b/networking/udhcp/clientpacket.c
@@ -86,7 +86,7 @@ int send_decline(uint32_t xid, uint32_t server)
86 86
87 bb_info_msg("Sending decline..."); 87 bb_info_msg("Sending decline...");
88 88
89 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 89 return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
90 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); 90 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
91} 91}
92#endif 92#endif
@@ -106,7 +106,7 @@ int send_discover(uint32_t xid, uint32_t requested)
106 add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576)); 106 add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
107 add_requests(&packet); 107 add_requests(&packet);
108 bb_info_msg("Sending discover..."); 108 bb_info_msg("Sending discover...");
109 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 109 return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
110 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); 110 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
111} 111}
112 112
@@ -126,7 +126,7 @@ int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
126 add_requests(&packet); 126 add_requests(&packet);
127 addr.s_addr = requested; 127 addr.s_addr = requested;
128 bb_info_msg("Sending select for %s...", inet_ntoa(addr)); 128 bb_info_msg("Sending select for %s...", inet_ntoa(addr));
129 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 129 return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
130 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); 130 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
131} 131}
132 132
@@ -143,9 +143,9 @@ int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
143 add_requests(&packet); 143 add_requests(&packet);
144 bb_info_msg("Sending renew..."); 144 bb_info_msg("Sending renew...");
145 if (server) 145 if (server)
146 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); 146 return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
147 147
148 return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 148 return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
149 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); 149 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
150} 150}
151 151
@@ -163,7 +163,7 @@ int send_release(uint32_t server, uint32_t ciaddr)
163 add_simple_option(packet.options, DHCP_SERVER_ID, server); 163 add_simple_option(packet.options, DHCP_SERVER_ID, server);
164 164
165 bb_info_msg("Sending release..."); 165 bb_info_msg("Sending release...");
166 return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); 166 return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
167} 167}
168 168
169 169
@@ -172,7 +172,6 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
172{ 172{
173 int bytes; 173 int bytes;
174 struct udp_dhcp_packet packet; 174 struct udp_dhcp_packet packet;
175 uint32_t source, dest;
176 uint16_t check; 175 uint16_t check;
177 176
178 memset(&packet, 0, sizeof(struct udp_dhcp_packet)); 177 memset(&packet, 0, sizeof(struct udp_dhcp_packet));
@@ -207,36 +206,31 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
207 return -2; 206 return -2;
208 } 207 }
209 208
210 /* check IP checksum */ 209 /* verify IP checksum */
211 check = packet.ip.check; 210 check = packet.ip.check;
212 packet.ip.check = 0; 211 packet.ip.check = 0;
213 if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) { 212 if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
214 DEBUG("bad IP header checksum, ignoring"); 213 DEBUG("Bad IP header checksum, ignoring");
215 return -1; 214 return -1;
216 } 215 }
217 216
218 /* verify the UDP checksum by replacing the header with a pseudo header */ 217 /* verify UDP checksum. IP header has to be modified for this */
219 source = packet.ip.saddr; 218 memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
220 dest = packet.ip.daddr; 219 /* fields which are not memset: protocol, check, saddr, daddr */
220 packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
221 check = packet.udp.check; 221 check = packet.udp.check;
222 packet.udp.check = 0; 222 packet.udp.check = 0;
223 memset(&packet.ip, 0, sizeof(packet.ip));
224
225 packet.ip.protocol = IPPROTO_UDP;
226 packet.ip.saddr = source;
227 packet.ip.daddr = dest;
228 packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
229 if (check && check != udhcp_checksum(&packet, bytes)) { 223 if (check && check != udhcp_checksum(&packet, bytes)) {
230 bb_error_msg("packet with bad UDP checksum received, ignoring"); 224 bb_error_msg("packet with bad UDP checksum received, ignoring");
231 return -2; 225 return -2;
232 } 226 }
233 227
234 memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); 228 memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
235 229
236 if (payload->cookie != htonl(DHCP_MAGIC)) { 230 if (payload->cookie != htonl(DHCP_MAGIC)) {
237 bb_error_msg("received bogus message (bad magic) - ignoring"); 231 bb_error_msg("received bogus message (bad magic) - ignoring");
238 return -2; 232 return -2;
239 } 233 }
240 DEBUG("oooooh!!! got some!"); 234 DEBUG("Got valid DHCP packet");
241 return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); 235 return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
242} 236}
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h
index 55782b51b..38ede0124 100644
--- a/networking/udhcp/common.h
+++ b/networking/udhcp/common.h
@@ -54,14 +54,16 @@ struct BUG_bad_sizeof_struct_udp_dhcp_packet {
54 [(sizeof(struct udp_dhcp_packet) != 576 + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS) ? -1 : 1]; 54 [(sizeof(struct udp_dhcp_packet) != 576 + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS) ? -1 : 1];
55}; 55};
56 56
57void udhcp_init_header(struct dhcpMessage *packet, char type);
58int udhcp_get_packet(struct dhcpMessage *packet, int fd);
59uint16_t udhcp_checksum(void *addr, int count); 57uint16_t udhcp_checksum(void *addr, int count);
60int udhcp_raw_packet(struct dhcpMessage *payload, 58
59void udhcp_init_header(struct dhcpMessage *packet, char type);
60
61int udhcp_recv_packet(struct dhcpMessage *packet, int fd);
62int udhcp_send_raw_packet(struct dhcpMessage *payload,
61 uint32_t source_ip, int source_port, 63 uint32_t source_ip, int source_port,
62 uint32_t dest_ip, int dest_port, 64 uint32_t dest_ip, int dest_port,
63 const uint8_t *dest_arp, int ifindex); 65 const uint8_t *dest_arp, int ifindex);
64int udhcp_kernel_packet(struct dhcpMessage *payload, 66int udhcp_send_kernel_packet(struct dhcpMessage *payload,
65 uint32_t source_ip, int source_port, 67 uint32_t source_ip, int source_port,
66 uint32_t dest_ip, int dest_port); 68 uint32_t dest_ip, int dest_port);
67 69
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index 69c35ca50..3de389f7b 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -451,7 +451,7 @@ int udhcpc_main(int argc, char **argv)
451 /* A packet is ready, read it */ 451 /* A packet is ready, read it */
452 452
453 if (listen_mode == LISTEN_KERNEL) 453 if (listen_mode == LISTEN_KERNEL)
454 len = udhcp_get_packet(&packet, sockfd); 454 len = udhcp_recv_packet(&packet, sockfd);
455 else len = get_raw_packet(&packet, sockfd); 455 else len = get_raw_packet(&packet, sockfd);
456 456
457 if (len == -1 && errno != EINTR) { 457 if (len == -1 && errno != EINTR) {
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c
index 9679e086c..45f445b48 100644
--- a/networking/udhcp/dhcpd.c
+++ b/networking/udhcp/dhcpd.c
@@ -133,7 +133,7 @@ int udhcpd_main(int argc, char **argv)
133 default: continue; /* signal or error (probably EINTR) */ 133 default: continue; /* signal or error (probably EINTR) */
134 } 134 }
135 135
136 bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */ 136 bytes = udhcp_recv_packet(&packet, server_socket); /* this waits for a packet - idle */
137 if (bytes < 0) { 137 if (bytes < 0) {
138 if (bytes == -1 && errno != EINTR) { 138 if (bytes == -1 && errno != EINTR) {
139 DEBUG("error on read, %s, reopening socket", strerror(errno)); 139 DEBUG("error on read, %s, reopening socket", strerror(errno));
diff --git a/networking/udhcp/dhcprelay.c b/networking/udhcp/dhcprelay.c
index 42378d602..a6483fc1f 100644
--- a/networking/udhcp/dhcprelay.c
+++ b/networking/udhcp/dhcprelay.c
@@ -256,7 +256,7 @@ static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **cli
256 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) { 256 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
257 /* server */ 257 /* server */
258 if (FD_ISSET(fds[0], &rfds)) { 258 if (FD_ISSET(fds[0], &rfds)) {
259 packlen = udhcp_get_packet(&dhcp_msg, fds[0]); 259 packlen = udhcp_recv_packet(&dhcp_msg, fds[0]);
260 if (packlen > 0) { 260 if (packlen > 0) {
261 pass_back(&dhcp_msg, packlen, fds); 261 pass_back(&dhcp_msg, packlen, fds);
262 } 262 }
diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c
index 0abe851a4..c3890229f 100644
--- a/networking/udhcp/packet.c
+++ b/networking/udhcp/packet.c
@@ -39,7 +39,7 @@ void udhcp_init_header(struct dhcpMessage *packet, char type)
39 39
40 40
41/* read a packet from socket fd, return -1 on read error, -2 on packet error */ 41/* read a packet from socket fd, return -1 on read error, -2 on packet error */
42int udhcp_get_packet(struct dhcpMessage *packet, int fd) 42int udhcp_recv_packet(struct dhcpMessage *packet, int fd)
43{ 43{
44#if 0 44#if 0
45 static const char broken_vendors[][8] = { 45 static const char broken_vendors[][8] = {
@@ -111,7 +111,7 @@ uint16_t udhcp_checksum(void *addr, int count)
111 /* Make sure that the left-over byte is added correctly both 111 /* Make sure that the left-over byte is added correctly both
112 * with little and big endian hosts */ 112 * with little and big endian hosts */
113 uint16_t tmp = 0; 113 uint16_t tmp = 0;
114 *(uint8_t *) (&tmp) = * (uint8_t *) source; 114 *(uint8_t*)&tmp = *(uint8_t*)source;
115 sum += tmp; 115 sum += tmp;
116 } 116 }
117 /* Fold 32-bit sum to 16 bits */ 117 /* Fold 32-bit sum to 16 bits */
@@ -123,7 +123,7 @@ uint16_t udhcp_checksum(void *addr, int count)
123 123
124 124
125/* Construct a ip/udp header for a packet, and specify the source and dest hardware address */ 125/* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
126int udhcp_raw_packet(struct dhcpMessage *payload, 126int udhcp_send_raw_packet(struct dhcpMessage *payload,
127 uint32_t source_ip, int source_port, 127 uint32_t source_ip, int source_port,
128 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex) 128 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex)
129{ 129{
@@ -132,6 +132,11 @@ int udhcp_raw_packet(struct dhcpMessage *payload,
132 struct sockaddr_ll dest; 132 struct sockaddr_ll dest;
133 struct udp_dhcp_packet packet; 133 struct udp_dhcp_packet packet;
134 134
135 enum {
136 IP_UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
137 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct udp_dhcp_packet, udp),
138 };
139
135 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); 140 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
136 if (fd < 0) { 141 if (fd < 0) {
137 bb_perror_msg("socket"); 142 bb_perror_msg("socket");
@@ -140,6 +145,7 @@ int udhcp_raw_packet(struct dhcpMessage *payload,
140 145
141 memset(&dest, 0, sizeof(dest)); 146 memset(&dest, 0, sizeof(dest));
142 memset(&packet, 0, sizeof(packet)); 147 memset(&packet, 0, sizeof(packet));
148 packet.data = *payload; /* struct copy */
143 149
144 dest.sll_family = AF_PACKET; 150 dest.sll_family = AF_PACKET;
145 dest.sll_protocol = htons(ETH_P_IP); 151 dest.sll_protocol = htons(ETH_P_IP);
@@ -157,19 +163,19 @@ int udhcp_raw_packet(struct dhcpMessage *payload,
157 packet.ip.daddr = dest_ip; 163 packet.ip.daddr = dest_ip;
158 packet.udp.source = htons(source_port); 164 packet.udp.source = htons(source_port);
159 packet.udp.dest = htons(dest_port); 165 packet.udp.dest = htons(dest_port);
160 packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */ 166 /* size, excluding IP header: */
167 packet.udp.len = htons(UPD_DHCP_SIZE);
168 /* for UDP checksumming, ip.len is set to UDP packet len */
161 packet.ip.tot_len = packet.udp.len; 169 packet.ip.tot_len = packet.udp.len;
162 memcpy(&(packet.data), payload, sizeof(struct dhcpMessage)); 170 packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
163 packet.udp.check = udhcp_checksum(&packet, sizeof(struct udp_dhcp_packet)); 171 /* but for sending, it is set to IP packet len */
164 172 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
165 packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
166 packet.ip.ihl = sizeof(packet.ip) >> 2; 173 packet.ip.ihl = sizeof(packet.ip) >> 2;
167 packet.ip.version = IPVERSION; 174 packet.ip.version = IPVERSION;
168 packet.ip.ttl = IPDEFTTL; 175 packet.ip.ttl = IPDEFTTL;
169 packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip)); 176 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
170 177
171 result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, 178 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, (struct sockaddr *) &dest, sizeof(dest));
172 (struct sockaddr *) &dest, sizeof(dest));
173 if (result <= 0) { 179 if (result <= 0) {
174 bb_perror_msg("sendto"); 180 bb_perror_msg("sendto");
175 } 181 }
@@ -179,13 +185,17 @@ int udhcp_raw_packet(struct dhcpMessage *payload,
179 185
180 186
181/* Let the kernel do all the work for packet generation */ 187/* Let the kernel do all the work for packet generation */
182int udhcp_kernel_packet(struct dhcpMessage *payload, 188int udhcp_send_kernel_packet(struct dhcpMessage *payload,
183 uint32_t source_ip, int source_port, 189 uint32_t source_ip, int source_port,
184 uint32_t dest_ip, int dest_port) 190 uint32_t dest_ip, int dest_port)
185{ 191{
186 int fd, result; 192 int fd, result;
187 struct sockaddr_in client; 193 struct sockaddr_in client;
188 194
195 enum {
196 DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
197 };
198
189 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 199 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
190 if (fd < 0) 200 if (fd < 0)
191 return -1; 201 return -1;
@@ -212,7 +222,7 @@ int udhcp_kernel_packet(struct dhcpMessage *payload,
212 return -1; 222 return -1;
213 } 223 }
214 224
215 result = write(fd, payload, sizeof(struct dhcpMessage)); 225 result = write(fd, payload, DHCP_SIZE);
216 close(fd); 226 close(fd);
217 return result; 227 return result;
218} 228}
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c
index ecbf50a14..764b9a8f6 100644
--- a/networking/udhcp/serverpacket.c
+++ b/networking/udhcp/serverpacket.c
@@ -30,7 +30,7 @@ static int send_packet_to_relay(struct dhcpMessage *payload)
30{ 30{
31 DEBUG("Forwarding packet to relay"); 31 DEBUG("Forwarding packet to relay");
32 32
33 return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT, 33 return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT,
34 payload->giaddr, SERVER_PORT); 34 payload->giaddr, SERVER_PORT);
35} 35}
36 36
@@ -58,7 +58,7 @@ static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcas
58 ciaddr = payload->yiaddr; 58 ciaddr = payload->yiaddr;
59 chaddr = payload->chaddr; 59 chaddr = payload->chaddr;
60 } 60 }
61 return udhcp_raw_packet(payload, server_config.server, SERVER_PORT, 61 return udhcp_send_raw_packet(payload, server_config.server, SERVER_PORT,
62 ciaddr, CLIENT_PORT, chaddr, server_config.ifindex); 62 ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
63} 63}
64 64