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