1 | /* $Id: bootp.c 29656 2010-05-19 06:05:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * NAT - BOOTP/DHCP server emulation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * QEMU BOOTP/DHCP server
|
---|
22 | *
|
---|
23 | * Copyright (c) 2004 Fabrice Bellard
|
---|
24 | *
|
---|
25 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
26 | * of this software and associated documentation files (the "Software"), to deal
|
---|
27 | * in the Software without restriction, including without limitation the rights
|
---|
28 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
29 | * copies of the Software, and to permit persons to whom the Software is
|
---|
30 | * furnished to do so, subject to the following conditions:
|
---|
31 | *
|
---|
32 | * The above copyright notice and this permission notice shall be included in
|
---|
33 | * all copies or substantial portions of the Software.
|
---|
34 | *
|
---|
35 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
36 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
37 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
38 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
39 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
40 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
41 | * THE SOFTWARE.
|
---|
42 | */
|
---|
43 | #include <slirp.h>
|
---|
44 |
|
---|
45 | /** Entry in the table of known DHCP clients. */
|
---|
46 | typedef struct
|
---|
47 | {
|
---|
48 | uint32_t xid;
|
---|
49 | bool allocated;
|
---|
50 | uint8_t macaddr[6];
|
---|
51 | struct in_addr addr;
|
---|
52 | int number;
|
---|
53 | } BOOTPClient;
|
---|
54 | /** Number of DHCP clients supported by NAT. */
|
---|
55 | #define NB_ADDR 16
|
---|
56 |
|
---|
57 | #define bootp_clients ((BOOTPClient *)pData->pbootp_clients)
|
---|
58 |
|
---|
59 | /* XXX: only DHCP is supported */
|
---|
60 | static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
|
---|
61 |
|
---|
62 | static void bootp_reply(PNATState pData, struct mbuf *m0, int offReply, uint16_t flags);
|
---|
63 |
|
---|
64 | static uint8_t *dhcp_find_option(uint8_t *vend, uint8_t tag)
|
---|
65 | {
|
---|
66 | uint8_t *q = vend;
|
---|
67 | uint8_t len;
|
---|
68 | /*@todo magic validation */
|
---|
69 | q += 4; /*magic*/
|
---|
70 | while(*q != RFC1533_END)
|
---|
71 | {
|
---|
72 | if (*q == RFC1533_PAD)
|
---|
73 | continue;
|
---|
74 | if (*q == tag)
|
---|
75 | return q;
|
---|
76 | q++;
|
---|
77 | len = *q;
|
---|
78 | q += 1 + len;
|
---|
79 | }
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static BOOTPClient *bc_alloc_client(PNATState pData)
|
---|
84 | {
|
---|
85 | int i;
|
---|
86 | for (i = 0; i < NB_ADDR; i++)
|
---|
87 | {
|
---|
88 | if (!bootp_clients[i].allocated)
|
---|
89 | {
|
---|
90 | BOOTPClient *bc;
|
---|
91 |
|
---|
92 | bc = &bootp_clients[i];
|
---|
93 | memset(bc, 0, sizeof(BOOTPClient));
|
---|
94 | bc->allocated = 1;
|
---|
95 | bc->number = i;
|
---|
96 | return bc;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static BOOTPClient *get_new_addr(PNATState pData, struct in_addr *paddr)
|
---|
103 | {
|
---|
104 | BOOTPClient *bc;
|
---|
105 | bc = bc_alloc_client(pData);
|
---|
106 | if (!bc)
|
---|
107 | return NULL;
|
---|
108 |
|
---|
109 | paddr->s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | (bc->number + START_ADDR));
|
---|
110 | bc->addr.s_addr = paddr->s_addr;
|
---|
111 | return bc;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static int release_addr(PNATState pData, struct in_addr *paddr)
|
---|
115 | {
|
---|
116 | unsigned i;
|
---|
117 | for (i = 0; i < NB_ADDR; i++)
|
---|
118 | {
|
---|
119 | if (paddr->s_addr == bootp_clients[i].addr.s_addr)
|
---|
120 | {
|
---|
121 | memset(&bootp_clients[i], 0, sizeof(BOOTPClient));
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return VERR_NOT_FOUND;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * from RFC 2131 4.3.1
|
---|
130 | * Field DHCPOFFER DHCPACK DHCPNAK
|
---|
131 | * ----- --------- ------- -------
|
---|
132 | * 'op' BOOTREPLY BOOTREPLY BOOTREPLY
|
---|
133 | * 'htype' (From "Assigned Numbers" RFC)
|
---|
134 | * 'hlen' (Hardware address length in octets)
|
---|
135 | * 'hops' 0 0 0
|
---|
136 | * 'xid' 'xid' from client 'xid' from client 'xid' from client
|
---|
137 | * DHCPDISCOVER DHCPREQUEST DHCPREQUEST
|
---|
138 | * message message message
|
---|
139 | * 'secs' 0 0 0
|
---|
140 | * 'ciaddr' 0 'ciaddr' from 0
|
---|
141 | * DHCPREQUEST or 0
|
---|
142 | * 'yiaddr' IP address offered IP address 0
|
---|
143 | * to client assigned to client
|
---|
144 | * 'siaddr' IP address of next IP address of next 0
|
---|
145 | * bootstrap server bootstrap server
|
---|
146 | * 'flags' 'flags' from 'flags' from 'flags' from
|
---|
147 | * client DHCPDISCOVER client DHCPREQUEST client DHCPREQUEST
|
---|
148 | * message message message
|
---|
149 | * 'giaddr' 'giaddr' from 'giaddr' from 'giaddr' from
|
---|
150 | * client DHCPDISCOVER client DHCPREQUEST client DHCPREQUEST
|
---|
151 | * message message message
|
---|
152 | * 'chaddr' 'chaddr' from 'chaddr' from 'chaddr' from
|
---|
153 | * client DHCPDISCOVER client DHCPREQUEST client DHCPREQUEST
|
---|
154 | * message message message
|
---|
155 | * 'sname' Server host name Server host name (unused)
|
---|
156 | * or options or options
|
---|
157 | * 'file' Client boot file Client boot file (unused)
|
---|
158 | * name or options name or options
|
---|
159 | * 'options' options options
|
---|
160 | *
|
---|
161 | * Option DHCPOFFER DHCPACK DHCPNAK
|
---|
162 | * ------ --------- ------- -------
|
---|
163 | * Requested IP address MUST NOT MUST NOT MUST NOT
|
---|
164 | * IP address lease time MUST MUST (DHCPREQUEST) MUST NOT
|
---|
165 | * MUST NOT (DHCPINFORM)
|
---|
166 | * Use 'file'/'sname' fields MAY MAY MUST NOT
|
---|
167 | * DHCP message type DHCPOFFER DHCPACK DHCPNAK
|
---|
168 | * Parameter request list MUST NOT MUST NOT MUST NOT
|
---|
169 | * Message SHOULD SHOULD SHOULD
|
---|
170 | * Client identifier MUST NOT MUST NOT MAY
|
---|
171 | * Vendor class identifier MAY MAY MAY
|
---|
172 | * Server identifier MUST MUST MUST
|
---|
173 | * Maximum message size MUST NOT MUST NOT MUST NOT
|
---|
174 | * All others MAY MAY MUST NOT
|
---|
175 | */
|
---|
176 | static BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
|
---|
177 | {
|
---|
178 | int i;
|
---|
179 |
|
---|
180 | for (i = 0; i < NB_ADDR; i++)
|
---|
181 | {
|
---|
182 | if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
|
---|
183 | {
|
---|
184 | BOOTPClient *bc;
|
---|
185 |
|
---|
186 | bc = &bootp_clients[i];
|
---|
187 | bc->allocated = 1;
|
---|
188 | paddr->s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | (i + START_ADDR));
|
---|
189 | return bc;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | return NULL;
|
---|
193 | }
|
---|
194 |
|
---|
195 | static struct mbuf *dhcp_create_msg(PNATState pData, struct bootp_t *bp, struct mbuf *m, uint8_t type)
|
---|
196 | {
|
---|
197 | struct bootp_t *rbp;
|
---|
198 | struct ethhdr *eh;
|
---|
199 | uint8_t *q;
|
---|
200 |
|
---|
201 | eh = mtod(m, struct ethhdr *);
|
---|
202 | memcpy(eh->h_source, bp->bp_hwaddr, ETH_ALEN); /* XXX: if_encap just swap source with dest*/
|
---|
203 |
|
---|
204 | m->m_data += if_maxlinkhdr; /*reserve ether header */
|
---|
205 |
|
---|
206 | rbp = mtod(m, struct bootp_t *);
|
---|
207 | memset(rbp, 0, sizeof(struct bootp_t));
|
---|
208 | rbp->bp_op = BOOTP_REPLY;
|
---|
209 | rbp->bp_xid = bp->bp_xid; /* see table 3 of rfc2131*/
|
---|
210 | rbp->bp_flags = bp->bp_flags; /* figure 2 of rfc2131 */
|
---|
211 | rbp->bp_giaddr.s_addr = bp->bp_giaddr.s_addr;
|
---|
212 | #if 0 /*check flags*/
|
---|
213 | saddr.sin_port = RT_H2N_U16_C(BOOTP_SERVER);
|
---|
214 | daddr.sin_port = RT_H2N_U16_C(BOOTP_CLIENT);
|
---|
215 | #endif
|
---|
216 | rbp->bp_htype = 1;
|
---|
217 | rbp->bp_hlen = 6;
|
---|
218 | memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
|
---|
219 |
|
---|
220 | memcpy(rbp->bp_vend, rfc1533_cookie, 4); /* cookie */
|
---|
221 | q = rbp->bp_vend;
|
---|
222 | q += 4;
|
---|
223 | *q++ = RFC2132_MSG_TYPE;
|
---|
224 | *q++ = 1;
|
---|
225 | *q++ = type;
|
---|
226 |
|
---|
227 | return m;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static int dhcp_do_ack_offer(PNATState pData, struct mbuf *m, BOOTPClient *bc, int fDhcpRequest)
|
---|
231 | {
|
---|
232 | struct bootp_t *rbp = NULL;
|
---|
233 | uint8_t *q;
|
---|
234 | struct in_addr saddr;
|
---|
235 | int val;
|
---|
236 |
|
---|
237 | struct dns_entry *de = NULL;
|
---|
238 | struct dns_domain_entry *dd = NULL;
|
---|
239 | int added = 0;
|
---|
240 | uint8_t *q_dns_header = NULL;
|
---|
241 | uint32_t lease_time = RT_H2N_U32_C(LEASE_TIME);
|
---|
242 | uint32_t netmask = RT_H2N_U32(pData->netmask);
|
---|
243 |
|
---|
244 | rbp = mtod(m, struct bootp_t *);
|
---|
245 | q = &rbp->bp_vend[0];
|
---|
246 | q += 7; /* !cookie rfc 2132 + TYPE*/
|
---|
247 |
|
---|
248 | /*DHCP Offer specific*/
|
---|
249 | if ( tftp_prefix
|
---|
250 | && RTDirExists(tftp_prefix)
|
---|
251 | && bootp_filename)
|
---|
252 | RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
|
---|
253 |
|
---|
254 | Log(("NAT: DHCP: bp_file:%s\n", &rbp->bp_file));
|
---|
255 | /* Address/port of the DHCP server. */
|
---|
256 | rbp->bp_yiaddr = bc->addr; /* Client IP address */
|
---|
257 | Log(("NAT: DHCP: bp_yiaddr:%R[IP4]\n", &rbp->bp_yiaddr));
|
---|
258 | rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
|
---|
259 | Log(("NAT: DHCP: bp_siaddr:%R[IP4]\n", &rbp->bp_siaddr));
|
---|
260 | if (fDhcpRequest)
|
---|
261 | {
|
---|
262 | rbp->bp_ciaddr.s_addr = bc->addr.s_addr; /* Client IP address */
|
---|
263 | }
|
---|
264 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
265 | saddr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
|
---|
266 | #else
|
---|
267 | saddr.s_addr = pData->special_addr.s_addr;
|
---|
268 | #endif
|
---|
269 | Log(("NAT: DHCP: s_addr:%R[IP4]\n", &saddr));
|
---|
270 |
|
---|
271 | #define FILL_BOOTP_EXT(q, tag, len, pvalue) \
|
---|
272 | do { \
|
---|
273 | struct bootp_ext *be = (struct bootp_ext *)(q); \
|
---|
274 | be->bpe_tag = (tag); \
|
---|
275 | be->bpe_len = (len); \
|
---|
276 | memcpy(&be[1], (pvalue), (len)); \
|
---|
277 | (q) = (uint8_t *)(&be[1]) + (len); \
|
---|
278 | }while(0)
|
---|
279 | /* appending another value to tag, calculates len of whole block*/
|
---|
280 | #define FILL_BOOTP_APP(head, q, tag, len, pvalue) \
|
---|
281 | do { \
|
---|
282 | struct bootp_ext *be = (struct bootp_ext *)(head); \
|
---|
283 | memcpy(q, (pvalue), (len)); \
|
---|
284 | (q) += (len); \
|
---|
285 | Assert(be->bpe_tag == (tag)); \
|
---|
286 | be->bpe_len += (len); \
|
---|
287 | }while(0)
|
---|
288 |
|
---|
289 |
|
---|
290 | FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
|
---|
291 | FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr);
|
---|
292 |
|
---|
293 | if (pData->fUseDnsProxy || pData->fUseHostResolver)
|
---|
294 | {
|
---|
295 | uint32_t addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_DNS);
|
---|
296 | FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &addr);
|
---|
297 | goto skip_dns_servers;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (!TAILQ_EMPTY(&pData->pDnsList))
|
---|
301 | {
|
---|
302 | de = TAILQ_LAST(&pData->pDnsList, dns_list_head);
|
---|
303 | q_dns_header = q;
|
---|
304 | FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
|
---|
305 | }
|
---|
306 |
|
---|
307 | TAILQ_FOREACH_REVERSE(de, &pData->pDnsList, dns_list_head, de_list)
|
---|
308 | {
|
---|
309 | if (TAILQ_LAST(&pData->pDnsList, dns_list_head) == de)
|
---|
310 | continue; /* first value with head we've ingected before */
|
---|
311 | FILL_BOOTP_APP(q_dns_header, q, RFC1533_DNS, 4, &de->de_addr.s_addr);
|
---|
312 | }
|
---|
313 |
|
---|
314 | skip_dns_servers:
|
---|
315 | if (LIST_EMPTY(&pData->pDomainList))
|
---|
316 | {
|
---|
317 | /* Microsoft dhcp client doen't like domain-less dhcp and trimmed packets*/
|
---|
318 | /* dhcpcd client very sad if no domain name is passed */
|
---|
319 | FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, " ");
|
---|
320 | }
|
---|
321 | if (pData->fPassDomain && !pData->fUseHostResolver)
|
---|
322 | {
|
---|
323 | LIST_FOREACH(dd, &pData->pDomainList, dd_list)
|
---|
324 | {
|
---|
325 |
|
---|
326 | if (dd->dd_pszDomain == NULL)
|
---|
327 | continue;
|
---|
328 | /* never meet valid separator here in RFC1533*/
|
---|
329 | if (added != 0)
|
---|
330 | FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, ",");
|
---|
331 | else
|
---|
332 | added = 1;
|
---|
333 | val = (int)strlen(dd->dd_pszDomain);
|
---|
334 | FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, dd->dd_pszDomain);
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
|
---|
339 |
|
---|
340 | if (*slirp_hostname)
|
---|
341 | {
|
---|
342 | val = (int)strlen(slirp_hostname);
|
---|
343 | FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
|
---|
344 | }
|
---|
345 | slirp_arp_cache_update_or_add(pData, rbp->bp_yiaddr.s_addr, bc->macaddr);
|
---|
346 | return q - rbp->bp_vend; /*return offset */
|
---|
347 | }
|
---|
348 |
|
---|
349 | static int dhcp_send_nack(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m)
|
---|
350 | {
|
---|
351 | struct bootp_t *rbp;
|
---|
352 | uint8_t *q = NULL;
|
---|
353 | rbp = mtod(m, struct bootp_t *);
|
---|
354 |
|
---|
355 | dhcp_create_msg(pData, bp, m, DHCPNAK);
|
---|
356 | return 7;
|
---|
357 | }
|
---|
358 |
|
---|
359 | static int dhcp_send_ack(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m, int fDhcpRequest)
|
---|
360 | {
|
---|
361 | int offReply = 0; /* boot_reply will fill general options and add END before sending response */
|
---|
362 |
|
---|
363 | dhcp_create_msg(pData, bp, m, DHCPACK);
|
---|
364 | offReply = dhcp_do_ack_offer(pData, m, bc, fDhcpRequest);
|
---|
365 | return offReply;
|
---|
366 | }
|
---|
367 |
|
---|
368 | static int dhcp_send_offer(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m)
|
---|
369 | {
|
---|
370 | int offReply = 0; /* boot_reply will fill general options and add END before sending response */
|
---|
371 |
|
---|
372 | dhcp_create_msg(pData, bp, m, DHCPOFFER);
|
---|
373 | offReply = dhcp_do_ack_offer(pData, m, bc, /* fDhcpRequest=*/ 0);
|
---|
374 | return offReply;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * decoding client messages RFC2131 (4.3.6)
|
---|
379 | * ---------------------------------------------------------------------
|
---|
380 | * | |INIT-REBOOT |SELECTING |RENEWING |REBINDING |
|
---|
381 | * ---------------------------------------------------------------------
|
---|
382 | * |broad/unicast |broadcast |broadcast |unicast |broadcast |
|
---|
383 | * |server-ip |MUST NOT |MUST |MUST NOT |MUST NOT |
|
---|
384 | * |requested-ip |MUST |MUST |MUST NOT |MUST NOT |
|
---|
385 | * |ciaddr |zero |zero |IP address |IP address|
|
---|
386 | * ---------------------------------------------------------------------
|
---|
387 | *
|
---|
388 | */
|
---|
389 |
|
---|
390 | enum DHCP_REQUEST_STATES
|
---|
391 | {
|
---|
392 | INIT_REBOOT,
|
---|
393 | SELECTING,
|
---|
394 | RENEWING,
|
---|
395 | REBINDING,
|
---|
396 | NONE
|
---|
397 | };
|
---|
398 |
|
---|
399 | static int dhcp_decode_request(PNATState pData, struct bootp_t *bp, const uint8_t *buf, int size, struct mbuf *m)
|
---|
400 | {
|
---|
401 | BOOTPClient *bc = NULL;
|
---|
402 | struct in_addr daddr;
|
---|
403 | int offReply;
|
---|
404 | uint8_t *req_ip = NULL;
|
---|
405 | uint8_t *server_ip = NULL;
|
---|
406 | uint32_t ui32;
|
---|
407 | enum DHCP_REQUEST_STATES dhcp_stat = NONE;
|
---|
408 |
|
---|
409 | /* need to understand which type of request we get */
|
---|
410 | req_ip = dhcp_find_option(&bp->bp_vend[0], RFC2132_REQ_ADDR);
|
---|
411 | server_ip = dhcp_find_option(&bp->bp_vend[0], RFC2132_SRV_ID);
|
---|
412 | bc = find_addr(pData, &daddr, bp->bp_hwaddr);
|
---|
413 |
|
---|
414 | if (server_ip != NULL)
|
---|
415 | {
|
---|
416 | /* selecting */
|
---|
417 | if (!bc)
|
---|
418 | {
|
---|
419 | LogRel(("NAT: DHCP no IP was allocated\n"));
|
---|
420 | return -1;
|
---|
421 | }
|
---|
422 |
|
---|
423 | dhcp_stat = SELECTING;
|
---|
424 | Assert((bp->bp_ciaddr.s_addr == INADDR_ANY));
|
---|
425 | Assert((*(uint32_t *)(req_ip + 2) == bc->addr.s_addr)); /*the same address as in offer*/
|
---|
426 | #if 0
|
---|
427 | /* DSL xid in request differ from offer */
|
---|
428 | Assert((bp->bp_xid == bc->xid));
|
---|
429 | #endif
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | if (req_ip != NULL)
|
---|
434 | {
|
---|
435 | /* init-reboot */
|
---|
436 | dhcp_stat = INIT_REBOOT;
|
---|
437 | }
|
---|
438 | else
|
---|
439 | {
|
---|
440 | /* table 4 of rfc2131 */
|
---|
441 | if (bp->bp_flags & RT_H2N_U16_C(DHCP_FLAGS_B))
|
---|
442 | dhcp_stat = REBINDING;
|
---|
443 | else
|
---|
444 | dhcp_stat = RENEWING;
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | /*?? renewing ??*/
|
---|
449 | switch (dhcp_stat)
|
---|
450 | {
|
---|
451 | case RENEWING:
|
---|
452 | Assert((server_ip == NULL && req_ip == NULL && bp->bp_ciaddr.s_addr != INADDR_ANY));
|
---|
453 | if (bc != NULL)
|
---|
454 | {
|
---|
455 | Assert((bc->addr.s_addr == bp->bp_ciaddr.s_addr));
|
---|
456 | /*if it already here well just do ack, we aren't aware of dhcp time expiration*/
|
---|
457 | }
|
---|
458 | else
|
---|
459 | {
|
---|
460 | if ((bp->bp_ciaddr.s_addr & RT_H2N_U32(pData->netmask)) != pData->special_addr.s_addr)
|
---|
461 | {
|
---|
462 | LogRel(("NAT: Client %R[IP4] requested IP -- sending NAK\n", &bp->bp_ciaddr));
|
---|
463 | offReply = dhcp_send_nack(pData, bp, bc, m);
|
---|
464 | return offReply;
|
---|
465 | }
|
---|
466 |
|
---|
467 | bc = bc_alloc_client(pData);
|
---|
468 | if (!bc)
|
---|
469 | {
|
---|
470 | LogRel(("NAT: can't alloc address. RENEW has been silently ignored.\n"));
|
---|
471 | return -1;
|
---|
472 | }
|
---|
473 |
|
---|
474 | Assert((bp->bp_hlen == ETH_ALEN));
|
---|
475 | memcpy(bc->macaddr, bp->bp_hwaddr, bp->bp_hlen);
|
---|
476 | bc->addr.s_addr = bp->bp_ciaddr.s_addr;
|
---|
477 | }
|
---|
478 | break;
|
---|
479 |
|
---|
480 | case INIT_REBOOT:
|
---|
481 | Assert(server_ip == NULL);
|
---|
482 | Assert(req_ip != NULL);
|
---|
483 | ui32 = *(uint32_t *)(req_ip + 2);
|
---|
484 | if ((ui32 & RT_H2N_U32(pData->netmask)) != pData->special_addr.s_addr)
|
---|
485 | {
|
---|
486 | LogRel(("NAT: address %R[IP4] has been requested -- sending NAK\n", &ui32));
|
---|
487 | offReply = dhcp_send_nack(pData, bp, bc, m);
|
---|
488 | return offReply;
|
---|
489 | }
|
---|
490 |
|
---|
491 | bc = bc_alloc_client(pData);
|
---|
492 | if (!bc)
|
---|
493 | {
|
---|
494 | LogRel(("NAT: can't alloc address. RENEW has been silently ignored\n"));
|
---|
495 | return -1;
|
---|
496 | }
|
---|
497 | Assert((bp->bp_hlen == ETH_ALEN));
|
---|
498 | memcpy(bc->macaddr, bp->bp_hwaddr, bp->bp_hlen);
|
---|
499 | bc->addr.s_addr = ui32;
|
---|
500 | break;
|
---|
501 |
|
---|
502 | case NONE:
|
---|
503 | Assert((dhcp_stat != NONE));
|
---|
504 | return -1;
|
---|
505 |
|
---|
506 | default:
|
---|
507 | break;
|
---|
508 | }
|
---|
509 |
|
---|
510 | LogRel(("NAT: DHCP offered IP address %R[IP4]\n", &bc->addr));
|
---|
511 | offReply = dhcp_send_ack(pData, bp, bc, m, /* fDhcpRequest=*/ 1);
|
---|
512 | return offReply;
|
---|
513 | }
|
---|
514 |
|
---|
515 | static int dhcp_decode_discover(PNATState pData, struct bootp_t *bp, const uint8_t *buf, int size, int fDhcpDiscover, struct mbuf *m)
|
---|
516 | {
|
---|
517 | BOOTPClient *bc;
|
---|
518 | struct in_addr daddr;
|
---|
519 | int offReply;
|
---|
520 |
|
---|
521 | if (fDhcpDiscover)
|
---|
522 | {
|
---|
523 | bc = find_addr(pData, &daddr, bp->bp_hwaddr);
|
---|
524 | if (!bc)
|
---|
525 | {
|
---|
526 | bc = get_new_addr(pData, &daddr);
|
---|
527 | if (!bc)
|
---|
528 | {
|
---|
529 | LogRel(("NAT: DHCP no IP address left\n"));
|
---|
530 | Log(("no address left\n"));
|
---|
531 | return -1;
|
---|
532 | }
|
---|
533 | memcpy(bc->macaddr, bp->bp_hwaddr, 6);
|
---|
534 | }
|
---|
535 |
|
---|
536 | bc->xid = bp->bp_xid;
|
---|
537 | LogRel(("NAT: DHCP offered IP address %R[IP4]\n", &bc->addr));
|
---|
538 | offReply = dhcp_send_offer(pData, bp, bc, m);
|
---|
539 | return offReply;
|
---|
540 | }
|
---|
541 | else
|
---|
542 | {
|
---|
543 | bc = find_addr(pData, &daddr, bp->bp_hwaddr);
|
---|
544 | if (!bc)
|
---|
545 | {
|
---|
546 | LogRel(("NAT: DHCP Inform was ignored no boot client was found\n"));
|
---|
547 | return -1;
|
---|
548 | }
|
---|
549 |
|
---|
550 | LogRel(("NAT: DHCP offered IP address %R[IP4]\n", &bc->addr));
|
---|
551 | offReply = dhcp_send_ack(pData, bp, bc, m, /* fDhcpRequest=*/ 0);
|
---|
552 | return offReply;
|
---|
553 | }
|
---|
554 |
|
---|
555 | return -1;
|
---|
556 | }
|
---|
557 |
|
---|
558 | static int dhcp_decode_release(PNATState pData, struct bootp_t *bp, const uint8_t *buf, int size)
|
---|
559 | {
|
---|
560 | int rc = release_addr(pData, &bp->bp_ciaddr);
|
---|
561 | LogRel(("NAT: %s %R[IP4]\n",
|
---|
562 | RT_SUCCESS(rc) ? "DHCP released IP address" : "Ignored DHCP release for IP address",
|
---|
563 | &bp->bp_ciaddr));
|
---|
564 | return 0;
|
---|
565 | }
|
---|
566 | /**
|
---|
567 | * fields for discovering t
|
---|
568 | * Field DHCPDISCOVER DHCPREQUEST DHCPDECLINE,
|
---|
569 | * DHCPINFORM DHCPRELEASE
|
---|
570 | * ----- ------------ ----------- -----------
|
---|
571 | * 'op' BOOTREQUEST BOOTREQUEST BOOTREQUEST
|
---|
572 | * 'htype' (From "Assigned Numbers" RFC)
|
---|
573 | * 'hlen' (Hardware address length in octets)
|
---|
574 | * 'hops' 0 0 0
|
---|
575 | * 'xid' selected by client 'xid' from server selected by
|
---|
576 | * DHCPOFFER message client
|
---|
577 | * 'secs' 0 or seconds since 0 or seconds since 0
|
---|
578 | * DHCP process started DHCP process started
|
---|
579 | * 'flags' Set 'BROADCAST' Set 'BROADCAST' 0
|
---|
580 | * flag if client flag if client
|
---|
581 | * requires broadcast requires broadcast
|
---|
582 | * reply reply
|
---|
583 | * 'ciaddr' 0 (DHCPDISCOVER) 0 or client's 0 (DHCPDECLINE)
|
---|
584 | * client's network address client's network
|
---|
585 | * network address (BOUND/RENEW/REBIND) address
|
---|
586 | * (DHCPINFORM) (DHCPRELEASE)
|
---|
587 | * 'yiaddr' 0 0 0
|
---|
588 | * 'siaddr' 0 0 0
|
---|
589 | * 'giaddr' 0 0 0
|
---|
590 | * 'chaddr' client's hardware client's hardware client's hardware
|
---|
591 | * address address address
|
---|
592 | * 'sname' options, if options, if (unused)
|
---|
593 | * indicated in indicated in
|
---|
594 | * 'sname/file' 'sname/file'
|
---|
595 | * option; otherwise option; otherwise
|
---|
596 | * unused unused
|
---|
597 | * 'file' options, if options, if (unused)
|
---|
598 | * indicated in indicated in
|
---|
599 | * 'sname/file' 'sname/file'
|
---|
600 | * option; otherwise option; otherwise
|
---|
601 | * unused unused
|
---|
602 | * 'options' options options (unused)
|
---|
603 | * Requested IP address MAY MUST (in MUST
|
---|
604 | * (DISCOVER) SELECTING or (DHCPDECLINE),
|
---|
605 | * MUST NOT INIT-REBOOT) MUST NOT
|
---|
606 | * (INFORM) MUST NOT (in (DHCPRELEASE)
|
---|
607 | * BOUND or
|
---|
608 | * RENEWING)
|
---|
609 | * IP address lease time MAY MAY MUST NOT
|
---|
610 | * (DISCOVER)
|
---|
611 | * MUST NOT
|
---|
612 | * (INFORM)
|
---|
613 | * Use 'file'/'sname' fields MAY MAY MAY
|
---|
614 | * DHCP message type DHCPDISCOVER/ DHCPREQUEST DHCPDECLINE/
|
---|
615 | * DHCPINFORM DHCPRELEASE
|
---|
616 | * Client identifier MAY MAY MAY
|
---|
617 | * Vendor class identifier MAY MAY MUST NOT
|
---|
618 | * Server identifier MUST NOT MUST (after MUST
|
---|
619 | * SELECTING)
|
---|
620 | * MUST NOT (after
|
---|
621 | * INIT-REBOOT,
|
---|
622 | * BOUND, RENEWING
|
---|
623 | * or REBINDING)
|
---|
624 | * Parameter request list MAY MAY MUST NOT
|
---|
625 | * Maximum message size MAY MAY MUST NOT
|
---|
626 | * Message SHOULD NOT SHOULD NOT SHOULD
|
---|
627 | * Site-specific MAY MAY MUST NOT
|
---|
628 | * All others MAY MAY MUST NOT
|
---|
629 | *
|
---|
630 | */
|
---|
631 | static void dhcp_decode(PNATState pData, struct bootp_t *bp, const uint8_t *buf, int size)
|
---|
632 | {
|
---|
633 | const uint8_t *p, *p_end;
|
---|
634 | int rc;
|
---|
635 | int pmsg_type;
|
---|
636 | struct in_addr req_ip;
|
---|
637 | int fDhcpDiscover = 0;
|
---|
638 | uint8_t *parameter_list = NULL;
|
---|
639 | struct mbuf *m = NULL;
|
---|
640 |
|
---|
641 | pmsg_type = 0;
|
---|
642 | p = buf;
|
---|
643 | p_end = buf + size;
|
---|
644 | if (size < 5)
|
---|
645 | return;
|
---|
646 |
|
---|
647 | if (memcmp(p, rfc1533_cookie, 4) != 0)
|
---|
648 | return;
|
---|
649 |
|
---|
650 | p = dhcp_find_option(bp->bp_vend, RFC2132_MSG_TYPE);
|
---|
651 | Assert(p);
|
---|
652 | if (p == NULL)
|
---|
653 | return;
|
---|
654 | /*
|
---|
655 | * We're going update dns list at least once per DHCP transaction (!not on every operation
|
---|
656 | * within transaction), assuming that transaction can't be longer than 1 min.
|
---|
657 | */
|
---|
658 | if ( !pData->fUseHostResolver
|
---|
659 | && ( pData->dnsLastUpdate == 0
|
---|
660 | || curtime - pData->dnsLastUpdate > 60 * 1000)) /* one minute*/
|
---|
661 | {
|
---|
662 | uint8_t i = 2; /* i = 0 - tag, i == 1 - length */
|
---|
663 | parameter_list = dhcp_find_option(&bp->bp_vend[0], RFC2132_PARAM_LIST);
|
---|
664 | for (;parameter_list && i < parameter_list[1]; ++i)
|
---|
665 | {
|
---|
666 | if (parameter_list[i] == RFC1533_DNS)
|
---|
667 | {
|
---|
668 | slirp_release_dns_list(pData);
|
---|
669 | slirp_init_dns_list(pData);
|
---|
670 | pData->dnsLastUpdate = curtime;
|
---|
671 | break;
|
---|
672 | }
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
677 | if ((m = m_get(pData)) == NULL)
|
---|
678 | #else
|
---|
679 | if ((m = m_getcl(pData, M_DONTWAIT, MT_HEADER, M_PKTHDR)) == NULL)
|
---|
680 | #endif
|
---|
681 | {
|
---|
682 | LogRel(("NAT: can't alocate memory for response!\n"));
|
---|
683 | return;
|
---|
684 | }
|
---|
685 |
|
---|
686 | switch (*(p+2))
|
---|
687 | {
|
---|
688 | case DHCPDISCOVER:
|
---|
689 | fDhcpDiscover = 1;
|
---|
690 | /**/
|
---|
691 | case DHCPINFORM:
|
---|
692 | rc = dhcp_decode_discover(pData, bp, buf, size, fDhcpDiscover, m);
|
---|
693 | if (rc > 0)
|
---|
694 | goto reply;
|
---|
695 | break;
|
---|
696 |
|
---|
697 | case DHCPREQUEST:
|
---|
698 | rc = dhcp_decode_request(pData, bp, buf, size, m);
|
---|
699 | if (rc > 0)
|
---|
700 | goto reply;
|
---|
701 | break;
|
---|
702 |
|
---|
703 | case DHCPRELEASE:
|
---|
704 | rc = dhcp_decode_release(pData, bp, buf, size);
|
---|
705 | /* no reply required */
|
---|
706 | break;
|
---|
707 |
|
---|
708 | case DHCPDECLINE:
|
---|
709 | p = dhcp_find_option(&bp->bp_vend[0], RFC2132_REQ_ADDR);
|
---|
710 | req_ip.s_addr = *(uint32_t *)(p + 2);
|
---|
711 | rc = bootp_cache_lookup_ether_by_ip(pData, req_ip.s_addr, NULL);
|
---|
712 | if (RT_FAILURE(rc))
|
---|
713 | {
|
---|
714 | /* Not registered */
|
---|
715 | BOOTPClient *bc;
|
---|
716 | bc = bc_alloc_client(pData);
|
---|
717 | Assert(bc);
|
---|
718 | bc->addr.s_addr = req_ip.s_addr;
|
---|
719 | slirp_arp_who_has(pData, bc->addr.s_addr);
|
---|
720 | LogRel(("NAT: %R[IP4] has been already registered\n", &req_ip));
|
---|
721 | }
|
---|
722 | /* no response required */
|
---|
723 | break;
|
---|
724 |
|
---|
725 | default:
|
---|
726 | AssertMsgFailed(("unsupported DHCP message type"));
|
---|
727 | }
|
---|
728 | Assert(m);
|
---|
729 | /*silently ignore*/
|
---|
730 | m_freem(pData, m);
|
---|
731 | return;
|
---|
732 |
|
---|
733 | reply:
|
---|
734 | bootp_reply(pData, m, rc, bp->bp_flags);
|
---|
735 | return;
|
---|
736 | }
|
---|
737 |
|
---|
738 | static void bootp_reply(PNATState pData, struct mbuf *m, int offReply, uint16_t flags)
|
---|
739 | {
|
---|
740 | struct sockaddr_in saddr, daddr;
|
---|
741 | struct bootp_t *rbp = NULL;
|
---|
742 | uint8_t *q = NULL;
|
---|
743 | int nack;
|
---|
744 | rbp = mtod(m, struct bootp_t *);
|
---|
745 | Assert((m));
|
---|
746 | Assert((rbp));
|
---|
747 | q = rbp->bp_vend;
|
---|
748 | nack = (q[6] == DHCPNAK);
|
---|
749 | q += offReply;
|
---|
750 |
|
---|
751 | #ifndef VBOX_WITH_NAT_SERVICE
|
---|
752 | saddr.sin_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
|
---|
753 | #else
|
---|
754 | saddr.sin_addr.s_addr = pData->special_addr.s_addr;
|
---|
755 | #endif
|
---|
756 |
|
---|
757 | FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
|
---|
758 |
|
---|
759 | *q++ = RFC1533_END; /* end of message */
|
---|
760 |
|
---|
761 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
762 | m->m_pkthdr.header = mtod(m, void *);
|
---|
763 | #endif
|
---|
764 | m->m_len = sizeof(struct bootp_t)
|
---|
765 | - sizeof(struct ip)
|
---|
766 | - sizeof(struct udphdr);
|
---|
767 | m->m_data += sizeof(struct udphdr)
|
---|
768 | + sizeof(struct ip);
|
---|
769 | if ( (flags & RT_H2N_U16_C(DHCP_FLAGS_B))
|
---|
770 | || nack != 0)
|
---|
771 | daddr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
772 | else
|
---|
773 | daddr.sin_addr.s_addr = rbp->bp_yiaddr.s_addr; /*unicast requested by client*/
|
---|
774 | saddr.sin_port = RT_H2N_U16_C(BOOTP_SERVER);
|
---|
775 | daddr.sin_port = RT_H2N_U16_C(BOOTP_CLIENT);
|
---|
776 | udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
|
---|
777 | }
|
---|
778 |
|
---|
779 | void bootp_input(PNATState pData, struct mbuf *m)
|
---|
780 | {
|
---|
781 | struct bootp_t *bp = mtod(m, struct bootp_t *);
|
---|
782 |
|
---|
783 | if (bp->bp_op == BOOTP_REQUEST)
|
---|
784 | dhcp_decode(pData, bp, bp->bp_vend, DHCP_OPT_LEN);
|
---|
785 | }
|
---|
786 |
|
---|
787 | int bootp_cache_lookup_ip_by_ether(PNATState pData,const uint8_t* ether, uint32_t *pip)
|
---|
788 | {
|
---|
789 | int i;
|
---|
790 |
|
---|
791 | if (!ether || !pip)
|
---|
792 | return VERR_INVALID_PARAMETER;
|
---|
793 |
|
---|
794 | for (i = 0; i < NB_ADDR; i++)
|
---|
795 | {
|
---|
796 | if ( bootp_clients[i].allocated
|
---|
797 | && memcmp(bootp_clients[i].macaddr, ether, ETH_ALEN) == 0)
|
---|
798 | {
|
---|
799 | *pip = bootp_clients[i].addr.s_addr;
|
---|
800 | return VINF_SUCCESS;
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 | *pip = INADDR_ANY;
|
---|
805 | return VERR_NOT_FOUND;
|
---|
806 | }
|
---|
807 |
|
---|
808 | int bootp_cache_lookup_ether_by_ip(PNATState pData, uint32_t ip, uint8_t *ether)
|
---|
809 | {
|
---|
810 | int i;
|
---|
811 | for (i = 0; i < NB_ADDR; i++)
|
---|
812 | {
|
---|
813 | if ( bootp_clients[i].allocated
|
---|
814 | && ip == bootp_clients[i].addr.s_addr)
|
---|
815 | {
|
---|
816 | if (ether != NULL)
|
---|
817 | memcpy(ether, bootp_clients[i].macaddr, ETH_ALEN);
|
---|
818 | return VINF_SUCCESS;
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | return VERR_NOT_FOUND;
|
---|
823 | }
|
---|
824 |
|
---|
825 | /*
|
---|
826 | * Initialize dhcp server
|
---|
827 | * @returns 0 - if initialization is ok, non-zero otherwise
|
---|
828 | */
|
---|
829 | int bootp_dhcp_init(PNATState pData)
|
---|
830 | {
|
---|
831 | pData->pbootp_clients = RTMemAllocZ(sizeof(BOOTPClient) * NB_ADDR);
|
---|
832 | if (!pData->pbootp_clients)
|
---|
833 | return VERR_NO_MEMORY;
|
---|
834 |
|
---|
835 | return VINF_SUCCESS;
|
---|
836 | }
|
---|
837 |
|
---|
838 | int bootp_dhcp_fini(PNATState pData)
|
---|
839 | {
|
---|
840 | if (pData->pbootp_clients != NULL)
|
---|
841 | RTMemFree(pData->pbootp_clients);
|
---|
842 |
|
---|
843 | return VINF_SUCCESS;
|
---|
844 | }
|
---|