[BACK]Return to ip_input.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/sys/netinet/ip_input.c between version 1.49 and 1.82.2.1

version 1.49, 1997/04/15 00:41:52 version 1.82.2.1, 1999/04/07 23:20:42
Line 1 
Line 1 
 /*      $NetBSD$        */  /*      $NetBSD$        */
   
   /*-
    * Copyright (c) 1998 The NetBSD Foundation, Inc.
    * All rights reserved.
    *
    * This code is derived from software contributed to The NetBSD Foundation
    * by Public Access Networks Corporation ("Panix").  It was developed under
    * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    * 1. Redistributions of source code must retain the above copyright
    *    notice, this list of conditions and the following disclaimer.
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in the
    *    documentation and/or other materials provided with the distribution.
    * 3. All advertising materials mentioning features or use of this software
    *    must display the following acknowledgement:
    *      This product includes software developed by the NetBSD
    *      Foundation, Inc. and its contributors.
    * 4. Neither the name of The NetBSD Foundation nor the names of its
    *    contributors may be used to endorse or promote products derived
    *    from this software without specific prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    * POSSIBILITY OF SUCH DAMAGE.
    */
   
 /*  /*
  * Copyright (c) 1982, 1986, 1988, 1993   * Copyright (c) 1982, 1986, 1988, 1993
  *      The Regents of the University of California.  All rights reserved.   *      The Regents of the University of California.  All rights reserved.
Line 35 
Line 72 
  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94   *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
  */   */
   
   #include "opt_gateway.h"
   #include "opt_pfil_hooks.h"
   #include "opt_mrouting.h"
   
 #include <sys/param.h>  #include <sys/param.h>
 #include <sys/systm.h>  #include <sys/systm.h>
 #include <sys/malloc.h>  #include <sys/malloc.h>
Line 47 
Line 88 
 #include <sys/time.h>  #include <sys/time.h>
 #include <sys/kernel.h>  #include <sys/kernel.h>
 #include <sys/proc.h>  #include <sys/proc.h>
   #include <sys/pool.h>
   
 #include <vm/vm.h>  #include <vm/vm.h>
 #include <sys/sysctl.h>  #include <sys/sysctl.h>
Line 64 
Line 106 
 #include <netinet/ip_var.h>  #include <netinet/ip_var.h>
 #include <netinet/ip_icmp.h>  #include <netinet/ip_icmp.h>
   
 /* XXX should really put this in libkern.h */  
 #define offsetof(type, member)  ((size_t)(&((type *)0)->member))  
   
 #ifndef IPFORWARDING  #ifndef IPFORWARDING
 #ifdef GATEWAY  #ifdef GATEWAY
 #define IPFORWARDING    1       /* forward IP packets not for us */  #define IPFORWARDING    1       /* forward IP packets not for us */
