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