tap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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 = ACCESS_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. if (__skb_array_full(&q->skb_array))
  272. goto drop;
  273. skb_push(skb, ETH_HLEN);
  274. /* Apply the forward feature mask so that we perform segmentation
  275. * according to users wishes. This only works if VNET_HDR is
  276. * enabled.
  277. */
  278. if (q->flags & IFF_VNET_HDR)
  279. features |= tap->tap_features;
  280. if (netif_needs_gso(skb, features)) {
  281. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  282. if (IS_ERR(segs))
  283. goto drop;
  284. if (!segs) {
  285. if (skb_array_produce(&q->skb_array, skb))
  286. goto drop;
  287. goto wake_up;
  288. }
  289. consume_skb(skb);
  290. while (segs) {
  291. struct sk_buff *nskb = segs->next;
  292. segs->next = NULL;
  293. if (skb_array_produce(&q->skb_array, segs)) {
  294. kfree_skb(segs);
  295. kfree_skb_list(nskb);
  296. break;
  297. }
  298. segs = nskb;
  299. }
  300. } else {
  301. /* If we receive a partial checksum and the tap side
  302. * doesn't support checksum offload, compute the checksum.
  303. * Note: it doesn't matter which checksum feature to
  304. * check, we either support them all or none.
  305. */
  306. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  307. !(features & NETIF_F_CSUM_MASK) &&
  308. skb_checksum_help(skb))
  309. goto drop;
  310. if (skb_array_produce(&q->skb_array, skb))
  311. goto drop;
  312. }
  313. wake_up:
  314. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  315. return RX_HANDLER_CONSUMED;
  316. drop:
  317. /* Count errors/drops only here, thus don't care about args. */
  318. if (tap->count_rx_dropped)
  319. tap->count_rx_dropped(tap);
  320. kfree_skb(skb);
  321. return RX_HANDLER_CONSUMED;
  322. }
  323. EXPORT_SYMBOL_GPL(tap_handle_frame);
  324. static struct major_info *tap_get_major(int major)
  325. {
  326. struct major_info *tap_major;
  327. list_for_each_entry_rcu(tap_major, &major_list, next) {
  328. if (tap_major->major == major)
  329. return tap_major;
  330. }
  331. return NULL;
  332. }
  333. int tap_get_minor(dev_t major, struct tap_dev *tap)
  334. {
  335. int retval = -ENOMEM;
  336. struct major_info *tap_major;
  337. rcu_read_lock();
  338. tap_major = tap_get_major(MAJOR(major));
  339. if (!tap_major) {
  340. retval = -EINVAL;
  341. goto unlock;
  342. }
  343. spin_lock(&tap_major->minor_lock);
  344. retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_ATOMIC);
  345. if (retval >= 0) {
  346. tap->minor = retval;
  347. } else if (retval == -ENOSPC) {
  348. netdev_err(tap->dev, "Too many tap devices\n");
  349. retval = -EINVAL;
  350. }
  351. spin_unlock(&tap_major->minor_lock);
  352. unlock:
  353. rcu_read_unlock();
  354. return retval < 0 ? retval : 0;
  355. }
  356. EXPORT_SYMBOL_GPL(tap_get_minor);
  357. void tap_free_minor(dev_t major, struct tap_dev *tap)
  358. {
  359. struct major_info *tap_major;
  360. rcu_read_lock();
  361. tap_major = tap_get_major(MAJOR(major));
  362. if (!tap_major) {
  363. goto unlock;
  364. }
  365. spin_lock(&tap_major->minor_lock);
  366. if (tap->minor) {
  367. idr_remove(&tap_major->minor_idr, tap->minor);
  368. tap->minor = 0;
  369. }
  370. spin_unlock(&tap_major->minor_lock);
  371. unlock:
  372. rcu_read_unlock();
  373. }
  374. EXPORT_SYMBOL_GPL(tap_free_minor);
  375. static struct tap_dev *dev_get_by_tap_file(int major, int minor)
  376. {
  377. struct net_device *dev = NULL;
  378. struct tap_dev *tap;
  379. struct major_info *tap_major;
  380. rcu_read_lock();
  381. tap_major = tap_get_major(major);
  382. if (!tap_major) {
  383. tap = NULL;
  384. goto unlock;
  385. }
  386. spin_lock(&tap_major->minor_lock);
  387. tap = idr_find(&tap_major->minor_idr, minor);
  388. if (tap) {
  389. dev = tap->dev;
  390. dev_hold(dev);
  391. }
  392. spin_unlock(&tap_major->minor_lock);
  393. unlock:
  394. rcu_read_unlock();
  395. return tap;
  396. }
  397. static void tap_sock_write_space(struct sock *sk)
  398. {
  399. wait_queue_head_t *wqueue;
  400. if (!sock_writeable(sk) ||
  401. !test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  402. return;
  403. wqueue = sk_sleep(sk);
  404. if (wqueue && waitqueue_active(wqueue))
  405. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  406. }
  407. static void tap_sock_destruct(struct sock *sk)
  408. {
  409. struct tap_queue *q = container_of(sk, struct tap_queue, sk);
  410. skb_array_cleanup(&q->skb_array);
  411. }
  412. static int tap_open(struct inode *inode, struct file *file)
  413. {
  414. struct net *net = current->nsproxy->net_ns;
  415. struct tap_dev *tap;
  416. struct tap_queue *q;
  417. int err = -ENODEV;
  418. rtnl_lock();
  419. tap = dev_get_by_tap_file(imajor(inode), iminor(inode));
  420. if (!tap)
  421. goto err;
  422. err = -ENOMEM;
  423. q = (struct tap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  424. &tap_proto, 0);
  425. if (!q)
  426. goto err;
  427. if (skb_array_init(&q->skb_array, tap->dev->tx_queue_len, GFP_KERNEL)) {
  428. sk_free(&q->sk);
  429. goto err;
  430. }
  431. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  432. init_waitqueue_head(&q->wq.wait);
  433. q->sock.type = SOCK_RAW;
  434. q->sock.state = SS_CONNECTED;
  435. q->sock.file = file;
  436. q->sock.ops = &tap_socket_ops;
  437. sock_init_data(&q->sock, &q->sk);
  438. q->sk.sk_write_space = tap_sock_write_space;
  439. q->sk.sk_destruct = tap_sock_destruct;
  440. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  441. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  442. /*
  443. * so far only KVM virtio_net uses tap, enable zero copy between
  444. * guest kernel and host kernel when lower device supports zerocopy
  445. *
  446. * The macvlan supports zerocopy iff the lower device supports zero
  447. * copy so we don't have to look at the lower device directly.
  448. */
  449. if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
  450. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  451. err = tap_set_queue(tap, file, q);
  452. if (err) {
  453. /* tap_sock_destruct() will take care of freeing skb_array */
  454. goto err_put;
  455. }
  456. dev_put(tap->dev);
  457. rtnl_unlock();
  458. return err;
  459. err_put:
  460. sock_put(&q->sk);
  461. err:
  462. if (tap)
  463. dev_put(tap->dev);
  464. rtnl_unlock();
  465. return err;
  466. }
  467. static int tap_release(struct inode *inode, struct file *file)
  468. {
  469. struct tap_queue *q = file->private_data;
  470. tap_put_queue(q);
  471. return 0;
  472. }
  473. static unsigned int tap_poll(struct file *file, poll_table *wait)
  474. {
  475. struct tap_queue *q = file->private_data;
  476. unsigned int mask = POLLERR;
  477. if (!q)
  478. goto out;
  479. mask = 0;
  480. poll_wait(file, &q->wq.wait, wait);
  481. if (!skb_array_empty(&q->skb_array))
  482. mask |= POLLIN | POLLRDNORM;
  483. if (sock_writeable(&q->sk) ||
  484. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
  485. sock_writeable(&q->sk)))
  486. mask |= POLLOUT | POLLWRNORM;
  487. out:
  488. return mask;
  489. }
  490. static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
  491. size_t len, size_t linear,
  492. int noblock, int *err)
  493. {
  494. struct sk_buff *skb;
  495. /* Under a page? Don't bother with paged skb. */
  496. if (prepad + len < PAGE_SIZE || !linear)
  497. linear = len;
  498. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  499. err, 0);
  500. if (!skb)
  501. return NULL;
  502. skb_reserve(skb, prepad);
  503. skb_put(skb, linear);
  504. skb->data_len = len - linear;
  505. skb->len += len - linear;
  506. return skb;
  507. }
  508. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  509. #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  510. /* Get packet from user space buffer */
  511. static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
  512. struct iov_iter *from, int noblock)
  513. {
  514. int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
  515. struct sk_buff *skb;
  516. struct tap_dev *tap;
  517. unsigned long total_len = iov_iter_count(from);
  518. unsigned long len = total_len;
  519. int err;
  520. struct virtio_net_hdr vnet_hdr = { 0 };
  521. int vnet_hdr_len = 0;
  522. int copylen = 0;
  523. int depth;
  524. bool zerocopy = false;
  525. size_t linear;
  526. if (q->flags & IFF_VNET_HDR) {
  527. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  528. err = -EINVAL;
  529. if (len < vnet_hdr_len)
  530. goto err;
  531. len -= vnet_hdr_len;
  532. err = -EFAULT;
  533. if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
  534. goto err;
  535. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  536. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  537. tap16_to_cpu(q, vnet_hdr.csum_start) +
  538. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  539. tap16_to_cpu(q, vnet_hdr.hdr_len))
  540. vnet_hdr.hdr_len = cpu_to_tap16(q,
  541. tap16_to_cpu(q, vnet_hdr.csum_start) +
  542. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  543. err = -EINVAL;
  544. if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  545. goto err;
  546. }
  547. err = -EINVAL;
  548. if (unlikely(len < ETH_HLEN))
  549. goto err;
  550. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  551. struct iov_iter i;
  552. copylen = vnet_hdr.hdr_len ?
  553. tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  554. if (copylen > good_linear)
  555. copylen = good_linear;
  556. else if (copylen < ETH_HLEN)
  557. copylen = ETH_HLEN;
  558. linear = copylen;
  559. i = *from;
  560. iov_iter_advance(&i, copylen);
  561. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  562. zerocopy = true;
  563. }
  564. if (!zerocopy) {
  565. copylen = len;
  566. linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
  567. if (linear > good_linear)
  568. linear = good_linear;
  569. else if (linear < ETH_HLEN)
  570. linear = ETH_HLEN;
  571. }
  572. skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
  573. linear, noblock, &err);
  574. if (!skb)
  575. goto err;
  576. if (zerocopy)
  577. err = zerocopy_sg_from_iter(skb, from);
  578. else
  579. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  580. if (err)
  581. goto err_kfree;
  582. skb_set_network_header(skb, ETH_HLEN);
  583. skb_reset_mac_header(skb);
  584. skb->protocol = eth_hdr(skb)->h_proto;
  585. if (vnet_hdr_len) {
  586. err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
  587. tap_is_little_endian(q));
  588. if (err)
  589. goto err_kfree;
  590. }
  591. skb_probe_transport_header(skb, ETH_HLEN);
  592. /* Move network header to the right position for VLAN tagged packets */
  593. if ((skb->protocol == htons(ETH_P_8021Q) ||
  594. skb->protocol == htons(ETH_P_8021AD)) &&
  595. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  596. skb_set_network_header(skb, depth);
  597. rcu_read_lock();
  598. tap = rcu_dereference(q->tap);
  599. /* copy skb_ubuf_info for callback when skb has no error */
  600. if (zerocopy) {
  601. skb_shinfo(skb)->destructor_arg = m->msg_control;
  602. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  603. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  604. } else if (m && m->msg_control) {
  605. struct ubuf_info *uarg = m->msg_control;
  606. uarg->callback(uarg, false);
  607. }
  608. if (tap) {
  609. skb->dev = tap->dev;
  610. dev_queue_xmit(skb);
  611. } else {
  612. kfree_skb(skb);
  613. }
  614. rcu_read_unlock();
  615. return total_len;
  616. err_kfree:
  617. kfree_skb(skb);
  618. err:
  619. rcu_read_lock();
  620. tap = rcu_dereference(q->tap);
  621. if (tap && tap->count_tx_dropped)
  622. tap->count_tx_dropped(tap);
  623. rcu_read_unlock();
  624. return err;
  625. }
  626. static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  627. {
  628. struct file *file = iocb->ki_filp;
  629. struct tap_queue *q = file->private_data;
  630. return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  631. }
  632. /* Put packet to the user space buffer */
  633. static ssize_t tap_put_user(struct tap_queue *q,
  634. const struct sk_buff *skb,
  635. struct iov_iter *iter)
  636. {
  637. int ret;
  638. int vnet_hdr_len = 0;
  639. int vlan_offset = 0;
  640. int total;
  641. if (q->flags & IFF_VNET_HDR) {
  642. struct virtio_net_hdr vnet_hdr;
  643. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  644. if (iov_iter_count(iter) < vnet_hdr_len)
  645. return -EINVAL;
  646. if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
  647. tap_is_little_endian(q), true))
  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. return 0;
  685. if (skb)
  686. goto put;
  687. while (1) {
  688. if (!noblock)
  689. prepare_to_wait(sk_sleep(&q->sk), &wait,
  690. TASK_INTERRUPTIBLE);
  691. /* Read frames from the queue */
  692. skb = skb_array_consume(&q->skb_array);
  693. if (skb)
  694. break;
  695. if (noblock) {
  696. ret = -EAGAIN;
  697. break;
  698. }
  699. if (signal_pending(current)) {
  700. ret = -ERESTARTSYS;
  701. break;
  702. }
  703. /* Nothing to read, let's sleep */
  704. schedule();
  705. }
  706. if (!noblock)
  707. finish_wait(sk_sleep(&q->sk), &wait);
  708. put:
  709. if (skb) {
  710. ret = tap_put_user(q, skb, to);
  711. if (unlikely(ret < 0))
  712. kfree_skb(skb);
  713. else
  714. consume_skb(skb);
  715. }
  716. return ret;
  717. }
  718. static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  719. {
  720. struct file *file = iocb->ki_filp;
  721. struct tap_queue *q = file->private_data;
  722. ssize_t len = iov_iter_count(to), ret;
  723. ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
  724. ret = min_t(ssize_t, ret, len);
  725. if (ret > 0)
  726. iocb->ki_pos = ret;
  727. return ret;
  728. }
  729. static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
  730. {
  731. struct tap_dev *tap;
  732. ASSERT_RTNL();
  733. tap = rtnl_dereference(q->tap);
  734. if (tap)
  735. dev_hold(tap->dev);
  736. return tap;
  737. }
  738. static void tap_put_tap_dev(struct tap_dev *tap)
  739. {
  740. dev_put(tap->dev);
  741. }
  742. static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
  743. {
  744. struct tap_queue *q = file->private_data;
  745. struct tap_dev *tap;
  746. int ret;
  747. tap = tap_get_tap_dev(q);
  748. if (!tap)
  749. return -EINVAL;
  750. if (flags & IFF_ATTACH_QUEUE)
  751. ret = tap_enable_queue(tap, file, q);
  752. else if (flags & IFF_DETACH_QUEUE)
  753. ret = tap_disable_queue(q);
  754. else
  755. ret = -EINVAL;
  756. tap_put_tap_dev(tap);
  757. return ret;
  758. }
  759. static int set_offload(struct tap_queue *q, unsigned long arg)
  760. {
  761. struct tap_dev *tap;
  762. netdev_features_t features;
  763. netdev_features_t feature_mask = 0;
  764. tap = rtnl_dereference(q->tap);
  765. if (!tap)
  766. return -ENOLINK;
  767. features = tap->dev->features;
  768. if (arg & TUN_F_CSUM) {
  769. feature_mask = NETIF_F_HW_CSUM;
  770. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  771. if (arg & TUN_F_TSO_ECN)
  772. feature_mask |= NETIF_F_TSO_ECN;
  773. if (arg & TUN_F_TSO4)
  774. feature_mask |= NETIF_F_TSO;
  775. if (arg & TUN_F_TSO6)
  776. feature_mask |= NETIF_F_TSO6;
  777. }
  778. }
  779. /* tun/tap driver inverts the usage for TSO offloads, where
  780. * setting the TSO bit means that the userspace wants to
  781. * accept TSO frames and turning it off means that user space
  782. * does not support TSO.
  783. * For tap, we have to invert it to mean the same thing.
  784. * When user space turns off TSO, we turn off GSO/LRO so that
  785. * user-space will not receive TSO frames.
  786. */
  787. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6))
  788. features |= RX_OFFLOADS;
  789. else
  790. features &= ~RX_OFFLOADS;
  791. /* tap_features are the same as features on tun/tap and
  792. * reflect user expectations.
  793. */
  794. tap->tap_features = feature_mask;
  795. if (tap->update_features)
  796. tap->update_features(tap, features);
  797. return 0;
  798. }
  799. /*
  800. * provide compatibility with generic tun/tap interface
  801. */
  802. static long tap_ioctl(struct file *file, unsigned int cmd,
  803. unsigned long arg)
  804. {
  805. struct tap_queue *q = file->private_data;
  806. struct tap_dev *tap;
  807. void __user *argp = (void __user *)arg;
  808. struct ifreq __user *ifr = argp;
  809. unsigned int __user *up = argp;
  810. unsigned short u;
  811. int __user *sp = argp;
  812. struct sockaddr sa;
  813. int s;
  814. int ret;
  815. switch (cmd) {
  816. case TUNSETIFF:
  817. /* ignore the name, just look at flags */
  818. if (get_user(u, &ifr->ifr_flags))
  819. return -EFAULT;
  820. ret = 0;
  821. if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
  822. ret = -EINVAL;
  823. else
  824. q->flags = (q->flags & ~TAP_IFFEATURES) | u;
  825. return ret;
  826. case TUNGETIFF:
  827. rtnl_lock();
  828. tap = tap_get_tap_dev(q);
  829. if (!tap) {
  830. rtnl_unlock();
  831. return -ENOLINK;
  832. }
  833. ret = 0;
  834. u = q->flags;
  835. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  836. put_user(u, &ifr->ifr_flags))
  837. ret = -EFAULT;
  838. tap_put_tap_dev(tap);
  839. rtnl_unlock();
  840. return ret;
  841. case TUNSETQUEUE:
  842. if (get_user(u, &ifr->ifr_flags))
  843. return -EFAULT;
  844. rtnl_lock();
  845. ret = tap_ioctl_set_queue(file, u);
  846. rtnl_unlock();
  847. return ret;
  848. case TUNGETFEATURES:
  849. if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
  850. return -EFAULT;
  851. return 0;
  852. case TUNSETSNDBUF:
  853. if (get_user(s, sp))
  854. return -EFAULT;
  855. q->sk.sk_sndbuf = s;
  856. return 0;
  857. case TUNGETVNETHDRSZ:
  858. s = q->vnet_hdr_sz;
  859. if (put_user(s, sp))
  860. return -EFAULT;
  861. return 0;
  862. case TUNSETVNETHDRSZ:
  863. if (get_user(s, sp))
  864. return -EFAULT;
  865. if (s < (int)sizeof(struct virtio_net_hdr))
  866. return -EINVAL;
  867. q->vnet_hdr_sz = s;
  868. return 0;
  869. case TUNGETVNETLE:
  870. s = !!(q->flags & TAP_VNET_LE);
  871. if (put_user(s, sp))
  872. return -EFAULT;
  873. return 0;
  874. case TUNSETVNETLE:
  875. if (get_user(s, sp))
  876. return -EFAULT;
  877. if (s)
  878. q->flags |= TAP_VNET_LE;
  879. else
  880. q->flags &= ~TAP_VNET_LE;
  881. return 0;
  882. case TUNGETVNETBE:
  883. return tap_get_vnet_be(q, sp);
  884. case TUNSETVNETBE:
  885. return tap_set_vnet_be(q, sp);
  886. case TUNSETOFFLOAD:
  887. /* let the user check for future flags */
  888. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  889. TUN_F_TSO_ECN))
  890. return -EINVAL;
  891. rtnl_lock();
  892. ret = set_offload(q, arg);
  893. rtnl_unlock();
  894. return ret;
  895. case SIOCGIFHWADDR:
  896. rtnl_lock();
  897. tap = tap_get_tap_dev(q);
  898. if (!tap) {
  899. rtnl_unlock();
  900. return -ENOLINK;
  901. }
  902. ret = 0;
  903. u = tap->dev->type;
  904. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  905. copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) ||
  906. put_user(u, &ifr->ifr_hwaddr.sa_family))
  907. ret = -EFAULT;
  908. tap_put_tap_dev(tap);
  909. rtnl_unlock();
  910. return ret;
  911. case SIOCSIFHWADDR:
  912. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  913. return -EFAULT;
  914. rtnl_lock();
  915. tap = tap_get_tap_dev(q);
  916. if (!tap) {
  917. rtnl_unlock();
  918. return -ENOLINK;
  919. }
  920. ret = dev_set_mac_address(tap->dev, &sa);
  921. tap_put_tap_dev(tap);
  922. rtnl_unlock();
  923. return ret;
  924. default:
  925. return -EINVAL;
  926. }
  927. }
  928. #ifdef CONFIG_COMPAT
  929. static long tap_compat_ioctl(struct file *file, unsigned int cmd,
  930. unsigned long arg)
  931. {
  932. return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  933. }
  934. #endif
  935. static const struct file_operations tap_fops = {
  936. .owner = THIS_MODULE,
  937. .open = tap_open,
  938. .release = tap_release,
  939. .read_iter = tap_read_iter,
  940. .write_iter = tap_write_iter,
  941. .poll = tap_poll,
  942. .llseek = no_llseek,
  943. .unlocked_ioctl = tap_ioctl,
  944. #ifdef CONFIG_COMPAT
  945. .compat_ioctl = tap_compat_ioctl,
  946. #endif
  947. };
  948. static int tap_sendmsg(struct socket *sock, struct msghdr *m,
  949. size_t total_len)
  950. {
  951. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  952. return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  953. }
  954. static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  955. size_t total_len, int flags)
  956. {
  957. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  958. int ret;
  959. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  960. return -EINVAL;
  961. ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
  962. m->msg_control);
  963. if (ret > total_len) {
  964. m->msg_flags |= MSG_TRUNC;
  965. ret = flags & MSG_TRUNC ? ret : total_len;
  966. }
  967. return ret;
  968. }
  969. static int tap_peek_len(struct socket *sock)
  970. {
  971. struct tap_queue *q = container_of(sock, struct tap_queue,
  972. sock);
  973. return skb_array_peek_len(&q->skb_array);
  974. }
  975. /* Ops structure to mimic raw sockets with tun */
  976. static const struct proto_ops tap_socket_ops = {
  977. .sendmsg = tap_sendmsg,
  978. .recvmsg = tap_recvmsg,
  979. .peek_len = tap_peek_len,
  980. };
  981. /* Get an underlying socket object from tun file. Returns error unless file is
  982. * attached to a device. The returned object works like a packet socket, it
  983. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  984. * holding a reference to the file for as long as the socket is in use. */
  985. struct socket *tap_get_socket(struct file *file)
  986. {
  987. struct tap_queue *q;
  988. if (file->f_op != &tap_fops)
  989. return ERR_PTR(-EINVAL);
  990. q = file->private_data;
  991. if (!q)
  992. return ERR_PTR(-EBADFD);
  993. return &q->sock;
  994. }
  995. EXPORT_SYMBOL_GPL(tap_get_socket);
  996. struct skb_array *tap_get_skb_array(struct file *file)
  997. {
  998. struct tap_queue *q;
  999. if (file->f_op != &tap_fops)
  1000. return ERR_PTR(-EINVAL);
  1001. q = file->private_data;
  1002. if (!q)
  1003. return ERR_PTR(-EBADFD);
  1004. return &q->skb_array;
  1005. }
  1006. EXPORT_SYMBOL_GPL(tap_get_skb_array);
  1007. int tap_queue_resize(struct tap_dev *tap)
  1008. {
  1009. struct net_device *dev = tap->dev;
  1010. struct tap_queue *q;
  1011. struct skb_array **arrays;
  1012. int n = tap->numqueues;
  1013. int ret, i = 0;
  1014. arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
  1015. if (!arrays)
  1016. return -ENOMEM;
  1017. list_for_each_entry(q, &tap->queue_list, next)
  1018. arrays[i++] = &q->skb_array;
  1019. ret = skb_array_resize_multiple(arrays, n,
  1020. dev->tx_queue_len, GFP_KERNEL);
  1021. kfree(arrays);
  1022. return ret;
  1023. }
  1024. EXPORT_SYMBOL_GPL(tap_queue_resize);
  1025. static int tap_list_add(dev_t major, const char *device_name)
  1026. {
  1027. struct major_info *tap_major;
  1028. tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
  1029. if (!tap_major)
  1030. return -ENOMEM;
  1031. tap_major->major = MAJOR(major);
  1032. idr_init(&tap_major->minor_idr);
  1033. spin_lock_init(&tap_major->minor_lock);
  1034. tap_major->device_name = device_name;
  1035. list_add_tail_rcu(&tap_major->next, &major_list);
  1036. return 0;
  1037. }
  1038. int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
  1039. const char *device_name, struct module *module)
  1040. {
  1041. int err;
  1042. err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
  1043. if (err)
  1044. goto out1;
  1045. cdev_init(tap_cdev, &tap_fops);
  1046. tap_cdev->owner = module;
  1047. err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
  1048. if (err)
  1049. goto out2;
  1050. err = tap_list_add(*tap_major, device_name);
  1051. if (err)
  1052. goto out3;
  1053. return 0;
  1054. out3:
  1055. cdev_del(tap_cdev);
  1056. out2:
  1057. unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
  1058. out1:
  1059. return err;
  1060. }
  1061. EXPORT_SYMBOL_GPL(tap_create_cdev);
  1062. void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
  1063. {
  1064. struct major_info *tap_major, *tmp;
  1065. cdev_del(tap_cdev);
  1066. unregister_chrdev_region(major, TAP_NUM_DEVS);
  1067. list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
  1068. if (tap_major->major == MAJOR(major)) {
  1069. idr_destroy(&tap_major->minor_idr);
  1070. list_del_rcu(&tap_major->next);
  1071. kfree_rcu(tap_major, rcu);
  1072. }
  1073. }
  1074. }
  1075. EXPORT_SYMBOL_GPL(tap_destroy_cdev);
  1076. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1077. MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
  1078. MODULE_LICENSE("GPL");