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