BSD 4_3_Tahoe release
[unix-history] / usr / src / sys / netns / ns_input.c
index 882151f..15e3240 100644 (file)
@@ -1,9 +1,20 @@
 /*
 /*
- * Copyright (c) 1982 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
+ * All rights reserved.
  *
  *
- *     @(#)ns_input.c  6.5 (Berkeley) %G%
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)ns_input.c  7.4 (Berkeley) 6/29/88
  */
 
 #include "param.h"
  */
 
 #include "param.h"
 union ns_host  ns_thishost;
 union ns_host  ns_zerohost;
 union ns_host  ns_broadhost;
 union ns_host  ns_thishost;
 union ns_host  ns_zerohost;
 union ns_host  ns_broadhost;
+union ns_net   ns_zeronet;
+union ns_net   ns_broadnet;
 
 
-static char allones[] = {-1, -1, -1, -1, -1, -1};
+static u_short allones[] = {-1, -1, -1};
 
 struct nspcb nspcb;
 struct nspcb nsrawpcb;
 
 struct nspcb nspcb;
 struct nspcb nsrawpcb;
@@ -44,13 +57,18 @@ struct ifqueue      nsintrq;
 int    nsqmaxlen = IFQ_MAXLEN;
 
 int    idpcksum = 1;
 int    nsqmaxlen = IFQ_MAXLEN;
 
 int    idpcksum = 1;
+long   ns_pexseq;
 
 ns_init()
 {
 
 ns_init()
 {
+       extern struct timeval time;
+
        ns_broadhost = * (union ns_host *) allones;
        ns_broadhost = * (union ns_host *) allones;
+       ns_broadnet = * (union ns_net *) allones;
        nspcb.nsp_next = nspcb.nsp_prev = &nspcb;
        nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb;
        nsintrq.ifq_maxlen = nsqmaxlen;
        nspcb.nsp_next = nspcb.nsp_prev = &nspcb;
        nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb;
        nsintrq.ifq_maxlen = nsqmaxlen;
+       ns_pexseq = time.tv_usec;
 }
 
 /*
 }
 
 /*
@@ -62,7 +80,8 @@ nsintr()
 {
        register struct idp *idp;
        register struct mbuf *m;
 {
        register struct idp *idp;
        register struct mbuf *m;
-       struct nspcb *nsp;
+       register struct nspcb *nsp;
+       struct ifnet *ifp;
        struct mbuf *m0;
        register int i;
        int len, s, error;
        struct mbuf *m0;
        register int i;
        int len, s, error;
@@ -74,7 +93,7 @@ next:
         * in first mbuf.
         */
        s = splimp();
         * in first mbuf.
         */
        s = splimp();
-       IF_DEQUEUE(&nsintrq, m);
+       IF_DEQUEUEIF(&nsintrq, m, ifp);
        splx(s);
        nsintr_getpck++;
        if (m == 0)
        splx(s);
        nsintr_getpck++;
        if (m == 0)
@@ -89,8 +108,8 @@ next:
         * Give any raw listeners a crack at the packet
         */
        for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
         * Give any raw listeners a crack at the packet
         */
        for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
-               struct mbuf *m1 = m_copy(m, 0, M_COPYALL);
-               if (m1) idp_input(m1, nsp);
+               struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
+               if (m1) idp_input(m1, nsp, ifp);
        }
 
        idp = mtod(m, struct idp *);
        }
 
        idp = mtod(m, struct idp *);
