whitespace fixes
[oweals/busybox.git] / networking / udhcp / dhcpd.c
1 /* vi: set sw=4 ts=4: */
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  *
10  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11  */
12
13 #include <syslog.h>
14 #include "common.h"
15 #include "dhcpd.h"
16 #include "options.h"
17
18
19 /* globals */
20 struct dhcpOfferedAddr *leases;
21 struct server_config_t server_config;
22
23
24 int udhcpd_main(int argc, char **argv);
25 int udhcpd_main(int argc, char **argv)
26 {
27         fd_set rfds;
28         struct timeval tv;
29         int server_socket = -1, bytes, retval, max_sock;
30         struct dhcpMessage packet;
31         uint8_t *state, *server_id, *requested;
32         uint32_t server_id_align, requested_align, static_lease_ip;
33         unsigned long timeout_end, num_ips;
34         struct option_set *option;
35         struct dhcpOfferedAddr *lease, static_lease;
36
37 //Huh, dhcpd don't have --foreground, --syslog options?? TODO
38
39         if (!ENABLE_FEATURE_UDHCP_DEBUG) {
40                 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
41                 logmode &= ~LOGMODE_STDIO;
42         }
43
44         if (ENABLE_FEATURE_UDHCP_SYSLOG) {
45                 openlog(applet_name, LOG_PID, LOG_LOCAL0);
46                 logmode |= LOGMODE_SYSLOG;
47         }
48
49         /* Would rather not do read_config before daemonization -
50          * otherwise NOMMU machines will parse config twice */
51         read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]);
52
53         udhcp_make_pidfile(server_config.pidfile);
54
55         option = find_option(server_config.options, DHCP_LEASE_TIME);
56         server_config.lease = LEASE_TIME;
57         if (option) {
58                 memcpy(&server_config.lease, option->data + 2, 4);
59                 server_config.lease = ntohl(server_config.lease);
60         }
61
62         /* Sanity check */
63         num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
64         if (server_config.max_leases > num_ips) {
65                 bb_error_msg("max_leases value (%lu) not sane, "
66                         "setting to %lu instead",
67                         server_config.max_leases, num_ips);
68                 server_config.max_leases = num_ips;
69         }
70
71         leases = xzalloc(server_config.max_leases * sizeof(struct dhcpOfferedAddr));
72         read_leases(server_config.lease_file);
73
74         if (read_interface(server_config.interface, &server_config.ifindex,
75                            &server_config.server, server_config.arp) < 0) {
76                 retval = 1;
77                 goto ret;
78         }
79
80         /* Setup the signal pipe */
81         udhcp_sp_setup();
82
83         timeout_end = time(0) + server_config.auto_time;
84         while (1) { /* loop until universe collapses */
85
86                 if (server_socket < 0) {
87                         server_socket = listen_socket(INADDR_ANY, SERVER_PORT,
88                                         server_config.interface);
89                 }
90
91                 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
92                 if (server_config.auto_time) {
93                         tv.tv_sec = timeout_end - time(0);
94                         tv.tv_usec = 0;
95                 }
96                 retval = 0;
97                 if (!server_config.auto_time || tv.tv_sec > 0) {
98                         retval = select(max_sock + 1, &rfds, NULL, NULL,
99                                         server_config.auto_time ? &tv : NULL);
100                 }
101                 if (retval == 0) {
102                         write_leases();
103                         timeout_end = time(0) + server_config.auto_time;
104                         continue;
105                 }
106                 if (retval < 0 && errno != EINTR) {
107                         DEBUG("error on select");
108                         continue;
109                 }
110
111                 switch (udhcp_sp_read(&rfds)) {
112                 case SIGUSR1:
113                         bb_info_msg("Received a SIGUSR1");
114                         write_leases();
115                         /* why not just reset the timeout, eh */
116                         timeout_end = time(0) + server_config.auto_time;
117                         continue;
118                 case SIGTERM:
119                         bb_info_msg("Received a SIGTERM");
120                         goto ret0;
121                 case 0: break;          /* no signal */
122                 default: continue;      /* signal or error (probably EINTR) */
123                 }
124
125                 bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
126                 if (bytes < 0) {
127                         if (bytes == -1 && errno != EINTR) {
128                                 DEBUG("error on read, %s, reopening socket", strerror(errno));
129                                 close(server_socket);
130                                 server_socket = -1;
131                         }
132                         continue;
133                 }
134
135                 state = get_option(&packet, DHCP_MESSAGE_TYPE);
136                 if (state == NULL) {
137                         bb_error_msg("cannot get option from packet, ignoring");
138                         continue;
139                 }
140
141                 /* Look for a static lease */
142                 static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
143
144                 if (static_lease_ip) {
145                         bb_info_msg("Found static lease: %x", static_lease_ip);
146
147                         memcpy(&static_lease.chaddr, &packet.chaddr, 16);
148                         static_lease.yiaddr = static_lease_ip;
149                         static_lease.expires = 0;
150
151                         lease = &static_lease;
152                 } else {
153                         lease = find_lease_by_chaddr(packet.chaddr);
154                 }
155
156                 switch (state[0]) {
157                 case DHCPDISCOVER:
158                         DEBUG("Received DISCOVER");
159
160                         if (sendOffer(&packet) < 0) {
161                                 bb_error_msg("send OFFER failed");
162                         }
163                         break;
164                 case DHCPREQUEST:
165                         DEBUG("received REQUEST");
166
167                         requested = get_option(&packet, DHCP_REQUESTED_IP);
168                         server_id = get_option(&packet, DHCP_SERVER_ID);
169
170                         if (requested) memcpy(&requested_align, requested, 4);
171                         if (server_id) memcpy(&server_id_align, server_id, 4);
172
173                         if (lease) {
174                                 if (server_id) {
175                                         /* SELECTING State */
176                                         DEBUG("server_id = %08x", ntohl(server_id_align));
177                                         if (server_id_align == server_config.server && requested
178                                          && requested_align == lease->yiaddr
179                                         ) {
180                                                 sendACK(&packet, lease->yiaddr);
181                                         }
182                                 } else if (requested) {
183                                         /* INIT-REBOOT State */
184                                         if (lease->yiaddr == requested_align)
185                                                 sendACK(&packet, lease->yiaddr);
186                                         else
187                                                 sendNAK(&packet);
188                                 } else if (lease->yiaddr == packet.ciaddr) {
189                                         /* RENEWING or REBINDING State */
190                                         sendACK(&packet, lease->yiaddr);
191                                 } else {
192                                         /* don't know what to do!!!! */
193                                         sendNAK(&packet);
194                                 }
195
196                         /* what to do if we have no record of the client */
197                         } else if (server_id) {
198                                 /* SELECTING State */
199
200                         } else if (requested) {
201                                 /* INIT-REBOOT State */
202                                 lease = find_lease_by_yiaddr(requested_align);
203                                 if (lease) {
204                                         if (lease_expired(lease)) {
205                                                 /* probably best if we drop this lease */
206                                                 memset(lease->chaddr, 0, 16);
207                                         /* make some contention for this address */
208                                         } else
209                                                 sendNAK(&packet);
210                                 } else if (requested_align < server_config.start
211                                         || requested_align > server_config.end
212                                 ) {
213                                         sendNAK(&packet);
214                                 } /* else remain silent */
215
216                         } else {
217                                 /* RENEWING or REBINDING State */
218                         }
219                         break;
220                 case DHCPDECLINE:
221                         DEBUG("Received DECLINE");
222                         if (lease) {
223                                 memset(lease->chaddr, 0, 16);
224                                 lease->expires = time(0) + server_config.decline_time;
225                         }
226                         break;
227                 case DHCPRELEASE:
228                         DEBUG("Received RELEASE");
229                         if (lease)
230                                 lease->expires = time(0);
231                         break;
232                 case DHCPINFORM:
233                         DEBUG("Received INFORM");
234                         send_inform(&packet);
235                         break;
236                 default:
237                         bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
238                 }
239         }
240  ret0:
241         retval = 0;
242  ret:
243         if (server_config.pidfile)
244                 remove_pidfile(server_config.pidfile);
245         return retval;
246 }