blob: 1dc7233f26766d0aa432e7cd4aa16c2cb6b02ecb [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* 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
Mike Frysinger7031f622006-05-08 03:20:50 +000023#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000024#include "dhcpc.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000025#include "dhcpd.h"
26#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000027
Mike Frysinger7031f622006-05-08 03:20:50 +000028
29/* send a packet to giaddr using the kernel ip stack */
30static int send_packet_to_relay(struct dhcpMessage *payload)
31{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000032 DEBUG("Forwarding packet to relay");
Mike Frysinger7031f622006-05-08 03:20:50 +000033
Denis Vlasenkofff145d2007-12-20 21:11:38 +000034 return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT,
Mike Frysinger7031f622006-05-08 03:20:50 +000035 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 */
40static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
41{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000042 const uint8_t *chaddr;
Mike Frysinger7031f622006-05-08 03:20:50 +000043 uint32_t ciaddr;
44
45 if (force_broadcast) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000046 DEBUG("broadcasting packet to client (NAK)");
Mike Frysinger7031f622006-05-08 03:20:50 +000047 ciaddr = INADDR_BROADCAST;
48 chaddr = MAC_BCAST_ADDR;
49 } else if (payload->ciaddr) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000050 DEBUG("unicasting packet to client ciaddr");
Mike Frysinger7031f622006-05-08 03:20:50 +000051 ciaddr = payload->ciaddr;
52 chaddr = payload->chaddr;
53 } else if (ntohs(payload->flags) & BROADCAST_FLAG) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000054 DEBUG("broadcasting packet to client (requested)");
Mike Frysinger7031f622006-05-08 03:20:50 +000055 ciaddr = INADDR_BROADCAST;
56 chaddr = MAC_BCAST_ADDR;
57 } else {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000058 DEBUG("unicasting packet to client yiaddr");
Mike Frysinger7031f622006-05-08 03:20:50 +000059 ciaddr = payload->yiaddr;
60 chaddr = payload->chaddr;
61 }
Denis Vlasenkofff145d2007-12-20 21:11:38 +000062 return udhcp_send_raw_packet(payload, server_config.server, SERVER_PORT,
Mike Frysinger7031f622006-05-08 03:20:50 +000063 ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
64}
65
66
67/* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
68static int send_packet(struct dhcpMessage *payload, int force_broadcast)
69{
70 int ret;
71
72 if (payload->giaddr)
73 ret = send_packet_to_relay(payload);
74 else ret = send_packet_to_client(payload, force_broadcast);
75 return ret;
76}
77
78
79static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
80{
Rob Landley3f785612006-05-28 01:06:36 +000081 udhcp_init_header(packet, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000082 packet->xid = oldpacket->xid;
83 memcpy(packet->chaddr, oldpacket->chaddr, 16);
84 packet->flags = oldpacket->flags;
85 packet->giaddr = oldpacket->giaddr;
86 packet->ciaddr = oldpacket->ciaddr;
87 add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server);
88}
89
90
91/* add in the bootp options */
92static void add_bootp_options(struct dhcpMessage *packet)
93{
94 packet->siaddr = server_config.siaddr;
95 if (server_config.sname)
96 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
97 if (server_config.boot_file)
98 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
99}
100
101
102/* send a DHCP OFFER to a DHCP DISCOVER */
103int sendOffer(struct dhcpMessage *oldpacket)
104{
105 struct dhcpMessage packet;
106 struct dhcpOfferedAddr *lease = NULL;
107 uint32_t req_align, lease_time_align = server_config.lease;
108 uint8_t *req, *lease_time;
109 struct option_set *curr;
110 struct in_addr addr;
111
112 uint32_t static_lease_ip;
113
114 init_packet(&packet, oldpacket, DHCPOFFER);
115
116 static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
117
118 /* ADDME: if static, short circuit */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000119 if (!static_lease_ip) {
120 /* the client is in our lease/offered table */
121 lease = find_lease_by_chaddr(oldpacket->chaddr);
122 if (lease) {
123 if (!lease_expired(lease))
124 lease_time_align = lease->expires - time(0);
125 packet.yiaddr = lease->yiaddr;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000126 /* Or the client has a requested ip */
127 } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP))
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000128 /* Don't look here (ugly hackish thing to do) */
129 && memcpy(&req_align, req, 4)
130 /* and the ip is in the lease range */
131 && ntohl(req_align) >= server_config.start_ip
132 && ntohl(req_align) <= server_config.end_ip
133 && !static_lease_ip /* Check that its not a static lease */
134 /* and is not already taken/offered */
135 && (!(lease = find_lease_by_yiaddr(req_align))
136 /* or its taken, but expired */ /* ADDME: or maybe in here */
137 || lease_expired(lease))
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000138 ) {
139 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
Mike Frysinger7031f622006-05-08 03:20:50 +0000140 /* otherwise, find a free IP */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000141 } else {
Mike Frysinger7031f622006-05-08 03:20:50 +0000142 /* Is it a static lease? (No, because find_address skips static lease) */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000143 packet.yiaddr = find_address(0);
144 /* try for an expired lease */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000145 if (!packet.yiaddr)
146 packet.yiaddr = find_address(1);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000147 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000148
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000149 if (!packet.yiaddr) {
150 bb_error_msg("no IP addresses to give - OFFER abandoned");
151 return -1;
152 }
153 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
154 bb_error_msg("lease pool is full - OFFER abandoned");
155 return -1;
156 }
157 lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
158 if (lease_time) {
159 memcpy(&lease_time_align, lease_time, 4);
160 lease_time_align = ntohl(lease_time_align);
161 if (lease_time_align > server_config.lease)
162 lease_time_align = server_config.lease;
163 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000164
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000165 /* Make sure we aren't just using the lease time from the previous offer */
166 if (lease_time_align < server_config.min_lease)
Mike Frysinger7031f622006-05-08 03:20:50 +0000167 lease_time_align = server_config.lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000168 /* ADDME: end of short circuit */
169 } else {
Mike Frysinger7031f622006-05-08 03:20:50 +0000170 /* It is a static lease... use it */
171 packet.yiaddr = static_lease_ip;
172 }
173
174 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
175
176 curr = server_config.options;
177 while (curr) {
178 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
179 add_option_string(packet.options, curr->data);
180 curr = curr->next;
181 }
182
183 add_bootp_options(&packet);
184
185 addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000186 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000187 return send_packet(&packet, 0);
188}
189
190
191int sendNAK(struct dhcpMessage *oldpacket)
192{
193 struct dhcpMessage packet;
194
195 init_packet(&packet, oldpacket, DHCPNAK);
196
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000197 DEBUG("Sending NAK");
Mike Frysinger7031f622006-05-08 03:20:50 +0000198 return send_packet(&packet, 1);
199}
200
201
202int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
203{
204 struct dhcpMessage packet;
205 struct option_set *curr;
206 uint8_t *lease_time;
207 uint32_t lease_time_align = server_config.lease;
208 struct in_addr addr;
209
210 init_packet(&packet, oldpacket, DHCPACK);
211 packet.yiaddr = yiaddr;
212
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000213 lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
214 if (lease_time) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000215 memcpy(&lease_time_align, lease_time, 4);
216 lease_time_align = ntohl(lease_time_align);
217 if (lease_time_align > server_config.lease)
218 lease_time_align = server_config.lease;
219 else if (lease_time_align < server_config.min_lease)
220 lease_time_align = server_config.lease;
221 }
222
223 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
224
225 curr = server_config.options;
226 while (curr) {
227 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
228 add_option_string(packet.options, curr->data);
229 curr = curr->next;
230 }
231
232 add_bootp_options(&packet);
233
234 addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000235 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000236
237 if (send_packet(&packet, 0) < 0)
238 return -1;
239
240 add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000241 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
242 /* rewrite the file with leases at every new acceptance */
243 write_leases();
244 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000245
246 return 0;
247}
248
249
250int send_inform(struct dhcpMessage *oldpacket)
251{
252 struct dhcpMessage packet;
253 struct option_set *curr;
254
255 init_packet(&packet, oldpacket, DHCPACK);
256
257 curr = server_config.options;
258 while (curr) {
259 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
260 add_option_string(packet.options, curr->data);
261 curr = curr->next;
262 }
263
264 add_bootp_options(&packet);
265
266 return send_packet(&packet, 0);
267}