"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 | */ |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 23 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 24 | #include "common.h" |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 25 | #include "dhcpc.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 26 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | |
| 28 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 29 | /* Send a packet to a specific mac address and ip address by creating our own ip packet */ |
| 30 | 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] | 31 | { |
| 32 | const uint8_t *chaddr; |
| 33 | uint32_t ciaddr; |
| 34 | |
| 35 | // Was: |
| 36 | //if (force_broadcast) { /* broadcast */ } |
| 37 | //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ } |
| 38 | //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } |
| 39 | //else { /* unicast to dhcp_pkt->yiaddr */ } |
| 40 | // But this is wrong: yiaddr is _our_ idea what client's IP is |
| 41 | // (for example, from lease file). Client may not know that, |
| 42 | // and may not have UDP socket listening on that IP! |
| 43 | // We should never unicast to dhcp_pkt->yiaddr! |
| 44 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, |
| 45 | // and can be used. |
| 46 | |
| 47 | if (force_broadcast |
| 48 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 49 | || dhcp_pkt->ciaddr == 0 |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 50 | ) { |
| 51 | log1("Broadcasting packet to client"); |
| 52 | ciaddr = INADDR_BROADCAST; |
| 53 | chaddr = MAC_BCAST_ADDR; |
| 54 | } else { |
| 55 | log1("Unicasting packet to client ciaddr"); |
| 56 | ciaddr = dhcp_pkt->ciaddr; |
| 57 | chaddr = dhcp_pkt->chaddr; |
| 58 | } |
| 59 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 60 | udhcp_send_raw_packet(dhcp_pkt, |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 61 | /*src*/ server_config.server_nip, SERVER_PORT, |
| 62 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
| 63 | server_config.ifindex); |
| 64 | } |
| 65 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 66 | /* Send a packet to gateway_nip using the kernel ip stack */ |
| 67 | static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt) |
| 68 | { |
| 69 | log1("Forwarding packet to relay"); |
| 70 | |
| 71 | udhcp_send_kernel_packet(dhcp_pkt, |
| 72 | server_config.server_nip, SERVER_PORT, |
| 73 | dhcp_pkt->gateway_nip, SERVER_PORT); |
| 74 | } |
| 75 | |
| 76 | static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 77 | { |
| 78 | if (dhcp_pkt->gateway_nip) |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 79 | send_packet_to_relay(dhcp_pkt); |
| 80 | else |
| 81 | send_packet_to_client(dhcp_pkt, force_broadcast); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) |
| 85 | { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 86 | /* Sets op, htype, hlen, cookie fields |
| 87 | * and adds DHCP_MESSAGE_TYPE option */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 88 | udhcp_init_header(packet, type); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 89 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 90 | packet->xid = oldpacket->xid; |
| 91 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); |
| 92 | packet->flags = oldpacket->flags; |
| 93 | packet->gateway_nip = oldpacket->gateway_nip; |
| 94 | packet->ciaddr = oldpacket->ciaddr; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 95 | udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 98 | /* Fill options field, siaddr_nip, and sname and boot_file fields. |
| 99 | * TODO: teach this code to use overload option. |
| 100 | */ |
| 101 | static void add_server_options(struct dhcp_packet *packet) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 102 | { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 103 | struct option_set *curr = server_config.options; |
| 104 | |
| 105 | while (curr) { |
| 106 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 107 | udhcp_add_binary_option(packet, curr->data); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 108 | curr = curr->next; |
| 109 | } |
| 110 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 111 | packet->siaddr_nip = server_config.siaddr_nip; |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 112 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 113 | if (server_config.sname) |
| 114 | strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); |
| 115 | if (server_config.boot_file) |
| 116 | strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1); |
| 117 | } |
| 118 | |
| 119 | static uint32_t select_lease_time(struct dhcp_packet *packet) |
| 120 | { |
| 121 | uint32_t lease_time_sec = server_config.max_lease_sec; |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 122 | uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 123 | if (lease_time_opt) { |
| 124 | move_from_unaligned32(lease_time_sec, lease_time_opt); |
| 125 | lease_time_sec = ntohl(lease_time_sec); |
| 126 | if (lease_time_sec > server_config.max_lease_sec) |
| 127 | lease_time_sec = server_config.max_lease_sec; |
| 128 | if (lease_time_sec < server_config.min_lease_sec) |
| 129 | lease_time_sec = server_config.min_lease_sec; |
| 130 | } |
| 131 | return lease_time_sec; |
| 132 | } |
| 133 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 134 | /* We got a DHCP DISCOVER. Send an OFFER. */ |
| 135 | static void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 136 | { |
| 137 | struct dhcp_packet packet; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 138 | uint32_t lease_time_sec; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 139 | struct in_addr addr; |
| 140 | |
| 141 | init_packet(&packet, oldpacket, DHCPOFFER); |
| 142 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 143 | /* If it is a static lease, use its IP */ |
| 144 | packet.yiaddr = static_lease_nip; |
| 145 | /* Else: */ |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 146 | if (!static_lease_nip) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 147 | /* We have no static lease for client's chaddr */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 148 | uint32_t req_nip; |
| 149 | uint8_t *req_ip_opt; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 150 | const char *p_host_name; |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 151 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 152 | if (lease) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 153 | /* We have a dynamic lease for client's chaddr. |
| 154 | * Reuse its IP (even if lease is expired). |
| 155 | * Note that we ignore requested IP in this case. |
| 156 | */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 157 | packet.yiaddr = lease->lease_nip; |
| 158 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 159 | /* Or: if client has requested an IP */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 160 | else if ((req_ip_opt = udhcp_get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 161 | /* (read IP) */ |
| 162 | && (move_from_unaligned32(req_nip, req_ip_opt), 1) |
| 163 | /* and the IP is in the lease range */ |
| 164 | && ntohl(req_nip) >= server_config.start_ip |
| 165 | && ntohl(req_nip) <= server_config.end_ip |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 166 | /* and */ |
| 167 | && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */ |
| 168 | || is_expired_lease(lease) /* or is taken, but expired */ |
| 169 | ) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 170 | ) { |
| 171 | packet.yiaddr = req_nip; |
| 172 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 173 | else { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 174 | /* Otherwise, find a free IP */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 175 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); |
| 176 | } |
| 177 | |
| 178 | if (!packet.yiaddr) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 179 | bb_error_msg("no free IP addresses. OFFER abandoned"); |
| 180 | return; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 181 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 182 | /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 183 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 184 | lease = add_lease(packet.chaddr, packet.yiaddr, |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 185 | server_config.offer_time, |
| 186 | p_host_name, |
| 187 | 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] | 188 | ); |
| 189 | if (!lease) { |
| 190 | bb_error_msg("no free IP addresses. OFFER abandoned"); |
| 191 | return; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 192 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 195 | lease_time_sec = select_lease_time(oldpacket); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 196 | udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 197 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 198 | |
| 199 | addr.s_addr = packet.yiaddr; |
| 200 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 201 | /* send_packet emits error message itself if it detects failure */ |
| 202 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 205 | static void send_NAK(struct dhcp_packet *oldpacket) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 206 | { |
| 207 | struct dhcp_packet packet; |
| 208 | |
| 209 | init_packet(&packet, oldpacket, DHCPNAK); |
| 210 | |
| 211 | log1("Sending NAK"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 212 | send_packet(&packet, /*force_bcast:*/ 1); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 215 | static void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 216 | { |
| 217 | struct dhcp_packet packet; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 218 | uint32_t lease_time_sec; |
| 219 | struct in_addr addr; |
| 220 | const char *p_host_name; |
| 221 | |
| 222 | init_packet(&packet, oldpacket, DHCPACK); |
| 223 | packet.yiaddr = yiaddr; |
| 224 | |
| 225 | lease_time_sec = select_lease_time(oldpacket); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 226 | udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 227 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 228 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 229 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 230 | addr.s_addr = yiaddr; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 231 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 232 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 233 | |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 234 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 235 | add_lease(packet.chaddr, packet.yiaddr, |
| 236 | lease_time_sec, |
| 237 | p_host_name, |
| 238 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 |
| 239 | ); |
| 240 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
| 241 | /* rewrite the file with leases at every new acceptance */ |
| 242 | write_leases(); |
| 243 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 246 | static void send_inform(struct dhcp_packet *oldpacket) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 247 | { |
| 248 | struct dhcp_packet packet; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 249 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 250 | /* "The server responds to a DHCPINFORM message by sending a DHCPACK |
| 251 | * message directly to the address given in the 'ciaddr' field |
| 252 | * of the DHCPINFORM message. The server MUST NOT send a lease |
| 253 | * expiration time to the client and SHOULD NOT fill in 'yiaddr'." |
| 254 | */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 255 | init_packet(&packet, oldpacket, DHCPACK); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 256 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 257 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 258 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 262 | /* globals */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 263 | struct dyn_lease *g_leases; |
Denis Vlasenko | deabacd | 2007-09-30 17:55:43 +0000 | [diff] [blame] | 264 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 265 | |
| 266 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 267 | int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 268 | int udhcpd_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 269 | { |
| 270 | fd_set rfds; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 271 | int server_socket = -1, retval, max_sock; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 272 | struct dhcp_packet packet; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 273 | uint8_t *state; |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 274 | uint32_t static_lease_nip; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 275 | unsigned timeout_end; |
| 276 | unsigned num_ips; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 277 | unsigned opt; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 278 | struct option_set *option; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 279 | struct dyn_lease *lease, fake_lease; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 280 | IF_FEATURE_UDHCP_PORT(char *str_P;) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 281 | |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 282 | #if ENABLE_FEATURE_UDHCP_PORT |
| 283 | SERVER_PORT = 67; |
| 284 | CLIENT_PORT = 68; |
| 285 | #endif |
| 286 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 287 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 288 | opt_complementary = "vv"; |
| 289 | #endif |
| 290 | opt = getopt32(argv, "fSv" |
| 291 | IF_FEATURE_UDHCP_PORT("P:", &str_P) |
| 292 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 293 | , &dhcp_verbose |
| 294 | #endif |
| 295 | ); |
Denis Vlasenko | 9f7b92a | 2007-08-15 20:03:36 +0000 | [diff] [blame] | 296 | argv += optind; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 297 | if (!(opt & 1)) { /* no -f */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 298 | bb_daemonize_or_rexec(0, argv); |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 299 | logmode = LOGMODE_NONE; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 300 | } |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 301 | if (opt & 2) { /* -S */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 302 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 303 | logmode |= LOGMODE_SYSLOG; |
| 304 | } |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 305 | #if ENABLE_FEATURE_UDHCP_PORT |
Denys Vlasenko | 406bd14 | 2010-03-27 23:24:57 +0100 | [diff] [blame^] | 306 | if (opt & 8) { /* -P */ |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 307 | SERVER_PORT = xatou16(str_P); |
| 308 | CLIENT_PORT = SERVER_PORT + 1; |
| 309 | } |
| 310 | #endif |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 311 | /* Would rather not do read_config before daemonization - |
| 312 | * otherwise NOMMU machines will parse config twice */ |
Denis Vlasenko | 9f7b92a | 2007-08-15 20:03:36 +0000 | [diff] [blame] | 313 | read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 314 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 315 | /* Make sure fd 0,1,2 are open */ |
| 316 | bb_sanitize_stdio(); |
| 317 | /* Equivalent of doing a fflush after every \n */ |
| 318 | setlinebuf(stdout); |
| 319 | |
| 320 | /* Create pidfile */ |
| 321 | write_pidfile(server_config.pidfile); |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 322 | /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 323 | |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 324 | bb_info_msg("%s (v"BB_VER") started", applet_name); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 325 | |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 326 | option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); |
Denys Vlasenko | 2e7aa92 | 2010-03-21 02:22:07 +0100 | [diff] [blame] | 327 | server_config.max_lease_sec = DEFAULT_LEASE_TIME; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 328 | if (option) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 329 | move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA); |
| 330 | server_config.max_lease_sec = ntohl(server_config.max_lease_sec); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 331 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 332 | |
| 333 | /* Sanity check */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 334 | num_ips = server_config.end_ip - server_config.start_ip + 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 335 | if (server_config.max_leases > num_ips) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 336 | bb_error_msg("max_leases=%u is too big, setting to %u", |
| 337 | (unsigned)server_config.max_leases, num_ips); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 338 | server_config.max_leases = num_ips; |
| 339 | } |
| 340 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 341 | g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 342 | read_leases(server_config.lease_file); |
| 343 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 344 | if (udhcp_read_interface(server_config.interface, |
| 345 | &server_config.ifindex, |
| 346 | &server_config.server_nip, |
| 347 | server_config.server_mac) |
| 348 | ) { |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 349 | retval = 1; |
| 350 | goto ret; |
| 351 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 352 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 353 | /* Setup the signal pipe */ |
| 354 | udhcp_sp_setup(); |
| 355 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 356 | timeout_end = monotonic_sec() + server_config.auto_time; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 357 | while (1) { /* loop until universe collapses */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 358 | int bytes; |
| 359 | struct timeval tv; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 360 | uint8_t *server_id_opt; |
| 361 | uint8_t *requested_opt; |
| 362 | uint32_t requested_nip = requested_nip; /* for compiler */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 363 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 364 | if (server_socket < 0) { |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 365 | server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 366 | server_config.interface); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 367 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 368 | |
| 369 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
| 370 | if (server_config.auto_time) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 371 | tv.tv_sec = timeout_end - monotonic_sec(); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 372 | tv.tv_usec = 0; |
| 373 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 374 | retval = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 375 | if (!server_config.auto_time || tv.tv_sec > 0) { |
| 376 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
| 377 | server_config.auto_time ? &tv : NULL); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 378 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 379 | if (retval == 0) { |
| 380 | write_leases(); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 381 | timeout_end = monotonic_sec() + server_config.auto_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 382 | continue; |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 383 | } |
| 384 | if (retval < 0 && errno != EINTR) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 385 | log1("Error on select"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 386 | continue; |
| 387 | } |
| 388 | |
| 389 | switch (udhcp_sp_read(&rfds)) { |
| 390 | case SIGUSR1: |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 391 | bb_info_msg("Received SIGUSR1"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 392 | write_leases(); |
| 393 | /* why not just reset the timeout, eh */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 394 | timeout_end = monotonic_sec() + server_config.auto_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 395 | continue; |
| 396 | case SIGTERM: |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 397 | bb_info_msg("Received SIGTERM"); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 398 | goto ret0; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 399 | case 0: /* no signal: read a packet */ |
| 400 | break; |
| 401 | default: /* signal or error (probably EINTR): back to select */ |
| 402 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 405 | bytes = udhcp_recv_kernel_packet(&packet, server_socket); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 406 | if (bytes < 0) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 407 | /* bytes can also be -2 ("bad packet data") */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 408 | if (bytes == -1 && errno != EINTR) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 409 | log1("Read error: %s, reopening socket", strerror(errno)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 410 | close(server_socket); |
| 411 | server_socket = -1; |
| 412 | } |
| 413 | continue; |
| 414 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 415 | if (packet.hlen != 6) { |
| 416 | bb_error_msg("MAC length != 6, ignoring packet"); |
| 417 | continue; |
| 418 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 419 | if (packet.op != BOOTREQUEST) { |
| 420 | bb_error_msg("not a REQUEST, ignoring packet"); |
| 421 | continue; |
| 422 | } |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 423 | state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 424 | if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { |
| 425 | bb_error_msg("no or bad message type option, ignoring packet"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 426 | continue; |
| 427 | } |
| 428 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 429 | /* Look for a static/dynamic lease */ |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 430 | static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr); |
| 431 | if (static_lease_nip) { |
| 432 | bb_info_msg("Found static lease: %x", static_lease_nip); |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 433 | memcpy(&fake_lease.lease_mac, &packet.chaddr, 6); |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 434 | fake_lease.lease_nip = static_lease_nip; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 435 | fake_lease.expires = 0; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 436 | lease = &fake_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 437 | } else { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 438 | lease = find_lease_by_mac(packet.chaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 441 | /* Get REQUESTED_IP and SERVER_ID if present */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 442 | server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 443 | if (server_id_opt) { |
| 444 | uint32_t server_id_net; |
| 445 | move_from_unaligned32(server_id_net, server_id_opt); |
| 446 | if (server_id_net != server_config.server_nip) { |
| 447 | /* client talks to somebody else */ |
| 448 | log1("server ID doesn't match, ignoring"); |
| 449 | continue; |
| 450 | } |
| 451 | } |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 452 | requested_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 453 | if (requested_opt) { |
| 454 | move_from_unaligned32(requested_nip, requested_opt); |
| 455 | } |
| 456 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 457 | switch (state[0]) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 458 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 459 | case DHCPDISCOVER: |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 460 | log1("Received DISCOVER"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 461 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 462 | send_offer(&packet, static_lease_nip, lease); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 463 | break; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 464 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 465 | case DHCPREQUEST: |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 466 | log1("Received REQUEST"); |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 467 | /* RFC 2131: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 468 | |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 469 | o DHCPREQUEST generated during SELECTING state: |
| 470 | |
| 471 | Client inserts the address of the selected server in 'server |
| 472 | identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be |
| 473 | filled in with the yiaddr value from the chosen DHCPOFFER. |
| 474 | |
| 475 | Note that the client may choose to collect several DHCPOFFER |
| 476 | messages and select the "best" offer. The client indicates its |
| 477 | selection by identifying the offering server in the DHCPREQUEST |
| 478 | message. If the client receives no acceptable offers, the client |
| 479 | may choose to try another DHCPDISCOVER message. Therefore, the |
| 480 | servers may not receive a specific DHCPREQUEST from which they can |
| 481 | decide whether or not the client has accepted the offer. |
| 482 | |
| 483 | o DHCPREQUEST generated during INIT-REBOOT state: |
| 484 | |
| 485 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 486 | option MUST be filled in with client's notion of its previously |
| 487 | assigned address. 'ciaddr' MUST be zero. The client is seeking to |
| 488 | verify a previously allocated, cached configuration. Server SHOULD |
| 489 | send a DHCPNAK message to the client if the 'requested IP address' |
| 490 | is incorrect, or is on the wrong network. |
| 491 | |
| 492 | Determining whether a client in the INIT-REBOOT state is on the |
| 493 | correct network is done by examining the contents of 'giaddr', the |
| 494 | 'requested IP address' option, and a database lookup. If the DHCP |
| 495 | server detects that the client is on the wrong net (i.e., the |
| 496 | result of applying the local subnet mask or remote subnet mask (if |
| 497 | 'giaddr' is not zero) to 'requested IP address' option value |
| 498 | doesn't match reality), then the server SHOULD send a DHCPNAK |
| 499 | message to the client. |
| 500 | |
| 501 | If the network is correct, then the DHCP server should check if |
| 502 | the client's notion of its IP address is correct. If not, then the |
| 503 | server SHOULD send a DHCPNAK message to the client. If the DHCP |
| 504 | server has no record of this client, then it MUST remain silent, |
| 505 | and MAY output a warning to the network administrator. This |
| 506 | behavior is necessary for peaceful coexistence of non- |
| 507 | communicating DHCP servers on the same wire. |
| 508 | |
| 509 | If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on |
| 510 | the same subnet as the server. The server MUST broadcast the |
| 511 | DHCPNAK message to the 0xffffffff broadcast address because the |
| 512 | client may not have a correct network address or subnet mask, and |
| 513 | the client may not be answering ARP requests. |
| 514 | |
| 515 | If 'giaddr' is set in the DHCPREQUEST message, the client is on a |
| 516 | different subnet. The server MUST set the broadcast bit in the |
| 517 | DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the |
| 518 | client, because the client may not have a correct network address |
| 519 | or subnet mask, and the client may not be answering ARP requests. |
| 520 | |
| 521 | o DHCPREQUEST generated during RENEWING state: |
| 522 | |
| 523 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 524 | option MUST NOT be filled in, 'ciaddr' MUST be filled in with |
| 525 | client's IP address. In this situation, the client is completely |
| 526 | configured, and is trying to extend its lease. This message will |
| 527 | be unicast, so no relay agents will be involved in its |
| 528 | transmission. Because 'giaddr' is therefore not filled in, the |
| 529 | DHCP server will trust the value in 'ciaddr', and use it when |
| 530 | replying to the client. |
| 531 | |
| 532 | A client MAY choose to renew or extend its lease prior to T1. The |
| 533 | server may choose not to extend the lease (as a policy decision by |
| 534 | the network administrator), but should return a DHCPACK message |
| 535 | regardless. |
| 536 | |
| 537 | o DHCPREQUEST generated during REBINDING state: |
| 538 | |
| 539 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 540 | option MUST NOT be filled in, 'ciaddr' MUST be filled in with |
| 541 | client's IP address. In this situation, the client is completely |
| 542 | configured, and is trying to extend its lease. This message MUST |
| 543 | be broadcast to the 0xffffffff IP broadcast address. The DHCP |
| 544 | server SHOULD check 'ciaddr' for correctness before replying to |
| 545 | the DHCPREQUEST. |
| 546 | |
| 547 | The DHCPREQUEST from a REBINDING client is intended to accommodate |
| 548 | sites that have multiple DHCP servers and a mechanism for |
| 549 | maintaining consistency among leases managed by multiple servers. |
| 550 | A DHCP server MAY extend a client's lease only if it has local |
| 551 | administrative authority to do so. |
| 552 | */ |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 553 | if (!requested_opt) { |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 554 | requested_nip = packet.ciaddr; |
| 555 | if (requested_nip == 0) { |
| 556 | log1("no requested IP and no ciaddr, ignoring"); |
| 557 | break; |
| 558 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 559 | } |
| 560 | if (lease && requested_nip == lease->lease_nip) { |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 561 | /* client requested or configured IP matches the lease. |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 562 | * ACK it, and bump lease expiration time. */ |
| 563 | send_ACK(&packet, lease->lease_nip); |
| 564 | break; |
| 565 | } |
| 566 | if (server_id_opt) { |
| 567 | /* client was talking specifically to us. |
| 568 | * "No, we don't have this IP for you". */ |
| 569 | send_NAK(&packet); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 570 | } |
| 571 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 572 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 573 | case DHCPDECLINE: |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 574 | /* RFC 2131: |
| 575 | * "If the server receives a DHCPDECLINE message, |
| 576 | * the client has discovered through some other means |
| 577 | * that the suggested network address is already |
| 578 | * in use. The server MUST mark the network address |
| 579 | * as not available and SHOULD notify the local |
| 580 | * sysadmin of a possible configuration problem." |
| 581 | * |
| 582 | * SERVER_ID must be present, |
| 583 | * REQUESTED_IP must be present, |
| 584 | * chaddr must be filled in, |
| 585 | * ciaddr must be 0 (we do not check this) |
| 586 | */ |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 587 | log1("Received DECLINE"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 588 | if (server_id_opt |
| 589 | && requested_opt |
| 590 | && lease /* chaddr matches this lease */ |
| 591 | && requested_nip == lease->lease_nip |
| 592 | ) { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 593 | memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 594 | lease->expires = time(NULL) + server_config.decline_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 595 | } |
| 596 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 597 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 598 | case DHCPRELEASE: |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 599 | /* "Upon receipt of a DHCPRELEASE message, the server |
| 600 | * marks the network address as not allocated." |
| 601 | * |
| 602 | * SERVER_ID must be present, |
| 603 | * REQUESTED_IP must not be present (we do not check this), |
| 604 | * chaddr must be filled in, |
| 605 | * ciaddr must be filled in |
| 606 | */ |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 607 | log1("Received RELEASE"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 608 | if (server_id_opt |
| 609 | && lease /* chaddr matches this lease */ |
| 610 | && packet.ciaddr == lease->lease_nip |
| 611 | ) { |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 612 | lease->expires = time(NULL); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 613 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 614 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 615 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 616 | case DHCPINFORM: |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 617 | log1("Received INFORM"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 618 | send_inform(&packet); |
| 619 | break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 622 | ret0: |
| 623 | retval = 0; |
| 624 | ret: |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 625 | /*if (server_config.pidfile) - server_config.pidfile is never NULL */ |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 626 | remove_pidfile(server_config.pidfile); |
| 627 | return retval; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 628 | } |