much more generous NFILE
[unix-history] / usr / src / sys / netinet / ip_output.c
index 70bac4c..26e041a 100644 (file)
@@ -1,4 +1,4 @@
-/*     ip_output.c     1.20    81/11/26        */
+/*     ip_output.c     1.31    82/03/31        */
 
 #include "../h/param.h"
 #include "../h/mbuf.h"
 
 #include "../h/param.h"
 #include "../h/mbuf.h"
 #include "../net/if.h"
 #include "../net/ip.h"
 #include "../net/ip_var.h"
 #include "../net/if.h"
 #include "../net/ip.h"
 #include "../net/ip_var.h"
+#include "../net/route.h"
 
 
-ip_output(m, opt)
+ip_output(m, opt, ro, allowbroadcast)
        struct mbuf *m;
        struct mbuf *opt;
        struct mbuf *m;
        struct mbuf *opt;
+       struct route *ro;
+       int allowbroadcast;
 {
        register struct ip *ip = mtod(m, struct ip *);
        register struct ifnet *ifp;
 {
        register struct ip *ip = mtod(m, struct ip *);
        register struct ifnet *ifp;
-       int len, hlen = ip->ip_hl << 2, off;
+       int len, hlen = sizeof (struct ip), off;
+       struct route iproute;
+       struct sockaddr *dst;
 
 COUNT(IP_OUTPUT);
 
 COUNT(IP_OUTPUT);
+       if (opt)                                /* XXX */
+               (void) m_free(opt);             /* XXX */
        /*
         * Fill in IP header.
         */
        /*
         * Fill in IP header.
         */
@@ -29,12 +36,34 @@ COUNT(IP_OUTPUT);
        ip->ip_id = htons(ip_id++);
 
        /*
        ip->ip_id = htons(ip_id++);
 
        /*
-        * Find interface for this packet.
+        * Find interface for this packet in the routing
+        * table.  Note each interface has placed itself
+        * in there at boot time, so calls to rtalloc
+        * degenerate to if_ifonnetof(ip->ip_dst.s_net).
         */
         */
-       ifp = if_ifonnetof(ip->ip_dst);
-       if (ifp == 0) {
-               ifp = if_gatewayfor(ip->ip_dst);
-               if (ifp == 0)
+       if (ro == 0) {
+               ro = &iproute;
+               bzero((caddr_t)ro, sizeof (*ro));
+       }
+       if (ro->ro_rt == 0) {
+               ro->ro_dst.sa_family = AF_INET;
+               ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst;
+               rtalloc(ro);
+       }
+       if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
+               printf("no route to %x (from %x, len %d)\n",
+                   ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
+               goto bad;
+       }
+       dst = ro->ro_rt->rt_flags&RTF_DIRECT ?
+           (struct sockaddr *)&ro->ro_dst : &ro->ro_rt->rt_gateway;
+       if (ro == &iproute)
+               RTFREE(ro->ro_rt);
+       if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) {
+               struct sockaddr_in *sin;
+
+               sin = (struct sockaddr_in *)&ifp->if_broadaddr;
+               if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
                        goto bad;
        }
 
                        goto bad;
        }
 
@@ -42,11 +71,14 @@ COUNT(IP_OUTPUT);
         * If small enough for interface, can just send directly.
         */
        if (ip->ip_len <= ifp->if_mtu) {
         * If small enough for interface, can just send directly.
         */
        if (ip->ip_len <= ifp->if_mtu) {
+#if vax
                ip->ip_len = htons((u_short)ip->ip_len);
                ip->ip_off = htons((u_short)ip->ip_off);
                ip->ip_len = htons((u_short)ip->ip_len);
                ip->ip_off = htons((u_short)ip->ip_off);
+#endif
                ip->ip_sum = 0;
                ip->ip_sum = in_cksum(m, hlen);
                ip->ip_sum = 0;
                ip->ip_sum = in_cksum(m, hlen);
-               return ((*ifp->if_output)(ifp, m, PF_INET));
+               ro->ro_rt->rt_use++;
+               return ((*ifp->if_output)(ifp, m, dst));
        }
 
        /*
        }
 
        /*
@@ -66,8 +98,8 @@ COUNT(IP_OUTPUT);
         */
        m->m_len -= sizeof (struct ip);
        m->m_off += sizeof (struct ip);
         */
        m->m_len -= sizeof (struct ip);
        m->m_off += sizeof (struct ip);
-       for (off = 0; off < ip->ip_len; off += len) {
-               struct mbuf *mh = m_get(0);
+       for (off = 0; off < ip->ip_len-hlen; off += len) {
+               struct mbuf *mh = m_get(M_DONTWAIT);
                struct ip *mhip;
 
                if (mh == 0)
                struct ip *mhip;
 
                if (mh == 0)
@@ -80,23 +112,29 @@ COUNT(IP_OUTPUT);
                        mh->m_len = sizeof (struct ip) + olen;
                } else
                        mh->m_len = sizeof (struct ip);
                        mh->m_len = sizeof (struct ip) + olen;
                } else
                        mh->m_len = sizeof (struct ip);
-               mhip->ip_off = off;
-               if (off + len >= ip->ip_len)
-                       mhip->ip_len = ip->ip_len - off;
+               mhip->ip_off = off >> 3;
+               if (off + len >= ip->ip_len-hlen)
+                       len = mhip->ip_len = ip->ip_len - hlen - off;
                else {
                        mhip->ip_len = len;
                        mhip->ip_off |= IP_MF;
                }
                else {
                        mhip->ip_len = len;
                        mhip->ip_off |= IP_MF;
                }
-               mhip->ip_len = htons((u_short)(mhip->ip_len + sizeof (struct ip)));
+               mhip->ip_len += sizeof (struct ip);
+#if vax
+               mhip->ip_len = htons((u_short)mhip->ip_len);
+#endif
                mh->m_next = m_copy(m, off, len);
                if (mh->m_next == 0) {
                        (void) m_free(mh);
                        goto bad;
                }
                mh->m_next = m_copy(m, off, len);
                if (mh->m_next == 0) {
                        (void) m_free(mh);
                        goto bad;
                }
-               ip->ip_off = htons((u_short)ip->ip_off);
-               ip->ip_sum = 0;
-               ip->ip_sum = in_cksum(m, hlen);
-               if ((*ifp->if_output)(ifp, mh, PF_INET) == 0)
+#if vax
+               mhip->ip_off = htons((u_short)mhip->ip_off);
+#endif
+               mhip->ip_sum = 0;
+               mhip->ip_sum = in_cksum(mh, hlen);
+               ro->ro_rt->rt_use++;
+               if ((*ifp->if_output)(ifp, mh, dst) == 0)
                        goto bad;
        }
        m_freem(m);
                        goto bad;
        }
        m_freem(m);