VirtualBox

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

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

NAT: removed VBOX_WITH_SIMPLIFIED_SLIRP_SYNC as the poller API was removed some days ago

  • Property svn:eol-style set to native
File size: 10.5 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
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
183 if (dhcp_msg_type == DHCPDISCOVER)
184 {
185 /* Do not allocate a new lease for clients that forgot that they had a lease. */
186 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
187 if (!bc)
188 {
189 new_addr:
190 bc = get_new_addr(pData, &daddr.sin_addr);
191 if (!bc)
192 {
193 LogRel(("NAT: DHCP no IP address left\n"));
194 Log(("no address left\n"));
195 return;
196 }
197 memcpy(bc->macaddr, client_ethaddr, 6);
198 }
199 }
200 else
201 {
202 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
203 if (!bc)
204 {
205 /* if never assigned, behaves as if it was already
206 assigned (windows fix because it remembers its address) */
207 goto new_addr;
208 }
209 }
210
211 if ( tftp_prefix
212 && RTDirExists(tftp_prefix)
213 && bootp_filename)
214 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
215
216 /* Address/port of the DHCP server. */
217 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
218 saddr.sin_port = htons(BOOTP_SERVER);
219
220 daddr.sin_port = htons(BOOTP_CLIENT);
221
222 rbp->bp_op = BOOTP_REPLY;
223 rbp->bp_xid = bp->bp_xid;
224 rbp->bp_htype = 1;
225 rbp->bp_hlen = 6;
226 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
227
228 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
229 rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
230
231 q = rbp->bp_vend;
232 memcpy(q, rfc1533_cookie, 4);
233 q += 4;
234
235 if (dhcp_msg_type == DHCPDISCOVER)
236 {
237 *q++ = RFC2132_MSG_TYPE;
238 *q++ = 1;
239 *q++ = DHCPOFFER;
240 }
241 else if (dhcp_msg_type == DHCPREQUEST)
242 {
243 *q++ = RFC2132_MSG_TYPE;
244 *q++ = 1;
245 if ( requested_ip.s_addr != 0xffffffff
246 && requested_ip.s_addr != daddr.sin_addr.s_addr)
247 {
248 /* network changed */
249 *q++ = DHCPNAK;
250 send_nak = 1;
251 }
252 else
253 *q++ = DHCPACK;
254 }
255
256 if (send_nak)
257 LogRel(("NAT: Client requested IP address %R[IP4] -- sending NAK\n",
258 &requested_ip));
259 else
260 LogRel(("NAT: DHCP offered IP address %R[IP4]\n",
261 &daddr.sin_addr));
262 if ( dhcp_msg_type == DHCPDISCOVER
263 || dhcp_msg_type == DHCPREQUEST)
264 {
265 FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
266 }
267
268 if (!send_nak &&
269 ( dhcp_msg_type == DHCPDISCOVER
270 || dhcp_msg_type == DHCPREQUEST))
271 {
272#ifdef VBOX_WITH_MULTI_DNS
273 struct dns_entry *de = NULL;
274 struct dns_domain_entry *dd = NULL;
275 int added = 0;
276#endif
277 uint32_t lease_time = htonl(LEASE_TIME);
278 uint32_t netmask = htonl(pData->netmask);
279
280 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
281 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr.sin_addr);
282
283#ifndef VBOX_WITH_MULTI_DNS
284 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
285 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &dns_addr_dhcp.s_addr);
286#else
287# ifdef VBOX_WITH_SLIRP_DNS_PROXY
288 if (pData->use_dns_proxy)
289 {
290 uint32_t addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
291 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &addr);
292 }
293 else
294# endif
295 LIST_FOREACH(de, &pData->dns_list_head, de_list)
296 {
297 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
298 }
299 if (LIST_EMPTY(&pData->dns_domain_list_head))
300 {
301 /* Microsoft dhcp client doen't like domain-less dhcp and trimmed packets*/
302 /* dhcpcd client very sad if no domain name is passed */
303 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, " ");
304 }
305 LIST_FOREACH(dd, &pData->dns_domain_list_head, dd_list)
306 {
307
308 if (dd->dd_pszDomain == NULL)
309 continue;
310 if (added != 0)
311 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, ","); /* never meet valid separator here in RFC1533*/
312 else
313 added = 1;
314 val = (int)strlen(dd->dd_pszDomain);
315 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, dd->dd_pszDomain);
316 }
317#endif
318
319 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
320
321 if (*slirp_hostname)
322 {
323 val = (int)strlen(slirp_hostname);
324 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
325 }
326
327#ifndef VBOX_WITH_MULTI_DNS
328 if (pData->pszDomain && pData->fPassDomain)
329 {
330 val = (int)strlen(pData->pszDomain);
331 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, pData->pszDomain);
332 }
333#endif
334 }
335 *q++ = RFC1533_END;
336
337 m->m_len = sizeof(struct bootp_t)
338 - sizeof(struct ip)
339 - sizeof(struct udphdr);
340 m->m_data += sizeof(struct udphdr)
341 + sizeof(struct ip);
342 /* Reply to the broadcast address, as some clients perform paranoid checks. */
343 daddr.sin_addr.s_addr = INADDR_BROADCAST;
344 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
345}
346
347void bootp_input(PNATState pData, struct mbuf *m)
348{
349 struct bootp_t *bp = mtod(m, struct bootp_t *);
350
351 if (bp->bp_op == BOOTP_REQUEST)
352 bootp_reply(pData, bp);
353}
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