diff options
Diffstat (limited to 'networking/udhcp/dhcpd.c')
-rw-r--r-- | networking/udhcp/dhcpd.c | 73 |
1 files changed, 30 insertions, 43 deletions
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index d7887d914..cc4cb9280 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c | |||
@@ -75,7 +75,9 @@ static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadca | |||
75 | server_config.ifindex); | 75 | server_config.ifindex); |
76 | } | 76 | } |
77 | 77 | ||
78 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ | 78 | /* Send the dhcp packet. |
79 | * If force broadcast is set, the packet will be broadcast. | ||
80 | */ | ||
79 | static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) | 81 | static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
80 | { | 82 | { |
81 | if (dhcp_pkt->gateway_nip) | 83 | if (dhcp_pkt->gateway_nip) |
@@ -94,10 +96,21 @@ static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacke | |||
94 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); | 96 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); |
95 | } | 97 | } |
96 | 98 | ||
97 | /* add in the bootp options */ | 99 | /* Fill options field, siaddr_nip, and sname and boot_file fields. |
98 | static void add_bootp_options(struct dhcp_packet *packet) | 100 | * TODO: teach this code to use overload option. |
101 | */ | ||
102 | static void add_server_options(struct dhcp_packet *packet) | ||
99 | { | 103 | { |
104 | struct option_set *curr = server_config.options; | ||
105 | |||
106 | while (curr) { | ||
107 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
108 | add_option_string(packet->options, curr->data); | ||
109 | curr = curr->next; | ||
110 | } | ||
111 | |||
100 | packet->siaddr_nip = server_config.siaddr_nip; | 112 | packet->siaddr_nip = server_config.siaddr_nip; |
113 | |||
101 | if (server_config.sname) | 114 | if (server_config.sname) |
102 | strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); | 115 | strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); |
103 | if (server_config.boot_file) | 116 | if (server_config.boot_file) |
@@ -123,17 +136,17 @@ static uint32_t select_lease_time(struct dhcp_packet *packet) | |||
123 | static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease) | 136 | static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease) |
124 | { | 137 | { |
125 | struct dhcp_packet packet; | 138 | struct dhcp_packet packet; |
126 | uint32_t req_nip; | ||
127 | uint32_t lease_time_sec = server_config.max_lease_sec; | 139 | uint32_t lease_time_sec = server_config.max_lease_sec; |
128 | uint8_t *req_ip_opt; | ||
129 | const char *p_host_name; | 140 | const char *p_host_name; |
130 | struct option_set *curr; | ||
131 | struct in_addr addr; | 141 | struct in_addr addr; |
132 | 142 | ||
133 | init_packet(&packet, oldpacket, DHCPOFFER); | 143 | init_packet(&packet, oldpacket, DHCPOFFER); |
134 | 144 | ||
135 | /* ADDME: if static, short circuit */ | 145 | /* ADDME: if static, short circuit */ |
136 | if (!static_lease_nip) { | 146 | if (!static_lease_nip) { |
147 | uint32_t req_nip; | ||
148 | uint8_t *req_ip_opt; | ||
149 | |||
137 | /* The client is in our lease/offered table */ | 150 | /* The client is in our lease/offered table */ |
138 | if (lease) { | 151 | if (lease) { |
139 | packet.yiaddr = lease->lease_nip; | 152 | packet.yiaddr = lease->lease_nip; |
@@ -145,10 +158,10 @@ static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, | |||
145 | /* and the IP is in the lease range */ | 158 | /* and the IP is in the lease range */ |
146 | && ntohl(req_nip) >= server_config.start_ip | 159 | && ntohl(req_nip) >= server_config.start_ip |
147 | && ntohl(req_nip) <= server_config.end_ip | 160 | && ntohl(req_nip) <= server_config.end_ip |
148 | /* and is not already taken/offered */ | 161 | /* and */ |
149 | && (!(lease = find_lease_by_nip(req_nip)) | 162 | && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */ |
150 | /* or its taken, but expired */ | 163 | || is_expired_lease(lease) /* or is taken, but expired */ |
151 | || is_expired_lease(lease)) | 164 | ) |
152 | ) { | 165 | ) { |
153 | packet.yiaddr = req_nip; | 166 | packet.yiaddr = req_nip; |
154 | } | 167 | } |
@@ -178,19 +191,11 @@ static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, | |||
178 | } | 191 | } |
179 | 192 | ||
180 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); | 193 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
181 | 194 | add_server_options(&packet); | |
182 | curr = server_config.options; | ||
183 | while (curr) { | ||
184 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
185 | add_option_string(packet.options, curr->data); | ||
186 | curr = curr->next; | ||
187 | } | ||
188 | |||
189 | add_bootp_options(&packet); | ||
190 | 195 | ||
191 | addr.s_addr = packet.yiaddr; | 196 | addr.s_addr = packet.yiaddr; |
192 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); | 197 | bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); |
193 | return send_packet(&packet, 0); | 198 | return send_packet(&packet, /*force_bcast:*/ 0); |
194 | } | 199 | } |
195 | 200 | ||
196 | static int send_NAK(struct dhcp_packet *oldpacket) | 201 | static int send_NAK(struct dhcp_packet *oldpacket) |
@@ -200,13 +205,12 @@ static int send_NAK(struct dhcp_packet *oldpacket) | |||
200 | init_packet(&packet, oldpacket, DHCPNAK); | 205 | init_packet(&packet, oldpacket, DHCPNAK); |
201 | 206 | ||
202 | log1("Sending NAK"); | 207 | log1("Sending NAK"); |
203 | return send_packet(&packet, 1); | 208 | return send_packet(&packet, /*force_bcast:*/ 1); |
204 | } | 209 | } |
205 | 210 | ||
206 | static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | 211 | static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) |
207 | { | 212 | { |
208 | struct dhcp_packet packet; | 213 | struct dhcp_packet packet; |
209 | struct option_set *curr; | ||
210 | uint32_t lease_time_sec; | 214 | uint32_t lease_time_sec; |
211 | struct in_addr addr; | 215 | struct in_addr addr; |
212 | const char *p_host_name; | 216 | const char *p_host_name; |
@@ -217,20 +221,12 @@ static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | |||
217 | lease_time_sec = select_lease_time(oldpacket); | 221 | lease_time_sec = select_lease_time(oldpacket); |
218 | 222 | ||
219 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); | 223 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
220 | 224 | add_server_options(&packet); | |
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 | 225 | ||
230 | addr.s_addr = packet.yiaddr; | 226 | addr.s_addr = packet.yiaddr; |
231 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); | 227 | bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); |
232 | 228 | ||
233 | if (send_packet(&packet, 0) < 0) | 229 | if (send_packet(&packet, /*force_bcast:*/ 0) < 0) |
234 | return -1; | 230 | return -1; |
235 | 231 | ||
236 | p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); | 232 | p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME); |
@@ -250,20 +246,11 @@ static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | |||
250 | static int send_inform(struct dhcp_packet *oldpacket) | 246 | static int send_inform(struct dhcp_packet *oldpacket) |
251 | { | 247 | { |
252 | struct dhcp_packet packet; | 248 | struct dhcp_packet packet; |
253 | struct option_set *curr; | ||
254 | 249 | ||
255 | init_packet(&packet, oldpacket, DHCPACK); | 250 | init_packet(&packet, oldpacket, DHCPACK); |
251 | add_server_options(&packet); | ||
256 | 252 | ||
257 | curr = server_config.options; | 253 | return send_packet(&packet, /*force_bcast:*/ 0); |
258 | while (curr) { | ||
259 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) | ||
260 | add_option_string(packet.options, curr->data); | ||
261 | curr = curr->next; | ||
262 | } | ||
263 | |||
264 | add_bootp_options(&packet); | ||
265 | |||
266 | return send_packet(&packet, 0); | ||
267 | } | 254 | } |
268 | 255 | ||
269 | 256 | ||