Line 83 
Line 122 
 #ifndef IPALLOWSRCRT  #ifndef IPALLOWSRCRT
 #define IPALLOWSRCRT    1       /* allow source-routed packets */  #define IPALLOWSRCRT    1       /* allow source-routed packets */
 #endif  #endif
   #ifndef IPMTUDISC
   #define IPMTUDISC       0
   #endif
   #ifndef IPMTUDISCTIMEOUT
   #define IPMTUDISCTIMEOUT (10 * 60)      /* as per RFC 1191 */
   #endif
   
 /*  /*
  * Note: DIRECTED_BROADCAST is handled this way so that previous   * Note: DIRECTED_BROADCAST is handled this way so that previous
  * configuration using this option will Just Work.   * configuration using this option will Just Work.
Line 100  int ip_defttl = IPDEFTTL;
Line 146  int ip_defttl = IPDEFTTL;
 int     ip_forwsrcrt = IPFORWSRCRT;  int     ip_forwsrcrt = IPFORWSRCRT;
 int     ip_directedbcast = IPDIRECTEDBCAST;  int     ip_directedbcast = IPDIRECTEDBCAST;
 int     ip_allowsrcrt = IPALLOWSRCRT;  int     ip_allowsrcrt = IPALLOWSRCRT;
   int     ip_mtudisc = IPMTUDISC;
   u_int   ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
 #ifdef DIAGNOSTIC  #ifdef DIAGNOSTIC
 int     ipprintfs = 0;  int     ipprintfs = 0;
 #endif  #endif
   
   struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
   
 extern  struct domain inetdomain;  extern  struct domain inetdomain;
 extern  struct protosw inetsw[];  extern  struct protosw inetsw[];
 u_char  ip_protox[IPPROTO_MAX];  u_char  ip_protox[IPPROTO_MAX];
 int     ipqmaxlen = IFQ_MAXLEN;  int     ipqmaxlen = IFQ_MAXLEN;
 struct  in_ifaddrhead in_ifaddr;  struct  in_ifaddrhead in_ifaddr;
   struct  in_ifaddrhashhead *in_ifaddrhashtbl;
 struct  ifqueue ipintrq;  struct  ifqueue ipintrq;
   struct  ipstat  ipstat;
   u_int16_t       ip_id;
   int     ip_defttl;
   
   struct ipqhead ipq;
   int     ipq_locked;
   
   static __inline int ipq_lock_try __P((void));
   static __inline void ipq_unlock __P((void));
   
   static __inline int
   ipq_lock_try()
   {
           int s;
   
           s = splimp();
           if (ipq_locked) {
                   splx(s);
                   return (0);
           }
           ipq_locked = 1;
           splx(s);
           return (1);
   }
   
   static __inline void
   ipq_unlock()
   {
           int s;
   
           s = splimp();
           ipq_locked = 0;
           splx(s);
   }
   
   #ifdef DIAGNOSTIC
   #define IPQ_LOCK()                                                      \
   do {                                                                    \
           if (ipq_lock_try() == 0) {                                      \
                   printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
                   panic("ipq_lock");                                      \
           }                                                               \
   } while (0)
   #define IPQ_LOCK_CHECK()                                                \
   do {                                                                    \
           if (ipq_locked == 0) {                                          \
                   printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
                   panic("ipq lock check");                                \
           }                                                               \
   } while (0)
   #else
   #define IPQ_LOCK()              (void) ipq_lock_try()
   #define IPQ_LOCK_CHECK()        /* nothing */
   #endif
   
   #define IPQ_UNLOCK()            ipq_unlock()
   
   struct pool ipqent_pool;
   
 /*  /*
  * We need to save the IP options in case a protocol wants to respond   * We need to save the IP options in case a protocol wants to respond
Line 138  ip_init()
Line 247  ip_init()
         register struct protosw *pr;          register struct protosw *pr;
         register int i;          register int i;
   
           pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
               0, NULL, NULL, M_IPQ);
   
         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);          pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
         if (pr == 0)          if (pr == 0)
                 panic("ip_init");                  panic("ip_init");
Line 152  ip_init()
Line 264  ip_init()
         ip_id = time.tv_sec & 0xffff;          ip_id = time.tv_sec & 0xffff;
         ipintrq.ifq_maxlen = ipqmaxlen;          ipintrq.ifq_maxlen = ipqmaxlen;
         TAILQ_INIT(&in_ifaddr);          TAILQ_INIT(&in_ifaddr);
           in_ifaddrhashtbl =
               hashinit(IN_IFADDR_HASH_SIZE, M_IFADDR, M_WAITOK, &in_ifaddrhash);
           if (ip_mtudisc != 0)
                   ip_mtudisc_timeout_q =
                       rt_timer_queue_create(ip_mtudisc_timeout);
   #ifdef GATEWAY
           ipflow_init();
   #endif
 }  }
   
 struct  sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };  struct  sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
Line 168  ipintr()
Line 288  ipintr()
         register struct mbuf *m;          register struct mbuf *m;
         register struct ipq *fp;          register struct ipq *fp;
         register struct in_ifaddr *ia;          register struct in_ifaddr *ia;
           register struct ifaddr *ifa;
         struct ipqent *ipqe;          struct ipqent *ipqe;
         int hlen = 0, mff, len, s;          int hlen = 0, mff, len, s;
 #ifdef PFIL_HOOKS  #ifdef PFIL_HOOKS
Line 219  next:
Line 340  next:
                 }                  }
                 ip = mtod(m, struct ip *);                  ip = mtod(m, struct ip *);
         }          }
         if ((ip->ip_sum = in_cksum(m, hlen)) != 0) {          if (in_cksum(m, hlen) != 0) {
                 ipstat.ips_badsum++;                  ipstat.ips_badsum++;
                 goto bad;                  goto bad;
         }          }
Line 228  next:
Line 349  next:
          * Convert fields to host representation.           * Convert fields to host representation.
          */           */
         NTOHS(ip->ip_len);          NTOHS(ip->ip_len);
         NTOHS(ip->ip_id);  
         NTOHS(ip->ip_off);          NTOHS(ip->ip_off);
         len = ip->ip_len;          len = ip->ip_len;
   
         /*          /*
            * Check for additional length bogosity
            */
           if (len < hlen)
           {
                   ipstat.ips_badlen++;
                   goto bad;
           }
   
           /*
          * Check that the amount of data in the buffers           * Check that the amount of data in the buffers
          * is as at least much as the IP header would have us expect.           * is as at least much as the IP header would have us expect.
          * Trim mbufs if longer than we expect.           * Trim mbufs if longer than we expect.
Line 250  next:
Line 379  next:
                         m_adj(m, len - m->m_pkthdr.len);                          m_adj(m, len - m->m_pkthdr.len);
         }          }
   
           /*
            * Assume that we can create a fast-forward IP flow entry
            * based on this packet.
            */
           m->m_flags |= M_CANFASTFWD;
   
 #ifdef PFIL_HOOKS  #ifdef PFIL_HOOKS
         /*          /*
          * Run through list of hooks for input packets.           * Run through list of hooks for input packets.  If there are any
            * filters which require that additional packets in the flow are
            * not fast-forwarded, they must clear the M_CANFASTFWD flag.
            * Note that filters must _never_ set this flag, as another filter
            * in the list may have previously cleared it.
          */           */
         m0 = m;          m0 = m;
         for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.le_next)          for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.tqe_next)
                 if (pfh->pfil_func) {                  if (pfh->pfil_func) {
                         rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0);                          rv = pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0);
                         if (rv)                          if (rv)
                                 goto next;                                  goto next;
                         ip = mtod(m = m0, struct ip *);                          m = m0;
                           if (m == NULL)
                                   goto next;
                           ip = mtod(m, struct ip *);
                 }                  }
 #endif /* PFIL_HOOKS */  #endif /* PFIL_HOOKS */
   
