The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_icmp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: dcc3c0b6549052c7afdbf84cd768eadb


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.