"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 3 | * udhcp server |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> |
| 5 | * Chris Trew <ctrew@moreton.com.au> |
| 6 | * |
| 7 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 8 | * |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 22 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | |
| 24 | //usage:#define udhcpd_trivial_usage |
Denys Vlasenko | 7b5d5c1 | 2013-03-14 02:18:52 +0100 | [diff] [blame] | 25 | //usage: "[-fS] [-I ADDR]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 26 | //usage:#define udhcpd_full_usage "\n\n" |
| 27 | //usage: "DHCP server\n" |
| 28 | //usage: "\n -f Run in foreground" |
| 29 | //usage: "\n -S Log to syslog too" |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 30 | //usage: "\n -I ADDR Local address" |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 31 | //usage: "\n -a MSEC Timeout for ARP ping (default 2000)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | //usage: IF_FEATURE_UDHCP_PORT( |
| 33 | //usage: "\n -P N Use port N (default 67)" |
| 34 | //usage: ) |
| 35 | |
Denys Vlasenko | 2bf2931 | 2016-10-04 00:37:50 +0200 | [diff] [blame^] | 36 | #include <netinet/ether.h> |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 37 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 38 | #include "common.h" |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 39 | #include "dhcpc.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 40 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | 2bf2931 | 2016-10-04 00:37:50 +0200 | [diff] [blame^] | 42 | /* on these functions, make sure your datatype matches */ |
| 43 | static int FAST_FUNC read_str(const char *line, void *arg) |
| 44 | { |
| 45 | char **dest = arg; |
| 46 | |
| 47 | free(*dest); |
| 48 | *dest = xstrdup(line); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | static int FAST_FUNC read_u32(const char *line, void *arg) |
| 53 | { |
| 54 | *(uint32_t*)arg = bb_strtou32(line, NULL, 10); |
| 55 | return errno == 0; |
| 56 | } |
| 57 | |
| 58 | static int FAST_FUNC read_staticlease(const char *const_line, void *arg) |
| 59 | { |
| 60 | char *line; |
| 61 | char *mac_string; |
| 62 | char *ip_string; |
| 63 | struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */ |
| 64 | uint32_t nip; |
| 65 | |
| 66 | /* Read mac */ |
| 67 | line = (char *) const_line; |
| 68 | mac_string = strtok_r(line, " \t", &line); |
| 69 | if (!mac_string || !ether_aton_r(mac_string, &mac_bytes)) |
| 70 | return 0; |
| 71 | |
| 72 | /* Read ip */ |
| 73 | ip_string = strtok_r(NULL, " \t", &line); |
| 74 | if (!ip_string || !udhcp_str2nip(ip_string, &nip)) |
| 75 | return 0; |
| 76 | |
| 77 | add_static_lease(arg, (uint8_t*) &mac_bytes, nip); |
| 78 | |
| 79 | log_static_leases(arg); |
| 80 | |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | struct config_keyword { |
| 85 | const char *keyword; |
| 86 | int (*handler)(const char *line, void *var) FAST_FUNC; |
| 87 | unsigned ofs; |
| 88 | const char *def; |
| 89 | }; |
| 90 | |
| 91 | #define OFS(field) offsetof(struct server_config_t, field) |
| 92 | |
| 93 | static const struct config_keyword keywords[] = { |
| 94 | /* keyword handler variable address default */ |
| 95 | {"start" , udhcp_str2nip , OFS(start_ip ), "192.168.0.20"}, |
| 96 | {"end" , udhcp_str2nip , OFS(end_ip ), "192.168.0.254"}, |
| 97 | {"interface" , read_str , OFS(interface ), "eth0"}, |
| 98 | /* Avoid "max_leases value not sane" warning by setting default |
| 99 | * to default_end_ip - default_start_ip + 1: */ |
| 100 | {"max_leases" , read_u32 , OFS(max_leases ), "235"}, |
| 101 | {"auto_time" , read_u32 , OFS(auto_time ), "7200"}, |
| 102 | {"decline_time" , read_u32 , OFS(decline_time ), "3600"}, |
| 103 | {"conflict_time", read_u32 , OFS(conflict_time), "3600"}, |
| 104 | {"offer_time" , read_u32 , OFS(offer_time ), "60"}, |
| 105 | {"min_lease" , read_u32 , OFS(min_lease_sec), "60"}, |
| 106 | {"lease_file" , read_str , OFS(lease_file ), LEASES_FILE}, |
| 107 | {"pidfile" , read_str , OFS(pidfile ), "/var/run/udhcpd.pid"}, |
| 108 | {"siaddr" , udhcp_str2nip , OFS(siaddr_nip ), "0.0.0.0"}, |
| 109 | /* keywords with no defaults must be last! */ |
| 110 | {"option" , udhcp_str2optset, OFS(options ), ""}, |
| 111 | {"opt" , udhcp_str2optset, OFS(options ), ""}, |
| 112 | {"notify_file" , read_str , OFS(notify_file ), NULL}, |
| 113 | {"sname" , read_str , OFS(sname ), NULL}, |
| 114 | {"boot_file" , read_str , OFS(boot_file ), NULL}, |
| 115 | {"static_lease" , read_staticlease, OFS(static_leases), ""}, |
| 116 | }; |
| 117 | enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; |
| 118 | |
| 119 | static NOINLINE void read_config(const char *file) |
| 120 | { |
| 121 | parser_t *parser; |
| 122 | const struct config_keyword *k; |
| 123 | unsigned i; |
| 124 | char *token[2]; |
| 125 | |
| 126 | for (i = 0; i < KWS_WITH_DEFAULTS; i++) |
| 127 | keywords[i].handler(keywords[i].def, (char*)&server_config + keywords[i].ofs); |
| 128 | |
| 129 | parser = config_open(file); |
| 130 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
| 131 | for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { |
| 132 | if (strcasecmp(token[0], k->keyword) == 0) { |
| 133 | if (!k->handler(token[1], (char*)&server_config + k->ofs)) { |
| 134 | bb_error_msg("can't parse line %u in %s", |
| 135 | parser->lineno, file); |
| 136 | /* reset back to the default value */ |
| 137 | k->handler(k->def, (char*)&server_config + k->ofs); |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | config_close(parser); |
| 144 | |
| 145 | server_config.start_ip = ntohl(server_config.start_ip); |
| 146 | server_config.end_ip = ntohl(server_config.end_ip); |
| 147 | } |
| 148 | |
| 149 | static void write_leases(void) |
| 150 | { |
| 151 | int fd; |
| 152 | unsigned i; |
| 153 | leasetime_t curr; |
| 154 | int64_t written_at; |
| 155 | |
| 156 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
| 157 | if (fd < 0) |
| 158 | return; |
| 159 | |
| 160 | curr = written_at = time(NULL); |
| 161 | |
| 162 | written_at = SWAP_BE64(written_at); |
| 163 | full_write(fd, &written_at, sizeof(written_at)); |
| 164 | |
| 165 | for (i = 0; i < server_config.max_leases; i++) { |
| 166 | leasetime_t tmp_time; |
| 167 | |
| 168 | if (g_leases[i].lease_nip == 0) |
| 169 | continue; |
| 170 | |
| 171 | /* Screw with the time in the struct, for easier writing */ |
| 172 | tmp_time = g_leases[i].expires; |
| 173 | |
| 174 | g_leases[i].expires -= curr; |
| 175 | if ((signed_leasetime_t) g_leases[i].expires < 0) |
| 176 | g_leases[i].expires = 0; |
| 177 | g_leases[i].expires = htonl(g_leases[i].expires); |
| 178 | |
| 179 | /* No error check. If the file gets truncated, |
| 180 | * we lose some leases on restart. Oh well. */ |
| 181 | full_write(fd, &g_leases[i], sizeof(g_leases[i])); |
| 182 | |
| 183 | /* Then restore it when done */ |
| 184 | g_leases[i].expires = tmp_time; |
| 185 | } |
| 186 | close(fd); |
| 187 | |
| 188 | if (server_config.notify_file) { |
| 189 | char *argv[3]; |
| 190 | argv[0] = server_config.notify_file; |
| 191 | argv[1] = server_config.lease_file; |
| 192 | argv[2] = NULL; |
| 193 | spawn_and_wait(argv); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static NOINLINE void read_leases(const char *file) |
| 198 | { |
| 199 | struct dyn_lease lease; |
| 200 | int64_t written_at, time_passed; |
| 201 | int fd; |
| 202 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 203 | unsigned i = 0; |
| 204 | #endif |
| 205 | |
| 206 | fd = open_or_warn(file, O_RDONLY); |
| 207 | if (fd < 0) |
| 208 | return; |
| 209 | |
| 210 | if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) |
| 211 | goto ret; |
| 212 | written_at = SWAP_BE64(written_at); |
| 213 | |
| 214 | time_passed = time(NULL) - written_at; |
| 215 | /* Strange written_at, or lease file from old version of udhcpd |
| 216 | * which had no "written_at" field? */ |
| 217 | if ((uint64_t)time_passed > 12 * 60 * 60) |
| 218 | goto ret; |
| 219 | |
| 220 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
| 221 | uint32_t y = ntohl(lease.lease_nip); |
| 222 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
| 223 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
| 224 | uint32_t static_nip; |
| 225 | |
| 226 | if (expires <= 0) |
| 227 | /* We keep expired leases: add_lease() will add |
| 228 | * a lease with 0 seconds remaining. |
| 229 | * Fewer IP address changes this way for mass reboot scenario. |
| 230 | */ |
| 231 | expires = 0; |
| 232 | |
| 233 | /* Check if there is a different static lease for this IP or MAC */ |
| 234 | static_nip = get_static_nip_by_mac(server_config.static_leases, lease.lease_mac); |
| 235 | if (static_nip) { |
| 236 | /* NB: we do not add lease even if static_nip == lease.lease_nip. |
| 237 | */ |
| 238 | continue; |
| 239 | } |
| 240 | if (is_nip_reserved(server_config.static_leases, lease.lease_nip)) |
| 241 | continue; |
| 242 | |
| 243 | /* NB: add_lease takes "relative time", IOW, |
| 244 | * lease duration, not lease deadline. */ |
| 245 | if (add_lease(lease.lease_mac, lease.lease_nip, |
| 246 | expires, |
| 247 | lease.hostname, sizeof(lease.hostname) |
| 248 | ) == 0 |
| 249 | ) { |
| 250 | bb_error_msg("too many leases while loading %s", file); |
| 251 | break; |
| 252 | } |
| 253 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 254 | i++; |
| 255 | #endif |
| 256 | } |
| 257 | } |
| 258 | log1("read %d leases", i); |
| 259 | ret: |
| 260 | close(fd); |
| 261 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 262 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 263 | /* Send a packet to a specific mac address and ip address by creating our own ip packet */ |
| 264 | static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 265 | { |
| 266 | const uint8_t *chaddr; |
| 267 | uint32_t ciaddr; |
| 268 | |
| 269 | // Was: |
| 270 | //if (force_broadcast) { /* broadcast */ } |
| 271 | //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ } |
| 272 | //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } |
| 273 | //else { /* unicast to dhcp_pkt->yiaddr */ } |
| 274 | // But this is wrong: yiaddr is _our_ idea what client's IP is |
| 275 | // (for example, from lease file). Client may not know that, |
| 276 | // and may not have UDP socket listening on that IP! |
| 277 | // We should never unicast to dhcp_pkt->yiaddr! |
| 278 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, |
| 279 | // and can be used. |
| 280 | |
| 281 | if (force_broadcast |
| 282 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 283 | || dhcp_pkt->ciaddr == 0 |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 284 | ) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 285 | log1("broadcasting packet to client"); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 286 | ciaddr = INADDR_BROADCAST; |
| 287 | chaddr = MAC_BCAST_ADDR; |
| 288 | } else { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 289 | log1("unicasting packet to client ciaddr"); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 290 | ciaddr = dhcp_pkt->ciaddr; |
| 291 | chaddr = dhcp_pkt->chaddr; |
| 292 | } |
| 293 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 294 | udhcp_send_raw_packet(dhcp_pkt, |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 295 | /*src*/ server_config.server_nip, SERVER_PORT, |
| 296 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
| 297 | server_config.ifindex); |
| 298 | } |
| 299 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 300 | /* Send a packet to gateway_nip using the kernel ip stack */ |
| 301 | static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt) |
| 302 | { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 303 | log1("forwarding packet to relay"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 304 | |
| 305 | udhcp_send_kernel_packet(dhcp_pkt, |
| 306 | server_config.server_nip, SERVER_PORT, |
| 307 | dhcp_pkt->gateway_nip, SERVER_PORT); |
| 308 | } |
| 309 | |
| 310 | static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 311 | { |
| 312 | if (dhcp_pkt->gateway_nip) |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 313 | send_packet_to_relay(dhcp_pkt); |
| 314 | else |
| 315 | send_packet_to_client(dhcp_pkt, force_broadcast); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) |
| 319 | { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 320 | /* Sets op, htype, hlen, cookie fields |
| 321 | * and adds DHCP_MESSAGE_TYPE option */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 322 | udhcp_init_header(packet, type); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 323 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 324 | packet->xid = oldpacket->xid; |
| 325 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); |
| 326 | packet->flags = oldpacket->flags; |
| 327 | packet->gateway_nip = oldpacket->gateway_nip; |
| 328 | packet->ciaddr = oldpacket->ciaddr; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 329 | udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 332 | /* Fill options field, siaddr_nip, and sname and boot_file fields. |
| 333 | * TODO: teach this code to use overload option. |
| 334 | */ |
| 335 | static void add_server_options(struct dhcp_packet *packet) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 336 | { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 337 | struct option_set *curr = server_config.options; |
| 338 | |
| 339 | while (curr) { |
| 340 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 341 | udhcp_add_binary_option(packet, curr->data); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 342 | curr = curr->next; |
| 343 | } |
| 344 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 345 | packet->siaddr_nip = server_config.siaddr_nip; |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 346 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 347 | if (server_config.sname) |
| 348 | strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); |
| 349 | if (server_config.boot_file) |
| 350 | strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1); |
| 351 | } |
| 352 | |
| 353 | static uint32_t select_lease_time(struct dhcp_packet *packet) |
| 354 | { |
| 355 | uint32_t lease_time_sec = server_config.max_lease_sec; |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 356 | uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 357 | if (lease_time_opt) { |
| 358 | move_from_unaligned32(lease_time_sec, lease_time_opt); |
| 359 | lease_time_sec = ntohl(lease_time_sec); |
| 360 | if (lease_time_sec > server_config.max_lease_sec) |
| 361 | lease_time_sec = server_config.max_lease_sec; |
| 362 | if (lease_time_sec < server_config.min_lease_sec) |
| 363 | lease_time_sec = server_config.min_lease_sec; |
| 364 | } |
| 365 | return lease_time_sec; |
| 366 | } |
| 367 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 368 | /* We got a DHCP DISCOVER. Send an OFFER. */ |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 369 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 370 | static NOINLINE void send_offer(struct dhcp_packet *oldpacket, |
| 371 | uint32_t static_lease_nip, |
| 372 | struct dyn_lease *lease, |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 373 | uint8_t *requested_ip_opt, |
| 374 | unsigned arpping_ms) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 375 | { |
| 376 | struct dhcp_packet packet; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 377 | uint32_t lease_time_sec; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 378 | struct in_addr addr; |
| 379 | |
| 380 | init_packet(&packet, oldpacket, DHCPOFFER); |
| 381 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 382 | /* If it is a static lease, use its IP */ |
| 383 | packet.yiaddr = static_lease_nip; |
| 384 | /* Else: */ |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 385 | if (!static_lease_nip) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 386 | /* We have no static lease for client's chaddr */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 387 | uint32_t req_nip; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 388 | const char *p_host_name; |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 389 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 390 | if (lease) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 391 | /* We have a dynamic lease for client's chaddr. |
| 392 | * Reuse its IP (even if lease is expired). |
| 393 | * Note that we ignore requested IP in this case. |
| 394 | */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 395 | packet.yiaddr = lease->lease_nip; |
| 396 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 397 | /* Or: if client has requested an IP */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 398 | else if (requested_ip_opt != NULL |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 399 | /* (read IP) */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 400 | && (move_from_unaligned32(req_nip, requested_ip_opt), 1) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 401 | /* and the IP is in the lease range */ |
| 402 | && ntohl(req_nip) >= server_config.start_ip |
| 403 | && ntohl(req_nip) <= server_config.end_ip |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 404 | /* and */ |
| 405 | && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */ |
| 406 | || is_expired_lease(lease) /* or is taken, but expired */ |
| 407 | ) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 408 | ) { |
| 409 | packet.yiaddr = req_nip; |
| 410 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 411 | else { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 412 | /* Otherwise, find a free IP */ |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 413 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | if (!packet.yiaddr) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 417 | bb_error_msg("no free IP addresses. OFFER abandoned"); |
| 418 | return; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 419 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 420 | /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 421 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 422 | lease = add_lease(packet.chaddr, packet.yiaddr, |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 423 | server_config.offer_time, |
| 424 | p_host_name, |
| 425 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 426 | ); |
| 427 | if (!lease) { |
| 428 | bb_error_msg("no free IP addresses. OFFER abandoned"); |
| 429 | return; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 430 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 433 | lease_time_sec = select_lease_time(oldpacket); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 434 | udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 435 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 436 | |
| 437 | addr.s_addr = packet.yiaddr; |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 438 | bb_error_msg("sending OFFER of %s", inet_ntoa(addr)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 439 | /* send_packet emits error message itself if it detects failure */ |
| 440 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 443 | /* NOINLINE: limit stack usage in caller */ |
| 444 | static NOINLINE void send_NAK(struct dhcp_packet *oldpacket) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 445 | { |
| 446 | struct dhcp_packet packet; |
| 447 | |
| 448 | init_packet(&packet, oldpacket, DHCPNAK); |
| 449 | |
Denys Vlasenko | 16efe19 | 2016-03-30 18:44:52 +0200 | [diff] [blame] | 450 | log1("sending %s", "NAK"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 451 | send_packet(&packet, /*force_bcast:*/ 1); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 454 | /* NOINLINE: limit stack usage in caller */ |
| 455 | static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 456 | { |
| 457 | struct dhcp_packet packet; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 458 | uint32_t lease_time_sec; |
| 459 | struct in_addr addr; |
| 460 | const char *p_host_name; |
| 461 | |
| 462 | init_packet(&packet, oldpacket, DHCPACK); |
| 463 | packet.yiaddr = yiaddr; |
| 464 | |
| 465 | lease_time_sec = select_lease_time(oldpacket); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 466 | udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 467 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 468 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 469 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 470 | addr.s_addr = yiaddr; |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 471 | bb_error_msg("sending ACK to %s", inet_ntoa(addr)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 472 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 473 | |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 474 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 475 | add_lease(packet.chaddr, packet.yiaddr, |
| 476 | lease_time_sec, |
| 477 | p_host_name, |
| 478 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 |
| 479 | ); |
| 480 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
| 481 | /* rewrite the file with leases at every new acceptance */ |
| 482 | write_leases(); |
| 483 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 486 | /* NOINLINE: limit stack usage in caller */ |
| 487 | static NOINLINE void send_inform(struct dhcp_packet *oldpacket) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 488 | { |
| 489 | struct dhcp_packet packet; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 490 | |
Denys Vlasenko | f8fcc18 | 2010-04-04 22:36:34 +0200 | [diff] [blame] | 491 | /* "If a client has obtained a network address through some other means |
| 492 | * (e.g., manual configuration), it may use a DHCPINFORM request message |
| 493 | * to obtain other local configuration parameters. Servers receiving a |
| 494 | * DHCPINFORM message construct a DHCPACK message with any local |
| 495 | * configuration parameters appropriate for the client without: |
| 496 | * allocating a new address, checking for an existing binding, filling |
| 497 | * in 'yiaddr' or including lease time parameters. The servers SHOULD |
| 498 | * unicast the DHCPACK reply to the address given in the 'ciaddr' field |
| 499 | * of the DHCPINFORM message. |
| 500 | * ... |
| 501 | * The server responds to a DHCPINFORM message by sending a DHCPACK |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 502 | * message directly to the address given in the 'ciaddr' field |
| 503 | * of the DHCPINFORM message. The server MUST NOT send a lease |
| 504 | * expiration time to the client and SHOULD NOT fill in 'yiaddr'." |
| 505 | */ |
Denys Vlasenko | f8fcc18 | 2010-04-04 22:36:34 +0200 | [diff] [blame] | 506 | //TODO: do a few sanity checks: is ciaddr set? |
| 507 | //Better yet: is ciaddr == IP source addr? |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 508 | init_packet(&packet, oldpacket, DHCPACK); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 509 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 510 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 511 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 514 | /* globals */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 515 | struct dyn_lease *g_leases; |
Denis Vlasenko | deabacd | 2007-09-30 17:55:43 +0000 | [diff] [blame] | 516 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 517 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 518 | int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 519 | int udhcpd_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 520 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 521 | int server_socket = -1, retval, max_sock; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 522 | uint8_t *state; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 523 | unsigned timeout_end; |
| 524 | unsigned num_ips; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 525 | unsigned opt; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 526 | struct option_set *option; |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 527 | char *str_I = str_I; |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 528 | const char *str_a = "2000"; |
| 529 | unsigned arpping_ms; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 530 | IF_FEATURE_UDHCP_PORT(char *str_P;) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 531 | |
Denys Vlasenko | df70a43 | 2016-04-21 18:54:36 +0200 | [diff] [blame] | 532 | setup_common_bufsiz(); |
| 533 | |
| 534 | IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) |
| 535 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 536 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 537 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 538 | opt_complementary = "vv"; |
| 539 | #endif |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 540 | opt = getopt32(argv, "fSI:va:" |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 541 | IF_FEATURE_UDHCP_PORT("P:") |
| 542 | , &str_I |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 543 | , &str_a |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 544 | IF_FEATURE_UDHCP_PORT(, &str_P) |
Leonid Lisovskiy | 6c9c0a1 | 2011-10-18 00:35:47 +0200 | [diff] [blame] | 545 | IF_UDHCP_VERBOSE(, &dhcp_verbose) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 546 | ); |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 547 | if (!(opt & 1)) { /* no -f */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 548 | bb_daemonize_or_rexec(0, argv); |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 549 | logmode = LOGMODE_NONE; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 550 | } |
Mike Frysinger | 6db1373 | 2010-06-04 13:24:50 -0400 | [diff] [blame] | 551 | /* update argv after the possible vfork+exec in daemonize */ |
| 552 | argv += optind; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 553 | if (opt & 2) { /* -S */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 554 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 555 | logmode |= LOGMODE_SYSLOG; |
| 556 | } |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 557 | if (opt & 4) { /* -I */ |
| 558 | len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET); |
| 559 | server_config.server_nip = lsa->u.sin.sin_addr.s_addr; |
| 560 | free(lsa); |
| 561 | } |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 562 | #if ENABLE_FEATURE_UDHCP_PORT |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 563 | if (opt & 32) { /* -P */ |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 564 | SERVER_PORT = xatou16(str_P); |
| 565 | CLIENT_PORT = SERVER_PORT + 1; |
| 566 | } |
| 567 | #endif |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 568 | arpping_ms = xatou(str_a); |
| 569 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 570 | /* Would rather not do read_config before daemonization - |
| 571 | * otherwise NOMMU machines will parse config twice */ |
Denis Vlasenko | 9f7b92a | 2007-08-15 20:03:36 +0000 | [diff] [blame] | 572 | read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 573 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 574 | /* Make sure fd 0,1,2 are open */ |
| 575 | bb_sanitize_stdio(); |
| 576 | /* Equivalent of doing a fflush after every \n */ |
| 577 | setlinebuf(stdout); |
| 578 | |
| 579 | /* Create pidfile */ |
| 580 | write_pidfile(server_config.pidfile); |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 581 | /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 582 | |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 583 | bb_error_msg("started, v"BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 584 | |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 585 | option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); |
Denys Vlasenko | 2e7aa92 | 2010-03-21 02:22:07 +0100 | [diff] [blame] | 586 | server_config.max_lease_sec = DEFAULT_LEASE_TIME; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 587 | if (option) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 588 | move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA); |
| 589 | server_config.max_lease_sec = ntohl(server_config.max_lease_sec); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 590 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 591 | |
| 592 | /* Sanity check */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 593 | num_ips = server_config.end_ip - server_config.start_ip + 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 594 | if (server_config.max_leases > num_ips) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 595 | bb_error_msg("max_leases=%u is too big, setting to %u", |
| 596 | (unsigned)server_config.max_leases, num_ips); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 597 | server_config.max_leases = num_ips; |
| 598 | } |
| 599 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 600 | g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 601 | read_leases(server_config.lease_file); |
| 602 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 603 | if (udhcp_read_interface(server_config.interface, |
| 604 | &server_config.ifindex, |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 605 | (server_config.server_nip == 0 ? &server_config.server_nip : NULL), |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 606 | server_config.server_mac) |
| 607 | ) { |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 608 | retval = 1; |
| 609 | goto ret; |
| 610 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 611 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 612 | /* Setup the signal pipe */ |
| 613 | udhcp_sp_setup(); |
| 614 | |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 615 | continue_with_autotime: |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 616 | timeout_end = monotonic_sec() + server_config.auto_time; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 617 | while (1) { /* loop until universe collapses */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 618 | fd_set rfds; |
| 619 | struct dhcp_packet packet; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 620 | int bytes; |
| 621 | struct timeval tv; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 622 | uint8_t *server_id_opt; |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 623 | uint8_t *requested_ip_opt; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 624 | uint32_t requested_nip = requested_nip; /* for compiler */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 625 | uint32_t static_lease_nip; |
| 626 | struct dyn_lease *lease, fake_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 627 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 628 | if (server_socket < 0) { |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 629 | server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 630 | server_config.interface); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 631 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 632 | |
| 633 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
| 634 | if (server_config.auto_time) { |
Denys Vlasenko | 936c401 | 2015-01-27 21:59:40 +0100 | [diff] [blame] | 635 | /* cast to signed is essential if tv_sec is wider than int */ |
| 636 | tv.tv_sec = (int)(timeout_end - monotonic_sec()); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 637 | tv.tv_usec = 0; |
| 638 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 639 | retval = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 640 | if (!server_config.auto_time || tv.tv_sec > 0) { |
| 641 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
| 642 | server_config.auto_time ? &tv : NULL); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 643 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 644 | if (retval == 0) { |
| 645 | write_leases(); |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 646 | goto continue_with_autotime; |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 647 | } |
| 648 | if (retval < 0 && errno != EINTR) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 649 | log1("error on select"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 650 | continue; |
| 651 | } |
| 652 | |
| 653 | switch (udhcp_sp_read(&rfds)) { |
| 654 | case SIGUSR1: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 655 | bb_error_msg("received %s", "SIGUSR1"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 656 | write_leases(); |
| 657 | /* why not just reset the timeout, eh */ |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 658 | goto continue_with_autotime; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 659 | case SIGTERM: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 660 | bb_error_msg("received %s", "SIGTERM"); |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 661 | write_leases(); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 662 | goto ret0; |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 663 | case 0: /* no signal: read a packet */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 664 | break; |
| 665 | default: /* signal or error (probably EINTR): back to select */ |
| 666 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 669 | bytes = udhcp_recv_kernel_packet(&packet, server_socket); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 670 | if (bytes < 0) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 671 | /* bytes can also be -2 ("bad packet data") */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 672 | if (bytes == -1 && errno != EINTR) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 673 | log1("read error: %s, reopening socket", strerror(errno)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 674 | close(server_socket); |
| 675 | server_socket = -1; |
| 676 | } |
| 677 | continue; |
| 678 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 679 | if (packet.hlen != 6) { |
| 680 | bb_error_msg("MAC length != 6, ignoring packet"); |
| 681 | continue; |
| 682 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 683 | if (packet.op != BOOTREQUEST) { |
| 684 | bb_error_msg("not a REQUEST, ignoring packet"); |
| 685 | continue; |
| 686 | } |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 687 | state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 688 | if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { |
| 689 | bb_error_msg("no or bad message type option, ignoring packet"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 690 | continue; |
| 691 | } |
| 692 | |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 693 | /* Get SERVER_ID if present */ |
| 694 | server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID); |
| 695 | if (server_id_opt) { |
Denys Vlasenko | 713d241 | 2010-11-28 21:51:44 +0100 | [diff] [blame] | 696 | uint32_t server_id_network_order; |
| 697 | move_from_unaligned32(server_id_network_order, server_id_opt); |
| 698 | if (server_id_network_order != server_config.server_nip) { |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 699 | /* client talks to somebody else */ |
| 700 | log1("server ID doesn't match, ignoring"); |
| 701 | continue; |
| 702 | } |
| 703 | } |
| 704 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 705 | /* Look for a static/dynamic lease */ |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 706 | static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr); |
| 707 | if (static_lease_nip) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 708 | bb_error_msg("found static lease: %x", static_lease_nip); |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 709 | memcpy(&fake_lease.lease_mac, &packet.chaddr, 6); |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 710 | fake_lease.lease_nip = static_lease_nip; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 711 | fake_lease.expires = 0; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 712 | lease = &fake_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 713 | } else { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 714 | lease = find_lease_by_mac(packet.chaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 717 | /* Get REQUESTED_IP if present */ |
| 718 | requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP); |
| 719 | if (requested_ip_opt) { |
| 720 | move_from_unaligned32(requested_nip, requested_ip_opt); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 723 | switch (state[0]) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 724 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 725 | case DHCPDISCOVER: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 726 | log1("received %s", "DISCOVER"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 727 | |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 728 | send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 729 | break; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 730 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 731 | case DHCPREQUEST: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 732 | log1("received %s", "REQUEST"); |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 733 | /* RFC 2131: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 734 | |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 735 | o DHCPREQUEST generated during SELECTING state: |
| 736 | |
| 737 | Client inserts the address of the selected server in 'server |
| 738 | identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be |
| 739 | filled in with the yiaddr value from the chosen DHCPOFFER. |
| 740 | |
| 741 | Note that the client may choose to collect several DHCPOFFER |
| 742 | messages and select the "best" offer. The client indicates its |
| 743 | selection by identifying the offering server in the DHCPREQUEST |
| 744 | message. If the client receives no acceptable offers, the client |
| 745 | may choose to try another DHCPDISCOVER message. Therefore, the |
| 746 | servers may not receive a specific DHCPREQUEST from which they can |
| 747 | decide whether or not the client has accepted the offer. |
| 748 | |
| 749 | o DHCPREQUEST generated during INIT-REBOOT state: |
| 750 | |
| 751 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 752 | option MUST be filled in with client's notion of its previously |
| 753 | assigned address. 'ciaddr' MUST be zero. The client is seeking to |
| 754 | verify a previously allocated, cached configuration. Server SHOULD |
| 755 | send a DHCPNAK message to the client if the 'requested IP address' |
| 756 | is incorrect, or is on the wrong network. |
| 757 | |
| 758 | Determining whether a client in the INIT-REBOOT state is on the |
| 759 | correct network is done by examining the contents of 'giaddr', the |
| 760 | 'requested IP address' option, and a database lookup. If the DHCP |
| 761 | server detects that the client is on the wrong net (i.e., the |
| 762 | result of applying the local subnet mask or remote subnet mask (if |
| 763 | 'giaddr' is not zero) to 'requested IP address' option value |
| 764 | doesn't match reality), then the server SHOULD send a DHCPNAK |
| 765 | message to the client. |
| 766 | |
| 767 | If the network is correct, then the DHCP server should check if |
| 768 | the client's notion of its IP address is correct. If not, then the |
| 769 | server SHOULD send a DHCPNAK message to the client. If the DHCP |
| 770 | server has no record of this client, then it MUST remain silent, |
| 771 | and MAY output a warning to the network administrator. This |
| 772 | behavior is necessary for peaceful coexistence of non- |
| 773 | communicating DHCP servers on the same wire. |
| 774 | |
| 775 | If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on |
| 776 | the same subnet as the server. The server MUST broadcast the |
| 777 | DHCPNAK message to the 0xffffffff broadcast address because the |
| 778 | client may not have a correct network address or subnet mask, and |
| 779 | the client may not be answering ARP requests. |
| 780 | |
| 781 | If 'giaddr' is set in the DHCPREQUEST message, the client is on a |
| 782 | different subnet. The server MUST set the broadcast bit in the |
| 783 | DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the |
| 784 | client, because the client may not have a correct network address |
| 785 | or subnet mask, and the client may not be answering ARP requests. |
| 786 | |
| 787 | o DHCPREQUEST generated during RENEWING state: |
| 788 | |
| 789 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 790 | option MUST NOT be filled in, 'ciaddr' MUST be filled in with |
| 791 | client's IP address. In this situation, the client is completely |
| 792 | configured, and is trying to extend its lease. This message will |
| 793 | be unicast, so no relay agents will be involved in its |
| 794 | transmission. Because 'giaddr' is therefore not filled in, the |
| 795 | DHCP server will trust the value in 'ciaddr', and use it when |
| 796 | replying to the client. |
| 797 | |
| 798 | A client MAY choose to renew or extend its lease prior to T1. The |
| 799 | server may choose not to extend the lease (as a policy decision by |
| 800 | the network administrator), but should return a DHCPACK message |
| 801 | regardless. |
| 802 | |
| 803 | o DHCPREQUEST generated during REBINDING state: |
| 804 | |
| 805 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 806 | option MUST NOT be filled in, 'ciaddr' MUST be filled in with |
| 807 | client's IP address. In this situation, the client is completely |
| 808 | configured, and is trying to extend its lease. This message MUST |
| 809 | be broadcast to the 0xffffffff IP broadcast address. The DHCP |
| 810 | server SHOULD check 'ciaddr' for correctness before replying to |
| 811 | the DHCPREQUEST. |
| 812 | |
| 813 | The DHCPREQUEST from a REBINDING client is intended to accommodate |
| 814 | sites that have multiple DHCP servers and a mechanism for |
| 815 | maintaining consistency among leases managed by multiple servers. |
| 816 | A DHCP server MAY extend a client's lease only if it has local |
| 817 | administrative authority to do so. |
| 818 | */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 819 | if (!requested_ip_opt) { |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 820 | requested_nip = packet.ciaddr; |
| 821 | if (requested_nip == 0) { |
| 822 | log1("no requested IP and no ciaddr, ignoring"); |
| 823 | break; |
| 824 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 825 | } |
| 826 | if (lease && requested_nip == lease->lease_nip) { |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 827 | /* client requested or configured IP matches the lease. |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 828 | * ACK it, and bump lease expiration time. */ |
| 829 | send_ACK(&packet, lease->lease_nip); |
| 830 | break; |
| 831 | } |
Denys Vlasenko | 713d241 | 2010-11-28 21:51:44 +0100 | [diff] [blame] | 832 | /* No lease for this MAC, or lease IP != requested IP */ |
| 833 | |
| 834 | if (server_id_opt /* client is in SELECTING state */ |
| 835 | || requested_ip_opt /* client is in INIT-REBOOT state */ |
| 836 | ) { |
| 837 | /* "No, we don't have this IP for you" */ |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 838 | send_NAK(&packet); |
Denys Vlasenko | 713d241 | 2010-11-28 21:51:44 +0100 | [diff] [blame] | 839 | } /* else: client is in RENEWING or REBINDING, do not answer */ |
| 840 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 841 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 842 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 843 | case DHCPDECLINE: |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 844 | /* RFC 2131: |
| 845 | * "If the server receives a DHCPDECLINE message, |
| 846 | * the client has discovered through some other means |
| 847 | * that the suggested network address is already |
| 848 | * in use. The server MUST mark the network address |
| 849 | * as not available and SHOULD notify the local |
| 850 | * sysadmin of a possible configuration problem." |
| 851 | * |
| 852 | * SERVER_ID must be present, |
| 853 | * REQUESTED_IP must be present, |
| 854 | * chaddr must be filled in, |
| 855 | * ciaddr must be 0 (we do not check this) |
| 856 | */ |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 857 | log1("received %s", "DECLINE"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 858 | if (server_id_opt |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 859 | && requested_ip_opt |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 860 | && lease /* chaddr matches this lease */ |
| 861 | && requested_nip == lease->lease_nip |
| 862 | ) { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 863 | memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 864 | lease->expires = time(NULL) + server_config.decline_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 865 | } |
| 866 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 867 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 868 | case DHCPRELEASE: |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 869 | /* "Upon receipt of a DHCPRELEASE message, the server |
| 870 | * marks the network address as not allocated." |
| 871 | * |
| 872 | * SERVER_ID must be present, |
| 873 | * REQUESTED_IP must not be present (we do not check this), |
| 874 | * chaddr must be filled in, |
| 875 | * ciaddr must be filled in |
| 876 | */ |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 877 | log1("received %s", "RELEASE"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 878 | if (server_id_opt |
| 879 | && lease /* chaddr matches this lease */ |
| 880 | && packet.ciaddr == lease->lease_nip |
| 881 | ) { |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 882 | lease->expires = time(NULL); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 883 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 884 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 885 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 886 | case DHCPINFORM: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 887 | log1("received %s", "INFORM"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 888 | send_inform(&packet); |
| 889 | break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 892 | ret0: |
| 893 | retval = 0; |
| 894 | ret: |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 895 | /*if (server_config.pidfile) - server_config.pidfile is never NULL */ |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 896 | remove_pidfile(server_config.pidfile); |
| 897 | return retval; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 898 | } |