"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 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 13 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 14 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 15 | #include "dhcpd.h" |
| 16 | #include "options.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | /* globals */ |
| 20 | struct dhcpOfferedAddr *leases; |
Denis Vlasenko | deabacd | 2007-09-30 17:55:43 +0000 | [diff] [blame] | 21 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 22 | |
| 23 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 24 | int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 25 | int udhcpd_main(int argc, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 26 | { |
| 27 | fd_set rfds; |
| 28 | struct timeval tv; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 29 | int server_socket = -1, bytes, retval, max_sock; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 30 | struct dhcpMessage packet; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 31 | uint8_t *state, *server_id, *requested; |
| 32 | uint32_t server_id_align, requested_align, static_lease_ip; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 33 | unsigned timeout_end; |
| 34 | unsigned num_ips; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 35 | unsigned opt; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | struct option_set *option; |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 37 | struct dhcpOfferedAddr *lease, static_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 39 | opt = getopt32(argv, "fS"); |
Denis Vlasenko | 9f7b92a | 2007-08-15 20:03:36 +0000 | [diff] [blame] | 40 | argv += optind; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 42 | if (!(opt & 1)) { /* no -f */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 43 | bb_daemonize_or_rexec(0, argv); |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 44 | logmode &= ~LOGMODE_STDIO; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 47 | if (opt & 2) { /* -S */ |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 48 | openlog(applet_name, LOG_PID, LOG_LOCAL0); |
| 49 | logmode |= LOGMODE_SYSLOG; |
| 50 | } |
| 51 | |
| 52 | /* Would rather not do read_config before daemonization - |
| 53 | * otherwise NOMMU machines will parse config twice */ |
Denis Vlasenko | 9f7b92a | 2007-08-15 20:03:36 +0000 | [diff] [blame] | 54 | read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 56 | /* Make sure fd 0,1,2 are open */ |
| 57 | bb_sanitize_stdio(); |
| 58 | /* Equivalent of doing a fflush after every \n */ |
| 59 | setlinebuf(stdout); |
| 60 | |
| 61 | /* Create pidfile */ |
| 62 | write_pidfile(server_config.pidfile); |
| 63 | /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */ |
| 64 | |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 65 | bb_info_msg("%s (v"BB_VER") started", applet_name); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 67 | option = find_option(server_config.options, DHCP_LEASE_TIME); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 68 | server_config.lease = LEASE_TIME; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 69 | if (option) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 70 | memcpy(&server_config.lease, option->data + 2, 4); |
| 71 | server_config.lease = ntohl(server_config.lease); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 72 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 73 | |
| 74 | /* Sanity check */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 75 | num_ips = server_config.end_ip - server_config.start_ip + 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 76 | if (server_config.max_leases > num_ips) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 77 | bb_error_msg("max_leases=%u is too big, setting to %u", |
| 78 | (unsigned)server_config.max_leases, num_ips); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 79 | server_config.max_leases = num_ips; |
| 80 | } |
| 81 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 82 | leases = xzalloc(server_config.max_leases * sizeof(*leases)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 83 | read_leases(server_config.lease_file); |
| 84 | |
| 85 | if (read_interface(server_config.interface, &server_config.ifindex, |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 86 | &server_config.server, server_config.arp)) { |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 87 | retval = 1; |
| 88 | goto ret; |
| 89 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 91 | /* Setup the signal pipe */ |
| 92 | udhcp_sp_setup(); |
| 93 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 94 | timeout_end = monotonic_sec() + server_config.auto_time; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 95 | while (1) { /* loop until universe collapses */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 97 | if (server_socket < 0) { |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 98 | server_socket = listen_socket(/*INADDR_ANY,*/ SERVER_PORT, |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 99 | server_config.interface); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 100 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 101 | |
| 102 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
| 103 | if (server_config.auto_time) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 104 | tv.tv_sec = timeout_end - monotonic_sec(); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 105 | tv.tv_usec = 0; |
| 106 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 107 | retval = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 108 | if (!server_config.auto_time || tv.tv_sec > 0) { |
| 109 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
| 110 | server_config.auto_time ? &tv : NULL); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 111 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | if (retval == 0) { |
| 113 | write_leases(); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 114 | timeout_end = monotonic_sec() + server_config.auto_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 115 | continue; |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 116 | } |
| 117 | if (retval < 0 && errno != EINTR) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 118 | DEBUG("error on select"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 119 | continue; |
| 120 | } |
| 121 | |
| 122 | switch (udhcp_sp_read(&rfds)) { |
| 123 | case SIGUSR1: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 124 | bb_info_msg("Received a SIGUSR1"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 125 | write_leases(); |
| 126 | /* why not just reset the timeout, eh */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 127 | timeout_end = monotonic_sec() + server_config.auto_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 128 | continue; |
| 129 | case SIGTERM: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 130 | bb_info_msg("Received a SIGTERM"); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 131 | goto ret0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 132 | case 0: break; /* no signal */ |
| 133 | default: continue; /* signal or error (probably EINTR) */ |
| 134 | } |
| 135 | |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 136 | bytes = udhcp_recv_packet(&packet, server_socket); /* this waits for a packet - idle */ |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 137 | if (bytes < 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 138 | if (bytes == -1 && errno != EINTR) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 139 | DEBUG("error on read, %s, reopening socket", strerror(errno)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 140 | close(server_socket); |
| 141 | server_socket = -1; |
| 142 | } |
| 143 | continue; |
| 144 | } |
| 145 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 146 | state = get_option(&packet, DHCP_MESSAGE_TYPE); |
| 147 | if (state == NULL) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 148 | bb_error_msg("cannot get option from packet, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
| 152 | /* Look for a static lease */ |
| 153 | static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr); |
| 154 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 155 | if (static_lease_ip) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 156 | bb_info_msg("Found static lease: %x", static_lease_ip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 157 | |
| 158 | memcpy(&static_lease.chaddr, &packet.chaddr, 16); |
| 159 | static_lease.yiaddr = static_lease_ip; |
| 160 | static_lease.expires = 0; |
| 161 | |
| 162 | lease = &static_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 163 | } else { |
| 164 | lease = find_lease_by_chaddr(packet.chaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | switch (state[0]) { |
| 168 | case DHCPDISCOVER: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 169 | DEBUG("Received DISCOVER"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 170 | |
| 171 | if (sendOffer(&packet) < 0) { |
Denis Vlasenko | a959588 | 2006-09-29 21:30:43 +0000 | [diff] [blame] | 172 | bb_error_msg("send OFFER failed"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 173 | } |
| 174 | break; |
| 175 | case DHCPREQUEST: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 176 | DEBUG("received REQUEST"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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); |
| 183 | |
| 184 | if (lease) { |
| 185 | if (server_id) { |
| 186 | /* SELECTING State */ |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 187 | DEBUG("server_id = %08x", ntohl(server_id_align)); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 188 | if (server_id_align == server_config.server && requested |
| 189 | && requested_align == lease->yiaddr |
| 190 | ) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 191 | sendACK(&packet, lease->yiaddr); |
| 192 | } |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 193 | } else if (requested) { |
| 194 | /* INIT-REBOOT State */ |
| 195 | if (lease->yiaddr == requested_align) |
| 196 | sendACK(&packet, lease->yiaddr); |
| 197 | else |
| 198 | sendNAK(&packet); |
| 199 | } else if (lease->yiaddr == packet.ciaddr) { |
| 200 | /* RENEWING or REBINDING State */ |
| 201 | sendACK(&packet, lease->yiaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 202 | } else { |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 203 | /* don't know what to do!!!! */ |
| 204 | sendNAK(&packet); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* what to do if we have no record of the client */ |
| 208 | } else if (server_id) { |
| 209 | /* SELECTING State */ |
| 210 | |
| 211 | } else if (requested) { |
| 212 | /* INIT-REBOOT State */ |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 213 | lease = find_lease_by_yiaddr(requested_align); |
| 214 | if (lease) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 215 | if (lease_expired(lease)) { |
| 216 | /* probably best if we drop this lease */ |
| 217 | memset(lease->chaddr, 0, 16); |
| 218 | /* make some contention for this address */ |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 219 | } else |
| 220 | sendNAK(&packet); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 221 | } else { |
| 222 | uint32_t r = ntohl(requested_align); |
| 223 | if (r < server_config.start_ip |
| 224 | || r > server_config.end_ip |
| 225 | ) { |
| 226 | sendNAK(&packet); |
| 227 | } |
Denis Vlasenko | e6bce97 | 2007-07-01 17:11:54 +0000 | [diff] [blame] | 228 | /* else remain silent */ |
| 229 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 230 | |
| 231 | } else { |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 232 | /* RENEWING or REBINDING State */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 233 | } |
| 234 | break; |
| 235 | case DHCPDECLINE: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 236 | DEBUG("Received DECLINE"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 237 | if (lease) { |
| 238 | memset(lease->chaddr, 0, 16); |
| 239 | lease->expires = time(0) + server_config.decline_time; |
| 240 | } |
| 241 | break; |
| 242 | case DHCPRELEASE: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 243 | DEBUG("Received RELEASE"); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 244 | if (lease) |
| 245 | lease->expires = time(0); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 246 | break; |
| 247 | case DHCPINFORM: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 248 | DEBUG("Received INFORM"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 249 | send_inform(&packet); |
| 250 | break; |
| 251 | default: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 252 | bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 255 | ret0: |
| 256 | retval = 0; |
| 257 | ret: |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 258 | /*if (server_config.pidfile) - server_config.pidfile is never NULL */ |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 259 | remove_pidfile(server_config.pidfile); |
| 260 | return retval; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 261 | } |