| @@ -1,74 +1,70 @@ | | | @@ -1,74 +1,70 @@ |
1 | /* $NetBSD: if_tun.c,v 1.134 2017/01/11 13:08:29 ozaki-r Exp $ */ | | 1 | /* $NetBSD: if_tun.c,v 1.135 2017/01/23 15:32:04 skrll Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> | | 4 | * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk> |
5 | * Nottingham University 1987. | | 5 | * Nottingham University 1987. |
6 | * | | 6 | * |
7 | * This source may be freely distributed, however I would be interested | | 7 | * This source may be freely distributed, however I would be interested |
8 | * in any changes that are made. | | 8 | * in any changes that are made. |
9 | * | | 9 | * |
10 | * This driver takes packets off the IP i/f and hands them up to a | | 10 | * This driver takes packets off the IP i/f and hands them up to a |
11 | * user process to have its wicked way with. This driver has its | | 11 | * user process to have its wicked way with. This driver has its |
12 | * roots in a similar driver written by Phil Cockcroft (formerly) at | | 12 | * roots in a similar driver written by Phil Cockcroft (formerly) at |
13 | * UCL. This driver is based much more on read/write/poll mode of | | 13 | * UCL. This driver is based much more on read/write/poll mode of |
14 | * operation though. | | 14 | * operation though. |
15 | */ | | 15 | */ |
16 | | | 16 | |
17 | #include <sys/cdefs.h> | | 17 | #include <sys/cdefs.h> |
18 | __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.134 2017/01/11 13:08:29 ozaki-r Exp $"); | | 18 | __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.135 2017/01/23 15:32:04 skrll Exp $"); |
19 | | | 19 | |
20 | #ifdef _KERNEL_OPT | | 20 | #ifdef _KERNEL_OPT |
21 | #include "opt_inet.h" | | 21 | #include "opt_inet.h" |
22 | #endif | | 22 | #endif |
23 | | | 23 | |
24 | #include <sys/param.h> | | 24 | #include <sys/param.h> |
25 | #include <sys/proc.h> | | | |
26 | #include <sys/systm.h> | | | |
27 | #include <sys/mbuf.h> | | | |
28 | #include <sys/buf.h> | | 25 | #include <sys/buf.h> |
29 | #include <sys/socket.h> | | | |
30 | #include <sys/ioctl.h> | | | |
31 | #include <sys/errno.h> | | | |
32 | #include <sys/syslog.h> | | | |
33 | #include <sys/select.h> | | | |
34 | #include <sys/poll.h> | | | |
35 | #include <sys/file.h> | | | |
36 | #include <sys/signalvar.h> | | | |
37 | #include <sys/conf.h> | | 26 | #include <sys/conf.h> |
38 | #include <sys/kauth.h> | | | |
39 | #include <sys/mutex.h> | | | |
40 | #include <sys/cpu.h> | | 27 | #include <sys/cpu.h> |
41 | #include <sys/device.h> | | 28 | #include <sys/device.h> |
| | | 29 | #include <sys/errno.h> |
| | | 30 | #include <sys/file.h> |
| | | 31 | #include <sys/ioctl.h> |
| | | 32 | #include <sys/kauth.h> |
| | | 33 | #include <sys/mbuf.h> |
42 | #include <sys/module.h> | | 34 | #include <sys/module.h> |
| | | 35 | #include <sys/mutex.h> |
| | | 36 | #include <sys/poll.h> |
| | | 37 | #include <sys/proc.h> |
| | | 38 | #include <sys/select.h> |
| | | 39 | #include <sys/signalvar.h> |
| | | 40 | #include <sys/socket.h> |
| | | 41 | #include <sys/syslog.h> |
| | | 42 | #include <sys/systm.h> |
| | | 43 | #include <sys/time.h> |
43 | | | 44 | |
| | | 45 | #include <net/bpf.h> |
44 | #include <net/if.h> | | 46 | #include <net/if.h> |
45 | #include <net/if_types.h> | | 47 | #include <net/if_types.h> |
46 | #include <net/netisr.h> | | | |
47 | #include <net/route.h> | | 48 | #include <net/route.h> |
48 | | | 49 | |
49 | | | | |
50 | #ifdef INET | | 50 | #ifdef INET |
51 | #include <netinet/in.h> | | 51 | #include <netinet/in.h> |
52 | #include <netinet/in_systm.h> | | 52 | #include <netinet/in_systm.h> |
53 | #include <netinet/in_var.h> | | 53 | #include <netinet/in_var.h> |
54 | #include <netinet/ip.h> | | 54 | #include <netinet/ip.h> |
55 | #include <netinet/if_inarp.h> | | 55 | #include <netinet/if_inarp.h> |
56 | #endif | | 56 | #endif |
57 | | | 57 | |
58 | | | | |
59 | #include <sys/time.h> | | | |
60 | #include <net/bpf.h> | | | |
61 | | | | |
62 | #include <net/if_tun.h> | | 58 | #include <net/if_tun.h> |
63 | | | 59 | |
64 | #include "ioconf.h" | | 60 | #include "ioconf.h" |
65 | | | 61 | |
66 | #define TUNDEBUG if (tundebug) printf | | 62 | #define TUNDEBUG if (tundebug) printf |
67 | int tundebug = 0; | | 63 | int tundebug = 0; |
68 | | | 64 | |
69 | extern int ifqmaxlen; | | 65 | extern int ifqmaxlen; |
70 | | | 66 | |
71 | static LIST_HEAD(, tun_softc) tun_softc_list; | | 67 | static LIST_HEAD(, tun_softc) tun_softc_list; |
72 | static LIST_HEAD(, tun_softc) tunz_softc_list; | | 68 | static LIST_HEAD(, tun_softc) tunz_softc_list; |
73 | static kmutex_t tun_softc_lock; | | 69 | static kmutex_t tun_softc_lock; |
74 | | | 70 | |
| @@ -169,51 +165,51 @@ static struct tun_softc * | | | @@ -169,51 +165,51 @@ static struct tun_softc * |
169 | tun_find_unit(dev_t dev) | | 165 | tun_find_unit(dev_t dev) |
170 | { | | 166 | { |
171 | struct tun_softc *tp; | | 167 | struct tun_softc *tp; |
172 | int unit = minor(dev); | | 168 | int unit = minor(dev); |
173 | | | 169 | |
174 | mutex_enter(&tun_softc_lock); | | 170 | mutex_enter(&tun_softc_lock); |
175 | LIST_FOREACH(tp, &tun_softc_list, tun_list) | | 171 | LIST_FOREACH(tp, &tun_softc_list, tun_list) |
176 | if (unit == tp->tun_unit) | | 172 | if (unit == tp->tun_unit) |
177 | break; | | 173 | break; |
178 | if (tp) | | 174 | if (tp) |
179 | mutex_enter(&tp->tun_lock); | | 175 | mutex_enter(&tp->tun_lock); |
180 | mutex_exit(&tun_softc_lock); | | 176 | mutex_exit(&tun_softc_lock); |
181 | | | 177 | |
182 | return (tp); | | 178 | return tp; |
183 | } | | 179 | } |
184 | | | 180 | |
185 | /* | | 181 | /* |
186 | * Find zombie driver instance by unit number. | | 182 | * Find zombie driver instance by unit number. |
187 | * Remove tp from list and return it unlocked (if found). | | 183 | * Remove tp from list and return it unlocked (if found). |
188 | */ | | 184 | */ |
189 | static struct tun_softc * | | 185 | static struct tun_softc * |
190 | tun_find_zunit(int unit) | | 186 | tun_find_zunit(int unit) |
191 | { | | 187 | { |
192 | struct tun_softc *tp; | | 188 | struct tun_softc *tp; |
193 | | | 189 | |
194 | mutex_enter(&tun_softc_lock); | | 190 | mutex_enter(&tun_softc_lock); |
195 | LIST_FOREACH(tp, &tunz_softc_list, tun_list) | | 191 | LIST_FOREACH(tp, &tunz_softc_list, tun_list) |
196 | if (unit == tp->tun_unit) | | 192 | if (unit == tp->tun_unit) |
197 | break; | | 193 | break; |
198 | if (tp) | | 194 | if (tp) |
199 | LIST_REMOVE(tp, tun_list); | | 195 | LIST_REMOVE(tp, tun_list); |
200 | mutex_exit(&tun_softc_lock); | | 196 | mutex_exit(&tun_softc_lock); |
201 | #ifdef DIAGNOSTIC | | 197 | #ifdef DIAGNOSTIC |
202 | if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN) | | 198 | if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN) |
203 | printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags); | | 199 | printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags); |
204 | #endif | | 200 | #endif |
205 | | | 201 | |
206 | return (tp); | | 202 | return tp; |
207 | } | | 203 | } |
208 | | | 204 | |
209 | static int | | 205 | static int |
210 | tun_clone_create(struct if_clone *ifc, int unit) | | 206 | tun_clone_create(struct if_clone *ifc, int unit) |
211 | { | | 207 | { |
212 | struct tun_softc *tp; | | 208 | struct tun_softc *tp; |
213 | | | 209 | |
214 | if ((tp = tun_find_zunit(unit)) == NULL) { | | 210 | if ((tp = tun_find_zunit(unit)) == NULL) { |
215 | /* Allocate a new instance */ | | 211 | /* Allocate a new instance */ |
216 | tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO); | | 212 | tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO); |
217 | | | 213 | |
218 | tp->tun_unit = unit; | | 214 | tp->tun_unit = unit; |
219 | mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET); | | 215 | mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET); |
| @@ -224,27 +220,27 @@ tun_clone_create(struct if_clone *ifc, i | | | @@ -224,27 +220,27 @@ tun_clone_create(struct if_clone *ifc, i |
224 | (void)memset(&tp->tun_if, 0, sizeof(struct ifnet)); | | 220 | (void)memset(&tp->tun_if, 0, sizeof(struct ifnet)); |
225 | } | | 221 | } |
226 | | | 222 | |
227 | if_initname(&tp->tun_if, ifc->ifc_name, unit); | | 223 | if_initname(&tp->tun_if, ifc->ifc_name, unit); |
228 | tunattach0(tp); | | 224 | tunattach0(tp); |
229 | tp->tun_flags |= TUN_INITED; | | 225 | tp->tun_flags |= TUN_INITED; |
230 | tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp); | | 226 | tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp); |
231 | tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp); | | 227 | tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp); |
232 | | | 228 | |
233 | mutex_enter(&tun_softc_lock); | | 229 | mutex_enter(&tun_softc_lock); |
234 | LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list); | | 230 | LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list); |
235 | mutex_exit(&tun_softc_lock); | | 231 | mutex_exit(&tun_softc_lock); |
236 | | | 232 | |
237 | return (0); | | 233 | return 0; |
238 | } | | 234 | } |
239 | | | 235 | |
240 | static void | | 236 | static void |
241 | tunattach0(struct tun_softc *tp) | | 237 | tunattach0(struct tun_softc *tp) |
242 | { | | 238 | { |
243 | struct ifnet *ifp; | | 239 | struct ifnet *ifp; |
244 | | | 240 | |
245 | ifp = &tp->tun_if; | | 241 | ifp = &tp->tun_if; |
246 | ifp->if_softc = tp; | | 242 | ifp->if_softc = tp; |
247 | ifp->if_mtu = TUNMTU; | | 243 | ifp->if_mtu = TUNMTU; |
248 | ifp->if_ioctl = tun_ioctl; | | 244 | ifp->if_ioctl = tun_ioctl; |
249 | ifp->if_output = tun_output; | | 245 | ifp->if_output = tun_output; |
250 | #ifdef ALTQ | | 246 | #ifdef ALTQ |
| @@ -300,68 +296,68 @@ tun_clone_destroy(struct ifnet *ifp) | | | @@ -300,68 +296,68 @@ tun_clone_destroy(struct ifnet *ifp) |
300 | | | 296 | |
301 | bpf_detach(ifp); | | 297 | bpf_detach(ifp); |
302 | if_detach(ifp); | | 298 | if_detach(ifp); |
303 | | | 299 | |
304 | if (!zombie) { | | 300 | if (!zombie) { |
305 | seldestroy(&tp->tun_rsel); | | 301 | seldestroy(&tp->tun_rsel); |
306 | seldestroy(&tp->tun_wsel); | | 302 | seldestroy(&tp->tun_wsel); |
307 | softint_disestablish(tp->tun_osih); | | 303 | softint_disestablish(tp->tun_osih); |
308 | softint_disestablish(tp->tun_isih); | | 304 | softint_disestablish(tp->tun_isih); |
309 | mutex_destroy(&tp->tun_lock); | | 305 | mutex_destroy(&tp->tun_lock); |
310 | free(tp, M_DEVBUF); | | 306 | free(tp, M_DEVBUF); |
311 | } | | 307 | } |
312 | | | 308 | |
313 | return (0); | | 309 | return 0; |
314 | } | | 310 | } |
315 | | | 311 | |
316 | /* | | 312 | /* |
317 | * tunnel open - must be superuser & the device must be | | 313 | * tunnel open - must be superuser & the device must be |
318 | * configured in | | 314 | * configured in |
319 | */ | | 315 | */ |
320 | static int | | 316 | static int |
321 | tunopen(dev_t dev, int flag, int mode, struct lwp *l) | | 317 | tunopen(dev_t dev, int flag, int mode, struct lwp *l) |
322 | { | | 318 | { |
323 | struct ifnet *ifp; | | 319 | struct ifnet *ifp; |
324 | struct tun_softc *tp; | | 320 | struct tun_softc *tp; |
325 | int error; | | 321 | int error; |
326 | | | 322 | |
327 | error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE_TUN, | | 323 | error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE_TUN, |
328 | KAUTH_REQ_NETWORK_INTERFACE_TUN_ADD, NULL, NULL, NULL); | | 324 | KAUTH_REQ_NETWORK_INTERFACE_TUN_ADD, NULL, NULL, NULL); |
329 | if (error) | | 325 | if (error) |
330 | return (error); | | 326 | return error; |
331 | | | 327 | |
332 | tp = tun_find_unit(dev); | | 328 | tp = tun_find_unit(dev); |
333 | | | 329 | |
334 | if (tp == NULL) { | | 330 | if (tp == NULL) { |
335 | (void)tun_clone_create(&tun_cloner, minor(dev)); | | 331 | (void)tun_clone_create(&tun_cloner, minor(dev)); |
336 | tp = tun_find_unit(dev); | | 332 | tp = tun_find_unit(dev); |
337 | if (tp == NULL) { | | 333 | if (tp == NULL) { |
338 | error = ENXIO; | | 334 | error = ENXIO; |
339 | goto out_nolock; | | 335 | goto out_nolock; |
340 | } | | 336 | } |
341 | } | | 337 | } |
342 | | | 338 | |
343 | if (tp->tun_flags & TUN_OPEN) { | | 339 | if (tp->tun_flags & TUN_OPEN) { |
344 | error = EBUSY; | | 340 | error = EBUSY; |
345 | goto out; | | 341 | goto out; |
346 | } | | 342 | } |
347 | | | 343 | |
348 | ifp = &tp->tun_if; | | 344 | ifp = &tp->tun_if; |
349 | tp->tun_flags |= TUN_OPEN; | | 345 | tp->tun_flags |= TUN_OPEN; |
350 | TUNDEBUG("%s: open\n", ifp->if_xname); | | 346 | TUNDEBUG("%s: open\n", ifp->if_xname); |
351 | out: | | 347 | out: |
352 | mutex_exit(&tp->tun_lock); | | 348 | mutex_exit(&tp->tun_lock); |
353 | out_nolock: | | 349 | out_nolock: |
354 | return (error); | | 350 | return error; |
355 | } | | 351 | } |
356 | | | 352 | |
357 | /* | | 353 | /* |
358 | * tunclose - close the device - mark i/f down & delete | | 354 | * tunclose - close the device - mark i/f down & delete |
359 | * routing info | | 355 | * routing info |
360 | */ | | 356 | */ |
361 | int | | 357 | int |
362 | tunclose(dev_t dev, int flag, int mode, | | 358 | tunclose(dev_t dev, int flag, int mode, |
363 | struct lwp *l) | | 359 | struct lwp *l) |
364 | { | | 360 | { |
365 | struct tun_softc *tp; | | 361 | struct tun_softc *tp; |
366 | struct ifnet *ifp; | | 362 | struct ifnet *ifp; |
367 | | | 363 | |
| @@ -403,27 +399,27 @@ tunclose(dev_t dev, int flag, int mode, | | | @@ -403,27 +399,27 @@ tunclose(dev_t dev, int flag, int mode, |
403 | #if defined(INET) || defined(INET6) | | 399 | #if defined(INET) || defined(INET6) |
404 | if (ifa->ifa_addr->sa_family == AF_INET || | | 400 | if (ifa->ifa_addr->sa_family == AF_INET || |
405 | ifa->ifa_addr->sa_family == AF_INET6) { | | 401 | ifa->ifa_addr->sa_family == AF_INET6) { |
406 | rtinit(ifa, (int)RTM_DELETE, | | 402 | rtinit(ifa, (int)RTM_DELETE, |
407 | tp->tun_flags & TUN_DSTADDR | | 403 | tp->tun_flags & TUN_DSTADDR |
408 | ? RTF_HOST | | 404 | ? RTF_HOST |
409 | : 0); | | 405 | : 0); |
410 | } | | 406 | } |
411 | #endif | | 407 | #endif |
412 | } | | 408 | } |
413 | } | | 409 | } |
414 | } | | 410 | } |
415 | out_nolock: | | 411 | out_nolock: |
416 | return (0); | | 412 | return 0; |
417 | } | | 413 | } |
418 | | | 414 | |
419 | /* | | 415 | /* |
420 | * Call at splnet(). | | 416 | * Call at splnet(). |
421 | */ | | 417 | */ |
422 | static void | | 418 | static void |
423 | tun_enable(struct tun_softc *tp, const struct ifaddr *ifa) | | 419 | tun_enable(struct tun_softc *tp, const struct ifaddr *ifa) |
424 | { | | 420 | { |
425 | struct ifnet *ifp = &tp->tun_if; | | 421 | struct ifnet *ifp = &tp->tun_if; |
426 | | | 422 | |
427 | TUNDEBUG("%s: %s\n", __func__, ifp->if_xname); | | 423 | TUNDEBUG("%s: %s\n", __func__, ifp->if_xname); |
428 | | | 424 | |
429 | mutex_enter(&tp->tun_lock); | | 425 | mutex_enter(&tp->tun_lock); |
| @@ -517,27 +513,27 @@ tun_ioctl(struct ifnet *ifp, u_long cmd, | | | @@ -517,27 +513,27 @@ tun_ioctl(struct ifnet *ifp, u_long cmd, |
517 | case AF_INET6: | | 513 | case AF_INET6: |
518 | break; | | 514 | break; |
519 | #endif | | 515 | #endif |
520 | default: | | 516 | default: |
521 | error = EAFNOSUPPORT; | | 517 | error = EAFNOSUPPORT; |
522 | break; | | 518 | break; |
523 | } | | 519 | } |
524 | break; | | 520 | break; |
525 | default: | | 521 | default: |
526 | error = ifioctl_common(ifp, cmd, data); | | 522 | error = ifioctl_common(ifp, cmd, data); |
527 | } | | 523 | } |
528 | | | 524 | |
529 | splx(s); | | 525 | splx(s); |
530 | return (error); | | 526 | return error; |
531 | } | | 527 | } |
532 | | | 528 | |
533 | /* | | 529 | /* |
534 | * tun_output - queue packets from higher level ready to put out. | | 530 | * tun_output - queue packets from higher level ready to put out. |
535 | */ | | 531 | */ |
536 | static int | | 532 | static int |
537 | tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, | | 533 | tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst, |
538 | const struct rtentry *rt) | | 534 | const struct rtentry *rt) |
539 | { | | 535 | { |
540 | struct tun_softc *tp = ifp->if_softc; | | 536 | struct tun_softc *tp = ifp->if_softc; |
541 | int s; | | 537 | int s; |
542 | int error; | | 538 | int error; |
543 | #if defined(INET) || defined(INET6) | | 539 | #if defined(INET) || defined(INET6) |
| @@ -753,27 +749,27 @@ tunioctl(dev_t dev, u_long cmd, void *da | | | @@ -753,27 +749,27 @@ tunioctl(dev_t dev, u_long cmd, void *da |
753 | case TIOCGPGRP: | | 749 | case TIOCGPGRP: |
754 | case FIOGETOWN: | | 750 | case FIOGETOWN: |
755 | error = fgetown(tp->tun_pgid, cmd, data); | | 751 | error = fgetown(tp->tun_pgid, cmd, data); |
756 | break; | | 752 | break; |
757 | | | 753 | |
758 | default: | | 754 | default: |
759 | error = ENOTTY; | | 755 | error = ENOTTY; |
760 | } | | 756 | } |
761 | | | 757 | |
762 | out: | | 758 | out: |
763 | mutex_exit(&tp->tun_lock); | | 759 | mutex_exit(&tp->tun_lock); |
764 | out_nolock: | | 760 | out_nolock: |
765 | splx(s); | | 761 | splx(s); |
766 | return (error); | | 762 | return error; |
767 | } | | 763 | } |
768 | | | 764 | |
769 | /* | | 765 | /* |
770 | * The cdevsw read interface - reads a packet at a time, or at | | 766 | * The cdevsw read interface - reads a packet at a time, or at |
771 | * least as much of a packet as can be read. | | 767 | * least as much of a packet as can be read. |
772 | */ | | 768 | */ |
773 | int | | 769 | int |
774 | tunread(dev_t dev, struct uio *uio, int ioflag) | | 770 | tunread(dev_t dev, struct uio *uio, int ioflag) |
775 | { | | 771 | { |
776 | struct tun_softc *tp; | | 772 | struct tun_softc *tp; |
777 | struct ifnet *ifp; | | 773 | struct ifnet *ifp; |
778 | struct mbuf *m, *m0; | | 774 | struct mbuf *m, *m0; |
779 | int error = 0, len, s, index; | | 775 | int error = 0, len, s, index; |
| @@ -839,33 +835,33 @@ tunread(dev_t dev, struct uio *uio, int | | | @@ -839,33 +835,33 @@ tunread(dev_t dev, struct uio *uio, int |
839 | len = min(uio->uio_resid, m0->m_len); | | 835 | len = min(uio->uio_resid, m0->m_len); |
840 | if (len != 0) | | 836 | if (len != 0) |
841 | error = uiomove(mtod(m0, void *), len, uio); | | 837 | error = uiomove(mtod(m0, void *), len, uio); |
842 | m0 = m = m_free(m0); | | 838 | m0 = m = m_free(m0); |
843 | } | | 839 | } |
844 | | | 840 | |
845 | if (m0) { | | 841 | if (m0) { |
846 | TUNDEBUG("Dropping mbuf\n"); | | 842 | TUNDEBUG("Dropping mbuf\n"); |
847 | m_freem(m0); | | 843 | m_freem(m0); |
848 | } | | 844 | } |
849 | if (error) | | 845 | if (error) |
850 | ifp->if_ierrors++; | | 846 | ifp->if_ierrors++; |
851 | | | 847 | |
852 | return (error); | | 848 | return error; |
853 | | | 849 | |
854 | out: | | 850 | out: |
855 | mutex_exit(&tp->tun_lock); | | 851 | mutex_exit(&tp->tun_lock); |
856 | out_nolock: | | 852 | out_nolock: |
857 | splx(s); | | 853 | splx(s); |
858 | return (error); | | 854 | return error; |
859 | } | | 855 | } |
860 | | | 856 | |
861 | /* | | 857 | /* |
862 | * the cdevsw write interface - an atomic write is a packet - or else! | | 858 | * the cdevsw write interface - an atomic write is a packet - or else! |
863 | */ | | 859 | */ |
864 | int | | 860 | int |
865 | tunwrite(dev_t dev, struct uio *uio, int ioflag) | | 861 | tunwrite(dev_t dev, struct uio *uio, int ioflag) |
866 | { | | 862 | { |
867 | struct tun_softc *tp; | | 863 | struct tun_softc *tp; |
868 | struct ifnet *ifp; | | 864 | struct ifnet *ifp; |
869 | struct mbuf *top, **mp, *m; | | 865 | struct mbuf *top, **mp, *m; |
870 | pktqueue_t *pktq; | | 866 | pktqueue_t *pktq; |
871 | struct sockaddr dst; | | 867 | struct sockaddr dst; |
| @@ -989,27 +985,27 @@ tunwrite(dev_t dev, struct uio *uio, int | | | @@ -989,27 +985,27 @@ tunwrite(dev_t dev, struct uio *uio, int |
989 | ifp->if_collisions++; | | 985 | ifp->if_collisions++; |
990 | mutex_exit(&tp->tun_lock); | | 986 | mutex_exit(&tp->tun_lock); |
991 | error = ENOBUFS; | | 987 | error = ENOBUFS; |
992 | m_freem(top); | | 988 | m_freem(top); |
993 | goto out_nolock; | | 989 | goto out_nolock; |
994 | } | | 990 | } |
995 | ifp->if_ipackets++; | | 991 | ifp->if_ipackets++; |
996 | ifp->if_ibytes += tlen; | | 992 | ifp->if_ibytes += tlen; |
997 | out: | | 993 | out: |
998 | mutex_exit(&tp->tun_lock); | | 994 | mutex_exit(&tp->tun_lock); |
999 | out_nolock: | | 995 | out_nolock: |
1000 | splx(s); | | 996 | splx(s); |
1001 | out0: | | 997 | out0: |
1002 | return (error); | | 998 | return error; |
1003 | } | | 999 | } |
1004 | | | 1000 | |
1005 | #ifdef ALTQ | | 1001 | #ifdef ALTQ |
1006 | /* | | 1002 | /* |
1007 | * Start packet transmission on the interface. | | 1003 | * Start packet transmission on the interface. |
1008 | * when the interface queue is rate-limited by ALTQ or TBR, | | 1004 | * when the interface queue is rate-limited by ALTQ or TBR, |
1009 | * if_start is needed to drain packets from the queue in order | | 1005 | * if_start is needed to drain packets from the queue in order |
1010 | * to notify readers when outgoing packets become ready. | | 1006 | * to notify readers when outgoing packets become ready. |
1011 | * | | 1007 | * |
1012 | * Should be called at splnet. | | 1008 | * Should be called at splnet. |
1013 | */ | | 1009 | */ |
1014 | static void | | 1010 | static void |
1015 | tunstart(struct ifnet *ifp) | | 1011 | tunstart(struct ifnet *ifp) |
| @@ -1063,60 +1059,60 @@ tunpoll(dev_t dev, int events, struct lw | | | @@ -1063,60 +1059,60 @@ tunpoll(dev_t dev, int events, struct lw |
1063 | revents |= events & (POLLIN | POLLRDNORM); | | 1059 | revents |= events & (POLLIN | POLLRDNORM); |
1064 | } else { | | 1060 | } else { |
1065 | TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname); | | 1061 | TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname); |
1066 | selrecord(l, &tp->tun_rsel); | | 1062 | selrecord(l, &tp->tun_rsel); |
1067 | } | | 1063 | } |
1068 | } | | 1064 | } |
1069 | | | 1065 | |
1070 | if (events & (POLLOUT | POLLWRNORM)) | | 1066 | if (events & (POLLOUT | POLLWRNORM)) |
1071 | revents |= events & (POLLOUT | POLLWRNORM); | | 1067 | revents |= events & (POLLOUT | POLLWRNORM); |
1072 | | | 1068 | |
1073 | mutex_exit(&tp->tun_lock); | | 1069 | mutex_exit(&tp->tun_lock); |
1074 | out_nolock: | | 1070 | out_nolock: |
1075 | splx(s); | | 1071 | splx(s); |
1076 | return (revents); | | 1072 | return revents; |
1077 | } | | 1073 | } |
1078 | | | 1074 | |
1079 | static void | | 1075 | static void |
1080 | filt_tunrdetach(struct knote *kn) | | 1076 | filt_tunrdetach(struct knote *kn) |
1081 | { | | 1077 | { |
1082 | struct tun_softc *tp = kn->kn_hook; | | 1078 | struct tun_softc *tp = kn->kn_hook; |
1083 | int s; | | 1079 | int s; |
1084 | | | 1080 | |
1085 | s = splnet(); | | 1081 | s = splnet(); |
1086 | SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext); | | 1082 | SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext); |
1087 | splx(s); | | 1083 | splx(s); |
1088 | } | | 1084 | } |
1089 | | | 1085 | |
1090 | static int | | 1086 | static int |
1091 | filt_tunread(struct knote *kn, long hint) | | 1087 | filt_tunread(struct knote *kn, long hint) |
1092 | { | | 1088 | { |
1093 | struct tun_softc *tp = kn->kn_hook; | | 1089 | struct tun_softc *tp = kn->kn_hook; |
1094 | struct ifnet *ifp = &tp->tun_if; | | 1090 | struct ifnet *ifp = &tp->tun_if; |
1095 | struct mbuf *m; | | 1091 | struct mbuf *m; |
1096 | int s; | | 1092 | int s; |
1097 | | | 1093 | |
1098 | s = splnet(); | | 1094 | s = splnet(); |
1099 | IF_POLL(&ifp->if_snd, m); | | 1095 | IF_POLL(&ifp->if_snd, m); |
1100 | if (m == NULL) { | | 1096 | if (m == NULL) { |
1101 | splx(s); | | 1097 | splx(s); |
1102 | return (0); | | 1098 | return 0; |
1103 | } | | 1099 | } |
1104 | | | 1100 | |
1105 | for (kn->kn_data = 0; m != NULL; m = m->m_next) | | 1101 | for (kn->kn_data = 0; m != NULL; m = m->m_next) |
1106 | kn->kn_data += m->m_len; | | 1102 | kn->kn_data += m->m_len; |
1107 | | | 1103 | |
1108 | splx(s); | | 1104 | splx(s); |
1109 | return (1); | | 1105 | return 1; |
1110 | } | | 1106 | } |
1111 | | | 1107 | |
1112 | static const struct filterops tunread_filtops = | | 1108 | static const struct filterops tunread_filtops = |
1113 | { 1, NULL, filt_tunrdetach, filt_tunread }; | | 1109 | { 1, NULL, filt_tunrdetach, filt_tunread }; |
1114 | | | 1110 | |
1115 | static const struct filterops tun_seltrue_filtops = | | 1111 | static const struct filterops tun_seltrue_filtops = |
1116 | { 1, NULL, filt_tunrdetach, filt_seltrue }; | | 1112 | { 1, NULL, filt_tunrdetach, filt_seltrue }; |
1117 | | | 1113 | |
1118 | int | | 1114 | int |
1119 | tunkqfilter(dev_t dev, struct knote *kn) | | 1115 | tunkqfilter(dev_t dev, struct knote *kn) |
1120 | { | | 1116 | { |
1121 | struct tun_softc *tp; | | 1117 | struct tun_softc *tp; |
1122 | struct klist *klist; | | 1118 | struct klist *klist; |
| @@ -1141,22 +1137,22 @@ tunkqfilter(dev_t dev, struct knote *kn) | | | @@ -1141,22 +1137,22 @@ tunkqfilter(dev_t dev, struct knote *kn) |
1141 | default: | | 1137 | default: |
1142 | rv = EINVAL; | | 1138 | rv = EINVAL; |
1143 | goto out; | | 1139 | goto out; |
1144 | } | | 1140 | } |
1145 | | | 1141 | |
1146 | kn->kn_hook = tp; | | 1142 | kn->kn_hook = tp; |
1147 | | | 1143 | |
1148 | SLIST_INSERT_HEAD(klist, kn, kn_selnext); | | 1144 | SLIST_INSERT_HEAD(klist, kn, kn_selnext); |
1149 | | | 1145 | |
1150 | out: | | 1146 | out: |
1151 | mutex_exit(&tp->tun_lock); | | 1147 | mutex_exit(&tp->tun_lock); |
1152 | out_nolock: | | 1148 | out_nolock: |
1153 | splx(s); | | 1149 | splx(s); |
1154 | return (rv); | | 1150 | return rv; |
1155 | } | | 1151 | } |
1156 | | | 1152 | |
1157 | /* | | 1153 | /* |
1158 | * Module infrastructure | | 1154 | * Module infrastructure |
1159 | */ | | 1155 | */ |
1160 | #include "if_module.h" | | 1156 | #include "if_module.h" |
1161 | | | 1157 | |
1162 | IF_MODULE(MODULE_CLASS_DRIVER, tun, "") | | 1158 | IF_MODULE(MODULE_CLASS_DRIVER, tun, "") |