Line 277  next:
Line 419  next:
         /*          /*
          * Check our list of addresses, to see if the packet is for us.           * Check our list of addresses, to see if the packet is for us.
          */           */
         for (ia = in_ifaddr.tqh_first; ia; ia = ia->ia_list.tqe_next) {          INADDR_TO_IA(ip->ip_dst, ia);
                 if (in_hosteq(ip->ip_dst, ia->ia_addr.sin_addr))          if (ia != NULL) {
                   if (ia->ia_ifp->if_flags & IFF_UP)
                         goto ours;                          goto ours;
                 if (((ip_directedbcast == 0) || (ip_directedbcast &&          }
                     ia->ia_ifp == m->m_pkthdr.rcvif)) &&          if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
                     (ia->ia_ifp->if_flags & IFF_BROADCAST)) {                  for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
                       ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
                           if (ifa->ifa_addr->sa_family != AF_INET) continue;
                           ia = ifatoia(ifa);
                         if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||                          if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
                             in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||                              in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
                             /*                              /*
Line 292  next:
Line 438  next:
                             ip->ip_dst.s_addr == ia->ia_subnet ||                              ip->ip_dst.s_addr == ia->ia_subnet ||
                             ip->ip_dst.s_addr == ia->ia_net)                              ip->ip_dst.s_addr == ia->ia_net)
                                 goto ours;                                  goto ours;
                           /*
                            * An interface with IP address zero accepts
                            * all packets that arrive on that interface.
                            */
                           if (in_nullhost(ia->ia_addr.sin_addr))
                                   goto ours;
                 }                  }
         }          }
         if (IN_MULTICAST(ip->ip_dst.s_addr)) {          if (IN_MULTICAST(ip->ip_dst.s_addr)) {
Line 320  next:
Line 472  next:
                          * as expected when ip_mforward() is called from                           * as expected when ip_mforward() is called from
                          * ip_output().)                           * ip_output().)
                          */                           */
                         ip->ip_id = htons(ip->ip_id);  
                         if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {                          if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
                                 ipstat.ips_cantforward++;                                  ipstat.ips_cantforward++;
                                 m_freem(m);                                  m_freem(m);
                                 goto next;                                  goto next;
                         }                          }
                         ip->ip_id = ntohs(ip->ip_id);  
   
                         /*                          /*
                          * The process-level routing demon needs to receive                           * The process-level routing demon needs to receive
Line 373  ours:
Line 523  ours:
          * but it's not worth the time; just let them time out.)           * but it's not worth the time; just let them time out.)
          */           */
         if (ip->ip_off & ~(IP_DF|IP_RF)) {          if (ip->ip_off & ~(IP_DF|IP_RF)) {
                 if (m->m_flags & M_EXT) {               /* XXX */  
                         if ((m = m_pullup(m, sizeof (struct ip))) == 0) {  
                                 ipstat.ips_toosmall++;  
                                 goto next;  
                         }  
                         ip = mtod(m, struct ip *);  
                 }  
                 /*                  /*
                  * Look for queue of fragments                   * Look for queue of fragments
                  * of this datagram.                   * of this datagram.
                  */                   */
                   IPQ_LOCK();
                 for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)                  for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
                         if (ip->ip_id == fp->ipq_id &&                          if (ip->ip_id == fp->ipq_id &&
                             in_hosteq(ip->ip_src, fp->ipq_src) &&                              in_hosteq(ip->ip_src, fp->ipq_src) &&
Line 407  found:
Line 551  found:
                          */                           */
                         if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {                          if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
                                 ipstat.ips_badfrags++;                                  ipstat.ips_badfrags++;
                                   IPQ_UNLOCK();
                                 goto bad;                                  goto bad;
                         }                          }
                 }                  }
