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