if_tun.c revision 1.45.2.1 1 1.45.2.1 thorpej /* $NetBSD: if_tun.c,v 1.45.2.1 2001/09/07 04:45:42 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.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.45.2.1 thorpej #include <sys/vnode.h>
39 1.45.2.1 thorpej
40 1.45.2.1 thorpej #include <miscfs/specfs/specdev.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 struct tun_softc tunctl[NTUN];
75 1.6 deraadt extern int ifqmaxlen;
76 1.22 christos void tunattach __P((int));
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.6 deraadt
82 1.26 pk static void tuninit __P((struct tun_softc *));
83 1.6 deraadt
84 1.8 deraadt void
85 1.8 deraadt tunattach(unused)
86 1.8 deraadt int unused;
87 1.8 deraadt {
88 1.39 augustss int i;
89 1.8 deraadt struct ifnet *ifp;
90 1.8 deraadt
91 1.8 deraadt for (i = 0; i < NTUN; i++) {
92 1.8 deraadt tunctl[i].tun_flags = TUN_INITED;
93 1.8 deraadt
94 1.8 deraadt ifp = &tunctl[i].tun_if;
95 1.29 christos sprintf(ifp->if_xname, "tun%d", i);
96 1.24 thorpej ifp->if_softc = &tunctl[i];
97 1.8 deraadt ifp->if_mtu = TUNMTU;
98 1.22 christos ifp->if_ioctl = tun_ioctl;
99 1.22 christos ifp->if_output = tun_output;
100 1.8 deraadt ifp->if_flags = IFF_POINTOPOINT;
101 1.8 deraadt ifp->if_snd.ifq_maxlen = ifqmaxlen;
102 1.8 deraadt ifp->if_collisions = 0;
103 1.8 deraadt ifp->if_ierrors = 0;
104 1.8 deraadt ifp->if_oerrors = 0;
105 1.8 deraadt ifp->if_ipackets = 0;
106 1.8 deraadt ifp->if_opackets = 0;
107 1.41 thorpej ifp->if_dlt = DLT_NULL;
108 1.8 deraadt if_attach(ifp);
109 1.42 thorpej if_alloc_sadl(ifp);
110 1.8 deraadt #if NBPFILTER > 0
111 1.40 thorpej bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
112 1.8 deraadt #endif
113 1.8 deraadt }
114 1.8 deraadt }
115 1.8 deraadt
116 1.6 deraadt /*
117 1.6 deraadt * tunnel open - must be superuser & the device must be
118 1.6 deraadt * configured in
119 1.6 deraadt */
120 1.6 deraadt int
121 1.45.2.1 thorpej tunopen(devvp, flag, mode, p)
122 1.45.2.1 thorpej struct vnode *devvp;
123 1.6 deraadt int flag, mode;
124 1.6 deraadt struct proc *p;
125 1.6 deraadt {
126 1.6 deraadt struct ifnet *ifp;
127 1.8 deraadt struct tun_softc *tp;
128 1.39 augustss int unit, error;
129 1.1 cgd
130 1.22 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
131 1.5 deraadt return (error);
132 1.5 deraadt
133 1.45.2.1 thorpej if ((unit = minor(devvp->v_rdev)) >= NTUN)
134 1.6 deraadt return (ENXIO);
135 1.6 deraadt tp = &tunctl[unit];
136 1.45.2.1 thorpej
137 1.45.2.1 thorpej devvp->v_devcookie = tp;
138 1.45.2.1 thorpej
139 1.6 deraadt if (tp->tun_flags & TUN_OPEN)
140 1.6 deraadt return ENXIO;
141 1.6 deraadt ifp = &tp->tun_if;
142 1.6 deraadt tp->tun_flags |= TUN_OPEN;
143 1.24 thorpej TUNDEBUG("%s: open\n", ifp->if_xname);
144 1.6 deraadt return (0);
145 1.1 cgd }
146 1.1 cgd
147 1.6 deraadt /*
148 1.6 deraadt * tunclose - close the device - mark i/f down & delete
149 1.6 deraadt * routing info
150 1.6 deraadt */
151 1.6 deraadt int
152 1.45.2.1 thorpej tunclose(devvp, flag, mode, p)
153 1.45.2.1 thorpej struct vnode *devvp;
154 1.6 deraadt int flag;
155 1.22 christos int mode;
156 1.22 christos struct proc *p;
157 1.6 deraadt {
158 1.45.2.1 thorpej struct tun_softc *tp = devvp->v_devcookie;
159 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
160 1.6 deraadt struct mbuf *m;
161 1.45.2.1 thorpej int s;
162 1.6 deraadt
163 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
164 1.6 deraadt
165 1.6 deraadt /*
166 1.6 deraadt * junk all pending output
167 1.6 deraadt */
168 1.6 deraadt do {
169 1.43 thorpej s = splnet();
170 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m);
171 1.6 deraadt splx(s);
172 1.6 deraadt if (m)
173 1.6 deraadt m_freem(m);
174 1.6 deraadt } while (m);
175 1.6 deraadt
176 1.6 deraadt if (ifp->if_flags & IFF_UP) {
177 1.43 thorpej s = splnet();
178 1.6 deraadt if_down(ifp);
179 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
180 1.17 mycroft /* find internet addresses and delete routes */
181 1.39 augustss struct ifaddr *ifa;
182 1.17 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
183 1.18 mycroft ifa = ifa->ifa_list.tqe_next) {
184 1.38 itojun #ifdef INET
185 1.18 mycroft if (ifa->ifa_addr->sa_family == AF_INET) {
186 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
187 1.26 pk tp->tun_flags & TUN_DSTADDR
188 1.26 pk ? RTF_HOST
189 1.26 pk : 0);
190 1.18 mycroft }
191 1.38 itojun #endif
192 1.11 deraadt }
193 1.8 deraadt }
194 1.6 deraadt splx(s);
195 1.6 deraadt }
196 1.6 deraadt tp->tun_pgrp = 0;
197 1.7 deraadt selwakeup(&tp->tun_rsel);
198 1.6 deraadt
199 1.24 thorpej TUNDEBUG ("%s: closed\n", ifp->if_xname);
200 1.6 deraadt return (0);
201 1.1 cgd }
202 1.1 cgd
203 1.26 pk static void
204 1.24 thorpej tuninit(tp)
205 1.24 thorpej struct tun_softc *tp;
206 1.6 deraadt {
207 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
208 1.39 augustss struct ifaddr *ifa;
209 1.8 deraadt
210 1.24 thorpej TUNDEBUG("%s: tuninit\n", ifp->if_xname);
211 1.6 deraadt
212 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
213 1.8 deraadt
214 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
215 1.17 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
216 1.26 pk ifa = ifa->ifa_list.tqe_next) {
217 1.38 itojun #ifdef INET
218 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
219 1.17 mycroft struct sockaddr_in *sin;
220 1.11 deraadt
221 1.17 mycroft sin = satosin(ifa->ifa_addr);
222 1.17 mycroft if (sin && sin->sin_addr.s_addr)
223 1.17 mycroft tp->tun_flags |= TUN_IASET;
224 1.17 mycroft
225 1.26 pk if (ifp->if_flags & IFF_POINTOPOINT) {
226 1.26 pk sin = satosin(ifa->ifa_dstaddr);
227 1.26 pk if (sin && sin->sin_addr.s_addr)
228 1.26 pk tp->tun_flags |= TUN_DSTADDR;
229 1.26 pk }
230 1.11 deraadt }
231 1.38 itojun #endif
232 1.18 mycroft }
233 1.8 deraadt
234 1.26 pk return;
235 1.1 cgd }
236 1.1 cgd
237 1.1 cgd /*
238 1.1 cgd * Process an ioctl request.
239 1.1 cgd */
240 1.1 cgd int
241 1.22 christos tun_ioctl(ifp, cmd, data)
242 1.6 deraadt struct ifnet *ifp;
243 1.25 mycroft u_long cmd;
244 1.6 deraadt caddr_t data;
245 1.6 deraadt {
246 1.6 deraadt int error = 0, s;
247 1.6 deraadt
248 1.43 thorpej s = splnet();
249 1.6 deraadt switch(cmd) {
250 1.6 deraadt case SIOCSIFADDR:
251 1.24 thorpej tuninit((struct tun_softc *)(ifp->if_softc));
252 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
253 1.6 deraadt break;
254 1.6 deraadt case SIOCSIFDSTADDR:
255 1.24 thorpej tuninit((struct tun_softc *)(ifp->if_softc));
256 1.24 thorpej TUNDEBUG("%s: destination address set\n", ifp->if_xname);
257 1.6 deraadt break;
258 1.26 pk case SIOCSIFBRDADDR:
259 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
260 1.26 pk break;
261 1.31 matt case SIOCSIFMTU: {
262 1.31 matt struct ifreq *ifr = (struct ifreq *) data;
263 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
264 1.31 matt error = EINVAL;
265 1.31 matt break;
266 1.31 matt }
267 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
268 1.31 matt ifp->if_mtu = ifr->ifr_mtu;
269 1.32 matt break;
270 1.32 matt }
271 1.32 matt case SIOCADDMULTI:
272 1.32 matt case SIOCDELMULTI: {
273 1.32 matt struct ifreq *ifr = (struct ifreq *) data;
274 1.32 matt if (ifr == 0) {
275 1.32 matt error = EAFNOSUPPORT; /* XXX */
276 1.32 matt break;
277 1.32 matt }
278 1.32 matt switch (ifr->ifr_addr.sa_family) {
279 1.32 matt
280 1.32 matt #ifdef INET
281 1.32 matt case AF_INET:
282 1.32 matt break;
283 1.32 matt #endif
284 1.32 matt
285 1.32 matt default:
286 1.32 matt error = EAFNOSUPPORT;
287 1.32 matt break;
288 1.32 matt }
289 1.31 matt break;
290 1.31 matt }
291 1.38 itojun case SIOCSIFFLAGS:
292 1.38 itojun break;
293 1.6 deraadt default:
294 1.6 deraadt error = EINVAL;
295 1.6 deraadt }
296 1.6 deraadt splx(s);
297 1.6 deraadt return (error);
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd /*
301 1.22 christos * tun_output - queue packets from higher level ready to put out.
302 1.1 cgd */
303 1.6 deraadt int
304 1.22 christos tun_output(ifp, m0, dst, rt)
305 1.6 deraadt struct ifnet *ifp;
306 1.6 deraadt struct mbuf *m0;
307 1.6 deraadt struct sockaddr *dst;
308 1.12 deraadt struct rtentry *rt;
309 1.6 deraadt {
310 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
311 1.6 deraadt struct proc *p;
312 1.38 itojun #ifdef INET
313 1.6 deraadt int s;
314 1.38 itojun #endif
315 1.6 deraadt
316 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
317 1.1 cgd
318 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
319 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
320 1.24 thorpej tp->tun_flags);
321 1.8 deraadt m_freem (m0);
322 1.26 pk return (EHOSTDOWN);
323 1.8 deraadt }
324 1.8 deraadt
325 1.8 deraadt #if NBPFILTER > 0
326 1.40 thorpej if (ifp->if_bpf) {
327 1.8 deraadt /*
328 1.8 deraadt * We need to prepend the address family as
329 1.8 deraadt * a four byte field. Cons up a dummy header
330 1.8 deraadt * to pacify bpf. This is safe because bpf
331 1.8 deraadt * will only read from the mbuf (i.e., it won't
332 1.8 deraadt * try to free it or keep a pointer to it).
333 1.8 deraadt */
334 1.8 deraadt struct mbuf m;
335 1.16 cgd u_int32_t af = dst->sa_family;
336 1.8 deraadt
337 1.8 deraadt m.m_next = m0;
338 1.16 cgd m.m_len = sizeof(af);
339 1.8 deraadt m.m_data = (char *)⁡
340 1.8 deraadt
341 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
342 1.8 deraadt }
343 1.8 deraadt #endif
344 1.8 deraadt
345 1.6 deraadt switch(dst->sa_family) {
346 1.1 cgd #ifdef INET
347 1.6 deraadt case AF_INET:
348 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
349 1.26 pk /* Simple link-layer header */
350 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
351 1.26 pk if (m0 == NULL) {
352 1.26 pk IF_DROP(&ifp->if_snd);
353 1.26 pk return (ENOBUFS);
354 1.26 pk }
355 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
356 1.26 pk }
357 1.36 sommerfe /* FALLTHROUGH */
358 1.36 sommerfe case AF_UNSPEC:
359 1.43 thorpej s = splnet();
360 1.6 deraadt if (IF_QFULL(&ifp->if_snd)) {
361 1.6 deraadt IF_DROP(&ifp->if_snd);
362 1.6 deraadt m_freem(m0);
363 1.6 deraadt splx(s);
364 1.6 deraadt ifp->if_collisions++;
365 1.6 deraadt return (ENOBUFS);
366 1.6 deraadt }
367 1.6 deraadt IF_ENQUEUE(&ifp->if_snd, m0);
368 1.6 deraadt splx(s);
369 1.6 deraadt ifp->if_opackets++;
370 1.6 deraadt break;
371 1.1 cgd #endif
372 1.6 deraadt default:
373 1.6 deraadt m_freem(m0);
374 1.26 pk return (EAFNOSUPPORT);
375 1.6 deraadt }
376 1.6 deraadt
377 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
378 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
379 1.6 deraadt wakeup((caddr_t)tp);
380 1.6 deraadt }
381 1.6 deraadt if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
382 1.6 deraadt if (tp->tun_pgrp > 0)
383 1.6 deraadt gsignal(tp->tun_pgrp, SIGIO);
384 1.22 christos else if ((p = pfind(-tp->tun_pgrp)) != NULL)
385 1.6 deraadt psignal(p, SIGIO);
386 1.6 deraadt }
387 1.6 deraadt selwakeup(&tp->tun_rsel);
388 1.26 pk return (0);
389 1.1 cgd }
390 1.1 cgd
391 1.1 cgd /*
392 1.1 cgd * the cdevsw interface is now pretty minimal.
393 1.1 cgd */
394 1.6 deraadt int
395 1.45.2.1 thorpej tunioctl(devvp, cmd, data, flag, p)
396 1.45.2.1 thorpej struct vnode *devvp;
397 1.15 cgd u_long cmd;
398 1.6 deraadt caddr_t data;
399 1.6 deraadt int flag;
400 1.12 deraadt struct proc *p;
401 1.6 deraadt {
402 1.45.2.1 thorpej struct tun_softc *tp = devvp->v_devcookie;
403 1.45.2.1 thorpej int s;
404 1.6 deraadt
405 1.6 deraadt switch (cmd) {
406 1.6 deraadt case TUNSDEBUG:
407 1.6 deraadt tundebug = *(int *)data;
408 1.6 deraadt break;
409 1.26 pk
410 1.6 deraadt case TUNGDEBUG:
411 1.6 deraadt *(int *)data = tundebug;
412 1.6 deraadt break;
413 1.26 pk
414 1.26 pk case TUNSIFMODE:
415 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
416 1.26 pk case IFF_POINTOPOINT:
417 1.26 pk case IFF_BROADCAST:
418 1.43 thorpej s = splnet();
419 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
420 1.26 pk splx(s);
421 1.26 pk return (EBUSY);
422 1.26 pk }
423 1.26 pk tp->tun_if.if_flags &=
424 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
425 1.26 pk tp->tun_if.if_flags |= *(int *)data;
426 1.26 pk splx(s);
427 1.26 pk break;
428 1.26 pk default:
429 1.26 pk return (EINVAL);
430 1.26 pk break;
431 1.26 pk }
432 1.26 pk break;
433 1.26 pk
434 1.26 pk case TUNSLMODE:
435 1.26 pk if (*(int *)data)
436 1.26 pk tp->tun_flags |= TUN_PREPADDR;
437 1.26 pk else
438 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
439 1.26 pk break;
440 1.26 pk
441 1.6 deraadt case FIONBIO:
442 1.6 deraadt if (*(int *)data)
443 1.6 deraadt tp->tun_flags |= TUN_NBIO;
444 1.6 deraadt else
445 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
446 1.6 deraadt break;
447 1.26 pk
448 1.6 deraadt case FIOASYNC:
449 1.6 deraadt if (*(int *)data)
450 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
451 1.6 deraadt else
452 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
453 1.6 deraadt break;
454 1.26 pk
455 1.6 deraadt case FIONREAD:
456 1.43 thorpej s = splnet();
457 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
458 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
459 1.6 deraadt else
460 1.6 deraadt *(int *)data = 0;
461 1.6 deraadt splx(s);
462 1.6 deraadt break;
463 1.26 pk
464 1.6 deraadt case TIOCSPGRP:
465 1.6 deraadt tp->tun_pgrp = *(int *)data;
466 1.6 deraadt break;
467 1.26 pk
468 1.6 deraadt case TIOCGPGRP:
469 1.6 deraadt *(int *)data = tp->tun_pgrp;
470 1.6 deraadt break;
471 1.26 pk
472 1.6 deraadt default:
473 1.6 deraadt return (ENOTTY);
474 1.6 deraadt }
475 1.6 deraadt return (0);
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd /*
479 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
480 1.6 deraadt * least as much of a packet as can be read.
481 1.1 cgd */
482 1.6 deraadt int
483 1.45.2.1 thorpej tunread(devvp, uio, ioflag)
484 1.45.2.1 thorpej struct vnode *devvp;
485 1.6 deraadt struct uio *uio;
486 1.22 christos int ioflag;
487 1.6 deraadt {
488 1.45.2.1 thorpej struct tun_softc *tp = devvp->v_devcookie;
489 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
490 1.6 deraadt struct mbuf *m, *m0;
491 1.6 deraadt int error=0, len, s;
492 1.6 deraadt
493 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
494 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
495 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
496 1.8 deraadt return EHOSTDOWN;
497 1.8 deraadt }
498 1.8 deraadt
499 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
500 1.6 deraadt
501 1.43 thorpej s = splnet();
502 1.6 deraadt do {
503 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m0);
504 1.6 deraadt if (m0 == 0) {
505 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
506 1.6 deraadt splx(s);
507 1.26 pk return (EWOULDBLOCK);
508 1.6 deraadt }
509 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
510 1.26 pk if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
511 1.26 pk splx(s);
512 1.26 pk return (EINTR);
513 1.26 pk }
514 1.6 deraadt }
515 1.6 deraadt } while (m0 == 0);
516 1.6 deraadt splx(s);
517 1.6 deraadt
518 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
519 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
520 1.45 itojun if (len != 0)
521 1.45 itojun error = uiomove(mtod(m0, caddr_t), len, uio);
522 1.6 deraadt MFREE(m0, m);
523 1.6 deraadt m0 = m;
524 1.6 deraadt }
525 1.6 deraadt
526 1.6 deraadt if (m0) {
527 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
528 1.6 deraadt m_freem(m0);
529 1.6 deraadt }
530 1.19 pk if (error)
531 1.19 pk ifp->if_ierrors++;
532 1.26 pk return (error);
533 1.1 cgd }
534 1.1 cgd
535 1.1 cgd /*
536 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
537 1.1 cgd */
538 1.6 deraadt int
539 1.45.2.1 thorpej tunwrite(devvp, uio, ioflag)
540 1.45.2.1 thorpej struct vnode *devvp;
541 1.6 deraadt struct uio *uio;
542 1.22 christos int ioflag;
543 1.6 deraadt {
544 1.45.2.1 thorpej struct tun_softc *tp = devvp->v_devcookie;
545 1.26 pk struct ifnet *ifp = &tp->tun_if;
546 1.6 deraadt struct mbuf *top, **mp, *m;
547 1.26 pk struct ifqueue *ifq;
548 1.26 pk struct sockaddr dst;
549 1.26 pk int isr, error=0, s, tlen, mlen;
550 1.6 deraadt
551 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
552 1.6 deraadt
553 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
554 1.26 pk if (uio->uio_resid < sizeof(dst))
555 1.26 pk return (EIO);
556 1.26 pk error = uiomove((caddr_t)&dst, sizeof(dst), uio);
557 1.26 pk if (dst.sa_len > sizeof(dst)) {
558 1.26 pk /* Duh.. */
559 1.26 pk char discard;
560 1.26 pk int n = dst.sa_len - sizeof(dst);
561 1.26 pk while (n--)
562 1.26 pk if ((error = uiomove(&discard, 1, uio)) != 0)
563 1.26 pk return (error);
564 1.26 pk }
565 1.26 pk } else {
566 1.26 pk #ifdef INET
567 1.26 pk dst.sa_family = AF_INET;
568 1.26 pk #endif
569 1.26 pk }
570 1.26 pk
571 1.6 deraadt if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
572 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
573 1.37 mjacob (unsigned long)uio->uio_resid);
574 1.26 pk return (EIO);
575 1.6 deraadt }
576 1.26 pk
577 1.26 pk switch (dst.sa_family) {
578 1.26 pk #ifdef INET
579 1.26 pk case AF_INET:
580 1.26 pk ifq = &ipintrq;
581 1.26 pk isr = NETISR_IP;
582 1.26 pk break;
583 1.26 pk #endif
584 1.26 pk default:
585 1.26 pk return (EAFNOSUPPORT);
586 1.26 pk }
587 1.26 pk
588 1.8 deraadt tlen = uio->uio_resid;
589 1.8 deraadt
590 1.8 deraadt /* get a header mbuf */
591 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
592 1.8 deraadt if (m == NULL)
593 1.26 pk return (ENOBUFS);
594 1.8 deraadt mlen = MHLEN;
595 1.8 deraadt
596 1.6 deraadt top = 0;
597 1.6 deraadt mp = ⊤
598 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
599 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
600 1.8 deraadt error = uiomove(mtod (m, caddr_t), m->m_len, uio);
601 1.6 deraadt *mp = m;
602 1.6 deraadt mp = &m->m_next;
603 1.8 deraadt if (uio->uio_resid > 0) {
604 1.8 deraadt MGET (m, M_DONTWAIT, MT_DATA);
605 1.8 deraadt if (m == 0) {
606 1.8 deraadt error = ENOBUFS;
607 1.8 deraadt break;
608 1.8 deraadt }
609 1.8 deraadt mlen = MLEN;
610 1.8 deraadt }
611 1.6 deraadt }
612 1.6 deraadt if (error) {
613 1.6 deraadt if (top)
614 1.8 deraadt m_freem (top);
615 1.19 pk ifp->if_ierrors++;
616 1.26 pk return (error);
617 1.6 deraadt }
618 1.6 deraadt
619 1.8 deraadt top->m_pkthdr.len = tlen;
620 1.8 deraadt top->m_pkthdr.rcvif = ifp;
621 1.8 deraadt
622 1.8 deraadt #if NBPFILTER > 0
623 1.40 thorpej if (ifp->if_bpf) {
624 1.8 deraadt /*
625 1.8 deraadt * We need to prepend the address family as
626 1.8 deraadt * a four byte field. Cons up a dummy header
627 1.8 deraadt * to pacify bpf. This is safe because bpf
628 1.8 deraadt * will only read from the mbuf (i.e., it won't
629 1.8 deraadt * try to free it or keep a pointer to it).
630 1.8 deraadt */
631 1.8 deraadt struct mbuf m;
632 1.16 cgd u_int32_t af = AF_INET;
633 1.8 deraadt
634 1.8 deraadt m.m_next = top;
635 1.16 cgd m.m_len = sizeof(af);
636 1.8 deraadt m.m_data = (char *)⁡
637 1.8 deraadt
638 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
639 1.6 deraadt }
640 1.8 deraadt #endif
641 1.6 deraadt
642 1.43 thorpej s = splnet();
643 1.26 pk if (IF_QFULL(ifq)) {
644 1.26 pk IF_DROP(ifq);
645 1.6 deraadt splx(s);
646 1.6 deraadt ifp->if_collisions++;
647 1.6 deraadt m_freem(top);
648 1.26 pk return (ENOBUFS);
649 1.6 deraadt }
650 1.26 pk IF_ENQUEUE(ifq, top);
651 1.6 deraadt splx(s);
652 1.6 deraadt ifp->if_ipackets++;
653 1.26 pk schednetisr(isr);
654 1.26 pk return (error);
655 1.1 cgd }
656 1.1 cgd
657 1.1 cgd /*
658 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
659 1.6 deraadt * really. The write detect always returns true, write never blocks
660 1.6 deraadt * anyway, it either accepts the packet or drops it.
661 1.6 deraadt */
662 1.6 deraadt int
663 1.45.2.1 thorpej tunpoll(devvp, events, p)
664 1.45.2.1 thorpej struct vnode *devvp;
665 1.27 mycroft int events;
666 1.22 christos struct proc *p;
667 1.6 deraadt {
668 1.45.2.1 thorpej struct tun_softc *tp = devvp->v_devcookie;
669 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
670 1.45.2.1 thorpej int s, revents = 0;
671 1.6 deraadt
672 1.43 thorpej s = splnet();
673 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
674 1.6 deraadt
675 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
676 1.6 deraadt if (ifp->if_snd.ifq_len > 0) {
677 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
678 1.24 thorpej ifp->if_snd.ifq_len);
679 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
680 1.27 mycroft } else {
681 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
682 1.27 mycroft selrecord(p, &tp->tun_rsel);
683 1.6 deraadt }
684 1.35 veego }
685 1.27 mycroft
686 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
687 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
688 1.27 mycroft
689 1.6 deraadt splx(s);
690 1.27 mycroft return (revents);
691 1.1 cgd }
692 1.8 deraadt
693 1.8 deraadt #endif /* NTUN */
694