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