"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* dhcpd.c |
| 3 | * |
| 4 | * udhcp Server |
| 5 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> |
| 6 | * Chris Trew <ctrew@moreton.com.au> |
| 7 | * |
| 8 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 9 | * |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 13 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 14 | #include "dhcpd.h" |
| 15 | #include "options.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | /* globals */ |
| 19 | struct dhcpOfferedAddr *leases; |
| 20 | struct server_config_t server_config; |
| 21 | |
| 22 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 23 | int udhcpd_main(int argc, char *argv[]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 24 | int udhcpd_main(int argc, char *argv[]) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | { |
| 26 | fd_set rfds; |
| 27 | struct timeval tv; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 28 | int server_socket = -1, bytes, retval, max_sock; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 29 | struct dhcpMessage packet; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 30 | uint8_t *state, *server_id, *requested; |
| 31 | uint32_t server_id_align, requested_align, static_lease_ip; |
| 32 | unsigned long timeout_end, num_ips; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 33 | struct option_set *option; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 34 | struct dhcpOfferedAddr *lease, static_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 35 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]); |
| 37 | |
| 38 | /* Start the log, sanitize fd's, and write a pid file */ |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 39 | udhcp_start_log_and_pid(server_config.pidfile); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 40 | |
| 41 | if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) { |
| 42 | memcpy(&server_config.lease, option->data + 2, 4); |
| 43 | server_config.lease = ntohl(server_config.lease); |
| 44 | } |
| 45 | else server_config.lease = LEASE_TIME; |
| 46 | |
| 47 | /* Sanity check */ |
| 48 | num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1; |
| 49 | if (server_config.max_leases > num_ips) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 50 | bb_error_msg("max_leases value (%lu) not sane, " |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 51 | "setting to %lu instead", |
| 52 | server_config.max_leases, num_ips); |
| 53 | server_config.max_leases = num_ips; |
| 54 | } |
| 55 | |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 56 | leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | read_leases(server_config.lease_file); |
| 58 | |
| 59 | if (read_interface(server_config.interface, &server_config.ifindex, |
| 60 | &server_config.server, server_config.arp) < 0) |
| 61 | return 1; |
| 62 | |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 63 | if (!ENABLE_FEATURE_UDHCP_DEBUG) |
| 64 | udhcp_background(server_config.pidfile); /* hold lock during fork. */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 65 | |
| 66 | /* Setup the signal pipe */ |
| 67 | udhcp_sp_setup(); |
| 68 | |
| 69 | timeout_end = time(0) + server_config.auto_time; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 70 | while (1) { /* loop until universe collapses */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 72 | if (server_socket < 0) { |
| 73 | server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 74 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | |
| 76 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
| 77 | if (server_config.auto_time) { |
| 78 | tv.tv_sec = timeout_end - time(0); |
| 79 | tv.tv_usec = 0; |
| 80 | } |
| 81 | if (!server_config.auto_time || tv.tv_sec > 0) { |
| 82 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
| 83 | server_config.auto_time ? &tv : NULL); |
| 84 | } else retval = 0; /* If we already timed out, fall through */ |
| 85 | |
| 86 | if (retval == 0) { |
| 87 | write_leases(); |
| 88 | timeout_end = time(0) + server_config.auto_time; |
| 89 | continue; |
| 90 | } else if (retval < 0 && errno != EINTR) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 91 | DEBUG("error on select"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 92 | continue; |
| 93 | } |
| 94 | |
| 95 | switch (udhcp_sp_read(&rfds)) { |
| 96 | case SIGUSR1: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 97 | bb_info_msg("Received a SIGUSR1"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 98 | write_leases(); |
| 99 | /* why not just reset the timeout, eh */ |
| 100 | timeout_end = time(0) + server_config.auto_time; |
| 101 | continue; |
| 102 | case SIGTERM: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 103 | bb_info_msg("Received a SIGTERM"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | case 0: break; /* no signal */ |
| 106 | default: continue; /* signal or error (probably EINTR) */ |
| 107 | } |
| 108 | |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 109 | if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 110 | if (bytes == -1 && errno != EINTR) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 111 | DEBUG("error on read, %s, reopening socket", strerror(errno)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | close(server_socket); |
| 113 | server_socket = -1; |
| 114 | } |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 119 | bb_error_msg("cannot get option from packet, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | |
| 123 | /* Look for a static lease */ |
| 124 | static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr); |
| 125 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 126 | if (static_lease_ip) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 127 | bb_info_msg("Found static lease: %x", static_lease_ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 128 | |
| 129 | memcpy(&static_lease.chaddr, &packet.chaddr, 16); |
| 130 | static_lease.yiaddr = static_lease_ip; |
| 131 | static_lease.expires = 0; |
| 132 | |
| 133 | lease = &static_lease; |
| 134 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 135 | } else { |
| 136 | lease = find_lease_by_chaddr(packet.chaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | switch (state[0]) { |
| 140 | case DHCPDISCOVER: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 141 | DEBUG("Received DISCOVER"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 142 | |
| 143 | if (sendOffer(&packet) < 0) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 144 | bb_error_msg("send OFFER failed"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 145 | } |
| 146 | break; |
| 147 | case DHCPREQUEST: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 148 | DEBUG("received REQUEST"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 149 | |
| 150 | requested = get_option(&packet, DHCP_REQUESTED_IP); |
| 151 | server_id = get_option(&packet, DHCP_SERVER_ID); |
| 152 | |
| 153 | if (requested) memcpy(&requested_align, requested, 4); |
| 154 | if (server_id) memcpy(&server_id_align, server_id, 4); |
| 155 | |
| 156 | if (lease) { |
| 157 | if (server_id) { |
| 158 | /* SELECTING State */ |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 159 | DEBUG("server_id = %08x", ntohl(server_id_align)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 160 | if (server_id_align == server_config.server && requested && |
| 161 | requested_align == lease->yiaddr) { |
| 162 | sendACK(&packet, lease->yiaddr); |
| 163 | } |
| 164 | } else { |
| 165 | if (requested) { |
| 166 | /* INIT-REBOOT State */ |
| 167 | if (lease->yiaddr == requested_align) |
| 168 | sendACK(&packet, lease->yiaddr); |
| 169 | else sendNAK(&packet); |
| 170 | } else { |
| 171 | /* RENEWING or REBINDING State */ |
| 172 | if (lease->yiaddr == packet.ciaddr) |
| 173 | sendACK(&packet, lease->yiaddr); |
| 174 | else { |
| 175 | /* don't know what to do!!!! */ |
| 176 | sendNAK(&packet); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* what to do if we have no record of the client */ |
| 182 | } else if (server_id) { |
| 183 | /* SELECTING State */ |
| 184 | |
| 185 | } else if (requested) { |
| 186 | /* INIT-REBOOT State */ |
| 187 | if ((lease = find_lease_by_yiaddr(requested_align))) { |
| 188 | if (lease_expired(lease)) { |
| 189 | /* probably best if we drop this lease */ |
| 190 | memset(lease->chaddr, 0, 16); |
| 191 | /* make some contention for this address */ |
| 192 | } else sendNAK(&packet); |
| 193 | } else if (requested_align < server_config.start || |
| 194 | requested_align > server_config.end) { |
| 195 | sendNAK(&packet); |
| 196 | } /* else remain silent */ |
| 197 | |
| 198 | } else { |
| 199 | /* RENEWING or REBINDING State */ |
| 200 | } |
| 201 | break; |
| 202 | case DHCPDECLINE: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 203 | DEBUG("Received DECLINE"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 204 | if (lease) { |
| 205 | memset(lease->chaddr, 0, 16); |
| 206 | lease->expires = time(0) + server_config.decline_time; |
| 207 | } |
| 208 | break; |
| 209 | case DHCPRELEASE: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 210 | DEBUG("Received RELEASE"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 211 | if (lease) lease->expires = time(0); |
| 212 | break; |
| 213 | case DHCPINFORM: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 214 | DEBUG("Received INFORM"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 215 | send_inform(&packet); |
| 216 | break; |
| 217 | default: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 218 | bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |