1 /* vi: set sw=4 ts=4: */
4 * Construct and send DHCP server packets
6 * Russ Dill <Russ.Dill@asu.edu> July 2001
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.
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.
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.
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
30 #include "serverpacket.h"
33 #include "static_leases.h"
35 /* send a packet to giaddr using the kernel ip stack */
36 static int send_packet_to_relay(struct dhcpMessage *payload)
38 DEBUG("Forwarding packet to relay");
40 return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,
41 payload->giaddr, SERVER_PORT);
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)
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;
64 DEBUG("unicasting packet to client yiaddr");
65 ciaddr = payload->yiaddr;
66 chaddr = payload->chaddr;
68 return udhcp_raw_packet(payload, server_config.server, SERVER_PORT,
69 ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
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)
79 ret = send_packet_to_relay(payload);
80 else ret = send_packet_to_client(payload, force_broadcast);
85 static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
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);
97 /* add in the bootp options */
98 static void add_bootp_options(struct dhcpMessage *packet)
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);
108 /* send a DHCP OFFER to a DHCP DISCOVER */
109 int sendOffer(struct dhcpMessage *oldpacket)
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;
118 uint32_t static_lease_ip;
120 init_packet(&packet, oldpacket, DHCPOFFER);
122 static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
124 /* ADDME: if static, short circuit */
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;
133 /* Or the client has a requested ip */
134 } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) &&
136 /* Don't look here (ugly hackish thing to do) */
137 memcpy(&req_align, req, 4) &&
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) &&
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)) ||
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? */
151 /* otherwise, find a free IP */
153 /* Is it a static lease? (No, because find_address skips static lease) */
154 packet.yiaddr = find_address(0);
156 /* try for an expired lease */
157 if (!packet.yiaddr) packet.yiaddr = find_address(1);
161 bb_error_msg("No IP addresses to give - OFFER abandoned");
165 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
166 bb_error_msg("Lease pool is full - OFFER abandoned");
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;
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;
181 /* ADDME: end of short circuit */
184 /* It is a static lease... use it */
185 packet.yiaddr = static_lease_ip;
188 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
190 curr = server_config.options;
192 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
193 add_option_string(packet.options, curr->data);
197 add_bootp_options(&packet);
199 addr.s_addr = packet.yiaddr;
200 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
201 return send_packet(&packet, 0);
205 int sendNAK(struct dhcpMessage *oldpacket)
207 struct dhcpMessage packet;
209 init_packet(&packet, oldpacket, DHCPNAK);
211 DEBUG("Sending NAK");
212 return send_packet(&packet, 1);
216 int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
218 struct dhcpMessage packet;
219 struct option_set *curr;
221 uint32_t lease_time_align = server_config.lease;
224 init_packet(&packet, oldpacket, DHCPACK);
225 packet.yiaddr = yiaddr;
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;
236 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align));
238 curr = server_config.options;
240 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
241 add_option_string(packet.options, curr->data);
245 add_bootp_options(&packet);
247 addr.s_addr = packet.yiaddr;
248 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
250 if (send_packet(&packet, 0) < 0)
253 add_lease(packet.chaddr, packet.yiaddr, lease_time_align);
259 int send_inform(struct dhcpMessage *oldpacket)
261 struct dhcpMessage packet;
262 struct option_set *curr;
264 init_packet(&packet, oldpacket, DHCPACK);
266 curr = server_config.options;
268 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
269 add_option_string(packet.options, curr->data);
273 add_bootp_options(&packet);
275 return send_packet(&packet, 0);