1 /*
2 * NET3: Implementation of the ICMP protocol layer.
3 *
4 * Alan Cox, <alan@redhat.com>
5 *
6 * Version: $Id: icmp.c,v 1.82.2.1 2001/12/13 08:59:27 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 at all.
56 * Tristan Greaves : Added sysctl option to ignore bogus broadcast
57 * responses from broken routers.
58 *
59 * To Fix:
60 *
61 * - Should use skb_pull() instead of all the manual checking.
62 * This would also greatly simply some upper layer error handlers. --AK
63 *
64 */
65
66 #include <linux/config.h>
67 #include <linux/types.h>
68 #include <linux/sched.h>
69 #include <linux/kernel.h>
70 #include <linux/fcntl.h>
71 #include <linux/socket.h>
72 #include <linux/in.h>
73 #include <linux/inet.h>
74 #include <linux/netdevice.h>
75 #include <linux/string.h>
76 #include <linux/netfilter_ipv4.h>
77 #include <net/snmp.h>
78 #include <net/ip.h>
79 #include <net/route.h>
80 #include <net/protocol.h>
81 #include <net/icmp.h>
82 #include <net/tcp.h>
83 #include <net/udp.h>
84 #include <net/raw.h>
85 #include <linux/skbuff.h>
86 #include <net/sock.h>
87 #include <linux/errno.h>
88 #include <linux/timer.h>
89 #include <linux/init.h>
90 #include <asm/system.h>
91 #include <asm/uaccess.h>
92 #include <net/checksum.h>
93
94 /*
95 * Build xmit assembly blocks
96 */
97
98 struct icmp_bxm
99 {
100 struct sk_buff *skb;
101 int offset;
102 int data_len;
103
104 unsigned int csum;
105 struct {
106 struct icmphdr icmph;
107 __u32 times[3];
108 } data;
109 int head_len;
110 struct ip_options replyopts;
111 unsigned char optbuf[40];
112 };
113
114 /*
115 * Statistics
116 */
117
118 struct icmp_mib icmp_statistics[NR_CPUS*2];
119
120 /* An array of errno for error messages from dest unreach. */
121 /* RFC 1122: 3.2.2.1 States that NET_UNREACH, HOS_UNREACH and SR_FAIELD MUST be considered 'transient errs'. */
122
123 struct icmp_err icmp_err_convert[] = {
124 { ENETUNREACH, 0 }, /* ICMP_NET_UNREACH */
125 { EHOSTUNREACH, 0 }, /* ICMP_HOST_UNREACH */
126 { ENOPROTOOPT, 1 }, /* ICMP_PROT_UNREACH */
127 { ECONNREFUSED, 1 }, /* ICMP_PORT_UNREACH */
128 { EMSGSIZE, 0 }, /* ICMP_FRAG_NEEDED */
129 { EOPNOTSUPP, 0 }, /* ICMP_SR_FAILED */
130 { ENETUNREACH, 1 }, /* ICMP_NET_UNKNOWN */
131 { EHOSTDOWN, 1 }, /* ICMP_HOST_UNKNOWN */
132 { ENONET, 1 }, /* ICMP_HOST_ISOLATED */
133 { ENETUNREACH, 1 }, /* ICMP_NET_ANO */
134 { EHOSTUNREACH, 1 }, /* ICMP_HOST_ANO */
135 { ENETUNREACH, 0 }, /* ICMP_NET_UNR_TOS */
136 { EHOSTUNREACH, 0 }, /* ICMP_HOST_UNR_TOS */
137 { EHOSTUNREACH, 1 }, /* ICMP_PKT_FILTERED */
138 { EHOSTUNREACH, 1 }, /* ICMP_PREC_VIOLATION */
139 { EHOSTUNREACH, 1 } /* ICMP_PREC_CUTOFF */
140 };
141
142 extern int sysctl_ip_default_ttl;
143
144 /* Control parameters for ECHO replies. */
145 int sysctl_icmp_echo_ignore_all;
146 int sysctl_icmp_echo_ignore_broadcasts;
147
148 /* Control parameter - ignore bogus broadcast responses? */
149 int sysctl_icmp_ignore_bogus_error_responses;
150
151 /*
152 * Configurable global rate limit.
153 *
154 * ratelimit defines tokens/packet consumed for dst->rate_token bucket
155 * ratemask defines which icmp types are ratelimited by setting
156 * it's bit position.
157 *
158 * default:
159 * dest unreachable (3), source quench (4),
160 * time exceeded (11), parameter problem (12)
161 */
162
163 int sysctl_icmp_ratelimit = 1*HZ;
164 int sysctl_icmp_ratemask = 0x1818;
165
166 /*
167 * ICMP control array. This specifies what to do with each ICMP.
168 */
169
170 struct icmp_control
171 {
172 unsigned long *output; /* Address to increment on output */
173 unsigned long *input; /* Address to increment on input */
174 void (*handler)(struct sk_buff *skb);
175 short error; /* This ICMP is classed as an error message */
176 };
177
178 static struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];
179
180 /*
181 * The ICMP socket(s). This is the most convenient way to flow control
182 * our ICMP output as well as maintain a clean interface throughout
183 * all layers. All Socketless IP sends will soon be gone.
184 */
185
186 static struct inode __icmp_inode[NR_CPUS];
187 #define icmp_socket (&__icmp_inode[smp_processor_id()].u.socket_i)
188 #define icmp_socket_cpu(X) (&__icmp_inode[(X)].u.socket_i)
189
icmp_xmit_lock(void)190 static int icmp_xmit_lock(void)
191 {
192 local_bh_disable();
193 if (unlikely(!spin_trylock(&icmp_socket->sk->lock.slock))) {
194 /* This can happen if the output path signals a
195 * dst_link_failure() for an outgoing ICMP packet.
196 */
197 local_bh_enable();
198 return 1;
199 }
200 return 0;
201 }
202
icmp_xmit_unlock(void)203 static void icmp_xmit_unlock(void)
204 {
205 spin_unlock_bh(&icmp_socket->sk->lock.slock);
206 }
207
208 /*
209 * Send an ICMP frame.
210 */
211
212 /*
213 * Check transmit rate limitation for given message.
214 * The rate information is held in the destination cache now.
215 * This function is generic and could be used for other purposes
216 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
217 *
218 * Note that the same dst_entry fields are modified by functions in
219 * route.c too, but these work for packet destinations while xrlim_allow
220 * works for icmp destinations. This means the rate limiting information
221 * for one "ip object" is shared - and these ICMPs are twice limited:
222 * by source and by destination.
223 *
224 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
225 * SHOULD allow setting of rate limits
226 *
227 * Shared between ICMPv4 and ICMPv6.
228 */
229 #define XRLIM_BURST_FACTOR 6
xrlim_allow(struct dst_entry * dst,int timeout)230 int xrlim_allow(struct dst_entry *dst, int timeout)
231 {
232 unsigned long now;
233
234 now = jiffies;
235 dst->rate_tokens += now - dst->rate_last;
236 dst->rate_last = now;
237 if (dst->rate_tokens > XRLIM_BURST_FACTOR*timeout)
238 dst->rate_tokens = XRLIM_BURST_FACTOR*timeout;
239 if (dst->rate_tokens >= timeout) {
240 dst->rate_tokens -= timeout;
241 return 1;
242 }
243 return 0;
244 }
245
icmpv4_xrlim_allow(struct rtable * rt,int type,int code)246 static inline int icmpv4_xrlim_allow(struct rtable *rt, int type, int code)
247 {
248 struct dst_entry *dst = &rt->u.dst;
249
250 if (type > NR_ICMP_TYPES)
251 return 1;
252
253 /* Don't limit PMTU discovery. */
254 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
255 return 1;
256
257 /* No rate limit on loopback */
258 if (dst->dev && (dst->dev->flags&IFF_LOOPBACK))
259 return 1;
260
261 /* Limit if icmp type is enabled in ratemask. */
262 if((1 << type) & sysctl_icmp_ratemask)
263 return xrlim_allow(dst, sysctl_icmp_ratelimit);
264 else
265 return 1;
266 }
267
268 /*
269 * Maintain the counters used in the SNMP statistics for outgoing ICMP
270 */
271
icmp_out_count(int type)272 static void icmp_out_count(int type)
273 {
274 if (type>NR_ICMP_TYPES)
275 return;
276 (icmp_pointers[type].output)[(smp_processor_id()*2+!in_softirq())*sizeof(struct icmp_mib)/sizeof(unsigned long)]++;
277 ICMP_INC_STATS(IcmpOutMsgs);
278 }
279
280 /*
281 * Checksum each fragment, and on the first include the headers and final checksum.
282 */
283
icmp_glue_bits(const void * p,char * to,unsigned int offset,unsigned int fraglen,struct sk_buff * skb)284 static int icmp_glue_bits(const void *p, char *to, unsigned int offset,
285 unsigned int fraglen, struct sk_buff *skb)
286 {
287 struct icmp_bxm *icmp_param = (struct icmp_bxm *)p;
288 struct icmphdr *icmph;
289 unsigned int csum;
290
291 if (icmp_pointers[icmp_param->data.icmph.type].error)
292 nf_ct_attach(skb, icmp_param->skb);
293
294 if (offset) {
295 icmp_param->csum=skb_copy_and_csum_bits(icmp_param->skb,
296 icmp_param->offset+(offset-icmp_param->head_len),
297 to, fraglen,icmp_param->csum);
298 return 0;
299 }
300
301 /*
302 * First fragment includes header. Note that we've done
303 * the other fragments first, so that we get the checksum
304 * for the whole packet here.
305 */
306 csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
307 to, icmp_param->head_len,
308 icmp_param->csum);
309 csum=skb_copy_and_csum_bits(icmp_param->skb,
310 icmp_param->offset,
311 to+icmp_param->head_len,
312 fraglen-icmp_param->head_len,
313 csum);
314 icmph=(struct icmphdr *)to;
315 icmph->checksum = csum_fold(csum);
316 return 0;
317 }
318
319 /*
320 * Driving logic for building and sending ICMP messages.
321 */
322
icmp_reply(struct icmp_bxm * icmp_param,struct sk_buff * skb)323 static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
324 {
325 struct sock *sk=icmp_socket->sk;
326 struct ipcm_cookie ipc;
327 struct rtable *rt = (struct rtable*)skb->dst;
328 u32 daddr;
329
330 if (ip_options_echo(&icmp_param->replyopts, skb))
331 return;
332
333 if (icmp_xmit_lock())
334 return;
335
336 icmp_param->data.icmph.checksum=0;
337 icmp_param->csum=0;
338 icmp_out_count(icmp_param->data.icmph.type);
339
340 sk->protinfo.af_inet.tos = skb->nh.iph->tos;
341 sk->protinfo.af_inet.ttl = sysctl_ip_default_ttl;
342 daddr = ipc.addr = rt->rt_src;
343 ipc.opt = NULL;
344 if (icmp_param->replyopts.optlen) {
345 ipc.opt = &icmp_param->replyopts;
346 if (ipc.opt->srr)
347 daddr = icmp_param->replyopts.faddr;
348 }
349 if (ip_route_output(&rt, daddr, rt->rt_spec_dst, RT_TOS(skb->nh.iph->tos), 0))
350 goto out;
351 if (icmpv4_xrlim_allow(rt, icmp_param->data.icmph.type,
352 icmp_param->data.icmph.code)) {
353 ip_build_xmit(sk, icmp_glue_bits, icmp_param,
354 icmp_param->data_len+icmp_param->head_len,
355 &ipc, rt, MSG_DONTWAIT);
356 }
357 ip_rt_put(rt);
358 out:
359 icmp_xmit_unlock();
360 }
361
362
363 /*
364 * Send an ICMP message in response to a situation
365 *
366 * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
367 * MUST NOT change this header information.
368 * MUST NOT reply to a multicast/broadcast IP address.
369 * MUST NOT reply to a multicast/broadcast MAC address.
370 * MUST reply to only the first fragment.
371 */
372
icmp_send(struct sk_buff * skb_in,int type,int code,u32 info)373 void icmp_send(struct sk_buff *skb_in, int type, int code, u32 info)
374 {
375 struct iphdr *iph;
376 int room;
377 struct icmp_bxm icmp_param;
378 struct rtable *rt = (struct rtable*)skb_in->dst;
379 struct ipcm_cookie ipc;
380 u32 saddr;
381 u8 tos;
382
383 if (!rt)
384 return;
385
386 /*
387 * Find the original header. It is expected to be valid, of course.
388 * Check this, icmp_send is called from the most obscure devices
389 * sometimes.
390 */
391 iph = skb_in->nh.iph;
392
393 if ((u8*)iph < skb_in->head || (u8*)(iph+1) > skb_in->tail)
394 return;
395
396 /*
397 * No replies to physical multicast/broadcast
398 */
399 if (skb_in->pkt_type!=PACKET_HOST)
400 return;
401
402 /*
403 * Now check at the protocol level
404 */
405 if (rt->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST))
406 return;
407
408 /*
409 * Only reply to fragment 0. We byte re-order the constant
410 * mask for efficiency.
411 */
412 if (iph->frag_off&htons(IP_OFFSET))
413 return;
414
415 /*
416 * If we send an ICMP error to an ICMP error a mess would result..
417 */
418 if (icmp_pointers[type].error) {
419 /*
420 * We are an error, check if we are replying to an ICMP error
421 */
422 if (iph->protocol==IPPROTO_ICMP) {
423 u8 inner_type;
424
425 if (skb_copy_bits(skb_in,
426 skb_in->nh.raw + (iph->ihl<<2)
427 + offsetof(struct icmphdr, type)
428 - skb_in->data,
429 &inner_type, 1))
430 return;
431
432 /*
433 * Assume any unknown ICMP type is an error. This isn't
434 * specified by the RFC, but think about it..
435 */
436 if (inner_type>NR_ICMP_TYPES || icmp_pointers[inner_type].error)
437 return;
438 }
439 }
440
441 if (icmp_xmit_lock())
442 return;
443
444 /*
445 * Construct source address and options.
446 */
447
448 #ifdef CONFIG_IP_ROUTE_NAT
449 /*
450 * Restore original addresses if packet has been translated.
451 */
452 if (rt->rt_flags&RTCF_NAT && IPCB(skb_in)->flags&IPSKB_TRANSLATED) {
453 iph->daddr = rt->key.dst;
454 iph->saddr = rt->key.src;
455 }
456 #endif
457
458 saddr = iph->daddr;
459 if (!(rt->rt_flags & RTCF_LOCAL))
460 saddr = 0;
461
462 tos = icmp_pointers[type].error ?
463 ((iph->tos & IPTOS_TOS_MASK) | IPTOS_PREC_INTERNETCONTROL) :
464 iph->tos;
465
466 if (ip_route_output(&rt, iph->saddr, saddr, RT_TOS(tos), 0))
467 goto out;
468
469 if (ip_options_echo(&icmp_param.replyopts, skb_in))
470 goto ende;
471
472
473 /*
474 * Prepare data for ICMP header.
475 */
476
477 icmp_param.data.icmph.type=type;
478 icmp_param.data.icmph.code=code;
479 icmp_param.data.icmph.un.gateway = info;
480 icmp_param.data.icmph.checksum=0;
481 icmp_param.csum=0;
482 icmp_param.skb=skb_in;
483 icmp_param.offset=skb_in->nh.raw - skb_in->data;
484 icmp_out_count(icmp_param.data.icmph.type);
485 icmp_socket->sk->protinfo.af_inet.tos = tos;
486 icmp_socket->sk->protinfo.af_inet.ttl = sysctl_ip_default_ttl;
487 ipc.addr = iph->saddr;
488 ipc.opt = &icmp_param.replyopts;
489 if (icmp_param.replyopts.srr) {
490 ip_rt_put(rt);
491 if (ip_route_output(&rt, icmp_param.replyopts.faddr, saddr, RT_TOS(tos), 0))
492 goto out;
493 }
494
495 if (!icmpv4_xrlim_allow(rt, type, code))
496 goto ende;
497
498 /* RFC says return as much as we can without exceeding 576 bytes. */
499
500 room = rt->u.dst.pmtu;
501 if (room > 576)
502 room = 576;
503 room -= sizeof(struct iphdr) + icmp_param.replyopts.optlen;
504 room -= sizeof(struct icmphdr);
505
506 icmp_param.data_len=skb_in->len-icmp_param.offset;
507 if (icmp_param.data_len > room)
508 icmp_param.data_len = room;
509 icmp_param.head_len = sizeof(struct icmphdr);
510
511 ip_build_xmit(icmp_socket->sk, icmp_glue_bits, &icmp_param,
512 icmp_param.data_len+sizeof(struct icmphdr),
513 &ipc, rt, MSG_DONTWAIT);
514
515 ende:
516 ip_rt_put(rt);
517 out:
518 icmp_xmit_unlock();
519 }
520
521
522 /*
523 * Handle ICMP_DEST_UNREACH, ICMP_TIME_EXCEED, and ICMP_QUENCH.
524 */
525
icmp_unreach(struct sk_buff * skb)526 static void icmp_unreach(struct sk_buff *skb)
527 {
528 struct iphdr *iph;
529 struct icmphdr *icmph;
530 int hash, protocol;
531 struct inet_protocol *ipprot;
532 struct sock *raw_sk;
533 u32 info = 0;
534
535 /*
536 * Incomplete header ?
537 * Only checks for the IP header, there should be an
538 * additional check for longer headers in upper levels.
539 */
540
541 if (!pskb_may_pull(skb, sizeof(struct iphdr))) {
542 ICMP_INC_STATS_BH(IcmpInErrors);
543 return;
544 }
545
546 icmph = skb->h.icmph;
547 iph = (struct iphdr *) skb->data;
548
549 if (iph->ihl<5) {
550 /* Mangled header, drop. */
551 ICMP_INC_STATS_BH(IcmpInErrors);
552 return;
553 }
554
555 if(icmph->type==ICMP_DEST_UNREACH) {
556 switch(icmph->code & 15) {
557 case ICMP_NET_UNREACH:
558 break;
559 case ICMP_HOST_UNREACH:
560 break;
561 case ICMP_PROT_UNREACH:
562 break;
563 case ICMP_PORT_UNREACH:
564 break;
565 case ICMP_FRAG_NEEDED:
566 if (ipv4_config.no_pmtu_disc) {
567 if (net_ratelimit())
568 printk(KERN_INFO "ICMP: %u.%u.%u.%u: fragmentation needed and DF set.\n",
569 NIPQUAD(iph->daddr));
570 } else {
571 info = ip_rt_frag_needed(iph, ntohs(icmph->un.frag.mtu));
572 if (!info)
573 goto out;
574 }
575 break;
576 case ICMP_SR_FAILED:
577 if (net_ratelimit())
578 printk(KERN_INFO "ICMP: %u.%u.%u.%u: Source Route Failed.\n", NIPQUAD(iph->daddr));
579 break;
580 default:
581 break;
582 }
583 if (icmph->code>NR_ICMP_UNREACH)
584 goto out;
585 } else if (icmph->type == ICMP_PARAMETERPROB) {
586 info = ntohl(icmph->un.gateway)>>24;
587 }
588
589 /*
590 * Throw it at our lower layers
591 *
592 * RFC 1122: 3.2.2 MUST extract the protocol ID from the passed header.
593 * RFC 1122: 3.2.2.1 MUST pass ICMP unreach messages to the transport layer.
594 * RFC 1122: 3.2.2.2 MUST pass ICMP time expired messages to transport layer.
595 */
596
597 /*
598 * Check the other end isnt violating RFC 1122. Some routers send
599 * bogus responses to broadcast frames. If you see this message
600 * first check your netmask matches at both ends, if it does then
601 * get the other vendor to fix their kit.
602 */
603
604 if (!sysctl_icmp_ignore_bogus_error_responses)
605 {
606
607 if (inet_addr_type(iph->daddr) == RTN_BROADCAST)
608 {
609 if (net_ratelimit())
610 printk(KERN_WARNING "%u.%u.%u.%u sent an invalid ICMP type %u, code %u error to a broadcast: %u.%u.%u.%u on %s\n",
611 NIPQUAD(skb->nh.iph->saddr),
612 icmph->type, icmph->code,
613 NIPQUAD(iph->daddr),
614 skb->dev->name);
615 goto out;
616 }
617 }
618
619 /* Checkin full IP header plus 8 bytes of protocol to
620 * avoid additional coding at protocol handlers.
621 */
622 if (!pskb_may_pull(skb, iph->ihl*4+8))
623 goto out;
624
625 iph = (struct iphdr *) skb->data;
626 protocol = iph->protocol;
627
628 /*
629 * Deliver ICMP message to raw sockets. Pretty useless feature?
630 */
631
632 /* Note: See raw.c and net/raw.h, RAWV4_HTABLE_SIZE==MAX_INET_PROTOS */
633 hash = protocol & (MAX_INET_PROTOS - 1);
634 read_lock(&raw_v4_lock);
635 if ((raw_sk = raw_v4_htable[hash]) != NULL)
636 {
637 while ((raw_sk = __raw_v4_lookup(raw_sk, protocol, iph->daddr,
638 iph->saddr, skb->dev->ifindex)) != NULL) {
639 raw_err(raw_sk, skb, info);
640 raw_sk = raw_sk->next;
641 iph = (struct iphdr *)skb->data;
642 }
643 }
644 read_unlock(&raw_v4_lock);
645
646 /*
647 * This can't change while we are doing it.
648 * Callers have obtained BR_NETPROTO_LOCK so
649 * we are OK.
650 */
651
652 ipprot = (struct inet_protocol *) inet_protos[hash];
653 while (ipprot) {
654 struct inet_protocol *nextip;
655
656 nextip = (struct inet_protocol *) ipprot->next;
657
658 /*
659 * Pass it off to everyone who wants it.
660 */
661
662 /* RFC1122: OK. Passes appropriate ICMP errors to the */
663 /* appropriate protocol layer (MUST), as per 3.2.2. */
664
665 if (protocol == ipprot->protocol && ipprot->err_handler)
666 ipprot->err_handler(skb, info);
667
668 ipprot = nextip;
669 }
670 out:;
671 }
672
673
674 /*
675 * Handle ICMP_REDIRECT.
676 */
677
icmp_redirect(struct sk_buff * skb)678 static void icmp_redirect(struct sk_buff *skb)
679 {
680 struct iphdr *iph;
681 unsigned long ip;
682
683 if (skb->len < sizeof(struct iphdr)) {
684 ICMP_INC_STATS_BH(IcmpInErrors);
685 return;
686 }
687
688 /*
689 * Get the copied header of the packet that caused the redirect
690 */
691 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
692 return;
693
694 iph = (struct iphdr *) skb->data;
695 ip = iph->daddr;
696
697 switch (skb->h.icmph->code & 7) {
698 case ICMP_REDIR_NET:
699 case ICMP_REDIR_NETTOS:
700 /*
701 * As per RFC recommendations now handle it as
702 * a host redirect.
703 */
704
705 case ICMP_REDIR_HOST:
706 case ICMP_REDIR_HOSTTOS:
707 ip_rt_redirect(skb->nh.iph->saddr, ip, skb->h.icmph->un.gateway, iph->saddr, iph->tos, skb->dev);
708 break;
709 default:
710 break;
711 }
712 }
713
714 /*
715 * Handle ICMP_ECHO ("ping") requests.
716 *
717 * RFC 1122: 3.2.2.6 MUST have an echo server that answers ICMP echo requests.
718 * RFC 1122: 3.2.2.6 Data received in the ICMP_ECHO request MUST be included in the reply.
719 * RFC 1812: 4.3.3.6 SHOULD have a config option for silently ignoring echo requests, MUST have default=NOT.
720 * See also WRT handling of options once they are done and working.
721 */
722
icmp_echo(struct sk_buff * skb)723 static void icmp_echo(struct sk_buff *skb)
724 {
725 if (!sysctl_icmp_echo_ignore_all) {
726 struct icmp_bxm icmp_param;
727
728 icmp_param.data.icmph=*skb->h.icmph;
729 icmp_param.data.icmph.type=ICMP_ECHOREPLY;
730 icmp_param.skb=skb;
731 icmp_param.offset=0;
732 icmp_param.data_len=skb->len;
733 icmp_param.head_len=sizeof(struct icmphdr);
734 icmp_reply(&icmp_param, skb);
735 }
736 }
737
738 /*
739 * Handle ICMP Timestamp requests.
740 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
741 * SHOULD be in the kernel for minimum random latency.
742 * MUST be accurate to a few minutes.
743 * MUST be updated at least at 15Hz.
744 */
745
icmp_timestamp(struct sk_buff * skb)746 static void icmp_timestamp(struct sk_buff *skb)
747 {
748 struct timeval tv;
749 struct icmp_bxm icmp_param;
750
751 /*
752 * Too short.
753 */
754
755 if (skb->len < 4) {
756 ICMP_INC_STATS_BH(IcmpInErrors);
757 return;
758 }
759
760 /*
761 * Fill in the current time as ms since midnight UT:
762 */
763 do_gettimeofday(&tv);
764 icmp_param.data.times[1] = htonl((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
765 icmp_param.data.times[2] = icmp_param.data.times[1];
766 if (skb_copy_bits(skb, 0, &icmp_param.data.times[0], 4))
767 BUG();
768 icmp_param.data.icmph=*skb->h.icmph;
769 icmp_param.data.icmph.type=ICMP_TIMESTAMPREPLY;
770 icmp_param.data.icmph.code=0;
771 icmp_param.skb=skb;
772 icmp_param.offset=0;
773 icmp_param.data_len=0;
774 icmp_param.head_len=sizeof(struct icmphdr)+12;
775 icmp_reply(&icmp_param, skb);
776 }
777
778
779 /*
780 * Handle ICMP_ADDRESS_MASK requests. (RFC950)
781 *
782 * RFC1122 (3.2.2.9). A host MUST only send replies to
783 * ADDRESS_MASK requests if it's been configured as an address mask
784 * agent. Receiving a request doesn't constitute implicit permission to
785 * act as one. Of course, implementing this correctly requires (SHOULD)
786 * a way to turn the functionality on and off. Another one for sysctl(),
787 * I guess. -- MS
788 *
789 * RFC1812 (4.3.3.9). A router MUST implement it.
790 * A router SHOULD have switch turning it on/off.
791 * This switch MUST be ON by default.
792 *
793 * Gratuitous replies, zero-source replies are not implemented,
794 * that complies with RFC. DO NOT implement them!!! All the idea
795 * of broadcast addrmask replies as specified in RFC950 is broken.
796 * The problem is that it is not uncommon to have several prefixes
797 * on one physical interface. Moreover, addrmask agent can even be
798 * not aware of existing another prefixes.
799 * If source is zero, addrmask agent cannot choose correct prefix.
800 * Gratuitous mask announcements suffer from the same problem.
801 * RFC1812 explains it, but still allows to use ADDRMASK,
802 * that is pretty silly. --ANK
803 *
804 * All these rules are so bizarre, that I removed kernel addrmask
805 * support at all. It is wrong, it is obsolete, nobody uses it in
806 * any case. --ANK
807 *
808 * Furthermore you can do it with a usermode address agent program
809 * anyway...
810 */
811
icmp_address(struct sk_buff * skb)812 static void icmp_address(struct sk_buff *skb)
813 {
814 #if 0
815 if (net_ratelimit())
816 printk(KERN_DEBUG "a guy asks for address mask. Who is it?\n");
817 #endif
818 }
819
820 /*
821 * RFC1812 (4.3.3.9). A router SHOULD listen all replies, and complain
822 * loudly if an inconsistency is found.
823 */
824
icmp_address_reply(struct sk_buff * skb)825 static void icmp_address_reply(struct sk_buff *skb)
826 {
827 struct rtable *rt = (struct rtable*)skb->dst;
828 struct net_device *dev = skb->dev;
829 struct in_device *in_dev;
830 struct in_ifaddr *ifa;
831 u32 mask;
832
833 if (skb->len < 4 || !(rt->rt_flags&RTCF_DIRECTSRC))
834 return;
835
836 in_dev = in_dev_get(dev);
837 if (!in_dev)
838 return;
839 read_lock(&in_dev->lock);
840 if (in_dev->ifa_list &&
841 IN_DEV_LOG_MARTIANS(in_dev) &&
842 IN_DEV_FORWARD(in_dev)) {
843 if (skb_copy_bits(skb, 0, &mask, 4))
844 BUG();
845 for (ifa=in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
846 if (mask == ifa->ifa_mask && inet_ifa_match(rt->rt_src, ifa))
847 break;
848 }
849 if (!ifa && net_ratelimit()) {
850 printk(KERN_INFO "Wrong address mask %u.%u.%u.%u from %s/%u.%u.%u.%u\n",
851 NIPQUAD(mask), dev->name, NIPQUAD(rt->rt_src));
852 }
853 }
854 read_unlock(&in_dev->lock);
855 in_dev_put(in_dev);
856 }
857
icmp_discard(struct sk_buff * skb)858 static void icmp_discard(struct sk_buff *skb)
859 {
860 }
861
862 /*
863 * Deal with incoming ICMP packets.
864 */
865
icmp_rcv(struct sk_buff * skb)866 int icmp_rcv(struct sk_buff *skb)
867 {
868 struct icmphdr *icmph;
869 struct rtable *rt = (struct rtable*)skb->dst;
870
871 ICMP_INC_STATS_BH(IcmpInMsgs);
872
873 switch (skb->ip_summed) {
874 case CHECKSUM_HW:
875 if ((u16)csum_fold(skb->csum) == 0)
876 break;
877 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG "icmp v4 hw csum failure\n"));
878 case CHECKSUM_NONE:
879 if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0)))
880 goto error;
881 default:;
882 }
883
884 if (!pskb_pull(skb, sizeof(struct icmphdr)))
885 goto error;
886
887 icmph = skb->h.icmph;
888
889 /*
890 * 18 is the highest 'known' ICMP type. Anything else is a mystery
891 *
892 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently discarded.
893 */
894 if (icmph->type > NR_ICMP_TYPES)
895 goto error;
896
897
898 /*
899 * Parse the ICMP message
900 */
901
902 if (rt->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST)) {
903 /*
904 * RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
905 * silently ignored (we let user decide with a sysctl).
906 * RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
907 * discarded if to broadcast/multicast.
908 */
909 if (icmph->type == ICMP_ECHO &&
910 sysctl_icmp_echo_ignore_broadcasts) {
911 goto error;
912 }
913 if (icmph->type != ICMP_ECHO &&
914 icmph->type != ICMP_TIMESTAMP &&
915 icmph->type != ICMP_ADDRESS &&
916 icmph->type != ICMP_ADDRESSREPLY) {
917 goto error;
918 }
919 }
920
921 icmp_pointers[icmph->type].input[smp_processor_id()*2*sizeof(struct icmp_mib)/sizeof(unsigned long)]++;
922 (icmp_pointers[icmph->type].handler)(skb);
923
924 drop:
925 kfree_skb(skb);
926 return 0;
927 error:
928 ICMP_INC_STATS_BH(IcmpInErrors);
929 goto drop;
930 }
931
932 /*
933 * This table is the definition of how we handle ICMP.
934 */
935
936 static struct icmp_control icmp_pointers[NR_ICMP_TYPES+1] = {
937 /* ECHO REPLY (0) */
938 { &icmp_statistics[0].IcmpOutEchoReps, &icmp_statistics[0].IcmpInEchoReps, icmp_discard, 0 },
939 { &icmp_statistics[0].dummy, &icmp_statistics[0].IcmpInErrors, icmp_discard, 1 },
940 { &icmp_statistics[0].dummy, &icmp_statistics[0].IcmpInErrors, icmp_discard, 1 },
941 /* DEST UNREACH (3) */
942 { &icmp_statistics[0].IcmpOutDestUnreachs, &icmp_statistics[0].IcmpInDestUnreachs, icmp_unreach, 1 },
943 /* SOURCE QUENCH (4) */
944 { &icmp_statistics[0].IcmpOutSrcQuenchs, &icmp_statistics[0].IcmpInSrcQuenchs, icmp_unreach, 1 },
945 /* REDIRECT (5) */
946 { &icmp_statistics[0].IcmpOutRedirects, &icmp_statistics[0].IcmpInRedirects, icmp_redirect, 1 },
947 { &icmp_statistics[0].dummy, &icmp_statistics[0].IcmpInErrors, icmp_discard, 1 },
948 { &icmp_statistics[0].dummy, &icmp_statistics[0].IcmpInErrors, icmp_discard, 1 },
949 /* ECHO (8) */
950 { &icmp_statistics[0].IcmpOutEchos, &icmp_statistics[0].IcmpInEchos, icmp_echo, 0 },
951 { &icmp_statistics[0].dummy, &icmp_statistics[0].IcmpInErrors, icmp_discard, 1 },
952 { &icmp_statistics[0].dummy, &icmp_statistics[0].IcmpInErrors, icmp_discard, 1 },
953 /* TIME EXCEEDED (11) */
954 { &icmp_statistics[0].IcmpOutTimeExcds, &icmp_statistics[0].IcmpInTimeExcds, icmp_unreach, 1 },
955 /* PARAMETER PROBLEM (12) */
956 { &icmp_statistics[0].IcmpOutParmProbs, &icmp_statistics[0].IcmpInParmProbs, icmp_unreach, 1 },
957 /* TIMESTAMP (13) */
958 { &icmp_statistics[0].IcmpOutTimestamps, &icmp_statistics[0].IcmpInTimestamps, icmp_timestamp, 0 },
959 /* TIMESTAMP REPLY (14) */
960 { &icmp_statistics[0].IcmpOutTimestampReps, &icmp_statistics[0].IcmpInTimestampReps, icmp_discard, 0 },
961 /* INFO (15) */
962 { &icmp_statistics[0].dummy, &icmp_statistics[0].dummy, icmp_discard, 0 },
963 /* INFO REPLY (16) */
964 { &icmp_statistics[0].dummy, &icmp_statistics[0].dummy, icmp_discard, 0 },
965 /* ADDR MASK (17) */
966 { &icmp_statistics[0].IcmpOutAddrMasks, &icmp_statistics[0].IcmpInAddrMasks, icmp_address, 0 },
967 /* ADDR MASK REPLY (18) */
968 { &icmp_statistics[0].IcmpOutAddrMaskReps, &icmp_statistics[0].IcmpInAddrMaskReps, icmp_address_reply, 0 }
969 };
970
icmp_init(struct net_proto_family * ops)971 void __init icmp_init(struct net_proto_family *ops)
972 {
973 int err, i;
974
975 for (i = 0; i < NR_CPUS; i++) {
976 __icmp_inode[i].i_mode = S_IFSOCK;
977 __icmp_inode[i].i_sock = 1;
978 __icmp_inode[i].i_uid = 0;
979 __icmp_inode[i].i_gid = 0;
980 init_waitqueue_head(&__icmp_inode[i].i_wait);
981 init_waitqueue_head(&__icmp_inode[i].u.socket_i.wait);
982
983 icmp_socket_cpu(i)->inode = &__icmp_inode[i];
984 icmp_socket_cpu(i)->state = SS_UNCONNECTED;
985 icmp_socket_cpu(i)->type = SOCK_RAW;
986
987 if ((err=ops->create(icmp_socket_cpu(i), IPPROTO_ICMP)) < 0)
988 panic("Failed to create the ICMP control socket.\n");
989
990 icmp_socket_cpu(i)->sk->allocation=GFP_ATOMIC;
991
992 /* Enough space for 2 64K ICMP packets, including
993 * sk_buff struct overhead.
994 */
995 icmp_socket_cpu(i)->sk->sndbuf =
996 (2 * ((64 * 1024) + sizeof(struct sk_buff)));
997
998 icmp_socket_cpu(i)->sk->protinfo.af_inet.ttl = MAXTTL;
999 icmp_socket_cpu(i)->sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_DONT;
1000
1001 /* Unhash it so that IP input processing does not even
1002 * see it, we do not wish this socket to see incoming
1003 * packets.
1004 */
1005 icmp_socket_cpu(i)->sk->prot->unhash(icmp_socket_cpu(i)->sk);
1006 }
1007 }
1008