Home | History | Annotate | Line # | Download | only in net
if_srt.c revision 1.9
      1 /* $NetBSD: if_srt.c,v 1.9 2008/11/07 00:20:13 dyoung 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.9 2008/11/07 00:20:13 dyoung 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 SIOCINITIFADDR:
    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     default:
    162        if ((err = ifioctl_common(intf, cmd, data)) == ENETRESET)
    163              err = 0;
    164        break;
    165   }
    166  splx(s);
    167  return(err);
    168 }
    169 
    170 static int srt_if_output(
    171 	struct ifnet *intf,
    172 	struct mbuf *m,
    173 	const struct sockaddr *to,
    174 	struct rtentry *rtp )
    175 {
    176  SOFTC *sc;
    177  RT *r;
    178 
    179  sc = intf->if_softc;
    180  if (! (intf->if_flags & IFF_UP))
    181   { m_freem(m);
    182     return(ENETDOWN);
    183   }
    184  switch (to->sa_family)
    185   {
    186 #ifdef INET
    187     case AF_INET:
    188 #endif
    189 	{ struct ip *ip;
    190 	  ip = mtod(m,struct ip *);
    191 	  r = find_rt(sc,AF_INET,ip->ip_src);
    192 	}
    193        break;
    194 #ifdef INET6
    195     case AF_INET6:
    196 #endif
    197 	{ struct ip6_hdr *ip;
    198 	  ip = mtod(m,struct ip6_hdr *);
    199 	  r = find_rt(sc,AF_INET6,ip->ip6_src);
    200 	}
    201        break;
    202     default:
    203        IF_DROP(&intf->if_snd);
    204        m_freem(m);
    205        return(EAFNOSUPPORT);
    206        break;
    207   }
    208  /* XXX Do we need to bpf_tap?  Or do higher layers now handle that? */
    209  /* if_gif.c seems to imply the latter. */
    210  intf->if_opackets ++;
    211  if (! r)
    212   { intf->if_oerrors ++;
    213     m_freem(m);
    214     return(0);
    215   }
    216  if (! (m->m_flags & M_PKTHDR))
    217   { printf("srt_if_output no PKTHDR\n");
    218     m_freem(m);
    219     return(0);
    220   }
    221  intf->if_obytes += m->m_pkthdr.len;
    222  if (! (r->u.dstifp->if_flags & IFF_UP))
    223   { m_freem(m);
    224     return(0); /* XXX ENETDOWN? */
    225   }
    226  /* XXX is 0 the right last arg here? */
    227  return((*r->u.dstifp->if_output)(r->u.dstifp,m,&r->dst.sa,0));
    228 }
    229 
    230 static int srt_clone_create(struct if_clone *cl, int unit)
    231 {
    232  SOFTC *sc;
    233 
    234  if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
    235  if (softcv[unit]) return(EBUSY);
    236  sc = malloc(sizeof(SOFTC),M_DEVBUF,M_WAIT);
    237  bzero(&sc->intf,sizeof(sc->intf)); /* XXX */
    238  sc->unit = unit;
    239  sc->nrt = 0;
    240  sc->rts = 0;
    241  sc->flags = 0;
    242  sc->kflags = 0;
    243  if_initname(&sc->intf,cl->ifc_name,unit);
    244  sc->intf.if_softc = sc;
    245  sc->intf.if_mtu = 65535;
    246  sc->intf.if_flags = IFF_POINTOPOINT;
    247  sc->intf.if_type = IFT_OTHER;
    248  sc->intf.if_ioctl = &srt_if_ioctl;
    249  sc->intf.if_output = &srt_if_output;
    250  sc->intf.if_dlt = DLT_RAW;
    251  if_attach(&sc->intf);
    252  if_alloc_sadl(&sc->intf);
    253 #if NBPFILTER > 0 /* see comment near top */
    254  bpfattach(&sc->intf,0/*???*/,0/*???*/);
    255 #endif
    256  softcv[unit] = sc;
    257  return(0);
    258 }
    259 
    260 static int srt_clone_destroy(struct ifnet *intf)
    261 {
    262  SOFTC *sc;
    263 
    264  sc = intf->if_softc;
    265  if ((intf->if_flags & IFF_UP) || (sc->kflags & SKF_CDEVOPEN)) return(EBUSY);
    266 #if NBPFILTER > 0
    267  bpfdetach(intf);
    268 #endif
    269  if_detach(intf);
    270  if ((sc->unit < 0) || (sc->unit > SRT_MAXUNIT))
    271   { panic("srt_clone_destroy: impossible unit %d\n",sc->unit);
    272   }
    273  if (softcv[sc->unit] != sc)
    274   { panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
    275 			sc->unit,(void *)softcv[sc->unit],(void *)sc);
    276   }
    277  softcv[sc->unit] = 0;
    278  free(sc,M_DEVBUF);
    279  return(0);
    280 }
    281 
    282 struct if_clone srt_clone =
    283     IF_CLONE_INITIALIZER("srt",&srt_clone_create,&srt_clone_destroy);
    284 
    285 extern void srtattach(void);
    286 void srtattach(void)
    287 {
    288  int i;
    289 
    290  for (i=SRT_MAXUNIT;i>=0;i--) softcv[i] = 0;
    291  global_flags = 0;
    292  if_clone_attach(&srt_clone);
    293 }
    294 
    295 /* Special-device interface. */
    296 
    297 static int srt_open(dev_t dev, int flag, int mode, struct lwp *l)
    298 {
    299  int unit;
    300  SOFTC *sc;
    301 
    302  unit = minor(dev);
    303  if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
    304  sc = softcv[unit];
    305  if (! sc) return(ENXIO);
    306  sc->kflags |= SKF_CDEVOPEN;
    307  return(0);
    308 }
    309 
    310 static int srt_close(dev_t dev, int flag, int mode, struct lwp *l)
    311 {
    312  int unit;
    313  SOFTC *sc;
    314 
    315  unit = minor(dev);
    316  if ((unit < 0) || (unit > SRT_MAXUNIT)) return(ENXIO);
    317  sc = softcv[unit];
    318  if (! sc) return(ENXIO);
    319  sc->kflags &= ~SKF_CDEVOPEN;
    320  return(0);
    321 }
    322 
    323 static int srt_ioctl(
    324 	dev_t dev,
    325 	u_long cmd,
    326 	void *data,
    327 	int flag,
    328 	struct lwp *l )
    329 {
    330  SOFTC *sc;
    331  RT *dr;
    332  RT *scr;
    333  struct ifnet *intf;
    334  char nbuf[IFNAMSIZ];
    335 
    336  sc = softcv[minor(dev)];
    337  if (! sc) panic("srt_ioctl: softc disappeared");
    338  switch (cmd)
    339   { case SRT_GETNRT:
    340        if (! (flag & FREAD)) return(EBADF);
    341        *(unsigned int *)data = sc->nrt;
    342        return(0);
    343        break;
    344     case SRT_GETRT:
    345        if (! (flag & FREAD)) return(EBADF);
    346        dr = (RT *) data;
    347        if (dr->inx >= sc->nrt) return(EDOM);
    348        scr = sc->rts[dr->inx];
    349        dr->af = scr->af;
    350        dr->srcmatch = scr->srcmatch;
    351        dr->srcmask = scr->srcmask;
    352        strncpy(&dr->u.dstifn[0],&scr->u.dstifp->if_xname[0],IFNAMSIZ);
    353        memcpy(&dr->dst,&scr->dst,scr->dst.sa.sa_len);
    354        return(0);
    355        break;
    356     case SRT_SETRT:
    357        if (! (flag & FWRITE)) return(EBADF);
    358        dr = (RT *) data;
    359        if (dr->inx > sc->nrt) return(EDOM);
    360        strncpy(&nbuf[0],&dr->u.dstifn[0],IFNAMSIZ);
    361        nbuf[IFNAMSIZ-1] = '\0';
    362        if (dr->dst.sa.sa_family != dr->af) return(EIO);
    363        switch (dr->af)
    364 	{
    365 #ifdef INET
    366 	  case AF_INET:
    367 	     if (dr->dst.sa.sa_len != sizeof(dr->dst.sin)) return(EIO);
    368 	     if (dr->srcmask > 32) return(EIO);
    369 	     break;
    370 #endif
    371 #ifdef INET6
    372 	  case AF_INET6:
    373 	     if (dr->dst.sa.sa_len != sizeof(dr->dst.sin6)) return(EIO);
    374 	     if (dr->srcmask > 128) return(EIO);
    375 	     break;
    376 #endif
    377 	     break;
    378 	  default:
    379 	     return(EAFNOSUPPORT);
    380 	     break;
    381 	}
    382        intf = ifunit(&nbuf[0]);
    383        if (intf == 0) return(ENXIO); /* needs translation */
    384        if (dr->inx == sc->nrt)
    385 	{ RT **tmp;
    386 	  tmp = malloc((sc->nrt+1)*sizeof(*tmp),M_DEVBUF,M_WAITOK);
    387 	  if (tmp == 0) return(ENOBUFS);
    388 	  tmp[sc->nrt] = 0;
    389 	  if (sc->nrt > 0)
    390 	   { memcpy(tmp,sc->rts,sc->nrt*sizeof(*tmp));
    391 	     free(sc->rts,M_DEVBUF);
    392 	   }
    393 	  sc->rts = tmp;
    394 	  sc->nrt ++;
    395 	}
    396        scr = sc->rts[dr->inx];
    397        if (scr == 0)
    398 	{ scr = malloc(sizeof(RT),M_DEVBUF,M_WAITOK);
    399 	  if (scr == 0) return(ENOBUFS);
    400 	  scr->inx = dr->inx;
    401 	  scr->af = AF_UNSPEC;
    402 	  sc->rts[dr->inx] = scr;
    403 	}
    404        scr->af = dr->af;
    405        scr->srcmatch = dr->srcmatch;
    406        scr->srcmask = dr->srcmask;
    407        scr->u.dstifp = intf;
    408        memcpy(&scr->dst,&dr->dst,dr->dst.sa.sa_len);
    409        update_mtu(sc);
    410        return(0);
    411        break;
    412     case SRT_DELRT:
    413 	{ unsigned int i;
    414 	  if (! (flag & FWRITE)) return(EBADF);
    415 	  i = *(unsigned int *)data;
    416 	  if (i >= sc->nrt) return(EDOM);
    417 	  scr = sc->rts[i];
    418 	  sc->rts[i] = 0;
    419 	  free(scr,M_DEVBUF);
    420 	  sc->nrt --;
    421 	  if (i < sc->nrt) memcpy(sc->rts+i,sc->rts+i+1,(sc->nrt-i)*sizeof(*sc->rts));
    422 	  if (sc->nrt == 0)
    423 	   { free(sc->rts,M_DEVBUF);
    424 	     sc->rts = 0;
    425 	     sc->intf.if_flags &= ~IFF_UP;
    426 	   }
    427 	}
    428        update_mtu(sc);
    429        return(0);
    430        break;
    431     case SRT_SFLAGS:
    432 	{ unsigned int f;
    433 	  if (! (flag & FWRITE)) return(EBADF);
    434 	  f = *(unsigned int *)data & SSF_UCHG;
    435 	  global_flags = (global_flags & ~SSF_UCHG) | (f & SSF_GLOBAL);
    436 	  sc->flags = (sc->flags & ~SSF_UCHG) | (f & ~SSF_GLOBAL);
    437 	}
    438        return(0);
    439        break;
    440     case SRT_GFLAGS:
    441        if (! (flag & FREAD)) return(EBADF);
    442        *(unsigned int *)data = sc->flags | global_flags;
    443        return(0);
    444        break;
    445     case SRT_SGFLAGS:
    446 	{ unsigned int o;
    447 	  unsigned int n;
    448 	  if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD)) return(EBADF);
    449 	  o = sc->flags | global_flags;
    450 	  n = *(unsigned int *)data & SSF_UCHG;
    451 	  global_flags = (global_flags & ~SSF_UCHG) | (n & SSF_GLOBAL);
    452 	  sc->flags = (sc->flags & ~SSF_UCHG) | (n & ~SSF_GLOBAL);
    453 	  *(unsigned int *)data = o;
    454 	}
    455        return(0);
    456        break;
    457     case SRT_DEBUG:
    458        return(0);
    459        break;
    460   }
    461  return(ENOTTY);
    462 }
    463 
    464 const struct cdevsw srt_cdevsw
    465  = { &srt_open,
    466      &srt_close,
    467      nullread,
    468      nullwrite,
    469      &srt_ioctl,
    470      nullstop,
    471      notty,
    472      nullpoll,
    473      nommap,
    474      nullkqfilter,
    475      D_OTHER };
    476