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