The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_input.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: ns_input.c,v 1.20 2003/09/30 00:01:18 christos Exp $   */
    2 
    3 /*
    4  * Copyright (c) 1984, 1985, 1986, 1987, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)ns_input.c  8.2 (Berkeley) 9/22/94
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __KERNEL_RCSID(0, "$NetBSD: ns_input.c,v 1.20 2003/09/30 00:01:18 christos Exp $");
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/malloc.h>
   40 #include <sys/mbuf.h>
   41 #include <sys/domain.h>
   42 #include <sys/protosw.h>
   43 #include <sys/socket.h>
   44 #include <sys/socketvar.h>
   45 #include <sys/errno.h>
   46 #include <sys/time.h>
   47 #include <sys/kernel.h>
   48 
   49 #include <net/if.h>
   50 #include <net/route.h>
   51 #include <net/raw_cb.h>
   52 
   53 #include <netns/ns.h>
   54 #include <netns/ns_pcb.h>
   55 #include <netns/ns_if.h>
   56 #include <netns/ns_var.h>
   57 #include <netns/idp.h>
   58 #include <netns/idp_var.h>
   59 #include <netns/ns_error.h>
   60 #include <netns/sp.h>
   61 #include <netns/spidp.h>
   62 #include <netns/spp_timer.h>
   63 #include <netns/spp_var.h>
   64 
   65 /*
   66  * NS initialization.
   67  */
   68 union ns_host   ns_thishost;
   69 union ns_host   ns_zerohost;
   70 union ns_host   ns_broadhost;
   71 union ns_net    ns_zeronet;
   72 union ns_net    ns_broadnet;
   73 struct sockaddr_ns ns_netmask, ns_hostmask;
   74 
   75 static u_int16_t allones[] = {-1, -1, -1};
   76 
   77 struct nspcb nspcb;
   78 struct nspcb nsrawpcb;
   79 
   80 struct idpstat idpstat;
   81 
   82 struct ifqueue  nsintrq;
   83 int     nsqmaxlen = IFQ_MAXLEN;
   84 
   85 int     idpcksum = 1;
   86 long    ns_pexseq;
   87 
   88 void
   89 ns_init()
   90 {
   91 
   92         ns_broadhost = * (union ns_host *) allones;
   93         ns_broadnet = * (union ns_net *) allones;
   94         nspcb.nsp_next = nspcb.nsp_prev = &nspcb;
   95         nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb;
   96         nsintrq.ifq_maxlen = nsqmaxlen;
   97         TAILQ_INIT(&ns_ifaddr);
   98         ns_pexseq = time.tv_usec;
   99         ns_netmask.sns_len = 6;
  100         ns_netmask.sns_addr.x_net = ns_broadnet;
  101         ns_hostmask.sns_len = 12;
  102         ns_hostmask.sns_addr.x_net = ns_broadnet;
  103         ns_hostmask.sns_addr.x_host = ns_broadhost;
  104 }
  105 
  106 /*
  107  * Idp input routine.  Pass to next level.
  108  */
  109 int nsintr_getpck = 0;
  110 int nsintr_swtch = 0;
  111 void
  112 nsintr()
  113 {
  114         struct idp *idp;
  115         struct mbuf *m;
  116         struct nspcb *nsp;
  117         int i;
  118         int len, s, error;
  119         char oddpacketp;
  120 
  121 next:
  122         /*
  123          * Get next datagram off input queue and get IDP header
  124          * in first mbuf.
  125          */
  126         s = splnet();
  127         IF_DEQUEUE(&nsintrq, m);
  128         splx(s);
  129         nsintr_getpck++;
  130         if (m == 0)
  131                 return;
  132         if ((m->m_flags & M_EXT || m->m_len < sizeof (struct idp)) &&
  133             (m = m_pullup(m, sizeof (struct idp))) == 0) {
  134                 idpstat.idps_toosmall++;
  135                 goto next;
  136         }
  137 
  138         /*
  139          * Give any raw listeners a crack at the packet
  140          */
  141         for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
  142                 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
  143                 if (m1) idp_input(m1, nsp);
  144         }
  145 
  146         idp = mtod(m, struct idp *);
  147         len = ntohs(idp->idp_len);
  148         if ((oddpacketp = len & 1) != 0) {
  149                 len++;          /* If this packet is of odd length,
  150                                    preserve garbage byte for checksum */
  151         }
  152 
  153         /*
  154          * Check that the amount of data in the buffers
  155          * is as at least much as the IDP header would have us expect.
  156          * Trim mbufs if longer than we expect.
  157          * Drop packet if shorter than we expect.
  158          */
  159         if (m->m_pkthdr.len < len) {
  160                 idpstat.idps_tooshort++;
  161                 goto bad;
  162         }
  163         if (m->m_pkthdr.len > len) {
  164                 if (m->m_len == m->m_pkthdr.len) {
  165                         m->m_len = len;
  166                         m->m_pkthdr.len = len;
  167                 } else
  168                         m_adj(m, len - m->m_pkthdr.len);
  169         }
  170         if (idpcksum && ((i = idp->idp_sum)!=0xffff)) {
  171                 idp->idp_sum = 0;
  172                 if (i != (idp->idp_sum = ns_cksum(m, len))) {
  173                         idpstat.idps_badsum++;
  174                         idp->idp_sum = i;
  175                         if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
  176                                 error = NS_ERR_BADSUM;
  177                         else
  178                                 error = NS_ERR_BADSUM_T;
  179                         ns_error(m, error, 0);
  180                         goto next;
  181                 }
  182         }
  183         /*
  184          * Is this a directed broadcast?
  185          */
  186         if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) {
  187                 if ((!ns_neteq(idp->idp_dna, idp->idp_sna)) &&
  188                     (!ns_neteqnn(idp->idp_dna.x_net, ns_broadnet)) &&
  189                     (!ns_neteqnn(idp->idp_sna.x_net, ns_zeronet)) &&
  190                     (!ns_neteqnn(idp->idp_dna.x_net, ns_zeronet)) ) {
  191                         /*
  192                          * Look to see if I need to eat this packet.
  193                          * Algorithm is to forward all young packets
  194                          * and prematurely age any packets which will
  195                          * by physically broadcasted.
  196                          * Any very old packets eaten without forwarding
  197                          * would die anyway.
  198                          *
  199                          * Suggestion of Bill Nesheim, Cornell U.
  200                          */
  201                         if (idp->idp_tc < NS_MAXHOPS) {
  202                                 idp_forward(m);
  203                                 goto next;
  204                         }
  205                 }
  206         /*
  207          * Is this our packet? If not, forward.
  208          */
  209         } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) {
  210                 idp_forward(m);
  211                 goto next;
  212         }
  213         /*
  214          * Locate pcb for datagram.
  215          */
  216         nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
  217         /*
  218          * Switch out to protocol's input routine.
  219          */
  220         nsintr_swtch++;
  221         if (nsp) {
  222                 if (oddpacketp) {
  223                         m_adj(m, -1);
  224                 }
  225                 if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0)
  226                         switch (idp->idp_pt) {
  227 
  228                             case NSPROTO_SPP:
  229                                     spp_input(m, nsp);
  230                                     goto next;
  231 
  232                             case NSPROTO_ERROR:
  233                                     ns_err_input(m);
  234                                     goto next;
  235                         }
  236                 idp_input(m, nsp);
  237         } else {
  238                 ns_error(m, NS_ERR_NOSOCK, 0);
  239         }
  240         goto next;
  241 
  242 bad:
  243         m_freem(m);
  244         goto next;
  245 }
  246 
  247 u_char nsctlerrmap[PRC_NCMDS] = {
  248         ECONNABORTED,   ECONNABORTED,   0,              0,
  249         0,              0,              EHOSTDOWN,      EHOSTUNREACH,
  250         ENETUNREACH,    EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
  251         EMSGSIZE,       0,              0,              0,
  252         0,              0,              0,              0
  253 };
  254 
  255 int idp_donosocks = 1;
  256 
  257 void *
  258 idp_ctlinput(cmd, sa, arg)
  259         int cmd;
  260         struct sockaddr *sa;
  261         void *arg;
  262 {
  263         struct ns_addr *ns;
  264         struct nspcb *nsp;
  265         struct ns_errp *errp = NULL;
  266         int type;
  267 
  268         if ((unsigned)cmd >= PRC_NCMDS)
  269                 return NULL;
  270         if (nsctlerrmap[cmd] == 0)
  271                 return NULL;            /* XXX */
  272         type = NS_ERR_UNREACH_HOST;
  273         switch (cmd) {
  274                 struct sockaddr_ns *sns;
  275 
  276         case PRC_IFDOWN:
  277         case PRC_HOSTDEAD:
  278         case PRC_HOSTUNREACH:
  279                 sns = (struct sockaddr_ns *) sa;
  280                 if (sns->sns_family != AF_NS)
  281                         return NULL;
  282                 ns = &sns->sns_addr;
  283                 break;
  284 
  285         default:
  286                 errp = arg;
  287                 ns = &errp->ns_err_idp.idp_dna;
  288                 type = errp->ns_err_num;
  289                 type = ntohs((u_int16_t)type);
  290         }
  291         switch (type) {
  292 
  293         case NS_ERR_UNREACH_HOST:
  294                 ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0);
  295                 break;
  296 
  297         case NS_ERR_NOSOCK:
  298                 nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port,
  299                                    NS_WILDCARD);
  300                 if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr))
  301                         (void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
  302         }
  303         return NULL;
  304 }
  305 
  306 int     idpprintfs = 0;
  307 int     idpforwarding = 1;
  308 /*
  309  * Forward a packet.  If some error occurs return the sender
  310  * an error packet.  Note we can't always generate a meaningful
  311  * error message because the NS errors don't have a large enough repetoire
  312  * of codes and types.
  313  */
  314 struct route idp_droute;
  315 struct route idp_sroute;
  316 
  317 void
  318 idp_forward(m)
  319         struct mbuf *m;
  320 {
  321         struct idp *idp = mtod(m, struct idp *);
  322         int error, type, code;
  323         struct mbuf *mcopy = NULL;
  324         int agedelta = 1;
  325         int flags = NS_FORWARDING;
  326         int ok_there = 0;
  327         int ok_back = 0;
  328 
  329         if (idpprintfs) {
  330                 printf("forward: src ");
  331                 ns_printhost(&idp->idp_sna);
  332                 printf(", dst ");
  333                 ns_printhost(&idp->idp_dna);
  334                 printf("hop count %d\n", idp->idp_tc);
  335         }
  336         if (idpforwarding == 0) {
  337                 /* can't tell difference between net and host */
  338                 type = NS_ERR_UNREACH_HOST, code = 0;
  339                 goto senderror;
  340         }
  341         idp->idp_tc++;
  342         if (idp->idp_tc > NS_MAXHOPS) {
  343                 type = NS_ERR_TOO_OLD, code = 0;
  344                 goto senderror;
  345         }
  346         /*
  347          * Save at most 42 bytes of the packet in case
  348          * we need to generate an NS error message to the src.
  349          */
  350         mcopy = m_copy(m, 0, imin((int)ntohs(idp->idp_len), 42));
  351 
  352         if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
  353                 type = NS_ERR_UNREACH_HOST, code = 0;
  354                 goto senderror;
  355         }
  356         /*
  357          * Here we think about  forwarding  broadcast packets,
  358          * so we try to insure that it doesn't go back out
  359          * on the interface it came in on.  Also, if we
  360          * are going to physically broadcast this, let us
  361          * age the packet so we can eat it safely the second time around.
  362          */
  363         if (idp->idp_dna.x_host.c_host[0] & 0x1) {
  364                 struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
  365                 struct ifnet *ifp;
  366                 if (ia) {
  367                         /* I'm gonna hafta eat this packet */
  368                         agedelta += NS_MAXHOPS - idp->idp_tc;
  369                         idp->idp_tc = NS_MAXHOPS;
  370                 }
  371                 if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) {
  372                         /* error = ENETUNREACH; He'll never get it! */
  373                         m_freem(m);
  374                         goto cleanup;
  375                 }
  376                 if (idp_droute.ro_rt &&
  377                     (ifp=idp_droute.ro_rt->rt_ifp) &&
  378                     idp_sroute.ro_rt &&
  379                     (ifp!=idp_sroute.ro_rt->rt_ifp)) {
  380                         flags |= NS_ALLOWBROADCAST;
  381                 } else {
  382                         type = NS_ERR_UNREACH_HOST, code = 0;
  383                         goto senderror;
  384                 }
  385         }
  386         /* need to adjust checksum */
  387         if (idp->idp_sum != 0xffff) {
  388                 union bytes {
  389                         u_int8_t c[4];
  390                         u_int16_t s[2];
  391                         u_int32_t l;
  392                 } x;
  393                 int shift;
  394                 x.l = 0; x.c[0] = agedelta;
  395                 shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
  396                 x.l = idp->idp_sum + (x.s[0] << shift);
  397                 x.l = x.s[0] + x.s[1];
  398                 x.l = x.s[0] + x.s[1];
  399                 if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
  400         }
  401         if ((error = ns_output(m, &idp_droute, flags)) != 0 && 
  402             (mcopy!=NULL)) {
  403                 idp = mtod(mcopy, struct idp *);
  404                 type = NS_ERR_UNSPEC_T, code = 0;
  405                 switch (error) {
  406 
  407                 case ENETUNREACH:
  408                 case EHOSTDOWN:
  409                 case EHOSTUNREACH:
  410                 case ENETDOWN:
  411                 case EPERM:
  412                         type = NS_ERR_UNREACH_HOST;
  413                         break;
  414 
  415                 case EMSGSIZE:
  416                         type = NS_ERR_TOO_BIG;
  417                         code = 576; /* too hard to figure out mtu here */
  418                         break;
  419 
  420                 case ENOBUFS:
  421                         type = NS_ERR_UNSPEC_T;
  422                         break;
  423                 }
  424                 mcopy = NULL;
  425         senderror:
  426                 ns_error(m, type, code);
  427         }
  428 cleanup:
  429         if (ok_there)
  430                 idp_undo_route(&idp_droute);
  431         if (ok_back)
  432                 idp_undo_route(&idp_sroute);
  433         if (mcopy != NULL)
  434                 m_freem(mcopy);
  435 }
  436 
  437 int
  438 idp_do_route(src, ro)
  439         struct ns_addr *src;
  440         struct route *ro;
  441 {
  442         
  443         struct sockaddr_ns *dst;
  444 
  445         bzero((caddr_t)ro, sizeof (*ro));
  446         dst = satosns(&ro->ro_dst);
  447 
  448         dst->sns_len = sizeof(*dst);
  449         dst->sns_family = AF_NS;
  450         dst->sns_addr = *src;
  451         dst->sns_addr.x_port = 0;
  452         rtalloc(ro);
  453         if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) {
  454                 return (0);
  455         }
  456         ro->ro_rt->rt_use++;
  457         return (1);
  458 }
  459 
  460 void
  461 idp_undo_route(ro)
  462         struct route *ro;
  463 {
  464         if (ro->ro_rt) {RTFREE(ro->ro_rt);}
  465 }
  466 
  467 void
  468 ns_watch_output(m, ifp)
  469         struct mbuf *m;
  470         struct ifnet *ifp;
  471 {
  472         struct nspcb *nsp;
  473         struct ifaddr *ifa;
  474         /*
  475          * Give any raw listeners a crack at the packet
  476          */
  477         for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
  478                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
  479                 if (m0) {
  480                         struct idp *idp;
  481 
  482                         M_PREPEND(m0, sizeof (*idp), M_DONTWAIT);
  483                         if (m0 == NULL)
  484                                 continue;
  485                         idp = mtod(m0, struct idp *);
  486                         idp->idp_sna.x_net = ns_zeronet;
  487                         idp->idp_sna.x_host = ns_thishost;
  488                         if (ifp && (ifp->if_flags & IFF_POINTOPOINT))
  489                                 for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
  490                                     ifa = ifa->ifa_list.tqe_next) {
  491                                         if (ifa->ifa_addr->sa_family == AF_NS) {
  492                                                 idp->idp_sna = IA_SNS(ifa)->sns_addr;
  493                                                 break;
  494                                         }
  495                                 }
  496                         idp->idp_len = ntohs((u_int16_t)m0->m_pkthdr.len);
  497                         idp_input(m0, nsp);
  498                 }
  499         }
  500 }

Cache object: 1fcb8f91ee08f75b841b0f896db69ef9


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.