VirtualBox

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

Last change on this file since 39409 was 39409, checked in by vboxsync, 13 years ago

NAT: warnings + NAT/netservice fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.6 KB
Line 
1/* $Id: bootp.c 39409 2011-11-24 15:28:32Z 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. */
46typedef 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 */
60static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
61
62static void bootp_reply(PNATState pData, struct mbuf *m0, int offReply, uint16_t flags);
63
64static 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
83static 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
102static 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
114static 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 */
176static 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
195static 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
230static 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 /*
250 * we're care in built-in tftp server about existence/validness of the boot file.
251 */
252 if (bootp_filename)
253 RTStrPrintf((char*)rbp->bp_file, sizeof(rbp->bp_file), "%s", bootp_filename);
254
255 Log(("NAT: DHCP: bp_file:%s\n", &rbp->bp_file));
256 /* Address/port of the DHCP server. */
257 rbp->bp_yiaddr = bc->addr; /* Client IP address */
258 Log(("NAT: DHCP: bp_yiaddr:%RTnaipv4\n", rbp->bp_yiaddr));
259 rbp->bp_siaddr = pData->tftp_server; /* Next Server IP address, i.e. TFTP */
260 Log(("NAT: DHCP: bp_siaddr:%RTnaipv4\n", rbp->bp_siaddr));
261 if (fDhcpRequest)
262 {
263 rbp->bp_ciaddr.s_addr = bc->addr.s_addr; /* Client IP address */
264 }
265 saddr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
266 Log(("NAT: DHCP: s_addr:%RTnaipv4\n", saddr));
267
268#define FILL_BOOTP_EXT(q, tag, len, pvalue) \
269 do { \
270 struct bootp_ext *be = (struct bootp_ext *)(q); \
271 be->bpe_tag = (tag); \
272 be->bpe_len = (len); \
273 memcpy(&be[1], (pvalue), (len)); \
274 (q) = (uint8_t *)(&be[1]) + (len); \
275 }while(0)
276/* appending another value to tag, calculates len of whole block*/
277#define FILL_BOOTP_APP(head, q, tag, len, pvalue) \
278 do { \
279 struct bootp_ext *be = (struct bootp_ext *)(head); \
280 memcpy(q, (pvalue), (len)); \
281 (q) += (len); \
282 Assert(be->bpe_tag == (tag)); \
283 be->bpe_len += (len); \
284 }while(0)
285
286
287 FILL_BOOTP_EXT(q, RFC1533_NETMASK, 4, &netmask);
288 FILL_BOOTP_EXT(q, RFC1533_GATEWAY, 4, &saddr);
289
290 if (pData->fUseDnsProxy || pData->fUseHostResolver)
291 {
292 uint32_t addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_DNS);
293 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &addr);
294 goto skip_dns_servers;
295 }
296
297 if (!TAILQ_EMPTY(&pData->pDnsList))
298 {
299 de = TAILQ_LAST(&pData->pDnsList, dns_list_head);
300 q_dns_header = q;
301 FILL_BOOTP_EXT(q, RFC1533_DNS, 4, &de->de_addr.s_addr);
302 }
303
304 TAILQ_FOREACH_REVERSE(de, &pData->pDnsList, dns_list_head, de_list)
305 {
306 if (TAILQ_LAST(&pData->pDnsList, dns_list_head) == de)
307 continue; /* first value with head we've ingected before */
308 FILL_BOOTP_APP(q_dns_header, q, RFC1533_DNS, 4, &de->de_addr.s_addr);
309 }
310
311skip_dns_servers:
312 if (pData->fPassDomain && !pData->fUseHostResolver)
313 {
314 LIST_FOREACH(dd, &pData->pDomainList, dd_list)
315 {
316
317 if (dd->dd_pszDomain == NULL)
318 continue;
319 /* never meet valid separator here in RFC1533*/
320 if (added != 0)
321 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, 1, ",");
322 else
323 added = 1;
324 val = (int)strlen(dd->dd_pszDomain);
325 FILL_BOOTP_EXT(q, RFC1533_DOMAINNAME, val, dd->dd_pszDomain);
326 }
327 }
328
329 FILL_BOOTP_EXT(q, RFC2132_LEASE_TIME, 4, &lease_time);
330
331 if (*slirp_hostname)
332 {
333 val = (int)strlen(slirp_hostname);
334 FILL_BOOTP_EXT(q, RFC1533_HOSTNAME, val, slirp_hostname);
335 }
336 slirp_arp_cache_update_or_add(pData, rbp->bp_yiaddr.s_addr, bc->macaddr);
337 return q - rbp->bp_vend; /*return offset */
338}
339
340static int dhcp_send_nack(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m)
341{
342 struct bootp_t *rbp;
343 rbp = mtod(m, struct bootp_t *);
344 NOREF(bc);
345
346 dhcp_create_msg(pData, bp, m, DHCPNAK);
347 return 7;
348}
349
350static int dhcp_send_ack(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m, int fDhcpRequest)
351{
352 int offReply = 0; /* boot_reply will fill general options and add END before sending response */
353
354 dhcp_create_msg(pData, bp, m, DHCPACK);
355 offReply = dhcp_do_ack_offer(pData, m, bc, fDhcpRequest);
356 return offReply;
357}
358
359static int dhcp_send_offer(PNATState pData, struct bootp_t *bp, BOOTPClient *bc, struct mbuf *m)
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, DHCPOFFER);
364 offReply = dhcp_do_ack_offer(pData, m, bc, /* fDhcpRequest=*/ 0);
365 return offReply;
366}
367
368/**
369 * decoding client messages RFC2131 (4.3.6)
370 * ---------------------------------------------------------------------
371 * | |INIT-REBOOT |SELECTING |RENEWING |REBINDING |
372 * ---------------------------------------------------------------------
373 * |broad/unicast |broadcast |broadcast |unicast |broadcast |
374 * |server-ip |MUST NOT |MUST |MUST NOT |MUST NOT |
375 * |requested-ip |MUST |MUST |MUST NOT |MUST NOT |
376 * |ciaddr |zero |zero |IP address |IP address|
377 * ---------------------------------------------------------------------
378 *
379 */
380
381enum DHCP_REQUEST_STATES
382{
383 INIT_REBOOT,
384 SELECTING,
385 RENEWING,
386 REBINDING,
387 NONE
388};
389
390static int dhcp_decode_request(PNATState pData, struct bootp_t *bp, struct mbuf *m)
391{
392 BOOTPClient *bc = NULL;
393 struct in_addr daddr;
394 int offReply;
395 uint8_t *req_ip = NULL;
396 uint8_t *server_ip = NULL;
397 uint32_t ui32;
398 enum DHCP_REQUEST_STATES dhcp_stat = NONE;
399
400 /* need to understand which type of request we get */
401 req_ip = dhcp_find_option(&bp->bp_vend[0], RFC2132_REQ_ADDR);
402 server_ip = dhcp_find_option(&bp->bp_vend[0], RFC2132_SRV_ID);
403 bc = find_addr(pData, &daddr, bp->bp_hwaddr);
404
405 if (server_ip != NULL)
406 {
407 /* selecting */
408 if (!bc)
409 {
410 LogRel(("NAT: DHCP no IP was allocated\n"));
411 return -1;
412 }
413
414 dhcp_stat = SELECTING;
415 Assert((bp->bp_ciaddr.s_addr == INADDR_ANY));
416 Assert((*(uint32_t *)(req_ip + 2) == bc->addr.s_addr)); /*the same address as in offer*/
417#if 0
418 /* DSL xid in request differ from offer */
419 Assert((bp->bp_xid == bc->xid));
420#endif
421 }
422 else
423 {
424 if (req_ip != NULL)
425 {
426 /* init-reboot */
427 dhcp_stat = INIT_REBOOT;
428 }
429 else
430 {
431 /* table 4 of rfc2131 */
432 if (bp->bp_flags & RT_H2N_U16_C(DHCP_FLAGS_B))
433 dhcp_stat = REBINDING;
434 else
435 dhcp_stat = RENEWING;
436 }
437 }
438
439 /*?? renewing ??*/
440 switch (dhcp_stat)
441 {
442 case RENEWING:
443 Assert((server_ip == NULL && req_ip == NULL && bp->bp_ciaddr.s_addr != INADDR_ANY));
444 if (bc != NULL)
445 {
446 Assert((bc->addr.s_addr == bp->bp_ciaddr.s_addr));
447 /*if it already here well just do ack, we aren't aware of dhcp time expiration*/
448 }
449 else
450 {
451 if ((bp->bp_ciaddr.s_addr & RT_H2N_U32(pData->netmask)) != pData->special_addr.s_addr)
452 {
453 LogRel(("NAT: Client %RTnaipv4 requested IP -- sending NAK\n", bp->bp_ciaddr));
454 offReply = dhcp_send_nack(pData, bp, bc, m);
455 return offReply;
456 }
457
458 bc = bc_alloc_client(pData);
459 if (!bc)
460 {
461 LogRel(("NAT: can't alloc address. RENEW has been silently ignored.\n"));
462 return -1;
463 }
464
465 Assert((bp->bp_hlen == ETH_ALEN));
466 memcpy(bc->macaddr, bp->bp_hwaddr, bp->bp_hlen);
467 bc->addr.s_addr = bp->bp_ciaddr.s_addr;
468 }
469 break;
470
471 case INIT_REBOOT:
472 Assert(server_ip == NULL);
473 Assert(req_ip != NULL);
474 ui32 = *(uint32_t *)(req_ip + 2);
475 if ((ui32 & RT_H2N_U32(pData->netmask)) != pData->special_addr.s_addr)
476 {
477 LogRel(("NAT: address %RTnaipv4 has been requested -- sending NAK\n", ui32));
478 offReply = dhcp_send_nack(pData, bp, bc, m);
479 return offReply;
480 }
481
482 bc = bc_alloc_client(pData);
483 if (!bc)
484 {
485 LogRel(("NAT: can't alloc address. RENEW has been silently ignored\n"));
486 return -1;
487 }
488 Assert((bp->bp_hlen == ETH_ALEN));
489 memcpy(bc->macaddr, bp->bp_hwaddr, bp->bp_hlen);
490 bc->addr.s_addr = ui32;
491 break;
492
493 case NONE:
494 Assert((dhcp_stat != NONE));
495 return -1;
496
497 default:
498 break;
499 }
500
501 LogRel(("NAT: DHCP offered IP address %RTnaipv4\n", bc->addr));
502 offReply = dhcp_send_ack(pData, bp, bc, m, /* fDhcpRequest=*/ 1);
503 return offReply;
504}
505
506static int dhcp_decode_discover(PNATState pData, struct bootp_t *bp, int fDhcpDiscover, struct mbuf *m)
507{
508 BOOTPClient *bc;
509 struct in_addr daddr;
510 int offReply;
511
512 if (fDhcpDiscover)
513 {
514 bc = find_addr(pData, &daddr, bp->bp_hwaddr);
515 if (!bc)
516 {
517 bc = get_new_addr(pData, &daddr);
518 if (!bc)
519 {
520 LogRel(("NAT: DHCP no IP address left\n"));
521 Log(("no address left\n"));
522 return -1;
523 }
524 memcpy(bc->macaddr, bp->bp_hwaddr, 6);
525 }
526
527 bc->xid = bp->bp_xid;
528 LogRel(("NAT: DHCP offered IP address %RTnaipv4\n", bc->addr));
529 offReply = dhcp_send_offer(pData, bp, bc, m);
530 return offReply;
531 }
532 else
533 {
534 bc = find_addr(pData, &daddr, bp->bp_hwaddr);
535 if (!bc)
536 {
537 LogRel(("NAT: DHCP Inform was ignored no boot client was found\n"));
538 return -1;
539 }
540
541 LogRel(("NAT: DHCP offered IP address %RTnaipv4\n", bc->addr));
542 offReply = dhcp_send_ack(pData, bp, bc, m, /* fDhcpRequest=*/ 0);
543 return offReply;
544 }
545
546 return -1;
547}
548
549static int dhcp_decode_release(PNATState pData, struct bootp_t *bp)
550{
551 int rc = release_addr(pData, &bp->bp_ciaddr);
552 LogRel(("NAT: %s %RTnaipv4\n",
553 RT_SUCCESS(rc) ? "DHCP released IP address" : "Ignored DHCP release for IP address",
554 &bp->bp_ciaddr));
555 return 0;
556}
557/**
558 * fields for discovering t
559 * Field DHCPDISCOVER DHCPREQUEST DHCPDECLINE,
560 * DHCPINFORM DHCPRELEASE
561 * ----- ------------ ----------- -----------
562 * 'op' BOOTREQUEST BOOTREQUEST BOOTREQUEST
563 * 'htype' (From "Assigned Numbers" RFC)
564 * 'hlen' (Hardware address length in octets)
565 * 'hops' 0 0 0
566 * 'xid' selected by client 'xid' from server selected by
567 * DHCPOFFER message client
568 * 'secs' 0 or seconds since 0 or seconds since 0
569 * DHCP process started DHCP process started
570 * 'flags' Set 'BROADCAST' Set 'BROADCAST' 0
571 * flag if client flag if client
572 * requires broadcast requires broadcast
573 * reply reply
574 * 'ciaddr' 0 (DHCPDISCOVER) 0 or client's 0 (DHCPDECLINE)
575 * client's network address client's network
576 * network address (BOUND/RENEW/REBIND) address
577 * (DHCPINFORM) (DHCPRELEASE)
578 * 'yiaddr' 0 0 0
579 * 'siaddr' 0 0 0
580 * 'giaddr' 0 0 0
581 * 'chaddr' client's hardware client's hardware client's hardware
582 * address address address
583 * 'sname' options, if options, if (unused)
584 * indicated in indicated in
585 * 'sname/file' 'sname/file'
586 * option; otherwise option; otherwise
587 * unused unused
588 * 'file' options, if options, if (unused)
589 * indicated in indicated in
590 * 'sname/file' 'sname/file'
591 * option; otherwise option; otherwise
592 * unused unused
593 * 'options' options options (unused)
594 * Requested IP address MAY MUST (in MUST
595 * (DISCOVER) SELECTING or (DHCPDECLINE),
596 * MUST NOT INIT-REBOOT) MUST NOT
597 * (INFORM) MUST NOT (in (DHCPRELEASE)
598 * BOUND or
599 * RENEWING)
600 * IP address lease time MAY MAY MUST NOT
601 * (DISCOVER)
602 * MUST NOT
603 * (INFORM)
604 * Use 'file'/'sname' fields MAY MAY MAY
605 * DHCP message type DHCPDISCOVER/ DHCPREQUEST DHCPDECLINE/
606 * DHCPINFORM DHCPRELEASE
607 * Client identifier MAY MAY MAY
608 * Vendor class identifier MAY MAY MUST NOT
609 * Server identifier MUST NOT MUST (after MUST
610 * SELECTING)
611 * MUST NOT (after
612 * INIT-REBOOT,
613 * BOUND, RENEWING
614 * or REBINDING)
615 * Parameter request list MAY MAY MUST NOT
616 * Maximum message size MAY MAY MUST NOT
617 * Message SHOULD NOT SHOULD NOT SHOULD
618 * Site-specific MAY MAY MUST NOT
619 * All others MAY MAY MUST NOT
620 *
621 */
622static void dhcp_decode(PNATState pData, struct bootp_t *bp, const uint8_t *buf, int size)
623{
624 const uint8_t *p, *p_end;
625 int rc;
626 int pmsg_type;
627 struct in_addr req_ip;
628 int fDhcpDiscover = 0;
629 uint8_t *parameter_list = NULL;
630 struct mbuf *m = NULL;
631
632 pmsg_type = 0;
633 p = buf;
634 p_end = buf + size;
635 if (size < 5)
636 return;
637
638 if (memcmp(p, rfc1533_cookie, 4) != 0)
639 return;
640
641 p = dhcp_find_option(bp->bp_vend, RFC2132_MSG_TYPE);
642 Assert(p);
643 if (!p)
644 return;
645 /*
646 * We're going update dns list at least once per DHCP transaction (!not on every operation
647 * within transaction), assuming that transaction can't be longer than 1 min.
648 */
649 if ( !pData->fUseHostResolver
650 && ( pData->dnsLastUpdate == 0
651 || curtime - pData->dnsLastUpdate > 60 * 1000)) /* one minute*/
652 {
653 uint8_t i = 2; /* i = 0 - tag, i == 1 - length */
654 parameter_list = dhcp_find_option(&bp->bp_vend[0], RFC2132_PARAM_LIST);
655 for (;parameter_list && i < parameter_list[1]; ++i)
656 {
657 if (parameter_list[i] == RFC1533_DNS)
658 {
659 slirp_release_dns_list(pData);
660 slirp_init_dns_list(pData);
661 pData->dnsLastUpdate = curtime;
662 break;
663 }
664 }
665 }
666
667 m = m_getcl(pData, M_DONTWAIT, MT_HEADER, M_PKTHDR);
668 if (!m)
669 {
670 LogRel(("NAT: can't alocate memory for response!\n"));
671 return;
672 }
673
674 switch (*(p+2))
675 {
676 case DHCPDISCOVER:
677 fDhcpDiscover = 1;
678 /* fall through */
679 case DHCPINFORM:
680 rc = dhcp_decode_discover(pData, bp, fDhcpDiscover, m);
681 if (rc > 0)
682 goto reply;
683 break;
684
685 case DHCPREQUEST:
686 rc = dhcp_decode_request(pData, bp, m);
687 if (rc > 0)
688 goto reply;
689 break;
690
691 case DHCPRELEASE:
692 rc = dhcp_decode_release(pData, bp);
693 /* no reply required */
694 break;
695
696 case DHCPDECLINE:
697 p = dhcp_find_option(&bp->bp_vend[0], RFC2132_REQ_ADDR);
698 req_ip.s_addr = *(uint32_t *)(p + 2);
699 rc = bootp_cache_lookup_ether_by_ip(pData, req_ip.s_addr, NULL);
700 if (RT_FAILURE(rc))
701 {
702 /* Not registered */
703 BOOTPClient *bc;
704 bc = bc_alloc_client(pData);
705 Assert(bc);
706 bc->addr.s_addr = req_ip.s_addr;
707 slirp_arp_who_has(pData, bc->addr.s_addr);
708 LogRel(("NAT: %RTnaipv4 has been already registered\n", req_ip));
709 }
710 /* no response required */
711 break;
712
713 default:
714 AssertMsgFailed(("unsupported DHCP message type"));
715 }
716 /* silently ignore */
717 m_freem(pData, m);
718 return;
719
720reply:
721 bootp_reply(pData, m, rc, bp->bp_flags);
722}
723
724static void bootp_reply(PNATState pData, struct mbuf *m, int offReply, uint16_t flags)
725{
726 struct sockaddr_in saddr, daddr;
727 struct bootp_t *rbp = NULL;
728 uint8_t *q = NULL;
729 int nack;
730 rbp = mtod(m, struct bootp_t *);
731 Assert((m));
732 Assert((rbp));
733 q = rbp->bp_vend;
734 nack = (q[6] == DHCPNAK);
735 q += offReply;
736
737 saddr.sin_addr.s_addr = RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_ALIAS);
738
739 FILL_BOOTP_EXT(q, RFC2132_SRV_ID, 4, &saddr.sin_addr);
740
741 *q++ = RFC1533_END; /* end of message */
742
743 m->m_pkthdr.header = mtod(m, void *);
744 m->m_len = sizeof(struct bootp_t)
745 - sizeof(struct ip)
746 - sizeof(struct udphdr);
747 m->m_data += sizeof(struct udphdr)
748 + sizeof(struct ip);
749 if ( (flags & RT_H2N_U16_C(DHCP_FLAGS_B))
750 || nack != 0)
751 daddr.sin_addr.s_addr = INADDR_BROADCAST;
752 else
753 daddr.sin_addr.s_addr = rbp->bp_yiaddr.s_addr; /*unicast requested by client*/
754 saddr.sin_port = RT_H2N_U16_C(BOOTP_SERVER);
755 daddr.sin_port = RT_H2N_U16_C(BOOTP_CLIENT);
756 udp_output2(pData, NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
757}
758
759void bootp_input(PNATState pData, struct mbuf *m)
760{
761 struct bootp_t *bp = mtod(m, struct bootp_t *);
762
763 if (bp->bp_op == BOOTP_REQUEST)
764 dhcp_decode(pData, bp, bp->bp_vend, DHCP_OPT_LEN);
765}
766
767int bootp_cache_lookup_ip_by_ether(PNATState pData,const uint8_t* ether, uint32_t *pip)
768{
769 int i;
770
771 if (!ether || !pip)
772 return VERR_INVALID_PARAMETER;
773
774 for (i = 0; i < NB_ADDR; i++)
775 {
776 if ( bootp_clients[i].allocated
777 && memcmp(bootp_clients[i].macaddr, ether, ETH_ALEN) == 0)
778 {
779 *pip = bootp_clients[i].addr.s_addr;
780 return VINF_SUCCESS;
781 }
782 }
783
784 *pip = INADDR_ANY;
785 return VERR_NOT_FOUND;
786}
787
788int bootp_cache_lookup_ether_by_ip(PNATState pData, uint32_t ip, uint8_t *ether)
789{
790 int i;
791 for (i = 0; i < NB_ADDR; i++)
792 {
793 if ( bootp_clients[i].allocated
794 && ip == bootp_clients[i].addr.s_addr)
795 {
796 if (ether != NULL)
797 memcpy(ether, bootp_clients[i].macaddr, ETH_ALEN);
798 return VINF_SUCCESS;
799 }
800 }
801
802 return VERR_NOT_FOUND;
803}
804
805/*
806 * Initialize dhcp server
807 * @returns 0 - if initialization is ok, non-zero otherwise
808 */
809int bootp_dhcp_init(PNATState pData)
810{
811 pData->pbootp_clients = RTMemAllocZ(sizeof(BOOTPClient) * NB_ADDR);
812 if (!pData->pbootp_clients)
813 return VERR_NO_MEMORY;
814
815 return VINF_SUCCESS;
816}
817
818int bootp_dhcp_fini(PNATState pData)
819{
820 if (pData->pbootp_clients != NULL)
821 RTMemFree(pData->pbootp_clients);
822
823 return VINF_SUCCESS;
824}
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