"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 */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 30 | static int send_packet_to_relay(struct dhcp_packet *dhcp_pkt) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 31 | { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 32 | log1("Forwarding packet to relay"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 33 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 34 | return udhcp_send_kernel_packet(dhcp_pkt, |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 35 | server_config.server_nip, SERVER_PORT, |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 36 | dhcp_pkt->gateway_nip, SERVER_PORT); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 40 | /* send a packet to a specific mac address and ip address by creating our own ip packet */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 41 | static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 */ } |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 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 */ } |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 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! |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 54 | // We should never unicast to dhcp_pkt->yiaddr! |
| 55 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 56 | // and can be used. |
| 57 | |
| 58 | if (force_broadcast |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 59 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) |
| 60 | || !dhcp_pkt->ciaddr |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 61 | ) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 62 | log1("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 | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 66 | log1("Unicasting packet to client ciaddr"); |
| 67 | ciaddr = dhcp_pkt->ciaddr; |
| 68 | chaddr = dhcp_pkt->chaddr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 69 | } |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 70 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 71 | return udhcp_send_raw_packet(dhcp_pkt, |
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 */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 79 | static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 80 | { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 81 | if (dhcp_pkt->gateway_nip) |
| 82 | return send_packet_to_relay(dhcp_pkt); |
| 83 | return send_packet_to_client(dhcp_pkt, force_broadcast); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 87 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 91 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 100 | static void add_bootp_options(struct dhcp_packet *packet) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 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 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 126 | /* send a DHCP OFFER to a DHCP DISCOVER */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 127 | int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 128 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 129 | struct dhcp_packet packet; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 130 | uint32_t req_nip; |
| 131 | uint32_t lease_time_sec = server_config.max_lease_sec; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 132 | uint32_t static_lease_ip; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 133 | uint8_t *req_ip_opt, *p_host_name; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 134 | struct option_set *curr; |
| 135 | struct in_addr addr; |
| 136 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 137 | init_packet(&packet, oldpacket, DHCPOFFER); |
| 138 | |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 139 | static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 140 | |
| 141 | /* ADDME: if static, short circuit */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 142 | if (!static_lease_ip) { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 143 | struct dyn_lease *lease; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 144 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 145 | lease = find_lease_by_mac(oldpacket->chaddr); |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 146 | /* The client is in our lease/offered table */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 147 | if (lease) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 148 | signed_leasetime_t tmp = lease->expires - time(NULL); |
| 149 | if (tmp >= 0) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 150 | lease_time_sec = tmp; |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 151 | packet.yiaddr = lease->lease_nip; |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 152 | } |
| 153 | /* Or the client has requested an IP */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 154 | else if ((req_ip_opt = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 155 | /* (read IP) */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 156 | && (move_from_unaligned32(req_nip, req_ip_opt), 1) |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 157 | /* and the IP is in the lease range */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 158 | && ntohl(req_nip) >= server_config.start_ip |
| 159 | && ntohl(req_nip) <= server_config.end_ip |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 160 | /* and is not already taken/offered */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 161 | && (!(lease = find_lease_by_nip(req_nip)) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 162 | /* or its taken, but expired */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 163 | || is_expired_lease(lease)) |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 164 | ) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 165 | packet.yiaddr = req_nip; |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 166 | } |
| 167 | /* Otherwise, find a free IP */ |
| 168 | else { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 169 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 170 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 171 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 172 | if (!packet.yiaddr) { |
| 173 | bb_error_msg("no IP addresses to give - OFFER abandoned"); |
| 174 | return -1; |
| 175 | } |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 176 | p_host_name = get_option(oldpacket, DHCP_HOST_NAME); |
| 177 | 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] | 178 | bb_error_msg("lease pool is full - OFFER abandoned"); |
| 179 | return -1; |
| 180 | } |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 181 | lease_time_sec = select_lease_time(oldpacket); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 182 | } else { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 183 | /* It is a static lease... use it */ |
| 184 | packet.yiaddr = static_lease_ip; |
| 185 | } |
| 186 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 187 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 188 | |
| 189 | curr = server_config.options; |
| 190 | while (curr) { |
| 191 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 192 | add_option_string(packet.options, curr->data); |
| 193 | curr = curr->next; |
| 194 | } |
| 195 | |
| 196 | add_bootp_options(&packet); |
| 197 | |
| 198 | addr.s_addr = packet.yiaddr; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 199 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 200 | return send_packet(&packet, 0); |
| 201 | } |
| 202 | |
| 203 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 204 | int FAST_FUNC send_NAK(struct dhcp_packet *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 205 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 206 | struct dhcp_packet packet; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 207 | |
| 208 | init_packet(&packet, oldpacket, DHCPNAK); |
| 209 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 210 | log1("Sending NAK"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 211 | return send_packet(&packet, 1); |
| 212 | } |
| 213 | |
| 214 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 215 | int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 216 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 217 | struct dhcp_packet packet; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 218 | struct option_set *curr; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 219 | uint32_t lease_time_sec; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 220 | struct in_addr addr; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 221 | uint8_t *p_host_name; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 222 | |
| 223 | init_packet(&packet, oldpacket, DHCPACK); |
| 224 | packet.yiaddr = yiaddr; |
| 225 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 226 | lease_time_sec = select_lease_time(oldpacket); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 227 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 228 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 240 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 241 | |
| 242 | if (send_packet(&packet, 0) < 0) |
| 243 | return -1; |
| 244 | |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 245 | p_host_name = get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 246 | add_lease(packet.chaddr, packet.yiaddr, lease_time_sec, p_host_name); |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 247 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
| 248 | /* rewrite the file with leases at every new acceptance */ |
| 249 | write_leases(); |
| 250 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 256 | int FAST_FUNC send_inform(struct dhcp_packet *oldpacket) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 257 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 258 | struct dhcp_packet packet; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 259 | struct option_set *curr; |
| 260 | |
| 261 | init_packet(&packet, oldpacket, DHCPACK); |
| 262 | |
| 263 | curr = server_config.options; |
| 264 | while (curr) { |
| 265 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
| 266 | add_option_string(packet.options, curr->data); |
| 267 | curr = curr->next; |
| 268 | } |
| 269 | |
| 270 | add_bootp_options(&packet); |
| 271 | |
| 272 | return send_packet(&packet, 0); |
| 273 | } |