blob: 831165d8e7bb4cb41160b50aeba19e5afc403e05 [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
Denys Vlasenko990a6172009-06-16 10:23:55 +020029/* send a packet to gateway_nip using the kernel ip stack */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020030static int send_packet_to_relay(struct dhcpMessage *dhcp_pkt)
Mike Frysinger7031f622006-05-08 03:20:50 +000031{
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020032 log1("Forwarding packet to relay");
Mike Frysinger7031f622006-05-08 03:20:50 +000033
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020034 return udhcp_send_kernel_packet(dhcp_pkt,
Denys Vlasenko990a6172009-06-16 10:23:55 +020035 server_config.server_nip, SERVER_PORT,
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020036 dhcp_pkt->gateway_nip, SERVER_PORT);
Mike Frysinger7031f622006-05-08 03:20:50 +000037}
38
39
Denys Vlasenko26918dd2009-06-16 12:04:23 +020040/* send a packet to a specific mac address and ip address by creating our own ip packet */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020041static int send_packet_to_client(struct dhcpMessage *dhcp_pkt, int force_broadcast)
Mike Frysinger7031f622006-05-08 03:20:50 +000042{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000043 const uint8_t *chaddr;
Mike Frysinger7031f622006-05-08 03:20:50 +000044 uint32_t ciaddr;
45
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020046 // Was:
47 //if (force_broadcast) { /* broadcast */ }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020048 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
49 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
50 //else { /* unicast to dhcp_pkt->yiaddr */ }
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020051 // But this is wrong: yiaddr is _our_ idea what client's IP is
52 // (for example, from lease file). Client may not know that,
53 // and may not have UDP socket listening on that IP!
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020054 // We should never unicast to dhcp_pkt->yiaddr!
55 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020056 // and can be used.
57
58 if (force_broadcast
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020059 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
60 || !dhcp_pkt->ciaddr
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020061 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020062 log1("Broadcasting packet to client");
Mike Frysinger7031f622006-05-08 03:20:50 +000063 ciaddr = INADDR_BROADCAST;
64 chaddr = MAC_BCAST_ADDR;
65 } else {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020066 log1("Unicasting packet to client ciaddr");
67 ciaddr = dhcp_pkt->ciaddr;
68 chaddr = dhcp_pkt->chaddr;
Mike Frysinger7031f622006-05-08 03:20:50 +000069 }
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020070
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020071 return udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko990a6172009-06-16 10:23:55 +020072 /*src*/ server_config.server_nip, SERVER_PORT,
Denis Vlasenkoc321b512008-09-26 16:29:12 +000073 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
74 server_config.ifindex);
Mike Frysinger7031f622006-05-08 03:20:50 +000075}
76
77
78/* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020079static int send_packet(struct dhcpMessage *dhcp_pkt, int force_broadcast)
Mike Frysinger7031f622006-05-08 03:20:50 +000080{
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020081 if (dhcp_pkt->gateway_nip)
82 return send_packet_to_relay(dhcp_pkt);
83 return send_packet_to_client(dhcp_pkt, force_broadcast);
Mike Frysinger7031f622006-05-08 03:20:50 +000084}
85
86
87static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type)
88{
Rob Landley3f785612006-05-28 01:06:36 +000089 udhcp_init_header(packet, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000090 packet->xid = oldpacket->xid;
91 memcpy(packet->chaddr, oldpacket->chaddr, 16);
92 packet->flags = oldpacket->flags;
Denys Vlasenko990a6172009-06-16 10:23:55 +020093 packet->gateway_nip = oldpacket->gateway_nip;
Mike Frysinger7031f622006-05-08 03:20:50 +000094 packet->ciaddr = oldpacket->ciaddr;
Denys Vlasenko990a6172009-06-16 10:23:55 +020095 add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
Mike Frysinger7031f622006-05-08 03:20:50 +000096}
97
98
99/* add in the bootp options */
100static void add_bootp_options(struct dhcpMessage *packet)
101{
Denys Vlasenko56f2d062009-06-16 10:25:35 +0200102 packet->siaddr_nip = server_config.siaddr_nip;
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 if (server_config.sname)
104 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
105 if (server_config.boot_file)
106 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
107}
108
109
110/* send a DHCP OFFER to a DHCP DISCOVER */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000111int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
Mike Frysinger7031f622006-05-08 03:20:50 +0000112{
113 struct dhcpMessage packet;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000114 uint32_t req_align;
115 uint32_t lease_time_aligned = server_config.lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000116 uint32_t static_lease_ip;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000117 uint8_t *req, *lease_time, *p_host_name;
Mike Frysinger7031f622006-05-08 03:20:50 +0000118 struct option_set *curr;
119 struct in_addr addr;
120
Mike Frysinger7031f622006-05-08 03:20:50 +0000121 init_packet(&packet, oldpacket, DHCPOFFER);
122
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200123 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000124
125 /* ADDME: if static, short circuit */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000126 if (!static_lease_ip) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000127 struct dhcpOfferedAddr *lease;
128
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000129 lease = find_lease_by_chaddr(oldpacket->chaddr);
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200130 /* The client is in our lease/offered table */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000131 if (lease) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000132 signed_leasetime_t tmp = lease->expires - time(NULL);
133 if (tmp >= 0)
134 lease_time_aligned = tmp;
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200135 packet.yiaddr = lease->lease_nip;
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200136 }
137 /* Or the client has requested an IP */
138 else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
139 /* (read IP) */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000140 && (move_from_unaligned32(req_align, req), 1)
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200141 /* and the IP is in the lease range */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000142 && ntohl(req_align) >= server_config.start_ip
143 && ntohl(req_align) <= server_config.end_ip
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000144 /* and is not already taken/offered */
145 && (!(lease = find_lease_by_yiaddr(req_align))
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000146 /* or its taken, but expired */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000147 || lease_expired(lease))
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000148 ) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000149 packet.yiaddr = req_align;
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200150 }
151 /* Otherwise, find a free IP */
152 else {
153 packet.yiaddr = find_free_or_expired_address(oldpacket->chaddr);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000154 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000155
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000156 if (!packet.yiaddr) {
157 bb_error_msg("no IP addresses to give - OFFER abandoned");
158 return -1;
159 }
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000160 p_host_name = get_option(oldpacket, DHCP_HOST_NAME);
161 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time, p_host_name)) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000162 bb_error_msg("lease pool is full - OFFER abandoned");
163 return -1;
164 }
165 lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
166 if (lease_time) {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000167 move_from_unaligned32(lease_time_aligned, lease_time);
168 lease_time_aligned = ntohl(lease_time_aligned);
169 if (lease_time_aligned > server_config.lease)
170 lease_time_aligned = server_config.lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000171 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000172
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000173 /* Make sure we aren't just using the lease time from the previous offer */
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000174 if (lease_time_aligned < server_config.min_lease)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000175 lease_time_aligned = server_config.min_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000176 } else {
Mike Frysinger7031f622006-05-08 03:20:50 +0000177 /* It is a static lease... use it */
178 packet.yiaddr = static_lease_ip;
179 }
180
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000181 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned));
Mike Frysinger7031f622006-05-08 03:20:50 +0000182
183 curr = server_config.options;
184 while (curr) {
185 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
186 add_option_string(packet.options, curr->data);
187 curr = curr->next;
188 }
189
190 add_bootp_options(&packet);
191
192 addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000193 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000194 return send_packet(&packet, 0);
195}
196
197
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000198int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket)
Mike Frysinger7031f622006-05-08 03:20:50 +0000199{
200 struct dhcpMessage packet;
201
202 init_packet(&packet, oldpacket, DHCPNAK);
203
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200204 log1("Sending NAK");
Mike Frysinger7031f622006-05-08 03:20:50 +0000205 return send_packet(&packet, 1);
206}
207
208
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000209int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000210{
211 struct dhcpMessage packet;
212 struct option_set *curr;
213 uint8_t *lease_time;
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000214 uint32_t lease_time_aligned = server_config.lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000215 struct in_addr addr;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000216 uint8_t *p_host_name;
Mike Frysinger7031f622006-05-08 03:20:50 +0000217
218 init_packet(&packet, oldpacket, DHCPACK);
219 packet.yiaddr = yiaddr;
220
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000221 lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
222 if (lease_time) {
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000223 move_from_unaligned32(lease_time_aligned, lease_time);
224 lease_time_aligned = ntohl(lease_time_aligned);
225 if (lease_time_aligned > server_config.lease)
226 lease_time_aligned = server_config.lease;
227 else if (lease_time_aligned < server_config.min_lease)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000228 lease_time_aligned = server_config.min_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000229 }
230
Denis Vlasenkoefb545b2008-12-08 22:56:18 +0000231 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned));
Mike Frysinger7031f622006-05-08 03:20:50 +0000232
233 curr = server_config.options;
234 while (curr) {
235 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
236 add_option_string(packet.options, curr->data);
237 curr = curr->next;
238 }
239
240 add_bootp_options(&packet);
241
242 addr.s_addr = packet.yiaddr;
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000243 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000244
245 if (send_packet(&packet, 0) < 0)
246 return -1;
247
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000248 p_host_name = get_option(oldpacket, DHCP_HOST_NAME);
249 add_lease(packet.chaddr, packet.yiaddr, lease_time_aligned, p_host_name);
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000250 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
251 /* rewrite the file with leases at every new acceptance */
252 write_leases();
253 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000254
255 return 0;
256}
257
258
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000259int FAST_FUNC send_inform(struct dhcpMessage *oldpacket)
Mike Frysinger7031f622006-05-08 03:20:50 +0000260{
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}