VirtualBox

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

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

NAT:bootp more readable

  • 屬性 svn:eol-style 設為 native
檔案大小: 9.2 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 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 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_TFTP);
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 = saddr.sin_addr; /* Server IP address */
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 uint32_t lease_time = htonl(LEASE_TIME);
273 uint32_t netmask = htonl(pData->netmask);
274
275 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
276 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr.sin_addr);
277
278 dns_addr_dhcp.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
279 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &dns_addr_dhcp.s_addr);
280
281 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
282
283 if (*slirp_hostname)
284 {
285 val = (int)strlen(slirp_hostname);
286 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
287 }
288
289 if (pData->pszDomain && pData->fPassDomain)
290 {
291 val = (int)strlen(pData->pszDomain);
292 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, pData->pszDomain);
293 }
294 }
295 *q++ = RFC1533_END;
296
297 m->m_len = sizeof(struct bootp_t)
298 - sizeof(struct ip)
299 - sizeof(struct udphdr);
300#ifdef VBOX_WITH_SIMPLIFIED_SLIRP_SYNC
301 m->m_data += sizeof(struct udphdr)
302 + sizeof(struct ip);
303#endif
304 /* Reply to the broadcast address, as some clients perform paranoid checks. */
305 daddr.sin_addr.s_addr = INADDR_BROADCAST;
306 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
307}
308
309void bootp_input(PNATState pData, struct mbuf *m)
310{
311 struct bootp_t *bp = mtod(m, struct bootp_t *);
312
313 if (bp->bp_op == BOOTP_REQUEST)
314 bootp_reply(pData, bp);
315}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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