tap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. struct virtio_net_hdr vnet_hdr;
  641. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  642. if (iov_iter_count(iter) < vnet_hdr_len)
  643. return -EINVAL;
  644. if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
  645. tap_is_little_endian(q), true))
  646. BUG();
  647. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  648. sizeof(vnet_hdr))
  649. return -EFAULT;
  650. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  651. }
  652. total = vnet_hdr_len;
  653. total += skb->len;
  654. if (skb_vlan_tag_present(skb)) {
  655. struct {
  656. __be16 h_vlan_proto;
  657. __be16 h_vlan_TCI;
  658. } veth;
  659. veth.h_vlan_proto = skb->vlan_proto;
  660. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  661. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  662. total += VLAN_HLEN;
  663. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  664. if (ret || !iov_iter_count(iter))
  665. goto done;
  666. ret = copy_to_iter(&veth, sizeof(veth), iter);
  667. if (ret != sizeof(veth) || !iov_iter_count(iter))
  668. goto done;
  669. }
  670. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  671. skb->len - vlan_offset);
  672. done:
  673. return ret ? ret : total;
  674. }
  675. static ssize_t tap_do_read(struct tap_queue *q,
  676. struct iov_iter *to,
  677. int noblock, struct sk_buff *skb)
  678. {
  679. DEFINE_WAIT(wait);
  680. ssize_t ret = 0;
  681. if (!iov_iter_count(to)) {
  682. if (skb)
  683. kfree_skb(skb);
  684. return 0;
  685. }
  686. if (skb)
  687. goto put;
  688. while (1) {
  689. if (!noblock)
  690. prepare_to_wait(sk_sleep(&q->sk), &wait,
  691. TASK_INTERRUPTIBLE);
  692. /* Read frames from the queue */
  693. skb = ptr_ring_consume(&q->ring);
  694. if (skb)
  695. break;
  696. if (noblock) {
  697. ret = -EAGAIN;
  698. break;
  699. }
  700. if (signal_pending(current)) {
  701. ret = -ERESTARTSYS;
  702. break;
  703. }
  704. /* Nothing to read, let's sleep */
  705. schedule();
  706. }
  707. if (!noblock)
  708. finish_wait(sk_sleep(&q->sk), &wait);
  709. put:
  710. if (skb) {
  711. ret = tap_put_user(q, skb, to);
  712. if (unlikely(ret < 0))
  713. kfree_skb(skb);
  714. else
  715. consume_skb(skb);
  716. }
  717. return ret;
  718. }
  719. static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  720. {
  721. struct file *file = iocb->ki_filp;
  722. struct tap_queue *q = file->private_data;
  723. ssize_t len = iov_iter_count(to), ret;
  724. ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
  725. ret = min_t(ssize_t, ret, len);
  726. if (ret > 0)
  727. iocb->ki_pos = ret;
  728. return ret;
  729. }
  730. static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
  731. {
  732. struct tap_dev *tap;
  733. ASSERT_RTNL();
  734. tap = rtnl_dereference(q->tap);
  735. if (tap)
  736. dev_hold(tap->dev);
  737. return tap;
  738. }
  739. static void tap_put_tap_dev(struct tap_dev *tap)
  740. {
  741. dev_put(tap->dev);
  742. }
  743. static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
  744. {
  745. struct tap_queue *q = file->private_data;
  746. struct tap_dev *tap;
  747. int ret;
  748. tap = tap_get_tap_dev(q);
  749. if (!tap)
  750. return -EINVAL;
  751. if (flags & IFF_ATTACH_QUEUE)
  752. ret = tap_enable_queue(tap, file, q);
  753. else if (flags & IFF_DETACH_QUEUE)
  754. ret = tap_disable_queue(q);
  755. else
  756. ret = -EINVAL;
  757. tap_put_tap_dev(tap);
  758. return ret;
  759. }
  760. static int set_offload(struct tap_queue *q, unsigned long arg)
  761. {
  762. struct tap_dev *tap;
  763. netdev_features_t features;
  764. netdev_features_t feature_mask = 0;
  765. tap = rtnl_dereference(q->tap);
  766. if (!tap)
  767. return -ENOLINK;
  768. features = tap->dev->features;
  769. if (arg & TUN_F_CSUM) {
  770. feature_mask = NETIF_F_HW_CSUM;
  771. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  772. if (arg & TUN_F_TSO_ECN)
  773. feature_mask |= NETIF_F_TSO_ECN;
  774. if (arg & TUN_F_TSO4)
  775. feature_mask |= NETIF_F_TSO;
  776. if (arg & TUN_F_TSO6)
  777. feature_mask |= NETIF_F_TSO6;
  778. }
  779. }
  780. /* tun/tap driver inverts the usage for TSO offloads, where
  781. * setting the TSO bit means that the userspace wants to
  782. * accept TSO frames and turning it off means that user space
  783. * does not support TSO.
  784. * For tap, we have to invert it to mean the same thing.
  785. * When user space turns off TSO, we turn off GSO/LRO so that
  786. * user-space will not receive TSO frames.
  787. */
  788. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
  789. features |= RX_OFFLOADS;
  790. else
  791. features &= ~RX_OFFLOADS;
  792. /* tap_features are the same as features on tun/tap and
  793. * reflect user expectations.
  794. */
  795. tap->tap_features = feature_mask;
  796. if (tap->update_features)
  797. tap->update_features(tap, features);
  798. return 0;
  799. }
  800. /*
  801. * provide compatibility with generic tun/tap interface
  802. */
  803. static long tap_ioctl(struct file *file, unsigned int cmd,
  804. unsigned long arg)
  805. {
  806. struct tap_queue *q = file->private_data;
  807. struct tap_dev *tap;
  808. void __user *argp = (void __user *)arg;
  809. struct ifreq __user *ifr = argp;
  810. unsigned int __user *up = argp;
  811. unsigned short u;
  812. int __user *sp = argp;
  813. struct sockaddr sa;
  814. int s;
  815. int ret;
  816. switch (cmd) {
  817. case TUNSETIFF:
  818. /* ignore the name, just look at flags */
  819. if (get_user(u, &ifr->ifr_flags))
  820. return -EFAULT;
  821. ret = 0;
  822. if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
  823. ret = -EINVAL;
  824. else
  825. q->flags = (q->flags & ~TAP_IFFEATURES) | u;
  826. return ret;
  827. case TUNGETIFF:
  828. rtnl_lock();
  829. tap = tap_get_tap_dev(q);
  830. if (!tap) {
  831. rtnl_unlock();
  832. return -ENOLINK;
  833. }
  834. ret = 0;
  835. u = q->flags;
  836. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  837. put_user(u, &ifr->ifr_flags))
  838. ret = -EFAULT;
  839. tap_put_tap_dev(tap);
  840. rtnl_unlock();
  841. return ret;
  842. case TUNSETQUEUE:
  843. if (get_user(u, &ifr->ifr_flags))
  844. return -EFAULT;
  845. rtnl_lock();
  846. ret = tap_ioctl_set_queue(file, u);
  847. rtnl_unlock();
  848. return ret;
  849. case TUNGETFEATURES:
  850. if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
  851. return -EFAULT;
  852. return 0;
  853. case TUNSETSNDBUF:
  854. if (get_user(s, sp))
  855. return -EFAULT;
  856. if (s <= 0)
  857. return -EINVAL;
  858. q->sk.sk_sndbuf = s;
  859. return 0;
  860. case TUNGETVNETHDRSZ:
  861. s = q->vnet_hdr_sz;
  862. if (put_user(s, sp))
  863. return -EFAULT;
  864. return 0;
  865. case TUNSETVNETHDRSZ:
  866. if (get_user(s, sp))
  867. return -EFAULT;
  868. if (s < (int)sizeof(struct virtio_net_hdr))
  869. return -EINVAL;
  870. q->vnet_hdr_sz = s;
  871. return 0;
  872. case TUNGETVNETLE:
  873. s = !!(q->flags & TAP_VNET_LE);
  874. if (put_user(s, sp))
  875. return -EFAULT;
  876. return 0;
  877. case TUNSETVNETLE:
  878. if (get_user(s, sp))
  879. return -EFAULT;
  880. if (s)
  881. q->flags |= TAP_VNET_LE;
  882. else
  883. q->flags &= ~TAP_VNET_LE;
  884. return 0;
  885. case TUNGETVNETBE:
  886. return tap_get_vnet_be(q, sp);
  887. case TUNSETVNETBE:
  888. return tap_set_vnet_be(q, sp);
  889. case TUNSETOFFLOAD:
  890. /* let the user check for future flags */
  891. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  892. TUN_F_TSO_ECN | TUN_F_UFO))
  893. return -EINVAL;
  894. rtnl_lock();
  895. ret = set_offload(q, arg);
  896. rtnl_unlock();
  897. return ret;
  898. case SIOCGIFHWADDR:
  899. rtnl_lock();
  900. tap = tap_get_tap_dev(q);
  901. if (!tap) {
  902. rtnl_unlock();
  903. return -ENOLINK;
  904. }
  905. ret = 0;
  906. u = tap->dev->type;
  907. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  908. copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) ||
  909. put_user(u, &ifr->ifr_hwaddr.sa_family))
  910. ret = -EFAULT;
  911. tap_put_tap_dev(tap);
  912. rtnl_unlock();
  913. return ret;
  914. case SIOCSIFHWADDR:
  915. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  916. return -EFAULT;
  917. rtnl_lock();
  918. tap = tap_get_tap_dev(q);
  919. if (!tap) {
  920. rtnl_unlock();
  921. return -ENOLINK;
  922. }
  923. ret = dev_set_mac_address(tap->dev, &sa);
  924. tap_put_tap_dev(tap);
  925. rtnl_unlock();
  926. return ret;
  927. default:
  928. return -EINVAL;
  929. }
  930. }
  931. #ifdef CONFIG_COMPAT
  932. static long tap_compat_ioctl(struct file *file, unsigned int cmd,
  933. unsigned long arg)
  934. {
  935. return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  936. }
  937. #endif
  938. static const struct file_operations tap_fops = {
  939. .owner = THIS_MODULE,
  940. .open = tap_open,
  941. .release = tap_release,
  942. .read_iter = tap_read_iter,
  943. .write_iter = tap_write_iter,
  944. .poll = tap_poll,
  945. .llseek = no_llseek,
  946. .unlocked_ioctl = tap_ioctl,
  947. #ifdef CONFIG_COMPAT
  948. .compat_ioctl = tap_compat_ioctl,
  949. #endif
  950. };
  951. static int tap_sendmsg(struct socket *sock, struct msghdr *m,
  952. size_t total_len)
  953. {
  954. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  955. return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  956. }
  957. static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  958. size_t total_len, int flags)
  959. {
  960. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  961. struct sk_buff *skb = m->msg_control;
  962. int ret;
  963. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) {
  964. if (skb)
  965. kfree_skb(skb);
  966. return -EINVAL;
  967. }
  968. ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT, skb);
  969. if (ret > total_len) {
  970. m->msg_flags |= MSG_TRUNC;
  971. ret = flags & MSG_TRUNC ? ret : total_len;
  972. }
  973. return ret;
  974. }
  975. static int tap_peek_len(struct socket *sock)
  976. {
  977. struct tap_queue *q = container_of(sock, struct tap_queue,
  978. sock);
  979. return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);
  980. }
  981. /* Ops structure to mimic raw sockets with tun */
  982. static const struct proto_ops tap_socket_ops = {
  983. .sendmsg = tap_sendmsg,
  984. .recvmsg = tap_recvmsg,
  985. .peek_len = tap_peek_len,
  986. };
  987. /* Get an underlying socket object from tun file. Returns error unless file is
  988. * attached to a device. The returned object works like a packet socket, it
  989. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  990. * holding a reference to the file for as long as the socket is in use. */
  991. struct socket *tap_get_socket(struct file *file)
  992. {
  993. struct tap_queue *q;
  994. if (file->f_op != &tap_fops)
  995. return ERR_PTR(-EINVAL);
  996. q = file->private_data;
  997. if (!q)
  998. return ERR_PTR(-EBADFD);
  999. return &q->sock;
  1000. }
  1001. EXPORT_SYMBOL_GPL(tap_get_socket);
  1002. struct ptr_ring *tap_get_ptr_ring(struct file *file)
  1003. {
  1004. struct tap_queue *q;
  1005. if (file->f_op != &tap_fops)
  1006. return ERR_PTR(-EINVAL);
  1007. q = file->private_data;
  1008. if (!q)
  1009. return ERR_PTR(-EBADFD);
  1010. return &q->ring;
  1011. }
  1012. EXPORT_SYMBOL_GPL(tap_get_ptr_ring);
  1013. int tap_queue_resize(struct tap_dev *tap)
  1014. {
  1015. struct net_device *dev = tap->dev;
  1016. struct tap_queue *q;
  1017. struct ptr_ring **rings;
  1018. int n = tap->numqueues;
  1019. int ret, i = 0;
  1020. rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
  1021. if (!rings)
  1022. return -ENOMEM;
  1023. list_for_each_entry(q, &tap->queue_list, next)
  1024. rings[i++] = &q->ring;
  1025. ret = ptr_ring_resize_multiple(rings, n,
  1026. dev->tx_queue_len, GFP_KERNEL,
  1027. __skb_array_destroy_skb);
  1028. kfree(rings);
  1029. return ret;
  1030. }
  1031. EXPORT_SYMBOL_GPL(tap_queue_resize);
  1032. static int tap_list_add(dev_t major, const char *device_name)
  1033. {
  1034. struct major_info *tap_major;
  1035. tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
  1036. if (!tap_major)
  1037. return -ENOMEM;
  1038. tap_major->major = MAJOR(major);
  1039. idr_init(&tap_major->minor_idr);
  1040. spin_lock_init(&tap_major->minor_lock);
  1041. tap_major->device_name = device_name;
  1042. list_add_tail_rcu(&tap_major->next, &major_list);
  1043. return 0;
  1044. }
  1045. int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
  1046. const char *device_name, struct module *module)
  1047. {
  1048. int err;
  1049. err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
  1050. if (err)
  1051. goto out1;
  1052. cdev_init(tap_cdev, &tap_fops);
  1053. tap_cdev->owner = module;
  1054. err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
  1055. if (err)
  1056. goto out2;
  1057. err = tap_list_add(*tap_major, device_name);
  1058. if (err)
  1059. goto out3;
  1060. return 0;
  1061. out3:
  1062. cdev_del(tap_cdev);
  1063. out2:
  1064. unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
  1065. out1:
  1066. return err;
  1067. }
  1068. EXPORT_SYMBOL_GPL(tap_create_cdev);
  1069. void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
  1070. {
  1071. struct major_info *tap_major, *tmp;
  1072. cdev_del(tap_cdev);
  1073. unregister_chrdev_region(major, TAP_NUM_DEVS);
  1074. list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
  1075. if (tap_major->major == MAJOR(major)) {
  1076. idr_destroy(&tap_major->minor_idr);
  1077. list_del_rcu(&tap_major->next);
  1078. kfree_rcu(tap_major, rcu);
  1079. }
  1080. }
  1081. }
  1082. EXPORT_SYMBOL_GPL(tap_destroy_cdev);
  1083. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1084. MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
  1085. MODULE_LICENSE("GPL");