Line 419  found:
Line 564  found:
                  */                   */
                 if (mff || ip->ip_off) {                  if (mff || ip->ip_off) {
                         ipstat.ips_fragments++;                          ipstat.ips_fragments++;
                         MALLOC(ipqe, struct ipqent *, sizeof (struct ipqent),                          ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
                             M_IPQ, M_NOWAIT);  
                         if (ipqe == NULL) {                          if (ipqe == NULL) {
                                 ipstat.ips_rcvmemdrop++;                                  ipstat.ips_rcvmemdrop++;
                                   IPQ_UNLOCK();
                                 goto bad;                                  goto bad;
                         }                          }
                         ipqe->ipqe_mff = mff;                          ipqe->ipqe_mff = mff;
                           ipqe->ipqe_m = m;
                         ipqe->ipqe_ip = ip;                          ipqe->ipqe_ip = ip;
                         ip = ip_reass(ipqe, fp);                          m = ip_reass(ipqe, fp);
                         if (ip == 0)                          if (m == 0) {
                                   IPQ_UNLOCK();
                                 goto next;                                  goto next;
                           }
                         ipstat.ips_reassembled++;                          ipstat.ips_reassembled++;
                         m = dtom(ip);                          ip = mtod(m, struct ip *);
                           hlen = ip->ip_hl << 2;
                           ip->ip_len += hlen;
                 } else                  } else
                         if (fp)                          if (fp)
                                 ip_freef(fp);                                  ip_freef(fp);
         } else                  IPQ_UNLOCK();
                 ip->ip_len -= hlen;          }
   
         /*          /*
          * Switch out to protocol's input routine.           * Switch out to protocol's input routine.
          */           */
   #if IFA_STATS
           ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len;
   #endif
         ipstat.ips_delivered++;          ipstat.ips_delivered++;
         (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);          (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
         goto next;          goto next;
Line 455  bad:
Line 608  bad:
  * reassembly of this datagram already exists, then it   * reassembly of this datagram already exists, then it
  * is given as fp; otherwise have to make a chain.   * is given as fp; otherwise have to make a chain.
  */   */
 struct ip *  struct mbuf *
 ip_reass(ipqe, fp)  ip_reass(ipqe, fp)
         register struct ipqent *ipqe;          register struct ipqent *ipqe;
         register struct ipq *fp;          register struct ipq *fp;
 {  {
         register struct mbuf *m = dtom(ipqe->ipqe_ip);          register struct mbuf *m = ipqe->ipqe_m;
         register struct ipqent *nq, *p, *q;          register struct ipqent *nq, *p, *q;
         struct ip *ip;          struct ip *ip;
         struct mbuf *t;          struct mbuf *t;
         int hlen = ipqe->ipqe_ip->ip_hl << 2;          int hlen = ipqe->ipqe_ip->ip_hl << 2;
         int i, next;          int i, next;
   
           IPQ_LOCK_CHECK();
   
         /*          /*
          * Presence of header sizes in mbufs           * Presence of header sizes in mbufs
          * would confuse code below.           * would confuse code below.
Line 478  ip_reass(ipqe, fp)
Line 633  ip_reass(ipqe, fp)
          * If first fragment to arrive, create a reassembly queue.           * If first fragment to arrive, create a reassembly queue.
          */           */
         if (fp == 0) {          if (fp == 0) {
                 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)                  MALLOC(fp, struct ipq *, sizeof (struct ipq),
                       M_FTABLE, M_NOWAIT);
                   if (fp == NULL)
                         goto dropfrag;                          goto dropfrag;
                 fp = mtod(t, struct ipq *);  
                 LIST_INSERT_HEAD(&ipq, fp, ipq_q);                  LIST_INSERT_HEAD(&ipq, fp, ipq_q);
                 fp->ipq_ttl = IPFRAGTTL;                  fp->ipq_ttl = IPFRAGTTL;
                 fp->ipq_p = ipqe->ipqe_ip->ip_p;                  fp->ipq_p = ipqe->ipqe_ip->ip_p;
Line 511  ip_reass(ipqe, fp)
Line 667  ip_reass(ipqe, fp)
                 if (i > 0) {                  if (i > 0) {
                         if (i >= ipqe->ipqe_ip->ip_len)                          if (i >= ipqe->ipqe_ip->ip_len)
                                 goto dropfrag;                                  goto dropfrag;
                         m_adj(dtom(ipqe->ipqe_ip), i);                          m_adj(ipqe->ipqe_m, i);
                         ipqe->ipqe_ip->ip_off += i;                          ipqe->ipqe_ip->ip_off += i;
                         ipqe->ipqe_ip->ip_len -= i;                          ipqe->ipqe_ip->ip_len -= i;
                 }                  }
Line 528  ip_reass(ipqe, fp)
Line 684  ip_reass(ipqe, fp)
                 if (i < q->ipqe_ip->ip_len) {                  if (i < q->ipqe_ip->ip_len) {
                         q->ipqe_ip->ip_len -= i;                          q->ipqe_ip->ip_len -= i;
                         q->ipqe_ip->ip_off += i;                          q->ipqe_ip->ip_off += i;
                         m_adj(dtom(q->ipqe_ip), i);                          m_adj(q->ipqe_m, i);
                         break;                          break;
                 }                  }
                 nq = q->ipqe_q.le_next;                  nq = q->ipqe_q.le_next;
                 m_freem(dtom(q->ipqe_ip));                  m_freem(q->ipqe_m);
                 LIST_REMOVE(q, ipqe_q);                  LIST_REMOVE(q, ipqe_q);
                 FREE(q, M_IPQ);                  pool_put(&ipqent_pool, q);
         }          }
   
 insert:  insert:
Line 568  insert:
Line 724  insert:
                 ip_freef(fp);                  ip_freef(fp);
                 return (0);                  return (0);
         }          }
         m = dtom(q->ipqe_ip);          m = q->ipqe_m;
         t = m->m_next;          t = m->m_next;
         m->m_next = 0;          m->m_next = 0;
         m_cat(m, t);          m_cat(m, t);
         nq = q->ipqe_q.le_next;          nq = q->ipqe_q.le_next;
         FREE(q, M_IPQ);          pool_put(&ipqent_pool, q);
         for (q = nq; q != NULL; q = nq) {          for (q = nq; q != NULL; q = nq) {
                 t = dtom(q->ipqe_ip);                  t = q->ipqe_m;
                 nq = q->ipqe_q.le_next;                  nq = q->ipqe_q.le_next;
                 FREE(q, M_IPQ);                  pool_put(&ipqent_pool, q);
                 m_cat(m, t);                  m_cat(m, t);
         }          }
   
