]>
Commit | Line | Data |
---|---|---|
cc192b50 | 1 | /* |
262a0e14 | 2 | * $Id$ |
cc192b50 | 3 | * |
4 | * DEBUG: section 42 ICMP Pinger program | |
5 | * AUTHOR: Duane Wessels, Amos Jeffries | |
6 | * | |
7 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
8 | * ---------------------------------------------------------- | |
9 | * | |
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. | |
18 | * | |
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. | |
26ac0430 | 23 | * |
cc192b50 | 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. | |
26ac0430 | 28 | * |
cc192b50 | 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. | |
32 | * | |
33 | */ | |
34 | //#define SQUID_HELPER 1 | |
35 | ||
f7f3304a | 36 | #include "squid-old.h" |
cc192b50 | 37 | |
055421ee | 38 | #if USE_ICMP |
cc192b50 | 39 | |
40 | #include "SquidTime.h" | |
41 | #include "Debug.h" | |
b826ffb5 AJ |
42 | #include "Icmp6.h" |
43 | #include "IcmpPinger.h" | |
cc192b50 | 44 | |
45 | // Some system headers are only neeed internally here. | |
46 | // They should not be included via the header. | |
47 | ||
48 | #if HAVE_NETINET_IP6_H | |
49 | #include <netinet/ip6.h> | |
50 | #endif | |
51 | ||
b826ffb5 | 52 | // Icmp6 OP-Codes |
cc192b50 | 53 | // see http://www.iana.org/assignments/icmpv6-parameters |
54 | // NP: LowPktStr is for codes 0-127 | |
26ac0430 AJ |
55 | static const char *icmp6LowPktStr[] = { |
56 | "ICMP 0", // 0 | |
57 | "Destination Unreachable", // 1 - RFC2463 | |
58 | "Packet Too Big", // 2 - RFC2463 | |
59 | "Time Exceeded", // 3 - RFC2463 | |
60 | "Parameter Problem", // 4 - RFC2463 | |
61 | "ICMP 5", // 5 | |
62 | "ICMP 6", // 6 | |
63 | "ICMP 7", // 7 | |
64 | "ICMP 8", // 8 | |
65 | "ICMP 9", // 9 | |
66 | "ICMP 10" // 10 | |
67 | }; | |
cc192b50 | 68 | |
69 | // NP: HighPktStr is for codes 128-255 | |
26ac0430 AJ |
70 | static const char *icmp6HighPktStr[] = { |
71 | "Echo Request", // 128 - RFC2463 | |
72 | "Echo Reply", // 129 - RFC2463 | |
73 | "Multicast Listener Query", // 130 - RFC2710 | |
74 | "Multicast Listener Report", // 131 - RFC2710 | |
75 | "Multicast Listener Done", // 132 - RFC2710 | |
76 | "Router Solicitation", // 133 - RFC4861 | |
77 | "Router Advertisement", // 134 - RFC4861 | |
78 | "Neighbor Solicitation", // 135 - RFC4861 | |
79 | "Neighbor Advertisement", // 136 - RFC4861 | |
80 | "Redirect Message", // 137 - RFC4861 | |
81 | "Router Renumbering", // 138 - Crawford | |
82 | "ICMP Node Information Query", // 139 - RFC4620 | |
83 | "ICMP Node Information Response", // 140 - RFC4620 | |
84 | "Inverse Neighbor Discovery Solicitation", // 141 - RFC3122 | |
85 | "Inverse Neighbor Discovery Advertisement", // 142 - RFC3122 | |
86 | "Version 2 Multicast Listener Report", // 143 - RFC3810 | |
87 | "Home Agent Address Discovery Request", // 144 - RFC3775 | |
88 | "Home Agent Address Discovery Reply", // 145 - RFC3775 | |
89 | "Mobile Prefix Solicitation", // 146 - RFC3775 | |
90 | "Mobile Prefix Advertisement", // 147 - RFC3775 | |
91 | "Certification Path Solicitation", // 148 - RFC3971 | |
92 | "Certification Path Advertisement", // 149 - RFC3971 | |
93 | "ICMP Experimental (150)", // 150 - RFC4065 | |
94 | "Multicast Router Advertisement", // 151 - RFC4286 | |
95 | "Multicast Router Solicitation", // 152 - RFC4286 | |
96 | "Multicast Router Termination", // 153 - [RFC4286] | |
97 | "ICMP 154", | |
98 | "ICMP 155", | |
99 | "ICMP 156", | |
100 | "ICMP 157", | |
101 | "ICMP 158", | |
102 | "ICMP 159", | |
103 | "ICMP 160" | |
104 | }; | |
cc192b50 | 105 | |
b826ffb5 | 106 | Icmp6::Icmp6() : Icmp() |
cc192b50 | 107 | { |
108 | ; // nothing new. | |
109 | } | |
110 | ||
b826ffb5 | 111 | Icmp6::~Icmp6() |
cc192b50 | 112 | { |
113 | Close(); | |
114 | } | |
115 | ||
116 | int | |
b826ffb5 | 117 | Icmp6::Open(void) |
cc192b50 | 118 | { |
119 | icmp_sock = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6); | |
120 | ||
121 | if (icmp_sock < 0) { | |
122 | debugs(50, 0, HERE << " icmp_sock: " << xstrerror()); | |
123 | return -1; | |
124 | } | |
125 | ||
126 | icmp_ident = getpid() & 0xffff; | |
9e39fd05 | 127 | debugs(42, 1, "pinger: ICMPv6 socket opened"); |
cc192b50 | 128 | |
129 | return icmp_sock; | |
130 | } | |
131 | ||
132 | /** | |
b826ffb5 | 133 | * Generates an RFC 4443 Icmp6 ECHO Packet and sends into the network. |
cc192b50 | 134 | */ |
135 | void | |
b7ac5457 | 136 | Icmp6::SendEcho(Ip::Address &to, int opcode, const char *payload, int len) |
cc192b50 | 137 | { |
138 | int x; | |
139 | LOCAL_ARRAY(char, pkt, MAX_PKT6_SZ); | |
140 | struct icmp6_hdr *icmp = NULL; | |
141 | icmpEchoData *echo = NULL; | |
142 | struct addrinfo *S = NULL; | |
143 | size_t icmp6_pktsize = 0; | |
144 | ||
145 | memset(pkt, '\0', MAX_PKT6_SZ); | |
146 | icmp = (struct icmp6_hdr *)pkt; | |
147 | ||
148 | /* | |
149 | * cevans - beware signed/unsigned issues in untrusted data from | |
150 | * the network!! | |
151 | */ | |
152 | if (len < 0) { | |
153 | len = 0; | |
154 | } | |
155 | ||
b826ffb5 | 156 | // Construct Icmp6 ECHO header |
cc192b50 | 157 | icmp->icmp6_type = ICMP6_ECHO_REQUEST; |
158 | icmp->icmp6_code = 0; | |
159 | icmp->icmp6_cksum = 0; | |
160 | icmp->icmp6_id = icmp_ident; | |
f45dd259 | 161 | icmp->icmp6_seq = (unsigned short) icmp_pkts_sent++; |
cc192b50 | 162 | |
163 | icmp6_pktsize = sizeof(struct icmp6_hdr); | |
164 | ||
165 | ||
b826ffb5 | 166 | // Fill Icmp6 ECHO data content |
cc192b50 | 167 | echo = (icmpEchoData *) (pkt + sizeof(icmp6_hdr)); |
168 | echo->opcode = (unsigned char) opcode; | |
c3c6695b | 169 | memcpy(&echo->tv, ¤t_time, sizeof(struct timeval)); |
cc192b50 | 170 | |
171 | icmp6_pktsize += sizeof(struct timeval) + sizeof(char); | |
172 | ||
26ac0430 | 173 | if (payload) { |
cc192b50 | 174 | if (len > MAX_PAYLOAD) |
175 | len = MAX_PAYLOAD; | |
176 | ||
41d00cd3 | 177 | memcpy(echo->payload, payload, len); |
cc192b50 | 178 | |
179 | icmp6_pktsize += len; | |
180 | } | |
181 | ||
f45dd259 | 182 | icmp->icmp6_cksum = CheckSum((unsigned short *) icmp, icmp6_pktsize); |
cc192b50 | 183 | |
184 | to.GetAddrInfo(S); | |
185 | ((sockaddr_in6*)S->ai_addr)->sin6_port = 0; | |
186 | ||
187 | assert(icmp6_pktsize <= MAX_PKT6_SZ); | |
188 | ||
b826ffb5 | 189 | debugs(42, 5, HERE << "Send Icmp6 packet to " << to << "."); |
cc192b50 | 190 | |
191 | x = sendto(icmp_sock, | |
26ac0430 AJ |
192 | (const void *) pkt, |
193 | icmp6_pktsize, | |
194 | 0, | |
195 | S->ai_addr, | |
196 | S->ai_addrlen); | |
cc192b50 | 197 | |
26ac0430 | 198 | if (x < 0) { |
cc192b50 | 199 | debugs(42, 1, HERE << "Error sending to ICMPv6 packet to " << to << ". ERR: " << xstrerror()); |
200 | } | |
201 | debugs(42,9, HERE << "x=" << x); | |
202 | ||
203 | Log(to, 0, NULL, 0, 0); | |
204 | } | |
205 | ||
206 | /** | |
b826ffb5 | 207 | * Reads an RFC 4443 Icmp6 ECHO-REPLY Packet from the network. |
cc192b50 | 208 | */ |
209 | void | |
b826ffb5 | 210 | Icmp6::Recv(void) |
cc192b50 | 211 | { |
212 | int n; | |
213 | struct addrinfo *from = NULL; | |
214 | // struct ip6_hdr *ip = NULL; | |
215 | static char *pkt = NULL; | |
b0365bd9 | 216 | struct icmp6_hdr *icmp6header = NULL; |
cc192b50 | 217 | icmpEchoData *echo = NULL; |
218 | struct timeval now; | |
219 | static pingerReplyData preply; | |
220 | ||
26ac0430 | 221 | if (icmp_sock < 0) { |
cc192b50 | 222 | debugs(42,0, HERE << "dropping ICMPv6 read. No socket!?"); |
223 | return; | |
224 | } | |
225 | ||
226 | if (pkt == NULL) { | |
227 | pkt = (char *)xmalloc(MAX_PKT6_SZ); | |
228 | } | |
229 | ||
230 | preply.from.InitAddrInfo(from); | |
231 | ||
232 | n = recvfrom(icmp_sock, | |
233 | (void *)pkt, | |
234 | MAX_PKT6_SZ, | |
235 | 0, | |
236 | from->ai_addr, | |
237 | &from->ai_addrlen); | |
238 | ||
239 | preply.from = *from; | |
240 | ||
241 | #if GETTIMEOFDAY_NO_TZP | |
242 | ||
243 | gettimeofday(&now); | |
244 | ||
245 | #else | |
246 | ||
247 | gettimeofday(&now, NULL); | |
248 | ||
249 | #endif | |
250 | ||
251 | debugs(42, 8, HERE << n << " bytes from " << preply.from); | |
252 | ||
253 | // FIXME INET6 : The IPv6 Header (ip6_hdr) is not availble directly >:-( | |
254 | // | |
255 | // TTL still has to come from the IP header somewhere. | |
256 | // still need to strip and process it properly. | |
257 | // probably have to rely on RTT as given by timestamp in data sent and current. | |
26ac0430 AJ |
258 | /* IPv6 Header Structures (linux) |
259 | struct ip6_hdr | |
cc192b50 | 260 | |
26ac0430 AJ |
261 | // fields (via simple define) |
262 | #define ip6_vfc // N.A | |
263 | #define ip6_flow // N/A | |
264 | #define ip6_plen // payload length. | |
265 | #define ip6_nxt // expect to be type 0x3a - ICMPv6 | |
266 | #define ip6_hlim // MAX hops (always 64, but no guarantee) | |
267 | #define ip6_hops // HOPS!!! (can it be true??) | |
cc192b50 | 268 | |
269 | ||
26ac0430 AJ |
270 | ip = (struct ip6_hdr *) pkt; |
271 | pkt += sizeof(ip6_hdr); | |
cc192b50 | 272 | |
26ac0430 AJ |
273 | debugs(42,0, HERE << "ip6_nxt=" << ip->ip6_nxt << |
274 | ", ip6_plen=" << ip->ip6_plen << | |
275 | ", ip6_hlim=" << ip->ip6_hlim << | |
276 | ", ip6_hops=" << ip->ip6_hops << | |
277 | " ::: 40 == sizef(ip6_hdr) == " << sizeof(ip6_hdr) | |
278 | ); | |
279 | */ | |
cc192b50 | 280 | |
b0365bd9 | 281 | icmp6header = (struct icmp6_hdr *) pkt; |
cc192b50 | 282 | pkt += sizeof(icmp6_hdr); |
283 | ||
b0365bd9 | 284 | if (icmp6header->icmp6_type != ICMP6_ECHO_REPLY) { |
cc192b50 | 285 | |
b0365bd9 | 286 | switch (icmp6header->icmp6_type) { |
26ac0430 AJ |
287 | case 134: |
288 | case 135: | |
289 | case 136: | |
290 | /* ignore Router/Neighbour Advertisements */ | |
cc192b50 | 291 | break; |
292 | ||
26ac0430 | 293 | default: |
b0365bd9 FC |
294 | debugs(42, 8, HERE << preply.from << " said: " << icmp6header->icmp6_type << "/" << (int)icmp6header->icmp6_code << " " << |
295 | ( icmp6header->icmp6_type&0x80 ? icmp6HighPktStr[(int)(icmp6header->icmp6_type&0x7f)] : icmp6LowPktStr[(int)(icmp6header->icmp6_type&0x7f)] ) | |
cc192b50 | 296 | ); |
297 | } | |
298 | return; | |
299 | } | |
300 | ||
b0365bd9 FC |
301 | if (icmp6header->icmp6_id != icmp_ident) { |
302 | debugs(42, 8, HERE << "dropping Icmp6 read. IDENT check failed. ident=='" << icmp_ident << "'=='" << icmp6header->icmp6_id << "'"); | |
cc192b50 | 303 | return; |
304 | } | |
305 | ||
306 | echo = (icmpEchoData *) pkt; | |
307 | ||
308 | preply.opcode = echo->opcode; | |
309 | ||
c3c6695b | 310 | struct timeval tv; |
311 | memcpy(&tv, &echo->tv, sizeof(struct timeval)); | |
312 | preply.rtt = tvSubMsec(tv, now); | |
cc192b50 | 313 | |
26ac0430 AJ |
314 | /* |
315 | * FIXME INET6: Without access to the IPv6-Hops header we must rely on the total RTT | |
316 | * and could caculate the hops from that, but it produces some weird value mappings using ipHops | |
317 | * for now everything is 1 v6 hop away with variant RTT | |
318 | * WANT: preply.hops = ip->ip6_hops; // ipHops(ip->ip_hops); | |
319 | */ | |
cc192b50 | 320 | preply.hops = 1; |
321 | ||
322 | preply.psize = n - /* sizeof(ip6_hdr) - */ sizeof(icmp6_hdr) - (sizeof(icmpEchoData) - MAX_PKT6_SZ); | |
323 | ||
324 | /* Ensure the response packet has safe payload size */ | |
26ac0430 | 325 | if ( preply.psize > (unsigned short) MAX_PKT6_SZ) { |
cc192b50 | 326 | preply.psize = MAX_PKT6_SZ; |
26ac0430 | 327 | } else if ( preply.psize < (unsigned short)0) { |
cc192b50 | 328 | preply.psize = 0; |
329 | } | |
330 | ||
331 | Log(preply.from, | |
b0365bd9 FC |
332 | icmp6header->icmp6_type, |
333 | ( icmp6header->icmp6_type&0x80 ? icmp6HighPktStr[(int)(icmp6header->icmp6_type&0x7f)] : icmp6LowPktStr[(int)(icmp6header->icmp6_type&0x7f)] ), | |
cc192b50 | 334 | preply.rtt, |
335 | preply.hops); | |
336 | ||
337 | /* send results of the lookup back to squid.*/ | |
338 | control.SendResult(preply, (sizeof(pingerReplyData) - PINGER_PAYLOAD_SZ + preply.psize) ); | |
339 | } | |
340 | ||
055421ee | 341 | #endif /* USE_ICMP */ |