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