VirtualBox

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

Last change on this file since 22984 was 22669, checked in by vboxsync, 15 years ago

NAT: warnings.

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