diff options
author | David Decotigny <ddecotig@gmail.com> | 2018-05-24 08:30:15 -0700 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-05-24 18:34:56 +0200 |
commit | 8f48fc01e9e43d16bf5860fa37252b43c76cb395 (patch) | |
tree | 8762e416ece3a9f6c80c5760f5054de9b491c5e2 | |
parent | a174c791ece5d7f2869e9efd338d91c8ea10f609 (diff) | |
download | busybox-w32-8f48fc01e9e43d16bf5860fa37252b43c76cb395.tar.gz busybox-w32-8f48fc01e9e43d16bf5860fa37252b43c76cb395.tar.bz2 busybox-w32-8f48fc01e9e43d16bf5860fa37252b43c76cb395.zip |
udhcpc6: carry along length of packet when parsing it.
This is to avoid parsing garbage past packet's actual end.
Also const-ize params to a few functions.
function old new delta
d6_run_script_no_option - 12 +12
option_to_env 791 798 +7
d6_run_script 253 255 +2
perform_d6_release 95 93 -2
udhcpc6_main 2596 2592 -4
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/2 up/down: 21/-6) Total: 15 bytes
Signed-off-by: David Decotigny <ddecotig@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/udhcp/d6_dhcpc.c | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/networking/udhcp/d6_dhcpc.c b/networking/udhcp/d6_dhcpc.c index 2ff9c5669..4dbc2b1bd 100644 --- a/networking/udhcp/d6_dhcpc.c +++ b/networking/udhcp/d6_dhcpc.c | |||
@@ -215,7 +215,8 @@ static char** new_env(void) | |||
215 | return &client6_data.env_ptr[client6_data.env_idx++]; | 215 | return &client6_data.env_ptr[client6_data.env_idx++]; |
216 | } | 216 | } |
217 | 217 | ||
218 | static char *string_option_to_env(uint8_t *option, uint8_t *option_end) | 218 | static char *string_option_to_env(const uint8_t *option, |
219 | const uint8_t *option_end) | ||
219 | { | 220 | { |
220 | const char *ptr, *name = NULL; | 221 | const char *ptr, *name = NULL; |
221 | unsigned val_len; | 222 | unsigned val_len; |
@@ -244,7 +245,7 @@ static char *string_option_to_env(uint8_t *option, uint8_t *option_end) | |||
244 | } | 245 | } |
245 | 246 | ||
246 | /* put all the parameters into the environment */ | 247 | /* put all the parameters into the environment */ |
247 | static void option_to_env(uint8_t *option, uint8_t *option_end) | 248 | static void option_to_env(const uint8_t *option, const uint8_t *option_end) |
248 | { | 249 | { |
249 | #if ENABLE_FEATURE_UDHCPC6_RFC3646 | 250 | #if ENABLE_FEATURE_UDHCPC6_RFC3646 |
250 | int addrs, option_offset; | 251 | int addrs, option_offset; |
@@ -422,7 +423,7 @@ static void option_to_env(uint8_t *option, uint8_t *option_end) | |||
422 | } | 423 | } |
423 | } | 424 | } |
424 | 425 | ||
425 | static char **fill_envp(struct d6_packet *packet) | 426 | static char **fill_envp(const uint8_t *option, const uint8_t *option_end) |
426 | { | 427 | { |
427 | char **envp, **curr; | 428 | char **envp, **curr; |
428 | 429 | ||
@@ -431,8 +432,8 @@ static char **fill_envp(struct d6_packet *packet) | |||
431 | 432 | ||
432 | *new_env() = xasprintf("interface=%s", client_config.interface); | 433 | *new_env() = xasprintf("interface=%s", client_config.interface); |
433 | 434 | ||
434 | if (packet) | 435 | if (option) |
435 | option_to_env(packet->d6_options, packet->d6_options + sizeof(packet->d6_options)); | 436 | option_to_env(option, option_end); |
436 | 437 | ||
437 | envp = curr = client6_data.env_ptr; | 438 | envp = curr = client6_data.env_ptr; |
438 | while (*curr) | 439 | while (*curr) |
@@ -442,12 +443,13 @@ static char **fill_envp(struct d6_packet *packet) | |||
442 | } | 443 | } |
443 | 444 | ||
444 | /* Call a script with a par file and env vars */ | 445 | /* Call a script with a par file and env vars */ |
445 | static void d6_run_script(struct d6_packet *packet, const char *name) | 446 | static void d6_run_script(const uint8_t *option, const uint8_t *option_end, |
447 | const char *name) | ||
446 | { | 448 | { |
447 | char **envp, **curr; | 449 | char **envp, **curr; |
448 | char *argv[3]; | 450 | char *argv[3]; |
449 | 451 | ||
450 | envp = fill_envp(packet); | 452 | envp = fill_envp(option, option_end); |
451 | 453 | ||
452 | /* call script */ | 454 | /* call script */ |
453 | log1("executing %s %s", client_config.script, name); | 455 | log1("executing %s %s", client_config.script, name); |
@@ -463,6 +465,11 @@ static void d6_run_script(struct d6_packet *packet, const char *name) | |||
463 | free(envp); | 465 | free(envp); |
464 | } | 466 | } |
465 | 467 | ||
468 | /* Call a script with a par file and no env var */ | ||
469 | static void d6_run_script_no_option(const char *name) | ||
470 | { | ||
471 | d6_run_script(NULL, NULL, name); | ||
472 | } | ||
466 | 473 | ||
467 | /*** Sending/receiving packets ***/ | 474 | /*** Sending/receiving packets ***/ |
468 | 475 | ||
@@ -1038,7 +1045,7 @@ static void perform_renew(void) | |||
1038 | state = RENEW_REQUESTED; | 1045 | state = RENEW_REQUESTED; |
1039 | break; | 1046 | break; |
1040 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ | 1047 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ |
1041 | d6_run_script(NULL, "deconfig"); | 1048 | d6_run_script_no_option("deconfig"); |
1042 | case REQUESTING: | 1049 | case REQUESTING: |
1043 | case RELEASED: | 1050 | case RELEASED: |
1044 | change_listen_mode(LISTEN_RAW); | 1051 | change_listen_mode(LISTEN_RAW); |
@@ -1067,7 +1074,7 @@ static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *ou | |||
1067 | * Users requested to be notified in all cases, even if not in one | 1074 | * Users requested to be notified in all cases, even if not in one |
1068 | * of the states above. | 1075 | * of the states above. |
1069 | */ | 1076 | */ |
1070 | d6_run_script(NULL, "deconfig"); | 1077 | d6_run_script_no_option("deconfig"); |
1071 | change_listen_mode(LISTEN_NONE); | 1078 | change_listen_mode(LISTEN_NONE); |
1072 | state = RELEASED; | 1079 | state = RELEASED; |
1073 | } | 1080 | } |
@@ -1276,7 +1283,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) | |||
1276 | udhcp_sp_setup(); | 1283 | udhcp_sp_setup(); |
1277 | 1284 | ||
1278 | state = INIT_SELECTING; | 1285 | state = INIT_SELECTING; |
1279 | d6_run_script(NULL, "deconfig"); | 1286 | d6_run_script_no_option("deconfig"); |
1280 | change_listen_mode(LISTEN_RAW); | 1287 | change_listen_mode(LISTEN_RAW); |
1281 | packet_num = 0; | 1288 | packet_num = 0; |
1282 | timeout = 0; | 1289 | timeout = 0; |
@@ -1357,7 +1364,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) | |||
1357 | continue; | 1364 | continue; |
1358 | } | 1365 | } |
1359 | leasefail: | 1366 | leasefail: |
1360 | d6_run_script(NULL, "leasefail"); | 1367 | d6_run_script_no_option("leasefail"); |
1361 | #if BB_MMU /* -b is not supported on NOMMU */ | 1368 | #if BB_MMU /* -b is not supported on NOMMU */ |
1362 | if (opt & OPT_b) { /* background if no lease */ | 1369 | if (opt & OPT_b) { /* background if no lease */ |
1363 | bb_error_msg("no lease, forking to background"); | 1370 | bb_error_msg("no lease, forking to background"); |
@@ -1431,7 +1438,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) | |||
1431 | } | 1438 | } |
1432 | /* Timed out, enter init state */ | 1439 | /* Timed out, enter init state */ |
1433 | bb_error_msg("lease lost, entering init state"); | 1440 | bb_error_msg("lease lost, entering init state"); |
1434 | d6_run_script(NULL, "deconfig"); | 1441 | d6_run_script_no_option("deconfig"); |
1435 | state = INIT_SELECTING; | 1442 | state = INIT_SELECTING; |
1436 | client_config.first_secs = 0; /* make secs field count from 0 */ | 1443 | client_config.first_secs = 0; /* make secs field count from 0 */ |
1437 | /*timeout = 0; - already is */ | 1444 | /*timeout = 0; - already is */ |
@@ -1538,9 +1545,10 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) | |||
1538 | if (option && (option->data[0] | option->data[1]) != 0) { | 1545 | if (option && (option->data[0] | option->data[1]) != 0) { |
1539 | /* return to init state */ | 1546 | /* return to init state */ |
1540 | bb_error_msg("received DHCP NAK (%u)", option->data[4]); | 1547 | bb_error_msg("received DHCP NAK (%u)", option->data[4]); |
1541 | d6_run_script(&packet, "nak"); | 1548 | d6_run_script(packet.d6_options, |
1549 | packet_end, "nak"); | ||
1542 | if (state != REQUESTING) | 1550 | if (state != REQUESTING) |
1543 | d6_run_script(NULL, "deconfig"); | 1551 | d6_run_script_no_option("deconfig"); |
1544 | change_listen_mode(LISTEN_RAW); | 1552 | change_listen_mode(LISTEN_RAW); |
1545 | sleep(3); /* avoid excessive network traffic */ | 1553 | sleep(3); /* avoid excessive network traffic */ |
1546 | state = INIT_SELECTING; | 1554 | state = INIT_SELECTING; |
@@ -1737,7 +1745,8 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) | |||
1737 | if (timeout < 0x10) | 1745 | if (timeout < 0x10) |
1738 | timeout = 0x10; | 1746 | timeout = 0x10; |
1739 | /* enter bound state */ | 1747 | /* enter bound state */ |
1740 | d6_run_script(&packet, state == REQUESTING ? "bound" : "renew"); | 1748 | d6_run_script(packet.d6_options, packet_end, |
1749 | (state == REQUESTING ? "bound" : "renew")); | ||
1741 | 1750 | ||
1742 | state = BOUND; | 1751 | state = BOUND; |
1743 | change_listen_mode(LISTEN_NONE); | 1752 | change_listen_mode(LISTEN_NONE); |