"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 | |
| 29 | /* send a packet to giaddr using the kernel ip stack */ |
| 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 | |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 34 | return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 35 | payload->giaddr, SERVER_PORT); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /* send a packet to a specific arp address and ip address by creating our own ip packet */ |
| 40 | static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast) |
| 41 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 42 | const uint8_t *chaddr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | uint32_t ciaddr; |
| 44 | |
| 45 | if (force_broadcast) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 46 | DEBUG("broadcasting packet to client (NAK)"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 47 | ciaddr = INADDR_BROADCAST; |
| 48 | chaddr = MAC_BCAST_ADDR; |
| 49 | } else if (payload->ciaddr) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 50 | DEBUG("unicasting packet to client ciaddr"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 51 | ciaddr = payload->ciaddr; |
| 52 | chaddr = payload->chaddr; |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 53 | } else if (payload->flags & htons(BROADCAST_FLAG)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 54 | DEBUG("broadcasting packet to client (requested)"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | ciaddr = INADDR_BROADCAST; |
| 56 | chaddr = MAC_BCAST_ADDR; |
| 57 | } else { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 58 | DEBUG("unicasting packet to client yiaddr"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 59 | ciaddr = payload->yiaddr; |
| 60 | chaddr = payload->chaddr; |
| 61 | } |
Denis Vlasenko | c321b51 | 2008-09-26 16:29:12 +0000 | [diff] [blame] | 62 | return udhcp_send_raw_packet(payload, |
| 63 | /*src*/ server_config.server, SERVER_PORT, |
| 64 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
| 65 | server_config.ifindex); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ |
| 70 | static int send_packet(struct dhcpMessage *payload, int force_broadcast) |
| 71 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | if (payload->giaddr) |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 73 | return send_packet_to_relay(payload); |
| 74 | return send_packet_to_client(payload, force_broadcast); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | static void init_packet(struct dhcpMessage *packet, struct dhcpMessage *oldpacket, char type) |
| 79 | { |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 80 | udhcp_init_header(packet, type); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 81 | 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 */ |
| 91 | static 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 */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 102 | int FAST_FUNC send_offer(struct dhcpMessage *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | { |
| 104 | struct dhcpMessage packet; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 105 | uint32_t req_align; |
| 106 | uint32_t lease_time_aligned = server_config.lease; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 107 | uint32_t static_lease_ip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 108 | uint8_t *req, *lease_time; |
| 109 | struct option_set *curr; |
| 110 | struct in_addr addr; |
| 111 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | init_packet(&packet, oldpacket, DHCPOFFER); |
| 113 | |
| 114 | static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr); |
| 115 | |
| 116 | /* ADDME: if static, short circuit */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 117 | if (!static_lease_ip) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 118 | struct dhcpOfferedAddr *lease; |
| 119 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 120 | lease = find_lease_by_chaddr(oldpacket->chaddr); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 121 | /* the client is in our lease/offered table */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 122 | if (lease) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 123 | signed_leasetime_t tmp = lease->expires - time(NULL); |
| 124 | if (tmp >= 0) |
| 125 | lease_time_aligned = tmp; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 126 | packet.yiaddr = lease->yiaddr; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 127 | /* Or the client has requested an ip */ |
| 128 | } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 129 | /* Don't look here (ugly hackish thing to do) */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 130 | && (move_from_unaligned32(req_align, req), 1) |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 131 | /* and the ip is in the lease range */ |
| 132 | && ntohl(req_align) >= server_config.start_ip |
| 133 | && ntohl(req_align) <= server_config.end_ip |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 134 | /* and is not already taken/offered */ |
| 135 | && (!(lease = find_lease_by_yiaddr(req_align)) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 136 | /* or its taken, but expired */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 137 | || lease_expired(lease)) |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 138 | ) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 139 | packet.yiaddr = req_align; |
| 140 | /* otherwise, find a free IP */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 141 | } else { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 142 | packet.yiaddr = find_free_or_expired_address(); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 143 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 145 | if (!packet.yiaddr) { |
| 146 | bb_error_msg("no IP addresses to give - OFFER abandoned"); |
| 147 | return -1; |
| 148 | } |
| 149 | if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) { |
| 150 | bb_error_msg("lease pool is full - OFFER abandoned"); |
| 151 | return -1; |
| 152 | } |
| 153 | lease_time = get_option(oldpacket, DHCP_LEASE_TIME); |
| 154 | if (lease_time) { |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 155 | move_from_unaligned32(lease_time_aligned, lease_time); |
| 156 | lease_time_aligned = ntohl(lease_time_aligned); |
| 157 | if (lease_time_aligned > server_config.lease) |
| 158 | lease_time_aligned = server_config.lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 159 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 160 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 161 | /* 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] | 162 | if (lease_time_aligned < server_config.min_lease) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 163 | lease_time_aligned = server_config.min_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 164 | } else { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 165 | /* It is a static lease... use it */ |
| 166 | packet.yiaddr = static_lease_ip; |
| 167 | } |
| 168 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 169 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 170 | |
| 171 | curr = server_config.options; |
| 172 | while (curr) { |
| 173 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 174 | add_option_string(packet.options, curr->data); |
| 175 | curr = curr->next; |
| 176 | } |
| 177 | |
| 178 | add_bootp_options(&packet); |
| 179 | |
| 180 | addr.s_addr = packet.yiaddr; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 181 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 182 | return send_packet(&packet, 0); |
| 183 | } |
| 184 | |
| 185 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 186 | int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 187 | { |
| 188 | struct dhcpMessage packet; |
| 189 | |
| 190 | init_packet(&packet, oldpacket, DHCPNAK); |
| 191 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 192 | DEBUG("Sending NAK"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 193 | return send_packet(&packet, 1); |
| 194 | } |
| 195 | |
| 196 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 197 | int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 198 | { |
| 199 | struct dhcpMessage packet; |
| 200 | struct option_set *curr; |
| 201 | uint8_t *lease_time; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 202 | uint32_t lease_time_aligned = server_config.lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 203 | struct in_addr addr; |
| 204 | |
| 205 | init_packet(&packet, oldpacket, DHCPACK); |
| 206 | packet.yiaddr = yiaddr; |
| 207 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 208 | lease_time = get_option(oldpacket, DHCP_LEASE_TIME); |
| 209 | if (lease_time) { |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 210 | move_from_unaligned32(lease_time_aligned, lease_time); |
| 211 | lease_time_aligned = ntohl(lease_time_aligned); |
| 212 | if (lease_time_aligned > server_config.lease) |
| 213 | lease_time_aligned = server_config.lease; |
| 214 | else if (lease_time_aligned < server_config.min_lease) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 215 | lease_time_aligned = server_config.min_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 218 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 219 | |
| 220 | curr = server_config.options; |
| 221 | while (curr) { |
| 222 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 223 | add_option_string(packet.options, curr->data); |
| 224 | curr = curr->next; |
| 225 | } |
| 226 | |
| 227 | add_bootp_options(&packet); |
| 228 | |
| 229 | addr.s_addr = packet.yiaddr; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 230 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 231 | |
| 232 | if (send_packet(&packet, 0) < 0) |
| 233 | return -1; |
| 234 | |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 235 | add_lease(packet.chaddr, packet.yiaddr, lease_time_aligned); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 236 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
| 237 | /* rewrite the file with leases at every new acceptance */ |
| 238 | write_leases(); |
| 239 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 245 | int FAST_FUNC send_inform(struct dhcpMessage *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 246 | { |
| 247 | struct dhcpMessage packet; |
| 248 | struct option_set *curr; |
| 249 | |
| 250 | init_packet(&packet, oldpacket, DHCPACK); |
| 251 | |
| 252 | curr = server_config.options; |
| 253 | while (curr) { |
| 254 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 255 | add_option_string(packet.options, curr->data); |
| 256 | curr = curr->next; |
| 257 | } |
| 258 | |
| 259 | add_bootp_options(&packet); |
| 260 | |
| 261 | return send_packet(&packet, 0); |
| 262 | } |