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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [netinet/] [ip_icmp.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/sys/netinet/ip_icmp.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*
23
 * Copyright (c) 1982, 1986, 1988, 1993
24
 *      The Regents of the University of California.  All rights reserved.
25
 *
26
 * Redistribution and use in source and binary forms, with or without
27
 * modification, are permitted provided that the following conditions
28
 * are met:
29
 * 1. Redistributions of source code must retain the above copyright
30
 *    notice, this list of conditions and the following disclaimer.
31
 * 2. Redistributions in binary form must reproduce the above copyright
32
 *    notice, this list of conditions and the following disclaimer in the
33
 *    documentation and/or other materials provided with the distribution.
34
 * 3. All advertising materials mentioning features or use of this software
35
 *    must display the following acknowledgement:
36
 *      This product includes software developed by the University of
37
 *      California, Berkeley and its contributors.
38
 * 4. Neither the name of the University nor the names of its contributors
39
 *    may be used to endorse or promote products derived from this software
40
 *    without specific prior written permission.
41
 *
42
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
 * SUCH DAMAGE.
53
 *
54
 *      @(#)ip_icmp.c   8.2 (Berkeley) 1/4/94
55
 * $FreeBSD: src/sys/netinet/ip_icmp.c,v 1.39.2.9 2001/07/03 11:01:46 ume Exp $
56
 */
57
 
58
#include <sys/param.h>
59
#include <sys/mbuf.h>
60
#include <sys/protosw.h>
61
#include <sys/socket.h>
62
 
63
#include <net/if.h>
64
#include <net/route.h>
65
 
66
#define _IP_VHL
67
#include <netinet/in.h>
68
#include <netinet/in_systm.h>
69
#include <netinet/in_var.h>
70
#include <netinet/ip.h>
71
#include <netinet/ip_icmp.h>
72
#include <netinet/ip_var.h>
73
#include <netinet/icmp_var.h>
74
 
75
#ifdef IPSEC
76
#include <netinet6/ipsec.h>
77
#include <netkey/key.h>
78
#endif
79
 
80
/*
81
 * ICMP routines: error generation, receive packet processing, and
82
 * routines to turnaround packets back to the originator, and
83
 * host table maintenance routines.
84
 */
85
 
86
static int      icmpmaskrepl = 0;
87
static int      drop_redirect = 0;
88
static int      log_redirect = 0;
89
 
90
#ifdef ICMP_BANDLIM 
91
 
92
/*
93
 * ICMP error-response bandwidth limiting sysctl.  If not enabled, sysctl
94
 *      variable content is -1 and read-only.
95
 */
96
 
97
static int      icmplim = 200;
98
#else
99
 
100
static int      icmplim = -1;
101
#endif 
102
 
103
/*
104
 * ICMP broadcast echo sysctl
105
 */
106
 
107
static int      icmpbmcastecho = 0;
108
 
109
#ifdef ICMPPRINTFS
110
int     icmpprintfs = 0;
111
#endif
112
 
113
static void     icmp_reflect __P((struct mbuf *));
114
static void     icmp_send __P((struct mbuf *, struct mbuf *));
115
static int      ip_next_mtu __P((int, int));
116
 
117
extern  struct protosw inetsw[];
118
 
119
/*
120
 * Generate an error packet of type error
121
 * in response to bad packet ip.
122
 */
123
void
124
icmp_error(n, type, code, dest, destifp)
125
        struct mbuf *n;
126
        int type, code;
127
        n_long dest;
128
        struct ifnet *destifp;
129
{
130
        register struct ip *oip = mtod(n, struct ip *), *nip;
131
        register unsigned oiplen = IP_VHL_HL(oip->ip_vhl) << 2;
132
        register struct icmp *icp;
133
        register struct mbuf *m;
134
        unsigned icmplen;
135
 
136
#ifdef ICMPPRINTFS
137
        if (icmpprintfs)
138
                printf("icmp_error(%p, %x, %d)\n", oip, type, code);
139
#endif
140
        if (type != ICMP_REDIRECT)
141
                icmpstat.icps_error++;
142
        /*
143
         * Don't send error if not the first fragment of message.
144
         * Don't error if the old packet protocol was ICMP
145
         * error message, only known informational types.
146
         */
147
        if (oip->ip_off &~ (IP_MF|IP_DF))
148
                goto freeit;
149
        if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
150
          n->m_len >= oiplen + ICMP_MINLEN &&
151
          !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
152
                icmpstat.icps_oldicmp++;
153
                goto freeit;
154
        }
155
        /* Don't send error in response to a multicast or broadcast packet */
156
        if (n->m_flags & (M_BCAST|M_MCAST))
157
                goto freeit;
158
        /*
159
         * First, formulate icmp message
160
         */
161
        m = m_gethdr(M_DONTWAIT, MT_HEADER);
162
        if (m == NULL)
163
                goto freeit;
164
        icmplen = min(oiplen + 8, oip->ip_len);
165
        if (icmplen < sizeof(struct ip)) {
166
                printf("icmp_error: bad length\n");
167
                m_free(m);
168
                goto freeit;
169
        }
170
        m->m_len = icmplen + ICMP_MINLEN;
171
        MH_ALIGN(m, m->m_len);
172
        icp = mtod(m, struct icmp *);
173
        if ((u_int)type > ICMP_MAXTYPE)
174
                panic("icmp_error");
175
        icmpstat.icps_outhist[type]++;
176
        icp->icmp_type = type;
177
        if (type == ICMP_REDIRECT)
178
                icp->icmp_gwaddr.s_addr = dest;
179
        else {
180
                icp->icmp_void = 0;
181
                /*
182
                 * The following assignments assume an overlay with the
183
                 * zeroed icmp_void field.
184
                 */
185
                if (type == ICMP_PARAMPROB) {
186
                        icp->icmp_pptr = code;
187
                        code = 0;
188
                } else if (type == ICMP_UNREACH &&
189
                        code == ICMP_UNREACH_NEEDFRAG && destifp) {
190
                        icp->icmp_nextmtu = htons(destifp->if_mtu);
191
                }
192
        }
193
 
194
        icp->icmp_code = code;
195
        m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
196
        nip = &icp->icmp_ip;
197
 
198
        /*
199
         * Convert fields to network representation.
200
         */
201
        HTONS(nip->ip_len);
202
        HTONS(nip->ip_off);
203
 
204
        /*
205
         * Now, copy old ip header (without options)
206
         * in front of icmp message.
207
         */
208
        if (m->m_data - sizeof(struct ip) < m->m_pktdat)
209
                panic("icmp len");
210
        m->m_data -= sizeof(struct ip);
211
        m->m_len += sizeof(struct ip);
212
        m->m_pkthdr.len = m->m_len;
213
        m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
214
        nip = mtod(m, struct ip *);
215
        bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
216
        nip->ip_len = m->m_len;
217
        nip->ip_vhl = IP_VHL_BORING;
218
        nip->ip_p = IPPROTO_ICMP;
219
        nip->ip_tos = 0;
220
        icmp_reflect(m);
221
 
222
freeit:
223
        m_freem(n);
224
}
225
 
226
static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
227
static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
228
static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
229
 
230
/*
231
 * Process a received ICMP message.
232
 */
233
void
234
icmp_input(m, off)
235
        register struct mbuf *m;
236
        int off;
237
{
238
        int hlen = off;
239
        register struct icmp *icp;
240
        register struct ip *ip = mtod(m, struct ip *);
241
        int icmplen = ip->ip_len;
242
        register int i;
243
        struct in_ifaddr *ia;
244
        void (*ctlfunc) __P((int, struct sockaddr *, void *));
245
        int code;
246
 
247
        /*
248
         * Locate icmp structure in mbuf, and check
249
         * that not corrupted and of at least minimum length.
250
         */
251
#ifdef ICMPPRINTFS
252
        if (icmpprintfs) {
253
                char buf[4 * sizeof "123"];
254
                strcpy(buf, inet_ntoa(ip->ip_src));
255
                printf("icmp_input from %s to %s, len %d\n",
256
                       buf, inet_ntoa(ip->ip_dst), icmplen);
257
        }
258
#endif
259
        if (icmplen < ICMP_MINLEN) {
260
                icmpstat.icps_tooshort++;
261
                goto freeit;
262
        }
263
        i = hlen + min(icmplen, ICMP_ADVLENMIN);
264
        if (m->m_len < i && (m = m_pullup(m, i)) == 0)  {
265
                icmpstat.icps_tooshort++;
266
                return;
267
        }
268
        ip = mtod(m, struct ip *);
269
        m->m_len -= hlen;
270
        m->m_data += hlen;
271
        icp = mtod(m, struct icmp *);
272
        if (in_cksum(m, icmplen)) {
273
                icmpstat.icps_checksum++;
274
                goto freeit;
275
        }
276
        m->m_len += hlen;
277
        m->m_data -= hlen;
278
 
279
#if defined(NFAITH) && 0 < NFAITH
280
        if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
281
                /*
282
                 * Deliver very specific ICMP type only.
283
                 */
284
                switch (icp->icmp_type) {
285
                case ICMP_UNREACH:
286
                case ICMP_TIMXCEED:
287
                        break;
288
                default:
289
                        goto freeit;
290
                }
291
        }
292
#endif
293
 
294
#ifdef ICMPPRINTFS
295
        if (icmpprintfs)
296
                printf("icmp_input, type %d code %d\n", icp->icmp_type,
297
                    icp->icmp_code);
298
#endif
299
 
300
        /*
301
         * Message type specific processing.
302
         */
303
        if (icp->icmp_type > ICMP_MAXTYPE)
304
                goto raw;
305
        icmpstat.icps_inhist[icp->icmp_type]++;
306
        code = icp->icmp_code;
307
        switch (icp->icmp_type) {
308
 
309
        case ICMP_UNREACH:
310
                switch (code) {
311
                        case ICMP_UNREACH_NET:
312
                        case ICMP_UNREACH_HOST:
313
                        case ICMP_UNREACH_SRCFAIL:
314
                        case ICMP_UNREACH_NET_UNKNOWN:
315
                        case ICMP_UNREACH_HOST_UNKNOWN:
316
                        case ICMP_UNREACH_ISOLATED:
317
                        case ICMP_UNREACH_TOSNET:
318
                        case ICMP_UNREACH_TOSHOST:
319
                        case ICMP_UNREACH_HOST_PRECEDENCE:
320
                        case ICMP_UNREACH_PRECEDENCE_CUTOFF:
321
                                code = PRC_UNREACH_NET;
322
                                break;
323
 
324
                        case ICMP_UNREACH_NEEDFRAG:
325
                                code = PRC_MSGSIZE;
326
                                break;
327
 
328
                        /*
329
                         * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
330
                         * Treat subcodes 2,3 as immediate RST
331
                         */
332
                        case ICMP_UNREACH_PROTOCOL:
333
                        case ICMP_UNREACH_PORT:
334
                                code = PRC_UNREACH_PORT;
335
                                break;
336
 
337
                        case ICMP_UNREACH_NET_PROHIB:
338
                        case ICMP_UNREACH_HOST_PROHIB:
339
                        case ICMP_UNREACH_FILTER_PROHIB:
340
                                code = PRC_UNREACH_ADMIN_PROHIB;
341
                                break;
342
 
343
                        default:
344
                                goto badcode;
345
                }
346
                goto deliver;
347
 
348
        case ICMP_TIMXCEED:
349
                if (code > 1)
350
                        goto badcode;
351
                code += PRC_TIMXCEED_INTRANS;
352
                goto deliver;
353
 
354
        case ICMP_PARAMPROB:
355
                if (code > 1)
356
                        goto badcode;
357
                code = PRC_PARAMPROB;
358
                goto deliver;
359
 
360
        case ICMP_SOURCEQUENCH:
361
                if (code)
362
                        goto badcode;
363
                code = PRC_QUENCH;
364
        deliver:
365
                /*
366
                 * Problem with datagram; advise higher level routines.
367
                 */
368
                if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
369
                    IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
370
                        icmpstat.icps_badlen++;
371
                        goto freeit;
372
                }
373
                NTOHS(icp->icmp_ip.ip_len);
374
                /* Discard ICMP's in response to multicast packets */
375
                if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
376
                        goto badcode;
377
#ifdef ICMPPRINTFS
378
                if (icmpprintfs)
379
                        printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
380
#endif
381
                icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
382
#if 1
383
                /*
384
                 * MTU discovery:
385
                 * If we got a needfrag and there is a host route to the
386
                 * original destination, and the MTU is not locked, then
387
                 * set the MTU in the route to the suggested new value
388
                 * (if given) and then notify as usual.  The ULPs will
389
                 * notice that the MTU has changed and adapt accordingly.
390
                 * If no new MTU was suggested, then we guess a new one
391
                 * less than the current value.  If the new MTU is
392
                 * unreasonably small (arbitrarily set at 296), then
393
                 * we reset the MTU to the interface value and enable the
394
                 * lock bit, indicating that we are no longer doing MTU
395
                 * discovery.
396
                 */
397
                if (code == PRC_MSGSIZE) {
398
                        struct rtentry *rt;
399
                        int mtu;
400
 
401
                        rt = rtalloc1((struct sockaddr *)&icmpsrc, 0,
402
                                      RTF_CLONING | RTF_PRCLONING);
403
                        if (rt && (rt->rt_flags & RTF_HOST)
404
                            && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
405
                                mtu = ntohs(icp->icmp_nextmtu);
406
                                if (!mtu)
407
                                        mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu,
408
                                                          1);
409
#ifdef DEBUG_MTUDISC
410
                                printf("MTU for %s reduced to %d\n",
411
                                        inet_ntoa(icmpsrc.sin_addr), mtu);
412
#endif
413
                                if (mtu < 296) {
414
                                        /* rt->rt_rmx.rmx_mtu =
415
                                                rt->rt_ifp->if_mtu; */
416
                                        rt->rt_rmx.rmx_locks |= RTV_MTU;
417
                                } else if (rt->rt_rmx.rmx_mtu > mtu) {
418
                                        rt->rt_rmx.rmx_mtu = mtu;
419
                                }
420
                        }
421
                        if (rt)
422
                                RTFREE(rt);
423
                }
