VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/pxping.c@ 96454

Last change on this file since 96454 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.0 KB
Line 
1/* $Id: pxping.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * NAT Network - ping proxy, raw sockets version.
4 */
5
6/*
7 * Copyright (C) 2013-2022 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, seq;
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 seq = icmph->seqno;
698
699 pcb = pxping_pcb_for_request(pxping, 1,
700 ipX_current_src_addr(),
701 ipX_current_dest_addr(),
702 id);
703 if (pcb == NULL) {
704 pbuf_free(p);
705 return;
706 }
707
708 DPRINTF(("ping %p: %R[ping_pcb] seq %d len %u hopl %d\n",
709 pcb, pcb,
710 ntohs(seq), (unsigned int)p->tot_len,
711 IP6H_HOPLIM(iph)));
712
713 hopl = IP6H_HOPLIM(iph);
714 if (!pcb->is_mapped) {
715 if (hopl == 1) {
716 status = pbuf_header(p, iphlen); /* back to IP header */
717 if (RT_LIKELY(status == 0)) {
718 icmp6_time_exceeded(p, ICMP6_TE_HL);
719 }
720 pbuf_free(p);
721 return;
722 }
723 --hopl;
724 }
725
726 /*
727 * Rewrite ICMPv6 echo header. We don't need to recompute the
728 * checksum since, unlike IPv4, checksum includes pseudo-header.
729 * OS computes checksum for us on send() since it needs to select
730 * source address.
731 */
732 icmph->id = pcb->host_id;
733
734 /** @todo use control messages to save a syscall? */
735 if (hopl != pxping->hopl) {
736 status = setsockopt(pxping->sock6, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
737 (char *)&hopl, sizeof(hopl));
738 if (status == 0) {
739 pxping->hopl = hopl;
740 }
741 else {
742 DPRINTF(("IPV6_HOPLIMIT: %R[sockerr]\n", SOCKERRNO()));
743 }
744 }
745
746 status = proxy_sendto(pxping->sock6, p,
747 &pcb->peer.sin6, sizeof(pcb->peer.sin6));
748 if (status != 0) {
749 int error = -status;
750 DPRINTF(("%s: sendto: %R[sockerr]\n", __func__, error));
751
752 status = pbuf_header(p, iphlen); /* back to IP header */
753 if (RT_UNLIKELY(status != 0)) {
754 pbuf_free(p);
755 return;
756 }
757
758 /* restore original ICMP header */
759 icmph->id = pcb->guest_id;
760
761 switch (error) {
762 case EACCES:
763 icmp6_dest_unreach(p, ICMP6_DUR_PROHIBITED);
764 break;
765
766#ifdef ENONET
767 case ENONET:
768#endif
769 case ENETDOWN:
770 case ENETUNREACH:
771 case EHOSTDOWN:
772 case EHOSTUNREACH:
773 icmp6_dest_unreach(p, ICMP6_DUR_NO_ROUTE);
774 break;
775 }
776 }
777
778 pbuf_free(p);
779}
780
781
782/**
783 * Formatter for %R[ping_pcb].
784 */
785static DECLCALLBACK(size_t)
786pxping_pcb_rtstrfmt(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
787 const char *pszType, const void *pvValue,
788 int cchWidth, int cchPrecision, unsigned int fFlags,
789 void *pvUser)
790{
791 const struct ping_pcb *pcb = (const struct ping_pcb *)pvValue;
792 size_t cb = 0;
793
794 NOREF(cchWidth);
795 NOREF(cchPrecision);
796 NOREF(fFlags);
797 NOREF(pvUser);
798
799 AssertReturn(strcmp(pszType, "ping_pcb") == 0, 0);
800
801 if (pcb == NULL) {
802 return RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL, "(null)");
803 }
804
805 /* XXX: %RTnaipv4 takes the value, but %RTnaipv6 takes the pointer */
806 if (pcb->is_ipv6) {
807 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
808 "%RTnaipv6 -> %RTnaipv6", &pcb->src, &pcb->dst);
809 if (pcb->is_mapped) {
810 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
811 " (%RTnaipv6)", &pcb->peer.sin6.sin6_addr);
812 }
813 }
814 else {
815 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
816 "%RTnaipv4 -> %RTnaipv4",
817 ip4_addr_get_u32(ipX_2_ip(&pcb->src)),
818 ip4_addr_get_u32(ipX_2_ip(&pcb->dst)));
819 if (pcb->is_mapped) {
820 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
821 " (%RTnaipv4)", pcb->peer.sin.sin_addr.s_addr);
822 }
823 }
824
825 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, NULL,
826 " id %04x->%04x", ntohs(pcb->guest_id), ntohs(pcb->host_id));
827
828 return cb;
829}
830
831
832static struct ping_pcb *
833pxping_pcb_allocate(struct pxping *pxping)
834{
835 struct ping_pcb *pcb;
836
837 if (pxping->npcbs >= PXPING_MAX_PCBS) {
838 return NULL;
839 }
840
841 pcb = (struct ping_pcb *)malloc(sizeof(*pcb));
842 if (pcb == NULL) {
843 return NULL;
844 }
845
846 ++pxping->npcbs;
847 return pcb;
848}
849
850
851static void
852pxping_pcb_delete(struct pxping *pxping, struct ping_pcb *pcb)
853{
854 LWIP_ASSERT1(pxping->npcbs > 0);
855 LWIP_ASSERT1(pcb->next == NULL);
856 LWIP_ASSERT1(pcb->pprev_timeout == NULL);
857
858 DPRINTF(("%s: ping %p\n", __func__, (void *)pcb));
859
860 --pxping->npcbs;
861 free(pcb);
862}
863
864
865static void
866pxping_timeout_add(struct pxping *pxping, struct ping_pcb *pcb)
867{
868 struct ping_pcb **chain;
869
870 LWIP_ASSERT1(pcb->pprev_timeout == NULL);
871
872 chain = &pxping->timeout_list[pcb->timeout_slot];
873 if ((pcb->next_timeout = *chain) != NULL) {
874 (*chain)->pprev_timeout = &pcb->next_timeout;
875 }
876 *chain = pcb;
877 pcb->pprev_timeout = chain;
878}
879
880
881static void
882pxping_timeout_del(struct pxping *pxping, struct ping_pcb *pcb)
883{
884 LWIP_UNUSED_ARG(pxping);
885
886 LWIP_ASSERT1(pcb->pprev_timeout != NULL);
887 if (pcb->next_timeout != NULL) {
888 pcb->next_timeout->pprev_timeout = pcb->pprev_timeout;
889 }
890 *pcb->pprev_timeout = pcb->next_timeout;
891 pcb->pprev_timeout = NULL;
892 pcb->next_timeout = NULL;
893}
894
895
896static void
897pxping_pcb_register(struct pxping *pxping, struct ping_pcb *pcb)
898{
899 pcb->next = pxping->pcbs;
900 pxping->pcbs = pcb;
901
902 pxping_timeout_add(pxping, pcb);
903}
904
905
906static void
907pxping_pcb_deregister(struct pxping *pxping, struct ping_pcb *pcb)
908{
909 struct ping_pcb **p;
910
911 for (p = &pxping->pcbs; *p != NULL; p = &(*p)->next) {
912 if (*p == pcb) {
913 *p = pcb->next;
914 pcb->next = NULL;
915 break;
916 }
917 }
918
919 pxping_timeout_del(pxping, pcb);
920}
921
922
923static struct ping_pcb *
924pxping_pcb_for_request(struct pxping *pxping,
925 int is_ipv6, ipX_addr_t *src, ipX_addr_t *dst,
926 u16_t guest_id)
927{
928 struct ping_pcb *pcb;
929
930 /* on lwip thread, so no concurrent updates */
931 for (pcb = pxping->pcbs; pcb != NULL; pcb = pcb->next) {
932 if (pcb->guest_id == guest_id
933 && pcb->is_ipv6 == is_ipv6
934 && ipX_addr_cmp(is_ipv6, &pcb->dst, dst)
935 && ipX_addr_cmp(is_ipv6, &pcb->src, src))
936 {
937 break;
938 }
939 }
940
941 if (pcb == NULL) {
942 int mapped;
943
944 pcb = pxping_pcb_allocate(pxping);
945 if (pcb == NULL) {
946 return NULL;
947 }
948
949 pcb->is_ipv6 = is_ipv6;
950 ipX_addr_copy(is_ipv6, pcb->src, *src);
951 ipX_addr_copy(is_ipv6, pcb->dst, *dst);
952
953 pcb->guest_id = guest_id;
954#ifdef RT_OS_WINDOWS
955# define random() (rand())
956#endif
957 pcb->host_id = random() & 0xffffUL;
958
959 pcb->pprev_timeout = NULL;
960 pcb->next_timeout = NULL;
961
962 if (is_ipv6) {
963 pcb->peer.sin6.sin6_family = AF_INET6;
964#if HAVE_SA_LEN
965 pcb->peer.sin6.sin6_len = sizeof(pcb->peer.sin6);
966#endif
967 pcb->peer.sin6.sin6_port = htons(IPPROTO_ICMPV6);
968 pcb->peer.sin6.sin6_flowinfo = 0;
969 mapped = pxremap_outbound_ip6((ip6_addr_t *)&pcb->peer.sin6.sin6_addr,
970 ipX_2_ip6(&pcb->dst));
971 }
972 else {
973 pcb->peer.sin.sin_family = AF_INET;
974#if HAVE_SA_LEN
975 pcb->peer.sin.sin_len = sizeof(pcb->peer.sin);
976#endif
977 pcb->peer.sin.sin_port = htons(IPPROTO_ICMP);
978 mapped = pxremap_outbound_ip4((ip_addr_t *)&pcb->peer.sin.sin_addr,
979 ipX_2_ip(&pcb->dst));
980 }
981
982 if (mapped == PXREMAP_FAILED) {
983 free(pcb);
984 return NULL;
985 }
986 else {
987 pcb->is_mapped = (mapped == PXREMAP_MAPPED);
988 }
989
990 pcb->timeout_slot = pxping->timeout_slot;
991
992 sys_mutex_lock(&pxping->lock);
993 pxping_pcb_register(pxping, pcb);
994 sys_mutex_unlock(&pxping->lock);
995
996 DPRINTF(("ping %p: %R[ping_pcb] - created\n", pcb, pcb));
997
998 pxping_timer_needed(pxping);
999 }
1000 else {
1001 /* just bump up expiration timeout lazily */
1002 DPRINTF(("ping %p: %R[ping_pcb] - slot %d -> %d\n",
1003 pcb, pcb,
1004 (unsigned int)pcb->timeout_slot,
1005 (unsigned int)pxping->timeout_slot));
1006 pcb->timeout_slot = pxping->timeout_slot;
1007 }
1008
1009 return pcb;
1010}
1011
1012
1013/**
1014 * Called on pollmgr thread. Caller must do the locking since caller
1015 * is going to use the returned pcb, which needs to be protected from
1016 * being expired by pxping_timer() on lwip thread.
1017 */
1018static struct ping_pcb *
1019pxping_pcb_for_reply(struct pxping *pxping,
1020 int is_ipv6, ipX_addr_t *dst, u16_t host_id)
1021{
1022 struct ping_pcb *pcb;
1023
1024 for (pcb = pxping->pcbs; pcb != NULL; pcb = pcb->next) {
1025 if (pcb->host_id == host_id
1026 && pcb->is_ipv6 == is_ipv6
1027 /* XXX: allow broadcast pings? */
1028 && ipX_addr_cmp(is_ipv6, &pcb->dst, dst))
1029 {
1030 return pcb;
1031 }
1032 }
1033
1034 return NULL;
1035}
1036
1037
1038static void
1039pxping_timer(void *arg)
1040{
1041 struct pxping *pxping = (struct pxping *)arg;
1042 struct ping_pcb **chain, *pcb;
1043
1044 pxping->timer_active = 0;
1045
1046 /*
1047 * New slot points to the list of pcbs to check for expiration.
1048 */
1049 LWIP_ASSERT1(pxping->timeout_slot < TIMEOUT);
1050 if (++pxping->timeout_slot == TIMEOUT) {
1051 pxping->timeout_slot = 0;
1052 }
1053
1054 chain = &pxping->timeout_list[pxping->timeout_slot];
1055 pcb = *chain;
1056
1057 /* protect from pollmgr concurrent reads */
1058 sys_mutex_lock(&pxping->lock);
1059
1060 while (pcb != NULL) {
1061 struct ping_pcb *xpcb = pcb;
1062 pcb = pcb->next_timeout;
1063
1064 if (xpcb->timeout_slot == pxping->timeout_slot) {
1065 /* expired */
1066 pxping_pcb_deregister(pxping, xpcb);
1067 pxping_pcb_delete(pxping, xpcb);
1068 }
1069 else {
1070 /*
1071 * If there was another request, we updated timeout_slot
1072 * but delayed actually moving the pcb until now.
1073 */
1074 pxping_timeout_del(pxping, xpcb); /* from current slot */
1075 pxping_timeout_add(pxping, xpcb); /* to new slot */
1076 }
1077 }
1078
1079 sys_mutex_unlock(&pxping->lock);
1080 pxping_timer_needed(pxping);
1081}
1082
1083
1084static void
1085pxping_timer_needed(struct pxping *pxping)
1086{
1087 if (!pxping->timer_active && pxping->pcbs != NULL) {
1088 pxping->timer_active = 1;
1089 sys_timeout(1 * 1000, pxping_timer, pxping);
1090 }
1091}
1092
1093
1094static int
1095pxping_pmgr_pump(struct pollmgr_handler *handler, SOCKET fd, int revents)
1096{
1097 struct pxping *pxping;
1098
1099 pxping = (struct pxping *)handler->data;
1100 LWIP_ASSERT1(fd == pxping->sock4 || fd == pxping->sock6);
1101
1102 if (revents & ~(POLLIN|POLLERR)) {
1103 DPRINTF0(("%s: unexpected revents 0x%x\n", __func__, revents));
1104 return POLLIN;
1105 }
1106
1107 if (revents & POLLERR) {
1108 int sockerr = -1;
1109 socklen_t optlen = (socklen_t)sizeof(sockerr);
1110 int status;
1111
1112 status = getsockopt(fd, SOL_SOCKET,
1113 SO_ERROR, (char *)&sockerr, &optlen);
1114 if (status < 0) {
1115 DPRINTF(("%s: sock %d: SO_ERROR failed: %R[sockerr]\n",
1116 __func__, fd, SOCKERRNO()));
1117 }
1118 else {
1119 DPRINTF(("%s: sock %d: %R[sockerr]\n",
1120 __func__, fd, sockerr));
1121 }
1122 }
1123
1124 if ((revents & POLLIN) == 0) {
1125 return POLLIN;
1126 }
1127
1128 if (fd == pxping->sock4) {
1129 pxping_pmgr_icmp4(pxping);
1130 }
1131 else /* fd == pxping->sock6 */ {
1132 pxping_pmgr_icmp6(pxping);
1133 }
1134
1135 return POLLIN;
1136}
1137
1138
1139/**
1140 * Process incoming ICMP message for the host.
1141 * NB: we will get a lot of spam here and have to sift through it.
1142 */
1143static void
1144pxping_pmgr_icmp4(struct pxping *pxping)
1145{
1146 struct sockaddr_in sin;
1147 socklen_t salen = sizeof(sin);
1148 ssize_t nread;
1149 struct ip_hdr *iph;
1150 struct icmp_echo_hdr *icmph;
1151 u16_t iplen, ipoff;
1152
1153 memset(&sin, 0, sizeof(sin));
1154
1155 /*
1156 * Reads from raw IPv4 sockets deliver complete IP datagrams with
1157 * IP header included.
1158 */
1159 nread = recvfrom(pxping->sock4, pollmgr_udpbuf, sizeof(pollmgr_udpbuf), 0,
1160 (struct sockaddr *)&sin, &salen);
1161 if (nread < 0) {
1162 DPRINTF(("%s: %R[sockerr]\n", __func__, SOCKERRNO()));
1163 return;
1164 }
1165
1166 if (nread < IP_HLEN) {
1167 DPRINTF2(("%s: read %d bytes, IP header truncated\n",
1168 __func__, (unsigned int)nread));
1169 return;
1170 }
1171
1172 iph = (struct ip_hdr *)pollmgr_udpbuf;
1173
1174 /* match version */
1175 if (IPH_V(iph) != 4) {
1176 DPRINTF2(("%s: unexpected IP version %d\n", __func__, IPH_V(iph)));
1177 return;
1178 }
1179
1180 /* no fragmentation */
1181 ipoff = IPH_OFFSET(iph);
1182#if defined(RT_OS_DARWIN)
1183 /* darwin reports IPH_OFFSET in host byte order */
1184 ipoff = htons(ipoff);
1185 IPH_OFFSET_SET(iph, ipoff);
1186#endif
1187 if ((ipoff & PP_HTONS(IP_OFFMASK | IP_MF)) != 0) {
1188 DPRINTF2(("%s: dropping fragmented datagram (0x%04x)\n",
1189 __func__, ntohs(ipoff)));
1190 return;
1191 }
1192
1193 /* no options */
1194 if (IPH_HL(iph) * 4 != IP_HLEN) {
1195 DPRINTF2(("%s: dropping datagram with options (IP header length %d)\n",
1196 __func__, IPH_HL(iph) * 4));
1197 return;
1198 }
1199
1200 if (IPH_PROTO(iph) != IP_PROTO_ICMP) {
1201 DPRINTF2(("%s: unexpected protocol %d\n", __func__, IPH_PROTO(iph)));
1202 return;
1203 }
1204
1205 iplen = IPH_LEN(iph);
1206#if !defined(RT_OS_DARWIN)
1207 /* darwin reports IPH_LEN in host byte order */
1208 iplen = ntohs(iplen);
1209#endif
1210#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
1211 /* darwin and solaris change IPH_LEN to payload length only */
1212 iplen += IP_HLEN; /* we verified there are no options */
1213 IPH_LEN_SET(iph, htons(iplen));
1214#endif
1215 if (nread < iplen) {
1216 DPRINTF2(("%s: read %d bytes but total length is %d bytes\n",
1217 __func__, (unsigned int)nread, (unsigned int)iplen));
1218 return;
1219 }
1220
1221 if (iplen < IP_HLEN + ICMP_HLEN) {
1222 DPRINTF2(("%s: IP length %d bytes, ICMP header truncated\n",
1223 __func__, iplen));
1224 return;
1225 }
1226
1227 icmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + IP_HLEN);
1228 if (ICMPH_TYPE(icmph) == ICMP_ER) {
1229 pxping_pmgr_icmp4_echo(pxping, iplen, &sin);
1230 }
1231 else if (ICMPH_TYPE(icmph) == ICMP_DUR || ICMPH_TYPE(icmph) == ICMP_TE) {
1232 pxping_pmgr_icmp4_error(pxping, iplen, &sin);
1233 }
1234#if 1
1235 else {
1236 DPRINTF2(("%s: ignoring ICMP type %d\n", __func__, ICMPH_TYPE(icmph)));
1237 }
1238#endif
1239}
1240
1241
1242/**
1243 * Check if this incoming ICMP echo reply is for one of our pings and
1244 * forward it to the guest.
1245 */
1246static void
1247pxping_pmgr_icmp4_echo(struct pxping *pxping,
1248 u16_t iplen, struct sockaddr_in *peer)
1249{
1250 struct ip_hdr *iph;
1251 struct icmp_echo_hdr *icmph;
1252 u16_t id, seq;
1253 ip_addr_t guest_ip, target_ip;
1254 int mapped;
1255 struct ping_pcb *pcb;
1256 u16_t guest_id;
1257 u16_t oipsum;
1258 u32_t sum;
1259 RT_NOREF(peer);
1260
1261 iph = (struct ip_hdr *)pollmgr_udpbuf;
1262 icmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + IP_HLEN);
1263
1264 id = icmph->id;
1265 seq = icmph->seqno;
1266
1267 DPRINTF(("<--- PING %RTnaipv4 id 0x%x seq %d\n",
1268 peer->sin_addr.s_addr, ntohs(id), ntohs(seq)));
1269
1270 /*
1271 * Is this a reply to one of our pings?
1272 */
1273
1274 ip_addr_copy(target_ip, iph->src);
1275 mapped = pxremap_inbound_ip4(&target_ip, &target_ip);
1276 if (mapped == PXREMAP_FAILED) {
1277 return;
1278 }
1279 if (mapped == PXREMAP_ASIS && IPH_TTL(iph) == 1) {
1280 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1281 return;
1282 }
1283
1284 sys_mutex_lock(&pxping->lock);
1285 pcb = pxping_pcb_for_reply(pxping, 0, ip_2_ipX(&target_ip), id);
1286 if (pcb == NULL) {
1287 sys_mutex_unlock(&pxping->lock);
1288 DPRINTF2(("%s: no match\n", __func__));
1289 return;
1290 }
1291
1292 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1293
1294 /* save info before unlocking since pcb may expire */
1295 ip_addr_copy(guest_ip, *ipX_2_ip(&pcb->src));
1296 guest_id = pcb->guest_id;
1297
1298 sys_mutex_unlock(&pxping->lock);
1299
1300
1301 /*
1302 * Rewrite headers and forward to guest.
1303 */
1304
1305 /* rewrite ICMP echo header */
1306 sum = (u16_t)~icmph->chksum;
1307 sum += chksum_update_16(&icmph->id, guest_id);
1308 sum = FOLD_U32T(sum);
1309 icmph->chksum = ~sum;
1310
1311 /* rewrite IP header */
1312 oipsum = IPH_CHKSUM(iph);
1313 if (oipsum == 0) {
1314 /* Solaris doesn't compute checksum for local replies */
1315 ip_addr_copy(iph->dest, guest_ip);
1316 if (mapped == PXREMAP_MAPPED) {
1317 ip_addr_copy(iph->src, target_ip);
1318 }
1319 else {
1320 IPH_TTL_SET(iph, IPH_TTL(iph) - 1);
1321 }
1322 IPH_CHKSUM_SET(iph, inet_chksum(iph, ntohs(IPH_LEN(iph))));
1323 }
1324 else {
1325 sum = (u16_t)~oipsum;
1326 sum += chksum_update_32((u32_t *)&iph->dest,
1327 ip4_addr_get_u32(&guest_ip));
1328 if (mapped == PXREMAP_MAPPED) {
1329 sum += chksum_update_32((u32_t *)&iph->src,
1330 ip4_addr_get_u32(&target_ip));
1331 }
1332 else {
1333 IPH_TTL_SET(iph, IPH_TTL(iph) - 1);
1334 sum += PP_NTOHS(~0x0100);
1335 }
1336 sum = FOLD_U32T(sum);
1337 IPH_CHKSUM_SET(iph, ~sum);
1338 }
1339
1340 pxping_pmgr_forward_inbound(pxping, iplen);
1341}
1342
1343
1344/**
1345 * Check if this incoming ICMP error (destination unreachable or time
1346 * exceeded) is about one of our pings and forward it to the guest.
1347 */
1348static void
1349pxping_pmgr_icmp4_error(struct pxping *pxping,
1350 u16_t iplen, struct sockaddr_in *peer)
1351{
1352 struct ip_hdr *iph, *oiph;
1353 struct icmp_echo_hdr *icmph, *oicmph;
1354 u16_t oipoff, oiphlen, oiplen;
1355 u16_t id, seq;
1356 ip_addr_t guest_ip, target_ip, error_ip;
1357 int target_mapped, error_mapped;
1358 struct ping_pcb *pcb;
1359 u16_t guest_id;
1360 u32_t sum;
1361 RT_NOREF(peer);
1362
1363 iph = (struct ip_hdr *)pollmgr_udpbuf;
1364 icmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + IP_HLEN);
1365
1366 /*
1367 * Inner IP datagram is not checked by the kernel and may be
1368 * anything, possibly malicious.
1369 */
1370
1371 oipoff = IP_HLEN + ICMP_HLEN;
1372 oiplen = iplen - oipoff; /* NB: truncated length, not IPH_LEN(oiph) */
1373 if (oiplen < IP_HLEN) {
1374 DPRINTF2(("%s: original datagram truncated to %d bytes\n",
1375 __func__, oiplen));
1376 }
1377
1378 /* IP header of the original message */
1379 oiph = (struct ip_hdr *)(pollmgr_udpbuf + oipoff);
1380
1381 /* match version */
1382 if (IPH_V(oiph) != 4) {
1383 DPRINTF2(("%s: unexpected IP version %d\n", __func__, IPH_V(oiph)));
1384 return;
1385 }
1386
1387 /* can't match fragments except the first one */
1388 if ((IPH_OFFSET(oiph) & PP_HTONS(IP_OFFMASK)) != 0) {
1389 DPRINTF2(("%s: ignoring fragment with offset %d\n",
1390 __func__, ntohs(IPH_OFFSET(oiph) & PP_HTONS(IP_OFFMASK))));
1391 return;
1392 }
1393
1394 if (IPH_PROTO(oiph) != IP_PROTO_ICMP) {
1395#if 0
1396 /* don't spam with every "destination unreachable" in the system */
1397 DPRINTF2(("%s: ignoring protocol %d\n", __func__, IPH_PROTO(oiph)));
1398#endif
1399 return;
1400 }
1401
1402 oiphlen = IPH_HL(oiph) * 4;
1403 if (oiplen < oiphlen + ICMP_HLEN) {
1404 DPRINTF2(("%s: original datagram truncated to %d bytes\n",
1405 __func__, oiplen));
1406 return;
1407 }
1408
1409 oicmph = (struct icmp_echo_hdr *)(pollmgr_udpbuf + oipoff + oiphlen);
1410 if (ICMPH_TYPE(oicmph) != ICMP_ECHO) {
1411 DPRINTF2(("%s: ignoring ICMP error for original ICMP type %d\n",
1412 __func__, ICMPH_TYPE(oicmph)));
1413 return;
1414 }
1415
1416 id = oicmph->id;
1417 seq = oicmph->seqno;
1418
1419 DPRINTF2(("%s: ping %RTnaipv4 id 0x%x seq %d",
1420 __func__, ip4_addr_get_u32(&oiph->dest), ntohs(id), ntohs(seq)));
1421 if (ICMPH_TYPE(icmph) == ICMP_DUR) {
1422 DPRINTF2((" unreachable (code %d)\n", ICMPH_CODE(icmph)));
1423 }
1424 else {
1425 DPRINTF2((" time exceeded\n"));
1426 }
1427
1428
1429 /*
1430 * Is the inner (failed) datagram one of our pings?
1431 */
1432
1433 ip_addr_copy(target_ip, oiph->dest); /* inner (failed) */
1434 target_mapped = pxremap_inbound_ip4(&target_ip, &target_ip);
1435 if (target_mapped == PXREMAP_FAILED) {
1436 return;
1437 }
1438
1439 sys_mutex_lock(&pxping->lock);
1440 pcb = pxping_pcb_for_reply(pxping, 0, ip_2_ipX(&target_ip), id);
1441 if (pcb == NULL) {
1442 sys_mutex_unlock(&pxping->lock);
1443 DPRINTF2(("%s: no match\n", __func__));
1444 return;
1445 }
1446
1447 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1448
1449 /* save info before unlocking since pcb may expire */
1450 ip_addr_copy(guest_ip, *ipX_2_ip(&pcb->src));
1451 guest_id = pcb->guest_id;
1452
1453 sys_mutex_unlock(&pxping->lock);
1454
1455
1456 /*
1457 * Rewrite both inner and outer headers and forward to guest.
1458 * Note that the checksum of the outer ICMP error message is
1459 * preserved by the changes we do to inner headers.
1460 */
1461
1462 ip_addr_copy(error_ip, iph->src); /* node that reports the error */
1463 error_mapped = pxremap_inbound_ip4(&error_ip, &error_ip);
1464 if (error_mapped == PXREMAP_FAILED) {
1465 return;
1466 }
1467 if (error_mapped == PXREMAP_ASIS && IPH_TTL(iph) == 1) {
1468 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1469 return;
1470 }
1471
1472 /* rewrite inner ICMP echo header */
1473 sum = (u16_t)~oicmph->chksum;
1474 sum += chksum_update_16(&oicmph->id, guest_id);
1475 sum = FOLD_U32T(sum);
1476 oicmph->chksum = ~sum;
1477
1478 /* rewrite inner IP header */
1479#if defined(RT_OS_DARWIN)
1480 /* darwin converts inner length to host byte order too */
1481 IPH_LEN_SET(oiph, htons(IPH_LEN(oiph)));
1482#endif
1483 sum = (u16_t)~IPH_CHKSUM(oiph);
1484 sum += chksum_update_32((u32_t *)&oiph->src, ip4_addr_get_u32(&guest_ip));
1485 if (target_mapped == PXREMAP_MAPPED) {
1486 sum += chksum_update_32((u32_t *)&oiph->dest, ip4_addr_get_u32(&target_ip));
1487 }
1488 sum = FOLD_U32T(sum);
1489 IPH_CHKSUM_SET(oiph, ~sum);
1490
1491 /* rewrite outer IP header */
1492 sum = (u16_t)~IPH_CHKSUM(iph);
1493 sum += chksum_update_32((u32_t *)&iph->dest, ip4_addr_get_u32(&guest_ip));
1494 if (error_mapped == PXREMAP_MAPPED) {
1495 sum += chksum_update_32((u32_t *)&iph->src, ip4_addr_get_u32(&error_ip));
1496 }
1497 else {
1498 IPH_TTL_SET(iph, IPH_TTL(iph) - 1);
1499 sum += PP_NTOHS(~0x0100);
1500 }
1501 sum = FOLD_U32T(sum);
1502 IPH_CHKSUM_SET(iph, ~sum);
1503
1504 pxping_pmgr_forward_inbound(pxping, iplen);
1505}
1506
1507
1508/**
1509 * Process incoming ICMPv6 message for the host.
1510 * NB: we will get a lot of spam here and have to sift through it.
1511 */
1512static void
1513pxping_pmgr_icmp6(struct pxping *pxping)
1514{
1515#ifndef RT_OS_WINDOWS
1516 struct msghdr mh;
1517 ssize_t nread;
1518#else
1519 WSAMSG mh;
1520 DWORD nread;
1521#endif
1522 IOVEC iov[1];
1523 static u8_t cmsgbuf[128];
1524 struct cmsghdr *cmh;
1525 struct sockaddr_in6 sin6;
1526 /* socklen_t salen = sizeof(sin6); - unused */
1527 struct icmp6_echo_hdr *icmph;
1528 struct in6_pktinfo *pktinfo;
1529 int hopl, tclass;
1530#ifdef RT_OS_WINDOWS
1531 int status;
1532#endif
1533
1534 /*
1535 * Reads from raw IPv6 sockets deliver only the payload. Full
1536 * headers are available via recvmsg(2)/cmsg(3).
1537 */
1538 IOVEC_SET_BASE(iov[0], pollmgr_udpbuf);
1539 IOVEC_SET_LEN(iov[0], sizeof(pollmgr_udpbuf));
1540
1541 memset(&mh, 0, sizeof(mh));
1542#ifndef RT_OS_WINDOWS
1543 mh.msg_name = &sin6;
1544 mh.msg_namelen = sizeof(sin6);
1545 mh.msg_iov = iov;
1546 mh.msg_iovlen = 1;
1547 mh.msg_control = cmsgbuf;
1548 mh.msg_controllen = sizeof(cmsgbuf);
1549 mh.msg_flags = 0;
1550
1551 nread = recvmsg(pxping->sock6, &mh, 0);
1552 if (nread < 0) {
1553 DPRINTF(("%s: %R[sockerr]\n", __func__, SOCKERRNO()));
1554 return;
1555 }
1556#else /* RT_OS_WINDOWS */
1557 mh.name = (LPSOCKADDR)&sin6;
1558 mh.namelen = sizeof(sin6);
1559 mh.lpBuffers = iov;
1560 mh.dwBufferCount = 1;
1561 mh.Control.buf = cmsgbuf;
1562 mh.Control.len = sizeof(cmsgbuf);
1563 mh.dwFlags = 0;
1564
1565 status = (*pxping->pfWSARecvMsg6)(pxping->sock6, &mh, &nread, NULL, NULL);
1566 if (status == SOCKET_ERROR) {
1567 DPRINTF2(("%s: error %d\n", __func__, WSAGetLastError()));
1568 return;
1569 }
1570#endif
1571
1572 icmph = (struct icmp6_echo_hdr *)pollmgr_udpbuf;
1573
1574 DPRINTF2(("%s: %RTnaipv6 ICMPv6: ", __func__, &sin6.sin6_addr));
1575
1576 if (icmph->type == ICMP6_TYPE_EREP) {
1577 DPRINTF2(("echo reply %04x %u\n",
1578 (unsigned int)icmph->id, (unsigned int)icmph->seqno));
1579 }
1580 else { /* XXX */
1581 if (icmph->type == ICMP6_TYPE_EREQ) {
1582 DPRINTF2(("echo request %04x %u\n",
1583 (unsigned int)icmph->id, (unsigned int)icmph->seqno));
1584 }
1585 else if (icmph->type == ICMP6_TYPE_DUR) {
1586 DPRINTF2(("destination unreachable\n"));
1587 }
1588 else if (icmph->type == ICMP6_TYPE_PTB) {
1589 DPRINTF2(("packet too big\n"));
1590 }
1591 else if (icmph->type == ICMP6_TYPE_TE) {
1592 DPRINTF2(("time exceeded\n"));
1593 }
1594 else if (icmph->type == ICMP6_TYPE_PP) {
1595 DPRINTF2(("parameter problem\n"));
1596 }
1597 else {
1598 DPRINTF2(("type %d len %u\n", icmph->type, (unsigned int)nread));
1599 }
1600
1601 if (icmph->type >= ICMP6_TYPE_EREQ) {
1602 return; /* informational message */
1603 }
1604 }
1605
1606 pktinfo = NULL;
1607 hopl = -1;
1608 tclass = -1;
1609 for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) {
1610 if (cmh->cmsg_len == 0)
1611 break;
1612
1613 if (cmh->cmsg_level == IPPROTO_IPV6
1614 && cmh->cmsg_type == IPV6_HOPLIMIT
1615 && cmh->cmsg_len == CMSG_LEN(sizeof(int)))
1616 {
1617 hopl = *(int *)CMSG_DATA(cmh);
1618 DPRINTF2(("hoplimit = %d\n", hopl));
1619 }
1620
1621 if (cmh->cmsg_level == IPPROTO_IPV6
1622 && cmh->cmsg_type == IPV6_PKTINFO
1623 && cmh->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo)))
1624 {
1625 pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmh);
1626 DPRINTF2(("pktinfo found\n"));
1627 }
1628 }
1629
1630 if (pktinfo == NULL) {
1631 /*
1632 * ip6_output_if() doesn't do checksum for us so we need to
1633 * manually recompute it - for this we must know the
1634 * destination address of the pseudo-header that we will
1635 * rewrite with guest's address. (TODO: yeah, yeah, we can
1636 * compute it from scratch...)
1637 */
1638 DPRINTF2(("%s: unable to get pktinfo\n", __func__));
1639 return;
1640 }
1641
1642 if (hopl < 0) {
1643 hopl = LWIP_ICMP6_HL;
1644 }
1645
1646 if (icmph->type == ICMP6_TYPE_EREP) {
1647 pxping_pmgr_icmp6_echo(pxping,
1648 (ip6_addr_t *)&sin6.sin6_addr,
1649 (ip6_addr_t *)&pktinfo->ipi6_addr,
1650 hopl, tclass, (u16_t)nread);
1651 }
1652 else if (icmph->type < ICMP6_TYPE_EREQ) {
1653 pxping_pmgr_icmp6_error(pxping,
1654 (ip6_addr_t *)&sin6.sin6_addr,
1655 (ip6_addr_t *)&pktinfo->ipi6_addr,
1656 hopl, tclass, (u16_t)nread);
1657 }
1658}
1659
1660
1661/**
1662 * Check if this incoming ICMPv6 echo reply is for one of our pings
1663 * and forward it to the guest.
1664 */
1665static void
1666pxping_pmgr_icmp6_echo(struct pxping *pxping,
1667 ip6_addr_t *src, ip6_addr_t *dst,
1668 int hopl, int tclass, u16_t icmplen)
1669{
1670 struct icmp6_echo_hdr *icmph;
1671 ip6_addr_t guest_ip, target_ip;
1672 int mapped;
1673 struct ping_pcb *pcb;
1674 u16_t id, guest_id;
1675 u32_t sum;
1676
1677 ip6_addr_copy(target_ip, *src);
1678 mapped = pxremap_inbound_ip6(&target_ip, &target_ip);
1679 if (mapped == PXREMAP_FAILED) {
1680 return;
1681 }
1682 else if (mapped == PXREMAP_ASIS) {
1683 if (hopl == 1) {
1684 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1685 return;
1686 }
1687 --hopl;
1688 }
1689
1690 icmph = (struct icmp6_echo_hdr *)pollmgr_udpbuf;
1691 id = icmph->id;
1692
1693 sys_mutex_lock(&pxping->lock);
1694 pcb = pxping_pcb_for_reply(pxping, 1, ip6_2_ipX(&target_ip), id);
1695 if (pcb == NULL) {
1696 sys_mutex_unlock(&pxping->lock);
1697 DPRINTF2(("%s: no match\n", __func__));
1698 return;
1699 }
1700
1701 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1702
1703 /* save info before unlocking since pcb may expire */
1704 ip6_addr_copy(guest_ip, *ipX_2_ip6(&pcb->src));
1705 guest_id = pcb->guest_id;
1706
1707 sys_mutex_unlock(&pxping->lock);
1708
1709 /* rewrite ICMPv6 echo header */
1710 sum = (u16_t)~icmph->chksum;
1711 sum += chksum_update_16(&icmph->id, guest_id);
1712 sum += chksum_delta_ipv6(dst, &guest_ip); /* pseudo */
1713 if (mapped) {
1714 sum += chksum_delta_ipv6(src, &target_ip); /* pseudo */
1715 }
1716 sum = FOLD_U32T(sum);
1717 icmph->chksum = ~sum;
1718
1719 pxping_pmgr_forward_inbound6(pxping,
1720 &target_ip, /* echo reply src */
1721 &guest_ip, /* echo reply dst */
1722 hopl, tclass, icmplen);
1723}
1724
1725
1726/**
1727 * Check if this incoming ICMPv6 error is about one of our pings and
1728 * forward it to the guest.
1729 */
1730static void
1731pxping_pmgr_icmp6_error(struct pxping *pxping,
1732 ip6_addr_t *src, ip6_addr_t *dst,
1733 int hopl, int tclass, u16_t icmplen)
1734{
1735 struct icmp6_hdr *icmph;
1736 u8_t *bufptr;
1737 size_t buflen, hlen;
1738 int proto;
1739 struct ip6_hdr *oiph;
1740 struct icmp6_echo_hdr *oicmph;
1741 struct ping_pcb *pcb;
1742 ip6_addr_t guest_ip, target_ip, error_ip;
1743 int target_mapped, error_mapped;
1744 u16_t guest_id;
1745 u32_t sum;
1746
1747 icmph = (struct icmp6_hdr *)pollmgr_udpbuf;
1748
1749 /*
1750 * Inner IP datagram is not checked by the kernel and may be
1751 * anything, possibly malicious.
1752 */
1753 oiph = NULL;
1754 oicmph = NULL;
1755
1756 bufptr = pollmgr_udpbuf;
1757 buflen = icmplen;
1758
1759 hlen = sizeof(*icmph);
1760 proto = IP6_NEXTH_ENCAPS; /* i.e. IPv6, lwIP's name is unfortuate */
1761 for (;;) {
1762 if (hlen > buflen) {
1763 DPRINTF2(("truncated datagram inside ICMPv6 error message is too short\n"));
1764 return;
1765 }
1766 buflen -= hlen;
1767 bufptr += hlen;
1768
1769 if (proto == IP6_NEXTH_ENCAPS && oiph == NULL) { /* outermost IPv6 */
1770 oiph = (struct ip6_hdr *)bufptr;
1771 if (IP6H_V(oiph) != 6) {
1772 DPRINTF2(("%s: unexpected IP version %d\n", __func__, IP6H_V(oiph)));
1773 return;
1774 }
1775
1776 proto = IP6H_NEXTH(oiph);
1777 hlen = IP6_HLEN;
1778 }
1779 else if (proto == IP6_NEXTH_ICMP6) {
1780 oicmph = (struct icmp6_echo_hdr *)bufptr;
1781 break;
1782 }
1783 else if (proto == IP6_NEXTH_ROUTING
1784 || proto == IP6_NEXTH_HOPBYHOP
1785 || proto == IP6_NEXTH_DESTOPTS)
1786 {
1787 proto = bufptr[0];
1788 hlen = (bufptr[1] + 1) * 8;
1789 }
1790 else {
1791 DPRINTF2(("%s: stopping at protocol %d\n", __func__, proto));
1792 break;
1793 }
1794 }
1795
1796 if (oiph == NULL || oicmph == NULL) {
1797 return;
1798 }
1799
1800 if (buflen < sizeof(*oicmph)) {
1801 DPRINTF2(("%s: original ICMPv6 is truncated too short\n", __func__));
1802 return;
1803 }
1804
1805 if (oicmph->type != ICMP6_TYPE_EREQ) {
1806 DPRINTF2(("%s: ignoring original ICMPv6 type %d\n", __func__, oicmph->type));
1807 return;
1808 }
1809
1810 ip6_addr_copy(target_ip, oiph->dest); /* inner (failed) */
1811 target_mapped = pxremap_inbound_ip6(&target_ip, &target_ip);
1812 if (target_mapped == PXREMAP_FAILED) {
1813 return;
1814 }
1815
1816 sys_mutex_lock(&pxping->lock);
1817 pcb = pxping_pcb_for_reply(pxping, 1, ip6_2_ipX(&target_ip), oicmph->id);
1818 if (pcb == NULL) {
1819 sys_mutex_unlock(&pxping->lock);
1820 DPRINTF2(("%s: no match\n", __func__));
1821 return;
1822 }
1823
1824 DPRINTF2(("%s: pcb %p\n", __func__, (void *)pcb));
1825
1826 /* save info before unlocking since pcb may expire */
1827 ip6_addr_copy(guest_ip, *ipX_2_ip6(&pcb->src));
1828 guest_id = pcb->guest_id;
1829
1830 sys_mutex_unlock(&pxping->lock);
1831
1832
1833 /*
1834 * Rewrite inner and outer headers and forward to guest. Note
1835 * that IPv6 has no IP header checksum, but uses pseudo-header for
1836 * ICMPv6, so we update both in one go, adjusting ICMPv6 checksum
1837 * as we rewrite IP header.
1838 */
1839
1840 ip6_addr_copy(error_ip, *src); /* node that reports the error */
1841 error_mapped = pxremap_inbound_ip6(&error_ip, &error_ip);
1842 if (error_mapped == PXREMAP_FAILED) {
1843 return;
1844 }
1845 if (error_mapped == PXREMAP_ASIS && hopl == 1) {
1846 DPRINTF2(("%s: dropping packet with ttl 1\n", __func__));
1847 return;
1848 }
1849
1850 /* rewrite inner ICMPv6 echo header and inner IPv6 header */
1851 sum = (u16_t)~oicmph->chksum;
1852 sum += chksum_update_16(&oicmph->id, guest_id);
1853 sum += chksum_update_ipv6((ip6_addr_t *)&oiph->src, &guest_ip);
1854 if (target_mapped) {
1855 sum += chksum_delta_ipv6((ip6_addr_t *)&oiph->dest, &target_ip);
1856 }
1857 sum = FOLD_U32T(sum);
1858 oicmph->chksum = ~sum;
1859
1860 /* rewrite outer ICMPv6 error header */
1861 sum = (u16_t)~icmph->chksum;
1862 sum += chksum_delta_ipv6(dst, &guest_ip); /* pseudo */
1863 if (error_mapped) {
1864 sum += chksum_delta_ipv6(src, &error_ip); /* pseudo */
1865 }
1866 sum = FOLD_U32T(sum);
1867 icmph->chksum = ~sum;
1868
1869 pxping_pmgr_forward_inbound6(pxping,
1870 &error_ip, /* error src */
1871 &guest_ip, /* error dst */
1872 hopl, tclass, icmplen);
1873}
1874
1875
1876/**
1877 * Hand off ICMP datagram to the lwip thread where it will be
1878 * forwarded to the guest.
1879 *
1880 * We no longer need ping_pcb. The pcb may get expired on the lwip
1881 * thread, but we have already patched necessary information into the
1882 * datagram.
1883 */
1884static void
1885pxping_pmgr_forward_inbound(struct pxping *pxping, u16_t iplen)
1886{
1887 struct pbuf *p;
1888 struct ping_msg *msg;
1889 err_t error;
1890
1891 p = pbuf_alloc(PBUF_LINK, iplen, PBUF_RAM);
1892 if (p == NULL) {
1893 DPRINTF(("%s: pbuf_alloc(%d) failed\n",
1894 __func__, (unsigned int)iplen));
1895 return;
1896 }
1897
1898 error = pbuf_take(p, pollmgr_udpbuf, iplen);
1899 if (error != ERR_OK) {
1900 DPRINTF(("%s: pbuf_take(%d) failed\n",
1901 __func__, (unsigned int)iplen));
1902 pbuf_free(p);
1903 return;
1904 }
1905
1906 msg = (struct ping_msg *)malloc(sizeof(*msg));
1907 if (msg == NULL) {
1908 pbuf_free(p);
1909 return;
1910 }
1911
1912 msg->msg.type = TCPIP_MSG_CALLBACK_STATIC;
1913 msg->msg.sem = NULL;
1914 msg->msg.msg.cb.function = pxping_pcb_forward_inbound;
1915 msg->msg.msg.cb.ctx = (void *)msg;
1916
1917 msg->pxping = pxping;
1918 msg->p = p;
1919
1920 proxy_lwip_post(&msg->msg);
1921}
1922
1923
1924static void
1925pxping_pcb_forward_inbound(void *arg)
1926{
1927 struct ping_msg *msg = (struct ping_msg *)arg;
1928 err_t error;
1929
1930 LWIP_ASSERT1(msg != NULL);
1931 LWIP_ASSERT1(msg->pxping != NULL);
1932 LWIP_ASSERT1(msg->p != NULL);
1933
1934 error = ip_raw_output_if(msg->p, msg->pxping->netif);
1935 if (error != ERR_OK) {
1936 DPRINTF(("%s: ip_output_if: %s\n",
1937 __func__, proxy_lwip_strerr(error)));
1938 }
1939 pbuf_free(msg->p);
1940 free(msg);
1941}
1942
1943
1944static void
1945pxping_pmgr_forward_inbound6(struct pxping *pxping,
1946 ip6_addr_t *src, ip6_addr_t *dst,
1947 u8_t hopl, u8_t tclass,
1948 u16_t icmplen)
1949{
1950 struct pbuf *p;
1951 struct ping6_msg *msg;
1952
1953 err_t error;
1954
1955 p = pbuf_alloc(PBUF_IP, icmplen, PBUF_RAM);
1956 if (p == NULL) {
1957 DPRINTF(("%s: pbuf_alloc(%d) failed\n",
1958 __func__, (unsigned int)icmplen));
1959 return;
1960 }
1961
1962 error = pbuf_take(p, pollmgr_udpbuf, icmplen);
1963 if (error != ERR_OK) {
1964 DPRINTF(("%s: pbuf_take(%d) failed\n",
1965 __func__, (unsigned int)icmplen));
1966 pbuf_free(p);
1967 return;
1968 }
1969
1970 msg = (struct ping6_msg *)malloc(sizeof(*msg));
1971 if (msg == NULL) {
1972 pbuf_free(p);
1973 return;
1974 }
1975
1976 msg->msg.type = TCPIP_MSG_CALLBACK_STATIC;
1977 msg->msg.sem = NULL;
1978 msg->msg.msg.cb.function = pxping_pcb_forward_inbound6;
1979 msg->msg.msg.cb.ctx = (void *)msg;
1980
1981 msg->pxping = pxping;
1982 msg->p = p;
1983 ip6_addr_copy(msg->src, *src);
1984 ip6_addr_copy(msg->dst, *dst);
1985 msg->hopl = hopl;
1986 msg->tclass = tclass;
1987
1988 proxy_lwip_post(&msg->msg);
1989}
1990
1991
1992static void
1993pxping_pcb_forward_inbound6(void *arg)
1994{
1995 struct ping6_msg *msg = (struct ping6_msg *)arg;
1996 err_t error;
1997
1998 LWIP_ASSERT1(msg != NULL);
1999 LWIP_ASSERT1(msg->pxping != NULL);
2000 LWIP_ASSERT1(msg->p != NULL);
2001
2002 error = ip6_output_if(msg->p,
2003 &msg->src, &msg->dst, msg->hopl, msg->tclass,
2004 IP6_NEXTH_ICMP6, msg->pxping->netif);
2005 if (error != ERR_OK) {
2006 DPRINTF(("%s: ip6_output_if: %s\n",
2007 __func__, proxy_lwip_strerr(error)));
2008 }
2009 pbuf_free(msg->p);
2010 free(msg);
2011}
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