much more generous NFILE
[unix-history] / usr / src / sys / netinet / ip_input.c
index 0947d1f..04fc07c 100644 (file)
@@ -1,4 +1,4 @@
-/*     ip_input.c      1.33    82/03/19        */
+/*     ip_input.c      1.38    82/03/31        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -16,6 +16,7 @@
 
 u_char ip_protox[IPPROTO_MAX];
 int    ipqmaxlen = IFQ_MAXLEN;
 
 u_char ip_protox[IPPROTO_MAX];
 int    ipqmaxlen = IFQ_MAXLEN;
+struct ifnet *ifinet;                  /* first inet interface */
 
 /*
  * IP initialization: fill in IP protocol switch table.
 
 /*
  * IP initialization: fill in IP protocol switch table.
@@ -39,10 +40,14 @@ COUNT(IP_INIT);
        ipq.next = ipq.prev = &ipq;
        ip_id = time & 0xffff;
        ipintrq.ifq_maxlen = ipqmaxlen;
        ipq.next = ipq.prev = &ipq;
        ip_id = time & 0xffff;
        ipintrq.ifq_maxlen = ipqmaxlen;
+       ifinet = if_ifwithaf(AF_INET);
 }
 
 u_char ipcksum = 1;
 struct ip *ip_reass();
 }
 
 u_char ipcksum = 1;
 struct ip *ip_reass();
+int    ipforwarding = 1;
+int    ipprintfs = 0;
+struct sockaddr_in ipaddr = { AF_INET };
 
 /*
  * Ip input routine.  Checksum and byte swap header.  If fragmented
 
 /*
  * Ip input routine.  Checksum and byte swap header.  If fragmented
@@ -121,27 +126,44 @@ next:
         */
        if (hlen > sizeof (struct ip))
                ip_dooptions(ip);
         */
        if (hlen > sizeof (struct ip))
                ip_dooptions(ip);
-       if (ifnet && ip->ip_dst.s_addr != ifnet->if_addr.s_addr &&
-           if_ifwithaddr(ip->ip_dst) == 0) {
-
-               goto bad;
-#ifdef notdef
-               printf("ip->ip_dst %x ip->ip_ttl %x\n",
-                   ip->ip_dst, ip->ip_ttl);
-               if (--ip->ip_ttl == 0) {
+
+       /*
+        * Fast check on the first internet
+        * interface in the list.
+        */
+       if (ifinet) {
+               struct sockaddr_in *sin;
+
+               sin = (struct sockaddr_in *)&ifinet->if_addr;
+               if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
+                       goto ours;
+               if ((ifinet->if_flags & IFF_BROADCAST) &&
+                   sin->sin_addr.s_addr == ip->ip_dst.s_addr)
+                       goto ours;
+       }
+       ipaddr.sin_addr = ip->ip_dst;
+       if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) {
+               if (ipprintfs)
+                       printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
+                               ip->ip_dst, ip->ip_ttl);
+               if (ipforwarding == 0)
+                       goto bad;
+               if (ip->ip_ttl < IPTTLDEC) {
                        icmp_error(ip, ICMP_TIMXCEED, 0);
                        goto next;
                }
                        icmp_error(ip, ICMP_TIMXCEED, 0);
                        goto next;
                }
+               ip->ip_ttl -= IPTTLDEC;
                mopt = m_get(M_DONTWAIT);
                if (mopt == 0)
                        goto bad;
                ip_stripoptions(ip, mopt);
                mopt = m_get(M_DONTWAIT);
                if (mopt == 0)
                        goto bad;
                ip_stripoptions(ip, mopt);
-               /* 0 here means no directed broadcast */
-               (void) ip_output(m0, mopt, 0);
+
+               /* last 0 here means no directed broadcast */
+               (void) ip_output(m0, mopt, 0, 0);
                goto next;
                goto next;
-#endif
        }
 
        }
 
+ours:
        /*
         * Look for queue of fragments
         * of this datagram.
        /*
         * Look for queue of fragments
         * of this datagram.
@@ -298,8 +320,12 @@ insert:
        t = m->m_next;
        m->m_next = 0;
        m_cat(m, t);
        t = m->m_next;
        m->m_next = 0;
        m_cat(m, t);
-       while ((q = q->ipf_next) != (struct ipasfrag *)fp)
-               m_cat(m, dtom(q));
+       q = q->ipf_next;
+       while (q != (struct ipasfrag *)fp) {
+               t = dtom(q);
+               q = q->ipf_next;
+               m_cat(m, t);
+       }
 
        /*
         * Create header for new ip packet by
 
        /*
         * Create header for new ip packet by
@@ -450,7 +476,8 @@ COUNT(IP_DOOPTIONS);
                        if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
                                break;
                        sin = (struct in_addr *)(cp + cp[2]);
                        if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1))
                                break;
                        sin = (struct in_addr *)(cp + cp[2]);
-                       ifp = if_ifwithaddr(*sin);
+                       ipaddr.sin_addr = *sin;
+                       ifp = if_ifwithaddr((struct sockaddr *)&ipaddr);
                        if (ifp == 0) {
                                if (opt == IPOPT_SSRR)
                                        goto bad;
                        if (ifp == 0) {
                                if (opt == IPOPT_SSRR)
                                        goto bad;
@@ -461,7 +488,8 @@ COUNT(IP_DOOPTIONS);
                        if (cp[2] > optlen - (sizeof (long) - 1))
                                break;
                        ip->ip_dst = sin[1];
                        if (cp[2] > optlen - (sizeof (long) - 1))
                                break;
                        ip->ip_dst = sin[1];
-                       if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0)
+                       if (opt == IPOPT_SSRR &&
+                           if_ifonnetof(ip->ip_dst.s_net) == 0)
                                goto bad;
                        break;
 
                                goto bad;
                        break;
 
@@ -483,12 +511,14 @@ COUNT(IP_DOOPTIONS);
                        case IPOPT_TS_TSANDADDR:
                                if (ipt->ipt_ptr + 8 > ipt->ipt_len)
                                        goto bad;
                        case IPOPT_TS_TSANDADDR:
                                if (ipt->ipt_ptr + 8 > ipt->ipt_len)
                                        goto bad;
-                               /* stamp with ``first'' interface address */
-                               *sin++ = ifnet->if_addr;
+                               if (ifinet == 0)
+                                       goto bad;       /* ??? */
+                               *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr;
                                break;
 
                        case IPOPT_TS_PRESPEC:
                                break;
 
                        case IPOPT_TS_PRESPEC:
-                               if (if_ifwithaddr(*sin) == 0)
+                               ipaddr.sin_addr = *sin;
+                               if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0)
                                        continue;
                                if (ipt->ipt_ptr + 8 > ipt->ipt_len)
                                        goto bad;
                                        continue;
                                if (ipt->ipt_ptr + 8 > ipt->ipt_len)
                                        goto bad;