fix #>&- syntax for closing fds
[oweals/busybox.git] / networking / udhcp / serverpacket.c
1 /* vi: set sw=4 ts=4: */
2 /* serverpacket.c
3  *
4  * Construct and send DHCP server packets
5  *
6  * Russ Dill <Russ.Dill@asu.edu> July 2001
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include "common.h"
24 #include "dhcpc.h"
25 #include "dhcpd.h"
26 #include "options.h"
27
28
29 /* send a packet to giaddr using the kernel ip stack */
30 static int send_packet_to_relay(struct dhcpMessage *payload)
31 {
32         DEBUG("Forwarding packet to relay");
33
34         return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT,
35                         payload->giaddr, SERVER_PORT);
36 }
37
38
39 /* send a packet to a specific arp address and ip address by creating our own ip packet */
40 static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
41 {
42         const uint8_t *chaddr;
43         uint32_t ciaddr;
44
45         if (force_broadcast) {
46                 DEBUG("broadcasting packet to client (NAK)");
47                 ciaddr = INADDR_BROADCAST;
48                 chaddr = MAC_BCAST_ADDR;
49         } else if (payload->ciaddr) {
50                 DEBUG("unicasting packet to client ciaddr");
51                 ciaddr = payload->ciaddr;
52                 chaddr = payload->chaddr;
53         } else if (payload->flags & htons(BROADCAST_FLAG)) {
54                 DEBUG("broadcasting packet to client (requested)");
55                 ciaddr = INADDR_BROADCAST;
56                 chaddr = MAC_BCAST_ADDR;
57         } else {
58                 DEBUG("unicasting packet to client yiaddr");
59                 ciaddr = payload->yiaddr;
60                 chaddr = payload->chaddr;
61         }
62         return udhcp_send_raw_packet(payload,
63                 /*src*/ server_config.server, SERVER_PORT,
64                 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
65                 server_config.ifindex);
66 }
67
68
69 /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
70 static int send_packet(struct dhcpMessage *payload, int force_broadcast)
71 {
72         if (payload->giaddr)
73                 return send_packet_to_relay(payload);
74         return send_packet_to_client(payload, force_broadcast);
75 }
76
77
78 static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
79 {
80         udhcp_init_header(packet, type);
81         packet->xid = oldpacket->xid;
82         memcpy(packet->chaddr, oldpacket->chaddr, 16);
83         packet->flags = oldpacket->flags;
84         packet->giaddr = oldpacket->giaddr;
85         packet->ciaddr = oldpacket->ciaddr;
86         add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
87 }
88
89
90 /* add in the bootp options */
91 static void add_bootp_options(struct dhcpMessage *packet)
92 {
93         packet->siaddr = server_config.siaddr;
94         if (server_config.sname)
95                 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
96         if (server_config.boot_file)
97                 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
98 }
99
100
101 /* send a DHCP OFFER to a DHCP DISCOVER */
102 int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
103 {
104         struct dhcpMessage packet;
105         uint32_t req_align;
106         uint32_t lease_time_aligned = server_config.lease;
107         uint32_t static_lease_ip;
108         uint8_t *req, *lease_time, *p_host_name;
109         struct option_set *curr;
110         struct in_addr addr;
111
112         init_packet(&packet, oldpacket, DHCPOFFER);
113
114         static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
115
116         /* ADDME: if static, short circuit */
117         if (!static_lease_ip) {
118                 struct dhcpOfferedAddr *lease;
119
120                 lease = find_lease_by_chaddr(oldpacket->chaddr);
121                 /* the client is in our lease/offered table */
122                 if (lease) {
123                         signed_leasetime_t tmp = lease->expires - time(NULL);
124                         if (tmp >= 0)
125                                 lease_time_aligned = tmp;
126                         packet.yiaddr = lease->yiaddr;
127                 /* Or the client has requested an ip */
128                 } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
129                  /* Don't look here (ugly hackish thing to do) */
130                  && (move_from_unaligned32(req_align, req), 1)
131                  /* and the ip is in the lease range */
132                  && ntohl(req_align) >= server_config.start_ip
133                  && ntohl(req_align) <= server_config.end_ip
134                  /* and is not already taken/offered */
135                  && (!(lease = find_lease_by_yiaddr(req_align))
136                         /* or its taken, but expired */
137                         || lease_expired(lease))
138                 ) {
139                         packet.yiaddr = req_align;
140                 /* otherwise, find a free IP */
141                 } else {
142                         packet.yiaddr = find_free_or_expired_address();
143                 }
144
145                 if (!packet.yiaddr) {
146                         bb_error_msg("no IP addresses to give - OFFER abandoned");
147                         return -1;
148                 }
149                 p_host_name = get_option(oldpacket, DHCP_HOST_NAME);
150                 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time, p_host_name)) {
151                         bb_error_msg("lease pool is full - OFFER abandoned");
152                         return -1;
153                 }
154                 lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
155                 if (lease_time) {
156                         move_from_unaligned32(lease_time_aligned, lease_time);
157                         lease_time_aligned = ntohl(lease_time_aligned);
158                         if (lease_time_aligned > server_config.lease)
159                                 lease_time_aligned = server_config.lease;
160                 }
161
162                 /* Make sure we aren't just using the lease time from the previous offer */
163                 if (lease_time_aligned < server_config.min_lease)
164                         lease_time_aligned = server_config.min_lease;
165         } else {
166                 /* It is a static lease... use it */
167                 packet.yiaddr = static_lease_ip;
168         }
169
170         add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned));
171
172         curr = server_config.options;
173         while (curr) {
174                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
175                         add_option_string(packet.options, curr->data);
176                 curr = curr->next;
177         }
178
179         add_bootp_options(&packet);
180
181         addr.s_addr = packet.yiaddr;
182         bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
183         return send_packet(&packet, 0);
184 }
185
186
187 int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket)
188 {
189         struct dhcpMessage packet;
190
191         init_packet(&packet, oldpacket, DHCPNAK);
192
193         DEBUG("Sending NAK");
194         return send_packet(&packet, 1);
195 }
196
197
198 int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
199 {
200         struct dhcpMessage packet;
201         struct option_set *curr;
202         uint8_t *lease_time;
203         uint32_t lease_time_aligned = server_config.lease;
204         struct in_addr addr;
205         uint8_t *p_host_name;
206
207         init_packet(&packet, oldpacket, DHCPACK);
208         packet.yiaddr = yiaddr;
209
210         lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
211         if (lease_time) {
212                 move_from_unaligned32(lease_time_aligned, lease_time);
213                 lease_time_aligned = ntohl(lease_time_aligned);
214                 if (lease_time_aligned > server_config.lease)
215                         lease_time_aligned = server_config.lease;
216                 else if (lease_time_aligned < server_config.min_lease)
217                         lease_time_aligned = server_config.min_lease;
218         }
219
220         add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned));
221
222         curr = server_config.options;
223         while (curr) {
224                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
225                         add_option_string(packet.options, curr->data);
226                 curr = curr->next;
227         }
228
229         add_bootp_options(&packet);
230
231         addr.s_addr = packet.yiaddr;
232         bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
233
234         if (send_packet(&packet, 0) < 0)
235                 return -1;
236
237         p_host_name = get_option(oldpacket, DHCP_HOST_NAME);
238         add_lease(packet.chaddr, packet.yiaddr, lease_time_aligned, p_host_name);
239         if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
240                 /* rewrite the file with leases at every new acceptance */
241                 write_leases();
242         }
243
244         return 0;
245 }
246
247
248 int FAST_FUNC send_inform(struct dhcpMessage *oldpacket)
249 {
250         struct dhcpMessage packet;
251         struct option_set *curr;
252
253         init_packet(&packet, oldpacket, DHCPACK);
254
255         curr = server_config.options;
256         while (curr) {
257                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
258                         add_option_string(packet.options, curr->data);
259                 curr = curr->next;
260         }
261
262         add_bootp_options(&packet);
263
264         return send_packet(&packet, 0);
265 }