Line 591  insert:
Line 747  insert:
         ip->ip_src = fp->ipq_src;          ip->ip_src = fp->ipq_src;
         ip->ip_dst = fp->ipq_dst;          ip->ip_dst = fp->ipq_dst;
         LIST_REMOVE(fp, ipq_q);          LIST_REMOVE(fp, ipq_q);
         (void) m_free(dtom(fp));          FREE(fp, M_FTABLE);
         m->m_len += (ip->ip_hl << 2);          m->m_len += (ip->ip_hl << 2);
         m->m_data -= (ip->ip_hl << 2);          m->m_data -= (ip->ip_hl << 2);
         /* some debugging cruft by sklower, below, will go away soon */          /* some debugging cruft by sklower, below, will go away soon */
         if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */          if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
                 register int plen = 0;                  register int plen = 0;
                 for (t = m; m; m = m->m_next)                  for (t = m; t; t = t->m_next)
                         plen += m->m_len;                          plen += t->m_len;
                 t->m_pkthdr.len = plen;                  m->m_pkthdr.len = plen;
         }          }
         return (ip);          return (m);
   
 dropfrag:  dropfrag:
         ipstat.ips_fragdropped++;          ipstat.ips_fragdropped++;
         m_freem(m);          m_freem(m);
         FREE(ipqe, M_IPQ);          pool_put(&ipqent_pool, ipqe);
         return (0);          return (0);
 }  }
   
