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