1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/jiffies.h>
- #include <linux/kernel.h>
- #include <linux/fcntl.h>
- #include <linux/socket.h>
- #include <linux/in.h>
- #include <linux/inet.h>
- #include <linux/inetdevice.h>
- #include <linux/netdevice.h>
- #include <linux/string.h>
- #include <linux/netfilter_ipv4.h>
- #include <linux/slab.h>
- #include <net/snmp.h>
- #include <net/ip.h>
- #include <net/route.h>
- #include <net/protocol.h>
- #include <net/icmp.h>
- #include <net/tcp.h>
- #include <net/udp.h>
- #include <net/raw.h>
- #include <net/ping.h>
- #include <linux/skbuff.h>
- #include <net/sock.h>
- #include <linux/errno.h>
- #include <linux/timer.h>
- #include <linux/init.h>
- #include <asm/uaccess.h>
- #include <net/checksum.h>
- #include <net/xfrm.h>
- #include <net/inet_common.h>
- #include <net/ip_fib.h>
- struct icmp_bxm {
- struct sk_buff *skb;
- int offset;
- int data_len;
- struct {
- struct icmphdr icmph;
- __be32 times[3];
- } data;
- int head_len;
- struct ip_options_data replyopts;
- };
- const struct icmp_err icmp_err_convert[] = {
- {
- .errno = ENETUNREACH,
- .fatal = 0,
- },
- {
- .errno = EHOSTUNREACH,
- .fatal = 0,
- },
- {
- .errno = ENOPROTOOPT ,
- .fatal = 1,
- },
- {
- .errno = ECONNREFUSED,
- .fatal = 1,
- },
- {
- .errno = EMSGSIZE,
- .fatal = 0,
- },
- {
- .errno = EOPNOTSUPP,
- .fatal = 0,
- },
- {
- .errno = ENETUNREACH,
- .fatal = 1,
- },
- {
- .errno = EHOSTDOWN,
- .fatal = 1,
- },
- {
- .errno = ENONET,
- .fatal = 1,
- },
- {
- .errno = ENETUNREACH,
- .fatal = 1,
- },
- {
- .errno = EHOSTUNREACH,
- .fatal = 1,
- },
- {
- .errno = ENETUNREACH,
- .fatal = 0,
- },
- {
- .errno = EHOSTUNREACH,
- .fatal = 0,
- },
- {
- .errno = EHOSTUNREACH,
- .fatal = 1,
- },
- {
- .errno = EHOSTUNREACH,
- .fatal = 1,
- },
- {
- .errno = EHOSTUNREACH,
- .fatal = 1,
- },
- };
- EXPORT_SYMBOL(icmp_err_convert);
- struct icmp_control {
- bool (*handler)(struct sk_buff *skb);
- short error;
- };
- static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
- static struct sock *icmp_sk(struct net *net)
- {
- return *this_cpu_ptr(net->ipv4.icmp_sk);
- }
- static inline struct sock *icmp_xmit_lock(struct net *net)
- {
- struct sock *sk;
- local_bh_disable();
- sk = icmp_sk(net);
- if (unlikely(!spin_trylock(&sk->sk_lock.slock))) {
-
- local_bh_enable();
- return NULL;
- }
- return sk;
- }
- static inline void icmp_xmit_unlock(struct sock *sk)
- {
- spin_unlock_bh(&sk->sk_lock.slock);
- }
- int sysctl_icmp_msgs_per_sec __read_mostly = 1000;
- int sysctl_icmp_msgs_burst __read_mostly = 50;
- static struct {
- spinlock_t lock;
- u32 credit;
- u32 stamp;
- } icmp_global = {
- .lock = __SPIN_LOCK_UNLOCKED(icmp_global.lock),
- };
- bool icmp_global_allow(void)
- {
- u32 credit, delta, incr = 0, now = (u32)jiffies;
- bool rc = false;
-
- if (!icmp_global.credit) {
- delta = min_t(u32, now - icmp_global.stamp, HZ);
- if (delta < HZ / 50)
- return false;
- }
- spin_lock(&icmp_global.lock);
- delta = min_t(u32, now - icmp_global.stamp, HZ);
- if (delta >= HZ / 50) {
- incr = sysctl_icmp_msgs_per_sec * delta / HZ ;
- if (incr)
- icmp_global.stamp = now;
- }
- credit = min_t(u32, icmp_global.credit + incr, sysctl_icmp_msgs_burst);
- if (credit) {
- credit--;
- rc = true;
- }
- icmp_global.credit = credit;
- spin_unlock(&icmp_global.lock);
- return rc;
- }
- EXPORT_SYMBOL(icmp_global_allow);
- static bool icmpv4_xrlim_allow(struct net *net, struct rtable *rt,
- struct flowi4 *fl4, int type, int code)
- {
- struct dst_entry *dst = &rt->dst;
- bool rc = true;
- if (type > NR_ICMP_TYPES)
- goto out;
-
- if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
- goto out;
-
- if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
- goto out;
-
- if (!((1 << type) & net->ipv4.sysctl_icmp_ratemask))
- goto out;
- rc = false;
- if (icmp_global_allow()) {
- struct inet_peer *peer;
- peer = inet_getpeer_v4(net->ipv4.peers, fl4->daddr, 1);
- rc = inet_peer_xrlim_allow(peer,
- net->ipv4.sysctl_icmp_ratelimit);
- if (peer)
- inet_putpeer(peer);
- }
- out:
- return rc;
- }
- void icmp_out_count(struct net *net, unsigned char type)
- {
- ICMPMSGOUT_INC_STATS(net, type);
- ICMP_INC_STATS(net, ICMP_MIB_OUTMSGS);
- }
- static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
- struct sk_buff *skb)
- {
- struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
- __wsum csum;
- csum = skb_copy_and_csum_bits(icmp_param->skb,
- icmp_param->offset + offset,
- to, len, 0);
- skb->csum = csum_block_add(skb->csum, csum, odd);
- if (icmp_pointers[icmp_param->data.icmph.type].error)
- nf_ct_attach(skb, icmp_param->skb);
- return 0;
- }
- static void icmp_push_reply(struct icmp_bxm *icmp_param,
- struct flowi4 *fl4,
- struct ipcm_cookie *ipc, struct rtable **rt)
- {
- struct sock *sk;
- struct sk_buff *skb;
- sk = icmp_sk(dev_net((*rt)->dst.dev));
- if (ip_append_data(sk, fl4, icmp_glue_bits, icmp_param,
- icmp_param->data_len+icmp_param->head_len,
- icmp_param->head_len,
- ipc, rt, MSG_DONTWAIT) < 0) {
- ICMP_INC_STATS_BH(sock_net(sk), ICMP_MIB_OUTERRORS);
- ip_flush_pending_frames(sk);
- } else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
- struct icmphdr *icmph = icmp_hdr(skb);
- __wsum csum = 0;
- struct sk_buff *skb1;
- skb_queue_walk(&sk->sk_write_queue, skb1) {
- csum = csum_add(csum, skb1->csum);
- }
- csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
- (char *)icmph,
- icmp_param->head_len, csum);
- icmph->checksum = csum_fold(csum);
- skb->ip_summed = CHECKSUM_NONE;
- ip_push_pending_frames(sk, fl4);
- }
- }
- static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
- {
- struct ipcm_cookie ipc;
- struct rtable *rt = skb_rtable(skb);
- struct net *net = dev_net(rt->dst.dev);
- struct flowi4 fl4;
- struct sock *sk;
- struct inet_sock *inet;
- __be32 daddr, saddr;
- u32 mark = IP4_REPLY_MARK(net, skb->mark);
- if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb))
- return;
- sk = icmp_xmit_lock(net);
- if (!sk)
- return;
- inet = inet_sk(sk);
- icmp_param->data.icmph.checksum = 0;
- inet->tos = ip_hdr(skb)->tos;
- sk->sk_mark = mark;
- daddr = ipc.addr = ip_hdr(skb)->saddr;
- saddr = fib_compute_spec_dst(skb);
- ipc.opt = NULL;
- ipc.tx_flags = 0;
- ipc.ttl = 0;
- ipc.tos = -1;
- if (icmp_param->replyopts.opt.opt.optlen) {
- ipc.opt = &icmp_param->replyopts.opt;
- if (ipc.opt->opt.srr)
- daddr = icmp_param->replyopts.opt.opt.faddr;
- }
- memset(&fl4, 0, sizeof(fl4));
- fl4.daddr = daddr;
- fl4.saddr = saddr;
- fl4.flowi4_mark = mark;
- fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
- fl4.flowi4_proto = IPPROTO_ICMP;
- security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
- rt = ip_route_output_key(net, &fl4);
- if (IS_ERR(rt))
- goto out_unlock;
- if (icmpv4_xrlim_allow(net, rt, &fl4, icmp_param->data.icmph.type,
- icmp_param->data.icmph.code))
- icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
- ip_rt_put(rt);
- out_unlock:
- icmp_xmit_unlock(sk);
- }
- static struct rtable *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)
- {
- struct rtable *rt, *rt2;
- struct flowi4 fl4_dec;
- int err;
- memset(fl4, 0, sizeof(*fl4));
- fl4->daddr = (param->replyopts.opt.opt.srr ?
- param->replyopts.opt.opt.faddr : iph->saddr);
- fl4->saddr = saddr;
- fl4->flowi4_mark = mark;
- fl4->flowi4_tos = RT_TOS(tos);
- fl4->flowi4_proto = IPPROTO_ICMP;
- fl4->fl4_icmp_type = type;
- fl4->fl4_icmp_code = code;
- security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
- rt = __ip_route_output_key(net, fl4);
- if (IS_ERR(rt))
- return rt;
-
- rt2 = rt;
- rt = (struct rtable *) xfrm_lookup(net, &rt->dst,
- flowi4_to_flowi(fl4), NULL, 0);
- if (!IS_ERR(rt)) {
- if (rt != rt2)
- return rt;
- } else if (PTR_ERR(rt) == -EPERM) {
- rt = NULL;
- } else
- return rt;
- err = xfrm_decode_session_reverse(skb_in, flowi4_to_flowi(&fl4_dec), AF_INET);
- if (err)
- goto relookup_failed;
- if (inet_addr_type(net, fl4_dec.saddr) == RTN_LOCAL) {
- rt2 = __ip_route_output_key(net, &fl4_dec);
- if (IS_ERR(rt2))
- err = PTR_ERR(rt2);
- } else {
- struct flowi4 fl4_2 = {};
- unsigned long orefdst;
- fl4_2.daddr = fl4_dec.saddr;
- rt2 = ip_route_output_key(net, &fl4_2);
- if (IS_ERR(rt2)) {
- err = PTR_ERR(rt2);
- goto relookup_failed;
- }
-
- orefdst = skb_in->_skb_refdst;
- err = ip_route_input(skb_in, fl4_dec.daddr, fl4_dec.saddr,
- RT_TOS(tos), rt2->dst.dev);
- dst_release(&rt2->dst);
- rt2 = skb_rtable(skb_in);
- skb_in->_skb_refdst = orefdst;
- }
- if (err)
- goto relookup_failed;
- rt2 = (struct rtable *) xfrm_lookup(net, &rt2->dst,
- flowi4_to_flowi(&fl4_dec), NULL,
- XFRM_LOOKUP_ICMP);
- if (!IS_ERR(rt2)) {
- dst_release(&rt->dst);
- memcpy(fl4, &fl4_dec, sizeof(*fl4));
- rt = rt2;
- } else if (PTR_ERR(rt2) == -EPERM) {
- if (rt)
- dst_release(&rt->dst);
- return rt2;
- } else {
- err = PTR_ERR(rt2);
- goto relookup_failed;
- }
- return rt;
- relookup_failed:
- if (rt)
- return rt;
- return ERR_PTR(err);
- }
- void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
- {
- struct iphdr *iph;
- int room;
- struct icmp_bxm *icmp_param;
- struct rtable *rt = skb_rtable(skb_in);
- struct ipcm_cookie ipc;
- struct flowi4 fl4;
- __be32 saddr;
- u8 tos;
- u32 mark;
- struct net *net;
- struct sock *sk;
- if (!rt)
- goto out;
- net = dev_net(rt->dst.dev);
-
- iph = ip_hdr(skb_in);
- if ((u8 *)iph < skb_in->head ||
- (skb_network_header(skb_in) + sizeof(*iph)) >
- skb_tail_pointer(skb_in))
- goto out;
-
- if (skb_in->pkt_type != PACKET_HOST)
- goto out;
-
- if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
- goto out;
-
- if (iph->frag_off & htons(IP_OFFSET))
- goto out;
-
- if (icmp_pointers[type].error) {
-
- if (iph->protocol == IPPROTO_ICMP) {
- u8 _inner_type, *itp;
- itp = skb_header_pointer(skb_in,
- skb_network_header(skb_in) +
- (iph->ihl << 2) +
- offsetof(struct icmphdr,
- type) -
- skb_in->data,
- sizeof(_inner_type),
- &_inner_type);
- if (!itp)
- goto out;
-
- if (*itp > NR_ICMP_TYPES ||
- icmp_pointers[*itp].error)
- goto out;
- }
- }
- icmp_param = kmalloc(sizeof(*icmp_param), GFP_ATOMIC);
- if (!icmp_param)
- return;
- sk = icmp_xmit_lock(net);
- if (!sk)
- goto out_free;
-
- saddr = iph->daddr;
- if (!(rt->rt_flags & RTCF_LOCAL)) {
- struct net_device *dev = NULL;
- rcu_read_lock();
- if (rt_is_input_route(rt) &&
- net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr)
- dev = dev_get_by_index_rcu(net, inet_iif(skb_in));
- if (dev)
- saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
- else
- saddr = 0;
- rcu_read_unlock();
- }
- tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
- IPTOS_PREC_INTERNETCONTROL) :
- iph->tos;
- mark = IP4_REPLY_MARK(net, skb_in->mark);
- if (ip_options_echo(&icmp_param->replyopts.opt.opt, skb_in))
- goto out_unlock;
-
- icmp_param->data.icmph.type = type;
- icmp_param->data.icmph.code = code;
- icmp_param->data.icmph.un.gateway = info;
- icmp_param->data.icmph.checksum = 0;
- icmp_param->skb = skb_in;
- icmp_param->offset = skb_network_offset(skb_in);
- inet_sk(sk)->tos = tos;
- sk->sk_mark = mark;
- ipc.addr = iph->saddr;
- ipc.opt = &icmp_param->replyopts.opt;
- ipc.tx_flags = 0;
- ipc.ttl = 0;
- ipc.tos = -1;
- rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, mark,
- type, code, icmp_param);
- if (IS_ERR(rt))
- goto out_unlock;
- if (!icmpv4_xrlim_allow(net, rt, &fl4, type, code))
- goto ende;
-
- room = dst_mtu(&rt->dst);
- if (room > 576)
- room = 576;
- room -= sizeof(struct iphdr) + icmp_param->replyopts.opt.opt.optlen;
- room -= sizeof(struct icmphdr);
- icmp_param->data_len = skb_in->len - icmp_param->offset;
- if (icmp_param->data_len > room)
- icmp_param->data_len = room;
- icmp_param->head_len = sizeof(struct icmphdr);
- icmp_push_reply(icmp_param, &fl4, &ipc, &rt);
- ende:
- ip_rt_put(rt);
- out_unlock:
- icmp_xmit_unlock(sk);
- out_free:
- kfree(icmp_param);
- out:;
- }
- EXPORT_SYMBOL(icmp_send);
- static void icmp_socket_deliver(struct sk_buff *skb, u32 info)
- {
- const struct iphdr *iph = (const struct iphdr *) skb->data;
- const struct net_protocol *ipprot;
- int protocol = iph->protocol;
-
- if (!pskb_may_pull(skb, iph->ihl * 4 + 8)) {
- ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
- return;
- }
- raw_icmp_error(skb, protocol, info);
- ipprot = rcu_dereference(inet_protos[protocol]);
- if (ipprot && ipprot->err_handler)
- ipprot->err_handler(skb, info);
- }
- static bool icmp_tag_validation(int proto)
- {
- bool ok;
- rcu_read_lock();
- ok = rcu_dereference(inet_protos[proto])->icmp_strict_tag_validation;
- rcu_read_unlock();
- return ok;
- }
- static bool icmp_unreach(struct sk_buff *skb)
- {
- const struct iphdr *iph;
- struct icmphdr *icmph;
- struct net *net;
- u32 info = 0;
- net = dev_net(skb_dst(skb)->dev);
-
- if (!pskb_may_pull(skb, sizeof(struct iphdr)))
- goto out_err;
- icmph = icmp_hdr(skb);
- iph = (const struct iphdr *)skb->data;
- if (iph->ihl < 5)
- goto out_err;
- if (icmph->type == ICMP_DEST_UNREACH) {
- switch (icmph->code & 15) {
- case ICMP_NET_UNREACH:
- case ICMP_HOST_UNREACH:
- case ICMP_PROT_UNREACH:
- case ICMP_PORT_UNREACH:
- break;
- case ICMP_FRAG_NEEDED:
-
- switch (net->ipv4.sysctl_ip_no_pmtu_disc) {
- default:
- net_dbg_ratelimited("%pI4: fragmentation needed and DF set\n",
- &iph->daddr);
- break;
- case 2:
- goto out;
- case 3:
- if (!icmp_tag_validation(iph->protocol))
- goto out;
-
- case 0:
- info = ntohs(icmph->un.frag.mtu);
- }
- break;
- case ICMP_SR_FAILED:
- net_dbg_ratelimited("%pI4: Source Route Failed\n",
- &iph->daddr);
- break;
- default:
- break;
- }
- if (icmph->code > NR_ICMP_UNREACH)
- goto out;
- } else if (icmph->type == ICMP_PARAMETERPROB)
- info = ntohl(icmph->un.gateway) >> 24;
-
-
- if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
- inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
- net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
- &ip_hdr(skb)->saddr,
- icmph->type, icmph->code,
- &iph->daddr, skb->dev->name);
- goto out;
- }
- icmp_socket_deliver(skb, info);
- out:
- return true;
- out_err:
- ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
- return false;
- }
- static bool icmp_redirect(struct sk_buff *skb)
- {
- if (skb->len < sizeof(struct iphdr)) {
- ICMP_INC_STATS_BH(dev_net(skb->dev), ICMP_MIB_INERRORS);
- return false;
- }
- if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
-
- return false;
- }
- icmp_socket_deliver(skb, icmp_hdr(skb)->un.gateway);
- return true;
- }
- static bool icmp_echo(struct sk_buff *skb)
- {
- struct net *net;
- net = dev_net(skb_dst(skb)->dev);
- if (!net->ipv4.sysctl_icmp_echo_ignore_all) {
- struct icmp_bxm icmp_param;
- icmp_param.data.icmph = *icmp_hdr(skb);
- icmp_param.data.icmph.type = ICMP_ECHOREPLY;
- icmp_param.skb = skb;
- icmp_param.offset = 0;
- icmp_param.data_len = skb->len;
- icmp_param.head_len = sizeof(struct icmphdr);
- icmp_reply(&icmp_param, skb);
- }
-
- return true;
- }
- static bool icmp_timestamp(struct sk_buff *skb)
- {
- struct timespec tv;
- struct icmp_bxm icmp_param;
-
- if (skb->len < 4)
- goto out_err;
-
- getnstimeofday(&tv);
- icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC +
- tv.tv_nsec / NSEC_PER_MSEC);
- icmp_param.data.times[2] = icmp_param.data.times[1];
- if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
- BUG();
- icmp_param.data.icmph = *icmp_hdr(skb);
- icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
- icmp_param.data.icmph.code = 0;
- icmp_param.skb = skb;
- icmp_param.offset = 0;
- icmp_param.data_len = 0;
- icmp_param.head_len = sizeof(struct icmphdr) + 12;
- icmp_reply(&icmp_param, skb);
- return true;
- out_err:
- ICMP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ICMP_MIB_INERRORS);
- return false;
- }
- static bool icmp_discard(struct sk_buff *skb)
- {
-
- return true;
- }
- int icmp_rcv(struct sk_buff *skb)
- {
- struct icmphdr *icmph;
- struct rtable *rt = skb_rtable(skb);
- struct net *net = dev_net(rt->dst.dev);
- bool success;
- if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
- struct sec_path *sp = skb_sec_path(skb);
- int nh;
- if (!(sp && sp->xvec[sp->len - 1]->props.flags &
- XFRM_STATE_ICMP))
- goto drop;
- if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
- goto drop;
- nh = skb_network_offset(skb);
- skb_set_network_header(skb, sizeof(*icmph));
- if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
- goto drop;
- skb_set_network_header(skb, nh);
- }
- ICMP_INC_STATS_BH(net, ICMP_MIB_INMSGS);
- if (skb_checksum_simple_validate(skb))
- goto csum_error;
- if (!pskb_pull(skb, sizeof(*icmph)))
- goto error;
- icmph = icmp_hdr(skb);
- ICMPMSGIN_INC_STATS_BH(net, icmph->type);
-
- if (icmph->type > NR_ICMP_TYPES)
- goto error;
-
- if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
-
- if ((icmph->type == ICMP_ECHO ||
- icmph->type == ICMP_TIMESTAMP) &&
- net->ipv4.sysctl_icmp_echo_ignore_broadcasts) {
- goto error;
- }
- if (icmph->type != ICMP_ECHO &&
- icmph->type != ICMP_TIMESTAMP &&
- icmph->type != ICMP_ADDRESS &&
- icmph->type != ICMP_ADDRESSREPLY) {
- goto error;
- }
- }
- success = icmp_pointers[icmph->type].handler(skb);
- if (success) {
- consume_skb(skb);
- return 0;
- }
- drop:
- kfree_skb(skb);
- return 0;
- csum_error:
- ICMP_INC_STATS_BH(net, ICMP_MIB_CSUMERRORS);
- error:
- ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
- goto drop;
- }
- void icmp_err(struct sk_buff *skb, u32 info)
- {
- struct iphdr *iph = (struct iphdr *)skb->data;
- int offset = iph->ihl<<2;
- struct icmphdr *icmph = (struct icmphdr *)(skb->data + offset);
- int type = icmp_hdr(skb)->type;
- int code = icmp_hdr(skb)->code;
- struct net *net = dev_net(skb->dev);
-
- if (icmph->type != ICMP_ECHOREPLY) {
- ping_err(skb, offset, info);
- return;
- }
- if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
- ipv4_update_pmtu(skb, net, info, 0, 0, IPPROTO_ICMP, 0);
- else if (type == ICMP_REDIRECT)
- ipv4_redirect(skb, net, 0, 0, IPPROTO_ICMP, 0);
- }
- static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
- [ICMP_ECHOREPLY] = {
- .handler = ping_rcv,
- },
- [1] = {
- .handler = icmp_discard,
- .error = 1,
- },
- [2] = {
- .handler = icmp_discard,
- .error = 1,
- },
- [ICMP_DEST_UNREACH] = {
- .handler = icmp_unreach,
- .error = 1,
- },
- [ICMP_SOURCE_QUENCH] = {
- .handler = icmp_unreach,
- .error = 1,
- },
- [ICMP_REDIRECT] = {
- .handler = icmp_redirect,
- .error = 1,
- },
- [6] = {
- .handler = icmp_discard,
- .error = 1,
- },
- [7] = {
- .handler = icmp_discard,
- .error = 1,
- },
- [ICMP_ECHO] = {
- .handler = icmp_echo,
- },
- [9] = {
- .handler = icmp_discard,
- .error = 1,
- },
- [10] = {
- .handler = icmp_discard,
- .error = 1,
- },
- [ICMP_TIME_EXCEEDED] = {
- .handler = icmp_unreach,
- .error = 1,
- },
- [ICMP_PARAMETERPROB] = {
- .handler = icmp_unreach,
- .error = 1,
- },
- [ICMP_TIMESTAMP] = {
- .handler = icmp_timestamp,
- },
- [ICMP_TIMESTAMPREPLY] = {
- .handler = icmp_discard,
- },
- [ICMP_INFO_REQUEST] = {
- .handler = icmp_discard,
- },
- [ICMP_INFO_REPLY] = {
- .handler = icmp_discard,
- },
- [ICMP_ADDRESS] = {
- .handler = icmp_discard,
- },
- [ICMP_ADDRESSREPLY] = {
- .handler = icmp_discard,
- },
- };
- static void __net_exit icmp_sk_exit(struct net *net)
- {
- int i;
- for_each_possible_cpu(i)
- inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
- free_percpu(net->ipv4.icmp_sk);
- net->ipv4.icmp_sk = NULL;
- }
- static int __net_init icmp_sk_init(struct net *net)
- {
- int i, err;
- net->ipv4.icmp_sk = alloc_percpu(struct sock *);
- if (!net->ipv4.icmp_sk)
- return -ENOMEM;
- for_each_possible_cpu(i) {
- struct sock *sk;
- err = inet_ctl_sock_create(&sk, PF_INET,
- SOCK_RAW, IPPROTO_ICMP, net);
- if (err < 0)
- goto fail;
- *per_cpu_ptr(net->ipv4.icmp_sk, i) = sk;
-
- sk->sk_sndbuf = 2 * SKB_TRUESIZE(64 * 1024);
-
- sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
- inet_sk(sk)->pmtudisc = IP_PMTUDISC_DONT;
- }
-
- net->ipv4.sysctl_icmp_echo_ignore_all = 0;
- net->ipv4.sysctl_icmp_echo_ignore_broadcasts = 1;
-
- net->ipv4.sysctl_icmp_ignore_bogus_error_responses = 1;
-
- net->ipv4.sysctl_icmp_ratelimit = 1 * HZ;
- net->ipv4.sysctl_icmp_ratemask = 0x1818;
- net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr = 0;
- return 0;
- fail:
- for_each_possible_cpu(i)
- inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.icmp_sk, i));
- free_percpu(net->ipv4.icmp_sk);
- return err;
- }
- static struct pernet_operations __net_initdata icmp_sk_ops = {
- .init = icmp_sk_init,
- .exit = icmp_sk_exit,
- };
- int __init icmp_init(void)
- {
- return register_pernet_subsys(&icmp_sk_ops);
- }
|