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