Line 620  ip_freef(fp)
Line 776  ip_freef(fp)
 {  {
         register struct ipqent *q, *p;          register struct ipqent *q, *p;
   
           IPQ_LOCK_CHECK();
   
         for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {          for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
                 p = q->ipqe_q.le_next;                  p = q->ipqe_q.le_next;
                 m_freem(dtom(q->ipqe_ip));                  m_freem(q->ipqe_m);
                 LIST_REMOVE(q, ipqe_q);                  LIST_REMOVE(q, ipqe_q);
                 FREE(q, M_IPQ);                  pool_put(&ipqent_pool, q);
         }          }
         LIST_REMOVE(fp, ipq_q);          LIST_REMOVE(fp, ipq_q);
         (void) m_free(dtom(fp));          FREE(fp, M_FTABLE);
 }  }
   
 /*  /*
Line 641  ip_slowtimo()
Line 799  ip_slowtimo()
         register struct ipq *fp, *nfp;          register struct ipq *fp, *nfp;
         int s = splsoftnet();          int s = splsoftnet();
   
           IPQ_LOCK();
         for (fp = ipq.lh_first; fp != NULL; fp = nfp) {          for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
                 nfp = fp->ipq_q.le_next;                  nfp = fp->ipq_q.le_next;
                 if (--fp->ipq_ttl == 0) {                  if (--fp->ipq_ttl == 0) {
Line 648  ip_slowtimo()
Line 807  ip_slowtimo()
                         ip_freef(fp);                          ip_freef(fp);
                 }                  }
         }          }
           IPQ_UNLOCK();
   #ifdef GATEWAY
           ipflow_slowtimo();
   #endif
         splx(s);          splx(s);
 }  }
   
Line 658  void
Line 821  void
 ip_drain()  ip_drain()
 {  {
   
           /*
            * We may be called from a device's interrupt context.  If
            * the ipq is already busy, just bail out now.
            */
           if (ipq_lock_try() == 0)
                   return;
   
         while (ipq.lh_first != NULL) {          while (ipq.lh_first != NULL) {
                 ipstat.ips_fragdropped++;                  ipstat.ips_fragdropped++;
                 ip_freef(ipq.lh_first);                  ip_freef(ipq.lh_first);
         }          }
   
           IPQ_UNLOCK();
 }  }
   
 /*  /*
Line 817  ip_dooptions(m)
Line 989  ip_dooptions(m)
                                 break;                                  break;
   
                         case IPOPT_TS_TSANDADDR:                          case IPOPT_TS_TSANDADDR:
                                 if (ipt->ipt_ptr + sizeof(n_time) +                                  if (ipt->ipt_ptr - 1 + sizeof(n_time) +
                                     sizeof(struct in_addr) > ipt->ipt_len)                                      sizeof(struct in_addr) > ipt->ipt_len)
                                         goto bad;                                          goto bad;
                                 ipaddr.sin_addr = dst;                                  ipaddr.sin_addr = dst;
Line 831  ip_dooptions(m)
Line 1003  ip_dooptions(m)
                                 break;                                  break;
   
                         case IPOPT_TS_PRESPEC:                          case IPOPT_TS_PRESPEC:
                                 if (ipt->ipt_ptr + sizeof(n_time) +                                  if (ipt->ipt_ptr - 1 + sizeof(n_time) +
                                     sizeof(struct in_addr) > ipt->ipt_len)                                      sizeof(struct in_addr) > ipt->ipt_len)
                                         goto bad;                                          goto bad;
                                 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,                                  bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
Line 861  ip_dooptions(m)
Line 1033  ip_dooptions(m)
         }          }
         return (0);          return (0);
 bad:  bad:
         ip->ip_len -= ip->ip_hl << 2;   /* XXX icmp_error adds in hdr length */  
         icmp_error(m, type, code, 0, 0);          icmp_error(m, type, code, 0, 0);
         ipstat.ips_badoptions++;          ipstat.ips_badoptions++;
         return (1);          return (1);