@@ -130,6 +149,7 @@ next:
                idp->idp_sum = 0;
                if (i != (idp->idp_sum = ns_cksum(m,len))) {
                        idpstat.idps_badsum++;
                idp->idp_sum = 0;
                if (i != (idp->idp_sum = ns_cksum(m,len))) {
                        idpstat.idps_badsum++;
+                       idp->idp_sum = i;
                        if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
                                error = NS_ERR_BADSUM;
                        else
                        if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
                                error = NS_ERR_BADSUM;
                        else
@@ -142,9 +162,10 @@ next:
         * Is this a directed broadcast?
         */
        if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) {
         * Is this a directed broadcast?
         */
        if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) {
-               if ((ns_netof(idp->idp_dna)!=ns_netof(idp->idp_sna)) &&
-                  (ns_netof(idp->idp_dna)!=-1) && (ns_netof(idp->idp_sna)!=0)
-                  && (ns_netof(idp->idp_dna)!=0)) {
+               if ((!ns_neteq(idp->idp_dna, idp->idp_sna)) &&
+                   (!ns_neteqnn(idp->idp_dna.x_net, ns_broadnet)) &&
+                   (!ns_neteqnn(idp->idp_sna.x_net, ns_zeronet)) &&
+                   (!ns_neteqnn(idp->idp_dna.x_net, ns_zeronet)) ) {
                        /*
                         * Look to see if I need to eat this packet.
                         * Algorithm is to forward all young packets
                        /*
                         * Look to see if I need to eat this packet.
                         * Algorithm is to forward all young packets
@@ -167,38 +188,31 @@ next:
                idp_forward(idp);
                goto next;
        }
                idp_forward(idp);
                goto next;
        }
-
        /*
         * Locate pcb for datagram.
         */
        nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
        /*
         * Locate pcb for datagram.
         */
        nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
-
-
-        /*
-         * Switch out to protocol's input routine.
-         */
-
+       /*
+        * Switch out to protocol's input routine.
+        */
        nsintr_swtch++;
        if (nsp) {
                if (oddpacketp) {
                        m_adj(m0, -1);
                }
        nsintr_swtch++;
        if (nsp) {
                if (oddpacketp) {
                        m_adj(m0, -1);
                }
-               switch (idp->idp_pt) {
+               if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0)
+                       switch (idp->idp_pt) {
 
 
-                       case NSPROTO_SPP:
-                               spp_input(m,nsp);
-                               break;
+                           case NSPROTO_SPP:
+                                   spp_input(m, nsp, ifp);
+                                   goto next;
 
 
-                       case NSPROTO_ERROR:
-                               ns_err_input(m);
-                               break;
-                       default:
-                               idp_input(m,nsp);
-               }
+                           case NSPROTO_ERROR:
+                                   ns_err_input(m);
+                                   goto next;
+                       }
+               idp_input(m, nsp, ifp);
        } else {
        } else {
-               /* don't send ERROR response for multicast packet */
-               if (idp->idp_dna.x_host.c_host[0] & 1)
-                       goto bad;
                ns_error(m, NS_ERR_NOSOCK, 0);
        }
        goto next;
                ns_error(m, NS_ERR_NOSOCK, 0);
        }
        goto next;
@@ -216,12 +230,17 @@ u_char nsctlerrmap[PRC_NCMDS] = {
        0,              0,              0,              0
 };
 
        0,              0,              0,              0
 };
 
+idp_donosocks = 1;
+
 idp_ctlinput(cmd, arg)
        int cmd;
        caddr_t arg;
 {
        struct ns_addr *ns;
 idp_ctlinput(cmd, arg)
        int cmd;
        caddr_t arg;
 {
        struct ns_addr *ns;
+       struct nspcb *nsp;
+       struct ns_errp *errp;
        int idp_abort();
        int idp_abort();
+       extern struct nspcb *idp_drop();
        int type;
 
        if (cmd < 0 || cmd > PRC_NCMDS)
        int type;
 
        if (cmd < 0 || cmd > PRC_NCMDS)
@@ -229,20 +248,35 @@ idp_ctlinput(cmd, arg)
        if (nsctlerrmap[cmd] == 0)
                return;         /* XXX */
        type = NS_ERR_UNREACH_HOST;
        if (nsctlerrmap[cmd] == 0)
                return;         /* XXX */
        type = NS_ERR_UNREACH_HOST;
-       if (cmd == PRC_IFDOWN)
-               ns = &((struct sockaddr_ns *)arg)->sns_addr;
-       else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH)
-               ns = (struct ns_addr *)arg;
-       else {
-               ns = &((struct ns_errp *)arg)->ns_err_idp.idp_dna;
-               type = ((struct ns_errp *)arg)->ns_err_num;
-               type = ntohs(type);
+       switch (cmd) {
+               struct sockaddr_ns *sns;
+
+       case PRC_IFDOWN:
+       case PRC_HOSTDEAD:
+       case PRC_HOSTUNREACH:
+               sns = (struct sockaddr_ns *)arg;
+               if (sns->sns_family != AF_INET)
+                       return;
+               ns = &sns->sns_addr;
+               break;
+
+       default:
+               errp = (struct ns_errp *)arg;
+               ns = &errp->ns_err_idp.idp_dna;
+               type = errp->ns_err_num;
+               type = ntohs((u_short)type);
        }
        switch (type) {
 
        case NS_ERR_UNREACH_HOST:
        }
        switch (type) {
 
        case NS_ERR_UNREACH_HOST:
+               ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0);
+               break;
+
        case NS_ERR_NOSOCK:
        case NS_ERR_NOSOCK:
-               ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, 0);
+               nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port,
+                       NS_WILDCARD);
+               if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr))
+                       (void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
        }
 }
 
        }
 }
 
