Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* serverpacket.c |
| 2 | * |
| 3 | * Constuct and send DHCP server packets |
| 4 | * |
| 5 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <string.h> |
| 26 | #include <time.h> |
| 27 | |
Russ Dill | 76729b8 | 2003-12-16 20:44:15 +0000 | [diff] [blame] | 28 | #include "serverpacket.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 29 | #include "dhcpd.h" |
| 30 | #include "options.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 31 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 32 | |
| 33 | /* send a packet to giaddr using the kernel ip stack */ |
| 34 | static int send_packet_to_relay(struct dhcpMessage *payload) |
| 35 | { |
| 36 | DEBUG(LOG_INFO, "Forwarding packet to relay"); |
| 37 | |
| 38 | return kernel_packet(payload, server_config.server, SERVER_PORT, |
| 39 | payload->giaddr, SERVER_PORT); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /* send a packet to a specific arp address and ip address by creating our own ip packet */ |
| 44 | static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast) |
| 45 | { |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 46 | uint8_t *chaddr; |
| 47 | uint32_t ciaddr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 48 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 49 | if (force_broadcast) { |
| 50 | DEBUG(LOG_INFO, "broadcasting packet to client (NAK)"); |
| 51 | ciaddr = INADDR_BROADCAST; |
| 52 | chaddr = MAC_BCAST_ADDR; |
| 53 | } else if (payload->ciaddr) { |
| 54 | DEBUG(LOG_INFO, "unicasting packet to client ciaddr"); |
| 55 | ciaddr = payload->ciaddr; |
| 56 | chaddr = payload->chaddr; |
| 57 | } else if (ntohs(payload->flags) & BROADCAST_FLAG) { |
| 58 | DEBUG(LOG_INFO, "broadcasting packet to client (requested)"); |
| 59 | ciaddr = INADDR_BROADCAST; |
| 60 | chaddr = MAC_BCAST_ADDR; |
| 61 | } else { |
| 62 | DEBUG(LOG_INFO, "unicasting packet to client yiaddr"); |
| 63 | ciaddr = payload->yiaddr; |
| 64 | chaddr = payload->chaddr; |
| 65 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 66 | return raw_packet(payload, server_config.server, SERVER_PORT, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 67 | ciaddr, CLIENT_PORT, chaddr, server_config.ifindex); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ |
| 72 | static int send_packet(struct dhcpMessage *payload, int force_broadcast) |
| 73 | { |
| 74 | int ret; |
| 75 | |
| 76 | if (payload->giaddr) |
| 77 | ret = send_packet_to_relay(payload); |
| 78 | else ret = send_packet_to_client(payload, force_broadcast); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type) |
| 84 | { |
| 85 | init_header(packet, type); |
| 86 | packet->xid = oldpacket->xid; |
| 87 | memcpy(packet->chaddr, oldpacket->chaddr, 16); |
| 88 | packet->flags = oldpacket->flags; |
| 89 | packet->giaddr = oldpacket->giaddr; |
| 90 | packet->ciaddr = oldpacket->ciaddr; |
| 91 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* add in the bootp options */ |
| 96 | static void add_bootp_options(struct dhcpMessage *packet) |
| 97 | { |
| 98 | packet->siaddr = server_config.siaddr; |
| 99 | if (server_config.sname) |
| 100 | strncpy(packet->sname, server_config.sname, sizeof(packet->sname) - 1); |
| 101 | if (server_config.boot_file) |
| 102 | strncpy(packet->file, server_config.boot_file, sizeof(packet->file) - 1); |
| 103 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 104 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 105 | |
| 106 | /* send a DHCP OFFER to a DHCP DISCOVER */ |
| 107 | int sendOffer(struct dhcpMessage *oldpacket) |
| 108 | { |
| 109 | struct dhcpMessage packet; |
| 110 | struct dhcpOfferedAddr *lease = NULL; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 111 | uint32_t req_align, lease_time_align = server_config.lease; |
| 112 | uint8_t *req, *lease_time; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 113 | struct option_set *curr; |
| 114 | struct in_addr addr; |
| 115 | |
| 116 | init_packet(&packet, oldpacket, DHCPOFFER); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 117 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 118 | /* ADDME: if static, short circuit */ |
| 119 | /* the client is in our lease/offered table */ |
| 120 | if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 121 | if (!lease_expired(lease)) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 122 | lease_time_align = lease->expires - time(0); |
| 123 | packet.yiaddr = lease->yiaddr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 124 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 125 | /* Or the client has a requested ip */ |
| 126 | } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) && |
| 127 | |
| 128 | /* Don't look here (ugly hackish thing to do) */ |
| 129 | memcpy(&req_align, req, 4) && |
| 130 | |
| 131 | /* and the ip is in the lease range */ |
| 132 | ntohl(req_align) >= ntohl(server_config.start) && |
| 133 | ntohl(req_align) <= ntohl(server_config.end) && |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 134 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 135 | /* and its not already taken/offered */ /* ADDME: check that its not a static lease */ |
| 136 | ((!(lease = find_lease_by_yiaddr(req_align)) || |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 137 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 138 | /* or its taken, but expired */ /* ADDME: or maybe in here */ |
| 139 | lease_expired(lease)))) { |
| 140 | packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */ |
| 141 | |
| 142 | /* otherwise, find a free IP */ /*ADDME: is it a static lease? */ |
| 143 | } else { |
| 144 | packet.yiaddr = find_address(0); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 145 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 146 | /* try for an expired lease */ |
| 147 | if (!packet.yiaddr) packet.yiaddr = find_address(1); |
| 148 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 149 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 150 | if(!packet.yiaddr) { |
| 151 | LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned"); |
| 152 | return -1; |
| 153 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 154 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 155 | if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) { |
| 156 | LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned"); |
| 157 | return -1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 158 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 159 | |
| 160 | if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) { |
| 161 | memcpy(&lease_time_align, lease_time, 4); |
| 162 | lease_time_align = ntohl(lease_time_align); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 163 | if (lease_time_align > server_config.lease) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 164 | lease_time_align = server_config.lease; |
| 165 | } |
| 166 | |
| 167 | /* Make sure we aren't just using the lease time from the previous offer */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 168 | if (lease_time_align < server_config.min_lease) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 169 | lease_time_align = server_config.lease; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 170 | /* ADDME: end of short circuit */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 171 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align)); |
| 172 | |
| 173 | curr = server_config.options; |
| 174 | while (curr) { |
| 175 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 176 | add_option_string(packet.options, curr->data); |
| 177 | curr = curr->next; |
| 178 | } |
| 179 | |
| 180 | add_bootp_options(&packet); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 181 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 182 | addr.s_addr = packet.yiaddr; |
| 183 | LOG(LOG_INFO, "sending OFFER of %s", inet_ntoa(addr)); |
| 184 | return send_packet(&packet, 0); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | int sendNAK(struct dhcpMessage *oldpacket) |
| 189 | { |
| 190 | struct dhcpMessage packet; |
| 191 | |
| 192 | init_packet(&packet, oldpacket, DHCPNAK); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 193 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 194 | DEBUG(LOG_INFO, "sending NAK"); |
| 195 | return send_packet(&packet, 1); |
| 196 | } |
| 197 | |
| 198 | |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 199 | int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 200 | { |
| 201 | struct dhcpMessage packet; |
| 202 | struct option_set *curr; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 203 | uint8_t *lease_time; |
| 204 | uint32_t lease_time_align = server_config.lease; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 205 | struct in_addr addr; |
| 206 | |
| 207 | init_packet(&packet, oldpacket, DHCPACK); |
| 208 | packet.yiaddr = yiaddr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 209 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 210 | if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) { |
| 211 | memcpy(&lease_time_align, lease_time, 4); |
| 212 | lease_time_align = ntohl(lease_time_align); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 213 | if (lease_time_align > server_config.lease) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 214 | lease_time_align = server_config.lease; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 215 | else if (lease_time_align < server_config.min_lease) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 216 | lease_time_align = server_config.lease; |
| 217 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 218 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 219 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_align)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 220 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 221 | curr = server_config.options; |
| 222 | while (curr) { |
| 223 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 224 | add_option_string(packet.options, curr->data); |
| 225 | curr = curr->next; |
| 226 | } |
| 227 | |
| 228 | add_bootp_options(&packet); |
| 229 | |
| 230 | addr.s_addr = packet.yiaddr; |
| 231 | LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr)); |
| 232 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 233 | if (send_packet(&packet, 0) < 0) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 234 | return -1; |
| 235 | |
| 236 | add_lease(packet.chaddr, packet.yiaddr, lease_time_align); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | int send_inform(struct dhcpMessage *oldpacket) |
| 243 | { |
| 244 | struct dhcpMessage packet; |
| 245 | struct option_set *curr; |
| 246 | |
| 247 | init_packet(&packet, oldpacket, DHCPACK); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 248 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 249 | curr = server_config.options; |
| 250 | while (curr) { |
| 251 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 252 | add_option_string(packet.options, curr->data); |
| 253 | curr = curr->next; |
| 254 | } |
| 255 | |
| 256 | add_bootp_options(&packet); |
| 257 | |
| 258 | return send_packet(&packet, 0); |
| 259 | } |