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