if_tun.c revision 1.130 1 1.130 ozaki /* $NetBSD: if_tun.c,v 1.130 2016/09/05 02:25:37 ozaki-r 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.48 lukem
17 1.48 lukem #include <sys/cdefs.h>
18 1.130 ozaki __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.130 2016/09/05 02:25:37 ozaki-r Exp $");
19 1.33 jonathan
20 1.123 pooka #ifdef _KERNEL_OPT
21 1.33 jonathan #include "opt_inet.h"
22 1.123 pooka #endif
23 1.8 deraadt
24 1.6 deraadt #include <sys/param.h>
25 1.8 deraadt #include <sys/proc.h>
26 1.6 deraadt #include <sys/systm.h>
27 1.6 deraadt #include <sys/mbuf.h>
28 1.6 deraadt #include <sys/buf.h>
29 1.6 deraadt #include <sys/protosw.h>
30 1.6 deraadt #include <sys/socket.h>
31 1.6 deraadt #include <sys/ioctl.h>
32 1.6 deraadt #include <sys/errno.h>
33 1.6 deraadt #include <sys/syslog.h>
34 1.6 deraadt #include <sys/select.h>
35 1.27 mycroft #include <sys/poll.h>
36 1.8 deraadt #include <sys/file.h>
37 1.22 christos #include <sys/signalvar.h>
38 1.23 christos #include <sys/conf.h>
39 1.89 elad #include <sys/kauth.h>
40 1.115 rmind #include <sys/mutex.h>
41 1.99 ad #include <sys/cpu.h>
42 1.128 christos #include <sys/device.h>
43 1.128 christos #include <sys/module.h>
44 1.6 deraadt
45 1.6 deraadt #include <net/if.h>
46 1.69 tron #include <net/if_types.h>
47 1.6 deraadt #include <net/netisr.h>
48 1.6 deraadt #include <net/route.h>
49 1.1 cgd
50 1.30 is
51 1.1 cgd #ifdef INET
52 1.6 deraadt #include <netinet/in.h>
53 1.6 deraadt #include <netinet/in_systm.h>
54 1.6 deraadt #include <netinet/in_var.h>
55 1.6 deraadt #include <netinet/ip.h>
56 1.30 is #include <netinet/if_inarp.h>
57 1.1 cgd #endif
58 1.1 cgd
59 1.1 cgd
60 1.8 deraadt #include <sys/time.h>
61 1.8 deraadt #include <net/bpf.h>
62 1.8 deraadt
63 1.8 deraadt #include <net/if_tun.h>
64 1.8 deraadt
65 1.122 christos #include "ioconf.h"
66 1.122 christos
67 1.29 christos #define TUNDEBUG if (tundebug) printf
68 1.6 deraadt int tundebug = 0;
69 1.1 cgd
70 1.6 deraadt extern int ifqmaxlen;
71 1.78 thorpej
72 1.78 thorpej static LIST_HEAD(, tun_softc) tun_softc_list;
73 1.78 thorpej static LIST_HEAD(, tun_softc) tunz_softc_list;
74 1.117 skrll static kmutex_t tun_softc_lock;
75 1.6 deraadt
76 1.97 christos static int tun_ioctl(struct ifnet *, u_long, void *);
77 1.96 dyoung static int tun_output(struct ifnet *, struct mbuf *,
78 1.125 ozaki const struct sockaddr *, const struct rtentry *rt);
79 1.78 thorpej static int tun_clone_create(struct if_clone *, int);
80 1.78 thorpej static int tun_clone_destroy(struct ifnet *);
81 1.6 deraadt
82 1.78 thorpej static struct if_clone tun_cloner =
83 1.46 atatat IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
84 1.46 atatat
85 1.78 thorpej static void tunattach0(struct tun_softc *);
86 1.128 christos static void tuncreate(struct tun_softc *);
87 1.106 ad static void tun_i_softintr(void *);
88 1.106 ad static void tun_o_softintr(void *);
89 1.50 itojun #ifdef ALTQ
90 1.78 thorpej static void tunstart(struct ifnet *);
91 1.50 itojun #endif
92 1.78 thorpej static struct tun_softc *tun_find_unit(dev_t);
93 1.78 thorpej static struct tun_softc *tun_find_zunit(int);
94 1.53 gehenna
95 1.78 thorpej static dev_type_open(tunopen);
96 1.78 thorpej static dev_type_close(tunclose);
97 1.78 thorpej static dev_type_read(tunread);
98 1.78 thorpej static dev_type_write(tunwrite);
99 1.78 thorpej static dev_type_ioctl(tunioctl);
100 1.78 thorpej static dev_type_poll(tunpoll);
101 1.78 thorpej static dev_type_kqfilter(tunkqfilter);
102 1.53 gehenna
103 1.53 gehenna const struct cdevsw tun_cdevsw = {
104 1.116 dholland .d_open = tunopen,
105 1.116 dholland .d_close = tunclose,
106 1.116 dholland .d_read = tunread,
107 1.116 dholland .d_write = tunwrite,
108 1.116 dholland .d_ioctl = tunioctl,
109 1.116 dholland .d_stop = nostop,
110 1.116 dholland .d_tty = notty,
111 1.116 dholland .d_poll = tunpoll,
112 1.116 dholland .d_mmap = nommap,
113 1.116 dholland .d_kqfilter = tunkqfilter,
114 1.120 dholland .d_discard = nodiscard,
115 1.116 dholland .d_flag = D_OTHER
116 1.53 gehenna };
117 1.6 deraadt
118 1.130 ozaki #ifdef _MODULE
119 1.130 ozaki devmajor_t tun_bmajor = -1, tun_cmajor = -1;
120 1.130 ozaki #endif
121 1.130 ozaki
122 1.8 deraadt void
123 1.94 christos tunattach(int unused)
124 1.8 deraadt {
125 1.46 atatat
126 1.128 christos /*
127 1.128 christos * Nothing to do here, initialization is handled by the
128 1.129 ozaki * module initialization code in tuninit() below).
129 1.128 christos */
130 1.128 christos }
131 1.128 christos
132 1.128 christos static void
133 1.128 christos tuninit(void)
134 1.128 christos {
135 1.128 christos
136 1.117 skrll mutex_init(&tun_softc_lock, MUTEX_DEFAULT, IPL_NET);
137 1.46 atatat LIST_INIT(&tun_softc_list);
138 1.70 pk LIST_INIT(&tunz_softc_list);
139 1.46 atatat if_clone_attach(&tun_cloner);
140 1.130 ozaki #ifdef _MODULE
141 1.130 ozaki devsw_attach("tun", NULL, &tun_bmajor, &tun_cdevsw, &tun_cmajor);
142 1.130 ozaki #endif
143 1.46 atatat }
144 1.46 atatat
145 1.128 christos static int
146 1.128 christos tundetach(void)
147 1.128 christos {
148 1.128 christos int error = 0;
149 1.128 christos
150 1.128 christos if (!LIST_EMPTY(&tun_softc_list) || !LIST_EMPTY(&tunz_softc_list))
151 1.128 christos error = EBUSY;
152 1.128 christos
153 1.130 ozaki #ifdef _MODULE
154 1.130 ozaki if (error == 0)
155 1.130 ozaki error = devsw_detach(NULL, &tun_cdevsw);
156 1.130 ozaki #endif
157 1.128 christos if (error == 0) {
158 1.128 christos if_clone_detach(&tun_cloner);
159 1.128 christos mutex_destroy(&tun_softc_lock);
160 1.128 christos }
161 1.128 christos
162 1.128 christos return error;
163 1.128 christos }
164 1.128 christos
165 1.70 pk /*
166 1.70 pk * Find driver instance from dev_t.
167 1.70 pk * Returns with tp locked (if found).
168 1.70 pk */
169 1.70 pk static struct tun_softc *
170 1.78 thorpej tun_find_unit(dev_t dev)
171 1.70 pk {
172 1.70 pk struct tun_softc *tp;
173 1.70 pk int unit = minor(dev);
174 1.70 pk
175 1.117 skrll mutex_enter(&tun_softc_lock);
176 1.70 pk LIST_FOREACH(tp, &tun_softc_list, tun_list)
177 1.70 pk if (unit == tp->tun_unit)
178 1.70 pk break;
179 1.70 pk if (tp)
180 1.115 rmind mutex_enter(&tp->tun_lock);
181 1.117 skrll mutex_exit(&tun_softc_lock);
182 1.70 pk
183 1.70 pk return (tp);
184 1.70 pk }
185 1.70 pk
186 1.70 pk /*
187 1.70 pk * Find zombie driver instance by unit number.
188 1.70 pk * Remove tp from list and return it unlocked (if found).
189 1.70 pk */
190 1.70 pk static struct tun_softc *
191 1.78 thorpej tun_find_zunit(int unit)
192 1.70 pk {
193 1.70 pk struct tun_softc *tp;
194 1.70 pk
195 1.117 skrll mutex_enter(&tun_softc_lock);
196 1.70 pk LIST_FOREACH(tp, &tunz_softc_list, tun_list)
197 1.70 pk if (unit == tp->tun_unit)
198 1.70 pk break;
199 1.70 pk if (tp)
200 1.70 pk LIST_REMOVE(tp, tun_list);
201 1.117 skrll mutex_exit(&tun_softc_lock);
202 1.70 pk #ifdef DIAGNOSTIC
203 1.70 pk if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN)
204 1.70 pk printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags);
205 1.70 pk #endif
206 1.70 pk
207 1.70 pk return (tp);
208 1.70 pk }
209 1.70 pk
210 1.78 thorpej static int
211 1.78 thorpej tun_clone_create(struct if_clone *ifc, int unit)
212 1.46 atatat {
213 1.70 pk struct tun_softc *tp;
214 1.46 atatat
215 1.70 pk if ((tp = tun_find_zunit(unit)) == NULL) {
216 1.70 pk /* Allocate a new instance */
217 1.107 christos tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO);
218 1.46 atatat
219 1.70 pk tp->tun_unit = unit;
220 1.115 rmind mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET);
221 1.104 rmind selinit(&tp->tun_rsel);
222 1.104 rmind selinit(&tp->tun_wsel);
223 1.70 pk } else {
224 1.70 pk /* Revive tunnel instance; clear ifp part */
225 1.70 pk (void)memset(&tp->tun_if, 0, sizeof(struct ifnet));
226 1.70 pk }
227 1.46 atatat
228 1.107 christos if_initname(&tp->tun_if, ifc->ifc_name, unit);
229 1.70 pk tunattach0(tp);
230 1.70 pk tp->tun_flags |= TUN_INITED;
231 1.106 ad tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp);
232 1.106 ad tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp);
233 1.46 atatat
234 1.117 skrll mutex_enter(&tun_softc_lock);
235 1.70 pk LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
236 1.117 skrll mutex_exit(&tun_softc_lock);
237 1.46 atatat
238 1.46 atatat return (0);
239 1.46 atatat }
240 1.46 atatat
241 1.78 thorpej static void
242 1.78 thorpej tunattach0(struct tun_softc *tp)
243 1.46 atatat {
244 1.70 pk struct ifnet *ifp;
245 1.46 atatat
246 1.70 pk ifp = &tp->tun_if;
247 1.70 pk ifp->if_softc = tp;
248 1.46 atatat ifp->if_mtu = TUNMTU;
249 1.46 atatat ifp->if_ioctl = tun_ioctl;
250 1.46 atatat ifp->if_output = tun_output;
251 1.50 itojun #ifdef ALTQ
252 1.50 itojun ifp->if_start = tunstart;
253 1.50 itojun #endif
254 1.46 atatat ifp->if_flags = IFF_POINTOPOINT;
255 1.69 tron ifp->if_type = IFT_TUNNEL;
256 1.46 atatat ifp->if_snd.ifq_maxlen = ifqmaxlen;
257 1.46 atatat ifp->if_collisions = 0;
258 1.46 atatat ifp->if_ierrors = 0;
259 1.46 atatat ifp->if_oerrors = 0;
260 1.46 atatat ifp->if_ipackets = 0;
261 1.46 atatat ifp->if_opackets = 0;
262 1.58 jdolecek ifp->if_ibytes = 0;
263 1.58 jdolecek ifp->if_obytes = 0;
264 1.46 atatat ifp->if_dlt = DLT_NULL;
265 1.50 itojun IFQ_SET_READY(&ifp->if_snd);
266 1.46 atatat if_attach(ifp);
267 1.46 atatat if_alloc_sadl(ifp);
268 1.113 joerg bpf_attach(ifp, DLT_NULL, sizeof(uint32_t));
269 1.46 atatat }
270 1.46 atatat
271 1.78 thorpej static int
272 1.78 thorpej tun_clone_destroy(struct ifnet *ifp)
273 1.46 atatat {
274 1.46 atatat struct tun_softc *tp = (void *)ifp;
275 1.117 skrll int zombie = 0;
276 1.8 deraadt
277 1.115 rmind IF_PURGE(&ifp->if_snd);
278 1.115 rmind ifp->if_flags &= ~IFF_RUNNING;
279 1.115 rmind
280 1.117 skrll mutex_enter(&tun_softc_lock);
281 1.115 rmind mutex_enter(&tp->tun_lock);
282 1.46 atatat LIST_REMOVE(tp, tun_list);
283 1.70 pk if (tp->tun_flags & TUN_OPEN) {
284 1.70 pk /* Hang on to storage until last close */
285 1.70 pk zombie = 1;
286 1.70 pk tp->tun_flags &= ~TUN_INITED;
287 1.70 pk LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
288 1.70 pk }
289 1.117 skrll mutex_exit(&tun_softc_lock);
290 1.46 atatat
291 1.46 atatat if (tp->tun_flags & TUN_RWAIT) {
292 1.46 atatat tp->tun_flags &= ~TUN_RWAIT;
293 1.97 christos wakeup((void *)tp);
294 1.46 atatat }
295 1.104 rmind selnotify(&tp->tun_rsel, 0, 0);
296 1.8 deraadt
297 1.115 rmind mutex_exit(&tp->tun_lock);
298 1.70 pk
299 1.106 ad if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
300 1.106 ad fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
301 1.106 ad
302 1.113 joerg bpf_detach(ifp);
303 1.46 atatat if_detach(ifp);
304 1.46 atatat
305 1.104 rmind if (!zombie) {
306 1.104 rmind seldestroy(&tp->tun_rsel);
307 1.104 rmind seldestroy(&tp->tun_wsel);
308 1.106 ad softint_disestablish(tp->tun_osih);
309 1.106 ad softint_disestablish(tp->tun_isih);
310 1.115 rmind mutex_destroy(&tp->tun_lock);
311 1.70 pk free(tp, M_DEVBUF);
312 1.104 rmind }
313 1.73 peter
314 1.73 peter return (0);
315 1.8 deraadt }
316 1.8 deraadt
317 1.6 deraadt /*
318 1.6 deraadt * tunnel open - must be superuser & the device must be
319 1.6 deraadt * configured in
320 1.6 deraadt */
321 1.78 thorpej static int
322 1.94 christos tunopen(dev_t dev, int flag, int mode, struct lwp *l)
323 1.6 deraadt {
324 1.6 deraadt struct ifnet *ifp;
325 1.8 deraadt struct tun_softc *tp;
326 1.117 skrll int error;
327 1.1 cgd
328 1.111 elad error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE_TUN,
329 1.111 elad KAUTH_REQ_NETWORK_INTERFACE_TUN_ADD, NULL, NULL, NULL);
330 1.111 elad if (error)
331 1.5 deraadt return (error);
332 1.5 deraadt
333 1.46 atatat tp = tun_find_unit(dev);
334 1.52 atatat
335 1.70 pk if (tp == NULL) {
336 1.52 atatat (void)tun_clone_create(&tun_cloner, minor(dev));
337 1.52 atatat tp = tun_find_unit(dev);
338 1.70 pk if (tp == NULL) {
339 1.70 pk error = ENXIO;
340 1.70 pk goto out_nolock;
341 1.70 pk }
342 1.52 atatat }
343 1.46 atatat
344 1.46 atatat if (tp->tun_flags & TUN_OPEN) {
345 1.70 pk error = EBUSY;
346 1.70 pk goto out;
347 1.46 atatat }
348 1.46 atatat
349 1.6 deraadt ifp = &tp->tun_if;
350 1.6 deraadt tp->tun_flags |= TUN_OPEN;
351 1.24 thorpej TUNDEBUG("%s: open\n", ifp->if_xname);
352 1.70 pk out:
353 1.115 rmind mutex_exit(&tp->tun_lock);
354 1.70 pk out_nolock:
355 1.70 pk return (error);
356 1.1 cgd }
357 1.1 cgd
358 1.6 deraadt /*
359 1.6 deraadt * tunclose - close the device - mark i/f down & delete
360 1.6 deraadt * routing info
361 1.6 deraadt */
362 1.6 deraadt int
363 1.94 christos tunclose(dev_t dev, int flag, int mode,
364 1.94 christos struct lwp *l)
365 1.6 deraadt {
366 1.46 atatat struct tun_softc *tp;
367 1.46 atatat struct ifnet *ifp;
368 1.6 deraadt
369 1.70 pk if ((tp = tun_find_zunit(minor(dev))) != NULL) {
370 1.70 pk /* interface was "destroyed" before the close */
371 1.104 rmind seldestroy(&tp->tun_rsel);
372 1.104 rmind seldestroy(&tp->tun_wsel);
373 1.106 ad softint_disestablish(tp->tun_osih);
374 1.106 ad softint_disestablish(tp->tun_isih);
375 1.115 rmind mutex_destroy(&tp->tun_lock);
376 1.70 pk free(tp, M_DEVBUF);
377 1.70 pk goto out_nolock;
378 1.70 pk }
379 1.46 atatat
380 1.70 pk if ((tp = tun_find_unit(dev)) == NULL)
381 1.70 pk goto out_nolock;
382 1.46 atatat
383 1.46 atatat ifp = &tp->tun_if;
384 1.46 atatat
385 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
386 1.6 deraadt
387 1.115 rmind tp->tun_pgid = 0;
388 1.115 rmind selnotify(&tp->tun_rsel, 0, 0);
389 1.115 rmind
390 1.115 rmind TUNDEBUG ("%s: closed\n", ifp->if_xname);
391 1.115 rmind mutex_exit(&tp->tun_lock);
392 1.115 rmind
393 1.6 deraadt /*
394 1.6 deraadt * junk all pending output
395 1.6 deraadt */
396 1.50 itojun IFQ_PURGE(&ifp->if_snd);
397 1.6 deraadt
398 1.6 deraadt if (ifp->if_flags & IFF_UP) {
399 1.6 deraadt if_down(ifp);
400 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
401 1.17 mycroft /* find internet addresses and delete routes */
402 1.39 augustss struct ifaddr *ifa;
403 1.127 ozaki IFADDR_READER_FOREACH(ifa, ifp) {
404 1.80 rpaulo #if defined(INET) || defined(INET6)
405 1.80 rpaulo if (ifa->ifa_addr->sa_family == AF_INET ||
406 1.80 rpaulo ifa->ifa_addr->sa_family == AF_INET6) {
407 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
408 1.26 pk tp->tun_flags & TUN_DSTADDR
409 1.26 pk ? RTF_HOST
410 1.26 pk : 0);
411 1.18 mycroft }
412 1.38 itojun #endif
413 1.11 deraadt }
414 1.8 deraadt }
415 1.6 deraadt }
416 1.70 pk out_nolock:
417 1.6 deraadt return (0);
418 1.1 cgd }
419 1.1 cgd
420 1.70 pk /*
421 1.110 dyoung * Call at splnet().
422 1.70 pk */
423 1.26 pk static void
424 1.128 christos tuncreate(struct tun_softc *tp)
425 1.6 deraadt {
426 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
427 1.46 atatat struct ifaddr *ifa;
428 1.8 deraadt
429 1.128 christos TUNDEBUG("%s: %s\n", __func__, ifp->if_xname);
430 1.6 deraadt
431 1.115 rmind mutex_enter(&tp->tun_lock);
432 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
433 1.8 deraadt
434 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
435 1.127 ozaki IFADDR_READER_FOREACH(ifa, ifp) {
436 1.38 itojun #ifdef INET
437 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
438 1.17 mycroft struct sockaddr_in *sin;
439 1.11 deraadt
440 1.17 mycroft sin = satosin(ifa->ifa_addr);
441 1.17 mycroft if (sin && sin->sin_addr.s_addr)
442 1.17 mycroft tp->tun_flags |= TUN_IASET;
443 1.17 mycroft
444 1.26 pk if (ifp->if_flags & IFF_POINTOPOINT) {
445 1.26 pk sin = satosin(ifa->ifa_dstaddr);
446 1.26 pk if (sin && sin->sin_addr.s_addr)
447 1.26 pk tp->tun_flags |= TUN_DSTADDR;
448 1.26 pk }
449 1.11 deraadt }
450 1.38 itojun #endif
451 1.80 rpaulo #ifdef INET6
452 1.80 rpaulo if (ifa->ifa_addr->sa_family == AF_INET6) {
453 1.80 rpaulo struct sockaddr_in6 *sin;
454 1.80 rpaulo
455 1.80 rpaulo sin = (struct sockaddr_in6 *)ifa->ifa_addr;
456 1.80 rpaulo if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
457 1.80 rpaulo tp->tun_flags |= TUN_IASET;
458 1.80 rpaulo
459 1.80 rpaulo if (ifp->if_flags & IFF_POINTOPOINT) {
460 1.80 rpaulo sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
461 1.80 rpaulo if (sin &&
462 1.80 rpaulo !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
463 1.80 rpaulo tp->tun_flags |= TUN_DSTADDR;
464 1.80 rpaulo } else
465 1.80 rpaulo tp->tun_flags &= ~TUN_DSTADDR;
466 1.80 rpaulo }
467 1.80 rpaulo #endif /* INET6 */
468 1.18 mycroft }
469 1.115 rmind mutex_exit(&tp->tun_lock);
470 1.1 cgd }
471 1.1 cgd
472 1.1 cgd /*
473 1.1 cgd * Process an ioctl request.
474 1.1 cgd */
475 1.78 thorpej static int
476 1.97 christos tun_ioctl(struct ifnet *ifp, u_long cmd, void *data)
477 1.6 deraadt {
478 1.6 deraadt int error = 0, s;
479 1.46 atatat struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
480 1.121 roy struct ifreq *ifr = (struct ifreq *)data;
481 1.121 roy struct ifaddr *ifa = (struct ifaddr *)data;
482 1.46 atatat
483 1.70 pk s = splnet();
484 1.6 deraadt
485 1.61 itojun switch (cmd) {
486 1.108 dyoung case SIOCINITIFADDR:
487 1.128 christos tuncreate(tp);
488 1.121 roy ifa->ifa_rtrequest = p2p_rtrequest;
489 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
490 1.6 deraadt break;
491 1.26 pk case SIOCSIFBRDADDR:
492 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
493 1.26 pk break;
494 1.102 dyoung case SIOCSIFMTU:
495 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
496 1.102 dyoung error = EINVAL;
497 1.102 dyoung break;
498 1.31 matt }
499 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
500 1.102 dyoung if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
501 1.102 dyoung error = 0;
502 1.32 matt break;
503 1.32 matt case SIOCADDMULTI:
504 1.102 dyoung case SIOCDELMULTI:
505 1.102 dyoung if (ifr == NULL) {
506 1.32 matt error = EAFNOSUPPORT; /* XXX */
507 1.32 matt break;
508 1.32 matt }
509 1.98 dyoung switch (ifreq_getaddr(cmd, ifr)->sa_family) {
510 1.32 matt #ifdef INET
511 1.32 matt case AF_INET:
512 1.32 matt break;
513 1.32 matt #endif
514 1.80 rpaulo #ifdef INET6
515 1.80 rpaulo case AF_INET6:
516 1.80 rpaulo break;
517 1.80 rpaulo #endif
518 1.32 matt default:
519 1.32 matt error = EAFNOSUPPORT;
520 1.32 matt break;
521 1.32 matt }
522 1.31 matt break;
523 1.6 deraadt default:
524 1.108 dyoung error = ifioctl_common(ifp, cmd, data);
525 1.6 deraadt }
526 1.70 pk
527 1.6 deraadt splx(s);
528 1.6 deraadt return (error);
529 1.1 cgd }
530 1.1 cgd
531 1.1 cgd /*
532 1.22 christos * tun_output - queue packets from higher level ready to put out.
533 1.1 cgd */
534 1.78 thorpej static int
535 1.96 dyoung tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
536 1.125 ozaki const struct rtentry *rt)
537 1.6 deraadt {
538 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
539 1.6 deraadt int s;
540 1.51 itojun int error;
541 1.80 rpaulo #if defined(INET) || defined(INET6)
542 1.75 christos int mlen;
543 1.80 rpaulo uint32_t *af;
544 1.38 itojun #endif
545 1.6 deraadt
546 1.70 pk s = splnet();
547 1.115 rmind mutex_enter(&tp->tun_lock);
548 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
549 1.1 cgd
550 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
551 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
552 1.24 thorpej tp->tun_flags);
553 1.70 pk error = EHOSTDOWN;
554 1.70 pk goto out;
555 1.8 deraadt }
556 1.8 deraadt
557 1.50 itojun /*
558 1.50 itojun * if the queueing discipline needs packet classification,
559 1.50 itojun * do it before prepending link headers.
560 1.50 itojun */
561 1.124 knakahar IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
562 1.70 pk
563 1.113 joerg bpf_mtap_af(ifp, dst->sa_family, m0);
564 1.8 deraadt
565 1.6 deraadt switch(dst->sa_family) {
566 1.79 rpaulo #ifdef INET6
567 1.79 rpaulo case AF_INET6:
568 1.79 rpaulo #endif
569 1.1 cgd #ifdef INET
570 1.6 deraadt case AF_INET:
571 1.79 rpaulo #endif
572 1.79 rpaulo #if defined(INET) || defined(INET6)
573 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
574 1.26 pk /* Simple link-layer header */
575 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
576 1.26 pk if (m0 == NULL) {
577 1.26 pk IF_DROP(&ifp->if_snd);
578 1.70 pk error = ENOBUFS;
579 1.70 pk goto out;
580 1.26 pk }
581 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
582 1.85 rpaulo }
583 1.85 rpaulo
584 1.85 rpaulo if (tp->tun_flags & TUN_IFHEAD) {
585 1.81 rpaulo /* Prepend the address family */
586 1.82 rpaulo M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
587 1.80 rpaulo if (m0 == NULL) {
588 1.80 rpaulo IF_DROP(&ifp->if_snd);
589 1.80 rpaulo error = ENOBUFS;
590 1.80 rpaulo goto out;
591 1.80 rpaulo }
592 1.80 rpaulo af = mtod(m0,uint32_t *);
593 1.80 rpaulo *af = htonl(dst->sa_family);
594 1.86 rpaulo } else {
595 1.115 rmind #ifdef INET
596 1.86 rpaulo if (dst->sa_family != AF_INET)
597 1.86 rpaulo #endif
598 1.86 rpaulo {
599 1.86 rpaulo error = EAFNOSUPPORT;
600 1.86 rpaulo goto out;
601 1.86 rpaulo }
602 1.26 pk }
603 1.36 sommerfe /* FALLTHROUGH */
604 1.36 sommerfe case AF_UNSPEC:
605 1.124 knakahar IFQ_ENQUEUE(&ifp->if_snd, m0, error);
606 1.50 itojun if (error) {
607 1.6 deraadt ifp->if_collisions++;
608 1.70 pk error = EAFNOSUPPORT;
609 1.115 rmind m0 = NULL;
610 1.70 pk goto out;
611 1.6 deraadt }
612 1.58 jdolecek mlen = m0->m_pkthdr.len;
613 1.6 deraadt ifp->if_opackets++;
614 1.58 jdolecek ifp->if_obytes += mlen;
615 1.6 deraadt break;
616 1.1 cgd #endif
617 1.6 deraadt default:
618 1.70 pk error = EAFNOSUPPORT;
619 1.70 pk goto out;
620 1.6 deraadt }
621 1.6 deraadt
622 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
623 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
624 1.97 christos wakeup((void *)tp);
625 1.6 deraadt }
626 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
627 1.106 ad softint_schedule(tp->tun_isih);
628 1.64 jdolecek
629 1.104 rmind selnotify(&tp->tun_rsel, 0, 0);
630 1.70 pk out:
631 1.115 rmind mutex_exit(&tp->tun_lock);
632 1.70 pk splx(s);
633 1.115 rmind
634 1.115 rmind if (error && m0) {
635 1.115 rmind m_freem(m0);
636 1.115 rmind }
637 1.115 rmind return 0;
638 1.1 cgd }
639 1.1 cgd
640 1.106 ad static void
641 1.106 ad tun_i_softintr(void *cookie)
642 1.106 ad {
643 1.106 ad struct tun_softc *tp = cookie;
644 1.106 ad
645 1.106 ad if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
646 1.106 ad fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
647 1.106 ad NULL);
648 1.106 ad }
649 1.106 ad
650 1.106 ad static void
651 1.106 ad tun_o_softintr(void *cookie)
652 1.106 ad {
653 1.106 ad struct tun_softc *tp = cookie;
654 1.106 ad
655 1.106 ad if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
656 1.106 ad fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM,
657 1.106 ad NULL);
658 1.106 ad }
659 1.106 ad
660 1.1 cgd /*
661 1.1 cgd * the cdevsw interface is now pretty minimal.
662 1.1 cgd */
663 1.6 deraadt int
664 1.97 christos tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
665 1.6 deraadt {
666 1.46 atatat struct tun_softc *tp;
667 1.70 pk int s, error = 0;
668 1.46 atatat
669 1.70 pk s = splnet();
670 1.46 atatat tp = tun_find_unit(dev);
671 1.46 atatat
672 1.46 atatat /* interface was "destroyed" already */
673 1.70 pk if (tp == NULL) {
674 1.70 pk error = ENXIO;
675 1.70 pk goto out_nolock;
676 1.70 pk }
677 1.6 deraadt
678 1.6 deraadt switch (cmd) {
679 1.6 deraadt case TUNSDEBUG:
680 1.6 deraadt tundebug = *(int *)data;
681 1.6 deraadt break;
682 1.26 pk
683 1.6 deraadt case TUNGDEBUG:
684 1.6 deraadt *(int *)data = tundebug;
685 1.6 deraadt break;
686 1.26 pk
687 1.26 pk case TUNSIFMODE:
688 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
689 1.26 pk case IFF_POINTOPOINT:
690 1.26 pk case IFF_BROADCAST:
691 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
692 1.70 pk error = EBUSY;
693 1.70 pk goto out;
694 1.26 pk }
695 1.26 pk tp->tun_if.if_flags &=
696 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
697 1.26 pk tp->tun_if.if_flags |= *(int *)data;
698 1.26 pk break;
699 1.26 pk default:
700 1.70 pk error = EINVAL;
701 1.70 pk goto out;
702 1.26 pk }
703 1.26 pk break;
704 1.26 pk
705 1.26 pk case TUNSLMODE:
706 1.87 rpaulo if (*(int *)data) {
707 1.26 pk tp->tun_flags |= TUN_PREPADDR;
708 1.87 rpaulo tp->tun_flags &= ~TUN_IFHEAD;
709 1.87 rpaulo } else
710 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
711 1.26 pk break;
712 1.26 pk
713 1.84 rpaulo case TUNSIFHEAD:
714 1.87 rpaulo if (*(int *)data) {
715 1.84 rpaulo tp->tun_flags |= TUN_IFHEAD;
716 1.88 rpaulo tp->tun_flags &= ~TUN_PREPADDR;
717 1.87 rpaulo } else
718 1.84 rpaulo tp->tun_flags &= ~TUN_IFHEAD;
719 1.84 rpaulo break;
720 1.84 rpaulo
721 1.84 rpaulo case TUNGIFHEAD:
722 1.84 rpaulo *(int *)data = (tp->tun_flags & TUN_IFHEAD);
723 1.84 rpaulo break;
724 1.84 rpaulo
725 1.6 deraadt case FIONBIO:
726 1.6 deraadt if (*(int *)data)
727 1.6 deraadt tp->tun_flags |= TUN_NBIO;
728 1.6 deraadt else
729 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
730 1.6 deraadt break;
731 1.26 pk
732 1.6 deraadt case FIOASYNC:
733 1.6 deraadt if (*(int *)data)
734 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
735 1.6 deraadt else
736 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
737 1.6 deraadt break;
738 1.26 pk
739 1.6 deraadt case FIONREAD:
740 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
741 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
742 1.70 pk else
743 1.6 deraadt *(int *)data = 0;
744 1.6 deraadt break;
745 1.26 pk
746 1.6 deraadt case TIOCSPGRP:
747 1.64 jdolecek case FIOSETOWN:
748 1.105 ad error = fsetown(&tp->tun_pgid, cmd, data);
749 1.6 deraadt break;
750 1.26 pk
751 1.6 deraadt case TIOCGPGRP:
752 1.64 jdolecek case FIOGETOWN:
753 1.105 ad error = fgetown(tp->tun_pgid, cmd, data);
754 1.6 deraadt break;
755 1.26 pk
756 1.6 deraadt default:
757 1.70 pk error = ENOTTY;
758 1.6 deraadt }
759 1.70 pk
760 1.70 pk out:
761 1.115 rmind mutex_exit(&tp->tun_lock);
762 1.70 pk out_nolock:
763 1.70 pk splx(s);
764 1.64 jdolecek return (error);
765 1.1 cgd }
766 1.1 cgd
767 1.1 cgd /*
768 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
769 1.6 deraadt * least as much of a packet as can be read.
770 1.1 cgd */
771 1.6 deraadt int
772 1.94 christos tunread(dev_t dev, struct uio *uio, int ioflag)
773 1.6 deraadt {
774 1.46 atatat struct tun_softc *tp;
775 1.46 atatat struct ifnet *ifp;
776 1.6 deraadt struct mbuf *m, *m0;
777 1.70 pk int error = 0, len, s, index;
778 1.46 atatat
779 1.70 pk s = splnet();
780 1.46 atatat tp = tun_find_unit(dev);
781 1.46 atatat
782 1.46 atatat /* interface was "destroyed" already */
783 1.70 pk if (tp == NULL) {
784 1.70 pk error = ENXIO;
785 1.70 pk goto out_nolock;
786 1.70 pk }
787 1.46 atatat
788 1.46 atatat index = tp->tun_if.if_index;
789 1.46 atatat ifp = &tp->tun_if;
790 1.6 deraadt
791 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
792 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
793 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
794 1.70 pk error = EHOSTDOWN;
795 1.70 pk goto out;
796 1.8 deraadt }
797 1.8 deraadt
798 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
799 1.6 deraadt
800 1.6 deraadt do {
801 1.50 itojun IFQ_DEQUEUE(&ifp->if_snd, m0);
802 1.6 deraadt if (m0 == 0) {
803 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
804 1.70 pk error = EWOULDBLOCK;
805 1.70 pk goto out;
806 1.6 deraadt }
807 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
808 1.115 rmind if (mtsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
809 1.70 pk "tunread", 0, &tp->tun_lock) != 0) {
810 1.70 pk error = EINTR;
811 1.70 pk goto out_nolock;
812 1.46 atatat } else {
813 1.46 atatat /*
814 1.46 atatat * Maybe the interface was destroyed while
815 1.46 atatat * we were sleeping, so let's ensure that
816 1.46 atatat * we're looking at the same (valid) tun
817 1.46 atatat * interface before looping.
818 1.46 atatat */
819 1.46 atatat tp = tun_find_unit(dev);
820 1.70 pk if (tp == NULL) {
821 1.70 pk error = ENXIO;
822 1.70 pk goto out_nolock;
823 1.70 pk }
824 1.70 pk if (tp->tun_if.if_index != index) {
825 1.70 pk error = ENXIO;
826 1.70 pk goto out;
827 1.46 atatat }
828 1.26 pk }
829 1.6 deraadt }
830 1.6 deraadt } while (m0 == 0);
831 1.70 pk
832 1.115 rmind mutex_exit(&tp->tun_lock);
833 1.6 deraadt splx(s);
834 1.6 deraadt
835 1.70 pk /* Copy the mbuf chain */
836 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
837 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
838 1.45 itojun if (len != 0)
839 1.97 christos error = uiomove(mtod(m0, void *), len, uio);
840 1.6 deraadt MFREE(m0, m);
841 1.6 deraadt m0 = m;
842 1.6 deraadt }
843 1.6 deraadt
844 1.6 deraadt if (m0) {
845 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
846 1.6 deraadt m_freem(m0);
847 1.6 deraadt }
848 1.19 pk if (error)
849 1.19 pk ifp->if_ierrors++;
850 1.70 pk
851 1.70 pk return (error);
852 1.70 pk
853 1.70 pk out:
854 1.115 rmind mutex_exit(&tp->tun_lock);
855 1.70 pk out_nolock:
856 1.70 pk splx(s);
857 1.26 pk return (error);
858 1.1 cgd }
859 1.1 cgd
860 1.1 cgd /*
861 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
862 1.1 cgd */
863 1.6 deraadt int
864 1.94 christos tunwrite(dev_t dev, struct uio *uio, int ioflag)
865 1.6 deraadt {
866 1.46 atatat struct tun_softc *tp;
867 1.46 atatat struct ifnet *ifp;
868 1.6 deraadt struct mbuf *top, **mp, *m;
869 1.118 rmind pktqueue_t *pktq;
870 1.26 pk struct sockaddr dst;
871 1.118 rmind int error = 0, s, tlen, mlen;
872 1.80 rpaulo uint32_t family;
873 1.6 deraadt
874 1.70 pk s = splnet();
875 1.46 atatat tp = tun_find_unit(dev);
876 1.46 atatat
877 1.46 atatat /* interface was "destroyed" already */
878 1.70 pk if (tp == NULL) {
879 1.70 pk error = ENXIO;
880 1.70 pk goto out_nolock;
881 1.70 pk }
882 1.70 pk
883 1.70 pk /* Unlock until we've got the data */
884 1.115 rmind mutex_exit(&tp->tun_lock);
885 1.70 pk splx(s);
886 1.46 atatat
887 1.46 atatat ifp = &tp->tun_if;
888 1.46 atatat
889 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
890 1.6 deraadt
891 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
892 1.46 atatat if (uio->uio_resid < sizeof(dst)) {
893 1.70 pk error = EIO;
894 1.70 pk goto out0;
895 1.46 atatat }
896 1.97 christos error = uiomove((void *)&dst, sizeof(dst), uio);
897 1.26 pk if (dst.sa_len > sizeof(dst)) {
898 1.26 pk /* Duh.. */
899 1.26 pk char discard;
900 1.26 pk int n = dst.sa_len - sizeof(dst);
901 1.26 pk while (n--)
902 1.46 atatat if ((error = uiomove(&discard, 1, uio)) != 0) {
903 1.70 pk goto out0;
904 1.46 atatat }
905 1.26 pk }
906 1.85 rpaulo } else if (tp->tun_flags & TUN_IFHEAD) {
907 1.80 rpaulo if (uio->uio_resid < sizeof(family)){
908 1.80 rpaulo error = EIO;
909 1.80 rpaulo goto out0;
910 1.80 rpaulo }
911 1.97 christos error = uiomove((void *)&family, sizeof(family), uio);
912 1.80 rpaulo dst.sa_family = ntohl(family);
913 1.85 rpaulo } else {
914 1.85 rpaulo #ifdef INET
915 1.85 rpaulo dst.sa_family = AF_INET;
916 1.85 rpaulo #endif
917 1.26 pk }
918 1.26 pk
919 1.54 simonb if (uio->uio_resid > TUNMTU) {
920 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
921 1.37 mjacob (unsigned long)uio->uio_resid);
922 1.70 pk error = EIO;
923 1.70 pk goto out0;
924 1.6 deraadt }
925 1.26 pk
926 1.26 pk switch (dst.sa_family) {
927 1.26 pk #ifdef INET
928 1.26 pk case AF_INET:
929 1.118 rmind pktq = ip_pktq;
930 1.26 pk break;
931 1.26 pk #endif
932 1.79 rpaulo #ifdef INET6
933 1.79 rpaulo case AF_INET6:
934 1.118 rmind pktq = ip6_pktq;
935 1.83 rpaulo break;
936 1.79 rpaulo #endif
937 1.26 pk default:
938 1.70 pk error = EAFNOSUPPORT;
939 1.70 pk goto out0;
940 1.26 pk }
941 1.26 pk
942 1.8 deraadt tlen = uio->uio_resid;
943 1.8 deraadt
944 1.8 deraadt /* get a header mbuf */
945 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
946 1.46 atatat if (m == NULL) {
947 1.70 pk error = ENOBUFS;
948 1.70 pk goto out0;
949 1.46 atatat }
950 1.8 deraadt mlen = MHLEN;
951 1.8 deraadt
952 1.68 tron top = NULL;
953 1.6 deraadt mp = ⊤
954 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
955 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
956 1.97 christos error = uiomove(mtod(m, void *), m->m_len, uio);
957 1.6 deraadt *mp = m;
958 1.6 deraadt mp = &m->m_next;
959 1.68 tron if (error == 0 && uio->uio_resid > 0) {
960 1.68 tron MGET(m, M_DONTWAIT, MT_DATA);
961 1.68 tron if (m == NULL) {
962 1.8 deraadt error = ENOBUFS;
963 1.8 deraadt break;
964 1.8 deraadt }
965 1.8 deraadt mlen = MLEN;
966 1.8 deraadt }
967 1.6 deraadt }
968 1.6 deraadt if (error) {
969 1.68 tron if (top != NULL)
970 1.8 deraadt m_freem (top);
971 1.19 pk ifp->if_ierrors++;
972 1.70 pk goto out0;
973 1.6 deraadt }
974 1.6 deraadt
975 1.8 deraadt top->m_pkthdr.len = tlen;
976 1.126 ozaki m_set_rcvif(top, ifp);
977 1.8 deraadt
978 1.113 joerg bpf_mtap_af(ifp, dst.sa_family, top);
979 1.6 deraadt
980 1.43 thorpej s = splnet();
981 1.115 rmind mutex_enter(&tp->tun_lock);
982 1.70 pk if ((tp->tun_flags & TUN_INITED) == 0) {
983 1.70 pk /* Interface was destroyed */
984 1.70 pk error = ENXIO;
985 1.70 pk goto out;
986 1.70 pk }
987 1.119 ws if (__predict_false(!pktq_enqueue(pktq, top, 0))) {
988 1.6 deraadt ifp->if_collisions++;
989 1.115 rmind mutex_exit(&tp->tun_lock);
990 1.70 pk error = ENOBUFS;
991 1.119 ws m_freem(top);
992 1.115 rmind goto out_nolock;
993 1.6 deraadt }
994 1.6 deraadt ifp->if_ipackets++;
995 1.58 jdolecek ifp->if_ibytes += tlen;
996 1.70 pk out:
997 1.115 rmind mutex_exit(&tp->tun_lock);
998 1.70 pk out_nolock:
999 1.70 pk splx(s);
1000 1.70 pk out0:
1001 1.26 pk return (error);
1002 1.1 cgd }
1003 1.1 cgd
1004 1.50 itojun #ifdef ALTQ
1005 1.50 itojun /*
1006 1.50 itojun * Start packet transmission on the interface.
1007 1.50 itojun * when the interface queue is rate-limited by ALTQ or TBR,
1008 1.50 itojun * if_start is needed to drain packets from the queue in order
1009 1.50 itojun * to notify readers when outgoing packets become ready.
1010 1.70 pk *
1011 1.70 pk * Should be called at splnet.
1012 1.50 itojun */
1013 1.50 itojun static void
1014 1.78 thorpej tunstart(struct ifnet *ifp)
1015 1.50 itojun {
1016 1.50 itojun struct tun_softc *tp = ifp->if_softc;
1017 1.50 itojun
1018 1.50 itojun if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
1019 1.50 itojun return;
1020 1.50 itojun
1021 1.115 rmind mutex_enter(&tp->tun_lock);
1022 1.70 pk if (!IF_IS_EMPTY(&ifp->if_snd)) {
1023 1.50 itojun if (tp->tun_flags & TUN_RWAIT) {
1024 1.50 itojun tp->tun_flags &= ~TUN_RWAIT;
1025 1.97 christos wakeup((void *)tp);
1026 1.50 itojun }
1027 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
1028 1.106 ad softint_schedule(tp->tun_osih);
1029 1.70 pk
1030 1.104 rmind selnotify(&tp->tun_rsel, 0, 0);
1031 1.50 itojun }
1032 1.115 rmind mutex_exit(&tp->tun_lock);
1033 1.50 itojun }
1034 1.50 itojun #endif /* ALTQ */
1035 1.1 cgd /*
1036 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
1037 1.6 deraadt * really. The write detect always returns true, write never blocks
1038 1.6 deraadt * anyway, it either accepts the packet or drops it.
1039 1.6 deraadt */
1040 1.6 deraadt int
1041 1.78 thorpej tunpoll(dev_t dev, int events, struct lwp *l)
1042 1.6 deraadt {
1043 1.46 atatat struct tun_softc *tp;
1044 1.46 atatat struct ifnet *ifp;
1045 1.46 atatat int s, revents = 0;
1046 1.46 atatat
1047 1.70 pk s = splnet();
1048 1.46 atatat tp = tun_find_unit(dev);
1049 1.46 atatat
1050 1.46 atatat /* interface was "destroyed" already */
1051 1.46 atatat if (tp == NULL)
1052 1.70 pk goto out_nolock;
1053 1.46 atatat
1054 1.46 atatat ifp = &tp->tun_if;
1055 1.6 deraadt
1056 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1057 1.6 deraadt
1058 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
1059 1.70 pk if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1060 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1061 1.24 thorpej ifp->if_snd.ifq_len);
1062 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
1063 1.27 mycroft } else {
1064 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1065 1.77 christos selrecord(l, &tp->tun_rsel);
1066 1.6 deraadt }
1067 1.35 veego }
1068 1.27 mycroft
1069 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
1070 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
1071 1.27 mycroft
1072 1.115 rmind mutex_exit(&tp->tun_lock);
1073 1.70 pk out_nolock:
1074 1.6 deraadt splx(s);
1075 1.27 mycroft return (revents);
1076 1.56 jdolecek }
1077 1.56 jdolecek
1078 1.56 jdolecek static void
1079 1.56 jdolecek filt_tunrdetach(struct knote *kn)
1080 1.56 jdolecek {
1081 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1082 1.56 jdolecek int s;
1083 1.56 jdolecek
1084 1.56 jdolecek s = splnet();
1085 1.57 christos SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1086 1.56 jdolecek splx(s);
1087 1.56 jdolecek }
1088 1.56 jdolecek
1089 1.56 jdolecek static int
1090 1.94 christos filt_tunread(struct knote *kn, long hint)
1091 1.56 jdolecek {
1092 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1093 1.56 jdolecek struct ifnet *ifp = &tp->tun_if;
1094 1.56 jdolecek struct mbuf *m;
1095 1.56 jdolecek int s;
1096 1.56 jdolecek
1097 1.56 jdolecek s = splnet();
1098 1.56 jdolecek IF_POLL(&ifp->if_snd, m);
1099 1.56 jdolecek if (m == NULL) {
1100 1.56 jdolecek splx(s);
1101 1.56 jdolecek return (0);
1102 1.56 jdolecek }
1103 1.56 jdolecek
1104 1.56 jdolecek for (kn->kn_data = 0; m != NULL; m = m->m_next)
1105 1.56 jdolecek kn->kn_data += m->m_len;
1106 1.56 jdolecek
1107 1.56 jdolecek splx(s);
1108 1.56 jdolecek return (1);
1109 1.56 jdolecek }
1110 1.56 jdolecek
1111 1.56 jdolecek static const struct filterops tunread_filtops =
1112 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_tunread };
1113 1.56 jdolecek
1114 1.56 jdolecek static const struct filterops tun_seltrue_filtops =
1115 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_seltrue };
1116 1.56 jdolecek
1117 1.56 jdolecek int
1118 1.56 jdolecek tunkqfilter(dev_t dev, struct knote *kn)
1119 1.56 jdolecek {
1120 1.70 pk struct tun_softc *tp;
1121 1.56 jdolecek struct klist *klist;
1122 1.70 pk int rv = 0, s;
1123 1.70 pk
1124 1.70 pk s = splnet();
1125 1.70 pk tp = tun_find_unit(dev);
1126 1.70 pk if (tp == NULL)
1127 1.70 pk goto out_nolock;
1128 1.56 jdolecek
1129 1.56 jdolecek switch (kn->kn_filter) {
1130 1.56 jdolecek case EVFILT_READ:
1131 1.57 christos klist = &tp->tun_rsel.sel_klist;
1132 1.56 jdolecek kn->kn_fop = &tunread_filtops;
1133 1.56 jdolecek break;
1134 1.56 jdolecek
1135 1.56 jdolecek case EVFILT_WRITE:
1136 1.57 christos klist = &tp->tun_rsel.sel_klist;
1137 1.56 jdolecek kn->kn_fop = &tun_seltrue_filtops;
1138 1.56 jdolecek break;
1139 1.56 jdolecek
1140 1.56 jdolecek default:
1141 1.100 pooka rv = EINVAL;
1142 1.70 pk goto out;
1143 1.56 jdolecek }
1144 1.56 jdolecek
1145 1.56 jdolecek kn->kn_hook = tp;
1146 1.56 jdolecek
1147 1.56 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1148 1.70 pk
1149 1.70 pk out:
1150 1.115 rmind mutex_exit(&tp->tun_lock);
1151 1.70 pk out_nolock:
1152 1.56 jdolecek splx(s);
1153 1.70 pk return (rv);
1154 1.1 cgd }
1155 1.128 christos
1156 1.128 christos /*
1157 1.128 christos * Module infrastructure
1158 1.128 christos */
1159 1.128 christos #include "if_module.h"
1160 1.128 christos
1161 1.128 christos IF_MODULE(MODULE_CLASS_DRIVER, tun, "")
1162