424
 
425
#endif
426
                /*
427
                 * XXX if the packet contains [IPv4 AH TCP], we can't make a
428
                 * notification to TCP layer.
429
                 */
430
                ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
431
                if (ctlfunc)
432
                        (*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
433
                                   (void *)&icp->icmp_ip);
434
                break;
435
 
436
        badcode:
437
                icmpstat.icps_badcode++;
438
                break;
439
 
440
        case ICMP_ECHO:
441
                if (!icmpbmcastecho
442
                    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
443
                        icmpstat.icps_bmcastecho++;
444
                        break;
445
                }
446
                icp->icmp_type = ICMP_ECHOREPLY;
447
#ifdef ICMP_BANDLIM
448
                if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
449
                        goto freeit;
450
                else
451
#endif
452
                        goto reflect;
453
 
454
        case ICMP_TSTAMP:
455
                if (!icmpbmcastecho
456
                    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
457
                        icmpstat.icps_bmcasttstamp++;
458
                        break;
459
                }
460
                if (icmplen < ICMP_TSLEN) {
461
                        icmpstat.icps_badlen++;
462
                        break;
463
                }
464
                icp->icmp_type = ICMP_TSTAMPREPLY;
465
                icp->icmp_rtime = iptime();
466
                icp->icmp_ttime = icp->icmp_rtime;      /* bogus, do later! */
