diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-03-20 03:48:11 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-03-20 03:48:11 +0100 |
| commit | 8a7c166ab9ad35eef8c90d6b0b3a25b8a9fbacd2 (patch) | |
| tree | 150a58a93b9698a4db16187ef1952deda0acae16 | |
| parent | de9e772f46eeba347c21014d4b159f28c2c2d9c1 (diff) | |
| download | busybox-w32-8a7c166ab9ad35eef8c90d6b0b3a25b8a9fbacd2.tar.gz busybox-w32-8a7c166ab9ad35eef8c90d6b0b3a25b8a9fbacd2.tar.bz2 busybox-w32-8a7c166ab9ad35eef8c90d6b0b3a25b8a9fbacd2.zip | |
udhcp: move serverpacket.c into dhcpd.c, no other changes
function old new delta
udhcpd_main 1480 1956 +476
send_inform 83 - -83
send_offer 376 - -376
------------------------------------------------------------------------------
(add/remove: 0/3 grow/shrink: 1/0 up/down: 476/-459) Total: 17 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | networking/udhcp/Kbuild | 3 | ||||
| -rw-r--r-- | networking/udhcp/dhcpd.c | 265 | ||||
| -rw-r--r-- | networking/udhcp/dhcpd.h | 8 | ||||
| -rw-r--r-- | networking/udhcp/serverpacket.c | 283 |
4 files changed, 263 insertions, 296 deletions
diff --git a/networking/udhcp/Kbuild b/networking/udhcp/Kbuild index d1d35670f..138a950aa 100644 --- a/networking/udhcp/Kbuild +++ b/networking/udhcp/Kbuild | |||
| @@ -17,8 +17,7 @@ lib-$(CONFIG_UDHCPC) += dhcpc.o clientpacket.o clientsocket.o \ | |||
| 17 | UDHCPC_NEEDS_ARPING-$(CONFIG_FEATURE_UDHCPC_ARPING) = y | 17 | UDHCPC_NEEDS_ARPING-$(CONFIG_FEATURE_UDHCPC_ARPING) = y |
| 18 | lib-$(UDHCPC_NEEDS_ARPING-y) += arpping.o | 18 | lib-$(UDHCPC_NEEDS_ARPING-y) += arpping.o |
| 19 | 19 | ||
| 20 | lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o \ | 20 | lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o files.o leases.o static_leases.o |
| 21 | serverpacket.o static_leases.o | ||
| 22 | 21 | ||
| 23 | lib-$(CONFIG_DUMPLEASES) += dumpleases.o | 22 | lib-$(CONFIG_DUMPLEASES) += dumpleases.o |
| 24 | lib-$(CONFIG_DHCPRELAY) += dhcprelay.o | 23 | lib-$(CONFIG_DHCPRELAY) += dhcprelay.o |
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index dc5d9ffe9..26fb3045f 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c | |||
| @@ -1,13 +1,24 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* dhcpd.c | 2 | /* |
| 3 | * | ||
| 4 | * udhcp Server | 3 | * udhcp Server |
| 5 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> | 4 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> |
| 6 | * Chris Trew <ctrew@moreton.com.au> | 5 | * Chris Trew <ctrew@moreton.com.au> |
| 7 | * | 6 | * |
| 8 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 | 7 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 9 | * | 8 | * |
| 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by | ||
| 11 | * the Free Software Foundation; either version 2 of the License, or | ||
| 12 | * (at your option) any later version. | ||
| 13 | * | ||
| 14 | * This program is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | * GNU General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU General Public License | ||
| 20 | * along with this program; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 11 | */ | 22 | */ |
| 12 | 23 | ||
| 13 | #include <syslog.h> | 24 | #include <syslog.h> |
| @@ -17,6 +28,254 @@ | |||
| 17 | #include "options.h" | 28 | #include "options.h" |
| 18 | 29 | ||
| 19 | 30 | ||
| 31 | /* send a packet to gateway_nip using the kernel ip stack */ | ||
| 32 | static int send_packet_to_relay(struct dhcp_packet *dhcp_pkt) | ||
| 33 | { | ||
| 34 | log1("Forwarding packet to relay"); | ||
| 35 | |||
| 36 | return udhcp_send_kernel_packet(dhcp_pkt, | ||
| 37 | server_config.server_nip, SERVER_PORT, | ||
| 38 | dhcp_pkt->gateway_nip, SERVER_PORT); | ||
| 39 | } | ||
| 40 | |||
| 41 | /* send a packet to a specific mac address and ip address by creating our own ip packet */ | ||
| 42 | static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast) | ||
| 43 | { | ||
| 44 | const uint8_t *chaddr; | ||
| 45 | uint32_t ciaddr; | ||
| 46 | |||
| 47 | // Was: | ||
| 48 | //if (force_broadcast) { /* broadcast */ } | ||
| 49 | //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ } | ||
| 50 | //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } | ||
| 51 | //else { /* unicast to dhcp_pkt->yiaddr */ } | ||
| 52 | // But this is wrong: yiaddr is _our_ idea what client's IP is | ||
| 53 | // (for example, from lease file). Client may not know that, | ||
| 54 | // and may not have UDP socket listening on that IP! | ||
| 55 | // We should never unicast to dhcp_pkt->yiaddr! | ||
| 56 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, | ||
| 57 | // and can be used. | ||
| 58 | |||
| 59 | if (force_broadcast | ||
| 60 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) | ||
| 61 | || !dhcp_pkt->ciaddr | ||
| 62 | ) { | ||
| 63 | log1("Broadcasting packet to client"); | ||
| 64 | ciaddr = INADDR_BROADCAST; | ||
| 65 | chaddr = MAC_BCAST_ADDR; | ||
| 66 | } else { | ||
| 67 | log1("Unicasting packet to client ciaddr"); | ||
| 68 | ciaddr = dhcp_pkt->ciaddr; | ||
| 69 | chaddr = dhcp_pkt->chaddr; | ||
| 70 | } | ||
| 71 | |||
| 72 | return udhcp_send_raw_packet(dhcp_pkt, | ||
| 73 | /*src*/ server_config.server_nip, SERVER_PORT, | ||
| 74 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, | ||
| 75 | server_config.ifindex); | ||
| 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 dhcp_packet *dhcp_pkt, int force_broadcast) | ||
| 80 | { | ||
| 81 | if (dhcp_pkt->gateway_nip) | ||
| 82 | return send_packet_to_relay(dhcp_pkt); | ||
| 83 | return send_packet_to_client(dhcp_pkt, force_broadcast); | ||
| 84 | } | ||
| 85 | |||
| 86 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) | ||
| 87 | { | ||
| 88 | udhcp_init_header(packet, type); | ||
| 89 | packet->xid = oldpacket->xid; | ||
| 90 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); | ||
| 91 | packet->flags = oldpacket->flags; | ||
| 92 | packet->gateway_nip = oldpacket->gateway_nip; | ||
| 93 | packet->ciaddr = oldpacket->ciaddr; | ||
| 94 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); | ||
| 95 | } | ||
| 96 | |||
| 97 | /* add in the bootp options */ | ||
| 98 | static void add_bootp_options(struct dhcp_packet *packet) | ||
| 99 | { | ||
| 100 | packet->siaddr_nip = server_config.siaddr_nip; | ||
| 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); | ||
| 105 | } | ||
| 106 | |||
| 107 | static uint32_t select_lease_time(struct dhcp_packet *packet) | ||
| 108 | { | ||
| 109 | uint32_t lease_time_sec = server_config.max_lease_sec; | ||
| 110 | uint8_t *lease_time_opt = get_option(packet, DHCP_LEASE_TIME); | ||
| 111 | if (lease_time_opt) { | ||
| 112 | move_from_unaligned32(lease_time_sec, lease_time_opt); | ||
| 113 | lease_time_sec = ntohl(lease_time_sec); | ||
| 114 | if (lease_time_sec > server_config.max_lease_sec) | ||
| 115 | lease_time_sec = server_config.max_lease_sec; | ||
| 116 | if (lease_time_sec < server_config.min_lease_sec) | ||
| 117 | lease_time_sec = server_config.min_lease_sec; | ||
| 118 | } | ||
| 119 | return lease_time_sec; | ||
| 120 | } | ||
| 121 | |||
| 122 | /* send a DHCP OFFER to a DHCP DISCOVER */ | ||
| 123 | static int send_offer(struct dhcp_packet *oldpacket) | ||
| 124 | { | ||
| 125 | struct dhcp_packet packet; | ||
| 126 | uint32_t req_nip; | ||
| 127 | uint32_t lease_time_sec = server_config.max_lease_sec; | ||
| 128 | uint32_t static_lease_ip; | ||
| 129 | uint8_t *req_ip_opt; | ||
| 130 | const char *p_host_name; | ||
| 131 | struct option_set *curr; | ||
| 132 | struct in_addr addr; | ||
| 133 | |||
| 134 | init_packet(&packet, oldpacket, DHCPOFFER); | ||
| 135 | |||
| 136 | static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr); | ||
| 137 | |||
| 138 | /* ADDME: if static, short circuit */ | ||
| 139 | if (!static_lease_ip) { | ||
| 140 | struct dyn_lease *lease; | ||
| 141 | |||
| 142 | lease = find_lease_by_mac(oldpacket->chaddr); | ||
| 143 | /* The client is in our lease/offered table */ | ||
| 144 | if (lease) { | ||
| 145 | signed_leasetime_t tmp = lease->expires - time(NULL); | ||
| 146 | if (tmp >= 0) | ||
| 147 | lease_time_sec = tmp; | ||
| 148 | packet.yiaddr = lease->lease_nip; | ||
| 149 | } | ||
| 150 | /* Or the client has requested an IP */ | ||
| 151 | else if ((req_ip_opt = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL | ||
| 152 | /* (read IP) */ | ||
| 153 | && (move_from_unaligned32(req_nip, req_ip_opt), 1) | ||
| 154 | /* and the IP is in the lease range */ | ||
| 155 | && ntohl(req_nip) >= server_config.start_ip | ||
| 156 | && ntohl(req_nip) <= server_config.end_ip | ||
| 157 | /* and is not already taken/offered */ | ||
| 158 | && (!(lease = find_lease_by_nip(req_nip)) | ||
| 159 | /* or its taken, but expired */ | ||
| 160 | || is_expired_lease(lease)) | ||
| 161 | ) { | ||
| 162 | packet.yiaddr = req_nip; | ||
| 163 | } | ||
| 164 | /* Otherwise, find a free IP */ | ||
| 165 | else { | ||
| 166 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); | ||
| 167 | } | ||
| 168 | |||
| 169 | if (!packet.yiaddr) { | ||
| 170 | bb_error_msg("no IP addresses to give - OFFER abandoned"); | ||
| 171 | return -1; | ||
| 172 | } | ||
| 173 | p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); | ||
| 174 | if (add_lease(packet.chaddr, packet.yiaddr, | ||
| 175 | server_config.offer_time, | ||
| 176 | p_host_name, | ||
| 177 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 | ||
| 178 | ) == 0 | ||
| 179 | ) { | ||
| 180 | bb_error_msg("lease pool is full - OFFER abandoned"); | ||
| 181 | return -1; | ||
| 182 | } | ||
| 183 | lease_time_sec = select_lease_time(oldpacket); | ||
| 184 | } else { | ||
| 185 | /* It is a static lease... use it */ | ||
| 186 | packet.yiaddr = static_lease_ip; | ||
| 187 | } | ||
| 188 | |||
| 189 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); | ||
| 190 | |||
| 191 | curr = server_config.options; | ||
| 192 | while (curr) { | ||
| 193 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
| 194 | add_option_string(packet.options, curr->data); | ||
| 195 | curr = curr->next; | ||
| 196 | } | ||
| 197 | |||
| 198 | add_bootp_options(&packet); | ||
| 199 | |||
| 200 | addr.s_addr = packet.yiaddr; | ||
| 201 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); | ||
| 202 | return send_packet(&packet, 0); | ||
| 203 | } | ||
| 204 | |||
| 205 | static int send_NAK(struct dhcp_packet *oldpacket) | ||
| 206 | { | ||
| 207 | struct dhcp_packet packet; | ||
| 208 | |||
| 209 | init_packet(&packet, oldpacket, DHCPNAK); | ||
| 210 | |||
| 211 | log1("Sending NAK"); | ||
| 212 | return send_packet(&packet, 1); | ||
| 213 | } | ||
| 214 | |||
| 215 | static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | ||
| 216 | { | ||
| 217 | struct dhcp_packet packet; | ||
| 218 | struct option_set *curr; | ||
| 219 | uint32_t lease_time_sec; | ||
| 220 | struct in_addr addr; | ||
| 221 | const char *p_host_name; | ||
| 222 | |||
| 223 | init_packet(&packet, oldpacket, DHCPACK); | ||
| 224 | packet.yiaddr = yiaddr; | ||
| 225 | |||
| 226 | lease_time_sec = select_lease_time(oldpacket); | ||
| 227 | |||
| 228 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); | ||
| 229 | |||
| 230 | curr = server_config.options; | ||
| 231 | while (curr) { | ||
| 232 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
| 233 | add_option_string(packet.options, curr->data); | ||
| 234 | curr = curr->next; | ||
| 235 | } | ||
| 236 | |||
| 237 | add_bootp_options(&packet); | ||
| 238 | |||
| 239 | addr.s_addr = packet.yiaddr; | ||
| 240 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); | ||
| 241 | |||
| 242 | if (send_packet(&packet, 0) < 0) | ||
| 243 | return -1; | ||
| 244 | |||
| 245 | p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); | ||
| 246 | add_lease(packet.chaddr, packet.yiaddr, | ||
| 247 | lease_time_sec, | ||
| 248 | p_host_name, | ||
| 249 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 | ||
| 250 | ); | ||
| 251 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { | ||
| 252 | /* rewrite the file with leases at every new acceptance */ | ||
| 253 | write_leases(); | ||
| 254 | } | ||
| 255 | |||
| 256 | return 0; | ||
| 257 | } | ||
| 258 | |||
| 259 | static int send_inform(struct dhcp_packet *oldpacket) | ||
| 260 | { | ||
| 261 | struct dhcp_packet 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 | } | ||
| 277 | |||
| 278 | |||
| 20 | /* globals */ | 279 | /* globals */ |
| 21 | struct dyn_lease *g_leases; | 280 | struct dyn_lease *g_leases; |
| 22 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ | 281 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ |
diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h index db2d1c776..b55fd6c06 100644 --- a/networking/udhcp/dhcpd.h +++ b/networking/udhcp/dhcpd.h | |||
| @@ -124,14 +124,6 @@ void log_static_leases(struct static_lease **st_lease_pp) FAST_FUNC; | |||
| 124 | #endif | 124 | #endif |
| 125 | 125 | ||
| 126 | 126 | ||
| 127 | /*** serverpacket.h ***/ | ||
| 128 | |||
| 129 | int send_offer(struct dhcp_packet *oldpacket) FAST_FUNC; | ||
| 130 | int send_NAK(struct dhcp_packet *oldpacket) FAST_FUNC; | ||
| 131 | int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) FAST_FUNC; | ||
| 132 | int send_inform(struct dhcp_packet *oldpacket) FAST_FUNC; | ||
| 133 | |||
| 134 | |||
| 135 | /*** files.h ***/ | 127 | /*** files.h ***/ |
| 136 | 128 | ||
| 137 | void read_config(const char *file) FAST_FUNC; | 129 | void read_config(const char *file) FAST_FUNC; |
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c deleted file mode 100644 index b48e4159a..000000000 --- a/networking/udhcp/serverpacket.c +++ /dev/null | |||
| @@ -1,283 +0,0 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 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 | |||
| 23 | #include "common.h" | ||
| 24 | #include "dhcpc.h" | ||
| 25 | #include "dhcpd.h" | ||
| 26 | #include "options.h" | ||
| 27 | |||
| 28 | |||
| 29 | /* send a packet to gateway_nip using the kernel ip stack */ | ||
| 30 | static int send_packet_to_relay(struct dhcp_packet *dhcp_pkt) | ||
| 31 | { | ||
| 32 | log1("Forwarding packet to relay"); | ||
| 33 | |||
| 34 | return udhcp_send_kernel_packet(dhcp_pkt, | ||
| 35 | server_config.server_nip, SERVER_PORT, | ||
| 36 | dhcp_pkt->gateway_nip, SERVER_PORT); | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | /* send a packet to a specific mac address and ip address by creating our own ip packet */ | ||
| 41 | static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast) | ||
| 42 | { | ||
| 43 | const uint8_t *chaddr; | ||
| 44 | uint32_t ciaddr; | ||
| 45 | |||
| 46 | // Was: | ||
| 47 | //if (force_broadcast) { /* broadcast */ } | ||
| 48 | //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 */ } | ||
| 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 dhcp_pkt->yiaddr! | ||
| 55 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, | ||
| 56 | // and can be used. | ||
| 57 | |||
| 58 | if (force_broadcast | ||
| 59 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) | ||
| 60 | || !dhcp_pkt->ciaddr | ||
| 61 | ) { | ||
| 62 | log1("Broadcasting packet to client"); | ||
| 63 | ciaddr = INADDR_BROADCAST; | ||
| 64 | chaddr = MAC_BCAST_ADDR; | ||
| 65 | } else { | ||
| 66 | log1("Unicasting packet to client ciaddr"); | ||
| 67 | ciaddr = dhcp_pkt->ciaddr; | ||
| 68 | chaddr = dhcp_pkt->chaddr; | ||
| 69 | } | ||
| 70 | |||
| 71 | return udhcp_send_raw_packet(dhcp_pkt, | ||
| 72 | /*src*/ server_config.server_nip, SERVER_PORT, | ||
| 73 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, | ||
| 74 | server_config.ifindex); | ||
| 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 dhcp_packet *dhcp_pkt, int force_broadcast) | ||
| 80 | { | ||
| 81 | if (dhcp_pkt->gateway_nip) | ||
| 82 | return send_packet_to_relay(dhcp_pkt); | ||
| 83 | return send_packet_to_client(dhcp_pkt, force_broadcast); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) | ||
| 88 | { | ||
| 89 | udhcp_init_header(packet, type); | ||
| 90 | packet->xid = oldpacket->xid; | ||
| 91 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); | ||
| 92 | packet->flags = oldpacket->flags; | ||
| 93 | packet->gateway_nip = oldpacket->gateway_nip; | ||
| 94 | packet->ciaddr = oldpacket->ciaddr; | ||
| 95 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | /* add in the bootp options */ | ||
| 100 | static void add_bootp_options(struct dhcp_packet *packet) | ||
| 101 | { | ||
| 102 | packet->siaddr_nip = server_config.siaddr_nip; | ||
| 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 | static uint32_t select_lease_time(struct dhcp_packet *packet) | ||
| 111 | { | ||
| 112 | uint32_t lease_time_sec = server_config.max_lease_sec; | ||
| 113 | uint8_t *lease_time_opt = get_option(packet, DHCP_LEASE_TIME); | ||
| 114 | if (lease_time_opt) { | ||
| 115 | move_from_unaligned32(lease_time_sec, lease_time_opt); | ||
| 116 | lease_time_sec = ntohl(lease_time_sec); | ||
| 117 | if (lease_time_sec > server_config.max_lease_sec) | ||
| 118 | lease_time_sec = server_config.max_lease_sec; | ||
| 119 | if (lease_time_sec < server_config.min_lease_sec) | ||
| 120 | lease_time_sec = server_config.min_lease_sec; | ||
| 121 | } | ||
| 122 | return lease_time_sec; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | /* send a DHCP OFFER to a DHCP DISCOVER */ | ||
| 127 | int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) | ||
| 128 | { | ||
| 129 | struct dhcp_packet packet; | ||
| 130 | uint32_t req_nip; | ||
| 131 | uint32_t lease_time_sec = server_config.max_lease_sec; | ||
| 132 | uint32_t static_lease_ip; | ||
| 133 | uint8_t *req_ip_opt; | ||
| 134 | const char *p_host_name; | ||
| 135 | struct option_set *curr; | ||
| 136 | struct in_addr addr; | ||
| 137 | |||
| 138 | init_packet(&packet, oldpacket, DHCPOFFER); | ||
| 139 | |||
| 140 | static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr); | ||
| 141 | |||
| 142 | /* ADDME: if static, short circuit */ | ||
| 143 | if (!static_lease_ip) { | ||
| 144 | struct dyn_lease *lease; | ||
| 145 | |||
| 146 | lease = find_lease_by_mac(oldpacket->chaddr); | ||
| 147 | /* The client is in our lease/offered table */ | ||
| 148 | if (lease) { | ||
| 149 | signed_leasetime_t tmp = lease->expires - time(NULL); | ||
| 150 | if (tmp >= 0) | ||
| 151 | lease_time_sec = tmp; | ||
| 152 | packet.yiaddr = lease->lease_nip; | ||
| 153 | } | ||
| 154 | /* Or the client has requested an IP */ | ||
| 155 | else if ((req_ip_opt = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL | ||
| 156 | /* (read IP) */ | ||
| 157 | && (move_from_unaligned32(req_nip, req_ip_opt), 1) | ||
| 158 | /* and the IP is in the lease range */ | ||
| 159 | && ntohl(req_nip) >= server_config.start_ip | ||
| 160 | && ntohl(req_nip) <= server_config.end_ip | ||
| 161 | /* and is not already taken/offered */ | ||
| 162 | && (!(lease = find_lease_by_nip(req_nip)) | ||
| 163 | /* or its taken, but expired */ | ||
| 164 | || is_expired_lease(lease)) | ||
| 165 | ) { | ||
| 166 | packet.yiaddr = req_nip; | ||
| 167 | } | ||
| 168 | /* Otherwise, find a free IP */ | ||
| 169 | else { | ||
| 170 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); | ||
| 171 | } | ||
| 172 | |||
| 173 | if (!packet.yiaddr) { | ||
| 174 | bb_error_msg("no IP addresses to give - OFFER abandoned"); | ||
| 175 | return -1; | ||
| 176 | } | ||
| 177 | p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); | ||
| 178 | if (add_lease(packet.chaddr, packet.yiaddr, | ||
| 179 | server_config.offer_time, | ||
| 180 | p_host_name, | ||
| 181 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 | ||
| 182 | ) == 0 | ||
| 183 | ) { | ||
| 184 | bb_error_msg("lease pool is full - OFFER abandoned"); | ||
| 185 | return -1; | ||
| 186 | } | ||
| 187 | lease_time_sec = select_lease_time(oldpacket); | ||
| 188 | } else { | ||
| 189 | /* It is a static lease... use it */ | ||
| 190 | packet.yiaddr = static_lease_ip; | ||
| 191 | } | ||
| 192 | |||
| 193 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); | ||
| 194 | |||
| 195 | curr = server_config.options; | ||
| 196 | while (curr) { | ||
| 197 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
| 198 | add_option_string(packet.options, curr->data); | ||
| 199 | curr = curr->next; | ||
| 200 | } | ||
| 201 | |||
| 202 | add_bootp_options(&packet); | ||
| 203 | |||
| 204 | addr.s_addr = packet.yiaddr; | ||
| 205 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); | ||
| 206 | return send_packet(&packet, 0); | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | int FAST_FUNC send_NAK(struct dhcp_packet *oldpacket) | ||
| 211 | { | ||
| 212 | struct dhcp_packet packet; | ||
| 213 | |||
| 214 | init_packet(&packet, oldpacket, DHCPNAK); | ||
| 215 | |||
| 216 | log1("Sending NAK"); | ||
| 217 | return send_packet(&packet, 1); | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | ||
| 222 | { | ||
| 223 | struct dhcp_packet packet; | ||
| 224 | struct option_set *curr; | ||
| 225 | uint32_t lease_time_sec; | ||
| 226 | struct in_addr addr; | ||
| 227 | const char *p_host_name; | ||
| 228 | |||
| 229 | init_packet(&packet, oldpacket, DHCPACK); | ||
| 230 | packet.yiaddr = yiaddr; | ||
| 231 | |||
| 232 | lease_time_sec = select_lease_time(oldpacket); | ||
| 233 | |||
| 234 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); | ||
| 235 | |||
| 236 | curr = server_config.options; | ||
| 237 | while (curr) { | ||
| 238 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
| 239 | add_option_string(packet.options, curr->data); | ||
| 240 | curr = curr->next; | ||
| 241 | } | ||
| 242 | |||
| 243 | add_bootp_options(&packet); | ||
| 244 | |||
| 245 | addr.s_addr = packet.yiaddr; | ||
| 246 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); | ||
| 247 | |||
| 248 | if (send_packet(&packet, 0) < 0) | ||
| 249 | return -1; | ||
| 250 | |||
| 251 | p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); | ||
| 252 | add_lease(packet.chaddr, packet.yiaddr, | ||
| 253 | lease_time_sec, | ||
| 254 | p_host_name, | ||
| 255 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 | ||
| 256 | ); | ||
| 257 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { | ||
| 258 | /* rewrite the file with leases at every new acceptance */ | ||
| 259 | write_leases(); | ||
| 260 | } | ||
| 261 | |||
| 262 | return 0; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | int FAST_FUNC send_inform(struct dhcp_packet *oldpacket) | ||
| 267 | { | ||
| 268 | struct dhcp_packet packet; | ||
| 269 | struct option_set *curr; | ||
| 270 | |||
| 271 | init_packet(&packet, oldpacket, DHCPACK); | ||
| 272 | |||
| 273 | curr = server_config.options; | ||
| 274 | while (curr) { | ||
| 275 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
| 276 | add_option_string(packet.options, curr->data); | ||
| 277 | curr = curr->next; | ||
| 278 | } | ||
| 279 | |||
| 280 | add_bootp_options(&packet); | ||
| 281 | |||
| 282 | return send_packet(&packet, 0); | ||
| 283 | } | ||
