]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/Icmp6.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 42 ICMP Pinger program */
11 //#define SQUID_HELPER 1
19 #include "IcmpPinger.h"
20 #include "leakcheck.h"
21 #include "SquidTime.h"
23 // Some system headers are only neeed internally here.
24 // They should not be included via the header.
26 #if HAVE_NETINET_IP6_H
27 #include <netinet/ip6.h>
31 // see http://www.iana.org/assignments/icmpv6-parameters
32 // NP: LowPktStr is for codes 0-127
33 static const char *icmp6LowPktStr
[] = {
35 "Destination Unreachable", // 1 - RFC2463
36 "Packet Too Big", // 2 - RFC2463
37 "Time Exceeded", // 3 - RFC2463
38 "Parameter Problem", // 4 - RFC2463
47 // NP: HighPktStr is for codes 128-255
48 static const char *icmp6HighPktStr
[] = {
49 "Echo Request", // 128 - RFC2463
50 "Echo Reply", // 129 - RFC2463
51 "Multicast Listener Query", // 130 - RFC2710
52 "Multicast Listener Report", // 131 - RFC2710
53 "Multicast Listener Done", // 132 - RFC2710
54 "Router Solicitation", // 133 - RFC4861
55 "Router Advertisement", // 134 - RFC4861
56 "Neighbor Solicitation", // 135 - RFC4861
57 "Neighbor Advertisement", // 136 - RFC4861
58 "Redirect Message", // 137 - RFC4861
59 "Router Renumbering", // 138 - Crawford
60 "ICMP Node Information Query", // 139 - RFC4620
61 "ICMP Node Information Response", // 140 - RFC4620
62 "Inverse Neighbor Discovery Solicitation", // 141 - RFC3122
63 "Inverse Neighbor Discovery Advertisement", // 142 - RFC3122
64 "Version 2 Multicast Listener Report", // 143 - RFC3810
65 "Home Agent Address Discovery Request", // 144 - RFC3775
66 "Home Agent Address Discovery Reply", // 145 - RFC3775
67 "Mobile Prefix Solicitation", // 146 - RFC3775
68 "Mobile Prefix Advertisement", // 147 - RFC3775
69 "Certification Path Solicitation", // 148 - RFC3971
70 "Certification Path Advertisement", // 149 - RFC3971
71 "ICMP Experimental (150)", // 150 - RFC4065
72 "Multicast Router Advertisement", // 151 - RFC4286
73 "Multicast Router Solicitation", // 152 - RFC4286
74 "Multicast Router Termination", // 153 - [RFC4286]
84 Icmp6::Icmp6() : Icmp()
97 icmp_sock
= socket(PF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
);
100 debugs(50, DBG_CRITICAL
, HERE
<< " icmp_sock: " << xstrerror());
104 icmp_ident
= getpid() & 0xffff;
105 debugs(42, DBG_IMPORTANT
, "pinger: ICMPv6 socket opened");
111 * Generates an RFC 4443 Icmp6 ECHO Packet and sends into the network.
114 Icmp6::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
117 LOCAL_ARRAY(char, pkt
, MAX_PKT6_SZ
);
118 struct icmp6_hdr
*icmp
= NULL
;
119 icmpEchoData
*echo
= NULL
;
120 struct addrinfo
*S
= NULL
;
121 size_t icmp6_pktsize
= 0;
123 memset(pkt
, '\0', MAX_PKT6_SZ
);
124 icmp
= (struct icmp6_hdr
*)pkt
;
127 * cevans - beware signed/unsigned issues in untrusted data from
134 // Construct Icmp6 ECHO header
135 icmp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
136 icmp
->icmp6_code
= 0;
137 icmp
->icmp6_cksum
= 0;
138 icmp
->icmp6_id
= icmp_ident
;
139 icmp
->icmp6_seq
= (unsigned short) icmp_pkts_sent
;
142 icmp6_pktsize
= sizeof(struct icmp6_hdr
);
144 // Fill Icmp6 ECHO data content
145 echo
= (icmpEchoData
*) (pkt
+ sizeof(icmp6_hdr
));
146 echo
->opcode
= (unsigned char) opcode
;
147 memcpy(&echo
->tv
, ¤t_time
, sizeof(struct timeval
));
149 icmp6_pktsize
+= sizeof(struct timeval
) + sizeof(char);
152 if (len
> MAX_PAYLOAD
)
155 memcpy(echo
->payload
, payload
, len
);
157 icmp6_pktsize
+= len
;
160 icmp
->icmp6_cksum
= CheckSum((unsigned short *) icmp
, icmp6_pktsize
);
163 ((sockaddr_in6
*)S
->ai_addr
)->sin6_port
= 0;
165 assert(icmp6_pktsize
<= MAX_PKT6_SZ
);
167 debugs(42, 5, HERE
<< "Send Icmp6 packet to " << to
<< ".");
169 x
= sendto(icmp_sock
,
177 debugs(42, DBG_IMPORTANT
, HERE
<< "Error sending to ICMPv6 packet to " << to
<< ". ERR: " << xstrerror());
179 debugs(42,9, HERE
<< "x=" << x
);
181 Log(to
, 0, NULL
, 0, 0);
182 Ip::Address::FreeAddrInfo(S
);
186 * Reads an RFC 4443 Icmp6 ECHO-REPLY Packet from the network.
192 struct addrinfo
*from
= NULL
;
193 // struct ip6_hdr *ip = NULL;
194 static char *pkt
= NULL
;
195 struct icmp6_hdr
*icmp6header
= NULL
;
196 icmpEchoData
*echo
= NULL
;
198 static pingerReplyData preply
;
201 debugs(42, DBG_CRITICAL
, HERE
<< "dropping ICMPv6 read. No socket!?");
206 pkt
= (char *)xmalloc(MAX_PKT6_SZ
);
209 Ip::Address::InitAddrInfo(from
);
211 n
= recvfrom(icmp_sock
,
220 #if GETTIMEOFDAY_NO_TZP
226 gettimeofday(&now
, NULL
);
230 debugs(42, 8, HERE
<< n
<< " bytes from " << preply
.from
);
232 // FIXME INET6 : The IPv6 Header (ip6_hdr) is not availble directly >:-(
234 // TTL still has to come from the IP header somewhere.
235 // still need to strip and process it properly.
236 // probably have to rely on RTT as given by timestamp in data sent and current.
237 /* IPv6 Header Structures (linux)
240 // fields (via simple define)
241 #define ip6_vfc // N.A
242 #define ip6_flow // N/A
243 #define ip6_plen // payload length.
244 #define ip6_nxt // expect to be type 0x3a - ICMPv6
245 #define ip6_hlim // MAX hops (always 64, but no guarantee)
246 #define ip6_hops // HOPS!!! (can it be true??)
248 ip = (struct ip6_hdr *) pkt;
249 pkt += sizeof(ip6_hdr);
251 debugs(42, DBG_CRITICAL, HERE << "ip6_nxt=" << ip->ip6_nxt <<
252 ", ip6_plen=" << ip->ip6_plen <<
253 ", ip6_hlim=" << ip->ip6_hlim <<
254 ", ip6_hops=" << ip->ip6_hops <<
255 " ::: 40 == sizef(ip6_hdr) == " << sizeof(ip6_hdr)
259 icmp6header
= (struct icmp6_hdr
*) pkt
;
260 pkt
+= sizeof(icmp6_hdr
);
262 if (icmp6header
->icmp6_type
!= ICMP6_ECHO_REPLY
) {
264 switch (icmp6header
->icmp6_type
) {
268 /* ignore Router/Neighbour Advertisements */
272 debugs(42, 8, HERE
<< preply
.from
<< " said: " << icmp6header
->icmp6_type
<< "/" << (int)icmp6header
->icmp6_code
<< " " <<
273 ( icmp6header
->icmp6_type
&0x80 ? icmp6HighPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] : icmp6LowPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] )
276 Ip::Address::FreeAddrInfo(from
);
280 if (icmp6header
->icmp6_id
!= icmp_ident
) {
281 debugs(42, 8, HERE
<< "dropping Icmp6 read. IDENT check failed. ident=='" << icmp_ident
<< "'=='" << icmp6header
->icmp6_id
<< "'");
282 Ip::Address::FreeAddrInfo(from
);
286 echo
= (icmpEchoData
*) pkt
;
288 preply
.opcode
= echo
->opcode
;
291 memcpy(&tv
, &echo
->tv
, sizeof(struct timeval
));
292 preply
.rtt
= tvSubMsec(tv
, now
);
295 * FIXME INET6: Without access to the IPv6-Hops header we must rely on the total RTT
296 * and could caculate the hops from that, but it produces some weird value mappings using ipHops
297 * for now everything is 1 v6 hop away with variant RTT
298 * WANT: preply.hops = ip->ip6_hops; // ipHops(ip->ip_hops);
302 preply
.psize
= n
- /* sizeof(ip6_hdr) - */ sizeof(icmp6_hdr
) - (sizeof(icmpEchoData
) - MAX_PKT6_SZ
);
304 /* Ensure the response packet has safe payload size */
305 if ( preply
.psize
> (unsigned short) MAX_PKT6_SZ
) {
306 preply
.psize
= MAX_PKT6_SZ
;
307 } else if ( preply
.psize
< (unsigned short)0) {
312 icmp6header
->icmp6_type
,
313 ( icmp6header
->icmp6_type
&0x80 ? icmp6HighPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] : icmp6LowPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] ),
317 /* send results of the lookup back to squid.*/
318 control
.SendResult(preply
, (sizeof(pingerReplyData
) - PINGER_PAYLOAD_SZ
+ preply
.psize
) );
319 Ip::Address::FreeAddrInfo(from
);
322 #endif /* USE_ICMP */