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. struct mutex 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. mutex_lock(&tap_major->minor_lock);
  344. retval = idr_alloc(&tap_major->minor_idr, tap, 1, TAP_NUM_DEVS, GFP_KERNEL);
  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. mutex_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. mutex_lock(&tap_major->minor_lock);
  366. if (tap->minor) {
  367. idr_remove(&tap_major->minor_idr, tap->minor);
  368. tap->minor = 0;
  369. }
  370. mutex_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. mutex_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. mutex_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. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  428. init_waitqueue_head(&q->wq.wait);
  429. q->sock.type = SOCK_RAW;
  430. q->sock.state = SS_CONNECTED;
  431. q->sock.file = file;
  432. q->sock.ops = &tap_socket_ops;
  433. sock_init_data(&q->sock, &q->sk);
  434. q->sk.sk_write_space = tap_sock_write_space;
  435. q->sk.sk_destruct = tap_sock_destruct;
  436. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  437. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  438. /*
  439. * so far only KVM virtio_net uses tap, enable zero copy between
  440. * guest kernel and host kernel when lower device supports zerocopy
  441. *
  442. * The macvlan supports zerocopy iff the lower device supports zero
  443. * copy so we don't have to look at the lower device directly.
  444. */
  445. if ((tap->dev->features & NETIF_F_HIGHDMA) && (tap->dev->features & NETIF_F_SG))
  446. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  447. err = -ENOMEM;
  448. if (skb_array_init(&q->skb_array, tap->dev->tx_queue_len, GFP_KERNEL))
  449. goto err_array;
  450. err = tap_set_queue(tap, file, q);
  451. if (err)
  452. goto err_queue;
  453. dev_put(tap->dev);
  454. rtnl_unlock();
  455. return err;
  456. err_queue:
  457. skb_array_cleanup(&q->skb_array);
  458. err_array:
  459. sock_put(&q->sk);
  460. err:
  461. if (tap)
  462. dev_put(tap->dev);
  463. rtnl_unlock();
  464. return err;
  465. }
  466. static int tap_release(struct inode *inode, struct file *file)
  467. {
  468. struct tap_queue *q = file->private_data;
  469. tap_put_queue(q);
  470. return 0;
  471. }
  472. static unsigned int tap_poll(struct file *file, poll_table *wait)
  473. {
  474. struct tap_queue *q = file->private_data;
  475. unsigned int mask = POLLERR;
  476. if (!q)
  477. goto out;
  478. mask = 0;
  479. poll_wait(file, &q->wq.wait, wait);
  480. if (!skb_array_empty(&q->skb_array))
  481. mask |= POLLIN | POLLRDNORM;
  482. if (sock_writeable(&q->sk) ||
  483. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &q->sock.flags) &&
  484. sock_writeable(&q->sk)))
  485. mask |= POLLOUT | POLLWRNORM;
  486. out:
  487. return mask;
  488. }
  489. static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
  490. size_t len, size_t linear,
  491. int noblock, int *err)
  492. {
  493. struct sk_buff *skb;
  494. /* Under a page? Don't bother with paged skb. */
  495. if (prepad + len < PAGE_SIZE || !linear)
  496. linear = len;
  497. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  498. err, 0);
  499. if (!skb)
  500. return NULL;
  501. skb_reserve(skb, prepad);
  502. skb_put(skb, linear);
  503. skb->data_len = len - linear;
  504. skb->len += len - linear;
  505. return skb;
  506. }
  507. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  508. #define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  509. /* Get packet from user space buffer */
  510. static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
  511. struct iov_iter *from, int noblock)
  512. {
  513. int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
  514. struct sk_buff *skb;
  515. struct tap_dev *tap;
  516. unsigned long total_len = iov_iter_count(from);
  517. unsigned long len = total_len;
  518. int err;
  519. struct virtio_net_hdr vnet_hdr = { 0 };
  520. int vnet_hdr_len = 0;
  521. int copylen = 0;
  522. int depth;
  523. bool zerocopy = false;
  524. size_t linear;
  525. if (q->flags & IFF_VNET_HDR) {
  526. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  527. err = -EINVAL;
  528. if (len < vnet_hdr_len)
  529. goto err;
  530. len -= vnet_hdr_len;
  531. err = -EFAULT;
  532. if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
  533. goto err;
  534. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  535. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  536. tap16_to_cpu(q, vnet_hdr.csum_start) +
  537. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  538. tap16_to_cpu(q, vnet_hdr.hdr_len))
  539. vnet_hdr.hdr_len = cpu_to_tap16(q,
  540. tap16_to_cpu(q, vnet_hdr.csum_start) +
  541. tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  542. err = -EINVAL;
  543. if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  544. goto err;
  545. }
  546. err = -EINVAL;
  547. if (unlikely(len < ETH_HLEN))
  548. goto err;
  549. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  550. struct iov_iter i;
  551. copylen = vnet_hdr.hdr_len ?
  552. tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  553. if (copylen > good_linear)
  554. copylen = good_linear;
  555. else if (copylen < ETH_HLEN)
  556. copylen = ETH_HLEN;
  557. linear = copylen;
  558. i = *from;
  559. iov_iter_advance(&i, copylen);
  560. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  561. zerocopy = true;
  562. }
  563. if (!zerocopy) {
  564. copylen = len;
  565. linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
  566. if (linear > good_linear)
  567. linear = good_linear;
  568. else if (linear < ETH_HLEN)
  569. linear = ETH_HLEN;
  570. }
  571. skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
  572. linear, noblock, &err);
  573. if (!skb)
  574. goto err;
  575. if (zerocopy)
  576. err = zerocopy_sg_from_iter(skb, from);
  577. else
  578. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  579. if (err)
  580. goto err_kfree;
  581. skb_set_network_header(skb, ETH_HLEN);
  582. skb_reset_mac_header(skb);
  583. skb->protocol = eth_hdr(skb)->h_proto;
  584. if (vnet_hdr_len) {
  585. err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
  586. tap_is_little_endian(q));
  587. if (err)
  588. goto err_kfree;
  589. }
  590. skb_probe_transport_header(skb, ETH_HLEN);
  591. /* Move network header to the right position for VLAN tagged packets */
  592. if ((skb->protocol == htons(ETH_P_8021Q) ||
  593. skb->protocol == htons(ETH_P_8021AD)) &&
  594. __vlan_get_protocol(skb, skb->protocol, &depth) != 0)
  595. skb_set_network_header(skb, depth);
  596. rcu_read_lock();
  597. tap = rcu_dereference(q->tap);
  598. /* copy skb_ubuf_info for callback when skb has no error */
  599. if (zerocopy) {
  600. skb_shinfo(skb)->destructor_arg = m->msg_control;
  601. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  602. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  603. } else if (m && m->msg_control) {
  604. struct ubuf_info *uarg = m->msg_control;
  605. uarg->callback(uarg, false);
  606. }
  607. if (tap) {
  608. skb->dev = tap->dev;
  609. dev_queue_xmit(skb);
  610. } else {
  611. kfree_skb(skb);
  612. }
  613. rcu_read_unlock();
  614. return total_len;
  615. err_kfree:
  616. kfree_skb(skb);
  617. err:
  618. rcu_read_lock();
  619. tap = rcu_dereference(q->tap);
  620. if (tap && tap->count_tx_dropped)
  621. tap->count_tx_dropped(tap);
  622. rcu_read_unlock();
  623. return err;
  624. }
  625. static ssize_t tap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  626. {
  627. struct file *file = iocb->ki_filp;
  628. struct tap_queue *q = file->private_data;
  629. return tap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  630. }
  631. /* Put packet to the user space buffer */
  632. static ssize_t tap_put_user(struct tap_queue *q,
  633. const struct sk_buff *skb,
  634. struct iov_iter *iter)
  635. {
  636. int ret;
  637. int vnet_hdr_len = 0;
  638. int vlan_offset = 0;
  639. int total;
  640. if (q->flags & IFF_VNET_HDR) {
  641. struct virtio_net_hdr vnet_hdr;
  642. vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
  643. if (iov_iter_count(iter) < vnet_hdr_len)
  644. return -EINVAL;
  645. if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
  646. tap_is_little_endian(q), true))
  647. BUG();
  648. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  649. sizeof(vnet_hdr))
  650. return -EFAULT;
  651. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  652. }
  653. total = vnet_hdr_len;
  654. total += skb->len;
  655. if (skb_vlan_tag_present(skb)) {
  656. struct {
  657. __be16 h_vlan_proto;
  658. __be16 h_vlan_TCI;
  659. } veth;
  660. veth.h_vlan_proto = skb->vlan_proto;
  661. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  662. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  663. total += VLAN_HLEN;
  664. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  665. if (ret || !iov_iter_count(iter))
  666. goto done;
  667. ret = copy_to_iter(&veth, sizeof(veth), iter);
  668. if (ret != sizeof(veth) || !iov_iter_count(iter))
  669. goto done;
  670. }
  671. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  672. skb->len - vlan_offset);
  673. done:
  674. return ret ? ret : total;
  675. }
  676. static ssize_t tap_do_read(struct tap_queue *q,
  677. struct iov_iter *to,
  678. int noblock, struct sk_buff *skb)
  679. {
  680. DEFINE_WAIT(wait);
  681. ssize_t ret = 0;
  682. if (!iov_iter_count(to))
  683. return 0;
  684. if (skb)
  685. goto put;
  686. while (1) {
  687. if (!noblock)
  688. prepare_to_wait(sk_sleep(&q->sk), &wait,
  689. TASK_INTERRUPTIBLE);
  690. /* Read frames from the queue */
  691. skb = skb_array_consume(&q->skb_array);
  692. if (skb)
  693. break;
  694. if (noblock) {
  695. ret = -EAGAIN;
  696. break;
  697. }
  698. if (signal_pending(current)) {
  699. ret = -ERESTARTSYS;
  700. break;
  701. }
  702. /* Nothing to read, let's sleep */
  703. schedule();
  704. }
  705. if (!noblock)
  706. finish_wait(sk_sleep(&q->sk), &wait);
  707. put:
  708. if (skb) {
  709. ret = tap_put_user(q, skb, to);
  710. if (unlikely(ret < 0))
  711. kfree_skb(skb);
  712. else
  713. consume_skb(skb);
  714. }
  715. return ret;
  716. }
  717. static ssize_t tap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  718. {
  719. struct file *file = iocb->ki_filp;
  720. struct tap_queue *q = file->private_data;
  721. ssize_t len = iov_iter_count(to), ret;
  722. ret = tap_do_read(q, to, file->f_flags & O_NONBLOCK, NULL);
  723. ret = min_t(ssize_t, ret, len);
  724. if (ret > 0)
  725. iocb->ki_pos = ret;
  726. return ret;
  727. }
  728. static struct tap_dev *tap_get_tap_dev(struct tap_queue *q)
  729. {
  730. struct tap_dev *tap;
  731. ASSERT_RTNL();
  732. tap = rtnl_dereference(q->tap);
  733. if (tap)
  734. dev_hold(tap->dev);
  735. return tap;
  736. }
  737. static void tap_put_tap_dev(struct tap_dev *tap)
  738. {
  739. dev_put(tap->dev);
  740. }
  741. static int tap_ioctl_set_queue(struct file *file, unsigned int flags)
  742. {
  743. struct tap_queue *q = file->private_data;
  744. struct tap_dev *tap;
  745. int ret;
  746. tap = tap_get_tap_dev(q);
  747. if (!tap)
  748. return -EINVAL;
  749. if (flags & IFF_ATTACH_QUEUE)
  750. ret = tap_enable_queue(tap, file, q);
  751. else if (flags & IFF_DETACH_QUEUE)
  752. ret = tap_disable_queue(q);
  753. else
  754. ret = -EINVAL;
  755. tap_put_tap_dev(tap);
  756. return ret;
  757. }
  758. static int set_offload(struct tap_queue *q, unsigned long arg)
  759. {
  760. struct tap_dev *tap;
  761. netdev_features_t features;
  762. netdev_features_t feature_mask = 0;
  763. tap = rtnl_dereference(q->tap);
  764. if (!tap)
  765. return -ENOLINK;
  766. features = tap->dev->features;
  767. if (arg & TUN_F_CSUM) {
  768. feature_mask = NETIF_F_HW_CSUM;
  769. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  770. if (arg & TUN_F_TSO_ECN)
  771. feature_mask |= NETIF_F_TSO_ECN;
  772. if (arg & TUN_F_TSO4)
  773. feature_mask |= NETIF_F_TSO;
  774. if (arg & TUN_F_TSO6)
  775. feature_mask |= NETIF_F_TSO6;
  776. }
  777. if (arg & TUN_F_UFO)
  778. feature_mask |= NETIF_F_UFO;
  779. }
  780. /* tun/tap driver inverts the usage for TSO offloads, where
  781. * setting the TSO bit means that the userspace wants to
  782. * accept TSO frames and turning it off means that user space
  783. * does not support TSO.
  784. * For tap, we have to invert it to mean the same thing.
  785. * When user space turns off TSO, we turn off GSO/LRO so that
  786. * user-space will not receive TSO frames.
  787. */
  788. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  789. features |= RX_OFFLOADS;
  790. else
  791. features &= ~RX_OFFLOADS;
  792. /* tap_features are the same as features on tun/tap and
  793. * reflect user expectations.
  794. */
  795. tap->tap_features = feature_mask;
  796. if (tap->update_features)
  797. tap->update_features(tap, features);
  798. return 0;
  799. }
  800. /*
  801. * provide compatibility with generic tun/tap interface
  802. */
  803. static long tap_ioctl(struct file *file, unsigned int cmd,
  804. unsigned long arg)
  805. {
  806. struct tap_queue *q = file->private_data;
  807. struct tap_dev *tap;
  808. void __user *argp = (void __user *)arg;
  809. struct ifreq __user *ifr = argp;
  810. unsigned int __user *up = argp;
  811. unsigned short u;
  812. int __user *sp = argp;
  813. struct sockaddr sa;
  814. int s;
  815. int ret;
  816. switch (cmd) {
  817. case TUNSETIFF:
  818. /* ignore the name, just look at flags */
  819. if (get_user(u, &ifr->ifr_flags))
  820. return -EFAULT;
  821. ret = 0;
  822. if ((u & ~TAP_IFFEATURES) != (IFF_NO_PI | IFF_TAP))
  823. ret = -EINVAL;
  824. else
  825. q->flags = (q->flags & ~TAP_IFFEATURES) | u;
  826. return ret;
  827. case TUNGETIFF:
  828. rtnl_lock();
  829. tap = tap_get_tap_dev(q);
  830. if (!tap) {
  831. rtnl_unlock();
  832. return -ENOLINK;
  833. }
  834. ret = 0;
  835. u = q->flags;
  836. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  837. put_user(u, &ifr->ifr_flags))
  838. ret = -EFAULT;
  839. tap_put_tap_dev(tap);
  840. rtnl_unlock();
  841. return ret;
  842. case TUNSETQUEUE:
  843. if (get_user(u, &ifr->ifr_flags))
  844. return -EFAULT;
  845. rtnl_lock();
  846. ret = tap_ioctl_set_queue(file, u);
  847. rtnl_unlock();
  848. return ret;
  849. case TUNGETFEATURES:
  850. if (put_user(IFF_TAP | IFF_NO_PI | TAP_IFFEATURES, up))
  851. return -EFAULT;
  852. return 0;
  853. case TUNSETSNDBUF:
  854. if (get_user(s, sp))
  855. return -EFAULT;
  856. q->sk.sk_sndbuf = s;
  857. return 0;
  858. case TUNGETVNETHDRSZ:
  859. s = q->vnet_hdr_sz;
  860. if (put_user(s, sp))
  861. return -EFAULT;
  862. return 0;
  863. case TUNSETVNETHDRSZ:
  864. if (get_user(s, sp))
  865. return -EFAULT;
  866. if (s < (int)sizeof(struct virtio_net_hdr))
  867. return -EINVAL;
  868. q->vnet_hdr_sz = s;
  869. return 0;
  870. case TUNGETVNETLE:
  871. s = !!(q->flags & TAP_VNET_LE);
  872. if (put_user(s, sp))
  873. return -EFAULT;
  874. return 0;
  875. case TUNSETVNETLE:
  876. if (get_user(s, sp))
  877. return -EFAULT;
  878. if (s)
  879. q->flags |= TAP_VNET_LE;
  880. else
  881. q->flags &= ~TAP_VNET_LE;
  882. return 0;
  883. case TUNGETVNETBE:
  884. return tap_get_vnet_be(q, sp);
  885. case TUNSETVNETBE:
  886. return tap_set_vnet_be(q, sp);
  887. case TUNSETOFFLOAD:
  888. /* let the user check for future flags */
  889. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  890. TUN_F_TSO_ECN | TUN_F_UFO))
  891. return -EINVAL;
  892. rtnl_lock();
  893. ret = set_offload(q, arg);
  894. rtnl_unlock();
  895. return ret;
  896. case SIOCGIFHWADDR:
  897. rtnl_lock();
  898. tap = tap_get_tap_dev(q);
  899. if (!tap) {
  900. rtnl_unlock();
  901. return -ENOLINK;
  902. }
  903. ret = 0;
  904. u = tap->dev->type;
  905. if (copy_to_user(&ifr->ifr_name, tap->dev->name, IFNAMSIZ) ||
  906. copy_to_user(&ifr->ifr_hwaddr.sa_data, tap->dev->dev_addr, ETH_ALEN) ||
  907. put_user(u, &ifr->ifr_hwaddr.sa_family))
  908. ret = -EFAULT;
  909. tap_put_tap_dev(tap);
  910. rtnl_unlock();
  911. return ret;
  912. case SIOCSIFHWADDR:
  913. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  914. return -EFAULT;
  915. rtnl_lock();
  916. tap = tap_get_tap_dev(q);
  917. if (!tap) {
  918. rtnl_unlock();
  919. return -ENOLINK;
  920. }
  921. ret = dev_set_mac_address(tap->dev, &sa);
  922. tap_put_tap_dev(tap);
  923. rtnl_unlock();
  924. return ret;
  925. default:
  926. return -EINVAL;
  927. }
  928. }
  929. #ifdef CONFIG_COMPAT
  930. static long tap_compat_ioctl(struct file *file, unsigned int cmd,
  931. unsigned long arg)
  932. {
  933. return tap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  934. }
  935. #endif
  936. const struct file_operations tap_fops = {
  937. .owner = THIS_MODULE,
  938. .open = tap_open,
  939. .release = tap_release,
  940. .read_iter = tap_read_iter,
  941. .write_iter = tap_write_iter,
  942. .poll = tap_poll,
  943. .llseek = no_llseek,
  944. .unlocked_ioctl = tap_ioctl,
  945. #ifdef CONFIG_COMPAT
  946. .compat_ioctl = tap_compat_ioctl,
  947. #endif
  948. };
  949. static int tap_sendmsg(struct socket *sock, struct msghdr *m,
  950. size_t total_len)
  951. {
  952. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  953. return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  954. }
  955. static int tap_recvmsg(struct socket *sock, struct msghdr *m,
  956. size_t total_len, int flags)
  957. {
  958. struct tap_queue *q = container_of(sock, struct tap_queue, sock);
  959. int ret;
  960. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  961. return -EINVAL;
  962. ret = tap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT,
  963. m->msg_control);
  964. if (ret > total_len) {
  965. m->msg_flags |= MSG_TRUNC;
  966. ret = flags & MSG_TRUNC ? ret : total_len;
  967. }
  968. return ret;
  969. }
  970. static int tap_peek_len(struct socket *sock)
  971. {
  972. struct tap_queue *q = container_of(sock, struct tap_queue,
  973. sock);
  974. return skb_array_peek_len(&q->skb_array);
  975. }
  976. /* Ops structure to mimic raw sockets with tun */
  977. static const struct proto_ops tap_socket_ops = {
  978. .sendmsg = tap_sendmsg,
  979. .recvmsg = tap_recvmsg,
  980. .peek_len = tap_peek_len,
  981. };
  982. /* Get an underlying socket object from tun file. Returns error unless file is
  983. * attached to a device. The returned object works like a packet socket, it
  984. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  985. * holding a reference to the file for as long as the socket is in use. */
  986. struct socket *tap_get_socket(struct file *file)
  987. {
  988. struct tap_queue *q;
  989. if (file->f_op != &tap_fops)
  990. return ERR_PTR(-EINVAL);
  991. q = file->private_data;
  992. if (!q)
  993. return ERR_PTR(-EBADFD);
  994. return &q->sock;
  995. }
  996. EXPORT_SYMBOL_GPL(tap_get_socket);
  997. struct skb_array *tap_get_skb_array(struct file *file)
  998. {
  999. struct tap_queue *q;
  1000. if (file->f_op != &tap_fops)
  1001. return ERR_PTR(-EINVAL);
  1002. q = file->private_data;
  1003. if (!q)
  1004. return ERR_PTR(-EBADFD);
  1005. return &q->skb_array;
  1006. }
  1007. EXPORT_SYMBOL_GPL(tap_get_skb_array);
  1008. int tap_queue_resize(struct tap_dev *tap)
  1009. {
  1010. struct net_device *dev = tap->dev;
  1011. struct tap_queue *q;
  1012. struct skb_array **arrays;
  1013. int n = tap->numqueues;
  1014. int ret, i = 0;
  1015. arrays = kmalloc(sizeof *arrays * n, GFP_KERNEL);
  1016. if (!arrays)
  1017. return -ENOMEM;
  1018. list_for_each_entry(q, &tap->queue_list, next)
  1019. arrays[i++] = &q->skb_array;
  1020. ret = skb_array_resize_multiple(arrays, n,
  1021. dev->tx_queue_len, GFP_KERNEL);
  1022. kfree(arrays);
  1023. return ret;
  1024. }
  1025. EXPORT_SYMBOL_GPL(tap_queue_resize);
  1026. static int tap_list_add(dev_t major, const char *device_name)
  1027. {
  1028. struct major_info *tap_major;
  1029. tap_major = kzalloc(sizeof(*tap_major), GFP_ATOMIC);
  1030. if (!tap_major)
  1031. return -ENOMEM;
  1032. tap_major->major = MAJOR(major);
  1033. idr_init(&tap_major->minor_idr);
  1034. mutex_init(&tap_major->minor_lock);
  1035. tap_major->device_name = device_name;
  1036. list_add_tail_rcu(&tap_major->next, &major_list);
  1037. return 0;
  1038. }
  1039. int tap_create_cdev(struct cdev *tap_cdev,
  1040. dev_t *tap_major, const char *device_name)
  1041. {
  1042. int err;
  1043. err = alloc_chrdev_region(tap_major, 0, TAP_NUM_DEVS, device_name);
  1044. if (err)
  1045. goto out1;
  1046. cdev_init(tap_cdev, &tap_fops);
  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");