VirtualBox

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

Last change on this file since 19318 was 19313, checked in by vboxsync, 16 years ago

NAT: slirp servicing several guests

  • Property svn:eol-style set to native
File size: 11.2 KB
Line 
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
129#ifndef VBOX_WITH_NAT_SERVICE
130static void bootp_reply(PNATState pData, struct bootp_t *bp)
131#else
132static void bootp_reply(PNATState pData, struct mbuf *m0)
133#endif
134{
135 BOOTPClient *bc;
136 struct mbuf *m; /* XXX: @todo vasily - it'd be better to reuse this mbuf here */
137#ifdef VBOX_WITH_NAT_SERVICE
138 struct bootp_t *bp = mtod(m0, struct bootp_t *);
139 struct ethhdr *eh;
140#endif
141 struct bootp_t *rbp;
142 struct sockaddr_in saddr, daddr;
143#ifndef VBOX_WITH_MULTI_DNS
144 struct in_addr dns_addr_dhcp;
145#endif
146 int dhcp_msg_type, val;
147 uint8_t *q;
148 struct in_addr requested_ip; /* the requested IP in DHCPREQUEST */
149 int send_nak = 0;
150
151#define FILL_BOOTP_EXT(q, tag, len, pvalue) \
152 do { \
153 struct bootp_ext *be = (struct bootp_ext *)(q); \
154 be->bpe_tag = (tag); \
155 be->bpe_len = (len); \
156 memcpy(&be[1], (pvalue), (len)); \
157 (q) = (uint8_t *)(&be[1]) + (len); \
158 }while(0)
159
160 /* extract exact DHCP msg type */
161 requested_ip.s_addr = 0xffffffff;
162 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type, &requested_ip);
163 Log(("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type));
164
165 if (dhcp_msg_type == 0)
166 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
167
168 if (dhcp_msg_type == DHCPRELEASE)
169 {
170 int rc;
171 rc = release_addr(pData, &bp->bp_ciaddr);
172 LogRel(("NAT: %s %R[IP4]\n",
173 rc ? "DHCP released IP address" : "Ignored DHCP release for IP address",
174 &bp->bp_ciaddr));
175 /* This message is not to be answered in any way. */
176 return;
177 }
178 if ( dhcp_msg_type != DHCPDISCOVER
179 && dhcp_msg_type != DHCPREQUEST)
180 return;
181
182#ifndef VBOX_WITH_NAT_SERVICE
183 /* XXX: this is a hack to get the client mac address */
184 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
185#endif
186
187 if ((m = m_get(pData)) == NULL)
188 return;
189#ifdef VBOX_WITH_NAT_SERVICE
190 eh = mtod(m, struct ethhdr *);
191 memcpy(eh->h_source, bp->bp_hwaddr, ETH_ALEN); /* XXX: if_encap just swap source with dest*/
192#endif
193 m->m_data += if_maxlinkhdr; /*reserve ether header */
194 rbp = mtod(m, struct bootp_t *);
195 memset(rbp, 0, sizeof(struct bootp_t));
196
197 if (dhcp_msg_type == DHCPDISCOVER)
198 {
199 /* Do not allocate a new lease for clients that forgot that they had a lease. */
200 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
201 if (!bc)
202 {
203 new_addr:
204 bc = get_new_addr(pData, &daddr.sin_addr);
205 if (!bc)
206 {
207 LogRel(("NAT: DHCP no IP address left\n"));
208 Log(("no address left\n"));
209 return;
210 }
211#ifdef VBOX_WITH_NAT_SERVICE
212 memcpy(bc->macaddr, bp->bp_hwaddr, 6);
213#else
214 memcpy(bc->macaddr, client_ethaddr, 6);
215#endif
216 }
217 }
218 else
219 {
220 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
221 if (!bc)
222 {
223 /* if never assigned, behaves as if it was already
224 assigned (windows fix because it remembers its address) */
225 goto new_addr;
226 }
227 }
228
229 if ( tftp_prefix
230 && RTDirExists(tftp_prefix)
231 && bootp_filename)
232 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
233
234 /* Address/port of the DHCP server. */
235#ifndef VBOX_WITH_NAT_SERVICE
236 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
237#else
238 saddr.sin_addr.s_addr = special_addr.s_addr;
239#endif
240
241 saddr.sin_port = htons(BOOTP_SERVER);
242
243 daddr.sin_port = htons(BOOTP_CLIENT);
244
245 rbp->bp_op = BOOTP_REPLY;
246 rbp->bp_xid = bp->bp_xid;
247 rbp->bp_htype = 1;
248 rbp->bp_hlen = 6;
249 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
250
251 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
252 rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
253
254 q = rbp->bp_vend;
255 memcpy(q, rfc1533_cookie, 4);
256 q += 4;
257
258 if (dhcp_msg_type == DHCPDISCOVER)
259 {
260 *q++ = RFC2132_MSG_TYPE;
261 *q++ = 1;
262 *q++ = DHCPOFFER;
263 }
264 else if (dhcp_msg_type == DHCPREQUEST)
265 {
266 *q++ = RFC2132_MSG_TYPE;
267 *q++ = 1;
268 if ( requested_ip.s_addr != 0xffffffff
269 && requested_ip.s_addr != daddr.sin_addr.s_addr)
270 {
271 /* network changed */
272 *q++ = DHCPNAK;
273 send_nak = 1;
274 }
275 else
276 *q++ = DHCPACK;
277 }
278
279 if (send_nak)
280 LogRel(("NAT: Client requested IP address %R[IP4] -- sending NAK\n",
281 &requested_ip));
282 else
283 LogRel(("NAT: DHCP offered IP address %R[IP4]\n",
284 &daddr.sin_addr));
285 if ( dhcp_msg_type == DHCPDISCOVER
286 || dhcp_msg_type == DHCPREQUEST)
287 {
288 FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
289 }
290
291 if (!send_nak &&
292 ( dhcp_msg_type == DHCPDISCOVER
293 || dhcp_msg_type == DHCPREQUEST))
294 {
295#ifdef VBOX_WITH_MULTI_DNS
296 struct dns_entry *de = NULL;
297 struct dns_domain_entry *dd = NULL;
298 int added = 0;
299#endif
300 uint32_t lease_time = htonl(LEASE_TIME);
301 uint32_t netmask = htonl(pData->netmask);
302
303 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
304 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr.sin_addr);
305
306#ifndef VBOX_WITH_MULTI_DNS
307 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
308 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &dns_addr_dhcp.s_addr);
309#else
310# ifdef VBOX_WITH_SLIRP_DNS_PROXY
311 if (pData->use_dns_proxy)
312 {
313 uint32_t addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
314 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &addr);
315 }
316 else
317# endif
318 LIST_FOREACH(de, &pData->dns_list_head, de_list)
319 {
320 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
321 }
322 if (LIST_EMPTY(&pData->dns_domain_list_head))
323 {
324 /* Microsoft dhcp client doen't like domain-less dhcp and trimmed packets*/
325 /* dhcpcd client very sad if no domain name is passed */
326 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, " ");
327 }
328 LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
329 {
330
331 if (dd->dd_pszDomain == NULL)
332 continue;
333 if (added != 0)
334 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, ","); /* never meet valid separator here in RFC1533*/
335 else
336 added = 1;
337 val = (int)strlen(dd->dd_pszDomain);
338 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, dd->dd_pszDomain);
339 }
340#endif
341
342 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
343
344 if (*slirp_hostname)
345 {
346 val = (int)strlen(slirp_hostname);
347 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
348 }
349
350#ifndef VBOX_WITH_MULTI_DNS
351 if (pData->pszDomain && pData->fPassDomain)
352 {
353 val = (int)strlen(pData->pszDomain);
354 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, pData->pszDomain);
355 }
356#endif
357 }
358 *q++ = RFC1533_END;
359
360 m->m_len = sizeof(struct bootp_t)
361 - sizeof(struct ip)
362 - sizeof(struct udphdr);
363 m->m_data += sizeof(struct udphdr)
364 + sizeof(struct ip);
365 /* Reply to the broadcast address, as some clients perform paranoid checks. */
366 daddr.sin_addr.s_addr = INADDR_BROADCAST;
367 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
368}
369
370void bootp_input(PNATState pData, struct mbuf *m)
371{
372 struct bootp_t *bp = mtod(m, struct bootp_t *);
373
374 if (bp->bp_op == BOOTP_REQUEST)
375#ifndef VBOX_WITH_NAT_SERVICE
376 bootp_reply(pData, bp);
377#else
378 bootp_reply(pData, m);
379#endif
380}
Note: See TracBrowser for help on using the repository browser.

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