@@ -288,7 +322,7 @@ idp_forward(idp)
         * Save at most 42 bytes of the packet in case
         * we need to generate an NS error message to the src.
         */
         * Save at most 42 bytes of the packet in case
         * we need to generate an NS error message to the src.
         */
-       mcopy = m_copy(dtom(idp), 0, imin(ntohs(idp->idp_len), 42));
+       mcopy = m_copy(dtom(idp), 0, imin((int)ntohs(idp->idp_len), 42));
 
        if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
                type = NS_ERR_UNREACH_HOST, code = 0;
 
        if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
                type = NS_ERR_UNREACH_HOST, code = 0;
@@ -302,7 +336,7 @@ idp_forward(idp)
         * age the packet so we can eat it safely the second time around.
         */
        if (idp->idp_dna.x_host.c_host[0] & 0x1) {
         * age the packet so we can eat it safely the second time around.
         */
        if (idp->idp_dna.x_host.c_host[0] & 0x1) {
-               struct ns_ifaddr *ia = ns_iaonnetof(idp->idp_dna.x_net);
+               struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
                struct ifnet *ifp;
                if (ia) {
                        /* I'm gonna hafta eat this packet */
                struct ifnet *ifp;
                if (ia) {
                        /* I'm gonna hafta eat this packet */
@@ -334,7 +368,7 @@ idp_forward(idp)
                register int shift;
                x.l = 0; x.c[0] = agedelta;
                shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
                register int shift;
                x.l = 0; x.c[0] = agedelta;
                shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
-               x.l = idp->idp_sum + (x.l << shift);
+               x.l = idp->idp_sum + (x.s[0] << shift);
                x.l = x.s[0] + x.s[1];
                x.l = x.s[0] + x.s[1];
                if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
                x.l = x.s[0] + x.s[1];
                x.l = x.s[0] + x.s[1];
                if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
@@ -401,15 +435,46 @@ register struct route *ro;
 {
        if (ro->ro_rt) {RTFREE(ro->ro_rt);}
 }
 {
        if (ro->ro_rt) {RTFREE(ro->ro_rt);}
 }
-ns_watch_output(m)
+static union ns_net
+ns_zeronet;
+
+ns_watch_output(m, ifp)
 struct mbuf *m;
 struct mbuf *m;
+struct ifnet *ifp;
 {
        register struct nspcb *nsp;
 {
        register struct nspcb *nsp;
+       register struct ifaddr *ia;
        /*
         * Give any raw listeners a crack at the packet
         */
        for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
        /*
         * Give any raw listeners a crack at the packet
         */
        for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
-               struct mbuf *m1 = m_copy(m, 0, M_COPYALL);
-               if (m1) idp_input(m1, nsp);
+               struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
+               if (m0) {
+                       struct mbuf *m1 = m_get(M_DONTWAIT, MT_DATA);
+
+                       if(m1 == NULL)
+                               m_freem(m0);
+                       else {
+                               register struct idp *idp;
+
+                               m1->m_off = MMINOFF;
+                               m1->m_len = sizeof (*idp);
+                               m1->m_next = m0;
+                               idp = mtod(m1, struct idp *);
+                               idp->idp_sna.x_net = ns_zeronet;
+                               idp->idp_sna.x_host = ns_thishost;
+                               if (ifp && (ifp->if_flags & IFF_POINTOPOINT))
+                                   for(ia = ifp->if_addrlist; ia;
+                                                       ia = ia->ifa_next) {
+                                       if (ia->ifa_addr.sa_family==AF_NS) {
+                                           idp->idp_sna = 
+                                               satons_addr(ia->ifa_dstaddr);
+                                           break;
+                                       }
+                                   }
+                               idp->idp_len = 0xffff;
+                               idp_input(m1, nsp, ifp);
+                       }
+               }
        }
 }
        }
 }