BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / netinet / ip_input.c
index 6f3a114..889e370 100644 (file)
@@ -2,9 +2,35 @@
  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * 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 University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)ip_input.c  7.17 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ *
+ *     @(#)ip_input.c  7.19 (Berkeley) 5/25/91
  */
 
 #include "param.h"
  */
 
 #include "param.h"
 #include "ip_var.h"
 #include "ip_icmp.h"
 
 #include "ip_var.h"
 #include "ip_icmp.h"
 
-#ifndef        IPFORWARDING
-#ifdef GATEWAY
-#define        IPFORWARDING    1
-#else /* GATEWAY */
-#define        IPFORWARDING    0
-#endif /* GATEWAY */
-#endif /* IPFORWARDING */
-#ifndef        IPSENDREDIRECTS
-#define        IPSENDREDIRECTS 1
-#endif
-int    ipprintfs = 0;
-int    ipforwarding = IPFORWARDING;
-extern int in_interfaces;
-int    ipsendredirects = IPSENDREDIRECTS;
-
 #ifndef        IPFORWARDING
 #ifdef GATEWAY
 #define        IPFORWARDING    1       /* forward IP packets not for us */
 #ifndef        IPFORWARDING
 #ifdef GATEWAY
 #define        IPFORWARDING    1       /* forward IP packets not for us */
@@ -56,10 +67,12 @@ int ipsendredirects = IPSENDREDIRECTS;
 #endif
 int    ipforwarding = IPFORWARDING;
 int    ipsendredirects = IPSENDREDIRECTS;
 #endif
 int    ipforwarding = IPFORWARDING;
 int    ipsendredirects = IPSENDREDIRECTS;
-#ifdef DEBUG
+#ifdef DIAGNOSTIC
 int    ipprintfs = 0;
 #endif
 
 int    ipprintfs = 0;
 #endif
 
+extern struct domain inetdomain;
+extern struct protosw inetsw[];
 u_char ip_protox[IPPROTO_MAX];
 int    ipqmaxlen = IFQ_MAXLEN;
 struct in_ifaddr *in_ifaddr;                   /* first inet address */
 u_char ip_protox[IPPROTO_MAX];
 int    ipqmaxlen = IFQ_MAXLEN;
 struct in_ifaddr *in_ifaddr;                   /* first inet address */
@@ -73,7 +86,6 @@ struct        in_ifaddr *in_ifaddr;                   /* first inet address */
  */
 int    ip_nhops = 0;
 static struct ip_srcrt {
  */
 int    ip_nhops = 0;
 static struct ip_srcrt {
-       struct  in_addr dst;                    /* final destination */
        struct  in_addr dst;                    /* final destination */
        char    nop;                            /* one NOP to align */
        char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
        struct  in_addr dst;                    /* final destination */
        char    nop;                            /* one NOP to align */
        char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
@@ -456,6 +468,13 @@ insert:
        m = dtom(ip);
        m->m_len += (ip->ip_hl << 2);
        m->m_data -= (ip->ip_hl << 2);
        m = dtom(ip);
        m->m_len += (ip->ip_hl << 2);
        m->m_data -= (ip->ip_hl << 2);
+       /* some debugging cruft by sklower, below, will go away soon */
+       if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
+               register int plen = 0;
+               for (t = m; m; m = m->m_next)
+                       plen += m->m_len;
+               t->m_pkthdr.len = plen;
+       }
        return ((struct ip *)ip);
 
 dropfrag:
        return ((struct ip *)ip);
 
 dropfrag:
@@ -558,13 +577,11 @@ struct in_ifaddr *ip_rtaddr();
 ip_dooptions(m)
        struct mbuf *m;
 {
 ip_dooptions(m)
        struct mbuf *m;
 {
-       register struct ip *ip = mtod(m, struct ip *);
        register struct ip *ip = mtod(m, struct ip *);
        register u_char *cp;
        register struct ip_timestamp *ipt;
        register struct in_ifaddr *ia;
        int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
        register struct ip *ip = mtod(m, struct ip *);
        register u_char *cp;
        register struct ip_timestamp *ipt;
        register struct in_ifaddr *ia;
        int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
-       int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
        struct in_addr *sin;
        n_time ntime;
 
        struct in_addr *sin;
        n_time ntime;
 
@@ -648,7 +665,6 @@ ip_dooptions(m)
                            (caddr_t)(cp + off), sizeof(struct in_addr));
                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                        forward = 1;
                            (caddr_t)(cp + off), sizeof(struct in_addr));
                        cp[IPOPT_OFFSET] += sizeof(struct in_addr);
                        forward = 1;
-                       forward = 1;
                        break;
 
                case IPOPT_RR:
                        break;
 
                case IPOPT_RR:
@@ -774,7 +790,7 @@ save_rte(option, dst)
        unsigned olen;
 
        olen = option[IPOPT_OLEN];
        unsigned olen;
 
        olen = option[IPOPT_OLEN];
-#ifdef DEBUG
+#ifdef DIAGNOSTIC
        if (ipprintfs)
                printf("save_rte: olen %d\n", olen);
 #endif
        if (ipprintfs)
                printf("save_rte: olen %d\n", olen);
 #endif
@@ -807,15 +823,7 @@ ip_srcroute()
        /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
        m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
            OPTSIZ;
        /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
        m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
            OPTSIZ;
-       if (ipprintfs)
-               printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
-
-#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
-
-       /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
-       m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
-           OPTSIZ;
-#ifdef DEBUG
+#ifdef DIAGNOSTIC
        if (ipprintfs)
                printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
 #endif
        if (ipprintfs)
                printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
 #endif
@@ -825,11 +833,9 @@ ip_srcroute()
         */
        p = &ip_srcrt.route[ip_nhops - 1];
        *(mtod(m, struct in_addr *)) = *p--;
         */
        p = &ip_srcrt.route[ip_nhops - 1];
        *(mtod(m, struct in_addr *)) = *p--;
-#ifdef DEBUG
-       if (ipprintfs)
-               printf(" hops %X", ntohl(*mtod(m, struct in_addr *)));
+#ifdef DIAGNOSTIC
        if (ipprintfs)
        if (ipprintfs)
-               printf(" hops %X", ntohl(*mtod(m, struct in_addr *)));
+               printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
 #endif
 
        /*
 #endif
 
        /*
@@ -847,9 +853,9 @@ ip_srcroute()
         * reversing the path (pointers are now aligned).
         */
        while (p >= ip_srcrt.route) {
         * reversing the path (pointers are now aligned).
         */
        while (p >= ip_srcrt.route) {
-#ifdef DEBUG
+#ifdef DIAGNOSTIC
                if (ipprintfs)
                if (ipprintfs)
-                       printf(" %X", ntohl(*q));
+                       printf(" %lx", ntohl(q->s_addr));
 #endif
                *q++ = *p--;
        }
 #endif
                *q++ = *p--;
        }
@@ -857,16 +863,9 @@ ip_srcroute()
         * Last hop goes to final destination.
         */
        *q = ip_srcrt.dst;
         * Last hop goes to final destination.
         */
        *q = ip_srcrt.dst;
+#ifdef DIAGNOSTIC
        if (ipprintfs)
        if (ipprintfs)
-               printf(" %X\n", ntohl(*q));
-       }
-       /*
-        * Last hop goes to final destination.
-        */
-       *q = ip_srcrt.dst;
-#ifdef DEBUG
-       if (ipprintfs)
-               printf(" %X\n", ntohl(*q));
+               printf(" %lx\n", ntohl(q->s_addr));
 #endif
        return (m);
 }
 #endif
        return (m);
 }
