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