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