"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* 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 Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 23 | #include "common.h" |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 24 | #include "dhcpc.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | #include "dhcpd.h" |
| 26 | #include "options.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 27 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 28 | |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 29 | /* send a packet to gateway_nip using the kernel ip stack */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 30 | static int send_packet_to_relay(struct dhcpMessage *payload) |
| 31 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 32 | DEBUG("Forwarding packet to relay"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 33 | |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 34 | return udhcp_send_kernel_packet(payload, |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 35 | server_config.server_nip, SERVER_PORT, |
| 36 | payload->gateway_nip, SERVER_PORT); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
| 40 | /* send a packet to a specific arp address and ip address by creating our own ip packet */ |
| 41 | static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast) |
| 42 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 43 | const uint8_t *chaddr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 44 | uint32_t ciaddr; |
| 45 | |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 46 | // Was: |
| 47 | //if (force_broadcast) { /* broadcast */ } |
| 48 | //else if (payload->ciaddr) { /* unicast to payload->ciaddr */ } |
| 49 | //else if (payload->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } |
| 50 | //else { /* unicast to payload->yiaddr */ } |
| 51 | // 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! |
| 54 | // We should never unicast to payload->yiaddr! |
| 55 | // payload->ciaddr, OTOH, comes from client's request packet, |
| 56 | // and can be used. |
| 57 | |
| 58 | if (force_broadcast |
| 59 | || (payload->flags & htons(BROADCAST_FLAG)) |
| 60 | || !payload->ciaddr |
| 61 | ) { |
| 62 | DEBUG("broadcasting packet to client"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 63 | ciaddr = INADDR_BROADCAST; |
| 64 | chaddr = MAC_BCAST_ADDR; |
| 65 | } else { |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 66 | DEBUG("unicasting packet to client ciaddr"); |
| 67 | ciaddr = payload->ciaddr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 68 | chaddr = payload->chaddr; |
| 69 | } |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 70 | |
Denis Vlasenko | c321b51 | 2008-09-26 16:29:12 +0000 | [diff] [blame] | 71 | return udhcp_send_raw_packet(payload, |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 72 | /*src*/ server_config.server_nip, SERVER_PORT, |
Denis Vlasenko | c321b51 | 2008-09-26 16:29:12 +0000 | [diff] [blame] | 73 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
| 74 | server_config.ifindex); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ |
| 79 | static int send_packet(struct dhcpMessage *payload, int force_broadcast) |
| 80 | { |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 81 | if (payload->gateway_nip) |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 82 | return send_packet_to_relay(payload); |
| 83 | return send_packet_to_client(payload, force_broadcast); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type) |
| 88 | { |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 89 | udhcp_init_header(packet, type); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | packet->xid = oldpacket->xid; |
| 91 | memcpy(packet->chaddr, oldpacket->chaddr, 16); |
| 92 | packet->flags = oldpacket->flags; |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 93 | packet->gateway_nip = oldpacket->gateway_nip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 94 | packet->ciaddr = oldpacket->ciaddr; |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 95 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | /* add in the bootp options */ |
| 100 | static void add_bootp_options(struct dhcpMessage *packet) |
| 101 | { |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame^] | 102 | packet->siaddr_nip = server_config.siaddr_nip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | 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 Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 111 | int FAST_FUNC send_offer(struct dhcpMessage *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | { |
| 113 | struct dhcpMessage packet; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 114 | uint32_t req_align; |
| 115 | uint32_t lease_time_aligned = server_config.lease; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 116 | uint32_t static_lease_ip; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 117 | uint8_t *req, *lease_time, *p_host_name; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 118 | struct option_set *curr; |
| 119 | struct in_addr addr; |
| 120 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 121 | init_packet(&packet, oldpacket, DHCPOFFER); |
| 122 | |
| 123 | static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr); |
| 124 | |
| 125 | /* ADDME: if static, short circuit */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 126 | if (!static_lease_ip) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 127 | struct dhcpOfferedAddr *lease; |
| 128 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 129 | lease = find_lease_by_chaddr(oldpacket->chaddr); |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 130 | /* The client is in our lease/offered table */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 131 | if (lease) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 132 | signed_leasetime_t tmp = lease->expires - time(NULL); |
| 133 | if (tmp >= 0) |
| 134 | lease_time_aligned = tmp; |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 135 | packet.yiaddr = lease->lease_nip; |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 136 | } |
| 137 | /* Or the client has requested an IP */ |
| 138 | else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL |
| 139 | /* (read IP) */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 140 | && (move_from_unaligned32(req_align, req), 1) |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 141 | /* and the IP is in the lease range */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 142 | && ntohl(req_align) >= server_config.start_ip |
| 143 | && ntohl(req_align) <= server_config.end_ip |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 144 | /* and is not already taken/offered */ |
| 145 | && (!(lease = find_lease_by_yiaddr(req_align)) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 146 | /* or its taken, but expired */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 147 | || lease_expired(lease)) |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 148 | ) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 149 | packet.yiaddr = req_align; |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 150 | } |
| 151 | /* Otherwise, find a free IP */ |
| 152 | else { |
| 153 | packet.yiaddr = find_free_or_expired_address(oldpacket->chaddr); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 154 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 156 | if (!packet.yiaddr) { |
| 157 | bb_error_msg("no IP addresses to give - OFFER abandoned"); |
| 158 | return -1; |
| 159 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 160 | 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 Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 162 | 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 Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 167 | 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 Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 171 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 172 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 173 | /* Make sure we aren't just using the lease time from the previous offer */ |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 174 | if (lease_time_aligned < server_config.min_lease) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 175 | lease_time_aligned = server_config.min_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 176 | } else { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 177 | /* It is a static lease... use it */ |
| 178 | packet.yiaddr = static_lease_ip; |
| 179 | } |
| 180 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 181 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 182 | |
| 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 Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 193 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 194 | return send_packet(&packet, 0); |
| 195 | } |
| 196 | |
| 197 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 198 | int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 199 | { |
| 200 | struct dhcpMessage packet; |
| 201 | |
| 202 | init_packet(&packet, oldpacket, DHCPNAK); |
| 203 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 204 | DEBUG("Sending NAK"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 205 | return send_packet(&packet, 1); |
| 206 | } |
| 207 | |
| 208 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 209 | int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 210 | { |
| 211 | struct dhcpMessage packet; |
| 212 | struct option_set *curr; |
| 213 | uint8_t *lease_time; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 214 | uint32_t lease_time_aligned = server_config.lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 215 | struct in_addr addr; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 216 | uint8_t *p_host_name; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 217 | |
| 218 | init_packet(&packet, oldpacket, DHCPACK); |
| 219 | packet.yiaddr = yiaddr; |
| 220 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 221 | lease_time = get_option(oldpacket, DHCP_LEASE_TIME); |
| 222 | if (lease_time) { |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 223 | 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 Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 228 | lease_time_aligned = server_config.min_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 231 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 232 | |
| 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 Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 243 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 244 | |
| 245 | if (send_packet(&packet, 0) < 0) |
| 246 | return -1; |
| 247 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 248 | p_host_name = get_option(oldpacket, DHCP_HOST_NAME); |
| 249 | add_lease(packet.chaddr, packet.yiaddr, lease_time_aligned, p_host_name); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 250 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
| 251 | /* rewrite the file with leases at every new acceptance */ |
| 252 | write_leases(); |
| 253 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 259 | int FAST_FUNC send_inform(struct dhcpMessage *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 260 | { |
| 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 | } |