aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/packet.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-06-17 11:54:52 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-06-17 11:54:52 +0200
commitac906fa85e61b4e34161709de777616f858bc945 (patch)
tree7b247714814fd9dcf3fd3dccf954521b29eef5a2 /networking/udhcp/packet.c
parented8982bfc0e9895fe707a5f6152cf184e06f2052 (diff)
downloadbusybox-w32-ac906fa85e61b4e34161709de777616f858bc945.tar.gz
busybox-w32-ac906fa85e61b4e34161709de777616f858bc945.tar.bz2
busybox-w32-ac906fa85e61b4e34161709de777616f858bc945.zip
udhcp: change UDHCP_DEBUG into int, make verbosity selectable with -v
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/udhcp/packet.c')
-rw-r--r--networking/udhcp/packet.c76
1 files changed, 60 insertions, 16 deletions
diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c
index e2c8e6ead..2cd5f6176 100644
--- a/networking/udhcp/packet.c
+++ b/networking/udhcp/packet.c
@@ -5,7 +5,6 @@
5 * 5 *
6 * Licensed under GPLv2, see file LICENSE in this tarball for details. 6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
7 */ 7 */
8
9#include <netinet/in.h> 8#include <netinet/in.h>
10#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION 9#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
11#include <netpacket/packet.h> 10#include <netpacket/packet.h>
@@ -20,7 +19,6 @@
20#include "dhcpd.h" 19#include "dhcpd.h"
21#include "options.h" 20#include "options.h"
22 21
23
24void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type) 22void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type)
25{ 23{
26 memset(packet, 0, sizeof(struct dhcpMessage)); 24 memset(packet, 0, sizeof(struct dhcpMessage));
@@ -38,8 +36,54 @@ void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type)
38 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type); 36 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
39} 37}
40 38
39#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
40void FAST_FUNC udhcp_dump_packet(struct dhcpMessage *packet)
41{
42 char buf[sizeof(packet->chaddr)*2 + 1];
43
44 if (dhcp_verbose < 2)
45 return;
46
47 bb_info_msg(
48 //" op %x"
49 //" htype %x"
50 " hlen %x"
51 //" hops %x"
52 " xid %x"
53 //" secs %x"
54 //" flags %x"
55 " ciaddr %x"
56 " yiaddr %x"
57 " siaddr %x"
58 " giaddr %x"
59 //" chaddr %s"
60 //" sname %s"
61 //" file %s"
62 //" cookie %x"
63 //" options %s"
64 //, packet->op
65 //, packet->htype
66 , packet->hlen
67 //, packet->hops
68 , packet->xid
69 //, packet->secs
70 //, packet->flags
71 , packet->ciaddr
72 , packet->yiaddr
73 , packet->siaddr_nip
74 , packet->gateway_nip
75 //, packet->chaddr[16]
76 //, packet->sname[64]
77 //, packet->file[128]
78 //, packet->cookie
79 //, packet->options[]
80 );
81 bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr));
82 bb_info_msg(" chaddr %s", buf);
83}
84#endif
41 85
42/* read a packet from socket fd, return -1 on read error, -2 on packet error */ 86/* Read a packet from socket fd, return -1 on read error, -2 on packet error */
43int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd) 87int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
44{ 88{
45 int bytes; 89 int bytes;
@@ -48,15 +92,16 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
48 memset(packet, 0, sizeof(*packet)); 92 memset(packet, 0, sizeof(*packet));
49 bytes = safe_read(fd, packet, sizeof(*packet)); 93 bytes = safe_read(fd, packet, sizeof(*packet));
50 if (bytes < 0) { 94 if (bytes < 0) {
51 DEBUG("cannot read on listening socket, ignoring"); 95 log1("Packet read error, ignoring");
52 return bytes; /* returns -1 */ 96 return bytes; /* returns -1 */
53 } 97 }
54 98
55 if (packet->cookie != htonl(DHCP_MAGIC)) { 99 if (packet->cookie != htonl(DHCP_MAGIC)) {
56 bb_error_msg("received bogus message, ignoring"); 100 bb_info_msg("Packet with bad magic, ignoring");
57 return -2; 101 return -2;
58 } 102 }
59 DEBUG("Received a packet"); 103 log1("Received a packet");
104 udhcp_dump_packet(packet);
60 105
61 if (packet->op == BOOTREQUEST) { 106 if (packet->op == BOOTREQUEST) {
62 vendor = get_option(packet, DHCP_VENDOR); 107 vendor = get_option(packet, DHCP_VENDOR);
@@ -71,7 +116,7 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
71 if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i]) 116 if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
72 && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2]) 117 && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
73 ) { 118 ) {
74 DEBUG("broken client (%s), forcing broadcast replies", 119 log1("Broken client (%s), forcing broadcast replies",
75 broken_vendors[i]); 120 broken_vendors[i]);
76 packet->flags |= htons(BROADCAST_FLAG); 121 packet->flags |= htons(BROADCAST_FLAG);
77 } 122 }
@@ -80,7 +125,7 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
80 if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1) 125 if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
81 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0 126 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
82 ) { 127 ) {
83 DEBUG("broken client (%s), forcing broadcast replies", "MSFT 98"); 128 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
84 packet->flags |= htons(BROADCAST_FLAG); 129 packet->flags |= htons(BROADCAST_FLAG);
85 } 130 }
86#endif 131#endif
@@ -90,11 +135,10 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
90 return bytes; 135 return bytes;
91} 136}
92 137
93
94uint16_t FAST_FUNC udhcp_checksum(void *addr, int count) 138uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
95{ 139{
96 /* Compute Internet Checksum for "count" bytes 140 /* Compute Internet Checksum for "count" bytes
97 * beginning at location "addr". 141 * beginning at location "addr".
98 */ 142 */
99 int32_t sum = 0; 143 int32_t sum = 0;
100 uint16_t *source = (uint16_t *) addr; 144 uint16_t *source = (uint16_t *) addr;
@@ -120,9 +164,8 @@ uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
120 return ~sum; 164 return ~sum;
121} 165}
122 166
123
124/* Construct a ip/udp header for a packet, send packet */ 167/* Construct a ip/udp header for a packet, send packet */
125int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload, 168int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *dhcp_pkt,
126 uint32_t source_ip, int source_port, 169 uint32_t source_ip, int source_port,
127 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, 170 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
128 int ifindex) 171 int ifindex)
@@ -146,7 +189,7 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload,
146 189
147 memset(&dest, 0, sizeof(dest)); 190 memset(&dest, 0, sizeof(dest));
148 memset(&packet, 0, sizeof(packet)); 191 memset(&packet, 0, sizeof(packet));
149 packet.data = *payload; /* struct copy */ 192 packet.data = *dhcp_pkt; /* struct copy */
150 193
151 dest.sll_family = AF_PACKET; 194 dest.sll_family = AF_PACKET;
152 dest.sll_protocol = htons(ETH_P_IP); 195 dest.sll_protocol = htons(ETH_P_IP);
@@ -179,6 +222,7 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload,
179 * If you need to change this: last byte of the packet is 222 * If you need to change this: last byte of the packet is
180 * packet.data.options[end_option(packet.data.options)] 223 * packet.data.options[end_option(packet.data.options)]
181 */ 224 */
225 udhcp_dump_packet(dhcp_pkt);
182 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, 226 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
183 (struct sockaddr *) &dest, sizeof(dest)); 227 (struct sockaddr *) &dest, sizeof(dest));
184 msg = "sendto"; 228 msg = "sendto";
@@ -191,9 +235,8 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload,
191 return result; 235 return result;
192} 236}
193 237
194
195/* Let the kernel do all the work for packet generation */ 238/* Let the kernel do all the work for packet generation */
196int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *payload, 239int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *dhcp_pkt,
197 uint32_t source_ip, int source_port, 240 uint32_t source_ip, int source_port,
198 uint32_t dest_ip, int dest_port) 241 uint32_t dest_ip, int dest_port)
199{ 242{
@@ -232,7 +275,8 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *payload,
232 } 275 }
233 276
234 /* Currently we send full-sized DHCP packets (see above) */ 277 /* Currently we send full-sized DHCP packets (see above) */
235 result = safe_write(fd, payload, DHCP_SIZE); 278 udhcp_dump_packet(dhcp_pkt);
279 result = safe_write(fd, dhcp_pkt, DHCP_SIZE);
236 msg = "write"; 280 msg = "write";
237 ret_close: 281 ret_close:
238 close(fd); 282 close(fd);