[ICMP]: Store sock rather than socket for ICMP flow control.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / icmp.c
CommitLineData
1da177e4
LT
1/*
2 * NET3: Implementation of the ICMP protocol layer.
3 *
4 * Alan Cox, <alan@redhat.com>
5 *
6 * Version: $Id: icmp.c,v 1.85 2002/02/01 22:01:03 davem Exp $
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * Some of the function names and the icmp unreach table for this
14 * module were derived from [icmp.c 1.0.11 06/02/93] by
15 * Ross Biro, Fred N. van Kempen, Mark Evans, Alan Cox, Gerhard Koerting.
16 * Other than that this module is a complete rewrite.
17 *
18 * Fixes:
19 * Clemens Fruhwirth : introduce global icmp rate limiting
20 * with icmp type masking ability instead
21 * of broken per type icmp timeouts.
22 * Mike Shaver : RFC1122 checks.
23 * Alan Cox : Multicast ping reply as self.
24 * Alan Cox : Fix atomicity lockup in ip_build_xmit
25 * call.
26 * Alan Cox : Added 216,128 byte paths to the MTU
27 * code.
28 * Martin Mares : RFC1812 checks.
29 * Martin Mares : Can be configured to follow redirects
30 * if acting as a router _without_ a
31 * routing protocol (RFC 1812).
32 * Martin Mares : Echo requests may be configured to
33 * be ignored (RFC 1812).
34 * Martin Mares : Limitation of ICMP error message
35 * transmit rate (RFC 1812).
36 * Martin Mares : TOS and Precedence set correctly
37 * (RFC 1812).
38 * Martin Mares : Now copying as much data from the
39 * original packet as we can without
40 * exceeding 576 bytes (RFC 1812).
41 * Willy Konynenberg : Transparent proxying support.
42 * Keith Owens : RFC1191 correction for 4.2BSD based
43 * path MTU bug.
44 * Thomas Quinot : ICMP Dest Unreach codes up to 15 are
45 * valid (RFC 1812).
46 * Andi Kleen : Check all packet lengths properly
47 * and moved all kfree_skb() up to
48 * icmp_rcv.
49 * Andi Kleen : Move the rate limit bookkeeping
50 * into the dest entry and use a token
51 * bucket filter (thanks to ANK). Make
52 * the rates sysctl configurable.
53 * Yu Tianli : Fixed two ugly bugs in icmp_send
54 * - IP option length was accounted wrongly
55 * - ICMP header length was not accounted
56 * at all.
57 * Tristan Greaves : Added sysctl option to ignore bogus
58 * broadcast responses from broken routers.
59 *
60 * To Fix:
61 *
62 * - Should use skb_pull() instead of all the manual checking.
63 * This would also greatly simply some upper layer error handlers. --AK
64 *
65 */
66
1da177e4
LT
67#include <linux/module.h>
68#include <linux/types.h>
69#include <linux/jiffies.h>
70#include <linux/kernel.h>
71#include <linux/fcntl.h>
72#include <linux/socket.h>
73#include <linux/in.h>
74#include <linux/inet.h>
14c85021 75#include <linux/inetdevice.h>
1da177e4
LT
76#include <linux/netdevice.h>
77#include <linux/string.h>
78#include <linux/netfilter_ipv4.h>
79#include <net/snmp.h>
80#include <net/ip.h>
81#include <net/route.h>
82#include <net/protocol.h>
83#include <net/icmp.h>
84#include <net/tcp.h>
85#include <net/udp.h>
86#include <net/raw.h>
87#include <linux/skbuff.h>
88#include <net/sock.h>
89#include <linux/errno.h>
90#include <linux/timer.h>
91#include <linux/init.h>
92#include <asm/system.h>
93#include <asm/uaccess.h>
94#include <net/checksum.h>
8b7817f3 95#include <net/xfrm.h>
1da177e4
LT
96
97/*
98 * Build xmit assembly blocks
99 */
100
101struct icmp_bxm {
102 struct sk_buff *skb;
103 int offset;
104 int data_len;
105
106 struct {
107 struct icmphdr icmph;
b03d73e3 108 __be32 times[3];
1da177e4
LT
109 } data;
110 int head_len;
111 struct ip_options replyopts;
112 unsigned char optbuf[40];
113};
114
115/*
116 * Statistics
117 */
ba89966c 118DEFINE_SNMP_STAT(struct icmp_mib, icmp_statistics) __read_mostly;
96793b48 119DEFINE_SNMP_STAT(struct icmpmsg_mib, icmpmsg_statistics) __read_mostly;
1da177e4
LT
120
121/* An array of errno for error messages from dest unreach. */
122/* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOST_UNREACH and SR_FAILED MUST be considered 'transient errs'. */
123
124struct icmp_err icmp_err_convert[] = {
125 {
126 .errno = ENETUNREACH, /* ICMP_NET_UNREACH */
127 .fatal = 0,
128 },
129 {
130 .errno = EHOSTUNREACH, /* ICMP_HOST_UNREACH */
131 .fatal = 0,
132 },
133 {
134 .errno = ENOPROTOOPT /* ICMP_PROT_UNREACH */,
135 .fatal = 1,
136 },
137 {
138 .errno = ECONNREFUSED, /* ICMP_PORT_UNREACH */
139 .fatal = 1,
140 },
141 {
142 .errno = EMSGSIZE, /* ICMP_FRAG_NEEDED */
143 .fatal = 0,
144 },
145 {
146 .errno = EOPNOTSUPP, /* ICMP_SR_FAILED */
147 .fatal = 0,
148 },
149 {
150 .errno = ENETUNREACH, /* ICMP_NET_UNKNOWN */
151 .fatal = 1,
152 },
153 {
154 .errno = EHOSTDOWN, /* ICMP_HOST_UNKNOWN */
155 .fatal = 1,
156 },
157 {
158 .errno = ENONET, /* ICMP_HOST_ISOLATED */
159 .fatal = 1,
160 },
161 {
162 .errno = ENETUNREACH, /* ICMP_NET_ANO */
163 .fatal = 1,
164 },
165 {
166 .errno = EHOSTUNREACH, /* ICMP_HOST_ANO */
167 .fatal = 1,
168 },
169 {
170 .errno = ENETUNREACH, /* ICMP_NET_UNR_TOS */
171 .fatal = 0,
172 },
173 {
174 .errno = EHOSTUNREACH, /* ICMP_HOST_UNR_TOS */
175 .fatal = 0,
176 },
177 {
178 .errno = EHOSTUNREACH, /* ICMP_PKT_FILTERED */
179 .fatal = 1,
180 },
181 {
182 .errno = EHOSTUNREACH, /* ICMP_PREC_VIOLATION */
183 .fatal = 1,
184 },
185 {
186 .errno = EHOSTUNREACH, /* ICMP_PREC_CUTOFF */
187 .fatal = 1,
188 },
189};
190
191/* Control parameters for ECHO replies. */
ab32ea5d
BH
192int sysctl_icmp_echo_ignore_all __read_mostly;
193int sysctl_icmp_echo_ignore_broadcasts __read_mostly = 1;
1da177e4
LT
194
195/* Control parameter - ignore bogus broadcast responses? */
ab32ea5d 196int sysctl_icmp_ignore_bogus_error_responses __read_mostly = 1;
1da177e4
LT
197
198/*
199 * Configurable global rate limit.
200 *
201 * ratelimit defines tokens/packet consumed for dst->rate_token bucket
202 * ratemask defines which icmp types are ratelimited by setting
203 * it's bit position.
204 *
205 * default:
206 * dest unreachable (3), source quench (4),
207 * time exceeded (11), parameter problem (12)
208 */
209
ab32ea5d
BH
210int sysctl_icmp_ratelimit __read_mostly = 1 * HZ;
211int sysctl_icmp_ratemask __read_mostly = 0x1818;
212int sysctl_icmp_errors_use_inbound_ifaddr __read_mostly;
1da177e4
LT
213
214/*
215 * ICMP control array. This specifies what to do with each ICMP.
216 */
217
218struct icmp_control {
1da177e4
LT
219 void (*handler)(struct sk_buff *skb);
220 short error; /* This ICMP is classed as an error message */
221};
222
9b5b5cff 223static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
1da177e4
LT
224
225/*
226 * The ICMP socket(s). This is the most convenient way to flow control
227 * our ICMP output as well as maintain a clean interface throughout
228 * all layers. All Socketless IP sends will soon be gone.
229 *
230 * On SMP we have one ICMP socket per-cpu.
231 */
b7e729c4
DL
232static DEFINE_PER_CPU(struct sock *, __icmp_sk) = NULL;
233#define icmp_sk __get_cpu_var(__icmp_sk)
1da177e4 234
74feb6e8 235static inline int icmp_xmit_lock(void)
1da177e4
LT
236{
237 local_bh_disable();
238
b7e729c4 239 if (unlikely(!spin_trylock(&icmp_sk->sk_lock.slock))) {
1da177e4
LT
240 /* This can happen if the output path signals a
241 * dst_link_failure() for an outgoing ICMP packet.
242 */
243 local_bh_enable();
244 return 1;
245 }
246 return 0;
247}
248
74feb6e8 249static inline void icmp_xmit_unlock(void)
1da177e4 250{
b7e729c4 251 spin_unlock_bh(&icmp_sk->sk_lock.slock);
1da177e4
LT
252}
253
254/*
255 * Send an ICMP frame.
256 */
257
258/*
259 * Check transmit rate limitation for given message.
260 * The rate information is held in the destination cache now.
261 * This function is generic and could be used for other purposes
262 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
263 *
264 * Note that the same dst_entry fields are modified by functions in
265 * route.c too, but these work for packet destinations while xrlim_allow
266 * works for icmp destinations. This means the rate limiting information
267 * for one "ip object" is shared - and these ICMPs are twice limited:
268 * by source and by destination.
269 *
270 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
271 * SHOULD allow setting of rate limits
272 *
273 * Shared between ICMPv4 and ICMPv6.
274 */
275#define XRLIM_BURST_FACTOR 6
276int xrlim_allow(struct dst_entry *dst, int timeout)
277{
69a73829 278 unsigned long now, token = dst->rate_tokens;
1da177e4
LT
279 int rc = 0;
280
281 now = jiffies;
69a73829 282 token += now - dst->rate_last;
1da177e4 283 dst->rate_last = now;
69a73829
ED
284 if (token > XRLIM_BURST_FACTOR * timeout)
285 token = XRLIM_BURST_FACTOR * timeout;
286 if (token >= timeout) {
287 token -= timeout;
1da177e4
LT
288 rc = 1;
289 }
69a73829 290 dst->rate_tokens = token;
1da177e4
LT
291 return rc;
292}
293
294static inline int icmpv4_xrlim_allow(struct rtable *rt, int type, int code)
295{
296 struct dst_entry *dst = &rt->u.dst;
297 int rc = 1;
298
299 if (type > NR_ICMP_TYPES)
300 goto out;
301
302 /* Don't limit PMTU discovery. */
303 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
304 goto out;
305
306 /* No rate limit on loopback */
307 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
e905a9ed 308 goto out;
1da177e4
LT
309
310 /* Limit if icmp type is enabled in ratemask. */
311 if ((1 << type) & sysctl_icmp_ratemask)
312 rc = xrlim_allow(dst, sysctl_icmp_ratelimit);
313out:
314 return rc;
315}
316
317/*
318 * Maintain the counters used in the SNMP statistics for outgoing ICMP
319 */
96793b48 320void icmp_out_count(unsigned char type)
1da177e4 321{
96793b48
DS
322 ICMPMSGOUT_INC_STATS(type);
323 ICMP_INC_STATS(ICMP_MIB_OUTMSGS);
1da177e4
LT
324}
325
326/*
327 * Checksum each fragment, and on the first include the headers and final
328 * checksum.
329 */
330static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,
331 struct sk_buff *skb)
332{
333 struct icmp_bxm *icmp_param = (struct icmp_bxm *)from;
5f92a738 334 __wsum csum;
1da177e4
LT
335
336 csum = skb_copy_and_csum_bits(icmp_param->skb,
337 icmp_param->offset + offset,
338 to, len, 0);
339
340 skb->csum = csum_block_add(skb->csum, csum, odd);
341 if (icmp_pointers[icmp_param->data.icmph.type].error)
342 nf_ct_attach(skb, icmp_param->skb);
343 return 0;
344}
345
346static void icmp_push_reply(struct icmp_bxm *icmp_param,
347 struct ipcm_cookie *ipc, struct rtable *rt)
348{
1e3cf683 349 struct sock *sk;
1da177e4
LT
350 struct sk_buff *skb;
351
b7e729c4 352 sk = icmp_sk;
1e3cf683 353 if (ip_append_data(sk, icmp_glue_bits, icmp_param,
e905a9ed
YH
354 icmp_param->data_len+icmp_param->head_len,
355 icmp_param->head_len,
356 ipc, rt, MSG_DONTWAIT) < 0)
1e3cf683
DL
357 ip_flush_pending_frames(sk);
358 else if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
88c7664f 359 struct icmphdr *icmph = icmp_hdr(skb);
d3bc23e7 360 __wsum csum = 0;
1da177e4
LT
361 struct sk_buff *skb1;
362
1e3cf683 363 skb_queue_walk(&sk->sk_write_queue, skb1) {
1da177e4
LT
364 csum = csum_add(csum, skb1->csum);
365 }
366 csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
367 (char *)icmph,
368 icmp_param->head_len, csum);
369 icmph->checksum = csum_fold(csum);
370 skb->ip_summed = CHECKSUM_NONE;
1e3cf683 371 ip_push_pending_frames(sk);
1da177e4
LT
372 }
373}
374
375/*
376 * Driving logic for building and sending ICMP messages.
377 */
378
379static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
380{
b7e729c4 381 struct sock *sk = icmp_sk;
1da177e4
LT
382 struct inet_sock *inet = inet_sk(sk);
383 struct ipcm_cookie ipc;
384 struct rtable *rt = (struct rtable *)skb->dst;
3ca3c68e 385 __be32 daddr;
1da177e4
LT
386
387 if (ip_options_echo(&icmp_param->replyopts, skb))
f00c401b 388 return;
1da177e4
LT
389
390 if (icmp_xmit_lock())
391 return;
392
393 icmp_param->data.icmph.checksum = 0;
1da177e4 394
eddc9ec5 395 inet->tos = ip_hdr(skb)->tos;
1da177e4
LT
396 daddr = ipc.addr = rt->rt_src;
397 ipc.opt = NULL;
398 if (icmp_param->replyopts.optlen) {
399 ipc.opt = &icmp_param->replyopts;
400 if (ipc.opt->srr)
401 daddr = icmp_param->replyopts.faddr;
402 }
403 {
404 struct flowi fl = { .nl_u = { .ip4_u =
405 { .daddr = daddr,
406 .saddr = rt->rt_spec_dst,
eddc9ec5 407 .tos = RT_TOS(ip_hdr(skb)->tos) } },
1da177e4 408 .proto = IPPROTO_ICMP };
beb8d13b 409 security_skb_classify_flow(skb, &fl);
dde1bc0e 410 if (ip_route_output_key(rt->u.dst.dev->nd_net, &rt, &fl))
1da177e4
LT
411 goto out_unlock;
412 }
413 if (icmpv4_xrlim_allow(rt, icmp_param->data.icmph.type,
414 icmp_param->data.icmph.code))
415 icmp_push_reply(icmp_param, &ipc, rt);
416 ip_rt_put(rt);
417out_unlock:
418 icmp_xmit_unlock();
1da177e4
LT
419}
420
421
422/*
423 * Send an ICMP message in response to a situation
424 *
425 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header.
426 * MAY send more (we do).
427 * MUST NOT change this header information.
428 * MUST NOT reply to a multicast/broadcast IP address.
429 * MUST NOT reply to a multicast/broadcast MAC address.
430 * MUST reply to only the first fragment.
431 */
432
e4883014 433void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
1da177e4
LT
434{
435 struct iphdr *iph;
436 int room;
437 struct icmp_bxm icmp_param;
438 struct rtable *rt = (struct rtable *)skb_in->dst;
439 struct ipcm_cookie ipc;
a61ced5d 440 __be32 saddr;
1da177e4 441 u8 tos;
dde1bc0e 442 struct net *net;
1da177e4
LT
443
444 if (!rt)
445 goto out;
dde1bc0e 446 net = rt->u.dst.dev->nd_net;
1da177e4
LT
447
448 /*
449 * Find the original header. It is expected to be valid, of course.
450 * Check this, icmp_send is called from the most obscure devices
451 * sometimes.
452 */
eddc9ec5 453 iph = ip_hdr(skb_in);
1da177e4 454
27a884dc
ACM
455 if ((u8 *)iph < skb_in->head ||
456 (skb_in->network_header + sizeof(*iph)) > skb_in->tail)
1da177e4
LT
457 goto out;
458
459 /*
460 * No replies to physical multicast/broadcast
461 */
462 if (skb_in->pkt_type != PACKET_HOST)
463 goto out;
464
465 /*
466 * Now check at the protocol level
467 */
468 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
469 goto out;
470
471 /*
472 * Only reply to fragment 0. We byte re-order the constant
473 * mask for efficiency.
474 */
475 if (iph->frag_off & htons(IP_OFFSET))
476 goto out;
477
478 /*
479 * If we send an ICMP error to an ICMP error a mess would result..
480 */
481 if (icmp_pointers[type].error) {
482 /*
483 * We are an error, check if we are replying to an
484 * ICMP error
485 */
486 if (iph->protocol == IPPROTO_ICMP) {
487 u8 _inner_type, *itp;
488
489 itp = skb_header_pointer(skb_in,
d56f90a7 490 skb_network_header(skb_in) +
1da177e4
LT
491 (iph->ihl << 2) +
492 offsetof(struct icmphdr,
493 type) -
494 skb_in->data,
495 sizeof(_inner_type),
496 &_inner_type);
497 if (itp == NULL)
498 goto out;
499
500 /*
501 * Assume any unknown ICMP type is an error. This
502 * isn't specified by the RFC, but think about it..
503 */
504 if (*itp > NR_ICMP_TYPES ||
505 icmp_pointers[*itp].error)
506 goto out;
507 }
508 }
509
510 if (icmp_xmit_lock())
511 return;
512
513 /*
514 * Construct source address and options.
515 */
516
517 saddr = iph->daddr;
1c2fb7f9 518 if (!(rt->rt_flags & RTCF_LOCAL)) {
6e1d9103
PM
519 struct net_device *dev = NULL;
520
521 if (rt->fl.iif && sysctl_icmp_errors_use_inbound_ifaddr)
dde1bc0e 522 dev = dev_get_by_index(net, rt->fl.iif);
6e1d9103
PM
523
524 if (dev) {
525 saddr = inet_select_addr(dev, 0, RT_SCOPE_LINK);
526 dev_put(dev);
527 } else
1c2fb7f9
S
528 saddr = 0;
529 }
1da177e4
LT
530
531 tos = icmp_pointers[type].error ? ((iph->tos & IPTOS_TOS_MASK) |
532 IPTOS_PREC_INTERNETCONTROL) :
533 iph->tos;
534
535 if (ip_options_echo(&icmp_param.replyopts, skb_in))
fa60cf7f 536 goto out_unlock;
1da177e4
LT
537
538
539 /*
540 * Prepare data for ICMP header.
541 */
542
543 icmp_param.data.icmph.type = type;
544 icmp_param.data.icmph.code = code;
545 icmp_param.data.icmph.un.gateway = info;
546 icmp_param.data.icmph.checksum = 0;
547 icmp_param.skb = skb_in;
d56f90a7 548 icmp_param.offset = skb_network_offset(skb_in);
b7e729c4 549 inet_sk(icmp_sk)->tos = tos;
1da177e4
LT
550 ipc.addr = iph->saddr;
551 ipc.opt = &icmp_param.replyopts;
552
553 {
554 struct flowi fl = {
555 .nl_u = {
556 .ip4_u = {
557 .daddr = icmp_param.replyopts.srr ?
558 icmp_param.replyopts.faddr :
559 iph->saddr,
560 .saddr = saddr,
561 .tos = RT_TOS(tos)
562 }
563 },
564 .proto = IPPROTO_ICMP,
565 .uli_u = {
566 .icmpt = {
567 .type = type,
568 .code = code
569 }
570 }
571 };
8b7817f3
HX
572 int err;
573 struct rtable *rt2;
574
beb8d13b 575 security_skb_classify_flow(skb_in, &fl);
dde1bc0e 576 if (__ip_route_output_key(net, &rt, &fl))
8b7817f3
HX
577 goto out_unlock;
578
579 /* No need to clone since we're just using its address. */
580 rt2 = rt;
581
582 err = xfrm_lookup((struct dst_entry **)&rt, &fl, NULL, 0);
583 switch (err) {
584 case 0:
585 if (rt != rt2)
586 goto route_done;
587 break;
588 case -EPERM:
589 rt = NULL;
590 break;
591 default:
592 goto out_unlock;
593 }
594
595 if (xfrm_decode_session_reverse(skb_in, &fl, AF_INET))
596 goto out_unlock;
597
dde1bc0e
DL
598 if (inet_addr_type(net, fl.fl4_src) == RTN_LOCAL)
599 err = __ip_route_output_key(net, &rt2, &fl);
8b7817f3
HX
600 else {
601 struct flowi fl2 = {};
602 struct dst_entry *odst;
603
604 fl2.fl4_dst = fl.fl4_src;
dde1bc0e 605 if (ip_route_output_key(net, &rt2, &fl2))
8b7817f3
HX
606 goto out_unlock;
607
608 /* Ugh! */
609 odst = skb_in->dst;
610 err = ip_route_input(skb_in, fl.fl4_dst, fl.fl4_src,
611 RT_TOS(tos), rt2->u.dst.dev);
612
613 dst_release(&rt2->u.dst);
614 rt2 = (struct rtable *)skb_in->dst;
615 skb_in->dst = odst;
616 }
617
618 if (err)
619 goto out_unlock;
620
621 err = xfrm_lookup((struct dst_entry **)&rt2, &fl, NULL,
622 XFRM_LOOKUP_ICMP);
623 if (err == -ENOENT) {
624 if (!rt)
625 goto out_unlock;
626 goto route_done;
627 }
628
629 dst_release(&rt->u.dst);
630 rt = rt2;
631
632 if (err)
1da177e4
LT
633 goto out_unlock;
634 }
635
8b7817f3 636route_done:
1da177e4
LT
637 if (!icmpv4_xrlim_allow(rt, type, code))
638 goto ende;
639
640 /* RFC says return as much as we can without exceeding 576 bytes. */
641
642 room = dst_mtu(&rt->u.dst);
643 if (room > 576)
644 room = 576;
645 room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
646 room -= sizeof(struct icmphdr);
647
648 icmp_param.data_len = skb_in->len - icmp_param.offset;
649 if (icmp_param.data_len > room)
650 icmp_param.data_len = room;
651 icmp_param.head_len = sizeof(struct icmphdr);
652
653 icmp_push_reply(&icmp_param, &ipc, rt);
654ende:
655 ip_rt_put(rt);
656out_unlock:
657 icmp_xmit_unlock();
658out:;
659}
660
661
662/*
663 * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
664 */
665
666static void icmp_unreach(struct sk_buff *skb)
667{
668 struct iphdr *iph;
669 struct icmphdr *icmph;
670 int hash, protocol;
671 struct net_protocol *ipprot;
1da177e4 672 u32 info = 0;
dde1bc0e
DL
673 struct net *net;
674
675 net = skb->dst->dev->nd_net;
1da177e4
LT
676
677 /*
678 * Incomplete header ?
679 * Only checks for the IP header, there should be an
680 * additional check for longer headers in upper levels.
681 */
682
683 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
684 goto out_err;
685
88c7664f 686 icmph = icmp_hdr(skb);
1da177e4
LT
687 iph = (struct iphdr *)skb->data;
688
689 if (iph->ihl < 5) /* Mangled header, drop. */
690 goto out_err;
691
692 if (icmph->type == ICMP_DEST_UNREACH) {
693 switch (icmph->code & 15) {
694 case ICMP_NET_UNREACH:
695 case ICMP_HOST_UNREACH:
696 case ICMP_PROT_UNREACH:
697 case ICMP_PORT_UNREACH:
698 break;
699 case ICMP_FRAG_NEEDED:
700 if (ipv4_config.no_pmtu_disc) {
64ce2073 701 LIMIT_NETDEBUG(KERN_INFO "ICMP: %u.%u.%u.%u: "
1da177e4
LT
702 "fragmentation needed "
703 "and DF set.\n",
64ce2073 704 NIPQUAD(iph->daddr));
1da177e4 705 } else {
dde1bc0e 706 info = ip_rt_frag_needed(net, iph,
1da177e4
LT
707 ntohs(icmph->un.frag.mtu));
708 if (!info)
709 goto out;
710 }
711 break;
712 case ICMP_SR_FAILED:
64ce2073 713 LIMIT_NETDEBUG(KERN_INFO "ICMP: %u.%u.%u.%u: Source "
1da177e4 714 "Route Failed.\n",
64ce2073 715 NIPQUAD(iph->daddr));
1da177e4
LT
716 break;
717 default:
718 break;
719 }
720 if (icmph->code > NR_ICMP_UNREACH)
721 goto out;
722 } else if (icmph->type == ICMP_PARAMETERPROB)
723 info = ntohl(icmph->un.gateway) >> 24;
724
725 /*
726 * Throw it at our lower layers
727 *
728 * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed
729 * header.
730 * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the
731 * transport layer.
732 * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to
733 * transport layer.
734 */
735
736 /*
737 * Check the other end isnt violating RFC 1122. Some routers send
738 * bogus responses to broadcast frames. If you see this message
739 * first check your netmask matches at both ends, if it does then
740 * get the other vendor to fix their kit.
741 */
742
743 if (!sysctl_icmp_ignore_bogus_error_responses &&
dde1bc0e 744 inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
1da177e4
LT
745 if (net_ratelimit())
746 printk(KERN_WARNING "%u.%u.%u.%u sent an invalid ICMP "
747 "type %u, code %u "
748 "error to a broadcast: %u.%u.%u.%u on %s\n",
eddc9ec5 749 NIPQUAD(ip_hdr(skb)->saddr),
1da177e4
LT
750 icmph->type, icmph->code,
751 NIPQUAD(iph->daddr),
752 skb->dev->name);
753 goto out;
754 }
755
756 /* Checkin full IP header plus 8 bytes of protocol to
757 * avoid additional coding at protocol handlers.
758 */
759 if (!pskb_may_pull(skb, iph->ihl * 4 + 8))
760 goto out;
761
762 iph = (struct iphdr *)skb->data;
763 protocol = iph->protocol;
764
765 /*
766 * Deliver ICMP message to raw sockets. Pretty useless feature?
767 */
7bc54c90 768 raw_icmp_error(skb, protocol, info);
1da177e4 769
1da177e4 770 hash = protocol & (MAX_INET_PROTOS - 1);
1da177e4
LT
771 rcu_read_lock();
772 ipprot = rcu_dereference(inet_protos[hash]);
773 if (ipprot && ipprot->err_handler)
774 ipprot->err_handler(skb, info);
775 rcu_read_unlock();
776
777out:
778 return;
779out_err:
780 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
781 goto out;
782}
783
784
785/*
786 * Handle ICMP_REDIRECT.
787 */
788
789static void icmp_redirect(struct sk_buff *skb)
790{
791 struct iphdr *iph;
1da177e4
LT
792
793 if (skb->len < sizeof(struct iphdr))
794 goto out_err;
795
796 /*
797 * Get the copied header of the packet that caused the redirect
798 */
799 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
800 goto out;
801
802 iph = (struct iphdr *)skb->data;
1da177e4 803
88c7664f 804 switch (icmp_hdr(skb)->code & 7) {
1da177e4
LT
805 case ICMP_REDIR_NET:
806 case ICMP_REDIR_NETTOS:
807 /*
808 * As per RFC recommendations now handle it as a host redirect.
809 */
810 case ICMP_REDIR_HOST:
811 case ICMP_REDIR_HOSTTOS:
eddc9ec5 812 ip_rt_redirect(ip_hdr(skb)->saddr, iph->daddr,
88c7664f 813 icmp_hdr(skb)->un.gateway,
cef2685e 814 iph->saddr, skb->dev);
1da177e4 815 break;
e905a9ed 816 }
1da177e4
LT
817out:
818 return;
819out_err:
820 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
821 goto out;
822}
823
824/*
825 * Handle ICMP_ECHO ("ping") requests.
826 *
827 * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo
828 * requests.
829 * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be
830 * included in the reply.
831 * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring
832 * echo requests, MUST have default=NOT.
833 * See also WRT handling of options once they are done and working.
834 */
835
836static void icmp_echo(struct sk_buff *skb)
837{
838 if (!sysctl_icmp_echo_ignore_all) {
839 struct icmp_bxm icmp_param;
840
88c7664f 841 icmp_param.data.icmph = *icmp_hdr(skb);
1da177e4
LT
842 icmp_param.data.icmph.type = ICMP_ECHOREPLY;
843 icmp_param.skb = skb;
844 icmp_param.offset = 0;
845 icmp_param.data_len = skb->len;
846 icmp_param.head_len = sizeof(struct icmphdr);
847 icmp_reply(&icmp_param, skb);
848 }
849}
850
851/*
852 * Handle ICMP Timestamp requests.
853 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
854 * SHOULD be in the kernel for minimum random latency.
855 * MUST be accurate to a few minutes.
856 * MUST be updated at least at 15Hz.
857 */
858static void icmp_timestamp(struct sk_buff *skb)
859{
860 struct timeval tv;
861 struct icmp_bxm icmp_param;
862 /*
863 * Too short.
864 */
865 if (skb->len < 4)
866 goto out_err;
867
868 /*
869 * Fill in the current time as ms since midnight UT:
870 */
871 do_gettimeofday(&tv);
872 icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * 1000 +
873 tv.tv_usec / 1000);
874 icmp_param.data.times[2] = icmp_param.data.times[1];
875 if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
876 BUG();
88c7664f 877 icmp_param.data.icmph = *icmp_hdr(skb);
1da177e4
LT
878 icmp_param.data.icmph.type = ICMP_TIMESTAMPREPLY;
879 icmp_param.data.icmph.code = 0;
880 icmp_param.skb = skb;
881 icmp_param.offset = 0;
882 icmp_param.data_len = 0;
883 icmp_param.head_len = sizeof(struct icmphdr) + 12;
884 icmp_reply(&icmp_param, skb);
885out:
886 return;
887out_err:
888 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
889 goto out;
890}
891
892
893/*
894 * Handle ICMP_ADDRESS_MASK requests. (RFC950)
895 *
896 * RFC1122 (3.2.2.9). A host MUST only send replies to
897 * ADDRESS_MASK requests if it's been configured as an address mask
898 * agent. Receiving a request doesn't constitute implicit permission to
899 * act as one. Of course, implementing this correctly requires (SHOULD)
900 * a way to turn the functionality on and off. Another one for sysctl(),
901 * I guess. -- MS
902 *
903 * RFC1812 (4.3.3.9). A router MUST implement it.
904 * A router SHOULD have switch turning it on/off.
905 * This switch MUST be ON by default.
906 *
907 * Gratuitous replies, zero-source replies are not implemented,
908 * that complies with RFC. DO NOT implement them!!! All the idea
909 * of broadcast addrmask replies as specified in RFC950 is broken.
910 * The problem is that it is not uncommon to have several prefixes
911 * on one physical interface. Moreover, addrmask agent can even be
912 * not aware of existing another prefixes.
913 * If source is zero, addrmask agent cannot choose correct prefix.
914 * Gratuitous mask announcements suffer from the same problem.
915 * RFC1812 explains it, but still allows to use ADDRMASK,
916 * that is pretty silly. --ANK
917 *
918 * All these rules are so bizarre, that I removed kernel addrmask
919 * support at all. It is wrong, it is obsolete, nobody uses it in
920 * any case. --ANK
921 *
922 * Furthermore you can do it with a usermode address agent program
923 * anyway...
924 */
925
926static void icmp_address(struct sk_buff *skb)
927{
928#if 0
929 if (net_ratelimit())
930 printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
931#endif
932}
933
934/*
935 * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
936 * loudly if an inconsistency is found.
937 */
938
939static void icmp_address_reply(struct sk_buff *skb)
940{
941 struct rtable *rt = (struct rtable *)skb->dst;
942 struct net_device *dev = skb->dev;
943 struct in_device *in_dev;
944 struct in_ifaddr *ifa;
945
946 if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
947 goto out;
948
949 in_dev = in_dev_get(dev);
950 if (!in_dev)
951 goto out;
952 rcu_read_lock();
953 if (in_dev->ifa_list &&
954 IN_DEV_LOG_MARTIANS(in_dev) &&
955 IN_DEV_FORWARD(in_dev)) {
a144ea4b 956 __be32 _mask, *mp;
1da177e4
LT
957
958 mp = skb_header_pointer(skb, 0, sizeof(_mask), &_mask);
09a62660 959 BUG_ON(mp == NULL);
1da177e4
LT
960 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
961 if (*mp == ifa->ifa_mask &&
962 inet_ifa_match(rt->rt_src, ifa))
963 break;
964 }
965 if (!ifa && net_ratelimit()) {
966 printk(KERN_INFO "Wrong address mask %u.%u.%u.%u from "
967 "%s/%u.%u.%u.%u\n",
968 NIPQUAD(*mp), dev->name, NIPQUAD(rt->rt_src));
969 }
970 }
971 rcu_read_unlock();
972 in_dev_put(in_dev);
973out:;
974}
975
976static void icmp_discard(struct sk_buff *skb)
977{
978}
979
980/*
981 * Deal with incoming ICMP packets.
982 */
983int icmp_rcv(struct sk_buff *skb)
984{
985 struct icmphdr *icmph;
986 struct rtable *rt = (struct rtable *)skb->dst;
987
aebcf82c 988 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
8b7817f3
HX
989 int nh;
990
aebcf82c
HX
991 if (!(skb->sp && skb->sp->xvec[skb->sp->len - 1]->props.flags &
992 XFRM_STATE_ICMP))
993 goto drop;
994
8b7817f3
HX
995 if (!pskb_may_pull(skb, sizeof(*icmph) + sizeof(struct iphdr)))
996 goto drop;
997
998 nh = skb_network_offset(skb);
999 skb_set_network_header(skb, sizeof(*icmph));
1000
1001 if (!xfrm4_policy_check_reverse(NULL, XFRM_POLICY_IN, skb))
1002 goto drop;
1003
1004 skb_set_network_header(skb, nh);
1005 }
1006
1da177e4
LT
1007 ICMP_INC_STATS_BH(ICMP_MIB_INMSGS);
1008
1009 switch (skb->ip_summed) {
84fa7933 1010 case CHECKSUM_COMPLETE:
d3bc23e7 1011 if (!csum_fold(skb->csum))
1da177e4 1012 break;
fb286bb2 1013 /* fall through */
1da177e4 1014 case CHECKSUM_NONE:
fb286bb2
HX
1015 skb->csum = 0;
1016 if (__skb_checksum_complete(skb))
1da177e4 1017 goto error;
1da177e4
LT
1018 }
1019
8cf22943
HX
1020 if (!pskb_pull(skb, sizeof(*icmph)))
1021 goto error;
1da177e4 1022
88c7664f 1023 icmph = icmp_hdr(skb);
1da177e4 1024
96793b48 1025 ICMPMSGIN_INC_STATS_BH(icmph->type);
1da177e4
LT
1026 /*
1027 * 18 is the highest 'known' ICMP type. Anything else is a mystery
1028 *
1029 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
1030 * discarded.
1031 */
1032 if (icmph->type > NR_ICMP_TYPES)
1033 goto error;
1034
1035
1036 /*
1037 * Parse the ICMP message
1038 */
1039
e905a9ed 1040 if (rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
1da177e4
LT
1041 /*
1042 * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
1043 * silently ignored (we let user decide with a sysctl).
1044 * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
1045 * discarded if to broadcast/multicast.
1046 */
4c866aa7
AK
1047 if ((icmph->type == ICMP_ECHO ||
1048 icmph->type == ICMP_TIMESTAMP) &&
1da177e4
LT
1049 sysctl_icmp_echo_ignore_broadcasts) {
1050 goto error;
1051 }
1052 if (icmph->type != ICMP_ECHO &&
1053 icmph->type != ICMP_TIMESTAMP &&
1054 icmph->type != ICMP_ADDRESS &&
1055 icmph->type != ICMP_ADDRESSREPLY) {
1056 goto error;
e905a9ed 1057 }
1da177e4
LT
1058 }
1059
1da177e4
LT
1060 icmp_pointers[icmph->type].handler(skb);
1061
1062drop:
1063 kfree_skb(skb);
1064 return 0;
1065error:
1066 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
1067 goto drop;
1068}
1069
1070/*
1071 * This table is the definition of how we handle ICMP.
1072 */
9b5b5cff 1073static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
1da177e4 1074 [ICMP_ECHOREPLY] = {
1da177e4
LT
1075 .handler = icmp_discard,
1076 },
1077 [1] = {
1da177e4
LT
1078 .handler = icmp_discard,
1079 .error = 1,
1080 },
1081 [2] = {
1da177e4
LT
1082 .handler = icmp_discard,
1083 .error = 1,
1084 },
1085 [ICMP_DEST_UNREACH] = {
1da177e4
LT
1086 .handler = icmp_unreach,
1087 .error = 1,
1088 },
1089 [ICMP_SOURCE_QUENCH] = {
1da177e4
LT
1090 .handler = icmp_unreach,
1091 .error = 1,
1092 },
1093 [ICMP_REDIRECT] = {
1da177e4
LT
1094 .handler = icmp_redirect,
1095 .error = 1,
1096 },
1097 [6] = {
1da177e4
LT
1098 .handler = icmp_discard,
1099 .error = 1,
1100 },
1101 [7] = {
1da177e4
LT
1102 .handler = icmp_discard,
1103 .error = 1,
1104 },
1105 [ICMP_ECHO] = {
1da177e4
LT
1106 .handler = icmp_echo,
1107 },
1108 [9] = {
1da177e4
LT
1109 .handler = icmp_discard,
1110 .error = 1,
1111 },
1112 [10] = {
1da177e4
LT
1113 .handler = icmp_discard,
1114 .error = 1,
1115 },
1116 [ICMP_TIME_EXCEEDED] = {
1da177e4
LT
1117 .handler = icmp_unreach,
1118 .error = 1,
1119 },
1120 [ICMP_PARAMETERPROB] = {
1da177e4
LT
1121 .handler = icmp_unreach,
1122 .error = 1,
1123 },
1124 [ICMP_TIMESTAMP] = {
1da177e4
LT
1125 .handler = icmp_timestamp,
1126 },
1127 [ICMP_TIMESTAMPREPLY] = {
1da177e4
LT
1128 .handler = icmp_discard,
1129 },
1130 [ICMP_INFO_REQUEST] = {
1da177e4
LT
1131 .handler = icmp_discard,
1132 },
e905a9ed 1133 [ICMP_INFO_REPLY] = {
1da177e4
LT
1134 .handler = icmp_discard,
1135 },
1136 [ICMP_ADDRESS] = {
1da177e4
LT
1137 .handler = icmp_address,
1138 },
1139 [ICMP_ADDRESSREPLY] = {
1da177e4
LT
1140 .handler = icmp_address_reply,
1141 },
1142};
1143
a5710d65 1144static void __exit icmp_exit(void)
1da177e4 1145{
1da177e4
LT
1146 int i;
1147
6f912042 1148 for_each_possible_cpu(i) {
b7e729c4 1149 struct sock *sk;
a5710d65 1150
b7e729c4
DL
1151 sk = per_cpu(__icmp_sk, i);
1152 if (sk == NULL)
a5710d65 1153 continue;
b7e729c4
DL
1154 per_cpu(__icmp_sk, i) = NULL;
1155 sock_release(sk->sk_socket);
a5710d65
DL
1156 }
1157}
1da177e4 1158
a5710d65
DL
1159int __init icmp_init(void)
1160{
a5710d65
DL
1161 int i, err;
1162
1163 for_each_possible_cpu(i) {
1e3cf683
DL
1164 struct sock *sk;
1165 struct socket *sock;
1166 struct inet_sock *inet;
1da177e4 1167
1e3cf683 1168 err = sock_create_kern(PF_INET, SOCK_RAW, IPPROTO_ICMP, &sock);
1da177e4 1169 if (err < 0)
a5710d65 1170 goto fail;
1da177e4 1171
b7e729c4 1172 per_cpu(__icmp_sk, i) = sk = sock->sk;
1e3cf683 1173 sk->sk_allocation = GFP_ATOMIC;
1da177e4
LT
1174
1175 /* Enough space for 2 64K ICMP packets, including
1176 * sk_buff struct overhead.
1177 */
1e3cf683 1178 sk->sk_sndbuf =
1da177e4
LT
1179 (2 * ((64 * 1024) + sizeof(struct sk_buff)));
1180
1e3cf683 1181 inet = inet_sk(sk);
1da177e4
LT
1182 inet->uc_ttl = -1;
1183 inet->pmtudisc = IP_PMTUDISC_DONT;
1184
1185 /* Unhash it so that IP input processing does not even
1186 * see it, we do not wish this socket to see incoming
1187 * packets.
1188 */
1e3cf683 1189 sk->sk_prot->unhash(sk);
1da177e4 1190 }
a5710d65
DL
1191 return 0;
1192
1193fail:
1194 icmp_exit();
1195 return err;
1da177e4
LT
1196}
1197
1198EXPORT_SYMBOL(icmp_err_convert);
1199EXPORT_SYMBOL(icmp_send);
1200EXPORT_SYMBOL(icmp_statistics);
1201EXPORT_SYMBOL(xrlim_allow);