]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/Icmp6.cc
4 * DEBUG: section 42 ICMP Pinger program
5 * AUTHOR: Duane Wessels, Amos Jeffries
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 //#define SQUID_HELPER 1
40 #include "leakcheck.h"
41 #include "SquidTime.h"
44 #include "IcmpPinger.h"
46 // Some system headers are only neeed internally here.
47 // They should not be included via the header.
49 #if HAVE_NETINET_IP6_H
50 #include <netinet/ip6.h>
54 // see http://www.iana.org/assignments/icmpv6-parameters
55 // NP: LowPktStr is for codes 0-127
56 static const char *icmp6LowPktStr
[] = {
58 "Destination Unreachable", // 1 - RFC2463
59 "Packet Too Big", // 2 - RFC2463
60 "Time Exceeded", // 3 - RFC2463
61 "Parameter Problem", // 4 - RFC2463
70 // NP: HighPktStr is for codes 128-255
71 static const char *icmp6HighPktStr
[] = {
72 "Echo Request", // 128 - RFC2463
73 "Echo Reply", // 129 - RFC2463
74 "Multicast Listener Query", // 130 - RFC2710
75 "Multicast Listener Report", // 131 - RFC2710
76 "Multicast Listener Done", // 132 - RFC2710
77 "Router Solicitation", // 133 - RFC4861
78 "Router Advertisement", // 134 - RFC4861
79 "Neighbor Solicitation", // 135 - RFC4861
80 "Neighbor Advertisement", // 136 - RFC4861
81 "Redirect Message", // 137 - RFC4861
82 "Router Renumbering", // 138 - Crawford
83 "ICMP Node Information Query", // 139 - RFC4620
84 "ICMP Node Information Response", // 140 - RFC4620
85 "Inverse Neighbor Discovery Solicitation", // 141 - RFC3122
86 "Inverse Neighbor Discovery Advertisement", // 142 - RFC3122
87 "Version 2 Multicast Listener Report", // 143 - RFC3810
88 "Home Agent Address Discovery Request", // 144 - RFC3775
89 "Home Agent Address Discovery Reply", // 145 - RFC3775
90 "Mobile Prefix Solicitation", // 146 - RFC3775
91 "Mobile Prefix Advertisement", // 147 - RFC3775
92 "Certification Path Solicitation", // 148 - RFC3971
93 "Certification Path Advertisement", // 149 - RFC3971
94 "ICMP Experimental (150)", // 150 - RFC4065
95 "Multicast Router Advertisement", // 151 - RFC4286
96 "Multicast Router Solicitation", // 152 - RFC4286
97 "Multicast Router Termination", // 153 - [RFC4286]
107 Icmp6::Icmp6() : Icmp()
120 icmp_sock
= socket(PF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
);
123 debugs(50, DBG_CRITICAL
, HERE
<< " icmp_sock: " << xstrerror());
127 icmp_ident
= getpid() & 0xffff;
128 debugs(42, DBG_IMPORTANT
, "pinger: ICMPv6 socket opened");
134 * Generates an RFC 4443 Icmp6 ECHO Packet and sends into the network.
137 Icmp6::SendEcho(Ip::Address
&to
, int opcode
, const char *payload
, int len
)
140 LOCAL_ARRAY(char, pkt
, MAX_PKT6_SZ
);
141 struct icmp6_hdr
*icmp
= NULL
;
142 icmpEchoData
*echo
= NULL
;
143 struct addrinfo
*S
= NULL
;
144 size_t icmp6_pktsize
= 0;
146 memset(pkt
, '\0', MAX_PKT6_SZ
);
147 icmp
= (struct icmp6_hdr
*)pkt
;
150 * cevans - beware signed/unsigned issues in untrusted data from
157 // Construct Icmp6 ECHO header
158 icmp
->icmp6_type
= ICMP6_ECHO_REQUEST
;
159 icmp
->icmp6_code
= 0;
160 icmp
->icmp6_cksum
= 0;
161 icmp
->icmp6_id
= icmp_ident
;
162 icmp
->icmp6_seq
= (unsigned short) icmp_pkts_sent
;
165 icmp6_pktsize
= sizeof(struct icmp6_hdr
);
168 // Fill Icmp6 ECHO data content
169 echo
= (icmpEchoData
*) (pkt
+ sizeof(icmp6_hdr
));
170 echo
->opcode
= (unsigned char) opcode
;
171 memcpy(&echo
->tv
, ¤t_time
, sizeof(struct timeval
));
173 icmp6_pktsize
+= sizeof(struct timeval
) + sizeof(char);
176 if (len
> MAX_PAYLOAD
)
179 memcpy(echo
->payload
, payload
, len
);
181 icmp6_pktsize
+= len
;
184 icmp
->icmp6_cksum
= CheckSum((unsigned short *) icmp
, icmp6_pktsize
);
187 ((sockaddr_in6
*)S
->ai_addr
)->sin6_port
= 0;
189 assert(icmp6_pktsize
<= MAX_PKT6_SZ
);
191 debugs(42, 5, HERE
<< "Send Icmp6 packet to " << to
<< ".");
193 x
= sendto(icmp_sock
,
201 debugs(42, DBG_IMPORTANT
, HERE
<< "Error sending to ICMPv6 packet to " << to
<< ". ERR: " << xstrerror());
203 debugs(42,9, HERE
<< "x=" << x
);
205 Log(to
, 0, NULL
, 0, 0);
209 * Reads an RFC 4443 Icmp6 ECHO-REPLY Packet from the network.
215 struct addrinfo
*from
= NULL
;
216 // struct ip6_hdr *ip = NULL;
217 static char *pkt
= NULL
;
218 struct icmp6_hdr
*icmp6header
= NULL
;
219 icmpEchoData
*echo
= NULL
;
221 static pingerReplyData preply
;
224 debugs(42, DBG_CRITICAL
, HERE
<< "dropping ICMPv6 read. No socket!?");
229 pkt
= (char *)xmalloc(MAX_PKT6_SZ
);
232 preply
.from
.InitAddrInfo(from
);
234 n
= recvfrom(icmp_sock
,
243 #if GETTIMEOFDAY_NO_TZP
249 gettimeofday(&now
, NULL
);
253 debugs(42, 8, HERE
<< n
<< " bytes from " << preply
.from
);
255 // FIXME INET6 : The IPv6 Header (ip6_hdr) is not availble directly >:-(
257 // TTL still has to come from the IP header somewhere.
258 // still need to strip and process it properly.
259 // probably have to rely on RTT as given by timestamp in data sent and current.
260 /* IPv6 Header Structures (linux)
263 // fields (via simple define)
264 #define ip6_vfc // N.A
265 #define ip6_flow // N/A
266 #define ip6_plen // payload length.
267 #define ip6_nxt // expect to be type 0x3a - ICMPv6
268 #define ip6_hlim // MAX hops (always 64, but no guarantee)
269 #define ip6_hops // HOPS!!! (can it be true??)
272 ip = (struct ip6_hdr *) pkt;
273 pkt += sizeof(ip6_hdr);
275 debugs(42, DBG_CRITICAL, HERE << "ip6_nxt=" << ip->ip6_nxt <<
276 ", ip6_plen=" << ip->ip6_plen <<
277 ", ip6_hlim=" << ip->ip6_hlim <<
278 ", ip6_hops=" << ip->ip6_hops <<
279 " ::: 40 == sizef(ip6_hdr) == " << sizeof(ip6_hdr)
283 icmp6header
= (struct icmp6_hdr
*) pkt
;
284 pkt
+= sizeof(icmp6_hdr
);
286 if (icmp6header
->icmp6_type
!= ICMP6_ECHO_REPLY
) {
288 switch (icmp6header
->icmp6_type
) {
292 /* ignore Router/Neighbour Advertisements */
296 debugs(42, 8, HERE
<< preply
.from
<< " said: " << icmp6header
->icmp6_type
<< "/" << (int)icmp6header
->icmp6_code
<< " " <<
297 ( icmp6header
->icmp6_type
&0x80 ? icmp6HighPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] : icmp6LowPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] )
303 if (icmp6header
->icmp6_id
!= icmp_ident
) {
304 debugs(42, 8, HERE
<< "dropping Icmp6 read. IDENT check failed. ident=='" << icmp_ident
<< "'=='" << icmp6header
->icmp6_id
<< "'");
308 echo
= (icmpEchoData
*) pkt
;
310 preply
.opcode
= echo
->opcode
;
313 memcpy(&tv
, &echo
->tv
, sizeof(struct timeval
));
314 preply
.rtt
= tvSubMsec(tv
, now
);
317 * FIXME INET6: Without access to the IPv6-Hops header we must rely on the total RTT
318 * and could caculate the hops from that, but it produces some weird value mappings using ipHops
319 * for now everything is 1 v6 hop away with variant RTT
320 * WANT: preply.hops = ip->ip6_hops; // ipHops(ip->ip_hops);
324 preply
.psize
= n
- /* sizeof(ip6_hdr) - */ sizeof(icmp6_hdr
) - (sizeof(icmpEchoData
) - MAX_PKT6_SZ
);
326 /* Ensure the response packet has safe payload size */
327 if ( preply
.psize
> (unsigned short) MAX_PKT6_SZ
) {
328 preply
.psize
= MAX_PKT6_SZ
;
329 } else if ( preply
.psize
< (unsigned short)0) {
334 icmp6header
->icmp6_type
,
335 ( icmp6header
->icmp6_type
&0x80 ? icmp6HighPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] : icmp6LowPktStr
[(int)(icmp6header
->icmp6_type
&0x7f)] ),
339 /* send results of the lookup back to squid.*/
340 control
.SendResult(preply
, (sizeof(pingerReplyData
) - PINGER_PAYLOAD_SZ
+ preply
.psize
) );
343 #endif /* USE_ICMP */