| @@ -1,1162 +1,1158 @@ | | | @@ -1,1162 +1,1158 @@ |
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 | |
75 | static int tun_ioctl(struct ifnet *, u_long, void *); | | 71 | static int tun_ioctl(struct ifnet *, u_long, void *); |
76 | static int tun_output(struct ifnet *, struct mbuf *, | | 72 | static int tun_output(struct ifnet *, struct mbuf *, |
77 | const struct sockaddr *, const struct rtentry *rt); | | 73 | const struct sockaddr *, const struct rtentry *rt); |
78 | static int tun_clone_create(struct if_clone *, int); | | 74 | static int tun_clone_create(struct if_clone *, int); |
79 | static int tun_clone_destroy(struct ifnet *); | | 75 | static int tun_clone_destroy(struct ifnet *); |
80 | | | 76 | |
81 | static struct if_clone tun_cloner = | | 77 | static struct if_clone tun_cloner = |
82 | IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy); | | 78 | IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy); |
83 | | | 79 | |
84 | static void tunattach0(struct tun_softc *); | | 80 | static void tunattach0(struct tun_softc *); |
85 | static void tun_enable(struct tun_softc *, const struct ifaddr *); | | 81 | static void tun_enable(struct tun_softc *, const struct ifaddr *); |
86 | static void tun_i_softintr(void *); | | 82 | static void tun_i_softintr(void *); |
87 | static void tun_o_softintr(void *); | | 83 | static void tun_o_softintr(void *); |
88 | #ifdef ALTQ | | 84 | #ifdef ALTQ |
89 | static void tunstart(struct ifnet *); | | 85 | static void tunstart(struct ifnet *); |
90 | #endif | | 86 | #endif |
91 | static struct tun_softc *tun_find_unit(dev_t); | | 87 | static struct tun_softc *tun_find_unit(dev_t); |
92 | static struct tun_softc *tun_find_zunit(int); | | 88 | static struct tun_softc *tun_find_zunit(int); |
93 | | | 89 | |
94 | static dev_type_open(tunopen); | | 90 | static dev_type_open(tunopen); |
95 | static dev_type_close(tunclose); | | 91 | static dev_type_close(tunclose); |
96 | static dev_type_read(tunread); | | 92 | static dev_type_read(tunread); |
97 | static dev_type_write(tunwrite); | | 93 | static dev_type_write(tunwrite); |
98 | static dev_type_ioctl(tunioctl); | | 94 | static dev_type_ioctl(tunioctl); |
99 | static dev_type_poll(tunpoll); | | 95 | static dev_type_poll(tunpoll); |
100 | static dev_type_kqfilter(tunkqfilter); | | 96 | static dev_type_kqfilter(tunkqfilter); |
101 | | | 97 | |
102 | const struct cdevsw tun_cdevsw = { | | 98 | const struct cdevsw tun_cdevsw = { |
103 | .d_open = tunopen, | | 99 | .d_open = tunopen, |
104 | .d_close = tunclose, | | 100 | .d_close = tunclose, |
105 | .d_read = tunread, | | 101 | .d_read = tunread, |
106 | .d_write = tunwrite, | | 102 | .d_write = tunwrite, |
107 | .d_ioctl = tunioctl, | | 103 | .d_ioctl = tunioctl, |
108 | .d_stop = nostop, | | 104 | .d_stop = nostop, |
109 | .d_tty = notty, | | 105 | .d_tty = notty, |
110 | .d_poll = tunpoll, | | 106 | .d_poll = tunpoll, |
111 | .d_mmap = nommap, | | 107 | .d_mmap = nommap, |
112 | .d_kqfilter = tunkqfilter, | | 108 | .d_kqfilter = tunkqfilter, |
113 | .d_discard = nodiscard, | | 109 | .d_discard = nodiscard, |
114 | .d_flag = D_OTHER | | 110 | .d_flag = D_OTHER |
115 | }; | | 111 | }; |
116 | | | 112 | |
117 | #ifdef _MODULE | | 113 | #ifdef _MODULE |
118 | devmajor_t tun_bmajor = -1, tun_cmajor = -1; | | 114 | devmajor_t tun_bmajor = -1, tun_cmajor = -1; |
119 | #endif | | 115 | #endif |
120 | | | 116 | |
121 | void | | 117 | void |
122 | tunattach(int unused) | | 118 | tunattach(int unused) |
123 | { | | 119 | { |
124 | | | 120 | |
125 | /* | | 121 | /* |
126 | * Nothing to do here, initialization is handled by the | | 122 | * Nothing to do here, initialization is handled by the |
127 | * module initialization code in tuninit() below). | | 123 | * module initialization code in tuninit() below). |
128 | */ | | 124 | */ |
129 | } | | 125 | } |
130 | | | 126 | |
131 | static void | | 127 | static void |
132 | tuninit(void) | | 128 | tuninit(void) |
133 | { | | 129 | { |
134 | | | 130 | |
135 | mutex_init(&tun_softc_lock, MUTEX_DEFAULT, IPL_NET); | | 131 | mutex_init(&tun_softc_lock, MUTEX_DEFAULT, IPL_NET); |
136 | LIST_INIT(&tun_softc_list); | | 132 | LIST_INIT(&tun_softc_list); |
137 | LIST_INIT(&tunz_softc_list); | | 133 | LIST_INIT(&tunz_softc_list); |
138 | if_clone_attach(&tun_cloner); | | 134 | if_clone_attach(&tun_cloner); |
139 | #ifdef _MODULE | | 135 | #ifdef _MODULE |
140 | devsw_attach("tun", NULL, &tun_bmajor, &tun_cdevsw, &tun_cmajor); | | 136 | devsw_attach("tun", NULL, &tun_bmajor, &tun_cdevsw, &tun_cmajor); |
141 | #endif | | 137 | #endif |
142 | } | | 138 | } |
143 | | | 139 | |
144 | static int | | 140 | static int |
145 | tundetach(void) | | 141 | tundetach(void) |
146 | { | | 142 | { |
147 | int error = 0; | | 143 | int error = 0; |
148 | | | 144 | |
149 | if (!LIST_EMPTY(&tun_softc_list) || !LIST_EMPTY(&tunz_softc_list)) | | 145 | if (!LIST_EMPTY(&tun_softc_list) || !LIST_EMPTY(&tunz_softc_list)) |
150 | error = EBUSY; | | 146 | error = EBUSY; |
151 | | | 147 | |
152 | #ifdef _MODULE | | 148 | #ifdef _MODULE |
153 | if (error == 0) | | 149 | if (error == 0) |
154 | error = devsw_detach(NULL, &tun_cdevsw); | | 150 | error = devsw_detach(NULL, &tun_cdevsw); |
155 | #endif | | 151 | #endif |
156 | if (error == 0) { | | 152 | if (error == 0) { |
157 | if_clone_detach(&tun_cloner); | | 153 | if_clone_detach(&tun_cloner); |
158 | mutex_destroy(&tun_softc_lock); | | 154 | mutex_destroy(&tun_softc_lock); |
159 | } | | 155 | } |
160 | | | 156 | |
161 | return error; | | 157 | return error; |
162 | } | | 158 | } |
163 | | | 159 | |
164 | /* | | 160 | /* |
165 | * Find driver instance from dev_t. | | 161 | * Find driver instance from dev_t. |
166 | * Returns with tp locked (if found). | | 162 | * Returns with tp locked (if found). |
167 | */ | | 163 | */ |
168 | static struct tun_softc * | | 164 | 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); |
220 | selinit(&tp->tun_rsel); | | 216 | selinit(&tp->tun_rsel); |
221 | selinit(&tp->tun_wsel); | | 217 | selinit(&tp->tun_wsel); |
222 | } else { | | 218 | } else { |
223 | /* Revive tunnel instance; clear ifp part */ | | 219 | /* Revive tunnel instance; clear ifp part */ |
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 |
251 | ifp->if_start = tunstart; | | 247 | ifp->if_start = tunstart; |
252 | #endif | | 248 | #endif |
253 | ifp->if_flags = IFF_POINTOPOINT; | | 249 | ifp->if_flags = IFF_POINTOPOINT; |
254 | ifp->if_type = IFT_TUNNEL; | | 250 | ifp->if_type = IFT_TUNNEL; |
255 | ifp->if_snd.ifq_maxlen = ifqmaxlen; | | 251 | ifp->if_snd.ifq_maxlen = ifqmaxlen; |
256 | ifp->if_collisions = 0; | | 252 | ifp->if_collisions = 0; |
257 | ifp->if_ierrors = 0; | | 253 | ifp->if_ierrors = 0; |
258 | ifp->if_oerrors = 0; | | 254 | ifp->if_oerrors = 0; |
259 | ifp->if_ipackets = 0; | | 255 | ifp->if_ipackets = 0; |
260 | ifp->if_opackets = 0; | | 256 | ifp->if_opackets = 0; |
261 | ifp->if_ibytes = 0; | | 257 | ifp->if_ibytes = 0; |
262 | ifp->if_obytes = 0; | | 258 | ifp->if_obytes = 0; |
263 | ifp->if_dlt = DLT_NULL; | | 259 | ifp->if_dlt = DLT_NULL; |
264 | IFQ_SET_READY(&ifp->if_snd); | | 260 | IFQ_SET_READY(&ifp->if_snd); |
265 | if_attach(ifp); | | 261 | if_attach(ifp); |
266 | if_alloc_sadl(ifp); | | 262 | if_alloc_sadl(ifp); |
267 | bpf_attach(ifp, DLT_NULL, sizeof(uint32_t)); | | 263 | bpf_attach(ifp, DLT_NULL, sizeof(uint32_t)); |
268 | } | | 264 | } |
269 | | | 265 | |
270 | static int | | 266 | static int |
271 | tun_clone_destroy(struct ifnet *ifp) | | 267 | tun_clone_destroy(struct ifnet *ifp) |
272 | { | | 268 | { |
273 | struct tun_softc *tp = (void *)ifp; | | 269 | struct tun_softc *tp = (void *)ifp; |
274 | int zombie = 0; | | 270 | int zombie = 0; |
275 | | | 271 | |
276 | IF_PURGE(&ifp->if_snd); | | 272 | IF_PURGE(&ifp->if_snd); |
277 | ifp->if_flags &= ~IFF_RUNNING; | | 273 | ifp->if_flags &= ~IFF_RUNNING; |
278 | | | 274 | |
279 | mutex_enter(&tun_softc_lock); | | 275 | mutex_enter(&tun_softc_lock); |
280 | mutex_enter(&tp->tun_lock); | | 276 | mutex_enter(&tp->tun_lock); |
281 | LIST_REMOVE(tp, tun_list); | | 277 | LIST_REMOVE(tp, tun_list); |
282 | if (tp->tun_flags & TUN_OPEN) { | | 278 | if (tp->tun_flags & TUN_OPEN) { |
283 | /* Hang on to storage until last close */ | | 279 | /* Hang on to storage until last close */ |
284 | zombie = 1; | | 280 | zombie = 1; |
285 | tp->tun_flags &= ~TUN_INITED; | | 281 | tp->tun_flags &= ~TUN_INITED; |
286 | LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list); | | 282 | LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list); |
287 | } | | 283 | } |
288 | mutex_exit(&tun_softc_lock); | | 284 | mutex_exit(&tun_softc_lock); |
289 | | | 285 | |
290 | if (tp->tun_flags & TUN_RWAIT) { | | 286 | if (tp->tun_flags & TUN_RWAIT) { |
291 | tp->tun_flags &= ~TUN_RWAIT; | | 287 | tp->tun_flags &= ~TUN_RWAIT; |
292 | wakeup((void *)tp); | | 288 | wakeup((void *)tp); |
293 | } | | 289 | } |
294 | selnotify(&tp->tun_rsel, 0, 0); | | 290 | selnotify(&tp->tun_rsel, 0, 0); |
295 | | | 291 | |
296 | mutex_exit(&tp->tun_lock); | | 292 | mutex_exit(&tp->tun_lock); |
297 | | | 293 | |
298 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) | | 294 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) |
299 | fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL); | | 295 | fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL); |
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 | |
368 | if ((tp = tun_find_zunit(minor(dev))) != NULL) { | | 364 | if ((tp = tun_find_zunit(minor(dev))) != NULL) { |
369 | /* interface was "destroyed" before the close */ | | 365 | /* interface was "destroyed" before the close */ |
370 | seldestroy(&tp->tun_rsel); | | 366 | seldestroy(&tp->tun_rsel); |
371 | seldestroy(&tp->tun_wsel); | | 367 | seldestroy(&tp->tun_wsel); |
372 | softint_disestablish(tp->tun_osih); | | 368 | softint_disestablish(tp->tun_osih); |
373 | softint_disestablish(tp->tun_isih); | | 369 | softint_disestablish(tp->tun_isih); |
374 | mutex_destroy(&tp->tun_lock); | | 370 | mutex_destroy(&tp->tun_lock); |
375 | free(tp, M_DEVBUF); | | 371 | free(tp, M_DEVBUF); |
376 | goto out_nolock; | | 372 | goto out_nolock; |
377 | } | | 373 | } |
378 | | | 374 | |
379 | if ((tp = tun_find_unit(dev)) == NULL) | | 375 | if ((tp = tun_find_unit(dev)) == NULL) |
380 | goto out_nolock; | | 376 | goto out_nolock; |
381 | | | 377 | |
382 | ifp = &tp->tun_if; | | 378 | ifp = &tp->tun_if; |
383 | | | 379 | |
384 | tp->tun_flags &= ~TUN_OPEN; | | 380 | tp->tun_flags &= ~TUN_OPEN; |
385 | | | 381 | |
386 | tp->tun_pgid = 0; | | 382 | tp->tun_pgid = 0; |
387 | selnotify(&tp->tun_rsel, 0, 0); | | 383 | selnotify(&tp->tun_rsel, 0, 0); |
388 | | | 384 | |
389 | TUNDEBUG ("%s: closed\n", ifp->if_xname); | | 385 | TUNDEBUG ("%s: closed\n", ifp->if_xname); |
390 | mutex_exit(&tp->tun_lock); | | 386 | mutex_exit(&tp->tun_lock); |
391 | | | 387 | |
392 | /* | | 388 | /* |
393 | * junk all pending output | | 389 | * junk all pending output |
394 | */ | | 390 | */ |
395 | IFQ_PURGE(&ifp->if_snd); | | 391 | IFQ_PURGE(&ifp->if_snd); |
396 | | | 392 | |
397 | if (ifp->if_flags & IFF_UP) { | | 393 | if (ifp->if_flags & IFF_UP) { |
398 | if_down(ifp); | | 394 | if_down(ifp); |
399 | if (ifp->if_flags & IFF_RUNNING) { | | 395 | if (ifp->if_flags & IFF_RUNNING) { |
400 | /* find internet addresses and delete routes */ | | 396 | /* find internet addresses and delete routes */ |
401 | struct ifaddr *ifa; | | 397 | struct ifaddr *ifa; |
402 | IFADDR_READER_FOREACH(ifa, ifp) { | | 398 | IFADDR_READER_FOREACH(ifa, ifp) { |
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); |
430 | ifp->if_flags |= IFF_UP | IFF_RUNNING; | | 426 | ifp->if_flags |= IFF_UP | IFF_RUNNING; |
431 | | | 427 | |
432 | tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR); | | 428 | tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR); |
433 | | | 429 | |
434 | switch (ifa->ifa_addr->sa_family) { | | 430 | switch (ifa->ifa_addr->sa_family) { |
435 | #ifdef INET | | 431 | #ifdef INET |
436 | case AF_INET: { | | 432 | case AF_INET: { |
437 | struct sockaddr_in *sin; | | 433 | struct sockaddr_in *sin; |
438 | | | 434 | |
439 | sin = satosin(ifa->ifa_addr); | | 435 | sin = satosin(ifa->ifa_addr); |
440 | if (sin && sin->sin_addr.s_addr) | | 436 | if (sin && sin->sin_addr.s_addr) |
441 | tp->tun_flags |= TUN_IASET; | | 437 | tp->tun_flags |= TUN_IASET; |
442 | | | 438 | |
443 | if (ifp->if_flags & IFF_POINTOPOINT) { | | 439 | if (ifp->if_flags & IFF_POINTOPOINT) { |
444 | sin = satosin(ifa->ifa_dstaddr); | | 440 | sin = satosin(ifa->ifa_dstaddr); |
445 | if (sin && sin->sin_addr.s_addr) | | 441 | if (sin && sin->sin_addr.s_addr) |
446 | tp->tun_flags |= TUN_DSTADDR; | | 442 | tp->tun_flags |= TUN_DSTADDR; |
447 | } | | 443 | } |
448 | break; | | 444 | break; |
449 | } | | 445 | } |
450 | #endif | | 446 | #endif |
451 | #ifdef INET6 | | 447 | #ifdef INET6 |
452 | case AF_INET6: { | | 448 | case AF_INET6: { |
453 | struct sockaddr_in6 *sin; | | 449 | struct sockaddr_in6 *sin; |
454 | | | 450 | |
455 | sin = satosin6(ifa->ifa_addr); | | 451 | sin = satosin6(ifa->ifa_addr); |
456 | if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)) | | 452 | if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)) |
457 | tp->tun_flags |= TUN_IASET; | | 453 | tp->tun_flags |= TUN_IASET; |
458 | | | 454 | |
459 | if (ifp->if_flags & IFF_POINTOPOINT) { | | 455 | if (ifp->if_flags & IFF_POINTOPOINT) { |
460 | sin = satosin6(ifa->ifa_dstaddr); | | 456 | sin = satosin6(ifa->ifa_dstaddr); |
461 | if (sin && !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)) | | 457 | if (sin && !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)) |
462 | tp->tun_flags |= TUN_DSTADDR; | | 458 | tp->tun_flags |= TUN_DSTADDR; |
463 | } else | | 459 | } else |
464 | tp->tun_flags &= ~TUN_DSTADDR; | | 460 | tp->tun_flags &= ~TUN_DSTADDR; |
465 | break; | | 461 | break; |
466 | } | | 462 | } |
467 | #endif /* INET6 */ | | 463 | #endif /* INET6 */ |
468 | default: | | 464 | default: |
469 | break; | | 465 | break; |
470 | } | | 466 | } |
471 | mutex_exit(&tp->tun_lock); | | 467 | mutex_exit(&tp->tun_lock); |
472 | } | | 468 | } |
473 | | | 469 | |
474 | /* | | 470 | /* |
475 | * Process an ioctl request. | | 471 | * Process an ioctl request. |
476 | */ | | 472 | */ |
477 | static int | | 473 | static int |
478 | tun_ioctl(struct ifnet *ifp, u_long cmd, void *data) | | 474 | tun_ioctl(struct ifnet *ifp, u_long cmd, void *data) |
479 | { | | 475 | { |
480 | int error = 0, s; | | 476 | int error = 0, s; |
481 | struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc); | | 477 | struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc); |
482 | struct ifreq *ifr = (struct ifreq *)data; | | 478 | struct ifreq *ifr = (struct ifreq *)data; |
483 | struct ifaddr *ifa = (struct ifaddr *)data; | | 479 | struct ifaddr *ifa = (struct ifaddr *)data; |
484 | | | 480 | |
485 | s = splnet(); | | 481 | s = splnet(); |
486 | | | 482 | |
487 | switch (cmd) { | | 483 | switch (cmd) { |
488 | case SIOCINITIFADDR: | | 484 | case SIOCINITIFADDR: |
489 | tun_enable(tp, ifa); | | 485 | tun_enable(tp, ifa); |
490 | ifa->ifa_rtrequest = p2p_rtrequest; | | 486 | ifa->ifa_rtrequest = p2p_rtrequest; |
491 | TUNDEBUG("%s: address set\n", ifp->if_xname); | | 487 | TUNDEBUG("%s: address set\n", ifp->if_xname); |
492 | break; | | 488 | break; |
493 | case SIOCSIFBRDADDR: | | 489 | case SIOCSIFBRDADDR: |
494 | TUNDEBUG("%s: broadcast address set\n", ifp->if_xname); | | 490 | TUNDEBUG("%s: broadcast address set\n", ifp->if_xname); |
495 | break; | | 491 | break; |
496 | case SIOCSIFMTU: | | 492 | case SIOCSIFMTU: |
497 | if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) { | | 493 | if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) { |
498 | error = EINVAL; | | 494 | error = EINVAL; |
499 | break; | | 495 | break; |
500 | } | | 496 | } |
501 | TUNDEBUG("%s: interface mtu set\n", ifp->if_xname); | | 497 | TUNDEBUG("%s: interface mtu set\n", ifp->if_xname); |
502 | if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) | | 498 | if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) |
503 | error = 0; | | 499 | error = 0; |
504 | break; | | 500 | break; |
505 | case SIOCADDMULTI: | | 501 | case SIOCADDMULTI: |
506 | case SIOCDELMULTI: | | 502 | case SIOCDELMULTI: |
507 | if (ifr == NULL) { | | 503 | if (ifr == NULL) { |
508 | error = EAFNOSUPPORT; /* XXX */ | | 504 | error = EAFNOSUPPORT; /* XXX */ |
509 | break; | | 505 | break; |
510 | } | | 506 | } |
511 | switch (ifreq_getaddr(cmd, ifr)->sa_family) { | | 507 | switch (ifreq_getaddr(cmd, ifr)->sa_family) { |
512 | #ifdef INET | | 508 | #ifdef INET |
513 | case AF_INET: | | 509 | case AF_INET: |
514 | break; | | 510 | break; |
515 | #endif | | 511 | #endif |
516 | #ifdef INET6 | | 512 | #ifdef INET6 |
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) |
544 | int mlen; | | 540 | int mlen; |
545 | uint32_t *af; | | 541 | uint32_t *af; |
546 | #endif | | 542 | #endif |
547 | | | 543 | |
548 | s = splnet(); | | 544 | s = splnet(); |
549 | mutex_enter(&tp->tun_lock); | | 545 | mutex_enter(&tp->tun_lock); |
550 | TUNDEBUG ("%s: tun_output\n", ifp->if_xname); | | 546 | TUNDEBUG ("%s: tun_output\n", ifp->if_xname); |
551 | | | 547 | |
552 | if ((tp->tun_flags & TUN_READY) != TUN_READY) { | | 548 | if ((tp->tun_flags & TUN_READY) != TUN_READY) { |
553 | TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, | | 549 | TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, |
554 | tp->tun_flags); | | 550 | tp->tun_flags); |
555 | error = EHOSTDOWN; | | 551 | error = EHOSTDOWN; |
556 | goto out; | | 552 | goto out; |
557 | } | | 553 | } |
558 | | | 554 | |
559 | /* | | 555 | /* |
560 | * if the queueing discipline needs packet classification, | | 556 | * if the queueing discipline needs packet classification, |
561 | * do it before prepending link headers. | | 557 | * do it before prepending link headers. |
562 | */ | | 558 | */ |
563 | IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family); | | 559 | IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family); |
564 | | | 560 | |
565 | bpf_mtap_af(ifp, dst->sa_family, m0); | | 561 | bpf_mtap_af(ifp, dst->sa_family, m0); |
566 | | | 562 | |
567 | switch(dst->sa_family) { | | 563 | switch(dst->sa_family) { |
568 | #ifdef INET6 | | 564 | #ifdef INET6 |
569 | case AF_INET6: | | 565 | case AF_INET6: |
570 | #endif | | 566 | #endif |
571 | #ifdef INET | | 567 | #ifdef INET |
572 | case AF_INET: | | 568 | case AF_INET: |
573 | #endif | | 569 | #endif |
574 | #if defined(INET) || defined(INET6) | | 570 | #if defined(INET) || defined(INET6) |
575 | if (tp->tun_flags & TUN_PREPADDR) { | | 571 | if (tp->tun_flags & TUN_PREPADDR) { |
576 | /* Simple link-layer header */ | | 572 | /* Simple link-layer header */ |
577 | M_PREPEND(m0, dst->sa_len, M_DONTWAIT); | | 573 | M_PREPEND(m0, dst->sa_len, M_DONTWAIT); |
578 | if (m0 == NULL) { | | 574 | if (m0 == NULL) { |
579 | IF_DROP(&ifp->if_snd); | | 575 | IF_DROP(&ifp->if_snd); |
580 | error = ENOBUFS; | | 576 | error = ENOBUFS; |
581 | goto out; | | 577 | goto out; |
582 | } | | 578 | } |
583 | bcopy(dst, mtod(m0, char *), dst->sa_len); | | 579 | bcopy(dst, mtod(m0, char *), dst->sa_len); |
584 | } | | 580 | } |
585 | | | 581 | |
586 | if (tp->tun_flags & TUN_IFHEAD) { | | 582 | if (tp->tun_flags & TUN_IFHEAD) { |
587 | /* Prepend the address family */ | | 583 | /* Prepend the address family */ |
588 | M_PREPEND(m0, sizeof(*af), M_DONTWAIT); | | 584 | M_PREPEND(m0, sizeof(*af), M_DONTWAIT); |
589 | if (m0 == NULL) { | | 585 | if (m0 == NULL) { |
590 | IF_DROP(&ifp->if_snd); | | 586 | IF_DROP(&ifp->if_snd); |
591 | error = ENOBUFS; | | 587 | error = ENOBUFS; |
592 | goto out; | | 588 | goto out; |
593 | } | | 589 | } |
594 | af = mtod(m0,uint32_t *); | | 590 | af = mtod(m0,uint32_t *); |
595 | *af = htonl(dst->sa_family); | | 591 | *af = htonl(dst->sa_family); |
596 | } else { | | 592 | } else { |
597 | #ifdef INET | | 593 | #ifdef INET |
598 | if (dst->sa_family != AF_INET) | | 594 | if (dst->sa_family != AF_INET) |
599 | #endif | | 595 | #endif |
600 | { | | 596 | { |
601 | error = EAFNOSUPPORT; | | 597 | error = EAFNOSUPPORT; |
602 | goto out; | | 598 | goto out; |
603 | } | | 599 | } |
604 | } | | 600 | } |
605 | /* FALLTHROUGH */ | | 601 | /* FALLTHROUGH */ |
606 | case AF_UNSPEC: | | 602 | case AF_UNSPEC: |
607 | IFQ_ENQUEUE(&ifp->if_snd, m0, error); | | 603 | IFQ_ENQUEUE(&ifp->if_snd, m0, error); |
608 | if (error) { | | 604 | if (error) { |
609 | ifp->if_collisions++; | | 605 | ifp->if_collisions++; |
610 | error = EAFNOSUPPORT; | | 606 | error = EAFNOSUPPORT; |
611 | m0 = NULL; | | 607 | m0 = NULL; |
612 | goto out; | | 608 | goto out; |
613 | } | | 609 | } |
614 | mlen = m0->m_pkthdr.len; | | 610 | mlen = m0->m_pkthdr.len; |
615 | ifp->if_opackets++; | | 611 | ifp->if_opackets++; |
616 | ifp->if_obytes += mlen; | | 612 | ifp->if_obytes += mlen; |
617 | break; | | 613 | break; |
618 | #endif | | 614 | #endif |
619 | default: | | 615 | default: |
620 | error = EAFNOSUPPORT; | | 616 | error = EAFNOSUPPORT; |
621 | goto out; | | 617 | goto out; |
622 | } | | 618 | } |
623 | | | 619 | |
624 | if (tp->tun_flags & TUN_RWAIT) { | | 620 | if (tp->tun_flags & TUN_RWAIT) { |
625 | tp->tun_flags &= ~TUN_RWAIT; | | 621 | tp->tun_flags &= ~TUN_RWAIT; |
626 | wakeup((void *)tp); | | 622 | wakeup((void *)tp); |
627 | } | | 623 | } |
628 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) | | 624 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) |
629 | softint_schedule(tp->tun_isih); | | 625 | softint_schedule(tp->tun_isih); |
630 | | | 626 | |
631 | selnotify(&tp->tun_rsel, 0, 0); | | 627 | selnotify(&tp->tun_rsel, 0, 0); |
632 | out: | | 628 | out: |
633 | mutex_exit(&tp->tun_lock); | | 629 | mutex_exit(&tp->tun_lock); |
634 | splx(s); | | 630 | splx(s); |
635 | | | 631 | |
636 | if (error && m0) { | | 632 | if (error && m0) { |
637 | m_freem(m0); | | 633 | m_freem(m0); |
638 | } | | 634 | } |
639 | return 0; | | 635 | return 0; |
640 | } | | 636 | } |
641 | | | 637 | |
642 | static void | | 638 | static void |
643 | tun_i_softintr(void *cookie) | | 639 | tun_i_softintr(void *cookie) |
644 | { | | 640 | { |
645 | struct tun_softc *tp = cookie; | | 641 | struct tun_softc *tp = cookie; |
646 | | | 642 | |
647 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) | | 643 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) |
648 | fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM, | | 644 | fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM, |
649 | NULL); | | 645 | NULL); |
650 | } | | 646 | } |
651 | | | 647 | |
652 | static void | | 648 | static void |
653 | tun_o_softintr(void *cookie) | | 649 | tun_o_softintr(void *cookie) |
654 | { | | 650 | { |
655 | struct tun_softc *tp = cookie; | | 651 | struct tun_softc *tp = cookie; |
656 | | | 652 | |
657 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) | | 653 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) |
658 | fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM, | | 654 | fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM, |
659 | NULL); | | 655 | NULL); |
660 | } | | 656 | } |
661 | | | 657 | |
662 | /* | | 658 | /* |
663 | * the cdevsw interface is now pretty minimal. | | 659 | * the cdevsw interface is now pretty minimal. |
664 | */ | | 660 | */ |
665 | int | | 661 | int |
666 | tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) | | 662 | tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) |
667 | { | | 663 | { |
668 | struct tun_softc *tp; | | 664 | struct tun_softc *tp; |
669 | int s, error = 0; | | 665 | int s, error = 0; |
670 | | | 666 | |
671 | s = splnet(); | | 667 | s = splnet(); |
672 | tp = tun_find_unit(dev); | | 668 | tp = tun_find_unit(dev); |
673 | | | 669 | |
674 | /* interface was "destroyed" already */ | | 670 | /* interface was "destroyed" already */ |
675 | if (tp == NULL) { | | 671 | if (tp == NULL) { |
676 | error = ENXIO; | | 672 | error = ENXIO; |
677 | goto out_nolock; | | 673 | goto out_nolock; |
678 | } | | 674 | } |
679 | | | 675 | |
680 | switch (cmd) { | | 676 | switch (cmd) { |
681 | case TUNSDEBUG: | | 677 | case TUNSDEBUG: |
682 | tundebug = *(int *)data; | | 678 | tundebug = *(int *)data; |
683 | break; | | 679 | break; |
684 | | | 680 | |
685 | case TUNGDEBUG: | | 681 | case TUNGDEBUG: |
686 | *(int *)data = tundebug; | | 682 | *(int *)data = tundebug; |
687 | break; | | 683 | break; |
688 | | | 684 | |
689 | case TUNSIFMODE: | | 685 | case TUNSIFMODE: |
690 | switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) { | | 686 | switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) { |
691 | case IFF_POINTOPOINT: | | 687 | case IFF_POINTOPOINT: |
692 | case IFF_BROADCAST: | | 688 | case IFF_BROADCAST: |
693 | if (tp->tun_if.if_flags & IFF_UP) { | | 689 | if (tp->tun_if.if_flags & IFF_UP) { |
694 | error = EBUSY; | | 690 | error = EBUSY; |
695 | goto out; | | 691 | goto out; |
696 | } | | 692 | } |
697 | tp->tun_if.if_flags &= | | 693 | tp->tun_if.if_flags &= |
698 | ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); | | 694 | ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST); |
699 | tp->tun_if.if_flags |= *(int *)data; | | 695 | tp->tun_if.if_flags |= *(int *)data; |
700 | break; | | 696 | break; |
701 | default: | | 697 | default: |
702 | error = EINVAL; | | 698 | error = EINVAL; |
703 | goto out; | | 699 | goto out; |
704 | } | | 700 | } |
705 | break; | | 701 | break; |
706 | | | 702 | |
707 | case TUNSLMODE: | | 703 | case TUNSLMODE: |
708 | if (*(int *)data) { | | 704 | if (*(int *)data) { |
709 | tp->tun_flags |= TUN_PREPADDR; | | 705 | tp->tun_flags |= TUN_PREPADDR; |
710 | tp->tun_flags &= ~TUN_IFHEAD; | | 706 | tp->tun_flags &= ~TUN_IFHEAD; |
711 | } else | | 707 | } else |
712 | tp->tun_flags &= ~TUN_PREPADDR; | | 708 | tp->tun_flags &= ~TUN_PREPADDR; |
713 | break; | | 709 | break; |
714 | | | 710 | |
715 | case TUNSIFHEAD: | | 711 | case TUNSIFHEAD: |
716 | if (*(int *)data) { | | 712 | if (*(int *)data) { |
717 | tp->tun_flags |= TUN_IFHEAD; | | 713 | tp->tun_flags |= TUN_IFHEAD; |
718 | tp->tun_flags &= ~TUN_PREPADDR; | | 714 | tp->tun_flags &= ~TUN_PREPADDR; |
719 | } else | | 715 | } else |
720 | tp->tun_flags &= ~TUN_IFHEAD; | | 716 | tp->tun_flags &= ~TUN_IFHEAD; |
721 | break; | | 717 | break; |
722 | | | 718 | |
723 | case TUNGIFHEAD: | | 719 | case TUNGIFHEAD: |
724 | *(int *)data = (tp->tun_flags & TUN_IFHEAD); | | 720 | *(int *)data = (tp->tun_flags & TUN_IFHEAD); |
725 | break; | | 721 | break; |
726 | | | 722 | |
727 | case FIONBIO: | | 723 | case FIONBIO: |
728 | if (*(int *)data) | | 724 | if (*(int *)data) |
729 | tp->tun_flags |= TUN_NBIO; | | 725 | tp->tun_flags |= TUN_NBIO; |
730 | else | | 726 | else |
731 | tp->tun_flags &= ~TUN_NBIO; | | 727 | tp->tun_flags &= ~TUN_NBIO; |
732 | break; | | 728 | break; |
733 | | | 729 | |
734 | case FIOASYNC: | | 730 | case FIOASYNC: |
735 | if (*(int *)data) | | 731 | if (*(int *)data) |
736 | tp->tun_flags |= TUN_ASYNC; | | 732 | tp->tun_flags |= TUN_ASYNC; |
737 | else | | 733 | else |
738 | tp->tun_flags &= ~TUN_ASYNC; | | 734 | tp->tun_flags &= ~TUN_ASYNC; |
739 | break; | | 735 | break; |
740 | | | 736 | |
741 | case FIONREAD: | | 737 | case FIONREAD: |
742 | if (tp->tun_if.if_snd.ifq_head) | | 738 | if (tp->tun_if.if_snd.ifq_head) |
743 | *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len; | | 739 | *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len; |
744 | else | | 740 | else |
745 | *(int *)data = 0; | | 741 | *(int *)data = 0; |
746 | break; | | 742 | break; |
747 | | | 743 | |
748 | case TIOCSPGRP: | | 744 | case TIOCSPGRP: |
749 | case FIOSETOWN: | | 745 | case FIOSETOWN: |
750 | error = fsetown(&tp->tun_pgid, cmd, data); | | 746 | error = fsetown(&tp->tun_pgid, cmd, data); |
751 | break; | | 747 | break; |
752 | | | 748 | |
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; |
780 | | | 776 | |
781 | s = splnet(); | | 777 | s = splnet(); |
782 | tp = tun_find_unit(dev); | | 778 | tp = tun_find_unit(dev); |
783 | | | 779 | |
784 | /* interface was "destroyed" already */ | | 780 | /* interface was "destroyed" already */ |
785 | if (tp == NULL) { | | 781 | if (tp == NULL) { |
786 | error = ENXIO; | | 782 | error = ENXIO; |
787 | goto out_nolock; | | 783 | goto out_nolock; |
788 | } | | 784 | } |
789 | | | 785 | |
790 | index = tp->tun_if.if_index; | | 786 | index = tp->tun_if.if_index; |
791 | ifp = &tp->tun_if; | | 787 | ifp = &tp->tun_if; |
792 | | | 788 | |
793 | TUNDEBUG ("%s: read\n", ifp->if_xname); | | 789 | TUNDEBUG ("%s: read\n", ifp->if_xname); |
794 | if ((tp->tun_flags & TUN_READY) != TUN_READY) { | | 790 | if ((tp->tun_flags & TUN_READY) != TUN_READY) { |
795 | TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags); | | 791 | TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags); |
796 | error = EHOSTDOWN; | | 792 | error = EHOSTDOWN; |
797 | goto out; | | 793 | goto out; |
798 | } | | 794 | } |
799 | | | 795 | |
800 | tp->tun_flags &= ~TUN_RWAIT; | | 796 | tp->tun_flags &= ~TUN_RWAIT; |
801 | | | 797 | |
802 | do { | | 798 | do { |
803 | IFQ_DEQUEUE(&ifp->if_snd, m0); | | 799 | IFQ_DEQUEUE(&ifp->if_snd, m0); |
804 | if (m0 == 0) { | | 800 | if (m0 == 0) { |
805 | if (tp->tun_flags & TUN_NBIO) { | | 801 | if (tp->tun_flags & TUN_NBIO) { |
806 | error = EWOULDBLOCK; | | 802 | error = EWOULDBLOCK; |
807 | goto out; | | 803 | goto out; |
808 | } | | 804 | } |
809 | tp->tun_flags |= TUN_RWAIT; | | 805 | tp->tun_flags |= TUN_RWAIT; |
810 | if (mtsleep((void *)tp, PZERO|PCATCH|PNORELOCK, | | 806 | if (mtsleep((void *)tp, PZERO|PCATCH|PNORELOCK, |
811 | "tunread", 0, &tp->tun_lock) != 0) { | | 807 | "tunread", 0, &tp->tun_lock) != 0) { |
812 | error = EINTR; | | 808 | error = EINTR; |
813 | goto out_nolock; | | 809 | goto out_nolock; |
814 | } else { | | 810 | } else { |
815 | /* | | 811 | /* |
816 | * Maybe the interface was destroyed while | | 812 | * Maybe the interface was destroyed while |
817 | * we were sleeping, so let's ensure that | | 813 | * we were sleeping, so let's ensure that |
818 | * we're looking at the same (valid) tun | | 814 | * we're looking at the same (valid) tun |
819 | * interface before looping. | | 815 | * interface before looping. |
820 | */ | | 816 | */ |
821 | tp = tun_find_unit(dev); | | 817 | tp = tun_find_unit(dev); |
822 | if (tp == NULL) { | | 818 | if (tp == NULL) { |
823 | error = ENXIO; | | 819 | error = ENXIO; |
824 | goto out_nolock; | | 820 | goto out_nolock; |
825 | } | | 821 | } |
826 | if (tp->tun_if.if_index != index) { | | 822 | if (tp->tun_if.if_index != index) { |
827 | error = ENXIO; | | 823 | error = ENXIO; |
828 | goto out; | | 824 | goto out; |
829 | } | | 825 | } |
830 | } | | 826 | } |
831 | } | | 827 | } |
832 | } while (m0 == 0); | | 828 | } while (m0 == 0); |
833 | | | 829 | |
834 | mutex_exit(&tp->tun_lock); | | 830 | mutex_exit(&tp->tun_lock); |
835 | splx(s); | | 831 | splx(s); |
836 | | | 832 | |
837 | /* Copy the mbuf chain */ | | 833 | /* Copy the mbuf chain */ |
838 | while (m0 && uio->uio_resid > 0 && error == 0) { | | 834 | while (m0 && uio->uio_resid > 0 && error == 0) { |
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; |
872 | int error = 0, s, tlen, mlen; | | 868 | int error = 0, s, tlen, mlen; |
873 | uint32_t family; | | 869 | uint32_t family; |
874 | | | 870 | |
875 | s = splnet(); | | 871 | s = splnet(); |
876 | tp = tun_find_unit(dev); | | 872 | tp = tun_find_unit(dev); |
877 | | | 873 | |
878 | /* interface was "destroyed" already */ | | 874 | /* interface was "destroyed" already */ |
879 | if (tp == NULL) { | | 875 | if (tp == NULL) { |
880 | error = ENXIO; | | 876 | error = ENXIO; |
881 | goto out_nolock; | | 877 | goto out_nolock; |
882 | } | | 878 | } |
883 | | | 879 | |
884 | /* Unlock until we've got the data */ | | 880 | /* Unlock until we've got the data */ |
885 | mutex_exit(&tp->tun_lock); | | 881 | mutex_exit(&tp->tun_lock); |
886 | splx(s); | | 882 | splx(s); |
887 | | | 883 | |
888 | ifp = &tp->tun_if; | | 884 | ifp = &tp->tun_if; |
889 | | | 885 | |
890 | TUNDEBUG("%s: tunwrite\n", ifp->if_xname); | | 886 | TUNDEBUG("%s: tunwrite\n", ifp->if_xname); |
891 | | | 887 | |
892 | if (tp->tun_flags & TUN_PREPADDR) { | | 888 | if (tp->tun_flags & TUN_PREPADDR) { |
893 | if (uio->uio_resid < sizeof(dst)) { | | 889 | if (uio->uio_resid < sizeof(dst)) { |
894 | error = EIO; | | 890 | error = EIO; |
895 | goto out0; | | 891 | goto out0; |
896 | } | | 892 | } |
897 | error = uiomove((void *)&dst, sizeof(dst), uio); | | 893 | error = uiomove((void *)&dst, sizeof(dst), uio); |
898 | if (dst.sa_len > sizeof(dst)) { | | 894 | if (dst.sa_len > sizeof(dst)) { |
899 | /* Duh.. */ | | 895 | /* Duh.. */ |
900 | char discard; | | 896 | char discard; |
901 | int n = dst.sa_len - sizeof(dst); | | 897 | int n = dst.sa_len - sizeof(dst); |
902 | while (n--) | | 898 | while (n--) |
903 | if ((error = uiomove(&discard, 1, uio)) != 0) { | | 899 | if ((error = uiomove(&discard, 1, uio)) != 0) { |
904 | goto out0; | | 900 | goto out0; |
905 | } | | 901 | } |
906 | } | | 902 | } |
907 | } else if (tp->tun_flags & TUN_IFHEAD) { | | 903 | } else if (tp->tun_flags & TUN_IFHEAD) { |
908 | if (uio->uio_resid < sizeof(family)){ | | 904 | if (uio->uio_resid < sizeof(family)){ |
909 | error = EIO; | | 905 | error = EIO; |
910 | goto out0; | | 906 | goto out0; |
911 | } | | 907 | } |
912 | error = uiomove((void *)&family, sizeof(family), uio); | | 908 | error = uiomove((void *)&family, sizeof(family), uio); |
913 | dst.sa_family = ntohl(family); | | 909 | dst.sa_family = ntohl(family); |
914 | } else { | | 910 | } else { |
915 | #ifdef INET | | 911 | #ifdef INET |
916 | dst.sa_family = AF_INET; | | 912 | dst.sa_family = AF_INET; |
917 | #endif | | 913 | #endif |
918 | } | | 914 | } |
919 | | | 915 | |
920 | if (uio->uio_resid > TUNMTU) { | | 916 | if (uio->uio_resid > TUNMTU) { |
921 | TUNDEBUG("%s: len=%lu!\n", ifp->if_xname, | | 917 | TUNDEBUG("%s: len=%lu!\n", ifp->if_xname, |
922 | (unsigned long)uio->uio_resid); | | 918 | (unsigned long)uio->uio_resid); |
923 | error = EIO; | | 919 | error = EIO; |
924 | goto out0; | | 920 | goto out0; |
925 | } | | 921 | } |
926 | | | 922 | |
927 | switch (dst.sa_family) { | | 923 | switch (dst.sa_family) { |
928 | #ifdef INET | | 924 | #ifdef INET |
929 | case AF_INET: | | 925 | case AF_INET: |
930 | pktq = ip_pktq; | | 926 | pktq = ip_pktq; |
931 | break; | | 927 | break; |
932 | #endif | | 928 | #endif |
933 | #ifdef INET6 | | 929 | #ifdef INET6 |
934 | case AF_INET6: | | 930 | case AF_INET6: |
935 | pktq = ip6_pktq; | | 931 | pktq = ip6_pktq; |
936 | break; | | 932 | break; |
937 | #endif | | 933 | #endif |
938 | default: | | 934 | default: |
939 | error = EAFNOSUPPORT; | | 935 | error = EAFNOSUPPORT; |
940 | goto out0; | | 936 | goto out0; |
941 | } | | 937 | } |
942 | | | 938 | |
943 | tlen = uio->uio_resid; | | 939 | tlen = uio->uio_resid; |
944 | | | 940 | |
945 | /* get a header mbuf */ | | 941 | /* get a header mbuf */ |
946 | MGETHDR(m, M_DONTWAIT, MT_DATA); | | 942 | MGETHDR(m, M_DONTWAIT, MT_DATA); |
947 | if (m == NULL) { | | 943 | if (m == NULL) { |
948 | error = ENOBUFS; | | 944 | error = ENOBUFS; |
949 | goto out0; | | 945 | goto out0; |
950 | } | | 946 | } |
951 | mlen = MHLEN; | | 947 | mlen = MHLEN; |
952 | | | 948 | |
953 | top = NULL; | | 949 | top = NULL; |
954 | mp = ⊤ | | 950 | mp = ⊤ |
955 | while (error == 0 && uio->uio_resid > 0) { | | 951 | while (error == 0 && uio->uio_resid > 0) { |
956 | m->m_len = min(mlen, uio->uio_resid); | | 952 | m->m_len = min(mlen, uio->uio_resid); |
957 | error = uiomove(mtod(m, void *), m->m_len, uio); | | 953 | error = uiomove(mtod(m, void *), m->m_len, uio); |
958 | *mp = m; | | 954 | *mp = m; |
959 | mp = &m->m_next; | | 955 | mp = &m->m_next; |
960 | if (error == 0 && uio->uio_resid > 0) { | | 956 | if (error == 0 && uio->uio_resid > 0) { |
961 | MGET(m, M_DONTWAIT, MT_DATA); | | 957 | MGET(m, M_DONTWAIT, MT_DATA); |
962 | if (m == NULL) { | | 958 | if (m == NULL) { |
963 | error = ENOBUFS; | | 959 | error = ENOBUFS; |
964 | break; | | 960 | break; |
965 | } | | 961 | } |
966 | mlen = MLEN; | | 962 | mlen = MLEN; |
967 | } | | 963 | } |
968 | } | | 964 | } |
969 | if (error) { | | 965 | if (error) { |
970 | if (top != NULL) | | 966 | if (top != NULL) |
971 | m_freem (top); | | 967 | m_freem (top); |
972 | ifp->if_ierrors++; | | 968 | ifp->if_ierrors++; |
973 | goto out0; | | 969 | goto out0; |
974 | } | | 970 | } |
975 | | | 971 | |
976 | top->m_pkthdr.len = tlen; | | 972 | top->m_pkthdr.len = tlen; |
977 | m_set_rcvif(top, ifp); | | 973 | m_set_rcvif(top, ifp); |
978 | | | 974 | |
979 | bpf_mtap_af(ifp, dst.sa_family, top); | | 975 | bpf_mtap_af(ifp, dst.sa_family, top); |
980 | | | 976 | |
981 | s = splnet(); | | 977 | s = splnet(); |
982 | mutex_enter(&tp->tun_lock); | | 978 | mutex_enter(&tp->tun_lock); |
983 | if ((tp->tun_flags & TUN_INITED) == 0) { | | 979 | if ((tp->tun_flags & TUN_INITED) == 0) { |
984 | /* Interface was destroyed */ | | 980 | /* Interface was destroyed */ |
985 | error = ENXIO; | | 981 | error = ENXIO; |
986 | goto out; | | 982 | goto out; |
987 | } | | 983 | } |
988 | if (__predict_false(!pktq_enqueue(pktq, top, 0))) { | | 984 | if (__predict_false(!pktq_enqueue(pktq, top, 0))) { |
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) |
1016 | { | | 1012 | { |
1017 | struct tun_softc *tp = ifp->if_softc; | | 1013 | struct tun_softc *tp = ifp->if_softc; |
1018 | | | 1014 | |
1019 | if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd)) | | 1015 | if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd)) |
1020 | return; | | 1016 | return; |
1021 | | | 1017 | |
1022 | mutex_enter(&tp->tun_lock); | | 1018 | mutex_enter(&tp->tun_lock); |
1023 | if (!IF_IS_EMPTY(&ifp->if_snd)) { | | 1019 | if (!IF_IS_EMPTY(&ifp->if_snd)) { |
1024 | if (tp->tun_flags & TUN_RWAIT) { | | 1020 | if (tp->tun_flags & TUN_RWAIT) { |
1025 | tp->tun_flags &= ~TUN_RWAIT; | | 1021 | tp->tun_flags &= ~TUN_RWAIT; |
1026 | wakeup((void *)tp); | | 1022 | wakeup((void *)tp); |
1027 | } | | 1023 | } |
1028 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) | | 1024 | if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid) |
1029 | softint_schedule(tp->tun_osih); | | 1025 | softint_schedule(tp->tun_osih); |
1030 | | | 1026 | |
1031 | selnotify(&tp->tun_rsel, 0, 0); | | 1027 | selnotify(&tp->tun_rsel, 0, 0); |
1032 | } | | 1028 | } |
1033 | mutex_exit(&tp->tun_lock); | | 1029 | mutex_exit(&tp->tun_lock); |
1034 | } | | 1030 | } |
1035 | #endif /* ALTQ */ | | 1031 | #endif /* ALTQ */ |
1036 | /* | | 1032 | /* |
1037 | * tunpoll - the poll interface, this is only useful on reads | | 1033 | * tunpoll - the poll interface, this is only useful on reads |
1038 | * really. The write detect always returns true, write never blocks | | 1034 | * really. The write detect always returns true, write never blocks |
1039 | * anyway, it either accepts the packet or drops it. | | 1035 | * anyway, it either accepts the packet or drops it. |
1040 | */ | | 1036 | */ |
1041 | int | | 1037 | int |
1042 | tunpoll(dev_t dev, int events, struct lwp *l) | | 1038 | tunpoll(dev_t dev, int events, struct lwp *l) |
1043 | { | | 1039 | { |
1044 | struct tun_softc *tp; | | 1040 | struct tun_softc *tp; |
1045 | struct ifnet *ifp; | | 1041 | struct ifnet *ifp; |
1046 | int s, revents = 0; | | 1042 | int s, revents = 0; |
1047 | | | 1043 | |
1048 | s = splnet(); | | 1044 | s = splnet(); |
1049 | tp = tun_find_unit(dev); | | 1045 | tp = tun_find_unit(dev); |
1050 | | | 1046 | |
1051 | /* interface was "destroyed" already */ | | 1047 | /* interface was "destroyed" already */ |
1052 | if (tp == NULL) | | 1048 | if (tp == NULL) |
1053 | goto out_nolock; | | 1049 | goto out_nolock; |
1054 | | | 1050 | |
1055 | ifp = &tp->tun_if; | | 1051 | ifp = &tp->tun_if; |
1056 | | | 1052 | |
1057 | TUNDEBUG("%s: tunpoll\n", ifp->if_xname); | | 1053 | TUNDEBUG("%s: tunpoll\n", ifp->if_xname); |
1058 | | | 1054 | |
1059 | if (events & (POLLIN | POLLRDNORM)) { | | 1055 | if (events & (POLLIN | POLLRDNORM)) { |
1060 | if (!IFQ_IS_EMPTY(&ifp->if_snd)) { | | 1056 | if (!IFQ_IS_EMPTY(&ifp->if_snd)) { |
1061 | TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname, | | 1057 | TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname, |
1062 | ifp->if_snd.ifq_len); | | 1058 | ifp->if_snd.ifq_len); |
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; |
1123 | int rv = 0, s; | | 1119 | int rv = 0, s; |
1124 | | | 1120 | |
1125 | s = splnet(); | | 1121 | s = splnet(); |
1126 | tp = tun_find_unit(dev); | | 1122 | tp = tun_find_unit(dev); |
1127 | if (tp == NULL) | | 1123 | if (tp == NULL) |
1128 | goto out_nolock; | | 1124 | goto out_nolock; |
1129 | | | 1125 | |
1130 | switch (kn->kn_filter) { | | 1126 | switch (kn->kn_filter) { |
1131 | case EVFILT_READ: | | 1127 | case EVFILT_READ: |
1132 | klist = &tp->tun_rsel.sel_klist; | | 1128 | klist = &tp->tun_rsel.sel_klist; |
1133 | kn->kn_fop = &tunread_filtops; | | 1129 | kn->kn_fop = &tunread_filtops; |
1134 | break; | | 1130 | break; |
1135 | | | 1131 | |
1136 | case EVFILT_WRITE: | | 1132 | case EVFILT_WRITE: |
1137 | klist = &tp->tun_rsel.sel_klist; | | 1133 | klist = &tp->tun_rsel.sel_klist; |
1138 | kn->kn_fop = &tun_seltrue_filtops; | | 1134 | kn->kn_fop = &tun_seltrue_filtops; |
1139 | break; | | 1135 | break; |
1140 | | | 1136 | |
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, "") |