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