tap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_tap.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/compat.h>
  7. #include <linux/if_tun.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/cache.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/cdev.h>
  16. #include <linux/idr.h>
  17. #include <linux/fs.h>
  18. #include <linux/uio.h>
  19. #include <net/net_namespace.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <linux/virtio_net.h>
  23. #include <linux/skb_array.h>
  24. #define TAP_IFFEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
  25. #define TAP_VNET_LE 0x80000000
  26. #define TAP_VNET_BE 0x40000000
  27. #ifdef CONFIG_TUN_VNET_CROSS_LE
  28. static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
  29. {
  30. return q->flags & TAP_VNET_BE ? false :
  31. virtio_legacy_is_little_endian();
  32. }
  33. static long tap_get_vnet_be(struct tap_queue *q, int __user *sp)
  34. {
  35. int s = !!(q->flags & TAP_VNET_BE);
  36. if (put_user(s, sp))
  37. return -EFAULT;
  38. return 0;
  39. }
  40. static long tap_set_vnet_be(struct tap_queue *q, int __user *sp)
  41. {
  42. int s;
  43. if (get_user(s, sp))
  44. return -EFAULT;
  45. if (s)
  46. q->flags |= TAP_VNET_BE;
  47. else
  48. q->flags &= ~TAP_VNET_BE;
  49. return 0;
  50. }
  51. #else
  52. static inline bool tap_legacy_is_little_endian(struct tap_queue *q)
  53. {
  54. return virtio_legacy_is_little_endian();
  55. }
  56. static long tap_get_vnet_be(struct tap_queue *q, int __user *argp)
  57. {
  58. return -EINVAL;
  59. }
  60. static long tap_set_vnet_be(struct tap_queue *q, int __user *argp)
  61. {
  62. return -EINVAL;
  63. }
  64. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  65. static inline bool tap_is_little_endian(struct tap_queue *q)
  66. {
  67. return q->flags & TAP_VNET_LE ||
  68. tap_legacy_is_little_endian(q);
  69. }
  70. static inline u16 tap16_to_cpu(struct tap_queue *q, __virtio16 val)
  71. {
  72. return __virtio16_to_cpu(tap_is_little_endian(q), val);
  73. }
  74. static inline __virtio16 cpu_to_tap16(struct tap_queue *q, u16 val)
  75. {
  76. return __cpu_to_virtio16(tap_is_little_endian(q), val);
  77. }
  78. static struct proto tap_proto = {
  79. .name = "tap",
  80. .owner = THIS_MODULE,
  81. .obj_size = sizeof(struct tap_queue),
  82. };
  83. #define TAP_NUM_DEVS (1U << MINORBITS)
  84. static LIST_HEAD(major_list);
  85. struct major_info {
  86. struct rcu_head rcu;
  87. dev_t major;
  88. struct idr minor_idr;
  89. spinlock_t minor_lock;
  90. const char *device_name;
  91. struct list_head next;
  92. };
  93. #define GOODCOPY_LEN 128
  94. static const struct proto_ops tap_socket_ops;
  95. #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
  96. #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG | NETIF_F_FRAGLIST)
  97. static struct tap_dev *tap_dev_get_rcu(const struct net_device *dev)
  98. {
  99. return rcu_dereference(dev->rx_handler_data);
  100. }
  101. /*
  102. * RCU usage:
  103. * The tap_queue and the macvlan_dev are loosely coupled, the
  104. * pointers from one to the other can only be read while rcu_read_lock
  105. * or rtnl is held.
  106. *
  107. * Both the file and the macvlan_dev hold a reference on the tap_queue
  108. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  109. * q->vlan becomes inaccessible. When the files gets closed,
  110. * tap_get_queue() fails.
  111. *
  112. * There may still be references to the struct sock inside of the
  113. * queue from outbound SKBs, but these never reference back to the
  114. * file or the dev. The data structure is freed through __sk_free
  115. * when both our references and any pending SKBs are gone.
  116. */
  117. static int tap_enable_queue(struct tap_dev *tap, struct file *file,
  118. struct tap_queue *q)
  119. {
  120. int err = -EINVAL;
  121. ASSERT_RTNL();
  122. if (q->enabled)
  123. goto out;
  124. err = 0;
  125. rcu_assign_pointer(tap->taps[tap->numvtaps], q);
  126. q->queue_index = tap->numvtaps;
  127. q->enabled = true;
  128. tap->numvtaps++;
  129. out:
  130. return err;
  131. }
  132. /* Requires RTNL */
  133. static int tap_set_queue(struct tap_dev *tap, struct file *file,
  134. struct tap_queue *q)
  135. {
  136. if (tap->numqueues == MAX_TAP_QUEUES)
  137. return -EBUSY;
  138. rcu_assign_pointer(q->tap, tap);
  139. rcu_assign_pointer(tap->taps[tap->numvtaps], q);
  140. sock_hold(&q->sk);
  141. q->file = file;
  142. q->queue_index = tap->numvtaps;
  143. q->enabled = true;
  144. file->private_data = q;
  145. list_add_tail(&q->next, &tap->queue_list);
  146. tap->numvtaps++;
  147. tap->numqueues++;
  148. return 0;
  149. }
  150. static int tap_disable_queue(struct tap_queue *q)
  151. {
  152. struct tap_dev *tap;
  153. struct tap_queue *nq;
  154. ASSERT_RTNL();
  155. if (!q->enabled)
  156. return -EINVAL;
  157. tap = rtnl_dereference(q->tap);
  158. if (tap) {
  159. int index = q->queue_index;
  160. BUG_ON(index >= tap->numvtaps);
  161. nq = rtnl_dereference(tap->taps[tap->numvtaps - 1]);
  162. nq->queue_index = index;
  163. rcu_assign_pointer(tap->taps[index], nq);
  164. RCU_INIT_POINTER(tap->taps[tap->numvtaps - 1], NULL);
  165. q->enabled = false;
  166. tap->numvtaps--;
  167. }
  168. return 0;
  169. }
  170. /*
  171. * The file owning the queue got closed, give up both
  172. * the reference that the files holds as well as the
  173. * one from the macvlan_dev if that still exists.
  174. *
  175. * Using the spinlock makes sure that we don't get
  176. * to the queue again after destroying it.
  177. */
  178. static void tap_put_queue(struct tap_queue *q)
  179. {
  180. struct tap_dev *tap;
  181. rtnl_lock();
  182. tap = rtnl_dereference(q->tap);
  183. if (tap) {
  184. if (q->enabled)
  185. BUG_ON(tap_disable_queue(q));
  186. tap->numqueues--;
  187. RCU_INIT_POINTER(q->tap, NULL);
  188. sock_put(&q->sk);
  189. list_del_init(&q->next);
  190. }
  191. rtnl_unlock();
  192. synchronize_rcu();
  193. sock_put(&q->sk);
  194. }
  195. /*
  196. * Select a queue based on the rxq of the device on which this packet
  197. * arrived. If the incoming device is not mq, calculate a flow hash
  198. * to select a queue. If all fails, find the first available queue.
  199. * Cache vlan->numvtaps since it can become zero during the execution
  200. * of this function.
  201. */
  202. static struct tap_queue *tap_get_queue(struct tap_dev *tap,
  203. struct sk_buff *skb)
  204. {
  205. struct tap_queue *queue = NULL;
  206. /* Access to taps array is protected by rcu, but access to numvtaps
  207. * isn't. Below we use it to lookup a queue, but treat it as a hint
  208. * and validate that the result isn't NULL - in case we are
  209. * racing against queue removal.
  210. */
  211. int numvtaps = READ_ONCE(tap->numvtaps);
  212. __u32 rxq;
  213. if (!numvtaps)
  214. goto out;
  215. if (numvtaps == 1)
  216. goto single;
  217. /* Check if we can use flow to select a queue */
  218. rxq = skb_get_hash(skb);
  219. if (rxq) {
  220. queue = rcu_dereference(tap->taps[rxq % numvtaps]);
  221. goto out;
  222. }
  223. if (likely(skb_rx_queue_recorded(skb))) {
  224. rxq = skb_get_rx_queue(skb);
  225. while (unlikely(rxq >= numvtaps))
  226. rxq -= numvtaps;
  227. queue = rcu_dereference(tap->taps[rxq]);
  228. goto out;
  229. }
  230. single:
  231. queue = rcu_dereference(tap->taps[0]);
  232. out:
  233. return queue;
  234. }
  235. /*
  236. * The net_device is going away, give up the reference
  237. * that it holds on all queues and safely set the pointer
  238. * from the queues to NULL.
  239. */
  240. void tap_del_queues(struct tap_dev *tap)
  241. {
  242. struct tap_queue *q, *tmp;
  243. ASSERT_RTNL();
  244. list_for_each_entry_safe(q, tmp, &tap->queue_list, next) {
  245. list_del_init(&q->next);
  246. RCU_INIT_POINTER(q->tap, NULL);
  247. if (q->enabled)
  248. tap->numvtaps--;
  249. tap->numqueues--;
  250. sock_put(&q->sk);
  251. }
  252. BUG_ON(tap->numvtaps);
  253. BUG_ON(tap->numqueues);
  254. /* guarantee that any future tap_set_queue will fail */
  255. tap->numvtaps = MAX_TAP_QUEUES;
  256. }
  257. EXPORT_SYMBOL_GPL(tap_del_queues);
  258. rx_handler_result_t tap_handle_frame(struct sk_buff **pskb)
  259. {
  260. struct sk_buff *skb = *pskb;
  261. struct net_device *dev = skb->dev;
  262. struct tap_dev *tap;
  263. struct tap_queue *q;
  264. netdev_features_t features = TAP_FEATURES;
  265. tap = tap_dev_get_rcu(dev);
  266. if (!tap)
  267. return RX_HANDLER_PASS;
  268. q = tap_get_queue(tap, skb);
  269. if (!q)
  270. return RX_HANDLER_PASS;
  271. skb_push(skb, ETH_HLEN);
  272. /* Apply the forward feature mask so that we perform segmentation
  273. * according to users wishes. This only works if VNET_HDR is
  274. * enabled.
  275. */
  276. if (q->flags & IFF_VNET_HDR)
  277. features |= tap->tap_features;
  278. if (netif_needs_gso(skb, features)) {
  279. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  280. if (IS_ERR(segs))
  281. goto drop;
  282. if (!segs) {
  283. if (ptr_ring_produce(&q->ring, skb))
  284. goto drop;
  285. goto wake_up;
  286. }
  287. consume_skb(skb);
  288. while (segs) {
  289. struct sk_buff *nskb = segs->next;
  290. segs->next = NULL;
  291. if (ptr_ring_produce(&q->ring, segs)) {
  292. kfree_skb(segs);
  293. kfree_skb_list(nskb);
  294. break;
  295. }
  296. segs = nskb;
  297. }
  298. } else {
  299. /* If we receive a partial checksum and the tap side
  300. * doesn't support checksum offload, compute the checksum.
  301. * Note: it doesn't matter which checksum feature to
  302. * check, we either support them all or none.
  303. */
  304. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  305. !(features & NETIF_F_CSUM_MASK) &&
  306. skb_checksum_help(skb))
  307. goto drop;
  308. if (ptr_ring_produce(&q->ring, skb))
  309. goto drop;
  310. }
  311. wake_up:
  312. wake_up_interruptible_poll(sk_sleep(&q->sk), EPOLLIN | EPOLLRDNORM | EPOLLRDBAND);
  313. return RX_HANDLER_CONSUMED;
  314. drop:
  315. /* Count errors/drops only here, thus don't care about args. */
  316. if (tap->count_rx_dropped)
  317. tap->count_rx_dropped(tap);
  318. kfree_skb(skb);
  319. return RX_HANDLER_CONSUMED;
  320. }
  321. EXPORT_SYMBOL_GPL(tap_handle_frame);
  322. static struct major_info *tap_get_major(int major)
  323. {
  324. struct major_info *tap_major;
  325. list_for_each_entry_rcu(tap_major, &major_list, next) {
  326. if (tap_major->major == major)
  327. return tap_major;
  328. }
  329. return NULL;
  330. }
  331. int tap_get_minor(dev_t major, struct tap_dev *tap)
  332. {
  333. int retval = -ENOMEM;
  334. struct major_info *tap_major;
  335. rcu_read_lock();
  336. tap_major = tap_get_major(MAJOR(major));
  337. if (!tap_major) {
  338. retval = -EINVAL;
  339. goto unlock;
  340. }
  341. spin_lock(&tap_major->minor_lock);
  342. retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
  343. if (retval >= 0) {
  344. tap->minor = retval;
  345. } else if (retval == -ENOSPC) {
  346. netdev_err(tap->dev, "Too many tap devices\n");
  347. retval = -EINVAL;
  348. }
  349. spin_unlock(&tap_major->minor_lock);
  350. unlock:
  351. rcu_read_unlock();
  352. return retval < 0 ? retval : 0;
  353. }
  354. EXPORT_SYMBOL_GPL(tap_get_minor);
  355. void tap_free_minor(dev_t major, struct tap_dev *tap)
  356. {
  357. struct major_info *tap_major;
  358. rcu_read_lock();
  359. tap_major = tap_get_major(MAJOR(major));
  360. if (!tap_major) {
  361. goto unlock;
  362. }
  363. spin_lock(&tap_major->minor_lock);
  364. if (tap->minor) {
  365. idr_remove(&tap_major->minor_idr, tap->minor);
  366. tap->minor = 0;
  367. }
  368. spin_unlock(&tap_major->minor_lock);
  369. unlock:
  370. rcu_read_unlock();
  371. }
  372. EXPORT_SYMBOL_GPL(tap_free_minor);
  373. static struct tap_dev *dev_get_by_tap_file(int major, int minor)
  374. {
  375. struct net_device *dev = NULL;
  376. struct tap_dev *tap;
  377. struct major_info *tap_major;
  378. rcu_read_lock();
  379. tap_major = tap_get_major(major);
  380. if (!tap_major) {
  381. tap = NULL;
  382. goto unlock;
  383. }
  384. spin_lock(&tap_major->minor_lock);
  385. tap = idr_find(&tap_major->minor_idr, minor);
  386. if (tap) {
  387. dev = tap->dev;
  388. dev_hold(dev);
  389. }
  390. spin_unlock(&tap_major->minor_lock);
  391. unlock:
  392. rcu_read_unlock();
  393. return tap;
  394. }
  395. static void tap_sock_write_space(struct sock *sk)
  396. {
  397. wait_queue_head_t *wqueue;
  398. if (!sock_writeable(sk) ||
  399. !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  400. return;
  401. wqueue = sk_sleep(sk);
  402. if (wqueue && waitqueue_active(wqueue))
  403. wake_up_interruptible_poll(wqueue, EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND);
  404. }
  405. static void tap_sock_destruct(struct sock *sk)
  406. {
  407. struct tap_queue *q = container_of(sk, struct tap_queue, sk);
  408. ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
  409. }
  410. static int tap_open(struct inode *inode, struct file *file)
  411. {
  412. struct net *net = current->nsproxy->net_ns;
  413. struct tap_dev *tap;
  414. struct tap_queue *q;
  415. int err = -ENODEV;
  416. rtnl_lock();
  417. tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
  418. if (!tap)
  419. goto err;
  420. err = -ENOMEM;
  421. q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  422. &tap_proto, 0);
  423. if (!q)
  424. goto err;
  425. if (ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL)) {
  426. sk_free(&q->sk);
  427. goto err;
  428. }
  429. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  430. init_waitqueue_head(&q->wq.wait);
  431. q->sock.type = SOCK_RAW;
  432. q->sock.state = SS_CONNECTED;
  433. q->sock.file = file;
  434. q->sock.ops = &tap_socket_ops;
  435. sock_init_data(&q->sock, &q->sk);
  436. q->sk.sk_write_space = tap_sock_write_space;
  437. q->sk.sk_destruct = tap_sock_destruct;
  438. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  439. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  440. /*
  441. * so far only KVM virtio_net uses tap, enable zero copy between
  442. * guest kernel and host kernel when lower device supports zerocopy
  443. *
  444. * The macvlan supports zerocopy iff the lower device supports zero
  445. * copy so we don't have to look at the lower device directly.
  446. */
  447. if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
  448. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  449. err = tap_set_queue(tap, file, q);
  450. if (err) {
  451. /* tap_sock_destruct() will take care of freeing ptr_ring */
  452. goto err_put;
  453. }
  454. dev_put(tap->dev);
  455. rtnl_unlock();
  456. return err;
  457. err_put:
  458. sock_put(&q->sk);
  459. err:
  460. if (tap)
  461. dev_put(tap->dev);
  462. rtnl_unlock();
  463. return err;
  464. }
  465. static int tap_release(struct inode *inode, struct file *file)
  466. {
  467. struct tap_queue *q = file->private_data;
  468. tap_put_queue(q);
  469. return 0;
  470. }
  471. static __poll_t tap_poll(struct file *file, poll_table *wait)
  472. {
  473. struct tap_queue *q = file->private_data;
  474. __poll_t mask = EPOLLERR;
  475. if (!q)
  476. goto out;
  477. mask = 0;
  478. poll_wait(file, &q->wq.wait, wait);
  479. if (!ptr_ring_empty(&q->ring))
  480. mask |= EPOLLIN | EPOLLRDNORM;
  481. if (sock_writeable(&q->sk) ||
  482. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
  483. sock_writeable(&q->sk)))
  484. mask |= EPOLLOUT | EPOLLWRNORM;
  485. out:
  486. return mask;
  487. }
  488. static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
  489. size_t len, size_t linear,
  490. int noblock, int *err)
  491. {
  492. struct sk_buff *skb;
  493. /* Under a page? Don't bother with paged skb. */
  494. if (prepad + len < PAGE_SIZE || !linear)
  495. linear = len;
  496. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  497. err, 0);
  498. if (!skb)
  499. return NULL;
  500. skb_reserve(skb, prepad);
  501. skb_put(skb, linear);
  502. skb->data_len = len - linear;
  503. skb->len += len - linear;
  504. return skb;
  505. }
  506. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  507. #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  508. /* Get packet from user space buffer */
  509. static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
  510. struct iov_iter *from, int noblock)
  511. {
  512. int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
  513. struct sk_buff *skb;
  514. struct tap_dev *tap;
  515. unsigned long total_len = iov_iter_count(from);
  516. unsigned long len = total_len;
  517. int err;
  518. struct virtio_net_hdr vnet_hdr = { 0 };
  519. int vnet_hdr_len = 0;
  520. int copylen = 0;
  521. int depth;
  522. bool zerocopy = false;
  523. size_t linear;
  524. if (q->flags & IFF_VNET_HDR) {
  525. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  526. err = -EINVAL;
  527. if (len < vnet_hdr_len)
  528. goto err;
  529. len -= vnet_hdr_len;
  530. err = -EFAULT;
  531. if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
  532. goto err;
  533. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  534. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  535. tap16_to_cpu(q, vnet_hdr.csum_start) +
  536. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  537. tap16_to_cpu(q, vnet_hdr.hdr_len))
  538. vnet_hdr.hdr_len = cpu_to_tap16(q,
  539. tap16_to_cpu(q, vnet_hdr.csum_start) +
  540. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  541. err = -EINVAL;
  542. if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  543. goto err;
  544. }
  545. err = -EINVAL;
  546. if (unlikely(len < ETH_HLEN))
  547. goto err;
  548. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  549. struct iov_iter i;
  550. copylen = vnet_hdr.hdr_len ?
  551. tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  552. if (copylen > good_linear)
  553. copylen = good_linear;
  554. else if (copylen < ETH_HLEN)
  555. copylen = ETH_HLEN;
  556. linear = copylen;
  557. i = *from;
  558. iov_iter_advance(&i, copylen);
  559. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  560. zerocopy = true;
  561. }
  562. if (!zerocopy) {
  563. copylen = len;
  564. linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
  565. if (linear > good_linear)
  566. linear = good_linear;
  567. else if (linear < ETH_HLEN)
  568. linear = ETH_HLEN;
  569. }
  570. skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
  571. linear, noblock, &err);
  572. if (!skb)
  573. goto err;
  574. if (zerocopy)
  575. err = zerocopy_sg_from_iter(skb, from);
  576. else
  577. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  578. if (err)
  579. goto err_kfree;
  580. skb_set_network_header(skb, ETH_HLEN);
  581. skb_reset_mac_header(skb);
  582. skb->protocol = eth_hdr(skb)->h_proto;
  583. if (vnet_hdr_len) {
  584. err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
  585. tap_is_little_endian(q));
  586. if (err)
  587. goto err_kfree;
  588. }
  589. skb_probe_transport_header(skb, ETH_HLEN);
  590. /* Move network header to the right position for VLAN tagged packets */
  591. if ((skb->protocol == htons(ETH_P_8021Q) ||
  592. skb->protocol == htons(ETH_P_8021AD)) &&
  593. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  594. skb_set_network_header(skb, depth);
  595. rcu_read_lock();
  596. tap = rcu_dereference(q->tap);
  597. /* copy skb_ubuf_info for callback when skb has no error */
  598. if (zerocopy) {
  599. skb_shinfo(skb)->destructor_arg = m->msg_control;
  600. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  601. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  602. } else if (m && m->msg_control) {
  603. struct ubuf_info *uarg = m->msg_control;
  604. uarg->callback(uarg, false);
  605. }
  606. if (tap) {
  607. skb->dev = tap->dev;
  608. dev_queue_xmit(skb);
  609. } else {
  610. kfree_skb(skb);
  611. }
  612. rcu_read_unlock();
  613. return total_len;
  614. err_kfree:
  615. kfree_skb(skb);
  616. err:
  617. rcu_read_lock();
  618. tap = rcu_dereference(q->tap);
  619. if (tap && tap->count_tx_dropped)
  620. tap->count_tx_dropped(tap);
  621. rcu_read_unlock();
  622. return err;
  623. }
  624. static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  625. {
  626. struct file *file = iocb->ki_filp;
  627. struct tap_queue *q = file->private_data;
  628. return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  629. }
  630. /* Put packet to the user space buffer */
  631. static ssize_t tap_put_user(struct tap_queue *q,
  632. const struct sk_buff *skb,
  633. struct iov_iter *iter)
  634. {
  635. int ret;
  636. int vnet_hdr_len = 0;
  637. int vlan_offset = 0;
  638. int total;
  639. if (q->flags & IFF_VNET_HDR) {
  640. int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
  641. struct virtio_net_hdr vnet_hdr;
  642. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  643. if (iov_iter_count(iter) < vnet_hdr_len)
  644. return -EINVAL;
  645. if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
  646. tap_is_little_endian(q), true,
  647. vlan_hlen))
  648. BUG();
  649. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  650. sizeof(vnet_hdr))
  651. return -EFAULT;
  652. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  653. }
  654. total = vnet_hdr_len;
  655. total += skb->len;
  656. if (skb_vlan_tag_present(skb)) {
  657. struct {
  658. __be16 h_vlan_proto;
  659. __be16 h_vlan_TCI;
  660. } veth;
  661. veth.h_vlan_proto = skb->vlan_proto;
  662. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  663. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  664. total += VLAN_HLEN;
  665. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  666. if (ret || !iov_iter_count(iter))
  667. goto done;
  668. ret = copy_to_iter(&veth, sizeof(veth), iter);
  669. if (ret != sizeof(veth) || !iov_iter_count(iter))
  670. goto done;
  671. }
  672. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  673. skb->len - vlan_offset);
  674. done:
  675. return ret ? ret : total;
  676. }
  677. static ssize_t tap_do_read(struct tap_queue *q,
  678. struct iov_iter *to,
  679. int noblock, struct sk_buff *skb)
  680. {
  681. DEFINE_WAIT(wait);
  682. ssize_t ret = 0;
  683. if (!iov_iter_count(to)) {
  684. if (skb)
  685. kfree_skb(skb);
  686. return 0;
  687. }
  688. if (skb)
  689. goto put;
  690. while (1) {
  691. if (!noblock)
  692. prepare_to_wait(sk_sleep(&q->sk), &wait,
  693. TASK_INTERRUPTIBLE);
  694. /* Read frames from the queue */
  695. skb = ptr_ring_consume(&q->ring);
  696. if (skb)
  697. break;
  698. if (noblock) {
  699. ret = -EAGAIN;
  700. break;
  701. }
  702. if (signal_pending(current)) {
  703. ret = -ERESTARTSYS;
  704. break;
  705. }
  706. /* Nothing to read, let's sleep */
  707. schedule();
  708. }
  709. if (!noblock)
  710. finish_wait(sk_sleep(&q->sk), &wait);
  711. put:
  712. if (skb) {
  713. ret = tap_put_user(q, skb, to);
  714. if (unlikely(ret < 0))
  715. kfree_skb(skb);
  716. else
  717. consume_skb(skb);
  718. }
  719. return ret;
  720. }
  721. static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  722. {
  723. struct file *file = iocb->ki_filp;
  724. struct tap_queue *q = file->private_data;
  725. ssize_t len = iov_iter_count(to), ret;
  726. ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
  727. ret = min_t(ssize_t, ret, len);
  728. if (ret > 0)
  729. iocb->ki_pos = ret;
  730. return ret;
  731. }
  732. static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
  733. {
  734. struct tap_dev *tap;
  735. ASSERT_RTNL();
  736. tap = rtnl_dereference(q->tap);
  737. if (tap)
  738. dev_hold(tap->dev);
  739. return tap;
  740. }
  741. static void tap_put_tap_dev(struct tap_dev *tap)
  742. {
  743. dev_put(tap->dev);
  744. }
  745. static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
  746. {
  747. struct tap_queue *q = file->private_data;
  748. struct tap_dev *tap;
  749. int ret;
  750. tap = tap_get_tap_dev(q);
  751. if (!tap)
  752. return -EINVAL;
  753. if (flags & IFF_ATTACH_QUEUE)
  754. ret = tap_enable_queue(tap, file, q);
  755. else if (flags & IFF_DETACH_QUEUE)
  756. ret = tap_disable_queue(q);
  757. else
  758. ret = -EINVAL;
  759. tap_put_tap_dev(tap);
  760. return ret;
  761. }
  762. static int set_offload(struct tap_queue *q, unsigned long arg)
  763. {
  764. struct tap_dev *tap;
  765. netdev_features_t features;
  766. netdev_features_t feature_mask = 0;
  767. tap = rtnl_dereference(q->tap);
  768. if (!tap)
  769. return -ENOLINK;
  770. features = tap->dev->features;
  771. if (arg & TUN_F_CSUM) {
  772. feature_mask = NETIF_F_HW_CSUM;
  773. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  774. if (arg & TUN_F_TSO_ECN)
  775. feature_mask |= NETIF_F_TSO_ECN;
  776. if (arg & TUN_F_TSO4)
  777. feature_mask |= NETIF_F_TSO;
  778. if (arg & TUN_F_TSO6)
  779. feature_mask |= NETIF_F_TSO6;
  780. }
  781. }
  782. /* tun/tap driver inverts the usage for TSO offloads, where
  783. * setting the TSO bit means that the userspace wants to
  784. * accept TSO frames and turning it off means that user space
  785. * does not support TSO.
  786. * For tap, we have to invert it to mean the same thing.
  787. * When user space turns off TSO, we turn off GSO/LRO so that
  788. * user-space will not receive TSO frames.
  789. */
  790. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
  791. features |= RX_OFFLOADS;
  792. else
  793. features &= ~RX_OFFLOADS;
  794. /* tap_features are the same as features on tun/tap and
  795. * reflect user expectations.
  796. */
  797. tap->tap_features = feature_mask;
  798. if (tap->update_features)
  799. tap->update_features(tap, features);
  800. return 0;
  801. }
  802. /*
  803. * provide compatibility with generic tun/tap interface
  804. */
  805. static long tap_ioctl(struct file *file, unsigned int cmd,
  806. unsigned long arg)
  807. {
  808. struct tap_queue *q = file->private_data;
  809. struct tap_dev *tap;
  810. void __user *argp = (void __user *)arg;
  811. struct ifreq __user *ifr = argp;
  812. unsigned int __user *up = argp;
  813. unsigned short u;
  814. int __user *sp = argp;
  815. struct sockaddr sa;
  816. int s;
  817. int ret;
  818. switch (cmd) {
  819. case TUNSETIFF:
  820. /* ignore the name, just look at flags */
  821. if (get_user(u, &ifr->ifr_flags))
  822. return -EFAULT;
  823. ret = 0;
  824. if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
  825. ret = -EINVAL;
  826. else
  827. q->flags = (q->flags & ~TAP_IFFEATURES) | u;
  828. return ret;
  829. case TUNGETIFF:
  830. rtnl_lock();
  831. tap = tap_get_tap_dev(q);
  832. if (!tap) {
  833. rtnl_unlock();
  834. return -ENOLINK;
  835. }
  836. ret = 0;
  837. u = q->flags;
  838. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  839. put_user(u, &ifr->ifr_flags))
  840. ret = -EFAULT;
  841. tap_put_tap_dev(tap);
  842. rtnl_unlock();
  843. return ret;
  844. case TUNSETQUEUE:
  845. if (get_user(u, &ifr->ifr_flags))
  846. return -EFAULT;
  847. rtnl_lock();
  848. ret = tap_ioctl_set_queue(file, u);
  849. rtnl_unlock();
  850. return ret;
  851. case TUNGETFEATURES:
  852. if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
  853. return -EFAULT;
  854. return 0;
  855. case TUNSETSNDBUF:
  856. if (get_user(s, sp))
  857. return -EFAULT;
  858. if (s <= 0)
  859. return -EINVAL;
  860. q->sk.sk_sndbuf = s;
  861. return 0;
  862. case TUNGETVNETHDRSZ:
  863. s = q->vnet_hdr_sz;
  864. if (put_user(s, sp))
  865. return -EFAULT;
  866. return 0;
  867. case TUNSETVNETHDRSZ:
  868. if (get_user(s, sp))
  869. return -EFAULT;
  870. if (s < (int)sizeof(struct virtio_net_hdr))
  871. return -EINVAL;
  872. q->vnet_hdr_sz = s;
  873. return 0;
  874. case TUNGETVNETLE:
  875. s = !!(q->flags & TAP_VNET_LE);
  876. if (put_user(s, sp))
  877. return -EFAULT;
  878. return 0;
  879. case TUNSETVNETLE:
  880. if (get_user(s, sp))
  881. return -EFAULT;
  882. if (s)
  883. q->flags |= TAP_VNET_LE;
  884. else
  885. q->flags &= ~TAP_VNET_LE;
  886. return 0;
  887. case TUNGETVNETBE:
  888. return tap_get_vnet_be(q, sp);
  889. case TUNSETVNETBE:
  890. return tap_set_vnet_be(q, sp);
  891. case TUNSETOFFLOAD:
  892. /* let the user check for future flags */
  893. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  894. TUN_F_TSO_ECN | TUN_F_UFO))
  895. return -EINVAL;
  896. rtnl_lock();
  897. ret = set_offload(q, arg);
  898. rtnl_unlock();
  899. return ret;
  900. case SIOCGIFHWADDR:
  901. rtnl_lock();
  902. tap = tap_get_tap_dev(q);
  903. if (!tap) {
  904. rtnl_unlock();
  905. return -ENOLINK;
  906. }
  907. ret = 0;
  908. u = tap->dev->type;
  909. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  910. copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) ||
  911. put_user(u, &ifr->ifr_hwaddr.sa_family))
  912. ret = -EFAULT;
  913. tap_put_tap_dev(tap);
  914. rtnl_unlock();
  915. return ret;
  916. case SIOCSIFHWADDR:
  917. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  918. return -EFAULT;
  919. rtnl_lock();
  920. tap = tap_get_tap_dev(q);
  921. if (!tap) {
  922. rtnl_unlock();
  923. return -ENOLINK;
  924. }
  925. ret = dev_set_mac_address(tap->dev, &sa);
  926. tap_put_tap_dev(tap);
  927. rtnl_unlock();
  928. return ret;
  929. default:
  930. return -EINVAL;
  931. }
  932. }
  933. #ifdef CONFIG_COMPAT
  934. static long tap_compat_ioctl(struct file *file, unsigned int cmd,
  935. unsigned long arg)
  936. {
  937. return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  938. }
  939. #endif
  940. static const struct file_operations tap_fops = {
  941. .owner = THIS_MODULE,
  942. .open = tap_open,
  943. .release = tap_release,
  944. .read_iter = tap_read_iter,
  945. .write_iter = tap_write_iter,
  946. .poll = tap_poll,
  947. .llseek = no_llseek,
  948. .unlocked_ioctl = tap_ioctl,
  949. #ifdef CONFIG_COMPAT
  950. .compat_ioctl = tap_compat_ioctl,
  951. #endif
  952. };
  953. static int tap_sendmsg(struct socket *sock, struct msghdr *m,
  954. size_t total_len)
  955. {
  956. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  957. return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  958. }
  959. static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  960. size_t total_len, int flags)
  961. {
  962. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  963. struct sk_buff *skb = m->msg_control;
  964. int ret;
  965. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
  966. if (skb)
  967. kfree_skb(skb);
  968. return -EINVAL;
  969. }
  970. ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
  971. if (ret > total_len) {
  972. m->msg_flags |= MSG_TRUNC;
  973. ret = flags & MSG_TRUNC ? ret : total_len;
  974. }
  975. return ret;
  976. }
  977. static int tap_peek_len(struct socket *sock)
  978. {
  979. struct tap_queue *q = container_of(sock, struct tap_queue,
  980. sock);
  981. return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
  982. }
  983. /* Ops structure to mimic raw sockets with tun */
  984. static const struct proto_ops tap_socket_ops = {
  985. .sendmsg = tap_sendmsg,
  986. .recvmsg = tap_recvmsg,
  987. .peek_len = tap_peek_len,
  988. };
  989. /* Get an underlying socket object from tun file. Returns error unless file is
  990. * attached to a device. The returned object works like a packet socket, it
  991. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  992. * holding a reference to the file for as long as the socket is in use. */
  993. struct socket *tap_get_socket(struct file *file)
  994. {
  995. struct tap_queue *q;
  996. if (file->f_op != &tap_fops)
  997. return ERR_PTR(-EINVAL);
  998. q = file->private_data;
  999. if (!q)
  1000. return ERR_PTR(-EBADFD);
  1001. return &q->sock;
  1002. }
  1003. EXPORT_SYMBOL_GPL(tap_get_socket);
  1004. struct ptr_ring *tap_get_ptr_ring(struct file *file)
  1005. {
  1006. struct tap_queue *q;
  1007. if (file->f_op != &tap_fops)
  1008. return ERR_PTR(-EINVAL);
  1009. q = file->private_data;
  1010. if (!q)
  1011. return ERR_PTR(-EBADFD);
  1012. return &q->ring;
  1013. }
  1014. EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
  1015. int tap_queue_resize(struct tap_dev *tap)
  1016. {
  1017. struct net_device *dev = tap->dev;
  1018. struct tap_queue *q;
  1019. struct ptr_ring **rings;
  1020. int n = tap->numqueues;
  1021. int ret, i = 0;
  1022. rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
  1023. if (!rings)
  1024. return -ENOMEM;
  1025. list_for_each_entry(q, &tap->queue_list, next)
  1026. rings[i++] = &q->ring;
  1027. ret = ptr_ring_resize_multiple(rings, n,
  1028. dev->tx_queue_len, GFP_KERNEL,
  1029. __skb_array_destroy_skb);
  1030. kfree(rings);
  1031. return ret;
  1032. }
  1033. EXPORT_SYMBOL_GPL(tap_queue_resize);
  1034. static int tap_list_add(dev_t major, const char *device_name)
  1035. {
  1036. struct major_info *tap_major;
  1037. tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
  1038. if (!tap_major)
  1039. return -ENOMEM;
  1040. tap_major->major = MAJOR(major);
  1041. idr_init(&tap_major->minor_idr);
  1042. spin_lock_init(&tap_major->minor_lock);
  1043. tap_major->device_name = device_name;
  1044. list_add_tail_rcu(&tap_major->next, &major_list);
  1045. return 0;
  1046. }
  1047. int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
  1048. const char *device_name, struct module *module)
  1049. {
  1050. int err;
  1051. err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
  1052. if (err)
  1053. goto out1;
  1054. cdev_init(tap_cdev, &tap_fops);
  1055. tap_cdev->owner = module;
  1056. err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
  1057. if (err)
  1058. goto out2;
  1059. err = tap_list_add(*tap_major, device_name);
  1060. if (err)
  1061. goto out3;
  1062. return 0;
  1063. out3:
  1064. cdev_del(tap_cdev);
  1065. out2:
  1066. unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
  1067. out1:
  1068. return err;
  1069. }
  1070. EXPORT_SYMBOL_GPL(tap_create_cdev);
  1071. void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
  1072. {
  1073. struct major_info *tap_major, *tmp;
  1074. cdev_del(tap_cdev);
  1075. unregister_chrdev_region(major, TAP_NUM_DEVS);
  1076. list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
  1077. if (tap_major->major == MAJOR(major)) {
  1078. idr_destroy(&tap_major->minor_idr);
  1079. list_del_rcu(&tap_major->next);
  1080. kfree_rcu(tap_major, rcu);
  1081. }
  1082. }
  1083. }
  1084. EXPORT_SYMBOL_GPL(tap_destroy_cdev);
  1085. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1086. MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
  1087. MODULE_LICENSE("GPL");