tap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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. if (s <= 0)
  856. return -EINVAL;
  857. q->sk.sk_sndbuf = s;
  858. return 0;
  859. case TUNGETVNETHDRSZ:
  860. s = q->vnet_hdr_sz;
  861. if (put_user(s, sp))
  862. return -EFAULT;
  863. return 0;
  864. case TUNSETVNETHDRSZ:
  865. if (get_user(s, sp))
  866. return -EFAULT;
  867. if (s < (int)sizeof(struct virtio_net_hdr))
  868. return -EINVAL;
  869. q->vnet_hdr_sz = s;
  870. return 0;
  871. case TUNGETVNETLE:
  872. s = !!(q->flags & TAP_VNET_LE);
  873. if (put_user(s, sp))
  874. return -EFAULT;
  875. return 0;
  876. case TUNSETVNETLE:
  877. if (get_user(s, sp))
  878. return -EFAULT;
  879. if (s)
  880. q->flags |= TAP_VNET_LE;
  881. else
  882. q->flags &= ~TAP_VNET_LE;
  883. return 0;
  884. case TUNGETVNETBE:
  885. return tap_get_vnet_be(q, sp);
  886. case TUNSETVNETBE:
  887. return tap_set_vnet_be(q, sp);
  888. case TUNSETOFFLOAD:
  889. /* let the user check for future flags */
  890. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  891. TUN_F_TSO_ECN))
  892. return -EINVAL;
  893. rtnl_lock();
  894. ret = set_offload(q, arg);
  895. rtnl_unlock();
  896. return ret;
  897. case SIOCGIFHWADDR:
  898. rtnl_lock();
  899. tap = tap_get_tap_dev(q);
  900. if (!tap) {
  901. rtnl_unlock();
  902. return -ENOLINK;
  903. }
  904. ret = 0;
  905. u = tap->dev->type;
  906. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  907. copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) ||
  908. put_user(u, &ifr->ifr_hwaddr.sa_family))
  909. ret = -EFAULT;
  910. tap_put_tap_dev(tap);
  911. rtnl_unlock();
  912. return ret;
  913. case SIOCSIFHWADDR:
  914. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  915. return -EFAULT;
  916. rtnl_lock();
  917. tap = tap_get_tap_dev(q);
  918. if (!tap) {
  919. rtnl_unlock();
  920. return -ENOLINK;
  921. }
  922. ret = dev_set_mac_address(tap->dev, &sa);
  923. tap_put_tap_dev(tap);
  924. rtnl_unlock();
  925. return ret;
  926. default:
  927. return -EINVAL;
  928. }
  929. }
  930. #ifdef CONFIG_COMPAT
  931. static long tap_compat_ioctl(struct file *file, unsigned int cmd,
  932. unsigned long arg)
  933. {
  934. return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  935. }
  936. #endif
  937. static const struct file_operations tap_fops = {
  938. .owner = THIS_MODULE,
  939. .open = tap_open,
  940. .release = tap_release,
  941. .read_iter = tap_read_iter,
  942. .write_iter = tap_write_iter,
  943. .poll = tap_poll,
  944. .llseek = no_llseek,
  945. .unlocked_ioctl = tap_ioctl,
  946. #ifdef CONFIG_COMPAT
  947. .compat_ioctl = tap_compat_ioctl,
  948. #endif
  949. };
  950. static int tap_sendmsg(struct socket *sock, struct msghdr *m,
  951. size_t total_len)
  952. {
  953. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  954. return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  955. }
  956. static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  957. size_t total_len, int flags)
  958. {
  959. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  960. int ret;
  961. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  962. return -EINVAL;
  963. ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
  964. m->msg_control);
  965. if (ret > total_len) {
  966. m->msg_flags |= MSG_TRUNC;
  967. ret = flags & MSG_TRUNC ? ret : total_len;
  968. }
  969. return ret;
  970. }
  971. static int tap_peek_len(struct socket *sock)
  972. {
  973. struct tap_queue *q = container_of(sock, struct tap_queue,
  974. sock);
  975. return skb_array_peek_len(&q->skb_array);
  976. }
  977. /* Ops structure to mimic raw sockets with tun */
  978. static const struct proto_ops tap_socket_ops = {
  979. .sendmsg = tap_sendmsg,
  980. .recvmsg = tap_recvmsg,
  981. .peek_len = tap_peek_len,
  982. };
  983. /* Get an underlying socket object from tun file. Returns error unless file is
  984. * attached to a device. The returned object works like a packet socket, it
  985. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  986. * holding a reference to the file for as long as the socket is in use. */
  987. struct socket *tap_get_socket(struct file *file)
  988. {
  989. struct tap_queue *q;
  990. if (file->f_op != &tap_fops)
  991. return ERR_PTR(-EINVAL);
  992. q = file->private_data;
  993. if (!q)
  994. return ERR_PTR(-EBADFD);
  995. return &q->sock;
  996. }
  997. EXPORT_SYMBOL_GPL(tap_get_socket);
  998. struct skb_array *tap_get_skb_array(struct file *file)
  999. {
  1000. struct tap_queue *q;
  1001. if (file->f_op != &tap_fops)
  1002. return ERR_PTR(-EINVAL);
  1003. q = file->private_data;
  1004. if (!q)
  1005. return ERR_PTR(-EBADFD);
  1006. return &q->skb_array;
  1007. }
  1008. EXPORT_SYMBOL_GPL(tap_get_skb_array);
  1009. int tap_queue_resize(struct tap_dev *tap)
  1010. {
  1011. struct net_device *dev = tap->dev;
  1012. struct tap_queue *q;
  1013. struct skb_array **arrays;
  1014. int n = tap->numqueues;
  1015. int ret, i = 0;
  1016. arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL);
  1017. if (!arrays)
  1018. return -ENOMEM;
  1019. list_for_each_entry(q, &tap->queue_list, next)
  1020. arrays[i++] = &q->skb_array;
  1021. ret = skb_array_resize_multiple(arrays, n,
  1022. dev->tx_queue_len, GFP_KERNEL);
  1023. kfree(arrays);
  1024. return ret;
  1025. }
  1026. EXPORT_SYMBOL_GPL(tap_queue_resize);
  1027. static int tap_list_add(dev_t major, const char *device_name)
  1028. {
  1029. struct major_info *tap_major;
  1030. tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
  1031. if (!tap_major)
  1032. return -ENOMEM;
  1033. tap_major->major = MAJOR(major);
  1034. idr_init(&tap_major->minor_idr);
  1035. spin_lock_init(&tap_major->minor_lock);
  1036. tap_major->device_name = device_name;
  1037. list_add_tail_rcu(&tap_major->next, &major_list);
  1038. return 0;
  1039. }
  1040. int tap_create_cdev(struct cdev *tap_cdev, dev_t *tap_major,
  1041. const char *device_name, struct module *module)
  1042. {
  1043. int err;
  1044. err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
  1045. if (err)
  1046. goto out1;
  1047. cdev_init(tap_cdev, &tap_fops);
  1048. tap_cdev->owner = module;
  1049. err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);
  1050. if (err)
  1051. goto out2;
  1052. err = tap_list_add(*tap_major, device_name);
  1053. if (err)
  1054. goto out3;
  1055. return 0;
  1056. out3:
  1057. cdev_del(tap_cdev);
  1058. out2:
  1059. unregister_chrdev_region(*tap_major, TAP_NUM_DEVS);
  1060. out1:
  1061. return err;
  1062. }
  1063. EXPORT_SYMBOL_GPL(tap_create_cdev);
  1064. void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
  1065. {
  1066. struct major_info *tap_major, *tmp;
  1067. cdev_del(tap_cdev);
  1068. unregister_chrdev_region(major, TAP_NUM_DEVS);
  1069. list_for_each_entry_safe(tap_major, tmp, &major_list, next) {
  1070. if (tap_major->major == MAJOR(major)) {
  1071. idr_destroy(&tap_major->minor_idr);
  1072. list_del_rcu(&tap_major->next);
  1073. kfree_rcu(tap_major, rcu);
  1074. }
  1075. }
  1076. }
  1077. EXPORT_SYMBOL_GPL(tap_destroy_cdev);
  1078. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1079. MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
  1080. MODULE_LICENSE("GPL");