Line 1004  ip_stripoptions(m, mopt)
Line 1175  ip_stripoptions(m, mopt)
         register caddr_t opts;          register caddr_t opts;
         int olen;          int olen;
   
         olen = (ip->ip_hl<<2) - sizeof (struct ip);          olen = (ip->ip_hl << 2) - sizeof (struct ip);
         opts = (caddr_t)(ip + 1);          opts = (caddr_t)(ip + 1);
         i = m->m_len - (sizeof (struct ip) + olen);          i = m->m_len - (sizeof (struct ip) + olen);
         bcopy(opts  + olen, opts, (unsigned)i);          bcopy(opts  + olen, opts, (unsigned)i);
         m->m_len -= olen;          m->m_len -= olen;
         if (m->m_flags & M_PKTHDR)          if (m->m_flags & M_PKTHDR)
                 m->m_pkthdr.len -= olen;                  m->m_pkthdr.len -= olen;
         ip->ip_hl = sizeof(struct ip) >> 2;          ip->ip_len -= olen;
           ip->ip_hl = sizeof (struct ip) >> 2;
 }  }
   
 int inetctlerrmap[PRC_NCMDS] = {  int inetctlerrmap[PRC_NCMDS] = {
Line 1053  ip_forward(m, srcrt)
Line 1225  ip_forward(m, srcrt)
         dest = 0;          dest = 0;
 #ifdef DIAGNOSTIC  #ifdef DIAGNOSTIC
         if (ipprintfs)          if (ipprintfs)
                 printf("forward: src %x dst %x ttl %x\n",                  printf("forward: src %2.2x dst %2.2x ttl %x\n",
                     ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl);                      ntohl(ip->ip_src.s_addr),
                       ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
 #endif  #endif
         if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {          if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
                 ipstat.ips_cantforward++;                  ipstat.ips_cantforward++;
                 m_freem(m);                  m_freem(m);
                 return;                  return;
         }          }
         HTONS(ip->ip_id);  
         if (ip->ip_ttl <= IPTTLDEC) {          if (ip->ip_ttl <= IPTTLDEC) {
                 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);                  icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
                 return;                  return;
Line 1108  ip_forward(m, srcrt)
Line 1280  ip_forward(m, srcrt)
                 if (rt->rt_ifa &&                  if (rt->rt_ifa &&
                     (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==                      (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
                     ifatoia(rt->rt_ifa)->ia_subnet) {                      ifatoia(rt->rt_ifa)->ia_subnet) {
                     if (rt->rt_flags & RTF_GATEWAY)                          if (rt->rt_flags & RTF_GATEWAY)
                         dest = satosin(rt->rt_gateway)->sin_addr.s_addr;                                  dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
                     else                          else
                         dest = ip->ip_dst.s_addr;                                  dest = ip->ip_dst.s_addr;
                     /* Router requirements says to only send host redirects */                          /*
                     type = ICMP_REDIRECT;                           * Router requirements says to only send host
                     code = ICMP_REDIRECT_HOST;                           * redirects.
                            */
                           type = ICMP_REDIRECT;
                           code = ICMP_REDIRECT_HOST;
 #ifdef DIAGNOSTIC  #ifdef DIAGNOSTIC
                     if (ipprintfs)                          if (ipprintfs)
                         printf("redirect (%d) to %x\n", code, (u_int32_t)dest);                                  printf("redirect (%d) to %x\n", code,
                                       (u_int32_t)dest);
 #endif  #endif
                 }                  }
         }          }
Line 1131  ip_forward(m, srcrt)
Line 1307  ip_forward(m, srcrt)
                 if (type)                  if (type)
                         ipstat.ips_redirectsent++;                          ipstat.ips_redirectsent++;
                 else {                  else {
                         if (mcopy)                          if (mcopy) {
   #ifdef GATEWAY
                                   if (mcopy->m_flags & M_CANFASTFWD)
                                           ipflow_create(&ipforward_rt, mcopy);
   #endif
                                 m_freem(mcopy);                                  m_freem(mcopy);
                           }
                         return;                          return;
                 }                  }
         }          }
Line 1240  ip_sysctl(name, namelen, oldp, oldlenp, 
Line 1421  ip_sysctl(name, namelen, oldp, oldlenp, 
         void *newp;          void *newp;
         size_t newlen;          size_t newlen;
 {  {
           extern int subnetsarelocal;
   
           int error, old;
   
         /* All sysctl names at this level are terminal. */          /* All sysctl names at this level are terminal. */
         if (namelen != 1)          if (namelen != 1)
                 return (ENOTDIR);                  return (ENOTDIR);
Line 1270  ip_sysctl(name, namelen, oldp, oldlenp, 
Line 1455  ip_sysctl(name, namelen, oldp, oldlenp, 
         case IPCTL_ALLOWSRCRT:          case IPCTL_ALLOWSRCRT:
                 return (sysctl_int(oldp, oldlenp, newp, newlen,                  return (sysctl_int(oldp, oldlenp, newp, newlen,
                     &ip_allowsrcrt));                      &ip_allowsrcrt));
           case IPCTL_SUBNETSARELOCAL:
                   return (sysctl_int(oldp, oldlenp, newp, newlen,
                       &subnetsarelocal));
           case IPCTL_MTUDISC:
                   error = sysctl_int(oldp, oldlenp, newp, newlen,
                       &ip_mtudisc);
                   if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
                           ip_mtudisc_timeout_q =
                               rt_timer_queue_create(ip_mtudisc_timeout);
                   } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
                           rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
                           ip_mtudisc_timeout_q = NULL;
                   }
                   return error;
           case IPCTL_ANONPORTMIN:
                   old = anonportmin;
                   error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
                   if (anonportmin >= anonportmax || anonportmin > 65535
   #ifndef IPNOPRIVPORTS
                       || anonportmin < IPPORT_RESERVED
   #endif
                       ) {
                           anonportmin = old;
                           return (EINVAL);
                   }
                   return (error);
           case IPCTL_ANONPORTMAX:
                   old = anonportmax;
                   error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
                   if (anonportmin >= anonportmax || anonportmax > 65535
   #ifndef IPNOPRIVPORTS
                       || anonportmax < IPPORT_RESERVED
   #endif
                       ) {
                           anonportmax = old;
                           return (EINVAL);
                   }
                   return (error);
           case IPCTL_MTUDISCTIMEOUT:
                   error = sysctl_int(oldp, oldlenp, newp, newlen,
                      &ip_mtudisc_timeout);
                   if (ip_mtudisc_timeout_q != NULL)
                           rt_timer_queue_change(ip_mtudisc_timeout_q,
                                                 ip_mtudisc_timeout);
                   return (error);
   #ifdef GATEWAY
           case IPCTL_MAXFLOWS:
               {
                   int s;
   
                   error = sysctl_int(oldp, oldlenp, newp, newlen,
                      &ip_maxflows);
                   s = splsoftnet();
                   ipflow_reap(0);
                   splx(s);
                   return (error);
               }
   #endif
         default:          default:
                 return (EOPNOTSUPP);                  return (EOPNOTSUPP);
         }          }

Legend:
Removed from v.1.49  
changed lines
  Added in v.1.82.2.1

CVSweb <webmaster@jp.NetBSD.org>