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