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 /*      $NetBSD: ip_icmp.c,v 1.120 2008/06/18 09:06:28 yamt Exp $       */
    2 
    3 /*
    4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the project nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
   34  * All rights reserved.
   35  *
   36  * This code is derived from software contributed to The NetBSD Foundation
   37  * by Public Access Networks Corporation ("Panix").  It was developed under
   38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
   39  *
   40  * This code is derived from software contributed to The NetBSD Foundation
   41  * by Jason R. Thorpe of Zembu Labs, Inc.
   42  *
   43  * Redistribution and use in source and binary forms, with or without
   44  * modification, are permitted provided that the following conditions
   45  * are met:
   46  * 1. Redistributions of source code must retain the above copyright
   47  *    notice, this list of conditions and the following disclaimer.
   48  * 2. Redistributions in binary form must reproduce the above copyright
   49  *    notice, this list of conditions and the following disclaimer in the
   50  *    documentation and/or other materials provided with the distribution.
   51  *
   52  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   53  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   62  * POSSIBILITY OF SUCH DAMAGE.
   63  */
   64 
   65 /*
   66  * Copyright (c) 1982, 1986, 1988, 1993
   67  *      The Regents of the University of California.  All rights reserved.
   68  *
   69  * Redistribution and use in source and binary forms, with or without
   70  * modification, are permitted provided that the following conditions
   71  * are met:
   72  * 1. Redistributions of source code must retain the above copyright
   73  *    notice, this list of conditions and the following disclaimer.
   74  * 2. Redistributions in binary form must reproduce the above copyright
   75  *    notice, this list of conditions and the following disclaimer in the
   76  *    documentation and/or other materials provided with the distribution.
   77  * 3. Neither the name of the University nor the names of its contributors
   78  *    may be used to endorse or promote products derived from this software
   79  *    without specific prior written permission.
   80  *
   81  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   82  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   83  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   84  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   85  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   86  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   87  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   88  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   89  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   90  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   91  * SUCH DAMAGE.
   92  *
   93  *      @(#)ip_icmp.c   8.2 (Berkeley) 1/4/94
   94  */
   95 
   96 #include <sys/cdefs.h>
   97 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.120 2008/06/18 09:06:28 yamt Exp $");
   98 
   99 #include "opt_ipsec.h"
  100 
  101 #include <sys/param.h>
  102 #include <sys/systm.h>
  103 #include <sys/malloc.h>
  104 #include <sys/mbuf.h>
  105 #include <sys/protosw.h>
  106 #include <sys/socket.h>
  107 #include <sys/time.h>
  108 #include <sys/kernel.h>
  109 #include <sys/syslog.h>
  110 #include <sys/sysctl.h>
  111 
  112 #include <net/if.h>
  113 #include <net/route.h>
  114 
  115 #include <netinet/in.h>
  116 #include <netinet/in_systm.h>
  117 #include <netinet/in_var.h>
  118 #include <netinet/ip.h>
  119 #include <netinet/ip_icmp.h>
  120 #include <netinet/ip_var.h>
  121 #include <netinet/in_pcb.h>
  122 #include <netinet/in_proto.h>
  123 #include <netinet/icmp_var.h>
  124 #include <netinet/icmp_private.h>
  125 
  126 #ifdef IPSEC
  127 #include <netinet6/ipsec.h>
  128 #include <netkey/key.h>
  129 #endif
  130 
  131 #ifdef FAST_IPSEC
  132 #include <netipsec/ipsec.h>
  133 #include <netipsec/key.h>
  134 #endif  /* FAST_IPSEC*/
  135 
  136 #include <machine/stdarg.h>
  137 
  138 /*
  139  * ICMP routines: error generation, receive packet processing, and
  140  * routines to turnaround packets back to the originator, and
  141  * host table maintenance routines.
  142  */
  143 
  144 int     icmpmaskrepl = 0;
  145 #ifdef ICMPPRINTFS
  146 int     icmpprintfs = 0;
  147 #endif
  148 int     icmpreturndatabytes = 8;
  149 
  150 percpu_t *icmpstat_percpu;
  151 
  152 /*
  153  * List of callbacks to notify when Path MTU changes are made.
  154  */
  155 struct icmp_mtudisc_callback {
  156         LIST_ENTRY(icmp_mtudisc_callback) mc_list;
  157         void (*mc_func)(struct in_addr);
  158 };
  159 
  160 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks =
  161     LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks);
  162 
  163 #if 0
  164 static u_int    ip_next_mtu(u_int, int);
  165 #else
  166 /*static*/ u_int        ip_next_mtu(u_int, int);
  167 #endif
  168 
  169 extern int icmperrppslim;
  170 static int icmperrpps_count = 0;
  171 static struct timeval icmperrppslim_last;
  172 static int icmp_rediraccept = 1;
  173 static int icmp_redirtimeout = 600;
  174 static struct rttimer_queue *icmp_redirect_timeout_q = NULL;
  175 
  176 static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *);
  177 static void icmp_redirect_timeout(struct rtentry *, struct rttimer *);
  178 
  179 static int icmp_ratelimit(const struct in_addr *, const int, const int);
  180 
  181 
  182 void
  183 icmp_init(void)
  184 {
  185         /*
  186          * This is only useful if the user initializes redirtimeout to
  187          * something other than zero.
  188          */
  189         if (icmp_redirtimeout != 0) {
  190                 icmp_redirect_timeout_q =
  191                         rt_timer_queue_create(icmp_redirtimeout);
  192         }
  193 
  194         icmpstat_percpu = percpu_alloc(sizeof(uint64_t) * ICMP_NSTATS);
  195 }
  196 
  197 /*
  198  * Register a Path MTU Discovery callback.
  199  */
  200 void
  201 icmp_mtudisc_callback_register(void (*func)(struct in_addr))
  202 {
  203         struct icmp_mtudisc_callback *mc;
  204 
  205         for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
  206              mc = LIST_NEXT(mc, mc_list)) {
  207                 if (mc->mc_func == func)
  208                         return;
  209         }
  210 
  211         mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT);
  212         if (mc == NULL)
  213                 panic("icmp_mtudisc_callback_register");
  214 
  215         mc->mc_func = func;
  216         LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list);
  217 }
  218 
  219 /*
  220  * Generate an error packet of type error
  221  * in response to bad packet ip.
  222  */
  223 void
  224 icmp_error(struct mbuf *n, int type, int code, n_long dest,
  225     int destmtu)
  226 {
  227         struct ip *oip = mtod(n, struct ip *), *nip;
  228         unsigned oiplen = oip->ip_hl << 2;
  229         struct icmp *icp;
  230         struct mbuf *m;
  231         struct m_tag *mtag;
  232         unsigned icmplen, mblen;
  233 
  234 #ifdef ICMPPRINTFS
  235         if (icmpprintfs)
  236                 printf("icmp_error(%p, type:%d, code:%d)\n", oip, type, code);
  237 #endif
  238         if (type != ICMP_REDIRECT)
  239                 ICMP_STATINC(ICMP_STAT_ERROR);
  240         /*
  241          * Don't send error if the original packet was encrypted.
  242          * Don't send error if not the first fragment of message.
  243          * Don't error if the old packet protocol was ICMP
  244          * error message, only known informational types.
  245          */
  246         if (n->m_flags & M_DECRYPTED)
  247                 goto freeit;
  248         if (oip->ip_off &~ htons(IP_MF|IP_DF))
  249                 goto freeit;
  250         if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
  251           n->m_len >= oiplen + ICMP_MINLEN &&
  252           !ICMP_INFOTYPE(((struct icmp *)((char *)oip + oiplen))->icmp_type)) {
  253                 ICMP_STATINC(ICMP_STAT_OLDICMP);
  254                 goto freeit;
  255         }
  256         /* Don't send error in response to a multicast or broadcast packet */
  257         if (n->m_flags & (M_BCAST|M_MCAST))
  258                 goto freeit;
  259 
  260         /*
  261          * First, do a rate limitation check.
  262          */
  263         if (icmp_ratelimit(&oip->ip_src, type, code)) {
  264                 /* XXX stat */
  265                 goto freeit;
  266         }
  267 
  268         /*
  269          * Now, formulate icmp message
  270          */
  271         icmplen = oiplen + min(icmpreturndatabytes,
  272             ntohs(oip->ip_len) - oiplen);
  273         /*
  274          * Defend against mbuf chains shorter than oip->ip_len - oiplen:
  275          */
  276         mblen = 0;
  277         for (m = n; m && (mblen < icmplen); m = m->m_next)
  278                 mblen += m->m_len;
  279         icmplen = min(mblen, icmplen);
  280 
  281         /*
  282          * As we are not required to return everything we have,
  283          * we return whatever we can return at ease.
  284          *
  285          * Note that ICMP datagrams longer than 576 octets are out of spec
  286          * according to RFC1812; the limit on icmpreturndatabytes below in
  287          * icmp_sysctl will keep things below that limit.
  288          */
  289 
  290         KASSERT(ICMP_MINLEN <= MCLBYTES);
  291 
  292         if (icmplen + ICMP_MINLEN > MCLBYTES)
  293                 icmplen = MCLBYTES - ICMP_MINLEN;
  294 
  295         m = m_gethdr(M_DONTWAIT, MT_HEADER);
  296         if (m && (icmplen + ICMP_MINLEN > MHLEN)) {
  297                 MCLGET(m, M_DONTWAIT);
  298                 if ((m->m_flags & M_EXT) == 0) {
  299                         m_freem(m);
  300                         m = NULL;
  301                 }
  302         }
  303         if (m == NULL)
  304                 goto freeit;
  305         MCLAIM(m, n->m_owner);
  306         m->m_len = icmplen + ICMP_MINLEN;
  307         if ((m->m_flags & M_EXT) == 0)
  308                 MH_ALIGN(m, m->m_len);
  309         icp = mtod(m, struct icmp *);
  310         if ((u_int)type > ICMP_MAXTYPE)
  311                 panic("icmp_error");
  312         ICMP_STATINC(ICMP_STAT_OUTHIST + type);
  313         icp->icmp_type = type;
  314         if (type == ICMP_REDIRECT)
  315                 icp->icmp_gwaddr.s_addr = dest;
  316         else {
  317                 icp->icmp_void = 0;
  318                 /*
  319                  * The following assignments assume an overlay with the
  320                  * zeroed icmp_void field.
  321                  */
  322                 if (type == ICMP_PARAMPROB) {
  323                         icp->icmp_pptr = code;
  324                         code = 0;
  325                 } else if (type == ICMP_UNREACH &&
  326                     code == ICMP_UNREACH_NEEDFRAG && destmtu)
  327                         icp->icmp_nextmtu = htons(destmtu);
  328         }
  329 
  330         icp->icmp_code = code;
  331         m_copydata(n, 0, icmplen, (void *)&icp->icmp_ip);
  332 
  333         /*
  334          * Now, copy old ip header (without options)
  335          * in front of icmp message.
  336          */
  337         if (m->m_data - sizeof(struct ip) < m->m_pktdat)
  338                 panic("icmp len");
  339         m->m_data -= sizeof(struct ip);
  340         m->m_len += sizeof(struct ip);
  341         m->m_pkthdr.len = m->m_len;
  342         m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
  343         nip = mtod(m, struct ip *);
  344         /* ip_v set in ip_output */
  345         nip->ip_hl = sizeof(struct ip) >> 2;
  346         nip->ip_tos = 0;
  347         nip->ip_len = htons(m->m_len);
  348         /* ip_id set in ip_output */
  349         nip->ip_off = htons(0);
  350         /* ip_ttl set in icmp_reflect */
  351         nip->ip_p = IPPROTO_ICMP;
  352         nip->ip_src = oip->ip_src;
  353         nip->ip_dst = oip->ip_dst;
  354         /* move PF m_tag to new packet, if it exists */
  355         mtag = m_tag_find(n, PACKET_TAG_PF, NULL);
  356         if (mtag != NULL) {
  357                 m_tag_unlink(n, mtag);
  358                 m_tag_prepend(m, mtag);
  359         }
  360         icmp_reflect(m);
  361 
  362 freeit:
  363         m_freem(n);
  364 }
  365 
  366 struct sockaddr_in icmpsrc = {
  367         .sin_len = sizeof (struct sockaddr_in),
  368         .sin_family = AF_INET,
  369 };
  370 static struct sockaddr_in icmpdst = {
  371         .sin_len = sizeof (struct sockaddr_in),
  372         .sin_family = AF_INET,
  373 };
  374 static struct sockaddr_in icmpgw = {
  375         .sin_len = sizeof (struct sockaddr_in),
  376         .sin_family = AF_INET,
  377 };
  378 struct sockaddr_in icmpmask = { 
  379         .sin_len = 8,
  380         .sin_family = 0,
  381 };
  382 
  383 /*
  384  * Process a received ICMP message.
  385  */
  386 void
  387 icmp_input(struct mbuf *m, ...)
  388 {
  389         int proto;
  390         struct icmp *icp;
  391         struct ip *ip = mtod(m, struct ip *);
  392         int icmplen;
  393         int i;
  394         struct in_ifaddr *ia;
  395         void *(*ctlfunc)(int, const struct sockaddr *, void *);
  396         int code;
  397         int hlen;
  398         va_list ap;
  399         struct rtentry *rt;
  400 
  401         va_start(ap, m);
  402         hlen = va_arg(ap, int);
  403         proto = va_arg(ap, int);
  404         va_end(ap);
  405 
  406         /*
  407          * Locate icmp structure in mbuf, and check
  408          * that not corrupted and of at least minimum length.
  409          */
  410         icmplen = ntohs(ip->ip_len) - hlen;
  411 #ifdef ICMPPRINTFS
  412         if (icmpprintfs) {
  413                 printf("icmp_input from `%s' to ", inet_ntoa(ip->ip_src));
  414                 printf("`%s', len %d\n", inet_ntoa(ip->ip_dst), icmplen);
  415         }
  416 #endif
  417         if (icmplen < ICMP_MINLEN) {
  418                 ICMP_STATINC(ICMP_STAT_TOOSHORT);
  419                 goto freeit;
  420         }
  421         i = hlen + min(icmplen, ICMP_ADVLENMIN);
  422         if ((m->m_len < i || M_READONLY(m)) && (m = m_pullup(m, i)) == 0) {
  423                 ICMP_STATINC(ICMP_STAT_TOOSHORT);
  424                 return;
  425         }
  426         ip = mtod(m, struct ip *);
  427         m->m_len -= hlen;
  428         m->m_data += hlen;
  429         icp = mtod(m, struct icmp *);
  430         /* Don't need to assert alignment, here. */
  431         if (in_cksum(m, icmplen)) {
  432                 ICMP_STATINC(ICMP_STAT_CHECKSUM);
  433                 goto freeit;
  434         }
  435         m->m_len += hlen;
  436         m->m_data -= hlen;
  437 
  438 #ifdef ICMPPRINTFS
  439         /*
  440          * Message type specific processing.
  441          */
  442         if (icmpprintfs)
  443                 printf("icmp_input(type:%d, code:%d)\n", icp->icmp_type,
  444                     icp->icmp_code);
  445 #endif
  446         if (icp->icmp_type > ICMP_MAXTYPE)
  447                 goto raw;
  448         ICMP_STATINC(ICMP_STAT_INHIST + icp->icmp_type);
  449         code = icp->icmp_code;
  450         switch (icp->icmp_type) {
  451 
  452         case ICMP_UNREACH:
  453                 switch (code) {
  454                         case ICMP_UNREACH_NET:
  455                                 code = PRC_UNREACH_NET;
  456                                 break;
  457 
  458                         case ICMP_UNREACH_HOST:
  459                                 code = PRC_UNREACH_HOST;
  460                                 break;
  461 
  462                         case ICMP_UNREACH_PROTOCOL:
  463                                 code = PRC_UNREACH_PROTOCOL;
  464                                 break;
  465 
  466                         case ICMP_UNREACH_PORT:
  467                                 code = PRC_UNREACH_PORT;
  468                                 break;
  469 
  470                         case ICMP_UNREACH_SRCFAIL:
  471                                 code = PRC_UNREACH_SRCFAIL;
  472                                 break;
  473 
  474                         case ICMP_UNREACH_NEEDFRAG:
  475                                 code = PRC_MSGSIZE;
  476                                 break;
  477 
  478                         case ICMP_UNREACH_NET_UNKNOWN:
  479                         case ICMP_UNREACH_NET_PROHIB:
  480                         case ICMP_UNREACH_TOSNET:
  481                                 code = PRC_UNREACH_NET;
  482                                 break;
  483 
  484                         case ICMP_UNREACH_HOST_UNKNOWN:
  485                         case ICMP_UNREACH_ISOLATED:
  486                         case ICMP_UNREACH_HOST_PROHIB:
  487                         case ICMP_UNREACH_TOSHOST:
  488                                 code = PRC_UNREACH_HOST;
  489                                 break;
  490 
  491                         default:
  492                                 goto badcode;
  493                 }
  494                 goto deliver;
  495 
  496         case ICMP_TIMXCEED:
  497                 if (code > 1)
  498                         goto badcode;
  499                 code += PRC_TIMXCEED_INTRANS;
  500                 goto deliver;
  501 
  502         case ICMP_PARAMPROB:
  503                 if (code > 1)
  504                         goto badcode;
  505                 code = PRC_PARAMPROB;
  506                 goto deliver;
  507 
  508         case ICMP_SOURCEQUENCH:
  509                 if (code)
  510                         goto badcode;
  511                 code = PRC_QUENCH;
  512                 goto deliver;
  513 
  514         deliver:
  515                 /*
  516                  * Problem with datagram; advise higher level routines.
  517                  */
  518                 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
  519                     icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
  520                         ICMP_STATINC(ICMP_STAT_BADLEN);
  521                         goto freeit;
  522                 }
  523                 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr))
  524                         goto badcode;
  525 #ifdef ICMPPRINTFS
  526                 if (icmpprintfs)
  527                         printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
  528 #endif
  529                 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
  530                 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
  531                 if (ctlfunc)
  532                         (void) (*ctlfunc)(code, sintosa(&icmpsrc),
  533                             &icp->icmp_ip);
  534                 break;
  535 
  536         badcode:
  537                 ICMP_STATINC(ICMP_STAT_BADCODE);
  538                 break;
  539 
  540         case ICMP_ECHO:
  541                 icp->icmp_type = ICMP_ECHOREPLY;
  542                 goto reflect;
  543 
  544         case ICMP_TSTAMP:
  545                 if (icmplen < ICMP_TSLEN) {
  546                         ICMP_STATINC(ICMP_STAT_BADLEN);
  547                         break;
  548                 }
  549                 icp->icmp_type = ICMP_TSTAMPREPLY;
  550                 icp->icmp_rtime = iptime();
  551                 icp->icmp_ttime = icp->icmp_rtime;      /* bogus, do later! */
  552                 goto reflect;
  553 
  554         case ICMP_MASKREQ:
  555                 if (icmpmaskrepl == 0)
  556                         break;
  557                 /*
  558                  * We are not able to respond with all ones broadcast
  559                  * unless we receive it over a point-to-point interface.
  560                  */
  561                 if (icmplen < ICMP_MASKLEN) {
  562                         ICMP_STATINC(ICMP_STAT_BADLEN);
  563                         break;
  564                 }
  565                 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
  566                     in_nullhost(ip->ip_dst))
  567                         icmpdst.sin_addr = ip->ip_src;
  568                 else
  569                         icmpdst.sin_addr = ip->ip_dst;
  570                 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst),
  571                     m->m_pkthdr.rcvif));
  572                 if (ia == 0)
  573                         break;
  574                 icp->icmp_type = ICMP_MASKREPLY;
  575                 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
  576                 if (in_nullhost(ip->ip_src)) {
  577                         if (ia->ia_ifp->if_flags & IFF_BROADCAST)
  578                                 ip->ip_src = ia->ia_broadaddr.sin_addr;
  579                         else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
  580                                 ip->ip_src = ia->ia_dstaddr.sin_addr;
  581                 }
  582 reflect:
  583                 {
  584                         uint64_t *icps = percpu_getref(icmpstat_percpu);
  585                         icps[ICMP_STAT_REFLECT]++;
  586                         icps[ICMP_STAT_OUTHIST + icp->icmp_type]++;
  587                         percpu_putref(icmpstat_percpu);
  588                 }
  589                 icmp_reflect(m);
  590                 return;
  591 
  592         case ICMP_REDIRECT:
  593                 if (code > 3)
  594                         goto badcode;
  595                 if (icmp_rediraccept == 0)
  596                         goto freeit;
  597                 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
  598                     icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
  599                         ICMP_STATINC(ICMP_STAT_BADLEN);
  600                         break;
  601                 }
  602                 /*
  603                  * Short circuit routing redirects to force
  604                  * immediate change in the kernel's routing
  605                  * tables.  The message is also handed to anyone
  606                  * listening on a raw socket (e.g. the routing
  607                  * daemon for use in updating its tables).
  608                  */
  609                 icmpgw.sin_addr = ip->ip_src;
  610                 icmpdst.sin_addr = icp->icmp_gwaddr;
  611 #ifdef  ICMPPRINTFS
  612                 if (icmpprintfs) {
  613                         printf("redirect dst `%s' to `%s'\n",
  614                             inet_ntoa(icp->icmp_ip.ip_dst),
  615                             inet_ntoa(icp->icmp_gwaddr));
  616                 }
  617 #endif
  618                 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
  619                 rt = NULL;
  620                 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst),
  621                     NULL, RTF_GATEWAY | RTF_HOST, sintosa(&icmpgw), &rt);
  622                 if (rt != NULL && icmp_redirtimeout != 0) {
  623                         i = rt_timer_add(rt, icmp_redirect_timeout,
  624                                          icmp_redirect_timeout_q);
  625                         if (i)
  626                                 log(LOG_ERR, "ICMP:  redirect failed to "
  627                                     "register timeout for route to %x, "
  628                                     "code %d\n",
  629                                     icp->icmp_ip.ip_dst.s_addr, i);
  630                 }
  631                 if (rt != NULL)
  632                         rtfree(rt);
  633 
  634                 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc));
  635 #if defined(IPSEC) || defined(FAST_IPSEC)
  636                 key_sa_routechange((struct sockaddr *)&icmpsrc);
  637 #endif
  638                 break;
  639 
  640         /*
  641          * No kernel processing for the following;
  642          * just fall through to send to raw listener.
  643          */
  644         case ICMP_ECHOREPLY:
  645         case ICMP_ROUTERADVERT:
  646         case ICMP_ROUTERSOLICIT:
  647         case ICMP_TSTAMPREPLY:
  648         case ICMP_IREQREPLY:
  649         case ICMP_MASKREPLY:
  650         default:
  651                 break;
  652         }
  653 
  654 raw:
  655         rip_input(m, hlen, proto);
  656         return;
  657 
  658 freeit:
  659         m_freem(m);
  660         return;
  661 }
  662 
  663 /*
  664  * Reflect the ip packet back to the source
  665  */
  666 void
  667 icmp_reflect(struct mbuf *m)
  668 {
  669         struct ip *ip = mtod(m, struct ip *);
  670         struct in_ifaddr *ia;
  671         struct ifaddr *ifa;
  672         struct sockaddr_in *sin = 0;
  673         struct in_addr t;
  674         struct mbuf *opts = 0;
  675         int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
  676 
  677         if (!in_canforward(ip->ip_src) &&
  678             ((ip->ip_src.s_addr & IN_CLASSA_NET) !=
  679              htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
  680                 m_freem(m);     /* Bad return address */
  681                 goto done;      /* ip_output() will check for broadcast */
  682         }
  683         t = ip->ip_dst;
  684         ip->ip_dst = ip->ip_src;
  685         /*
  686          * If the incoming packet was addressed directly to us, use
  687          * dst as the src for the reply.  Otherwise (broadcast or
  688          * anonymous), use an address which corresponds to the
  689          * incoming interface, with a preference for the address which
  690          * corresponds to the route to the destination of the ICMP.
  691          */
  692 
  693         /* Look for packet addressed to us */
  694         INADDR_TO_IA(t, ia);
  695 
  696         /* look for packet sent to broadcast address */
  697         if (ia == NULL && m->m_pkthdr.rcvif &&
  698             (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) {
  699                 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
  700                         if (ifa->ifa_addr->sa_family != AF_INET)
  701                                 continue;
  702                         if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) {
  703                                 ia = ifatoia(ifa);
  704                                 break;
  705                         }
  706                 }
  707         }
  708 
  709         if (ia)
  710                 sin = &ia->ia_addr;
  711 
  712         icmpdst.sin_addr = t;
  713 
  714         /*
  715          * if the packet is addressed somewhere else, compute the
  716          * source address for packets routed back to the source, and
  717          * use that, if it's an address on the interface which
  718          * received the packet
  719          */
  720         if (sin == NULL && m->m_pkthdr.rcvif) {
  721                 struct sockaddr_in sin_dst;
  722                 struct route icmproute;
  723                 int errornum;
  724 
  725                 sockaddr_in_init(&sin_dst, &ip->ip_dst, 0);
  726                 memset(&icmproute, 0, sizeof(icmproute));
  727                 errornum = 0;
  728                 sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum);
  729                 /* errornum is never used */
  730                 rtcache_free(&icmproute);
  731                 /* check to make sure sin is a source address on rcvif */
  732                 if (sin) {
  733                         t = sin->sin_addr;
  734                         sin = NULL;
  735                         INADDR_TO_IA(t, ia);
  736                         while (ia) {
  737                                 if (ia->ia_ifp == m->m_pkthdr.rcvif) {
  738                                         sin = &ia->ia_addr;
  739                                         break;
  740                                 }
  741                                 NEXT_IA_WITH_SAME_ADDR(ia);
  742                         }
  743                 }
  744         }
  745 
  746         /*
  747          * if it was not addressed to us, but the route doesn't go out
  748          * the source interface, pick an address on the source
  749          * interface.  This can happen when routing is asymmetric, or
  750          * when the incoming packet was encapsulated
  751          */
  752         if (sin == NULL && m->m_pkthdr.rcvif) {
  753                 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
  754                         if (ifa->ifa_addr->sa_family != AF_INET)
  755                                 continue;
  756                         sin = &(ifatoia(ifa)->ia_addr);
  757                         break;
  758                 }
  759         }
  760 
  761         /*
  762          * The following happens if the packet was not addressed to us,
  763          * and was received on an interface with no IP address:
  764          * We find the first AF_INET address on the first non-loopback
  765          * interface.
  766          */
  767         if (sin == NULL)
  768                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
  769                         if (ia->ia_ifp->if_flags & IFF_LOOPBACK)
  770                                 continue;
  771                         sin = &ia->ia_addr;
  772                         break;
  773                 }
  774 
  775         /*
  776          * If we still didn't find an address, punt.  We could have an
  777          * interface up (and receiving packets) with no address.
  778          */
  779         if (sin == NULL) {
  780                 m_freem(m);
  781                 goto done;
  782         }
  783 
  784         ip->ip_src = sin->sin_addr;
  785         ip->ip_ttl = MAXTTL;
  786 
  787         if (optlen > 0) {
  788                 u_char *cp;
  789                 int opt, cnt;
  790                 u_int len;
  791 
  792                 /*
  793                  * Retrieve any source routing from the incoming packet;
  794                  * add on any record-route or timestamp options.
  795                  */
  796                 cp = (u_char *) (ip + 1);
  797                 if ((opts = ip_srcroute()) == 0 &&
  798                     (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
  799                         MCLAIM(opts, m->m_owner);
  800                         opts->m_len = sizeof(struct in_addr);
  801                         *mtod(opts, struct in_addr *) = zeroin_addr;
  802                 }
  803                 if (opts) {
  804 #ifdef ICMPPRINTFS
  805                     if (icmpprintfs)
  806                             printf("icmp_reflect optlen %d rt %d => ",
  807                                 optlen, opts->m_len);
  808 #endif
  809                     for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
  810                             opt = cp[IPOPT_OPTVAL];
  811                             if (opt == IPOPT_EOL)
  812                                     break;
  813                             if (opt == IPOPT_NOP)
  814                                     len = 1;
  815                             else {
  816                                     if (cnt < IPOPT_OLEN + sizeof(*cp))
  817                                             break;
  818                                     len = cp[IPOPT_OLEN];
  819                                     if (len < IPOPT_OLEN + sizeof(*cp) ||
  820                                         len > cnt)
  821                                             break;
  822                             }
  823                             /*
  824                              * Should check for overflow, but it "can't happen"
  825                              */
  826                             if (opt == IPOPT_RR || opt == IPOPT_TS ||
  827                                 opt == IPOPT_SECURITY) {
  828                                     memmove(mtod(opts, char *) + opts->m_len,
  829                                         cp, len);
  830                                     opts->m_len += len;
  831                             }
  832                     }
  833                     /* Terminate & pad, if necessary */
  834                     if ((cnt = opts->m_len % 4) != 0) {
  835                             for (; cnt < 4; cnt++) {
  836                                     *(mtod(opts, char *) + opts->m_len) =
  837                                         IPOPT_EOL;
  838                                     opts->m_len++;
  839                             }
  840                     }
  841 #ifdef ICMPPRINTFS
  842                     if (icmpprintfs)
  843                             printf("%d\n", opts->m_len);
  844 #endif
  845                 }
  846                 /*
  847                  * Now strip out original options by copying rest of first
  848                  * mbuf's data back, and adjust the IP length.
  849                  */
  850                 ip->ip_len = htons(ntohs(ip->ip_len) - optlen);
  851                 ip->ip_hl = sizeof(struct ip) >> 2;
  852                 m->m_len -= optlen;
  853                 if (m->m_flags & M_PKTHDR)
  854                         m->m_pkthdr.len -= optlen;
  855                 optlen += sizeof(struct ip);
  856                 memmove(ip + 1, (char *)ip + optlen,
  857                     (unsigned)(m->m_len - sizeof(struct ip)));
  858         }
  859         m_tag_delete_nonpersistent(m);
  860         m->m_flags &= ~(M_BCAST|M_MCAST);
  861 
  862         /*
  863          * Clear any in-bound checksum flags for this packet.
  864          */
  865         if (m->m_flags & M_PKTHDR)
  866                 m->m_pkthdr.csum_flags = 0;
  867 
  868         icmp_send(m, opts);
  869 done:
  870         if (opts)
  871                 (void)m_free(opts);
  872 }
  873 
  874 /*
  875  * Send an icmp packet back to the ip level,
  876  * after supplying a checksum.
  877  */
  878 void
  879 icmp_send(struct mbuf *m, struct mbuf *opts)
  880 {
  881         struct ip *ip = mtod(m, struct ip *);
  882         int hlen;
  883         struct icmp *icp;
  884 
  885         hlen = ip->ip_hl << 2;
  886         m->m_data += hlen;
  887         m->m_len -= hlen;
  888         icp = mtod(m, struct icmp *);
  889         icp->icmp_cksum = 0;
  890         icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
  891         m->m_data -= hlen;
  892         m->m_len += hlen;
  893 #ifdef ICMPPRINTFS
  894         if (icmpprintfs) {
  895                 printf("icmp_send to destination `%s' from `%s'\n",
  896                     inet_ntoa(ip->ip_dst), inet_ntoa(ip->ip_src));
  897         }
  898 #endif
  899         (void)ip_output(m, opts, NULL, 0, NULL, NULL);
  900 }
  901 
  902 n_time
  903 iptime(void)
  904 {
  905         struct timeval atv;
  906         u_long t;
  907 
  908         microtime(&atv);
  909         t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
  910         return (htonl(t));
  911 }
  912 
  913 /*
  914  * sysctl helper routine for net.inet.icmp.returndatabytes.  ensures
  915  * that the new value is in the correct range.
  916  */
  917 static int
  918 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS)
  919 {
  920         int error, t;
  921         struct sysctlnode node;
  922 
  923         node = *rnode;
  924         node.sysctl_data = &t;
  925         t = icmpreturndatabytes;
  926         error = sysctl_lookup(SYSCTLFN_CALL(&node));
  927         if (error || newp == NULL)
  928                 return (error);
  929 
  930         if (t < 8 || t > 512)
  931                 return (EINVAL);
  932         icmpreturndatabytes = t;
  933 
  934         return (0);
  935 }
  936 
  937 /*
  938  * sysctl helper routine for net.inet.icmp.redirtimeout.  ensures that
  939  * the given value is not less than zero and then resets the timeout
  940  * queue.
  941  */
  942 static int
  943 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS)
  944 {
  945         int error, tmp;
  946         struct sysctlnode node;
  947 
  948         node = *rnode;
  949         node.sysctl_data = &tmp;
  950         tmp = icmp_redirtimeout;
  951         error = sysctl_lookup(SYSCTLFN_CALL(&node));
  952         if (error || newp == NULL)
  953                 return (error);
  954         if (tmp < 0)
  955                 return (EINVAL);
  956         icmp_redirtimeout = tmp;
  957 
  958         /*
  959          * was it a *defined* side-effect that anyone even *reading*
  960          * this value causes these things to happen?
  961          */
  962         if (icmp_redirect_timeout_q != NULL) {
  963                 if (icmp_redirtimeout == 0) {
  964                         rt_timer_queue_destroy(icmp_redirect_timeout_q,
  965                             true);
  966                         icmp_redirect_timeout_q = NULL;
  967                 } else {
  968                         rt_timer_queue_change(icmp_redirect_timeout_q,
  969                             icmp_redirtimeout);
  970                 }
  971         } else if (icmp_redirtimeout > 0) {
  972                 icmp_redirect_timeout_q =
  973                     rt_timer_queue_create(icmp_redirtimeout);
  974         }
  975 
  976         return (0);
  977 }
  978 
  979 static int
  980 sysctl_net_inet_icmp_stats(SYSCTLFN_ARGS)
  981 {
  982 
  983         return (NETSTAT_SYSCTL(icmpstat_percpu, ICMP_NSTATS));
  984 }
  985 
  986 SYSCTL_SETUP(sysctl_net_inet_icmp_setup, "sysctl net.inet.icmp subtree setup")
  987 {
  988 
  989         sysctl_createv(clog, 0, NULL, NULL,
  990                        CTLFLAG_PERMANENT,
  991                        CTLTYPE_NODE, "net", NULL,
  992                        NULL, 0, NULL, 0,
  993                        CTL_NET, CTL_EOL);
  994         sysctl_createv(clog, 0, NULL, NULL,
  995                        CTLFLAG_PERMANENT,
  996                        CTLTYPE_NODE, "inet", NULL,
  997                        NULL, 0, NULL, 0,
  998                        CTL_NET, PF_INET, CTL_EOL);
  999         sysctl_createv(clog, 0, NULL, NULL,
 1000                        CTLFLAG_PERMANENT,
 1001                        CTLTYPE_NODE, "icmp",
 1002                        SYSCTL_DESCR("ICMPv4 related settings"),
 1003                        NULL, 0, NULL, 0,
 1004                        CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL);
 1005 
 1006         sysctl_createv(clog, 0, NULL, NULL,
 1007                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 1008                        CTLTYPE_INT, "maskrepl",
 1009                        SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"),
 1010                        NULL, 0, &icmpmaskrepl, 0,
 1011                        CTL_NET, PF_INET, IPPROTO_ICMP,
 1012                        ICMPCTL_MASKREPL, CTL_EOL);
 1013         sysctl_createv(clog, 0, NULL, NULL,
 1014                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 1015                        CTLTYPE_INT, "returndatabytes",
 1016                        SYSCTL_DESCR("Number of bytes to return in an ICMP "
 1017                                     "error message"),
 1018                        sysctl_net_inet_icmp_returndatabytes, 0,
 1019                        &icmpreturndatabytes, 0,
 1020                        CTL_NET, PF_INET, IPPROTO_ICMP,
 1021                        ICMPCTL_RETURNDATABYTES, CTL_EOL);
 1022         sysctl_createv(clog, 0, NULL, NULL,
 1023                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 1024                        CTLTYPE_INT, "errppslimit",
 1025                        SYSCTL_DESCR("Maximum number of outgoing ICMP error "
 1026                                     "messages per second"),
 1027                        NULL, 0, &icmperrppslim, 0,
 1028                        CTL_NET, PF_INET, IPPROTO_ICMP,
 1029                        ICMPCTL_ERRPPSLIMIT, CTL_EOL);
 1030         sysctl_createv(clog, 0, NULL, NULL,
 1031                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 1032                        CTLTYPE_INT, "rediraccept",
 1033                        SYSCTL_DESCR("Accept ICMP_REDIRECT messages"),
 1034                        NULL, 0, &icmp_rediraccept, 0,
 1035                        CTL_NET, PF_INET, IPPROTO_ICMP,
 1036                        ICMPCTL_REDIRACCEPT, CTL_EOL);
 1037         sysctl_createv(clog, 0, NULL, NULL,
 1038                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
 1039                        CTLTYPE_INT, "redirtimeout",
 1040                        SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated "
 1041                                     "routes"),
 1042                        sysctl_net_inet_icmp_redirtimeout, 0,
 1043                        &icmp_redirtimeout, 0,
 1044                        CTL_NET, PF_INET, IPPROTO_ICMP,
 1045                        ICMPCTL_REDIRTIMEOUT, CTL_EOL);
 1046         sysctl_createv(clog, 0, NULL, NULL,
 1047                        CTLFLAG_PERMANENT,
 1048                        CTLTYPE_STRUCT, "stats",
 1049                        SYSCTL_DESCR("ICMP statistics"), 
 1050                        sysctl_net_inet_icmp_stats, 0, NULL, 0,
 1051                        CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS,
 1052                        CTL_EOL);
 1053 }
 1054 
 1055 void
 1056 icmp_statinc(u_int stat)
 1057 {
 1058 
 1059         KASSERT(stat < ICMP_NSTATS);
 1060         ICMP_STATINC(stat);
 1061 }
 1062 
 1063 /* Table of common MTUs: */
 1064 
 1065 static const u_int mtu_table[] = {
 1066         65535, 65280, 32000, 17914, 9180, 8166,
 1067         4352, 2002, 1492, 1006, 508, 296, 68, 0
 1068 };
 1069 
 1070 void
 1071 icmp_mtudisc(struct icmp *icp, struct in_addr faddr)
 1072 {
 1073         struct icmp_mtudisc_callback *mc;
 1074         struct sockaddr *dst = sintosa(&icmpsrc);
 1075         struct rtentry *rt;
 1076         u_long mtu = ntohs(icp->icmp_nextmtu);  /* Why a long?  IPv6 */
 1077         int    error;
 1078 
 1079         rt = rtalloc1(dst, 1);
 1080         if (rt == 0)
 1081                 return;
 1082 
 1083         /* If we didn't get a host route, allocate one */
 1084 
 1085         if ((rt->rt_flags & RTF_HOST) == 0) {
 1086                 struct rtentry *nrt;
 1087 
 1088                 error = rtrequest((int) RTM_ADD, dst,
 1089                     (struct sockaddr *) rt->rt_gateway,
 1090                     (struct sockaddr *) 0,
 1091                     RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
 1092                 if (error) {
 1093                         rtfree(rt);
 1094                         return;
 1095                 }
 1096                 nrt->rt_rmx = rt->rt_rmx;
 1097                 rtfree(rt);
 1098                 rt = nrt;
 1099         }
 1100         error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q);
 1101         if (error) {
 1102                 rtfree(rt);
 1103                 return;
 1104         }
 1105 
 1106         if (mtu == 0) {
 1107                 int i = 0;
 1108 
 1109                 mtu = ntohs(icp->icmp_ip.ip_len);
 1110                 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */
 1111                 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0)
 1112                         mtu -= (icp->icmp_ip.ip_hl << 2);
 1113 
 1114                 /* If we still can't guess a value, try the route */
 1115 
 1116                 if (mtu == 0) {
 1117                         mtu = rt->rt_rmx.rmx_mtu;
 1118 
 1119                         /* If no route mtu, default to the interface mtu */
 1120 
 1121                         if (mtu == 0)
 1122                                 mtu = rt->rt_ifp->if_mtu;
 1123                 }
 1124 
 1125                 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++)
 1126                         if (mtu > mtu_table[i]) {
 1127                                 mtu = mtu_table[i];
 1128                                 break;
 1129                         }
 1130         }
 1131 
 1132         /*
 1133          * XXX:   RTV_MTU is overloaded, since the admin can set it
 1134          *        to turn off PMTU for a route, and the kernel can
 1135          *        set it to indicate a serious problem with PMTU
 1136          *        on a route.  We should be using a separate flag
 1137          *        for the kernel to indicate this.
 1138          */
 1139 
 1140         if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
 1141                 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu)
 1142                         rt->rt_rmx.rmx_locks |= RTV_MTU;
 1143                 else if (rt->rt_rmx.rmx_mtu > mtu ||
 1144                          rt->rt_rmx.rmx_mtu == 0) {
 1145                         ICMP_STATINC(ICMP_STAT_PMTUCHG);
 1146                         rt->rt_rmx.rmx_mtu = mtu;
 1147                 }
 1148         }
 1149 
 1150         if (rt)
 1151                 rtfree(rt);
 1152 
 1153         /*
 1154          * Notify protocols that the MTU for this destination
 1155          * has changed.
 1156          */
 1157         for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL;
 1158              mc = LIST_NEXT(mc, mc_list))
 1159                 (*mc->mc_func)(faddr);
 1160 }
 1161 
 1162 /*
 1163  * Return the next larger or smaller MTU plateau (table from RFC 1191)
 1164  * given current value MTU.  If DIR is less than zero, a larger plateau
 1165  * is returned; otherwise, a smaller value is returned.
 1166  */
 1167 u_int
 1168 ip_next_mtu(u_int mtu, int dir) /* XXX */
 1169 {
 1170         int i;
 1171 
 1172         for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) {
 1173                 if (mtu >= mtu_table[i])
 1174                         break;
 1175         }
 1176 
 1177         if (dir < 0) {
 1178                 if (i == 0) {
 1179                         return 0;
 1180                 } else {
 1181                         return mtu_table[i - 1];
 1182                 }
 1183         } else {
 1184                 if (mtu_table[i] == 0) {
 1185                         return 0;
 1186                 } else if (mtu > mtu_table[i]) {
 1187                         return mtu_table[i];
 1188                 } else {
 1189                         return mtu_table[i + 1];
 1190                 }
 1191         }
 1192 }
 1193 
 1194 static void
 1195 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r)
 1196 {
 1197         if (rt == NULL)
 1198                 panic("icmp_mtudisc_timeout:  bad route to timeout");
 1199         if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
 1200             (RTF_DYNAMIC | RTF_HOST)) {
 1201                 rtrequest((int) RTM_DELETE, rt_getkey(rt),
 1202                     rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
 1203         } else {
 1204                 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
 1205                         rt->rt_rmx.rmx_mtu = 0;
 1206                 }
 1207         }
 1208 }
 1209 
 1210 static void
 1211 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r)
 1212 {
 1213         if (rt == NULL)
 1214                 panic("icmp_redirect_timeout:  bad route to timeout");
 1215         if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
 1216             (RTF_DYNAMIC | RTF_HOST)) {
 1217                 rtrequest((int) RTM_DELETE, rt_getkey(rt),
 1218                     rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
 1219         }
 1220 }
 1221 
 1222 /*
 1223  * Perform rate limit check.
 1224  * Returns 0 if it is okay to send the icmp packet.
 1225  * Returns 1 if the router SHOULD NOT send this icmp packet due to rate
 1226  * limitation.
 1227  *
 1228  * XXX per-destination/type check necessary?
 1229  */
 1230 static int
 1231 icmp_ratelimit(const struct in_addr *dst, const int type,
 1232     const int code)
 1233 {
 1234 
 1235         /* PPS limit */
 1236         if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count,
 1237             icmperrppslim)) {
 1238                 /* The packet is subject to rate limit */
 1239                 return 1;
 1240         }
 1241 
 1242         /* okay to send */
 1243         return 0;
 1244 }

Cache object: 10cca5b9dcf83cc075c1c28fafcdb7f2


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