65 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
67 #include <linux/module.h>
68 #include <linux/types.h>
70 #include <linux/kernel.h>
71 #include <linux/fcntl.h>
72 #include <linux/socket.h>
76 #include <linux/netdevice.h>
77 #include <linux/string.h>
78 #include <linux/netfilter_ipv4.h>
79 #include <linux/slab.h>
91 #include <linux/errno.h>
94 #include <asm/uaccess.h>
211 static inline struct sock *icmp_xmit_lock(
struct net *
net)
229 static inline void icmp_xmit_unlock(
struct sock *sk)
238 static inline bool icmpv4_xrlim_allow(
struct net *net,
struct rtable *rt,
256 if ((1 << type) & net->
ipv4.sysctl_icmp_ratemask) {
259 net->
ipv4.sysctl_icmp_ratelimit);
280 static int icmp_glue_bits(
void *
from,
char *to,
int offset,
int len,
int odd,
287 icmp_param->
offset + offset,
290 skb->
csum = csum_block_add(skb->
csum, csum, odd);
291 if (icmp_pointers[icmp_param->
data.icmph.type].error)
292 nf_ct_attach(skb, icmp_param->
skb);
296 static void icmp_push_reply(
struct icmp_bxm *icmp_param,
303 sk = icmp_sk(dev_net((*rt)->dst.dev));
311 struct icmphdr *icmph = icmp_hdr(skb);
316 csum = csum_add(csum, skb1->
csum);
331 static void icmp_reply(
struct icmp_bxm *icmp_param,
struct sk_buff *skb)
334 struct rtable *rt = skb_rtable(skb);
335 struct net *net = dev_net(rt->
dst.dev);
344 sk = icmp_xmit_lock(net);
349 icmp_param->
data.icmph.checksum = 0;
351 inet->
tos = ip_hdr(skb)->tos;
352 daddr = ipc.
addr = ip_hdr(skb)->saddr;
356 if (icmp_param->
replyopts.opt.opt.optlen) {
358 if (ipc.
opt->opt.srr)
359 daddr = icmp_param->
replyopts.opt.opt.faddr;
361 memset(&fl4, 0,
sizeof(fl4));
364 fl4.flowi4_tos =
RT_TOS(ip_hdr(skb)->
tos);
366 security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
367 rt = ip_route_output_key(net, &fl4);
370 if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->
data.icmph.type,
371 icmp_param->
data.icmph.code))
372 icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
375 icmp_xmit_unlock(sk);
378 static struct rtable *icmp_route_lookup(
struct net *net,
381 const struct iphdr *iph,
390 memset(fl4, 0,
sizeof(*fl4));
394 fl4->flowi4_tos =
RT_TOS(tos);
396 fl4->fl4_icmp_type =
type;
397 fl4->fl4_icmp_code =
code;
398 security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
407 flowi4_to_flowi(fl4),
NULL, 0);
411 }
else if (PTR_ERR(rt) == -
EPERM) {
416 err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec),
AF_INET);
418 goto relookup_failed;
426 unsigned long orefdst;
428 fl4_2.
daddr = fl4_dec.saddr;
429 rt2 = ip_route_output_key(net, &fl4_2);
432 goto relookup_failed;
436 err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
440 rt2 = skb_rtable(skb_in);
445 goto relookup_failed;
448 flowi4_to_flowi(&fl4_dec),
NULL,
452 memcpy(fl4, &fl4_dec,
sizeof(*fl4));
454 }
else if (PTR_ERR(rt2) == -
EPERM) {
460 goto relookup_failed;
486 struct rtable *rt = skb_rtable(skb_in);
496 net = dev_net(rt->
dst.dev);
503 iph = ip_hdr(skb_in);
505 if ((
u8 *)iph < skb_in->
head ||
531 if (icmp_pointers[type].
error) {
537 u8 _inner_type, *itp;
539 itp = skb_header_pointer(skb_in,
540 skb_network_header(skb_in) +
555 icmp_pointers[*itp].error)
560 sk = icmp_xmit_lock(net);
573 if (rt_is_input_route(rt) &&
574 net->
ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
598 icmp_param.
data.icmph.un.gateway =
info;
599 icmp_param.
data.icmph.checksum = 0;
600 icmp_param.
skb = skb_in;
601 icmp_param.
offset = skb_network_offset(skb_in);
602 inet_sk(sk)->tos = tos;
607 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos,
608 type, code, &icmp_param);
612 if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
617 room = dst_mtu(&rt->
dst);
620 room -=
sizeof(
struct iphdr) + icmp_param.replyopts.opt.opt.optlen;
621 room -=
sizeof(
struct icmphdr);
623 icmp_param.data_len = skb_in->
len - icmp_param.offset;
624 if (icmp_param.data_len > room)
625 icmp_param.data_len = room;
626 icmp_param.head_len =
sizeof(
struct icmphdr);
628 icmp_push_reply(&icmp_param, &fl4, &ipc, &rt);
632 icmp_xmit_unlock(sk);
647 if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
654 if (ipprot && ipprot->err_handler)
655 ipprot->err_handler(skb, info);
663 static void icmp_unreach(
struct sk_buff *skb)
665 const struct iphdr *iph;
670 net = dev_net(skb_dst(skb)->
dev);
678 if (!pskb_may_pull(skb,
sizeof(
struct iphdr)))
681 icmph = icmp_hdr(skb);
688 switch (icmph->
code & 15) {
734 if (!net->
ipv4.sysctl_icmp_ignore_bogus_error_responses &&
736 net_warn_ratelimited(
"%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
743 icmp_socket_deliver(skb, info);
757 static void icmp_redirect(
struct sk_buff *skb)
759 if (skb->
len <
sizeof(
struct iphdr)) {
764 if (!pskb_may_pull(skb,
sizeof(
struct iphdr)))
767 icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
782 static void icmp_echo(
struct sk_buff *skb)
786 net = dev_net(skb_dst(skb)->
dev);
787 if (!net->
ipv4.sysctl_icmp_echo_ignore_all) {
790 icmp_param.
data.icmph = *icmp_hdr(skb);
796 icmp_reply(&icmp_param, skb);
807 static void icmp_timestamp(
struct sk_buff *skb)
823 icmp_param.
data.times[2] = icmp_param.
data.times[1];
826 icmp_param.
data.icmph = *icmp_hdr(skb);
828 icmp_param.
data.icmph.code = 0;
833 icmp_reply(&icmp_param, skb);
841 static void icmp_discard(
struct sk_buff *skb)
851 struct rtable *rt = skb_rtable(skb);
852 struct net *net = dev_net(rt->
dst.dev);
858 if (!(sp && sp->
xvec[sp->
len - 1]->props.flags &
862 if (!pskb_may_pull(skb,
sizeof(*icmph) +
sizeof(
struct iphdr)))
865 nh = skb_network_offset(skb);
866 skb_set_network_header(skb,
sizeof(*icmph));
871 skb_set_network_header(skb, nh);
878 if (!csum_fold(skb->
csum))
887 if (!pskb_pull(skb,
sizeof(*icmph)))
890 icmph = icmp_hdr(skb);
916 net->
ipv4.sysctl_icmp_echo_ignore_broadcasts) {
927 icmp_pointers[icmph->
type].handler(skb);
945 .handler = icmp_discard,
949 .handler = icmp_discard,
953 .handler = icmp_unreach,
957 .handler = icmp_unreach,
961 .handler = icmp_redirect,
965 .handler = icmp_discard,
969 .handler = icmp_discard,
973 .handler = icmp_echo,
976 .handler = icmp_discard,
980 .handler = icmp_discard,
984 .handler = icmp_unreach,
988 .handler = icmp_unreach,
992 .handler = icmp_timestamp,
995 .handler = icmp_discard,
998 .handler = icmp_discard,
1001 .handler = icmp_discard,
1004 .handler = icmp_discard,
1007 .handler = icmp_discard,
1011 static void __net_exit icmp_sk_exit(
struct net *net)
1016 inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
1017 kfree(net->ipv4.icmp_sk);
1018 net->ipv4.icmp_sk =
NULL;
1027 if (net->ipv4.icmp_sk == NULL)
1038 net->ipv4.icmp_sk[
i] =
sk;
1053 net->ipv4.sysctl_icmp_echo_ignore_all = 0;
1054 net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
1057 net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
1071 net->ipv4.sysctl_icmp_ratelimit = 1 *
HZ;
1072 net->ipv4.sysctl_icmp_ratemask = 0x1818;
1073 net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
1079 inet_ctl_sock_destroy(net->ipv4.icmp_sk[i]);
1080 kfree(net->ipv4.icmp_sk);
1085 .init = icmp_sk_init,
1086 .exit = icmp_sk_exit,