"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* dhcpc.c |
| 3 | * |
| 4 | * udhcp DHCP client |
| 5 | * |
| 6 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 7 | * |
| 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
| 9 | */ |
| 10 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 11 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 13 | /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ |
| 14 | #define WANT_PIDFILE 1 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 15 | #include "common.h" |
| 16 | #include "dhcpd.h" |
| 17 | #include "dhcpc.h" |
| 18 | #include "options.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 19 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 21 | static int sockfd = -1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 22 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 23 | #define LISTEN_NONE 0 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 24 | #define LISTEN_KERNEL 1 |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 25 | #define LISTEN_RAW 2 |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 26 | static smallint listen_mode; |
| 27 | |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 28 | #define INIT_SELECTING 0 |
| 29 | #define REQUESTING 1 |
| 30 | #define BOUND 2 |
| 31 | #define RENEWING 3 |
| 32 | #define REBINDING 4 |
| 33 | #define INIT_REBOOT 5 |
| 34 | #define RENEW_REQUESTED 6 |
| 35 | #define RELEASED 7 |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 36 | static smallint state; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | deabacd | 2007-09-30 17:55:43 +0000 | [diff] [blame] | 38 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 39 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 40 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 41 | /* just a little helper */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 42 | static void change_listen_mode(int new_mode) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | { |
Denys Vlasenko | 4945ed3 | 2009-06-17 11:58:11 +0200 | [diff] [blame] | 44 | log1("Entering listen mode: %s", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 45 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 46 | |
| 47 | listen_mode = new_mode; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 48 | if (sockfd >= 0) { |
| 49 | close(sockfd); |
| 50 | sockfd = -1; |
| 51 | } |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 52 | if (new_mode == LISTEN_KERNEL) |
| 53 | sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface); |
| 54 | else if (new_mode != LISTEN_NONE) |
| 55 | sockfd = udhcp_raw_socket(client_config.ifindex); |
| 56 | /* else LISTEN_NONE: sockfd stay closed */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
| 60 | /* perform a renew */ |
| 61 | static void perform_renew(void) |
| 62 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 63 | bb_info_msg("Performing a DHCP renew"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | switch (state) { |
| 65 | case BOUND: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 66 | change_listen_mode(LISTEN_KERNEL); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 67 | case RENEWING: |
| 68 | case REBINDING: |
| 69 | state = RENEW_REQUESTED; |
| 70 | break; |
| 71 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 72 | udhcp_run_script(NULL, "deconfig"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 73 | case REQUESTING: |
| 74 | case RELEASED: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 75 | change_listen_mode(LISTEN_RAW); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 76 | state = INIT_SELECTING; |
| 77 | break; |
| 78 | case INIT_SELECTING: |
| 79 | break; |
| 80 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | /* perform a release */ |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 85 | static void perform_release(uint32_t requested_ip, uint32_t server_addr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 86 | { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 87 | char buffer[sizeof("255.255.255.255")]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 88 | struct in_addr temp_addr; |
| 89 | |
| 90 | /* send release packet */ |
| 91 | if (state == BOUND || state == RENEWING || state == REBINDING) { |
| 92 | temp_addr.s_addr = server_addr; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 93 | strcpy(buffer, inet_ntoa(temp_addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 94 | temp_addr.s_addr = requested_ip; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 95 | bb_info_msg("Unicasting a release of %s to %s", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | inet_ntoa(temp_addr), buffer); |
| 97 | send_release(server_addr, requested_ip); /* unicast */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 98 | udhcp_run_script(NULL, "deconfig"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 99 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 100 | bb_info_msg("Entering released state"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 102 | change_listen_mode(LISTEN_NONE); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | state = RELEASED; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 107 | #if BB_MMU |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 108 | static void client_background(void) |
| 109 | { |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 110 | bb_daemonize(0); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 111 | logmode &= ~LOGMODE_STDIO; |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 112 | /* rewrite pidfile, as our pid is different now */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 113 | write_pidfile(client_config.pidfile); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 114 | } |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 115 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 116 | |
| 117 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 118 | static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) |
| 119 | { |
| 120 | uint8_t *storage; |
| 121 | int len = strlen(str); |
| 122 | if (len > 255) len = 255; |
| 123 | storage = xzalloc(len + extra + OPT_DATA); |
| 124 | storage[OPT_CODE] = code; |
| 125 | storage[OPT_LEN] = len + extra; |
| 126 | memcpy(storage + extra + OPT_DATA, str, len); |
| 127 | return storage; |
| 128 | } |
| 129 | |
| 130 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 131 | int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 132 | int udhcpc_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 133 | { |
| 134 | uint8_t *temp, *message; |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 135 | char *str_c, *str_V, *str_h, *str_F, *str_r; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 136 | IF_FEATURE_UDHCP_PORT(char *str_P;) |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 137 | llist_t *list_O = NULL; |
Denis Vlasenko | 68af8e7 | 2007-11-22 21:41:14 +0000 | [diff] [blame] | 138 | int tryagain_timeout = 20; |
| 139 | int discover_timeout = 3; |
| 140 | int discover_retries = 3; |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 141 | uint32_t server_addr = server_addr; /* for compiler */ |
| 142 | uint32_t requested_ip = 0; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 143 | uint32_t xid = 0; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 144 | uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 145 | int packet_num; |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 146 | int timeout; /* must be signed */ |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 147 | unsigned already_waited_sec; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 148 | unsigned opt; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 149 | int max_fd; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 150 | int retval; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 151 | struct timeval tv; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 152 | struct dhcp_packet packet; |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 153 | fd_set rfds; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 154 | |
Denys Vlasenko | f3b92d3 | 2009-06-19 12:10:38 +0200 | [diff] [blame] | 155 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 156 | static const char udhcpc_longopts[] ALIGN1 = |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 157 | "clientid\0" Required_argument "c" |
| 158 | "clientid-none\0" No_argument "C" |
| 159 | "vendorclass\0" Required_argument "V" |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 160 | "hostname\0" Required_argument "H" |
| 161 | "fqdn\0" Required_argument "F" |
| 162 | "interface\0" Required_argument "i" |
| 163 | "now\0" No_argument "n" |
| 164 | "pidfile\0" Required_argument "p" |
| 165 | "quit\0" No_argument "q" |
| 166 | "release\0" No_argument "R" |
| 167 | "request\0" Required_argument "r" |
| 168 | "script\0" Required_argument "s" |
| 169 | "timeout\0" Required_argument "T" |
| 170 | "version\0" No_argument "v" |
| 171 | "retries\0" Required_argument "t" |
| 172 | "tryagain\0" Required_argument "A" |
| 173 | "syslog\0" No_argument "S" |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 174 | "request-option\0" Required_argument "O" |
| 175 | "no-default-options\0" No_argument "o" |
| 176 | "foreground\0" No_argument "f" |
| 177 | "background\0" No_argument "b" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 178 | IF_FEATURE_UDHCPC_ARPING("arping\0" No_argument "a") |
| 179 | IF_FEATURE_UDHCP_PORT("client-port\0" Required_argument "P") |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 180 | ; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 181 | #endif |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 182 | enum { |
| 183 | OPT_c = 1 << 0, |
| 184 | OPT_C = 1 << 1, |
| 185 | OPT_V = 1 << 2, |
| 186 | OPT_H = 1 << 3, |
| 187 | OPT_h = 1 << 4, |
| 188 | OPT_F = 1 << 5, |
Denis Vlasenko | f716a6d | 2008-06-15 09:49:21 +0000 | [diff] [blame] | 189 | OPT_i = 1 << 6, |
| 190 | OPT_n = 1 << 7, |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 191 | OPT_p = 1 << 8, |
| 192 | OPT_q = 1 << 9, |
| 193 | OPT_R = 1 << 10, |
| 194 | OPT_r = 1 << 11, |
| 195 | OPT_s = 1 << 12, |
| 196 | OPT_T = 1 << 13, |
| 197 | OPT_t = 1 << 14, |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 198 | OPT_S = 1 << 15, |
| 199 | OPT_A = 1 << 16, |
| 200 | OPT_O = 1 << 17, |
| 201 | OPT_o = 1 << 18, |
| 202 | OPT_f = 1 << 19, |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 203 | /* The rest has variable bit positions, need to be clever */ |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 204 | OPTBIT_f = 19, |
| 205 | USE_FOR_MMU( OPTBIT_b,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 206 | IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,) |
| 207 | IF_FEATURE_UDHCP_PORT( OPTBIT_P,) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 208 | USE_FOR_MMU( OPT_b = 1 << OPTBIT_b,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 209 | IF_FEATURE_UDHCPC_ARPING(OPT_a = 1 << OPTBIT_a,) |
| 210 | IF_FEATURE_UDHCP_PORT( OPT_P = 1 << OPTBIT_P,) |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 213 | /* Default options. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 214 | IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) |
| 215 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 216 | client_config.interface = "eth0"; |
| 217 | client_config.script = DEFAULT_SCRIPT; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 218 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 219 | /* Parse command line */ |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 220 | /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */ |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 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 | ; |
Denys Vlasenko | f3b92d3 | 2009-06-19 12:10:38 +0200 | [diff] [blame] | 226 | IF_LONG_OPTS(applet_long_options = udhcpc_longopts;) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 227 | opt = getopt32(argv, "c:CV:H:h:F:i:np:qRr:s:T:t:SA:O:of" |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 228 | USE_FOR_MMU("b") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 229 | IF_FEATURE_UDHCPC_ARPING("a") |
| 230 | IF_FEATURE_UDHCP_PORT("P:") |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 231 | "v" |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 232 | , &str_c, &str_V, &str_h, &str_h, &str_F |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 233 | , &client_config.interface, &client_config.pidfile, &str_r /* i,p */ |
| 234 | , &client_config.script /* s */ |
| 235 | , &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */ |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 236 | , &list_O |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 237 | IF_FEATURE_UDHCP_PORT(, &str_P) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 238 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 239 | , &dhcp_verbose |
| 240 | #endif |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 241 | ); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 242 | if (opt & OPT_c) |
| 243 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 244 | if (opt & OPT_V) |
| 245 | client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0); |
Denis Vlasenko | 68af8e7 | 2007-11-22 21:41:14 +0000 | [diff] [blame] | 246 | if (opt & (OPT_h|OPT_H)) |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 247 | client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0); |
| 248 | if (opt & OPT_F) { |
| 249 | client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3); |
| 250 | /* Flags: 0000NEOS |
| 251 | S: 1 => Client requests Server to update A RR in DNS as well as PTR |
| 252 | O: 1 => Server indicates to client that DNS has been updated regardless |
Denis Vlasenko | 873b895 | 2008-06-15 05:40:08 +0000 | [diff] [blame] | 253 | E: 1 => Name data is DNS format, i.e. <4>host<6>domain<3>com<0> not "host.domain.com" |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 254 | N: 1 => Client requests Server to not update DNS |
| 255 | */ |
| 256 | client_config.fqdn[OPT_DATA + 0] = 0x1; |
| 257 | /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */ |
| 258 | /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */ |
| 259 | } |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 260 | if (opt & OPT_r) |
| 261 | requested_ip = inet_addr(str_r); |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 262 | #if ENABLE_FEATURE_UDHCP_PORT |
| 263 | if (opt & OPT_P) { |
| 264 | CLIENT_PORT = xatou16(str_P); |
| 265 | SERVER_PORT = CLIENT_PORT - 1; |
| 266 | } |
| 267 | #endif |
Denis Vlasenko | 2e4c3c4 | 2008-04-02 13:04:19 +0000 | [diff] [blame] | 268 | if (opt & OPT_o) |
| 269 | client_config.no_default_options = 1; |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 270 | while (list_O) { |
Denis Vlasenko | 18c9302 | 2008-08-27 22:29:43 +0000 | [diff] [blame] | 271 | char *optstr = llist_pop(&list_O); |
| 272 | int n = index_in_strings(dhcp_option_strings, optstr); |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 273 | if (n < 0) |
Denis Vlasenko | 18c9302 | 2008-08-27 22:29:43 +0000 | [diff] [blame] | 274 | bb_error_msg_and_die("unknown option '%s'", optstr); |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 275 | n = dhcp_options[n].code; |
| 276 | client_config.opt_mask[n >> 3] |= 1 << (n & 7); |
Denis Vlasenko | 1918368 | 2007-12-10 07:03:38 +0000 | [diff] [blame] | 277 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 278 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 279 | if (udhcp_read_interface(client_config.interface, |
| 280 | &client_config.ifindex, |
| 281 | NULL, |
| 282 | client_config.client_mac) |
| 283 | ) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 284 | return 1; |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 287 | #if !BB_MMU |
| 288 | /* on NOMMU reexec (i.e., background) early */ |
| 289 | if (!(opt & OPT_f)) { |
| 290 | bb_daemonize_or_rexec(0 /* flags */, argv); |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 291 | logmode = LOGMODE_NONE; |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 292 | } |
| 293 | #endif |
| 294 | if (opt & OPT_S) { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 295 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 296 | logmode |= LOGMODE_SYSLOG; |
| 297 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 298 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 299 | /* Make sure fd 0,1,2 are open */ |
| 300 | bb_sanitize_stdio(); |
| 301 | /* Equivalent of doing a fflush after every \n */ |
| 302 | setlinebuf(stdout); |
| 303 | |
| 304 | /* Create pidfile */ |
| 305 | write_pidfile(client_config.pidfile); |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 306 | |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 307 | /* Goes to stdout (unless NOMMU) and possibly syslog */ |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 308 | bb_info_msg("%s (v"BB_VER") started", applet_name); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 309 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 310 | /* If not set, and not suppressed, set up the default client ID */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 311 | if (!client_config.clientid && !(opt & OPT_C)) { |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 312 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 313 | client_config.clientid[OPT_DATA] = 1; |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 314 | memcpy(client_config.clientid + OPT_DATA+1, client_config.client_mac, 6); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 317 | if (!client_config.vendorclass) |
| 318 | client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 319 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 320 | /* Set up the signal pipe */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 321 | udhcp_sp_setup(); |
| 322 | |
| 323 | state = INIT_SELECTING; |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 324 | udhcp_run_script(NULL, "deconfig"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 325 | change_listen_mode(LISTEN_RAW); |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 326 | packet_num = 0; |
Denis Vlasenko | fc9e108 | 2008-05-26 17:32:35 +0000 | [diff] [blame] | 327 | timeout = 0; |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 328 | already_waited_sec = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 329 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 330 | /* Main event loop. select() waits on signal pipe and possibly |
| 331 | * on sockfd. |
| 332 | * "continue" statements in code below jump to the top of the loop. |
| 333 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 334 | for (;;) { |
Denis Vlasenko | ec64a57 | 2009-01-14 00:28:03 +0000 | [diff] [blame] | 335 | /* silence "uninitialized!" warning */ |
| 336 | unsigned timestamp_before_wait = timestamp_before_wait; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 338 | //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode); |
| 339 | |
| 340 | /* Was opening raw or udp socket here |
| 341 | * if (listen_mode != LISTEN_NONE && sockfd < 0), |
| 342 | * but on fast network renew responses return faster |
| 343 | * than we open sockets. Thus this code is moved |
| 344 | * to change_listen_mode(). Thus we open listen socket |
| 345 | * BEFORE we send renew request (see "case BOUND:"). */ |
| 346 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 347 | max_fd = udhcp_sp_fd_set(&rfds, sockfd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 348 | |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 349 | tv.tv_sec = timeout - already_waited_sec; |
Denis Vlasenko | b234291 | 2008-05-21 07:02:16 +0000 | [diff] [blame] | 350 | tv.tv_usec = 0; |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 351 | retval = 0; /* If we already timed out, fall through, else... */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 352 | if (tv.tv_sec > 0) { |
Denis Vlasenko | b234291 | 2008-05-21 07:02:16 +0000 | [diff] [blame] | 353 | timestamp_before_wait = (unsigned)monotonic_sec(); |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 354 | log1("Waiting on select..."); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 355 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
Denis Vlasenko | b234291 | 2008-05-21 07:02:16 +0000 | [diff] [blame] | 356 | if (retval < 0) { |
| 357 | /* EINTR? A signal was caught, don't panic */ |
| 358 | if (errno == EINTR) |
| 359 | continue; |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 360 | /* Else: an error occured, panic! */ |
| 361 | bb_perror_msg_and_die("select"); |
| 362 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* If timeout dropped to zero, time to become active: |
| 366 | * resend discover/renew/whatever |
| 367 | */ |
| 368 | if (retval == 0) { |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 369 | /* We will restart the wait in any case */ |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 370 | already_waited_sec = 0; |
| 371 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 372 | switch (state) { |
| 373 | case INIT_SELECTING: |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 374 | if (packet_num < discover_retries) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 375 | if (packet_num == 0) |
| 376 | xid = random_xid(); |
| 377 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 378 | send_discover(xid, requested_ip); /* broadcast */ |
| 379 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 380 | timeout = discover_timeout; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 381 | packet_num++; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 382 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 383 | } |
Denis Vlasenko | cdb0b65 | 2008-09-26 09:34:15 +0000 | [diff] [blame] | 384 | leasefail: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 385 | udhcp_run_script(NULL, "leasefail"); |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 386 | #if BB_MMU /* -b is not supported on NOMMU */ |
| 387 | if (opt & OPT_b) { /* background if no lease */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 388 | bb_info_msg("No lease, forking to background"); |
| 389 | client_background(); |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 390 | /* do not background again! */ |
| 391 | opt = ((opt & ~OPT_b) | OPT_f); |
| 392 | } else |
| 393 | #endif |
| 394 | if (opt & OPT_n) { /* abort if no lease */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 395 | bb_info_msg("No lease, failing"); |
| 396 | retval = 1; |
| 397 | goto ret; |
| 398 | } |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 399 | /* wait before trying again */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 400 | timeout = tryagain_timeout; |
| 401 | packet_num = 0; |
| 402 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 403 | case RENEW_REQUESTED: |
| 404 | case REQUESTING: |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 405 | if (packet_num < discover_retries) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 406 | /* send request packet */ |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 407 | if (state == RENEW_REQUESTED) /* unicast */ |
| 408 | send_renew(xid, server_addr, requested_ip); |
| 409 | else /* broadcast */ |
Denis Vlasenko | c321b51 | 2008-09-26 16:29:12 +0000 | [diff] [blame] | 410 | send_select(xid, server_addr, requested_ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 411 | |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 412 | timeout = discover_timeout; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 413 | packet_num++; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 414 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 415 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 416 | /* timed out, go back to init state */ |
| 417 | if (state == RENEW_REQUESTED) |
| 418 | udhcp_run_script(NULL, "deconfig"); |
| 419 | change_listen_mode(LISTEN_RAW); |
Denis Vlasenko | cdb0b65 | 2008-09-26 09:34:15 +0000 | [diff] [blame] | 420 | /* "discover...select...discover..." loops |
Denis Vlasenko | 7d9399e | 2008-09-26 22:21:03 +0000 | [diff] [blame] | 421 | * were seen in the wild. Treat them similarly |
Denis Vlasenko | cdb0b65 | 2008-09-26 09:34:15 +0000 | [diff] [blame] | 422 | * to "no response to discover" case */ |
Denis Vlasenko | 7d9399e | 2008-09-26 22:21:03 +0000 | [diff] [blame] | 423 | if (state == REQUESTING) { |
| 424 | state = INIT_SELECTING; |
Denis Vlasenko | cdb0b65 | 2008-09-26 09:34:15 +0000 | [diff] [blame] | 425 | goto leasefail; |
Denis Vlasenko | 7d9399e | 2008-09-26 22:21:03 +0000 | [diff] [blame] | 426 | } |
| 427 | state = INIT_SELECTING; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 428 | timeout = 0; |
| 429 | packet_num = 0; |
| 430 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 431 | case BOUND: |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 432 | /* Half of the lease passed, time to enter renewing state */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 433 | change_listen_mode(LISTEN_KERNEL); |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 434 | log1("Entering renew state"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 435 | state = RENEWING; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 436 | /* fall right through */ |
| 437 | case RENEWING: |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 438 | if (timeout > 60) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 439 | /* send a request packet */ |
| 440 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 441 | timeout >>= 1; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 442 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 443 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 444 | /* Timed out, enter rebinding state */ |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 445 | log1("Entering rebinding state"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 446 | state = REBINDING; |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 447 | /* fall right through */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 448 | case REBINDING: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 449 | /* Lease is *really* about to run out, |
| 450 | * try to find DHCP server using broadcast */ |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 451 | if (timeout > 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 452 | /* send a request packet */ |
Denis Vlasenko | ec64a57 | 2009-01-14 00:28:03 +0000 | [diff] [blame] | 453 | send_renew(xid, 0 /*INADDR_ANY*/, requested_ip); /* broadcast */ |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 454 | timeout >>= 1; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 455 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 456 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 457 | /* Timed out, enter init state */ |
| 458 | bb_info_msg("Lease lost, entering init state"); |
| 459 | udhcp_run_script(NULL, "deconfig"); |
| 460 | change_listen_mode(LISTEN_RAW); |
| 461 | state = INIT_SELECTING; |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 462 | /*timeout = 0; - already is */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 463 | packet_num = 0; |
| 464 | continue; |
| 465 | /* case RELEASED: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 466 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 467 | /* yah, I know, *you* say it would never happen */ |
| 468 | timeout = INT_MAX; |
| 469 | continue; /* back to main loop */ |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 470 | } /* if select timed out */ |
| 471 | |
| 472 | /* select() didn't timeout, something happened */ |
| 473 | |
| 474 | /* Is it a signal? */ |
| 475 | /* note: udhcp_sp_read checks FD_ISSET before reading */ |
| 476 | switch (udhcp_sp_read(&rfds)) { |
| 477 | case SIGUSR1: |
| 478 | perform_renew(); |
| 479 | /* Start things over */ |
| 480 | packet_num = 0; |
| 481 | /* Kill any timeouts because the user wants this to hurry along */ |
| 482 | timeout = 0; |
| 483 | continue; |
| 484 | case SIGUSR2: |
| 485 | perform_release(requested_ip, server_addr); |
| 486 | timeout = INT_MAX; |
| 487 | continue; |
| 488 | case SIGTERM: |
| 489 | bb_info_msg("Received SIGTERM"); |
| 490 | if (opt & OPT_R) /* release on quit */ |
| 491 | perform_release(requested_ip, server_addr); |
| 492 | goto ret0; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 495 | /* Is it a packet? */ |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 496 | if (listen_mode == LISTEN_NONE || !FD_ISSET(sockfd, &rfds)) |
| 497 | continue; /* no */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 498 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 499 | { |
| 500 | int len; |
| 501 | |
| 502 | /* A packet is ready, read it */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 503 | if (listen_mode == LISTEN_KERNEL) |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 504 | len = udhcp_recv_kernel_packet(&packet, sockfd); |
Denis Vlasenko | b76b9a4 | 2008-01-25 22:46:34 +0000 | [diff] [blame] | 505 | else |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 506 | len = udhcp_recv_raw_packet(&packet, sockfd); |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 507 | if (len == -1) { |
| 508 | /* Error is severe, reopen socket */ |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 509 | bb_info_msg("Read error: %s, reopening socket", strerror(errno)); |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 510 | sleep(discover_timeout); /* 3 seconds by default */ |
| 511 | change_listen_mode(listen_mode); /* just close and reopen */ |
| 512 | } |
Denis Vlasenko | b234291 | 2008-05-21 07:02:16 +0000 | [diff] [blame] | 513 | /* If this packet will turn out to be unrelated/bogus, |
| 514 | * we will go back and wait for next one. |
| 515 | * Be sure timeout is properly decreased. */ |
Denis Vlasenko | 19903f0 | 2008-05-21 07:03:03 +0000 | [diff] [blame] | 516 | already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait; |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 517 | if (len < 0) |
| 518 | continue; |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 519 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 520 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 521 | if (packet.xid != xid) { |
| 522 | log1("xid %x (our is %x), ignoring packet", |
| 523 | (unsigned)packet.xid, (unsigned)xid); |
| 524 | continue; |
| 525 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 526 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 527 | /* Ignore packets that aren't for us */ |
| 528 | if (packet.hlen != 6 |
| 529 | || memcmp(packet.chaddr, client_config.client_mac, 6) |
| 530 | ) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 531 | //FIXME: need to also check that last 10 bytes are zero |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 532 | log1("chaddr does not match, ignoring packet"); // log2? |
| 533 | continue; |
| 534 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 535 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 536 | message = get_option(&packet, DHCP_MESSAGE_TYPE); |
| 537 | if (message == NULL) { |
| 538 | bb_error_msg("no message type option, ignoring packet"); |
| 539 | continue; |
| 540 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 541 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 542 | switch (state) { |
| 543 | case INIT_SELECTING: |
| 544 | /* Must be a DHCPOFFER to one of our xid's */ |
| 545 | if (*message == DHCPOFFER) { |
| 546 | /* TODO: why we don't just fetch server's IP from IP header? */ |
| 547 | temp = get_option(&packet, DHCP_SERVER_ID); |
| 548 | if (!temp) { |
| 549 | bb_error_msg("no server ID in message"); |
| 550 | continue; |
| 551 | /* still selecting - this server looks bad */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 552 | } |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 553 | /* it IS unaligned sometimes, don't "optimize" */ |
| 554 | move_from_unaligned32(server_addr, temp); |
| 555 | xid = packet.xid; |
| 556 | requested_ip = packet.yiaddr; |
| 557 | |
| 558 | /* enter requesting state */ |
| 559 | state = REQUESTING; |
| 560 | timeout = 0; |
| 561 | packet_num = 0; |
| 562 | already_waited_sec = 0; |
| 563 | } |
| 564 | continue; |
| 565 | case RENEW_REQUESTED: |
| 566 | case REQUESTING: |
| 567 | case RENEWING: |
| 568 | case REBINDING: |
| 569 | if (*message == DHCPACK) { |
| 570 | temp = get_option(&packet, DHCP_LEASE_TIME); |
| 571 | if (!temp) { |
| 572 | bb_error_msg("no lease time with ACK, using 1 hour lease"); |
| 573 | lease_seconds = 60 * 60; |
| 574 | } else { |
| 575 | /* it IS unaligned sometimes, don't "optimize" */ |
| 576 | move_from_unaligned32(lease_seconds, temp); |
| 577 | lease_seconds = ntohl(lease_seconds); |
| 578 | lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */ |
| 579 | if (lease_seconds < 10) /* and not too small */ |
| 580 | lease_seconds = 10; |
| 581 | } |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 582 | #if ENABLE_FEATURE_UDHCPC_ARPING |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 583 | if (opt & OPT_a) { |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 584 | /* RFC 2131 3.1 paragraph 5: |
| 585 | * "The client receives the DHCPACK message with configuration |
| 586 | * parameters. The client SHOULD perform a final check on the |
| 587 | * parameters (e.g., ARP for allocated network address), and notes |
| 588 | * the duration of the lease specified in the DHCPACK message. At this |
| 589 | * point, the client is configured. If the client detects that the |
| 590 | * address is already in use (e.g., through the use of ARP), |
| 591 | * the client MUST send a DHCPDECLINE message to the server and restarts |
| 592 | * the configuration process..." */ |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 593 | if (!arpping(packet.yiaddr, |
| 594 | NULL, |
| 595 | (uint32_t) 0, |
| 596 | client_config.client_mac, |
| 597 | client_config.interface) |
| 598 | ) { |
| 599 | bb_info_msg("Offered address is in use " |
| 600 | "(got ARP reply), declining"); |
| 601 | send_decline(xid, server_addr, packet.yiaddr); |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 602 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 603 | if (state != REQUESTING) |
| 604 | udhcp_run_script(NULL, "deconfig"); |
| 605 | change_listen_mode(LISTEN_RAW); |
| 606 | state = INIT_SELECTING; |
| 607 | requested_ip = 0; |
| 608 | timeout = tryagain_timeout; |
| 609 | packet_num = 0; |
| 610 | already_waited_sec = 0; |
| 611 | continue; /* back to main loop */ |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 612 | } |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 613 | } |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 614 | #endif |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 615 | /* enter bound state */ |
| 616 | timeout = lease_seconds / 2; |
| 617 | { |
| 618 | struct in_addr temp_addr; |
| 619 | temp_addr.s_addr = packet.yiaddr; |
| 620 | bb_info_msg("Lease of %s obtained, lease time %u", |
| 621 | inet_ntoa(temp_addr), (unsigned)lease_seconds); |
| 622 | } |
| 623 | requested_ip = packet.yiaddr; |
| 624 | udhcp_run_script(&packet, |
| 625 | ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 626 | |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 627 | state = BOUND; |
| 628 | change_listen_mode(LISTEN_NONE); |
| 629 | if (opt & OPT_q) { /* quit after lease */ |
| 630 | if (opt & OPT_R) /* release on quit */ |
| 631 | perform_release(requested_ip, server_addr); |
| 632 | goto ret0; |
| 633 | } |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 634 | #if BB_MMU /* NOMMU case backgrounded earlier */ |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 635 | if (!(opt & OPT_f)) { |
| 636 | client_background(); |
| 637 | /* do not background again! */ |
| 638 | opt = ((opt & ~OPT_b) | OPT_f); |
| 639 | } |
Denis Vlasenko | 21765fa | 2008-06-13 20:44:05 +0000 | [diff] [blame] | 640 | #endif |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 641 | already_waited_sec = 0; |
| 642 | continue; /* back to main loop */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 643 | } |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 644 | if (*message == DHCPNAK) { |
| 645 | /* return to init state */ |
| 646 | bb_info_msg("Received DHCP NAK"); |
| 647 | udhcp_run_script(&packet, "nak"); |
| 648 | if (state != REQUESTING) |
| 649 | udhcp_run_script(NULL, "deconfig"); |
| 650 | change_listen_mode(LISTEN_RAW); |
| 651 | sleep(3); /* avoid excessive network traffic */ |
| 652 | state = INIT_SELECTING; |
| 653 | requested_ip = 0; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 654 | timeout = 0; |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 655 | packet_num = 0; |
| 656 | already_waited_sec = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 657 | } |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 658 | continue; |
| 659 | /* case BOUND: - ignore all packets */ |
| 660 | /* case RELEASED: - ignore all packets */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 661 | } |
Denys Vlasenko | 4248c33 | 2009-06-26 23:23:16 +0200 | [diff] [blame] | 662 | /* back to main loop */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame] | 663 | } /* for (;;) - main loop ends */ |
| 664 | |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 665 | ret0: |
| 666 | retval = 0; |
| 667 | ret: |
Denis Vlasenko | b234291 | 2008-05-21 07:02:16 +0000 | [diff] [blame] | 668 | /*if (client_config.pidfile) - remove_pidfile has its own check */ |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 669 | remove_pidfile(client_config.pidfile); |
| 670 | return retval; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 671 | } |