Fix kbuild bugs noticed by Bernhard Fischer <rep.nop@aon.at>
[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 <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <string.h>
27 #include <time.h>
28
29 #include "common.h"
30 #include "serverpacket.h"
31 #include "dhcpd.h"
32 #include "options.h"
33 #include "static_leases.h"
34
35 /* send a packet to giaddr using the kernel ip stack */
36 static int send_packet_to_relay(struct dhcpMessage *payload)
37 {
38         DEBUG("Forwarding packet to relay");
39
40         return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,
41                         payload->giaddr, SERVER_PORT);
42 }
43
44
45 /* send a packet to a specific arp address and ip address by creating our own ip packet */
46 static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
47 {
48         uint8_t *chaddr;
49         uint32_t ciaddr;
50
51         if (force_broadcast) {
52                 DEBUG("broadcasting packet to client (NAK)");
53                 ciaddr = INADDR_BROADCAST;
54                 chaddr = MAC_BCAST_ADDR;
55         } else if (payload->ciaddr) {
56                 DEBUG("unicasting packet to client ciaddr");
57                 ciaddr = payload->ciaddr;
58                 chaddr = payload->chaddr;
59         } else if (ntohs(payload->flags) & BROADCAST_FLAG) {
60                 DEBUG("broadcasting packet to client (requested)");
61                 ciaddr = INADDR_BROADCAST;
62                 chaddr = MAC_BCAST_ADDR;
63         } else {
64                 DEBUG("unicasting packet to client yiaddr");
65                 ciaddr = payload->yiaddr;
66                 chaddr = payload->chaddr;
67         }
68         return udhcp_raw_packet(payload, server_config.server, SERVER_PORT,
69                         ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
70 }
71
72
73 /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
74 static int send_packet(struct dhcpMessage *payload, int force_broadcast)
75 {
76         int ret;
77
78         if (payload->giaddr)
79                 ret = send_packet_to_relay(payload);
80         else ret = send_packet_to_client(payload, force_broadcast);
81         return ret;
82 }
83
84
85 static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
86 {
87         udhcp_init_header(packet, type);
88         packet->xid = oldpacket->xid;
89         memcpy(packet->chaddr, oldpacket->chaddr, 16);
90         packet->flags = oldpacket->flags;
91         packet->giaddr = oldpacket->giaddr;
92         packet->ciaddr = oldpacket->ciaddr;
93         add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
94 }
95
96
97 /* add in the bootp options */
98 static void add_bootp_options(struct dhcpMessage *packet)
99 {
100         packet->siaddr = server_config.siaddr;
101         if (server_config.sname)
102                 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
103         if (server_config.boot_file)
104                 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
105 }
106
107
108 /* send a DHCP OFFER to a DHCP DISCOVER */
109 int sendOffer(struct dhcpMessage *oldpacket)
110 {
111         struct dhcpMessage packet;
112         struct dhcpOfferedAddr *lease = NULL;
113         uint32_t req_align, lease_time_align = server_config.lease;
114         uint8_t *req, *lease_time;
115         struct option_set *curr;
116         struct in_addr addr;
117
118         uint32_t static_lease_ip;
119
120         init_packet(&packet, oldpacket, DHCPOFFER);
121
122         static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
123
124         /* ADDME: if static, short circuit */
125         if(!static_lease_ip)
126         {
127         /* the client is in our lease/offered table */
128         if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) {
129                 if (!lease_expired(lease))
130                         lease_time_align = lease->expires - time(0);
131                 packet.yiaddr = lease->yiaddr;
132
133         /* Or the client has a requested ip */
134         } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) &&
135
136                    /* Don't look here (ugly hackish thing to do) */
137                    memcpy(&req_align, req, 4) &&
138
139                    /* and the ip is in the lease range */
140                    ntohl(req_align) >= ntohl(server_config.start) &&
141                    ntohl(req_align) <= ntohl(server_config.end) &&
142
143                         !static_lease_ip &&  /* Check that its not a static lease */
144                         /* and is not already taken/offered */
145                    ((!(lease = find_lease_by_yiaddr(req_align)) ||
146
147                    /* or its taken, but expired */ /* ADDME: or maybe in here */
148                    lease_expired(lease)))) {
149                                 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
150
151                         /* otherwise, find a free IP */
152         } else {
153                         /* Is it a static lease? (No, because find_address skips static lease) */
154                 packet.yiaddr = find_address(0);
155
156                 /* try for an expired lease */
157                 if (!packet.yiaddr) packet.yiaddr = find_address(1);
158         }
159
160         if(!packet.yiaddr) {
161                 bb_error_msg("no IP addresses to give - OFFER abandoned");
162                 return -1;
163         }
164
165         if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
166                 bb_error_msg("lease pool is full - OFFER abandoned");
167                 return -1;
168         }
169
170         if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
171                 memcpy(&lease_time_align, lease_time, 4);
172                 lease_time_align = ntohl(lease_time_align);
173                 if (lease_time_align > server_config.lease)
174                         lease_time_align = server_config.lease;
175         }
176
177         /* Make sure we aren't just using the lease time from the previous offer */
178         if (lease_time_align < server_config.min_lease)
179                 lease_time_align = server_config.lease;
180         }
181         /* ADDME: end of short circuit */
182         else
183         {
184                 /* It is a static lease... use it */
185                 packet.yiaddr = static_lease_ip;
186         }
187
188         add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
189
190         curr = server_config.options;
191         while (curr) {
192                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
193                         add_option_string(packet.options, curr->data);
194                 curr = curr->next;
195         }
196
197         add_bootp_options(&packet);
198
199         addr.s_addr = packet.yiaddr;
200         bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
201         return send_packet(&packet, 0);
202 }
203
204
205 int sendNAK(struct dhcpMessage *oldpacket)
206 {
207         struct dhcpMessage packet;
208
209         init_packet(&packet, oldpacket, DHCPNAK);
210
211         DEBUG("Sending NAK");
212         return send_packet(&packet, 1);
213 }
214
215
216 int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
217 {
218         struct dhcpMessage packet;
219         struct option_set *curr;
220         uint8_t *lease_time;
221         uint32_t lease_time_align = server_config.lease;
222         struct in_addr addr;
223
224         init_packet(&packet, oldpacket, DHCPACK);
225         packet.yiaddr = yiaddr;
226
227         if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
228                 memcpy(&lease_time_align, lease_time, 4);
229                 lease_time_align = ntohl(lease_time_align);
230                 if (lease_time_align > server_config.lease)
231                         lease_time_align = server_config.lease;
232                 else if (lease_time_align < server_config.min_lease)
233                         lease_time_align = server_config.lease;
234         }
235
236         add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
237
238         curr = server_config.options;
239         while (curr) {
240                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
241                         add_option_string(packet.options, curr->data);
242                 curr = curr->next;
243         }
244
245         add_bootp_options(&packet);
246
247         addr.s_addr = packet.yiaddr;
248         bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
249
250         if (send_packet(&packet, 0) < 0)
251                 return -1;
252
253         add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
254
255         return 0;
256 }
257
258
259 int send_inform(struct dhcpMessage *oldpacket)
260 {
261         struct dhcpMessage packet;
262         struct option_set *curr;
263
264         init_packet(&packet, oldpacket, DHCPACK);
265
266         curr = server_config.options;
267         while (curr) {
268                 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
269                         add_option_string(packet.options, curr->data);
270                 curr = curr->next;
271         }
272
273         add_bootp_options(&packet);
274
275         return send_packet(&packet, 0);
276 }