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