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

Cache object: c4a8cfe40c9ecb230ebcf9a44f6e24e3


[ 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.