fix per Jeffrey Jongeward
[unix-history] / usr / src / sys / netinet / ip_output.c
index a023d55..fb2d21d 100644 (file)
@@ -1,4 +1,4 @@
-/*     ip_output.c     1.24    82/02/12        */
+/*     ip_output.c     1.32    82/04/10        */
 
 #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"
+#include <errno.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 = sizeof (struct ip), off;
+       int len, hlen = sizeof (struct ip), off, error = 0;
+       struct route iproute;
+       struct sockaddr *dst;
 
 COUNT(IP_OUTPUT);
        if (opt)                                /* XXX */
 
 COUNT(IP_OUTPUT);
        if (opt)                                /* XXX */
@@ -31,13 +37,41 @@ 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) {
+               extern int ipprintfs;
+
+               if (ipprintfs)
+                       printf("no route to %x (from %x, len %d)\n",
+                           ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len);
+               error = ENETUNREACH;
+               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) {
+                       error = EPERM;          /* ??? */
                        goto bad;
                        goto bad;
+               }
        }
 
        /*
        }
 
        /*
@@ -50,18 +84,23 @@ COUNT(IP_OUTPUT);
 #endif
                ip->ip_sum = 0;
                ip->ip_sum = in_cksum(m, hlen);
 #endif
                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));
        }
 
        /*
         * Too large for interface; fragment if possible.
         * Must be able to put at least 8 bytes per fragment.
         */
        }
 
        /*
         * Too large for interface; fragment if possible.
         * Must be able to put at least 8 bytes per fragment.
         */
-       if (ip->ip_off & IP_DF)
+       if (ip->ip_off & IP_DF) {
+               error = EMSGSIZE;
                goto bad;
                goto bad;
+       }
        len = (ifp->if_mtu - hlen) &~ 7;
        len = (ifp->if_mtu - hlen) &~ 7;
-       if (len < 8)
+       if (len < 8) {
+               error = EMSGSIZE;
                goto bad;
                goto bad;
+       }
 
        /*
         * Discard IP header from logical mbuf for m_copy's sake.
 
        /*
         * Discard IP header from logical mbuf for m_copy's sake.
@@ -70,12 +109,14 @@ 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) {
+       for (off = 0; off < ip->ip_len-hlen; off += len) {
                struct mbuf *mh = m_get(M_DONTWAIT);
                struct ip *mhip;
 
                struct mbuf *mh = m_get(M_DONTWAIT);
                struct ip *mhip;
 
-               if (mh == 0)
+               if (mh == 0) {
+                       error = ENOBUFS;
                        goto bad;
                        goto bad;
+               }
                mh->m_off = MMAXOFF - hlen;
                mhip = mtod(mh, struct ip *);
                *mhip = *ip;
                mh->m_off = MMAXOFF - hlen;
                mhip = mtod(mh, struct ip *);
                *mhip = *ip;
@@ -85,8 +126,8 @@ COUNT(IP_OUTPUT);
                } else
                        mh->m_len = sizeof (struct ip);
                mhip->ip_off = off >> 3;
                } else
                        mh->m_len = sizeof (struct ip);
                mhip->ip_off = off >> 3;
-               if (off + len >= ip->ip_len)
-                       len = mhip->ip_len = ip->ip_len - off;
+               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;
@@ -98,21 +139,21 @@ COUNT(IP_OUTPUT);
                mh->m_next = m_copy(m, off, len);
                if (mh->m_next == 0) {
                        (void) m_free(mh);
                mh->m_next = m_copy(m, off, len);
                if (mh->m_next == 0) {
                        (void) m_free(mh);
+                       error = ENOBUFS;        /* ??? */
                        goto bad;
                }
 #if vax
                        goto bad;
                }
 #if vax
-               ip->ip_off = htons((u_short)ip->ip_off);
+               mhip->ip_off = htons((u_short)mhip->ip_off);
 #endif
 #endif
-               ip->ip_sum = 0;
-               ip->ip_sum = in_cksum(m, hlen);
-               if ((*ifp->if_output)(ifp, mh, PF_INET) == 0)
-                       goto bad;
+               mhip->ip_sum = 0;
+               mhip->ip_sum = in_cksum(mh, hlen);
+               ro->ro_rt->rt_use++;
+               if (error = (*ifp->if_output)(ifp, mh, dst))
+                       break;
        }
        }
-       m_freem(m);
-       return (1);
 bad:
        m_freem(m);
 bad:
        m_freem(m);
-       return (0);
+       return (error);
 }
 
 /*
 }
 
 /*