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/net/if_srt.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 /* $NetBSD: if_srt.c,v 1.8 2008/06/15 16:37:21 christos Exp $ */
    2 /* This file is in the public domain. */
    3 
    4 #include <sys/cdefs.h>
    5 __KERNEL_RCSID(0, "$NetBSD: if_srt.c,v 1.8 2008/06/15 16:37:21 christos Exp $");
    6 
    7 #include "opt_inet.h"
    8 
    9 #if !defined(INET) && !defined(INET6)
   10 #error "srt without INET/INET6?"
   11 #endif
   12 
   13 #ifndef SRT_MAXUNIT
   14 #define SRT_MAXUNIT 255
   15 #endif
   16 
   17 /* include-file bug workarounds */
   18 #include <sys/types.h> /* sys/conf.h */
   19 #include <sys/resource.h> /* sys/resourcevar.h (uvm/uvm_param.h, sys/mbuf.h) */
   20 #include <netinet/in.h> /* netinet/ip.h */
   21 #include <sys/param.h> /* sys/mbuf.h */
   22 #include <netinet/in_systm.h> /* netinet/ip.h */
   23 
   24 #include <sys/conf.h>
   25 #include <sys/mbuf.h>
   26 #include <sys/errno.h>
   27 #include <sys/fcntl.h>
   28 #include <sys/param.h>
   29 #include <sys/ioctl.h>
   30 #include <netinet/ip.h>
   31 #include <netinet/ip6.h>
   32 #include <net/if_types.h>
   33 #include <machine/stdarg.h>
   34 
   35 #include "if_srt.h"
   36 #include "bpfilter.h"
   37 
   38 /* until we know what to pass to bpfattach.... */
   39 #undef NBPFILTER
   40 #define NBPFILTER 0
   41 
   42 typedef struct srt_rt RT;
   43 typedef struct softc SOFTC;
   44 
   45 struct softc {
   46   struct ifnet intf;    /* XXX interface botch */
   47   int unit;
   48   int nrt;
   49   RT **rts;
   50   unsigned int flags;   /* SSF_* values from if_srt.h */
   51 #define SSF_UCHG (SSF_MTULOCK) /* userland-changeable bits */
   52   unsigned int kflags;  /* bits private to this file */
   53 #define SKF_CDEVOPEN 0x00000001
   54   } ;
   55 
   56 static SOFTC *softcv[SRT_MAXUNIT+1];
   57 static unsigned int global_flags;
   58 
   59 /* Internal routines. */
   60 
   61 static unsigned int ipv4_masks[33]
   62  = { 0x00000000, /* /0 */
   63      0x80000000, 0xc0000000, 0xe0000000, 0xf0000000, /* /1 - /4 */
   64      0xf8000000, 0xfc000000, 0xfe000000, 0xff000000, /* /5 - /8 */
   65      0xff800000, 0xffc00000, 0xffe00000, 0xfff00000, /* /9 - /12 */
   66      0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000, /* /13 - /16 */
   67      0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000, /* /17 - /20 */
   68      0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00, /* /21 - /24 */
   69      0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0, /* /25 - /28 */
   70      0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff  /* /29 - /32 */ };
   71 
   72 static void update_mtu(SOFTC *sc)
   73 {
   74  int mtu;
   75  int i;
   76  RT *r;
   77 
   78  if (sc->flags & SSF_MTULOCK) return;
   79  mtu = 65535;
   80  for (i=sc->nrt-1;i>=0;i--)
   81   { r = sc->rts[i];
   82     if (r->u.dstifp->if_mtu < mtu) mtu = r->u.dstifp->if_mtu;
   83   }
   84  sc->intf.if_mtu = mtu;
   85 }
   86 
   87 static RT *find_rt(SOFTC *sc, int af, ...)
   88 {
   89  int i;
   90  RT *r;
   91  struct in_addr ia;
   92  struct in6_addr ia6;
   93  va_list ap;
   94 
   95  ia.s_addr = 0; ia6.s6_addr[0] = 0; /* shut up incorrect -Wuninitialized */
   96  va_start(ap,af);
   97  switch (af)
   98   { case AF_INET:
   99        ia = va_arg(ap,struct in_addr);
  100        break;
  101     case AF_INET6:
  102        ia6 = va_arg(ap,struct in6_addr);
  103        break;
  104     default:
  105        panic("if_srt find_rt: impossible address family");
  106        break;
  107   }
  108  va_end(ap);
  109  for (i=0;i<sc->nrt;i++)
  110   { r = sc->rts[i];
  111     if (r->af != af) continue;
  112     switch (af)
  113      { case AF_INET:
  114           if ((ia.s_addr & ipv4_masks[r->srcmask]) == r->srcmatch.v4.s_addr) return(r);
  115           break;
  116        case AF_INET6:
  117           if ((r->srcmask >= 8) && bcmp(&ia6,&r->srcmatch.v6,r->srcmask/8)) continue;
  118           if ( (r->srcmask % 8) &&
  119                ( ( ia6.s6_addr[r->srcmask/8] ^
  120                    r->srcmatch.v6.s6_addr[r->srcmask/8] ) &
  121                  0xff & (0xff00 >> (r->srcmask%8)) ) ) continue;
  122           return(r);
  123           break;
  124        default:
  125           panic("if_srt find_rt: impossible address family 2");
  126           break;
  127      }
  128   }
  129  return(0);
  130 }
  131 
  132 /* Network device interface. */
  133 
  134 static int srt_if_ioctl(struct ifnet *intf, u_long cmd, void *data)
  135 {
  136  struct ifaddr *ifa;
  137  int s;
  138  int err;
  139 
  140  err = 0;
  141  s = splnet();
  142  switch (cmd)
  143   { case SIOCSIFADDR:
  144     case SIOCSIFDSTADDR:
  145        ifa = (void *) data;
  146        switch (ifa->ifa_addr->sa_family)
  147         {
  148 #ifdef INET
  149           case AF_INET:
  150 #endif
  151 #ifdef INET6
  152           case AF_INET6:
  153 #endif
  154              break;
  155           default:
  156              err = EAFNOSUPPORT;
  157              break;
  158         }
  159        /* XXX do we need to do more here for either of these? */
  160        break;
  161     case SIOCSIFMTU:
  162     case SIOCGIFMTU:
  163        if ((err = ifioctl_common(intf, cmd, data)) == ENETRESET)
  164              err = 0;
  165        break;
  166     default:
  167        err = EINVAL;
  168        break;
  169   }
  170  splx(s);
  171  return(err);
  172 }
  173 
  174 static int srt_if_output(
  175         struct ifnet *intf,
  176         struct mbuf *m,
  177         const struct sockaddr *to,
  178         struct rtentry *rtp )
  179 {
  180  SOFTC *sc;
  181  RT *r;
  182 
  183  sc = intf->if_softc;
  184  if (! (intf->if_flags & IFF_UP))
  185   { m_freem(m);
  186     return(ENETDOWN);
  187   }
  188  switch (to->sa_family)
  189   {
  190 #ifdef INET
  191     case AF_INET:
  192 #endif
  193         { struct ip *ip;
  194           ip = mtod(m,struct ip *);
  195           r = find_rt(sc,AF_INET,ip->ip_src);
  196         }
  197        break;
  198 #ifdef INET6
  199     case AF_INET6:
  200 #endif
  201         { struct ip6_hdr *ip;
  202           ip = mtod(m,struct ip6_hdr *);
  203           r = find_rt(sc,AF_INET6,ip->ip6_src);
  204         }
  205        break;
  206     default:
  207        IF_DROP(&intf->if_snd);
  208        m_freem(m);
  209        return(EAFNOSUPPORT);
  210        break;
  211   }
  212  /* XXX Do we need to bpf_tap?  Or do higher layers now handle that? */
  213  /* if_gif.c seems to imply the latter. */
  214  intf->if_opackets ++;
  215  if (! r)
  216   { intf->if_oerrors ++;
  217     m_freem(m);
  218     return(0);
  219   }
  220  if (! (m->m_flags & M_PKTHDR))
  221   { printf("srt_if_output no PKTHDR\n");
  222     m_freem(m);
  223     return(0);
  224   }
  225  intf->if_obytes += m->m_pkthdr.len;
  226  if (! (r->u.dstifp->if_flags & IFF_UP))
  227   { m_freem(m);
  228     return(0); /* XXX ENETDOWN? */
  229   }
  230  /* XXX is 0 the right last arg here? */
  231  return((*r->u.dstifp->if_output)(r->u.dstifp,m,&r->dst.sa,0));
  232 }
  233 
  234 static int srt_clone_create(struct if_clone *cl, int unit)
  235 {
  236  SOFTC *sc;
  237 
  238  if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
  239  if (softcv[unit]) return(EBUSY);
  240  sc = malloc(sizeof(SOFTC),M_DEVBUF,M_WAIT);
  241  bzero(&sc->intf,sizeof(sc->intf)); /* XXX */
  242  sc->unit = unit;
  243  sc->nrt = 0;
  244  sc->rts = 0;
  245  sc->flags = 0;
  246  sc->kflags = 0;
  247  if_initname(&sc->intf,cl->ifc_name,unit);
  248  sc->intf.if_softc = sc;
  249  sc->intf.if_mtu = 65535;
  250  sc->intf.if_flags = IFF_POINTOPOINT;
  251  sc->intf.if_type = IFT_OTHER;
  252  sc->intf.if_ioctl = &srt_if_ioctl;
  253  sc->intf.if_output = &srt_if_output;
  254  sc->intf.if_dlt = DLT_RAW;
  255  if_attach(&sc->intf);
  256  if_alloc_sadl(&sc->intf);
  257 #if NBPFILTER > 0 /* see comment near top */
  258  bpfattach(&sc->intf,0/*???*/,0/*???*/);
  259 #endif
  260  softcv[unit] = sc;
  261  return(0);
  262 }
  263 
  264 static int srt_clone_destroy(struct ifnet *intf)
  265 {
  266  SOFTC *sc;
  267 
  268  sc = intf->if_softc;
  269  if ((intf->if_flags & IFF_UP) || (sc->kflags & SKF_CDEVOPEN)) return(EBUSY);
  270 #if NBPFILTER > 0
  271  bpfdetach(intf);
  272 #endif
  273  if_detach(intf);
  274  if ((sc->unit < 0) || (sc->unit > SRT_MAXUNIT))
  275   { panic("srt_clone_destroy: impossible unit %d\n",sc->unit);
  276   }
  277  if (softcv[sc->unit] != sc)
  278   { panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
  279                         sc->unit,(void *)softcv[sc->unit],(void *)sc);
  280   }
  281  softcv[sc->unit] = 0;
  282  free(sc,M_DEVBUF);
  283  return(0);
  284 }
  285 
  286 struct if_clone srt_clone =
  287     IF_CLONE_INITIALIZER("srt",&srt_clone_create,&srt_clone_destroy);
  288 
  289 extern void srtattach(void);
  290 void srtattach(void)
  291 {
  292  int i;
  293 
  294  for (i=SRT_MAXUNIT;i>=0;i--) softcv[i] = 0;
  295  global_flags = 0;
  296  if_clone_attach(&srt_clone);
  297 }
  298 
  299 /* Special-device interface. */
  300 
  301 static int srt_open(dev_t dev, int flag, int mode, struct lwp *l)
  302 {
  303  int unit;
  304  SOFTC *sc;
  305 
  306  unit = minor(dev);
  307  if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
  308  sc = softcv[unit];
  309  if (! sc) return(ENXIO);
  310  sc->kflags |= SKF_CDEVOPEN;
  311  return(0);
  312 }
  313 
  314 static int srt_close(dev_t dev, int flag, int mode, struct lwp *l)
  315 {
  316  int unit;
  317  SOFTC *sc;
  318 
  319  unit = minor(dev);
  320  if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
  321  sc = softcv[unit];
  322  if (! sc) return(ENXIO);
  323  sc->kflags &= ~SKF_CDEVOPEN;
  324  return(0);
  325 }
  326 
  327 static int srt_ioctl(
  328         dev_t dev,
  329         u_long cmd,
  330         void *data,
  331         int flag,
  332         struct lwp *l )
  333 {
  334  SOFTC *sc;
  335  RT *dr;
  336  RT *scr;
  337  struct ifnet *intf;
  338  char nbuf[IFNAMSIZ];
  339 
  340  sc = softcv[minor(dev)];
  341  if (! sc) panic("srt_ioctl: softc disappeared");
  342  switch (cmd)
  343   { case SRT_GETNRT:
  344        if (! (flag & FREAD)) return(EBADF);
  345        *(unsigned int *)data = sc->nrt;
  346        return(0);
  347        break;
  348     case SRT_GETRT:
  349        if (! (flag & FREAD)) return(EBADF);
  350        dr = (RT *) data;
  351        if (dr->inx >= sc->nrt) return(EDOM);
  352        scr = sc->rts[dr->inx];
  353        dr->af = scr->af;
  354        dr->srcmatch = scr->srcmatch;
  355        dr->srcmask = scr->srcmask;
  356        strncpy(&dr->u.dstifn[0],&scr->u.dstifp->if_xname[0],IFNAMSIZ);
  357        memcpy(&dr->dst,&scr->dst,scr->dst.sa.sa_len);
  358        return(0);
  359        break;
  360     case SRT_SETRT:
  361        if (! (flag & FWRITE)) return(EBADF);
  362        dr = (RT *) data;
  363        if (dr->inx > sc->nrt) return(EDOM);
  364        strncpy(&nbuf[0],&dr->u.dstifn[0],IFNAMSIZ);
  365        nbuf[IFNAMSIZ-1] = '\0';
  366        if (dr->dst.sa.sa_family != dr->af) return(EIO);
  367        switch (dr->af)
  368         {
  369 #ifdef INET
  370           case AF_INET:
  371              if (dr->dst.sa.sa_len != sizeof(dr->dst.sin)) return(EIO);
  372              if (dr->srcmask > 32) return(EIO);
  373              break;
  374 #endif
  375 #ifdef INET6
  376           case AF_INET6:
  377              if (dr->dst.sa.sa_len != sizeof(dr->dst.sin6)) return(EIO);
  378              if (dr->srcmask > 128) return(EIO);
  379              break;
  380 #endif
  381              break;
  382           default:
  383              return(EAFNOSUPPORT);
  384              break;
  385         }
  386        intf = ifunit(&nbuf[0]);
  387        if (intf == 0) return(ENXIO); /* needs translation */
  388        if (dr->inx == sc->nrt)
  389         { RT **tmp;
  390           tmp = malloc((sc->nrt+1)*sizeof(*tmp),M_DEVBUF,M_WAITOK);
  391           if (tmp == 0) return(ENOBUFS);
  392           tmp[sc->nrt] = 0;
  393           if (sc->nrt > 0)
  394            { memcpy(tmp,sc->rts,sc->nrt*sizeof(*tmp));
  395              free(sc->rts,M_DEVBUF);
  396            }
  397           sc->rts = tmp;
  398           sc->nrt ++;
  399         }
  400        scr = sc->rts[dr->inx];
  401        if (scr == 0)
  402         { scr = malloc(sizeof(RT),M_DEVBUF,M_WAITOK);
  403           if (scr == 0) return(ENOBUFS);
  404           scr->inx = dr->inx;
  405           scr->af = AF_UNSPEC;
  406           sc->rts[dr->inx] = scr;
  407         }
  408        scr->af = dr->af;
  409        scr->srcmatch = dr->srcmatch;
  410        scr->srcmask = dr->srcmask;
  411        scr->u.dstifp = intf;
  412        memcpy(&scr->dst,&dr->dst,dr->dst.sa.sa_len);
  413        update_mtu(sc);
  414        return(0);
  415        break;
  416     case SRT_DELRT:
  417         { unsigned int i;
  418           if (! (flag & FWRITE)) return(EBADF);
  419           i = *(unsigned int *)data;
  420           if (i >= sc->nrt) return(EDOM);
  421           scr = sc->rts[i];
  422           sc->rts[i] = 0;
  423           free(scr,M_DEVBUF);
  424           sc->nrt --;
  425           if (i < sc->nrt) memcpy(sc->rts+i,sc->rts+i+1,(sc->nrt-i)*sizeof(*sc->rts));
  426           if (sc->nrt == 0)
  427            { free(sc->rts,M_DEVBUF);
  428              sc->rts = 0;
  429              sc->intf.if_flags &= ~IFF_UP;
  430            }
  431         }
  432        update_mtu(sc);
  433        return(0);
  434        break;
  435     case SRT_SFLAGS:
  436         { unsigned int f;
  437           if (! (flag & FWRITE)) return(EBADF);
  438           f = *(unsigned int *)data & SSF_UCHG;
  439           global_flags = (global_flags & ~SSF_UCHG) | (f & SSF_GLOBAL);
  440           sc->flags = (sc->flags & ~SSF_UCHG) | (f & ~SSF_GLOBAL);
  441         }
  442        return(0);
  443        break;
  444     case SRT_GFLAGS:
  445        if (! (flag & FREAD)) return(EBADF);
  446        *(unsigned int *)data = sc->flags | global_flags;
  447        return(0);
  448        break;
  449     case SRT_SGFLAGS:
  450         { unsigned int o;
  451           unsigned int n;
  452           if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD)) return(EBADF);
  453           o = sc->flags | global_flags;
  454           n = *(unsigned int *)data & SSF_UCHG;
  455           global_flags = (global_flags & ~SSF_UCHG) | (n & SSF_GLOBAL);
  456           sc->flags = (sc->flags & ~SSF_UCHG) | (n & ~SSF_GLOBAL);
  457           *(unsigned int *)data = o;
  458         }
  459        return(0);
  460        break;
  461     case SRT_DEBUG:
  462        return(0);
  463        break;
  464   }
  465  return(ENOTTY);
  466 }
  467 
  468 const struct cdevsw srt_cdevsw
  469  = { &srt_open,
  470      &srt_close,
  471      nullread,
  472      nullwrite,
  473      &srt_ioctl,
  474      nullstop,
  475      notty,
  476      nullpoll,
  477      nommap,
  478      nullkqfilter,
  479      D_OTHER };

Cache object: a9723cf6d3a28c4877eedeb9eda447d8


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