@@ -876,9 +875,6 @@ ip_srcroute()
  * level protocol in the kernel.
  * Second argument is buffer to which options
  * will be moved, and return value is their length.
  * level protocol in the kernel.
  * Second argument is buffer to which options
  * will be moved, and return value is their length.
-#ifdef NEW
- * XXX should be deleted; last arg currently ignored.
-#endif NEW
  * XXX should be deleted; last arg currently ignored.
  */
 ip_stripoptions(m, mopt)
  * XXX should be deleted; last arg currently ignored.
  */
 ip_stripoptions(m, mopt)
@@ -927,7 +923,6 @@ ip_forward(m, srcrt)
        struct mbuf *m;
        int srcrt;
 {
        struct mbuf *m;
        int srcrt;
 {
-       register struct ip *ip = mtod(m, struct ip *);
        register struct ip *ip = mtod(m, struct ip *);
        register struct sockaddr_in *sin;
        register struct rtentry *rt;
        register struct ip *ip = mtod(m, struct ip *);
        register struct sockaddr_in *sin;
        register struct rtentry *rt;
@@ -936,7 +931,7 @@ ip_forward(m, srcrt)
        struct in_addr dest;
 
        dest.s_addr = 0;
        struct in_addr dest;
 
        dest.s_addr = 0;
-#ifdef DEBUG
+#ifdef DIAGNOSTIC
        if (ipprintfs)
                printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
                        ip->ip_dst, ip->ip_ttl);
        if (ipprintfs)
                printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
                        ip->ip_dst, ip->ip_ttl);
@@ -946,7 +941,7 @@ ip_forward(m, srcrt)
                m_freem(m);
                return;
        }
                m_freem(m);
                return;
        }
-       ip->ip_id = htons(ip->ip_id);
+       HTONS(ip->ip_id);
        if (ip->ip_ttl <= IPTTLDEC) {
                icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
                return;
        if (ip->ip_ttl <= IPTTLDEC) {
                icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
                return;
@@ -1021,7 +1016,7 @@ ip_forward(m, srcrt)
                            code = ICMP_REDIRECT_HOST;
                    else
                            code = ICMP_REDIRECT_NET;
                            code = ICMP_REDIRECT_HOST;
                    else
                            code = ICMP_REDIRECT_NET;
-#ifdef DEBUG
+#ifdef DIAGNOSTIC
                    if (ipprintfs)
                        printf("redirect (%d) to %x\n", code, dest.s_addr);
 #endif
                    if (ipprintfs)
                        printf("redirect (%d) to %x\n", code, dest.s_addr);
 #endif