Diff markup
1 /* 1 /*
2 * TUN - Universal TUN/TAP device driver. 2 * TUN - Universal TUN/TAP device driver.
3 * Copyright (C) 1999-2002 Maxim Krasnyansky 3 * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 * 4 *
5 * This program is free software; you can red 5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Publ 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either versi 7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope th 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULA 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more detail 13 * GNU General Public License for more details.
14 * 14 *
15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk 15 * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
16 */ 16 */
17 17
18 /* 18 /*
19 * Changes: 19 * Changes:
20 * 20 *
21 * Brian Braunstein <linuxkernel@bristyle.com <<
22 * Fixed hw address handling. Now net_devi <<
23 * with tun.dev_addr when the address is se <<
24 * <<
25 * Mike Kershaw <dragorn@kismetwireless.net> <<
26 * Add TUNSETLINK ioctl to set the link enc <<
27 * <<
28 * Mark Smith <markzzzsmith@yahoo.com.au> 21 * Mark Smith <markzzzsmith@yahoo.com.au>
29 * Use random_ether_addr() for tap MAC addre 22 * Use random_ether_addr() for tap MAC address.
30 * 23 *
31 * Harald Roelle <harald.roelle@ifi.lmu.de> 24 * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
32 * Fixes in packet dropping, queue length s 25 * Fixes in packet dropping, queue length setting and queue wakeup.
33 * Increased default tx queue length. 26 * Increased default tx queue length.
34 * Added ethtool API. 27 * Added ethtool API.
35 * Minor cleanups 28 * Minor cleanups
36 * 29 *
37 * Daniel Podlejski <underley@underley.eu.org 30 * Daniel Podlejski <underley@underley.eu.org>
38 * Modifications for 2.3.99-pre5 kernel. 31 * Modifications for 2.3.99-pre5 kernel.
39 */ 32 */
40 33
41 #define DRV_NAME "tun" 34 #define DRV_NAME "tun"
42 #define DRV_VERSION "1.6" 35 #define DRV_VERSION "1.6"
43 #define DRV_DESCRIPTION "Universal TUN/TAP dev 36 #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
44 #define DRV_COPYRIGHT "(C) 1999-2004 Max Kra 37 #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
45 38
>> 39 #include <linux/config.h>
46 #include <linux/module.h> 40 #include <linux/module.h>
47 #include <linux/errno.h> 41 #include <linux/errno.h>
48 #include <linux/kernel.h> 42 #include <linux/kernel.h>
49 #include <linux/major.h> 43 #include <linux/major.h>
50 #include <linux/slab.h> 44 #include <linux/slab.h>
51 #include <linux/poll.h> 45 #include <linux/poll.h>
52 #include <linux/fcntl.h> 46 #include <linux/fcntl.h>
53 #include <linux/init.h> 47 #include <linux/init.h>
54 #include <linux/skbuff.h> 48 #include <linux/skbuff.h>
55 #include <linux/netdevice.h> 49 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h> 50 #include <linux/etherdevice.h>
57 #include <linux/miscdevice.h> 51 #include <linux/miscdevice.h>
58 #include <linux/ethtool.h> 52 #include <linux/ethtool.h>
59 #include <linux/rtnetlink.h> 53 #include <linux/rtnetlink.h>
60 #include <linux/if.h> 54 #include <linux/if.h>
61 #include <linux/if_arp.h> 55 #include <linux/if_arp.h>
62 #include <linux/if_ether.h> 56 #include <linux/if_ether.h>
63 #include <linux/if_tun.h> 57 #include <linux/if_tun.h>
64 #include <linux/crc32.h> 58 #include <linux/crc32.h>
65 #include <net/net_namespace.h> <<
66 59
67 #include <asm/system.h> 60 #include <asm/system.h>
68 #include <asm/uaccess.h> 61 #include <asm/uaccess.h>
69 62
70 /* Uncomment to enable debugging */ <<
71 /* #define TUN_DEBUG 1 */ <<
72 <<
73 #ifdef TUN_DEBUG 63 #ifdef TUN_DEBUG
74 static int debug; 64 static int debug;
75 <<
76 #define DBG if(tun->debug)printk <<
77 #define DBG1 if(debug==2)printk <<
78 #else <<
79 #define DBG( a... ) <<
80 #define DBG1( a... ) <<
81 #endif 65 #endif
82 66
83 struct tun_struct { <<
84 struct list_head list; <<
85 unsigned long flags; <<
86 int attached; <<
87 uid_t owner; <<
88 gid_t group; <<
89 <<
90 wait_queue_head_t read_wait; <<
91 struct sk_buff_head readq; <<
92 <<
93 struct net_device *dev; <<
94 <<
95 struct fasync_struct *fasync; <<
96 <<
97 unsigned long if_flags; <<
98 u8 dev_addr[ETH_ALEN]; <<
99 u32 chr_filter[2]; <<
100 u32 net_filter[2]; <<
101 <<
102 #ifdef TUN_DEBUG <<
103 int debug; <<
104 #endif <<
105 }; <<
106 <<
107 /* Network device part of the driver */ 67 /* Network device part of the driver */
108 68
109 static LIST_HEAD(tun_dev_list); 69 static LIST_HEAD(tun_dev_list);
110 static const struct ethtool_ops tun_ethtool_op !! 70 static struct ethtool_ops tun_ethtool_ops;
111 71
112 /* Net device open. */ 72 /* Net device open. */
113 static int tun_net_open(struct net_device *dev 73 static int tun_net_open(struct net_device *dev)
114 { 74 {
115 netif_start_queue(dev); 75 netif_start_queue(dev);
116 return 0; 76 return 0;
117 } 77 }
118 78
119 /* Net device close. */ 79 /* Net device close. */
120 static int tun_net_close(struct net_device *de 80 static int tun_net_close(struct net_device *dev)
121 { 81 {
122 netif_stop_queue(dev); 82 netif_stop_queue(dev);
123 return 0; 83 return 0;
124 } 84 }
125 85
126 /* Net device start xmit */ 86 /* Net device start xmit */
127 static int tun_net_xmit(struct sk_buff *skb, s 87 static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
128 { 88 {
129 struct tun_struct *tun = netdev_priv(d 89 struct tun_struct *tun = netdev_priv(dev);
130 90
131 DBG(KERN_INFO "%s: tun_net_xmit %d\n", 91 DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len);
132 92
133 /* Drop packet if interface is not att 93 /* Drop packet if interface is not attached */
134 if (!tun->attached) 94 if (!tun->attached)
135 goto drop; 95 goto drop;
136 96
137 /* Packet dropping */ 97 /* Packet dropping */
138 if (skb_queue_len(&tun->readq) >= dev- 98 if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) {
139 if (!(tun->flags & TUN_ONE_QUE 99 if (!(tun->flags & TUN_ONE_QUEUE)) {
140 /* Normal queueing mod 100 /* Normal queueing mode. */
141 /* Packet scheduler ha 101 /* Packet scheduler handles dropping of further packets. */
142 netif_stop_queue(dev); 102 netif_stop_queue(dev);
143 103
144 /* We won't see all dr 104 /* We won't see all dropped packets individually, so overrun
145 * error is more appro 105 * error is more appropriate. */
146 dev->stats.tx_fifo_err !! 106 tun->stats.tx_fifo_errors++;
147 } else { 107 } else {
148 /* Single queue mode. 108 /* Single queue mode.
149 * Driver handles drop 109 * Driver handles dropping of all packets itself. */
150 goto drop; 110 goto drop;
151 } 111 }
152 } 112 }
153 113
154 /* Queue packet */ 114 /* Queue packet */
155 skb_queue_tail(&tun->readq, skb); 115 skb_queue_tail(&tun->readq, skb);
156 dev->trans_start = jiffies; 116 dev->trans_start = jiffies;
157 117
158 /* Notify and wake up reader process * 118 /* Notify and wake up reader process */
159 if (tun->flags & TUN_FASYNC) 119 if (tun->flags & TUN_FASYNC)
160 kill_fasync(&tun->fasync, SIGI 120 kill_fasync(&tun->fasync, SIGIO, POLL_IN);
161 wake_up_interruptible(&tun->read_wait) 121 wake_up_interruptible(&tun->read_wait);
162 return 0; 122 return 0;
163 123
164 drop: 124 drop:
165 dev->stats.tx_dropped++; !! 125 tun->stats.tx_dropped++;
166 kfree_skb(skb); 126 kfree_skb(skb);
167 return 0; 127 return 0;
168 } 128 }
169 129
170 /** Add the specified Ethernet address to this 130 /** Add the specified Ethernet address to this multicast filter. */
171 static void 131 static void
172 add_multi(u32* filter, const u8* addr) 132 add_multi(u32* filter, const u8* addr)
173 { 133 {
174 int bit_nr = ether_crc(ETH_ALEN, addr) 134 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
175 filter[bit_nr >> 5] |= 1 << (bit_nr & 135 filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
176 } 136 }
177 137
178 /** Remove the specified Ethernet addres from 138 /** Remove the specified Ethernet addres from this multicast filter. */
179 static void 139 static void
180 del_multi(u32* filter, const u8* addr) 140 del_multi(u32* filter, const u8* addr)
181 { 141 {
182 int bit_nr = ether_crc(ETH_ALEN, addr) 142 int bit_nr = ether_crc(ETH_ALEN, addr) >> 26;
183 filter[bit_nr >> 5] &= ~(1 << (bit_nr 143 filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31));
184 } 144 }
185 145
186 /** Update the list of multicast groups to whi 146 /** Update the list of multicast groups to which the network device belongs.
187 * This list is used to filter packets being s 147 * This list is used to filter packets being sent from the character device to
188 * the network device. */ 148 * the network device. */
189 static void 149 static void
190 tun_net_mclist(struct net_device *dev) 150 tun_net_mclist(struct net_device *dev)
191 { 151 {
192 struct tun_struct *tun = netdev_priv(d 152 struct tun_struct *tun = netdev_priv(dev);
193 const struct dev_mc_list *mclist; 153 const struct dev_mc_list *mclist;
194 int i; 154 int i;
195 DECLARE_MAC_BUF(mac); <<
196 DBG(KERN_DEBUG "%s: tun_net_mclist: mc 155 DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n",
197 dev->name, dev->mc_cou 156 dev->name, dev->mc_count);
198 memset(tun->chr_filter, 0, sizeof tun- 157 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
199 for (i = 0, mclist = dev->mc_list; i < 158 for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL;
200 i++, mclist = mclist-> 159 i++, mclist = mclist->next) {
201 add_multi(tun->net_filter, mcl 160 add_multi(tun->net_filter, mclist->dmi_addr);
202 DBG(KERN_DEBUG "%s: tun_net_mc !! 161 DBG(KERN_DEBUG "%s: tun_net_mclist: %x:%x:%x:%x:%x:%x\n",
203 dev->name, print_mac(mac, !! 162 dev->name,
>> 163 mclist->dmi_addr[0], mclist->dmi_addr[1], mclist->dmi_addr[2],
>> 164 mclist->dmi_addr[3], mclist->dmi_addr[4], mclist->dmi_addr[5]);
204 } 165 }
205 } 166 }
206 167
207 #define MIN_MTU 68 !! 168 static struct net_device_stats *tun_net_stats(struct net_device *dev)
208 #define MAX_MTU 65535 <<
209 <<
210 static int <<
211 tun_net_change_mtu(struct net_device *dev, int <<
212 { 169 {
213 if (new_mtu < MIN_MTU || new_mtu + dev !! 170 struct tun_struct *tun = netdev_priv(dev);
214 return -EINVAL; !! 171 return &tun->stats;
215 dev->mtu = new_mtu; <<
216 return 0; <<
217 } 172 }
218 173
219 /* Initialize net device. */ 174 /* Initialize net device. */
220 static void tun_net_init(struct net_device *de 175 static void tun_net_init(struct net_device *dev)
221 { 176 {
222 struct tun_struct *tun = netdev_priv(d 177 struct tun_struct *tun = netdev_priv(dev);
223 !! 178
224 switch (tun->flags & TUN_TYPE_MASK) { 179 switch (tun->flags & TUN_TYPE_MASK) {
225 case TUN_TUN_DEV: 180 case TUN_TUN_DEV:
226 /* Point-to-Point TUN Device * 181 /* Point-to-Point TUN Device */
227 dev->hard_header_len = 0; 182 dev->hard_header_len = 0;
228 dev->addr_len = 0; 183 dev->addr_len = 0;
229 dev->mtu = 1500; 184 dev->mtu = 1500;
230 dev->change_mtu = tun_net_chan <<
231 185
232 /* Zero header length */ 186 /* Zero header length */
233 dev->type = ARPHRD_NONE; !! 187 dev->type = ARPHRD_NONE;
234 dev->flags = IFF_POINTOPOINT | 188 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
235 dev->tx_queue_len = TUN_READQ_ 189 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
236 break; 190 break;
237 191
238 case TUN_TAP_DEV: 192 case TUN_TAP_DEV:
239 /* Ethernet TAP Device */ 193 /* Ethernet TAP Device */
240 dev->set_multicast_list = tun_ 194 dev->set_multicast_list = tun_net_mclist;
241 195
242 ether_setup(dev); 196 ether_setup(dev);
243 dev->change_mtu = tun_net_chan !! 197 random_ether_addr(dev->dev_addr);
244 <<
245 /* random address already crea <<
246 memcpy(dev->dev_addr, tun->dev <<
247 <<
248 dev->tx_queue_len = TUN_READQ_ 198 dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
249 break; 199 break;
250 } 200 }
251 } 201 }
252 202
253 /* Character device part */ 203 /* Character device part */
254 204
255 /* Poll */ 205 /* Poll */
256 static unsigned int tun_chr_poll(struct file * 206 static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
257 { !! 207 {
258 struct tun_struct *tun = file->private 208 struct tun_struct *tun = file->private_data;
259 unsigned int mask = POLLOUT | POLLWRNO 209 unsigned int mask = POLLOUT | POLLWRNORM;
260 210
261 if (!tun) 211 if (!tun)
262 return -EBADFD; 212 return -EBADFD;
263 213
264 DBG(KERN_INFO "%s: tun_chr_poll\n", tu 214 DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);
265 215
266 poll_wait(file, &tun->read_wait, wait) 216 poll_wait(file, &tun->read_wait, wait);
267 !! 217
268 if (!skb_queue_empty(&tun->readq)) !! 218 if (skb_queue_len(&tun->readq))
269 mask |= POLLIN | POLLRDNORM; 219 mask |= POLLIN | POLLRDNORM;
270 220
271 return mask; 221 return mask;
272 } 222 }
273 223
274 /* Get packet from user space buffer */ 224 /* Get packet from user space buffer */
275 static __inline__ ssize_t tun_get_user(struct 225 static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count)
276 { 226 {
277 struct tun_pi pi = { 0, __constant_hto 227 struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) };
278 struct sk_buff *skb; 228 struct sk_buff *skb;
279 size_t len = count, align = 0; !! 229 size_t len = count;
280 230
281 if (!(tun->flags & TUN_NO_PI)) { 231 if (!(tun->flags & TUN_NO_PI)) {
282 if ((len -= sizeof(pi)) > coun 232 if ((len -= sizeof(pi)) > count)
283 return -EINVAL; 233 return -EINVAL;
284 234
285 if(memcpy_fromiovec((void *)&p 235 if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi)))
286 return -EFAULT; 236 return -EFAULT;
287 } 237 }
288 !! 238
289 if ((tun->flags & TUN_TYPE_MASK) == TU !! 239 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
290 align = NET_IP_ALIGN; !! 240 tun->stats.rx_dropped++;
291 if (unlikely(len < ETH_HLEN)) <<
292 return -EINVAL; <<
293 } <<
294 <<
295 if (!(skb = alloc_skb(len + align, GFP <<
296 tun->dev->stats.rx_dropped++; <<
297 return -ENOMEM; 241 return -ENOMEM;
298 } 242 }
299 243
300 if (align) !! 244 skb_reserve(skb, 2);
301 skb_reserve(skb, align); !! 245 if (memcpy_fromiovec(skb_put(skb, len), iv, len))
302 if (memcpy_fromiovec(skb_put(skb, len) <<
303 tun->dev->stats.rx_dropped++; <<
304 kfree_skb(skb); <<
305 return -EFAULT; 246 return -EFAULT;
306 } <<
307 247
>> 248 skb->dev = tun->dev;
308 switch (tun->flags & TUN_TYPE_MASK) { 249 switch (tun->flags & TUN_TYPE_MASK) {
309 case TUN_TUN_DEV: 250 case TUN_TUN_DEV:
310 skb_reset_mac_header(skb); !! 251 skb->mac.raw = skb->data;
311 skb->protocol = pi.proto; 252 skb->protocol = pi.proto;
312 skb->dev = tun->dev; <<
313 break; 253 break;
314 case TUN_TAP_DEV: 254 case TUN_TAP_DEV:
315 skb->protocol = eth_type_trans 255 skb->protocol = eth_type_trans(skb, tun->dev);
316 break; 256 break;
317 }; 257 };
318 258
319 if (tun->flags & TUN_NOCHECKSUM) 259 if (tun->flags & TUN_NOCHECKSUM)
320 skb->ip_summed = CHECKSUM_UNNE 260 skb->ip_summed = CHECKSUM_UNNECESSARY;
321 !! 261
322 netif_rx_ni(skb); 262 netif_rx_ni(skb);
323 tun->dev->last_rx = jiffies; 263 tun->dev->last_rx = jiffies;
324 !! 264
325 tun->dev->stats.rx_packets++; !! 265 tun->stats.rx_packets++;
326 tun->dev->stats.rx_bytes += len; !! 266 tun->stats.rx_bytes += len;
327 267
328 return count; 268 return count;
>> 269 }
>> 270
>> 271 static inline size_t iov_total(const struct iovec *iv, unsigned long count)
>> 272 {
>> 273 unsigned long i;
>> 274 size_t len;
>> 275
>> 276 for (i = 0, len = 0; i < count; i++)
>> 277 len += iv[i].iov_len;
>> 278
>> 279 return len;
329 } 280 }
330 281
331 static ssize_t tun_chr_aio_write(struct kiocb !! 282 /* Writev */
332 unsigned long co !! 283 static ssize_t tun_chr_writev(struct file * file, const struct iovec *iv,
>> 284 unsigned long count, loff_t *pos)
333 { 285 {
334 struct tun_struct *tun = iocb->ki_filp !! 286 struct tun_struct *tun = file->private_data;
335 287
336 if (!tun) 288 if (!tun)
337 return -EBADFD; 289 return -EBADFD;
338 290
339 DBG(KERN_INFO "%s: tun_chr_write %ld\n 291 DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
340 292
341 return tun_get_user(tun, (struct iovec !! 293 return tun_get_user(tun, (struct iovec *) iv, iov_total(iv, count));
>> 294 }
>> 295
>> 296 /* Write */
>> 297 static ssize_t tun_chr_write(struct file * file, const char __user * buf,
>> 298 size_t count, loff_t *pos)
>> 299 {
>> 300 struct iovec iv = { (void __user *) buf, count };
>> 301 return tun_chr_writev(file, &iv, 1, pos);
342 } 302 }
343 303
344 /* Put packet to the user space buffer */ 304 /* Put packet to the user space buffer */
345 static __inline__ ssize_t tun_put_user(struct 305 static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
346 struct 306 struct sk_buff *skb,
347 struct 307 struct iovec *iv, int len)
348 { 308 {
349 struct tun_pi pi = { 0, skb->protocol 309 struct tun_pi pi = { 0, skb->protocol };
350 ssize_t total = 0; 310 ssize_t total = 0;
351 311
352 if (!(tun->flags & TUN_NO_PI)) { 312 if (!(tun->flags & TUN_NO_PI)) {
353 if ((len -= sizeof(pi)) < 0) 313 if ((len -= sizeof(pi)) < 0)
354 return -EINVAL; 314 return -EINVAL;
355 315
356 if (len < skb->len) { 316 if (len < skb->len) {
357 /* Packet will be stri 317 /* Packet will be striped */
358 pi.flags |= TUN_PKT_ST 318 pi.flags |= TUN_PKT_STRIP;
359 } 319 }
360 !! 320
361 if (memcpy_toiovec(iv, (void * 321 if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi)))
362 return -EFAULT; 322 return -EFAULT;
363 total += sizeof(pi); 323 total += sizeof(pi);
364 } !! 324 }
365 325
366 len = min_t(int, skb->len, len); 326 len = min_t(int, skb->len, len);
367 327
368 skb_copy_datagram_iovec(skb, 0, iv, le 328 skb_copy_datagram_iovec(skb, 0, iv, len);
369 total += len; 329 total += len;
370 330
371 tun->dev->stats.tx_packets++; !! 331 tun->stats.tx_packets++;
372 tun->dev->stats.tx_bytes += len; !! 332 tun->stats.tx_bytes += len;
373 333
374 return total; 334 return total;
375 } 335 }
376 336
377 static ssize_t tun_chr_aio_read(struct kiocb * !! 337 /* Readv */
378 unsigned long coun !! 338 static ssize_t tun_chr_readv(struct file *file, const struct iovec *iv,
>> 339 unsigned long count, loff_t *pos)
379 { 340 {
380 struct file *file = iocb->ki_filp; <<
381 struct tun_struct *tun = file->private 341 struct tun_struct *tun = file->private_data;
382 DECLARE_WAITQUEUE(wait, current); 342 DECLARE_WAITQUEUE(wait, current);
383 struct sk_buff *skb; 343 struct sk_buff *skb;
384 ssize_t len, ret = 0; 344 ssize_t len, ret = 0;
385 DECLARE_MAC_BUF(mac); <<
386 345
387 if (!tun) 346 if (!tun)
388 return -EBADFD; 347 return -EBADFD;
389 348
390 DBG(KERN_INFO "%s: tun_chr_read\n", tu 349 DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name);
391 350
392 len = iov_length(iv, count); !! 351 len = iov_total(iv, count);
393 if (len < 0) 352 if (len < 0)
394 return -EINVAL; 353 return -EINVAL;
395 354
396 add_wait_queue(&tun->read_wait, &wait) 355 add_wait_queue(&tun->read_wait, &wait);
397 while (len) { 356 while (len) {
398 const u8 ones[ ETH_ALEN] = { 0 357 const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
399 u8 addr[ ETH_ALEN]; 358 u8 addr[ ETH_ALEN];
400 int bit_nr; 359 int bit_nr;
401 360
402 current->state = TASK_INTERRUP 361 current->state = TASK_INTERRUPTIBLE;
403 362
404 /* Read frames from the queue 363 /* Read frames from the queue */
405 if (!(skb=skb_dequeue(&tun->re 364 if (!(skb=skb_dequeue(&tun->readq))) {
406 if (file->f_flags & O_ 365 if (file->f_flags & O_NONBLOCK) {
407 ret = -EAGAIN; 366 ret = -EAGAIN;
408 break; 367 break;
409 } 368 }
410 if (signal_pending(cur 369 if (signal_pending(current)) {
411 ret = -ERESTAR 370 ret = -ERESTARTSYS;
412 break; 371 break;
413 } 372 }
414 373
415 /* Nothing to read, le 374 /* Nothing to read, let's sleep */
416 schedule(); 375 schedule();
417 continue; 376 continue;
418 } 377 }
419 netif_wake_queue(tun->dev); 378 netif_wake_queue(tun->dev);
420 379
421 /** Decide whether to accept t 380 /** Decide whether to accept this packet. This code is designed to
422 * behave identically to an Et 381 * behave identically to an Ethernet interface. Accept the packet if
423 * - we are promiscuous. 382 * - we are promiscuous.
424 * - the packet is addressed t 383 * - the packet is addressed to us.
425 * - the packet is broadcast. 384 * - the packet is broadcast.
426 * - the packet is multicast a 385 * - the packet is multicast and
427 * - we are multicast promis 386 * - we are multicast promiscous.
428 * - we belong to the multic 387 * - we belong to the multicast group.
429 */ 388 */
430 skb_copy_from_linear_data(skb, !! 389 memcpy(addr, skb->data,
431 !! 390 min_t(size_t, sizeof addr, skb->len));
432 bit_nr = ether_crc(sizeof addr 391 bit_nr = ether_crc(sizeof addr, addr) >> 26;
433 if ((tun->if_flags & IFF_PROMI 392 if ((tun->if_flags & IFF_PROMISC) ||
434 memcmp(addr, t 393 memcmp(addr, tun->dev_addr, sizeof addr) == 0 ||
435 memcmp(addr, o 394 memcmp(addr, ones, sizeof addr) == 0 ||
436 (((addr[0] == 395 (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) ||
437 (addr[0] == 396 (addr[0] == 0x33 && addr[1] == 0x33)) &&
438 ((tun->if_fla 397 ((tun->if_flags & IFF_ALLMULTI) ||
439 (tun->chr_fi 398 (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) {
440 DBG(KERN_DEBUG "%s: tu !! 399 DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %x:%x:%x:%x:%x:%x\n",
441 tun->d !! 400 tun->dev->name, addr[0], addr[1], addr[2],
>> 401 addr[3], addr[4], addr[5]);
442 ret = tun_put_user(tun 402 ret = tun_put_user(tun, skb, (struct iovec *) iv, len);
443 kfree_skb(skb); 403 kfree_skb(skb);
444 break; 404 break;
445 } else { 405 } else {
446 DBG(KERN_DEBUG "%s: tu !! 406 DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %x:%x:%x:%x:%x:%x\n",
447 tun->d !! 407 tun->dev->name, addr[0], addr[1], addr[2],
>> 408 addr[3], addr[4], addr[5]);
448 kfree_skb(skb); 409 kfree_skb(skb);
449 continue; 410 continue;
450 } 411 }
451 } 412 }
452 413
453 current->state = TASK_RUNNING; 414 current->state = TASK_RUNNING;
454 remove_wait_queue(&tun->read_wait, &wa 415 remove_wait_queue(&tun->read_wait, &wait);
455 416
456 return ret; 417 return ret;
457 } 418 }
458 419
>> 420 /* Read */
>> 421 static ssize_t tun_chr_read(struct file * file, char __user * buf,
>> 422 size_t count, loff_t *pos)
>> 423 {
>> 424 struct iovec iv = { buf, count };
>> 425 return tun_chr_readv(file, &iv, 1, pos);
>> 426 }
>> 427
459 static void tun_setup(struct net_device *dev) 428 static void tun_setup(struct net_device *dev)
460 { 429 {
461 struct tun_struct *tun = netdev_priv(d 430 struct tun_struct *tun = netdev_priv(dev);
462 431
463 skb_queue_head_init(&tun->readq); 432 skb_queue_head_init(&tun->readq);
464 init_waitqueue_head(&tun->read_wait); 433 init_waitqueue_head(&tun->read_wait);
465 434
466 tun->owner = -1; 435 tun->owner = -1;
467 tun->group = -1; <<
468 436
>> 437 SET_MODULE_OWNER(dev);
469 dev->open = tun_net_open; 438 dev->open = tun_net_open;
470 dev->hard_start_xmit = tun_net_xmit; 439 dev->hard_start_xmit = tun_net_xmit;
471 dev->stop = tun_net_close; 440 dev->stop = tun_net_close;
>> 441 dev->get_stats = tun_net_stats;
472 dev->ethtool_ops = &tun_ethtool_ops; 442 dev->ethtool_ops = &tun_ethtool_ops;
473 dev->destructor = free_netdev; 443 dev->destructor = free_netdev;
474 } 444 }
475 445
476 static struct tun_struct *tun_get_by_name(cons 446 static struct tun_struct *tun_get_by_name(const char *name)
477 { 447 {
478 struct tun_struct *tun; 448 struct tun_struct *tun;
479 449
480 ASSERT_RTNL(); 450 ASSERT_RTNL();
481 list_for_each_entry(tun, &tun_dev_list 451 list_for_each_entry(tun, &tun_dev_list, list) {
482 if (!strncmp(tun->dev->name, n 452 if (!strncmp(tun->dev->name, name, IFNAMSIZ))
483 return tun; 453 return tun;
484 } 454 }
485 455
486 return NULL; 456 return NULL;
487 } 457 }
488 458
489 static int tun_set_iff(struct file *file, stru 459 static int tun_set_iff(struct file *file, struct ifreq *ifr)
490 { 460 {
491 struct tun_struct *tun; 461 struct tun_struct *tun;
492 struct net_device *dev; 462 struct net_device *dev;
493 int err; 463 int err;
494 464
495 tun = tun_get_by_name(ifr->ifr_name); 465 tun = tun_get_by_name(ifr->ifr_name);
496 if (tun) { 466 if (tun) {
497 if (tun->attached) 467 if (tun->attached)
498 return -EBUSY; 468 return -EBUSY;
499 469
500 /* Check permissions */ 470 /* Check permissions */
501 if (((tun->owner != -1 && !! 471 if (tun->owner != -1 &&
502 current->euid != tun->ow !! 472 current->euid != tun->owner && !capable(CAP_NET_ADMIN))
503 (tun->group != -1 && <<
504 current->egid != tun->gr <<
505 !capable(CAP_NET_ADMIN)) <<
506 return -EPERM; 473 return -EPERM;
507 } !! 474 }
508 else if (__dev_get_by_name(&init_net, !! 475 else if (__dev_get_by_name(ifr->ifr_name))
509 return -EINVAL; 476 return -EINVAL;
510 else { 477 else {
511 char *name; 478 char *name;
512 unsigned long flags = 0; 479 unsigned long flags = 0;
513 480
514 err = -EINVAL; 481 err = -EINVAL;
515 482
516 if (!capable(CAP_NET_ADMIN)) <<
517 return -EPERM; <<
518 <<
519 /* Set dev type */ 483 /* Set dev type */
520 if (ifr->ifr_flags & IFF_TUN) 484 if (ifr->ifr_flags & IFF_TUN) {
521 /* TUN device */ 485 /* TUN device */
522 flags |= TUN_TUN_DEV; 486 flags |= TUN_TUN_DEV;
523 name = "tun%d"; 487 name = "tun%d";
524 } else if (ifr->ifr_flags & IF 488 } else if (ifr->ifr_flags & IFF_TAP) {
525 /* TAP device */ 489 /* TAP device */
526 flags |= TUN_TAP_DEV; 490 flags |= TUN_TAP_DEV;
527 name = "tap%d"; 491 name = "tap%d";
528 } else !! 492 } else
529 goto failed; 493 goto failed;
530 !! 494
531 if (*ifr->ifr_name) 495 if (*ifr->ifr_name)
532 name = ifr->ifr_name; 496 name = ifr->ifr_name;
533 497
534 dev = alloc_netdev(sizeof(stru 498 dev = alloc_netdev(sizeof(struct tun_struct), name,
535 tun_setup); 499 tun_setup);
536 if (!dev) 500 if (!dev)
537 return -ENOMEM; 501 return -ENOMEM;
538 502
539 tun = netdev_priv(dev); 503 tun = netdev_priv(dev);
540 tun->dev = dev; 504 tun->dev = dev;
541 tun->flags = flags; 505 tun->flags = flags;
542 /* Be promiscuous by default t 506 /* Be promiscuous by default to maintain previous behaviour. */
543 tun->if_flags = IFF_PROMISC; 507 tun->if_flags = IFF_PROMISC;
544 /* Generate random Ethernet ad 508 /* Generate random Ethernet address. */
545 *(__be16 *)tun->dev_addr = hto !! 509 *(u16 *)tun->dev_addr = htons(0x00FF);
546 get_random_bytes(tun->dev_addr 510 get_random_bytes(tun->dev_addr + sizeof(u16), 4);
547 memset(tun->chr_filter, 0, siz 511 memset(tun->chr_filter, 0, sizeof tun->chr_filter);
548 512
549 tun_net_init(dev); 513 tun_net_init(dev);
550 514
551 if (strchr(dev->name, '%')) { 515 if (strchr(dev->name, '%')) {
552 err = dev_alloc_name(d 516 err = dev_alloc_name(dev, dev->name);
553 if (err < 0) 517 if (err < 0)
554 goto err_free_ 518 goto err_free_dev;
555 } 519 }
556 520
557 err = register_netdevice(tun-> 521 err = register_netdevice(tun->dev);
558 if (err < 0) 522 if (err < 0)
559 goto err_free_dev; 523 goto err_free_dev;
560 !! 524
561 list_add(&tun->list, &tun_dev_ 525 list_add(&tun->list, &tun_dev_list);
562 } 526 }
563 527
564 DBG(KERN_INFO "%s: tun_set_iff\n", tun 528 DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name);
565 529
566 if (ifr->ifr_flags & IFF_NO_PI) 530 if (ifr->ifr_flags & IFF_NO_PI)
567 tun->flags |= TUN_NO_PI; 531 tun->flags |= TUN_NO_PI;
568 else <<
569 tun->flags &= ~TUN_NO_PI; <<
570 532
571 if (ifr->ifr_flags & IFF_ONE_QUEUE) 533 if (ifr->ifr_flags & IFF_ONE_QUEUE)
572 tun->flags |= TUN_ONE_QUEUE; 534 tun->flags |= TUN_ONE_QUEUE;
573 else <<
574 tun->flags &= ~TUN_ONE_QUEUE; <<
575 535
576 file->private_data = tun; 536 file->private_data = tun;
577 tun->attached = 1; 537 tun->attached = 1;
578 538
579 strcpy(ifr->ifr_name, tun->dev->name); 539 strcpy(ifr->ifr_name, tun->dev->name);
580 return 0; 540 return 0;
581 541
582 err_free_dev: 542 err_free_dev:
583 free_netdev(dev); 543 free_netdev(dev);
584 failed: 544 failed:
585 return err; 545 return err;
586 } 546 }
587 547
588 static int tun_chr_ioctl(struct inode *inode, !! 548 static int tun_chr_ioctl(struct inode *inode, struct file *file,
589 unsigned int cmd, uns 549 unsigned int cmd, unsigned long arg)
590 { 550 {
591 struct tun_struct *tun = file->private 551 struct tun_struct *tun = file->private_data;
592 void __user* argp = (void __user*)arg; 552 void __user* argp = (void __user*)arg;
593 struct ifreq ifr; 553 struct ifreq ifr;
594 DECLARE_MAC_BUF(mac); <<
595 554
596 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) 555 if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
597 if (copy_from_user(&ifr, argp, 556 if (copy_from_user(&ifr, argp, sizeof ifr))
598 return -EFAULT; 557 return -EFAULT;
599 558
600 if (cmd == TUNSETIFF && !tun) { 559 if (cmd == TUNSETIFF && !tun) {
601 int err; 560 int err;
602 561
603 ifr.ifr_name[IFNAMSIZ-1] = '\0 562 ifr.ifr_name[IFNAMSIZ-1] = '\0';
604 563
605 rtnl_lock(); 564 rtnl_lock();
606 err = tun_set_iff(file, &ifr); 565 err = tun_set_iff(file, &ifr);
607 rtnl_unlock(); 566 rtnl_unlock();
608 567
609 if (err) 568 if (err)
610 return err; 569 return err;
611 570
612 if (copy_to_user(argp, &ifr, s 571 if (copy_to_user(argp, &ifr, sizeof(ifr)))
613 return -EFAULT; 572 return -EFAULT;
614 return 0; 573 return 0;
615 } 574 }
616 575
617 if (!tun) 576 if (!tun)
618 return -EBADFD; 577 return -EBADFD;
619 578
620 DBG(KERN_INFO "%s: tun_chr_ioctl cmd % 579 DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd);
621 580
622 switch (cmd) { 581 switch (cmd) {
623 case TUNSETNOCSUM: 582 case TUNSETNOCSUM:
624 /* Disable/Enable checksum */ 583 /* Disable/Enable checksum */
625 if (arg) 584 if (arg)
626 tun->flags |= TUN_NOCH 585 tun->flags |= TUN_NOCHECKSUM;
627 else 586 else
628 tun->flags &= ~TUN_NOC 587 tun->flags &= ~TUN_NOCHECKSUM;
629 588
630 DBG(KERN_INFO "%s: checksum %s 589 DBG(KERN_INFO "%s: checksum %s\n",
631 tun->dev->name, arg ? "dis 590 tun->dev->name, arg ? "disabled" : "enabled");
632 break; 591 break;
633 592
634 case TUNSETPERSIST: 593 case TUNSETPERSIST:
635 /* Disable/Enable persist mode 594 /* Disable/Enable persist mode */
636 if (arg) 595 if (arg)
637 tun->flags |= TUN_PERS 596 tun->flags |= TUN_PERSIST;
638 else 597 else
639 tun->flags &= ~TUN_PER 598 tun->flags &= ~TUN_PERSIST;
640 599
641 DBG(KERN_INFO "%s: persist %s\ 600 DBG(KERN_INFO "%s: persist %s\n",
642 tun->dev->name, arg ? "ena !! 601 tun->dev->name, arg ? "disabled" : "enabled");
643 break; 602 break;
644 603
645 case TUNSETOWNER: 604 case TUNSETOWNER:
646 /* Set owner of the device */ 605 /* Set owner of the device */
647 tun->owner = (uid_t) arg; 606 tun->owner = (uid_t) arg;
648 607
649 DBG(KERN_INFO "%s: owner set t 608 DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner);
650 break; 609 break;
651 610
652 case TUNSETGROUP: <<
653 /* Set group of the device */ <<
654 tun->group= (gid_t) arg; <<
655 <<
656 DBG(KERN_INFO "%s: group set t <<
657 break; <<
658 <<
659 case TUNSETLINK: <<
660 /* Only allow setting the type <<
661 if (tun->dev->flags & IFF_UP) <<
662 DBG(KERN_INFO "%s: Lin <<
663 tun->dev->name <<
664 return -EBUSY; <<
665 } else { <<
666 tun->dev->type = (int) <<
667 DBG(KERN_INFO "%s: lin <<
668 } <<
669 break; <<
670 <<
671 #ifdef TUN_DEBUG 611 #ifdef TUN_DEBUG
672 case TUNSETDEBUG: 612 case TUNSETDEBUG:
673 tun->debug = arg; 613 tun->debug = arg;
674 break; 614 break;
675 #endif 615 #endif
676 616
677 case SIOCGIFFLAGS: 617 case SIOCGIFFLAGS:
678 ifr.ifr_flags = tun->if_flags; 618 ifr.ifr_flags = tun->if_flags;
679 if (copy_to_user( argp, &ifr, 619 if (copy_to_user( argp, &ifr, sizeof ifr))
680 return -EFAULT; 620 return -EFAULT;
681 return 0; 621 return 0;
682 622
683 case SIOCSIFFLAGS: 623 case SIOCSIFFLAGS:
684 /** Set the character device's 624 /** Set the character device's interface flags. Currently only
685 * IFF_PROMISC and IFF_ALLMULT 625 * IFF_PROMISC and IFF_ALLMULTI are used. */
686 tun->if_flags = ifr.ifr_flags; 626 tun->if_flags = ifr.ifr_flags;
687 DBG(KERN_INFO "%s: interface f 627 DBG(KERN_INFO "%s: interface flags 0x%lx\n",
688 tun->dev->name 628 tun->dev->name, tun->if_flags);
689 return 0; 629 return 0;
690 630
691 case SIOCGIFHWADDR: 631 case SIOCGIFHWADDR:
692 /* Note: the actual net device <<
693 memcpy(ifr.ifr_hwaddr.sa_data, 632 memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr,
694 min(sizeof ifr 633 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
695 if (copy_to_user( argp, &ifr, 634 if (copy_to_user( argp, &ifr, sizeof ifr))
696 return -EFAULT; 635 return -EFAULT;
697 return 0; 636 return 0;
698 637
699 case SIOCSIFHWADDR: 638 case SIOCSIFHWADDR:
700 { !! 639 /** Set the character device's hardware address. This is used when
701 /* try to set the actual net d !! 640 * filtering packets being sent from the network device to the character
702 int ret; !! 641 * device. */
703 !! 642 memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data,
704 rtnl_lock(); !! 643 min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr));
705 ret = dev_set_mac_address(tun- !! 644 DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n",
706 rtnl_unlock(); !! 645 tun->dev->name,
707 !! 646 tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2],
708 if (ret == 0) { !! 647 tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]);
709 /** Set the character !! 648 return 0;
710 * filtering packets b <<
711 * device. */ <<
712 memcpy(tun->dev_addr, <<
713 min(si <<
714 DBG(KERN_DEBUG "%s: se <<
715 tun->d <<
716 tun->d <<
717 tun->d <<
718 } <<
719 <<
720 return ret; <<
721 } <<
722 649
723 case SIOCADDMULTI: 650 case SIOCADDMULTI:
724 /** Add the specified group to 651 /** Add the specified group to the character device's multicast filter
725 * list. */ 652 * list. */
726 add_multi(tun->chr_filter, ifr 653 add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
727 DBG(KERN_DEBUG "%s: add multi: !! 654 DBG(KERN_DEBUG "%s: add multi: %x:%x:%x:%x:%x:%x\n",
728 tun->dev->name, print_mac( !! 655 tun->dev->name,
>> 656 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
>> 657 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
>> 658 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
729 return 0; 659 return 0;
730 660
731 case SIOCDELMULTI: 661 case SIOCDELMULTI:
732 /** Remove the specified group 662 /** Remove the specified group from the character device's multicast
733 * filter list. */ 663 * filter list. */
734 del_multi(tun->chr_filter, ifr 664 del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data);
735 DBG(KERN_DEBUG "%s: del multi: !! 665 DBG(KERN_DEBUG "%s: del multi: %x:%x:%x:%x:%x:%x\n",
736 tun->dev->name, print_mac( !! 666 tun->dev->name,
>> 667 (u8)ifr.ifr_hwaddr.sa_data[0], (u8)ifr.ifr_hwaddr.sa_data[1],
>> 668 (u8)ifr.ifr_hwaddr.sa_data[2], (u8)ifr.ifr_hwaddr.sa_data[3],
>> 669 (u8)ifr.ifr_hwaddr.sa_data[4], (u8)ifr.ifr_hwaddr.sa_data[5]);
737 return 0; 670 return 0;
738 671
739 default: 672 default:
740 return -EINVAL; 673 return -EINVAL;
741 }; 674 };
742 675
743 return 0; 676 return 0;
744 } 677 }
745 678
746 static int tun_chr_fasync(int fd, struct file 679 static int tun_chr_fasync(int fd, struct file *file, int on)
747 { 680 {
748 struct tun_struct *tun = file->private 681 struct tun_struct *tun = file->private_data;
749 int ret; 682 int ret;
750 683
751 if (!tun) 684 if (!tun)
752 return -EBADFD; 685 return -EBADFD;
753 686
754 DBG(KERN_INFO "%s: tun_chr_fasync %d\n 687 DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on);
755 688
756 if ((ret = fasync_helper(fd, file, on, 689 if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
757 return ret; !! 690 return ret;
758 !! 691
759 if (on) { 692 if (on) {
760 ret = __f_setown(file, task_pi !! 693 ret = f_setown(file, current->pid, 0);
761 if (ret) 694 if (ret)
762 return ret; 695 return ret;
763 tun->flags |= TUN_FASYNC; 696 tun->flags |= TUN_FASYNC;
764 } else !! 697 } else
765 tun->flags &= ~TUN_FASYNC; 698 tun->flags &= ~TUN_FASYNC;
766 699
767 return 0; 700 return 0;
768 } 701 }
769 702
770 static int tun_chr_open(struct inode *inode, s 703 static int tun_chr_open(struct inode *inode, struct file * file)
771 { 704 {
772 DBG1(KERN_INFO "tunX: tun_chr_open\n") 705 DBG1(KERN_INFO "tunX: tun_chr_open\n");
773 file->private_data = NULL; 706 file->private_data = NULL;
774 return 0; 707 return 0;
775 } 708 }
776 709
777 static int tun_chr_close(struct inode *inode, 710 static int tun_chr_close(struct inode *inode, struct file *file)
778 { 711 {
779 struct tun_struct *tun = file->private 712 struct tun_struct *tun = file->private_data;
780 713
781 if (!tun) 714 if (!tun)
782 return 0; 715 return 0;
783 716
784 DBG(KERN_INFO "%s: tun_chr_close\n", t 717 DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name);
785 718
786 tun_chr_fasync(-1, file, 0); 719 tun_chr_fasync(-1, file, 0);
787 720
788 rtnl_lock(); 721 rtnl_lock();
789 722
790 /* Detach from net device */ 723 /* Detach from net device */
791 file->private_data = NULL; 724 file->private_data = NULL;
792 tun->attached = 0; 725 tun->attached = 0;
793 726
794 /* Drop read queue */ 727 /* Drop read queue */
795 skb_queue_purge(&tun->readq); 728 skb_queue_purge(&tun->readq);
796 729
797 if (!(tun->flags & TUN_PERSIST)) { 730 if (!(tun->flags & TUN_PERSIST)) {
798 list_del(&tun->list); 731 list_del(&tun->list);
799 unregister_netdevice(tun->dev) 732 unregister_netdevice(tun->dev);
800 } 733 }
801 734
802 rtnl_unlock(); 735 rtnl_unlock();
803 736
804 return 0; 737 return 0;
805 } 738 }
806 739
807 static const struct file_operations tun_fops = !! 740 static struct file_operations tun_fops = {
808 .owner = THIS_MODULE, !! 741 .owner = THIS_MODULE,
809 .llseek = no_llseek, 742 .llseek = no_llseek,
810 .read = do_sync_read, !! 743 .read = tun_chr_read,
811 .aio_read = tun_chr_aio_read, !! 744 .readv = tun_chr_readv,
812 .write = do_sync_write, !! 745 .write = tun_chr_write,
813 .aio_write = tun_chr_aio_write, !! 746 .writev = tun_chr_writev,
814 .poll = tun_chr_poll, 747 .poll = tun_chr_poll,
815 .ioctl = tun_chr_ioctl, 748 .ioctl = tun_chr_ioctl,
816 .open = tun_chr_open, 749 .open = tun_chr_open,
817 .release = tun_chr_close, 750 .release = tun_chr_close,
818 .fasync = tun_chr_fasync !! 751 .fasync = tun_chr_fasync
819 }; 752 };
820 753
821 static struct miscdevice tun_miscdev = { 754 static struct miscdevice tun_miscdev = {
822 .minor = TUN_MINOR, 755 .minor = TUN_MINOR,
823 .name = "tun", 756 .name = "tun",
824 .fops = &tun_fops, 757 .fops = &tun_fops,
>> 758 .devfs_name = "net/tun",
825 }; 759 };
826 760
827 /* ethtool interface */ 761 /* ethtool interface */
828 762
829 static int tun_get_settings(struct net_device 763 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
830 { 764 {
831 cmd->supported = 0; 765 cmd->supported = 0;
832 cmd->advertising = 0; 766 cmd->advertising = 0;
833 cmd->speed = SPEED_10; 767 cmd->speed = SPEED_10;
834 cmd->duplex = DUPLEX_FULL; 768 cmd->duplex = DUPLEX_FULL;
835 cmd->port = PORT_TP; 769 cmd->port = PORT_TP;
836 cmd->phy_address = 0; 770 cmd->phy_address = 0;
837 cmd->transceiver = XCVR_INTERNA 771 cmd->transceiver = XCVR_INTERNAL;
838 cmd->autoneg = AUTONEG_DISA 772 cmd->autoneg = AUTONEG_DISABLE;
839 cmd->maxtxpkt = 0; 773 cmd->maxtxpkt = 0;
840 cmd->maxrxpkt = 0; 774 cmd->maxrxpkt = 0;
841 return 0; 775 return 0;
842 } 776 }
843 777
844 static void tun_get_drvinfo(struct net_device 778 static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
845 { 779 {
846 struct tun_struct *tun = netdev_priv(d 780 struct tun_struct *tun = netdev_priv(dev);
847 781
848 strcpy(info->driver, DRV_NAME); 782 strcpy(info->driver, DRV_NAME);
849 strcpy(info->version, DRV_VERSION); 783 strcpy(info->version, DRV_VERSION);
850 strcpy(info->fw_version, "N/A"); 784 strcpy(info->fw_version, "N/A");
851 785
852 switch (tun->flags & TUN_TYPE_MASK) { 786 switch (tun->flags & TUN_TYPE_MASK) {
853 case TUN_TUN_DEV: 787 case TUN_TUN_DEV:
854 strcpy(info->bus_info, "tun"); 788 strcpy(info->bus_info, "tun");
855 break; 789 break;
856 case TUN_TAP_DEV: 790 case TUN_TAP_DEV:
857 strcpy(info->bus_info, "tap"); 791 strcpy(info->bus_info, "tap");
858 break; 792 break;
859 } 793 }
860 } 794 }
861 795
862 static u32 tun_get_msglevel(struct net_device 796 static u32 tun_get_msglevel(struct net_device *dev)
863 { 797 {
864 #ifdef TUN_DEBUG 798 #ifdef TUN_DEBUG
865 struct tun_struct *tun = netdev_priv(d 799 struct tun_struct *tun = netdev_priv(dev);
866 return tun->debug; 800 return tun->debug;
867 #else 801 #else
868 return -EOPNOTSUPP; 802 return -EOPNOTSUPP;
869 #endif 803 #endif
870 } 804 }
871 805
872 static void tun_set_msglevel(struct net_device 806 static void tun_set_msglevel(struct net_device *dev, u32 value)
873 { 807 {
874 #ifdef TUN_DEBUG 808 #ifdef TUN_DEBUG
875 struct tun_struct *tun = netdev_priv(d 809 struct tun_struct *tun = netdev_priv(dev);
876 tun->debug = value; 810 tun->debug = value;
877 #endif 811 #endif
878 } 812 }
879 813
880 static u32 tun_get_link(struct net_device *dev 814 static u32 tun_get_link(struct net_device *dev)
881 { 815 {
882 struct tun_struct *tun = netdev_priv(d 816 struct tun_struct *tun = netdev_priv(dev);
883 return tun->attached; 817 return tun->attached;
884 } 818 }
885 819
886 static u32 tun_get_rx_csum(struct net_device * 820 static u32 tun_get_rx_csum(struct net_device *dev)
887 { 821 {
888 struct tun_struct *tun = netdev_priv(d 822 struct tun_struct *tun = netdev_priv(dev);
889 return (tun->flags & TUN_NOCHECKSUM) = 823 return (tun->flags & TUN_NOCHECKSUM) == 0;
890 } 824 }
891 825
892 static int tun_set_rx_csum(struct net_device * 826 static int tun_set_rx_csum(struct net_device *dev, u32 data)
893 { 827 {
894 struct tun_struct *tun = netdev_priv(d 828 struct tun_struct *tun = netdev_priv(dev);
895 if (data) 829 if (data)
896 tun->flags &= ~TUN_NOCHECKSUM; 830 tun->flags &= ~TUN_NOCHECKSUM;
897 else 831 else
898 tun->flags |= TUN_NOCHECKSUM; 832 tun->flags |= TUN_NOCHECKSUM;
899 return 0; 833 return 0;
900 } 834 }
901 835
902 static const struct ethtool_ops tun_ethtool_op !! 836 static struct ethtool_ops tun_ethtool_ops = {
903 .get_settings = tun_get_settings, 837 .get_settings = tun_get_settings,
904 .get_drvinfo = tun_get_drvinfo, 838 .get_drvinfo = tun_get_drvinfo,
905 .get_msglevel = tun_get_msglevel, 839 .get_msglevel = tun_get_msglevel,
906 .set_msglevel = tun_set_msglevel, 840 .set_msglevel = tun_set_msglevel,
907 .get_link = tun_get_link, 841 .get_link = tun_get_link,
908 .get_rx_csum = tun_get_rx_csum, 842 .get_rx_csum = tun_get_rx_csum,
909 .set_rx_csum = tun_set_rx_csum 843 .set_rx_csum = tun_set_rx_csum
910 }; 844 };
911 845
912 static int __init tun_init(void) !! 846 int __init tun_init(void)
913 { 847 {
914 int ret = 0; 848 int ret = 0;
915 849
916 printk(KERN_INFO "tun: %s, %s\n", DRV_ 850 printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
917 printk(KERN_INFO "tun: %s\n", DRV_COPY 851 printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT);
918 852
919 ret = misc_register(&tun_miscdev); 853 ret = misc_register(&tun_miscdev);
920 if (ret) 854 if (ret)
921 printk(KERN_ERR "tun: Can't re 855 printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR);
922 return ret; 856 return ret;
923 } 857 }
924 858
925 static void tun_cleanup(void) !! 859 void tun_cleanup(void)
926 { 860 {
927 struct tun_struct *tun, *nxt; 861 struct tun_struct *tun, *nxt;
928 862
929 misc_deregister(&tun_miscdev); !! 863 misc_deregister(&tun_miscdev);
930 864
931 rtnl_lock(); 865 rtnl_lock();
932 list_for_each_entry_safe(tun, nxt, &tu 866 list_for_each_entry_safe(tun, nxt, &tun_dev_list, list) {
933 DBG(KERN_INFO "%s cleaned up\n 867 DBG(KERN_INFO "%s cleaned up\n", tun->dev->name);
934 unregister_netdevice(tun->dev) 868 unregister_netdevice(tun->dev);
935 } 869 }
936 rtnl_unlock(); 870 rtnl_unlock();
937 !! 871
938 } 872 }
939 873
940 module_init(tun_init); 874 module_init(tun_init);
941 module_exit(tun_cleanup); 875 module_exit(tun_cleanup);
942 MODULE_DESCRIPTION(DRV_DESCRIPTION); 876 MODULE_DESCRIPTION(DRV_DESCRIPTION);
943 MODULE_AUTHOR(DRV_COPYRIGHT); 877 MODULE_AUTHOR(DRV_COPYRIGHT);
944 MODULE_LICENSE("GPL"); 878 MODULE_LICENSE("GPL");
945 MODULE_ALIAS_MISCDEV(TUN_MINOR); 879 MODULE_ALIAS_MISCDEV(TUN_MINOR);
946 880
|
This page was automatically generated by the
LXR engine.
|