blob: e1a88addd0f4277cee6f253e3fcdc0bf954b7f46 [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"
Mike Frysinger7031f622006-05-08 03:20:50 +000024#include "dhcpd.h"
25#include "options.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000026
Mike Frysinger7031f622006-05-08 03:20:50 +000027
28/* send a packet to giaddr using the kernel ip stack */
29static int send_packet_to_relay(struct dhcpMessage *payload)
30{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000031 DEBUG("Forwarding packet to relay");
Mike Frysinger7031f622006-05-08 03:20:50 +000032
Rob Landley3f785612006-05-28 01:06:36 +000033 return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,
Mike Frysinger7031f622006-05-08 03:20:50 +000034 payload->giaddr, SERVER_PORT);
35}
36
37
38/* send a packet to a specific arp address and ip address by creating our own ip packet */
39static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
40{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000041 const uint8_t *chaddr;
Mike Frysinger7031f622006-05-08 03:20:50 +000042 uint32_t ciaddr;
43
44 if (force_broadcast) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000045 DEBUG("broadcasting packet to client (NAK)");
Mike Frysinger7031f622006-05-08 03:20:50 +000046 ciaddr = INADDR_BROADCAST;
47 chaddr = MAC_BCAST_ADDR;
48 } else if (payload->ciaddr) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000049 DEBUG("unicasting packet to client ciaddr");
Mike Frysinger7031f622006-05-08 03:20:50 +000050 ciaddr = payload->ciaddr;
51 chaddr = payload->chaddr;
52 } else if (ntohs(payload->flags) & BROADCAST_FLAG) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000053 DEBUG("broadcasting packet to client (requested)");
Mike Frysinger7031f622006-05-08 03:20:50 +000054 ciaddr = INADDR_BROADCAST;
55 chaddr = MAC_BCAST_ADDR;
56 } else {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000057 DEBUG("unicasting packet to client yiaddr");
Mike Frysinger7031f622006-05-08 03:20:50 +000058 ciaddr = payload->yiaddr;
59 chaddr = payload->chaddr;
60 }
Rob Landley3f785612006-05-28 01:06:36 +000061 return udhcp_raw_packet(payload, server_config.server, SERVER_PORT,
Mike Frysinger7031f622006-05-08 03:20:50 +000062 ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
63}
64
65
66/* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
67static int send_packet(struct dhcpMessage *payload, int force_broadcast)
68{
69 int ret;
70
71 if (payload->giaddr)
72 ret = send_packet_to_relay(payload);
73 else ret = send_packet_to_client(payload, force_broadcast);
74 return ret;
75}
76
77
78static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
79{
Rob Landley3f785612006-05-28 01:06:36 +000080 udhcp_init_header(packet, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000081 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 */
91static 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 */
102int sendOffer(struct dhcpMessage *oldpacket)
103{
104 struct dhcpMessage packet;
105 struct dhcpOfferedAddr *lease = NULL;
106 uint32_t req_align, lease_time_align = server_config.lease;
107 uint8_t *req, *lease_time;
108 struct option_set *curr;
109 struct in_addr addr;
110
111 uint32_t static_lease_ip;
112
113 init_packet(&packet, oldpacket, DHCPOFFER);
114
115 static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
116
117 /* ADDME: if static, short circuit */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000118 if (!static_lease_ip) {
119 /* the client is in our lease/offered table */
120 lease = find_lease_by_chaddr(oldpacket->chaddr);
121 if (lease) {
122 if (!lease_expired(lease))
123 lease_time_align = lease->expires - time(0);
124 packet.yiaddr = lease->yiaddr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000125
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))
128 /* 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) >= ntohl(server_config.start)
132 && ntohl(req_align) <= ntohl(server_config.end)
133 && !static_lease_ip /* Check that its not a static lease */
Mike Frysinger7031f622006-05-08 03:20:50 +0000134 /* and is not already taken/offered */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000135 && (!(lease = find_lease_by_yiaddr(req_align))
136 /* or its taken, but expired */ /* ADDME: or maybe in here */
137 || lease_expired(lease))
138 ) {
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 */
145 if (!packet.yiaddr) packet.yiaddr = find_address(1);
146 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000147
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000148 if (!packet.yiaddr) {
149 bb_error_msg("no IP addresses to give - OFFER abandoned");
150 return -1;
151 }
152 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
153 bb_error_msg("lease pool is full - OFFER abandoned");
154 return -1;
155 }
156 lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
157 if (lease_time) {
158 memcpy(&lease_time_align, lease_time, 4);
159 lease_time_align = ntohl(lease_time_align);
160 if (lease_time_align > server_config.lease)
161 lease_time_align = server_config.lease;
162 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000163
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000164 /* Make sure we aren't just using the lease time from the previous offer */
165 if (lease_time_align < server_config.min_lease)
Mike Frysinger7031f622006-05-08 03:20:50 +0000166 lease_time_align = server_config.lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000167 /* ADDME: end of short circuit */
168 } else {
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 /* It is a static lease... use it */
170 packet.yiaddr = static_lease_ip;
171 }
172
173 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
174
175 curr = server_config.options;
176 while (curr) {
177 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
178 add_option_string(packet.options, curr->data);
179 curr = curr->next;
180 }
181
182 add_bootp_options(&packet);
183
184 addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000185 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000186 return send_packet(&packet, 0);
187}
188
189
190int sendNAK(struct dhcpMessage *oldpacket)
191{
192 struct dhcpMessage packet;
193
194 init_packet(&packet, oldpacket, DHCPNAK);
195
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000196 DEBUG("Sending NAK");
Mike Frysinger7031f622006-05-08 03:20:50 +0000197 return send_packet(&packet, 1);
198}
199
200
201int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
202{
203 struct dhcpMessage packet;
204 struct option_set *curr;
205 uint8_t *lease_time;
206 uint32_t lease_time_align = server_config.lease;
207 struct in_addr addr;
208
209 init_packet(&packet, oldpacket, DHCPACK);
210 packet.yiaddr = yiaddr;
211
212 if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {
213 memcpy(&lease_time_align, lease_time, 4);
214 lease_time_align = ntohl(lease_time_align);
215 if (lease_time_align > server_config.lease)
216 lease_time_align = server_config.lease;
217 else if (lease_time_align < server_config.min_lease)
218 lease_time_align = server_config.lease;
219 }
220
221 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
222
223 curr = server_config.options;
224 while (curr) {
225 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
226 add_option_string(packet.options, curr->data);
227 curr = curr->next;
228 }
229
230 add_bootp_options(&packet);
231
232 addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000233 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000234
235 if (send_packet(&packet, 0) < 0)
236 return -1;
237
238 add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
239
240 return 0;
241}
242
243
244int send_inform(struct dhcpMessage *oldpacket)
245{
246 struct dhcpMessage packet;
247 struct option_set *curr;
248
249 init_packet(&packet, oldpacket, DHCPACK);
250
251 curr = server_config.options;
252 while (curr) {
253 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
254 add_option_string(packet.options, curr->data);
255 curr = curr->next;
256 }
257
258 add_bootp_options(&packet);
259
260 return send_packet(&packet, 0);
261}