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