467
#ifdef ICMP_BANDLIM
468
                if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
469
                        goto freeit;
470
                else
471
#endif
472
                        goto reflect;
473
 
474
        case ICMP_MASKREQ:
475
#define satosin(sa)     ((struct sockaddr_in *)(sa))
476
                if (icmpmaskrepl == 0)
477
                        break;
478
                /*
479
                 * We are not able to respond with all ones broadcast
480
                 * unless we receive it over a point-to-point interface.
481
                 */
482
                if (icmplen < ICMP_MASKLEN)
483
                        break;
484
                switch (ip->ip_dst.s_addr) {
485
 
486
                case INADDR_BROADCAST:
487
                case INADDR_ANY:
488
                        icmpdst.sin_addr = ip->ip_src;
489
                        break;
490
 
491
                default:
492
                        icmpdst.sin_addr = ip->ip_dst;
493
                }
494
                ia = (struct in_ifaddr *)ifaof_ifpforaddr(
495
                            (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
496
                if (ia == 0)
497
                        break;
498
                if (ia->ia_ifp == 0)
499
                        break;
500
                icp->icmp_type = ICMP_MASKREPLY;
501
                icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
502
                if (ip->ip_src.s_addr == 0) {
503
                        if (ia->ia_ifp->if_flags & IFF_BROADCAST)
504
                            ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
505
                        else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
506
                            ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
507
                }
508
reflect:
509
                ip->ip_len += hlen;     /* since ip_input deducts this */
510
                icmpstat.icps_reflect++;
511
                icmpstat.icps_outhist[icp->icmp_type]++;
512
                icmp_reflect(m);
513
                return;
514
 
515
        case ICMP_REDIRECT:
516
                if (log_redirect) {
517
                        u_long src, dst, gw;
518
 
519
                        src = ntohl(ip->ip_src.s_addr);
520
                        dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
521
                        gw = ntohl(icp->icmp_gwaddr.s_addr);
522
                        printf("icmp redirect from %d.%d.%d.%d: "
523
                               "%d.%d.%d.%d => %d.%d.%d.%d\n",
524
                               (int)(src >> 24), (int)((src >> 16) & 0xff),
525
                               (int)((src >> 8) & 0xff), (int)(src & 0xff),
526
                               (int)(dst >> 24), (int)((dst >> 16) & 0xff),
527
                               (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
528
                               (int)(gw >> 24), (int)((gw >> 16) & 0xff),
529
                               (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
530
                }
531
                if (drop_redirect)
532
                        break;
533
                if (code > 3)
534
                        goto badcode;
535
                if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
536
                    IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
537
                        icmpstat.icps_badlen++;
538
                        break;
539
                }
540
                /*
541
                 * Short circuit routing redirects to force
542
                 * immediate change in the kernel's routing
543
                 * tables.  The message is also handed to anyone
544
                 * listening on a raw socket (e.g. the routing
545
                 * daemon for use in updating its tables).
546
                 */
547
                icmpgw.sin_addr = ip->ip_src;
548
                icmpdst.sin_addr = icp->icmp_gwaddr;
549
#ifdef  ICMPPRINTFS
550
                if (icmpprintfs) {
551
                        char buf[4 * sizeof "123"];
552
                        strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
553
 
554
                        printf("redirect dst %s to %s\n",
555
                               buf, inet_ntoa(icp->icmp_gwaddr));
556
                }
557
#endif
558
                icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
559
                rtredirect((struct sockaddr *)&icmpsrc,
560
                  (struct sockaddr *)&icmpdst,
561
                  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
562
                  (struct sockaddr *)&icmpgw, (struct rtentry **)0);
563
                pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
564
#ifdef IPSEC
565
                key_sa_routechange((struct sockaddr *)&icmpsrc);
566
#endif
567
                break;
568
 
569
        /*
570
         * No kernel processing for the following;
571
         * just fall through to send to raw listener.
572
         */
573
        case ICMP_ECHOREPLY:
574
        case ICMP_ROUTERADVERT:
575
        case ICMP_ROUTERSOLICIT:
576
        case ICMP_TSTAMPREPLY:
577
        case ICMP_IREQREPLY:
578
        case ICMP_MASKREPLY:
579
        default:
580
                break;
581
        }
582
 
583
raw:
584
        rip_input(m, off);
585
        return;
586
 
587
freeit:
588
        m_freem(m);
589
}
590
 
591
/*
592
 * Reflect the ip packet back to the source
593
 */
594
static void
595
icmp_reflect(m)
596
        struct mbuf *m;
597
{
598
        register struct ip *ip = mtod(m, struct ip *);
599
        register struct in_ifaddr *ia;
600
        struct in_addr t;
601
        struct mbuf *opts = 0;
602
        int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
603
 
604
        if (!in_canforward(ip->ip_src) &&
605
            ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
606
             (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
607
                m_freem(m);     /* Bad return address */
608
                goto done;      /* Ip_output() will check for broadcast */
609
        }
610
        t = ip->ip_dst;
611
        ip->ip_dst = ip->ip_src;
612
        /*
613
         * If the incoming packet was addressed directly to us,
614
         * use dst as the src for the reply.  Otherwise (broadcast
615
         * or anonymous), use the address which corresponds
616
         * to the incoming interface.
617
         */
618
        for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) {
619
                if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
620
                        break;
621
                if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) &&
622
                    t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
623
                        break;
624
        }
625
        icmpdst.sin_addr = t;
626
        if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif)
627
                ia = (struct in_ifaddr *)ifaof_ifpforaddr(
628
                        (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
629
        /*
630
         * The following happens if the packet was not addressed to us,
631
         * and was received on an interface with no IP address.
632
         */
633
        if (ia == (struct in_ifaddr *)0)
634
                ia = in_ifaddrhead.tqh_first;
635
        t = IA_SIN(ia)->sin_addr;
636
        ip->ip_src = t;
637
        ip->ip_ttl = ip_defttl;
638
 
639
        if (optlen > 0) {
640
                register u_char *cp;
641
                int opt, cnt;
642
                u_int len;
643
 
644
                /*
645
                 * Retrieve any source routing from the incoming packet;
646
                 * add on any record-route or timestamp options.
647
                 */
648
                cp = (u_char *) (ip + 1);
649
                if ((opts = ip_srcroute()) == 0 &&
650
                    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
651
                        opts->m_len = sizeof(struct in_addr);
652
                        mtod(opts, struct in_addr *)->s_addr = 0;
653
                }
654
                if (opts) {
655
#ifdef ICMPPRINTFS
656
                    if (icmpprintfs)
657
                            printf("icmp_reflect optlen %d rt %d => ",
658
                                optlen, opts->m_len);
659
#endif
660
                    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
661
                            opt = cp[IPOPT_OPTVAL];
662
                            if (opt == IPOPT_EOL)
663
                                    break;
664
                            if (opt == IPOPT_NOP)
665
                                    len = 1;
666
                            else {
667
                                    if (cnt < IPOPT_OLEN + sizeof(*cp))
668
                                            break;
669
                                    len = cp[IPOPT_OLEN];
670
                                    if (len < IPOPT_OLEN + sizeof(*cp) ||
671
                                        len > cnt)
672
                                            break;
673
                            }
674
                            /*
675
                             * Should check for overflow, but it "can't happen"
676
                             */
677
                            if (opt == IPOPT_RR || opt == IPOPT_TS ||
678
                                opt == IPOPT_SECURITY) {
679
                                    bcopy((caddr_t)cp,
680
                                        mtod(opts, caddr_t) + opts->m_len, len);
681
                                    opts->m_len += len;
682
                            }
683
                    }
684
                    /* Terminate & pad, if necessary */
685
                    cnt = opts->m_len % 4;
686
                    if (cnt) {
687
                            for (; cnt < 4; cnt++) {
688
                                    *(mtod(opts, caddr_t) + opts->m_len) =
689
                                        IPOPT_EOL;
690
                                    opts->m_len++;
691
                            }
692
                    }
693
#ifdef ICMPPRINTFS
694
                    if (icmpprintfs)
695
                            printf("%d\n", opts->m_len);
696
#endif
697
                }
698
                /*
699
                 * Now strip out original options by copying rest of first
700
                 * mbuf's data back, and adjust the IP length.
701
                 */
702
                ip->ip_len -= optlen;
703
                ip->ip_vhl = IP_VHL_BORING;
704
                m->m_len -= optlen;
705
                if (m->m_flags & M_PKTHDR)
706
                        m->m_pkthdr.len -= optlen;
707
                optlen += sizeof(struct ip);
708
                bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
709
                         (unsigned)(m->m_len - sizeof(struct ip)));
710
        }
711
        m->m_flags &= ~(M_BCAST|M_MCAST);
712
        icmp_send(m, opts);
713
done:
714
        if (opts)
715
                (void)m_free(opts);
716
}
717
 
