if_tun.c revision 1.46 1 1.46 atatat /* $NetBSD: if_tun.c,v 1.46 2001/10/31 20:08:17 atatat 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.17 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
283 1.18 mycroft ifa = ifa->ifa_list.tqe_next) {
284 1.38 itojun #ifdef INET
285 1.18 mycroft if (ifa->ifa_addr->sa_family == AF_INET) {
286 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
287 1.26 pk tp->tun_flags & TUN_DSTADDR
288 1.26 pk ? RTF_HOST
289 1.26 pk : 0);
290 1.18 mycroft }
291 1.38 itojun #endif
292 1.11 deraadt }
293 1.8 deraadt }
294 1.6 deraadt splx(s);
295 1.6 deraadt }
296 1.6 deraadt tp->tun_pgrp = 0;
297 1.7 deraadt selwakeup(&tp->tun_rsel);
298 1.6 deraadt
299 1.24 thorpej TUNDEBUG ("%s: closed\n", ifp->if_xname);
300 1.46 atatat simple_unlock(&tp->tun_lock);
301 1.6 deraadt return (0);
302 1.1 cgd }
303 1.1 cgd
304 1.26 pk static void
305 1.24 thorpej tuninit(tp)
306 1.24 thorpej struct tun_softc *tp;
307 1.6 deraadt {
308 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
309 1.46 atatat struct ifaddr *ifa;
310 1.8 deraadt
311 1.24 thorpej TUNDEBUG("%s: tuninit\n", ifp->if_xname);
312 1.6 deraadt
313 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
314 1.8 deraadt
315 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
316 1.17 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
317 1.26 pk ifa = ifa->ifa_list.tqe_next) {
318 1.38 itojun #ifdef INET
319 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
320 1.17 mycroft struct sockaddr_in *sin;
321 1.11 deraadt
322 1.17 mycroft sin = satosin(ifa->ifa_addr);
323 1.17 mycroft if (sin && sin->sin_addr.s_addr)
324 1.17 mycroft tp->tun_flags |= TUN_IASET;
325 1.17 mycroft
326 1.26 pk if (ifp->if_flags & IFF_POINTOPOINT) {
327 1.26 pk sin = satosin(ifa->ifa_dstaddr);
328 1.26 pk if (sin && sin->sin_addr.s_addr)
329 1.26 pk tp->tun_flags |= TUN_DSTADDR;
330 1.26 pk }
331 1.11 deraadt }
332 1.38 itojun #endif
333 1.18 mycroft }
334 1.8 deraadt
335 1.26 pk return;
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd /*
339 1.1 cgd * Process an ioctl request.
340 1.1 cgd */
341 1.1 cgd int
342 1.22 christos tun_ioctl(ifp, cmd, data)
343 1.6 deraadt struct ifnet *ifp;
344 1.25 mycroft u_long cmd;
345 1.6 deraadt caddr_t data;
346 1.6 deraadt {
347 1.6 deraadt int error = 0, s;
348 1.46 atatat struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
349 1.46 atatat
350 1.46 atatat simple_lock(&tp->tun_lock);
351 1.6 deraadt
352 1.43 thorpej s = splnet();
353 1.6 deraadt switch(cmd) {
354 1.6 deraadt case SIOCSIFADDR:
355 1.24 thorpej tuninit((struct tun_softc *)(ifp->if_softc));
356 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
357 1.6 deraadt break;
358 1.6 deraadt case SIOCSIFDSTADDR:
359 1.24 thorpej tuninit((struct tun_softc *)(ifp->if_softc));
360 1.24 thorpej TUNDEBUG("%s: destination address set\n", ifp->if_xname);
361 1.6 deraadt break;
362 1.26 pk case SIOCSIFBRDADDR:
363 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
364 1.26 pk break;
365 1.31 matt case SIOCSIFMTU: {
366 1.31 matt struct ifreq *ifr = (struct ifreq *) data;
367 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
368 1.31 matt error = EINVAL;
369 1.31 matt break;
370 1.31 matt }
371 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
372 1.31 matt ifp->if_mtu = ifr->ifr_mtu;
373 1.32 matt break;
374 1.32 matt }
375 1.32 matt case SIOCADDMULTI:
376 1.32 matt case SIOCDELMULTI: {
377 1.32 matt struct ifreq *ifr = (struct ifreq *) data;
378 1.32 matt if (ifr == 0) {
379 1.32 matt error = EAFNOSUPPORT; /* XXX */
380 1.32 matt break;
381 1.32 matt }
382 1.32 matt switch (ifr->ifr_addr.sa_family) {
383 1.32 matt
384 1.32 matt #ifdef INET
385 1.32 matt case AF_INET:
386 1.32 matt break;
387 1.32 matt #endif
388 1.32 matt
389 1.32 matt default:
390 1.32 matt error = EAFNOSUPPORT;
391 1.32 matt break;
392 1.32 matt }
393 1.31 matt break;
394 1.31 matt }
395 1.38 itojun case SIOCSIFFLAGS:
396 1.38 itojun break;
397 1.6 deraadt default:
398 1.6 deraadt error = EINVAL;
399 1.6 deraadt }
400 1.6 deraadt splx(s);
401 1.46 atatat simple_unlock(&tp->tun_lock);
402 1.6 deraadt return (error);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd /*
406 1.22 christos * tun_output - queue packets from higher level ready to put out.
407 1.1 cgd */
408 1.6 deraadt int
409 1.22 christos tun_output(ifp, m0, dst, rt)
410 1.6 deraadt struct ifnet *ifp;
411 1.6 deraadt struct mbuf *m0;
412 1.6 deraadt struct sockaddr *dst;
413 1.12 deraadt struct rtentry *rt;
414 1.6 deraadt {
415 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
416 1.6 deraadt struct proc *p;
417 1.38 itojun #ifdef INET
418 1.6 deraadt int s;
419 1.38 itojun #endif
420 1.6 deraadt
421 1.46 atatat simple_lock(&tp->tun_lock);
422 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
423 1.1 cgd
424 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
425 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
426 1.24 thorpej tp->tun_flags);
427 1.8 deraadt m_freem (m0);
428 1.46 atatat simple_unlock(&tp->tun_lock);
429 1.26 pk return (EHOSTDOWN);
430 1.8 deraadt }
431 1.8 deraadt
432 1.8 deraadt #if NBPFILTER > 0
433 1.40 thorpej if (ifp->if_bpf) {
434 1.8 deraadt /*
435 1.8 deraadt * We need to prepend the address family as
436 1.8 deraadt * a four byte field. Cons up a dummy header
437 1.8 deraadt * to pacify bpf. This is safe because bpf
438 1.8 deraadt * will only read from the mbuf (i.e., it won't
439 1.8 deraadt * try to free it or keep a pointer to it).
440 1.8 deraadt */
441 1.8 deraadt struct mbuf m;
442 1.16 cgd u_int32_t af = dst->sa_family;
443 1.8 deraadt
444 1.8 deraadt m.m_next = m0;
445 1.16 cgd m.m_len = sizeof(af);
446 1.8 deraadt m.m_data = (char *)⁡
447 1.8 deraadt
448 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
449 1.8 deraadt }
450 1.8 deraadt #endif
451 1.8 deraadt
452 1.6 deraadt switch(dst->sa_family) {
453 1.1 cgd #ifdef INET
454 1.6 deraadt case AF_INET:
455 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
456 1.26 pk /* Simple link-layer header */
457 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
458 1.26 pk if (m0 == NULL) {
459 1.26 pk IF_DROP(&ifp->if_snd);
460 1.46 atatat simple_unlock(&tp->tun_lock);
461 1.26 pk return (ENOBUFS);
462 1.26 pk }
463 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
464 1.26 pk }
465 1.36 sommerfe /* FALLTHROUGH */
466 1.36 sommerfe case AF_UNSPEC:
467 1.43 thorpej s = splnet();
468 1.6 deraadt if (IF_QFULL(&ifp->if_snd)) {
469 1.6 deraadt IF_DROP(&ifp->if_snd);
470 1.6 deraadt m_freem(m0);
471 1.6 deraadt splx(s);
472 1.6 deraadt ifp->if_collisions++;
473 1.46 atatat simple_unlock(&tp->tun_lock);
474 1.6 deraadt return (ENOBUFS);
475 1.6 deraadt }
476 1.6 deraadt IF_ENQUEUE(&ifp->if_snd, m0);
477 1.6 deraadt splx(s);
478 1.6 deraadt ifp->if_opackets++;
479 1.6 deraadt break;
480 1.1 cgd #endif
481 1.6 deraadt default:
482 1.6 deraadt m_freem(m0);
483 1.46 atatat simple_unlock(&tp->tun_lock);
484 1.26 pk return (EAFNOSUPPORT);
485 1.6 deraadt }
486 1.6 deraadt
487 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
488 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
489 1.6 deraadt wakeup((caddr_t)tp);
490 1.6 deraadt }
491 1.6 deraadt if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
492 1.6 deraadt if (tp->tun_pgrp > 0)
493 1.6 deraadt gsignal(tp->tun_pgrp, SIGIO);
494 1.22 christos else if ((p = pfind(-tp->tun_pgrp)) != NULL)
495 1.6 deraadt psignal(p, SIGIO);
496 1.6 deraadt }
497 1.6 deraadt selwakeup(&tp->tun_rsel);
498 1.46 atatat simple_unlock(&tp->tun_lock);
499 1.26 pk return (0);
500 1.1 cgd }
501 1.1 cgd
502 1.1 cgd /*
503 1.1 cgd * the cdevsw interface is now pretty minimal.
504 1.1 cgd */
505 1.6 deraadt int
506 1.20 mycroft tunioctl(dev, cmd, data, flag, p)
507 1.6 deraadt dev_t dev;
508 1.15 cgd u_long cmd;
509 1.6 deraadt caddr_t data;
510 1.6 deraadt int flag;
511 1.12 deraadt struct proc *p;
512 1.6 deraadt {
513 1.46 atatat int s;
514 1.46 atatat struct tun_softc *tp;
515 1.46 atatat
516 1.46 atatat tp = tun_find_unit(dev);
517 1.46 atatat
518 1.46 atatat /* interface was "destroyed" already */
519 1.46 atatat if (tp == NULL)
520 1.46 atatat return (ENXIO);
521 1.6 deraadt
522 1.6 deraadt switch (cmd) {
523 1.6 deraadt case TUNSDEBUG:
524 1.6 deraadt tundebug = *(int *)data;
525 1.6 deraadt break;
526 1.26 pk
527 1.6 deraadt case TUNGDEBUG:
528 1.6 deraadt *(int *)data = tundebug;
529 1.6 deraadt break;
530 1.26 pk
531 1.26 pk case TUNSIFMODE:
532 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
533 1.26 pk case IFF_POINTOPOINT:
534 1.26 pk case IFF_BROADCAST:
535 1.43 thorpej s = splnet();
536 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
537 1.26 pk splx(s);
538 1.46 atatat simple_unlock(&tp->tun_lock);
539 1.26 pk return (EBUSY);
540 1.26 pk }
541 1.26 pk tp->tun_if.if_flags &=
542 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
543 1.26 pk tp->tun_if.if_flags |= *(int *)data;
544 1.26 pk splx(s);
545 1.26 pk break;
546 1.26 pk default:
547 1.46 atatat simple_unlock(&tp->tun_lock);
548 1.26 pk return (EINVAL);
549 1.26 pk break;
550 1.26 pk }
551 1.26 pk break;
552 1.26 pk
553 1.26 pk case TUNSLMODE:
554 1.26 pk if (*(int *)data)
555 1.26 pk tp->tun_flags |= TUN_PREPADDR;
556 1.26 pk else
557 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
558 1.26 pk break;
559 1.26 pk
560 1.6 deraadt case FIONBIO:
561 1.6 deraadt if (*(int *)data)
562 1.6 deraadt tp->tun_flags |= TUN_NBIO;
563 1.6 deraadt else
564 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
565 1.6 deraadt break;
566 1.26 pk
567 1.6 deraadt case FIOASYNC:
568 1.6 deraadt if (*(int *)data)
569 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
570 1.6 deraadt else
571 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
572 1.6 deraadt break;
573 1.26 pk
574 1.6 deraadt case FIONREAD:
575 1.43 thorpej s = splnet();
576 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
577 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
578 1.6 deraadt else
579 1.6 deraadt *(int *)data = 0;
580 1.6 deraadt splx(s);
581 1.6 deraadt break;
582 1.26 pk
583 1.6 deraadt case TIOCSPGRP:
584 1.6 deraadt tp->tun_pgrp = *(int *)data;
585 1.6 deraadt break;
586 1.26 pk
587 1.6 deraadt case TIOCGPGRP:
588 1.6 deraadt *(int *)data = tp->tun_pgrp;
589 1.6 deraadt break;
590 1.26 pk
591 1.6 deraadt default:
592 1.46 atatat simple_unlock(&tp->tun_lock);
593 1.6 deraadt return (ENOTTY);
594 1.6 deraadt }
595 1.46 atatat simple_unlock(&tp->tun_lock);
596 1.6 deraadt return (0);
597 1.1 cgd }
598 1.1 cgd
599 1.1 cgd /*
600 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
601 1.6 deraadt * least as much of a packet as can be read.
602 1.1 cgd */
603 1.6 deraadt int
604 1.22 christos tunread(dev, uio, ioflag)
605 1.6 deraadt dev_t dev;
606 1.6 deraadt struct uio *uio;
607 1.22 christos int ioflag;
608 1.6 deraadt {
609 1.46 atatat struct tun_softc *tp;
610 1.46 atatat struct ifnet *ifp;
611 1.6 deraadt struct mbuf *m, *m0;
612 1.46 atatat int error=0, len, s, index;
613 1.46 atatat
614 1.46 atatat tp = tun_find_unit(dev);
615 1.46 atatat
616 1.46 atatat /* interface was "destroyed" already */
617 1.46 atatat if (tp == NULL)
618 1.46 atatat return (ENXIO);
619 1.46 atatat
620 1.46 atatat index = tp->tun_if.if_index;
621 1.46 atatat ifp = &tp->tun_if;
622 1.6 deraadt
623 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
624 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
625 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
626 1.46 atatat simple_unlock(&tp->tun_lock);
627 1.8 deraadt return EHOSTDOWN;
628 1.8 deraadt }
629 1.8 deraadt
630 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
631 1.6 deraadt
632 1.43 thorpej s = splnet();
633 1.6 deraadt do {
634 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m0);
635 1.6 deraadt if (m0 == 0) {
636 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
637 1.6 deraadt splx(s);
638 1.46 atatat simple_unlock(&tp->tun_lock);
639 1.26 pk return (EWOULDBLOCK);
640 1.6 deraadt }
641 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
642 1.46 atatat simple_unlock(&tp->tun_lock);
643 1.26 pk if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
644 1.26 pk splx(s);
645 1.26 pk return (EINTR);
646 1.46 atatat } else {
647 1.46 atatat /*
648 1.46 atatat * Maybe the interface was destroyed while
649 1.46 atatat * we were sleeping, so let's ensure that
650 1.46 atatat * we're looking at the same (valid) tun
651 1.46 atatat * interface before looping.
652 1.46 atatat */
653 1.46 atatat tp = tun_find_unit(dev);
654 1.46 atatat if (tp == NULL ||
655 1.46 atatat tp->tun_if.if_index != index) {
656 1.46 atatat splx(s);
657 1.46 atatat if (tp)
658 1.46 atatat simple_unlock(&tp->tun_lock);
659 1.46 atatat return (ENXIO);
660 1.46 atatat }
661 1.26 pk }
662 1.6 deraadt }
663 1.6 deraadt } while (m0 == 0);
664 1.6 deraadt splx(s);
665 1.6 deraadt
666 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
667 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
668 1.45 itojun if (len != 0)
669 1.45 itojun error = uiomove(mtod(m0, caddr_t), len, uio);
670 1.6 deraadt MFREE(m0, m);
671 1.6 deraadt m0 = m;
672 1.6 deraadt }
673 1.6 deraadt
674 1.6 deraadt if (m0) {
675 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
676 1.6 deraadt m_freem(m0);
677 1.6 deraadt }
678 1.19 pk if (error)
679 1.19 pk ifp->if_ierrors++;
680 1.46 atatat simple_unlock(&tp->tun_lock);
681 1.26 pk return (error);
682 1.1 cgd }
683 1.1 cgd
684 1.1 cgd /*
685 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
686 1.1 cgd */
687 1.6 deraadt int
688 1.22 christos tunwrite(dev, uio, ioflag)
689 1.8 deraadt dev_t dev;
690 1.6 deraadt struct uio *uio;
691 1.22 christos int ioflag;
692 1.6 deraadt {
693 1.46 atatat struct tun_softc *tp;
694 1.46 atatat struct ifnet *ifp;
695 1.6 deraadt struct mbuf *top, **mp, *m;
696 1.26 pk struct ifqueue *ifq;
697 1.26 pk struct sockaddr dst;
698 1.26 pk int isr, error=0, s, tlen, mlen;
699 1.6 deraadt
700 1.46 atatat tp = tun_find_unit(dev);
701 1.46 atatat
702 1.46 atatat /* interface was "destroyed" already */
703 1.46 atatat if (tp == NULL)
704 1.46 atatat return (ENXIO);
705 1.46 atatat
706 1.46 atatat ifp = &tp->tun_if;
707 1.46 atatat
708 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
709 1.6 deraadt
710 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
711 1.46 atatat if (uio->uio_resid < sizeof(dst)) {
712 1.46 atatat simple_unlock(&tp->tun_lock);
713 1.26 pk return (EIO);
714 1.46 atatat }
715 1.26 pk error = uiomove((caddr_t)&dst, sizeof(dst), uio);
716 1.26 pk if (dst.sa_len > sizeof(dst)) {
717 1.26 pk /* Duh.. */
718 1.26 pk char discard;
719 1.26 pk int n = dst.sa_len - sizeof(dst);
720 1.26 pk while (n--)
721 1.46 atatat if ((error = uiomove(&discard, 1, uio)) != 0) {
722 1.46 atatat simple_unlock(&tp->tun_lock);
723 1.26 pk return (error);
724 1.46 atatat }
725 1.26 pk }
726 1.26 pk } else {
727 1.26 pk #ifdef INET
728 1.26 pk dst.sa_family = AF_INET;
729 1.26 pk #endif
730 1.26 pk }
731 1.26 pk
732 1.6 deraadt if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
733 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
734 1.37 mjacob (unsigned long)uio->uio_resid);
735 1.46 atatat simple_unlock(&tp->tun_lock);
736 1.26 pk return (EIO);
737 1.6 deraadt }
738 1.26 pk
739 1.26 pk switch (dst.sa_family) {
740 1.26 pk #ifdef INET
741 1.26 pk case AF_INET:
742 1.26 pk ifq = &ipintrq;
743 1.26 pk isr = NETISR_IP;
744 1.26 pk break;
745 1.26 pk #endif
746 1.26 pk default:
747 1.46 atatat simple_unlock(&tp->tun_lock);
748 1.26 pk return (EAFNOSUPPORT);
749 1.26 pk }
750 1.26 pk
751 1.8 deraadt tlen = uio->uio_resid;
752 1.8 deraadt
753 1.8 deraadt /* get a header mbuf */
754 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
755 1.46 atatat if (m == NULL) {
756 1.46 atatat simple_unlock(&tp->tun_lock);
757 1.26 pk return (ENOBUFS);
758 1.46 atatat }
759 1.8 deraadt mlen = MHLEN;
760 1.8 deraadt
761 1.6 deraadt top = 0;
762 1.6 deraadt mp = ⊤
763 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
764 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
765 1.8 deraadt error = uiomove(mtod (m, caddr_t), m->m_len, uio);
766 1.6 deraadt *mp = m;
767 1.6 deraadt mp = &m->m_next;
768 1.8 deraadt if (uio->uio_resid > 0) {
769 1.8 deraadt MGET (m, M_DONTWAIT, MT_DATA);
770 1.8 deraadt if (m == 0) {
771 1.8 deraadt error = ENOBUFS;
772 1.8 deraadt break;
773 1.8 deraadt }
774 1.8 deraadt mlen = MLEN;
775 1.8 deraadt }
776 1.6 deraadt }
777 1.6 deraadt if (error) {
778 1.6 deraadt if (top)
779 1.8 deraadt m_freem (top);
780 1.19 pk ifp->if_ierrors++;
781 1.46 atatat simple_unlock(&tp->tun_lock);
782 1.26 pk return (error);
783 1.6 deraadt }
784 1.6 deraadt
785 1.8 deraadt top->m_pkthdr.len = tlen;
786 1.8 deraadt top->m_pkthdr.rcvif = ifp;
787 1.8 deraadt
788 1.8 deraadt #if NBPFILTER > 0
789 1.40 thorpej if (ifp->if_bpf) {
790 1.8 deraadt /*
791 1.8 deraadt * We need to prepend the address family as
792 1.8 deraadt * a four byte field. Cons up a dummy header
793 1.8 deraadt * to pacify bpf. This is safe because bpf
794 1.8 deraadt * will only read from the mbuf (i.e., it won't
795 1.8 deraadt * try to free it or keep a pointer to it).
796 1.8 deraadt */
797 1.8 deraadt struct mbuf m;
798 1.16 cgd u_int32_t af = AF_INET;
799 1.8 deraadt
800 1.8 deraadt m.m_next = top;
801 1.16 cgd m.m_len = sizeof(af);
802 1.8 deraadt m.m_data = (char *)⁡
803 1.8 deraadt
804 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
805 1.6 deraadt }
806 1.8 deraadt #endif
807 1.6 deraadt
808 1.43 thorpej s = splnet();
809 1.26 pk if (IF_QFULL(ifq)) {
810 1.26 pk IF_DROP(ifq);
811 1.6 deraadt splx(s);
812 1.6 deraadt ifp->if_collisions++;
813 1.6 deraadt m_freem(top);
814 1.46 atatat simple_unlock(&tp->tun_lock);
815 1.26 pk return (ENOBUFS);
816 1.6 deraadt }
817 1.26 pk IF_ENQUEUE(ifq, top);
818 1.6 deraadt splx(s);
819 1.6 deraadt ifp->if_ipackets++;
820 1.26 pk schednetisr(isr);
821 1.46 atatat simple_unlock(&tp->tun_lock);
822 1.26 pk return (error);
823 1.1 cgd }
824 1.1 cgd
825 1.1 cgd /*
826 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
827 1.6 deraadt * really. The write detect always returns true, write never blocks
828 1.6 deraadt * anyway, it either accepts the packet or drops it.
829 1.6 deraadt */
830 1.6 deraadt int
831 1.27 mycroft tunpoll(dev, events, p)
832 1.6 deraadt dev_t dev;
833 1.27 mycroft int events;
834 1.22 christos struct proc *p;
835 1.6 deraadt {
836 1.46 atatat struct tun_softc *tp;
837 1.46 atatat struct ifnet *ifp;
838 1.46 atatat int s, revents = 0;
839 1.46 atatat
840 1.46 atatat tp = tun_find_unit(dev);
841 1.46 atatat
842 1.46 atatat /* interface was "destroyed" already */
843 1.46 atatat if (tp == NULL)
844 1.46 atatat return (0);
845 1.46 atatat
846 1.46 atatat ifp = &tp->tun_if;
847 1.6 deraadt
848 1.43 thorpej s = splnet();
849 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
850 1.6 deraadt
851 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
852 1.6 deraadt if (ifp->if_snd.ifq_len > 0) {
853 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
854 1.24 thorpej ifp->if_snd.ifq_len);
855 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
856 1.27 mycroft } else {
857 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
858 1.27 mycroft selrecord(p, &tp->tun_rsel);
859 1.6 deraadt }
860 1.35 veego }
861 1.27 mycroft
862 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
863 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
864 1.27 mycroft
865 1.6 deraadt splx(s);
866 1.46 atatat simple_unlock(&tp->tun_lock);
867 1.27 mycroft return (revents);
868 1.1 cgd }
869 1.8 deraadt
870 1.8 deraadt #endif /* NTUN */
871