source: rtems/cpukit/libnetworking/netinet/ip_icmp.c @ 6e401331

4.104.115
Last change on this file since 6e401331 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/10 at 05:50:29

Add HAVE_CONFIG_H support to let files receive configure defines.

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