Advertisement
Guest User

Untitled

a guest
Oct 8th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 63.04 KB | None | 0 0
  1. /*
  2. * TUN - Universal TUN/TAP device driver.
  3. * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  16. */
  17.  
  18. /*
  19. * Changes:
  20. *
  21. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  22. * Add TUNSETLINK ioctl to set the link encapsulation
  23. *
  24. * Mark Smith <markzzzsmith@yahoo.com.au>
  25. * Use eth_random_addr() for tap MAC address.
  26. *
  27. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  28. * Fixes in packet dropping, queue length setting and queue wakeup.
  29. * Increased default tx queue length.
  30. * Added ethtool API.
  31. * Minor cleanups
  32. *
  33. * Daniel Podlejski <underley@underley.eu.org>
  34. * Modifications for 2.3.99-pre5 kernel.
  35. */
  36.  
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38.  
  39. #define DRV_NAME "tun"
  40. #define DRV_VERSION "1.6"
  41. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  42. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  43.  
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/kernel.h>
  47. #include <linux/major.h>
  48. #include <linux/slab.h>
  49. #include <linux/poll.h>
  50. #include <linux/fcntl.h>
  51. #include <linux/init.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/etherdevice.h>
  55. #include <linux/miscdevice.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/rtnetlink.h>
  58. #include <linux/compat.h>
  59. #include <linux/if.h>
  60. #include <linux/if_arp.h>
  61. #include <linux/if_ether.h>
  62. #include <linux/if_tun.h>
  63. #include <linux/if_vlan.h>
  64. #include <linux/crc32.h>
  65. #include <linux/nsproxy.h>
  66. #include <linux/virtio_net.h>
  67. #include <linux/rcupdate.h>
  68. #include <net/net_namespace.h>
  69. #include <net/netns/generic.h>
  70. #include <net/rtnetlink.h>
  71. #include <net/sock.h>
  72.  
  73. #include <asm/uaccess.h>
  74.  
  75. /* Uncomment to enable debugging */
  76. /* #define TUN_DEBUG 1 */
  77.  
  78. #ifdef TUN_DEBUG
  79. static int debug;
  80.  
  81. #define tun_debug(level, tun, fmt, args...) \
  82. do { \
  83. if (tun->debug) \
  84. netdev_printk(level, tun->dev, fmt, ##args); \
  85. } while (0)
  86. #define DBG1(level, fmt, args...) \
  87. do { \
  88. if (debug == 2) \
  89. printk(level fmt, ##args); \
  90. } while (0)
  91. #else
  92. #define tun_debug(level, tun, fmt, args...) \
  93. do { \
  94. if (0) \
  95. netdev_printk(level, tun->dev, fmt, ##args); \
  96. } while (0)
  97. #define DBG1(level, fmt, args...) \
  98. do { \
  99. if (0) \
  100. printk(level fmt, ##args); \
  101. } while (0)
  102. #endif
  103.  
  104. /* TUN device flags */
  105.  
  106. /* IFF_ATTACH_QUEUE is never stored in device flags,
  107. * overload it to mean fasync when stored there.
  108. */
  109. #define TUN_FASYNC IFF_ATTACH_QUEUE
  110. /* High bits in flags field are unused. */
  111. #define TUN_VNET_LE 0x80000000
  112. #define TUN_VNET_BE 0x40000000
  113.  
  114. #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
  115. IFF_MULTI_QUEUE)
  116. #define GOODCOPY_LEN 128
  117.  
  118. #define FLT_EXACT_COUNT 8
  119. struct tap_filter {
  120. unsigned int count; /* Number of addrs. Zero means disabled */
  121. u32 mask[2]; /* Mask of the hashed addrs */
  122. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  123. };
  124.  
  125. /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
  126. * to max number of VCPUs in guest. */
  127. #define MAX_TAP_QUEUES 256
  128. #define MAX_TAP_FLOWS 4096
  129.  
  130. #define TUN_FLOW_EXPIRE (3 * HZ)
  131.  
  132. struct tun_pcpu_stats {
  133. u64 rx_packets;
  134. u64 rx_bytes;
  135. u64 tx_packets;
  136. u64 tx_bytes;
  137. struct u64_stats_sync syncp;
  138. u32 rx_dropped;
  139. u32 tx_dropped;
  140. u32 rx_frame_errors;
  141. };
  142.  
  143. /* A tun_file connects an open character device to a tuntap netdevice. It
  144. * also contains all socket related strctures (except sock_fprog and tap_filter)
  145. * to serve as one transmit queue for tuntap device. The sock_fprog and
  146. * tap_filter were kept in tun_struct since they were used for filtering for the
  147. * netdevice not for a specific queue (at least I didn't see the requirement for
  148. * this).
  149. *
  150. * RCU usage:
  151. * The tun_file and tun_struct are loosely coupled, the pointer from one to the
  152. * other can only be read while rcu_read_lock or rtnl_lock is held.
  153. */
  154. struct tun_file {
  155. struct sock sk;
  156. struct socket socket;
  157. struct socket_wq wq;
  158. struct tun_struct __rcu *tun;
  159. struct net *net;
  160. struct fasync_struct *fasync;
  161. /* only used for fasnyc */
  162. unsigned int flags;
  163. union {
  164. u16 queue_index;
  165. unsigned int ifindex;
  166. };
  167. struct list_head next;
  168. struct tun_struct *detached;
  169. };
  170.  
  171. struct tun_flow_entry {
  172. struct hlist_node hash_link;
  173. struct rcu_head rcu;
  174. struct tun_struct *tun;
  175.  
  176. u32 rxhash;
  177. int queue_index;
  178. unsigned long updated;
  179. };
  180.  
  181. #define TUN_NUM_FLOW_ENTRIES 1024
  182.  
  183. /* Since the socket were moved to tun_file, to preserve the behavior of persist
  184. * device, socket filter, sndbuf and vnet header size were restore when the
  185. * file were attached to a persist device.
  186. */
  187. struct tun_struct {
  188. struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
  189. unsigned int numqueues;
  190. unsigned int flags;
  191. kuid_t owner;
  192. kgid_t group;
  193.  
  194. struct net_device *dev;
  195. netdev_features_t set_features;
  196. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  197. NETIF_F_TSO6|NETIF_F_UFO)
  198.  
  199. int align;
  200. int vnet_hdr_sz;
  201. int sndbuf;
  202. struct tap_filter txflt;
  203. struct sock_fprog fprog;
  204. /* protected by rtnl lock */
  205. bool filter_attached;
  206. #ifdef TUN_DEBUG
  207. int debug;
  208. #endif
  209. spinlock_t lock;
  210. struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
  211. struct timer_list flow_gc_timer;
  212. unsigned long ageing_time;
  213. unsigned int numdisabled;
  214. struct list_head disabled;
  215. void *security;
  216. u32 flow_count;
  217. struct tun_pcpu_stats __percpu *pcpu_stats;
  218. };
  219.  
  220. #ifdef CONFIG_TUN_VNET_CROSS_LE
  221. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  222. {
  223. return tun->flags & TUN_VNET_BE ? false :
  224. virtio_legacy_is_little_endian();
  225. }
  226.  
  227. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  228. {
  229. int be = !!(tun->flags & TUN_VNET_BE);
  230.  
  231. if (put_user(be, argp))
  232. return -EFAULT;
  233.  
  234. return 0;
  235. }
  236.  
  237. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  238. {
  239. int be;
  240.  
  241. if (get_user(be, argp))
  242. return -EFAULT;
  243.  
  244. if (be)
  245. tun->flags |= TUN_VNET_BE;
  246. else
  247. tun->flags &= ~TUN_VNET_BE;
  248.  
  249. return 0;
  250. }
  251. #else
  252. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  253. {
  254. return virtio_legacy_is_little_endian();
  255. }
  256.  
  257. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  258. {
  259. return -EINVAL;
  260. }
  261.  
  262. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  263. {
  264. return -EINVAL;
  265. }
  266. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  267.  
  268. static inline bool tun_is_little_endian(struct tun_struct *tun)
  269. {
  270. return tun->flags & TUN_VNET_LE ||
  271. tun_legacy_is_little_endian(tun);
  272. }
  273.  
  274. static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
  275. {
  276. return __virtio16_to_cpu(tun_is_little_endian(tun), val);
  277. }
  278.  
  279. static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
  280. {
  281. return __cpu_to_virtio16(tun_is_little_endian(tun), val);
  282. }
  283.  
  284. static inline u32 tun_hashfn(u32 rxhash)
  285. {
  286. return rxhash & 0x3ff;
  287. }
  288.  
  289. static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
  290. {
  291. struct tun_flow_entry *e;
  292.  
  293. hlist_for_each_entry_rcu(e, head, hash_link) {
  294. if (e->rxhash == rxhash)
  295. return e;
  296. }
  297. return NULL;
  298. }
  299.  
  300. static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
  301. struct hlist_head *head,
  302. u32 rxhash, u16 queue_index)
  303. {
  304. struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
  305.  
  306. if (e) {
  307. tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
  308. rxhash, queue_index);
  309. e->updated = jiffies;
  310. e->rxhash = rxhash;
  311. e->queue_index = queue_index;
  312. e->tun = tun;
  313. hlist_add_head_rcu(&e->hash_link, head);
  314. ++tun->flow_count;
  315. }
  316. return e;
  317. }
  318.  
  319. static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
  320. {
  321. tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
  322. e->rxhash, e->queue_index);
  323. hlist_del_rcu(&e->hash_link);
  324. kfree_rcu(e, rcu);
  325. --tun->flow_count;
  326. }
  327.  
  328. static void tun_flow_flush(struct tun_struct *tun)
  329. {
  330. int i;
  331.  
  332. spin_lock_bh(&tun->lock);
  333. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  334. struct tun_flow_entry *e;
  335. struct hlist_node *n;
  336.  
  337. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
  338. tun_flow_delete(tun, e);
  339. }
  340. spin_unlock_bh(&tun->lock);
  341. }
  342.  
  343. static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
  344. {
  345. int i;
  346.  
  347. spin_lock_bh(&tun->lock);
  348. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  349. struct tun_flow_entry *e;
  350. struct hlist_node *n;
  351.  
  352. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  353. if (e->queue_index == queue_index)
  354. tun_flow_delete(tun, e);
  355. }
  356. }
  357. spin_unlock_bh(&tun->lock);
  358. }
  359.  
  360. static void tun_flow_cleanup(unsigned long data)
  361. {
  362. struct tun_struct *tun = (struct tun_struct *)data;
  363. unsigned long delay = tun->ageing_time;
  364. unsigned long next_timer = jiffies + delay;
  365. unsigned long count = 0;
  366. int i;
  367.  
  368. tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
  369.  
  370. spin_lock_bh(&tun->lock);
  371. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  372. struct tun_flow_entry *e;
  373. struct hlist_node *n;
  374.  
  375. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  376. unsigned long this_timer;
  377. count++;
  378. this_timer = e->updated + delay;
  379. if (time_before_eq(this_timer, jiffies))
  380. tun_flow_delete(tun, e);
  381. else if (time_before(this_timer, next_timer))
  382. next_timer = this_timer;
  383. }
  384. }
  385.  
  386. if (count)
  387. mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
  388. spin_unlock_bh(&tun->lock);
  389. }
  390.  
  391. static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
  392. struct tun_file *tfile)
  393. {
  394. struct hlist_head *head;
  395. struct tun_flow_entry *e;
  396. unsigned long delay = tun->ageing_time;
  397. u16 queue_index = tfile->queue_index;
  398.  
  399. if (!rxhash)
  400. return;
  401. else
  402. head = &tun->flows[tun_hashfn(rxhash)];
  403.  
  404. rcu_read_lock();
  405.  
  406. /* We may get a very small possibility of OOO during switching, not
  407. * worth to optimize.*/
  408. if (tun->numqueues == 1 || tfile->detached)
  409. goto unlock;
  410.  
  411. e = tun_flow_find(head, rxhash);
  412. if (likely(e)) {
  413. /* TODO: keep queueing to old queue until it's empty? */
  414. e->queue_index = queue_index;
  415. e->updated = jiffies;
  416. } else {
  417. spin_lock_bh(&tun->lock);
  418. if (!tun_flow_find(head, rxhash) &&
  419. tun->flow_count < MAX_TAP_FLOWS)
  420. tun_flow_create(tun, head, rxhash, queue_index);
  421.  
  422. if (!timer_pending(&tun->flow_gc_timer))
  423. mod_timer(&tun->flow_gc_timer,
  424. round_jiffies_up(jiffies + delay));
  425. spin_unlock_bh(&tun->lock);
  426. }
  427.  
  428. unlock:
  429. rcu_read_unlock();
  430. }
  431.  
  432. /* We try to identify a flow through its rxhash first. The reason that
  433. * we do not check rxq no. is becuase some cards(e.g 82599), chooses
  434. * the rxq based on the txq where the last packet of the flow comes. As
  435. * the userspace application move between processors, we may get a
  436. * different rxq no. here. If we could not get rxhash, then we would
  437. * hope the rxq no. may help here.
  438. */
  439. static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
  440. void *accel_priv, select_queue_fallback_t fallback)
  441. {
  442. struct tun_struct *tun = netdev_priv(dev);
  443. struct tun_flow_entry *e;
  444. u32 txq = 0;
  445. u32 numqueues = 0;
  446.  
  447. rcu_read_lock();
  448. numqueues = ACCESS_ONCE(tun->numqueues);
  449.  
  450. txq = skb_get_hash(skb);
  451. if (txq) {
  452. e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
  453. if (e)
  454. txq = e->queue_index;
  455. else
  456. /* use multiply and shift instead of expensive divide */
  457. txq = ((u64)txq * numqueues) >> 32;
  458. } else if (likely(skb_rx_queue_recorded(skb))) {
  459. txq = skb_get_rx_queue(skb);
  460. while (unlikely(txq >= numqueues))
  461. txq -= numqueues;
  462. }
  463.  
  464. rcu_read_unlock();
  465. return txq;
  466. }
  467.  
  468. static inline bool tun_not_capable(struct tun_struct *tun)
  469. {
  470. const struct cred *cred = current_cred();
  471. struct net *net = dev_net(tun->dev);
  472.  
  473. return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
  474. (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
  475. !ns_capable(net->user_ns, CAP_NET_ADMIN);
  476. }
  477.  
  478. static void tun_set_real_num_queues(struct tun_struct *tun)
  479. {
  480. netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
  481. netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
  482. }
  483.  
  484. static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
  485. {
  486. tfile->detached = tun;
  487. list_add_tail(&tfile->next, &tun->disabled);
  488. ++tun->numdisabled;
  489. }
  490.  
  491. static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
  492. {
  493. struct tun_struct *tun = tfile->detached;
  494.  
  495. tfile->detached = NULL;
  496. list_del_init(&tfile->next);
  497. --tun->numdisabled;
  498. return tun;
  499. }
  500.  
  501. static void __tun_detach(struct tun_file *tfile, bool clean)
  502. {
  503. struct tun_file *ntfile;
  504. struct tun_struct *tun;
  505.  
  506. tun = rtnl_dereference(tfile->tun);
  507.  
  508. if (tun && !tfile->detached) {
  509. u16 index = tfile->queue_index;
  510. BUG_ON(index >= tun->numqueues);
  511.  
  512. rcu_assign_pointer(tun->tfiles[index],
  513. tun->tfiles[tun->numqueues - 1]);
  514. ntfile = rtnl_dereference(tun->tfiles[index]);
  515. ntfile->queue_index = index;
  516.  
  517. --tun->numqueues;
  518. if (clean) {
  519. rcu_assign_pointer(tfile->tun, NULL);
  520. sock_put(&tfile->sk);
  521. } else
  522. tun_disable_queue(tun, tfile);
  523.  
  524. synchronize_net();
  525. tun_flow_delete_by_queue(tun, tun->numqueues + 1);
  526. /* Drop read queue */
  527. skb_queue_purge(&tfile->sk.sk_receive_queue);
  528. tun_set_real_num_queues(tun);
  529. } else if (tfile->detached && clean) {
  530. tun = tun_enable_queue(tfile);
  531. sock_put(&tfile->sk);
  532. }
  533.  
  534. if (clean) {
  535. if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
  536. netif_carrier_off(tun->dev);
  537.  
  538. if (!(tun->flags & IFF_PERSIST) &&
  539. tun->dev->reg_state == NETREG_REGISTERED)
  540. unregister_netdevice(tun->dev);
  541. }
  542.  
  543. BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
  544. &tfile->socket.flags));
  545. sk_release_kernel(&tfile->sk);
  546. }
  547. }
  548.  
  549. static void tun_detach(struct tun_file *tfile, bool clean)
  550. {
  551. rtnl_lock();
  552. __tun_detach(tfile, clean);
  553. rtnl_unlock();
  554. }
  555.  
  556. static void tun_detach_all(struct net_device *dev)
  557. {
  558. struct tun_struct *tun = netdev_priv(dev);
  559. struct tun_file *tfile, *tmp;
  560. int i, n = tun->numqueues;
  561.  
  562. for (i = 0; i < n; i++) {
  563. tfile = rtnl_dereference(tun->tfiles[i]);
  564. BUG_ON(!tfile);
  565. wake_up_all(&tfile->wq.wait);
  566. rcu_assign_pointer(tfile->tun, NULL);
  567. --tun->numqueues;
  568. }
  569. list_for_each_entry(tfile, &tun->disabled, next) {
  570. wake_up_all(&tfile->wq.wait);
  571. rcu_assign_pointer(tfile->tun, NULL);
  572. }
  573. BUG_ON(tun->numqueues != 0);
  574.  
  575. synchronize_net();
  576. for (i = 0; i < n; i++) {
  577. tfile = rtnl_dereference(tun->tfiles[i]);
  578. /* Drop read queue */
  579. skb_queue_purge(&tfile->sk.sk_receive_queue);
  580. sock_put(&tfile->sk);
  581. }
  582. list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
  583. tun_enable_queue(tfile);
  584. skb_queue_purge(&tfile->sk.sk_receive_queue);
  585. sock_put(&tfile->sk);
  586. }
  587. BUG_ON(tun->numdisabled != 0);
  588.  
  589. if (tun->flags & IFF_PERSIST)
  590. module_put(THIS_MODULE);
  591. }
  592.  
  593. static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter)
  594. {
  595. struct tun_file *tfile = file->private_data;
  596. int err;
  597.  
  598. err = security_tun_dev_attach(tfile->socket.sk, tun->security);
  599. if (err < 0)
  600. goto out;
  601.  
  602. err = -EINVAL;
  603. if (rtnl_dereference(tfile->tun) && !tfile->detached)
  604. goto out;
  605.  
  606. err = -EBUSY;
  607. if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
  608. goto out;
  609.  
  610. err = -E2BIG;
  611. if (!tfile->detached &&
  612. tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
  613. goto out;
  614.  
  615. err = 0;
  616.  
  617. /* Re-attach the filter to presist device */
  618. if (!skip_filter && (tun->filter_attached == true)) {
  619. err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  620. if (!err)
  621. goto out;
  622. }
  623. tfile->queue_index = tun->numqueues;
  624. rcu_assign_pointer(tfile->tun, tun);
  625. rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
  626. tun->numqueues++;
  627.  
  628. if (tfile->detached)
  629. tun_enable_queue(tfile);
  630. else
  631. sock_hold(&tfile->sk);
  632.  
  633. tun_set_real_num_queues(tun);
  634.  
  635. /* device is allowed to go away first, so no need to hold extra
  636. * refcnt.
  637. */
  638.  
  639. out:
  640. return err;
  641. }
  642.  
  643. static struct tun_struct *__tun_get(struct tun_file *tfile)
  644. {
  645. struct tun_struct *tun;
  646.  
  647. rcu_read_lock();
  648. tun = rcu_dereference(tfile->tun);
  649. if (tun)
  650. dev_hold(tun->dev);
  651. rcu_read_unlock();
  652.  
  653. return tun;
  654. }
  655.  
  656. static struct tun_struct *tun_get(struct file *file)
  657. {
  658. return __tun_get(file->private_data);
  659. }
  660.  
  661. static void tun_put(struct tun_struct *tun)
  662. {
  663. dev_put(tun->dev);
  664. }
  665.  
  666. /* TAP filtering */
  667. static void addr_hash_set(u32 *mask, const u8 *addr)
  668. {
  669. int n = ether_crc(ETH_ALEN, addr) >> 26;
  670. mask[n >> 5] |= (1 << (n & 31));
  671. }
  672.  
  673. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  674. {
  675. int n = ether_crc(ETH_ALEN, addr) >> 26;
  676. return mask[n >> 5] & (1 << (n & 31));
  677. }
  678.  
  679. static int update_filter(struct tap_filter *filter, void __user *arg)
  680. {
  681. struct { u8 u[ETH_ALEN]; } *addr;
  682. struct tun_filter uf;
  683. int err, alen, n, nexact;
  684.  
  685. if (copy_from_user(&uf, arg, sizeof(uf)))
  686. return -EFAULT;
  687.  
  688. if (!uf.count) {
  689. /* Disabled */
  690. filter->count = 0;
  691. return 0;
  692. }
  693.  
  694. alen = ETH_ALEN * uf.count;
  695. addr = kmalloc(alen, GFP_KERNEL);
  696. if (!addr)
  697. return -ENOMEM;
  698.  
  699. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  700. err = -EFAULT;
  701. goto done;
  702. }
  703.  
  704. /* The filter is updated without holding any locks. Which is
  705. * perfectly safe. We disable it first and in the worst
  706. * case we'll accept a few undesired packets. */
  707. filter->count = 0;
  708. wmb();
  709.  
  710. /* Use first set of addresses as an exact filter */
  711. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  712. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  713.  
  714. nexact = n;
  715.  
  716. /* Remaining multicast addresses are hashed,
  717. * unicast will leave the filter disabled. */
  718. memset(filter->mask, 0, sizeof(filter->mask));
  719. for (; n < uf.count; n++) {
  720. if (!is_multicast_ether_addr(addr[n].u)) {
  721. err = 0; /* no filter */
  722. goto done;
  723. }
  724. addr_hash_set(filter->mask, addr[n].u);
  725. }
  726.  
  727. /* For ALLMULTI just set the mask to all ones.
  728. * This overrides the mask populated above. */
  729. if ((uf.flags & TUN_FLT_ALLMULTI))
  730. memset(filter->mask, ~0, sizeof(filter->mask));
  731.  
  732. /* Now enable the filter */
  733. wmb();
  734. filter->count = nexact;
  735.  
  736. /* Return the number of exact filters */
  737. err = nexact;
  738.  
  739. done:
  740. kfree(addr);
  741. return err;
  742. }
  743.  
  744. /* Returns: 0 - drop, !=0 - accept */
  745. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  746. {
  747. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  748. * at this point. */
  749. struct ethhdr *eh = (struct ethhdr *) skb->data;
  750. int i;
  751.  
  752. /* Exact match */
  753. for (i = 0; i < filter->count; i++)
  754. if (ether_addr_equal(eh->h_dest, filter->addr[i]))
  755. return 1;
  756.  
  757. /* Inexact match (multicast only) */
  758. if (is_multicast_ether_addr(eh->h_dest))
  759. return addr_hash_test(filter->mask, eh->h_dest);
  760.  
  761. return 0;
  762. }
  763.  
  764. /*
  765. * Checks whether the packet is accepted or not.
  766. * Returns: 0 - drop, !=0 - accept
  767. */
  768. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  769. {
  770. if (!filter->count)
  771. return 1;
  772.  
  773. return run_filter(filter, skb);
  774. }
  775.  
  776. /* Network device part of the driver */
  777.  
  778. static const struct ethtool_ops tun_ethtool_ops;
  779.  
  780. /* Net device detach from fd. */
  781. static void tun_net_uninit(struct net_device *dev)
  782. {
  783. tun_detach_all(dev);
  784. }
  785.  
  786. /* Net device open. */
  787. static int tun_net_open(struct net_device *dev)
  788. {
  789. netif_tx_start_all_queues(dev);
  790. return 0;
  791. }
  792.  
  793. /* Net device close. */
  794. static int tun_net_close(struct net_device *dev)
  795. {
  796. netif_tx_stop_all_queues(dev);
  797. return 0;
  798. }
  799.  
  800. /* Net device start xmit */
  801. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  802. {
  803. struct tun_struct *tun = netdev_priv(dev);
  804. int txq = skb->queue_mapping;
  805. struct tun_file *tfile;
  806. u32 numqueues = 0;
  807.  
  808. rcu_read_lock();
  809. tfile = rcu_dereference(tun->tfiles[txq]);
  810. numqueues = ACCESS_ONCE(tun->numqueues);
  811.  
  812. /* Drop packet if interface is not attached */
  813. if (txq >= numqueues)
  814. goto drop;
  815.  
  816. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  817.  
  818. BUG_ON(!tfile);
  819.  
  820. /* Drop if the filter does not like it.
  821. * This is a noop if the filter is disabled.
  822. * Filter can be enabled only for the TAP devices. */
  823. if (!check_filter(&tun->txflt, skb))
  824. goto drop;
  825.  
  826. if (tfile->socket.sk->sk_filter &&
  827. sk_filter(tfile->socket.sk, skb))
  828. goto drop;
  829.  
  830. /* Limit the number of packets queued by dividing txq length with the
  831. * number of queues.
  832. */
  833. if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues
  834. >= dev->tx_queue_len)
  835. goto drop;
  836.  
  837. /* Orphan the skb - required as we might hang on to it
  838. * for indefinite time. */
  839. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  840. goto drop;
  841. skb_orphan(skb);
  842.  
  843. nf_reset(skb);
  844.  
  845. /* Enqueue packet */
  846. skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
  847.  
  848. /* Notify and wake up reader process */
  849. if (tfile->flags & TUN_FASYNC)
  850. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  851. wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
  852. POLLRDNORM | POLLRDBAND);
  853.  
  854. rcu_read_unlock();
  855. return NETDEV_TX_OK;
  856.  
  857. drop:
  858. this_cpu_inc(tun->pcpu_stats->tx_dropped);
  859. skb_tx_error(skb);
  860. kfree_skb(skb);
  861. rcu_read_unlock();
  862. return NETDEV_TX_OK;
  863. }
  864.  
  865. static void tun_net_mclist(struct net_device *dev)
  866. {
  867. /*
  868. * This callback is supposed to deal with mc filter in
  869. * _rx_ path and has nothing to do with the _tx_ path.
  870. * In rx path we always accept everything userspace gives us.
  871. */
  872. }
  873.  
  874. #define MIN_MTU 68
  875. #define MAX_MTU 65535
  876.  
  877. static int
  878. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  879. {
  880. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  881. return -EINVAL;
  882. dev->mtu = new_mtu;
  883. return 0;
  884. }
  885.  
  886. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  887. netdev_features_t features)
  888. {
  889. struct tun_struct *tun = netdev_priv(dev);
  890.  
  891. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  892. }
  893.  
  894. static struct rtnl_link_stats64 *
  895. tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  896. {
  897. u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
  898. struct tun_struct *tun = netdev_priv(dev);
  899. struct tun_pcpu_stats *p;
  900. int i;
  901.  
  902. for_each_possible_cpu(i) {
  903. u64 rxpackets, rxbytes, txpackets, txbytes;
  904. unsigned int start;
  905.  
  906. p = per_cpu_ptr(tun->pcpu_stats, i);
  907. do {
  908. start = u64_stats_fetch_begin(&p->syncp);
  909. rxpackets = p->rx_packets;
  910. rxbytes = p->rx_bytes;
  911. txpackets = p->tx_packets;
  912. txbytes = p->tx_bytes;
  913. } while (u64_stats_fetch_retry(&p->syncp, start));
  914.  
  915. stats->rx_packets += rxpackets;
  916. stats->rx_bytes += rxbytes;
  917. stats->tx_packets += txpackets;
  918. stats->tx_bytes += txbytes;
  919.  
  920. /* u32 counters */
  921. rx_dropped += p->rx_dropped;
  922. rx_frame_errors += p->rx_frame_errors;
  923. tx_dropped += p->tx_dropped;
  924. }
  925. stats->rx_dropped = rx_dropped;
  926. stats->rx_frame_errors = rx_frame_errors;
  927. stats->tx_dropped = tx_dropped;
  928. return stats;
  929. }
  930.  
  931. #ifdef CONFIG_NET_POLL_CONTROLLER
  932. static void tun_poll_controller(struct net_device *dev)
  933. {
  934. /*
  935. * Tun only receives frames when:
  936. * 1) the char device endpoint gets data from user space
  937. * 2) the tun socket gets a sendmsg call from user space
  938. * Since both of those are syncronous operations, we are guaranteed
  939. * never to have pending data when we poll for it
  940. * so theres nothing to do here but return.
  941. * We need this though so netpoll recognizes us as an interface that
  942. * supports polling, which enables bridge devices in virt setups to
  943. * still use netconsole
  944. */
  945. return;
  946. }
  947. #endif
  948.  
  949. static void tun_set_headroom(struct net_device *dev, int new_hr)
  950. {
  951. struct tun_struct *tun = netdev_priv(dev);
  952.  
  953. if (new_hr < NET_SKB_PAD)
  954. new_hr = NET_SKB_PAD;
  955.  
  956. tun->align = new_hr;
  957. }
  958.  
  959. static const struct net_device_ops tun_netdev_ops = {
  960. .ndo_uninit = tun_net_uninit,
  961. .ndo_open = tun_net_open,
  962. .ndo_stop = tun_net_close,
  963. .ndo_start_xmit = tun_net_xmit,
  964. .ndo_change_mtu = tun_net_change_mtu,
  965. .ndo_fix_features = tun_net_fix_features,
  966. .ndo_select_queue = tun_select_queue,
  967. #ifdef CONFIG_NET_POLL_CONTROLLER
  968. .ndo_poll_controller = tun_poll_controller,
  969. #endif
  970. .ndo_size = sizeof(struct net_device_ops),
  971. .extended.ndo_set_rx_headroom = tun_set_headroom,
  972. .ndo_get_stats64 = tun_net_get_stats64,
  973. };
  974.  
  975. static const struct net_device_ops tap_netdev_ops = {
  976. .ndo_uninit = tun_net_uninit,
  977. .ndo_open = tun_net_open,
  978. .ndo_stop = tun_net_close,
  979. .ndo_start_xmit = tun_net_xmit,
  980. .ndo_change_mtu = tun_net_change_mtu,
  981. .ndo_fix_features = tun_net_fix_features,
  982. .ndo_set_rx_mode = tun_net_mclist,
  983. .ndo_set_mac_address = eth_mac_addr,
  984. .ndo_validate_addr = eth_validate_addr,
  985. .ndo_select_queue = tun_select_queue,
  986. #ifdef CONFIG_NET_POLL_CONTROLLER
  987. .ndo_poll_controller = tun_poll_controller,
  988. #endif
  989. .ndo_size = sizeof(struct net_device_ops),
  990. .extended.ndo_set_rx_headroom = tun_set_headroom,
  991. .ndo_get_stats64 = tun_net_get_stats64,
  992. };
  993.  
  994. static int tun_flow_init(struct tun_struct *tun)
  995. {
  996. int i;
  997.  
  998. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
  999. INIT_HLIST_HEAD(&tun->flows[i]);
  1000.  
  1001. tun->ageing_time = TUN_FLOW_EXPIRE;
  1002. setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun);
  1003. mod_timer(&tun->flow_gc_timer,
  1004. round_jiffies_up(jiffies + tun->ageing_time));
  1005.  
  1006. return 0;
  1007. }
  1008.  
  1009. static void tun_flow_uninit(struct tun_struct *tun)
  1010. {
  1011. del_timer_sync(&tun->flow_gc_timer);
  1012. tun_flow_flush(tun);
  1013. }
  1014.  
  1015. /* Initialize net device. */
  1016. static void tun_net_init(struct net_device *dev)
  1017. {
  1018. struct tun_struct *tun = netdev_priv(dev);
  1019.  
  1020. switch (tun->flags & TUN_TYPE_MASK) {
  1021. case IFF_TUN:
  1022. dev->netdev_ops = &tun_netdev_ops;
  1023.  
  1024. /* Point-to-Point TUN Device */
  1025. dev->hard_header_len = 0;
  1026. dev->addr_len = 0;
  1027. dev->mtu = 1500;
  1028.  
  1029. /* Zero header length */
  1030. dev->type = ARPHRD_NONE;
  1031. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1032. break;
  1033.  
  1034. case IFF_TAP:
  1035. dev->netdev_ops = &tap_netdev_ops;
  1036. /* Ethernet TAP Device */
  1037. ether_setup(dev);
  1038. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1039. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1040.  
  1041. eth_hw_addr_random(dev);
  1042.  
  1043. break;
  1044. }
  1045. }
  1046.  
  1047. /* Character device part */
  1048.  
  1049. /* Poll */
  1050. static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
  1051. {
  1052. struct tun_file *tfile = file->private_data;
  1053. struct tun_struct *tun = __tun_get(tfile);
  1054. struct sock *sk;
  1055. unsigned int mask = 0;
  1056.  
  1057. if (!tun)
  1058. return POLLERR;
  1059.  
  1060. sk = tfile->socket.sk;
  1061.  
  1062. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  1063.  
  1064. poll_wait(file, &tfile->wq.wait, wait);
  1065.  
  1066. if (!skb_queue_empty(&sk->sk_receive_queue))
  1067. mask |= POLLIN | POLLRDNORM;
  1068.  
  1069. if (sock_writeable(sk) ||
  1070. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  1071. sock_writeable(sk)))
  1072. mask |= POLLOUT | POLLWRNORM;
  1073.  
  1074. if (tun->dev->reg_state != NETREG_REGISTERED)
  1075. mask = POLLERR;
  1076.  
  1077. tun_put(tun);
  1078. return mask;
  1079. }
  1080.  
  1081. /* prepad is the amount to reserve at front. len is length after that.
  1082. * linear is a hint as to how much to copy (usually headers). */
  1083. static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
  1084. size_t prepad, size_t len,
  1085. size_t linear, int noblock)
  1086. {
  1087. struct sock *sk = tfile->socket.sk;
  1088. struct sk_buff *skb;
  1089. int err;
  1090.  
  1091. /* Under a page? Don't bother with paged skb. */
  1092. if (prepad + len < PAGE_SIZE || !linear)
  1093. linear = len;
  1094.  
  1095. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1096. &err, 0);
  1097. if (!skb)
  1098. return ERR_PTR(err);
  1099.  
  1100. skb_reserve(skb, prepad);
  1101. skb_put(skb, linear);
  1102. skb->data_len = len - linear;
  1103. skb->len += len - linear;
  1104.  
  1105. return skb;
  1106. }
  1107.  
  1108. /* set skb frags from iovec, this can move to core network code for reuse */
  1109. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  1110. int offset, size_t count)
  1111. {
  1112. int len = iov_length(from, count) - offset;
  1113. int copy = skb_headlen(skb);
  1114. int size, offset1 = 0;
  1115. int i = 0;
  1116.  
  1117. /* Skip over from offset */
  1118. while (count && (offset >= from->iov_len)) {
  1119. offset -= from->iov_len;
  1120. ++from;
  1121. --count;
  1122. }
  1123.  
  1124. /* copy up to skb headlen */
  1125. while (count && (copy > 0)) {
  1126. size = min_t(unsigned int, copy, from->iov_len - offset);
  1127. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  1128. size))
  1129. return -EFAULT;
  1130. if (copy > size) {
  1131. ++from;
  1132. --count;
  1133. offset = 0;
  1134. } else
  1135. offset += size;
  1136. copy -= size;
  1137. offset1 += size;
  1138. }
  1139.  
  1140. if (len == offset1)
  1141. return 0;
  1142.  
  1143. while (count--) {
  1144. struct page *page[MAX_SKB_FRAGS];
  1145. int num_pages;
  1146. unsigned long base;
  1147. unsigned long truesize;
  1148.  
  1149. len = from->iov_len - offset;
  1150. if (!len) {
  1151. offset = 0;
  1152. ++from;
  1153. continue;
  1154. }
  1155. base = (unsigned long)from->iov_base + offset;
  1156. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  1157. if (i + size > MAX_SKB_FRAGS)
  1158. return -EMSGSIZE;
  1159. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  1160. if (num_pages != size) {
  1161. int j;
  1162.  
  1163. for (j = 0; j < num_pages; j++)
  1164. put_page(page[i + j]);
  1165. return -EFAULT;
  1166. }
  1167. truesize = size * PAGE_SIZE;
  1168. skb->data_len += len;
  1169. skb->len += len;
  1170. skb->truesize += truesize;
  1171. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  1172. while (len) {
  1173. int off = base & ~PAGE_MASK;
  1174. int size = min_t(int, len, PAGE_SIZE - off);
  1175. __skb_fill_page_desc(skb, i, page[i], off, size);
  1176. skb_shinfo(skb)->nr_frags++;
  1177. /* increase sk_wmem_alloc */
  1178. base += size;
  1179. len -= size;
  1180. i++;
  1181. }
  1182. offset = 0;
  1183. ++from;
  1184. }
  1185. return 0;
  1186. }
  1187.  
  1188. static unsigned long iov_pages(const struct iovec *iv, int offset,
  1189. unsigned long nr_segs)
  1190. {
  1191. unsigned long seg, base;
  1192. int pages = 0, len, size;
  1193.  
  1194. while (nr_segs && (offset >= iv->iov_len)) {
  1195. offset -= iv->iov_len;
  1196. ++iv;
  1197. --nr_segs;
  1198. }
  1199.  
  1200. for (seg = 0; seg < nr_segs; seg++) {
  1201. base = (unsigned long)iv[seg].iov_base + offset;
  1202. len = iv[seg].iov_len - offset;
  1203. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  1204. pages += size;
  1205. offset = 0;
  1206. }
  1207.  
  1208. return pages;
  1209. }
  1210.  
  1211. /* Get packet from user space buffer */
  1212. static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
  1213. void *msg_control, const struct iovec *iv,
  1214. size_t total_len, size_t count, int noblock)
  1215. {
  1216. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  1217. struct sk_buff *skb;
  1218. size_t len = total_len, align = tun->align, linear;
  1219. struct virtio_net_hdr gso = { 0 };
  1220. struct tun_pcpu_stats *stats;
  1221. int good_linear;
  1222. int offset = 0;
  1223. int copylen;
  1224. bool zerocopy = false;
  1225. int err;
  1226. u32 rxhash;
  1227.  
  1228. if (!(tun->flags & IFF_NO_PI)) {
  1229. if (len < sizeof(pi))
  1230. return -EINVAL;
  1231. len -= sizeof(pi);
  1232.  
  1233. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  1234. return -EFAULT;
  1235. offset += sizeof(pi);
  1236. }
  1237.  
  1238. if (tun->flags & IFF_VNET_HDR) {
  1239. if (len < tun->vnet_hdr_sz)
  1240. return -EINVAL;
  1241. len -= tun->vnet_hdr_sz;
  1242.  
  1243. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  1244. return -EFAULT;
  1245.  
  1246. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1247. tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
  1248. gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
  1249.  
  1250. if (tun16_to_cpu(tun, gso.hdr_len) > len)
  1251. return -EINVAL;
  1252. offset += tun->vnet_hdr_sz;
  1253. }
  1254.  
  1255. if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
  1256. align += NET_IP_ALIGN;
  1257. if (unlikely(len < ETH_HLEN ||
  1258. (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
  1259. return -EINVAL;
  1260. }
  1261.  
  1262. good_linear = SKB_MAX_HEAD(align);
  1263.  
  1264. if (msg_control) {
  1265. /* There are 256 bytes to be copied in skb, so there is
  1266. * enough room for skb expand head in case it is used.
  1267. * The rest of the buffer is mapped from userspace.
  1268. */
  1269. copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
  1270. if (copylen > good_linear)
  1271. copylen = good_linear;
  1272. linear = copylen;
  1273. if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
  1274. zerocopy = true;
  1275. }
  1276.  
  1277. if (!zerocopy) {
  1278. copylen = len;
  1279. if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
  1280. linear = good_linear;
  1281. else
  1282. linear = tun16_to_cpu(tun, gso.hdr_len);
  1283. }
  1284.  
  1285. skb = tun_alloc_skb(tfile, align, copylen, linear, noblock);
  1286. if (IS_ERR(skb)) {
  1287. if (PTR_ERR(skb) != -EAGAIN)
  1288. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1289. return PTR_ERR(skb);
  1290. }
  1291.  
  1292. if (zerocopy)
  1293. err = zerocopy_sg_from_iovec(skb, iv, offset, count);
  1294. else {
  1295. err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
  1296. if (!err && msg_control) {
  1297. struct ubuf_info *uarg = msg_control;
  1298. uarg->callback(uarg, false);
  1299. }
  1300. }
  1301.  
  1302. if (err) {
  1303. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1304. kfree_skb(skb);
  1305. return -EFAULT;
  1306. }
  1307.  
  1308. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  1309. if (!skb_partial_csum_set(skb, tun16_to_cpu(tun, gso.csum_start),
  1310. tun16_to_cpu(tun, gso.csum_offset))) {
  1311. this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
  1312. kfree_skb(skb);
  1313. return -EINVAL;
  1314. }
  1315. }
  1316.  
  1317. switch (tun->flags & TUN_TYPE_MASK) {
  1318. case IFF_TUN:
  1319. if (tun->flags & IFF_NO_PI) {
  1320. switch (skb->data[0] & 0xf0) {
  1321. case 0x40:
  1322. pi.proto = htons(ETH_P_IP);
  1323. break;
  1324. case 0x60:
  1325. pi.proto = htons(ETH_P_IPV6);
  1326. break;
  1327. default:
  1328. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1329. kfree_skb(skb);
  1330. return -EINVAL;
  1331. }
  1332. }
  1333.  
  1334. skb_reset_mac_header(skb);
  1335. skb->protocol = pi.proto;
  1336. skb->dev = tun->dev;
  1337. break;
  1338. case IFF_TAP:
  1339. skb->protocol = eth_type_trans(skb, tun->dev);
  1340. break;
  1341. }
  1342.  
  1343. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  1344. pr_debug("GSO!\n");
  1345. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  1346. case VIRTIO_NET_HDR_GSO_TCPV4:
  1347. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  1348. break;
  1349. case VIRTIO_NET_HDR_GSO_TCPV6:
  1350. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  1351. break;
  1352. case VIRTIO_NET_HDR_GSO_UDP:
  1353. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  1354. break;
  1355. default:
  1356. this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
  1357. kfree_skb(skb);
  1358. return -EINVAL;
  1359. }
  1360.  
  1361. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  1362. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  1363.  
  1364. skb_shinfo(skb)->gso_size = tun16_to_cpu(tun, gso.gso_size);
  1365. if (skb_shinfo(skb)->gso_size == 0) {
  1366. this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
  1367. kfree_skb(skb);
  1368. return -EINVAL;
  1369. }
  1370.  
  1371. /* Header must be checked, and gso_segs computed. */
  1372. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1373. skb_shinfo(skb)->gso_segs = 0;
  1374. }
  1375.  
  1376. /* copy skb_ubuf_info for callback when skb has no error */
  1377. if (zerocopy) {
  1378. skb_shinfo(skb)->destructor_arg = msg_control;
  1379. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1380. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  1381. }
  1382.  
  1383. skb_reset_network_header(skb);
  1384. skb_probe_transport_header(skb, 0);
  1385.  
  1386. rxhash = skb_get_hash(skb);
  1387. netif_rx_ni(skb);
  1388.  
  1389. stats = get_cpu_ptr(tun->pcpu_stats);
  1390. u64_stats_update_begin(&stats->syncp);
  1391. stats->rx_packets++;
  1392. stats->rx_bytes += len;
  1393. u64_stats_update_end(&stats->syncp);
  1394. put_cpu_ptr(stats);
  1395.  
  1396. tun_flow_update(tun, rxhash, tfile);
  1397. return total_len;
  1398. }
  1399.  
  1400. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  1401. unsigned long count, loff_t pos)
  1402. {
  1403. struct file *file = iocb->ki_filp;
  1404. struct tun_struct *tun = tun_get(file);
  1405. struct tun_file *tfile = file->private_data;
  1406. ssize_t result;
  1407.  
  1408. if (!tun)
  1409. return -EBADFD;
  1410.  
  1411. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  1412.  
  1413. result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
  1414. count, file->f_flags & O_NONBLOCK);
  1415.  
  1416. tun_put(tun);
  1417. return result;
  1418. }
  1419.  
  1420. /* Put packet to the user space buffer */
  1421. static ssize_t tun_put_user(struct tun_struct *tun,
  1422. struct tun_file *tfile,
  1423. struct sk_buff *skb,
  1424. const struct iovec *iv, int len)
  1425. {
  1426. struct tun_pi pi = { 0, skb->protocol };
  1427. struct tun_pcpu_stats *stats;
  1428. ssize_t total = 0;
  1429. int vlan_offset = 0, copied;
  1430. int vlan_hlen = 0;
  1431. int vnet_hdr_sz = 0;
  1432.  
  1433. if (skb_vlan_tag_present(skb))
  1434. vlan_hlen = VLAN_HLEN;
  1435.  
  1436. if (tun->flags & IFF_VNET_HDR)
  1437. vnet_hdr_sz = tun->vnet_hdr_sz;
  1438.  
  1439. if (!(tun->flags & IFF_NO_PI)) {
  1440. if ((len -= sizeof(pi)) < 0)
  1441. return -EINVAL;
  1442.  
  1443. if (len < skb->len + vlan_hlen + vnet_hdr_sz) {
  1444. /* Packet will be striped */
  1445. pi.flags |= TUN_PKT_STRIP;
  1446. }
  1447.  
  1448. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  1449. return -EFAULT;
  1450. total += sizeof(pi);
  1451. }
  1452.  
  1453. if (vnet_hdr_sz) {
  1454. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  1455. if ((len -= vnet_hdr_sz) < 0)
  1456. return -EINVAL;
  1457.  
  1458. if (skb_is_gso(skb)) {
  1459. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1460.  
  1461. /* This is a hint as to how much should be linear. */
  1462. gso.hdr_len = cpu_to_tun16(tun, skb_headlen(skb));
  1463. gso.gso_size = cpu_to_tun16(tun, sinfo->gso_size);
  1464. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1465. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1466. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1467. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1468. else if (sinfo->gso_type & SKB_GSO_UDP)
  1469. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1470. else {
  1471. pr_err("unexpected GSO type: "
  1472. "0x%x, gso_size %d, hdr_len %d\n",
  1473. sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
  1474. tun16_to_cpu(tun, gso.hdr_len));
  1475. print_hex_dump(KERN_ERR, "tun: ",
  1476. DUMP_PREFIX_NONE,
  1477. 16, 1, skb->head,
  1478. min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
  1479. WARN_ON_ONCE(1);
  1480. return -EINVAL;
  1481. }
  1482. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1483. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1484. } else
  1485. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1486.  
  1487. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1488. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1489. gso.csum_start = cpu_to_tun16(tun, skb_checksum_start_offset(skb) +
  1490. vlan_hlen);
  1491. gso.csum_offset = cpu_to_tun16(tun, skb->csum_offset);
  1492. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1493. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1494. } /* else everything is zero */
  1495.  
  1496. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  1497. sizeof(gso))))
  1498. return -EFAULT;
  1499. total += vnet_hdr_sz;
  1500. }
  1501.  
  1502. copied = total;
  1503. len = min_t(int, skb->len + vlan_hlen, len);
  1504. total += skb->len + vlan_hlen;
  1505. if (vlan_hlen) {
  1506. int copy, ret;
  1507. struct {
  1508. __be16 h_vlan_proto;
  1509. __be16 h_vlan_TCI;
  1510. } veth;
  1511.  
  1512. veth.h_vlan_proto = skb->vlan_proto;
  1513. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  1514.  
  1515. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  1516.  
  1517. copy = min_t(int, vlan_offset, len);
  1518. ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
  1519. len -= copy;
  1520. copied += copy;
  1521. if (ret || !len)
  1522. goto done;
  1523.  
  1524. copy = min_t(int, sizeof(veth), len);
  1525. ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
  1526. len -= copy;
  1527. copied += copy;
  1528. if (ret || !len)
  1529. goto done;
  1530. }
  1531.  
  1532. skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
  1533.  
  1534. done:
  1535. /* caller is in process context, */
  1536. stats = get_cpu_ptr(tun->pcpu_stats);
  1537. u64_stats_update_begin(&stats->syncp);
  1538. stats->tx_packets++;
  1539. stats->tx_bytes += skb->len + vlan_hlen;
  1540. u64_stats_update_end(&stats->syncp);
  1541. put_cpu_ptr(tun->pcpu_stats);
  1542.  
  1543. return total;
  1544. }
  1545.  
  1546. static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
  1547. struct kiocb *iocb, const struct iovec *iv,
  1548. ssize_t len, int noblock)
  1549. {
  1550. DECLARE_WAITQUEUE(wait, current);
  1551. struct sk_buff *skb;
  1552. ssize_t ret = 0;
  1553.  
  1554. tun_debug(KERN_INFO, tun, "tun_do_read\n");
  1555.  
  1556. if (unlikely(!noblock))
  1557. add_wait_queue(&tfile->wq.wait, &wait);
  1558. while (len) {
  1559. current->state = TASK_INTERRUPTIBLE;
  1560.  
  1561. /* Read frames from the queue */
  1562. if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
  1563. if (noblock) {
  1564. ret = -EAGAIN;
  1565. break;
  1566. }
  1567. if (signal_pending(current)) {
  1568. ret = -ERESTARTSYS;
  1569. break;
  1570. }
  1571. if (tun->dev->reg_state != NETREG_REGISTERED) {
  1572. ret = -EIO;
  1573. break;
  1574. }
  1575.  
  1576. /* Nothing to read, let's sleep */
  1577. schedule();
  1578. continue;
  1579. }
  1580.  
  1581. ret = tun_put_user(tun, tfile, skb, iv, len);
  1582. if (unlikely(ret < 0))
  1583. kfree_skb(skb);
  1584. else
  1585. consume_skb(skb);
  1586. break;
  1587. }
  1588.  
  1589. current->state = TASK_RUNNING;
  1590. if (unlikely(!noblock))
  1591. remove_wait_queue(&tfile->wq.wait, &wait);
  1592.  
  1593. return ret;
  1594. }
  1595.  
  1596. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  1597. unsigned long count, loff_t pos)
  1598. {
  1599. struct file *file = iocb->ki_filp;
  1600. struct tun_file *tfile = file->private_data;
  1601. struct tun_struct *tun = __tun_get(tfile);
  1602. ssize_t len, ret;
  1603.  
  1604. if (!tun)
  1605. return -EBADFD;
  1606. len = iov_length(iv, count);
  1607. if (len < 0) {
  1608. ret = -EINVAL;
  1609. goto out;
  1610. }
  1611.  
  1612. ret = tun_do_read(tun, tfile, iocb, iv, len,
  1613. file->f_flags & O_NONBLOCK);
  1614. ret = min_t(ssize_t, ret, len);
  1615. out:
  1616. tun_put(tun);
  1617. return ret;
  1618. }
  1619.  
  1620. static void tun_free_netdev(struct net_device *dev)
  1621. {
  1622. struct tun_struct *tun = netdev_priv(dev);
  1623.  
  1624. BUG_ON(!(list_empty(&tun->disabled)));
  1625. free_percpu(tun->pcpu_stats);
  1626. tun_flow_uninit(tun);
  1627. security_tun_dev_free_security(tun->security);
  1628. free_netdev(dev);
  1629. }
  1630.  
  1631. static void tun_setup(struct net_device *dev)
  1632. {
  1633. struct tun_struct *tun = netdev_priv(dev);
  1634.  
  1635. tun->owner = INVALID_UID;
  1636. tun->group = INVALID_GID;
  1637.  
  1638. dev->ethtool_ops = &tun_ethtool_ops;
  1639. dev->destructor = tun_free_netdev;
  1640. /* We prefer our own queue length */
  1641. dev->tx_queue_len = TUN_READQ_SIZE;
  1642. }
  1643.  
  1644. /* Trivial set of netlink ops to allow deleting tun or tap
  1645. * device with netlink.
  1646. */
  1647. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  1648. {
  1649. return -EINVAL;
  1650. }
  1651.  
  1652. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  1653. .kind = DRV_NAME,
  1654. .priv_size = sizeof(struct tun_struct),
  1655. .setup = tun_setup,
  1656. .validate = tun_validate,
  1657. };
  1658.  
  1659. static void tun_sock_write_space(struct sock *sk)
  1660. {
  1661. struct tun_file *tfile;
  1662. wait_queue_head_t *wqueue;
  1663.  
  1664. if (!sock_writeable(sk))
  1665. return;
  1666.  
  1667. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  1668. return;
  1669.  
  1670. wqueue = sk_sleep(sk);
  1671. if (wqueue && waitqueue_active(wqueue))
  1672. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  1673. POLLWRNORM | POLLWRBAND);
  1674.  
  1675. tfile = container_of(sk, struct tun_file, sk);
  1676. kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
  1677. }
  1678.  
  1679. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  1680. struct msghdr *m, size_t total_len)
  1681. {
  1682. int ret;
  1683. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1684. struct tun_struct *tun = __tun_get(tfile);
  1685.  
  1686. if (!tun)
  1687. return -EBADFD;
  1688. ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
  1689. m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
  1690. tun_put(tun);
  1691. return ret;
  1692. }
  1693.  
  1694.  
  1695. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  1696. struct msghdr *m, size_t total_len,
  1697. int flags)
  1698. {
  1699. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1700. struct tun_struct *tun = __tun_get(tfile);
  1701. int ret;
  1702.  
  1703. if (!tun)
  1704. return -EBADFD;
  1705.  
  1706. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
  1707. ret = -EINVAL;
  1708. goto out;
  1709. }
  1710. ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
  1711. flags & MSG_DONTWAIT);
  1712. if (ret > total_len) {
  1713. m->msg_flags |= MSG_TRUNC;
  1714. ret = flags & MSG_TRUNC ? ret : total_len;
  1715. }
  1716. out:
  1717. tun_put(tun);
  1718. return ret;
  1719. }
  1720.  
  1721. static int tun_release(struct socket *sock)
  1722. {
  1723. if (sock->sk)
  1724. sock_put(sock->sk);
  1725. return 0;
  1726. }
  1727.  
  1728. /* Ops structure to mimic raw sockets with tun */
  1729. static const struct proto_ops tun_socket_ops = {
  1730. .sendmsg = tun_sendmsg,
  1731. .recvmsg = tun_recvmsg,
  1732. .release = tun_release,
  1733. };
  1734.  
  1735. static struct proto tun_proto = {
  1736. .name = "tun",
  1737. .owner = THIS_MODULE,
  1738. .obj_size = sizeof(struct tun_file),
  1739. };
  1740.  
  1741. static int tun_flags(struct tun_struct *tun)
  1742. {
  1743. return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
  1744. }
  1745.  
  1746. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  1747. char *buf)
  1748. {
  1749. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1750. return sprintf(buf, "0x%x\n", tun_flags(tun));
  1751. }
  1752.  
  1753. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  1754. char *buf)
  1755. {
  1756. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1757. return uid_valid(tun->owner)?
  1758. sprintf(buf, "%u\n",
  1759. from_kuid_munged(current_user_ns(), tun->owner)):
  1760. sprintf(buf, "-1\n");
  1761. }
  1762.  
  1763. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  1764. char *buf)
  1765. {
  1766. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1767. return gid_valid(tun->group) ?
  1768. sprintf(buf, "%u\n",
  1769. from_kgid_munged(current_user_ns(), tun->group)):
  1770. sprintf(buf, "-1\n");
  1771. }
  1772.  
  1773. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  1774. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  1775. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  1776.  
  1777. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  1778. {
  1779. struct tun_struct *tun;
  1780. struct tun_file *tfile = file->private_data;
  1781. struct net_device *dev;
  1782. int err;
  1783.  
  1784. if (tfile->detached)
  1785. return -EINVAL;
  1786.  
  1787. dev = __dev_get_by_name(net, ifr->ifr_name);
  1788. if (dev) {
  1789. if (ifr->ifr_flags & IFF_TUN_EXCL)
  1790. return -EBUSY;
  1791. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  1792. tun = netdev_priv(dev);
  1793. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  1794. tun = netdev_priv(dev);
  1795. else
  1796. return -EINVAL;
  1797.  
  1798. if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
  1799. !!(tun->flags & IFF_MULTI_QUEUE))
  1800. return -EINVAL;
  1801.  
  1802. if (tun_not_capable(tun))
  1803. return -EPERM;
  1804. err = security_tun_dev_open(tun->security);
  1805. if (err < 0)
  1806. return err;
  1807.  
  1808. err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER);
  1809. if (err < 0)
  1810. return err;
  1811.  
  1812. if (tun->flags & IFF_MULTI_QUEUE &&
  1813. (tun->numqueues + tun->numdisabled > 1)) {
  1814. /* One or more queue has already been attached, no need
  1815. * to initialize the device again.
  1816. */
  1817. return 0;
  1818. }
  1819. }
  1820. else {
  1821. char *name;
  1822. unsigned long flags = 0;
  1823. int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
  1824. MAX_TAP_QUEUES : 1;
  1825.  
  1826. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1827. return -EPERM;
  1828. err = security_tun_dev_create();
  1829. if (err < 0)
  1830. return err;
  1831.  
  1832. /* Set dev type */
  1833. if (ifr->ifr_flags & IFF_TUN) {
  1834. /* TUN device */
  1835. flags |= IFF_TUN;
  1836. name = "tun%d";
  1837. } else if (ifr->ifr_flags & IFF_TAP) {
  1838. /* TAP device */
  1839. flags |= IFF_TAP;
  1840. name = "tap%d";
  1841. } else
  1842. return -EINVAL;
  1843.  
  1844. if (*ifr->ifr_name)
  1845. name = ifr->ifr_name;
  1846.  
  1847. dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
  1848. tun_setup, queues, queues);
  1849.  
  1850. if (!dev)
  1851. return -ENOMEM;
  1852.  
  1853. dev_net_set(dev, net);
  1854. dev->rtnl_link_ops = &tun_link_ops;
  1855. dev->ifindex = tfile->ifindex;
  1856.  
  1857. tun = netdev_priv(dev);
  1858. tun->dev = dev;
  1859. tun->flags = flags;
  1860. tun->txflt.count = 0;
  1861. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1862.  
  1863. tun->align = NET_SKB_PAD;
  1864. tun->filter_attached = false;
  1865. tun->sndbuf = tfile->socket.sk->sk_sndbuf;
  1866.  
  1867. tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
  1868. if (!tun->pcpu_stats) {
  1869. err = -ENOMEM;
  1870. goto err_free_dev;
  1871. }
  1872.  
  1873. spin_lock_init(&tun->lock);
  1874.  
  1875. err = security_tun_dev_alloc_security(&tun->security);
  1876. if (err < 0)
  1877. goto err_free_stat;
  1878.  
  1879. tun_net_init(dev);
  1880.  
  1881. err = tun_flow_init(tun);
  1882. if (err < 0)
  1883. goto err_free_dev;
  1884.  
  1885. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1886. TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
  1887. NETIF_F_HW_VLAN_STAG_TX;
  1888. dev->features = dev->hw_features | NETIF_F_LLTX;
  1889. dev->vlan_features = dev->features &
  1890. ~(NETIF_F_HW_VLAN_CTAG_TX |
  1891. NETIF_F_HW_VLAN_STAG_TX);
  1892.  
  1893. INIT_LIST_HEAD(&tun->disabled);
  1894. err = tun_attach(tun, file, false);
  1895. if (err < 0)
  1896. goto err_free_flow;
  1897.  
  1898. err = register_netdevice(tun->dev);
  1899. if (err < 0)
  1900. goto err_detach;
  1901.  
  1902. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  1903. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  1904. device_create_file(&tun->dev->dev, &dev_attr_group))
  1905. pr_err("Failed to create tun sysfs files\n");
  1906. }
  1907.  
  1908. netif_carrier_on(tun->dev);
  1909.  
  1910. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1911.  
  1912. tun->flags = (tun->flags & ~TUN_FEATURES) |
  1913. (ifr->ifr_flags & TUN_FEATURES);
  1914.  
  1915. /* Make sure persistent devices do not get stuck in
  1916. * xoff state.
  1917. */
  1918. if (netif_running(tun->dev))
  1919. netif_tx_wake_all_queues(tun->dev);
  1920.  
  1921. strcpy(ifr->ifr_name, tun->dev->name);
  1922. return 0;
  1923.  
  1924. err_detach:
  1925. tun_detach_all(dev);
  1926. err_free_flow:
  1927. tun_flow_uninit(tun);
  1928. security_tun_dev_free_security(tun->security);
  1929. err_free_stat:
  1930. free_percpu(tun->pcpu_stats);
  1931. err_free_dev:
  1932. free_netdev(dev);
  1933. return err;
  1934. }
  1935.  
  1936. static void tun_get_iff(struct net *net, struct tun_struct *tun,
  1937. struct ifreq *ifr)
  1938. {
  1939. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1940.  
  1941. strcpy(ifr->ifr_name, tun->dev->name);
  1942.  
  1943. ifr->ifr_flags = tun_flags(tun);
  1944.  
  1945. }
  1946.  
  1947. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1948. * privs required. */
  1949. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1950. {
  1951. netdev_features_t features = 0;
  1952.  
  1953. if (arg & TUN_F_CSUM) {
  1954. features |= NETIF_F_HW_CSUM;
  1955. arg &= ~TUN_F_CSUM;
  1956.  
  1957. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1958. if (arg & TUN_F_TSO_ECN) {
  1959. features |= NETIF_F_TSO_ECN;
  1960. arg &= ~TUN_F_TSO_ECN;
  1961. }
  1962. if (arg & TUN_F_TSO4)
  1963. features |= NETIF_F_TSO;
  1964. if (arg & TUN_F_TSO6)
  1965. features |= NETIF_F_TSO6;
  1966. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1967. }
  1968.  
  1969. if (arg & TUN_F_UFO) {
  1970. features |= NETIF_F_UFO;
  1971. arg &= ~TUN_F_UFO;
  1972. }
  1973. }
  1974.  
  1975. /* This gives the user a way to test for new features in future by
  1976. * trying to set them. */
  1977. if (arg)
  1978. return -EINVAL;
  1979.  
  1980. tun->set_features = features;
  1981. netdev_update_features(tun->dev);
  1982.  
  1983. return 0;
  1984. }
  1985.  
  1986. static void tun_detach_filter(struct tun_struct *tun, int n)
  1987. {
  1988. int i;
  1989. struct tun_file *tfile;
  1990.  
  1991. for (i = 0; i < n; i++) {
  1992. tfile = rtnl_dereference(tun->tfiles[i]);
  1993. sk_detach_filter(tfile->socket.sk);
  1994. }
  1995.  
  1996. tun->filter_attached = false;
  1997. }
  1998.  
  1999. static int tun_attach_filter(struct tun_struct *tun)
  2000. {
  2001. int i, ret = 0;
  2002. struct tun_file *tfile;
  2003.  
  2004. for (i = 0; i < tun->numqueues; i++) {
  2005. tfile = rtnl_dereference(tun->tfiles[i]);
  2006. ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  2007. if (ret) {
  2008. tun_detach_filter(tun, i);
  2009. return ret;
  2010. }
  2011. }
  2012.  
  2013. tun->filter_attached = true;
  2014. return ret;
  2015. }
  2016.  
  2017. static void tun_set_sndbuf(struct tun_struct *tun)
  2018. {
  2019. struct tun_file *tfile;
  2020. int i;
  2021.  
  2022. for (i = 0; i < tun->numqueues; i++) {
  2023. tfile = rtnl_dereference(tun->tfiles[i]);
  2024. tfile->socket.sk->sk_sndbuf = tun->sndbuf;
  2025. }
  2026. }
  2027.  
  2028. static int tun_set_queue(struct file *file, struct ifreq *ifr)
  2029. {
  2030. struct tun_file *tfile = file->private_data;
  2031. struct tun_struct *tun;
  2032. int ret = 0;
  2033.  
  2034. rtnl_lock();
  2035.  
  2036. if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
  2037. tun = tfile->detached;
  2038. if (!tun) {
  2039. ret = -EINVAL;
  2040. goto unlock;
  2041. }
  2042. ret = security_tun_dev_attach_queue(tun->security);
  2043. if (ret < 0)
  2044. goto unlock;
  2045. ret = tun_attach(tun, file, false);
  2046. } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
  2047. tun = rtnl_dereference(tfile->tun);
  2048. if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
  2049. ret = -EINVAL;
  2050. else
  2051. __tun_detach(tfile, false);
  2052. } else
  2053. ret = -EINVAL;
  2054.  
  2055. unlock:
  2056. rtnl_unlock();
  2057. return ret;
  2058. }
  2059.  
  2060. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  2061. unsigned long arg, int ifreq_len)
  2062. {
  2063. struct tun_file *tfile = file->private_data;
  2064. struct tun_struct *tun;
  2065. void __user* argp = (void __user*)arg;
  2066. struct ifreq ifr;
  2067. kuid_t owner;
  2068. kgid_t group;
  2069. int sndbuf;
  2070. int vnet_hdr_sz;
  2071. unsigned int ifindex;
  2072. int le;
  2073. int ret;
  2074.  
  2075. if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
  2076. if (copy_from_user(&ifr, argp, ifreq_len))
  2077. return -EFAULT;
  2078. } else {
  2079. memset(&ifr, 0, sizeof(ifr));
  2080. }
  2081. if (cmd == TUNGETFEATURES) {
  2082. /* Currently this just means: "what IFF flags are valid?".
  2083. * This is needed because we never checked for invalid flags on
  2084. * TUNSETIFF.
  2085. */
  2086. return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
  2087. (unsigned int __user*)argp);
  2088. } else if (cmd == TUNSETQUEUE)
  2089. return tun_set_queue(file, &ifr);
  2090.  
  2091. ret = 0;
  2092. rtnl_lock();
  2093.  
  2094. tun = __tun_get(tfile);
  2095. if (cmd == TUNSETIFF && !tun) {
  2096. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  2097.  
  2098. ret = tun_set_iff(tfile->net, file, &ifr);
  2099.  
  2100. if (ret)
  2101. goto unlock;
  2102.  
  2103. if (copy_to_user(argp, &ifr, ifreq_len))
  2104. ret = -EFAULT;
  2105. goto unlock;
  2106. }
  2107. if (cmd == TUNSETIFINDEX) {
  2108. ret = -EPERM;
  2109. if (tun)
  2110. goto unlock;
  2111.  
  2112. ret = -EFAULT;
  2113. if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
  2114. goto unlock;
  2115.  
  2116. ret = 0;
  2117. tfile->ifindex = ifindex;
  2118. goto unlock;
  2119. }
  2120.  
  2121. ret = -EBADFD;
  2122. if (!tun)
  2123. goto unlock;
  2124.  
  2125. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
  2126.  
  2127. ret = 0;
  2128. switch (cmd) {
  2129. case TUNGETIFF:
  2130. tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  2131.  
  2132. if (tfile->detached)
  2133. ifr.ifr_flags |= IFF_DETACH_QUEUE;
  2134. if (!tfile->socket.sk->sk_filter)
  2135. ifr.ifr_flags |= IFF_NOFILTER;
  2136.  
  2137. if (copy_to_user(argp, &ifr, ifreq_len))
  2138. ret = -EFAULT;
  2139. break;
  2140.  
  2141. case TUNSETNOCSUM:
  2142. /* Disable/Enable checksum */
  2143.  
  2144. /* [unimplemented] */
  2145. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  2146. arg ? "disabled" : "enabled");
  2147. break;
  2148.  
  2149. case TUNSETPERSIST:
  2150. /* Disable/Enable persist mode. Keep an extra reference to the
  2151. * module to prevent the module being unprobed.
  2152. */
  2153. if (arg && !(tun->flags & IFF_PERSIST)) {
  2154. tun->flags |= IFF_PERSIST;
  2155. __module_get(THIS_MODULE);
  2156. }
  2157. if (!arg && (tun->flags & IFF_PERSIST)) {
  2158. tun->flags &= ~IFF_PERSIST;
  2159. module_put(THIS_MODULE);
  2160. }
  2161.  
  2162. tun_debug(KERN_INFO, tun, "persist %s\n",
  2163. arg ? "enabled" : "disabled");
  2164. break;
  2165.  
  2166. case TUNSETOWNER:
  2167. /* Set owner of the device */
  2168. owner = make_kuid(current_user_ns(), arg);
  2169. if (!uid_valid(owner)) {
  2170. ret = -EINVAL;
  2171. break;
  2172. }
  2173. tun->owner = owner;
  2174. tun_debug(KERN_INFO, tun, "owner set to %u\n",
  2175. from_kuid(&init_user_ns, tun->owner));
  2176. break;
  2177.  
  2178. case TUNSETGROUP:
  2179. /* Set group of the device */
  2180. group = make_kgid(current_user_ns(), arg);
  2181. if (!gid_valid(group)) {
  2182. ret = -EINVAL;
  2183. break;
  2184. }
  2185. tun->group = group;
  2186. tun_debug(KERN_INFO, tun, "group set to %u\n",
  2187. from_kgid(&init_user_ns, tun->group));
  2188. break;
  2189.  
  2190. case TUNSETLINK:
  2191. /* Only allow setting the type when the interface is down */
  2192. if (tun->dev->flags & IFF_UP) {
  2193. tun_debug(KERN_INFO, tun,
  2194. "Linktype set failed because interface is up\n");
  2195. ret = -EBUSY;
  2196. } else {
  2197. tun->dev->type = (int) arg;
  2198. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  2199. tun->dev->type);
  2200. ret = 0;
  2201. }
  2202. break;
  2203.  
  2204. #ifdef TUN_DEBUG
  2205. case TUNSETDEBUG:
  2206. tun->debug = arg;
  2207. break;
  2208. #endif
  2209. case TUNSETOFFLOAD:
  2210. ret = set_offload(tun, arg);
  2211. break;
  2212.  
  2213. case TUNSETTXFILTER:
  2214. /* Can be set only for TAPs */
  2215. ret = -EINVAL;
  2216. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2217. break;
  2218. ret = update_filter(&tun->txflt, (void __user *)arg);
  2219. break;
  2220.  
  2221. case SIOCGIFHWADDR:
  2222. /* Get hw address */
  2223. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  2224. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  2225. if (copy_to_user(argp, &ifr, ifreq_len))
  2226. ret = -EFAULT;
  2227. break;
  2228.  
  2229. case SIOCSIFHWADDR:
  2230. /* Set hw address */
  2231. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  2232. ifr.ifr_hwaddr.sa_data);
  2233.  
  2234. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  2235. break;
  2236.  
  2237. case TUNGETSNDBUF:
  2238. sndbuf = tfile->socket.sk->sk_sndbuf;
  2239. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  2240. ret = -EFAULT;
  2241. break;
  2242.  
  2243. case TUNSETSNDBUF:
  2244. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  2245. ret = -EFAULT;
  2246. break;
  2247. }
  2248.  
  2249. tun->sndbuf = sndbuf;
  2250. tun_set_sndbuf(tun);
  2251. break;
  2252.  
  2253. case TUNGETVNETHDRSZ:
  2254. vnet_hdr_sz = tun->vnet_hdr_sz;
  2255. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  2256. ret = -EFAULT;
  2257. break;
  2258.  
  2259. case TUNSETVNETHDRSZ:
  2260. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  2261. ret = -EFAULT;
  2262. break;
  2263. }
  2264. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  2265. ret = -EINVAL;
  2266. break;
  2267. }
  2268.  
  2269. tun->vnet_hdr_sz = vnet_hdr_sz;
  2270. break;
  2271.  
  2272. case TUNGETVNETLE:
  2273. le = !!(tun->flags & TUN_VNET_LE);
  2274. if (put_user(le, (int __user *)argp))
  2275. ret = -EFAULT;
  2276. break;
  2277.  
  2278. case TUNSETVNETLE:
  2279. if (get_user(le, (int __user *)argp)) {
  2280. ret = -EFAULT;
  2281. break;
  2282. }
  2283. if (le)
  2284. tun->flags |= TUN_VNET_LE;
  2285. else
  2286. tun->flags &= ~TUN_VNET_LE;
  2287. break;
  2288.  
  2289. case TUNGETVNETBE:
  2290. ret = tun_get_vnet_be(tun, argp);
  2291. break;
  2292.  
  2293. case TUNSETVNETBE:
  2294. ret = tun_set_vnet_be(tun, argp);
  2295. break;
  2296.  
  2297. case TUNATTACHFILTER:
  2298. /* Can be set only for TAPs */
  2299. ret = -EINVAL;
  2300. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2301. break;
  2302. ret = -EFAULT;
  2303. if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
  2304. break;
  2305.  
  2306. ret = tun_attach_filter(tun);
  2307. break;
  2308.  
  2309. case TUNDETACHFILTER:
  2310. /* Can be set only for TAPs */
  2311. ret = -EINVAL;
  2312. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2313. break;
  2314. ret = 0;
  2315. tun_detach_filter(tun, tun->numqueues);
  2316. break;
  2317.  
  2318. case TUNGETFILTER:
  2319. ret = -EINVAL;
  2320. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  2321. break;
  2322. ret = -EFAULT;
  2323. if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
  2324. break;
  2325. ret = 0;
  2326. break;
  2327.  
  2328. default:
  2329. ret = -EINVAL;
  2330. break;
  2331. }
  2332.  
  2333. unlock:
  2334. rtnl_unlock();
  2335. if (tun)
  2336. tun_put(tun);
  2337. return ret;
  2338. }
  2339.  
  2340. static long tun_chr_ioctl(struct file *file,
  2341. unsigned int cmd, unsigned long arg)
  2342. {
  2343. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  2344. }
  2345.  
  2346. #ifdef CONFIG_COMPAT
  2347. static long tun_chr_compat_ioctl(struct file *file,
  2348. unsigned int cmd, unsigned long arg)
  2349. {
  2350. switch (cmd) {
  2351. case TUNSETIFF:
  2352. case TUNGETIFF:
  2353. case TUNSETTXFILTER:
  2354. case TUNGETSNDBUF:
  2355. case TUNSETSNDBUF:
  2356. case SIOCGIFHWADDR:
  2357. case SIOCSIFHWADDR:
  2358. arg = (unsigned long)compat_ptr(arg);
  2359. break;
  2360. default:
  2361. arg = (compat_ulong_t)arg;
  2362. break;
  2363. }
  2364.  
  2365. /*
  2366. * compat_ifreq is shorter than ifreq, so we must not access beyond
  2367. * the end of that structure. All fields that are used in this
  2368. * driver are compatible though, we don't need to convert the
  2369. * contents.
  2370. */
  2371. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  2372. }
  2373. #endif /* CONFIG_COMPAT */
  2374.  
  2375. static int tun_chr_fasync(int fd, struct file *file, int on)
  2376. {
  2377. struct tun_file *tfile = file->private_data;
  2378. int ret;
  2379.  
  2380. if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
  2381. goto out;
  2382.  
  2383. if (on) {
  2384. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  2385. if (ret)
  2386. goto out;
  2387. tfile->flags |= TUN_FASYNC;
  2388. } else
  2389. tfile->flags &= ~TUN_FASYNC;
  2390. ret = 0;
  2391. out:
  2392. return ret;
  2393. }
  2394.  
  2395. static int tun_chr_open(struct inode *inode, struct file * file)
  2396. {
  2397. struct tun_file *tfile;
  2398.  
  2399. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  2400.  
  2401. tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
  2402. &tun_proto);
  2403. if (!tfile)
  2404. return -ENOMEM;
  2405. rcu_assign_pointer(tfile->tun, NULL);
  2406. tfile->net = get_net(current->nsproxy->net_ns);
  2407. tfile->flags = 0;
  2408. tfile->ifindex = 0;
  2409.  
  2410. rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
  2411. init_waitqueue_head(&tfile->wq.wait);
  2412.  
  2413. tfile->socket.file = file;
  2414. tfile->socket.ops = &tun_socket_ops;
  2415.  
  2416. sock_init_data(&tfile->socket, &tfile->sk);
  2417. sk_change_net(&tfile->sk, tfile->net);
  2418.  
  2419. tfile->sk.sk_write_space = tun_sock_write_space;
  2420. tfile->sk.sk_sndbuf = INT_MAX;
  2421.  
  2422. file->private_data = tfile;
  2423. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
  2424. INIT_LIST_HEAD(&tfile->next);
  2425.  
  2426. sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
  2427.  
  2428. return 0;
  2429. }
  2430.  
  2431. static int tun_chr_close(struct inode *inode, struct file *file)
  2432. {
  2433. struct tun_file *tfile = file->private_data;
  2434. struct net *net = tfile->net;
  2435.  
  2436. tun_detach(tfile, true);
  2437. put_net(net);
  2438.  
  2439. return 0;
  2440. }
  2441.  
  2442. static const struct file_operations tun_fops = {
  2443. .owner = THIS_MODULE,
  2444. .llseek = no_llseek,
  2445. .read = do_sync_read,
  2446. .aio_read = tun_chr_aio_read,
  2447. .write = do_sync_write,
  2448. .aio_write = tun_chr_aio_write,
  2449. .poll = tun_chr_poll,
  2450. .unlocked_ioctl = tun_chr_ioctl,
  2451. #ifdef CONFIG_COMPAT
  2452. .compat_ioctl = tun_chr_compat_ioctl,
  2453. #endif
  2454. .open = tun_chr_open,
  2455. .release = tun_chr_close,
  2456. .fasync = tun_chr_fasync
  2457. };
  2458.  
  2459. static struct miscdevice tun_miscdev = {
  2460. .minor = TUN_MINOR,
  2461. .name = "tun",
  2462. .nodename = "net/tun",
  2463. .fops = &tun_fops,
  2464. };
  2465.  
  2466. /* ethtool interface */
  2467.  
  2468. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2469. {
  2470. cmd->supported = 0;
  2471. cmd->advertising = 0;
  2472. ethtool_cmd_speed_set(cmd, SPEED_10);
  2473. cmd->duplex = DUPLEX_FULL;
  2474. cmd->port = PORT_TP;
  2475. cmd->phy_address = 0;
  2476. cmd->transceiver = XCVR_INTERNAL;
  2477. cmd->autoneg = AUTONEG_DISABLE;
  2478. cmd->maxtxpkt = 0;
  2479. cmd->maxrxpkt = 0;
  2480. return 0;
  2481. }
  2482.  
  2483. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  2484. {
  2485. struct tun_struct *tun = netdev_priv(dev);
  2486.  
  2487. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  2488. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  2489.  
  2490. switch (tun->flags & TUN_TYPE_MASK) {
  2491. case IFF_TUN:
  2492. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  2493. break;
  2494. case IFF_TAP:
  2495. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  2496. break;
  2497. }
  2498. }
  2499.  
  2500. static u32 tun_get_msglevel(struct net_device *dev)
  2501. {
  2502. #ifdef TUN_DEBUG
  2503. struct tun_struct *tun = netdev_priv(dev);
  2504. return tun->debug;
  2505. #else
  2506. return -EOPNOTSUPP;
  2507. #endif
  2508. }
  2509.  
  2510. static void tun_set_msglevel(struct net_device *dev, u32 value)
  2511. {
  2512. #ifdef TUN_DEBUG
  2513. struct tun_struct *tun = netdev_priv(dev);
  2514. tun->debug = value;
  2515. #endif
  2516. }
  2517.  
  2518. static const struct ethtool_ops tun_ethtool_ops = {
  2519. .get_settings = tun_get_settings,
  2520. .get_drvinfo = tun_get_drvinfo,
  2521. .get_msglevel = tun_get_msglevel,
  2522. .set_msglevel = tun_set_msglevel,
  2523. .get_link = ethtool_op_get_link,
  2524. };
  2525.  
  2526.  
  2527. static int __init tun_init(void)
  2528. {
  2529. int ret = 0;
  2530.  
  2531. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  2532. pr_info("%s\n", DRV_COPYRIGHT);
  2533.  
  2534. ret = rtnl_link_register(&tun_link_ops);
  2535. if (ret) {
  2536. pr_err("Can't register link_ops\n");
  2537. goto err_linkops;
  2538. }
  2539.  
  2540. ret = misc_register(&tun_miscdev);
  2541. if (ret) {
  2542. pr_err("Can't register misc device %d\n", TUN_MINOR);
  2543. goto err_misc;
  2544. }
  2545. return 0;
  2546. err_misc:
  2547. rtnl_link_unregister(&tun_link_ops);
  2548. err_linkops:
  2549. return ret;
  2550. }
  2551.  
  2552. static void tun_cleanup(void)
  2553. {
  2554. misc_deregister(&tun_miscdev);
  2555. rtnl_link_unregister(&tun_link_ops);
  2556. }
  2557.  
  2558. /* Get an underlying socket object from tun file. Returns error unless file is
  2559. * attached to a device. The returned object works like a packet socket, it
  2560. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  2561. * holding a reference to the file for as long as the socket is in use. */
  2562. struct socket *tun_get_socket(struct file *file)
  2563. {
  2564. struct tun_file *tfile;
  2565. if (file->f_op != &tun_fops)
  2566. return ERR_PTR(-EINVAL);
  2567. tfile = file->private_data;
  2568. if (!tfile)
  2569. return ERR_PTR(-EBADFD);
  2570. return &tfile->socket;
  2571. }
  2572. EXPORT_SYMBOL_GPL(tun_get_socket);
  2573.  
  2574. module_init(tun_init);
  2575. module_exit(tun_cleanup);
  2576. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  2577. MODULE_AUTHOR(DRV_COPYRIGHT);
  2578. MODULE_LICENSE("GPL");
  2579. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  2580. MODULE_ALIAS("devname:net/tun");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement