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