718
/*
719
 * Send an icmp packet back to the ip level,
720
 * after supplying a checksum.
721
 */
722
static void
723
icmp_send(m, opts)
724
        register struct mbuf *m;
725
        struct mbuf *opts;
726
{
727
        register struct ip *ip = mtod(m, struct ip *);
728
        register int hlen;
729
        register struct icmp *icp;
730
        struct route ro;
731
 
732
        hlen = IP_VHL_HL(ip->ip_vhl) << 2;
733
        m->m_data += hlen;
734
        m->m_len -= hlen;
735
        icp = mtod(m, struct icmp *);
736
        icp->icmp_cksum = 0;
737
        icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
738
        m->m_data -= hlen;
739
        m->m_len += hlen;
740
        m->m_pkthdr.rcvif = (struct ifnet *)0;
741
#ifdef ICMPPRINTFS
742
        if (icmpprintfs) {
743
                char buf[4 * sizeof "123"];
744
                strcpy(buf, inet_ntoa(ip->ip_dst));
745
                printf("icmp_send dst %s src %s\n",
746
                       buf, inet_ntoa(ip->ip_src));
747
        }
748
#endif
749
        bzero(&ro, sizeof ro);
750
        (void) ip_output(m, opts, &ro, 0, NULL);
751
        if (ro.ro_rt)
752
                RTFREE(ro.ro_rt);
753
}
754
 
