xref: /netbsd-src/sys/net/if_srt.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: if_srt.c,v 1.27 2017/10/23 09:32:55 msaitoh 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.27 2017/10/23 09:32:55 msaitoh 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 	int rv;
253 
254 	if (unit < 0 || unit > SRT_MAXUNIT)
255 		return ENXIO;
256 	if (softcv[unit])
257 		return EBUSY;
258 	sc = malloc(sizeof(struct srt_softc), M_DEVBUF, M_WAITOK | M_ZERO);
259 	sc->unit = unit;
260 	sc->nrt = 0;
261 	sc->rts = 0;
262 	sc->flags = 0;
263 	sc->kflags = 0;
264 	if_initname(&sc->intf,cl->ifc_name,unit);
265 	sc->intf.if_softc = sc;
266 	sc->intf.if_mtu = 65535;
267 	sc->intf.if_flags = IFF_POINTOPOINT;
268 	sc->intf.if_type = IFT_OTHER;
269 	sc->intf.if_ioctl = &srt_if_ioctl;
270 	sc->intf.if_output = &srt_if_output;
271 	sc->intf.if_dlt = DLT_RAW;
272 	rv = if_attach(&sc->intf);
273 	if (rv != 0) {
274 		aprint_error("%s: if_initialize failed(%d)\n",
275 		    sc->intf.if_xname, rv);
276 		free(sc, M_DEVBUF);
277 		return rv;
278 	}
279 	if_alloc_sadl(&sc->intf);
280 #ifdef BPFILTER_NOW_AVAILABLE
281 	bpf_attach(&sc->intf, 0, 0);
282 #endif
283 	softcv[unit] = sc;
284 	atomic_inc_uint(&srt_count);
285 	return 0;
286 }
287 
288 static int
289 srt_clone_destroy(struct ifnet *ifp)
290 {
291 	struct srt_softc *sc;
292 
293 	sc = ifp->if_softc;
294 	if ((ifp->if_flags & IFF_UP) || (sc->kflags & SKF_CDEVOPEN))
295 		return EBUSY;
296 #ifdef BPFILTER_NOW_AVAILABLE
297 	bpf_detach(ifp);
298 #endif
299 	if_detach(ifp);
300 	if (sc->unit < 0 || sc->unit > SRT_MAXUNIT) {
301 		panic("srt_clone_destroy: impossible unit %d\n",sc->unit);
302 	}
303 	if (softcv[sc->unit] != sc) {
304 		panic("srt_clone_destroy: bad backpointer ([%d]=%p not %p)\n",
305 		sc->unit, (void *)softcv[sc->unit], (void *)sc);
306 	}
307 	softcv[sc->unit] = 0;
308 	free(sc, M_DEVBUF);
309 	atomic_inc_uint(&srt_count);
310 	return 0;
311 }
312 
313 struct if_clone srt_clone =
314     IF_CLONE_INITIALIZER("srt", &srt_clone_create, &srt_clone_destroy);
315 
316 void
317 srtattach(int n)
318 {
319 
320 	/*
321 	 * Nothing to do here, initialization is handled by the
322 	 * module initialization code in srtinit() below).
323 	 */
324 }
325 
326 static void
327 srtinit(void)
328 {
329 	int i;
330 
331 	for (i = SRT_MAXUNIT; i >= 0; i--)
332 		softcv[i] = 0;
333 	global_flags = 0;
334 	if_clone_attach(&srt_clone);
335 }
336 
337 static int
338 srtdetach(void)
339 {
340 	int error = 0;
341 	int i;
342 
343 	for (i = SRT_MAXUNIT; i >= 0; i--)
344 		if(softcv[i]) {
345 			error = EBUSY;
346 			break;
347 		}
348 
349 	if (error == 0)
350 		if_clone_detach(&srt_clone);
351 
352 	return error;
353 }
354 
355 /* Special-device interface. */
356 
357 static int
358 srt_open(dev_t dev, int flag, int mode, struct lwp *l)
359 {
360 	int unit;
361 	struct srt_softc *sc;
362 
363 	unit = minor(dev);
364 	if (unit < 0 || unit > SRT_MAXUNIT)
365 		return ENXIO;
366 	sc = softcv[unit];
367 	if (! sc)
368 		return ENXIO;
369 	sc->kflags |= SKF_CDEVOPEN;
370 	return 0;
371 }
372 
373 static int
374 srt_close(dev_t dev, int flag, int mode, struct lwp *l)
375 {
376 	int unit;
377 	struct srt_softc *sc;
378 
379 	unit = minor(dev);
380 	if (unit < 0 || unit > SRT_MAXUNIT)
381 		return ENXIO;
382 	sc = softcv[unit];
383 	if (! sc)
384 		return ENXIO;
385 	sc->kflags &= ~SKF_CDEVOPEN;
386 	return 0;
387 }
388 
389 static int
390 srt_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
391 {
392 	unsigned int f, i, n, o;
393 	struct srt_softc *sc;
394 	struct srt_rt *dr;
395 	struct srt_rt *scr;
396 	struct ifnet *ifp;
397 	char nbuf[IFNAMSIZ];
398 
399 	sc = softcv[minor(dev)];
400 	if (! sc)
401 		panic("srt_ioctl: softc disappeared");
402 	switch (cmd) {
403 	case SRT_GETNRT:
404 		if (! (flag & FREAD))
405 			return EBADF;
406 		*(unsigned int *)data = sc->nrt;
407 		return 0;
408 	case SRT_GETRT:
409 		if (! (flag & FREAD))
410 			return EBADF;
411 		dr = (struct srt_rt *) data;
412 		if (dr->inx >= sc->nrt)
413 			return EDOM;
414 		scr = sc->rts[dr->inx];
415 		dr->af = scr->af;
416 		dr->srcmatch = scr->srcmatch;
417 		dr->srcmask = scr->srcmask;
418 		strlcpy(&dr->u.dstifn[0], &scr->u.dstifp->if_xname[0],
419 		    IFNAMSIZ);
420 		memcpy(&dr->dst, &scr->dst, scr->dst.sa.sa_len);
421 		return 0;
422 	case SRT_SETRT:
423 		if (! (flag & FWRITE))
424 			return EBADF;
425 		dr = (struct srt_rt *) data;
426 		if (dr->inx > sc->nrt)
427 			return EDOM;
428 		strlcpy(&nbuf[0], &dr->u.dstifn[0], IFNAMSIZ);
429 		nbuf[IFNAMSIZ-1] = '\0';
430 		if (dr->dst.sa.sa_family != dr->af)
431 			return EIO;
432 		switch (dr->af) {
433 #ifdef INET
434 		case AF_INET:
435 			if (dr->dst.sa.sa_len != sizeof(dr->dst.sin))
436 				return EIO;
437 			if (dr->srcmask > 32)
438 				return EIO;
439 		break;
440 #endif
441 #ifdef INET6
442 		case AF_INET6:
443 			if (dr->dst.sa.sa_len != sizeof(dr->dst.sin6))
444 				return EIO;
445 			if (dr->srcmask > 128)
446 				return EIO;
447 		break;
448 #endif
449 		default:
450 			return EAFNOSUPPORT;
451 		}
452 		ifp = ifunit(&nbuf[0]);
453 		if (ifp == 0)
454 			return ENXIO; /* needs translation */
455 		if (dr->inx == sc->nrt) {
456 			struct srt_rt **tmp;
457 			tmp = malloc((sc->nrt+1)*sizeof(*tmp), M_DEVBUF,
458 			    M_WAITOK);
459 			if (tmp == 0)
460 				return ENOBUFS;
461 			tmp[sc->nrt] = 0;
462 			if (sc->nrt > 0) {
463 				memcpy(tmp, sc->rts, sc->nrt*sizeof(*tmp));
464 				free(sc->rts, M_DEVBUF);
465 			}
466 			sc->rts = tmp;
467 			sc->nrt ++;
468 		}
469 		scr = sc->rts[dr->inx];
470 		if (scr == 0) {
471 			scr = malloc(sizeof(struct srt_rt), M_DEVBUF,M_WAITOK);
472 			if (scr == 0)
473 				return ENOBUFS;
474 			scr->inx = dr->inx;
475 			scr->af = AF_UNSPEC;
476 			sc->rts[dr->inx] = scr;
477 		}
478 		scr->af = dr->af;
479 		scr->srcmatch = dr->srcmatch;
480 		scr->srcmask = dr->srcmask;
481 		scr->u.dstifp = ifp;
482 		memcpy(&scr->dst,&dr->dst,dr->dst.sa.sa_len);
483 		if (dr->af == AF_INET6)
484 			in6_setzoneid(&scr->dst.sin6.sin6_addr, ifp->if_index);
485 		update_mtu(sc);
486 		return 0;
487 	case SRT_DELRT:
488 		if (! (flag & FWRITE))
489 			return EBADF;
490 		i = *(unsigned int *)data;
491 		if (i >= sc->nrt)
492 			return EDOM;
493 		scr = sc->rts[i];
494 		sc->rts[i] = 0;
495 		free(scr, M_DEVBUF);
496 		sc->nrt--;
497 		if (i < sc->nrt) {
498 			memcpy(sc->rts+i, sc->rts+i+1,
499 			    (sc->nrt-i) * sizeof(*sc->rts));
500 		}
501 		if (sc->nrt == 0) {
502 			free(sc->rts, M_DEVBUF);
503 			sc->rts = 0;
504 			sc->intf.if_flags &= ~IFF_UP;
505 		}
506 		update_mtu(sc);
507 		return 0;
508 	case SRT_SFLAGS:
509 		if (! (flag & FWRITE))
510 			return EBADF;
511 		f = *(unsigned int *)data & SSF_UCHG;
512 		global_flags = (global_flags & ~SSF_UCHG) | (f & SSF_GLOBAL);
513 		sc->flags = (sc->flags & ~SSF_UCHG) | (f & ~SSF_GLOBAL);
514 		return 0;
515 	case SRT_GFLAGS:
516 		if (! (flag & FREAD))
517 			return EBADF;
518 		*(unsigned int *)data = sc->flags | global_flags;
519 		return 0;
520 	case SRT_SGFLAGS:
521 		if ((flag & (FWRITE | FREAD)) != (FWRITE | FREAD))
522 			return EBADF;
523 		o = sc->flags | global_flags;
524 		n = *(unsigned int *)data & SSF_UCHG;
525 		global_flags = (global_flags & ~SSF_UCHG) | (n & SSF_GLOBAL);
526 		sc->flags = (sc->flags & ~SSF_UCHG) | (n & ~SSF_GLOBAL);
527 		*(unsigned int *)data = o;
528 		return 0;
529 	case SRT_DEBUG:
530 		return 0;
531 		break;
532 	}
533 	return ENOTTY;
534 }
535 
536 const struct cdevsw srt_cdevsw = {
537 	.d_open = srt_open,
538 	.d_close = srt_close,
539 	.d_read = nullread,
540 	.d_write = nullwrite,
541 	.d_ioctl = srt_ioctl,
542 	.d_stop = nullstop,
543 	.d_tty = notty,
544 	.d_poll = nullpoll,
545 	.d_mmap = nommap,
546 	.d_kqfilter = nullkqfilter,
547 	.d_discard = nodiscard,
548 	.d_flag = D_OTHER
549 };
550 
551 /*
552  * Module infrastructure
553  */
554 #include "if_module.h"
555 
556 IF_MODULE(MODULE_CLASS_DRIVER, srt, "")
557