if_tun.c revision 1.8 1 1.1 cgd /*
2 1.8 deraadt * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
3 1.8 deraadt * Nottingham University 1987.
4 1.1 cgd *
5 1.1 cgd * This source may be freely distributed, however I would be interested
6 1.1 cgd * in any changes that are made.
7 1.6 deraadt *
8 1.1 cgd * This driver takes packets off the IP i/f and hands them up to a
9 1.1 cgd * user process to have it's wicked way with. This driver has it's
10 1.1 cgd * roots in a similar driver written by Phil Cockcroft (formerly) at
11 1.1 cgd * UCL. This driver is based much more on read/write/select mode of
12 1.1 cgd * operation though.
13 1.1 cgd *
14 1.8 deraadt * $Id: if_tun.c,v 1.8 1993/12/13 05:06:33 deraadt Exp $
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.8 deraadt #include <sys/file.h>
32 1.6 deraadt
33 1.6 deraadt #include <net/if.h>
34 1.6 deraadt #include <net/netisr.h>
35 1.6 deraadt #include <net/route.h>
36 1.1 cgd
37 1.1 cgd #ifdef INET
38 1.6 deraadt #include <netinet/in.h>
39 1.6 deraadt #include <netinet/in_systm.h>
40 1.6 deraadt #include <netinet/in_var.h>
41 1.6 deraadt #include <netinet/ip.h>
42 1.6 deraadt #include <netinet/if_ether.h>
43 1.1 cgd #endif
44 1.1 cgd
45 1.1 cgd #ifdef NS
46 1.6 deraadt #include <netns/ns.h>
47 1.6 deraadt #include <netns/ns_if.h>
48 1.1 cgd #endif
49 1.1 cgd
50 1.8 deraadt #include "bpfilter.h"
51 1.8 deraadt #if NBPFILTER > 0
52 1.8 deraadt #include <sys/time.h>
53 1.8 deraadt #include <net/bpf.h>
54 1.8 deraadt #endif
55 1.8 deraadt
56 1.8 deraadt #include <net/if_tun.h>
57 1.8 deraadt
58 1.6 deraadt #define TUNDEBUG if (tundebug) printf
59 1.6 deraadt int tundebug = 0;
60 1.1 cgd
61 1.6 deraadt struct tun_softc tunctl[NTUN];
62 1.6 deraadt extern int ifqmaxlen;
63 1.6 deraadt
64 1.8 deraadt int tunopen __P((dev_t, int, int, struct proc *));
65 1.6 deraadt int tunclose __P((dev_t, int));
66 1.6 deraadt int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *));
67 1.6 deraadt int tunread __P((dev_t, struct uio *));
68 1.6 deraadt int tunwrite __P((dev_t, struct uio *));
69 1.8 deraadt int tuncioctl __P((dev_t, int, caddr_t, int));
70 1.6 deraadt int tunioctl __P((struct ifnet *, int, caddr_t, int));
71 1.8 deraadt int tunselect __P((dev_t, int));
72 1.8 deraadt void tunattach __P((int));
73 1.6 deraadt
74 1.6 deraadt static int tuninit __P((int));
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 struct sockaddr_in *sin;
83 1.8 deraadt
84 1.8 deraadt for (i = 0; i < NTUN; i++) {
85 1.8 deraadt tunctl[i].tun_flags = TUN_INITED;
86 1.8 deraadt
87 1.8 deraadt ifp = &tunctl[i].tun_if;
88 1.8 deraadt ifp->if_unit = i;
89 1.8 deraadt ifp->if_name = "tun";
90 1.8 deraadt ifp->if_mtu = TUNMTU;
91 1.8 deraadt ifp->if_ioctl = tunioctl;
92 1.8 deraadt ifp->if_output = tunoutput;
93 1.8 deraadt ifp->if_flags = IFF_POINTOPOINT;
94 1.8 deraadt ifp->if_snd.ifq_maxlen = ifqmaxlen;
95 1.8 deraadt ifp->if_collisions = 0;
96 1.8 deraadt ifp->if_ierrors = 0;
97 1.8 deraadt ifp->if_oerrors = 0;
98 1.8 deraadt ifp->if_ipackets = 0;
99 1.8 deraadt ifp->if_opackets = 0;
100 1.8 deraadt if_attach(ifp);
101 1.8 deraadt #if NBPFILTER > 0
102 1.8 deraadt bpfattach(&tunctl[i].tun_bpf, ifp, DLT_NULL, sizeof(u_int));
103 1.8 deraadt #endif
104 1.8 deraadt }
105 1.8 deraadt }
106 1.8 deraadt
107 1.6 deraadt /*
108 1.6 deraadt * tunnel open - must be superuser & the device must be
109 1.6 deraadt * configured in
110 1.6 deraadt */
111 1.6 deraadt int
112 1.6 deraadt tunopen(dev, flag, mode, p)
113 1.6 deraadt dev_t dev;
114 1.6 deraadt int flag, mode;
115 1.6 deraadt struct proc *p;
116 1.6 deraadt {
117 1.6 deraadt struct ifnet *ifp;
118 1.8 deraadt struct tun_softc *tp;
119 1.6 deraadt register int unit, error;
120 1.1 cgd
121 1.5 deraadt if (error = suser(p->p_ucred, &p->p_acflag))
122 1.5 deraadt return (error);
123 1.5 deraadt
124 1.6 deraadt if ((unit = minor(dev)) >= NTUN)
125 1.6 deraadt return (ENXIO);
126 1.6 deraadt tp = &tunctl[unit];
127 1.6 deraadt if (tp->tun_flags & TUN_OPEN)
128 1.6 deraadt return ENXIO;
129 1.6 deraadt ifp = &tp->tun_if;
130 1.6 deraadt tp->tun_flags |= TUN_OPEN;
131 1.6 deraadt TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
132 1.6 deraadt return (0);
133 1.1 cgd }
134 1.1 cgd
135 1.6 deraadt /*
136 1.6 deraadt * tunclose - close the device - mark i/f down & delete
137 1.6 deraadt * routing info
138 1.6 deraadt */
139 1.6 deraadt int
140 1.6 deraadt tunclose(dev, flag)
141 1.6 deraadt dev_t dev;
142 1.6 deraadt int flag;
143 1.6 deraadt {
144 1.8 deraadt register int unit = minor(dev), s;
145 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
146 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
147 1.6 deraadt struct mbuf *m;
148 1.6 deraadt
149 1.8 deraadt tp->tun_flags &= TUN_OPEN;
150 1.6 deraadt
151 1.6 deraadt /*
152 1.6 deraadt * junk all pending output
153 1.6 deraadt */
154 1.6 deraadt do {
155 1.6 deraadt s = splimp();
156 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m);
157 1.6 deraadt splx(s);
158 1.6 deraadt if (m)
159 1.6 deraadt m_freem(m);
160 1.6 deraadt } while (m);
161 1.6 deraadt
162 1.6 deraadt if (ifp->if_flags & IFF_UP) {
163 1.6 deraadt s = splimp();
164 1.6 deraadt if_down(ifp);
165 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
166 1.8 deraadt rtinit(ifp->if_addrlist, (int)RTM_DELETE,
167 1.8 deraadt tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
168 1.8 deraadt }
169 1.6 deraadt splx(s);
170 1.6 deraadt }
171 1.6 deraadt tp->tun_pgrp = 0;
172 1.7 deraadt selwakeup(&tp->tun_rsel);
173 1.6 deraadt
174 1.6 deraadt TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
175 1.6 deraadt return (0);
176 1.1 cgd }
177 1.1 cgd
178 1.6 deraadt static int
179 1.6 deraadt tuninit(unit)
180 1.6 deraadt int unit;
181 1.6 deraadt {
182 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
183 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
184 1.8 deraadt register struct ifaddr *ifa;
185 1.8 deraadt
186 1.8 deraadt TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
187 1.6 deraadt
188 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
189 1.8 deraadt
190 1.8 deraadt for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
191 1.8 deraadt struct sockaddr_in *si;
192 1.8 deraadt
193 1.8 deraadt si = (struct sockaddr_in *)ifa->ifa_addr;
194 1.8 deraadt if (si && si->sin_addr.s_addr)
195 1.8 deraadt tp->tun_flags |= TUN_IASET;
196 1.8 deraadt
197 1.8 deraadt si = (struct sockaddr_in *)ifa->ifa_dstaddr;
198 1.8 deraadt if (si && si->sin_addr.s_addr)
199 1.8 deraadt tp->tun_flags |= TUN_DSTADDR;
200 1.8 deraadt }
201 1.8 deraadt
202 1.6 deraadt return 0;
203 1.1 cgd }
204 1.1 cgd
205 1.1 cgd /*
206 1.1 cgd * Process an ioctl request.
207 1.1 cgd */
208 1.1 cgd int
209 1.6 deraadt tunioctl(ifp, cmd, data, flag)
210 1.6 deraadt struct ifnet *ifp;
211 1.6 deraadt int cmd;
212 1.6 deraadt caddr_t data;
213 1.6 deraadt int flag;
214 1.6 deraadt {
215 1.8 deraadt struct tun_softc *tp = &tunctl[ifp->if_unit];
216 1.6 deraadt int error = 0, s;
217 1.6 deraadt
218 1.6 deraadt s = splimp();
219 1.6 deraadt switch(cmd) {
220 1.6 deraadt case SIOCSIFADDR:
221 1.6 deraadt tuninit(ifp->if_unit);
222 1.6 deraadt break;
223 1.6 deraadt case SIOCSIFDSTADDR:
224 1.6 deraadt tp->tun_flags |= TUN_DSTADDR;
225 1.6 deraadt TUNDEBUG("%s%d: destination address set\n", ifp->if_name,
226 1.6 deraadt ifp->if_unit);
227 1.6 deraadt break;
228 1.6 deraadt default:
229 1.6 deraadt error = EINVAL;
230 1.6 deraadt }
231 1.6 deraadt splx(s);
232 1.6 deraadt return (error);
233 1.1 cgd }
234 1.1 cgd
235 1.1 cgd /*
236 1.1 cgd * tunoutput - queue packets from higher level ready to put out.
237 1.1 cgd */
238 1.6 deraadt int
239 1.6 deraadt tunoutput(ifp, m0, dst)
240 1.6 deraadt struct ifnet *ifp;
241 1.6 deraadt struct mbuf *m0;
242 1.6 deraadt struct sockaddr *dst;
243 1.6 deraadt {
244 1.8 deraadt struct tun_softc *tp = &tunctl[ifp->if_unit];
245 1.6 deraadt struct proc *p;
246 1.6 deraadt int s;
247 1.6 deraadt
248 1.6 deraadt TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
249 1.1 cgd
250 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
251 1.8 deraadt TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
252 1.8 deraadt ifp->if_unit, tp->tun_flags);
253 1.8 deraadt m_freem (m0);
254 1.8 deraadt return EHOSTDOWN;
255 1.8 deraadt }
256 1.8 deraadt
257 1.8 deraadt #if NBPFILTER > 0
258 1.8 deraadt if (tp->tun_bpf) {
259 1.8 deraadt /*
260 1.8 deraadt * We need to prepend the address family as
261 1.8 deraadt * a four byte field. Cons up a dummy header
262 1.8 deraadt * to pacify bpf. This is safe because bpf
263 1.8 deraadt * will only read from the mbuf (i.e., it won't
264 1.8 deraadt * try to free it or keep a pointer to it).
265 1.8 deraadt */
266 1.8 deraadt struct mbuf m;
267 1.8 deraadt u_int af = dst->sa_family;
268 1.8 deraadt
269 1.8 deraadt m.m_next = m0;
270 1.8 deraadt m.m_len = 4;
271 1.8 deraadt m.m_data = (char *)⁡
272 1.8 deraadt
273 1.8 deraadt bpf_mtap(tp->tun_bpf, &m);
274 1.8 deraadt }
275 1.8 deraadt #endif
276 1.8 deraadt
277 1.6 deraadt switch(dst->sa_family) {
278 1.1 cgd #ifdef INET
279 1.6 deraadt case AF_INET:
280 1.6 deraadt s = splimp();
281 1.6 deraadt if (IF_QFULL(&ifp->if_snd)) {
282 1.6 deraadt IF_DROP(&ifp->if_snd);
283 1.6 deraadt m_freem(m0);
284 1.6 deraadt splx(s);
285 1.6 deraadt ifp->if_collisions++;
286 1.6 deraadt return (ENOBUFS);
287 1.6 deraadt }
288 1.6 deraadt IF_ENQUEUE(&ifp->if_snd, m0);
289 1.6 deraadt splx(s);
290 1.6 deraadt ifp->if_opackets++;
291 1.6 deraadt break;
292 1.1 cgd #endif
293 1.6 deraadt default:
294 1.6 deraadt m_freem(m0);
295 1.6 deraadt return EAFNOSUPPORT;
296 1.6 deraadt }
297 1.6 deraadt
298 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
299 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
300 1.6 deraadt wakeup((caddr_t)tp);
301 1.6 deraadt }
302 1.6 deraadt if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
303 1.6 deraadt if (tp->tun_pgrp > 0)
304 1.6 deraadt gsignal(tp->tun_pgrp, SIGIO);
305 1.6 deraadt else if (p = pfind(-tp->tun_pgrp))
306 1.6 deraadt psignal(p, SIGIO);
307 1.6 deraadt }
308 1.6 deraadt selwakeup(&tp->tun_rsel);
309 1.6 deraadt return 0;
310 1.1 cgd }
311 1.1 cgd
312 1.1 cgd /*
313 1.1 cgd * the cdevsw interface is now pretty minimal.
314 1.1 cgd */
315 1.6 deraadt int
316 1.6 deraadt tuncioctl(dev, cmd, data, flag)
317 1.6 deraadt dev_t dev;
318 1.6 deraadt int cmd;
319 1.6 deraadt caddr_t data;
320 1.6 deraadt int flag;
321 1.6 deraadt {
322 1.6 deraadt int unit = minor(dev), s;
323 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
324 1.6 deraadt
325 1.6 deraadt switch (cmd) {
326 1.6 deraadt case TUNSDEBUG:
327 1.6 deraadt tundebug = *(int *)data;
328 1.6 deraadt break;
329 1.6 deraadt case TUNGDEBUG:
330 1.6 deraadt *(int *)data = tundebug;
331 1.6 deraadt break;
332 1.6 deraadt case FIONBIO:
333 1.6 deraadt if (*(int *)data)
334 1.6 deraadt tp->tun_flags |= TUN_NBIO;
335 1.6 deraadt else
336 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
337 1.6 deraadt break;
338 1.6 deraadt case FIOASYNC:
339 1.6 deraadt if (*(int *)data)
340 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
341 1.6 deraadt else
342 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
343 1.6 deraadt break;
344 1.6 deraadt case FIONREAD:
345 1.6 deraadt s = splimp();
346 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
347 1.6 deraadt *(int *)data = tp->tun_if.if_snd.ifq_head->m_len;
348 1.6 deraadt else
349 1.6 deraadt *(int *)data = 0;
350 1.6 deraadt splx(s);
351 1.6 deraadt break;
352 1.6 deraadt case TIOCSPGRP:
353 1.6 deraadt tp->tun_pgrp = *(int *)data;
354 1.6 deraadt break;
355 1.6 deraadt case TIOCGPGRP:
356 1.6 deraadt *(int *)data = tp->tun_pgrp;
357 1.6 deraadt break;
358 1.6 deraadt default:
359 1.6 deraadt return (ENOTTY);
360 1.6 deraadt }
361 1.6 deraadt return (0);
362 1.1 cgd }
363 1.1 cgd
364 1.1 cgd /*
365 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
366 1.6 deraadt * least as much of a packet as can be read.
367 1.1 cgd */
368 1.6 deraadt int
369 1.6 deraadt tunread(dev, uio)
370 1.6 deraadt dev_t dev;
371 1.6 deraadt struct uio *uio;
372 1.6 deraadt {
373 1.8 deraadt int unit = minor(dev);
374 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
375 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
376 1.6 deraadt struct mbuf *m, *m0;
377 1.6 deraadt int error=0, len, s;
378 1.6 deraadt
379 1.6 deraadt TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
380 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
381 1.8 deraadt TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
382 1.8 deraadt ifp->if_unit, tp->tun_flags);
383 1.8 deraadt return EHOSTDOWN;
384 1.8 deraadt }
385 1.8 deraadt
386 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
387 1.6 deraadt
388 1.6 deraadt s = splimp();
389 1.6 deraadt do {
390 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m0);
391 1.6 deraadt if (m0 == 0) {
392 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
393 1.6 deraadt splx(s);
394 1.6 deraadt return EWOULDBLOCK;
395 1.6 deraadt }
396 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
397 1.6 deraadt tsleep((caddr_t)tp, PZERO + 1, "tunread", 0);
398 1.6 deraadt }
399 1.6 deraadt } while (m0 == 0);
400 1.6 deraadt splx(s);
401 1.6 deraadt
402 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
403 1.6 deraadt len = MIN(uio->uio_resid, m0->m_len);
404 1.6 deraadt if (len == 0)
405 1.6 deraadt break;
406 1.8 deraadt error = uiomove(mtod(m0, caddr_t), len, uio);
407 1.6 deraadt MFREE(m0, m);
408 1.6 deraadt m0 = m;
409 1.6 deraadt }
410 1.6 deraadt
411 1.6 deraadt if (m0) {
412 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
413 1.6 deraadt m_freem(m0);
414 1.6 deraadt }
415 1.6 deraadt return error;
416 1.1 cgd }
417 1.1 cgd
418 1.1 cgd /*
419 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
420 1.1 cgd */
421 1.6 deraadt int
422 1.6 deraadt tunwrite(dev, uio)
423 1.8 deraadt dev_t dev;
424 1.6 deraadt struct uio *uio;
425 1.6 deraadt {
426 1.6 deraadt int unit = minor (dev);
427 1.8 deraadt struct ifnet *ifp = &tunctl[unit].tun_if;
428 1.6 deraadt struct mbuf *top, **mp, *m;
429 1.8 deraadt int error=0, s, tlen, mlen;
430 1.6 deraadt
431 1.6 deraadt TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
432 1.6 deraadt
433 1.6 deraadt if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
434 1.6 deraadt TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
435 1.6 deraadt uio->uio_resid);
436 1.6 deraadt return EIO;
437 1.6 deraadt }
438 1.8 deraadt tlen = uio->uio_resid;
439 1.8 deraadt
440 1.8 deraadt /* get a header mbuf */
441 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
442 1.8 deraadt if (m == NULL)
443 1.8 deraadt return ENOBUFS;
444 1.8 deraadt mlen = MHLEN;
445 1.8 deraadt
446 1.6 deraadt top = 0;
447 1.6 deraadt mp = ⊤
448 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
449 1.8 deraadt m->m_len = MIN (mlen, uio->uio_resid);
450 1.8 deraadt error = uiomove(mtod (m, caddr_t), m->m_len, uio);
451 1.6 deraadt *mp = m;
452 1.6 deraadt mp = &m->m_next;
453 1.8 deraadt if (uio->uio_resid > 0) {
454 1.8 deraadt MGET (m, M_DONTWAIT, MT_DATA);
455 1.8 deraadt if (m == 0) {
456 1.8 deraadt error = ENOBUFS;
457 1.8 deraadt break;
458 1.8 deraadt }
459 1.8 deraadt mlen = MLEN;
460 1.8 deraadt }
461 1.6 deraadt }
462 1.6 deraadt if (error) {
463 1.6 deraadt if (top)
464 1.8 deraadt m_freem (top);
465 1.6 deraadt return error;
466 1.6 deraadt }
467 1.6 deraadt
468 1.8 deraadt top->m_pkthdr.len = tlen;
469 1.8 deraadt top->m_pkthdr.rcvif = ifp;
470 1.8 deraadt
471 1.8 deraadt #if NBPFILTER > 0
472 1.8 deraadt if (tunctl[unit].tun_bpf) {
473 1.8 deraadt /*
474 1.8 deraadt * We need to prepend the address family as
475 1.8 deraadt * a four byte field. Cons up a dummy header
476 1.8 deraadt * to pacify bpf. This is safe because bpf
477 1.8 deraadt * will only read from the mbuf (i.e., it won't
478 1.8 deraadt * try to free it or keep a pointer to it).
479 1.8 deraadt */
480 1.8 deraadt struct mbuf m;
481 1.8 deraadt u_int af = AF_INET;
482 1.8 deraadt
483 1.8 deraadt m.m_next = top;
484 1.8 deraadt m.m_len = 4;
485 1.8 deraadt m.m_data = (char *)⁡
486 1.8 deraadt
487 1.8 deraadt bpf_mtap(tunctl[unit].tun_bpf, &m);
488 1.6 deraadt }
489 1.8 deraadt #endif
490 1.6 deraadt
491 1.6 deraadt s = splimp();
492 1.6 deraadt if (IF_QFULL (&ipintrq)) {
493 1.6 deraadt IF_DROP(&ipintrq);
494 1.6 deraadt splx(s);
495 1.6 deraadt ifp->if_collisions++;
496 1.6 deraadt m_freem(top);
497 1.6 deraadt return ENOBUFS;
498 1.6 deraadt }
499 1.6 deraadt IF_ENQUEUE(&ipintrq, top);
500 1.6 deraadt splx(s);
501 1.6 deraadt ifp->if_ipackets++;
502 1.6 deraadt schednetisr(NETISR_IP);
503 1.6 deraadt return error;
504 1.1 cgd }
505 1.1 cgd
506 1.1 cgd /*
507 1.6 deraadt * tunselect - the select interface, this is only useful on reads
508 1.6 deraadt * really. The write detect always returns true, write never blocks
509 1.6 deraadt * anyway, it either accepts the packet or drops it.
510 1.6 deraadt */
511 1.6 deraadt int
512 1.8 deraadt tunselect(dev, rw)
513 1.6 deraadt dev_t dev;
514 1.6 deraadt int rw;
515 1.6 deraadt {
516 1.6 deraadt int unit = minor(dev), s;
517 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
518 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
519 1.6 deraadt
520 1.6 deraadt s = splimp();
521 1.6 deraadt TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
522 1.6 deraadt
523 1.6 deraadt switch (rw) {
524 1.6 deraadt case FREAD:
525 1.6 deraadt if (ifp->if_snd.ifq_len > 0) {
526 1.6 deraadt splx(s);
527 1.6 deraadt TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
528 1.6 deraadt ifp->if_unit, ifp->if_snd.ifq_len);
529 1.6 deraadt return 1;
530 1.6 deraadt }
531 1.8 deraadt selrecord(curproc, &tp->tun_rsel);
532 1.6 deraadt break;
533 1.6 deraadt case FWRITE:
534 1.6 deraadt splx(s);
535 1.6 deraadt return 1;
536 1.6 deraadt }
537 1.6 deraadt splx(s);
538 1.6 deraadt TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
539 1.6 deraadt return 0;
540 1.1 cgd }
541 1.8 deraadt
542 1.8 deraadt #endif /* NTUN */
543