macvtap.c 29 KB

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