755
n_time
756
iptime()
757
{
758
        struct timeval atv;
759
        u_long t;
760
 
761
        microtime(&atv);
762
        t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
763
        return (htonl(t));
764
}
765
 
766
#if 1
767
/*
768
 * Return the next larger or smaller MTU plateau (table from RFC 1191)
769
 * given current value MTU.  If DIR is less than zero, a larger plateau
770
 * is returned; otherwise, a smaller value is returned.
771
 */
772
static int
773
ip_next_mtu(mtu, dir)
774
        int mtu;
775
        int dir;
776
{
777
        static int mtutab[] = {
778
                65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296,
779
                68, 0
780
        };
781
        int i;
782
 
783
        for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
784
                if (mtu >= mtutab[i])
785
                        break;
786
        }
787
 
788
        if (dir < 0) {
789
                if (i == 0) {
790
                        return 0;
791
                } else {
792
                        return mtutab[i - 1];
793
                }
794
        } else {
795
                if (mtutab[i] == 0) {
796
                        return 0;
797
                } else if(mtu > mtutab[i]) {
798
                        return mtutab[i];
799
                } else {
800
                        return mtutab[i + 1];
801
                }
802
        }
803
}
804
#endif
805
 
806
#ifdef ICMP_BANDLIM
807
 
808
/*
809
 * badport_bandlim() - check for ICMP bandwidth limit
810
 *
811
 *      Return 0 if it is ok to send an ICMP error response, -1 if we have
812
 *      hit our bandwidth limit and it is not ok.
813
 *
814
 *      If icmplim is <= 0, the feature is disabled and 0 is returned.
815
 *
816
 *      For now we separate the TCP and UDP subsystems w/ different 'which'
817
 *      values.  We may eventually remove this separation (and simplify the
818
 *      code further).
819
 *
820
 *      Note that the printing of the error message is delayed so we can
821
 *      properly print the icmp error rate that the system was trying to do
822
 *      (i.e. 22000/100 pps, etc...).  This can cause long delays in printing
823
 *      the 'final' error, but it doesn't make sense to solve the printing
824
 *      delay with more complex code.
825
 */
