VirtualBox

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

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

NAT: clean up dhcp a bit, and fox the TFTP server address calculation

  • Property svn:eol-style set to native
File size: 9.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 struct in_addr dns_addr_dhcp;
136 int dhcp_msg_type, val;
137 uint8_t *q;
138 struct in_addr requested_ip; /* the requested IP in DHCPREQUEST */
139 int send_nak = 0;
140
141#define FILL_BOOTP_EXT(q, tag, len, pvalue) \
142 do { \
143 struct bootp_ext *be = (struct bootp_ext *)(q); \
144 be->bpe_tag = (tag); \
145 be->bpe_len = (len); \
146 memcpy(&be[1], (pvalue), (len)); \
147 (q) = (uint8_t *)(&be[1]) + (len); \
148 }while(0)
149
150 /* extract exact DHCP msg type */
151 requested_ip.s_addr = 0xffffffff;
152 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type, &requested_ip);
153 Log(("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type));
154
155 if (dhcp_msg_type == 0)
156 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
157
158 if (dhcp_msg_type == DHCPRELEASE)
159 {
160 int rc;
161 rc = release_addr(pData, &bp->bp_ciaddr);
162 LogRel(("NAT: %s %R[IP4]\n",
163 rc ? "DHCP released IP address" : "Ignored DHCP release for IP address",
164 &bp->bp_ciaddr));
165 /* This message is not to be answered in any way. */
166 return;
167 }
168 if ( dhcp_msg_type != DHCPDISCOVER
169 && dhcp_msg_type != DHCPREQUEST)
170 return;
171
172 /* XXX: this is a hack to get the client mac address */
173 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
174
175 if ((m = m_get(pData)) == NULL)
176 return;
177 m->m_data += if_maxlinkhdr; /*reserve ether header */
178 rbp = mtod(m, struct bootp_t *);
179 memset(rbp, 0, sizeof(struct bootp_t));
180#ifndef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
181 m->m_data += sizeof(struct udpiphdr);
182#endif
183
184 if (dhcp_msg_type == DHCPDISCOVER)
185 {
186 /* Do not allocate a new lease for clients that forgot that they had a lease. */
187 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
188 if (!bc)
189 {
190 new_addr:
191 bc = get_new_addr(pData, &daddr.sin_addr);
192 if (!bc)
193 {
194 LogRel(("NAT: DHCP no IP address left\n"));
195 Log(("no address left\n"));
196 return;
197 }
198 memcpy(bc->macaddr, client_ethaddr, 6);
199 }
200 }
201 else
202 {
203 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
204 if (!bc)
205 {
206 /* if never assigned, behaves as if it was already
207 assigned (windows fix because it remembers its address) */
208 goto new_addr;
209 }
210 }
211
212 if ( tftp_prefix
213 && RTDirExists(tftp_prefix)
214 && bootp_filename)
215 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
216
217 /* Address/port of the DHCP server. */
218 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
219 saddr.sin_port = htons(BOOTP_SERVER);
220
221 daddr.sin_port = htons(BOOTP_CLIENT);
222
223 rbp->bp_op = BOOTP_REPLY;
224 rbp->bp_xid = bp->bp_xid;
225 rbp->bp_htype = 1;
226 rbp->bp_hlen = 6;
227 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
228
229 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
230 rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
231
232 q = rbp->bp_vend;
233 memcpy(q, rfc1533_cookie, 4);
234 q += 4;
235
236 if (dhcp_msg_type == DHCPDISCOVER)
237 {
238 *q++ = RFC2132_MSG_TYPE;
239 *q++ = 1;
240 *q++ = DHCPOFFER;
241 }
242 else if (dhcp_msg_type == DHCPREQUEST)
243 {
244 *q++ = RFC2132_MSG_TYPE;
245 *q++ = 1;
246 if ( requested_ip.s_addr != 0xffffffff
247 && requested_ip.s_addr != daddr.sin_addr.s_addr)
248 {
249 /* network changed */
250 *q++ = DHCPNAK;
251 send_nak = 1;
252 }
253 else
254 *q++ = DHCPACK;
255 }
256
257 if (send_nak)
258 LogRel(("NAT: Client requested IP address %R[IP4] -- sending NAK\n",
259 &requested_ip));
260 else
261 LogRel(("NAT: DHCP offered IP address %R[IP4]\n",
262 &daddr.sin_addr));
263 if ( dhcp_msg_type == DHCPDISCOVER
264 || dhcp_msg_type == DHCPREQUEST)
265 {
266 FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
267 }
268
269 if (!send_nak &&
270 ( dhcp_msg_type == DHCPDISCOVER
271 || dhcp_msg_type == DHCPREQUEST))
272 {
273#ifdef VBOX_WITH_MULTI_DNS
274 struct dns_entry *de = NULL;
275#endif
276 uint32_t lease_time = htonl(LEASE_TIME);
277 uint32_t netmask = htonl(pData->netmask);
278
279 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
280 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr.sin_addr);
281
282#ifndef VBOX_WITH_MULTI_DNS
283 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
284 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &dns_addr_dhcp.s_addr);
285#else
286 LIST_FOREACH(de, &pData->dns_list_head, de_list)
287 {
288 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
289 }
290#endif
291
292 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
293
294 if (*slirp_hostname)
295 {
296 val = (int)strlen(slirp_hostname);
297 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
298 }
299
300 if (pData->pszDomain && pData->fPassDomain)
301 {
302 val = (int)strlen(pData->pszDomain);
303 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, pData->pszDomain);
304 }
305 }
306 *q++ = RFC1533_END;
307
308 m->m_len = sizeof(struct bootp_t)
309 - sizeof(struct ip)
310 - sizeof(struct udphdr);
311#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
312 m->m_data += sizeof(struct udphdr)
313 + sizeof(struct ip);
314#endif
315 /* Reply to the broadcast address, as some clients perform paranoid checks. */
316 daddr.sin_addr.s_addr = INADDR_BROADCAST;
317 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
318}
319
320void bootp_input(PNATState pData, struct mbuf *m)
321{
322 struct bootp_t *bp = mtod(m, struct bootp_t *);
323
324 if (bp->bp_op == BOOTP_REQUEST)
325 bootp_reply(pData, bp);
326}
Note: See TracBrowser for help on using the repository browser.

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