VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/bootp.c@ 15035

最後變更 在這個檔案從15035是 9109,由 vboxsync 提交於 17 年 前

fix crash in slirp code if the wrong IP was released from DHCP (#2893)

  • 屬性 svn:eol-style 設為 native
檔案大小: 9.5 KB
 
1/*
2 * QEMU BOOTP/DHCP server
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include <slirp.h>
25
26/* XXX: only DHCP is supported */
27
28
29static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
30
31DECLINLINE(void) dprintf(const char *pszFormat, ...)
32{
33#ifdef LOG_ENABLED
34 va_list args;
35 va_start(args, pszFormat);
36 Log(("dhcp: %N", pszFormat, &args));
37 va_end(args);
38#endif
39}
40
41static BOOTPClient *get_new_addr(PNATState pData, struct in_addr *paddr)
42{
43 BOOTPClient *bc;
44 int i;
45
46 for(i = 0; i < NB_ADDR; i++) {
47 if (!bootp_clients[i].allocated)
48 goto found;
49 }
50 return NULL;
51 found:
52 bc = &bootp_clients[i];
53 bc->allocated = 1;
54 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
55 return bc;
56}
57
58static int release_addr(PNATState pData, struct in_addr *paddr)
59{
60 unsigned i;
61
62 i = ntohl(paddr->s_addr) - START_ADDR - ntohl(special_addr.s_addr);
63 if (i >= NB_ADDR)
64 return 0;
65 memset(bootp_clients[i].macaddr, '\0', 6);
66 bootp_clients[i].allocated = 0;
67 return 1;
68}
69
70static BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
71{
72 BOOTPClient *bc;
73 int i;
74
75 for(i = 0; i < NB_ADDR; i++) {
76 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
77 goto found;
78 }
79 return NULL;
80 found:
81 bc = &bootp_clients[i];
82 bc->allocated = 1;
83 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
84 return bc;
85}
86
87static void dhcp_decode(const uint8_t *buf, int size,
88 int *pmsg_type, struct in_addr *req_ip)
89{
90 const uint8_t *p, *p_end;
91 int len, tag;
92
93 *pmsg_type = 0;
94
95 p = buf;
96 p_end = buf + size;
97 if (size < 5)
98 return;
99 if (memcmp(p, rfc1533_cookie, 4) != 0)
100 return;
101 p += 4;
102 while (p < p_end) {
103 tag = p[0];
104 if (tag == RFC1533_PAD) {
105 p++;
106 } else if (tag == RFC1533_END) {
107 break;
108 } else {
109 p++;
110 if (p >= p_end)
111 break;
112 len = *p++;
113 dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
114
115 switch(tag) {
116 case RFC2132_REQ_ADDR:
117 if (len >= 4)
118 *req_ip = *(struct in_addr*)p;
119 break;
120 case RFC2132_MSG_TYPE:
121 if (len >= 1)
122 *pmsg_type = p[0];
123 break;
124 default:
125 break;
126 }
127 p += len;
128 }
129 }
130}
131
132static void bootp_reply(PNATState pData, struct bootp_t *bp)
133{
134 BOOTPClient *bc;
135 struct mbuf *m;
136 struct bootp_t *rbp;
137 struct sockaddr_in saddr, daddr;
138 struct in_addr dns_addr_dhcp;
139 int dhcp_msg_type, val;
140 uint8_t *q;
141 struct in_addr requested_ip; /* the requested IP in DHCPREQUEST */
142 int send_nak = 0;
143 uint32_t ipv4_addr;
144
145 /* extract exact DHCP msg type */
146 requested_ip.s_addr = 0xffffffff;
147 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type, &requested_ip);
148 dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
149
150 if (dhcp_msg_type == 0)
151 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
152
153 if (dhcp_msg_type == DHCPRELEASE) {
154 int rc;
155 ipv4_addr = ntohl(bp->bp_ciaddr.s_addr);
156 rc = release_addr(pData, &bp->bp_ciaddr);
157 LogRel(("NAT: %s %u.%u.%u.%u\n",
158 rc ? "DHCP released IP address" : "Ignored DHCP release for IP address",
159 ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
160 dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
161 /* This message is not to be answered in any way. */
162 return;
163 }
164 if (dhcp_msg_type != DHCPDISCOVER &&
165 dhcp_msg_type != DHCPREQUEST)
166 return;
167 /* XXX: this is a hack to get the client mac address */
168 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
169
170 if ((m = m_get(pData)) == NULL)
171 return;
172 m->m_data += if_maxlinkhdr;
173 rbp = (struct bootp_t *)m->m_data;
174 m->m_data += sizeof(struct udpiphdr);
175 memset(rbp, 0, sizeof(struct bootp_t));
176
177 if (dhcp_msg_type == DHCPDISCOVER) {
178 /* Do not allocate a new lease for clients that forgot that they had a lease. */
179 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
180 if (!bc)
181 {
182 new_addr:
183 bc = get_new_addr(pData, &daddr.sin_addr);
184 if (!bc) {
185 LogRel(("NAT: DHCP no IP address left\n"));
186 dprintf("no address left\n");
187 return;
188 }
189 memcpy(bc->macaddr, client_ethaddr, 6);
190 }
191 } else {
192 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
193 if (!bc) {
194 /* if never assigned, behaves as if it was already
195 assigned (windows fix because it remembers its address) */
196 goto new_addr;
197 }
198 }
199
200 if (tftp_prefix && RTDirExists(tftp_prefix) && bootp_filename)
201 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
202
203 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
204 saddr.sin_port = htons(BOOTP_SERVER);
205
206 daddr.sin_port = htons(BOOTP_CLIENT);
207
208 rbp->bp_op = BOOTP_REPLY;
209 rbp->bp_xid = bp->bp_xid;
210 rbp->bp_htype = 1;
211 rbp->bp_hlen = 6;
212 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
213
214 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
215 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
216
217 q = rbp->bp_vend;
218 memcpy(q, rfc1533_cookie, 4);
219 q += 4;
220
221 if (dhcp_msg_type == DHCPDISCOVER) {
222 *q++ = RFC2132_MSG_TYPE;
223 *q++ = 1;
224 *q++ = DHCPOFFER;
225 } else if (dhcp_msg_type == DHCPREQUEST) {
226 *q++ = RFC2132_MSG_TYPE;
227 *q++ = 1;
228 if ( requested_ip.s_addr != 0xffffffff
229 && requested_ip.s_addr != daddr.sin_addr.s_addr)
230 {
231 /* network changed */
232 *q++ = DHCPNAK;
233 send_nak = 1;
234 }
235 else
236 *q++ = DHCPACK;
237 }
238
239 if (send_nak)
240 {
241 ipv4_addr = ntohl(requested_ip.s_addr);
242 LogRel(("NAT: Client requested IP address %u.%u.%u.%u -- sending NAK\n",
243 ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
244 }
245 else
246 {
247 ipv4_addr = ntohl(daddr.sin_addr.s_addr);
248 LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
249 ipv4_addr >> 24, (ipv4_addr >> 16) & 0xff, (ipv4_addr >> 8) & 0xff, ipv4_addr & 0xff));
250 dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
251 }
252
253 if (dhcp_msg_type == DHCPDISCOVER ||
254 dhcp_msg_type == DHCPREQUEST) {
255 *q++ = RFC2132_SRV_ID;
256 *q++ = 4;
257 memcpy(q, &saddr.sin_addr, 4);
258 q += 4;
259 }
260
261 if (!send_nak &&
262 (dhcp_msg_type == DHCPDISCOVER ||
263 dhcp_msg_type == DHCPREQUEST)) {
264 *q++ = RFC1533_NETMASK;
265 *q++ = 4;
266 *q++ = (pData->netmask & 0xff000000) >> 24;
267 *q++ = (pData->netmask & 0x00ff0000) >> 16;
268 *q++ = (pData->netmask & 0x0000ff00) >> 8;
269 *q++ = (pData->netmask & 0x000000ff);
270
271 *q++ = RFC1533_GATEWAY;
272 *q++ = 4;
273 memcpy(q, &saddr.sin_addr, 4);
274 q += 4;
275
276 *q++ = RFC1533_DNS;
277 *q++ = 4;
278 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
279 memcpy(q, &dns_addr_dhcp, 4);
280 q += 4;
281
282 *q++ = RFC2132_LEASE_TIME;
283 *q++ = 4;
284 val = htonl(LEASE_TIME);
285 memcpy(q, &val, 4);
286 q += 4;
287
288 if (*slirp_hostname) {
289 val = strlen(slirp_hostname);
290 *q++ = RFC1533_HOSTNAME;
291 *q++ = val;
292 memcpy(q, slirp_hostname, val);
293 q += val;
294 }
295
296 if (pData->pszDomain && pData->fPassDomain)
297 {
298 val = strlen(pData->pszDomain);
299 *q++ = RFC1533_DOMAINNAME;
300 *q++ = val;
301 memcpy(q, pData->pszDomain, val);
302 q += val;
303 }
304 }
305 *q++ = RFC1533_END;
306
307 m->m_len = sizeof(struct bootp_t) -
308 sizeof(struct ip) - sizeof(struct udphdr);
309 /* Reply to the broadcast address, as some clients perform paranoid checks. */
310 daddr.sin_addr.s_addr = INADDR_BROADCAST;
311 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
312}
313
314void bootp_input(PNATState pData, struct mbuf *m)
315{
316 struct bootp_t *bp = mtod(m, struct bootp_t *);
317
318 if (bp->bp_op == BOOTP_REQUEST) {
319 bootp_reply(pData, bp);
320 }
321}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette