macvtap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.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.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/wait.h>
  16. #include <linux/cdev.h>
  17. #include <linux/idr.h>
  18. #include <linux/fs.h>
  19. #include <net/net_namespace.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <linux/virtio_net.h>
  23. /*
  24. * A macvtap queue is the central object of this driver, it connects
  25. * an open character device to a macvlan interface. There can be
  26. * multiple queues on one interface, which map back to queues
  27. * implemented in hardware on the underlying device.
  28. *
  29. * macvtap_proto is used to allocate queues through the sock allocation
  30. * mechanism.
  31. *
  32. */
  33. struct macvtap_queue {
  34. struct sock sk;
  35. struct socket sock;
  36. struct socket_wq wq;
  37. int vnet_hdr_sz;
  38. struct macvlan_dev __rcu *vlan;
  39. struct file *file;
  40. unsigned int flags;
  41. u16 queue_index;
  42. bool enabled;
  43. struct list_head next;
  44. };
  45. static struct proto macvtap_proto = {
  46. .name = "macvtap",
  47. .owner = THIS_MODULE,
  48. .obj_size = sizeof (struct macvtap_queue),
  49. };
  50. /*
  51. * Variables for dealing with macvtaps device numbers.
  52. */
  53. static dev_t macvtap_major;
  54. #define MACVTAP_NUM_DEVS (1U << MINORBITS)
  55. static DEFINE_MUTEX(minor_lock);
  56. static DEFINE_IDR(minor_idr);
  57. #define GOODCOPY_LEN 128
  58. static struct class *macvtap_class;
  59. static struct cdev macvtap_cdev;
  60. static const struct proto_ops macvtap_socket_ops;
  61. /*
  62. * RCU usage:
  63. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  64. * pointers from one to the other can only be read while rcu_read_lock
  65. * or rtnl is held.
  66. *
  67. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  68. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  69. * q->vlan becomes inaccessible. When the files gets closed,
  70. * macvtap_get_queue() fails.
  71. *
  72. * There may still be references to the struct sock inside of the
  73. * queue from outbound SKBs, but these never reference back to the
  74. * file or the dev. The data structure is freed through __sk_free
  75. * when both our references and any pending SKBs are gone.
  76. */
  77. static int macvtap_enable_queue(struct net_device *dev, struct file *file,
  78. struct macvtap_queue *q)
  79. {
  80. struct macvlan_dev *vlan = netdev_priv(dev);
  81. int err = -EINVAL;
  82. ASSERT_RTNL();
  83. if (q->enabled)
  84. goto out;
  85. err = 0;
  86. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  87. q->queue_index = vlan->numvtaps;
  88. q->enabled = true;
  89. vlan->numvtaps++;
  90. out:
  91. return err;
  92. }
  93. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  94. struct macvtap_queue *q)
  95. {
  96. struct macvlan_dev *vlan = netdev_priv(dev);
  97. int err = -EBUSY;
  98. rtnl_lock();
  99. if (vlan->numqueues == MAX_MACVTAP_QUEUES)
  100. goto out;
  101. err = 0;
  102. rcu_assign_pointer(q->vlan, vlan);
  103. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  104. sock_hold(&q->sk);
  105. q->file = file;
  106. q->queue_index = vlan->numvtaps;
  107. q->enabled = true;
  108. file->private_data = q;
  109. list_add_tail(&q->next, &vlan->queue_list);
  110. vlan->numvtaps++;
  111. vlan->numqueues++;
  112. out:
  113. rtnl_unlock();
  114. return err;
  115. }
  116. static int macvtap_disable_queue(struct macvtap_queue *q)
  117. {
  118. struct macvlan_dev *vlan;
  119. struct macvtap_queue *nq;
  120. ASSERT_RTNL();
  121. if (!q->enabled)
  122. return -EINVAL;
  123. vlan = rtnl_dereference(q->vlan);
  124. if (vlan) {
  125. int index = q->queue_index;
  126. BUG_ON(index >= vlan->numvtaps);
  127. nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
  128. nq->queue_index = index;
  129. rcu_assign_pointer(vlan->taps[index], nq);
  130. RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
  131. q->enabled = false;
  132. vlan->numvtaps--;
  133. }
  134. return 0;
  135. }
  136. /*
  137. * The file owning the queue got closed, give up both
  138. * the reference that the files holds as well as the
  139. * one from the macvlan_dev if that still exists.
  140. *
  141. * Using the spinlock makes sure that we don't get
  142. * to the queue again after destroying it.
  143. */
  144. static void macvtap_put_queue(struct macvtap_queue *q)
  145. {
  146. struct macvlan_dev *vlan;
  147. rtnl_lock();
  148. vlan = rtnl_dereference(q->vlan);
  149. if (vlan) {
  150. if (q->enabled)
  151. BUG_ON(macvtap_disable_queue(q));
  152. vlan->numqueues--;
  153. RCU_INIT_POINTER(q->vlan, NULL);
  154. sock_put(&q->sk);
  155. list_del_init(&q->next);
  156. }
  157. rtnl_unlock();
  158. synchronize_rcu();
  159. sock_put(&q->sk);
  160. }
  161. /*
  162. * Select a queue based on the rxq of the device on which this packet
  163. * arrived. If the incoming device is not mq, calculate a flow hash
  164. * to select a queue. If all fails, find the first available queue.
  165. * Cache vlan->numvtaps since it can become zero during the execution
  166. * of this function.
  167. */
  168. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  169. struct sk_buff *skb)
  170. {
  171. struct macvlan_dev *vlan = netdev_priv(dev);
  172. struct macvtap_queue *tap = NULL;
  173. /* Access to taps array is protected by rcu, but access to numvtaps
  174. * isn't. Below we use it to lookup a queue, but treat it as a hint
  175. * and validate that the result isn't NULL - in case we are
  176. * racing against queue removal.
  177. */
  178. int numvtaps = ACCESS_ONCE(vlan->numvtaps);
  179. __u32 rxq;
  180. if (!numvtaps)
  181. goto out;
  182. /* Check if we can use flow to select a queue */
  183. rxq = skb_get_rxhash(skb);
  184. if (rxq) {
  185. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  186. goto out;
  187. }
  188. if (likely(skb_rx_queue_recorded(skb))) {
  189. rxq = skb_get_rx_queue(skb);
  190. while (unlikely(rxq >= numvtaps))
  191. rxq -= numvtaps;
  192. tap = rcu_dereference(vlan->taps[rxq]);
  193. goto out;
  194. }
  195. tap = rcu_dereference(vlan->taps[0]);
  196. out:
  197. return tap;
  198. }
  199. /*
  200. * The net_device is going away, give up the reference
  201. * that it holds on all queues and safely set the pointer
  202. * from the queues to NULL.
  203. */
  204. static void macvtap_del_queues(struct net_device *dev)
  205. {
  206. struct macvlan_dev *vlan = netdev_priv(dev);
  207. struct macvtap_queue *q, *tmp, *qlist[MAX_MACVTAP_QUEUES];
  208. int i, j = 0;
  209. ASSERT_RTNL();
  210. list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
  211. list_del_init(&q->next);
  212. qlist[j++] = q;
  213. RCU_INIT_POINTER(q->vlan, NULL);
  214. if (q->enabled)
  215. vlan->numvtaps--;
  216. vlan->numqueues--;
  217. }
  218. for (i = 0; i < vlan->numvtaps; i++)
  219. RCU_INIT_POINTER(vlan->taps[i], NULL);
  220. BUG_ON(vlan->numvtaps);
  221. BUG_ON(vlan->numqueues);
  222. /* guarantee that any future macvtap_set_queue will fail */
  223. vlan->numvtaps = MAX_MACVTAP_QUEUES;
  224. for (--j; j >= 0; j--)
  225. sock_put(&qlist[j]->sk);
  226. }
  227. /*
  228. * Forward happens for data that gets sent from one macvlan
  229. * endpoint to another one in bridge mode. We just take
  230. * the skb and put it into the receive queue.
  231. */
  232. static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
  233. {
  234. struct macvtap_queue *q = macvtap_get_queue(dev, skb);
  235. if (!q)
  236. goto drop;
  237. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  238. goto drop;
  239. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  240. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  241. return NET_RX_SUCCESS;
  242. drop:
  243. kfree_skb(skb);
  244. return NET_RX_DROP;
  245. }
  246. /*
  247. * Receive is for data from the external interface (lowerdev),
  248. * in case of macvtap, we can treat that the same way as
  249. * forward, which macvlan cannot.
  250. */
  251. static int macvtap_receive(struct sk_buff *skb)
  252. {
  253. skb_push(skb, ETH_HLEN);
  254. return macvtap_forward(skb->dev, skb);
  255. }
  256. static int macvtap_get_minor(struct macvlan_dev *vlan)
  257. {
  258. int retval = -ENOMEM;
  259. mutex_lock(&minor_lock);
  260. retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL);
  261. if (retval >= 0) {
  262. vlan->minor = retval;
  263. } else if (retval == -ENOSPC) {
  264. printk(KERN_ERR "too many macvtap devices\n");
  265. retval = -EINVAL;
  266. }
  267. mutex_unlock(&minor_lock);
  268. return retval < 0 ? retval : 0;
  269. }
  270. static void macvtap_free_minor(struct macvlan_dev *vlan)
  271. {
  272. mutex_lock(&minor_lock);
  273. if (vlan->minor) {
  274. idr_remove(&minor_idr, vlan->minor);
  275. vlan->minor = 0;
  276. }
  277. mutex_unlock(&minor_lock);
  278. }
  279. static struct net_device *dev_get_by_macvtap_minor(int minor)
  280. {
  281. struct net_device *dev = NULL;
  282. struct macvlan_dev *vlan;
  283. mutex_lock(&minor_lock);
  284. vlan = idr_find(&minor_idr, minor);
  285. if (vlan) {
  286. dev = vlan->dev;
  287. dev_hold(dev);
  288. }
  289. mutex_unlock(&minor_lock);
  290. return dev;
  291. }
  292. static int macvtap_newlink(struct net *src_net,
  293. struct net_device *dev,
  294. struct nlattr *tb[],
  295. struct nlattr *data[])
  296. {
  297. struct macvlan_dev *vlan = netdev_priv(dev);
  298. INIT_LIST_HEAD(&vlan->queue_list);
  299. /* Don't put anything that may fail after macvlan_common_newlink
  300. * because we can't undo what it does.
  301. */
  302. return macvlan_common_newlink(src_net, dev, tb, data,
  303. macvtap_receive, macvtap_forward);
  304. }
  305. static void macvtap_dellink(struct net_device *dev,
  306. struct list_head *head)
  307. {
  308. macvtap_del_queues(dev);
  309. macvlan_dellink(dev, head);
  310. }
  311. static void macvtap_setup(struct net_device *dev)
  312. {
  313. macvlan_common_setup(dev);
  314. dev->tx_queue_len = TUN_READQ_SIZE;
  315. }
  316. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  317. .kind = "macvtap",
  318. .setup = macvtap_setup,
  319. .newlink = macvtap_newlink,
  320. .dellink = macvtap_dellink,
  321. };
  322. static void macvtap_sock_write_space(struct sock *sk)
  323. {
  324. wait_queue_head_t *wqueue;
  325. if (!sock_writeable(sk) ||
  326. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  327. return;
  328. wqueue = sk_sleep(sk);
  329. if (wqueue && waitqueue_active(wqueue))
  330. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  331. }
  332. static void macvtap_sock_destruct(struct sock *sk)
  333. {
  334. skb_queue_purge(&sk->sk_receive_queue);
  335. }
  336. static int macvtap_open(struct inode *inode, struct file *file)
  337. {
  338. struct net *net = current->nsproxy->net_ns;
  339. struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
  340. struct macvtap_queue *q;
  341. int err;
  342. err = -ENODEV;
  343. if (!dev)
  344. goto out;
  345. err = -ENOMEM;
  346. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  347. &macvtap_proto);
  348. if (!q)
  349. goto out;
  350. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  351. init_waitqueue_head(&q->wq.wait);
  352. q->sock.type = SOCK_RAW;
  353. q->sock.state = SS_CONNECTED;
  354. q->sock.file = file;
  355. q->sock.ops = &macvtap_socket_ops;
  356. sock_init_data(&q->sock, &q->sk);
  357. q->sk.sk_write_space = macvtap_sock_write_space;
  358. q->sk.sk_destruct = macvtap_sock_destruct;
  359. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  360. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  361. /*
  362. * so far only KVM virtio_net uses macvtap, enable zero copy between
  363. * guest kernel and host kernel when lower device supports zerocopy
  364. *
  365. * The macvlan supports zerocopy iff the lower device supports zero
  366. * copy so we don't have to look at the lower device directly.
  367. */
  368. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  369. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  370. err = macvtap_set_queue(dev, file, q);
  371. if (err)
  372. sock_put(&q->sk);
  373. out:
  374. if (dev)
  375. dev_put(dev);
  376. return err;
  377. }
  378. static int macvtap_release(struct inode *inode, struct file *file)
  379. {
  380. struct macvtap_queue *q = file->private_data;
  381. macvtap_put_queue(q);
  382. return 0;
  383. }
  384. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  385. {
  386. struct macvtap_queue *q = file->private_data;
  387. unsigned int mask = POLLERR;
  388. if (!q)
  389. goto out;
  390. mask = 0;
  391. poll_wait(file, &q->wq.wait, wait);
  392. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  393. mask |= POLLIN | POLLRDNORM;
  394. if (sock_writeable(&q->sk) ||
  395. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  396. sock_writeable(&q->sk)))
  397. mask |= POLLOUT | POLLWRNORM;
  398. out:
  399. return mask;
  400. }
  401. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  402. size_t len, size_t linear,
  403. int noblock, int *err)
  404. {
  405. struct sk_buff *skb;
  406. /* Under a page? Don't bother with paged skb. */
  407. if (prepad + len < PAGE_SIZE || !linear)
  408. linear = len;
  409. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  410. err);
  411. if (!skb)
  412. return NULL;
  413. skb_reserve(skb, prepad);
  414. skb_put(skb, linear);
  415. skb->data_len = len - linear;
  416. skb->len += len - linear;
  417. return skb;
  418. }
  419. /* set skb frags from iovec, this can move to core network code for reuse */
  420. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  421. int offset, size_t count)
  422. {
  423. int len = iov_length(from, count) - offset;
  424. int copy = skb_headlen(skb);
  425. int size, offset1 = 0;
  426. int i = 0;
  427. /* Skip over from offset */
  428. while (count && (offset >= from->iov_len)) {
  429. offset -= from->iov_len;
  430. ++from;
  431. --count;
  432. }
  433. /* copy up to skb headlen */
  434. while (count && (copy > 0)) {
  435. size = min_t(unsigned int, copy, from->iov_len - offset);
  436. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  437. size))
  438. return -EFAULT;
  439. if (copy > size) {
  440. ++from;
  441. --count;
  442. offset = 0;
  443. } else
  444. offset += size;
  445. copy -= size;
  446. offset1 += size;
  447. }
  448. if (len == offset1)
  449. return 0;
  450. while (count--) {
  451. struct page *page[MAX_SKB_FRAGS];
  452. int num_pages;
  453. unsigned long base;
  454. unsigned long truesize;
  455. len = from->iov_len - offset;
  456. if (!len) {
  457. offset = 0;
  458. ++from;
  459. continue;
  460. }
  461. base = (unsigned long)from->iov_base + offset;
  462. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  463. if (i + size > MAX_SKB_FRAGS)
  464. return -EMSGSIZE;
  465. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  466. if (num_pages != size) {
  467. for (i = 0; i < num_pages; i++)
  468. put_page(page[i]);
  469. return -EFAULT;
  470. }
  471. truesize = size * PAGE_SIZE;
  472. skb->data_len += len;
  473. skb->len += len;
  474. skb->truesize += truesize;
  475. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  476. while (len) {
  477. int off = base & ~PAGE_MASK;
  478. int size = min_t(int, len, PAGE_SIZE - off);
  479. __skb_fill_page_desc(skb, i, page[i], off, size);
  480. skb_shinfo(skb)->nr_frags++;
  481. /* increase sk_wmem_alloc */
  482. base += size;
  483. len -= size;
  484. i++;
  485. }
  486. offset = 0;
  487. ++from;
  488. }
  489. return 0;
  490. }
  491. /*
  492. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  493. * be shared with the tun/tap driver.
  494. */
  495. static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
  496. struct virtio_net_hdr *vnet_hdr)
  497. {
  498. unsigned short gso_type = 0;
  499. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  500. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  501. case VIRTIO_NET_HDR_GSO_TCPV4:
  502. gso_type = SKB_GSO_TCPV4;
  503. break;
  504. case VIRTIO_NET_HDR_GSO_TCPV6:
  505. gso_type = SKB_GSO_TCPV6;
  506. break;
  507. case VIRTIO_NET_HDR_GSO_UDP:
  508. gso_type = SKB_GSO_UDP;
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  514. gso_type |= SKB_GSO_TCP_ECN;
  515. if (vnet_hdr->gso_size == 0)
  516. return -EINVAL;
  517. }
  518. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  519. if (!skb_partial_csum_set(skb, vnet_hdr->csum_start,
  520. vnet_hdr->csum_offset))
  521. return -EINVAL;
  522. }
  523. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  524. skb_shinfo(skb)->gso_size = vnet_hdr->gso_size;
  525. skb_shinfo(skb)->gso_type = gso_type;
  526. /* Header must be checked, and gso_segs computed. */
  527. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  528. skb_shinfo(skb)->gso_segs = 0;
  529. }
  530. return 0;
  531. }
  532. static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
  533. struct virtio_net_hdr *vnet_hdr)
  534. {
  535. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  536. if (skb_is_gso(skb)) {
  537. struct skb_shared_info *sinfo = skb_shinfo(skb);
  538. /* This is a hint as to how much should be linear. */
  539. vnet_hdr->hdr_len = skb_headlen(skb);
  540. vnet_hdr->gso_size = sinfo->gso_size;
  541. if (sinfo->gso_type & SKB_GSO_TCPV4)
  542. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  543. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  544. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  545. else if (sinfo->gso_type & SKB_GSO_UDP)
  546. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  547. else
  548. BUG();
  549. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  550. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  551. } else
  552. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  553. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  554. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  555. vnet_hdr->csum_start = skb_checksum_start_offset(skb);
  556. vnet_hdr->csum_offset = skb->csum_offset;
  557. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  558. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  559. } /* else everything is zero */
  560. return 0;
  561. }
  562. /* Get packet from user space buffer */
  563. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  564. const struct iovec *iv, unsigned long total_len,
  565. size_t count, int noblock)
  566. {
  567. struct sk_buff *skb;
  568. struct macvlan_dev *vlan;
  569. unsigned long len = total_len;
  570. int err;
  571. struct virtio_net_hdr vnet_hdr = { 0 };
  572. int vnet_hdr_len = 0;
  573. int copylen = 0;
  574. bool zerocopy = false;
  575. if (q->flags & IFF_VNET_HDR) {
  576. vnet_hdr_len = q->vnet_hdr_sz;
  577. err = -EINVAL;
  578. if (len < vnet_hdr_len)
  579. goto err;
  580. len -= vnet_hdr_len;
  581. err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
  582. sizeof(vnet_hdr));
  583. if (err < 0)
  584. goto err;
  585. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  586. vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  587. vnet_hdr.hdr_len)
  588. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  589. vnet_hdr.csum_offset + 2;
  590. err = -EINVAL;
  591. if (vnet_hdr.hdr_len > len)
  592. goto err;
  593. }
  594. err = -EINVAL;
  595. if (unlikely(len < ETH_HLEN))
  596. goto err;
  597. err = -EMSGSIZE;
  598. if (unlikely(count > UIO_MAXIOV))
  599. goto err;
  600. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
  601. zerocopy = true;
  602. if (zerocopy) {
  603. /* Userspace may produce vectors with count greater than
  604. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  605. * to let the rest of data to be fit in the frags.
  606. */
  607. if (count > MAX_SKB_FRAGS) {
  608. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  609. if (copylen < vnet_hdr_len)
  610. copylen = 0;
  611. else
  612. copylen -= vnet_hdr_len;
  613. }
  614. /* There are 256 bytes to be copied in skb, so there is enough
  615. * room for skb expand head in case it is used.
  616. * The rest buffer is mapped from userspace.
  617. */
  618. if (copylen < vnet_hdr.hdr_len)
  619. copylen = vnet_hdr.hdr_len;
  620. if (!copylen)
  621. copylen = GOODCOPY_LEN;
  622. } else
  623. copylen = len;
  624. skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
  625. vnet_hdr.hdr_len, noblock, &err);
  626. if (!skb)
  627. goto err;
  628. if (zerocopy)
  629. err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
  630. else
  631. err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
  632. len);
  633. if (err)
  634. goto err_kfree;
  635. skb_set_network_header(skb, ETH_HLEN);
  636. skb_reset_mac_header(skb);
  637. skb->protocol = eth_hdr(skb)->h_proto;
  638. if (vnet_hdr_len) {
  639. err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
  640. if (err)
  641. goto err_kfree;
  642. }
  643. skb_probe_transport_header(skb, ETH_HLEN);
  644. rcu_read_lock();
  645. vlan = rcu_dereference(q->vlan);
  646. /* copy skb_ubuf_info for callback when skb has no error */
  647. if (zerocopy) {
  648. skb_shinfo(skb)->destructor_arg = m->msg_control;
  649. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  650. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  651. }
  652. if (vlan)
  653. macvlan_start_xmit(skb, vlan->dev);
  654. else
  655. kfree_skb(skb);
  656. rcu_read_unlock();
  657. return total_len;
  658. err_kfree:
  659. kfree_skb(skb);
  660. err:
  661. rcu_read_lock();
  662. vlan = rcu_dereference(q->vlan);
  663. if (vlan)
  664. vlan->dev->stats.tx_dropped++;
  665. rcu_read_unlock();
  666. return err;
  667. }
  668. static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
  669. unsigned long count, loff_t pos)
  670. {
  671. struct file *file = iocb->ki_filp;
  672. ssize_t result = -ENOLINK;
  673. struct macvtap_queue *q = file->private_data;
  674. result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
  675. file->f_flags & O_NONBLOCK);
  676. return result;
  677. }
  678. /* Put packet to the user space buffer */
  679. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  680. const struct sk_buff *skb,
  681. const struct iovec *iv, int len)
  682. {
  683. struct macvlan_dev *vlan;
  684. int ret;
  685. int vnet_hdr_len = 0;
  686. int vlan_offset = 0;
  687. int copied;
  688. if (q->flags & IFF_VNET_HDR) {
  689. struct virtio_net_hdr vnet_hdr;
  690. vnet_hdr_len = q->vnet_hdr_sz;
  691. if ((len -= vnet_hdr_len) < 0)
  692. return -EINVAL;
  693. ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
  694. if (ret)
  695. return ret;
  696. if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr)))
  697. return -EFAULT;
  698. }
  699. copied = vnet_hdr_len;
  700. if (!vlan_tx_tag_present(skb))
  701. len = min_t(int, skb->len, len);
  702. else {
  703. int copy;
  704. struct {
  705. __be16 h_vlan_proto;
  706. __be16 h_vlan_TCI;
  707. } veth;
  708. veth.h_vlan_proto = htons(ETH_P_8021Q);
  709. veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
  710. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  711. len = min_t(int, skb->len + VLAN_HLEN, len);
  712. copy = min_t(int, vlan_offset, len);
  713. ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
  714. len -= copy;
  715. copied += copy;
  716. if (ret || !len)
  717. goto done;
  718. copy = min_t(int, sizeof(veth), len);
  719. ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
  720. len -= copy;
  721. copied += copy;
  722. if (ret || !len)
  723. goto done;
  724. }
  725. ret = skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len);
  726. copied += len;
  727. done:
  728. rcu_read_lock();
  729. vlan = rcu_dereference(q->vlan);
  730. if (vlan)
  731. macvlan_count_rx(vlan, copied - vnet_hdr_len, ret == 0, 0);
  732. rcu_read_unlock();
  733. return ret ? ret : copied;
  734. }
  735. static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
  736. const struct iovec *iv, unsigned long len,
  737. int noblock)
  738. {
  739. DEFINE_WAIT(wait);
  740. struct sk_buff *skb;
  741. ssize_t ret = 0;
  742. while (len) {
  743. if (!noblock)
  744. prepare_to_wait(sk_sleep(&q->sk), &wait,
  745. TASK_INTERRUPTIBLE);
  746. /* Read frames from the queue */
  747. skb = skb_dequeue(&q->sk.sk_receive_queue);
  748. if (!skb) {
  749. if (noblock) {
  750. ret = -EAGAIN;
  751. break;
  752. }
  753. if (signal_pending(current)) {
  754. ret = -ERESTARTSYS;
  755. break;
  756. }
  757. /* Nothing to read, let's sleep */
  758. schedule();
  759. continue;
  760. }
  761. ret = macvtap_put_user(q, skb, iv, len);
  762. kfree_skb(skb);
  763. break;
  764. }
  765. if (!noblock)
  766. finish_wait(sk_sleep(&q->sk), &wait);
  767. return ret;
  768. }
  769. static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
  770. unsigned long count, loff_t pos)
  771. {
  772. struct file *file = iocb->ki_filp;
  773. struct macvtap_queue *q = file->private_data;
  774. ssize_t len, ret = 0;
  775. len = iov_length(iv, count);
  776. if (len < 0) {
  777. ret = -EINVAL;
  778. goto out;
  779. }
  780. ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
  781. ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
  782. out:
  783. return ret;
  784. }
  785. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  786. {
  787. struct macvlan_dev *vlan;
  788. ASSERT_RTNL();
  789. vlan = rtnl_dereference(q->vlan);
  790. if (vlan)
  791. dev_hold(vlan->dev);
  792. return vlan;
  793. }
  794. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  795. {
  796. dev_put(vlan->dev);
  797. }
  798. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  799. {
  800. struct macvtap_queue *q = file->private_data;
  801. struct macvlan_dev *vlan;
  802. int ret;
  803. vlan = macvtap_get_vlan(q);
  804. if (!vlan)
  805. return -EINVAL;
  806. if (flags & IFF_ATTACH_QUEUE)
  807. ret = macvtap_enable_queue(vlan->dev, file, q);
  808. else if (flags & IFF_DETACH_QUEUE)
  809. ret = macvtap_disable_queue(q);
  810. else
  811. ret = -EINVAL;
  812. macvtap_put_vlan(vlan);
  813. return ret;
  814. }
  815. /*
  816. * provide compatibility with generic tun/tap interface
  817. */
  818. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  819. unsigned long arg)
  820. {
  821. struct macvtap_queue *q = file->private_data;
  822. struct macvlan_dev *vlan;
  823. void __user *argp = (void __user *)arg;
  824. struct ifreq __user *ifr = argp;
  825. unsigned int __user *up = argp;
  826. unsigned int u;
  827. int __user *sp = argp;
  828. int s;
  829. int ret;
  830. switch (cmd) {
  831. case TUNSETIFF:
  832. /* ignore the name, just look at flags */
  833. if (get_user(u, &ifr->ifr_flags))
  834. return -EFAULT;
  835. ret = 0;
  836. if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
  837. (IFF_NO_PI | IFF_TAP))
  838. ret = -EINVAL;
  839. else
  840. q->flags = u;
  841. return ret;
  842. case TUNGETIFF:
  843. rtnl_lock();
  844. vlan = macvtap_get_vlan(q);
  845. if (!vlan) {
  846. rtnl_unlock();
  847. return -ENOLINK;
  848. }
  849. ret = 0;
  850. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  851. put_user(q->flags, &ifr->ifr_flags))
  852. ret = -EFAULT;
  853. macvtap_put_vlan(vlan);
  854. rtnl_unlock();
  855. return ret;
  856. case TUNSETQUEUE:
  857. if (get_user(u, &ifr->ifr_flags))
  858. return -EFAULT;
  859. rtnl_lock();
  860. ret = macvtap_ioctl_set_queue(file, u);
  861. rtnl_unlock();
  862. case TUNGETFEATURES:
  863. if (put_user(IFF_TAP | IFF_NO_PI | IFF_VNET_HDR |
  864. IFF_MULTI_QUEUE, up))
  865. return -EFAULT;
  866. return 0;
  867. case TUNSETSNDBUF:
  868. if (get_user(u, up))
  869. return -EFAULT;
  870. q->sk.sk_sndbuf = u;
  871. return 0;
  872. case TUNGETVNETHDRSZ:
  873. s = q->vnet_hdr_sz;
  874. if (put_user(s, sp))
  875. return -EFAULT;
  876. return 0;
  877. case TUNSETVNETHDRSZ:
  878. if (get_user(s, sp))
  879. return -EFAULT;
  880. if (s < (int)sizeof(struct virtio_net_hdr))
  881. return -EINVAL;
  882. q->vnet_hdr_sz = s;
  883. return 0;
  884. case TUNSETOFFLOAD:
  885. /* let the user check for future flags */
  886. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  887. TUN_F_TSO_ECN | TUN_F_UFO))
  888. return -EINVAL;
  889. /* TODO: only accept frames with the features that
  890. got enabled for forwarded frames */
  891. if (!(q->flags & IFF_VNET_HDR))
  892. return -EINVAL;
  893. return 0;
  894. default:
  895. return -EINVAL;
  896. }
  897. }
  898. #ifdef CONFIG_COMPAT
  899. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  900. unsigned long arg)
  901. {
  902. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  903. }
  904. #endif
  905. static const struct file_operations macvtap_fops = {
  906. .owner = THIS_MODULE,
  907. .open = macvtap_open,
  908. .release = macvtap_release,
  909. .aio_read = macvtap_aio_read,
  910. .aio_write = macvtap_aio_write,
  911. .poll = macvtap_poll,
  912. .llseek = no_llseek,
  913. .unlocked_ioctl = macvtap_ioctl,
  914. #ifdef CONFIG_COMPAT
  915. .compat_ioctl = macvtap_compat_ioctl,
  916. #endif
  917. };
  918. static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
  919. struct msghdr *m, size_t total_len)
  920. {
  921. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  922. return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
  923. m->msg_flags & MSG_DONTWAIT);
  924. }
  925. static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
  926. struct msghdr *m, size_t total_len,
  927. int flags)
  928. {
  929. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  930. int ret;
  931. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  932. return -EINVAL;
  933. ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
  934. flags & MSG_DONTWAIT);
  935. if (ret > total_len) {
  936. m->msg_flags |= MSG_TRUNC;
  937. ret = flags & MSG_TRUNC ? ret : total_len;
  938. }
  939. return ret;
  940. }
  941. /* Ops structure to mimic raw sockets with tun */
  942. static const struct proto_ops macvtap_socket_ops = {
  943. .sendmsg = macvtap_sendmsg,
  944. .recvmsg = macvtap_recvmsg,
  945. };
  946. /* Get an underlying socket object from tun file. Returns error unless file is
  947. * attached to a device. The returned object works like a packet socket, it
  948. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  949. * holding a reference to the file for as long as the socket is in use. */
  950. struct socket *macvtap_get_socket(struct file *file)
  951. {
  952. struct macvtap_queue *q;
  953. if (file->f_op != &macvtap_fops)
  954. return ERR_PTR(-EINVAL);
  955. q = file->private_data;
  956. if (!q)
  957. return ERR_PTR(-EBADFD);
  958. return &q->sock;
  959. }
  960. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  961. static int macvtap_device_event(struct notifier_block *unused,
  962. unsigned long event, void *ptr)
  963. {
  964. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  965. struct macvlan_dev *vlan;
  966. struct device *classdev;
  967. dev_t devt;
  968. int err;
  969. if (dev->rtnl_link_ops != &macvtap_link_ops)
  970. return NOTIFY_DONE;
  971. vlan = netdev_priv(dev);
  972. switch (event) {
  973. case NETDEV_REGISTER:
  974. /* Create the device node here after the network device has
  975. * been registered but before register_netdevice has
  976. * finished running.
  977. */
  978. err = macvtap_get_minor(vlan);
  979. if (err)
  980. return notifier_from_errno(err);
  981. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  982. classdev = device_create(macvtap_class, &dev->dev, devt,
  983. dev, "tap%d", dev->ifindex);
  984. if (IS_ERR(classdev)) {
  985. macvtap_free_minor(vlan);
  986. return notifier_from_errno(PTR_ERR(classdev));
  987. }
  988. break;
  989. case NETDEV_UNREGISTER:
  990. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  991. device_destroy(macvtap_class, devt);
  992. macvtap_free_minor(vlan);
  993. break;
  994. }
  995. return NOTIFY_DONE;
  996. }
  997. static struct notifier_block macvtap_notifier_block __read_mostly = {
  998. .notifier_call = macvtap_device_event,
  999. };
  1000. static int macvtap_init(void)
  1001. {
  1002. int err;
  1003. err = alloc_chrdev_region(&macvtap_major, 0,
  1004. MACVTAP_NUM_DEVS, "macvtap");
  1005. if (err)
  1006. goto out1;
  1007. cdev_init(&macvtap_cdev, &macvtap_fops);
  1008. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1009. if (err)
  1010. goto out2;
  1011. macvtap_class = class_create(THIS_MODULE, "macvtap");
  1012. if (IS_ERR(macvtap_class)) {
  1013. err = PTR_ERR(macvtap_class);
  1014. goto out3;
  1015. }
  1016. err = register_netdevice_notifier(&macvtap_notifier_block);
  1017. if (err)
  1018. goto out4;
  1019. err = macvlan_link_register(&macvtap_link_ops);
  1020. if (err)
  1021. goto out5;
  1022. return 0;
  1023. out5:
  1024. unregister_netdevice_notifier(&macvtap_notifier_block);
  1025. out4:
  1026. class_unregister(macvtap_class);
  1027. out3:
  1028. cdev_del(&macvtap_cdev);
  1029. out2:
  1030. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1031. out1:
  1032. return err;
  1033. }
  1034. module_init(macvtap_init);
  1035. static void macvtap_exit(void)
  1036. {
  1037. rtnl_link_unregister(&macvtap_link_ops);
  1038. unregister_netdevice_notifier(&macvtap_notifier_block);
  1039. class_unregister(macvtap_class);
  1040. cdev_del(&macvtap_cdev);
  1041. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1042. }
  1043. module_exit(macvtap_exit);
  1044. MODULE_ALIAS_RTNL_LINK("macvtap");
  1045. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1046. MODULE_LICENSE("GPL");