source: rtems/cpukit/libnetworking/netinet/ip_icmp.c @ 261e743

4.104.114.84.95
Last change on this file since 261e743 was 261e743, checked in by Eric Norum <WENorum@…>, on 05/30/06 at 15:12:54

Add flag to inhibit ICMP replies.

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