source: rtems/cpukit/libnetworking/netinet/ip_icmp.c @ 657e6c93

5
Last change on this file since 657e6c93 was 657e6c93, checked in by Christian Mauderer <Christian.Mauderer@…>, on 06/24/16 at 05:57:17

libnetworking: Import current <netinet/in.h>

Import the <netinet/in.h> from current FreeBSD. This allows to build
some current software (e.g. libressl).

Add legacy support like

  • prototype for in_cksum(),
  • IPPORT_USERRESERVED,
  • deprecated IPCTL_RT* defines,
  • ip_fw_chk_t and ip_fw_ctl_t,
  • ip_nat_... (IP NAT hooks), and
  • IP_NAT option for get/setsockopt()

to new <rtems/rtems_netinet_in.h>.

  • Property mode set to 100644
File size: 19.3 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#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#include <sys/param.h>
39#include <sys/systm.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/sysctl.h>
46
47#include <net/if.h>
48#include <net/if_types.h>
49#include <net/route.h>
50
51#define _IP_VHL
52#include <netinet/in.h>
53#include <rtems/rtems_netinet_in.h>
54#include <netinet/in_pcb.h>
55#include <netinet/in_systm.h>
56#include <netinet/in_var.h>
57#include <netinet/ip.h>
58#include <netinet/ip_icmp.h>
59#include <netinet/ip_var.h>
60#include <netinet/tcp.h>
61#include <netinet/tcp_var.h>
62#include <netinet/tcpip.h>
63#include <netinet/icmp_var.h>
64
65#ifdef IPSEC
66#include <netinet6/ipsec.h>
67#include <netkey/key.h>
68#endif
69
70#ifdef FAST_IPSEC
71#include <netipsec/ipsec.h>
72#include <netipsec/key.h>
73#define IPSEC
74#endif
75
76#include <machine/in_cksum.h>
77
78/*
79 * ICMP routines: error generation, receive packet processing, and
80 * routines to turnaround packets back to the originator, and
81 * host table maintenance routines.
82 */
83
84       struct   icmpstat icmpstat;
85SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RD,
86        &icmpstat, icmpstat, "");
87
88static int      icmpmaskrepl = 0;
89SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
90        &icmpmaskrepl, 0, "");
91
92static int      icmpbmcastecho = 1;
93SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, &icmpbmcastecho,
94           0, "");
95
96static int  icmpallecho = 1;
97SYSCTL_INT(_net_inet_icmp, OID_AUTO, allecho, CTLFLAG_RW, &icmpallecho,
98           0, "");
99
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)) == NULL)  {
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 == NULL)
470                        break;
471                if (ia->ia_ifp == NULL) {
472                        break;
473                }
474                icp->icmp_type = ICMP_MASKREPLY;
475                icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
476                if (ip->ip_src.s_addr == 0) {
477                        if (ia->ia_ifp->if_flags & IFF_BROADCAST)
478                            ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
479                        else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
480                            ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
481                }
482reflect:
483                ip->ip_len += hlen;     /* since ip_input deducts this */
484                icmpstat.icps_reflect++;
485                icmpstat.icps_outhist[icp->icmp_type]++;
486                icmp_reflect(m);
487                return;
488
489        case ICMP_REDIRECT:
490                if (code > 3)
491                        goto badcode;
492                if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
493#ifdef _IP_VHL
494                    IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) {
495#else
496                    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
497#endif
498                        icmpstat.icps_badlen++;
499                        break;
500                }
501                /*
502                 * Short circuit routing redirects to force
503                 * immediate change in the kernel's routing
504                 * tables.  The message is also handed to anyone
505                 * listening on a raw socket (e.g. the routing
506                 * daemon for use in updating its tables).
507                 */
508                icmpgw.sin_addr = ip->ip_src;
509                icmpdst.sin_addr = icp->icmp_gwaddr;
510#ifdef  ICMPPRINTFS
511                if (icmpprintfs) {
512                        char buf[4 * sizeof "123"];
513                        strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
514
515                        printf("redirect dst %s to %s\n",
516                               buf, inet_ntoa(icp->icmp_gwaddr));
517                }
518#endif
519                icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
520                rtredirect((struct sockaddr *)&icmpsrc,
521                  (struct sockaddr *)&icmpdst,
522                  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
523                  (struct sockaddr *)&icmpgw, (struct rtentry **)0);
524                pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
525                break;
526
527        /*
528         * No kernel processing for the following;
529         * just fall through to send to raw listener.
530         */
531        case ICMP_ECHOREPLY:
532        case ICMP_ROUTERADVERT:
533        case ICMP_ROUTERSOLICIT:
534        case ICMP_TSTAMPREPLY:
535        case ICMP_IREQREPLY:
536        case ICMP_MASKREPLY:
537        default:
538                break;
539        }
540
541raw:
542        rip_input(m, hlen);
543        return;
544
545freeit:
546        m_freem(m);
547}
548
549/*
550 * Reflect the ip packet back to the source
551 */
552static void
553icmp_reflect(struct mbuf *m)
554{
555        struct ip *ip = mtod(m, struct ip *);
556        struct in_ifaddr *ia;
557        struct in_addr t;
558        struct mbuf *opts = 0;
559#ifdef _IP_VHL
560        int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
561#else
562        int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
563#endif
564        if (!in_canforward(ip->ip_src) &&
565            ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
566             (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
567                m_freem(m);     /* Bad return address */
568                goto done;      /* Ip_output() will check for broadcast */
569        }
570        t = ip->ip_dst;
571        ip->ip_dst = ip->ip_src;
572        /*
573         * If the incoming packet was addressed directly to us,
574         * use dst as the src for the reply.  Otherwise (broadcast
575         * or anonymous), use the address which corresponds
576         * to the incoming interface.
577         */
578        for (ia = in_ifaddr; ia; ia = ia->ia_next) {
579                if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
580                        break;
581                if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) &&
582                    t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
583                        break;
584        }
585        icmpdst.sin_addr = t;
586        if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif)
587                ia = (struct in_ifaddr *)ifaof_ifpforaddr(
588                        (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
589        /*
590         * The following happens if the packet was not addressed to us,
591         * and was received on an interface with no IP address.
592         */
593        if (ia == (struct in_ifaddr *)0)
594                ia = in_ifaddr;
595        t = IA_SIN(ia)->sin_addr;
596        ip->ip_src = t;
597        ip->ip_ttl = MAXTTL;
598
599        if (optlen > 0) {
600                register u_char *cp;
601                int opt, cnt;
602                u_int len;
603
604                /*
605                 * Retrieve any source routing from the incoming packet;
606                 * add on any record-route or timestamp options.
607                 */
608                cp = (u_char *) (ip + 1);
609                if ((opts = ip_srcroute()) == 0 &&
610                    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
611                        opts->m_len = sizeof(struct in_addr);
612                        mtod(opts, struct in_addr *)->s_addr = 0;
613                }
614                if (opts) {
615#ifdef ICMPPRINTFS
616                    if (icmpprintfs)
617                            printf("icmp_reflect optlen %d rt %d => ",
618                                optlen, opts->m_len);
619#endif
620                    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
621                            opt = cp[IPOPT_OPTVAL];
622                            if (opt == IPOPT_EOL)
623                                    break;
624                            if (opt == IPOPT_NOP)
625                                    len = 1;
626                            else {
627                                    if (cnt < IPOPT_OLEN + sizeof(*cp))
628                                            break;
629                                    len = cp[IPOPT_OLEN];
630                                    if (len < IPOPT_OLEN + sizeof(*cp) ||
631                                        len > cnt)
632                                            break;
633                            }
634                            /*
635                             * Should check for overflow, but it "can't happen"
636                             */
637                            if (opt == IPOPT_RR || opt == IPOPT_TS ||
638                                opt == IPOPT_SECURITY) {
639                                    bcopy((caddr_t)cp,
640                                        mtod(opts, caddr_t) + opts->m_len, len);
641                                    opts->m_len += len;
642                            }
643                    }
644                    /* Terminate & pad, if necessary */
645                    cnt = opts->m_len % 4;
646                    if (cnt) {
647                            for (; cnt < 4; cnt++) {
648                                    *(mtod(opts, caddr_t) + opts->m_len) =
649                                        IPOPT_EOL;
650                                    opts->m_len++;
651                            }
652                    }
653#ifdef ICMPPRINTFS
654                    if (icmpprintfs)
655                            printf("%d\n", opts->m_len);
656#endif
657                }
658                /*
659                 * Now strip out original options by copying rest of first
660                 * mbuf's data back, and adjust the IP length.
661                 */
662                ip->ip_len -= optlen;
663#ifdef _IP_VHL
664                ip->ip_vhl = IP_VHL_BORING;
665#else
666                ip->ip_v = IPVERSION;
667                ip->ip_hl = 5;
668#endif
669                m->m_len -= optlen;
670                if (m->m_flags & M_PKTHDR)
671                        m->m_pkthdr.len -= optlen;
672                optlen += sizeof(struct ip);
673                bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
674                         (unsigned)(m->m_len - sizeof(struct ip)));
675        }
676        m->m_flags &= ~(M_BCAST|M_MCAST);
677        icmp_send(m, opts);
678done:
679        if (opts)
680                (void)m_free(opts);
681}
682
683/*
684 * Send an icmp packet back to the ip level,
685 * after supplying a checksum.
686 */
687static void
688icmp_send(struct mbuf *m, struct mbuf *opts)
689{
690        register struct ip *ip = mtod(m, struct ip *);
691        register int hlen;
692        register struct icmp *icp;
693        struct route ro;
694
695#ifdef _IP_VHL
696        hlen = IP_VHL_HL(ip->ip_vhl) << 2;
697#else
698        hlen = ip->ip_hl << 2;
699#endif
700        m->m_data += hlen;
701        m->m_len -= hlen;
702        icp = mtod(m, struct icmp *);
703        icp->icmp_cksum = 0;
704        icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
705        m->m_data -= hlen;
706        m->m_len += hlen;
707#ifdef ICMPPRINTFS
708        if (icmpprintfs) {
709                char buf[4 * sizeof "123"];
710                strcpy(buf, inet_ntoa(ip->ip_dst));
711                printf("icmp_send dst %s src %s\n",
712                       buf, inet_ntoa(ip->ip_src));
713        }
714#endif
715        bzero(&ro, sizeof ro);
716        (void) ip_output(m, opts, &ro, 0, NULL);
717        if (ro.ro_rt)
718                RTFREE(ro.ro_rt);
719}
720
721/*
722 * Return milliseconds since 00:00 GMT in network format.
723 */
724uint32_t
725iptime(void)
726{
727        struct timeval atv;
728        u_long t;
729
730        microtime(&atv);
731        t = (atv.tv_sec % (24L*60L*60L)) * 1000L + atv.tv_usec / 1000L;
732        return (htonl(t));
733}
734
735/*
736 * Return the next larger or smaller MTU plateau (table from RFC 1191)
737 * given current value MTU.  If DIR is less than zero, a larger plateau
738 * is returned; otherwise, a smaller value is returned.
739 */
740int
741ip_next_mtu(int mtu, int dir)
742{
743        static int mtutab[] = {
744                65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
745                296, 68, 0
746        };
747        int i;
748
749        for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
750                if (mtu >= mtutab[i])
751                        break;
752        }
753
754        if (dir < 0) {
755                if (i == 0) {
756                        return 0;
757                } else {
758                        return mtutab[i - 1];
759                }
760        } else {
761                if (mtutab[i] == 0) {
762                        return 0;
763                } else if(mtu > mtutab[i]) {
764                        return mtutab[i];
765                } else {
766                        return mtutab[i + 1];
767                }
768        }
769}
Note: See TracBrowser for help on using the repository browser.