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/bsd/net/if_tun.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
    3  *
    4  * @APPLE_LICENSE_HEADER_START@
    5  * 
    6  * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
    7  * 
    8  * This file contains Original Code and/or Modifications of Original Code
    9  * as defined in and that are subject to the Apple Public Source License
   10  * Version 2.0 (the 'License'). You may not use this file except in
   11  * compliance with the License. Please obtain a copy of the License at
   12  * http://www.opensource.apple.com/apsl/ and read it before using this
   13  * file.
   14  * 
   15  * The Original Code and all software distributed under the License are
   16  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
   17  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
   18  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
   19  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
   20  * Please see the License for the specific language governing rights and
   21  * limitations under the License.
   22  * 
   23  * @APPLE_LICENSE_HEADER_END@
   24  */
   25 /*      $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $  */
   26 
   27 /*
   28  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
   29  * Nottingham University 1987.
   30  *
   31  * This source may be freely distributed, however I would be interested
   32  * in any changes that are made.
   33  *
   34  * This driver takes packets off the IP i/f and hands them up to a
   35  * user process to have its wicked way with. This driver has it's
   36  * roots in a similar driver written by Phil Cockcroft (formerly) at
   37  * UCL. This driver is based much more on read/write/poll mode of
   38  * operation though.
   39  */
   40 
   41 #include "tun.h"
   42 #if NTUN > 0
   43 
   44 #include "opt_devfs.h"
   45 #include "opt_inet.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/proc.h>
   49 #include <sys/systm.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/socket.h>
   52 #include <sys/filio.h>
   53 #include <sys/sockio.h>
   54 #include <sys/ttycom.h>
   55 #include <sys/poll.h>
   56 #include <sys/signalvar.h>
   57 #include <sys/filedesc.h>
   58 #include <sys/kernel.h>
   59 #include <sys/sysctl.h>
   60 #if DEVFS
   61 #include <sys/devfsext.h>
   62 #endif /*DEVFS*/
   63 #include <sys/conf.h>
   64 #include <sys/uio.h>
   65 #include <sys/vnode.h>
   66 
   67 #include <net/if.h>
   68 #include <net/if_types.h>
   69 #include <net/netisr.h>
   70 #include <net/route.h>
   71 
   72 #if INET
   73 #include <netinet/in.h>
   74 #include <netinet/in_var.h>
   75 #endif
   76 
   77 #if INET6
   78 #include <netinet/ip6.h>
   79 #include <netinet6/ip6_var.h>
   80 #include <netinet6/in6_ifattach.h>
   81 #endif /* INET6 */
   82 
   83 #include "bpfilter.h"
   84 #if NBPFILTER > 0
   85 #include <net/bpf.h>
   86 #endif
   87 
   88 #include <net/if_tunvar.h>
   89 #include <net/if_tun.h>
   90 
   91 static void tunattach __P((void *));
   92 PSEUDO_SET(tunattach, if_tun);
   93 
   94 #define TUNDEBUG        if (tundebug) printf
   95 static int tundebug = 0;
   96 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
   97 
   98 static struct tun_softc tunctl[NTUN];
   99 
  100 static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
  101             struct rtentry *rt));
  102 static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
  103 static int tuninit __P((int, int, u_char));
  104 
  105 static  d_open_t        tunopen;
  106 static  d_close_t       tunclose;
  107 static  d_read_t        tunread;
  108 static  d_write_t       tunwrite;
  109 static  d_ioctl_t       tunioctl;
  110 static  d_poll_t        tunpoll;
  111 
  112 #define CDEV_MAJOR 52
  113 static struct cdevsw tun_cdevsw = {
  114         tunopen,        tunclose,       tunread,        tunwrite,
  115         tunioctl,       nullstop,       noreset,        nodevtotty,
  116         tunpoll,        nommap,         nostrategy,     "tun",  NULL,   -1
  117 };
  118 
  119 
  120 static  int     tun_devsw_installed;
  121 #if     DEVFS
  122 static  void    *tun_devfs_token[NTUN];
  123 #endif
  124 
  125 #define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
  126 #define dev_val(n) (((n) >> 8) | ((n) & 0xff))
  127 
  128 static void
  129 tunattach(dummy)
  130         void *dummy;
  131 {
  132         register int i;
  133         struct ifnet *ifp;
  134         dev_t dev;
  135 
  136         if ( tun_devsw_installed )
  137                 return;
  138         dev = makedev(CDEV_MAJOR, 0);
  139         cdevsw_add(&dev, &tun_cdevsw, NULL);
  140         tun_devsw_installed = 1;
  141         for ( i = 0; i < NTUN; i++ ) {
  142 #if DEVFS
  143                 tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i),
  144                                                       DV_CHR, UID_UUCP,
  145                                                       GID_DIALER, 0600,
  146                                                       "tun%d", i);
  147 #endif
  148                 tunctl[i].tun_flags = TUN_INITED;
  149 
  150                 ifp = &tunctl[i].tun_if;
  151                 ifp->if_unit = i;
  152                 ifp->if_name = "tun";
  153                 ifp->if_family = APPLE_IF_FAM_TUN;
  154                 ifp->if_mtu = TUNMTU;
  155                 ifp->if_ioctl = tunifioctl;
  156                 ifp->if_output = tunoutput;
  157                 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
  158                 ifp->if_type = IFT_PPP; /* necessary init value for IPv6 lladdr auto conf */
  159                 ifp->if_snd.ifq_maxlen = ifqmaxlen;
  160                 if_attach(ifp);
  161 #if NBPFILTER > 0
  162                 bpfattach(ifp, DLT_NULL, sizeof(u_int));
  163 #endif
  164         }
  165 }
  166 
  167 /*
  168  * tunnel open - must be superuser & the device must be
  169  * configured in
  170  */
  171 static  int
  172 tunopen(dev, flag, mode, p)
  173         dev_t   dev;
  174         int     flag, mode;
  175         struct proc *p;
  176 {
  177         struct ifnet    *ifp;
  178         struct tun_softc *tp;
  179         register int    unit, error;
  180 
  181         error = suser(p->p_ucred, &p->p_acflag);
  182         if (error)
  183                 return (error);
  184 
  185         if ((unit = dev_val(minor(dev))) >= NTUN)
  186                 return (ENXIO);
  187         tp = &tunctl[unit];
  188         if (tp->tun_flags & TUN_OPEN)
  189                 return EBUSY;
  190         ifp = &tp->tun_if;
  191         tp->tun_flags |= TUN_OPEN;
  192         TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
  193         return (0);
  194 }
  195 
  196 /*
  197  * tunclose - close the device - mark i/f down & delete
  198  * routing info
  199  */
  200 static  int
  201 tunclose(dev, foo, bar, p)
  202         dev_t dev;
  203         int foo;
  204         int bar;
  205         struct proc *p;
  206 {
  207         register int    unit = dev_val(minor(dev)), s;
  208         struct tun_softc *tp = &tunctl[unit];
  209         struct ifnet    *ifp = &tp->tun_if;
  210         struct mbuf     *m;
  211 
  212         tp->tun_flags &= ~TUN_OPEN;
  213 
  214         /*
  215          * junk all pending output
  216          */
  217         do {
  218                 s = splimp();
  219                 IF_DEQUEUE(&ifp->if_snd, m);
  220                 splx(s);
  221                 if (m)
  222                         m_freem(m);
  223         } while (m);
  224 
  225         if (ifp->if_flags & IFF_UP) {
  226                 s = splimp();
  227                 if_down(ifp);
  228                 if (ifp->if_flags & IFF_RUNNING) {
  229                     /* find internet addresses and delete routes */
  230                     register struct ifaddr *ifa;
  231                     for (ifa = ifp->if_addrhead.tqh_first; ifa;
  232                          ifa = ifa->ifa_link.tqe_next) {
  233                         switch (ifa->ifa_addr->sa_family) {
  234 #if INET
  235                         case AF_INET:
  236 #endif
  237 #if INET6
  238                         case AF_INET6:
  239 #endif
  240                             rtinit(ifa, (int)RTM_DELETE,
  241                                    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
  242                             break;
  243                         }
  244                     }
  245                 }
  246                 splx(s);
  247         }
  248         ifp->if_flags &= ~IFF_RUNNING;
  249         funsetown(tp->tun_sigio);
  250         selwakeup(&tp->tun_rsel);
  251         selthreadclear(&tp->tun_rsel);
  252 
  253         TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
  254         return (0);
  255 }
  256 
  257 static int
  258 tuninit(unit, cmd, af)
  259         int     unit;
  260         int     cmd;
  261         u_char  af;
  262 {
  263         struct tun_softc *tp = &tunctl[unit];
  264         struct ifnet    *ifp = &tp->tun_if;
  265         register struct ifaddr *ifa;
  266 
  267         TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
  268 
  269         ifp->if_flags |= IFF_UP | IFF_RUNNING;
  270         getmicrotime(&ifp->if_lastchange);
  271 
  272         for (ifa = ifp->if_addrhead.tqh_first; ifa; 
  273              ifa = ifa->ifa_link.tqe_next) {
  274 #if INET
  275                 if (ifa->ifa_addr->sa_family == AF_INET) {
  276                     struct sockaddr_in *si;
  277 
  278                     si = (struct sockaddr_in *)ifa->ifa_addr;
  279                     if (si && si->sin_addr.s_addr)
  280                             tp->tun_flags |= TUN_IASET;
  281 
  282                     si = (struct sockaddr_in *)ifa->ifa_dstaddr;
  283                     if (si && si->sin_addr.s_addr)
  284                             tp->tun_flags |= TUN_DSTADDR;
  285                 }
  286 #endif
  287         }
  288         return 0;
  289 }
  290 
  291 /*
  292  * Process an ioctl request.
  293  */
  294 int
  295 tunifioctl(ifp, cmd, data)
  296         struct ifnet *ifp;
  297         u_long  cmd;
  298         caddr_t data;
  299 {
  300         register struct ifreq *ifr = (struct ifreq *)data;
  301         int             error = 0, s;
  302 
  303         s = splimp();
  304         switch(cmd) {
  305         case SIOCGIFSTATUS:
  306                 ifs = (struct ifstat *)data;
  307                 if (tp->tun_pid)
  308                         sprintf(ifs->ascii + strlen(ifs->ascii),
  309                             "\tOpened by PID %d\n", tp->tun_pid);
  310                 break;
  311         case SIOCSIFADDR:
  312                 tuninit(ifp->if_unit);
  313                 TUNDEBUG("%s%d: address set\n",
  314                          ifp->if_name, ifp->if_unit);
  315                 break;
  316         case SIOCSIFDSTADDR:
  317 #if 0
  318 #if defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3
  319                 if (found_first_ifid == 0)
  320                         in6_ifattach_noifid(ifp);
  321 #endif /* defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3 */
  322 #endif
  323                 tuninit(ifp->if_unit, cmd, ifr->ifr_addr.sa_family);
  324                 break;
  325         case SIOCSIFMTU:
  326                 ifp->if_mtu = ifr->ifr_mtu;
  327                 TUNDEBUG("%s%d: mtu set\n",
  328                          ifp->if_name, ifp->if_unit);
  329                 break;
  330         case SIOCADDMULTI:
  331         case SIOCDELMULTI:
  332                 break;
  333 
  334         case SIOCSIFFLAGS:
  335                 if ((ifp->if_flags & IFF_UP) != 0)
  336                         ifp->if_flags |= IFF_RUNNING;
  337                 else if ((ifp->if_flags & IFF_UP) == 0)
  338                         ifp->if_flags &= ~IFF_RUNNING;
  339                 break;
  340 
  341         default:
  342                 error = EINVAL;
  343         }
  344         splx(s);
  345         return (error);
  346 }
  347 
  348 /*
  349  * tunoutput - queue packets from higher level ready to put out.
  350  */
  351 /* Packet data format between tun and ppp is changed to enable checking of
  352  * Address Family of sending packet. When INET6 is defined, 4byte AF field
  353  * is appended to packet data as following.
  354  *
  355  *        0  1  2  3  4  5  6  7  8 .....
  356  *       ------------------------------
  357  *       |    af     |  packet data .....
  358  *       ------------------------------
  359  *       ^^^^^^^^^^^^^
  360  *       Newly added part. The size is sizeof(u_long).
  361  *
  362  * However, this is not adopted for tun -> ppp AF_INET packet for 
  363  * backword compatibility, because the ppp process may be an existing
  364  * ip only supporting one.  
  365  * Also in ppp->tun case, when af value is unknown, (af > 255) is checked and
  366  * if it is true, AF_INET is assumed. (the 4byte may be the head of
  367  * AF_INET packet. Despite the byte order, the value must always be
  368  * greater than 255, because of ip_len field or (ip_v and ip_hl)
  369  * field. (Idea from Mr. Noritoshi Demize)
  370  */
  371 int
  372 tunoutput(ifp, m0, dst, rt)
  373         struct ifnet   *ifp;
  374         struct mbuf    *m0;
  375         struct sockaddr *dst;
  376         struct rtentry *rt;
  377 {
  378         struct tun_softc *tp = &tunctl[ifp->if_unit];
  379         int             s;
  380 
  381         TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
  382 
  383         if ((tp->tun_flags & TUN_READY) != TUN_READY) {
  384                 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
  385                           ifp->if_unit, tp->tun_flags);
  386                 m_freem (m0);
  387                 return EHOSTDOWN;
  388         }
  389 
  390 #if NBPFILTER > 0
  391         /* BPF write needs to be handled specially */
  392         if (dst->sa_family == AF_UNSPEC) {
  393                 dst->sa_family = *(mtod(m0, int *));
  394                 m0->m_len -= sizeof(int);
  395                 m0->m_pkthdr.len -= sizeof(int);
  396                 m0->m_data += sizeof(int);
  397         }
  398 
  399         if (ifp->if_bpf) {
  400                 /*
  401                  * We need to prepend the address family as
  402                  * a four byte field.  Cons up a dummy header
  403                  * to pacify bpf.  This is safe because bpf
  404                  * will only read from the mbuf (i.e., it won't
  405                  * try to free it or keep a pointer to it).
  406                  */
  407                 struct mbuf m;
  408                 u_int af = dst->sa_family;
  409 
  410                 m.m_next = m0;
  411                 m.m_len = 4;
  412                 m.m_data = (char *)&af;
  413 
  414                 bpf_mtap(ifp, &m);
  415         }
  416 #endif
  417 
  418         switch(dst->sa_family) {
  419 #if defined(INET) || defined(INET6)
  420 #if INET6
  421         case AF_INET6:
  422                 M_PREPEND(m0, sizeof(u_long) /* af field passed to upper */,
  423                           M_DONTWAIT);
  424                 if (m0 == 0)
  425                         return (ENOBUFS);
  426                 *mtod(m0, u_long *) = (u_long)dst->sa_family;
  427                 /* FALLTHROUGH */
  428 #endif /* INET6 */
  429 #if INET
  430         case AF_INET:
  431 #endif /* INET */
  432 #endif /* INET || INET6 */
  433                 s = splimp();
  434                 if (IF_QFULL(&ifp->if_snd)) {
  435                         IF_DROP(&ifp->if_snd);
  436                         m_freem(m0);
  437                         splx(s);
  438                         ifp->if_collisions++;
  439                         return (ENOBUFS);
  440                 }
  441                 ifp->if_obytes += m0->m_pkthdr.len;
  442                 IF_ENQUEUE(&ifp->if_snd, m0);
  443                 splx(s);
  444                 ifp->if_opackets++;
  445                 break;
  446         default:
  447                 m_freem(m0);
  448                 return EAFNOSUPPORT;
  449         }
  450 
  451         if (tp->tun_flags & TUN_RWAIT) {
  452                 tp->tun_flags &= ~TUN_RWAIT;
  453                 wakeup((caddr_t)tp);
  454         }
  455         if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
  456                 pgsigio(tp->tun_sigio, SIGIO, 0);
  457         selwakeup(&tp->tun_rsel);
  458         return 0;
  459 }
  460 
  461 /*
  462  * the cdevsw interface is now pretty minimal.
  463  */
  464 static  int
  465 tunioctl(dev, cmd, data, flag, p)
  466         dev_t           dev;
  467         u_long          cmd;
  468         caddr_t         data;
  469         int             flag;
  470         struct proc     *p;
  471 {
  472         int             unit = dev_val(minor(dev)), s;
  473         struct tun_softc *tp = &tunctl[unit];
  474         struct tuninfo *tunp;
  475 
  476         switch (cmd) {
  477         case TUNSIFINFO:
  478                 tunp = (struct tuninfo *)data;
  479                 tp->tun_if.if_mtu = tunp->mtu;
  480                 tp->tun_if.if_type = tunp->type;
  481                 tp->tun_if.if_baudrate = tunp->baudrate;
  482                 break;
  483         case TUNGIFINFO:
  484                 tunp = (struct tuninfo *)data;
  485                 tunp->mtu = tp->tun_if.if_mtu;
  486                 tunp->type = tp->tun_if.if_type;
  487                 tunp->baudrate = tp->tun_if.if_baudrate;
  488                 break;
  489         case TUNSDEBUG:
  490                 tundebug = *(int *)data;
  491                 break;
  492         case TUNGDEBUG:
  493                 *(int *)data = tundebug;
  494                 break;
  495         case FIONBIO:
  496                 break;
  497         case FIOASYNC:
  498                 if (*(int *)data)
  499                         tp->tun_flags |= TUN_ASYNC;
  500                 else
  501                         tp->tun_flags &= ~TUN_ASYNC;
  502                 break;
  503         case FIONREAD:
  504                 s = splimp();
  505                 if (tp->tun_if.if_snd.ifq_head) {
  506                         struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
  507                         for( *(int *)data = 0; mb != 0; mb = mb->m_next) 
  508                                 *(int *)data += mb->m_len;
  509                 } else
  510                         *(int *)data = 0;
  511                 splx(s);
  512                 break;
  513         case FIOSETOWN:
  514                 return (fsetown(*(int *)data, &tp->tun_sigio));
  515 
  516         case FIOGETOWN:
  517                 *(int *)data = fgetown(tp->tun_sigio);
  518                 return (0);
  519 
  520         /* This is deprecated, FIOSETOWN should be used instead. */
  521         case TIOCSPGRP:
  522                 return (fsetown(-(*(int *)data), &tp->tun_sigio));
  523 
  524         /* This is deprecated, FIOGETOWN should be used instead. */
  525         case TIOCGPGRP:
  526                 *(int *)data = -fgetown(tp->tun_sigio);
  527                 return (0);
  528 
  529         default:
  530                 return (ENOTTY);
  531         }
  532         return (0);
  533 }
  534 
  535 /*
  536  * The cdevsw read interface - reads a packet at a time, or at
  537  * least as much of a packet as can be read.
  538  */
  539 static  int
  540 tunread(dev, uio, flag)
  541         dev_t dev;
  542         struct uio *uio;
  543         int flag;
  544 {
  545         int             unit = dev_val(minor(dev));
  546         struct tun_softc *tp = &tunctl[unit];
  547         struct ifnet    *ifp = &tp->tun_if;
  548         struct mbuf     *m, *m0;
  549         int             error=0, len, s;
  550 
  551         TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
  552         if ((tp->tun_flags & TUN_READY) != TUN_READY) {
  553                 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
  554                           ifp->if_unit, tp->tun_flags);
  555                 return EHOSTDOWN;
  556         }
  557 
  558         tp->tun_flags &= ~TUN_RWAIT;
  559 
  560         s = splimp();
  561         do {
  562                 IF_DEQUEUE(&ifp->if_snd, m0);
  563                 if (m0 == 0) {
  564                         if (flag & IO_NDELAY) {
  565                                 splx(s);
  566                                 return EWOULDBLOCK;
  567                         }
  568                         tp->tun_flags |= TUN_RWAIT;
  569                         if( error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
  570                                         "tunread", 0)) {
  571                                 splx(s);
  572                                 return error;
  573                         }
  574                 }
  575         } while (m0 == 0);
  576         splx(s);
  577 
  578         while (m0 && uio->uio_resid > 0 && error == 0) {
  579                 len = min(uio->uio_resid, m0->m_len);
  580                 if (len == 0)
  581                         break;
  582                 error = uiomove(mtod(m0, caddr_t), len, uio);
  583                 MFREE(m0, m);
  584                 m0 = m;
  585         }
  586 
  587         if (m0) {
  588                 TUNDEBUG("Dropping mbuf\n");
  589                 m_freem(m0);
  590         }
  591         return error;
  592 }
  593 
  594 /*
  595  * the cdevsw write interface - an atomic write is a packet - or else!
  596  */
  597 /* See top of tunoutput() about interface change between ppp process and 
  598  * tun. */
  599 static  int
  600 tunwrite(dev, uio, flag)
  601         dev_t dev;
  602         struct uio *uio;
  603         int flag;
  604 {
  605         int             unit = dev_val(minor(dev));
  606         struct ifnet    *ifp = &tunctl[unit].tun_if;
  607         struct mbuf     *top, **mp, *m;
  608         int             error=0, s, tlen, mlen;
  609         u_long af;
  610         u_int netisr_af;
  611         struct ifqueue *afintrq = NULL;
  612 
  613         TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
  614 
  615         if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
  616                 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
  617                     uio->uio_resid);
  618                 return EIO;
  619         }
  620         tlen = uio->uio_resid;
  621 
  622         /* get a header mbuf */
  623         MGETHDR(m, M_DONTWAIT, MT_DATA);
  624         if (m == NULL)
  625                 return ENOBUFS;
  626         if (tlen > MHLEN) {
  627           MCLGET(m, M_DONTWAIT);
  628           if ((m->m_flags & M_EXT) == 0) {
  629             m_free(m);
  630             return ENOBUFS;
  631           }
  632           mlen = m->m_ext.ext_size;
  633         } else
  634           mlen = MHLEN;
  635 
  636         top = 0;
  637         mp = &top;
  638         while (error == 0 && uio->uio_resid > 0) {
  639                 m->m_len = min(mlen, uio->uio_resid);
  640                 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
  641                 *mp = m;
  642                 mp = &m->m_next;
  643                 if (uio->uio_resid > 0) {
  644                         MGET (m, M_DONTWAIT, MT_DATA);
  645                         if (m == 0) {
  646                                 error = ENOBUFS;
  647                                 break;
  648                         }
  649                         mlen = MLEN;
  650                 }
  651         }
  652         /* Change for checking Address Family of sending packet. */
  653         af = *mtod(top, u_long *);
  654         switch (af) {
  655 #if INET
  656         case AF_INET:
  657                 netisr_af = NETISR_IP;
  658                 afintrq = &ipintrq;
  659                 break;
  660 #endif /* INET */
  661 #if INET6
  662         case AF_INET6:
  663                 netisr_af = NETISR_IPV6;
  664                 afintrq = &ip6intrq;
  665                 break;
  666 #endif /* INET6 */
  667         default:
  668                 if (af > 255) { /* see description at the top of tunoutput */
  669                         af = AF_INET;
  670                         netisr_af = NETISR_IP;
  671                         afintrq = &ipintrq;
  672                         goto af_decided;
  673                 }
  674                 error = EAFNOSUPPORT;
  675                 break;
  676         }
  677         m_adj(top, sizeof(u_long)); /* remove af field passed from upper */
  678         tlen -= sizeof(u_long);
  679       af_decided:
  680         if (error) {
  681                 if (top)
  682                         m_freem (top);
  683                 return error;
  684         }
  685 
  686         top->m_pkthdr.len = tlen;
  687         top->m_pkthdr.rcvif = ifp;
  688 
  689 #if NBPFILTER > 0
  690         if (ifp->if_bpf) {
  691                 /*
  692                  * We need to prepend the address family as
  693                  * a four byte field.  Cons up a dummy header
  694                  * to pacify bpf.  This is safe because bpf
  695                  * will only read from the mbuf (i.e., it won't
  696                  * try to free it or keep a pointer to it).
  697                  */
  698                 struct mbuf m;
  699 
  700                 m.m_next = top;
  701                 m.m_len = 4;
  702                 m.m_data = (char *)&af;
  703 
  704                 bpf_mtap(ifp, &m);
  705         }
  706 #endif
  707 
  708         /* just for safety */
  709         if (!afintrq)
  710                 return EAFNOSUPPORT;
  711 
  712         s = splimp();
  713         if (IF_QFULL (afintrq)) {
  714                 IF_DROP(afintrq);
  715                 splx(s);
  716                 ifp->if_collisions++;
  717                 m_freem(top);
  718                 return ENOBUFS;
  719         }
  720         IF_ENQUEUE(afintrq, top);
  721         splx(s);
  722         ifp->if_ibytes += tlen;
  723         ifp->if_ipackets++;
  724         schednetisr(netisr_af);
  725         return error;
  726 }
  727 
  728 /*
  729  * tunpoll - the poll interface, this is only useful on reads
  730  * really. The write detect always returns true, write never blocks
  731  * anyway, it either accepts the packet or drops it.
  732  */
  733 static  int
  734 tunpoll(dev, events, wql, p)
  735         dev_t dev;
  736         int events;
  737         void * wql;
  738         struct proc *p;
  739 {
  740         int             unit = dev_val(minor(dev)), s;
  741         struct tun_softc *tp = &tunctl[unit];
  742         struct ifnet    *ifp = &tp->tun_if;
  743         int             revents = 0;
  744 
  745         s = splimp();
  746         TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
  747 
  748         if (events & (POLLIN | POLLRDNORM))
  749                 if (ifp->if_snd.ifq_len > 0) {
  750                         TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
  751                             ifp->if_unit, ifp->if_snd.ifq_len);
  752                         revents |= events & (POLLIN | POLLRDNORM);
  753                 } else {
  754                         TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
  755                             ifp->if_unit);
  756                         selrecord(p, &tp->tun_rsel, wql);
  757                 }
  758 
  759         if (events & (POLLOUT | POLLWRNORM))
  760                 revents |= events & (POLLOUT | POLLWRNORM);
  761 
  762         splx(s);
  763         return (revents);
  764 }
  765 
  766 
  767 #endif  /* NTUN */

Cache object: 18f37aa4ff58fb99a590c6223759a64e


[ 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.