if_tun.c revision 1.8 1 /*
2 * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
3 * Nottingham University 1987.
4 *
5 * This source may be freely distributed, however I would be interested
6 * in any changes that are made.
7 *
8 * This driver takes packets off the IP i/f and hands them up to a
9 * user process to have it's wicked way with. This driver has it's
10 * roots in a similar driver written by Phil Cockcroft (formerly) at
11 * UCL. This driver is based much more on read/write/select mode of
12 * operation though.
13 *
14 * $Id: if_tun.c,v 1.8 1993/12/13 05:06:33 deraadt Exp $
15 */
16
17 #include "tun.h"
18 #if NTUN > 0
19
20 #include <sys/param.h>
21 #include <sys/proc.h>
22 #include <sys/systm.h>
23 #include <sys/mbuf.h>
24 #include <sys/buf.h>
25 #include <sys/protosw.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <sys/errno.h>
29 #include <sys/syslog.h>
30 #include <sys/select.h>
31 #include <sys/file.h>
32
33 #include <net/if.h>
34 #include <net/netisr.h>
35 #include <net/route.h>
36
37 #ifdef INET
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/in_var.h>
41 #include <netinet/ip.h>
42 #include <netinet/if_ether.h>
43 #endif
44
45 #ifdef NS
46 #include <netns/ns.h>
47 #include <netns/ns_if.h>
48 #endif
49
50 #include "bpfilter.h"
51 #if NBPFILTER > 0
52 #include <sys/time.h>
53 #include <net/bpf.h>
54 #endif
55
56 #include <net/if_tun.h>
57
58 #define TUNDEBUG if (tundebug) printf
59 int tundebug = 0;
60
61 struct tun_softc tunctl[NTUN];
62 extern int ifqmaxlen;
63
64 int tunopen __P((dev_t, int, int, struct proc *));
65 int tunclose __P((dev_t, int));
66 int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *));
67 int tunread __P((dev_t, struct uio *));
68 int tunwrite __P((dev_t, struct uio *));
69 int tuncioctl __P((dev_t, int, caddr_t, int));
70 int tunioctl __P((struct ifnet *, int, caddr_t, int));
71 int tunselect __P((dev_t, int));
72 void tunattach __P((int));
73
74 static int tuninit __P((int));
75
76 void
77 tunattach(unused)
78 int unused;
79 {
80 register int i;
81 struct ifnet *ifp;
82 struct sockaddr_in *sin;
83
84 for (i = 0; i < NTUN; i++) {
85 tunctl[i].tun_flags = TUN_INITED;
86
87 ifp = &tunctl[i].tun_if;
88 ifp->if_unit = i;
89 ifp->if_name = "tun";
90 ifp->if_mtu = TUNMTU;
91 ifp->if_ioctl = tunioctl;
92 ifp->if_output = tunoutput;
93 ifp->if_flags = IFF_POINTOPOINT;
94 ifp->if_snd.ifq_maxlen = ifqmaxlen;
95 ifp->if_collisions = 0;
96 ifp->if_ierrors = 0;
97 ifp->if_oerrors = 0;
98 ifp->if_ipackets = 0;
99 ifp->if_opackets = 0;
100 if_attach(ifp);
101 #if NBPFILTER > 0
102 bpfattach(&tunctl[i].tun_bpf, ifp, DLT_NULL, sizeof(u_int));
103 #endif
104 }
105 }
106
107 /*
108 * tunnel open - must be superuser & the device must be
109 * configured in
110 */
111 int
112 tunopen(dev, flag, mode, p)
113 dev_t dev;
114 int flag, mode;
115 struct proc *p;
116 {
117 struct ifnet *ifp;
118 struct tun_softc *tp;
119 register int unit, error;
120
121 if (error = suser(p->p_ucred, &p->p_acflag))
122 return (error);
123
124 if ((unit = minor(dev)) >= NTUN)
125 return (ENXIO);
126 tp = &tunctl[unit];
127 if (tp->tun_flags & TUN_OPEN)
128 return ENXIO;
129 ifp = &tp->tun_if;
130 tp->tun_flags |= TUN_OPEN;
131 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
132 return (0);
133 }
134
135 /*
136 * tunclose - close the device - mark i/f down & delete
137 * routing info
138 */
139 int
140 tunclose(dev, flag)
141 dev_t dev;
142 int flag;
143 {
144 register int unit = minor(dev), s;
145 struct tun_softc *tp = &tunctl[unit];
146 struct ifnet *ifp = &tp->tun_if;
147 struct mbuf *m;
148
149 tp->tun_flags &= TUN_OPEN;
150
151 /*
152 * junk all pending output
153 */
154 do {
155 s = splimp();
156 IF_DEQUEUE(&ifp->if_snd, m);
157 splx(s);
158 if (m)
159 m_freem(m);
160 } while (m);
161
162 if (ifp->if_flags & IFF_UP) {
163 s = splimp();
164 if_down(ifp);
165 if (ifp->if_flags & IFF_RUNNING) {
166 rtinit(ifp->if_addrlist, (int)RTM_DELETE,
167 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
168 }
169 splx(s);
170 }
171 tp->tun_pgrp = 0;
172 selwakeup(&tp->tun_rsel);
173
174 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
175 return (0);
176 }
177
178 static int
179 tuninit(unit)
180 int unit;
181 {
182 struct tun_softc *tp = &tunctl[unit];
183 struct ifnet *ifp = &tp->tun_if;
184 register struct ifaddr *ifa;
185
186 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
187
188 ifp->if_flags |= IFF_UP | IFF_RUNNING;
189
190 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
191 struct sockaddr_in *si;
192
193 si = (struct sockaddr_in *)ifa->ifa_addr;
194 if (si && si->sin_addr.s_addr)
195 tp->tun_flags |= TUN_IASET;
196
197 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
198 if (si && si->sin_addr.s_addr)
199 tp->tun_flags |= TUN_DSTADDR;
200 }
201
202 return 0;
203 }
204
205 /*
206 * Process an ioctl request.
207 */
208 int
209 tunioctl(ifp, cmd, data, flag)
210 struct ifnet *ifp;
211 int cmd;
212 caddr_t data;
213 int flag;
214 {
215 struct tun_softc *tp = &tunctl[ifp->if_unit];
216 int error = 0, s;
217
218 s = splimp();
219 switch(cmd) {
220 case SIOCSIFADDR:
221 tuninit(ifp->if_unit);
222 break;
223 case SIOCSIFDSTADDR:
224 tp->tun_flags |= TUN_DSTADDR;
225 TUNDEBUG("%s%d: destination address set\n", ifp->if_name,
226 ifp->if_unit);
227 break;
228 default:
229 error = EINVAL;
230 }
231 splx(s);
232 return (error);
233 }
234
235 /*
236 * tunoutput - queue packets from higher level ready to put out.
237 */
238 int
239 tunoutput(ifp, m0, dst)
240 struct ifnet *ifp;
241 struct mbuf *m0;
242 struct sockaddr *dst;
243 {
244 struct tun_softc *tp = &tunctl[ifp->if_unit];
245 struct proc *p;
246 int s;
247
248 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
249
250 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
251 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
252 ifp->if_unit, tp->tun_flags);
253 m_freem (m0);
254 return EHOSTDOWN;
255 }
256
257 #if NBPFILTER > 0
258 if (tp->tun_bpf) {
259 /*
260 * We need to prepend the address family as
261 * a four byte field. Cons up a dummy header
262 * to pacify bpf. This is safe because bpf
263 * will only read from the mbuf (i.e., it won't
264 * try to free it or keep a pointer to it).
265 */
266 struct mbuf m;
267 u_int af = dst->sa_family;
268
269 m.m_next = m0;
270 m.m_len = 4;
271 m.m_data = (char *)⁡
272
273 bpf_mtap(tp->tun_bpf, &m);
274 }
275 #endif
276
277 switch(dst->sa_family) {
278 #ifdef INET
279 case AF_INET:
280 s = splimp();
281 if (IF_QFULL(&ifp->if_snd)) {
282 IF_DROP(&ifp->if_snd);
283 m_freem(m0);
284 splx(s);
285 ifp->if_collisions++;
286 return (ENOBUFS);
287 }
288 IF_ENQUEUE(&ifp->if_snd, m0);
289 splx(s);
290 ifp->if_opackets++;
291 break;
292 #endif
293 default:
294 m_freem(m0);
295 return EAFNOSUPPORT;
296 }
297
298 if (tp->tun_flags & TUN_RWAIT) {
299 tp->tun_flags &= ~TUN_RWAIT;
300 wakeup((caddr_t)tp);
301 }
302 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
303 if (tp->tun_pgrp > 0)
304 gsignal(tp->tun_pgrp, SIGIO);
305 else if (p = pfind(-tp->tun_pgrp))
306 psignal(p, SIGIO);
307 }
308 selwakeup(&tp->tun_rsel);
309 return 0;
310 }
311
312 /*
313 * the cdevsw interface is now pretty minimal.
314 */
315 int
316 tuncioctl(dev, cmd, data, flag)
317 dev_t dev;
318 int cmd;
319 caddr_t data;
320 int flag;
321 {
322 int unit = minor(dev), s;
323 struct tun_softc *tp = &tunctl[unit];
324
325 switch (cmd) {
326 case TUNSDEBUG:
327 tundebug = *(int *)data;
328 break;
329 case TUNGDEBUG:
330 *(int *)data = tundebug;
331 break;
332 case FIONBIO:
333 if (*(int *)data)
334 tp->tun_flags |= TUN_NBIO;
335 else
336 tp->tun_flags &= ~TUN_NBIO;
337 break;
338 case FIOASYNC:
339 if (*(int *)data)
340 tp->tun_flags |= TUN_ASYNC;
341 else
342 tp->tun_flags &= ~TUN_ASYNC;
343 break;
344 case FIONREAD:
345 s = splimp();
346 if (tp->tun_if.if_snd.ifq_head)
347 *(int *)data = tp->tun_if.if_snd.ifq_head->m_len;
348 else
349 *(int *)data = 0;
350 splx(s);
351 break;
352 case TIOCSPGRP:
353 tp->tun_pgrp = *(int *)data;
354 break;
355 case TIOCGPGRP:
356 *(int *)data = tp->tun_pgrp;
357 break;
358 default:
359 return (ENOTTY);
360 }
361 return (0);
362 }
363
364 /*
365 * The cdevsw read interface - reads a packet at a time, or at
366 * least as much of a packet as can be read.
367 */
368 int
369 tunread(dev, uio)
370 dev_t dev;
371 struct uio *uio;
372 {
373 int unit = minor(dev);
374 struct tun_softc *tp = &tunctl[unit];
375 struct ifnet *ifp = &tp->tun_if;
376 struct mbuf *m, *m0;
377 int error=0, len, s;
378
379 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
380 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
381 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
382 ifp->if_unit, tp->tun_flags);
383 return EHOSTDOWN;
384 }
385
386 tp->tun_flags &= ~TUN_RWAIT;
387
388 s = splimp();
389 do {
390 IF_DEQUEUE(&ifp->if_snd, m0);
391 if (m0 == 0) {
392 if (tp->tun_flags & TUN_NBIO) {
393 splx(s);
394 return EWOULDBLOCK;
395 }
396 tp->tun_flags |= TUN_RWAIT;
397 tsleep((caddr_t)tp, PZERO + 1, "tunread", 0);
398 }
399 } while (m0 == 0);
400 splx(s);
401
402 while (m0 && uio->uio_resid > 0 && error == 0) {
403 len = MIN(uio->uio_resid, m0->m_len);
404 if (len == 0)
405 break;
406 error = uiomove(mtod(m0, caddr_t), len, uio);
407 MFREE(m0, m);
408 m0 = m;
409 }
410
411 if (m0) {
412 TUNDEBUG("Dropping mbuf\n");
413 m_freem(m0);
414 }
415 return error;
416 }
417
418 /*
419 * the cdevsw write interface - an atomic write is a packet - or else!
420 */
421 int
422 tunwrite(dev, uio)
423 dev_t dev;
424 struct uio *uio;
425 {
426 int unit = minor (dev);
427 struct ifnet *ifp = &tunctl[unit].tun_if;
428 struct mbuf *top, **mp, *m;
429 int error=0, s, tlen, mlen;
430
431 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
432
433 if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
434 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
435 uio->uio_resid);
436 return EIO;
437 }
438 tlen = uio->uio_resid;
439
440 /* get a header mbuf */
441 MGETHDR(m, M_DONTWAIT, MT_DATA);
442 if (m == NULL)
443 return ENOBUFS;
444 mlen = MHLEN;
445
446 top = 0;
447 mp = ⊤
448 while (error == 0 && uio->uio_resid > 0) {
449 m->m_len = MIN (mlen, uio->uio_resid);
450 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
451 *mp = m;
452 mp = &m->m_next;
453 if (uio->uio_resid > 0) {
454 MGET (m, M_DONTWAIT, MT_DATA);
455 if (m == 0) {
456 error = ENOBUFS;
457 break;
458 }
459 mlen = MLEN;
460 }
461 }
462 if (error) {
463 if (top)
464 m_freem (top);
465 return error;
466 }
467
468 top->m_pkthdr.len = tlen;
469 top->m_pkthdr.rcvif = ifp;
470
471 #if NBPFILTER > 0
472 if (tunctl[unit].tun_bpf) {
473 /*
474 * We need to prepend the address family as
475 * a four byte field. Cons up a dummy header
476 * to pacify bpf. This is safe because bpf
477 * will only read from the mbuf (i.e., it won't
478 * try to free it or keep a pointer to it).
479 */
480 struct mbuf m;
481 u_int af = AF_INET;
482
483 m.m_next = top;
484 m.m_len = 4;
485 m.m_data = (char *)⁡
486
487 bpf_mtap(tunctl[unit].tun_bpf, &m);
488 }
489 #endif
490
491 s = splimp();
492 if (IF_QFULL (&ipintrq)) {
493 IF_DROP(&ipintrq);
494 splx(s);
495 ifp->if_collisions++;
496 m_freem(top);
497 return ENOBUFS;
498 }
499 IF_ENQUEUE(&ipintrq, top);
500 splx(s);
501 ifp->if_ipackets++;
502 schednetisr(NETISR_IP);
503 return error;
504 }
505
506 /*
507 * tunselect - the select interface, this is only useful on reads
508 * really. The write detect always returns true, write never blocks
509 * anyway, it either accepts the packet or drops it.
510 */
511 int
512 tunselect(dev, rw)
513 dev_t dev;
514 int rw;
515 {
516 int unit = minor(dev), s;
517 struct tun_softc *tp = &tunctl[unit];
518 struct ifnet *ifp = &tp->tun_if;
519
520 s = splimp();
521 TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
522
523 switch (rw) {
524 case FREAD:
525 if (ifp->if_snd.ifq_len > 0) {
526 splx(s);
527 TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
528 ifp->if_unit, ifp->if_snd.ifq_len);
529 return 1;
530 }
531 selrecord(curproc, &tp->tun_rsel);
532 break;
533 case FWRITE:
534 splx(s);
535 return 1;
536 }
537 splx(s);
538 TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
539 return 0;
540 }
541
542 #endif /* NTUN */
543