if_tun.c revision 1.132 1 1.132 ozaki /* $NetBSD: if_tun.c,v 1.132 2016/09/07 10:27:44 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.132 ozaki __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.132 2016/09/07 10:27:44 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.132 ozaki static void tun_enable(struct tun_softc *, const struct ifaddr *);
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.132 ozaki tun_enable(struct tun_softc *tp, const struct ifaddr *ifa)
425 1.6 deraadt {
426 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
427 1.8 deraadt
428 1.128 christos TUNDEBUG("%s: %s\n", __func__, ifp->if_xname);
429 1.6 deraadt
430 1.115 rmind mutex_enter(&tp->tun_lock);
431 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
432 1.8 deraadt
433 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
434 1.132 ozaki
435 1.132 ozaki switch (ifa->ifa_addr->sa_family) {
436 1.38 itojun #ifdef INET
437 1.132 ozaki case AF_INET: {
438 1.132 ozaki struct sockaddr_in *sin;
439 1.132 ozaki
440 1.132 ozaki sin = satosin(ifa->ifa_addr);
441 1.132 ozaki if (sin && sin->sin_addr.s_addr)
442 1.132 ozaki tp->tun_flags |= TUN_IASET;
443 1.11 deraadt
444 1.132 ozaki if (ifp->if_flags & IFF_POINTOPOINT) {
445 1.132 ozaki sin = satosin(ifa->ifa_dstaddr);
446 1.17 mycroft if (sin && sin->sin_addr.s_addr)
447 1.132 ozaki tp->tun_flags |= TUN_DSTADDR;
448 1.11 deraadt }
449 1.132 ozaki break;
450 1.132 ozaki }
451 1.38 itojun #endif
452 1.80 rpaulo #ifdef INET6
453 1.132 ozaki case AF_INET6: {
454 1.132 ozaki struct sockaddr_in6 *sin;
455 1.80 rpaulo
456 1.132 ozaki sin = satosin6(ifa->ifa_addr);
457 1.132 ozaki if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
458 1.132 ozaki tp->tun_flags |= TUN_IASET;
459 1.132 ozaki
460 1.132 ozaki if (ifp->if_flags & IFF_POINTOPOINT) {
461 1.132 ozaki sin = satosin6(ifa->ifa_dstaddr);
462 1.132 ozaki if (sin && !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
463 1.132 ozaki tp->tun_flags |= TUN_DSTADDR;
464 1.132 ozaki } else
465 1.132 ozaki tp->tun_flags &= ~TUN_DSTADDR;
466 1.132 ozaki break;
467 1.132 ozaki }
468 1.80 rpaulo #endif /* INET6 */
469 1.132 ozaki default:
470 1.132 ozaki break;
471 1.18 mycroft }
472 1.115 rmind mutex_exit(&tp->tun_lock);
473 1.1 cgd }
474 1.1 cgd
475 1.1 cgd /*
476 1.1 cgd * Process an ioctl request.
477 1.1 cgd */
478 1.78 thorpej static int
479 1.97 christos tun_ioctl(struct ifnet *ifp, u_long cmd, void *data)
480 1.6 deraadt {
481 1.6 deraadt int error = 0, s;
482 1.46 atatat struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
483 1.121 roy struct ifreq *ifr = (struct ifreq *)data;
484 1.121 roy struct ifaddr *ifa = (struct ifaddr *)data;
485 1.46 atatat
486 1.70 pk s = splnet();
487 1.6 deraadt
488 1.61 itojun switch (cmd) {
489 1.108 dyoung case SIOCINITIFADDR:
490 1.132 ozaki tun_enable(tp, ifa);
491 1.121 roy ifa->ifa_rtrequest = p2p_rtrequest;
492 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
493 1.6 deraadt break;
494 1.26 pk case SIOCSIFBRDADDR:
495 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
496 1.26 pk break;
497 1.102 dyoung case SIOCSIFMTU:
498 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
499 1.102 dyoung error = EINVAL;
500 1.102 dyoung break;
501 1.31 matt }
502 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
503 1.102 dyoung if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
504 1.102 dyoung error = 0;
505 1.32 matt break;
506 1.32 matt case SIOCADDMULTI:
507 1.102 dyoung case SIOCDELMULTI:
508 1.102 dyoung if (ifr == NULL) {
509 1.32 matt error = EAFNOSUPPORT; /* XXX */
510 1.32 matt break;
511 1.32 matt }
512 1.98 dyoung switch (ifreq_getaddr(cmd, ifr)->sa_family) {
513 1.32 matt #ifdef INET
514 1.32 matt case AF_INET:
515 1.32 matt break;
516 1.32 matt #endif
517 1.80 rpaulo #ifdef INET6
518 1.80 rpaulo case AF_INET6:
519 1.80 rpaulo break;
520 1.80 rpaulo #endif
521 1.32 matt default:
522 1.32 matt error = EAFNOSUPPORT;
523 1.32 matt break;
524 1.32 matt }
525 1.31 matt break;
526 1.6 deraadt default:
527 1.108 dyoung error = ifioctl_common(ifp, cmd, data);
528 1.6 deraadt }
529 1.70 pk
530 1.6 deraadt splx(s);
531 1.6 deraadt return (error);
532 1.1 cgd }
533 1.1 cgd
534 1.1 cgd /*
535 1.22 christos * tun_output - queue packets from higher level ready to put out.
536 1.1 cgd */
537 1.78 thorpej static int
538 1.96 dyoung tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
539 1.125 ozaki const struct rtentry *rt)
540 1.6 deraadt {
541 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
542 1.6 deraadt int s;
543 1.51 itojun int error;
544 1.80 rpaulo #if defined(INET) || defined(INET6)
545 1.75 christos int mlen;
546 1.80 rpaulo uint32_t *af;
547 1.38 itojun #endif
548 1.6 deraadt
549 1.70 pk s = splnet();
550 1.115 rmind mutex_enter(&tp->tun_lock);
551 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
552 1.1 cgd
553 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
554 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
555 1.24 thorpej tp->tun_flags);
556 1.70 pk error = EHOSTDOWN;
557 1.70 pk goto out;
558 1.8 deraadt }
559 1.8 deraadt
560 1.50 itojun /*
561 1.50 itojun * if the queueing discipline needs packet classification,
562 1.50 itojun * do it before prepending link headers.
563 1.50 itojun */
564 1.124 knakahar IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
565 1.70 pk
566 1.113 joerg bpf_mtap_af(ifp, dst->sa_family, m0);
567 1.8 deraadt
568 1.6 deraadt switch(dst->sa_family) {
569 1.79 rpaulo #ifdef INET6
570 1.79 rpaulo case AF_INET6:
571 1.79 rpaulo #endif
572 1.1 cgd #ifdef INET
573 1.6 deraadt case AF_INET:
574 1.79 rpaulo #endif
575 1.79 rpaulo #if defined(INET) || defined(INET6)
576 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
577 1.26 pk /* Simple link-layer header */
578 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
579 1.26 pk if (m0 == NULL) {
580 1.26 pk IF_DROP(&ifp->if_snd);
581 1.70 pk error = ENOBUFS;
582 1.70 pk goto out;
583 1.26 pk }
584 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
585 1.85 rpaulo }
586 1.85 rpaulo
587 1.85 rpaulo if (tp->tun_flags & TUN_IFHEAD) {
588 1.81 rpaulo /* Prepend the address family */
589 1.82 rpaulo M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
590 1.80 rpaulo if (m0 == NULL) {
591 1.80 rpaulo IF_DROP(&ifp->if_snd);
592 1.80 rpaulo error = ENOBUFS;
593 1.80 rpaulo goto out;
594 1.80 rpaulo }
595 1.80 rpaulo af = mtod(m0,uint32_t *);
596 1.80 rpaulo *af = htonl(dst->sa_family);
597 1.86 rpaulo } else {
598 1.115 rmind #ifdef INET
599 1.86 rpaulo if (dst->sa_family != AF_INET)
600 1.86 rpaulo #endif
601 1.86 rpaulo {
602 1.86 rpaulo error = EAFNOSUPPORT;
603 1.86 rpaulo goto out;
604 1.86 rpaulo }
605 1.26 pk }
606 1.36 sommerfe /* FALLTHROUGH */
607 1.36 sommerfe case AF_UNSPEC:
608 1.124 knakahar IFQ_ENQUEUE(&ifp->if_snd, m0, error);
609 1.50 itojun if (error) {
610 1.6 deraadt ifp->if_collisions++;
611 1.70 pk error = EAFNOSUPPORT;
612 1.115 rmind m0 = NULL;
613 1.70 pk goto out;
614 1.6 deraadt }
615 1.58 jdolecek mlen = m0->m_pkthdr.len;
616 1.6 deraadt ifp->if_opackets++;
617 1.58 jdolecek ifp->if_obytes += mlen;
618 1.6 deraadt break;
619 1.1 cgd #endif
620 1.6 deraadt default:
621 1.70 pk error = EAFNOSUPPORT;
622 1.70 pk goto out;
623 1.6 deraadt }
624 1.6 deraadt
625 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
626 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
627 1.97 christos wakeup((void *)tp);
628 1.6 deraadt }
629 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
630 1.106 ad softint_schedule(tp->tun_isih);
631 1.64 jdolecek
632 1.104 rmind selnotify(&tp->tun_rsel, 0, 0);
633 1.70 pk out:
634 1.115 rmind mutex_exit(&tp->tun_lock);
635 1.70 pk splx(s);
636 1.115 rmind
637 1.115 rmind if (error && m0) {
638 1.115 rmind m_freem(m0);
639 1.115 rmind }
640 1.115 rmind return 0;
641 1.1 cgd }
642 1.1 cgd
643 1.106 ad static void
644 1.106 ad tun_i_softintr(void *cookie)
645 1.106 ad {
646 1.106 ad struct tun_softc *tp = cookie;
647 1.106 ad
648 1.106 ad if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
649 1.106 ad fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
650 1.106 ad NULL);
651 1.106 ad }
652 1.106 ad
653 1.106 ad static void
654 1.106 ad tun_o_softintr(void *cookie)
655 1.106 ad {
656 1.106 ad struct tun_softc *tp = cookie;
657 1.106 ad
658 1.106 ad if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
659 1.106 ad fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM,
660 1.106 ad NULL);
661 1.106 ad }
662 1.106 ad
663 1.1 cgd /*
664 1.1 cgd * the cdevsw interface is now pretty minimal.
665 1.1 cgd */
666 1.6 deraadt int
667 1.97 christos tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
668 1.6 deraadt {
669 1.46 atatat struct tun_softc *tp;
670 1.70 pk int s, error = 0;
671 1.46 atatat
672 1.70 pk s = splnet();
673 1.46 atatat tp = tun_find_unit(dev);
674 1.46 atatat
675 1.46 atatat /* interface was "destroyed" already */
676 1.70 pk if (tp == NULL) {
677 1.70 pk error = ENXIO;
678 1.70 pk goto out_nolock;
679 1.70 pk }
680 1.6 deraadt
681 1.6 deraadt switch (cmd) {
682 1.6 deraadt case TUNSDEBUG:
683 1.6 deraadt tundebug = *(int *)data;
684 1.6 deraadt break;
685 1.26 pk
686 1.6 deraadt case TUNGDEBUG:
687 1.6 deraadt *(int *)data = tundebug;
688 1.6 deraadt break;
689 1.26 pk
690 1.26 pk case TUNSIFMODE:
691 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
692 1.26 pk case IFF_POINTOPOINT:
693 1.26 pk case IFF_BROADCAST:
694 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
695 1.70 pk error = EBUSY;
696 1.70 pk goto out;
697 1.26 pk }
698 1.26 pk tp->tun_if.if_flags &=
699 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
700 1.26 pk tp->tun_if.if_flags |= *(int *)data;
701 1.26 pk break;
702 1.26 pk default:
703 1.70 pk error = EINVAL;
704 1.70 pk goto out;
705 1.26 pk }
706 1.26 pk break;
707 1.26 pk
708 1.26 pk case TUNSLMODE:
709 1.87 rpaulo if (*(int *)data) {
710 1.26 pk tp->tun_flags |= TUN_PREPADDR;
711 1.87 rpaulo tp->tun_flags &= ~TUN_IFHEAD;
712 1.87 rpaulo } else
713 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
714 1.26 pk break;
715 1.26 pk
716 1.84 rpaulo case TUNSIFHEAD:
717 1.87 rpaulo if (*(int *)data) {
718 1.84 rpaulo tp->tun_flags |= TUN_IFHEAD;
719 1.88 rpaulo tp->tun_flags &= ~TUN_PREPADDR;
720 1.87 rpaulo } else
721 1.84 rpaulo tp->tun_flags &= ~TUN_IFHEAD;
722 1.84 rpaulo break;
723 1.84 rpaulo
724 1.84 rpaulo case TUNGIFHEAD:
725 1.84 rpaulo *(int *)data = (tp->tun_flags & TUN_IFHEAD);
726 1.84 rpaulo break;
727 1.84 rpaulo
728 1.6 deraadt case FIONBIO:
729 1.6 deraadt if (*(int *)data)
730 1.6 deraadt tp->tun_flags |= TUN_NBIO;
731 1.6 deraadt else
732 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
733 1.6 deraadt break;
734 1.26 pk
735 1.6 deraadt case FIOASYNC:
736 1.6 deraadt if (*(int *)data)
737 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
738 1.6 deraadt else
739 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
740 1.6 deraadt break;
741 1.26 pk
742 1.6 deraadt case FIONREAD:
743 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
744 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
745 1.70 pk else
746 1.6 deraadt *(int *)data = 0;
747 1.6 deraadt break;
748 1.26 pk
749 1.6 deraadt case TIOCSPGRP:
750 1.64 jdolecek case FIOSETOWN:
751 1.105 ad error = fsetown(&tp->tun_pgid, cmd, data);
752 1.6 deraadt break;
753 1.26 pk
754 1.6 deraadt case TIOCGPGRP:
755 1.64 jdolecek case FIOGETOWN:
756 1.105 ad error = fgetown(tp->tun_pgid, cmd, data);
757 1.6 deraadt break;
758 1.26 pk
759 1.6 deraadt default:
760 1.70 pk error = ENOTTY;
761 1.6 deraadt }
762 1.70 pk
763 1.70 pk out:
764 1.115 rmind mutex_exit(&tp->tun_lock);
765 1.70 pk out_nolock:
766 1.70 pk splx(s);
767 1.64 jdolecek return (error);
768 1.1 cgd }
769 1.1 cgd
770 1.1 cgd /*
771 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
772 1.6 deraadt * least as much of a packet as can be read.
773 1.1 cgd */
774 1.6 deraadt int
775 1.94 christos tunread(dev_t dev, struct uio *uio, int ioflag)
776 1.6 deraadt {
777 1.46 atatat struct tun_softc *tp;
778 1.46 atatat struct ifnet *ifp;
779 1.6 deraadt struct mbuf *m, *m0;
780 1.70 pk int error = 0, len, s, index;
781 1.46 atatat
782 1.70 pk s = splnet();
783 1.46 atatat tp = tun_find_unit(dev);
784 1.46 atatat
785 1.46 atatat /* interface was "destroyed" already */
786 1.70 pk if (tp == NULL) {
787 1.70 pk error = ENXIO;
788 1.70 pk goto out_nolock;
789 1.70 pk }
790 1.46 atatat
791 1.46 atatat index = tp->tun_if.if_index;
792 1.46 atatat ifp = &tp->tun_if;
793 1.6 deraadt
794 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
795 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
796 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
797 1.70 pk error = EHOSTDOWN;
798 1.70 pk goto out;
799 1.8 deraadt }
800 1.8 deraadt
801 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
802 1.6 deraadt
803 1.6 deraadt do {
804 1.50 itojun IFQ_DEQUEUE(&ifp->if_snd, m0);
805 1.6 deraadt if (m0 == 0) {
806 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
807 1.70 pk error = EWOULDBLOCK;
808 1.70 pk goto out;
809 1.6 deraadt }
810 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
811 1.115 rmind if (mtsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
812 1.70 pk "tunread", 0, &tp->tun_lock) != 0) {
813 1.70 pk error = EINTR;
814 1.70 pk goto out_nolock;
815 1.46 atatat } else {
816 1.46 atatat /*
817 1.46 atatat * Maybe the interface was destroyed while
818 1.46 atatat * we were sleeping, so let's ensure that
819 1.46 atatat * we're looking at the same (valid) tun
820 1.46 atatat * interface before looping.
821 1.46 atatat */
822 1.46 atatat tp = tun_find_unit(dev);
823 1.70 pk if (tp == NULL) {
824 1.70 pk error = ENXIO;
825 1.70 pk goto out_nolock;
826 1.70 pk }
827 1.70 pk if (tp->tun_if.if_index != index) {
828 1.70 pk error = ENXIO;
829 1.70 pk goto out;
830 1.46 atatat }
831 1.26 pk }
832 1.6 deraadt }
833 1.6 deraadt } while (m0 == 0);
834 1.70 pk
835 1.115 rmind mutex_exit(&tp->tun_lock);
836 1.6 deraadt splx(s);
837 1.6 deraadt
838 1.70 pk /* Copy the mbuf chain */
839 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
840 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
841 1.45 itojun if (len != 0)
842 1.97 christos error = uiomove(mtod(m0, void *), len, uio);
843 1.6 deraadt MFREE(m0, m);
844 1.6 deraadt m0 = m;
845 1.6 deraadt }
846 1.6 deraadt
847 1.6 deraadt if (m0) {
848 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
849 1.6 deraadt m_freem(m0);
850 1.6 deraadt }
851 1.19 pk if (error)
852 1.19 pk ifp->if_ierrors++;
853 1.70 pk
854 1.70 pk return (error);
855 1.70 pk
856 1.70 pk out:
857 1.115 rmind mutex_exit(&tp->tun_lock);
858 1.70 pk out_nolock:
859 1.70 pk splx(s);
860 1.26 pk return (error);
861 1.1 cgd }
862 1.1 cgd
863 1.1 cgd /*
864 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
865 1.1 cgd */
866 1.6 deraadt int
867 1.94 christos tunwrite(dev_t dev, struct uio *uio, int ioflag)
868 1.6 deraadt {
869 1.46 atatat struct tun_softc *tp;
870 1.46 atatat struct ifnet *ifp;
871 1.6 deraadt struct mbuf *top, **mp, *m;
872 1.118 rmind pktqueue_t *pktq;
873 1.26 pk struct sockaddr dst;
874 1.118 rmind int error = 0, s, tlen, mlen;
875 1.80 rpaulo uint32_t family;
876 1.6 deraadt
877 1.70 pk s = splnet();
878 1.46 atatat tp = tun_find_unit(dev);
879 1.46 atatat
880 1.46 atatat /* interface was "destroyed" already */
881 1.70 pk if (tp == NULL) {
882 1.70 pk error = ENXIO;
883 1.70 pk goto out_nolock;
884 1.70 pk }
885 1.70 pk
886 1.70 pk /* Unlock until we've got the data */
887 1.115 rmind mutex_exit(&tp->tun_lock);
888 1.70 pk splx(s);
889 1.46 atatat
890 1.46 atatat ifp = &tp->tun_if;
891 1.46 atatat
892 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
893 1.6 deraadt
894 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
895 1.46 atatat if (uio->uio_resid < sizeof(dst)) {
896 1.70 pk error = EIO;
897 1.70 pk goto out0;
898 1.46 atatat }
899 1.97 christos error = uiomove((void *)&dst, sizeof(dst), uio);
900 1.26 pk if (dst.sa_len > sizeof(dst)) {
901 1.26 pk /* Duh.. */
902 1.26 pk char discard;
903 1.26 pk int n = dst.sa_len - sizeof(dst);
904 1.26 pk while (n--)
905 1.46 atatat if ((error = uiomove(&discard, 1, uio)) != 0) {
906 1.70 pk goto out0;
907 1.46 atatat }
908 1.26 pk }
909 1.85 rpaulo } else if (tp->tun_flags & TUN_IFHEAD) {
910 1.80 rpaulo if (uio->uio_resid < sizeof(family)){
911 1.80 rpaulo error = EIO;
912 1.80 rpaulo goto out0;
913 1.80 rpaulo }
914 1.97 christos error = uiomove((void *)&family, sizeof(family), uio);
915 1.80 rpaulo dst.sa_family = ntohl(family);
916 1.85 rpaulo } else {
917 1.85 rpaulo #ifdef INET
918 1.85 rpaulo dst.sa_family = AF_INET;
919 1.85 rpaulo #endif
920 1.26 pk }
921 1.26 pk
922 1.54 simonb if (uio->uio_resid > TUNMTU) {
923 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
924 1.37 mjacob (unsigned long)uio->uio_resid);
925 1.70 pk error = EIO;
926 1.70 pk goto out0;
927 1.6 deraadt }
928 1.26 pk
929 1.26 pk switch (dst.sa_family) {
930 1.26 pk #ifdef INET
931 1.26 pk case AF_INET:
932 1.118 rmind pktq = ip_pktq;
933 1.26 pk break;
934 1.26 pk #endif
935 1.79 rpaulo #ifdef INET6
936 1.79 rpaulo case AF_INET6:
937 1.118 rmind pktq = ip6_pktq;
938 1.83 rpaulo break;
939 1.79 rpaulo #endif
940 1.26 pk default:
941 1.70 pk error = EAFNOSUPPORT;
942 1.70 pk goto out0;
943 1.26 pk }
944 1.26 pk
945 1.8 deraadt tlen = uio->uio_resid;
946 1.8 deraadt
947 1.8 deraadt /* get a header mbuf */
948 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
949 1.46 atatat if (m == NULL) {
950 1.70 pk error = ENOBUFS;
951 1.70 pk goto out0;
952 1.46 atatat }
953 1.8 deraadt mlen = MHLEN;
954 1.8 deraadt
955 1.68 tron top = NULL;
956 1.6 deraadt mp = ⊤
957 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
958 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
959 1.97 christos error = uiomove(mtod(m, void *), m->m_len, uio);
960 1.6 deraadt *mp = m;
961 1.6 deraadt mp = &m->m_next;
962 1.68 tron if (error == 0 && uio->uio_resid > 0) {
963 1.68 tron MGET(m, M_DONTWAIT, MT_DATA);
964 1.68 tron if (m == NULL) {
965 1.8 deraadt error = ENOBUFS;
966 1.8 deraadt break;
967 1.8 deraadt }
968 1.8 deraadt mlen = MLEN;
969 1.8 deraadt }
970 1.6 deraadt }
971 1.6 deraadt if (error) {
972 1.68 tron if (top != NULL)
973 1.8 deraadt m_freem (top);
974 1.19 pk ifp->if_ierrors++;
975 1.70 pk goto out0;
976 1.6 deraadt }
977 1.6 deraadt
978 1.8 deraadt top->m_pkthdr.len = tlen;
979 1.126 ozaki m_set_rcvif(top, ifp);
980 1.8 deraadt
981 1.113 joerg bpf_mtap_af(ifp, dst.sa_family, top);
982 1.6 deraadt
983 1.43 thorpej s = splnet();
984 1.115 rmind mutex_enter(&tp->tun_lock);
985 1.70 pk if ((tp->tun_flags & TUN_INITED) == 0) {
986 1.70 pk /* Interface was destroyed */
987 1.70 pk error = ENXIO;
988 1.70 pk goto out;
989 1.70 pk }
990 1.119 ws if (__predict_false(!pktq_enqueue(pktq, top, 0))) {
991 1.6 deraadt ifp->if_collisions++;
992 1.115 rmind mutex_exit(&tp->tun_lock);
993 1.70 pk error = ENOBUFS;
994 1.119 ws m_freem(top);
995 1.115 rmind goto out_nolock;
996 1.6 deraadt }
997 1.6 deraadt ifp->if_ipackets++;
998 1.58 jdolecek ifp->if_ibytes += tlen;
999 1.70 pk out:
1000 1.115 rmind mutex_exit(&tp->tun_lock);
1001 1.70 pk out_nolock:
1002 1.70 pk splx(s);
1003 1.70 pk out0:
1004 1.26 pk return (error);
1005 1.1 cgd }
1006 1.1 cgd
1007 1.50 itojun #ifdef ALTQ
1008 1.50 itojun /*
1009 1.50 itojun * Start packet transmission on the interface.
1010 1.50 itojun * when the interface queue is rate-limited by ALTQ or TBR,
1011 1.50 itojun * if_start is needed to drain packets from the queue in order
1012 1.50 itojun * to notify readers when outgoing packets become ready.
1013 1.70 pk *
1014 1.70 pk * Should be called at splnet.
1015 1.50 itojun */
1016 1.50 itojun static void
1017 1.78 thorpej tunstart(struct ifnet *ifp)
1018 1.50 itojun {
1019 1.50 itojun struct tun_softc *tp = ifp->if_softc;
1020 1.50 itojun
1021 1.50 itojun if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
1022 1.50 itojun return;
1023 1.50 itojun
1024 1.115 rmind mutex_enter(&tp->tun_lock);
1025 1.70 pk if (!IF_IS_EMPTY(&ifp->if_snd)) {
1026 1.50 itojun if (tp->tun_flags & TUN_RWAIT) {
1027 1.50 itojun tp->tun_flags &= ~TUN_RWAIT;
1028 1.97 christos wakeup((void *)tp);
1029 1.50 itojun }
1030 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
1031 1.106 ad softint_schedule(tp->tun_osih);
1032 1.70 pk
1033 1.104 rmind selnotify(&tp->tun_rsel, 0, 0);
1034 1.50 itojun }
1035 1.115 rmind mutex_exit(&tp->tun_lock);
1036 1.50 itojun }
1037 1.50 itojun #endif /* ALTQ */
1038 1.1 cgd /*
1039 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
1040 1.6 deraadt * really. The write detect always returns true, write never blocks
1041 1.6 deraadt * anyway, it either accepts the packet or drops it.
1042 1.6 deraadt */
1043 1.6 deraadt int
1044 1.78 thorpej tunpoll(dev_t dev, int events, struct lwp *l)
1045 1.6 deraadt {
1046 1.46 atatat struct tun_softc *tp;
1047 1.46 atatat struct ifnet *ifp;
1048 1.46 atatat int s, revents = 0;
1049 1.46 atatat
1050 1.70 pk s = splnet();
1051 1.46 atatat tp = tun_find_unit(dev);
1052 1.46 atatat
1053 1.46 atatat /* interface was "destroyed" already */
1054 1.46 atatat if (tp == NULL)
1055 1.70 pk goto out_nolock;
1056 1.46 atatat
1057 1.46 atatat ifp = &tp->tun_if;
1058 1.6 deraadt
1059 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1060 1.6 deraadt
1061 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
1062 1.70 pk if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1063 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1064 1.24 thorpej ifp->if_snd.ifq_len);
1065 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
1066 1.27 mycroft } else {
1067 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1068 1.77 christos selrecord(l, &tp->tun_rsel);
1069 1.6 deraadt }
1070 1.35 veego }
1071 1.27 mycroft
1072 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
1073 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
1074 1.27 mycroft
1075 1.115 rmind mutex_exit(&tp->tun_lock);
1076 1.70 pk out_nolock:
1077 1.6 deraadt splx(s);
1078 1.27 mycroft return (revents);
1079 1.56 jdolecek }
1080 1.56 jdolecek
1081 1.56 jdolecek static void
1082 1.56 jdolecek filt_tunrdetach(struct knote *kn)
1083 1.56 jdolecek {
1084 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1085 1.56 jdolecek int s;
1086 1.56 jdolecek
1087 1.56 jdolecek s = splnet();
1088 1.57 christos SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1089 1.56 jdolecek splx(s);
1090 1.56 jdolecek }
1091 1.56 jdolecek
1092 1.56 jdolecek static int
1093 1.94 christos filt_tunread(struct knote *kn, long hint)
1094 1.56 jdolecek {
1095 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1096 1.56 jdolecek struct ifnet *ifp = &tp->tun_if;
1097 1.56 jdolecek struct mbuf *m;
1098 1.56 jdolecek int s;
1099 1.56 jdolecek
1100 1.56 jdolecek s = splnet();
1101 1.56 jdolecek IF_POLL(&ifp->if_snd, m);
1102 1.56 jdolecek if (m == NULL) {
1103 1.56 jdolecek splx(s);
1104 1.56 jdolecek return (0);
1105 1.56 jdolecek }
1106 1.56 jdolecek
1107 1.56 jdolecek for (kn->kn_data = 0; m != NULL; m = m->m_next)
1108 1.56 jdolecek kn->kn_data += m->m_len;
1109 1.56 jdolecek
1110 1.56 jdolecek splx(s);
1111 1.56 jdolecek return (1);
1112 1.56 jdolecek }
1113 1.56 jdolecek
1114 1.56 jdolecek static const struct filterops tunread_filtops =
1115 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_tunread };
1116 1.56 jdolecek
1117 1.56 jdolecek static const struct filterops tun_seltrue_filtops =
1118 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_seltrue };
1119 1.56 jdolecek
1120 1.56 jdolecek int
1121 1.56 jdolecek tunkqfilter(dev_t dev, struct knote *kn)
1122 1.56 jdolecek {
1123 1.70 pk struct tun_softc *tp;
1124 1.56 jdolecek struct klist *klist;
1125 1.70 pk int rv = 0, s;
1126 1.70 pk
1127 1.70 pk s = splnet();
1128 1.70 pk tp = tun_find_unit(dev);
1129 1.70 pk if (tp == NULL)
1130 1.70 pk goto out_nolock;
1131 1.56 jdolecek
1132 1.56 jdolecek switch (kn->kn_filter) {
1133 1.56 jdolecek case EVFILT_READ:
1134 1.57 christos klist = &tp->tun_rsel.sel_klist;
1135 1.56 jdolecek kn->kn_fop = &tunread_filtops;
1136 1.56 jdolecek break;
1137 1.56 jdolecek
1138 1.56 jdolecek case EVFILT_WRITE:
1139 1.57 christos klist = &tp->tun_rsel.sel_klist;
1140 1.56 jdolecek kn->kn_fop = &tun_seltrue_filtops;
1141 1.56 jdolecek break;
1142 1.56 jdolecek
1143 1.56 jdolecek default:
1144 1.100 pooka rv = EINVAL;
1145 1.70 pk goto out;
1146 1.56 jdolecek }
1147 1.56 jdolecek
1148 1.56 jdolecek kn->kn_hook = tp;
1149 1.56 jdolecek
1150 1.56 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1151 1.70 pk
1152 1.70 pk out:
1153 1.115 rmind mutex_exit(&tp->tun_lock);
1154 1.70 pk out_nolock:
1155 1.56 jdolecek splx(s);
1156 1.70 pk return (rv);
1157 1.1 cgd }
1158 1.128 christos
1159 1.128 christos /*
1160 1.128 christos * Module infrastructure
1161 1.128 christos */
1162 1.128 christos #include "if_module.h"
1163 1.128 christos
1164 1.128 christos IF_MODULE(MODULE_CLASS_DRIVER, tun, "")
1165