OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [icmp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.