VirtualBox

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

最後變更 在這個檔案從18639是 18584,由 vboxsync 提交於 16 年 前

NAT: adding name domains improved.
multibytes handling in IPRT style
memory leak seems to be illuminated

  • 屬性 svn:eol-style 設為 native
檔案大小: 10.4 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
28static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
29
30static BOOTPClient *get_new_addr(PNATState pData, struct in_addr *paddr)
31{
32 int i;
33
34 for(i = 0; i < NB_ADDR; i++)
35 {
36 if (!bootp_clients[i].allocated)
37 {
38 BOOTPClient *bc;
39
40 bc = &bootp_clients[i];
41 bc->allocated = 1;
42 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
43 return bc;
44 }
45 }
46 return NULL;
47}
48
49static int release_addr(PNATState pData, struct in_addr *paddr)
50{
51 unsigned i;
52
53 i = ntohl(paddr->s_addr) - START_ADDR - ntohl(special_addr.s_addr);
54 if (i >= NB_ADDR)
55 return 0;
56
57 memset(bootp_clients[i].macaddr, '\0', 6);
58 bootp_clients[i].allocated = 0;
59 return 1;
60}
61
62static BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
63{
64 int i;
65
66 for(i = 0; i < NB_ADDR; i++)
67 {
68 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
69 {
70 BOOTPClient *bc;
71
72 bc = &bootp_clients[i];
73 bc->allocated = 1;
74 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
75 return bc;
76 }
77 }
78 return NULL;
79}
80
81static void dhcp_decode(const uint8_t *buf, int size,
82 int *pmsg_type, struct in_addr *req_ip)
83{
84 const uint8_t *p, *p_end;
85 int len, tag;
86
87 *pmsg_type = 0;
88
89 p = buf;
90 p_end = buf + size;
91 if (size < 5)
92 return;
93 if (memcmp(p, rfc1533_cookie, 4) != 0)
94 return;
95 p += 4;
96 while (p < p_end)
97 {
98 tag = p[0];
99 if (tag == RFC1533_PAD)
100 p++;
101 else if (tag == RFC1533_END)
102 break;
103 else
104 {
105 p++;
106 if (p >= p_end)
107 break;
108 len = *p++;
109 Log(("dhcp: tag=0x%02x len=%d\n", tag, len));
110
111 switch(tag)
112 {
113 case RFC2132_REQ_ADDR:
114 if (len >= 4)
115 *req_ip = *(struct in_addr*)p;
116 break;
117 case RFC2132_MSG_TYPE:
118 if (len >= 1)
119 *pmsg_type = p[0];
120 break;
121 default:
122 break;
123 }
124 p += len;
125 }
126 }
127}
128
129static void bootp_reply(PNATState pData, struct bootp_t *bp)
130{
131 BOOTPClient *bc;
132 struct mbuf *m;
133 struct bootp_t *rbp;
134 struct sockaddr_in saddr, daddr;
135#ifndef VBOX_WITH_MULTI_DNS
136 struct in_addr dns_addr_dhcp;
137#endif
138 int dhcp_msg_type, val;
139 uint8_t *q;
140 struct in_addr requested_ip; /* the requested IP in DHCPREQUEST */
141 int send_nak = 0;
142
143#define FILL_BOOTP_EXT(q, tag, len, pvalue) \
144 do { \
145 struct bootp_ext *be = (struct bootp_ext *)(q); \
146 be->bpe_tag = (tag); \
147 be->bpe_len = (len); \
148 memcpy(&be[1], (pvalue), (len)); \
149 (q) = (uint8_t *)(&be[1]) + (len); \
150 }while(0)
151
152 /* extract exact DHCP msg type */
153 requested_ip.s_addr = 0xffffffff;
154 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type, &requested_ip);
155 Log(("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type));
156
157 if (dhcp_msg_type == 0)
158 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
159
160 if (dhcp_msg_type == DHCPRELEASE)
161 {
162 int rc;
163 rc = release_addr(pData, &bp->bp_ciaddr);
164 LogRel(("NAT: %s %R[IP4]\n",
165 rc ? "DHCP released IP address" : "Ignored DHCP release for IP address",
166 &bp->bp_ciaddr));
167 /* This message is not to be answered in any way. */
168 return;
169 }
170 if ( dhcp_msg_type != DHCPDISCOVER
171 && dhcp_msg_type != DHCPREQUEST)
172 return;
173
174 /* XXX: this is a hack to get the client mac address */
175 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
176
177 if ((m = m_get(pData)) == NULL)
178 return;
179 m->m_data += if_maxlinkhdr; /*reserve ether header */
180 rbp = mtod(m, struct bootp_t *);
181 memset(rbp, 0, sizeof(struct bootp_t));
182#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
183 m->m_data += sizeof(struct udpiphdr);
184#endif
185
186 if (dhcp_msg_type == DHCPDISCOVER)
187 {
188 /* Do not allocate a new lease for clients that forgot that they had a lease. */
189 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
190 if (!bc)
191 {
192 new_addr:
193 bc = get_new_addr(pData, &daddr.sin_addr);
194 if (!bc)
195 {
196 LogRel(("NAT: DHCP no IP address left\n"));
197 Log(("no address left\n"));
198 return;
199 }
200 memcpy(bc->macaddr, client_ethaddr, 6);
201 }
202 }
203 else
204 {
205 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
206 if (!bc)
207 {
208 /* if never assigned, behaves as if it was already
209 assigned (windows fix because it remembers its address) */
210 goto new_addr;
211 }
212 }
213
214 if ( tftp_prefix
215 && RTDirExists(tftp_prefix)
216 && bootp_filename)
217 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
218
219 /* Address/port of the DHCP server. */
220 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
221 saddr.sin_port = htons(BOOTP_SERVER);
222
223 daddr.sin_port = htons(BOOTP_CLIENT);
224
225 rbp->bp_op = BOOTP_REPLY;
226 rbp->bp_xid = bp->bp_xid;
227 rbp->bp_htype = 1;
228 rbp->bp_hlen = 6;
229 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
230
231 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
232 rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
233
234 q = rbp->bp_vend;
235 memcpy(q, rfc1533_cookie, 4);
236 q += 4;
237
238 if (dhcp_msg_type == DHCPDISCOVER)
239 {
240 *q++ = RFC2132_MSG_TYPE;
241 *q++ = 1;
242 *q++ = DHCPOFFER;
243 }
244 else if (dhcp_msg_type == DHCPREQUEST)
245 {
246 *q++ = RFC2132_MSG_TYPE;
247 *q++ = 1;
248 if ( requested_ip.s_addr != 0xffffffff
249 && requested_ip.s_addr != daddr.sin_addr.s_addr)
250 {
251 /* network changed */
252 *q++ = DHCPNAK;
253 send_nak = 1;
254 }
255 else
256 *q++ = DHCPACK;
257 }
258
259 if (send_nak)
260 LogRel(("NAT: Client requested IP address %R[IP4] -- sending NAK\n",
261 &requested_ip));
262 else
263 LogRel(("NAT: DHCP offered IP address %R[IP4]\n",
264 &daddr.sin_addr));
265 if ( dhcp_msg_type == DHCPDISCOVER
266 || dhcp_msg_type == DHCPREQUEST)
267 {
268 FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
269 }
270
271 if (!send_nak &&
272 ( dhcp_msg_type == DHCPDISCOVER
273 || dhcp_msg_type == DHCPREQUEST))
274 {
275#ifdef VBOX_WITH_MULTI_DNS
276 struct dns_entry *de = NULL;
277 struct dns_domain_entry *dd = NULL;
278 int added = 0;
279#endif
280 uint32_t lease_time = htonl(LEASE_TIME);
281 uint32_t netmask = htonl(pData->netmask);
282
283 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
284 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr.sin_addr);
285
286#ifndef VBOX_WITH_MULTI_DNS
287 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
288 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &dns_addr_dhcp.s_addr);
289#else
290# ifdef VBOX_WITH_SLIRP_DNS_PROXY
291 if (pData->use_dns_proxy)
292 {
293 uint32_t addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
294 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &addr);
295 }
296 else
297# endif
298 LIST_FOREACH(de, &pData->dns_list_head, de_list)
299 {
300 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
301 }
302 LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
303 {
304
305 if (dd->dd_pszDomain == NULL)
306 continue;
307 if (added != 0)
308 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, ","); /* never meet valid separator here in RFC1533*/
309 else
310 added = 1;
311 val = (int)strlen(dd->dd_pszDomain);
312 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, dd->dd_pszDomain);
313 }
314#endif
315
316 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
317
318 if (*slirp_hostname)
319 {
320 val = (int)strlen(slirp_hostname);
321 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
322 }
323
324#ifndef VBOX_WITH_MULTI_DNS
325 if (pData->pszDomain && pData->fPassDomain)
326 {
327 val = (int)strlen(pData->pszDomain);
328 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, pData->pszDomain);
329 }
330#endif
331 }
332 *q++ = RFC1533_END;
333
334 m->m_len = sizeof(struct bootp_t)
335 - sizeof(struct ip)
336 - sizeof(struct udphdr);
337#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
338 m->m_data += sizeof(struct udphdr)
339 + sizeof(struct ip);
340#endif
341 /* Reply to the broadcast address, as some clients perform paranoid checks. */
342 daddr.sin_addr.s_addr = INADDR_BROADCAST;
343 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
344}
345
346void bootp_input(PNATState pData, struct mbuf *m)
347{
348 struct bootp_t *bp = mtod(m, struct bootp_t *);
349
350 if (bp->bp_op == BOOTP_REQUEST)
351 bootp_reply(pData, bp);
352}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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