tap.c 29 KB

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