VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/pxdns.c

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.0 KB
Line 
1/* $Id: pxdns.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * NAT Network - DNS proxy.
4 */
5
6/*
7 * Copyright (C) 2009-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/*
29 * Copyright (c) 2003,2004,2005 Armin Wolfermann
30 *
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
37 *
38 * The above copyright notice and this permission notice shall be included in
39 * all copies or substantial portions of the Software.
40 *
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
44 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47 * DEALINGS IN THE SOFTWARE.
48 */
49#define LOG_GROUP LOG_GROUP_NAT_SERVICE
50
51#include "winutils.h"
52
53#include "proxy.h"
54#include "proxy_pollmgr.h"
55#include "pxtcp.h"
56
57#include "lwip/sys.h"
58#include "lwip/tcpip.h"
59#include "lwip/ip_addr.h"
60#include "lwip/udp.h"
61#include "lwip/tcp.h"
62
63#ifndef RT_OS_WINDOWS
64#include <sys/poll.h>
65#include <sys/socket.h>
66#include <netinet/in.h>
67#include <netdb.h>
68#else
69#include "winpoll.h"
70#endif
71
72#include <stdio.h>
73#include <string.h>
74
75
76union sockaddr_inet {
77 struct sockaddr sa;
78 struct sockaddr_in sin;
79 struct sockaddr_in6 sin6;
80};
81
82
83struct request;
84
85
86/**
87 * DNS Proxy
88 */
89struct pxdns {
90 SOCKET sock4;
91 SOCKET sock6;
92
93 struct pollmgr_handler pmhdl4;
94 struct pollmgr_handler pmhdl6;
95
96 struct udp_pcb *pcb4;
97 struct udp_pcb *pcb6;
98
99 struct tcp_pcb *ltcp;
100
101 size_t generation;
102 size_t nresolvers;
103 union sockaddr_inet *resolvers;
104
105 u16_t id;
106
107 sys_mutex_t lock;
108
109 size_t active_queries;
110 size_t expired_queries;
111 size_t late_answers;
112 size_t hash_collisions;
113
114#define TIMEOUT 5
115 size_t timeout_slot;
116 u32_t timeout_mask;
117 struct request *timeout_list[TIMEOUT];
118
119#define HASHSIZE 10
120#define HASH(id) ((id) & ((1 << HASHSIZE) - 1))
121 struct request *request_hash[1 << HASHSIZE];
122} g_pxdns;
123
124
125struct request {
126 /**
127 * Request ID that we use in relayed request.
128 */
129 u16_t id;
130
131 /**
132 * pxdns::generation used for this request
133 */
134 size_t generation;
135
136 /**
137 * Current index into pxdns::resolvers
138 */
139 size_t residx;
140
141 /**
142 * PCB from which we have received this request. lwIP doesn't
143 * support listening for both IPv4 and IPv6 on the same pcb, so we
144 * use two and need to keep track.
145 */
146 struct udp_pcb *pcb;
147
148 /**
149 * Client this request is from and its original request ID.
150 */
151 ipX_addr_t client_addr;
152 u16_t client_port;
153 u16_t client_id;
154
155 /**
156 * Chaining for pxdns::request_hash
157 */
158 struct request **pprev_hash;
159 struct request *next_hash;
160
161 /**
162 * Chaining for pxdns::timeout_list
163 */
164 struct request **pprev_timeout;
165 struct request *next_timeout;
166
167 /**
168 * Slot in pxdns::timeout_list
169 */
170 size_t timeout_slot;
171
172 /**
173 * Pbuf with reply received on pollmgr thread.
174 */
175 struct pbuf *reply;
176
177 /**
178 * Preallocated lwIP message to send reply from the lwIP thread.
179 */
180 struct tcpip_msg msg_reply;
181
182 /**
183 * Client request. ID is replaced with ours, original saved in
184 * client_id. Use a copy since we might need to resend and we
185 * don't want to hold onto pbuf of the request.
186 */
187 size_t size;
188 u8_t data[1];
189};
190
191
192static void pxdns_create_resolver_sockaddrs(struct pxdns *pxdns,
193 const char **nameservers);
194
195static err_t pxdns_accept_syn(void *arg, struct tcp_pcb *newpcb, struct pbuf *syn);
196
197static void pxdns_recv4(void *arg, struct udp_pcb *pcb, struct pbuf *p,
198 ip_addr_t *addr, u16_t port);
199static void pxdns_recv6(void *arg, struct udp_pcb *pcb, struct pbuf *p,
200 ip6_addr_t *addr, u16_t port);
201static void pxdns_query(struct pxdns *pxdns, struct udp_pcb *pcb, struct pbuf *p,
202 ipX_addr_t *addr, u16_t port);
203static void pxdns_timer(void *arg);
204static int pxdns_rexmit(struct pxdns *pxdns, struct request *req);
205static int pxdns_forward_outbound(struct pxdns *pxdns, struct request *req);
206
207static int pxdns_pmgr_pump(struct pollmgr_handler *handler, SOCKET fd, int revents);
208static void pxdns_pcb_reply(void *ctx);
209
210static void pxdns_request_register(struct pxdns *pxdns, struct request *req);
211static void pxdns_request_deregister(struct pxdns *pxdns, struct request *req);
212static struct request *pxdns_request_find(struct pxdns *pxdns, u16_t id);
213
214static void pxdns_hash_add(struct pxdns *pxdns, struct request *req);
215static void pxdns_hash_del(struct pxdns *pxdns, struct request *req);
216static void pxdns_timeout_add(struct pxdns *pxdns, struct request *req);
217static void pxdns_timeout_del(struct pxdns *pxdns, struct request *req);
218
219static void pxdns_request_free(struct request *req);
220
221
222err_t
223pxdns_init(struct netif *proxy_netif)
224{
225 struct pxdns *pxdns = &g_pxdns;
226 err_t error;
227
228 LWIP_UNUSED_ARG(proxy_netif);
229
230 pxdns->ltcp = tcp_new();
231 if (pxdns->ltcp != NULL) {
232 tcp_bind_ip6(pxdns->ltcp, IP6_ADDR_ANY, 53);
233 pxdns->ltcp = tcp_listen_dual(pxdns->ltcp);
234 if (pxdns->ltcp != NULL) {
235 tcp_arg(pxdns->ltcp, pxdns);
236 tcp_accept_syn(pxdns->ltcp, pxdns_accept_syn);
237 }
238 }
239
240 pxdns->pmhdl4.callback = pxdns_pmgr_pump;
241 pxdns->pmhdl4.data = (void *)pxdns;
242 pxdns->pmhdl4.slot = -1;
243
244 pxdns->pmhdl6.callback = pxdns_pmgr_pump;
245 pxdns->pmhdl6.data = (void *)pxdns;
246 pxdns->pmhdl6.slot = -1;
247
248 pxdns->pcb4 = udp_new();
249 if (pxdns->pcb4 == NULL) {
250 error = ERR_MEM;
251 goto err_cleanup_pcb;
252 }
253
254 pxdns->pcb6 = udp_new_ip6();
255 if (pxdns->pcb6 == NULL) {
256 error = ERR_MEM;
257 goto err_cleanup_pcb;
258 }
259
260 error = udp_bind(pxdns->pcb4, IP_ADDR_ANY, 53);
261 if (error != ERR_OK) {
262 goto err_cleanup_pcb;
263 }
264
265 error = udp_bind_ip6(pxdns->pcb6, IP6_ADDR_ANY, 53);
266 if (error != ERR_OK) {
267 goto err_cleanup_pcb;
268 }
269
270 udp_recv(pxdns->pcb4, pxdns_recv4, pxdns);
271 udp_recv_ip6(pxdns->pcb6, pxdns_recv6, pxdns);
272
273 pxdns->sock4 = socket(AF_INET, SOCK_DGRAM, 0);
274 if (pxdns->sock4 == INVALID_SOCKET) {
275 goto err_cleanup_pcb;
276 }
277
278 pxdns->sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
279 if (pxdns->sock6 == INVALID_SOCKET) {
280 /* it's ok if the host doesn't support IPv6 */
281 /* XXX: TODO: log */
282 }
283
284 pxdns->generation = 0;
285 pxdns->nresolvers = 0;
286 pxdns->resolvers = NULL;
287 pxdns_create_resolver_sockaddrs(pxdns, g_proxy_options->nameservers);
288
289 sys_mutex_new(&pxdns->lock);
290
291 pxdns->timeout_slot = 0;
292 pxdns->timeout_mask = 0;
293
294 /* NB: assumes pollmgr thread is not running yet */
295 pollmgr_add(&pxdns->pmhdl4, pxdns->sock4, POLLIN);
296 if (pxdns->sock6 != INVALID_SOCKET) {
297 pollmgr_add(&pxdns->pmhdl6, pxdns->sock6, POLLIN);
298 }
299
300 return ERR_OK;
301
302 err_cleanup_pcb:
303 if (pxdns->pcb4 != NULL) {
304 udp_remove(pxdns->pcb4);
305 pxdns->pcb4 = NULL;
306 }
307 if (pxdns->pcb6 != NULL) {
308 udp_remove(pxdns->pcb6);
309 pxdns->pcb4 = NULL;
310 }
311
312 return error;
313}
314
315
316/**
317 * lwIP thread callback to set the new list of nameservers.
318 */
319void
320pxdns_set_nameservers(void *arg)
321{
322 const char **nameservers = (const char **)arg;
323
324 if (g_proxy_options->nameservers != NULL) {
325 RTMemFree((void *)g_proxy_options->nameservers);
326 }
327 g_proxy_options->nameservers = nameservers;
328
329 pxdns_create_resolver_sockaddrs(&g_pxdns, nameservers);
330}
331
332
333/**
334 * Use this list of nameservers to resolve guest requests.
335 *
336 * Runs on lwIP thread, so no new queries or retramsmits compete with
337 * it for the use of the existing list of resolvers (to be replaced).
338 */
339static void
340pxdns_create_resolver_sockaddrs(struct pxdns *pxdns, const char **nameservers)
341{
342 struct addrinfo hints;
343 union sockaddr_inet *resolvers;
344 size_t nnames, nresolvers;
345 const char **p;
346 int status;
347
348 resolvers = NULL;
349 nresolvers = 0;
350
351 if (nameservers == NULL) {
352 goto update_resolvers;
353 }
354
355 nnames = 0;
356 for (p = nameservers; *p != NULL; ++p) {
357 ++nnames;
358 }
359
360 if (nnames == 0) {
361 goto update_resolvers;
362 }
363
364 resolvers = (union sockaddr_inet *)calloc(nnames, sizeof(resolvers[0]));
365 if (resolvers == NULL) {
366 nresolvers = 0;
367 goto update_resolvers;
368 }
369
370 memset(&hints, 0, sizeof(hints));
371 hints.ai_family = AF_UNSPEC;
372 hints.ai_socktype = SOCK_DGRAM;
373 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
374
375 for (p = nameservers; *p != NULL; ++p) {
376 const char *name = *p;
377 struct addrinfo *ai;
378 status = getaddrinfo(name, /* "domain" */ "53", &hints, &ai);
379 if (status != 0) {
380 /* XXX: log failed resolution */
381 continue;
382 }
383
384 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
385 /* XXX: log unsupported address family */
386 freeaddrinfo(ai);
387 continue;
388 }
389
390 if (ai->ai_addrlen > sizeof(resolvers[nresolvers])) {
391 /* XXX: log */
392 freeaddrinfo(ai);
393 continue;
394 }
395
396 if (ai->ai_family == AF_INET6 && pxdns->sock6 == INVALID_SOCKET) {
397 /* no IPv6 support on the host, can't use this resolver */
398 freeaddrinfo(ai);
399 continue;
400 }
401
402 memcpy(&resolvers[nresolvers], ai->ai_addr, ai->ai_addrlen);
403 freeaddrinfo(ai);
404 ++nresolvers;
405 }
406
407 if (nresolvers == 0) {
408 if (resolvers != NULL) {
409 free(resolvers);
410 }
411 resolvers = NULL;
412 }
413
414 update_resolvers:
415 ++pxdns->generation;
416 if (pxdns->resolvers != NULL) {
417 free(pxdns->resolvers);
418 }
419 pxdns->resolvers = resolvers;
420 pxdns->nresolvers = nresolvers;
421}
422
423
424static void
425pxdns_request_free(struct request *req)
426{
427 LWIP_ASSERT1(req->pprev_hash == NULL);
428 LWIP_ASSERT1(req->pprev_timeout == NULL);
429
430 if (req->reply != NULL) {
431 pbuf_free(req->reply);
432 }
433 free(req);
434}
435
436
437static void
438pxdns_hash_add(struct pxdns *pxdns, struct request *req)
439{
440 struct request **chain;
441
442 LWIP_ASSERT1(req->pprev_hash == NULL);
443 ++pxdns->active_queries;
444
445 chain = &pxdns->request_hash[HASH(req->id)];
446 if ((req->next_hash = *chain) != NULL) {
447 (*chain)->pprev_hash = &req->next_hash;
448 ++pxdns->hash_collisions;
449 }
450 *chain = req;
451 req->pprev_hash = chain;
452}
453
454
455static void
456pxdns_timeout_add(struct pxdns *pxdns, struct request *req)
457{
458 struct request **chain;
459 u32_t omask;
460
461 LWIP_ASSERT1(req->pprev_timeout == NULL);
462
463 req->timeout_slot = pxdns->timeout_slot;
464 chain = &pxdns->timeout_list[req->timeout_slot];
465 if ((req->next_timeout = *chain) != NULL) {
466 (*chain)->pprev_timeout = &req->next_timeout;
467 }
468 *chain = req;
469 req->pprev_timeout = chain;
470
471 omask = pxdns->timeout_mask;
472 pxdns->timeout_mask |= 1U << req->timeout_slot;
473 if (omask == 0) {
474 sys_untimeout(pxdns_timer, pxdns);
475 sys_timeout(1 * 1000, pxdns_timer, pxdns);
476 }
477}
478
479
480static void
481pxdns_hash_del(struct pxdns *pxdns, struct request *req)
482{
483 LWIP_ASSERT1(req->pprev_hash != NULL);
484 --pxdns->active_queries;
485
486 if (req->next_hash != NULL) {
487 req->next_hash->pprev_hash = req->pprev_hash;
488 }
489 *req->pprev_hash = req->next_hash;
490 req->pprev_hash = NULL;
491 req->next_hash = NULL;
492}
493
494
495static void
496pxdns_timeout_del(struct pxdns *pxdns, struct request *req)
497{
498 LWIP_ASSERT1(req->pprev_timeout != NULL);
499 LWIP_ASSERT1(req->timeout_slot < TIMEOUT);
500
501 if (req->next_timeout != NULL) {
502 req->next_timeout->pprev_timeout = req->pprev_timeout;
503 }
504 *req->pprev_timeout = req->next_timeout;
505 req->pprev_timeout = NULL;
506 req->next_timeout = NULL;
507
508 if (pxdns->timeout_list[req->timeout_slot] == NULL) {
509 pxdns->timeout_mask &= ~(1U << req->timeout_slot);
510 /* may be on pollmgr thread so no sys_untimeout */
511 }
512}
513
514
515
516/**
517 * Do bookkeeping on new request. Called from pxdns_query().
518 */
519static void
520pxdns_request_register(struct pxdns *pxdns, struct request *req)
521{
522 sys_mutex_lock(&pxdns->lock);
523
524 pxdns_hash_add(pxdns, req);
525 pxdns_timeout_add(pxdns, req);
526
527 sys_mutex_unlock(&pxdns->lock);
528}
529
530
531static void
532pxdns_request_deregister(struct pxdns *pxdns, struct request *req)
533{
534 sys_mutex_lock(&pxdns->lock);
535
536 pxdns_hash_del(pxdns, req);
537 pxdns_timeout_del(pxdns, req);
538
539 sys_mutex_unlock(&pxdns->lock);
540}
541
542
543/**
544 * Find request by the id we used when relaying it and remove it from
545 * id hash and timeout list. Called from pxdns_pmgr_pump() when reply
546 * comes.
547 */
548static struct request *
549pxdns_request_find(struct pxdns *pxdns, u16_t id)
550{
551 struct request *req = NULL;
552
553 sys_mutex_lock(&pxdns->lock);
554
555 /* find request in the id->req hash */
556 for (req = pxdns->request_hash[HASH(id)]; req != NULL; req = req->next_hash) {
557 if (req->id == id) {
558 break;
559 }
560 }
561
562 if (req != NULL) {
563 pxdns_hash_del(pxdns, req);
564 pxdns_timeout_del(pxdns, req);
565 }
566
567 sys_mutex_unlock(&pxdns->lock);
568 return req;
569}
570
571
572/**
573 * Retransmit of g/c expired requests and move timeout slot forward.
574 */
575static void
576pxdns_timer(void *arg)
577{
578 struct pxdns *pxdns = (struct pxdns *)arg;
579 struct request **chain, *req;
580 u32_t mask;
581
582 sys_mutex_lock(&pxdns->lock);
583
584 /*
585 * Move timeout slot first. New slot points to the list of
586 * expired requests. If any expired request is retransmitted, we
587 * keep it on the list (that is now current), effectively
588 * resetting the timeout.
589 */
590 LWIP_ASSERT1(pxdns->timeout_slot < TIMEOUT);
591 if (++pxdns->timeout_slot == TIMEOUT) {
592 pxdns->timeout_slot = 0;
593 }
594
595 chain = &pxdns->timeout_list[pxdns->timeout_slot];
596 req = *chain;
597 while (req != NULL) {
598 struct request *expired = req;
599 req = req->next_timeout;
600
601 if (pxdns_rexmit(pxdns, expired)) {
602 continue;
603 }
604
605 pxdns_hash_del(pxdns, expired);
606 pxdns_timeout_del(pxdns, expired);
607 ++pxdns->expired_queries;
608
609 pxdns_request_free(expired);
610 }
611
612 if (pxdns->timeout_list[pxdns->timeout_slot] == NULL) {
613 pxdns->timeout_mask &= ~(1U << pxdns->timeout_slot);
614 }
615 else {
616 pxdns->timeout_mask |= 1U << pxdns->timeout_slot;
617 }
618 mask = pxdns->timeout_mask;
619
620 sys_mutex_unlock(&pxdns->lock);
621
622 if (mask != 0) {
623 sys_timeout(1 * 1000, pxdns_timer, pxdns);
624 }
625}
626
627
628static void
629pxdns_recv4(void *arg, struct udp_pcb *pcb, struct pbuf *p,
630 ip_addr_t *addr, u16_t port)
631{
632 struct pxdns *pxdns = (struct pxdns *)arg;
633 pxdns_query(pxdns, pcb, p, ip_2_ipX(addr), port);
634}
635
636static void
637pxdns_recv6(void *arg, struct udp_pcb *pcb, struct pbuf *p,
638 ip6_addr_t *addr, u16_t port)
639{
640 struct pxdns *pxdns = (struct pxdns *)arg;
641 pxdns_query(pxdns, pcb, p, ip6_2_ipX(addr), port);
642}
643
644
645static void
646pxdns_query(struct pxdns *pxdns, struct udp_pcb *pcb, struct pbuf *p,
647 ipX_addr_t *addr, u16_t port)
648{
649 struct request *req;
650 int sent;
651
652 if (pxdns->nresolvers == 0) {
653 /* nothing we can do */
654 pbuf_free(p);
655 return;
656 }
657
658 req = calloc(1, sizeof(struct request) - 1 + p->tot_len);
659 if (req == NULL) {
660 pbuf_free(p);
661 return;
662 }
663
664 /* copy request data */
665 req->size = p->tot_len;
666 pbuf_copy_partial(p, req->data, p->tot_len, 0);
667
668 /* save client identity and client's request id */
669 req->pcb = pcb;
670 ipX_addr_copy(PCB_ISIPV6(pcb), req->client_addr, *addr);
671 req->client_port = port;
672 memcpy(&req->client_id, req->data, sizeof(req->client_id));
673
674 /* slap our request id onto it */
675 req->id = pxdns->id++;
676 memcpy(req->data, &req->id, sizeof(u16_t));
677
678 /* resolver to forward to */
679 req->generation = pxdns->generation;
680 req->residx = 0;
681
682 /* prepare for relaying the reply back to guest */
683 req->msg_reply.type = TCPIP_MSG_CALLBACK_STATIC;
684 req->msg_reply.sem = NULL;
685 req->msg_reply.msg.cb.function = pxdns_pcb_reply;
686 req->msg_reply.msg.cb.ctx = (void *)req;
687
688 DPRINTF2(("%s: req=%p: client id %d -> id %d\n",
689 __func__, (void *)req, req->client_id, req->id));
690
691 pxdns_request_register(pxdns, req);
692
693 sent = pxdns_forward_outbound(pxdns, req);
694 if (!sent) {
695 sent = pxdns_rexmit(pxdns, req);
696 }
697 if (!sent) {
698 pxdns_request_deregister(pxdns, req);
699 pxdns_request_free(req);
700 }
701}
702
703
704/**
705 * Forward request to the req::residx resolver in the pxdns::resolvers
706 * array of upstream resolvers.
707 *
708 * Returns 1 on success, 0 on failure.
709 */
710static int
711pxdns_forward_outbound(struct pxdns *pxdns, struct request *req)
712{
713 union sockaddr_inet *resolver;
714 ssize_t nsent;
715#ifdef RT_OS_WINDOWS
716 const char *pSendData = (const char *)&req->data[0];
717 int cbSendData = (int)req->size;
718 Assert((size_t)cbSendData == req->size);
719#else
720 const void *pSendData = &req->data[0];
721 size_t cbSendData = req->size;
722#endif
723
724 DPRINTF2(("%s: req %p: sending to resolver #%lu\n",
725 __func__, (void *)req, (unsigned long)req->residx));
726
727 LWIP_ASSERT1(req->generation == pxdns->generation);
728 LWIP_ASSERT1(req->residx < pxdns->nresolvers);
729 resolver = &pxdns->resolvers[req->residx];
730
731 if (resolver->sa.sa_family == AF_INET) {
732 nsent = sendto(pxdns->sock4, pSendData, cbSendData, 0,
733 &resolver->sa, sizeof(resolver->sin));
734
735 }
736 else if (resolver->sa.sa_family == AF_INET6) {
737 if (pxdns->sock6 != INVALID_SOCKET) {
738 nsent = sendto(pxdns->sock6, pSendData, cbSendData, 0,
739 &resolver->sa, sizeof(resolver->sin6));
740 }
741 else {
742 /* shouldn't happen, we should have weeded out IPv6 resolvers */
743 return 0;
744 }
745 }
746 else {
747 /* shouldn't happen, we should have weeded out unsupported families */
748 return 0;
749 }
750
751 if ((size_t)nsent == req->size) {
752 return 1; /* sent */
753 }
754
755 if (nsent < 0) {
756 DPRINTF2(("%s: send: %R[sockerr]\n", __func__, SOCKERRNO()));
757 }
758 else {
759 DPRINTF2(("%s: sent only %lu of %lu\n",
760 __func__, (unsigned long)nsent, (unsigned long)req->size));
761 }
762 return 0; /* not sent, caller will retry as necessary */
763}
764
765
766/**
767 * Forward request to the next resolver in the pxdns::resolvers array
768 * of upstream resolvers if there are any left.
769 */
770static int
771pxdns_rexmit(struct pxdns *pxdns, struct request *req)
772{
773 int sent;
774
775 if (/* __predict_false */ req->generation != pxdns->generation) {
776 DPRINTF2(("%s: req %p: generation %lu != pxdns generation %lu\n",
777 __func__, (void *)req,
778 (unsigned long)req->generation,
779 (unsigned long)pxdns->generation));
780 return 0;
781 }
782
783 LWIP_ASSERT1(req->residx < pxdns->nresolvers);
784 do {
785 if (++req->residx == pxdns->nresolvers) {
786 return 0;
787 }
788
789 sent = pxdns_forward_outbound(pxdns, req);
790 } while (!sent);
791
792 return 1;
793}
794
795
796static int
797pxdns_pmgr_pump(struct pollmgr_handler *handler, SOCKET fd, int revents)
798{
799 struct pxdns *pxdns;
800 struct request *req;
801 ssize_t nread;
802 err_t error;
803 u16_t id;
804
805 pxdns = (struct pxdns *)handler->data;
806 LWIP_ASSERT1(handler == &pxdns->pmhdl4 || handler == &pxdns->pmhdl6);
807 LWIP_ASSERT1(fd == (handler == &pxdns->pmhdl4 ? pxdns->sock4 : pxdns->sock6));
808
809 if (revents & ~(POLLIN|POLLERR)) {
810 DPRINTF0(("%s: unexpected revents 0x%x\n", __func__, revents));
811 return POLLIN;
812 }
813
814 if (revents & POLLERR) {
815 int sockerr = -1;
816 socklen_t optlen = (socklen_t)sizeof(sockerr);
817 int status;
818
819 status = getsockopt(fd, SOL_SOCKET,
820 SO_ERROR, (char *)&sockerr, &optlen);
821 if (status < 0) {
822 DPRINTF(("%s: sock %d: SO_ERROR failed: %R[sockerr]\n",
823 __func__, fd, SOCKERRNO()));
824 }
825 else {
826 DPRINTF(("%s: sock %d: %R[sockerr]\n",
827 __func__, fd, sockerr));
828 }
829 }
830
831 if ((revents & POLLIN) == 0) {
832 return POLLIN;
833 }
834
835
836#ifdef RT_OS_WINDOWS
837 nread = recv(fd, (char *)pollmgr_udpbuf, sizeof(pollmgr_udpbuf), 0);
838#else
839 nread = recv(fd, pollmgr_udpbuf, sizeof(pollmgr_udpbuf), 0);
840#endif
841 if (nread < 0) {
842 DPRINTF(("%s: %R[sockerr]\n", __func__, SOCKERRNO()));
843 return POLLIN;
844 }
845
846 /* check for minimum dns packet length */
847 if (nread < 12) {
848 DPRINTF2(("%s: short reply %lu bytes\n",
849 __func__, (unsigned long)nread));
850 return POLLIN;
851 }
852
853 /* XXX: shall we proxy back RCODE=Refused responses? */
854
855 memcpy(&id, pollmgr_udpbuf, sizeof(id));
856 req = pxdns_request_find(pxdns, id);
857 if (req == NULL) {
858 DPRINTF2(("%s: orphaned reply for %d\n", __func__, id));
859 ++pxdns->late_answers;
860 return POLLIN;
861 }
862
863 DPRINTF2(("%s: reply for req=%p: id %d -> client id %d\n",
864 __func__, (void *)req, req->id, req->client_id));
865
866 req->reply = pbuf_alloc(PBUF_RAW, nread, PBUF_RAM);
867 if (req->reply == NULL) {
868 DPRINTF(("%s: pbuf_alloc(%d) failed\n", __func__, (int)nread));
869 pxdns_request_free(req);
870 return POLLIN;
871 }
872
873 memcpy(pollmgr_udpbuf, &req->client_id, sizeof(req->client_id));
874 error = pbuf_take(req->reply, pollmgr_udpbuf, nread);
875 if (error != ERR_OK) {
876 DPRINTF(("%s: pbuf_take(%d) failed\n", __func__, (int)nread));
877 pxdns_request_free(req);
878 return POLLIN;
879 }
880
881 proxy_lwip_post(&req->msg_reply);
882 return POLLIN;
883}
884
885
886/**
887 * Called on lwIP thread via request::msg_reply callback.
888 */
889static void
890pxdns_pcb_reply(void *ctx)
891{
892 struct request *req = (struct request *)ctx;
893 err_t error;
894
895 error = udp_sendto(req->pcb, req->reply,
896 ipX_2_ip(&req->client_addr), req->client_port);
897 if (error != ERR_OK) {
898 DPRINTF(("%s: udp_sendto err %s\n",
899 __func__, proxy_lwip_strerr(error)));
900 }
901
902 pxdns_request_free(req);
903}
904
905
906/**
907 * TCP DNS proxy. This kicks in for large replies that don't fit into
908 * 512 bytes of UDP payload. Client will retry with TCP to get
909 * complete reply.
910 */
911static err_t
912pxdns_accept_syn(void *arg, struct tcp_pcb *newpcb, struct pbuf *syn)
913{
914 struct pxdns *pxdns = (struct pxdns *)arg;
915 union sockaddr_inet *si;
916 ipX_addr_t *dst;
917 u16_t dst_port;
918
919 tcp_accepted(pxdns->ltcp);
920
921 if (pxdns->nresolvers == 0) {
922 return ERR_CONN;
923 }
924
925 si = &pxdns->resolvers[0];
926
927 if (si->sa.sa_family == AF_INET6) {
928 dst = (ipX_addr_t *)&si->sin6.sin6_addr;
929 dst_port = ntohs(si->sin6.sin6_port);
930 }
931 else {
932 dst = (ipX_addr_t *)&si->sin.sin_addr;
933 dst_port = ntohs(si->sin.sin_port);
934 }
935
936 /*
937 * XXX: TODO: need to implement protocol hooks. E.g. here if
938 * connect fails, we should try connecting to a different server.
939 */
940 return pxtcp_pcb_accept_outbound(newpcb, syn,
941 si->sa.sa_family == AF_INET6, dst, dst_port);
942}
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