]>
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
33 IcmpPacketType(uint8_t v
)
35 // NP: LowPktStr is for codes 0-127
36 static const char *icmp6LowPktStr
[] = {
38 "Destination Unreachable", // 1 - RFC2463
39 "Packet Too Big", // 2 - RFC2463
40 "Time Exceeded", // 3 - RFC2463
41 "Parameter Problem", // 4 - RFC2463
44 // low codes 1-4 registered
46 return icmp6LowPktStr
[(int)(v
&0x7f)];
48 // NP: HighPktStr is for codes 128-255
49 static const char *icmp6HighPktStr
[] = {
50 "Echo Request", // 128 - RFC2463
51 "Echo Reply", // 129 - RFC2463
52 "Multicast Listener Query", // 130 - RFC2710
53 "Multicast Listener Report", // 131 - RFC2710
54 "Multicast Listener Done", // 132 - RFC2710
55 "Router Solicitation", // 133 - RFC4861
56 "Router Advertisement", // 134 - RFC4861
57 "Neighbor Solicitation", // 135 - RFC4861
58 "Neighbor Advertisement", // 136 - RFC4861
59 "Redirect Message", // 137 - RFC4861
60 "Router Renumbering", // 138 - Crawford
61 "ICMP Node Information Query", // 139 - RFC4620
62 "ICMP Node Information Response", // 140 - RFC4620
63 "Inverse Neighbor Discovery Solicitation", // 141 - RFC3122
64 "Inverse Neighbor Discovery Advertisement", // 142 - RFC3122
65 "Version 2 Multicast Listener Report", // 143 - RFC3810
66 "Home Agent Address Discovery Request", // 144 - RFC3775
67 "Home Agent Address Discovery Reply", // 145 - RFC3775
68 "Mobile Prefix Solicitation", // 146 - RFC3775
69 "Mobile Prefix Advertisement", // 147 - RFC3775
70 "Certification Path Solicitation", // 148 - RFC3971
71 "Certification Path Advertisement", // 149 - RFC3971
72 "ICMP Experimental (150)", // 150 - RFC4065
73 "Multicast Router Advertisement", // 151 - RFC4286
74 "Multicast Router Solicitation", // 152 - RFC4286
75 "Multicast Router Termination", // 153 - [RFC4286]
78 // high codes 127-153 registered
79 if (127 < v
&& v
< 154)
80 return icmp6HighPktStr
[(int)(v
&0x7f)];
82 // give all others a generic display
84 snprintf(buf
, sizeof(buf
), "ICMPv6 %u", v
);
88 Icmp6::Icmp6() : Icmp()
101 icmp_sock
= socket(PF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
);
104 debugs(50, DBG_CRITICAL
, HERE
<< " icmp_sock: " << xstrerror());
108 icmp_ident
= getpid() & 0xffff;
109 debugs(42, DBG_IMPORTANT
, "pinger: ICMPv6 socket opened");
115 * Generates an RFC 4443 Icmp6 ECHO Packet and sends into the network.
118 Icmp6::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
121 LOCAL_ARRAY(char, pkt
, MAX_PKT6_SZ
);
122 struct icmp6_hdr
*icmp
= NULL
;
123 icmpEchoData
*echo
= NULL
;
124 struct addrinfo
*S
= NULL
;
125 size_t icmp6_pktsize
= 0;
127 memset(pkt
, '\0', MAX_PKT6_SZ
);
128 icmp
= (struct icmp6_hdr
*)pkt
;
131 * cevans - beware signed/unsigned issues in untrusted data from
138 // Construct Icmp6 ECHO header
139 icmp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
140 icmp
->icmp6_code
= 0;
141 icmp
->icmp6_cksum
= 0;
142 icmp
->icmp6_id
= icmp_ident
;
143 icmp
->icmp6_seq
= (unsigned short) icmp_pkts_sent
;
146 icmp6_pktsize
= sizeof(struct icmp6_hdr
);
148 // Fill Icmp6 ECHO data content
149 echo
= (icmpEchoData
*) (pkt
+ sizeof(icmp6_hdr
));
150 echo
->opcode
= (unsigned char) opcode
;
151 memcpy(&echo
->tv
, ¤t_time
, sizeof(struct timeval
));
153 icmp6_pktsize
+= sizeof(struct timeval
) + sizeof(char);
156 if (len
> MAX_PAYLOAD
)
159 memcpy(echo
->payload
, payload
, len
);
161 icmp6_pktsize
+= len
;
164 icmp
->icmp6_cksum
= CheckSum((unsigned short *) icmp
, icmp6_pktsize
);
167 ((sockaddr_in6
*)S
->ai_addr
)->sin6_port
= 0;
169 assert(icmp6_pktsize
<= MAX_PKT6_SZ
);
171 debugs(42, 5, HERE
<< "Send Icmp6 packet to " << to
<< ".");
173 x
= sendto(icmp_sock
,
181 debugs(42, DBG_IMPORTANT
, HERE
<< "Error sending to ICMPv6 packet to " << to
<< ". ERR: " << xstrerror());
183 debugs(42,9, HERE
<< "x=" << x
);
185 Log(to
, 0, NULL
, 0, 0);
186 Ip::Address::FreeAddr(S
);
190 * Reads an RFC 4443 Icmp6 ECHO-REPLY Packet from the network.
196 struct addrinfo
*from
= NULL
;
197 // struct ip6_hdr *ip = NULL;
198 static char *pkt
= NULL
;
199 struct icmp6_hdr
*icmp6header
= NULL
;
200 icmpEchoData
*echo
= NULL
;
202 static pingerReplyData preply
;
205 debugs(42, DBG_CRITICAL
, HERE
<< "dropping ICMPv6 read. No socket!?");
210 pkt
= (char *)xmalloc(MAX_PKT6_SZ
);
213 Ip::Address::InitAddr(from
);
215 n
= recvfrom(icmp_sock
,
223 debugs(42, DBG_CRITICAL
, HERE
<< "Error when calling recvfrom() on ICMPv6 socket.");
224 Ip::Address::FreeAddr(from
);
230 #if GETTIMEOFDAY_NO_TZP
236 gettimeofday(&now
, NULL
);
240 debugs(42, 8, HERE
<< n
<< " bytes from " << preply
.from
);
242 // FIXME INET6 : The IPv6 Header (ip6_hdr) is not availble directly >:-(
244 // TTL still has to come from the IP header somewhere.
245 // still need to strip and process it properly.
246 // probably have to rely on RTT as given by timestamp in data sent and current.
247 /* IPv6 Header Structures (linux)
250 // fields (via simple define)
251 #define ip6_vfc // N.A
252 #define ip6_flow // N/A
253 #define ip6_plen // payload length.
254 #define ip6_nxt // expect to be type 0x3a - ICMPv6
255 #define ip6_hlim // MAX hops (always 64, but no guarantee)
256 #define ip6_hops // HOPS!!! (can it be true??)
258 ip = (struct ip6_hdr *) pkt;
259 pkt += sizeof(ip6_hdr);
261 debugs(42, DBG_CRITICAL, HERE << "ip6_nxt=" << ip->ip6_nxt <<
262 ", ip6_plen=" << ip->ip6_plen <<
263 ", ip6_hlim=" << ip->ip6_hlim <<
264 ", ip6_hops=" << ip->ip6_hops <<
265 " ::: 40 == sizef(ip6_hdr) == " << sizeof(ip6_hdr)
269 icmp6header
= (struct icmp6_hdr
*) pkt
;
270 pkt
+= sizeof(icmp6_hdr
);
272 if (icmp6header
->icmp6_type
!= ICMP6_ECHO_REPLY
) {
274 switch (icmp6header
->icmp6_type
) {
278 /* ignore Router/Neighbour Advertisements */
282 debugs(42, 8, HERE
<< preply
.from
<< " said: " << icmp6header
->icmp6_type
<< "/" << (int)icmp6header
->icmp6_code
<< " " <<
283 IcmpPacketType(icmp6header
->icmp6_type
));
285 Ip::Address::FreeAddr(from
);
289 if (icmp6header
->icmp6_id
!= icmp_ident
) {
290 debugs(42, 8, HERE
<< "dropping Icmp6 read. IDENT check failed. ident=='" << icmp_ident
<< "'=='" << icmp6header
->icmp6_id
<< "'");
291 Ip::Address::FreeAddr(from
);
295 echo
= (icmpEchoData
*) pkt
;
297 preply
.opcode
= echo
->opcode
;
300 memcpy(&tv
, &echo
->tv
, sizeof(struct timeval
));
301 preply
.rtt
= tvSubMsec(tv
, now
);
304 * FIXME INET6: Without access to the IPv6-Hops header we must rely on the total RTT
305 * and could caculate the hops from that, but it produces some weird value mappings using ipHops
306 * for now everything is 1 v6 hop away with variant RTT
307 * WANT: preply.hops = ip->ip6_hops; // ipHops(ip->ip_hops);
311 preply
.psize
= n
- /* sizeof(ip6_hdr) - */ sizeof(icmp6_hdr
) - (sizeof(icmpEchoData
) - MAX_PKT6_SZ
);
313 /* Ensure the response packet has safe payload size */
314 if ( preply
.psize
> (unsigned short) MAX_PKT6_SZ
) {
315 preply
.psize
= MAX_PKT6_SZ
;
316 } else if ( preply
.psize
< (unsigned short)0) {
321 icmp6header
->icmp6_type
,
322 IcmpPacketType(icmp6header
->icmp6_type
),
326 /* send results of the lookup back to squid.*/
327 control
.SendResult(preply
, (sizeof(pingerReplyData
) - PINGER_PAYLOAD_SZ
+ preply
.psize
) );
328 Ip::Address::FreeAddr(from
);
331 #endif /* USE_ICMP */