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