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