VirtualBox

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

Last change on this file since 6290 was 5332, checked in by vboxsync, 17 years ago

Make PXE booting in NAT mode work without much hassle.

  • Property svn:eol-style set to native
File size: 8.3 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
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 void release_addr(PNATState pData, struct in_addr *paddr)
59{
60 int i;
61
62 i = ntohl(paddr->s_addr) - START_ADDR - ntohl(special_addr.s_addr);
63 if (i >= NB_ADDR)
64 return;
65 memset(bootp_clients[i].macaddr, '\0', 6);
66 bootp_clients[i].allocated = 0;
67}
68
69static BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
70{
71 BOOTPClient *bc;
72 int i;
73
74 for(i = 0; i < NB_ADDR; i++) {
75 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
76 goto found;
77 }
78 return NULL;
79 found:
80 bc = &bootp_clients[i];
81 bc->allocated = 1;
82 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
83 return bc;
84}
85
86static void dhcp_decode(const uint8_t *buf, int size,
87 int *pmsg_type)
88{
89 const uint8_t *p, *p_end;
90 int len, tag;
91
92 *pmsg_type = 0;
93
94 p = buf;
95 p_end = buf + size;
96 if (size < 5)
97 return;
98 if (memcmp(p, rfc1533_cookie, 4) != 0)
99 return;
100 p += 4;
101 while (p < p_end) {
102 tag = p[0];
103 if (tag == RFC1533_PAD) {
104 p++;
105 } else if (tag == RFC1533_END) {
106 break;
107 } else {
108 p++;
109 if (p >= p_end)
110 break;
111 len = *p++;
112 dprintf("dhcp: tag=0x%02x len=%d\n", tag, len);
113
114 switch(tag) {
115 case RFC2132_MSG_TYPE:
116 if (len >= 1)
117 *pmsg_type = p[0];
118 break;
119 default:
120 break;
121 }
122 p += len;
123 }
124 }
125}
126
127static void bootp_reply(PNATState pData, struct bootp_t *bp)
128{
129 BOOTPClient *bc;
130 struct mbuf *m;
131 struct bootp_t *rbp;
132 struct sockaddr_in saddr, daddr;
133 struct in_addr dns_addr_dhcp;
134 int dhcp_msg_type, val;
135 uint8_t *q;
136
137 /* extract exact DHCP msg type */
138 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
139 dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
140
141 if (dhcp_msg_type == 0)
142 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
143
144 if (dhcp_msg_type == DHCPRELEASE) {
145 uint32_t addr = ntohl(bp->bp_ciaddr.s_addr);
146 release_addr(pData, &bp->bp_ciaddr);
147 LogRel(("NAT: DHCP released IP address %u.%u.%u.%u\n",
148 addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
149 dprintf("released addr=%08x\n", ntohl(bp->bp_ciaddr.s_addr));
150 /* This message is not to be answered in any way. */
151 return;
152 }
153 if (dhcp_msg_type != DHCPDISCOVER &&
154 dhcp_msg_type != DHCPREQUEST)
155 return;
156 /* XXX: this is a hack to get the client mac address */
157 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
158
159 if ((m = m_get(pData)) == NULL)
160 return;
161 m->m_data += if_maxlinkhdr;
162 rbp = (struct bootp_t *)m->m_data;
163 m->m_data += sizeof(struct udpiphdr);
164 memset(rbp, 0, sizeof(struct bootp_t));
165
166 if (dhcp_msg_type == DHCPDISCOVER) {
167 /* Do not allocate a new lease for clients that forgot that they had a lease. */
168 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
169 if (!bc)
170 {
171 new_addr:
172 bc = get_new_addr(pData, &daddr.sin_addr);
173 if (!bc) {
174 LogRel(("NAT: DHCP no IP address left\n"));
175 dprintf("no address left\n");
176 return;
177 }
178 memcpy(bc->macaddr, client_ethaddr, 6);
179 }
180 } else {
181 bc = find_addr(pData, &daddr.sin_addr, bp->bp_hwaddr);
182 if (!bc) {
183 /* if never assigned, behaves as if it was already
184 assigned (windows fix because it remembers its address) */
185 goto new_addr;
186 }
187 }
188
189 if (tftp_prefix && RTDirExists(tftp_prefix) && bootp_filename)
190 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
191
192 {
193 uint32_t addr = ntohl(daddr.sin_addr.s_addr);
194 LogRel(("NAT: DHCP offered IP address %u.%u.%u.%u\n",
195 addr >> 24, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff));
196 }
197 dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
198
199 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
200 saddr.sin_port = htons(BOOTP_SERVER);
201
202 daddr.sin_port = htons(BOOTP_CLIENT);
203
204 rbp->bp_op = BOOTP_REPLY;
205 rbp->bp_xid = bp->bp_xid;
206 rbp->bp_htype = 1;
207 rbp->bp_hlen = 6;
208 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
209
210 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
211 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
212
213 q = rbp->bp_vend;
214 memcpy(q, rfc1533_cookie, 4);
215 q += 4;
216
217 if (dhcp_msg_type == DHCPDISCOVER) {
218 *q++ = RFC2132_MSG_TYPE;
219 *q++ = 1;
220 *q++ = DHCPOFFER;
221 } else if (dhcp_msg_type == DHCPREQUEST) {
222 *q++ = RFC2132_MSG_TYPE;
223 *q++ = 1;
224 *q++ = DHCPACK;
225 }
226
227 if (dhcp_msg_type == DHCPDISCOVER ||
228 dhcp_msg_type == DHCPREQUEST) {
229 *q++ = RFC2132_SRV_ID;
230 *q++ = 4;
231 memcpy(q, &saddr.sin_addr, 4);
232 q += 4;
233
234 *q++ = RFC1533_NETMASK;
235 *q++ = 4;
236 *q++ = 0xff;
237 *q++ = 0xff;
238 *q++ = 0xff;
239 *q++ = 0x00;
240
241 *q++ = RFC1533_GATEWAY;
242 *q++ = 4;
243 memcpy(q, &saddr.sin_addr, 4);
244 q += 4;
245
246 *q++ = RFC1533_DNS;
247 *q++ = 4;
248 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
249 memcpy(q, &dns_addr_dhcp, 4);
250 q += 4;
251
252 *q++ = RFC2132_LEASE_TIME;
253 *q++ = 4;
254 val = htonl(LEASE_TIME);
255 memcpy(q, &val, 4);
256 q += 4;
257
258 if (*slirp_hostname) {
259 val = strlen(slirp_hostname);
260 *q++ = RFC1533_HOSTNAME;
261 *q++ = val;
262 memcpy(q, slirp_hostname, val);
263 q += val;
264 }
265
266 if (pData->pszDomain && pData->fPassDomain)
267 {
268 val = strlen(pData->pszDomain);
269 *q++ = RFC1533_DOMAINNAME;
270 *q++ = val;
271 memcpy(q, pData->pszDomain, val);
272 q += val;
273 }
274 }
275 *q++ = RFC1533_END;
276
277 m->m_len = sizeof(struct bootp_t) -
278 sizeof(struct ip) - sizeof(struct udphdr);
279 /* Reply to the broadcast address, as some clients perform paranoid checks. */
280 daddr.sin_addr.s_addr = INADDR_BROADCAST;
281 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
282}
283
284void bootp_input(PNATState pData, struct mbuf *m)
285{
286 struct bootp_t *bp = mtod(m, struct bootp_t *);
287
288 if (bp->bp_op == BOOTP_REQUEST) {
289 bootp_reply(pData, bp);
290 }
291}
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