if_tun.c revision 1.43.2.6 1 1.43.2.6 thorpej /* $NetBSD: if_tun.c,v 1.43.2.6 2002/01/10 20:02:16 thorpej 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.43.2.6 thorpej #include <sys/cdefs.h>
18 1.43.2.6 thorpej __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.43.2.6 2002/01/10 20:02:16 thorpej Exp $");
19 1.43.2.6 thorpej
20 1.8 deraadt #include "tun.h"
21 1.33 jonathan
22 1.33 jonathan #include "opt_inet.h"
23 1.34 jonathan #include "opt_ns.h"
24 1.8 deraadt
25 1.6 deraadt #include <sys/param.h>
26 1.8 deraadt #include <sys/proc.h>
27 1.6 deraadt #include <sys/systm.h>
28 1.6 deraadt #include <sys/mbuf.h>
29 1.6 deraadt #include <sys/buf.h>
30 1.6 deraadt #include <sys/protosw.h>
31 1.6 deraadt #include <sys/socket.h>
32 1.6 deraadt #include <sys/ioctl.h>
33 1.6 deraadt #include <sys/errno.h>
34 1.6 deraadt #include <sys/syslog.h>
35 1.6 deraadt #include <sys/select.h>
36 1.27 mycroft #include <sys/poll.h>
37 1.8 deraadt #include <sys/file.h>
38 1.22 christos #include <sys/signalvar.h>
39 1.23 christos #include <sys/conf.h>
40 1.9 deraadt
41 1.9 deraadt #include <machine/cpu.h>
42 1.6 deraadt
43 1.6 deraadt #include <net/if.h>
44 1.30 is #include <net/if_ether.h>
45 1.6 deraadt #include <net/netisr.h>
46 1.6 deraadt #include <net/route.h>
47 1.1 cgd
48 1.30 is
49 1.1 cgd #ifdef INET
50 1.6 deraadt #include <netinet/in.h>
51 1.6 deraadt #include <netinet/in_systm.h>
52 1.6 deraadt #include <netinet/in_var.h>
53 1.6 deraadt #include <netinet/ip.h>
54 1.30 is #include <netinet/if_inarp.h>
55 1.1 cgd #endif
56 1.1 cgd
57 1.1 cgd #ifdef NS
58 1.6 deraadt #include <netns/ns.h>
59 1.6 deraadt #include <netns/ns_if.h>
60 1.1 cgd #endif
61 1.1 cgd
62 1.8 deraadt #include "bpfilter.h"
63 1.8 deraadt #if NBPFILTER > 0
64 1.8 deraadt #include <sys/time.h>
65 1.8 deraadt #include <net/bpf.h>
66 1.8 deraadt #endif
67 1.8 deraadt
68 1.8 deraadt #include <net/if_tun.h>
69 1.8 deraadt
70 1.29 christos #define TUNDEBUG if (tundebug) printf
71 1.6 deraadt int tundebug = 0;
72 1.1 cgd
73 1.6 deraadt extern int ifqmaxlen;
74 1.22 christos void tunattach __P((int));
75 1.43.2.6 thorpej LIST_HEAD(, tun_softc) tun_softc_list;
76 1.43.2.6 thorpej static struct simplelock tun_softc_lock;
77 1.6 deraadt
78 1.22 christos int tun_ioctl __P((struct ifnet *, u_long, caddr_t));
79 1.22 christos int tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
80 1.22 christos struct rtentry *rt));
81 1.43.2.6 thorpej int tun_clone_create __P((struct if_clone *, int));
82 1.43.2.6 thorpej void tun_clone_destroy __P((struct ifnet *));
83 1.43.2.6 thorpej
84 1.43.2.6 thorpej struct if_clone tun_cloner =
85 1.43.2.6 thorpej IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
86 1.6 deraadt
87 1.43.2.6 thorpej static void tunattach0 __P((struct tun_softc *));
88 1.26 pk static void tuninit __P((struct tun_softc *));
89 1.43.2.6 thorpej static struct tun_softc *tun_find_unit __P((dev_t));
90 1.6 deraadt
91 1.8 deraadt void
92 1.8 deraadt tunattach(unused)
93 1.8 deraadt int unused;
94 1.8 deraadt {
95 1.8 deraadt
96 1.43.2.6 thorpej simple_lock_init(&tun_softc_lock);
97 1.43.2.6 thorpej LIST_INIT(&tun_softc_list);
98 1.43.2.6 thorpej if_clone_attach(&tun_cloner);
99 1.43.2.6 thorpej }
100 1.43.2.6 thorpej
101 1.43.2.6 thorpej int
102 1.43.2.6 thorpej tun_clone_create(ifc, unit)
103 1.43.2.6 thorpej struct if_clone *ifc;
104 1.43.2.6 thorpej int unit;
105 1.43.2.6 thorpej {
106 1.43.2.6 thorpej struct tun_softc *sc;
107 1.43.2.6 thorpej
108 1.43.2.6 thorpej sc = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
109 1.43.2.6 thorpej (void)memset(sc, 0, sizeof(struct tun_softc));
110 1.43.2.6 thorpej
111 1.43.2.6 thorpej (void)snprintf(sc->tun_if.if_xname, sizeof(sc->tun_if.if_xname),
112 1.43.2.6 thorpej "%s%d", ifc->ifc_name, unit);
113 1.43.2.6 thorpej sc->tun_unit = unit;
114 1.43.2.6 thorpej simple_lock_init(&sc->tun_lock);
115 1.43.2.6 thorpej
116 1.43.2.6 thorpej tunattach0(sc);
117 1.43.2.6 thorpej
118 1.43.2.6 thorpej simple_lock(&tun_softc_lock);
119 1.43.2.6 thorpej LIST_INSERT_HEAD(&tun_softc_list, sc, tun_list);
120 1.43.2.6 thorpej simple_unlock(&tun_softc_lock);
121 1.43.2.6 thorpej
122 1.43.2.6 thorpej return (0);
123 1.43.2.6 thorpej }
124 1.43.2.6 thorpej
125 1.43.2.6 thorpej void
126 1.43.2.6 thorpej tunattach0(sc)
127 1.43.2.6 thorpej struct tun_softc *sc;
128 1.43.2.6 thorpej {
129 1.43.2.6 thorpej struct ifnet *ifp = (void *)sc;
130 1.43.2.6 thorpej
131 1.43.2.6 thorpej sc->tun_flags = TUN_INITED;
132 1.8 deraadt
133 1.43.2.6 thorpej ifp = &sc->tun_if;
134 1.43.2.6 thorpej ifp->if_softc = sc;
135 1.43.2.6 thorpej ifp->if_mtu = TUNMTU;
136 1.43.2.6 thorpej ifp->if_ioctl = tun_ioctl;
137 1.43.2.6 thorpej ifp->if_output = tun_output;
138 1.43.2.6 thorpej ifp->if_flags = IFF_POINTOPOINT;
139 1.43.2.6 thorpej ifp->if_snd.ifq_maxlen = ifqmaxlen;
140 1.43.2.6 thorpej ifp->if_collisions = 0;
141 1.43.2.6 thorpej ifp->if_ierrors = 0;
142 1.43.2.6 thorpej ifp->if_oerrors = 0;
143 1.43.2.6 thorpej ifp->if_ipackets = 0;
144 1.43.2.6 thorpej ifp->if_opackets = 0;
145 1.43.2.6 thorpej ifp->if_dlt = DLT_NULL;
146 1.43.2.6 thorpej if_attach(ifp);
147 1.43.2.6 thorpej if_alloc_sadl(ifp);
148 1.8 deraadt #if NBPFILTER > 0
149 1.43.2.6 thorpej bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
150 1.8 deraadt #endif
151 1.43.2.6 thorpej }
152 1.43.2.6 thorpej
153 1.43.2.6 thorpej void
154 1.43.2.6 thorpej tun_clone_destroy(ifp)
155 1.43.2.6 thorpej struct ifnet *ifp;
156 1.43.2.6 thorpej {
157 1.43.2.6 thorpej struct tun_softc *tp = (void *)ifp;
158 1.43.2.6 thorpej struct proc *p;
159 1.43.2.6 thorpej
160 1.43.2.6 thorpej simple_lock(&tun_softc_lock);
161 1.43.2.6 thorpej simple_lock(&tp->tun_lock);
162 1.43.2.6 thorpej LIST_REMOVE(tp, tun_list);
163 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
164 1.43.2.6 thorpej simple_unlock(&tun_softc_lock);
165 1.43.2.6 thorpej
166 1.43.2.6 thorpej if (tp->tun_flags & TUN_RWAIT) {
167 1.43.2.6 thorpej tp->tun_flags &= ~TUN_RWAIT;
168 1.43.2.6 thorpej wakeup((caddr_t)tp);
169 1.43.2.6 thorpej }
170 1.43.2.6 thorpej if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
171 1.43.2.6 thorpej if (tp->tun_pgrp > 0)
172 1.43.2.6 thorpej gsignal(tp->tun_pgrp, SIGIO);
173 1.43.2.6 thorpej else if ((p = pfind(-tp->tun_pgrp)) != NULL)
174 1.43.2.6 thorpej psignal(p, SIGIO);
175 1.8 deraadt }
176 1.43.2.6 thorpej selwakeup(&tp->tun_rsel);
177 1.43.2.6 thorpej
178 1.43.2.6 thorpej #if NBPFILTER > 0
179 1.43.2.6 thorpej bpfdetach(ifp);
180 1.43.2.6 thorpej #endif
181 1.43.2.6 thorpej if_detach(ifp);
182 1.43.2.6 thorpej
183 1.43.2.6 thorpej free(tp, M_DEVBUF);
184 1.43.2.6 thorpej }
185 1.43.2.6 thorpej
186 1.43.2.6 thorpej static struct tun_softc *
187 1.43.2.6 thorpej tun_find_unit(dev)
188 1.43.2.6 thorpej dev_t dev;
189 1.43.2.6 thorpej {
190 1.43.2.6 thorpej struct tun_softc *tp;
191 1.43.2.6 thorpej int unit = minor(dev);
192 1.43.2.6 thorpej
193 1.43.2.6 thorpej simple_lock(&tun_softc_lock);
194 1.43.2.6 thorpej LIST_FOREACH(tp, &tun_softc_list, tun_list)
195 1.43.2.6 thorpej if (unit == tp->tun_unit)
196 1.43.2.6 thorpej break;
197 1.43.2.6 thorpej if (tp)
198 1.43.2.6 thorpej simple_lock(&tp->tun_lock);
199 1.43.2.6 thorpej simple_unlock(&tun_softc_lock);
200 1.43.2.6 thorpej
201 1.43.2.6 thorpej return (tp);
202 1.8 deraadt }
203 1.8 deraadt
204 1.6 deraadt /*
205 1.6 deraadt * tunnel open - must be superuser & the device must be
206 1.6 deraadt * configured in
207 1.6 deraadt */
208 1.6 deraadt int
209 1.6 deraadt tunopen(dev, flag, mode, p)
210 1.6 deraadt dev_t dev;
211 1.6 deraadt int flag, mode;
212 1.6 deraadt struct proc *p;
213 1.6 deraadt {
214 1.6 deraadt struct ifnet *ifp;
215 1.8 deraadt struct tun_softc *tp;
216 1.43.2.6 thorpej int error;
217 1.1 cgd
218 1.22 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
219 1.5 deraadt return (error);
220 1.5 deraadt
221 1.43.2.6 thorpej if (NTUN < 1)
222 1.43.2.6 thorpej return (ENXIO);
223 1.43.2.6 thorpej
224 1.43.2.6 thorpej tp = tun_find_unit(dev);
225 1.43.2.6 thorpej
226 1.43.2.6 thorpej if (!tp)
227 1.6 deraadt return (ENXIO);
228 1.43.2.6 thorpej
229 1.43.2.6 thorpej if (tp->tun_flags & TUN_OPEN) {
230 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
231 1.43.2.6 thorpej return (EBUSY);
232 1.43.2.6 thorpej }
233 1.43.2.6 thorpej
234 1.6 deraadt ifp = &tp->tun_if;
235 1.6 deraadt tp->tun_flags |= TUN_OPEN;
236 1.24 thorpej TUNDEBUG("%s: open\n", ifp->if_xname);
237 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
238 1.6 deraadt return (0);
239 1.1 cgd }
240 1.1 cgd
241 1.6 deraadt /*
242 1.6 deraadt * tunclose - close the device - mark i/f down & delete
243 1.6 deraadt * routing info
244 1.6 deraadt */
245 1.6 deraadt int
246 1.22 christos tunclose(dev, flag, mode, p)
247 1.6 deraadt dev_t dev;
248 1.6 deraadt int flag;
249 1.22 christos int mode;
250 1.22 christos struct proc *p;
251 1.6 deraadt {
252 1.43.2.6 thorpej int s;
253 1.43.2.6 thorpej struct tun_softc *tp;
254 1.43.2.6 thorpej struct ifnet *ifp;
255 1.6 deraadt struct mbuf *m;
256 1.6 deraadt
257 1.43.2.6 thorpej tp = tun_find_unit(dev);
258 1.43.2.6 thorpej
259 1.43.2.6 thorpej /* interface was "destroyed" before the close */
260 1.43.2.6 thorpej if (tp == NULL)
261 1.43.2.6 thorpej return (0);
262 1.43.2.6 thorpej
263 1.43.2.6 thorpej ifp = &tp->tun_if;
264 1.43.2.6 thorpej
265 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
266 1.6 deraadt
267 1.6 deraadt /*
268 1.6 deraadt * junk all pending output
269 1.6 deraadt */
270 1.6 deraadt do {
271 1.43 thorpej s = splnet();
272 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m);
273 1.6 deraadt splx(s);
274 1.6 deraadt if (m)
275 1.6 deraadt m_freem(m);
276 1.6 deraadt } while (m);
277 1.6 deraadt
278 1.6 deraadt if (ifp->if_flags & IFF_UP) {
279 1.43 thorpej s = splnet();
280 1.6 deraadt if_down(ifp);
281 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
282 1.17 mycroft /* find internet addresses and delete routes */
283 1.39 augustss struct ifaddr *ifa;
284 1.43.2.6 thorpej TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
285 1.38 itojun #ifdef INET
286 1.18 mycroft if (ifa->ifa_addr->sa_family == AF_INET) {
287 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
288 1.26 pk tp->tun_flags & TUN_DSTADDR
289 1.26 pk ? RTF_HOST
290 1.26 pk : 0);
291 1.18 mycroft }
292 1.38 itojun #endif
293 1.11 deraadt }
294 1.8 deraadt }
295 1.6 deraadt splx(s);
296 1.6 deraadt }
297 1.6 deraadt tp->tun_pgrp = 0;
298 1.43.2.4 thorpej selnotify(&tp->tun_rsel, 0);
299 1.6 deraadt
300 1.24 thorpej TUNDEBUG ("%s: closed\n", ifp->if_xname);
301 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
302 1.6 deraadt return (0);
303 1.1 cgd }
304 1.1 cgd
305 1.26 pk static void
306 1.24 thorpej tuninit(tp)
307 1.24 thorpej struct tun_softc *tp;
308 1.6 deraadt {
309 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
310 1.43.2.6 thorpej struct ifaddr *ifa;
311 1.8 deraadt
312 1.24 thorpej TUNDEBUG("%s: tuninit\n", ifp->if_xname);
313 1.6 deraadt
314 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
315 1.8 deraadt
316 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
317 1.43.2.6 thorpej TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
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.43.2.6 thorpej struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
349 1.43.2.6 thorpej
350 1.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.4 thorpej selnotify(&tp->tun_rsel, 0);
498 1.43.2.6 thorpej 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.43.2.6 thorpej int s;
514 1.43.2.6 thorpej struct tun_softc *tp;
515 1.43.2.6 thorpej
516 1.43.2.6 thorpej tp = tun_find_unit(dev);
517 1.43.2.6 thorpej
518 1.43.2.6 thorpej /* interface was "destroyed" already */
519 1.43.2.6 thorpej if (tp == NULL)
520 1.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej simple_unlock(&tp->tun_lock);
593 1.6 deraadt return (ENOTTY);
594 1.6 deraadt }
595 1.43.2.6 thorpej 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.43.2.6 thorpej struct tun_softc *tp;
610 1.43.2.6 thorpej struct ifnet *ifp;
611 1.6 deraadt struct mbuf *m, *m0;
612 1.43.2.6 thorpej int error=0, len, s, index;
613 1.43.2.6 thorpej
614 1.43.2.6 thorpej tp = tun_find_unit(dev);
615 1.43.2.6 thorpej
616 1.43.2.6 thorpej /* interface was "destroyed" already */
617 1.43.2.6 thorpej if (tp == NULL)
618 1.43.2.6 thorpej return (ENXIO);
619 1.43.2.6 thorpej
620 1.43.2.6 thorpej index = tp->tun_if.if_index;
621 1.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej } else {
647 1.43.2.6 thorpej /*
648 1.43.2.6 thorpej * Maybe the interface was destroyed while
649 1.43.2.6 thorpej * we were sleeping, so let's ensure that
650 1.43.2.6 thorpej * we're looking at the same (valid) tun
651 1.43.2.6 thorpej * interface before looping.
652 1.43.2.6 thorpej */
653 1.43.2.6 thorpej tp = tun_find_unit(dev);
654 1.43.2.6 thorpej if (tp == NULL ||
655 1.43.2.6 thorpej tp->tun_if.if_index != index) {
656 1.43.2.6 thorpej splx(s);
657 1.43.2.6 thorpej if (tp)
658 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
659 1.43.2.6 thorpej return (ENXIO);
660 1.43.2.6 thorpej }
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.43.2.2 thorpej if (len != 0)
669 1.43.2.2 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej struct tun_softc *tp;
694 1.43.2.6 thorpej 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.43.2.6 thorpej tp = tun_find_unit(dev);
701 1.43.2.6 thorpej
702 1.43.2.6 thorpej /* interface was "destroyed" already */
703 1.43.2.6 thorpej if (tp == NULL)
704 1.43.2.6 thorpej return (ENXIO);
705 1.43.2.6 thorpej
706 1.43.2.6 thorpej ifp = &tp->tun_if;
707 1.43.2.6 thorpej
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.43.2.6 thorpej if (uio->uio_resid < sizeof(dst)) {
712 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
713 1.26 pk return (EIO);
714 1.43.2.6 thorpej }
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.43.2.6 thorpej if ((error = uiomove(&discard, 1, uio)) != 0) {
722 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
723 1.26 pk return (error);
724 1.43.2.6 thorpej }
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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej if (m == NULL) {
756 1.43.2.6 thorpej simple_unlock(&tp->tun_lock);
757 1.26 pk return (ENOBUFS);
758 1.43.2.6 thorpej }
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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej 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.43.2.6 thorpej struct tun_softc *tp;
837 1.43.2.6 thorpej struct ifnet *ifp;
838 1.43.2.6 thorpej int s, revents = 0;
839 1.43.2.6 thorpej
840 1.43.2.6 thorpej tp = tun_find_unit(dev);
841 1.43.2.6 thorpej
842 1.43.2.6 thorpej /* interface was "destroyed" already */
843 1.43.2.6 thorpej if (tp == NULL)
844 1.43.2.6 thorpej return (0);
845 1.43.2.6 thorpej
846 1.43.2.6 thorpej 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.43.2.6 thorpej simple_unlock(&tp->tun_lock);
867 1.27 mycroft return (revents);
868 1.43.2.3 thorpej }
869 1.43.2.3 thorpej
870 1.43.2.3 thorpej static void
871 1.43.2.3 thorpej filt_tunrdetach(struct knote *kn)
872 1.43.2.3 thorpej {
873 1.43.2.3 thorpej struct tun_softc *tp = (void *) kn->kn_hook;
874 1.43.2.3 thorpej int s;
875 1.43.2.3 thorpej
876 1.43.2.3 thorpej s = splnet();
877 1.43.2.3 thorpej SLIST_REMOVE(&tp->tun_rsel.si_klist, kn, knote, kn_selnext);
878 1.43.2.3 thorpej splx(s);
879 1.43.2.3 thorpej }
880 1.43.2.3 thorpej
881 1.43.2.3 thorpej static int
882 1.43.2.3 thorpej filt_tunread(struct knote *kn, long hint)
883 1.43.2.3 thorpej {
884 1.43.2.3 thorpej struct tun_softc *tp = (void *) kn->kn_hook;
885 1.43.2.3 thorpej struct ifnet *ifp = &tp->tun_if;
886 1.43.2.3 thorpej struct mbuf *m;
887 1.43.2.3 thorpej int s;
888 1.43.2.3 thorpej
889 1.43.2.3 thorpej s = splnet();
890 1.43.2.3 thorpej IF_POLL(&ifp->if_snd, m);
891 1.43.2.3 thorpej if (m == NULL) {
892 1.43.2.3 thorpej splx(s);
893 1.43.2.3 thorpej return (0);
894 1.43.2.3 thorpej }
895 1.43.2.3 thorpej
896 1.43.2.3 thorpej for (kn->kn_data = 0; m != NULL; m = m->m_next)
897 1.43.2.3 thorpej kn->kn_data += m->m_len;
898 1.43.2.3 thorpej
899 1.43.2.3 thorpej splx(s);
900 1.43.2.3 thorpej return (1);
901 1.43.2.3 thorpej }
902 1.43.2.3 thorpej
903 1.43.2.3 thorpej static const struct filterops tunread_filtops =
904 1.43.2.3 thorpej { 1, NULL, filt_tunrdetach, filt_tunread };
905 1.43.2.3 thorpej
906 1.43.2.5 thorpej static const struct filterops tun_seltrue_filtops =
907 1.43.2.5 thorpej { 1, NULL, filt_tunrdetach, filt_seltrue };
908 1.43.2.5 thorpej
909 1.43.2.3 thorpej int
910 1.43.2.3 thorpej tunkqfilter(dev_t dev, struct knote *kn)
911 1.43.2.3 thorpej {
912 1.43.2.6 thorpej struct tun_softc *tp = tun_find_unit(dev);
913 1.43.2.3 thorpej struct klist *klist;
914 1.43.2.3 thorpej int s;
915 1.43.2.3 thorpej
916 1.43.2.3 thorpej switch (kn->kn_filter) {
917 1.43.2.3 thorpej case EVFILT_READ:
918 1.43.2.3 thorpej klist = &tp->tun_rsel.si_klist;
919 1.43.2.3 thorpej kn->kn_fop = &tunread_filtops;
920 1.43.2.5 thorpej break;
921 1.43.2.5 thorpej
922 1.43.2.5 thorpej case EVFILT_WRITE:
923 1.43.2.5 thorpej klist = &tp->tun_rsel.si_klist;
924 1.43.2.5 thorpej kn->kn_fop = &tun_seltrue_filtops;
925 1.43.2.3 thorpej break;
926 1.43.2.3 thorpej
927 1.43.2.3 thorpej default:
928 1.43.2.3 thorpej return (1);
929 1.43.2.3 thorpej }
930 1.43.2.3 thorpej
931 1.43.2.3 thorpej kn->kn_hook = (void *) tp;
932 1.43.2.3 thorpej
933 1.43.2.3 thorpej s = splnet();
934 1.43.2.3 thorpej SLIST_INSERT_HEAD(klist, kn, kn_selnext);
935 1.43.2.3 thorpej splx(s);
936 1.43.2.3 thorpej
937 1.43.2.3 thorpej return (0);
938 1.1 cgd }
939