VirtualBox

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

Last change on this file since 76747 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.5 KB
Line 
1/* $Id: bootp.c 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * NAT - BOOTP/DHCP server emulation.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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
44#include <slirp.h>
45#include <libslirp.h>
46#include <iprt/errcore.h>
47
48/** Entry in the table of known DHCP clients. */
49typedef struct
50{
51 uint32_t xid;
52 bool allocated;
53 uint8_t macaddr[ETH_ALEN];
54 struct in_addr addr;
55 int number;
56} BOOTPClient;
57/** Number of DHCP clients supported by NAT. */
58#define NB_ADDR 16
59
60#define bootp_clients ((BOOTPClient *)pData->pbootp_clients)
61
62/* XXX: only DHCP is supported */
63static const uint8_t rfc1533_cookie[4] = { RFC1533_COOKIE };
64
65static void bootp_reply(PNATState pData, struct mbuf *m0, int offReply, uint16_t flags);
66
67
68static uint8_t *dhcp_find_option(uint8_t *vendor, size_t vlen, uint8_t tag, ssize_t checklen)
69{
70 uint8_t *q = vendor;
71 size_t len = vlen;
72
73 q += sizeof(rfc1533_cookie);
74 len -= sizeof(rfc1533_cookie);
75
76 while (len > 0)
77 {
78 uint8_t *optptr = q;
79 uint8_t opt;
80 uint8_t optlen;
81
82 opt = *q++;
83 --len;
84
85 if (opt == RFC1533_END)
86 break;
87
88 if (opt == RFC1533_PAD)
89 continue;
90
91 if (len == 0)
92 break; /* no option length byte */
93
94 optlen = *q++;
95 --len;
96
97 if (len < optlen)
98 break; /* option value truncated */
99
100 if (opt == tag)
101 {
102 if (checklen > 0 && optlen != checklen)
103 break; /* wrong option size */
104
105 return optptr;
106 }
107
108 q += optlen;
109 len -= optlen;
110 }
111
112 return NULL;
113}
114
115static BOOTPClient *bc_alloc_client(PNATState pData)
116{
117 int i;
118 LogFlowFuncEnter();
119 for (i = 0; i < NB_ADDR; i++)
120 {
121 if (!bootp_clients[i].allocated)
122 {
123 BOOTPClient *bc;
124
125 bc = &bootp_clients[i];
126 memset(bc, 0, sizeof(BOOTPClient));
127 bc->allocated = 1;
128 bc->number = i;
129 LogFlowFunc(("LEAVE: bc:%d\n", bc->number));
130 return bc;
131 }
132 }
133 LogFlowFunc(("LEAVE: NULL\n"));
134 return NULL;
135}
136
137static BOOTPClient *get_new_addr(PNATState pData, struct in_addr *paddr)
138{
139 BOOTPClient *bc;
140 LogFlowFuncEnter();
141 bc = bc_alloc_client(pData);
142 if (!bc)
143 return NULL;
144
145 paddr->s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | (bc->number + START_ADDR));
146 bc->addr.s_addr = paddr->s_addr;
147 LogFlowFunc(("LEAVE: paddr:%RTnaipv4, bc:%d\n", paddr->s_addr, bc->number));
148 return bc;
149}
150
151static int release_addr(PNATState pData, struct in_addr *paddr)
152{
153 unsigned i;
154 for (i = 0; i < NB_ADDR; i++)
155 {
156 if (paddr->s_addr == bootp_clients[i].addr.s_addr)
157 {
158 memset(&bootp_clients[i], 0, sizeof(BOOTPClient));
159 return VINF_SUCCESS;
160 }
161 }
162 return VERR_NOT_FOUND;
163}
164
165/*
166 * from RFC 2131 4.3.1
167 * Field DHCPOFFER DHCPACK DHCPNAK
168 * ----- --------- ------- -------
169 * 'op' BOOTREPLY BOOTREPLY BOOTREPLY
170 * 'htype' (From "Assigned Numbers" RFC)
171 * 'hlen' (Hardware address length in octets)
172 * 'hops' 0 0 0
173 * 'xid' 'xid' from client 'xid' from client 'xid' from client
174 * DHCPDISCOVER DHCPREQUEST DHCPREQUEST
175 * message message message
176 * 'secs' 0 0 0
177 * 'ciaddr' 0 'ciaddr' from 0
178 * DHCPREQUEST or 0
179 * 'yiaddr' IP address offered IP address 0
180 * to client assigned to client
181 * 'siaddr' IP address of next IP address of next 0
182 * bootstrap server bootstrap server
183 * 'flags' 'flags' from 'flags' from 'flags' from
184 * client DHCPDISCOVER client DHCPREQUEST client DHCPREQUEST
185 * message message message
186 * 'giaddr' 'giaddr' from 'giaddr' from 'giaddr' from
187 * client DHCPDISCOVER client DHCPREQUEST client DHCPREQUEST
188 * message message message
189 * 'chaddr' 'chaddr' from 'chaddr' from 'chaddr' from
190 * client DHCPDISCOVER client DHCPREQUEST client DHCPREQUEST
191 * message message message
192 * 'sname' Server host name Server host name (unused)
193 * or options or options
194 * 'file' Client boot file Client boot file (unused)
195 * name or options name or options
196 * 'options' options options
197 *
198 * Option DHCPOFFER DHCPACK DHCPNAK
199 * ------ --------- ------- -------
200 * Requested IP address MUST NOT MUST NOT MUST NOT
201 * IP address lease time MUST MUST (DHCPREQUEST) MUST NOT
202 * MUST NOT (DHCPINFORM)
203 * Use 'file'/'sname' fields MAY MAY MUST NOT
204 * DHCP message type DHCPOFFER DHCPACK DHCPNAK
205 * Parameter request list MUST NOT MUST NOT MUST NOT
206 * Message SHOULD SHOULD SHOULD
207 * Client identifier MUST NOT MUST NOT MAY
208 * Vendor class identifier MAY MAY MAY
209 * Server identifier MUST MUST MUST
210 * Maximum message size MUST NOT MUST NOT MUST NOT
211 * All others MAY MAY MUST NOT
212 */
213static BOOTPClient *find_addr(PNATState pData, struct in_addr *paddr, const uint8_t *macaddr)
214{
215 int i;
216
217 LogFlowFunc(("macaddr:%RTmac\n", macaddr));
218 for (i = 0; i < NB_ADDR; i++)
219 {
220 if ( memcmp(macaddr, bootp_clients[i].macaddr, ETH_ALEN) == 0
221 && bootp_clients[i].allocated != 0)
222 {
223 BOOTPClient *bc;
224
225 bc = &bootp_clients[i];
226 bc->allocated = 1;
227 paddr->s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | (i + START_ADDR));
228 LogFlowFunc(("LEAVE: paddr:%RTnaipv4 bc:%d\n", paddr->s_addr, bc->number));
229 return bc;
230 }
231 }
232 LogFlowFunc(("LEAVE: NULL\n"));
233 return NULL;
234}
235
236static struct mbuf *dhcp_create_msg(PNATState pData, struct bootp_t *bp, struct mbuf *m, uint8_t type)
237{
238 struct bootp_t *rbp;
239 struct ethhdr *eh;
240 uint8_t *q;
241
242 eh = mtod(m, struct ethhdr *);
243 memcpy(eh->h_source, bp->bp_hwaddr, ETH_ALEN); /* XXX: if_encap just swap source with dest */
244
245 m->m_data += if_maxlinkhdr; /*reserve ether header */
246
247 rbp = mtod(m, struct bootp_t *);
248 memset(rbp, 0, sizeof(struct bootp_t));
249 rbp->bp_op = BOOTP_REPLY;
250 rbp->bp_xid = bp->bp_xid; /* see table 3 of rfc2131*/
251 rbp->bp_flags = bp->bp_flags; /* figure 2 of rfc2131 */
252 rbp->bp_giaddr.s_addr = bp->bp_giaddr.s_addr;
253#if 0 /*check flags*/
254 saddr.sin_port = RT_H2N_U16_C(BOOTP_SERVER);
255 daddr.sin_port = RT_H2N_U16_C(BOOTP_CLIENT);
256#endif
257 rbp->bp_htype = 1;
258 rbp->bp_hlen = 6;
259 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
260
261 memcpy(rbp->bp_vend, rfc1533_cookie, 4); /* cookie */
262 q = rbp->bp_vend;
263 q += 4;
264 *q++ = RFC2132_MSG_TYPE;
265 *q++ = 1;
266 *q++ = type;
267
268 return m;
269}
270
271static int dhcp_do_ack_offer(PNATState pData, struct mbuf *m, BOOTPClient *bc, int fDhcpRequest)
272{
273 struct bootp_t *rbp = NULL;
274 uint8_t *q;
275 struct in_addr saddr;
276 int val;
277
278 struct dns_entry *de = NULL;
279 struct dns_domain_entry *dd = NULL;
280 int added = 0;
281 uint8_t *q_dns_header = NULL;
282 uint32_t lease_time = RT_H2N_U32_C(LEASE_TIME);
283 uint32_t netmask = RT_H2N_U32(pData->netmask);
284
285 rbp = mtod(m, struct bootp_t *);
286 q = &rbp->bp_vend[0];
287 q += 7; /* !cookie rfc 2132 + TYPE*/
288
289 /*DHCP Offer specific*/
290 /*
291 * we're care in built-in tftp server about existence/validness of the boot file.
292 */
293 if (bootp_filename)
294 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
295
296 Log(("NAT: DHCP: bp_file:%s\n", &rbp->bp_file));
297 /* Address/port of the DHCP server. */
298 rbp->bp_yiaddr = bc->addr; /* Client IP address */
299 Log(("NAT: DHCP: bp_yiaddr:%RTnaipv4\n", rbp->bp_yiaddr.s_addr));
300 rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
301 Log(("NAT: DHCP: bp_siaddr:%RTnaipv4\n", rbp->bp_siaddr.s_addr));
302 if (fDhcpRequest)
303 {
304 rbp->bp_ciaddr.s_addr = bc->addr.s_addr; /* Client IP address */
305 }
306 saddr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
307 Log(("NAT: DHCP: s_addr:%RTnaipv4\n", saddr.s_addr));
308
309#define FILL_BOOTP_EXT(q, tag, len, pvalue) \
310 do { \
311 struct bootp_ext *be = (struct bootp_ext *)(q); \
312 be->bpe_tag = (tag); \
313 be->bpe_len = (len); \
314 memcpy(&be[1], (pvalue), (len)); \
315 (q) = (uint8_t *)(&be[1]) + (len); \
316 }while(0)
317/* appending another value to tag, calculates len of whole block*/
318#define FILL_BOOTP_APP(head, q, tag, len, pvalue) \
319 do { \
320 struct bootp_ext *be = (struct bootp_ext *)(head); \
321 memcpy(q, (pvalue), (len)); \
322 (q) += (len); \
323 Assert(be->bpe_tag == (tag)); \
324 be->bpe_len += (len); \
325 }while(0)
326
327
328 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
329 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr);
330
331 if (pData->fUseDnsProxy || pData->fUseHostResolver)
332 {
333 uint32_t addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_DNS);
334 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &addr);
335 }
336 else if (!TAILQ_EMPTY(&pData->pDnsList))
337 {
338 de = TAILQ_LAST(&pData->pDnsList, dns_list_head);
339 q_dns_header = q;
340 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
341
342 TAILQ_FOREACH_REVERSE(de, &pData->pDnsList, dns_list_head, de_list)
343 {
344 if (TAILQ_LAST(&pData->pDnsList, dns_list_head) == de)
345 continue; /* first value with head we've ingected before */
346 FILL_BOOTP_APP(q_dns_header, q, RFC1533_DNS, 4, &de->de_addr.s_addr);
347 }
348 }
349
350 if (pData->fPassDomain && !pData->fUseHostResolver)
351 {
352 LIST_FOREACH(dd, &pData->pDomainList, dd_list)
353 {
354
355 if (dd->dd_pszDomain == NULL)
356 continue;
357 /* never meet valid separator here in RFC1533*/
358 if (added != 0)
359 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, ",");
360 else
361 added = 1;
362 val = (int)strlen(dd->dd_pszDomain);
363 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, dd->dd_pszDomain);
364 }
365 }
366
367 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
368
369 if (*slirp_hostname)
370 {
371 val = (int)strlen(slirp_hostname);
372 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
373 }
374 /* Temporary fix: do not pollute ARP cache from BOOTP because it may result
375 in network loss due to cache entry override w/ invalid MAC address. */
376 /*slirp_arp_cache_update_or_add(pData, rbp->bp_yiaddr.s_addr, bc->macaddr);*/
377 return q - rbp->bp_vend; /*return offset */
378}
379
380static int dhcp_send_nack(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m)
381{
382 NOREF(bc);
383
384 dhcp_create_msg(pData, bp, m, DHCPNAK);
385 return 7;
386}
387
388static int dhcp_send_ack(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m, int fDhcpRequest)
389{
390 int offReply = 0; /* boot_reply will fill general options and add END before sending response */
391
392 dhcp_create_msg(pData, bp, m, DHCPACK);
393 slirp_update_guest_addr_guess(pData, bc->addr.s_addr, "DHCP ACK");
394 offReply = dhcp_do_ack_offer(pData, m, bc, fDhcpRequest);
395 return offReply;
396}
397
398static int dhcp_send_offer(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m)
399{
400 int offReply = 0; /* boot_reply will fill general options and add END before sending response */
401
402 dhcp_create_msg(pData, bp, m, DHCPOFFER);
403 offReply = dhcp_do_ack_offer(pData, m, bc, /* fDhcpRequest=*/ 0);
404 return offReply;
405}
406
407/**
408 * decoding client messages RFC2131 (4.3.6)
409 * ---------------------------------------------------------------------
410 * | |INIT-REBOOT |SELECTING |RENEWING |REBINDING |
411 * ---------------------------------------------------------------------
412 * |broad/unicast |broadcast |broadcast |unicast |broadcast |
413 * |server-ip |MUST NOT |MUST |MUST NOT |MUST NOT |
414 * |requested-ip |MUST |MUST |MUST NOT |MUST NOT |
415 * |ciaddr |zero |zero |IP address |IP address|
416 * ---------------------------------------------------------------------
417 *
418 */
419
420enum DHCP_REQUEST_STATES
421{
422 INIT_REBOOT,
423 SELECTING,
424 RENEWING,
425 REBINDING,
426 NONE
427};
428
429static int dhcp_decode_request(PNATState pData, struct bootp_t *bp, size_t vlen, struct mbuf *m)
430{
431 BOOTPClient *bc = NULL;
432 struct in_addr daddr;
433 int offReply;
434 uint8_t *req_ip = NULL;
435 uint8_t *server_ip = NULL;
436 uint32_t ui32;
437 enum DHCP_REQUEST_STATES dhcp_stat = NONE;
438
439 /* need to understand which type of request we get */
440 req_ip = dhcp_find_option(bp->bp_vend, vlen,
441 RFC2132_REQ_ADDR, sizeof(struct in_addr));
442 server_ip = dhcp_find_option(bp->bp_vend, vlen,
443 RFC2132_SRV_ID, sizeof(struct in_addr));
444
445 bc = find_addr(pData, &daddr, bp->bp_hwaddr);
446
447 if (server_ip != NULL)
448 {
449 /* selecting */
450 if (!bc)
451 {
452 LogRel(("NAT: DHCP no IP was allocated\n"));
453 return -1;
454 }
455
456 if ( !req_ip
457 || bp->bp_ciaddr.s_addr != INADDR_ANY)
458 {
459 LogRel(("NAT: Invalid SELECTING request\n"));
460 return -1; /* silently ignored */
461 }
462 dhcp_stat = SELECTING;
463 /* Assert((bp->bp_ciaddr.s_addr == INADDR_ANY)); */
464 }
465 else
466 {
467 if (req_ip != NULL)
468 {
469 /* init-reboot */
470 dhcp_stat = INIT_REBOOT;
471 }
472 else
473 {
474 /* table 4 of rfc2131 */
475 if (bp->bp_flags & RT_H2N_U16_C(DHCP_FLAGS_B))
476 dhcp_stat = REBINDING;
477 else
478 dhcp_stat = RENEWING;
479 }
480 }
481
482 /*?? renewing ??*/
483 switch (dhcp_stat)
484 {
485 case RENEWING:
486 /**
487 * decoding client messages RFC2131 (4.3.6)
488 * ------------------------------
489 * | |RENEWING |
490 * ------------------------------
491 * |broad/unicast |unicast |
492 * |server-ip |MUST NOT |
493 * |requested-ip |MUST NOT |
494 * |ciaddr |IP address |
495 * ------------------------------
496 */
497 if ( server_ip
498 || req_ip
499 || bp->bp_ciaddr.s_addr == INADDR_ANY)
500 {
501 LogRel(("NAT: Invalid RENEWING dhcp request\n"));
502 return -1; /* silent ignorance */
503 }
504 if (bc != NULL)
505 {
506 /* Assert((bc->addr.s_addr == bp->bp_ciaddr.s_addr)); */
507 /*if it already here well just do ack, we aren't aware of dhcp time expiration*/
508 }
509 else
510 {
511 if ((bp->bp_ciaddr.s_addr & RT_H2N_U32(pData->netmask)) != pData->special_addr.s_addr)
512 {
513 LogRel(("NAT: Client %RTnaipv4 requested IP -- sending NAK\n", bp->bp_ciaddr.s_addr));
514 offReply = dhcp_send_nack(pData, bp, bc, m);
515 return offReply;
516 }
517
518 bc = bc_alloc_client(pData);
519 if (!bc)
520 {
521 LogRel(("NAT: Can't allocate address. RENEW has been silently ignored\n"));
522 return -1;
523 }
524
525 memcpy(bc->macaddr, bp->bp_hwaddr, ETH_ALEN);
526 bc->addr.s_addr = bp->bp_ciaddr.s_addr;
527 }
528 break;
529
530 case INIT_REBOOT:
531 /**
532 * decoding client messages RFC2131 (4.3.6)
533 * ------------------------------
534 * | |INIT-REBOOT |
535 * ------------------------------
536 * |broad/unicast |broadcast |
537 * |server-ip |MUST NOT |
538 * |requested-ip |MUST |
539 * |ciaddr |zero |
540 * ------------------------------
541 *
542 */
543 if ( server_ip
544 || !req_ip
545 || bp->bp_ciaddr.s_addr != INADDR_ANY)
546 {
547 LogRel(("NAT: Invalid INIT-REBOOT dhcp request\n"));
548 return -1; /* silently ignored */
549 }
550 ui32 = *(uint32_t *)(req_ip + 2);
551 if ((ui32 & RT_H2N_U32(pData->netmask)) != pData->special_addr.s_addr)
552 {
553 LogRel(("NAT: Address %RTnaipv4 has been requested -- sending NAK\n", ui32));
554 offReply = dhcp_send_nack(pData, bp, bc, m);
555 return offReply;
556 }
557
558 /* find_addr() got some result? */
559 if (!bc)
560 {
561 bc = bc_alloc_client(pData);
562 if (!bc)
563 {
564 LogRel(("NAT: Can't allocate address. RENEW has been silently ignored\n"));
565 return -1;
566 }
567 }
568
569 memcpy(bc->macaddr, bp->bp_hwaddr, ETH_ALEN);
570 bc->addr.s_addr = ui32;
571 break;
572
573 case NONE:
574 if (dhcp_stat == REBINDING)
575 LogRel(("NAT: REBINDING state isn't impemented\n"));
576 else if (dhcp_stat == SELECTING)
577 LogRel(("NAT: SELECTING state isn't impemented\n"));
578 return -1;
579
580 default:
581 break;
582 }
583
584 LogRel(("NAT: DHCP offered IP address %RTnaipv4\n", bc->addr.s_addr));
585 offReply = dhcp_send_ack(pData, bp, bc, m, /* fDhcpRequest=*/ 1);
586 return offReply;
587}
588
589static int dhcp_decode_discover(PNATState pData, struct bootp_t *bp, int fDhcpDiscover, struct mbuf *m)
590{
591 BOOTPClient *bc;
592 struct in_addr daddr;
593 int offReply;
594
595 if (fDhcpDiscover)
596 {
597 bc = find_addr(pData, &daddr, bp->bp_hwaddr);
598 if (!bc)
599 {
600 bc = get_new_addr(pData, &daddr);
601 if (!bc)
602 {
603 LogRel(("NAT: DHCP no IP address left\n"));
604 Log(("no address left\n"));
605 return -1;
606 }
607 memcpy(bc->macaddr, bp->bp_hwaddr, ETH_ALEN);
608 }
609
610 bc->xid = bp->bp_xid;
611 LogRel(("NAT: DHCP offered IP address %RTnaipv4\n", bc->addr.s_addr));
612 offReply = dhcp_send_offer(pData, bp, bc, m);
613 return offReply;
614 }
615
616 bc = find_addr(pData, &daddr, bp->bp_hwaddr);
617 if (!bc)
618 {
619 LogRel(("NAT: DHCP Inform was ignored no boot client was found\n"));
620 return -1;
621 }
622
623 LogRel(("NAT: DHCP offered IP address %RTnaipv4\n", bc->addr.s_addr));
624 offReply = dhcp_send_ack(pData, bp, bc, m, /* fDhcpRequest=*/ 0);
625 return offReply;
626}
627
628static int dhcp_decode_release(PNATState pData, struct bootp_t *bp)
629{
630 int rc = release_addr(pData, &bp->bp_ciaddr);
631 LogRel(("NAT: %s %RTnaipv4\n",
632 RT_SUCCESS(rc) ? "DHCP released IP address" : "Ignored DHCP release for IP address",
633 bp->bp_ciaddr.s_addr));
634 return 0;
635}
636
637/**
638 * fields for discovering t
639 * Field DHCPDISCOVER DHCPREQUEST DHCPDECLINE,
640 * DHCPINFORM DHCPRELEASE
641 * ----- ------------ ----------- -----------
642 * 'op' BOOTREQUEST BOOTREQUEST BOOTREQUEST
643 * 'htype' (From "Assigned Numbers" RFC)
644 * 'hlen' (Hardware address length in octets)
645 * 'hops' 0 0 0
646 * 'xid' selected by client 'xid' from server selected by
647 * DHCPOFFER message client
648 * 'secs' 0 or seconds since 0 or seconds since 0
649 * DHCP process started DHCP process started
650 * 'flags' Set 'BROADCAST' Set 'BROADCAST' 0
651 * flag if client flag if client
652 * requires broadcast requires broadcast
653 * reply reply
654 * 'ciaddr' 0 (DHCPDISCOVER) 0 or client's 0 (DHCPDECLINE)
655 * client's network address client's network
656 * network address (BOUND/RENEW/REBIND) address
657 * (DHCPINFORM) (DHCPRELEASE)
658 * 'yiaddr' 0 0 0
659 * 'siaddr' 0 0 0
660 * 'giaddr' 0 0 0
661 * 'chaddr' client's hardware client's hardware client's hardware
662 * address address address
663 * 'sname' options, if options, if (unused)
664 * indicated in indicated in
665 * 'sname/file' 'sname/file'
666 * option; otherwise option; otherwise
667 * unused unused
668 * 'file' options, if options, if (unused)
669 * indicated in indicated in
670 * 'sname/file' 'sname/file'
671 * option; otherwise option; otherwise
672 * unused unused
673 * 'options' options options (unused)
674 * Requested IP address MAY MUST (in MUST
675 * (DISCOVER) SELECTING or (DHCPDECLINE),
676 * MUST NOT INIT-REBOOT) MUST NOT
677 * (INFORM) MUST NOT (in (DHCPRELEASE)
678 * BOUND or
679 * RENEWING)
680 * IP address lease time MAY MAY MUST NOT
681 * (DISCOVER)
682 * MUST NOT
683 * (INFORM)
684 * Use 'file'/'sname' fields MAY MAY MAY
685 * DHCP message type DHCPDISCOVER/ DHCPREQUEST DHCPDECLINE/
686 * DHCPINFORM DHCPRELEASE
687 * Client identifier MAY MAY MAY
688 * Vendor class identifier MAY MAY MUST NOT
689 * Server identifier MUST NOT MUST (after MUST
690 * SELECTING)
691 * MUST NOT (after
692 * INIT-REBOOT,
693 * BOUND, RENEWING
694 * or REBINDING)
695 * Parameter request list MAY MAY MUST NOT
696 * Maximum message size MAY MAY MUST NOT
697 * Message SHOULD NOT SHOULD NOT SHOULD
698 * Site-specific MAY MAY MUST NOT
699 * All others MAY MAY MUST NOT
700 *
701 */
702static void dhcp_decode(PNATState pData, struct bootp_t *bp, size_t vlen)
703{
704 const uint8_t *pu8RawDhcpObject;
705 int rc;
706 struct in_addr req_ip;
707 int fDhcpDiscover = 0;
708 uint8_t *parameter_list = NULL;
709 struct mbuf *m = NULL;
710
711 if (memcmp(bp->bp_vend, rfc1533_cookie, sizeof(rfc1533_cookie)) != 0)
712 return;
713
714 pu8RawDhcpObject = dhcp_find_option(bp->bp_vend, vlen, RFC2132_MSG_TYPE, 1);
715 if (pu8RawDhcpObject == NULL)
716 return;
717 if (pu8RawDhcpObject[1] != 1) /* option length */
718 return;
719
720 /**
721 * We're going update dns list at least once per DHCP transaction (!not on every operation
722 * within transaction), assuming that transaction can't be longer than 1 min.
723 *
724 * @note: if we have notification update (HAVE_NOTIFICATION_FOR_DNS_UPDATE)
725 * provided by host, we don't need implicitly re-initialize dns list.
726 *
727 * @note: NATState::fUseHostResolver became (r89055) the flag signalling that Slirp
728 * wasn't able to fetch fresh host DNS info and fall down to use host-resolver, on one
729 * of the previous attempts to proxy dns requests to Host's name-resolving API
730 *
731 * @note: Checking NATState::fUseHostResolver == true, we want to try restore behaviour initialy
732 * wanted by user ASAP (P here when host serialize its configuration in files parsed by Slirp).
733 */
734 if ( !HAVE_NOTIFICATION_FOR_DNS_UPDATE
735 && !pData->fUseHostResolverPermanent
736 && ( pData->dnsLastUpdate == 0
737 || curtime - pData->dnsLastUpdate > 60 * 1000 /* one minute */
738 || pData->fUseHostResolver))
739 {
740 uint8_t i;
741
742 parameter_list = dhcp_find_option(bp->bp_vend, vlen, RFC2132_PARAM_LIST, -1);
743 for (i = 0; parameter_list && i < parameter_list[1]; ++i)
744 {
745 if (parameter_list[2 + i] == RFC1533_DNS)
746 {
747 /* XXX: How differs it from host Suspend/Resume? */
748 slirpReleaseDnsSettings(pData);
749 slirpInitializeDnsSettings(pData);
750 pData->dnsLastUpdate = curtime;
751 break;
752 }
753 }
754 }
755
756 m = m_getcl(pData, M_DONTWAIT, MT_HEADER, M_PKTHDR);
757 if (!m)
758 {
759 LogRel(("NAT: Can't allocate memory for response!\n"));
760 return;
761 }
762
763 switch (*(pu8RawDhcpObject + 2))
764 {
765 case DHCPDISCOVER:
766 fDhcpDiscover = 1;
767 RT_FALL_THRU();
768 case DHCPINFORM:
769 rc = dhcp_decode_discover(pData, bp, fDhcpDiscover, m);
770 if (rc > 0)
771 goto reply;
772 break;
773
774 case DHCPREQUEST:
775 rc = dhcp_decode_request(pData, bp, vlen, m);
776 if (rc > 0)
777 goto reply;
778 break;
779
780 case DHCPRELEASE:
781 dhcp_decode_release(pData, bp);
782 /* no reply required */
783 break;
784
785 case DHCPDECLINE:
786 pu8RawDhcpObject = dhcp_find_option(bp->bp_vend, vlen,
787 RFC2132_REQ_ADDR, sizeof(struct in_addr));
788 if (pu8RawDhcpObject == NULL)
789 {
790 Log(("NAT: RFC2132_REQ_ADDR not found\n"));
791 break;
792 }
793
794 req_ip.s_addr = *(uint32_t *)(pu8RawDhcpObject + 2);
795 rc = bootp_cache_lookup_ether_by_ip(pData, req_ip.s_addr, NULL);
796 if (RT_FAILURE(rc))
797 {
798 /* Not registered */
799 BOOTPClient *bc;
800 bc = bc_alloc_client(pData);
801 Assert(bc);
802 if (!bc)
803 {
804 LogRel(("NAT: Can't allocate bootp client object\n"));
805 break;
806 }
807 bc->addr.s_addr = req_ip.s_addr;
808 slirp_arp_who_has(pData, bc->addr.s_addr);
809 LogRel(("NAT: %RTnaipv4 has been already registered\n", req_ip));
810 }
811 /* no response required */
812 break;
813
814 default:
815 /* unsupported DHCP message type */
816 break;
817 }
818 /* silently ignore */
819 m_freem(pData, m);
820 return;
821
822reply:
823 bootp_reply(pData, m, rc, bp->bp_flags);
824}
825
826static void bootp_reply(PNATState pData, struct mbuf *m, int offReply, uint16_t flags)
827{
828 struct sockaddr_in saddr, daddr;
829 struct bootp_t *rbp = NULL;
830 uint8_t *q = NULL;
831 int nack;
832 rbp = mtod(m, struct bootp_t *);
833 Assert((m));
834 Assert((rbp));
835 q = rbp->bp_vend;
836 nack = (q[6] == DHCPNAK);
837 q += offReply;
838
839 saddr.sin_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
840
841 FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
842
843 *q++ = RFC1533_END; /* end of message */
844
845 m->m_pkthdr.header = mtod(m, void *);
846 m->m_len = sizeof(struct bootp_t)
847 - sizeof(struct ip)
848 - sizeof(struct udphdr);
849 m->m_data += sizeof(struct udphdr)
850 + sizeof(struct ip);
851 if ( (flags & RT_H2N_U16_C(DHCP_FLAGS_B))
852 || nack != 0)
853 daddr.sin_addr.s_addr = INADDR_BROADCAST;
854 else
855 daddr.sin_addr.s_addr = rbp->bp_yiaddr.s_addr; /*unicast requested by client*/
856 saddr.sin_port = RT_H2N_U16_C(BOOTP_SERVER);
857 daddr.sin_port = RT_H2N_U16_C(BOOTP_CLIENT);
858 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
859}
860
861void bootp_input(PNATState pData, struct mbuf *m)
862{
863 struct bootp_t *bp = mtod(m, struct bootp_t *);
864 u_int mlen = m_length(m, NULL);
865 size_t vlen;
866
867 if (mlen < RT_UOFFSETOF(struct bootp_t, bp_vend) + sizeof(rfc1533_cookie))
868 {
869 LogRelMax(50, ("NAT: ignoring invalid BOOTP request (mlen %u too short)\n", mlen));
870 return;
871 }
872
873 if (bp->bp_op != BOOTP_REQUEST)
874 {
875 LogRelMax(50, ("NAT: ignoring invalid BOOTP request (wrong opcode %u)\n", bp->bp_op));
876 return;
877 }
878
879 if (bp->bp_htype != RTNET_ARP_ETHER)
880 {
881 LogRelMax(50, ("NAT: ignoring invalid BOOTP request (wrong HW type %u)\n", bp->bp_htype));
882 return;
883 }
884
885 if (bp->bp_hlen != ETH_ALEN)
886 {
887 LogRelMax(50, ("NAT: ignoring invalid BOOTP request (wrong HW address length %u)\n", bp->bp_hlen));
888 return;
889 }
890
891 if (bp->bp_hops != 0)
892 {
893 LogRelMax(50, ("NAT: ignoring invalid BOOTP request (wrong hop count %u)\n", bp->bp_hops));
894 return;
895 }
896
897 vlen = mlen - RT_UOFFSETOF(struct bootp_t, bp_vend);
898 dhcp_decode(pData, bp, vlen);
899}
900
901int bootp_cache_lookup_ip_by_ether(PNATState pData,const uint8_t* ether, uint32_t *pip)
902{
903 int i;
904
905 if (!ether || !pip)
906 return VERR_INVALID_PARAMETER;
907
908 for (i = 0; i < NB_ADDR; i++)
909 {
910 if ( bootp_clients[i].allocated
911 && memcmp(bootp_clients[i].macaddr, ether, ETH_ALEN) == 0)
912 {
913 *pip = bootp_clients[i].addr.s_addr;
914 return VINF_SUCCESS;
915 }
916 }
917
918 *pip = INADDR_ANY;
919 return VERR_NOT_FOUND;
920}
921
922int bootp_cache_lookup_ether_by_ip(PNATState pData, uint32_t ip, uint8_t *ether)
923{
924 int i;
925 for (i = 0; i < NB_ADDR; i++)
926 {
927 if ( bootp_clients[i].allocated
928 && ip == bootp_clients[i].addr.s_addr)
929 {
930 if (ether != NULL)
931 memcpy(ether, bootp_clients[i].macaddr, ETH_ALEN);
932 return VINF_SUCCESS;
933 }
934 }
935
936 return VERR_NOT_FOUND;
937}
938
939/*
940 * Initialize dhcp server
941 * @returns 0 - if initialization is ok, non-zero otherwise
942 */
943int bootp_dhcp_init(PNATState pData)
944{
945 pData->pbootp_clients = RTMemAllocZ(sizeof(BOOTPClient) * NB_ADDR);
946 if (!pData->pbootp_clients)
947 return VERR_NO_MEMORY;
948
949 return VINF_SUCCESS;
950}
951
952int bootp_dhcp_fini(PNATState pData)
953{
954 if (pData->pbootp_clients != NULL)
955 RTMemFree(pData->pbootp_clients);
956
957 return VINF_SUCCESS;
958}
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