Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* dhcpd.c |
| 2 | * |
| 3 | * udhcp Server |
| 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 | * |
| 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. |
| 22 | */ |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/wait.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 28 | #include <arpa/inet.h> |
| 29 | #include <netdb.h> |
| 30 | #include <netinet/in.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 31 | #include <sys/socket.h> |
| 32 | #include <unistd.h> |
| 33 | #include <signal.h> |
| 34 | #include <errno.h> |
| 35 | #include <sys/ioctl.h> |
| 36 | #include <time.h> |
| 37 | #include <sys/time.h> |
| 38 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 39 | #include "dhcpd.h" |
| 40 | #include "arpping.h" |
| 41 | #include "socket.h" |
| 42 | #include "options.h" |
| 43 | #include "files.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 44 | #include "serverpacket.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 45 | #include "common.h" |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 46 | #include "signalpipe.h" |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 47 | #include "static_leases.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /* globals */ |
| 51 | struct dhcpOfferedAddr *leases; |
| 52 | struct server_config_t server_config; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 53 | |
| 54 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 55 | #ifdef COMBINED_BINARY |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 56 | int udhcpd_main(int argc, char *argv[]) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 57 | #else |
| 58 | int main(int argc, char *argv[]) |
| 59 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 60 | { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 61 | fd_set rfds; |
| 62 | struct timeval tv; |
| 63 | int server_socket = -1; |
| 64 | int bytes, retval; |
| 65 | struct dhcpMessage packet; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 66 | uint8_t *state; |
| 67 | uint8_t *server_id, *requested; |
| 68 | uint32_t server_id_align, requested_align; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 69 | unsigned long timeout_end; |
| 70 | struct option_set *option; |
| 71 | struct dhcpOfferedAddr *lease; |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 72 | struct dhcpOfferedAddr static_lease; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 73 | int max_sock; |
Russ Dill | 9f4395c | 2002-12-11 21:40:46 +0000 | [diff] [blame] | 74 | unsigned long num_ips; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 75 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 76 | uint32_t static_lease_ip; |
| 77 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 78 | memset(&server_config, 0, sizeof(struct server_config_t)); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 79 | read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]); |
| 80 | |
| 81 | /* Start the log, sanitize fd's, and write a pid file */ |
| 82 | start_log_and_pid("udhcpd", server_config.pidfile); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 83 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 84 | if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) { |
| 85 | memcpy(&server_config.lease, option->data + 2, 4); |
| 86 | server_config.lease = ntohl(server_config.lease); |
| 87 | } |
| 88 | else server_config.lease = LEASE_TIME; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 89 | |
Russ Dill | 9f4395c | 2002-12-11 21:40:46 +0000 | [diff] [blame] | 90 | /* Sanity check */ |
| 91 | num_ips = ntohl(server_config.end) - ntohl(server_config.start); |
| 92 | if (server_config.max_leases > num_ips) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 93 | LOG(LOG_ERR, "max_leases value (%lu) not sane, " |
| 94 | "setting to %lu instead", |
Russ Dill | 9f4395c | 2002-12-11 21:40:46 +0000 | [diff] [blame] | 95 | server_config.max_leases, num_ips); |
| 96 | server_config.max_leases = num_ips; |
| 97 | } |
| 98 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 99 | leases = xcalloc(server_config.max_leases, sizeof(struct dhcpOfferedAddr)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 100 | read_leases(server_config.lease_file); |
| 101 | |
| 102 | if (read_interface(server_config.interface, &server_config.ifindex, |
| 103 | &server_config.server, server_config.arp) < 0) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 104 | return 1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 105 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 106 | #ifndef UDHCP_DEBUG |
| 107 | background(server_config.pidfile); /* hold lock during fork. */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 110 | /* Setup the signal pipe */ |
| 111 | udhcp_sp_setup(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 112 | |
| 113 | timeout_end = time(0) + server_config.auto_time; |
| 114 | while(1) { /* loop until universe collapses */ |
| 115 | |
| 116 | if (server_socket < 0) |
| 117 | if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 118 | LOG(LOG_ERR, "FATAL: couldn't create server socket, %m"); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 119 | return 2; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 120 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 121 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 122 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 123 | if (server_config.auto_time) { |
| 124 | tv.tv_sec = timeout_end - time(0); |
| 125 | tv.tv_usec = 0; |
| 126 | } |
| 127 | if (!server_config.auto_time || tv.tv_sec > 0) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 128 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 129 | server_config.auto_time ? &tv : NULL); |
| 130 | } else retval = 0; /* If we already timed out, fall through */ |
| 131 | |
| 132 | if (retval == 0) { |
| 133 | write_leases(); |
| 134 | timeout_end = time(0) + server_config.auto_time; |
| 135 | continue; |
| 136 | } else if (retval < 0 && errno != EINTR) { |
| 137 | DEBUG(LOG_INFO, "error on select"); |
| 138 | continue; |
| 139 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 140 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 141 | switch (udhcp_sp_read(&rfds)) { |
| 142 | case SIGUSR1: |
| 143 | LOG(LOG_INFO, "Received a SIGUSR1"); |
| 144 | write_leases(); |
| 145 | /* why not just reset the timeout, eh */ |
| 146 | timeout_end = time(0) + server_config.auto_time; |
| 147 | continue; |
| 148 | case SIGTERM: |
| 149 | LOG(LOG_INFO, "Received a SIGTERM"); |
| 150 | return 0; |
| 151 | case 0: break; /* no signal */ |
| 152 | default: continue; /* signal or error (probably EINTR) */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ |
| 156 | if (bytes == -1 && errno != EINTR) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 157 | DEBUG(LOG_INFO, "error on read, %m, reopening socket"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 158 | close(server_socket); |
| 159 | server_socket = -1; |
| 160 | } |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { |
| 165 | DEBUG(LOG_ERR, "couldn't get option from packet, ignoring"); |
| 166 | continue; |
| 167 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 168 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 169 | /* Look for a static lease */ |
| 170 | static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr); |
| 171 | |
| 172 | if(static_lease_ip) |
| 173 | { |
| 174 | printf("Found static lease: %x\n", static_lease_ip); |
| 175 | |
| 176 | memcpy(&static_lease.chaddr, &packet.chaddr, 16); |
| 177 | static_lease.yiaddr = static_lease_ip; |
| 178 | static_lease.expires = 0; |
| 179 | |
| 180 | lease = &static_lease; |
| 181 | |
| 182 | } |
| 183 | else |
| 184 | { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 185 | lease = find_lease_by_chaddr(packet.chaddr); |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 188 | switch (state[0]) { |
| 189 | case DHCPDISCOVER: |
| 190 | DEBUG(LOG_INFO,"received DISCOVER"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 191 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 192 | if (sendOffer(&packet) < 0) { |
| 193 | LOG(LOG_ERR, "send OFFER failed"); |
| 194 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 195 | break; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 196 | case DHCPREQUEST: |
| 197 | DEBUG(LOG_INFO, "received REQUEST"); |
| 198 | |
| 199 | requested = get_option(&packet, DHCP_REQUESTED_IP); |
| 200 | server_id = get_option(&packet, DHCP_SERVER_ID); |
| 201 | |
| 202 | if (requested) memcpy(&requested_align, requested, 4); |
| 203 | if (server_id) memcpy(&server_id_align, server_id, 4); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 204 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame] | 205 | if (lease) { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 206 | if (server_id) { |
| 207 | /* SELECTING State */ |
| 208 | DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 209 | if (server_id_align == server_config.server && requested && |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 210 | requested_align == lease->yiaddr) { |
| 211 | sendACK(&packet, lease->yiaddr); |
| 212 | } |
| 213 | } else { |
| 214 | if (requested) { |
| 215 | /* INIT-REBOOT State */ |
| 216 | if (lease->yiaddr == requested_align) |
| 217 | sendACK(&packet, lease->yiaddr); |
| 218 | else sendNAK(&packet); |
| 219 | } else { |
| 220 | /* RENEWING or REBINDING State */ |
| 221 | if (lease->yiaddr == packet.ciaddr) |
| 222 | sendACK(&packet, lease->yiaddr); |
| 223 | else { |
| 224 | /* don't know what to do!!!! */ |
| 225 | sendNAK(&packet); |
| 226 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 227 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 228 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 229 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 230 | /* what to do if we have no record of the client */ |
| 231 | } else if (server_id) { |
| 232 | /* SELECTING State */ |
| 233 | |
| 234 | } else if (requested) { |
| 235 | /* INIT-REBOOT State */ |
| 236 | if ((lease = find_lease_by_yiaddr(requested_align))) { |
| 237 | if (lease_expired(lease)) { |
| 238 | /* probably best if we drop this lease */ |
| 239 | memset(lease->chaddr, 0, 16); |
| 240 | /* make some contention for this address */ |
| 241 | } else sendNAK(&packet); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 242 | } else if (requested_align < server_config.start || |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 243 | requested_align > server_config.end) { |
| 244 | sendNAK(&packet); |
| 245 | } /* else remain silent */ |
| 246 | |
| 247 | } else { |
| 248 | /* RENEWING or REBINDING State */ |
| 249 | } |
| 250 | break; |
| 251 | case DHCPDECLINE: |
| 252 | DEBUG(LOG_INFO,"received DECLINE"); |
| 253 | if (lease) { |
| 254 | memset(lease->chaddr, 0, 16); |
| 255 | lease->expires = time(0) + server_config.decline_time; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 256 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 257 | break; |
| 258 | case DHCPRELEASE: |
| 259 | DEBUG(LOG_INFO,"received RELEASE"); |
| 260 | if (lease) lease->expires = time(0); |
| 261 | break; |
| 262 | case DHCPINFORM: |
| 263 | DEBUG(LOG_INFO,"received INFORM"); |
| 264 | send_inform(&packet); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 265 | break; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 266 | default: |
| 267 | LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 273 | |