Icmp6.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 42 ICMP Pinger program */
10 
11 //#define SQUID_HELPER 1
12 
13 #include "squid.h"
14 
15 #if USE_ICMP
16 
17 #include "compat/socket.h"
18 #include "debug/Stream.h"
19 #include "Icmp6.h"
20 #include "IcmpPinger.h"
21 #include "time/gadgets.h"
22 
23 // Some system headers are only needed internally here.
24 // They should not be included via the header.
25 
26 #if HAVE_NETINET_IP6_H
27 #include <netinet/ip6.h>
28 #endif
29 
30 // Icmp6 OP-Codes
31 // see http://www.iana.org/assignments/icmpv6-parameters
32 static const char *
33 IcmpPacketType(uint8_t v)
34 {
35  // NP: LowPktStr is for codes 0-127
36  static const char *icmp6LowPktStr[] = {
37  "ICMPv6 0", // 0
38  "Destination Unreachable", // 1 - RFC2463
39  "Packet Too Big", // 2 - RFC2463
40  "Time Exceeded", // 3 - RFC2463
41  "Parameter Problem", // 4 - RFC2463
42  };
43 
44  // low codes 1-4 registered
45  if (0 < v && v < 5)
46  return icmp6LowPktStr[(int)(v&0x7f)];
47 
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]
76  };
77 
78  // high codes 127-153 registered
79  if (127 < v && v < 154)
80  return icmp6HighPktStr[(int)(v&0x7f)];
81 
82  // give all others a generic display
83  static char buf[50];
84  snprintf(buf, sizeof(buf), "ICMPv6 %u", v);
85  return buf;
86 }
87 
89 {
90  ; // nothing new.
91 }
92 
94 {
95  Close();
96 }
97 
98 int
100 {
101  icmp_sock = xsocket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
102 
103  if (icmp_sock < 0) {
104  int xerrno = errno;
105  debugs(50, DBG_CRITICAL, MYNAME << " icmp_sock: " << xstrerr(xerrno));
106  return -1;
107  }
108 
109  icmp_ident = getpid() & 0xffff;
110  debugs(42, DBG_IMPORTANT, "ICMPv6 socket opened");
111 
112  return icmp_sock;
113 }
114 
118 void
119 Icmp6::SendEcho(Ip::Address &to, int opcode, const char *payload, int len)
120 {
121  int x;
122  LOCAL_ARRAY(char, pkt, MAX_PKT6_SZ);
123  struct icmp6_hdr *icmp = nullptr;
124  icmpEchoData *echo = nullptr;
125  struct addrinfo *S = nullptr;
126  size_t icmp6_pktsize = 0;
127 
128  static_assert(sizeof(*icmp) + sizeof(*echo) <= sizeof(pkt), "our custom ICMPv6 Echo payload fits the packet buffer");
129 
130  memset(pkt, '\0', MAX_PKT6_SZ);
131  icmp = (struct icmp6_hdr *)pkt;
132 
133  /*
134  * cevans - beware signed/unsigned issues in untrusted data from
135  * the network!!
136  */
137  if (len < 0) {
138  len = 0;
139  }
140 
141  // Construct Icmp6 ECHO header
142  icmp->icmp6_type = ICMP6_ECHO_REQUEST;
143  icmp->icmp6_code = 0;
144  icmp->icmp6_cksum = 0;
145  icmp->icmp6_id = icmp_ident;
146  icmp->icmp6_seq = (unsigned short) icmp_pkts_sent;
147  ++icmp_pkts_sent;
148 
149  icmp6_pktsize = sizeof(struct icmp6_hdr);
150 
151  // Fill Icmp6 ECHO data content
152  echo = reinterpret_cast<icmpEchoData *>(reinterpret_cast<char *>(pkt) + sizeof(*icmp));
153  echo->opcode = (unsigned char) opcode;
154  memcpy(&echo->tv, &current_time, sizeof(struct timeval));
155 
156  icmp6_pktsize += sizeof(struct timeval) + sizeof(char);
157 
158  if (payload) {
159  if (len > MAX_PAYLOAD)
160  len = MAX_PAYLOAD;
161 
162  memcpy(echo->payload, payload, len);
163 
164  icmp6_pktsize += len;
165  }
166 
167  icmp->icmp6_cksum = CheckSum((unsigned short *) icmp, icmp6_pktsize);
168 
169  to.getAddrInfo(S);
170  ((sockaddr_in6*)S->ai_addr)->sin6_port = 0;
171 
172  assert(icmp6_pktsize <= MAX_PKT6_SZ);
173 
174  debugs(42, 5, "Send Icmp6 packet to " << to << ".");
175 
176  x = xsendto(icmp_sock,
177  pkt,
178  icmp6_pktsize,
179  0,
180  S->ai_addr,
181  S->ai_addrlen);
182 
183  if (x < 0) {
184  int xerrno = errno;
185  debugs(42, DBG_IMPORTANT, MYNAME << "ERROR: sending to ICMPv6 packet to " << to << ": " << xstrerr(xerrno));
186  }
187  debugs(42,9, "x=" << x);
188 
189  Log(to, 0, nullptr, 0, 0);
191 }
192 
196 void
198 {
199  int n;
200  struct addrinfo *from = nullptr;
201 // struct ip6_hdr *ip = nullptr;
202  static char *pkt = nullptr;
203  struct icmp6_hdr *icmp6header = nullptr;
204  icmpEchoData *echo = nullptr;
205  struct timeval now;
206  static pingerReplyData preply;
207 
208  if (icmp_sock < 0) {
209  debugs(42, DBG_CRITICAL, "dropping ICMPv6 read. No socket!?");
210  return;
211  }
212 
213  if (pkt == nullptr) {
214  pkt = (char *)xmalloc(MAX_PKT6_SZ);
215  }
216 
217  Ip::Address::InitAddr(from);
218 
219  n = xrecvfrom(icmp_sock,
220  pkt,
221  MAX_PKT6_SZ,
222  0,
223  from->ai_addr,
224  &from->ai_addrlen);
225 
226  if (n <= 0) {
227  debugs(42, DBG_CRITICAL, "ERROR: when calling recvfrom() on ICMPv6 socket.");
228  Ip::Address::FreeAddr(from);
229  return;
230  }
231 
232  preply.from = *from;
233 
234 #if GETTIMEOFDAY_NO_TZP
235 
236  gettimeofday(&now);
237 
238 #else
239 
240  gettimeofday(&now, nullptr);
241 
242 #endif
243 
244  debugs(42, 8, n << " bytes from " << preply.from);
245 
246 // XXX: The IPv6 Header (ip6_hdr) is not available directly
247 //
248 // TTL still has to come from the IP header somewhere.
249 // still need to strip and process it properly.
250 // probably have to rely on RTT as given by timestamp in data sent and current.
251  /* IPv6 Header Structures (linux)
252  struct ip6_hdr
253 
254  // fields (via simple define)
255  #define ip6_vfc // N.A
256  #define ip6_flow // N/A
257  #define ip6_plen // payload length.
258  #define ip6_nxt // expect to be type 0x3a - ICMPv6
259  #define ip6_hlim // MAX hops (always 64, but no guarantee)
260  #define ip6_hops // HOPS!!! (can it be true??)
261 
262  ip = (struct ip6_hdr *) pkt;
263  // += sizeof(ip6_hdr);
264 
265  debugs(42, DBG_CRITICAL, "ip6_nxt=" << ip->ip6_nxt <<
266  ", ip6_plen=" << ip->ip6_plen <<
267  ", ip6_hlim=" << ip->ip6_hlim <<
268  ", ip6_hops=" << ip->ip6_hops <<
269  " ::: 40 == sizef(ip6_hdr) == " << sizeof(ip6_hdr)
270  );
271  */
272 
273  icmp6header = (struct icmp6_hdr *) pkt;
274 
275  if (icmp6header->icmp6_type != ICMP6_ECHO_REPLY) {
276 
277  switch (icmp6header->icmp6_type) {
278  case 134:
279  case 135:
280  case 136:
281  /* ignore Router/Neighbour Advertisements */
282  break;
283 
284  default:
285  debugs(42, 8, preply.from << " said: " << icmp6header->icmp6_type << "/" << (int)icmp6header->icmp6_code << " " <<
286  IcmpPacketType(icmp6header->icmp6_type));
287  }
288  Ip::Address::FreeAddr(from);
289  return;
290  }
291 
292  if (icmp6header->icmp6_id != icmp_ident) {
293  debugs(42, 8, "dropping Icmp6 read. IDENT check failed. ident=='" << icmp_ident << "'=='" << icmp6header->icmp6_id << "'");
294  Ip::Address::FreeAddr(from);
295  return;
296  }
297 
298  echo = (icmpEchoData *) (pkt + sizeof(icmp6_hdr));
299 
300  preply.opcode = echo->opcode;
301 
302  struct timeval tv;
303  memcpy(&tv, &echo->tv, sizeof(struct timeval));
304  preply.rtt = tvSubMsec(tv, now);
305 
306  /*
307  * Without access to the IPv6-Hops header we must rely on the total RTT
308  * and could calculate the hops from that, but it produces some weird value mappings using ipHops
309  * for now everything is 1 v6 hop away with variant RTT
310  * WANT: preply.hops = ip->ip6_hops; // ipHops(ip->ip_hops);
311  */
312  preply.hops = 1;
313 
314  preply.psize = n - /* sizeof(ip6_hdr) - */ sizeof(icmp6_hdr) - (sizeof(icmpEchoData) - MAX_PKT6_SZ);
315 
316  /* Ensure the response packet has safe payload size */
317  if ( preply.psize > (unsigned short) MAX_PKT6_SZ) {
318  preply.psize = MAX_PKT6_SZ;
319  } else if ( preply.psize < (unsigned short)0) {
320  preply.psize = 0;
321  }
322 
323  Log(preply.from,
324  icmp6header->icmp6_type,
325  IcmpPacketType(icmp6header->icmp6_type),
326  preply.rtt,
327  preply.hops);
328 
329  /* send results of the lookup back to squid.*/
330  control.SendResult(preply, (sizeof(pingerReplyData) - PINGER_PAYLOAD_SZ + preply.psize) );
331  Ip::Address::FreeAddr(from);
332 }
333 
334 #endif /* USE_ICMP */
335 
const char * xstrerr(int error)
Definition: xstrerror.cc:83
#define DBG_CRITICAL
Definition: Stream.h:37
#define xmalloc
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:62
#define PINGER_PAYLOAD_SZ
Definition: Icmp.h:16
IcmpPinger control
pinger helper contains one of these as a global object.
Definition: pinger.cc:92
int Open() override
Start pinger helper and initiate control channel.
Definition: Icmp6.cc:99
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:706
int icmp_ident
Definition: Icmp.h:122
virtual void Close()
Shutdown pinger helper and control channel.
Definition: Icmp.cc:26
int icmp_sock
Definition: Icmp.h:121
int tvSubMsec(struct timeval t1, struct timeval t2)
Definition: gadgets.cc:51
ssize_t xrecvfrom(int socketFd, void *buf, size_t bufLength, int flags, struct sockaddr *from, socklen_t *fromLength)
POSIX recvfrom(2) equivalent.
Definition: socket.h:104
struct timeval current_time
the current UNIX time in timeval {seconds, microseconds} format
Definition: gadgets.cc:18
void Recv(void) override
Definition: Icmp6.cc:197
#define MAX_PAYLOAD
Definition: Icmp.h:18
void SendEcho(Ip::Address &, int, const char *, int) override
Definition: Icmp6.cc:119
char payload[MAX_PAYLOAD]
Definition: Icmp.h:48
Icmp6()
Definition: Icmp6.cc:88
unsigned char opcode
Definition: Icmp.h:47
#define assert(EX)
Definition: assert.h:17
static const char * IcmpPacketType(uint8_t v)
Definition: Icmp6.cc:33
int xsocket(int domain, int type, int protocol)
POSIX socket(2) equivalent.
Definition: socket.h:128
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:619
void SendResult(pingerReplyData &preply, int len)
Send ICMP results back to squid.
Definition: IcmpPinger.cc:220
ssize_t xsendto(int socketFd, const void *buf, size_t bufLength, int flags, const struct sockaddr *to, socklen_t toLength)
POSIX sendto(2) equivalent.
Definition: socket.h:116
struct timeval tv
Definition: Icmp.h:46
#define DBG_IMPORTANT
Definition: Stream.h:38
#define MYNAME
Definition: Stream.h:219
int CheckSum(unsigned short *ptr, int size)
Calculate a packet checksum.
Definition: Icmp.cc:39
int icmp_pkts_sent
Definition: pinger.cc:96
#define IPPROTO_ICMPV6
Definition: Icmp6.h:38
#define MAX_PKT6_SZ
Definition: Icmp.h:20
~Icmp6() override
Definition: Icmp6.cc:93
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
void Log(const Ip::Address &addr, const uint8_t type, const char *pkt_str, const int rtt, const int hops)
Log the packet.
Definition: Icmp.cc:89
static void InitAddr(struct addrinfo *&ai)
Definition: Address.cc:688
int unsigned int
Definition: stub_fd.cc:19
Definition: Icmp.h:67

 

Introduction

Documentation

Support

Miscellaneous