826
 
827
int
828
badport_bandlim(int which)
829
{
830
        static int lticks[BANDLIM_MAX + 1];
831
        static int lpackets[BANDLIM_MAX + 1];
832
        int dticks;
833
        const char *bandlimittype[] = {
834
                "Limiting icmp unreach response",
835
                "Limiting icmp ping response",
836
                "Limiting icmp tstamp response",
837
                "Limiting closed port RST response",
838
                "Limiting open port RST response"
839
                };
840
 
841
        /*
842
         * Return ok status if feature disabled or argument out of
843
         * ranage.
844
         */
845
 
846
        if (icmplim <= 0 || which > BANDLIM_MAX || which < 0)
847
                return(0);
848
        dticks = ticks - lticks[which];
849
 
850
        /*
851
         * reset stats when cumulative dt exceeds one second.
852
         */
853
 
854
        if ((unsigned int)dticks > hz) {
855
                if (lpackets[which] > icmplim) {
856
                        printf("%s from %d to %d packets per second\n",
857
                                bandlimittype[which],
858
                                lpackets[which],
859
                                icmplim
860
                        );
861
                }
862
                lticks[which] = ticks;
863
                lpackets[which] = 0;
864
        }
865
 
866
        /*
867
         * bump packet count
868
         */
869
 
870
        if (++lpackets[which] > icmplim) {
871
                return(-1);
872
        }
873
        return(0);
874
}
875
 
876
#endif
877
 
878
 

powered by: WebSVN 2.1.0

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