if_tun.c revision 1.47 1 1.47 matt /* $NetBSD: if_tun.c,v 1.47 2001/11/05 18:02:16 matt Exp $ */
2 1.14 cgd
3 1.1 cgd /*
4 1.8 deraadt * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
5 1.8 deraadt * Nottingham University 1987.
6 1.1 cgd *
7 1.1 cgd * This source may be freely distributed, however I would be interested
8 1.1 cgd * in any changes that are made.
9 1.6 deraadt *
10 1.1 cgd * This driver takes packets off the IP i/f and hands them up to a
11 1.21 scottr * user process to have its wicked way with. This driver has its
12 1.1 cgd * roots in a similar driver written by Phil Cockcroft (formerly) at
13 1.27 mycroft * UCL. This driver is based much more on read/write/poll mode of
14 1.1 cgd * operation though.
15 1.1 cgd */
16 1.1 cgd
17 1.8 deraadt #include "tun.h"
18 1.8 deraadt #if NTUN > 0
19 1.33 jonathan
20 1.33 jonathan #include "opt_inet.h"
21 1.34 jonathan #include "opt_ns.h"
22 1.8 deraadt
23 1.6 deraadt #include <sys/param.h>
24 1.8 deraadt #include <sys/proc.h>
25 1.6 deraadt #include <sys/systm.h>
26 1.6 deraadt #include <sys/mbuf.h>
27 1.6 deraadt #include <sys/buf.h>
28 1.6 deraadt #include <sys/protosw.h>
29 1.6 deraadt #include <sys/socket.h>
30 1.6 deraadt #include <sys/ioctl.h>
31 1.6 deraadt #include <sys/errno.h>
32 1.6 deraadt #include <sys/syslog.h>
33 1.6 deraadt #include <sys/select.h>
34 1.27 mycroft #include <sys/poll.h>
35 1.8 deraadt #include <sys/file.h>
36 1.22 christos #include <sys/signalvar.h>
37 1.23 christos #include <sys/conf.h>
38 1.9 deraadt
39 1.9 deraadt #include <machine/cpu.h>
40 1.6 deraadt
41 1.6 deraadt #include <net/if.h>
42 1.30 is #include <net/if_ether.h>
43 1.6 deraadt #include <net/netisr.h>
44 1.6 deraadt #include <net/route.h>
45 1.1 cgd
46 1.30 is
47 1.1 cgd #ifdef INET
48 1.6 deraadt #include <netinet/in.h>
49 1.6 deraadt #include <netinet/in_systm.h>
50 1.6 deraadt #include <netinet/in_var.h>
51 1.6 deraadt #include <netinet/ip.h>
52 1.30 is #include <netinet/if_inarp.h>
53 1.1 cgd #endif
54 1.1 cgd
55 1.1 cgd #ifdef NS
56 1.6 deraadt #include <netns/ns.h>
57 1.6 deraadt #include <netns/ns_if.h>
58 1.1 cgd #endif
59 1.1 cgd
60 1.8 deraadt #include "bpfilter.h"
61 1.8 deraadt #if NBPFILTER > 0
62 1.8 deraadt #include <sys/time.h>
63 1.8 deraadt #include <net/bpf.h>
64 1.8 deraadt #endif
65 1.8 deraadt
66 1.8 deraadt #include <net/if_tun.h>
67 1.8 deraadt
68 1.29 christos #define TUNDEBUG if (tundebug) printf
69 1.6 deraadt int tundebug = 0;
70 1.1 cgd
71 1.6 deraadt extern int ifqmaxlen;
72 1.22 christos void tunattach __P((int));
73 1.46 atatat LIST_HEAD(, tun_softc) tun_softc_list;
74 1.46 atatat static struct simplelock tun_softc_lock;
75 1.6 deraadt
76 1.22 christos int tun_ioctl __P((struct ifnet *, u_long, caddr_t));
77 1.22 christos int tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
78 1.22 christos struct rtentry *rt));
79 1.46 atatat int tun_clone_create __P((struct if_clone *, int));
80 1.46 atatat void tun_clone_destroy __P((struct ifnet *));
81 1.6 deraadt
82 1.46 atatat struct if_clone tun_cloner =
83 1.46 atatat IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
84 1.46 atatat
85 1.46 atatat static void tunattach0 __P((struct tun_softc *));
86 1.26 pk static void tuninit __P((struct tun_softc *));
87 1.46 atatat static struct tun_softc *tun_find_unit __P((dev_t));
88 1.6 deraadt
89 1.8 deraadt void
90 1.8 deraadt tunattach(unused)
91 1.8 deraadt int unused;
92 1.8 deraadt {
93 1.46 atatat
94 1.46 atatat simple_lock_init(&tun_softc_lock);
95 1.46 atatat LIST_INIT(&tun_softc_list);
96 1.46 atatat if_clone_attach(&tun_cloner);
97 1.46 atatat }
98 1.46 atatat
99 1.46 atatat int
100 1.46 atatat tun_clone_create(ifc, unit)
101 1.46 atatat struct if_clone *ifc;
102 1.46 atatat int unit;
103 1.46 atatat {
104 1.46 atatat struct tun_softc *sc;
105 1.46 atatat
106 1.46 atatat sc = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
107 1.46 atatat (void)memset(sc, 0, sizeof(struct tun_softc));
108 1.46 atatat
109 1.46 atatat (void)snprintf(sc->tun_if.if_xname, sizeof(sc->tun_if.if_xname),
110 1.46 atatat "%s%d", ifc->ifc_name, unit);
111 1.46 atatat sc->tun_unit = unit;
112 1.46 atatat simple_lock_init(&sc->tun_lock);
113 1.46 atatat
114 1.46 atatat tunattach0(sc);
115 1.46 atatat
116 1.46 atatat simple_lock(&tun_softc_lock);
117 1.46 atatat LIST_INSERT_HEAD(&tun_softc_list, sc, tun_list);
118 1.46 atatat simple_unlock(&tun_softc_lock);
119 1.46 atatat
120 1.46 atatat return (0);
121 1.46 atatat }
122 1.46 atatat
123 1.46 atatat void
124 1.46 atatat tunattach0(sc)
125 1.46 atatat struct tun_softc *sc;
126 1.46 atatat {
127 1.46 atatat struct ifnet *ifp = (void *)sc;
128 1.46 atatat
129 1.46 atatat sc->tun_flags = TUN_INITED;
130 1.46 atatat
131 1.46 atatat ifp = &sc->tun_if;
132 1.46 atatat ifp->if_softc = sc;
133 1.46 atatat ifp->if_mtu = TUNMTU;
134 1.46 atatat ifp->if_ioctl = tun_ioctl;
135 1.46 atatat ifp->if_output = tun_output;
136 1.46 atatat ifp->if_flags = IFF_POINTOPOINT;
137 1.46 atatat ifp->if_snd.ifq_maxlen = ifqmaxlen;
138 1.46 atatat ifp->if_collisions = 0;
139 1.46 atatat ifp->if_ierrors = 0;
140 1.46 atatat ifp->if_oerrors = 0;
141 1.46 atatat ifp->if_ipackets = 0;
142 1.46 atatat ifp->if_opackets = 0;
143 1.46 atatat ifp->if_dlt = DLT_NULL;
144 1.46 atatat if_attach(ifp);
145 1.46 atatat if_alloc_sadl(ifp);
146 1.46 atatat #if NBPFILTER > 0
147 1.46 atatat bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
148 1.46 atatat #endif
149 1.46 atatat }
150 1.46 atatat
151 1.46 atatat void
152 1.46 atatat tun_clone_destroy(ifp)
153 1.8 deraadt struct ifnet *ifp;
154 1.46 atatat {
155 1.46 atatat struct tun_softc *tp = (void *)ifp;
156 1.46 atatat struct proc *p;
157 1.8 deraadt
158 1.46 atatat simple_lock(&tun_softc_lock);
159 1.46 atatat simple_lock(&tp->tun_lock);
160 1.46 atatat LIST_REMOVE(tp, tun_list);
161 1.46 atatat simple_unlock(&tp->tun_lock);
162 1.46 atatat simple_unlock(&tun_softc_lock);
163 1.46 atatat
164 1.46 atatat if (tp->tun_flags & TUN_RWAIT) {
165 1.46 atatat tp->tun_flags &= ~TUN_RWAIT;
166 1.46 atatat wakeup((caddr_t)tp);
167 1.46 atatat }
168 1.46 atatat if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
169 1.46 atatat if (tp->tun_pgrp > 0)
170 1.46 atatat gsignal(tp->tun_pgrp, SIGIO);
171 1.46 atatat else if ((p = pfind(-tp->tun_pgrp)) != NULL)
172 1.46 atatat psignal(p, SIGIO);
173 1.46 atatat }
174 1.46 atatat selwakeup(&tp->tun_rsel);
175 1.8 deraadt
176 1.8 deraadt #if NBPFILTER > 0
177 1.46 atatat bpfdetach(ifp);
178 1.8 deraadt #endif
179 1.46 atatat if_detach(ifp);
180 1.46 atatat
181 1.46 atatat free(tp, M_DEVBUF);
182 1.46 atatat }
183 1.46 atatat
184 1.46 atatat static struct tun_softc *
185 1.46 atatat tun_find_unit(dev)
186 1.46 atatat dev_t dev;
187 1.46 atatat {
188 1.46 atatat struct tun_softc *tp;
189 1.46 atatat int unit = minor(dev);
190 1.46 atatat
191 1.46 atatat simple_lock(&tun_softc_lock);
192 1.46 atatat LIST_FOREACH(tp, &tun_softc_list, tun_list)
193 1.46 atatat if (unit == tp->tun_unit)
194 1.46 atatat break;
195 1.46 atatat if (tp)
196 1.46 atatat simple_lock(&tp->tun_lock);
197 1.46 atatat simple_unlock(&tun_softc_lock);
198 1.46 atatat
199 1.46 atatat return (tp);
200 1.8 deraadt }
201 1.8 deraadt
202 1.6 deraadt /*
203 1.6 deraadt * tunnel open - must be superuser & the device must be
204 1.6 deraadt * configured in
205 1.6 deraadt */
206 1.6 deraadt int
207 1.6 deraadt tunopen(dev, flag, mode, p)
208 1.6 deraadt dev_t dev;
209 1.6 deraadt int flag, mode;
210 1.6 deraadt struct proc *p;
211 1.6 deraadt {
212 1.6 deraadt struct ifnet *ifp;
213 1.8 deraadt struct tun_softc *tp;
214 1.46 atatat int error;
215 1.1 cgd
216 1.22 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
217 1.5 deraadt return (error);
218 1.5 deraadt
219 1.46 atatat if (NTUN < 1)
220 1.6 deraadt return (ENXIO);
221 1.46 atatat
222 1.46 atatat tp = tun_find_unit(dev);
223 1.46 atatat
224 1.46 atatat if (!tp)
225 1.46 atatat return (ENXIO);
226 1.46 atatat
227 1.46 atatat if (tp->tun_flags & TUN_OPEN) {
228 1.46 atatat simple_unlock(&tp->tun_lock);
229 1.46 atatat return (EBUSY);
230 1.46 atatat }
231 1.46 atatat
232 1.6 deraadt ifp = &tp->tun_if;
233 1.6 deraadt tp->tun_flags |= TUN_OPEN;
234 1.24 thorpej TUNDEBUG("%s: open\n", ifp->if_xname);
235 1.46 atatat simple_unlock(&tp->tun_lock);
236 1.6 deraadt return (0);
237 1.1 cgd }
238 1.1 cgd
239 1.6 deraadt /*
240 1.6 deraadt * tunclose - close the device - mark i/f down & delete
241 1.6 deraadt * routing info
242 1.6 deraadt */
243 1.6 deraadt int
244 1.22 christos tunclose(dev, flag, mode, p)
245 1.6 deraadt dev_t dev;
246 1.6 deraadt int flag;
247 1.22 christos int mode;
248 1.22 christos struct proc *p;
249 1.6 deraadt {
250 1.46 atatat int s;
251 1.46 atatat struct tun_softc *tp;
252 1.46 atatat struct ifnet *ifp;
253 1.6 deraadt struct mbuf *m;
254 1.6 deraadt
255 1.46 atatat tp = tun_find_unit(dev);
256 1.46 atatat
257 1.46 atatat /* interface was "destroyed" before the close */
258 1.46 atatat if (tp == NULL)
259 1.46 atatat return (0);
260 1.46 atatat
261 1.46 atatat ifp = &tp->tun_if;
262 1.46 atatat
263 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
264 1.6 deraadt
265 1.6 deraadt /*
266 1.6 deraadt * junk all pending output
267 1.6 deraadt */
268 1.6 deraadt do {
269 1.43 thorpej s = splnet();
270 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m);
271 1.6 deraadt splx(s);
272 1.6 deraadt if (m)
273 1.6 deraadt m_freem(m);
274 1.6 deraadt } while (m);
275 1.6 deraadt
276 1.6 deraadt if (ifp->if_flags & IFF_UP) {
277 1.43 thorpej s = splnet();
278 1.6 deraadt if_down(ifp);
279 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
280 1.17 mycroft /* find internet addresses and delete routes */
281 1.39 augustss struct ifaddr *ifa;
282 1.47 matt TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
283 1.38 itojun #ifdef INET
284 1.18 mycroft if (ifa->ifa_addr->sa_family == AF_INET) {
285 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
286 1.26 pk tp->tun_flags & TUN_DSTADDR
287 1.26 pk ? RTF_HOST
288 1.26 pk : 0);
289 1.18 mycroft }
290 1.38 itojun #endif
291 1.11 deraadt }
292 1.8 deraadt }
293 1.6 deraadt splx(s);
294 1.6 deraadt }
295 1.6 deraadt tp->tun_pgrp = 0;
296 1.7 deraadt selwakeup(&tp->tun_rsel);
297 1.6 deraadt
298 1.24 thorpej TUNDEBUG ("%s: closed\n", ifp->if_xname);
299 1.46 atatat simple_unlock(&tp->tun_lock);
300 1.6 deraadt return (0);
301 1.1 cgd }
302 1.1 cgd
303 1.26 pk static void
304 1.24 thorpej tuninit(tp)
305 1.24 thorpej struct tun_softc *tp;
306 1.6 deraadt {
307 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
308 1.46 atatat struct ifaddr *ifa;
309 1.8 deraadt
310 1.24 thorpej TUNDEBUG("%s: tuninit\n", ifp->if_xname);
311 1.6 deraadt
312 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
313 1.8 deraadt
314 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
315 1.47 matt TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
316 1.38 itojun #ifdef INET
317 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
318 1.17 mycroft struct sockaddr_in *sin;
319 1.11 deraadt
320 1.17 mycroft sin = satosin(ifa->ifa_addr);
321 1.17 mycroft if (sin && sin->sin_addr.s_addr)
322 1.17 mycroft tp->tun_flags |= TUN_IASET;
323 1.17 mycroft
324 1.26 pk if (ifp->if_flags & IFF_POINTOPOINT) {
325 1.26 pk sin = satosin(ifa->ifa_dstaddr);
326 1.26 pk if (sin && sin->sin_addr.s_addr)
327 1.26 pk tp->tun_flags |= TUN_DSTADDR;
328 1.26 pk }
329 1.11 deraadt }
330 1.38 itojun #endif
331 1.18 mycroft }
332 1.8 deraadt
333 1.26 pk return;
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd /*
337 1.1 cgd * Process an ioctl request.
338 1.1 cgd */
339 1.1 cgd int
340 1.22 christos tun_ioctl(ifp, cmd, data)
341 1.6 deraadt struct ifnet *ifp;
342 1.25 mycroft u_long cmd;
343 1.6 deraadt caddr_t data;
344 1.6 deraadt {
345 1.6 deraadt int error = 0, s;
346 1.46 atatat struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
347 1.46 atatat
348 1.46 atatat simple_lock(&tp->tun_lock);
349 1.6 deraadt
350 1.43 thorpej s = splnet();
351 1.6 deraadt switch(cmd) {
352 1.6 deraadt case SIOCSIFADDR:
353 1.24 thorpej tuninit((struct tun_softc *)(ifp->if_softc));
354 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
355 1.6 deraadt break;
356 1.6 deraadt case SIOCSIFDSTADDR:
357 1.24 thorpej tuninit((struct tun_softc *)(ifp->if_softc));
358 1.24 thorpej TUNDEBUG("%s: destination address set\n", ifp->if_xname);
359 1.6 deraadt break;
360 1.26 pk case SIOCSIFBRDADDR:
361 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
362 1.26 pk break;
363 1.31 matt case SIOCSIFMTU: {
364 1.31 matt struct ifreq *ifr = (struct ifreq *) data;
365 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
366 1.31 matt error = EINVAL;
367 1.31 matt break;
368 1.31 matt }
369 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
370 1.31 matt ifp->if_mtu = ifr->ifr_mtu;
371 1.32 matt break;
372 1.32 matt }
373 1.32 matt case SIOCADDMULTI:
374 1.32 matt case SIOCDELMULTI: {
375 1.32 matt struct ifreq *ifr = (struct ifreq *) data;
376 1.32 matt if (ifr == 0) {
377 1.32 matt error = EAFNOSUPPORT; /* XXX */
378 1.32 matt break;
379 1.32 matt }
380 1.32 matt switch (ifr->ifr_addr.sa_family) {
381 1.32 matt
382 1.32 matt #ifdef INET
383 1.32 matt case AF_INET:
384 1.32 matt break;
385 1.32 matt #endif
386 1.32 matt
387 1.32 matt default:
388 1.32 matt error = EAFNOSUPPORT;
389 1.32 matt break;
390 1.32 matt }
391 1.31 matt break;
392 1.31 matt }
393 1.38 itojun case SIOCSIFFLAGS:
394 1.38 itojun break;
395 1.6 deraadt default:
396 1.6 deraadt error = EINVAL;
397 1.6 deraadt }
398 1.6 deraadt splx(s);
399 1.46 atatat simple_unlock(&tp->tun_lock);
400 1.6 deraadt return (error);
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd /*
404 1.22 christos * tun_output - queue packets from higher level ready to put out.
405 1.1 cgd */
406 1.6 deraadt int
407 1.22 christos tun_output(ifp, m0, dst, rt)
408 1.6 deraadt struct ifnet *ifp;
409 1.6 deraadt struct mbuf *m0;
410 1.6 deraadt struct sockaddr *dst;
411 1.12 deraadt struct rtentry *rt;
412 1.6 deraadt {
413 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
414 1.6 deraadt struct proc *p;
415 1.38 itojun #ifdef INET
416 1.6 deraadt int s;
417 1.38 itojun #endif
418 1.6 deraadt
419 1.46 atatat simple_lock(&tp->tun_lock);
420 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
421 1.1 cgd
422 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
423 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
424 1.24 thorpej tp->tun_flags);
425 1.8 deraadt m_freem (m0);
426 1.46 atatat simple_unlock(&tp->tun_lock);
427 1.26 pk return (EHOSTDOWN);
428 1.8 deraadt }
429 1.8 deraadt
430 1.8 deraadt #if NBPFILTER > 0
431 1.40 thorpej if (ifp->if_bpf) {
432 1.8 deraadt /*
433 1.8 deraadt * We need to prepend the address family as
434 1.8 deraadt * a four byte field. Cons up a dummy header
435 1.8 deraadt * to pacify bpf. This is safe because bpf
436 1.8 deraadt * will only read from the mbuf (i.e., it won't
437 1.8 deraadt * try to free it or keep a pointer to it).
438 1.8 deraadt */
439 1.8 deraadt struct mbuf m;
440 1.16 cgd u_int32_t af = dst->sa_family;
441 1.8 deraadt
442 1.8 deraadt m.m_next = m0;
443 1.16 cgd m.m_len = sizeof(af);
444 1.8 deraadt m.m_data = (char *)⁡
445 1.8 deraadt
446 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
447 1.8 deraadt }
448 1.8 deraadt #endif
449 1.8 deraadt
450 1.6 deraadt switch(dst->sa_family) {
451 1.1 cgd #ifdef INET
452 1.6 deraadt case AF_INET:
453 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
454 1.26 pk /* Simple link-layer header */
455 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
456 1.26 pk if (m0 == NULL) {
457 1.26 pk IF_DROP(&ifp->if_snd);
458 1.46 atatat simple_unlock(&tp->tun_lock);
459 1.26 pk return (ENOBUFS);
460 1.26 pk }
461 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
462 1.26 pk }
463 1.36 sommerfe /* FALLTHROUGH */
464 1.36 sommerfe case AF_UNSPEC:
465 1.43 thorpej s = splnet();
466 1.6 deraadt if (IF_QFULL(&ifp->if_snd)) {
467 1.6 deraadt IF_DROP(&ifp->if_snd);
468 1.6 deraadt m_freem(m0);
469 1.6 deraadt splx(s);
470 1.6 deraadt ifp->if_collisions++;
471 1.46 atatat simple_unlock(&tp->tun_lock);
472 1.6 deraadt return (ENOBUFS);
473 1.6 deraadt }
474 1.6 deraadt IF_ENQUEUE(&ifp->if_snd, m0);
475 1.6 deraadt splx(s);
476 1.6 deraadt ifp->if_opackets++;
477 1.6 deraadt break;
478 1.1 cgd #endif
479 1.6 deraadt default:
480 1.6 deraadt m_freem(m0);
481 1.46 atatat simple_unlock(&tp->tun_lock);
482 1.26 pk return (EAFNOSUPPORT);
483 1.6 deraadt }
484 1.6 deraadt
485 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
486 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
487 1.6 deraadt wakeup((caddr_t)tp);
488 1.6 deraadt }
489 1.6 deraadt if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
490 1.6 deraadt if (tp->tun_pgrp > 0)
491 1.6 deraadt gsignal(tp->tun_pgrp, SIGIO);
492 1.22 christos else if ((p = pfind(-tp->tun_pgrp)) != NULL)
493 1.6 deraadt psignal(p, SIGIO);
494 1.6 deraadt }
495 1.6 deraadt selwakeup(&tp->tun_rsel);
496 1.46 atatat simple_unlock(&tp->tun_lock);
497 1.26 pk return (0);
498 1.1 cgd }
499 1.1 cgd
500 1.1 cgd /*
501 1.1 cgd * the cdevsw interface is now pretty minimal.
502 1.1 cgd */
503 1.6 deraadt int
504 1.20 mycroft tunioctl(dev, cmd, data, flag, p)
505 1.6 deraadt dev_t dev;
506 1.15 cgd u_long cmd;
507 1.6 deraadt caddr_t data;
508 1.6 deraadt int flag;
509 1.12 deraadt struct proc *p;
510 1.6 deraadt {
511 1.46 atatat int s;
512 1.46 atatat struct tun_softc *tp;
513 1.46 atatat
514 1.46 atatat tp = tun_find_unit(dev);
515 1.46 atatat
516 1.46 atatat /* interface was "destroyed" already */
517 1.46 atatat if (tp == NULL)
518 1.46 atatat return (ENXIO);
519 1.6 deraadt
520 1.6 deraadt switch (cmd) {
521 1.6 deraadt case TUNSDEBUG:
522 1.6 deraadt tundebug = *(int *)data;
523 1.6 deraadt break;
524 1.26 pk
525 1.6 deraadt case TUNGDEBUG:
526 1.6 deraadt *(int *)data = tundebug;
527 1.6 deraadt break;
528 1.26 pk
529 1.26 pk case TUNSIFMODE:
530 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
531 1.26 pk case IFF_POINTOPOINT:
532 1.26 pk case IFF_BROADCAST:
533 1.43 thorpej s = splnet();
534 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
535 1.26 pk splx(s);
536 1.46 atatat simple_unlock(&tp->tun_lock);
537 1.26 pk return (EBUSY);
538 1.26 pk }
539 1.26 pk tp->tun_if.if_flags &=
540 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
541 1.26 pk tp->tun_if.if_flags |= *(int *)data;
542 1.26 pk splx(s);
543 1.26 pk break;
544 1.26 pk default:
545 1.46 atatat simple_unlock(&tp->tun_lock);
546 1.26 pk return (EINVAL);
547 1.26 pk break;
548 1.26 pk }
549 1.26 pk break;
550 1.26 pk
551 1.26 pk case TUNSLMODE:
552 1.26 pk if (*(int *)data)
553 1.26 pk tp->tun_flags |= TUN_PREPADDR;
554 1.26 pk else
555 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
556 1.26 pk break;
557 1.26 pk
558 1.6 deraadt case FIONBIO:
559 1.6 deraadt if (*(int *)data)
560 1.6 deraadt tp->tun_flags |= TUN_NBIO;
561 1.6 deraadt else
562 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
563 1.6 deraadt break;
564 1.26 pk
565 1.6 deraadt case FIOASYNC:
566 1.6 deraadt if (*(int *)data)
567 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
568 1.6 deraadt else
569 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
570 1.6 deraadt break;
571 1.26 pk
572 1.6 deraadt case FIONREAD:
573 1.43 thorpej s = splnet();
574 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
575 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
576 1.6 deraadt else
577 1.6 deraadt *(int *)data = 0;
578 1.6 deraadt splx(s);
579 1.6 deraadt break;
580 1.26 pk
581 1.6 deraadt case TIOCSPGRP:
582 1.6 deraadt tp->tun_pgrp = *(int *)data;
583 1.6 deraadt break;
584 1.26 pk
585 1.6 deraadt case TIOCGPGRP:
586 1.6 deraadt *(int *)data = tp->tun_pgrp;
587 1.6 deraadt break;
588 1.26 pk
589 1.6 deraadt default:
590 1.46 atatat simple_unlock(&tp->tun_lock);
591 1.6 deraadt return (ENOTTY);
592 1.6 deraadt }
593 1.46 atatat simple_unlock(&tp->tun_lock);
594 1.6 deraadt return (0);
595 1.1 cgd }
596 1.1 cgd
597 1.1 cgd /*
598 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
599 1.6 deraadt * least as much of a packet as can be read.
600 1.1 cgd */
601 1.6 deraadt int
602 1.22 christos tunread(dev, uio, ioflag)
603 1.6 deraadt dev_t dev;
604 1.6 deraadt struct uio *uio;
605 1.22 christos int ioflag;
606 1.6 deraadt {
607 1.46 atatat struct tun_softc *tp;
608 1.46 atatat struct ifnet *ifp;
609 1.6 deraadt struct mbuf *m, *m0;
610 1.46 atatat int error=0, len, s, index;
611 1.46 atatat
612 1.46 atatat tp = tun_find_unit(dev);
613 1.46 atatat
614 1.46 atatat /* interface was "destroyed" already */
615 1.46 atatat if (tp == NULL)
616 1.46 atatat return (ENXIO);
617 1.46 atatat
618 1.46 atatat index = tp->tun_if.if_index;
619 1.46 atatat ifp = &tp->tun_if;
620 1.6 deraadt
621 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
622 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
623 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
624 1.46 atatat simple_unlock(&tp->tun_lock);
625 1.8 deraadt return EHOSTDOWN;
626 1.8 deraadt }
627 1.8 deraadt
628 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
629 1.6 deraadt
630 1.43 thorpej s = splnet();
631 1.6 deraadt do {
632 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m0);
633 1.6 deraadt if (m0 == 0) {
634 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
635 1.6 deraadt splx(s);
636 1.46 atatat simple_unlock(&tp->tun_lock);
637 1.26 pk return (EWOULDBLOCK);
638 1.6 deraadt }
639 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
640 1.46 atatat simple_unlock(&tp->tun_lock);
641 1.26 pk if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
642 1.26 pk splx(s);
643 1.26 pk return (EINTR);
644 1.46 atatat } else {
645 1.46 atatat /*
646 1.46 atatat * Maybe the interface was destroyed while
647 1.46 atatat * we were sleeping, so let's ensure that
648 1.46 atatat * we're looking at the same (valid) tun
649 1.46 atatat * interface before looping.
650 1.46 atatat */
651 1.46 atatat tp = tun_find_unit(dev);
652 1.46 atatat if (tp == NULL ||
653 1.46 atatat tp->tun_if.if_index != index) {
654 1.46 atatat splx(s);
655 1.46 atatat if (tp)
656 1.46 atatat simple_unlock(&tp->tun_lock);
657 1.46 atatat return (ENXIO);
658 1.46 atatat }
659 1.26 pk }
660 1.6 deraadt }
661 1.6 deraadt } while (m0 == 0);
662 1.6 deraadt splx(s);
663 1.6 deraadt
664 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
665 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
666 1.45 itojun if (len != 0)
667 1.45 itojun error = uiomove(mtod(m0, caddr_t), len, uio);
668 1.6 deraadt MFREE(m0, m);
669 1.6 deraadt m0 = m;
670 1.6 deraadt }
671 1.6 deraadt
672 1.6 deraadt if (m0) {
673 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
674 1.6 deraadt m_freem(m0);
675 1.6 deraadt }
676 1.19 pk if (error)
677 1.19 pk ifp->if_ierrors++;
678 1.46 atatat simple_unlock(&tp->tun_lock);
679 1.26 pk return (error);
680 1.1 cgd }
681 1.1 cgd
682 1.1 cgd /*
683 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
684 1.1 cgd */
685 1.6 deraadt int
686 1.22 christos tunwrite(dev, uio, ioflag)
687 1.8 deraadt dev_t dev;
688 1.6 deraadt struct uio *uio;
689 1.22 christos int ioflag;
690 1.6 deraadt {
691 1.46 atatat struct tun_softc *tp;
692 1.46 atatat struct ifnet *ifp;
693 1.6 deraadt struct mbuf *top, **mp, *m;
694 1.26 pk struct ifqueue *ifq;
695 1.26 pk struct sockaddr dst;
696 1.26 pk int isr, error=0, s, tlen, mlen;
697 1.6 deraadt
698 1.46 atatat tp = tun_find_unit(dev);
699 1.46 atatat
700 1.46 atatat /* interface was "destroyed" already */
701 1.46 atatat if (tp == NULL)
702 1.46 atatat return (ENXIO);
703 1.46 atatat
704 1.46 atatat ifp = &tp->tun_if;
705 1.46 atatat
706 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
707 1.6 deraadt
708 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
709 1.46 atatat if (uio->uio_resid < sizeof(dst)) {
710 1.46 atatat simple_unlock(&tp->tun_lock);
711 1.26 pk return (EIO);
712 1.46 atatat }
713 1.26 pk error = uiomove((caddr_t)&dst, sizeof(dst), uio);
714 1.26 pk if (dst.sa_len > sizeof(dst)) {
715 1.26 pk /* Duh.. */
716 1.26 pk char discard;
717 1.26 pk int n = dst.sa_len - sizeof(dst);
718 1.26 pk while (n--)
719 1.46 atatat if ((error = uiomove(&discard, 1, uio)) != 0) {
720 1.46 atatat simple_unlock(&tp->tun_lock);
721 1.26 pk return (error);
722 1.46 atatat }
723 1.26 pk }
724 1.26 pk } else {
725 1.26 pk #ifdef INET
726 1.26 pk dst.sa_family = AF_INET;
727 1.26 pk #endif
728 1.26 pk }
729 1.26 pk
730 1.6 deraadt if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
731 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
732 1.37 mjacob (unsigned long)uio->uio_resid);
733 1.46 atatat simple_unlock(&tp->tun_lock);
734 1.26 pk return (EIO);
735 1.6 deraadt }
736 1.26 pk
737 1.26 pk switch (dst.sa_family) {
738 1.26 pk #ifdef INET
739 1.26 pk case AF_INET:
740 1.26 pk ifq = &ipintrq;
741 1.26 pk isr = NETISR_IP;
742 1.26 pk break;
743 1.26 pk #endif
744 1.26 pk default:
745 1.46 atatat simple_unlock(&tp->tun_lock);
746 1.26 pk return (EAFNOSUPPORT);
747 1.26 pk }
748 1.26 pk
749 1.8 deraadt tlen = uio->uio_resid;
750 1.8 deraadt
751 1.8 deraadt /* get a header mbuf */
752 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
753 1.46 atatat if (m == NULL) {
754 1.46 atatat simple_unlock(&tp->tun_lock);
755 1.26 pk return (ENOBUFS);
756 1.46 atatat }
757 1.8 deraadt mlen = MHLEN;
758 1.8 deraadt
759 1.6 deraadt top = 0;
760 1.6 deraadt mp = ⊤
761 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
762 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
763 1.8 deraadt error = uiomove(mtod (m, caddr_t), m->m_len, uio);
764 1.6 deraadt *mp = m;
765 1.6 deraadt mp = &m->m_next;
766 1.8 deraadt if (uio->uio_resid > 0) {
767 1.8 deraadt MGET (m, M_DONTWAIT, MT_DATA);
768 1.8 deraadt if (m == 0) {
769 1.8 deraadt error = ENOBUFS;
770 1.8 deraadt break;
771 1.8 deraadt }
772 1.8 deraadt mlen = MLEN;
773 1.8 deraadt }
774 1.6 deraadt }
775 1.6 deraadt if (error) {
776 1.6 deraadt if (top)
777 1.8 deraadt m_freem (top);
778 1.19 pk ifp->if_ierrors++;
779 1.46 atatat simple_unlock(&tp->tun_lock);
780 1.26 pk return (error);
781 1.6 deraadt }
782 1.6 deraadt
783 1.8 deraadt top->m_pkthdr.len = tlen;
784 1.8 deraadt top->m_pkthdr.rcvif = ifp;
785 1.8 deraadt
786 1.8 deraadt #if NBPFILTER > 0
787 1.40 thorpej if (ifp->if_bpf) {
788 1.8 deraadt /*
789 1.8 deraadt * We need to prepend the address family as
790 1.8 deraadt * a four byte field. Cons up a dummy header
791 1.8 deraadt * to pacify bpf. This is safe because bpf
792 1.8 deraadt * will only read from the mbuf (i.e., it won't
793 1.8 deraadt * try to free it or keep a pointer to it).
794 1.8 deraadt */
795 1.8 deraadt struct mbuf m;
796 1.16 cgd u_int32_t af = AF_INET;
797 1.8 deraadt
798 1.8 deraadt m.m_next = top;
799 1.16 cgd m.m_len = sizeof(af);
800 1.8 deraadt m.m_data = (char *)⁡
801 1.8 deraadt
802 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
803 1.6 deraadt }
804 1.8 deraadt #endif
805 1.6 deraadt
806 1.43 thorpej s = splnet();
807 1.26 pk if (IF_QFULL(ifq)) {
808 1.26 pk IF_DROP(ifq);
809 1.6 deraadt splx(s);
810 1.6 deraadt ifp->if_collisions++;
811 1.6 deraadt m_freem(top);
812 1.46 atatat simple_unlock(&tp->tun_lock);
813 1.26 pk return (ENOBUFS);
814 1.6 deraadt }
815 1.26 pk IF_ENQUEUE(ifq, top);
816 1.6 deraadt splx(s);
817 1.6 deraadt ifp->if_ipackets++;
818 1.26 pk schednetisr(isr);
819 1.46 atatat simple_unlock(&tp->tun_lock);
820 1.26 pk return (error);
821 1.1 cgd }
822 1.1 cgd
823 1.1 cgd /*
824 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
825 1.6 deraadt * really. The write detect always returns true, write never blocks
826 1.6 deraadt * anyway, it either accepts the packet or drops it.
827 1.6 deraadt */
828 1.6 deraadt int
829 1.27 mycroft tunpoll(dev, events, p)
830 1.6 deraadt dev_t dev;
831 1.27 mycroft int events;
832 1.22 christos struct proc *p;
833 1.6 deraadt {
834 1.46 atatat struct tun_softc *tp;
835 1.46 atatat struct ifnet *ifp;
836 1.46 atatat int s, revents = 0;
837 1.46 atatat
838 1.46 atatat tp = tun_find_unit(dev);
839 1.46 atatat
840 1.46 atatat /* interface was "destroyed" already */
841 1.46 atatat if (tp == NULL)
842 1.46 atatat return (0);
843 1.46 atatat
844 1.46 atatat ifp = &tp->tun_if;
845 1.6 deraadt
846 1.43 thorpej s = splnet();
847 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
848 1.6 deraadt
849 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
850 1.6 deraadt if (ifp->if_snd.ifq_len > 0) {
851 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
852 1.24 thorpej ifp->if_snd.ifq_len);
853 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
854 1.27 mycroft } else {
855 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
856 1.27 mycroft selrecord(p, &tp->tun_rsel);
857 1.6 deraadt }
858 1.35 veego }
859 1.27 mycroft
860 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
861 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
862 1.27 mycroft
863 1.6 deraadt splx(s);
864 1.46 atatat simple_unlock(&tp->tun_lock);
865 1.27 mycroft return (revents);
866 1.1 cgd }
867 1.8 deraadt
868 1.8 deraadt #endif /* NTUN */
869