1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * NET3: Implementation of the ICMP protocol layer.
4 *
5 * Alan Cox, <alan@lxorguk.ukuu.org.uk>
6 *
7 * Some of the function names and the icmp unreach table for this
8 * module were derived from [icmp.c 1.0.11 06/02/93] by
9 * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
10 * Other than that this module is a complete rewrite.
11 *
12 * Fixes:
13 * Clemens Fruhwirth : introduce global icmp rate limiting
14 * with icmp type masking ability instead
15 * of broken per type icmp timeouts.
16 * Mike Shaver : RFC1122 checks.
17 * Alan Cox : Multicast ping reply as self.
18 * Alan Cox : Fix atomicity lockup in ip_build_xmit
19 * call.
20 * Alan Cox : Added 216,128 byte paths to the MTU
21 * code.
22 * Martin Mares : RFC1812 checks.
23 * Martin Mares : Can be configured to follow redirects
24 * if acting as a router _without_ a
25 * routing protocol (RFC 1812).
26 * Martin Mares : Echo requests may be configured to
27 * be ignored (RFC 1812).
28 * Martin Mares : Limitation of ICMP error message
29 * transmit rate (RFC 1812).
30 * Martin Mares : TOS and Precedence set correctly
31 * (RFC 1812).
32 * Martin Mares : Now copying as much data from the
33 * original packet as we can without
34 * exceeding 576 bytes (RFC 1812).
35 * Willy Konynenberg : Transparent proxying support.
36 * Keith Owens : RFC1191 correction for 4.2BSD based
37 * path MTU bug.
38 * Thomas Quinot : ICMP Dest Unreach codes up to 15 are
39 * valid (RFC 1812).
40 * Andi Kleen : Check all packet lengths properly
41 * and moved all kfree_skb() up to
42 * icmp_rcv.
43 * Andi Kleen : Move the rate limit bookkeeping
44 * into the dest entry and use a token
45 * bucket filter (thanks to ANK). Make
46 * the rates sysctl configurable.
47 * Yu Tianli : Fixed two ugly bugs in icmp_send
48 * - IP option length was accounted wrongly
49 * - ICMP header length was not accounted
50 * at all.
51 * Tristan Greaves : Added sysctl option to ignore bogus
52 * broadcast responses from broken routers.
53 *
54 * To Fix:
55 *
56 * - Should use skb_pull() instead of all the manual checking.
57 * This would also greatly simply some upper layer error handlers. --AK
58 */
59
60 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
61
62 #include <linux/module.h>
63 #include <linux/types.h>
64 #include <linux/jiffies.h>
65 #include <linux/kernel.h>
66 #include <linux/fcntl.h>
67 #include <linux/socket.h>
68 #include <linux/in.h>
69 #include <linux/inet.h>
70 #include <linux/inetdevice.h>
71 #include <linux/netdevice.h>
72 #include <linux/string.h>
73 #include <linux/netfilter_ipv4.h>
74 #include <linux/slab.h>
75 #include <net/snmp.h>
76 #include <net/ip.h>
77 #include <net/route.h>
78 #include <net/protocol.h>
79 #include <net/icmp.h>
80 #include <net/tcp.h>
81 #include <net/udp.h>
82 #include <net/raw.h>
83 #include <net/ping.h>
84 #include <linux/skbuff.h>
85 #include <net/sock.h>
86 #include <linux/errno.h>
87 #include <linux/timer.h>
88 #include <linux/init.h>
89 #include <linux/uaccess.h>
90 #include <net/checksum.h>
91 #include <net/xfrm.h>
92 #include <net/inet_common.h>
93 #include <net/ip_fib.h>
94 #include <net/l3mdev.h>
95 #include <net/addrconf.h>
96
97 /*
98 * Build xmit assembly blocks
99 */
100
101 struct icmp_bxm {
102 struct sk_buff *skb;
103 int offset;
104 int data_len;
105
106 struct {
107 struct icmphdr icmph;
108 __be32 times[3];
109 } data;
110 int head_len;
111 struct ip_options_data replyopts;
112 };
113
114 /* An array of errno for error messages from dest unreach. */
115 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
116
117 const struct icmp_err icmp_err_convert[] = {
118 {
119 .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
120 .fatal = 0,
121 },
122 {
123 .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
124 .fatal = 0,
125 },
126 {
127 .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
128 .fatal = 1,
129 },
130 {
131 .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
132 .fatal = 1,
133 },
134 {
135 .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
136 .fatal = 0,
137 },
138 {
139 .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
140 .fatal = 0,
141 },
142 {
143 .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
144 .fatal = 1,
145 },
146 {
147 .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
148 .fatal = 1,
149 },
150 {
151 .errno = ENONET, /* ICMP_HOST_ISOLATED */
152 .fatal = 1,
153 },
154 {
155 .errno = ENETUNREACH, /* ICMP_NET_ANO */
156 .fatal = 1,
157 },
158 {
159 .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
160 .fatal = 1,
161 },
162 {
163 .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
164 .fatal = 0,
165 },
166 {
167 .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
168 .fatal = 0,
169 },
170 {
171 .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
172 .fatal = 1,
173 },
174 {
175 .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
176 .fatal = 1,
177 },
178 {
179 .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
180 .fatal = 1,
181 },
182 };
183 EXPORT_SYMBOL(icmp_err_convert);
184
185 /*
186 * ICMP control array. This specifies what to do with each ICMP.
187 */
188
189 struct icmp_control {
190 enum skb_drop_reason (*handler)(struct sk_buff *skb);
191 short error; /* This ICMP is classed as an error message */
192 };
193
194 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
195
196 static DEFINE_PER_CPU(struct sock *, ipv4_icmp_sk);
197
198 /* Called with BH disabled */
icmp_xmit_lock(struct net * net)199 static inline struct sock *icmp_xmit_lock(struct net *net)
200 {
201 struct sock *sk;
202
203 sk = this_cpu_read(ipv4_icmp_sk);
204
205 if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
206 /* This can happen if the output path signals a
207 * dst_link_failure() for an outgoing ICMP packet.
208 */
209 return NULL;
210 }
211 sock_net_set(sk, net);
212 return sk;
213 }
214
icmp_xmit_unlock(struct sock * sk)215 static inline void icmp_xmit_unlock(struct sock *sk)
216 {
217 sock_net_set(sk, &init_net);
218 spin_unlock(&sk->sk_lock.slock);
219 }
220
221 int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
222 int sysctl_icmp_msgs_burst __read_mostly = 50;
223
224 static struct {
225 atomic_t credit;
226 u32 stamp;
227 } icmp_global;
228
229 /**
230 * icmp_global_allow - Are we allowed to send one more ICMP message ?
231 *
232 * Uses a token bucket to limit our ICMP messages to ~sysctl_icmp_msgs_per_sec.
233 * Returns false if we reached the limit and can not send another packet.
234 * Works in tandem with icmp_global_consume().
235 */
icmp_global_allow(void)236 bool icmp_global_allow(void)
237 {
238 u32 delta, now, oldstamp;
239 int incr, new, old;
240
241 /* Note: many cpus could find this condition true.
242 * Then later icmp_global_consume() could consume more credits,
243 * this is an acceptable race.
244 */
245 if (atomic_read(&icmp_global.credit) > 0)
246 return true;
247
248 now = jiffies;
249 oldstamp = READ_ONCE(icmp_global.stamp);
250 delta = min_t(u32, now - oldstamp, HZ);
251 if (delta < HZ / 50)
252 return false;
253
254 incr = READ_ONCE(sysctl_icmp_msgs_per_sec) * delta / HZ;
255 if (!incr)
256 return false;
257
258 if (cmpxchg(&icmp_global.stamp, oldstamp, now) == oldstamp) {
259 old = atomic_read(&icmp_global.credit);
260 do {
261 new = min(old + incr, READ_ONCE(sysctl_icmp_msgs_burst));
262 } while (!atomic_try_cmpxchg(&icmp_global.credit, &old, new));
263 }
264 return true;
265 }
266 EXPORT_SYMBOL(icmp_global_allow);
267
icmp_global_consume(void)268 void icmp_global_consume(void)
269 {
270 int credits = get_random_u32_below(3);
271
272 /* Note: this might make icmp_global.credit negative. */
273 if (credits)
274 atomic_sub(credits, &icmp_global.credit);
275 }
276 EXPORT_SYMBOL(icmp_global_consume);
277
icmpv4_mask_allow(struct net * net,int type,int code)278 static bool icmpv4_mask_allow(struct net *net, int type, int code)
279 {
280 if (type > NR_ICMP_TYPES)
281 return true;
282
283 /* Don't limit PMTU discovery. */
284 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
285 return true;
286
287 /* Limit if icmp type is enabled in ratemask. */
288 if (!((1 << type) & READ_ONCE(net->ipv4.sysctl_icmp_ratemask)))
289 return true;
290
291 return false;
292 }
293
icmpv4_global_allow(struct net * net,int type,int code,bool * apply_ratelimit)294 static bool icmpv4_global_allow(struct net *net, int type, int code,
295 bool *apply_ratelimit)
296 {
297 if (icmpv4_mask_allow(net, type, code))
298 return true;
299
300 if (icmp_global_allow()) {
301 *apply_ratelimit = true;
302 return true;
303 }
304 __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITGLOBAL);
305 return false;
306 }
307
308 /*
309 * Send an ICMP frame.
310 */
311
icmpv4_xrlim_allow(struct net * net,struct rtable * rt,struct flowi4 * fl4,int type,int code,bool apply_ratelimit)312 static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
313 struct flowi4 *fl4, int type, int code,
314 bool apply_ratelimit)
315 {
316 struct dst_entry *dst = &rt->dst;
317 struct inet_peer *peer;
318 bool rc = true;
319
320 if (!apply_ratelimit)
321 return true;
322
323 /* No rate limit on loopback */
324 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
325 goto out;
326
327 rcu_read_lock();
328 peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr,
329 l3mdev_master_ifindex_rcu(dst->dev));
330 rc = inet_peer_xrlim_allow(peer,
331 READ_ONCE(net->ipv4.sysctl_icmp_ratelimit));
332 rcu_read_unlock();
333 out:
334 if (!rc)
335 __ICMP_INC_STATS(net, ICMP_MIB_RATELIMITHOST);
336 else
337 icmp_global_consume();
338 return rc;
339 }
340
341 /*
342 * Maintain the counters used in the SNMP statistics for outgoing ICMP
343 */
icmp_out_count(struct net * net,unsigned char type)344 void icmp_out_count(struct net *net, unsigned char type)
345 {
346 ICMPMSGOUT_INC_STATS(net, type);
347 ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
348 }
349
350 /*
351 * Checksum each fragment, and on the first include the headers and final
352 * checksum.
353 */
icmp_glue_bits(void * from,char * to,int offset,int len,int odd,struct sk_buff * skb)354 static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
355 struct sk_buff *skb)
356 {
357 struct icmp_bxm *icmp_param = from;
358 __wsum csum;
359
360 csum = skb_copy_and_csum_bits(icmp_param->skb,
361 icmp_param->offset + offset,
362 to, len);
363
364 skb->csum = csum_block_add(skb->csum, csum, odd);
365 if (icmp_pointers[icmp_param->data.icmph.type].error)
366 nf_ct_attach(skb, icmp_param->skb);
367 return 0;
368 }
369
icmp_push_reply(struct sock * sk,struct icmp_bxm * icmp_param,struct flowi4 * fl4,struct ipcm_cookie * ipc,struct rtable ** rt)370 static void icmp_push_reply(struct sock *sk,
371 struct icmp_bxm *icmp_param,
372 struct flowi4 *fl4,
373 struct ipcm_cookie *ipc, struct rtable **rt)
374 {
375 struct sk_buff *skb;
376
377 if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
378 icmp_param->data_len+icmp_param->head_len,
379 icmp_param->head_len,
380 ipc, rt, MSG_DONTWAIT) < 0) {
381 __ICMP_INC_STATS(sock_net(sk), ICMP_MIB_OUTERRORS);
382 ip_flush_pending_frames(sk);
383 } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
384 struct icmphdr *icmph = icmp_hdr(skb);
385 __wsum csum;
386 struct sk_buff *skb1;
387
388 csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
389 (char *)icmph,
390 icmp_param->head_len);
391 skb_queue_walk(&sk->sk_write_queue, skb1) {
392 csum = csum_add(csum, skb1->csum);
393 }
394 icmph->checksum = csum_fold(csum);
395 skb->ip_summed = CHECKSUM_NONE;
396 ip_push_pending_frames(sk, fl4);
397 }
398 }
399
400 /*
401 * Driving logic for building and sending ICMP messages.
402 */
403
icmp_reply(struct icmp_bxm * icmp_param,struct sk_buff * skb)404 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
405 {
406 struct ipcm_cookie ipc;
407 struct rtable *rt = skb_rtable(skb);
408 struct net *net = dev_net(rt->dst.dev);
409 bool apply_ratelimit = false;
410 struct flowi4 fl4;
411 struct sock *sk;
412 struct inet_sock *inet;
413 __be32 daddr, saddr;
414 u32 mark = IP4_REPLY_MARK(net, skb->mark);
415 int type = icmp_param->data.icmph.type;
416 int code = icmp_param->data.icmph.code;
417
418 if (ip_options_echo(net, &icmp_param->replyopts.opt.opt, skb))
419 return;
420
421 /* Needed by both icmpv4_global_allow and icmp_xmit_lock */
422 local_bh_disable();
423
424 /* is global icmp_msgs_per_sec exhausted ? */
425 if (!icmpv4_global_allow(net, type, code, &apply_ratelimit))
426 goto out_bh_enable;
427
428 sk = icmp_xmit_lock(net);
429 if (!sk)
430 goto out_bh_enable;
431 inet = inet_sk(sk);
432
433 icmp_param->data.icmph.checksum = 0;
434
435 ipcm_init(&ipc);
436 inet->tos = ip_hdr(skb)->tos;
437 ipc.sockc.mark = mark;
438 daddr = ipc.addr = ip_hdr(skb)->saddr;
439 saddr = fib_compute_spec_dst(skb);
440
441 if (icmp_param->replyopts.opt.opt.optlen) {
442 ipc.opt = &icmp_param->replyopts.opt;
443 if (ipc.opt->opt.srr)
444 daddr = icmp_param->replyopts.opt.opt.faddr;
445 }
446 memset(&fl4, 0, sizeof(fl4));
447 fl4.daddr = daddr;
448 fl4.saddr = saddr;
449 fl4.flowi4_mark = mark;
450 fl4.flowi4_uid = sock_net_uid(net, NULL);
451 fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
452 fl4.flowi4_proto = IPPROTO_ICMP;
453 fl4.flowi4_oif = l3mdev_master_ifindex(skb->dev);
454 security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
455 rt = ip_route_output_key(net, &fl4);
456 if (IS_ERR(rt))
457 goto out_unlock;
458 if (icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
459 icmp_push_reply(sk, icmp_param, &fl4, &ipc, &rt);
460 ip_rt_put(rt);
461 out_unlock:
462 icmp_xmit_unlock(sk);
463 out_bh_enable:
464 local_bh_enable();
465 }
466
467 /*
468 * The device used for looking up which routing table to use for sending an ICMP
469 * error is preferably the source whenever it is set, which should ensure the
470 * icmp error can be sent to the source host, else lookup using the routing
471 * table of the destination device, else use the main routing table (index 0).
472 */
icmp_get_route_lookup_dev(struct sk_buff * skb)473 static struct net_device *icmp_get_route_lookup_dev(struct sk_buff *skb)
474 {
475 struct net_device *route_lookup_dev = NULL;
476
477 if (skb->dev)
478 route_lookup_dev = skb->dev;
479 else if (skb_dst(skb))
480 route_lookup_dev = skb_dst(skb)->dev;
481 return route_lookup_dev;
482 }
483
icmp_route_lookup(struct net * net,struct flowi4 * fl4,struct sk_buff * skb_in,const struct iphdr * iph,__be32 saddr,u8 tos,u32 mark,int type,int code,struct icmp_bxm * param)484 static struct rtable *icmp_route_lookup(struct net *net,
485 struct flowi4 *fl4,
486 struct sk_buff *skb_in,
487 const struct iphdr *iph,
488 __be32 saddr, u8 tos, u32 mark,
489 int type, int code,
490 struct icmp_bxm *param)
491 {
492 struct net_device *route_lookup_dev;
493 struct rtable *rt, *rt2;
494 struct flowi4 fl4_dec;
495 int err;
496
497 memset(fl4, 0, sizeof(*fl4));
498 fl4->daddr = (param->replyopts.opt.opt.srr ?
499 param->replyopts.opt.opt.faddr : iph->saddr);
500 fl4->saddr = saddr;
501 fl4->flowi4_mark = mark;
502 fl4->flowi4_uid = sock_net_uid(net, NULL);
503 fl4->flowi4_tos = RT_TOS(tos);
504 fl4->flowi4_proto = IPPROTO_ICMP;
505 fl4->fl4_icmp_type = type;
506 fl4->fl4_icmp_code = code;
507 route_lookup_dev = icmp_get_route_lookup_dev(skb_in);
508 fl4->flowi4_oif = l3mdev_master_ifindex(route_lookup_dev);
509
510 security_skb_classify_flow(skb_in, flowi4_to_flowi_common(fl4));
511 rt = ip_route_output_key_hash(net, fl4, skb_in);
512 if (IS_ERR(rt))
513 return rt;
514
515 /* No need to clone since we're just using its address. */
516 rt2 = rt;
517
518 rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
519 flowi4_to_flowi(fl4), NULL, 0);
520 if (!IS_ERR(rt)) {
521 if (rt != rt2)
522 return rt;
523 } else if (PTR_ERR(rt) == -EPERM) {
524 rt = NULL;
525 } else
526 return rt;
527
528 err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
529 if (err)
530 goto relookup_failed;
531
532 if (inet_addr_type_dev_table(net, route_lookup_dev,
533 fl4_dec.saddr) == RTN_LOCAL) {
534 rt2 = __ip_route_output_key(net, &fl4_dec);
535 if (IS_ERR(rt2))
536 err = PTR_ERR(rt2);
537 } else {
538 struct flowi4 fl4_2 = {};
539 unsigned long orefdst;
540
541 fl4_2.daddr = fl4_dec.saddr;
542 rt2 = ip_route_output_key(net, &fl4_2);
543 if (IS_ERR(rt2)) {
544 err = PTR_ERR(rt2);
545 goto relookup_failed;
546 }
547 /* Ugh! */
548 orefdst = skb_in->_skb_refdst; /* save old refdst */
549 skb_dst_set(skb_in, NULL);
550 err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
551 RT_TOS(tos), rt2->dst.dev);
552
553 dst_release(&rt2->dst);
554 rt2 = skb_rtable(skb_in);
555 skb_in->_skb_refdst = orefdst; /* restore old refdst */
556 }
557
558 if (err)
559 goto relookup_failed;
560
561 rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
562 flowi4_to_flowi(&fl4_dec), NULL,
563 XFRM_LOOKUP_ICMP);
564 if (!IS_ERR(rt2)) {
565 dst_release(&rt->dst);
566 memcpy(fl4, &fl4_dec, sizeof(*fl4));
567 rt = rt2;
568 } else if (PTR_ERR(rt2) == -EPERM) {
569 if (rt)
570 dst_release(&rt->dst);
571 return rt2;
572 } else {
573 err = PTR_ERR(rt2);
574 goto relookup_failed;
575 }
576 return rt;
577
578 relookup_failed:
579 if (rt)
580 return rt;
581 return ERR_PTR(err);
582 }
583
584 /*
585 * Send an ICMP message in response to a situation
586 *
587 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
588 * MAY send more (we do).
589 * MUST NOT change this header information.
590 * MUST NOT reply to a multicast/broadcast IP address.
591 * MUST NOT reply to a multicast/broadcast MAC address.
592 * MUST reply to only the first fragment.
593 */
594
__icmp_send(struct sk_buff * skb_in,int type,int code,__be32 info,const struct ip_options * opt)595 void __icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info,
596 const struct ip_options *opt)
597 {
598 struct iphdr *iph;
599 int room;
600 struct icmp_bxm icmp_param;
601 struct rtable *rt = skb_rtable(skb_in);
602 bool apply_ratelimit = false;
603 struct ipcm_cookie ipc;
604 struct flowi4 fl4;
605 __be32 saddr;
606 u8 tos;
607 u32 mark;
608 struct net *net;
609 struct sock *sk;
610
611 if (!rt)
612 goto out;
613
614 if (rt->dst.dev)
615 net = dev_net(rt->dst.dev);
616 else if (skb_in->dev)
617 net = dev_net(skb_in->dev);
618 else
619 goto out;
620
621 /*
622 * Find the original header. It is expected to be valid, of course.
623 * Check this, icmp_send is called from the most obscure devices
624 * sometimes.
625 */
626 iph = ip_hdr(skb_in);
627
628 if ((u8 *)iph < skb_in->head ||
629 (skb_network_header(skb_in) + sizeof(*iph)) >
630 skb_tail_pointer(skb_in))
631 goto out;
632
633 /*
634 * No replies to physical multicast/broadcast
635 */
636 if (skb_in->pkt_type != PACKET_HOST)
637 goto out;
638
639 /*
640 * Now check at the protocol level
641 */
642 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
643 goto out;
644
645 /*
646 * Only reply to fragment 0. We byte re-order the constant
647 * mask for efficiency.
648 */
649 if (iph->frag_off & htons(IP_OFFSET))
650 goto out;
651
652 /*
653 * If we send an ICMP error to an ICMP error a mess would result..
654 */
655 if (icmp_pointers[type].error) {
656 /*
657 * We are an error, check if we are replying to an
658 * ICMP error
659 */
660 if (iph->protocol == IPPROTO_ICMP) {
661 u8 _inner_type, *itp;
662
663 itp = skb_header_pointer(skb_in,
664 skb_network_header(skb_in) +
665 (iph->ihl << 2) +
666 offsetof(struct icmphdr,
667 type) -
668 skb_in->data,
669 sizeof(_inner_type),
670 &_inner_type);
671 if (!itp)
672 goto out;
673
674 /*
675 * Assume any unknown ICMP type is an error. This
676 * isn't specified by the RFC, but think about it..
677 */
678 if (*itp > NR_ICMP_TYPES ||
679 icmp_pointers[*itp].error)
680 goto out;
681 }
682 }
683
684 /* Needed by both icmpv4_global_allow and icmp_xmit_lock */
685 local_bh_disable();
686
687 /* Check global sysctl_icmp_msgs_per_sec ratelimit, unless
688 * incoming dev is loopback. If outgoing dev change to not be
689 * loopback, then peer ratelimit still work (in icmpv4_xrlim_allow)
690 */
691 if (!(skb_in->dev && (skb_in->dev->flags&IFF_LOOPBACK)) &&
692 !icmpv4_global_allow(net, type, code, &apply_ratelimit))
693 goto out_bh_enable;
694
695 sk = icmp_xmit_lock(net);
696 if (!sk)
697 goto out_bh_enable;
698
699 /*
700 * Construct source address and options.
701 */
702
703 saddr = iph->daddr;
704 if (!(rt->rt_flags & RTCF_LOCAL)) {
705 struct net_device *dev = NULL;
706
707 rcu_read_lock();
708 if (rt_is_input_route(rt) &&
709 READ_ONCE(net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr))
710 dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
711
712 if (dev)
713 saddr = inet_select_addr(dev, iph->saddr,
714 RT_SCOPE_LINK);
715 else
716 saddr = 0;
717 rcu_read_unlock();
718 }
719
720 tos = icmp_pointers[type].error ? (RT_TOS(iph->tos) |
721 IPTOS_PREC_INTERNETCONTROL) :
722 iph->tos;
723 mark = IP4_REPLY_MARK(net, skb_in->mark);
724
725 if (__ip_options_echo(net, &icmp_param.replyopts.opt.opt, skb_in, opt))
726 goto out_unlock;
727
728
729 /*
730 * Prepare data for ICMP header.
731 */
732
733 icmp_param.data.icmph.type = type;
734 icmp_param.data.icmph.code = code;
735 icmp_param.data.icmph.un.gateway = info;
736 icmp_param.data.icmph.checksum = 0;
737 icmp_param.skb = skb_in;
738 icmp_param.offset = skb_network_offset(skb_in);
739 inet_sk(sk)->tos = tos;
740 ipcm_init(&ipc);
741 ipc.addr = iph->saddr;
742 ipc.opt = &icmp_param.replyopts.opt;
743 ipc.sockc.mark = mark;
744
745 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
746 type, code, &icmp_param);
747 if (IS_ERR(rt))
748 goto out_unlock;
749
750 /* peer icmp_ratelimit */
751 if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code, apply_ratelimit))
752 goto ende;
753
754 /* RFC says return as much as we can without exceeding 576 bytes. */
755
756 room = dst_mtu(&rt->dst);
757 if (room > 576)
758 room = 576;
759 room -= sizeof(struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
760 room -= sizeof(struct icmphdr);
761 /* Guard against tiny mtu. We need to include at least one
762 * IP network header for this message to make any sense.
763 */
764 if (room <= (int)sizeof(struct iphdr))
765 goto ende;
766
767 icmp_param.data_len = skb_in->len - icmp_param.offset;
768 if (icmp_param.data_len > room)
769 icmp_param.data_len = room;
770 icmp_param.head_len = sizeof(struct icmphdr);
771
772 /* if we don't have a source address at this point, fall back to the
773 * dummy address instead of sending out a packet with a source address
774 * of 0.0.0.0
775 */
776 if (!fl4.saddr)
777 fl4.saddr = htonl(INADDR_DUMMY);
778
779 icmp_push_reply(sk, &icmp_param, &fl4, &ipc, &rt);
780 ende:
781 ip_rt_put(rt);
782 out_unlock:
783 icmp_xmit_unlock(sk);
784 out_bh_enable:
785 local_bh_enable();
786 out:;
787 }
788 EXPORT_SYMBOL(__icmp_send);
789
790 #if IS_ENABLED(CONFIG_NF_NAT)
791 #include <net/netfilter/nf_conntrack.h>
icmp_ndo_send(struct sk_buff * skb_in,int type,int code,__be32 info)792 void icmp_ndo_send(struct sk_buff *skb_in, int type, int code, __be32 info)
793 {
794 struct sk_buff *cloned_skb = NULL;
795 struct ip_options opts = { 0 };
796 enum ip_conntrack_info ctinfo;
797 struct nf_conn *ct;
798 __be32 orig_ip;
799
800 ct = nf_ct_get(skb_in, &ctinfo);
801 if (!ct || !(ct->status & IPS_SRC_NAT)) {
802 __icmp_send(skb_in, type, code, info, &opts);
803 return;
804 }
805
806 if (skb_shared(skb_in))
807 skb_in = cloned_skb = skb_clone(skb_in, GFP_ATOMIC);
808
809 if (unlikely(!skb_in || skb_network_header(skb_in) < skb_in->head ||
810 (skb_network_header(skb_in) + sizeof(struct iphdr)) >
811 skb_tail_pointer(skb_in) || skb_ensure_writable(skb_in,
812 skb_network_offset(skb_in) + sizeof(struct iphdr))))
813 goto out;
814
815 orig_ip = ip_hdr(skb_in)->saddr;
816 ip_hdr(skb_in)->saddr = ct->tuplehash[0].tuple.src.u3.ip;
817 __icmp_send(skb_in, type, code, info, &opts);
818 ip_hdr(skb_in)->saddr = orig_ip;
819 out:
820 consume_skb(cloned_skb);
821 }
822 EXPORT_SYMBOL(icmp_ndo_send);
823 #endif
824
icmp_socket_deliver(struct sk_buff * skb,u32 info)825 static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
826 {
827 const struct iphdr *iph = (const struct iphdr *)skb->data;
828 const struct net_protocol *ipprot;
829 int protocol = iph->protocol;
830
831 /* Checkin full IP header plus 8 bytes of protocol to
832 * avoid additional coding at protocol handlers.
833 */
834 if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
835 __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
836 return;
837 }
838
839 raw_icmp_error(skb, protocol, info);
840
841 ipprot = rcu_dereference(inet_protos[protocol]);
842 if (ipprot && ipprot->err_handler)
843 ipprot->err_handler(skb, info);
844 }
845
icmp_tag_validation(int proto)846 static bool icmp_tag_validation(int proto)
847 {
848 bool ok;
849
850 rcu_read_lock();
851 ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
852 rcu_read_unlock();
853 return ok;
854 }
855
856 /*
857 * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEEDED, ICMP_QUENCH, and
858 * ICMP_PARAMETERPROB.
859 */
860
icmp_unreach(struct sk_buff * skb)861 static enum skb_drop_reason icmp_unreach(struct sk_buff *skb)
862 {
863 enum skb_drop_reason reason = SKB_NOT_DROPPED_YET;
864 const struct iphdr *iph;
865 struct icmphdr *icmph;
866 struct net *net;
867 u32 info = 0;
868
869 net = dev_net(skb_dst(skb)->dev);
870
871 /*
872 * Incomplete header ?
873 * Only checks for the IP header, there should be an
874 * additional check for longer headers in upper levels.
875 */
876
877 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
878 goto out_err;
879
880 icmph = icmp_hdr(skb);
881 iph = (const struct iphdr *)skb->data;
882
883 if (iph->ihl < 5) { /* Mangled header, drop. */
884 reason = SKB_DROP_REASON_IP_INHDR;
885 goto out_err;
886 }
887
888 switch (icmph->type) {
889 case ICMP_DEST_UNREACH:
890 switch (icmph->code & 15) {
891 case ICMP_NET_UNREACH:
892 case ICMP_HOST_UNREACH:
893 case ICMP_PROT_UNREACH:
894 case ICMP_PORT_UNREACH:
895 break;
896 case ICMP_FRAG_NEEDED:
897 /* for documentation of the ip_no_pmtu_disc
898 * values please see
899 * Documentation/networking/ip-sysctl.rst
900 */
901 switch (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) {
902 default:
903 net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
904 &iph->daddr);
905 break;
906 case 2:
907 goto out;
908 case 3:
909 if (!icmp_tag_validation(iph->protocol))
910 goto out;
911 fallthrough;
912 case 0:
913 info = ntohs(icmph->un.frag.mtu);
914 }
915 break;
916 case ICMP_SR_FAILED:
917 net_dbg_ratelimited("%pI4: Source Route Failed\n",
918 &iph->daddr);
919 break;
920 default:
921 break;
922 }
923 if (icmph->code > NR_ICMP_UNREACH)
924 goto out;
925 break;
926 case ICMP_PARAMETERPROB:
927 info = ntohl(icmph->un.gateway) >> 24;
928 break;
929 case ICMP_TIME_EXCEEDED:
930 __ICMP_INC_STATS(net, ICMP_MIB_INTIMEEXCDS);
931 if (icmph->code == ICMP_EXC_FRAGTIME)
932 goto out;
933 break;
934 }
935
936 /*
937 * Throw it at our lower layers
938 *
939 * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
940 * header.
941 * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
942 * transport layer.
943 * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
944 * transport layer.
945 */
946
947 /*
948 * Check the other end isn't violating RFC 1122. Some routers send
949 * bogus responses to broadcast frames. If you see this message
950 * first check your netmask matches at both ends, if it does then
951 * get the other vendor to fix their kit.
952 */
953
954 if (!READ_ONCE(net->ipv4.sysctl_icmp_ignore_bogus_error_responses) &&
955 inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
956 net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
957 &ip_hdr(skb)->saddr,
958 icmph->type, icmph->code,
959 &iph->daddr, skb->dev->name);
960 goto out;
961 }
962
963 icmp_socket_deliver(skb, info);
964
965 out:
966 return reason;
967 out_err:
968 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
969 return reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
970 }
971
972
973 /*
974 * Handle ICMP_REDIRECT.
975 */
976
icmp_redirect(struct sk_buff * skb)977 static enum skb_drop_reason icmp_redirect(struct sk_buff *skb)
978 {
979 if (skb->len < sizeof(struct iphdr)) {
980 __ICMP_INC_STATS(dev_net(skb->dev), ICMP_MIB_INERRORS);
981 return SKB_DROP_REASON_PKT_TOO_SMALL;
982 }
983
984 if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
985 /* there aught to be a stat */
986 return SKB_DROP_REASON_NOMEM;
987 }
988
989 icmp_socket_deliver(skb, ntohl(icmp_hdr(skb)->un.gateway));
990 return SKB_NOT_DROPPED_YET;
991 }
992
993 /*
994 * Handle ICMP_ECHO ("ping") and ICMP_EXT_ECHO ("PROBE") requests.
995 *
996 * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
997 * requests.
998 * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
999 * included in the reply.
1000 * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
1001 * echo requests, MUST have default=NOT.
1002 * RFC 8335: 8 MUST have a config option to enable/disable ICMP
1003 * Extended Echo Functionality, MUST be disabled by default
1004 * See also WRT handling of options once they are done and working.
1005 */
1006
icmp_echo(struct sk_buff * skb)1007 static enum skb_drop_reason icmp_echo(struct sk_buff *skb)
1008 {
1009 struct icmp_bxm icmp_param;
1010 struct net *net;
1011
1012 net = dev_net(skb_dst(skb)->dev);
1013 /* should there be an ICMP stat for ignored echos? */
1014 if (READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_all))
1015 return SKB_NOT_DROPPED_YET;
1016
1017 icmp_param.data.icmph = *icmp_hdr(skb);
1018 icmp_param.skb = skb;
1019 icmp_param.offset = 0;
1020 icmp_param.data_len = skb->len;
1021 icmp_param.head_len = sizeof(struct icmphdr);
1022
1023 if (icmp_param.data.icmph.type == ICMP_ECHO)
1024 icmp_param.data.icmph.type = ICMP_ECHOREPLY;
1025 else if (!icmp_build_probe(skb, &icmp_param.data.icmph))
1026 return SKB_NOT_DROPPED_YET;
1027
1028 icmp_reply(&icmp_param, skb);
1029 return SKB_NOT_DROPPED_YET;
1030 }
1031
1032 /* Helper for icmp_echo and icmpv6_echo_reply.
1033 * Searches for net_device that matches PROBE interface identifier
1034 * and builds PROBE reply message in icmphdr.
1035 *
1036 * Returns false if PROBE responses are disabled via sysctl
1037 */
1038
icmp_build_probe(struct sk_buff * skb,struct icmphdr * icmphdr)1039 bool icmp_build_probe(struct sk_buff *skb, struct icmphdr *icmphdr)
1040 {
1041 struct icmp_ext_hdr *ext_hdr, _ext_hdr;
1042 struct icmp_ext_echo_iio *iio, _iio;
1043 struct net *net = dev_net(skb->dev);
1044 struct inet6_dev *in6_dev;
1045 struct in_device *in_dev;
1046 struct net_device *dev;
1047 char buff[IFNAMSIZ];
1048 u16 ident_len;
1049 u8 status;
1050
1051 if (!READ_ONCE(net->ipv4.sysctl_icmp_echo_enable_probe))
1052 return false;
1053
1054 /* We currently only support probing interfaces on the proxy node
1055 * Check to ensure L-bit is set
1056 */
1057 if (!(ntohs(icmphdr->un.echo.sequence) & 1))
1058 return false;
1059 /* Clear status bits in reply message */
1060 icmphdr->un.echo.sequence &= htons(0xFF00);
1061 if (icmphdr->type == ICMP_EXT_ECHO)
1062 icmphdr->type = ICMP_EXT_ECHOREPLY;
1063 else
1064 icmphdr->type = ICMPV6_EXT_ECHO_REPLY;
1065 ext_hdr = skb_header_pointer(skb, 0, sizeof(_ext_hdr), &_ext_hdr);
1066 /* Size of iio is class_type dependent.
1067 * Only check header here and assign length based on ctype in the switch statement
1068 */
1069 iio = skb_header_pointer(skb, sizeof(_ext_hdr), sizeof(iio->extobj_hdr), &_iio);
1070 if (!ext_hdr || !iio)
1071 goto send_mal_query;
1072 if (ntohs(iio->extobj_hdr.length) <= sizeof(iio->extobj_hdr) ||
1073 ntohs(iio->extobj_hdr.length) > sizeof(_iio))
1074 goto send_mal_query;
1075 ident_len = ntohs(iio->extobj_hdr.length) - sizeof(iio->extobj_hdr);
1076 iio = skb_header_pointer(skb, sizeof(_ext_hdr),
1077 sizeof(iio->extobj_hdr) + ident_len, &_iio);
1078 if (!iio)
1079 goto send_mal_query;
1080
1081 status = 0;
1082 dev = NULL;
1083 switch (iio->extobj_hdr.class_type) {
1084 case ICMP_EXT_ECHO_CTYPE_NAME:
1085 if (ident_len >= IFNAMSIZ)
1086 goto send_mal_query;
1087 memset(buff, 0, sizeof(buff));
1088 memcpy(buff, &iio->ident.name, ident_len);
1089 dev = dev_get_by_name(net, buff);
1090 break;
1091 case ICMP_EXT_ECHO_CTYPE_INDEX:
1092 if (ident_len != sizeof(iio->ident.ifindex))
1093 goto send_mal_query;
1094 dev = dev_get_by_index(net, ntohl(iio->ident.ifindex));
1095 break;
1096 case ICMP_EXT_ECHO_CTYPE_ADDR:
1097 if (ident_len < sizeof(iio->ident.addr.ctype3_hdr) ||
1098 ident_len != sizeof(iio->ident.addr.ctype3_hdr) +
1099 iio->ident.addr.ctype3_hdr.addrlen)
1100 goto send_mal_query;
1101 switch (ntohs(iio->ident.addr.ctype3_hdr.afi)) {
1102 case ICMP_AFI_IP:
1103 if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in_addr))
1104 goto send_mal_query;
1105 dev = ip_dev_find(net, iio->ident.addr.ip_addr.ipv4_addr);
1106 break;
1107 #if IS_ENABLED(CONFIG_IPV6)
1108 case ICMP_AFI_IP6:
1109 if (iio->ident.addr.ctype3_hdr.addrlen != sizeof(struct in6_addr))
1110 goto send_mal_query;
1111 dev = ipv6_stub->ipv6_dev_find(net, &iio->ident.addr.ip_addr.ipv6_addr, dev);
1112 dev_hold(dev);
1113 break;
1114 #endif
1115 default:
1116 goto send_mal_query;
1117 }
1118 break;
1119 default:
1120 goto send_mal_query;
1121 }
1122 if (!dev) {
1123 icmphdr->code = ICMP_EXT_CODE_NO_IF;
1124 return true;
1125 }
1126 /* Fill bits in reply message */
1127 if (dev->flags & IFF_UP)
1128 status |= ICMP_EXT_ECHOREPLY_ACTIVE;
1129
1130 in_dev = __in_dev_get_rcu(dev);
1131 if (in_dev && rcu_access_pointer(in_dev->ifa_list))
1132 status |= ICMP_EXT_ECHOREPLY_IPV4;
1133
1134 in6_dev = __in6_dev_get(dev);
1135 if (in6_dev && !list_empty(&in6_dev->addr_list))
1136 status |= ICMP_EXT_ECHOREPLY_IPV6;
1137
1138 dev_put(dev);
1139 icmphdr->un.echo.sequence |= htons(status);
1140 return true;
1141 send_mal_query:
1142 icmphdr->code = ICMP_EXT_CODE_MAL_QUERY;
1143 return true;
1144 }
1145 EXPORT_SYMBOL_GPL(icmp_build_probe);
1146
1147 /*
1148 * Handle ICMP Timestamp requests.
1149 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
1150 * SHOULD be in the kernel for minimum random latency.
1151 * MUST be accurate to a few minutes.
1152 * MUST be updated at least at 15Hz.
1153 */
icmp_timestamp(struct sk_buff * skb)1154 static enum skb_drop_reason icmp_timestamp(struct sk_buff *skb)
1155 {
1156 struct icmp_bxm icmp_param;
1157 /*
1158 * Too short.
1159 */
1160 if (skb->len < 4)
1161 goto out_err;
1162
1163 /*
1164 * Fill in the current time as ms since midnight UT:
1165 */
1166 icmp_param.data.times[1] = inet_current_timestamp();
1167 icmp_param.data.times[2] = icmp_param.data.times[1];
1168
1169 BUG_ON(skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4));
1170
1171 icmp_param.data.icmph = *icmp_hdr(skb);
1172 icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
1173 icmp_param.data.icmph.code = 0;
1174 icmp_param.skb = skb;
1175 icmp_param.offset = 0;
1176 icmp_param.data_len = 0;
1177 icmp_param.head_len = sizeof(struct icmphdr) + 12;
1178 icmp_reply(&icmp_param, skb);
1179 return SKB_NOT_DROPPED_YET;
1180
1181 out_err:
1182 __ICMP_INC_STATS(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
1183 return SKB_DROP_REASON_PKT_TOO_SMALL;
1184 }
1185
icmp_discard(struct sk_buff * skb)1186 static enum skb_drop_reason icmp_discard(struct sk_buff *skb)
1187 {
1188 /* pretend it was a success */
1189 return SKB_NOT_DROPPED_YET;
1190 }
1191
1192 /*
1193 * Deal with incoming ICMP packets.
1194 */
icmp_rcv(struct sk_buff * skb)1195 int icmp_rcv(struct sk_buff *skb)
1196 {
1197 enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
1198 struct rtable *rt = skb_rtable(skb);
1199 struct net *net = dev_net(rt->dst.dev);
1200 struct icmphdr *icmph;
1201
1202 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1203 struct sec_path *sp = skb_sec_path(skb);
1204 int nh;
1205
1206 if (!(sp && sp->xvec[sp->len - 1]->props.flags &
1207 XFRM_STATE_ICMP)) {
1208 reason = SKB_DROP_REASON_XFRM_POLICY;
1209 goto drop;
1210 }
1211
1212 if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
1213 goto drop;
1214
1215 nh = skb_network_offset(skb);
1216 skb_set_network_header(skb, sizeof(*icmph));
1217
1218 if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN,
1219 skb)) {
1220 reason = SKB_DROP_REASON_XFRM_POLICY;
1221 goto drop;
1222 }
1223
1224 skb_set_network_header(skb, nh);
1225 }
1226
1227 __ICMP_INC_STATS(net, ICMP_MIB_INMSGS);
1228
1229 if (skb_checksum_simple_validate(skb))
1230 goto csum_error;
1231
1232 if (!pskb_pull(skb, sizeof(*icmph)))
1233 goto error;
1234
1235 icmph = icmp_hdr(skb);
1236
1237 ICMPMSGIN_INC_STATS(net, icmph->type);
1238
1239 /* Check for ICMP Extended Echo (PROBE) messages */
1240 if (icmph->type == ICMP_EXT_ECHO) {
1241 /* We can't use icmp_pointers[].handler() because it is an array of
1242 * size NR_ICMP_TYPES + 1 (19 elements) and PROBE has code 42.
1243 */
1244 reason = icmp_echo(skb);
1245 goto reason_check;
1246 }
1247
1248 if (icmph->type == ICMP_EXT_ECHOREPLY) {
1249 reason = ping_rcv(skb);
1250 goto reason_check;
1251 }
1252
1253 /*
1254 * 18 is the highest 'known' ICMP type. Anything else is a mystery
1255 *
1256 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
1257 * discarded.
1258 */
1259 if (icmph->type > NR_ICMP_TYPES) {
1260 reason = SKB_DROP_REASON_UNHANDLED_PROTO;
1261 goto error;
1262 }
1263
1264 /*
1265 * Parse the ICMP message
1266 */
1267
1268 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
1269 /*
1270 * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
1271 * silently ignored (we let user decide with a sysctl).
1272 * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
1273 * discarded if to broadcast/multicast.
1274 */
1275 if ((icmph->type == ICMP_ECHO ||
1276 icmph->type == ICMP_TIMESTAMP) &&
1277 READ_ONCE(net->ipv4.sysctl_icmp_echo_ignore_broadcasts)) {
1278 reason = SKB_DROP_REASON_INVALID_PROTO;
1279 goto error;
1280 }
1281 if (icmph->type != ICMP_ECHO &&
1282 icmph->type != ICMP_TIMESTAMP &&
1283 icmph->type != ICMP_ADDRESS &&
1284 icmph->type != ICMP_ADDRESSREPLY) {
1285 reason = SKB_DROP_REASON_INVALID_PROTO;
1286 goto error;
1287 }
1288 }
1289
1290 reason = icmp_pointers[icmph->type].handler(skb);
1291 reason_check:
1292 if (!reason) {
1293 consume_skb(skb);
1294 return NET_RX_SUCCESS;
1295 }
1296
1297 drop:
1298 kfree_skb_reason(skb, reason);
1299 return NET_RX_DROP;
1300 csum_error:
1301 reason = SKB_DROP_REASON_ICMP_CSUM;
1302 __ICMP_INC_STATS(net, ICMP_MIB_CSUMERRORS);
1303 error:
1304 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
1305 goto drop;
1306 }
1307
ip_icmp_error_rfc4884_validate(const struct sk_buff * skb,int off)1308 static bool ip_icmp_error_rfc4884_validate(const struct sk_buff *skb, int off)
1309 {
1310 struct icmp_extobj_hdr *objh, _objh;
1311 struct icmp_ext_hdr *exth, _exth;
1312 u16 olen;
1313
1314 exth = skb_header_pointer(skb, off, sizeof(_exth), &_exth);
1315 if (!exth)
1316 return false;
1317 if (exth->version != 2)
1318 return true;
1319
1320 if (exth->checksum &&
1321 csum_fold(skb_checksum(skb, off, skb->len - off, 0)))
1322 return false;
1323
1324 off += sizeof(_exth);
1325 while (off < skb->len) {
1326 objh = skb_header_pointer(skb, off, sizeof(_objh), &_objh);
1327 if (!objh)
1328 return false;
1329
1330 olen = ntohs(objh->length);
1331 if (olen < sizeof(_objh))
1332 return false;
1333
1334 off += olen;
1335 if (off > skb->len)
1336 return false;
1337 }
1338
1339 return true;
1340 }
1341
ip_icmp_error_rfc4884(const struct sk_buff * skb,struct sock_ee_data_rfc4884 * out,int thlen,int off)1342 void ip_icmp_error_rfc4884(const struct sk_buff *skb,
1343 struct sock_ee_data_rfc4884 *out,
1344 int thlen, int off)
1345 {
1346 int hlen;
1347
1348 /* original datagram headers: end of icmph to payload (skb->data) */
1349 hlen = -skb_transport_offset(skb) - thlen;
1350
1351 /* per rfc 4884: minimal datagram length of 128 bytes */
1352 if (off < 128 || off < hlen)
1353 return;
1354
1355 /* kernel has stripped headers: return payload offset in bytes */
1356 off -= hlen;
1357 if (off + sizeof(struct icmp_ext_hdr) > skb->len)
1358 return;
1359
1360 out->len = off;
1361
1362 if (!ip_icmp_error_rfc4884_validate(skb, off))
1363 out->flags |= SO_EE_RFC4884_FLAG_INVALID;
1364 }
1365 EXPORT_SYMBOL_GPL(ip_icmp_error_rfc4884);
1366
icmp_err(struct sk_buff * skb,u32 info)1367 int icmp_err(struct sk_buff *skb, u32 info)
1368 {
1369 struct iphdr *iph = (struct iphdr *)skb->data;
1370 int offset = iph->ihl<<2;
1371 struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
1372 int type = icmp_hdr(skb)->type;
1373 int code = icmp_hdr(skb)->code;
1374 struct net *net = dev_net(skb->dev);
1375
1376 /*
1377 * Use ping_err to handle all icmp errors except those
1378 * triggered by ICMP_ECHOREPLY which sent from kernel.
1379 */
1380 if (icmph->type != ICMP_ECHOREPLY) {
1381 ping_err(skb, offset, info);
1382 return 0;
1383 }
1384
1385 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
1386 ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ICMP);
1387 else if (type == ICMP_REDIRECT)
1388 ipv4_redirect(skb, net, 0, IPPROTO_ICMP);
1389
1390 return 0;
1391 }
1392
1393 /*
1394 * This table is the definition of how we handle ICMP.
1395 */
1396 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1397 [ICMP_ECHOREPLY] = {
1398 .handler = ping_rcv,
1399 },
1400 [1] = {
1401 .handler = icmp_discard,
1402 .error = 1,
1403 },
1404 [2] = {
1405 .handler = icmp_discard,
1406 .error = 1,
1407 },
1408 [ICMP_DEST_UNREACH] = {
1409 .handler = icmp_unreach,
1410 .error = 1,
1411 },
1412 [ICMP_SOURCE_QUENCH] = {
1413 .handler = icmp_unreach,
1414 .error = 1,
1415 },
1416 [ICMP_REDIRECT] = {
1417 .handler = icmp_redirect,
1418 .error = 1,
1419 },
1420 [6] = {
1421 .handler = icmp_discard,
1422 .error = 1,
1423 },
1424 [7] = {
1425 .handler = icmp_discard,
1426 .error = 1,
1427 },
1428 [ICMP_ECHO] = {
1429 .handler = icmp_echo,
1430 },
1431 [9] = {
1432 .handler = icmp_discard,
1433 .error = 1,
1434 },
1435 [10] = {
1436 .handler = icmp_discard,
1437 .error = 1,
1438 },
1439 [ICMP_TIME_EXCEEDED] = {
1440 .handler = icmp_unreach,
1441 .error = 1,
1442 },
1443 [ICMP_PARAMETERPROB] = {
1444 .handler = icmp_unreach,
1445 .error = 1,
1446 },
1447 [ICMP_TIMESTAMP] = {
1448 .handler = icmp_timestamp,
1449 },
1450 [ICMP_TIMESTAMPREPLY] = {
1451 .handler = icmp_discard,
1452 },
1453 [ICMP_INFO_REQUEST] = {
1454 .handler = icmp_discard,
1455 },
1456 [ICMP_INFO_REPLY] = {
1457 .handler = icmp_discard,
1458 },
1459 [ICMP_ADDRESS] = {
1460 .handler = icmp_discard,
1461 },
1462 [ICMP_ADDRESSREPLY] = {
1463 .handler = icmp_discard,
1464 },
1465 };
1466
icmp_sk_init(struct net * net)1467 static int __net_init icmp_sk_init(struct net *net)
1468 {
1469 /* Control parameters for ECHO replies. */
1470 net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1471 net->ipv4.sysctl_icmp_echo_enable_probe = 0;
1472 net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1473
1474 /* Control parameter - ignore bogus broadcast responses? */
1475 net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1476
1477 /*
1478 * Configurable global rate limit.
1479 *
1480 * ratelimit defines tokens/packet consumed for dst->rate_token
1481 * bucket ratemask defines which icmp types are ratelimited by
1482 * setting it's bit position.
1483 *
1484 * default:
1485 * dest unreachable (3), source quench (4),
1486 * time exceeded (11), parameter problem (12)
1487 */
1488
1489 net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
1490 net->ipv4.sysctl_icmp_ratemask = 0x1818;
1491 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1492
1493 return 0;
1494 }
1495
1496 static struct pernet_operations __net_initdata icmp_sk_ops = {
1497 .init = icmp_sk_init,
1498 };
1499
icmp_init(void)1500 int __init icmp_init(void)
1501 {
1502 int err, i;
1503
1504 for_each_possible_cpu(i) {
1505 struct sock *sk;
1506
1507 err = inet_ctl_sock_create(&sk, PF_INET,
1508 SOCK_RAW, IPPROTO_ICMP, &init_net);
1509 if (err < 0)
1510 return err;
1511
1512 per_cpu(ipv4_icmp_sk, i) = sk;
1513
1514 /* Enough space for 2 64K ICMP packets, including
1515 * sk_buff/skb_shared_info struct overhead.
1516 */
1517 sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
1518
1519 /*
1520 * Speedup sock_wfree()
1521 */
1522 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1523 inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
1524 }
1525 return register_pernet_subsys(&icmp_sk_ops);
1526 }
1527