VirtualBox

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

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

NAT: unify slirp_init() and adapt the coding style of some slirp_state variables

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