diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-06-17 11:54:52 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-06-17 11:54:52 +0200 |
| commit | ac906fa85e61b4e34161709de777616f858bc945 (patch) | |
| tree | 7b247714814fd9dcf3fd3dccf954521b29eef5a2 | |
| parent | ed8982bfc0e9895fe707a5f6152cf184e06f2052 (diff) | |
| download | busybox-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>
| -rw-r--r-- | networking/udhcp/Config.in | 16 | ||||
| -rw-r--r-- | networking/udhcp/arpping.c | 2 | ||||
| -rw-r--r-- | networking/udhcp/clientpacket.c | 23 | ||||
| -rw-r--r-- | networking/udhcp/clientsocket.c | 8 | ||||
| -rw-r--r-- | networking/udhcp/common.c | 5 | ||||
| -rw-r--r-- | networking/udhcp/common.h | 22 | ||||
| -rw-r--r-- | networking/udhcp/dhcpc.c | 56 | ||||
| -rw-r--r-- | networking/udhcp/dhcpc.h | 9 | ||||
| -rw-r--r-- | networking/udhcp/dhcpd.c | 28 | ||||
| -rw-r--r-- | networking/udhcp/dhcpd.h | 1 | ||||
| -rw-r--r-- | networking/udhcp/files.c | 17 | ||||
| -rw-r--r-- | networking/udhcp/options.c | 2 | ||||
| -rw-r--r-- | networking/udhcp/packet.c | 76 | ||||
| -rw-r--r-- | networking/udhcp/script.c | 2 | ||||
| -rw-r--r-- | networking/udhcp/serverpacket.c | 44 | ||||
| -rw-r--r-- | networking/udhcp/socket.c | 8 |
16 files changed, 197 insertions, 122 deletions
diff --git a/networking/udhcp/Config.in b/networking/udhcp/Config.in index 495b07941..a31240a45 100644 --- a/networking/udhcp/Config.in +++ b/networking/udhcp/Config.in | |||
| @@ -74,15 +74,17 @@ config FEATURE_UDHCP_PORT | |||
| 74 | At the cost of ~300 bytes, enables -P port option. | 74 | At the cost of ~300 bytes, enables -P port option. |
| 75 | This feature is typically not needed. | 75 | This feature is typically not needed. |
| 76 | 76 | ||
| 77 | # TODO: UDHCP_DEBUG is inconvenient to use. | ||
| 78 | # Make it controllable at runtime (say, via -v, -vv, -vvv) | ||
| 79 | |||
| 80 | config UDHCP_DEBUG | 77 | config UDHCP_DEBUG |
| 81 | bool "Compile udhcp with noisy debugging messages" | 78 | int "Maximum verbosity level for udhcp applets (0..3)" |
| 82 | default n | 79 | default 0 |
| 83 | depends on APP_UDHCPD || APP_UDHCPC | 80 | range 0 3 |
| 81 | depends on APP_UDHCPD || APP_UDHCPC || APP_DHCPRELAY | ||
| 84 | help | 82 | help |
| 85 | If selected, udhcpd will output extra debugging output. | 83 | Verbosity can be increased with multiple -v options. |
| 84 | This options controls how high it can be cranked up. | ||
| 85 | |||
| 86 | Bigger values result in bigger code. Levels above 1 | ||
| 87 | are very verbose and useful for debugging only. | ||
| 86 | 88 | ||
| 87 | config FEATURE_UDHCP_RFC3397 | 89 | config FEATURE_UDHCP_RFC3397 |
| 88 | bool "Support for RFC3397 domain search (experimental)" | 90 | bool "Support for RFC3397 domain search (experimental)" |
diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c index fa0989d0f..47a7b1351 100644 --- a/networking/udhcp/arpping.c +++ b/networking/udhcp/arpping.c | |||
| @@ -128,6 +128,6 @@ int FAST_FUNC arpping(uint32_t test_ip, | |||
| 128 | 128 | ||
| 129 | ret: | 129 | ret: |
| 130 | close(s); | 130 | close(s); |
| 131 | DEBUG("%srp reply received for this address", rv ? "No a" : "A"); | 131 | log1("%srp reply received for this address", rv ? "No a" : "A"); |
| 132 | return rv; | 132 | return rv; |
| 133 | } | 133 | } |
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c index 068f947cc..7d44697ec 100644 --- a/networking/udhcp/clientpacket.c +++ b/networking/udhcp/clientpacket.c | |||
| @@ -203,7 +203,7 @@ int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr) | |||
| 203 | 203 | ||
| 204 | 204 | ||
| 205 | /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */ | 205 | /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */ |
| 206 | int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd) | 206 | int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *dhcp_pkt, int fd) |
| 207 | { | 207 | { |
| 208 | int bytes; | 208 | int bytes; |
| 209 | struct udp_dhcp_packet packet; | 209 | struct udp_dhcp_packet packet; |
| @@ -212,19 +212,19 @@ int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd) | |||
| 212 | memset(&packet, 0, sizeof(packet)); | 212 | memset(&packet, 0, sizeof(packet)); |
| 213 | bytes = safe_read(fd, &packet, sizeof(packet)); | 213 | bytes = safe_read(fd, &packet, sizeof(packet)); |
| 214 | if (bytes < 0) { | 214 | if (bytes < 0) { |
| 215 | DEBUG("Cannot read on raw listening socket - ignoring"); | 215 | log1("Packet read error, ignoring"); |
| 216 | /* NB: possible down interface, etc. Caller should pause. */ | 216 | /* NB: possible down interface, etc. Caller should pause. */ |
| 217 | return bytes; /* returns -1 */ | 217 | return bytes; /* returns -1 */ |
| 218 | } | 218 | } |
| 219 | 219 | ||
| 220 | if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) { | 220 | if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) { |
| 221 | DEBUG("Packet is too short, ignoring"); | 221 | log1("Packet is too short, ignoring"); |
| 222 | return -2; | 222 | return -2; |
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | if (bytes < ntohs(packet.ip.tot_len)) { | 225 | if (bytes < ntohs(packet.ip.tot_len)) { |
| 226 | /* packet is bigger than sizeof(packet), we did partial read */ | 226 | /* packet is bigger than sizeof(packet), we did partial read */ |
| 227 | DEBUG("Oversized packet, ignoring"); | 227 | log1("Oversized packet, ignoring"); |
| 228 | return -2; | 228 | return -2; |
| 229 | } | 229 | } |
| 230 | 230 | ||
| @@ -238,7 +238,7 @@ int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd) | |||
| 238 | /* || bytes > (int) sizeof(packet) - can't happen */ | 238 | /* || bytes > (int) sizeof(packet) - can't happen */ |
| 239 | || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) | 239 | || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) |
| 240 | ) { | 240 | ) { |
| 241 | DEBUG("Unrelated/bogus packet"); | 241 | log1("Unrelated/bogus packet, ignoring"); |
| 242 | return -2; | 242 | return -2; |
| 243 | } | 243 | } |
| 244 | 244 | ||
| @@ -246,7 +246,7 @@ int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd) | |||
| 246 | check = packet.ip.check; | 246 | check = packet.ip.check; |
| 247 | packet.ip.check = 0; | 247 | packet.ip.check = 0; |
| 248 | if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) { | 248 | if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) { |
| 249 | DEBUG("Bad IP header checksum, ignoring"); | 249 | log1("Bad IP header checksum, ignoring"); |
| 250 | return -2; | 250 | return -2; |
| 251 | } | 251 | } |
| 252 | 252 | ||
| @@ -257,16 +257,17 @@ int FAST_FUNC udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd) | |||
| 257 | check = packet.udp.check; | 257 | check = packet.udp.check; |
| 258 | packet.udp.check = 0; | 258 | packet.udp.check = 0; |
| 259 | if (check && check != udhcp_checksum(&packet, bytes)) { | 259 | if (check && check != udhcp_checksum(&packet, bytes)) { |
| 260 | bb_error_msg("packet with bad UDP checksum received, ignoring"); | 260 | log1("Packet with bad UDP checksum received, ignoring"); |
| 261 | return -2; | 261 | return -2; |
| 262 | } | 262 | } |
| 263 | 263 | ||
| 264 | memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp))); | 264 | memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp))); |
| 265 | 265 | ||
| 266 | if (payload->cookie != htonl(DHCP_MAGIC)) { | 266 | if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) { |
| 267 | bb_error_msg("received bogus message (bad magic), ignoring"); | 267 | bb_info_msg("Packet with bad magic, ignoring"); |
| 268 | return -2; | 268 | return -2; |
| 269 | } | 269 | } |
| 270 | DEBUG("Got valid DHCP packet"); | 270 | log1("Got valid DHCP packet"); |
| 271 | udhcp_dump_packet(dhcp_pkt); | ||
| 271 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); | 272 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); |
| 272 | } | 273 | } |
diff --git a/networking/udhcp/clientsocket.c b/networking/udhcp/clientsocket.c index 1dcc10570..851d5ab5f 100644 --- a/networking/udhcp/clientsocket.c +++ b/networking/udhcp/clientsocket.c | |||
| @@ -85,24 +85,24 @@ int FAST_FUNC udhcp_raw_socket(int ifindex) | |||
| 85 | .filter = (struct sock_filter *) filter_instr, | 85 | .filter = (struct sock_filter *) filter_instr, |
| 86 | }; | 86 | }; |
| 87 | 87 | ||
| 88 | DEBUG("opening raw socket on ifindex %d", ifindex); | 88 | log1("Opening raw socket on ifindex %d", ifindex); //log2? |
| 89 | 89 | ||
| 90 | fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); | 90 | fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); |
| 91 | DEBUG("got raw socket fd %d", fd); | 91 | log1("Got raw socket fd %d", fd); //log2? |
| 92 | 92 | ||
| 93 | if (SERVER_PORT == 67 && CLIENT_PORT == 68) { | 93 | if (SERVER_PORT == 67 && CLIENT_PORT == 68) { |
| 94 | /* Use only if standard ports are in use */ | 94 | /* Use only if standard ports are in use */ |
| 95 | /* Ignoring error (kernel may lack support for this) */ | 95 | /* Ignoring error (kernel may lack support for this) */ |
| 96 | if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, | 96 | if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, |
| 97 | sizeof(filter_prog)) >= 0) | 97 | sizeof(filter_prog)) >= 0) |
| 98 | DEBUG("attached filter to raw socket fd %d", fd); | 98 | log1("Attached filter to raw socket fd %d", fd); // log? |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | sock.sll_family = AF_PACKET; | 101 | sock.sll_family = AF_PACKET; |
| 102 | sock.sll_protocol = htons(ETH_P_IP); | 102 | sock.sll_protocol = htons(ETH_P_IP); |
| 103 | sock.sll_ifindex = ifindex; | 103 | sock.sll_ifindex = ifindex; |
| 104 | xbind(fd, (struct sockaddr *) &sock, sizeof(sock)); | 104 | xbind(fd, (struct sockaddr *) &sock, sizeof(sock)); |
| 105 | DEBUG("bound to raw socket fd %d", fd); | 105 | log1("Created raw socket"); |
| 106 | 106 | ||
| 107 | return fd; | 107 | return fd; |
| 108 | } | 108 | } |
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index a47bbafff..c44f38239 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c | |||
| @@ -3,9 +3,12 @@ | |||
| 3 | * | 3 | * |
| 4 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 4 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 5 | */ | 5 | */ |
| 6 | |||
| 7 | #include "common.h" | 6 | #include "common.h" |
| 8 | 7 | ||
| 8 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 | ||
| 9 | int dhcp_verbose; | ||
| 10 | #endif | ||
| 11 | |||
| 9 | const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = { | 12 | const uint8_t MAC_BCAST_ADDR[6] ALIGN2 = { |
| 10 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff | 13 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 11 | }; | 14 | }; |
diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index 0f12be063..40b8df4df 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h | |||
| @@ -62,15 +62,15 @@ uint16_t udhcp_checksum(void *addr, int count) FAST_FUNC; | |||
| 62 | 62 | ||
| 63 | void udhcp_init_header(struct dhcpMessage *packet, char type) FAST_FUNC; | 63 | void udhcp_init_header(struct dhcpMessage *packet, char type) FAST_FUNC; |
| 64 | 64 | ||
| 65 | /*int udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd); - in dhcpc.h */ | 65 | /*int udhcp_recv_raw_packet(struct dhcpMessage *dhcp_pkt, int fd); - in dhcpc.h */ |
| 66 | int udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd) FAST_FUNC; | 66 | int udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd) FAST_FUNC; |
| 67 | 67 | ||
| 68 | int udhcp_send_raw_packet(struct dhcpMessage *payload, | 68 | int udhcp_send_raw_packet(struct dhcpMessage *dhcp_pkt, |
| 69 | uint32_t source_ip, int source_port, | 69 | uint32_t source_ip, int source_port, |
| 70 | uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, | 70 | uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, |
| 71 | int ifindex) FAST_FUNC; | 71 | int ifindex) FAST_FUNC; |
| 72 | 72 | ||
| 73 | int udhcp_send_kernel_packet(struct dhcpMessage *payload, | 73 | int udhcp_send_kernel_packet(struct dhcpMessage *dhcp_pkt, |
| 74 | uint32_t source_ip, int source_port, | 74 | uint32_t source_ip, int source_port, |
| 75 | uint32_t dest_ip, int dest_port) FAST_FUNC; | 75 | uint32_t dest_ip, int dest_port) FAST_FUNC; |
| 76 | 76 | ||
| @@ -100,10 +100,20 @@ int arpping(uint32_t test_ip, | |||
| 100 | uint8_t *from_mac, | 100 | uint8_t *from_mac, |
| 101 | const char *interface) FAST_FUNC; | 101 | const char *interface) FAST_FUNC; |
| 102 | 102 | ||
| 103 | #if ENABLE_UDHCP_DEBUG | 103 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 104 | # define DEBUG(str, args...) bb_info_msg("### " str, ## args) | 104 | extern int dhcp_verbose; |
| 105 | # define log1(...) do { if (dhcp_verbose >= 1) bb_info_msg(__VA_ARGS__); } while (0) | ||
| 106 | # if CONFIG_UDHCP_DEBUG >= 2 | ||
| 107 | void udhcp_dump_packet(struct dhcpMessage *packet) FAST_FUNC; | ||
| 108 | # define log2(...) do { if (dhcp_verbose >= 2) bb_info_msg(__VA_ARGS__); } while (0) | ||
| 109 | # else | ||
| 110 | # define udhcp_dump_packet(...) ((void)0) | ||
| 111 | # define log2(...) ((void)0) | ||
| 112 | # endif | ||
| 105 | #else | 113 | #else |
| 106 | # define DEBUG(str, args...) do {;} while (0) | 114 | # define udhcp_dump_packet(...) ((void)0) |
| 115 | # define log1(...) ((void)0) | ||
| 116 | # define log2(...) ((void)0) | ||
| 107 | #endif | 117 | #endif |
| 108 | 118 | ||
| 109 | POP_SAVED_FUNCTION_VISIBILITY | 119 | POP_SAVED_FUNCTION_VISIBILITY |
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 44ff197b9..eed9d9a01 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
| @@ -20,9 +20,9 @@ | |||
| 20 | 20 | ||
| 21 | static int sockfd = -1; | 21 | static int sockfd = -1; |
| 22 | 22 | ||
| 23 | #define LISTEN_NONE 0 | 23 | #define LISTEN_NONE 0 |
| 24 | #define LISTEN_KERNEL 1 | 24 | #define LISTEN_KERNEL 1 |
| 25 | #define LISTEN_RAW 2 | 25 | #define LISTEN_RAW 2 |
| 26 | static smallint listen_mode; | 26 | static smallint listen_mode; |
| 27 | 27 | ||
| 28 | #define INIT_SELECTING 0 | 28 | #define INIT_SELECTING 0 |
| @@ -41,7 +41,7 @@ static smallint state; | |||
| 41 | /* just a little helper */ | 41 | /* just a little helper */ |
| 42 | static void change_listen_mode(int new_mode) | 42 | static void change_listen_mode(int new_mode) |
| 43 | { | 43 | { |
| 44 | DEBUG("entering %s listen mode", | 44 | log1("entering %s listen mode", |
| 45 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); | 45 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); |
| 46 | 46 | ||
| 47 | listen_mode = new_mode; | 47 | listen_mode = new_mode; |
| @@ -195,18 +195,17 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 195 | OPT_s = 1 << 12, | 195 | OPT_s = 1 << 12, |
| 196 | OPT_T = 1 << 13, | 196 | OPT_T = 1 << 13, |
| 197 | OPT_t = 1 << 14, | 197 | OPT_t = 1 << 14, |
| 198 | OPT_v = 1 << 15, | 198 | OPT_S = 1 << 15, |
| 199 | OPT_S = 1 << 16, | 199 | OPT_A = 1 << 16, |
| 200 | OPT_A = 1 << 17, | 200 | OPT_O = 1 << 17, |
| 201 | OPT_O = 1 << 18, | 201 | OPT_o = 1 << 18, |
| 202 | OPT_o = 1 << 19, | 202 | OPT_f = 1 << 19, |
| 203 | OPT_f = 1 << 20, | ||
| 204 | /* The rest has variable bit positions, need to be clever */ | 203 | /* The rest has variable bit positions, need to be clever */ |
| 205 | OPTBIT_f = 20, | 204 | OPTBIT_f = 19, |
| 206 | USE_FOR_MMU( OPTBIT_b,) | 205 | USE_FOR_MMU( OPTBIT_b,) |
| 207 | IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,) | 206 | IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,) |
| 208 | IF_FEATURE_UDHCP_PORT( OPTBIT_P,) | 207 | IF_FEATURE_UDHCP_PORT( OPTBIT_P,) |
| 209 | USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,) | 208 | USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,) |
| 210 | IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,) | 209 | IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,) |
| 211 | IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,) | 210 | IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,) |
| 212 | }; | 211 | }; |
| @@ -219,18 +218,26 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 219 | 218 | ||
| 220 | /* Parse command line */ | 219 | /* Parse command line */ |
| 221 | /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */ | 220 | /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */ |
| 222 | opt_complementary = "c--C:C--c:O::T+:t+:A+"; | 221 | opt_complementary = "c--C:C--c:O::T+:t+:A+" |
| 222 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 | ||
| 223 | ":vv" | ||
| 224 | #endif | ||
| 225 | ; | ||
| 223 | IF_GETOPT_LONG(applet_long_options = udhcpc_longopts;) | 226 | IF_GETOPT_LONG(applet_long_options = udhcpc_longopts;) |
| 224 | opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:vSA:O:of" | 227 | opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:SA:O:of" |
| 225 | USE_FOR_MMU("b") | 228 | USE_FOR_MMU("b") |
| 226 | IF_FEATURE_UDHCPC_ARPING("a") | 229 | IF_FEATURE_UDHCPC_ARPING("a") |
| 227 | IF_FEATURE_UDHCP_PORT("P:") | 230 | IF_FEATURE_UDHCP_PORT("P:") |
| 231 | "v" | ||
| 228 | , &str_c, &str_V, &str_h, &str_h, &str_F | 232 | , &str_c, &str_V, &str_h, &str_h, &str_F |
| 229 | , &client_config.interface, &client_config.pidfile, &str_r /* i,p */ | 233 | , &client_config.interface, &client_config.pidfile, &str_r /* i,p */ |
| 230 | , &client_config.script /* s */ | 234 | , &client_config.script /* s */ |
| 231 | , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */ | 235 | , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */ |
| 232 | , &list_O | 236 | , &list_O |
| 233 | IF_FEATURE_UDHCP_PORT(, &str_P) | 237 | IF_FEATURE_UDHCP_PORT(, &str_P) |
| 238 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 | ||
| 239 | , &dhcp_verbose | ||
| 240 | #endif | ||
| 234 | ); | 241 | ); |
| 235 | if (opt & OPT_c) | 242 | if (opt & OPT_c) |
| 236 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); | 243 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); |
| @@ -252,10 +259,6 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 252 | } | 259 | } |
| 253 | if (opt & OPT_r) | 260 | if (opt & OPT_r) |
| 254 | requested_ip = inet_addr(str_r); | 261 | requested_ip = inet_addr(str_r); |
| 255 | if (opt & OPT_v) { | ||
| 256 | puts("version "BB_VER); | ||
| 257 | return 0; | ||
| 258 | } | ||
| 259 | #if ENABLE_FEATURE_UDHCP_PORT | 262 | #if ENABLE_FEATURE_UDHCP_PORT |
| 260 | if (opt & OPT_P) { | 263 | if (opt & OPT_P) { |
| 261 | CLIENT_PORT = xatou16(str_P); | 264 | CLIENT_PORT = xatou16(str_P); |
| @@ -348,7 +351,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 348 | retval = 0; /* If we already timed out, fall through, else... */ | 351 | retval = 0; /* If we already timed out, fall through, else... */ |
| 349 | if (tv.tv_sec > 0) { | 352 | if (tv.tv_sec > 0) { |
| 350 | timestamp_before_wait = (unsigned)monotonic_sec(); | 353 | timestamp_before_wait = (unsigned)monotonic_sec(); |
| 351 | DEBUG("Waiting on select..."); | 354 | log1("Waiting on select..."); |
| 352 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); | 355 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
| 353 | if (retval < 0) { | 356 | if (retval < 0) { |
| 354 | /* EINTR? A signal was caught, don't panic */ | 357 | /* EINTR? A signal was caught, don't panic */ |
| @@ -428,7 +431,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 428 | case BOUND: | 431 | case BOUND: |
| 429 | /* Half of the lease passed, time to enter renewing state */ | 432 | /* Half of the lease passed, time to enter renewing state */ |
| 430 | change_listen_mode(LISTEN_KERNEL); | 433 | change_listen_mode(LISTEN_KERNEL); |
| 431 | DEBUG("Entering renew state"); | 434 | log1("Entering renew state"); |
| 432 | state = RENEWING; | 435 | state = RENEWING; |
| 433 | /* fall right through */ | 436 | /* fall right through */ |
| 434 | case RENEWING: | 437 | case RENEWING: |
| @@ -439,7 +442,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 439 | continue; | 442 | continue; |
| 440 | } | 443 | } |
| 441 | /* Timed out, enter rebinding state */ | 444 | /* Timed out, enter rebinding state */ |
| 442 | DEBUG("Entering rebinding state"); | 445 | log1("Entering rebinding state"); |
| 443 | state = REBINDING; | 446 | state = REBINDING; |
| 444 | /* fall right through */ | 447 | /* fall right through */ |
| 445 | case REBINDING: | 448 | case REBINDING: |
| @@ -477,7 +480,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 477 | else | 480 | else |
| 478 | len = udhcp_recv_raw_packet(&packet, sockfd); | 481 | len = udhcp_recv_raw_packet(&packet, sockfd); |
| 479 | if (len == -1) { /* error is severe, reopen socket */ | 482 | if (len == -1) { /* error is severe, reopen socket */ |
| 480 | DEBUG("error on read, %s, reopening socket", strerror(errno)); | 483 | bb_info_msg("Read error: %s, reopening socket", strerror(errno)); |
| 481 | sleep(discover_timeout); /* 3 seconds by default */ | 484 | sleep(discover_timeout); /* 3 seconds by default */ |
| 482 | change_listen_mode(listen_mode); /* just close and reopen */ | 485 | change_listen_mode(listen_mode); /* just close and reopen */ |
| 483 | } | 486 | } |
| @@ -489,20 +492,21 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 489 | continue; | 492 | continue; |
| 490 | 493 | ||
| 491 | if (packet.xid != xid) { | 494 | if (packet.xid != xid) { |
| 492 | DEBUG("Ignoring xid %x (our xid is %x)", | 495 | log1("xid %x (our is %x), ignoring packet", |
| 493 | (unsigned)packet.xid, (unsigned)xid); | 496 | (unsigned)packet.xid, (unsigned)xid); |
| 494 | continue; | 497 | continue; |
| 495 | } | 498 | } |
| 496 | 499 | ||
| 497 | /* Ignore packets that aren't for us */ | 500 | /* Ignore packets that aren't for us */ |
| 498 | if (memcmp(packet.chaddr, client_config.client_mac, 6)) { | 501 | if (memcmp(packet.chaddr, client_config.client_mac, 6)) { |
| 499 | DEBUG("Packet does not have our chaddr - ignoring"); | 502 | //FIXME: need to also check that last 10 bytes are zero |
| 503 | log1("chaddr does not match, ignoring packet"); // log2? | ||
| 500 | continue; | 504 | continue; |
| 501 | } | 505 | } |
| 502 | 506 | ||
| 503 | message = get_option(&packet, DHCP_MESSAGE_TYPE); | 507 | message = get_option(&packet, DHCP_MESSAGE_TYPE); |
| 504 | if (message == NULL) { | 508 | if (message == NULL) { |
| 505 | bb_error_msg("cannot get message type from packet - ignoring"); | 509 | bb_error_msg("no message type option, ignoring packet"); |
| 506 | continue; | 510 | continue; |
| 507 | } | 511 | } |
| 508 | 512 | ||
| @@ -563,7 +567,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 563 | client_config.client_mac, | 567 | client_config.client_mac, |
| 564 | client_config.interface) | 568 | client_config.interface) |
| 565 | ) { | 569 | ) { |
| 566 | bb_info_msg("offered address is in use " | 570 | bb_info_msg("Offered address is in use " |
| 567 | "(got ARP reply), declining"); | 571 | "(got ARP reply), declining"); |
| 568 | send_decline(xid, server_addr, packet.yiaddr); | 572 | send_decline(xid, server_addr, packet.yiaddr); |
| 569 | 573 | ||
diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h index 861e134bd..a7ed779a1 100644 --- a/networking/udhcp/dhcpc.h +++ b/networking/udhcp/dhcpc.h | |||
| @@ -6,12 +6,11 @@ | |||
| 6 | PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN | 6 | PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
| 7 | 7 | ||
| 8 | struct client_config_t { | 8 | struct client_config_t { |
| 9 | uint8_t client_mac[6]; /* Our arp address */ | 9 | uint8_t client_mac[6]; /* Our mac address */ |
| 10 | /* TODO: combine flag fields into single "unsigned opt" */ | 10 | char no_default_options; /* Do not include default options in request */ |
| 11 | /* (can be set directly to the result of getopt32) */ | ||
| 12 | char no_default_options; /* Do not include default optins in request */ | ||
| 13 | IF_FEATURE_UDHCP_PORT(uint16_t port;) | 11 | IF_FEATURE_UDHCP_PORT(uint16_t port;) |
| 14 | int ifindex; /* Index number of the interface to use */ | 12 | int ifindex; /* Index number of the interface to use */ |
| 13 | int verbose; | ||
| 15 | uint8_t opt_mask[256 / 8]; /* Bitmask of options to send (-O option) */ | 14 | uint8_t opt_mask[256 / 8]; /* Bitmask of options to send (-O option) */ |
| 16 | const char *interface; /* The name of the interface to use */ | 15 | const char *interface; /* The name of the interface to use */ |
| 17 | char *pidfile; /* Optionally store the process ID */ | 16 | char *pidfile; /* Optionally store the process ID */ |
| @@ -44,7 +43,7 @@ int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) FAST_FUNC; | |||
| 44 | int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) FAST_FUNC; | 43 | int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) FAST_FUNC; |
| 45 | int send_release(uint32_t server, uint32_t ciaddr) FAST_FUNC; | 44 | int send_release(uint32_t server, uint32_t ciaddr) FAST_FUNC; |
| 46 | 45 | ||
| 47 | int udhcp_recv_raw_packet(struct dhcpMessage *payload, int fd) FAST_FUNC; | 46 | int udhcp_recv_raw_packet(struct dhcpMessage *dhcp_pkt, int fd) FAST_FUNC; |
| 48 | 47 | ||
| 49 | POP_SAVED_FUNCTION_VISIBILITY | 48 | POP_SAVED_FUNCTION_VISIBILITY |
| 50 | 49 | ||
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index c74a11b31..cbc968401 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c | |||
| @@ -44,7 +44,15 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 44 | CLIENT_PORT = 68; | 44 | CLIENT_PORT = 68; |
| 45 | #endif | 45 | #endif |
| 46 | 46 | ||
| 47 | opt = getopt32(argv, "fS" IF_FEATURE_UDHCP_PORT("P:", &str_P)); | 47 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 48 | opt_complementary = "vv"; | ||
| 49 | #endif | ||
| 50 | opt = getopt32(argv, "fSv" | ||
| 51 | IF_FEATURE_UDHCP_PORT("P:", &str_P) | ||
| 52 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 | ||
| 53 | , &dhcp_verbose | ||
| 54 | #endif | ||
| 55 | ); | ||
| 48 | argv += optind; | 56 | argv += optind; |
| 49 | if (!(opt & 1)) { /* no -f */ | 57 | if (!(opt & 1)) { /* no -f */ |
| 50 | bb_daemonize_or_rexec(0, argv); | 58 | bb_daemonize_or_rexec(0, argv); |
| @@ -131,7 +139,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 131 | continue; | 139 | continue; |
| 132 | } | 140 | } |
| 133 | if (retval < 0 && errno != EINTR) { | 141 | if (retval < 0 && errno != EINTR) { |
| 134 | DEBUG("error on select"); | 142 | log1("Error on select"); |
| 135 | continue; | 143 | continue; |
| 136 | } | 144 | } |
| 137 | 145 | ||
| @@ -155,7 +163,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 155 | if (bytes < 0) { | 163 | if (bytes < 0) { |
| 156 | /* bytes can also be -2 ("bad packet data") */ | 164 | /* bytes can also be -2 ("bad packet data") */ |
| 157 | if (bytes == -1 && errno != EINTR) { | 165 | if (bytes == -1 && errno != EINTR) { |
| 158 | DEBUG("error on read, %s, reopening socket", strerror(errno)); | 166 | log1("Read error: %s, reopening socket", strerror(errno)); |
| 159 | close(server_socket); | 167 | close(server_socket); |
| 160 | server_socket = -1; | 168 | server_socket = -1; |
| 161 | } | 169 | } |
| @@ -164,7 +172,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 164 | 172 | ||
| 165 | state = get_option(&packet, DHCP_MESSAGE_TYPE); | 173 | state = get_option(&packet, DHCP_MESSAGE_TYPE); |
| 166 | if (state == NULL) { | 174 | if (state == NULL) { |
| 167 | bb_error_msg("cannot get option from packet, ignoring"); | 175 | bb_error_msg("no message type option, ignoring packet"); |
| 168 | continue; | 176 | continue; |
| 169 | } | 177 | } |
| 170 | 178 | ||
| @@ -184,14 +192,14 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 184 | 192 | ||
| 185 | switch (state[0]) { | 193 | switch (state[0]) { |
| 186 | case DHCPDISCOVER: | 194 | case DHCPDISCOVER: |
| 187 | DEBUG("Received DISCOVER"); | 195 | log1("Received DISCOVER"); |
| 188 | 196 | ||
| 189 | if (send_offer(&packet) < 0) { | 197 | if (send_offer(&packet) < 0) { |
| 190 | bb_error_msg("send OFFER failed"); | 198 | bb_error_msg("send OFFER failed"); |
| 191 | } | 199 | } |
| 192 | break; | 200 | break; |
| 193 | case DHCPREQUEST: | 201 | case DHCPREQUEST: |
| 194 | DEBUG("received REQUEST"); | 202 | log1("Received REQUEST"); |
| 195 | 203 | ||
| 196 | requested = get_option(&packet, DHCP_REQUESTED_IP); | 204 | requested = get_option(&packet, DHCP_REQUESTED_IP); |
| 197 | server_id = get_option(&packet, DHCP_SERVER_ID); | 205 | server_id = get_option(&packet, DHCP_SERVER_ID); |
| @@ -204,7 +212,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 204 | if (lease) { | 212 | if (lease) { |
| 205 | if (server_id) { | 213 | if (server_id) { |
| 206 | /* SELECTING State */ | 214 | /* SELECTING State */ |
| 207 | DEBUG("server_id = %08x", ntohl(server_id_aligned)); | 215 | log1("server_id = %08x", ntohl(server_id_aligned)); |
| 208 | if (server_id_aligned == server_config.server_nip | 216 | if (server_id_aligned == server_config.server_nip |
| 209 | && requested | 217 | && requested |
| 210 | && requested_aligned == lease->lease_nip | 218 | && requested_aligned == lease->lease_nip |
| @@ -254,19 +262,19 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
| 254 | } | 262 | } |
| 255 | break; | 263 | break; |
| 256 | case DHCPDECLINE: | 264 | case DHCPDECLINE: |
| 257 | DEBUG("Received DECLINE"); | 265 | log1("Received DECLINE"); |
| 258 | if (lease) { | 266 | if (lease) { |
| 259 | memset(lease->lease_mac16, 0, 16); | 267 | memset(lease->lease_mac16, 0, 16); |
| 260 | lease->expires = time(NULL) + server_config.decline_time; | 268 | lease->expires = time(NULL) + server_config.decline_time; |
| 261 | } | 269 | } |
| 262 | break; | 270 | break; |
| 263 | case DHCPRELEASE: | 271 | case DHCPRELEASE: |
| 264 | DEBUG("Received RELEASE"); | 272 | log1("Received RELEASE"); |
| 265 | if (lease) | 273 | if (lease) |
| 266 | lease->expires = time(NULL); | 274 | lease->expires = time(NULL); |
| 267 | break; | 275 | break; |
| 268 | case DHCPINFORM: | 276 | case DHCPINFORM: |
| 269 | DEBUG("Received INFORM"); | 277 | log1("Received INFORM"); |
| 270 | send_inform(&packet); | 278 | send_inform(&packet); |
| 271 | break; | 279 | break; |
| 272 | default: | 280 | default: |
diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h index bb2febbba..53cfad41f 100644 --- a/networking/udhcp/dhcpd.h +++ b/networking/udhcp/dhcpd.h | |||
| @@ -42,6 +42,7 @@ struct server_config_t { | |||
| 42 | #endif | 42 | #endif |
| 43 | uint8_t server_mac[6]; /* our MAC address (used only for ARP probing) */ | 43 | uint8_t server_mac[6]; /* our MAC address (used only for ARP probing) */ |
| 44 | struct option_set *options; /* list of DHCP options loaded from the config file */ | 44 | struct option_set *options; /* list of DHCP options loaded from the config file */ |
| 45 | int verbose; | ||
| 45 | /* start,end are in host order: we need to compare start <= ip <= end */ | 46 | /* start,end are in host order: we need to compare start <= ip <= end */ |
| 46 | uint32_t start_ip; /* start address of leases, in host order */ | 47 | uint32_t start_ip; /* start address of leases, in host order */ |
| 47 | uint32_t end_ip; /* end of leases, in host order */ | 48 | uint32_t end_ip; /* end of leases, in host order */ |
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index f3899711c..9d5633b75 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c | |||
| @@ -96,7 +96,7 @@ static void attach_option(struct option_set **opt_list, | |||
| 96 | 96 | ||
| 97 | existing = find_option(*opt_list, option->code); | 97 | existing = find_option(*opt_list, option->code); |
| 98 | if (!existing) { | 98 | if (!existing) { |
| 99 | DEBUG("Attaching option %02x to list", option->code); | 99 | log2("Attaching option %02x to list", option->code); |
| 100 | 100 | ||
| 101 | #if ENABLE_FEATURE_UDHCP_RFC3397 | 101 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 102 | if ((option->flags & TYPE_MASK) == OPTION_STR1035) | 102 | if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
| @@ -125,7 +125,7 @@ static void attach_option(struct option_set **opt_list, | |||
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | /* add it to an existing option */ | 127 | /* add it to an existing option */ |
| 128 | DEBUG("Attaching option %02x to existing member of list", option->code); | 128 | log1("Attaching option %02x to existing member of list", option->code); |
| 129 | if (option->flags & OPTION_LIST) { | 129 | if (option->flags & OPTION_LIST) { |
| 130 | #if ENABLE_FEATURE_UDHCP_RFC3397 | 130 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
| 131 | if ((option->flags & TYPE_MASK) == OPTION_STR1035) | 131 | if ((option->flags & TYPE_MASK) == OPTION_STR1035) |
| @@ -393,7 +393,9 @@ void FAST_FUNC read_leases(const char *file) | |||
| 393 | struct dhcpOfferedAddr lease; | 393 | struct dhcpOfferedAddr lease; |
| 394 | int64_t written_at, time_passed; | 394 | int64_t written_at, time_passed; |
| 395 | int fd; | 395 | int fd; |
| 396 | IF_UDHCP_DEBUG(unsigned i;) | 396 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 397 | unsigned i = 0; | ||
| 398 | #endif | ||
| 397 | 399 | ||
| 398 | fd = open_or_warn(file, O_RDONLY); | 400 | fd = open_or_warn(file, O_RDONLY); |
| 399 | if (fd < 0) | 401 | if (fd < 0) |
| @@ -409,9 +411,8 @@ void FAST_FUNC read_leases(const char *file) | |||
| 409 | if ((uint64_t)time_passed > 12 * 60 * 60) | 411 | if ((uint64_t)time_passed > 12 * 60 * 60) |
| 410 | goto ret; | 412 | goto ret; |
| 411 | 413 | ||
| 412 | IF_UDHCP_DEBUG(i = 0;) | ||
| 413 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { | 414 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
| 414 | /* ADDME: what if it matches some static lease? */ | 415 | //FIXME: what if it matches some static lease? |
| 415 | uint32_t y = ntohl(lease.lease_nip); | 416 | uint32_t y = ntohl(lease.lease_nip); |
| 416 | if (y >= server_config.start_ip && y <= server_config.end_ip) { | 417 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
| 417 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; | 418 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
| @@ -423,10 +424,12 @@ void FAST_FUNC read_leases(const char *file) | |||
| 423 | bb_error_msg("too many leases while loading %s", file); | 424 | bb_error_msg("too many leases while loading %s", file); |
| 424 | break; | 425 | break; |
| 425 | } | 426 | } |
| 426 | IF_UDHCP_DEBUG(i++;) | 427 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 428 | i++; | ||
| 429 | #endif | ||
| 427 | } | 430 | } |
| 428 | } | 431 | } |
| 429 | DEBUG("Read %d leases", i); | 432 | log1("Read %d leases", i); |
| 430 | ret: | 433 | ret: |
| 431 | close(fd); | 434 | close(fd); |
| 432 | } | 435 | } |
diff --git a/networking/udhcp/options.c b/networking/udhcp/options.c index 143a1fd1c..7b80e6b28 100644 --- a/networking/udhcp/options.c +++ b/networking/udhcp/options.c | |||
| @@ -205,7 +205,7 @@ int FAST_FUNC add_option_string(uint8_t *optionptr, uint8_t *string) | |||
| 205 | string[OPT_CODE]); | 205 | string[OPT_CODE]); |
| 206 | return 0; | 206 | return 0; |
| 207 | } | 207 | } |
| 208 | DEBUG("adding option 0x%02x", string[OPT_CODE]); | 208 | log1("Adding option 0x%02x", string[OPT_CODE]); |
| 209 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); | 209 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); |
| 210 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; | 210 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; |
| 211 | return string[OPT_LEN] + 2; | 211 | return string[OPT_LEN] + 2; |
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 | |||
| 24 | void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type) | 22 | void 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 | ||
| 40 | void 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 */ |
| 43 | int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd) | 87 | int 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 | |||
| 94 | uint16_t FAST_FUNC udhcp_checksum(void *addr, int count) | 138 | uint16_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 */ |
| 125 | int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload, | 168 | int 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 */ |
| 196 | int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *payload, | 239 | int 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); |
diff --git a/networking/udhcp/script.c b/networking/udhcp/script.c index 97c1d30c3..22780d86a 100644 --- a/networking/udhcp/script.c +++ b/networking/udhcp/script.c | |||
| @@ -218,7 +218,7 @@ void FAST_FUNC udhcp_run_script(struct dhcpMessage *packet, const char *name) | |||
| 218 | if (client_config.script == NULL) | 218 | if (client_config.script == NULL) |
| 219 | return; | 219 | return; |
| 220 | 220 | ||
| 221 | DEBUG("vfork'ing and exec'ing %s", client_config.script); | 221 | log1("Executing %s", client_config.script); |
| 222 | 222 | ||
| 223 | envp = fill_envp(packet); | 223 | envp = fill_envp(packet); |
| 224 | 224 | ||
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c index d9c5ce3ed..831165d8e 100644 --- a/networking/udhcp/serverpacket.c +++ b/networking/udhcp/serverpacket.c | |||
| @@ -27,48 +27,48 @@ | |||
| 27 | 27 | ||
| 28 | 28 | ||
| 29 | /* send a packet to gateway_nip using the kernel ip stack */ | 29 | /* send a packet to gateway_nip using the kernel ip stack */ |
| 30 | static int send_packet_to_relay(struct dhcpMessage *payload) | 30 | static int send_packet_to_relay(struct dhcpMessage *dhcp_pkt) |
| 31 | { | 31 | { |
| 32 | DEBUG("Forwarding packet to relay"); | 32 | log1("Forwarding packet to relay"); |
| 33 | 33 | ||
| 34 | return udhcp_send_kernel_packet(payload, | 34 | return udhcp_send_kernel_packet(dhcp_pkt, |
| 35 | server_config.server_nip, SERVER_PORT, | 35 | server_config.server_nip, SERVER_PORT, |
| 36 | payload->gateway_nip, SERVER_PORT); | 36 | dhcp_pkt->gateway_nip, SERVER_PORT); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | /* send a packet to a specific mac address and ip address by creating our own ip packet */ | 40 | /* send a packet to a specific mac address and ip address by creating our own ip packet */ |
| 41 | static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast) | 41 | static int send_packet_to_client(struct dhcpMessage *dhcp_pkt, int force_broadcast) |
| 42 | { | 42 | { |
| 43 | const uint8_t *chaddr; | 43 | const uint8_t *chaddr; |
| 44 | uint32_t ciaddr; | 44 | uint32_t ciaddr; |
| 45 | 45 | ||
| 46 | // Was: | 46 | // Was: |
| 47 | //if (force_broadcast) { /* broadcast */ } | 47 | //if (force_broadcast) { /* broadcast */ } |
| 48 | //else if (payload->ciaddr) { /* unicast to payload->ciaddr */ } | 48 | //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ } |
| 49 | //else if (payload->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } | 49 | //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } |
| 50 | //else { /* unicast to payload->yiaddr */ } | 50 | //else { /* unicast to dhcp_pkt->yiaddr */ } |
| 51 | // But this is wrong: yiaddr is _our_ idea what client's IP is | 51 | // But this is wrong: yiaddr is _our_ idea what client's IP is |
| 52 | // (for example, from lease file). Client may not know that, | 52 | // (for example, from lease file). Client may not know that, |
| 53 | // and may not have UDP socket listening on that IP! | 53 | // and may not have UDP socket listening on that IP! |
| 54 | // We should never unicast to payload->yiaddr! | 54 | // We should never unicast to dhcp_pkt->yiaddr! |
| 55 | // payload->ciaddr, OTOH, comes from client's request packet, | 55 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, |
| 56 | // and can be used. | 56 | // and can be used. |
| 57 | 57 | ||
| 58 | if (force_broadcast | 58 | if (force_broadcast |
| 59 | || (payload->flags & htons(BROADCAST_FLAG)) | 59 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) |
| 60 | || !payload->ciaddr | 60 | || !dhcp_pkt->ciaddr |
| 61 | ) { | 61 | ) { |
| 62 | DEBUG("broadcasting packet to client"); | 62 | log1("Broadcasting packet to client"); |
| 63 | ciaddr = INADDR_BROADCAST; | 63 | ciaddr = INADDR_BROADCAST; |
| 64 | chaddr = MAC_BCAST_ADDR; | 64 | chaddr = MAC_BCAST_ADDR; |
| 65 | } else { | 65 | } else { |
| 66 | DEBUG("unicasting packet to client ciaddr"); | 66 | log1("Unicasting packet to client ciaddr"); |
| 67 | ciaddr = payload->ciaddr; | 67 | ciaddr = dhcp_pkt->ciaddr; |
| 68 | chaddr = payload->chaddr; | 68 | chaddr = dhcp_pkt->chaddr; |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | return udhcp_send_raw_packet(payload, | 71 | return udhcp_send_raw_packet(dhcp_pkt, |
| 72 | /*src*/ server_config.server_nip, SERVER_PORT, | 72 | /*src*/ server_config.server_nip, SERVER_PORT, |
| 73 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, | 73 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
| 74 | server_config.ifindex); | 74 | server_config.ifindex); |
| @@ -76,11 +76,11 @@ static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcas | |||
| 76 | 76 | ||
| 77 | 77 | ||
| 78 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ | 78 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ |
| 79 | static int send_packet(struct dhcpMessage *payload, int force_broadcast) | 79 | static int send_packet(struct dhcpMessage *dhcp_pkt, int force_broadcast) |
| 80 | { | 80 | { |
| 81 | if (payload->gateway_nip) | 81 | if (dhcp_pkt->gateway_nip) |
| 82 | return send_packet_to_relay(payload); | 82 | return send_packet_to_relay(dhcp_pkt); |
| 83 | return send_packet_to_client(payload, force_broadcast); | 83 | return send_packet_to_client(dhcp_pkt, force_broadcast); |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | 86 | ||
| @@ -201,7 +201,7 @@ int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket) | |||
| 201 | 201 | ||
| 202 | init_packet(&packet, oldpacket, DHCPNAK); | 202 | init_packet(&packet, oldpacket, DHCPNAK); |
| 203 | 203 | ||
| 204 | DEBUG("Sending NAK"); | 204 | log1("Sending NAK"); |
| 205 | return send_packet(&packet, 1); | 205 | return send_packet(&packet, 1); |
| 206 | } | 206 | } |
| 207 | 207 | ||
diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index de494ca38..6b15766c9 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c | |||
| @@ -57,7 +57,7 @@ int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t | |||
| 57 | } | 57 | } |
| 58 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; | 58 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
| 59 | *nip = our_ip->sin_addr.s_addr; | 59 | *nip = our_ip->sin_addr.s_addr; |
| 60 | DEBUG("ip of %s = %s", interface, inet_ntoa(our_ip->sin_addr)); | 60 | log1("IP %s", inet_ntoa(our_ip->sin_addr)); |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | if (ifindex) { | 63 | if (ifindex) { |
| @@ -65,7 +65,7 @@ int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t | |||
| 65 | close(fd); | 65 | close(fd); |
| 66 | return -1; | 66 | return -1; |
| 67 | } | 67 | } |
| 68 | DEBUG("adapter index %d", ifr.ifr_ifindex); | 68 | log1("Adapter index %d", ifr.ifr_ifindex); |
| 69 | *ifindex = ifr.ifr_ifindex; | 69 | *ifindex = ifr.ifr_ifindex; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| @@ -75,7 +75,7 @@ int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t | |||
| 75 | return -1; | 75 | return -1; |
| 76 | } | 76 | } |
| 77 | memcpy(mac, ifr.ifr_hwaddr.sa_data, 6); | 77 | memcpy(mac, ifr.ifr_hwaddr.sa_data, 6); |
| 78 | DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", | 78 | log1("MAC %02x:%02x:%02x:%02x:%02x:%02x", |
| 79 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | 79 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 80 | } | 80 | } |
| 81 | 81 | ||
| @@ -90,7 +90,7 @@ int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) | |||
| 90 | int fd; | 90 | int fd; |
| 91 | struct sockaddr_in addr; | 91 | struct sockaddr_in addr; |
| 92 | 92 | ||
| 93 | DEBUG("Opening listen socket on *:%d %s", port, inf); | 93 | log1("Opening listen socket on *:%d %s", port, inf); |
| 94 | fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); | 94 | fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 95 | 95 | ||
| 96 | setsockopt_reuseaddr(fd); | 96 | setsockopt_reuseaddr(fd); |
