VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/pxping.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: 55.3 KB
Line 
1/* $Id: pxping.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * NAT Network - ping proxy, raw sockets version.
4 */
5
6/*
7 * Copyright (C) 2013-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#define LOG_GROUP LOG_GROUP_NAT_SERVICE
29
30#include "winutils.h"
31#include "proxy.h"
32#include "proxy_pollmgr.h"
33#include "pxremap.h"
34
35#include <iprt/string.h>
36
37#ifndef RT_OS_WINDOWS
38#include <sys/types.h>
39#include <sys/socket.h>
40#ifdef RT_OS_DARWIN
41# define __APPLE_USE_RFC_3542
42#endif
43#include <netinet/in.h>
44#include <poll.h>
45#include <stdint.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#else
50#include <iprt/stdint.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include "winpoll.h"
55#endif
56
57#include "lwip/opt.h"
58
59#include "lwip/sys.h"
60#include "lwip/tcpip.h"
61#include "lwip/inet_chksum.h"
62#include "lwip/ip.h"
63#include "lwip/icmp.h"
64
65#if defined(RT_OS_LINUX) && !defined(__USE_GNU)
66#if __GLIBC_PREREQ(2, 8)
67/*
68 * XXX: This is gross. in6_pktinfo is now hidden behind _GNU_SOURCE
69 * https://sourceware.org/bugzilla/show_bug.cgi?id=6775
70 *
71 * But in older glibc versions, e.g. RHEL5, it is not! I don't want
72 * to deal with _GNU_SOURCE now, so as a kludge check for glibc
73 * version. It seems the __USE_GNU guard was introduced in 2.8.
74 */
75struct in6_pktinfo {
76 struct in6_addr ipi6_addr;
77 unsigned int ipi6_ifindex;
78};
79#endif /* __GLIBC_PREREQ */
80#endif /* RT_OS_LINUX && !__USE_GNU */
81
82
83/* forward */
84struct ping_pcb;
85
86
87/**
88 * Global state for ping proxy collected in one entity to minimize
89 * globals. There's only one instance of this structure.
90 *
91 * Raw ICMP sockets are promiscuous, so it doesn't make sense to have
92 * multiple. If this code ever needs to support multiple netifs, the
93 * netif member should be exiled into "pcb".
94 */
95struct pxping {
96 SOCKET sock4;
97
98#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
99# define DF_WITH_IP_HDRINCL
100 int hdrincl;
101#else
102 int df;
103#endif
104 int ttl;
105 int tos;
106
107 SOCKET sock6;
108#ifdef RT_OS_WINDOWS
109 LPFN_WSARECVMSG pfWSARecvMsg6;
110#endif
111 int hopl;
112
113 struct pollmgr_handler pmhdl4;
114 struct pollmgr_handler pmhdl6;
115
116 struct netif *netif;
117
118 /**
119 * Protect lwIP and pmgr accesses to the list of pcbs.
120 */
121 sys_mutex_t lock;
122
123 /*
124 * We need to find pcbs both from the guest side and from the host
125 * side. If we need to support industrial grade ping throughput,
126 * we will need two pcb hashes. For now, a short linked list
127 * should be enough. Cf. pxping_pcb_for_request() and
128 * pxping_pcb_for_reply().
129 */
130#define PXPING_MAX_PCBS 8
131 size_t npcbs;
132 struct ping_pcb *pcbs;
133
134#define TIMEOUT 5
135 int timer_active;
136 size_t timeout_slot;
137 struct ping_pcb *timeout_list[TIMEOUT];
138};
139
140
141/**
142 * Quasi PCB for ping.
143 */
144struct ping_pcb {
145 ipX_addr_t src;
146 ipX_addr_t dst;
147
148 u8_t is_ipv6;
149 u8_t is_mapped;
150
151 u16_t guest_id;
152 u16_t host_id;
153
154 /**
155 * Desired slot in pxping::timeout_list. See pxping_timer().
156 */
157 size_t timeout_slot;
158
159 /**
160 * Chaining for pxping::timeout_list
161 */
162 struct ping_pcb **pprev_timeout;
163 struct ping_pcb *next_timeout;
164
165 /**
166 * Chaining for pxping::pcbs
167 */
168 struct ping_pcb *next;
169
170 union {
171 struct sockaddr_in sin;
172 struct sockaddr_in6 sin6;
173 } peer;
174};
175
176
177/**
178 * lwIP thread callback message for IPv4 ping.
179 *
180 * We pass raw IP datagram for ip_output_if() so we only need pbuf and
181 * netif (from pxping).
182 */
183struct ping_msg {
184 struct tcpip_msg msg;
185 struct pxping *pxping;
186 struct pbuf *p;
187};
188
189
190/**
191 * lwIP thread callback message for IPv6 ping.
192 *
193 * We cannot obtain raw IPv6 datagram from host without extra trouble,
194 * so we pass ICMPv6 payload in pbuf and also other parameters to
195 * ip6_output_if().
196 */
197struct ping6_msg {
198 struct tcpip_msg msg;
199 struct pxping *pxping;
200 struct pbuf *p;
201 ip6_addr_t src, dst;
202 int hopl, tclass;
203};
204
205
206#ifdef RT_OS_WINDOWS
207static int pxping_init_windows(struct pxping *pxping);
208#endif
209static void pxping_recv4(void *arg, struct pbuf *p);
210static void pxping_recv6(void *arg, struct pbuf *p);
211
212static void pxping_timer(void *arg);
213static void pxping_timer_needed(struct pxping *pxping);
214
215static struct ping_pcb *pxping_pcb_for_request(struct pxping *pxping,
216 int is_ipv6,
217 ipX_addr_t *src, ipX_addr_t *dst,
218 u16_t guest_id);
219static struct ping_pcb *pxping_pcb_for_reply(struct pxping *pxping, int is_ipv6,
220 ipX_addr_t *dst, u16_t host_id);
221
222static FNRTSTRFORMATTYPE pxping_pcb_rtstrfmt;
223static struct ping_pcb *pxping_pcb_allocate(struct pxping *pxping);
224static void pxping_pcb_register(struct pxping *pxping, struct ping_pcb *pcb);
225static void pxping_pcb_deregister(struct pxping *pxping, struct ping_pcb *pcb);
226static void pxping_pcb_delete(struct pxping *pxping, struct ping_pcb *pcb);
227static void pxping_timeout_add(struct pxping *pxping, struct ping_pcb *pcb);
228static void pxping_timeout_del(struct pxping *pxping, struct ping_pcb *pcb);
229
230static int pxping_pmgr_pump(struct pollmgr_handler *handler, SOCKET fd, int revents);
231
232static void pxping_pmgr_icmp4(struct pxping *pxping);
233static void pxping_pmgr_icmp4_echo(struct pxping *pxping,
234 u16_t iplen, struct sockaddr_in *peer);
235static void pxping_pmgr_icmp4_error(struct pxping *pxping,
236 u16_t iplen, struct sockaddr_in *peer);
237static void pxping_pmgr_icmp6(struct pxping *pxping);
238static void pxping_pmgr_icmp6_echo(struct pxping *pxping,
239 ip6_addr_t *src, ip6_addr_t *dst,
240 int hopl, int tclass, u16_t icmplen);
241static void pxping_pmgr_icmp6_error(struct pxping *pxping,
242 ip6_addr_t *src, ip6_addr_t *dst,
243 int hopl, int tclass, u16_t icmplen);
244
245static void pxping_pmgr_forward_inbound(struct pxping *pxping, u16_t iplen);
246static void pxping_pcb_forward_inbound(void *arg);
247
248static void pxping_pmgr_forward_inbound6(struct pxping *pxping,
249 ip6_addr_t *src, ip6_addr_t *dst,
250 u8_t hopl, u8_t tclass,
251 u16_t icmplen);
252static void pxping_pcb_forward_inbound6(void *arg);
253
254/*
255 * NB: This is not documented except in RTFS.
256 *
257 * If ip_output_if() is passed dest == NULL then it treats p as
258 * complete IP packet with payload pointing to the IP header. It does
259 * not build IP header, ignores all header-related arguments, fetches
260 * real destination from the header in the pbuf and outputs pbuf to
261 * the specified netif.
262 */
263#define ip_raw_output_if(p, netif) \
264 (ip_output_if((p), NULL, NULL, 0, 0, 0, (netif)))
265
266
267
268static struct pxping g_pxping;
269
270
271err_t
272pxping_init(struct netif *netif, SOCKET sock4, SOCKET sock6)
273{
274 const int on = 1;
275 int status;
276
277 if (sock4 == INVALID_SOCKET && sock6 == INVALID_SOCKET) {
278 return ERR_VAL;
279 }
280
281 g_pxping.netif = netif;
282 sys_mutex_new(&g_pxping.lock);
283
284 g_pxping.sock4 = sock4;
285 if (g_pxping.sock4 != INVALID_SOCKET) {
286#ifdef DF_WITH_IP_HDRINCL
287 g_pxping.hdrincl = 0;
288#else
289 g_pxping.df = -1;
290#endif
291 g_pxping.ttl = -1;
292 g_pxping.tos = 0;
293
294#ifdef RT_OS_LINUX
295 {
296 const int dont = IP_PMTUDISC_DONT;
297 status = setsockopt(sock4, IPPROTO_IP, IP_MTU_DISCOVER,
298 &dont, sizeof(dont));
299 if (status != 0) {
300 DPRINTF(("IP_MTU_DISCOVER: %R[sockerr]\n", SOCKERRNO()));
301 }
302 }
303#endif /* RT_OS_LINUX */
304
305 g_pxping.pmhdl4.callback = pxping_pmgr_pump;
306 g_pxping.pmhdl4.data = (void *)&g_pxping;
307 g_pxping.pmhdl4.slot = -1;
308 pollmgr_add(&g_pxping.pmhdl4, g_pxping.sock4, POLLIN);
309
310 ping_proxy_accept(pxping_recv4, &g_pxping);
311 }
312
313 g_pxping.sock6 = sock6;
314#ifdef RT_OS_WINDOWS
315 /* we need recvmsg */
316 if (g_pxping.sock6 != INVALID_SOCKET) {
317 status = pxping_init_windows(&g_pxping);
318 if (status == SOCKET_ERROR) {
319 g_pxping.sock6 = INVALID_SOCKET;
320 /* close(sock6); */
321 }
322 }
323#endif
324 if (g_pxping.sock6 != INVALID_SOCKET) {
325 g_pxping.hopl = -1;
326
327#if !defined(IPV6_RECVPKTINFO)
328#define IPV6_RECVPKTINFO (IPV6_PKTINFO)
329#endif
330 status = setsockopt(sock6, IPPROTO_IPV6, IPV6_RECVPKTINFO,
331 (const char *)&on, sizeof(on));
332 if (status < 0) {
333 DPRINTF(("IPV6_RECVPKTINFO: %R[sockerr]\n", SOCKERRNO()));
334 /* XXX: for now this is fatal */
335 }
336
337#if !defined(IPV6_RECVHOPLIMIT)
338#define IPV6_RECVHOPLIMIT (IPV6_HOPLIMIT)
339#endif
340 status = setsockopt(sock6, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
341 (const char *)&on, sizeof(on));
342 if (status < 0) {
343 DPRINTF(("IPV6_RECVHOPLIMIT: %R[sockerr]\n", SOCKERRNO()));
344 }
345
346#ifdef IPV6_RECVTCLASS /* new in RFC 3542, there's no RFC 2292 counterpart */
347 /** @todo IPV6_RECVTCLASS */
348#endif
349
350 g_pxping.pmhdl6.callback = pxping_pmgr_pump;
351 g_pxping.pmhdl6.data = (void *)&g_pxping;
352 g_pxping.pmhdl6.slot = -1;
353 pollmgr_add(&g_pxping.pmhdl6, g_pxping.sock6, POLLIN);
354
355 ping6_proxy_accept(pxping_recv6, &g_pxping);
356 }
357
358 status = RTStrFormatTypeRegister("ping_pcb", pxping_pcb_rtstrfmt, NULL);
359 AssertRC(status);
360
361 return ERR_OK;
362}
363
364
365#ifdef RT_OS_WINDOWS
366static int
367pxping_init_windows(struct pxping *pxping)
368{
369 GUID WSARecvMsgGUID = WSAID_WSARECVMSG;
370 DWORD nread;
371 int status;
372
373 pxping->pfWSARecvMsg6 = NULL;
374 status = WSAIoctl(pxping->sock6,
375 SIO_GET_EXTENSION_FUNCTION_POINTER,
376 &WSARecvMsgGUID, sizeof(WSARecvMsgGUID),
377 &pxping->pfWSARecvMsg6, sizeof(pxping->pfWSARecvMsg6),
378 &nread,
379 NULL, NULL);
380 return status;
381}
382#endif /* RT_OS_WINDOWS */
383
384
385static u32_t
386chksum_delta_16(u16_t oval, u16_t nval)
387{
388 u32_t sum = (u16_t)~oval;
389 sum += nval;
390 return sum;
391}
392
393
394static u32_t
395chksum_update_16(u16_t *oldp, u16_t nval)
396{
397 u32_t sum = chksum_delta_16(*oldp, nval);
398 *oldp = nval;
399 return sum;
400}
401
402
403static u32_t
404chksum_delta_32(u32_t oval, u32_t nval)
405{
406 u32_t sum = ~oval;
407 sum = FOLD_U32T(sum);
408 sum += FOLD_U32T(nval);
409 return sum;
410}
411
412
413static u32_t
414chksum_update_32(u32_t *oldp, u32_t nval)
415{
416 u32_t sum = chksum_delta_32(*oldp, nval);
417 *oldp = nval;
418 return sum;
419}
420
421
422static u32_t
423chksum_delta_ipv6(const ip6_addr_t *oldp, const ip6_addr_t *newp)
424{
425 u32_t sum;
426
427 sum = chksum_delta_32(oldp->addr[0], newp->addr[0]);
428 sum += chksum_delta_32(oldp->addr[1], newp->addr[1]);
429 sum += chksum_delta_32(oldp->addr[2], newp->addr[2]);
430 sum += chksum_delta_32(oldp->addr[3], newp->addr[3]);
431
432 return sum;
433}
434
435
436static u32_t
437chksum_update_ipv6(ip6_addr_t *oldp, const ip6_addr_t *newp)
438{
439 u32_t sum;
440
441 sum = chksum_update_32(&oldp->addr[0], newp->addr[0]);
442 sum += chksum_update_32(&oldp->addr[1], newp->addr[1]);
443 sum += chksum_update_32(&oldp->addr[2], newp->addr[2]);
444 sum += chksum_update_32(&oldp->addr[3], newp->addr[3]);
445
446 return sum;
447}
448
449
450/**
451 * ICMP Echo Request in pbuf "p" is to be proxied.
452 */
453static void
454pxping_recv4(void *arg, struct pbuf *p)
455{
456 struct pxping *pxping = (struct pxping *)arg;
457 struct ping_pcb *pcb;
458#ifdef DF_WITH_IP_HDRINCL
459 struct ip_hdr iph_orig;
460#endif
461 struct icmp_echo_hdr icmph_orig;
462 struct ip_hdr *iph;
463 struct icmp_echo_hdr *icmph;
464 int df, ttl, tos;
465 u32_t sum;
466 u16_t iphlen;
467 int status;
468
469 iphlen = ip_current_header_tot_len();
470 if (iphlen != IP_HLEN) { /* we don't do options */
471 pbuf_free(p);
472 return;
473 }
474
475 iph = (/* UNCONST */ struct ip_hdr *)ip_current_header();
476 icmph = (struct icmp_echo_hdr *)p->payload;
477
478 pcb = pxping_pcb_for_request(pxping, 0,
479 ipX_current_src_addr(),
480 ipX_current_dest_addr(),
481 icmph->id);
482 if (pcb == NULL) {
483 pbuf_free(p);
484 return;
485 }
486
487 DPRINTF(("ping %p: %R[ping_pcb] seq %d len %u ttl %d\n",
488 pcb, pcb,
489 ntohs(icmph->seqno), (unsigned int)p->tot_len,
490 IPH_TTL(iph)));
491
492 ttl = IPH_TTL(iph);
493 if (!pcb->is_mapped) {
494 if (RT_UNLIKELY(ttl == 1)) {
495 status = pbuf_header(p, iphlen); /* back to IP header */
496 if (RT_LIKELY(status == 0)) {
497 icmp_time_exceeded(p, ICMP_TE_TTL);
498 }
499 pbuf_free(p);
500 return;
501 }
502 --ttl;
503 }
504
505 /*
506 * OS X doesn't provide a socket option to control fragmentation.
507 * Solaris doesn't provide IP_DONTFRAG on all releases we support.
508 * In this case we have to use IP_HDRINCL. We don't want to use
509 * it always since it doesn't handle fragmentation (but that's ok
510 * for DF) and Windows doesn't do automatic source address
511 * selection with IP_HDRINCL.
512 */
513 df = (IPH_OFFSET(iph) & PP_HTONS(IP_DF)) != 0;
514
515#ifdef DF_WITH_IP_HDRINCL
516 if (df != pxping->hdrincl) {
517 status = setsockopt(pxping->sock4, IPPROTO_IP, IP_HDRINCL,
518 &df, sizeof(df));
519 if (RT_LIKELY(status == 0)) {
520 pxping->hdrincl = df;
521 }
522 else {
523 DPRINTF(("IP_HDRINCL: %R[sockerr]\n", SOCKERRNO()));
524 }
525 }
526
527 if (pxping->hdrincl) {
528 status = pbuf_header(p, iphlen); /* back to IP header */
529 if (RT_UNLIKELY(status != 0)) {
530 pbuf_free(p);
531 return;
532 }
533
534 /* we will overwrite IP header, save original for ICMP errors */
535 memcpy(&iph_orig, iph, iphlen);
536
537 if (pcb->is_mapped) {
538 ip4_addr_set_u32(&iph->dest, pcb->peer.sin.sin_addr.s_addr);
539 }
540
541 if (g_proxy_options->src4 != NULL) {
542 ip4_addr_set_u32(&iph->src, g_proxy_options->src4->sin_addr.s_addr);
543 }
544 else {
545 /* let the kernel select suitable source address */
546 ip_addr_set_any(&iph->src);
547 }
548
549 IPH_TTL_SET(iph, ttl); /* already decremented */
550 IPH_ID_SET(iph, 0); /* kernel will set one */
551#ifdef RT_OS_DARWIN
552 /* wants ip_offset and ip_len fields in host order */
553 IPH_OFFSET_SET(iph, ntohs(IPH_OFFSET(iph)));
554 IPH_LEN_SET(iph, ntohs(IPH_LEN(iph)));
555 /* wants checksum of everything (sic!), in host order */
556 sum = inet_chksum_pbuf(p);
557 IPH_CHKSUM_SET(iph, sum);
558#else /* !RT_OS_DARWIN */
559 IPH_CHKSUM_SET(iph, 0); /* kernel will recalculate */
560#endif
561 }
562 else /* !pxping->hdrincl */
563#endif /* DF_WITH_IP_HDRINCL */
564 {
565#if !defined(DF_WITH_IP_HDRINCL)
566 /* control DF flag via setsockopt(2) */
567#define USE_DF_OPTION(_Optname) \
568 const int dfopt = _Optname; \
569 const char * const dfoptname = #_Optname; NOREF(dfoptname)
570#if defined(RT_OS_LINUX)
571 USE_DF_OPTION(IP_MTU_DISCOVER);
572 df = df ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
573#elif defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
574 USE_DF_OPTION(IP_DONTFRAG);
575#elif defined(RT_OS_WINDOWS)
576 USE_DF_OPTION(IP_DONTFRAGMENT);
577#endif
578 if (df != pxping->df) {
579 status = setsockopt(pxping->sock4, IPPROTO_IP, dfopt,
580 (char *)&df, sizeof(df));
581 if (RT_LIKELY(status == 0)) {
582 pxping->df = df;
583 }
584 else {
585 DPRINTF(("%s: %R[sockerr]\n", dfoptname, SOCKERRNO()));
586 }
587 }
588#endif /* !DF_WITH_IP_HDRINCL */
589
590 if (ttl != pxping->ttl) {
591 status = setsockopt(pxping->sock4, IPPROTO_IP, IP_TTL,
592 (char *)&ttl, sizeof(ttl));
593 if (RT_LIKELY(status == 0)) {
594 pxping->ttl = ttl;
595 }
596 else {
597 DPRINTF(("IP_TTL: %R[sockerr]\n", SOCKERRNO()));
598 }
599 }
600
601 tos = IPH_TOS(iph);
602 if (tos != pxping->tos) {
603 status = setsockopt(pxping->sock4, IPPROTO_IP, IP_TOS,
604 (char *)&tos, sizeof(tos));
605 if (RT_LIKELY(status == 0)) {
606 pxping->tos = tos;
607 }
608 else {
609 DPRINTF(("IP_TOS: %R[sockerr]\n", SOCKERRNO()));
610 }
611 }
612 }
613
614 /* rewrite ICMP echo header */
615 memcpy(&icmph_orig, icmph, sizeof(*icmph));
616 sum = (u16_t)~icmph->chksum;
617 sum += chksum_update_16(&icmph->id, pcb->host_id);
618 sum = FOLD_U32T(sum);
619 icmph->chksum = ~sum;
620
621 status = proxy_sendto(pxping->sock4, p,
622 &pcb->peer.sin, sizeof(pcb->peer.sin));
623 if (status != 0) {
624 int error = -status;
625 DPRINTF(("%s: sendto: %R[sockerr]\n", __func__, error));
626
627#ifdef DF_WITH_IP_HDRINCL
628 if (pxping->hdrincl) {
629 /* restore original IP header */
630 memcpy(iph, &iph_orig, iphlen);
631 }
632 else
633#endif
634 {
635 status = pbuf_header(p, iphlen); /* back to IP header */
636 if (RT_UNLIKELY(status != 0)) {
637 pbuf_free(p);
638 return;
639 }
640 }
641
642 /* restore original ICMP header */
643 memcpy(icmph, &icmph_orig, sizeof(*icmph));
644
645 /*
646 * Some ICMP errors may be generated by the kernel and we read
647 * them from the socket and forward them normally, hence the
648 * ifdefs below.
649 */
650 switch (error) {
651
652#if !( defined(RT_OS_SOLARIS) \
653 || (defined(RT_OS_LINUX) && !defined(DF_WITH_IP_HDRINCL)) \
654 )
655 case EMSGSIZE:
656 icmp_dest_unreach(p, ICMP_DUR_FRAG);
657 break;
658#endif
659
660 case ENETDOWN:
661 case ENETUNREACH:
662 icmp_dest_unreach(p, ICMP_DUR_NET);
663 break;
664
665 case EHOSTDOWN:
666 case EHOSTUNREACH:
667 icmp_dest_unreach(p, ICMP_DUR_HOST);
668 break;
669 }
670 }
671
672 pbuf_free(p);
673}
674
675
676/**
677 * ICMPv6 Echo Request in pbuf "p" is to be proxied.
678 */
679static void
680pxping_recv6(void *arg, struct pbuf *p)
681{
682 struct pxping *pxping = (struct pxping *)arg;
683 struct ping_pcb *pcb;
684 struct ip6_hdr *iph;
685 struct icmp6_echo_hdr *icmph;
686 int hopl;
687 u16_t iphlen;
688 u16_t id;
689 int status;
690
691 iph = (/* UNCONST */ struct ip6_hdr *)ip6_current_header();
692 iphlen = ip_current_header_tot_len();
693
694 icmph = (struct icmp6_echo_hdr *)p->payload;
695
696 id = icmph->id;
697 pcb = pxping_pcb_for_request(pxping, 1,
698 ipX_current_src_addr(),
699 ipX_current_dest_addr(),
700 id);
701 if (pcb == NULL) {
702 pbuf_free(p);
703 return;
704 }
705
706 DPRINTF(("ping %p: %R[ping_pcb] seq %d len %u hopl %d\n",
707 pcb, pcb,
708 ntohs(icmph->seqno), (unsigned int)p->tot_len,
709 IP6H_HOPLIM(iph)));
710
711 hopl = IP6H_HOPLIM(iph);
712 if (!pcb->is_mapped) {
713 if (hopl == 1) {
714 status = pbuf_header(p, iphlen); /* back to IP header */
715 if (RT_LIKELY(status == 0)) {
716 icmp6_time_exceeded(p, ICMP6_TE_HL);
717 }
718 pbuf_free(p);
719 return;
720 }
721 --hopl;
722 }
723
724 /*
725 * Rewrite ICMPv6 echo header. We don't need to recompute the
726 * checksum since, unlike IPv4, checksum includes pseudo-header.
727 * OS computes checksum for us on send() since it needs to select
728 * source address.
729 */
730 icmph->id = pcb->host_id;
731
732 /** @todo use control messages to save a syscall? */
733 if (hopl != pxping->hopl) {
734 status = setsockopt(pxping->sock6, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
735 (char *)&hopl, sizeof(hopl));
736 if (status == 0) {
737 pxping->hopl = hopl;
738 }
739 else {
740 DPRINTF(("IPV6_HOPLIMIT: %R[sockerr]\n", SOCKERRNO()));
741 }
742 }
743
744 status = proxy_sendto(pxping->sock6, p,
745 &pcb->peer.sin6, sizeof(pcb->peer.sin6));
746 if (status != 0) {
747 int error = -status;
748 DPRINTF(("%s: sendto: %R[sockerr]\n", __func__, error));
749
750 status = pbuf_header(p, iphlen); /* back to IP header */
751 if (RT_UNLIKELY(status != 0)) {
752 pbuf_free(p);
753 return;
754 }
755
756 /* restore original ICMP header */
757 icmph->id = pcb->guest_id;
758
759 switch (error) {
760 case EACCES:
761 icmp6_dest_unreach(p, ICMP6_DUR_PROHIBITED);
762 break;
763
764#ifdef ENONET
765 case ENONET:
766#endif
767 case ENETDOWN:
768 case ENETUNREACH:
769 case EHOSTDOWN:
770 case EHOSTUNREACH:
771 icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
772 break;
773 }
774 }
775
776 pbuf_free(p);
777}
778
779
780/**
781 * Formatter for %R[ping_pcb].
782 */
783static DECLCALLBACK(size_t)
784pxping_pcb_rtstrfmt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
785 const char *pszType, const void *pvValue,
786 int cchWidth, int cchPrecision, unsigned int fFlags,
787 void *pvUser)
788{
789 const struct ping_pcb *pcb = (const struct ping_pcb *)pvValue;
790 size_t cb = 0;
791
792 NOREF(cchWidth);
793 NOREF(cchPrecision);
794 NOREF(fFlags);
795 NOREF(pvUser);
796
797 AssertReturn(strcmp(pszType, "ping_pcb") == 0, 0);
798
799 if (pcb == NULL) {
800 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "(null)");
801 }
802
803 /* XXX: %RTnaipv4 takes the value, but %RTnaipv6 takes the pointer */
804 if (pcb->is_ipv6) {
805 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
806 "%RTnaipv6 -> %RTnaipv6", &pcb->src, &pcb->dst);
807 if (pcb->is_mapped) {
808 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
809 " (%RTnaipv6)", &pcb->peer.sin6.sin6_addr);
810 }
811 }
812 else {
813 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
814 "%RTnaipv4 -> %RTnaipv4",
815 ip4_addr_get_u32(ipX_2_ip(&pcb->src)),
816 ip4_addr_get_u32(ipX_2_ip(&pcb->dst)));
817 if (pcb->is_mapped) {
818 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
819 " (%RTnaipv4)", pcb->peer.sin.sin_addr.s_addr);
820 }
821 }
822
823 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
824 " id %04x->%04x", ntohs(pcb->guest_id), ntohs(pcb->host_id));
825
826 return cb;
827}
828
829
830static struct ping_pcb *
831pxping_pcb_allocate(struct pxping *pxping)
832{
833 struct ping_pcb *pcb;
834
835 if (pxping->npcbs >= PXPING_MAX_PCBS) {
836 return NULL;
837 }
838
839 pcb = (struct ping_pcb *)malloc(sizeof(*pcb));
840 if (pcb == NULL) {
841 return NULL;
842 }
843
844 ++pxping->npcbs;
845 return pcb;
846}
847
848
849static void
850pxping_pcb_delete(struct pxping *pxping, struct ping_pcb *pcb)
851{
852 LWIP_ASSERT1(pxping->npcbs > 0);
853 LWIP_ASSERT1(pcb->next == NULL);
854 LWIP_ASSERT1(pcb->pprev_timeout == NULL);
855
856 DPRINTF(("%s: ping %p\n", __func__, (void *)pcb));
857
858 --pxping->npcbs;
859 free(pcb);
860}
861
862
863static void
864pxping_timeout_add(struct pxping *pxping, struct ping_pcb *pcb)
865{
866 struct ping_pcb **chain;
867
868 LWIP_ASSERT1(pcb->pprev_timeout == NULL);
869
870 chain = &pxping->timeout_list[pcb->timeout_slot];
871 if ((pcb->next_timeout = *chain) != NULL) {
872 (*chain)->pprev_timeout = &pcb->next_timeout;
873 }
874 *chain = pcb;
875 pcb->pprev_timeout = chain;
876}
877
878
879static void
880pxping_timeout_del(struct pxping *pxping, struct ping_pcb *pcb)
881{
882 LWIP_UNUSED_ARG(pxping);
883
884 LWIP_ASSERT1(pcb->pprev_timeout != NULL);
885 if (pcb->next_timeout != NULL) {
886 pcb->next_timeout->pprev_timeout = pcb->pprev_timeout;
887 }
888 *pcb->pprev_timeout = pcb->next_timeout;
889 pcb->pprev_timeout = NULL;
890 pcb->next_timeout = NULL;
891}
892
893
894static void
895pxping_pcb_register(struct pxping *pxping, struct ping_pcb *pcb)
896{
897 pcb->next = pxping->pcbs;
898 pxping->pcbs = pcb;
899
900 pxping_timeout_add(pxping, pcb);
901}
902
903
904static void
905pxping_pcb_deregister(struct pxping *pxping, struct ping_pcb *pcb)
906{
907 struct ping_pcb **p;
908
909 for (p = &pxping->pcbs; *p != NULL; p = &(*p)->next) {
910 if (*p == pcb) {
911 *p = pcb->next;
912 pcb->next = NULL;
913 break;
914 }
915 }
916
917 pxping_timeout_del(pxping, pcb);
918}
919
920
921static struct ping_pcb *
922pxping_pcb_for_request(struct pxping *pxping,
923 int is_ipv6, ipX_addr_t *src, ipX_addr_t *dst,
924 u16_t guest_id)
925{
926 struct ping_pcb *pcb;
927
928 /* on lwip thread, so no concurrent updates */
929 for (pcb = pxping->pcbs; pcb != NULL; pcb = pcb->next) {
930 if (pcb->guest_id == guest_id
931 && pcb->is_ipv6 == is_ipv6
932 && ipX_addr_cmp(is_ipv6, &pcb->dst, dst)
933 && ipX_addr_cmp(is_ipv6, &pcb->src, src))
934 {
935 break;
936 }
937 }
938
939 if (pcb == NULL) {
940 int mapped;
941
942 pcb = pxping_pcb_allocate(pxping);
943 if (pcb == NULL) {
944 return NULL;
945 }
946
947 pcb->is_ipv6 = is_ipv6;
948 ipX_addr_copy(is_ipv6, pcb->src, *src);
949 ipX_addr_copy(is_ipv6, pcb->dst, *dst);
950
951 pcb->guest_id = guest_id;
952#ifdef RT_OS_WINDOWS
953# define random() (rand())
954#endif
955 pcb->host_id = random() & 0xffffUL;
956
957 pcb->pprev_timeout = NULL;
958 pcb->next_timeout = NULL;
959
960 if (is_ipv6) {
961 pcb->peer.sin6.sin6_family = AF_INET6;
962#if HAVE_SA_LEN
963 pcb->peer.sin6.sin6_len = sizeof(pcb->peer.sin6);
964#endif
965 pcb->peer.sin6.sin6_port = htons(IPPROTO_ICMPV6);
966 pcb->peer.sin6.sin6_flowinfo = 0;
967 mapped = pxremap_outbound_ip6((ip6_addr_t *)&pcb->peer.sin6.sin6_addr,
968 ipX_2_ip6(&pcb->dst));
969 }
970 else {
971 pcb->peer.sin.sin_family = AF_INET;
972#if HAVE_SA_LEN
973 pcb->peer.sin.sin_len = sizeof(pcb->peer.sin);
974#endif
975 pcb->peer.sin.sin_port = htons(IPPROTO_ICMP);
976 mapped = pxremap_outbound_ip4((ip_addr_t *)&pcb->peer.sin.sin_addr,
977 ipX_2_ip(&pcb->dst));
978 }
979
980 if (mapped == PXREMAP_FAILED) {
981 free(pcb);
982 return NULL;
983 }
984 else {
985 pcb->is_mapped = (mapped == PXREMAP_MAPPED);
986 }
987
988 pcb->timeout_slot = pxping->timeout_slot;
989
990 sys_mutex_lock(&pxping->lock);
991 pxping_pcb_register(pxping, pcb);
992 sys_mutex_unlock(&pxping->lock);
993
994 DPRINTF(("ping %p: %R[ping_pcb] - created\n", pcb, pcb));
995
996 pxping_timer_needed(pxping);
997 }
998 else {
999 /* just bump up expiration timeout lazily */
1000 DPRINTF(("ping %p: %R[ping_pcb] - slot %d -> %d\n",
1001 pcb, pcb,
1002 (unsigned int)pcb->timeout_slot,
1003 (unsigned int)pxping->timeout_slot));
1004 pcb->timeout_slot = pxping->timeout_slot;
1005 }
1006
1007 return pcb;
1008}
1009
1010
1011/* GCC 12.2.1 complains that array subscript is partly outside
1012 * of array bounds in expansion of ipX_addr_cmp. */
1013#if RT_GNUC_PREREQ(12, 0)
1014# pragma GCC diagnostic push
1015# pragma GCC diagnostic ignored "-Warray-bounds"
1016#endif
1017/**
1018 * Called on pollmgr thread. Caller must do the locking since caller
1019 * is going to use the returned pcb, which needs to be protected from
1020 * being expired by pxping_timer() on lwip thread.
1021 */
1022static struct ping_pcb *
1023pxping_pcb_for_reply(struct pxping *pxping,
1024 int is_ipv6, ipX_addr_t *dst, u16_t host_id)
1025{
1026 struct ping_pcb *pcb;
1027
1028 for (pcb = pxping->pcbs; pcb != NULL; pcb = pcb->next) {
1029 if (pcb->host_id == host_id
1030 && pcb->is_ipv6 == is_ipv6
1031 /* XXX: allow broadcast pings? */
1032 && ipX_addr_cmp(is_ipv6, &pcb->dst, dst))
1033 {
1034 return pcb;
1035 }
1036 }
1037
1038 return NULL;
1039}
1040#if RT_GNUC_PREREQ(12, 0)
1041# pragma GCC diagnostic pop
1042#endif
1043
1044
1045static void
1046pxping_timer(void *arg)
1047{
1048 struct pxping *pxping = (struct pxping *)arg;
1049 struct ping_pcb **chain, *pcb;
1050
1051 pxping->timer_active = 0;
1052
1053 /*
1054 * New slot points to the list of pcbs to check for expiration.
1055 */
1056 LWIP_ASSERT1(pxping->timeout_slot < TIMEOUT);
1057 if (++pxping->timeout_slot == TIMEOUT) {
1058 pxping->timeout_slot = 0;
1059 }
1060
1061 chain = &pxping->timeout_list[pxping->timeout_slot];
1062 pcb = *chain;
1063
1064 /* protect from pollmgr concurrent reads */
1065 sys_mutex_lock(&pxping->lock);
1066
1067 while (pcb != NULL) {
1068 struct ping_pcb *xpcb = pcb;
1069 pcb = pcb->next_timeout;
1070
1071 if (xpcb->timeout_slot == pxping->timeout_slot) {
1072 /* expired */
1073 pxping_pcb_deregister(pxping, xpcb);
1074 pxping_pcb_delete(pxping, xpcb);
1075 }
1076 else {
1077 /*
1078 * If there was another request, we updated timeout_slot
1079 * but delayed actually moving the pcb until now.
1080 */
1081 pxping_timeout_del(pxping, xpcb); /* from current slot */
1082 pxping_timeout_add(pxping, xpcb); /* to new slot */
1083 }
1084 }
1085
1086 sys_mutex_unlock(&pxping->lock);
1087 pxping_timer_needed(pxping);
1088}
1089
1090
1091static void
1092pxping_timer_needed(struct pxping *pxping)
1093{
1094 if (!pxping->timer_active && pxping->pcbs != NULL) {
1095 pxping->timer_active = 1;
1096 sys_timeout(1 * 1000, pxping_timer, pxping);
1097 }
1098}
1099
1100
1101static int
1102pxping_pmgr_pump(struct pollmgr_handler *handler, SOCKET fd, int revents)
1103{
1104 struct pxping *pxping;
1105
1106 pxping = (struct pxping *)handler->data;
1107 LWIP_ASSERT1(fd == pxping->sock4 || fd == pxping->sock6);
1108
1109 if (revents & ~(POLLIN|POLLERR)) {
1110 DPRINTF0(("%s: unexpected revents 0x%x\n", __func__, revents));
1111 return POLLIN;
1112 }
1113
1114 if (revents & POLLERR) {
1115 int sockerr = -1;
1116 socklen_t optlen = (socklen_t)sizeof(sockerr);
1117 int status;
1118
1119 status = getsockopt(fd, SOL_SOCKET,
1120 SO_ERROR, (char *)&sockerr, &optlen);
1121 if (status < 0) {
1122 DPRINTF(("%s: sock %d: SO_ERROR failed: %R[sockerr]\n",
1123 __func__, fd, SOCKERRNO()));
1124 }
1125 else {
1126 DPRINTF(("%s: sock %d: %R[sockerr]\n",
1127 __func__, fd, sockerr));
1128 }
1129 }
1130
1131 if ((revents & POLLIN) == 0) {
1132 return POLLIN;
1133 }
1134
1135 if (fd == pxping->sock4) {
1136 pxping_pmgr_icmp4(pxping);
1137 }
1138 else /* fd == pxping->sock6 */ {
1139 pxping_pmgr_icmp6(pxping);
1140 }
1141
1142 return POLLIN;
1143}
1144
1145
1146/**
1147 * Process incoming ICMP message for the host.
1148 * NB: we will get a lot of spam here and have to sift through it.
1149 */
1150static void
1151pxping_pmgr_icmp4(struct pxping *pxping)
1152{
1153 struct sockaddr_in sin;
1154 socklen_t salen = sizeof(sin);
1155 ssize_t nread;
1156 struct ip_hdr *iph;
1157 struct icmp_echo_hdr *icmph;
1158 u16_t iplen, ipoff;
1159
1160 memset(&sin, 0, sizeof(sin));
1161
1162 /*
1163 * Reads from raw IPv4 sockets deliver complete IP datagrams with
1164 * IP header included.
1165 */
1166 nread = recvfrom(pxping->sock4, pollmgr_udpbuf, sizeof(pollmgr_udpbuf), 0,
1167 (struct sockaddr *)&sin, &salen);
1168 if (nread < 0) {
1169 DPRINTF(("%s: %R[sockerr]\n", __func__, SOCKERRNO()));
1170 return;
1171 }
1172
1173 if (nread < IP_HLEN) {
1174 DPRINTF2(("%s: read %d bytes, IP header truncated\n",
1175 __func__, (unsigned int)nread));
1176 return;
1177 }
1178
1179 iph = (struct ip_hdr *)pollmgr_udpbuf;
1180
1181 /* match version */
1182 if (IPH_V(iph) != 4) {
1183 DPRINTF2(("%s: unexpected IP version %d\n", __func__, IPH_V(iph)));
1184 return;
1185 }
1186
1187 /* no fragmentation */
1188 ipoff = IPH_OFFSET(iph);
1189#if defined(RT_OS_DARWIN)
1190 /* darwin reports IPH_OFFSET in host byte order */
1191 ipoff = htons(ipoff);
1192 IPH_OFFSET_SET(iph, ipoff);
1193#endif
1194 if ((ipoff & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
1195 DPRINTF2(("%s: dropping fragmented datagram (0x%04x)\n",
1196 __func__, ntohs(ipoff)));
1197 return;
1198 }
1199
1200 /* no options */
1201 if (IPH_HL(iph) * 4 != IP_HLEN) {
1202 DPRINTF2(("%s: dropping datagram with options (IP header length %d)\n",
1203 __func__, IPH_HL(iph) * 4));
1204 return;
1205 }
1206
1207 if (IPH_PROTO(iph) != IP_PROTO_ICMP) {
1208 DPRINTF2(("%s: unexpected protocol %d\n", __func__, IPH_PROTO(iph)));
1209 return;
1210 }
1211
1212 iplen = IPH_LEN(iph);
1213#if !defined(RT_OS_DARWIN)
1214 /* darwin reports IPH_LEN in host byte order */
1215 iplen = ntohs(iplen);
1216#endif
1217#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
1218 /* darwin and solaris change IPH_LEN to payload length only */
1219 iplen += IP_HLEN; /* we verified there are no options */
1220 IPH_LEN_SET(iph, htons(iplen));
1221#endif
1222 if (nread < iplen) {
1223 DPRINTF2(("%s: read %d bytes but total length is %d bytes\n",
1224 __func__, (unsigned int)nread, (unsigned int)iplen));
1225 return;
1226 }
1227
1228 if (iplen < IP_HLEN + ICMP_HLEN) {
1229 DPRINTF2(("%s: IP length %d bytes, ICMP header truncated\n",
1230 __func__, iplen));
1231 return;
1232 }
1233
1234 icmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + IP_HLEN);
1235 if (ICMPH_TYPE(icmph) == ICMP_ER) {
1236 pxping_pmgr_icmp4_echo(pxping, iplen, &sin);
1237 }
1238 else if (ICMPH_TYPE(icmph) == ICMP_DUR || ICMPH_TYPE(icmph) == ICMP_TE) {
1239 pxping_pmgr_icmp4_error(pxping, iplen, &sin);
1240 }
1241#if 1
1242 else {
1243 DPRINTF2(("%s: ignoring ICMP type %d\n", __func__, ICMPH_TYPE(icmph)));
1244 }
1245#endif
1246}
1247
1248
1249/**
1250 * Check if this incoming ICMP echo reply is for one of our pings and
1251 * forward it to the guest.
1252 */
1253static void
1254pxping_pmgr_icmp4_echo(struct pxping *pxping,
1255 u16_t iplen, struct sockaddr_in *peer)
1256{
1257 struct ip_hdr *iph;
1258 struct icmp_echo_hdr *icmph;
1259 u16_t id;
1260 ip_addr_t guest_ip, target_ip;
1261 int mapped;
1262 struct ping_pcb *pcb;
1263 u16_t guest_id;
1264 u16_t oipsum;
1265 u32_t sum;
1266 RT_NOREF(peer);
1267
1268 iph = (struct ip_hdr *)pollmgr_udpbuf;
1269 icmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + IP_HLEN);
1270
1271 id = icmph->id;
1272 DPRINTF(("<--- PING %RTnaipv4 id 0x%x seq %d\n",
1273 peer->sin_addr.s_addr, ntohs(id), ntohs(icmph->seqno)));
1274
1275 /*
1276 * Is this a reply to one of our pings?
1277 */
1278
1279 ip_addr_copy(target_ip, iph->src);
1280 mapped = pxremap_inbound_ip4(&target_ip, &target_ip);
1281 if (mapped == PXREMAP_FAILED) {
1282 return;
1283 }
1284 if (mapped == PXREMAP_ASIS && IPH_TTL(iph) == 1) {
1285 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1286 return;
1287 }
1288
1289 sys_mutex_lock(&pxping->lock);
1290 pcb = pxping_pcb_for_reply(pxping, 0, ip_2_ipX(&target_ip), id);
1291 if (pcb == NULL) {
1292 sys_mutex_unlock(&pxping->lock);
1293 DPRINTF2(("%s: no match\n", __func__));
1294 return;
1295 }
1296
1297 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1298
1299 /* save info before unlocking since pcb may expire */
1300 ip_addr_copy(guest_ip, *ipX_2_ip(&pcb->src));
1301 guest_id = pcb->guest_id;
1302
1303 sys_mutex_unlock(&pxping->lock);
1304
1305
1306 /*
1307 * Rewrite headers and forward to guest.
1308 */
1309
1310 /* rewrite ICMP echo header */
1311 sum = (u16_t)~icmph->chksum;
1312 sum += chksum_update_16(&icmph->id, guest_id);
1313 sum = FOLD_U32T(sum);
1314 icmph->chksum = ~sum;
1315
1316 /* rewrite IP header */
1317 oipsum = IPH_CHKSUM(iph);
1318 if (oipsum == 0) {
1319 /* Solaris doesn't compute checksum for local replies */
1320 ip_addr_copy(iph->dest, guest_ip);
1321 if (mapped == PXREMAP_MAPPED) {
1322 ip_addr_copy(iph->src, target_ip);
1323 }
1324 else {
1325 IPH_TTL_SET(iph, IPH_TTL(iph) - 1);
1326 }
1327 IPH_CHKSUM_SET(iph, inet_chksum(iph, ntohs(IPH_LEN(iph))));
1328 }
1329 else {
1330 sum = (u16_t)~oipsum;
1331 sum += chksum_update_32((u32_t *)&iph->dest,
1332 ip4_addr_get_u32(&guest_ip));
1333 if (mapped == PXREMAP_MAPPED) {
1334 sum += chksum_update_32((u32_t *)&iph->src,
1335 ip4_addr_get_u32(&target_ip));
1336 }
1337 else {
1338 IPH_TTL_SET(iph, IPH_TTL(iph) - 1);
1339 sum += PP_NTOHS(~0x0100);
1340 }
1341 sum = FOLD_U32T(sum);
1342 IPH_CHKSUM_SET(iph, ~sum);
1343 }
1344
1345 pxping_pmgr_forward_inbound(pxping, iplen);
1346}
1347
1348
1349/**
1350 * Check if this incoming ICMP error (destination unreachable or time
1351 * exceeded) is about one of our pings and forward it to the guest.
1352 */
1353static void
1354pxping_pmgr_icmp4_error(struct pxping *pxping,
1355 u16_t iplen, struct sockaddr_in *peer)
1356{
1357 struct ip_hdr *iph, *oiph;
1358 struct icmp_echo_hdr *icmph, *oicmph;
1359 u16_t oipoff, oiphlen, oiplen;
1360 u16_t id;
1361 ip_addr_t guest_ip, target_ip, error_ip;
1362 int target_mapped, error_mapped;
1363 struct ping_pcb *pcb;
1364 u16_t guest_id;
1365 u32_t sum;
1366 RT_NOREF(peer);
1367
1368 iph = (struct ip_hdr *)pollmgr_udpbuf;
1369 icmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + IP_HLEN);
1370
1371 /*
1372 * Inner IP datagram is not checked by the kernel and may be
1373 * anything, possibly malicious.
1374 */
1375
1376 oipoff = IP_HLEN + ICMP_HLEN;
1377 oiplen = iplen - oipoff; /* NB: truncated length, not IPH_LEN(oiph) */
1378 if (oiplen < IP_HLEN) {
1379 DPRINTF2(("%s: original datagram truncated to %d bytes\n",
1380 __func__, oiplen));
1381 }
1382
1383 /* IP header of the original message */
1384 oiph = (struct ip_hdr *)(pollmgr_udpbuf + oipoff);
1385
1386 /* match version */
1387 if (IPH_V(oiph) != 4) {
1388 DPRINTF2(("%s: unexpected IP version %d\n", __func__, IPH_V(oiph)));
1389 return;
1390 }
1391
1392 /* can't match fragments except the first one */
1393 if ((IPH_OFFSET(oiph) & PP_HTONS(IP_OFFMASK)) != 0) {
1394 DPRINTF2(("%s: ignoring fragment with offset %d\n",
1395 __func__, ntohs(IPH_OFFSET(oiph) & PP_HTONS(IP_OFFMASK))));
1396 return;
1397 }
1398
1399 if (IPH_PROTO(oiph) != IP_PROTO_ICMP) {
1400#if 0
1401 /* don't spam with every "destination unreachable" in the system */
1402 DPRINTF2(("%s: ignoring protocol %d\n", __func__, IPH_PROTO(oiph)));
1403#endif
1404 return;
1405 }
1406
1407 oiphlen = IPH_HL(oiph) * 4;
1408 if (oiplen < oiphlen + ICMP_HLEN) {
1409 DPRINTF2(("%s: original datagram truncated to %d bytes\n",
1410 __func__, oiplen));
1411 return;
1412 }
1413
1414 oicmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + oipoff + oiphlen);
1415 if (ICMPH_TYPE(oicmph) != ICMP_ECHO) {
1416 DPRINTF2(("%s: ignoring ICMP error for original ICMP type %d\n",
1417 __func__, ICMPH_TYPE(oicmph)));
1418 return;
1419 }
1420
1421 id = oicmph->id;
1422
1423 DPRINTF2(("%s: ping %RTnaipv4 id 0x%x seq %d",
1424 __func__, ip4_addr_get_u32(&oiph->dest), ntohs(id), ntohs(oicmph->seqno)));
1425 if (ICMPH_TYPE(icmph) == ICMP_DUR) {
1426 DPRINTF2((" unreachable (code %d)\n", ICMPH_CODE(icmph)));
1427 }
1428 else {
1429 DPRINTF2((" time exceeded\n"));
1430 }
1431
1432
1433 /*
1434 * Is the inner (failed) datagram one of our pings?
1435 */
1436
1437 ip_addr_copy(target_ip, oiph->dest); /* inner (failed) */
1438 target_mapped = pxremap_inbound_ip4(&target_ip, &target_ip);
1439 if (target_mapped == PXREMAP_FAILED) {
1440 return;
1441 }
1442
1443 sys_mutex_lock(&pxping->lock);
1444 pcb = pxping_pcb_for_reply(pxping, 0, ip_2_ipX(&target_ip), id);
1445 if (pcb == NULL) {
1446 sys_mutex_unlock(&pxping->lock);
1447 DPRINTF2(("%s: no match\n", __func__));
1448 return;
1449 }
1450
1451 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1452
1453 /* save info before unlocking since pcb may expire */
1454 ip_addr_copy(guest_ip, *ipX_2_ip(&pcb->src));
1455 guest_id = pcb->guest_id;
1456
1457 sys_mutex_unlock(&pxping->lock);
1458
1459
1460 /*
1461 * Rewrite both inner and outer headers and forward to guest.
1462 * Note that the checksum of the outer ICMP error message is
1463 * preserved by the changes we do to inner headers.
1464 */
1465
1466 ip_addr_copy(error_ip, iph->src); /* node that reports the error */
1467 error_mapped = pxremap_inbound_ip4(&error_ip, &error_ip);
1468 if (error_mapped == PXREMAP_FAILED) {
1469 return;
1470 }
1471 if (error_mapped == PXREMAP_ASIS && IPH_TTL(iph) == 1) {
1472 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1473 return;
1474 }
1475
1476 /* rewrite inner ICMP echo header */
1477 sum = (u16_t)~oicmph->chksum;
1478 sum += chksum_update_16(&oicmph->id, guest_id);
1479 sum = FOLD_U32T(sum);
1480 oicmph->chksum = ~sum;
1481
1482 /* rewrite inner IP header */
1483#if defined(RT_OS_DARWIN)
1484 /* darwin converts inner length to host byte order too */
1485 IPH_LEN_SET(oiph, htons(IPH_LEN(oiph)));
1486#endif
1487 sum = (u16_t)~IPH_CHKSUM(oiph);
1488 sum += chksum_update_32((u32_t *)&oiph->src, ip4_addr_get_u32(&guest_ip));
1489 if (target_mapped == PXREMAP_MAPPED) {
1490 sum += chksum_update_32((u32_t *)&oiph->dest, ip4_addr_get_u32(&target_ip));
1491 }
1492 sum = FOLD_U32T(sum);
1493 IPH_CHKSUM_SET(oiph, ~sum);
1494
1495 /* rewrite outer IP header */
1496 sum = (u16_t)~IPH_CHKSUM(iph);
1497 sum += chksum_update_32((u32_t *)&iph->dest, ip4_addr_get_u32(&guest_ip));
1498 if (error_mapped == PXREMAP_MAPPED) {
1499 sum += chksum_update_32((u32_t *)&iph->src, ip4_addr_get_u32(&error_ip));
1500 }
1501 else {
1502 IPH_TTL_SET(iph, IPH_TTL(iph) - 1);
1503 sum += PP_NTOHS(~0x0100);
1504 }
1505 sum = FOLD_U32T(sum);
1506 IPH_CHKSUM_SET(iph, ~sum);
1507
1508 pxping_pmgr_forward_inbound(pxping, iplen);
1509}
1510
1511
1512/**
1513 * Process incoming ICMPv6 message for the host.
1514 * NB: we will get a lot of spam here and have to sift through it.
1515 */
1516static void
1517pxping_pmgr_icmp6(struct pxping *pxping)
1518{
1519#ifndef RT_OS_WINDOWS
1520 struct msghdr mh;
1521 ssize_t nread;
1522#else
1523 WSAMSG mh;
1524 DWORD nread;
1525#endif
1526 IOVEC iov[1];
1527 static u8_t cmsgbuf[128];
1528 struct cmsghdr *cmh;
1529 struct sockaddr_in6 sin6;
1530 /* socklen_t salen = sizeof(sin6); - unused */
1531 struct icmp6_echo_hdr *icmph;
1532 struct in6_pktinfo *pktinfo;
1533 int hopl, tclass;
1534#ifdef RT_OS_WINDOWS
1535 int status;
1536#endif
1537
1538 /*
1539 * Reads from raw IPv6 sockets deliver only the payload. Full
1540 * headers are available via recvmsg(2)/cmsg(3).
1541 */
1542 IOVEC_SET_BASE(iov[0], pollmgr_udpbuf);
1543 IOVEC_SET_LEN(iov[0], sizeof(pollmgr_udpbuf));
1544
1545 memset(&mh, 0, sizeof(mh));
1546#ifndef RT_OS_WINDOWS
1547 mh.msg_name = &sin6;
1548 mh.msg_namelen = sizeof(sin6);
1549 mh.msg_iov = iov;
1550 mh.msg_iovlen = 1;
1551 mh.msg_control = cmsgbuf;
1552 mh.msg_controllen = sizeof(cmsgbuf);
1553 mh.msg_flags = 0;
1554
1555 nread = recvmsg(pxping->sock6, &mh, 0);
1556 if (nread < 0) {
1557 DPRINTF(("%s: %R[sockerr]\n", __func__, SOCKERRNO()));
1558 return;
1559 }
1560#else /* RT_OS_WINDOWS */
1561 mh.name = (LPSOCKADDR)&sin6;
1562 mh.namelen = sizeof(sin6);
1563 mh.lpBuffers = iov;
1564 mh.dwBufferCount = 1;
1565 mh.Control.buf = cmsgbuf;
1566 mh.Control.len = sizeof(cmsgbuf);
1567 mh.dwFlags = 0;
1568
1569 status = (*pxping->pfWSARecvMsg6)(pxping->sock6, &mh, &nread, NULL, NULL);
1570 if (status == SOCKET_ERROR) {
1571 DPRINTF2(("%s: error %d\n", __func__, WSAGetLastError()));
1572 return;
1573 }
1574#endif
1575
1576 icmph = (struct icmp6_echo_hdr *)pollmgr_udpbuf;
1577
1578 DPRINTF2(("%s: %RTnaipv6 ICMPv6: ", __func__, &sin6.sin6_addr));
1579
1580 if (icmph->type == ICMP6_TYPE_EREP) {
1581 DPRINTF2(("echo reply %04x %u\n",
1582 (unsigned int)icmph->id, (unsigned int)icmph->seqno));
1583 }
1584 else { /* XXX */
1585 if (icmph->type == ICMP6_TYPE_EREQ) {
1586 DPRINTF2(("echo request %04x %u\n",
1587 (unsigned int)icmph->id, (unsigned int)icmph->seqno));
1588 }
1589 else if (icmph->type == ICMP6_TYPE_DUR) {
1590 DPRINTF2(("destination unreachable\n"));
1591 }
1592 else if (icmph->type == ICMP6_TYPE_PTB) {
1593 DPRINTF2(("packet too big\n"));
1594 }
1595 else if (icmph->type == ICMP6_TYPE_TE) {
1596 DPRINTF2(("time exceeded\n"));
1597 }
1598 else if (icmph->type == ICMP6_TYPE_PP) {
1599 DPRINTF2(("parameter problem\n"));
1600 }
1601 else {
1602 DPRINTF2(("type %d len %u\n", icmph->type, (unsigned int)nread));
1603 }
1604
1605 if (icmph->type >= ICMP6_TYPE_EREQ) {
1606 return; /* informational message */
1607 }
1608 }
1609
1610 pktinfo = NULL;
1611 hopl = -1;
1612 tclass = -1;
1613 for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) {
1614 if (cmh->cmsg_len == 0)
1615 break;
1616
1617 if (cmh->cmsg_level == IPPROTO_IPV6
1618 && cmh->cmsg_type == IPV6_HOPLIMIT
1619 && cmh->cmsg_len == CMSG_LEN(sizeof(int)))
1620 {
1621 hopl = *(int *)CMSG_DATA(cmh);
1622 DPRINTF2(("hoplimit = %d\n", hopl));
1623 }
1624
1625 if (cmh->cmsg_level == IPPROTO_IPV6
1626 && cmh->cmsg_type == IPV6_PKTINFO
1627 && cmh->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
1628 {
1629 pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmh);
1630 DPRINTF2(("pktinfo found\n"));
1631 }
1632 }
1633
1634 if (pktinfo == NULL) {
1635 /*
1636 * ip6_output_if() doesn't do checksum for us so we need to
1637 * manually recompute it - for this we must know the
1638 * destination address of the pseudo-header that we will
1639 * rewrite with guest's address. (TODO: yeah, yeah, we can
1640 * compute it from scratch...)
1641 */
1642 DPRINTF2(("%s: unable to get pktinfo\n", __func__));
1643 return;
1644 }
1645
1646 if (hopl < 0) {
1647 hopl = LWIP_ICMP6_HL;
1648 }
1649
1650 if (icmph->type == ICMP6_TYPE_EREP) {
1651 pxping_pmgr_icmp6_echo(pxping,
1652 (ip6_addr_t *)&sin6.sin6_addr,
1653 (ip6_addr_t *)&pktinfo->ipi6_addr,
1654 hopl, tclass, (u16_t)nread);
1655 }
1656 else if (icmph->type < ICMP6_TYPE_EREQ) {
1657 pxping_pmgr_icmp6_error(pxping,
1658 (ip6_addr_t *)&sin6.sin6_addr,
1659 (ip6_addr_t *)&pktinfo->ipi6_addr,
1660 hopl, tclass, (u16_t)nread);
1661 }
1662}
1663
1664
1665/**
1666 * Check if this incoming ICMPv6 echo reply is for one of our pings
1667 * and forward it to the guest.
1668 */
1669static void
1670pxping_pmgr_icmp6_echo(struct pxping *pxping,
1671 ip6_addr_t *src, ip6_addr_t *dst,
1672 int hopl, int tclass, u16_t icmplen)
1673{
1674 struct icmp6_echo_hdr *icmph;
1675 ip6_addr_t guest_ip, target_ip;
1676 int mapped;
1677 struct ping_pcb *pcb;
1678 u16_t id, guest_id;
1679 u32_t sum;
1680
1681 ip6_addr_copy(target_ip, *src);
1682 mapped = pxremap_inbound_ip6(&target_ip, &target_ip);
1683 if (mapped == PXREMAP_FAILED) {
1684 return;
1685 }
1686 else if (mapped == PXREMAP_ASIS) {
1687 if (hopl == 1) {
1688 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1689 return;
1690 }
1691 --hopl;
1692 }
1693
1694 icmph = (struct icmp6_echo_hdr *)pollmgr_udpbuf;
1695 id = icmph->id;
1696
1697 sys_mutex_lock(&pxping->lock);
1698 pcb = pxping_pcb_for_reply(pxping, 1, ip6_2_ipX(&target_ip), id);
1699 if (pcb == NULL) {
1700 sys_mutex_unlock(&pxping->lock);
1701 DPRINTF2(("%s: no match\n", __func__));
1702 return;
1703 }
1704
1705 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1706
1707 /* save info before unlocking since pcb may expire */
1708 ip6_addr_copy(guest_ip, *ipX_2_ip6(&pcb->src));
1709 guest_id = pcb->guest_id;
1710
1711 sys_mutex_unlock(&pxping->lock);
1712
1713 /* rewrite ICMPv6 echo header */
1714 sum = (u16_t)~icmph->chksum;
1715 sum += chksum_update_16(&icmph->id, guest_id);
1716 sum += chksum_delta_ipv6(dst, &guest_ip); /* pseudo */
1717 if (mapped) {
1718 sum += chksum_delta_ipv6(src, &target_ip); /* pseudo */
1719 }
1720 sum = FOLD_U32T(sum);
1721 icmph->chksum = ~sum;
1722
1723 pxping_pmgr_forward_inbound6(pxping,
1724 &target_ip, /* echo reply src */
1725 &guest_ip, /* echo reply dst */
1726 hopl, tclass, icmplen);
1727}
1728
1729
1730/**
1731 * Check if this incoming ICMPv6 error is about one of our pings and
1732 * forward it to the guest.
1733 */
1734static void
1735pxping_pmgr_icmp6_error(struct pxping *pxping,
1736 ip6_addr_t *src, ip6_addr_t *dst,
1737 int hopl, int tclass, u16_t icmplen)
1738{
1739 struct icmp6_hdr *icmph;
1740 u8_t *bufptr;
1741 size_t buflen, hlen;
1742 int proto;
1743 struct ip6_hdr *oiph;
1744 struct icmp6_echo_hdr *oicmph;
1745 struct ping_pcb *pcb;
1746 ip6_addr_t guest_ip, target_ip, error_ip;
1747 int target_mapped, error_mapped;
1748 u16_t guest_id;
1749 u32_t sum;
1750
1751 icmph = (struct icmp6_hdr *)pollmgr_udpbuf;
1752
1753 /*
1754 * Inner IP datagram is not checked by the kernel and may be
1755 * anything, possibly malicious.
1756 */
1757 oiph = NULL;
1758 oicmph = NULL;
1759
1760 bufptr = pollmgr_udpbuf;
1761 buflen = icmplen;
1762
1763 hlen = sizeof(*icmph);
1764 proto = IP6_NEXTH_ENCAPS; /* i.e. IPv6, lwIP's name is unfortuate */
1765 for (;;) {
1766 if (hlen > buflen) {
1767 DPRINTF2(("truncated datagram inside ICMPv6 error message is too short\n"));
1768 return;
1769 }
1770 buflen -= hlen;
1771 bufptr += hlen;
1772
1773 if (proto == IP6_NEXTH_ENCAPS && oiph == NULL) { /* outermost IPv6 */
1774 oiph = (struct ip6_hdr *)bufptr;
1775 if (IP6H_V(oiph) != 6) {
1776 DPRINTF2(("%s: unexpected IP version %d\n", __func__, IP6H_V(oiph)));
1777 return;
1778 }
1779
1780 proto = IP6H_NEXTH(oiph);
1781 hlen = IP6_HLEN;
1782 }
1783 else if (proto == IP6_NEXTH_ICMP6) {
1784 oicmph = (struct icmp6_echo_hdr *)bufptr;
1785 break;
1786 }
1787 else if (proto == IP6_NEXTH_ROUTING
1788 || proto == IP6_NEXTH_HOPBYHOP
1789 || proto == IP6_NEXTH_DESTOPTS)
1790 {
1791 proto = bufptr[0];
1792 hlen = (bufptr[1] + 1) * 8;
1793 }
1794 else {
1795 DPRINTF2(("%s: stopping at protocol %d\n", __func__, proto));
1796 break;
1797 }
1798 }
1799
1800 if (oiph == NULL || oicmph == NULL) {
1801 return;
1802 }
1803
1804 if (buflen < sizeof(*oicmph)) {
1805 DPRINTF2(("%s: original ICMPv6 is truncated too short\n", __func__));
1806 return;
1807 }
1808
1809 if (oicmph->type != ICMP6_TYPE_EREQ) {
1810 DPRINTF2(("%s: ignoring original ICMPv6 type %d\n", __func__, oicmph->type));
1811 return;
1812 }
1813
1814 ip6_addr_copy(target_ip, oiph->dest); /* inner (failed) */
1815 target_mapped = pxremap_inbound_ip6(&target_ip, &target_ip);
1816 if (target_mapped == PXREMAP_FAILED) {
1817 return;
1818 }
1819
1820 sys_mutex_lock(&pxping->lock);
1821 pcb = pxping_pcb_for_reply(pxping, 1, ip6_2_ipX(&target_ip), oicmph->id);
1822 if (pcb == NULL) {
1823 sys_mutex_unlock(&pxping->lock);
1824 DPRINTF2(("%s: no match\n", __func__));
1825 return;
1826 }
1827
1828 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1829
1830 /* save info before unlocking since pcb may expire */
1831 ip6_addr_copy(guest_ip, *ipX_2_ip6(&pcb->src));
1832 guest_id = pcb->guest_id;
1833
1834 sys_mutex_unlock(&pxping->lock);
1835
1836
1837 /*
1838 * Rewrite inner and outer headers and forward to guest. Note
1839 * that IPv6 has no IP header checksum, but uses pseudo-header for
1840 * ICMPv6, so we update both in one go, adjusting ICMPv6 checksum
1841 * as we rewrite IP header.
1842 */
1843
1844 ip6_addr_copy(error_ip, *src); /* node that reports the error */
1845 error_mapped = pxremap_inbound_ip6(&error_ip, &error_ip);
1846 if (error_mapped == PXREMAP_FAILED) {
1847 return;
1848 }
1849 if (error_mapped == PXREMAP_ASIS && hopl == 1) {
1850 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1851 return;
1852 }
1853
1854 /* rewrite inner ICMPv6 echo header and inner IPv6 header */
1855 sum = (u16_t)~oicmph->chksum;
1856 sum += chksum_update_16(&oicmph->id, guest_id);
1857 sum += chksum_update_ipv6((ip6_addr_t *)&oiph->src, &guest_ip);
1858 if (target_mapped) {
1859 sum += chksum_delta_ipv6((ip6_addr_t *)&oiph->dest, &target_ip);
1860 }
1861 sum = FOLD_U32T(sum);
1862 oicmph->chksum = ~sum;
1863
1864 /* rewrite outer ICMPv6 error header */
1865 sum = (u16_t)~icmph->chksum;
1866 sum += chksum_delta_ipv6(dst, &guest_ip); /* pseudo */
1867 if (error_mapped) {
1868 sum += chksum_delta_ipv6(src, &error_ip); /* pseudo */
1869 }
1870 sum = FOLD_U32T(sum);
1871 icmph->chksum = ~sum;
1872
1873 pxping_pmgr_forward_inbound6(pxping,
1874 &error_ip, /* error src */
1875 &guest_ip, /* error dst */
1876 hopl, tclass, icmplen);
1877}
1878
1879
1880/**
1881 * Hand off ICMP datagram to the lwip thread where it will be
1882 * forwarded to the guest.
1883 *
1884 * We no longer need ping_pcb. The pcb may get expired on the lwip
1885 * thread, but we have already patched necessary information into the
1886 * datagram.
1887 */
1888static void
1889pxping_pmgr_forward_inbound(struct pxping *pxping, u16_t iplen)
1890{
1891 struct pbuf *p;
1892 struct ping_msg *msg;
1893 err_t error;
1894
1895 p = pbuf_alloc(PBUF_LINK, iplen, PBUF_RAM);
1896 if (p == NULL) {
1897 DPRINTF(("%s: pbuf_alloc(%d) failed\n",
1898 __func__, (unsigned int)iplen));
1899 return;
1900 }
1901
1902 error = pbuf_take(p, pollmgr_udpbuf, iplen);
1903 if (error != ERR_OK) {
1904 DPRINTF(("%s: pbuf_take(%d) failed\n",
1905 __func__, (unsigned int)iplen));
1906 pbuf_free(p);
1907 return;
1908 }
1909
1910 msg = (struct ping_msg *)malloc(sizeof(*msg));
1911 if (msg == NULL) {
1912 pbuf_free(p);
1913 return;
1914 }
1915
1916 msg->msg.type = TCPIP_MSG_CALLBACK_STATIC;
1917 msg->msg.sem = NULL;
1918 msg->msg.msg.cb.function = pxping_pcb_forward_inbound;
1919 msg->msg.msg.cb.ctx = (void *)msg;
1920
1921 msg->pxping = pxping;
1922 msg->p = p;
1923
1924 proxy_lwip_post(&msg->msg);
1925}
1926
1927
1928static void
1929pxping_pcb_forward_inbound(void *arg)
1930{
1931 struct ping_msg *msg = (struct ping_msg *)arg;
1932 err_t error;
1933
1934 LWIP_ASSERT1(msg != NULL);
1935 LWIP_ASSERT1(msg->pxping != NULL);
1936 LWIP_ASSERT1(msg->p != NULL);
1937
1938 error = ip_raw_output_if(msg->p, msg->pxping->netif);
1939 if (error != ERR_OK) {
1940 DPRINTF(("%s: ip_output_if: %s\n",
1941 __func__, proxy_lwip_strerr(error)));
1942 }
1943 pbuf_free(msg->p);
1944 free(msg);
1945}
1946
1947
1948static void
1949pxping_pmgr_forward_inbound6(struct pxping *pxping,
1950 ip6_addr_t *src, ip6_addr_t *dst,
1951 u8_t hopl, u8_t tclass,
1952 u16_t icmplen)
1953{
1954 struct pbuf *p;
1955 struct ping6_msg *msg;
1956
1957 err_t error;
1958
1959 p = pbuf_alloc(PBUF_IP, icmplen, PBUF_RAM);
1960 if (p == NULL) {
1961 DPRINTF(("%s: pbuf_alloc(%d) failed\n",
1962 __func__, (unsigned int)icmplen));
1963 return;
1964 }
1965
1966 error = pbuf_take(p, pollmgr_udpbuf, icmplen);
1967 if (error != ERR_OK) {
1968 DPRINTF(("%s: pbuf_take(%d) failed\n",
1969 __func__, (unsigned int)icmplen));
1970 pbuf_free(p);
1971 return;
1972 }
1973
1974 msg = (struct ping6_msg *)malloc(sizeof(*msg));
1975 if (msg == NULL) {
1976 pbuf_free(p);
1977 return;
1978 }
1979
1980 msg->msg.type = TCPIP_MSG_CALLBACK_STATIC;
1981 msg->msg.sem = NULL;
1982 msg->msg.msg.cb.function = pxping_pcb_forward_inbound6;
1983 msg->msg.msg.cb.ctx = (void *)msg;
1984
1985 msg->pxping = pxping;
1986 msg->p = p;
1987 ip6_addr_copy(msg->src, *src);
1988 ip6_addr_copy(msg->dst, *dst);
1989 msg->hopl = hopl;
1990 msg->tclass = tclass;
1991
1992 proxy_lwip_post(&msg->msg);
1993}
1994
1995
1996static void
1997pxping_pcb_forward_inbound6(void *arg)
1998{
1999 struct ping6_msg *msg = (struct ping6_msg *)arg;
2000 err_t error;
2001
2002 LWIP_ASSERT1(msg != NULL);
2003 LWIP_ASSERT1(msg->pxping != NULL);
2004 LWIP_ASSERT1(msg->p != NULL);
2005
2006 error = ip6_output_if(msg->p,
2007 &msg->src, &msg->dst, msg->hopl, msg->tclass,
2008 IP6_NEXTH_ICMP6, msg->pxping->netif);
2009 if (error != ERR_OK) {
2010 DPRINTF(("%s: ip6_output_if: %s\n",
2011 __func__, proxy_lwip_strerr(error)));
2012 }
2013 pbuf_free(msg->p);
2014 free(msg);
2015}
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