Annotation of /trunk/mkinitrd-magellan/busybox/networking/udhcp/serverpacket.c
Parent Directory | Revision Log
Revision 984 -
(hide annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 8159 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 8159 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | niro | 532 | /* 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 | niro | 816 | #include "dhcpc.h" |
25 | niro | 532 | #include "dhcpd.h" |
26 | #include "options.h" | ||
27 | |||
28 | |||
29 | niro | 984 | /* send a packet to gateway_nip using the kernel ip stack */ |
30 | static int send_packet_to_relay(struct dhcp_packet *dhcp_pkt) | ||
31 | niro | 532 | { |
32 | niro | 984 | log1("Forwarding packet to relay"); |
33 | niro | 532 | |
34 | niro | 984 | return udhcp_send_kernel_packet(dhcp_pkt, |
35 | server_config.server_nip, SERVER_PORT, | ||
36 | dhcp_pkt->gateway_nip, SERVER_PORT); | ||
37 | niro | 532 | } |
38 | |||
39 | |||
40 | niro | 984 | /* 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 | niro | 532 | { |
43 | niro | 816 | const uint8_t *chaddr; |
44 | niro | 532 | uint32_t ciaddr; |
45 | |||
46 | niro | 984 | // 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 | niro | 532 | ciaddr = INADDR_BROADCAST; |
64 | chaddr = MAC_BCAST_ADDR; | ||
65 | } else { | ||
66 | niro | 984 | log1("Unicasting packet to client ciaddr"); |
67 | ciaddr = dhcp_pkt->ciaddr; | ||
68 | chaddr = dhcp_pkt->chaddr; | ||
69 | niro | 532 | } |
70 | niro | 984 | |
71 | return udhcp_send_raw_packet(dhcp_pkt, | ||
72 | /*src*/ server_config.server_nip, SERVER_PORT, | ||
73 | niro | 816 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
74 | server_config.ifindex); | ||
75 | niro | 532 | } |
76 | |||
77 | |||
78 | /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */ | ||
79 | niro | 984 | static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
80 | niro | 532 | { |
81 | niro | 984 | if (dhcp_pkt->gateway_nip) |
82 | return send_packet_to_relay(dhcp_pkt); | ||
83 | return send_packet_to_client(dhcp_pkt, force_broadcast); | ||
84 | niro | 532 | } |
85 | |||
86 | |||
87 | niro | 984 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) |
88 | niro | 532 | { |
89 | udhcp_init_header(packet, type); | ||
90 | packet->xid = oldpacket->xid; | ||
91 | niro | 984 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); |
92 | niro | 532 | packet->flags = oldpacket->flags; |
93 | niro | 984 | packet->gateway_nip = oldpacket->gateway_nip; |
94 | niro | 532 | packet->ciaddr = oldpacket->ciaddr; |
95 | niro | 984 | add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip); |
96 | niro | 532 | } |
97 | |||
98 | |||
99 | /* add in the bootp options */ | ||
100 | niro | 984 | static void add_bootp_options(struct dhcp_packet *packet) |
101 | niro | 532 | { |
102 | niro | 984 | packet->siaddr_nip = server_config.siaddr_nip; |
103 | niro | 532 | 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 | niro | 984 | 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 | niro | 532 | /* send a DHCP OFFER to a DHCP DISCOVER */ |
127 | niro | 984 | int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) |
128 | niro | 532 | { |
129 | niro | 984 | 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 | niro | 532 | struct option_set *curr; |
136 | struct in_addr addr; | ||
137 | |||
138 | init_packet(&packet, oldpacket, DHCPOFFER); | ||
139 | |||
140 | niro | 984 | static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr); |
141 | niro | 532 | |
142 | /* ADDME: if static, short circuit */ | ||
143 | if (!static_lease_ip) { | ||
144 | niro | 984 | struct dyn_lease *lease; |
145 | |||
146 | lease = find_lease_by_mac(oldpacket->chaddr); | ||
147 | /* The client is in our lease/offered table */ | ||
148 | niro | 532 | if (lease) { |
149 | niro | 984 | 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 | niro | 816 | /* and is not already taken/offered */ |
162 | niro | 984 | && (!(lease = find_lease_by_nip(req_nip)) |
163 | /* or its taken, but expired */ | ||
164 | || is_expired_lease(lease)) | ||
165 | niro | 532 | ) { |
166 | niro | 984 | packet.yiaddr = req_nip; |
167 | niro | 532 | } |
168 | niro | 984 | /* Otherwise, find a free IP */ |
169 | else { | ||
170 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); | ||
171 | } | ||
172 | niro | 532 | |
173 | if (!packet.yiaddr) { | ||
174 | bb_error_msg("no IP addresses to give - OFFER abandoned"); | ||
175 | return -1; | ||
176 | } | ||
177 | niro | 984 | 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 | niro | 532 | bb_error_msg("lease pool is full - OFFER abandoned"); |
185 | return -1; | ||
186 | } | ||
187 | niro | 984 | lease_time_sec = select_lease_time(oldpacket); |
188 | niro | 532 | } else { |
189 | /* It is a static lease... use it */ | ||
190 | packet.yiaddr = static_lease_ip; | ||
191 | } | ||
192 | |||
193 | niro | 984 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
194 | niro | 532 | |
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 | niro | 984 | int FAST_FUNC send_NAK(struct dhcp_packet *oldpacket) |
211 | niro | 532 | { |
212 | niro | 984 | struct dhcp_packet packet; |
213 | niro | 532 | |
214 | init_packet(&packet, oldpacket, DHCPNAK); | ||
215 | |||
216 | niro | 984 | log1("Sending NAK"); |
217 | niro | 532 | return send_packet(&packet, 1); |
218 | } | ||
219 | |||
220 | |||
221 | niro | 984 | int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) |
222 | niro | 532 | { |
223 | niro | 984 | struct dhcp_packet packet; |
224 | niro | 532 | struct option_set *curr; |
225 | niro | 984 | uint32_t lease_time_sec; |
226 | niro | 532 | struct in_addr addr; |
227 | niro | 984 | const char *p_host_name; |
228 | niro | 532 | |
229 | init_packet(&packet, oldpacket, DHCPACK); | ||
230 | packet.yiaddr = yiaddr; | ||
231 | |||
232 | niro | 984 | lease_time_sec = select_lease_time(oldpacket); |
233 | niro | 532 | |
234 | niro | 984 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
235 | niro | 532 | |
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 | niro | 984 | 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 | niro | 816 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
258 | /* rewrite the file with leases at every new acceptance */ | ||
259 | write_leases(); | ||
260 | } | ||
261 | niro | 532 | |
262 | return 0; | ||
263 | } | ||
264 | |||
265 | |||
266 | niro | 984 | int FAST_FUNC send_inform(struct dhcp_packet *oldpacket) |
267 | niro | 532 | { |
268 | niro | 984 | struct dhcp_packet packet; |
269 | niro | 532 | 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 | } |