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